megaraid_sas_fusion.c 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2009-2013 LSI Corporation
  5. * Copyright (c) 2013-2014 Avago Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * FILE: megaraid_sas_fusion.c
  21. *
  22. * Authors: Avago Technologies
  23. * Sumant Patro
  24. * Adam Radford
  25. * Kashyap Desai <kashyap.desai@avagotech.com>
  26. * Sumit Saxena <sumit.saxena@avagotech.com>
  27. *
  28. * Send feedback to: megaraidlinux.pdl@avagotech.com
  29. *
  30. * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
  31. * San Jose, California 95131
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/types.h>
  35. #include <linux/pci.h>
  36. #include <linux/list.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/module.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/delay.h>
  42. #include <linux/uio.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/fs.h>
  45. #include <linux/compat.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/mutex.h>
  48. #include <linux/poll.h>
  49. #include <linux/vmalloc.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_cmnd.h>
  52. #include <scsi/scsi_device.h>
  53. #include <scsi/scsi_host.h>
  54. #include <scsi/scsi_dbg.h>
  55. #include <linux/dmi.h>
  56. #include "megaraid_sas_fusion.h"
  57. #include "megaraid_sas.h"
  58. extern void megasas_free_cmds(struct megasas_instance *instance);
  59. extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  60. *instance);
  61. extern void
  62. megasas_complete_cmd(struct megasas_instance *instance,
  63. struct megasas_cmd *cmd, u8 alt_status);
  64. int
  65. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  66. int seconds);
  67. void
  68. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
  69. int megasas_alloc_cmds(struct megasas_instance *instance);
  70. int
  71. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
  72. int
  73. megasas_issue_polled(struct megasas_instance *instance,
  74. struct megasas_cmd *cmd);
  75. void
  76. megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
  77. int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  78. void megaraid_sas_kill_hba(struct megasas_instance *instance);
  79. extern u32 megasas_dbg_lvl;
  80. int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
  81. int initial);
  82. void megasas_start_timer(struct megasas_instance *instance);
  83. extern struct megasas_mgmt_info megasas_mgmt_info;
  84. extern unsigned int resetwaittime;
  85. extern unsigned int dual_qdepth_disable;
  86. static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
  87. static void megasas_free_reply_fusion(struct megasas_instance *instance);
  88. static inline
  89. void megasas_configure_queue_sizes(struct megasas_instance *instance);
  90. /**
  91. * megasas_check_same_4gb_region - check if allocation
  92. * crosses same 4GB boundary or not
  93. * @instance - adapter's soft instance
  94. * start_addr - start address of DMA allocation
  95. * size - size of allocation in bytes
  96. * return - true : allocation does not cross same
  97. * 4GB boundary
  98. * false: allocation crosses same
  99. * 4GB boundary
  100. */
  101. static inline bool megasas_check_same_4gb_region
  102. (struct megasas_instance *instance, dma_addr_t start_addr, size_t size)
  103. {
  104. dma_addr_t end_addr;
  105. end_addr = start_addr + size;
  106. if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) {
  107. dev_err(&instance->pdev->dev,
  108. "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n",
  109. (unsigned long long)start_addr,
  110. (unsigned long long)end_addr);
  111. return false;
  112. }
  113. return true;
  114. }
  115. /**
  116. * megasas_enable_intr_fusion - Enables interrupts
  117. * @regs: MFI register set
  118. */
  119. void
  120. megasas_enable_intr_fusion(struct megasas_instance *instance)
  121. {
  122. struct megasas_register_set __iomem *regs;
  123. regs = instance->reg_set;
  124. instance->mask_interrupts = 0;
  125. /* For Thunderbolt/Invader also clear intr on enable */
  126. writel(~0, &regs->outbound_intr_status);
  127. readl(&regs->outbound_intr_status);
  128. writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  129. /* Dummy readl to force pci flush */
  130. readl(&regs->outbound_intr_mask);
  131. }
  132. /**
  133. * megasas_disable_intr_fusion - Disables interrupt
  134. * @regs: MFI register set
  135. */
  136. void
  137. megasas_disable_intr_fusion(struct megasas_instance *instance)
  138. {
  139. u32 mask = 0xFFFFFFFF;
  140. u32 status;
  141. struct megasas_register_set __iomem *regs;
  142. regs = instance->reg_set;
  143. instance->mask_interrupts = 1;
  144. writel(mask, &regs->outbound_intr_mask);
  145. /* Dummy readl to force pci flush */
  146. status = readl(&regs->outbound_intr_mask);
  147. }
  148. int
  149. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
  150. {
  151. u32 status;
  152. /*
  153. * Check if it is our interrupt
  154. */
  155. status = readl(&regs->outbound_intr_status);
  156. if (status & 1) {
  157. writel(status, &regs->outbound_intr_status);
  158. readl(&regs->outbound_intr_status);
  159. return 1;
  160. }
  161. if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
  162. return 0;
  163. return 1;
  164. }
  165. /**
  166. * megasas_get_cmd_fusion - Get a command from the free pool
  167. * @instance: Adapter soft state
  168. *
  169. * Returns a blk_tag indexed mpt frame
  170. */
  171. inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
  172. *instance, u32 blk_tag)
  173. {
  174. struct fusion_context *fusion;
  175. fusion = instance->ctrl_context;
  176. return fusion->cmd_list[blk_tag];
  177. }
  178. /**
  179. * megasas_return_cmd_fusion - Return a cmd to free command pool
  180. * @instance: Adapter soft state
  181. * @cmd: Command packet to be returned to free command pool
  182. */
  183. inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
  184. struct megasas_cmd_fusion *cmd)
  185. {
  186. cmd->scmd = NULL;
  187. memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
  188. cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
  189. cmd->cmd_completed = false;
  190. }
  191. /**
  192. * megasas_fire_cmd_fusion - Sends command to the FW
  193. * @instance: Adapter soft state
  194. * @req_desc: 64bit Request descriptor
  195. *
  196. * Perform PCI Write.
  197. */
  198. static void
  199. megasas_fire_cmd_fusion(struct megasas_instance *instance,
  200. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
  201. {
  202. #if defined(writeq) && defined(CONFIG_64BIT)
  203. u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
  204. le32_to_cpu(req_desc->u.low));
  205. writeq(req_data, &instance->reg_set->inbound_low_queue_port);
  206. #else
  207. unsigned long flags;
  208. spin_lock_irqsave(&instance->hba_lock, flags);
  209. writel(le32_to_cpu(req_desc->u.low),
  210. &instance->reg_set->inbound_low_queue_port);
  211. writel(le32_to_cpu(req_desc->u.high),
  212. &instance->reg_set->inbound_high_queue_port);
  213. mmiowb();
  214. spin_unlock_irqrestore(&instance->hba_lock, flags);
  215. #endif
  216. }
  217. /**
  218. * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
  219. * @instance: Adapter soft state
  220. * fw_boot_context: Whether this function called during probe or after OCR
  221. *
  222. * This function is only for fusion controllers.
  223. * Update host can queue, if firmware downgrade max supported firmware commands.
  224. * Firmware upgrade case will be skiped because underlying firmware has
  225. * more resource than exposed to the OS.
  226. *
  227. */
  228. static void
  229. megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
  230. {
  231. u16 cur_max_fw_cmds = 0;
  232. u16 ldio_threshold = 0;
  233. struct megasas_register_set __iomem *reg_set;
  234. reg_set = instance->reg_set;
  235. /* ventura FW does not fill outbound_scratch_pad_3 with queue depth */
  236. if (instance->adapter_type < VENTURA_SERIES)
  237. cur_max_fw_cmds =
  238. readl(&instance->reg_set->outbound_scratch_pad_3) & 0x00FFFF;
  239. if (dual_qdepth_disable || !cur_max_fw_cmds)
  240. cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  241. else
  242. ldio_threshold =
  243. (instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
  244. dev_info(&instance->pdev->dev,
  245. "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
  246. cur_max_fw_cmds, ldio_threshold);
  247. if (fw_boot_context == OCR_CONTEXT) {
  248. cur_max_fw_cmds = cur_max_fw_cmds - 1;
  249. if (cur_max_fw_cmds < instance->max_fw_cmds) {
  250. instance->cur_can_queue =
  251. cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
  252. MEGASAS_FUSION_IOCTL_CMDS);
  253. instance->host->can_queue = instance->cur_can_queue;
  254. instance->ldio_threshold = ldio_threshold;
  255. }
  256. } else {
  257. instance->max_fw_cmds = cur_max_fw_cmds;
  258. instance->ldio_threshold = ldio_threshold;
  259. if (reset_devices)
  260. instance->max_fw_cmds = min(instance->max_fw_cmds,
  261. (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
  262. /*
  263. * Reduce the max supported cmds by 1. This is to ensure that the
  264. * reply_q_sz (1 more than the max cmd that driver may send)
  265. * does not exceed max cmds that the FW can support
  266. */
  267. instance->max_fw_cmds = instance->max_fw_cmds-1;
  268. }
  269. }
  270. /**
  271. * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
  272. * @instance: Adapter soft state
  273. */
  274. void
  275. megasas_free_cmds_fusion(struct megasas_instance *instance)
  276. {
  277. int i;
  278. struct fusion_context *fusion = instance->ctrl_context;
  279. struct megasas_cmd_fusion *cmd;
  280. if (fusion->sense)
  281. dma_pool_free(fusion->sense_dma_pool, fusion->sense,
  282. fusion->sense_phys_addr);
  283. /* SG */
  284. if (fusion->cmd_list) {
  285. for (i = 0; i < instance->max_mpt_cmds; i++) {
  286. cmd = fusion->cmd_list[i];
  287. if (cmd) {
  288. if (cmd->sg_frame)
  289. dma_pool_free(fusion->sg_dma_pool,
  290. cmd->sg_frame,
  291. cmd->sg_frame_phys_addr);
  292. }
  293. kfree(cmd);
  294. }
  295. kfree(fusion->cmd_list);
  296. }
  297. if (fusion->sg_dma_pool) {
  298. dma_pool_destroy(fusion->sg_dma_pool);
  299. fusion->sg_dma_pool = NULL;
  300. }
  301. if (fusion->sense_dma_pool) {
  302. dma_pool_destroy(fusion->sense_dma_pool);
  303. fusion->sense_dma_pool = NULL;
  304. }
  305. /* Reply Frame, Desc*/
  306. if (instance->is_rdpq)
  307. megasas_free_rdpq_fusion(instance);
  308. else
  309. megasas_free_reply_fusion(instance);
  310. /* Request Frame, Desc*/
  311. if (fusion->req_frames_desc)
  312. dma_free_coherent(&instance->pdev->dev,
  313. fusion->request_alloc_sz, fusion->req_frames_desc,
  314. fusion->req_frames_desc_phys);
  315. if (fusion->io_request_frames)
  316. dma_pool_free(fusion->io_request_frames_pool,
  317. fusion->io_request_frames,
  318. fusion->io_request_frames_phys);
  319. if (fusion->io_request_frames_pool) {
  320. dma_pool_destroy(fusion->io_request_frames_pool);
  321. fusion->io_request_frames_pool = NULL;
  322. }
  323. }
  324. /**
  325. * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
  326. * @instance: Adapter soft state
  327. *
  328. */
  329. static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
  330. {
  331. int i;
  332. u16 max_cmd;
  333. struct fusion_context *fusion;
  334. struct megasas_cmd_fusion *cmd;
  335. int sense_sz;
  336. u32 offset;
  337. fusion = instance->ctrl_context;
  338. max_cmd = instance->max_fw_cmds;
  339. sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE;
  340. fusion->sg_dma_pool =
  341. dma_pool_create("mr_sg", &instance->pdev->dev,
  342. instance->max_chain_frame_sz,
  343. MR_DEFAULT_NVME_PAGE_SIZE, 0);
  344. /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
  345. fusion->sense_dma_pool =
  346. dma_pool_create("mr_sense", &instance->pdev->dev,
  347. sense_sz, 64, 0);
  348. if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
  349. dev_err(&instance->pdev->dev,
  350. "Failed from %s %d\n", __func__, __LINE__);
  351. return -ENOMEM;
  352. }
  353. fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
  354. GFP_KERNEL, &fusion->sense_phys_addr);
  355. if (!fusion->sense) {
  356. dev_err(&instance->pdev->dev,
  357. "failed from %s %d\n", __func__, __LINE__);
  358. return -ENOMEM;
  359. }
  360. /* sense buffer, request frame and reply desc pool requires to be in
  361. * same 4 gb region. Below function will check this.
  362. * In case of failure, new pci pool will be created with updated
  363. * alignment.
  364. * Older allocation and pool will be destroyed.
  365. * Alignment will be used such a way that next allocation if success,
  366. * will always meet same 4gb region requirement.
  367. * Actual requirement is not alignment, but we need start and end of
  368. * DMA address must have same upper 32 bit address.
  369. */
  370. if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr,
  371. sense_sz)) {
  372. dma_pool_free(fusion->sense_dma_pool, fusion->sense,
  373. fusion->sense_phys_addr);
  374. fusion->sense = NULL;
  375. dma_pool_destroy(fusion->sense_dma_pool);
  376. fusion->sense_dma_pool =
  377. dma_pool_create("mr_sense_align", &instance->pdev->dev,
  378. sense_sz, roundup_pow_of_two(sense_sz),
  379. 0);
  380. if (!fusion->sense_dma_pool) {
  381. dev_err(&instance->pdev->dev,
  382. "Failed from %s %d\n", __func__, __LINE__);
  383. return -ENOMEM;
  384. }
  385. fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
  386. GFP_KERNEL,
  387. &fusion->sense_phys_addr);
  388. if (!fusion->sense) {
  389. dev_err(&instance->pdev->dev,
  390. "failed from %s %d\n", __func__, __LINE__);
  391. return -ENOMEM;
  392. }
  393. }
  394. /*
  395. * Allocate and attach a frame to each of the commands in cmd_list
  396. */
  397. for (i = 0; i < max_cmd; i++) {
  398. cmd = fusion->cmd_list[i];
  399. cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool,
  400. GFP_KERNEL, &cmd->sg_frame_phys_addr);
  401. offset = SCSI_SENSE_BUFFERSIZE * i;
  402. cmd->sense = (u8 *)fusion->sense + offset;
  403. cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
  404. if (!cmd->sg_frame) {
  405. dev_err(&instance->pdev->dev,
  406. "Failed from %s %d\n", __func__, __LINE__);
  407. return -ENOMEM;
  408. }
  409. }
  410. /* create sense buffer for the raid 1/10 fp */
  411. for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
  412. cmd = fusion->cmd_list[i];
  413. offset = SCSI_SENSE_BUFFERSIZE * i;
  414. cmd->sense = (u8 *)fusion->sense + offset;
  415. cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
  416. }
  417. return 0;
  418. }
  419. int
  420. megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
  421. {
  422. u32 max_mpt_cmd, i, j;
  423. struct fusion_context *fusion;
  424. fusion = instance->ctrl_context;
  425. max_mpt_cmd = instance->max_mpt_cmds;
  426. /*
  427. * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
  428. * Allocate the dynamic array first and then allocate individual
  429. * commands.
  430. */
  431. fusion->cmd_list =
  432. kzalloc(sizeof(struct megasas_cmd_fusion *) * max_mpt_cmd,
  433. GFP_KERNEL);
  434. if (!fusion->cmd_list) {
  435. dev_err(&instance->pdev->dev,
  436. "Failed from %s %d\n", __func__, __LINE__);
  437. return -ENOMEM;
  438. }
  439. for (i = 0; i < max_mpt_cmd; i++) {
  440. fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
  441. GFP_KERNEL);
  442. if (!fusion->cmd_list[i]) {
  443. for (j = 0; j < i; j++)
  444. kfree(fusion->cmd_list[j]);
  445. kfree(fusion->cmd_list);
  446. dev_err(&instance->pdev->dev,
  447. "Failed from %s %d\n", __func__, __LINE__);
  448. return -ENOMEM;
  449. }
  450. }
  451. return 0;
  452. }
  453. int
  454. megasas_alloc_request_fusion(struct megasas_instance *instance)
  455. {
  456. struct fusion_context *fusion;
  457. fusion = instance->ctrl_context;
  458. retry_alloc:
  459. fusion->io_request_frames_pool =
  460. dma_pool_create("mr_ioreq", &instance->pdev->dev,
  461. fusion->io_frames_alloc_sz, 16, 0);
  462. if (!fusion->io_request_frames_pool) {
  463. dev_err(&instance->pdev->dev,
  464. "Failed from %s %d\n", __func__, __LINE__);
  465. return -ENOMEM;
  466. }
  467. fusion->io_request_frames =
  468. dma_pool_alloc(fusion->io_request_frames_pool,
  469. GFP_KERNEL, &fusion->io_request_frames_phys);
  470. if (!fusion->io_request_frames) {
  471. if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) {
  472. instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT;
  473. dma_pool_destroy(fusion->io_request_frames_pool);
  474. megasas_configure_queue_sizes(instance);
  475. goto retry_alloc;
  476. } else {
  477. dev_err(&instance->pdev->dev,
  478. "Failed from %s %d\n", __func__, __LINE__);
  479. return -ENOMEM;
  480. }
  481. }
  482. if (!megasas_check_same_4gb_region(instance,
  483. fusion->io_request_frames_phys,
  484. fusion->io_frames_alloc_sz)) {
  485. dma_pool_free(fusion->io_request_frames_pool,
  486. fusion->io_request_frames,
  487. fusion->io_request_frames_phys);
  488. fusion->io_request_frames = NULL;
  489. dma_pool_destroy(fusion->io_request_frames_pool);
  490. fusion->io_request_frames_pool =
  491. dma_pool_create("mr_ioreq_align",
  492. &instance->pdev->dev,
  493. fusion->io_frames_alloc_sz,
  494. roundup_pow_of_two(fusion->io_frames_alloc_sz),
  495. 0);
  496. if (!fusion->io_request_frames_pool) {
  497. dev_err(&instance->pdev->dev,
  498. "Failed from %s %d\n", __func__, __LINE__);
  499. return -ENOMEM;
  500. }
  501. fusion->io_request_frames =
  502. dma_pool_alloc(fusion->io_request_frames_pool,
  503. GFP_KERNEL,
  504. &fusion->io_request_frames_phys);
  505. if (!fusion->io_request_frames) {
  506. dev_err(&instance->pdev->dev,
  507. "Failed from %s %d\n", __func__, __LINE__);
  508. return -ENOMEM;
  509. }
  510. }
  511. fusion->req_frames_desc =
  512. dma_alloc_coherent(&instance->pdev->dev,
  513. fusion->request_alloc_sz,
  514. &fusion->req_frames_desc_phys, GFP_KERNEL);
  515. if (!fusion->req_frames_desc) {
  516. dev_err(&instance->pdev->dev,
  517. "Failed from %s %d\n", __func__, __LINE__);
  518. return -ENOMEM;
  519. }
  520. return 0;
  521. }
  522. int
  523. megasas_alloc_reply_fusion(struct megasas_instance *instance)
  524. {
  525. int i, count;
  526. struct fusion_context *fusion;
  527. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  528. fusion = instance->ctrl_context;
  529. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  530. fusion->reply_frames_desc_pool =
  531. dma_pool_create("mr_reply", &instance->pdev->dev,
  532. fusion->reply_alloc_sz * count, 16, 0);
  533. if (!fusion->reply_frames_desc_pool) {
  534. dev_err(&instance->pdev->dev,
  535. "Failed from %s %d\n", __func__, __LINE__);
  536. return -ENOMEM;
  537. }
  538. fusion->reply_frames_desc[0] =
  539. dma_pool_alloc(fusion->reply_frames_desc_pool,
  540. GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
  541. if (!fusion->reply_frames_desc[0]) {
  542. dev_err(&instance->pdev->dev,
  543. "Failed from %s %d\n", __func__, __LINE__);
  544. return -ENOMEM;
  545. }
  546. if (!megasas_check_same_4gb_region(instance,
  547. fusion->reply_frames_desc_phys[0],
  548. (fusion->reply_alloc_sz * count))) {
  549. dma_pool_free(fusion->reply_frames_desc_pool,
  550. fusion->reply_frames_desc[0],
  551. fusion->reply_frames_desc_phys[0]);
  552. fusion->reply_frames_desc[0] = NULL;
  553. dma_pool_destroy(fusion->reply_frames_desc_pool);
  554. fusion->reply_frames_desc_pool =
  555. dma_pool_create("mr_reply_align",
  556. &instance->pdev->dev,
  557. fusion->reply_alloc_sz * count,
  558. roundup_pow_of_two(fusion->reply_alloc_sz * count),
  559. 0);
  560. if (!fusion->reply_frames_desc_pool) {
  561. dev_err(&instance->pdev->dev,
  562. "Failed from %s %d\n", __func__, __LINE__);
  563. return -ENOMEM;
  564. }
  565. fusion->reply_frames_desc[0] =
  566. dma_pool_alloc(fusion->reply_frames_desc_pool,
  567. GFP_KERNEL,
  568. &fusion->reply_frames_desc_phys[0]);
  569. if (!fusion->reply_frames_desc[0]) {
  570. dev_err(&instance->pdev->dev,
  571. "Failed from %s %d\n", __func__, __LINE__);
  572. return -ENOMEM;
  573. }
  574. }
  575. reply_desc = fusion->reply_frames_desc[0];
  576. for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
  577. reply_desc->Words = cpu_to_le64(ULLONG_MAX);
  578. /* This is not a rdpq mode, but driver still populate
  579. * reply_frame_desc array to use same msix index in ISR path.
  580. */
  581. for (i = 0; i < (count - 1); i++)
  582. fusion->reply_frames_desc[i + 1] =
  583. fusion->reply_frames_desc[i] +
  584. (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
  585. return 0;
  586. }
  587. int
  588. megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
  589. {
  590. int i, j, k, msix_count;
  591. struct fusion_context *fusion;
  592. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  593. union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT];
  594. dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT];
  595. u8 dma_alloc_count, abs_index;
  596. u32 chunk_size, array_size, offset;
  597. fusion = instance->ctrl_context;
  598. chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
  599. array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) *
  600. MAX_MSIX_QUEUES_FUSION;
  601. fusion->rdpq_virt = pci_zalloc_consistent(instance->pdev, array_size,
  602. &fusion->rdpq_phys);
  603. if (!fusion->rdpq_virt) {
  604. dev_err(&instance->pdev->dev,
  605. "Failed from %s %d\n", __func__, __LINE__);
  606. return -ENOMEM;
  607. }
  608. msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  609. fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq",
  610. &instance->pdev->dev,
  611. chunk_size, 16, 0);
  612. fusion->reply_frames_desc_pool_align =
  613. dma_pool_create("mr_rdpq_align",
  614. &instance->pdev->dev,
  615. chunk_size,
  616. roundup_pow_of_two(chunk_size),
  617. 0);
  618. if (!fusion->reply_frames_desc_pool ||
  619. !fusion->reply_frames_desc_pool_align) {
  620. dev_err(&instance->pdev->dev,
  621. "Failed from %s %d\n", __func__, __LINE__);
  622. return -ENOMEM;
  623. }
  624. /*
  625. * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
  626. * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
  627. * within 4GB boundary and also reply queues in a set must have same
  628. * upper 32-bits in their memory address. so here driver is allocating the
  629. * DMA'able memory for reply queues according. Driver uses limitation of
  630. * VENTURA_SERIES to manage INVADER_SERIES as well.
  631. */
  632. dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK);
  633. for (i = 0; i < dma_alloc_count; i++) {
  634. rdpq_chunk_virt[i] =
  635. dma_pool_alloc(fusion->reply_frames_desc_pool,
  636. GFP_KERNEL, &rdpq_chunk_phys[i]);
  637. if (!rdpq_chunk_virt[i]) {
  638. dev_err(&instance->pdev->dev,
  639. "Failed from %s %d\n", __func__, __LINE__);
  640. return -ENOMEM;
  641. }
  642. /* reply desc pool requires to be in same 4 gb region.
  643. * Below function will check this.
  644. * In case of failure, new pci pool will be created with updated
  645. * alignment.
  646. * For RDPQ buffers, driver always allocate two separate pci pool.
  647. * Alignment will be used such a way that next allocation if
  648. * success, will always meet same 4gb region requirement.
  649. * rdpq_tracker keep track of each buffer's physical,
  650. * virtual address and pci pool descriptor. It will help driver
  651. * while freeing the resources.
  652. *
  653. */
  654. if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i],
  655. chunk_size)) {
  656. dma_pool_free(fusion->reply_frames_desc_pool,
  657. rdpq_chunk_virt[i],
  658. rdpq_chunk_phys[i]);
  659. rdpq_chunk_virt[i] =
  660. dma_pool_alloc(fusion->reply_frames_desc_pool_align,
  661. GFP_KERNEL, &rdpq_chunk_phys[i]);
  662. if (!rdpq_chunk_virt[i]) {
  663. dev_err(&instance->pdev->dev,
  664. "Failed from %s %d\n",
  665. __func__, __LINE__);
  666. return -ENOMEM;
  667. }
  668. fusion->rdpq_tracker[i].dma_pool_ptr =
  669. fusion->reply_frames_desc_pool_align;
  670. } else {
  671. fusion->rdpq_tracker[i].dma_pool_ptr =
  672. fusion->reply_frames_desc_pool;
  673. }
  674. fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i];
  675. fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i];
  676. }
  677. for (k = 0; k < dma_alloc_count; k++) {
  678. for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) {
  679. abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i;
  680. if (abs_index == msix_count)
  681. break;
  682. offset = fusion->reply_alloc_sz * i;
  683. fusion->rdpq_virt[abs_index].RDPQBaseAddress =
  684. cpu_to_le64(rdpq_chunk_phys[k] + offset);
  685. fusion->reply_frames_desc_phys[abs_index] =
  686. rdpq_chunk_phys[k] + offset;
  687. fusion->reply_frames_desc[abs_index] =
  688. (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset);
  689. reply_desc = fusion->reply_frames_desc[abs_index];
  690. for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
  691. reply_desc->Words = ULLONG_MAX;
  692. }
  693. }
  694. return 0;
  695. }
  696. static void
  697. megasas_free_rdpq_fusion(struct megasas_instance *instance) {
  698. int i;
  699. struct fusion_context *fusion;
  700. fusion = instance->ctrl_context;
  701. for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) {
  702. if (fusion->rdpq_tracker[i].pool_entry_virt)
  703. dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr,
  704. fusion->rdpq_tracker[i].pool_entry_virt,
  705. fusion->rdpq_tracker[i].pool_entry_phys);
  706. }
  707. if (fusion->reply_frames_desc_pool)
  708. dma_pool_destroy(fusion->reply_frames_desc_pool);
  709. if (fusion->reply_frames_desc_pool_align)
  710. dma_pool_destroy(fusion->reply_frames_desc_pool_align);
  711. if (fusion->rdpq_virt)
  712. pci_free_consistent(instance->pdev,
  713. sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
  714. fusion->rdpq_virt, fusion->rdpq_phys);
  715. }
  716. static void
  717. megasas_free_reply_fusion(struct megasas_instance *instance) {
  718. struct fusion_context *fusion;
  719. fusion = instance->ctrl_context;
  720. if (fusion->reply_frames_desc[0])
  721. dma_pool_free(fusion->reply_frames_desc_pool,
  722. fusion->reply_frames_desc[0],
  723. fusion->reply_frames_desc_phys[0]);
  724. if (fusion->reply_frames_desc_pool)
  725. dma_pool_destroy(fusion->reply_frames_desc_pool);
  726. }
  727. /**
  728. * megasas_alloc_cmds_fusion - Allocates the command packets
  729. * @instance: Adapter soft state
  730. *
  731. *
  732. * Each frame has a 32-bit field called context. This context is used to get
  733. * back the megasas_cmd_fusion from the frame when a frame gets completed
  734. * In this driver, the 32 bit values are the indices into an array cmd_list.
  735. * This array is used only to look up the megasas_cmd_fusion given the context.
  736. * The free commands themselves are maintained in a linked list called cmd_pool.
  737. *
  738. * cmds are formed in the io_request and sg_frame members of the
  739. * megasas_cmd_fusion. The context field is used to get a request descriptor
  740. * and is used as SMID of the cmd.
  741. * SMID value range is from 1 to max_fw_cmds.
  742. */
  743. int
  744. megasas_alloc_cmds_fusion(struct megasas_instance *instance)
  745. {
  746. int i;
  747. struct fusion_context *fusion;
  748. struct megasas_cmd_fusion *cmd;
  749. u32 offset;
  750. dma_addr_t io_req_base_phys;
  751. u8 *io_req_base;
  752. fusion = instance->ctrl_context;
  753. if (megasas_alloc_request_fusion(instance))
  754. goto fail_exit;
  755. if (instance->is_rdpq) {
  756. if (megasas_alloc_rdpq_fusion(instance))
  757. goto fail_exit;
  758. } else
  759. if (megasas_alloc_reply_fusion(instance))
  760. goto fail_exit;
  761. if (megasas_alloc_cmdlist_fusion(instance))
  762. goto fail_exit;
  763. dev_info(&instance->pdev->dev, "Configured max firmware commands: %d\n",
  764. instance->max_fw_cmds);
  765. /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
  766. io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  767. io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  768. /*
  769. * Add all the commands to command pool (fusion->cmd_pool)
  770. */
  771. /* SMID 0 is reserved. Set SMID/index from 1 */
  772. for (i = 0; i < instance->max_mpt_cmds; i++) {
  773. cmd = fusion->cmd_list[i];
  774. offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
  775. memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
  776. cmd->index = i + 1;
  777. cmd->scmd = NULL;
  778. cmd->sync_cmd_idx =
  779. (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
  780. (i - instance->max_scsi_cmds) :
  781. (u32)ULONG_MAX; /* Set to Invalid */
  782. cmd->instance = instance;
  783. cmd->io_request =
  784. (struct MPI2_RAID_SCSI_IO_REQUEST *)
  785. (io_req_base + offset);
  786. memset(cmd->io_request, 0,
  787. sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
  788. cmd->io_request_phys_addr = io_req_base_phys + offset;
  789. cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
  790. }
  791. if (megasas_create_sg_sense_fusion(instance))
  792. goto fail_exit;
  793. return 0;
  794. fail_exit:
  795. megasas_free_cmds_fusion(instance);
  796. return -ENOMEM;
  797. }
  798. /**
  799. * wait_and_poll - Issues a polling command
  800. * @instance: Adapter soft state
  801. * @cmd: Command packet to be issued
  802. *
  803. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  804. */
  805. int
  806. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  807. int seconds)
  808. {
  809. int i;
  810. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  811. struct fusion_context *fusion;
  812. u32 msecs = seconds * 1000;
  813. fusion = instance->ctrl_context;
  814. /*
  815. * Wait for cmd_status to change
  816. */
  817. for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
  818. rmb();
  819. msleep(20);
  820. }
  821. if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
  822. return DCMD_TIMEOUT;
  823. else if (frame_hdr->cmd_status == MFI_STAT_OK)
  824. return DCMD_SUCCESS;
  825. else
  826. return DCMD_FAILED;
  827. }
  828. /**
  829. * megasas_ioc_init_fusion - Initializes the FW
  830. * @instance: Adapter soft state
  831. *
  832. * Issues the IOC Init cmd
  833. */
  834. int
  835. megasas_ioc_init_fusion(struct megasas_instance *instance)
  836. {
  837. struct megasas_init_frame *init_frame;
  838. struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
  839. dma_addr_t ioc_init_handle;
  840. struct megasas_cmd *cmd;
  841. u8 ret, cur_rdpq_mode;
  842. struct fusion_context *fusion;
  843. union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
  844. int i;
  845. struct megasas_header *frame_hdr;
  846. const char *sys_info;
  847. MFI_CAPABILITIES *drv_ops;
  848. u32 scratch_pad_2;
  849. ktime_t time;
  850. bool cur_fw_64bit_dma_capable;
  851. fusion = instance->ctrl_context;
  852. ioc_init_handle = fusion->ioc_init_request_phys;
  853. IOCInitMessage = fusion->ioc_init_request;
  854. cmd = fusion->ioc_init_cmd;
  855. scratch_pad_2 = readl
  856. (&instance->reg_set->outbound_scratch_pad_2);
  857. cur_rdpq_mode = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
  858. if (instance->adapter_type == INVADER_SERIES) {
  859. cur_fw_64bit_dma_capable =
  860. (scratch_pad_2 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
  861. if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
  862. dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
  863. "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
  864. megaraid_sas_kill_hba(instance);
  865. ret = 1;
  866. goto fail_fw_init;
  867. }
  868. }
  869. if (instance->is_rdpq && !cur_rdpq_mode) {
  870. dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
  871. " from RDPQ mode to non RDPQ mode\n");
  872. ret = 1;
  873. goto fail_fw_init;
  874. }
  875. instance->fw_sync_cache_support = (scratch_pad_2 &
  876. MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
  877. dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
  878. instance->fw_sync_cache_support ? "Yes" : "No");
  879. memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
  880. IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
  881. IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  882. IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
  883. IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  884. IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
  885. IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
  886. IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
  887. cpu_to_le64(fusion->rdpq_phys) :
  888. cpu_to_le64(fusion->reply_frames_desc_phys[0]);
  889. IOCInitMessage->MsgFlags = instance->is_rdpq ?
  890. MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
  891. IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
  892. IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
  893. IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
  894. IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
  895. time = ktime_get_real();
  896. /* Convert to milliseconds as per FW requirement */
  897. IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
  898. init_frame = (struct megasas_init_frame *)cmd->frame;
  899. memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
  900. frame_hdr = &cmd->frame->hdr;
  901. frame_hdr->cmd_status = 0xFF;
  902. frame_hdr->flags = cpu_to_le16(
  903. le16_to_cpu(frame_hdr->flags) |
  904. MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
  905. init_frame->cmd = MFI_CMD_INIT;
  906. init_frame->cmd_status = 0xFF;
  907. drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
  908. /* driver support Extended MSIX */
  909. if (instance->adapter_type >= INVADER_SERIES)
  910. drv_ops->mfi_capabilities.support_additional_msix = 1;
  911. /* driver supports HA / Remote LUN over Fast Path interface */
  912. drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
  913. drv_ops->mfi_capabilities.support_max_255lds = 1;
  914. drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
  915. drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
  916. if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
  917. drv_ops->mfi_capabilities.support_ext_io_size = 1;
  918. drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
  919. if (!dual_qdepth_disable)
  920. drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
  921. drv_ops->mfi_capabilities.support_qd_throttling = 1;
  922. drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
  923. drv_ops->mfi_capabilities.support_nvme_passthru = 1;
  924. if (instance->consistent_mask_64bit)
  925. drv_ops->mfi_capabilities.support_64bit_mode = 1;
  926. /* Convert capability to LE32 */
  927. cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
  928. sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
  929. if (instance->system_info_buf && sys_info) {
  930. memcpy(instance->system_info_buf->systemId, sys_info,
  931. strlen(sys_info) > 64 ? 64 : strlen(sys_info));
  932. instance->system_info_buf->systemIdLength =
  933. strlen(sys_info) > 64 ? 64 : strlen(sys_info);
  934. init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h));
  935. init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h));
  936. }
  937. init_frame->queue_info_new_phys_addr_hi =
  938. cpu_to_le32(upper_32_bits(ioc_init_handle));
  939. init_frame->queue_info_new_phys_addr_lo =
  940. cpu_to_le32(lower_32_bits(ioc_init_handle));
  941. init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
  942. req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
  943. req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
  944. req_desc.MFAIo.RequestFlags =
  945. (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
  946. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  947. /*
  948. * disable the intr before firing the init frame
  949. */
  950. instance->instancet->disable_intr(instance);
  951. for (i = 0; i < (10 * 1000); i += 20) {
  952. if (readl(&instance->reg_set->doorbell) & 1)
  953. msleep(20);
  954. else
  955. break;
  956. }
  957. megasas_fire_cmd_fusion(instance, &req_desc);
  958. wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
  959. frame_hdr = &cmd->frame->hdr;
  960. if (frame_hdr->cmd_status != 0) {
  961. ret = 1;
  962. goto fail_fw_init;
  963. }
  964. return 0;
  965. fail_fw_init:
  966. dev_err(&instance->pdev->dev,
  967. "Init cmd return status FAILED for SCSI host %d\n",
  968. instance->host->host_no);
  969. return ret;
  970. }
  971. /**
  972. * megasas_sync_pd_seq_num - JBOD SEQ MAP
  973. * @instance: Adapter soft state
  974. * @pend: set to 1, if it is pended jbod map.
  975. *
  976. * Issue Jbod map to the firmware. If it is pended command,
  977. * issue command and return. If it is first instance of jbod map
  978. * issue and receive command.
  979. */
  980. int
  981. megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
  982. int ret = 0;
  983. u32 pd_seq_map_sz;
  984. struct megasas_cmd *cmd;
  985. struct megasas_dcmd_frame *dcmd;
  986. struct fusion_context *fusion = instance->ctrl_context;
  987. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  988. dma_addr_t pd_seq_h;
  989. pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
  990. pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
  991. pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
  992. (sizeof(struct MR_PD_CFG_SEQ) *
  993. (MAX_PHYSICAL_DEVICES - 1));
  994. cmd = megasas_get_cmd(instance);
  995. if (!cmd) {
  996. dev_err(&instance->pdev->dev,
  997. "Could not get mfi cmd. Fail from %s %d\n",
  998. __func__, __LINE__);
  999. return -ENOMEM;
  1000. }
  1001. dcmd = &cmd->frame->dcmd;
  1002. memset(pd_sync, 0, pd_seq_map_sz);
  1003. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1004. if (pend) {
  1005. dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
  1006. dcmd->flags = MFI_FRAME_DIR_WRITE;
  1007. instance->jbod_seq_cmd = cmd;
  1008. } else {
  1009. dcmd->flags = MFI_FRAME_DIR_READ;
  1010. }
  1011. dcmd->cmd = MFI_CMD_DCMD;
  1012. dcmd->cmd_status = 0xFF;
  1013. dcmd->sge_count = 1;
  1014. dcmd->timeout = 0;
  1015. dcmd->pad_0 = 0;
  1016. dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
  1017. dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
  1018. megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz);
  1019. if (pend) {
  1020. instance->instancet->issue_dcmd(instance, cmd);
  1021. return 0;
  1022. }
  1023. /* Below code is only for non pended DCMD */
  1024. if (!instance->mask_interrupts)
  1025. ret = megasas_issue_blocked_cmd(instance, cmd,
  1026. MFI_IO_TIMEOUT_SECS);
  1027. else
  1028. ret = megasas_issue_polled(instance, cmd);
  1029. if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
  1030. dev_warn(&instance->pdev->dev,
  1031. "driver supports max %d JBOD, but FW reports %d\n",
  1032. MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
  1033. ret = -EINVAL;
  1034. }
  1035. if (ret == DCMD_TIMEOUT)
  1036. megaraid_sas_kill_hba(instance);
  1037. if (ret == DCMD_SUCCESS)
  1038. instance->pd_seq_map_id++;
  1039. megasas_return_cmd(instance, cmd);
  1040. return ret;
  1041. }
  1042. /*
  1043. * megasas_get_ld_map_info - Returns FW's ld_map structure
  1044. * @instance: Adapter soft state
  1045. * @pend: Pend the command or not
  1046. * Issues an internal command (DCMD) to get the FW's controller PD
  1047. * list structure. This information is mainly used to find out SYSTEM
  1048. * supported by the FW.
  1049. * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
  1050. * dcmd.mbox.b[0] - number of LDs being sync'd
  1051. * dcmd.mbox.b[1] - 0 - complete command immediately.
  1052. * - 1 - pend till config change
  1053. * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
  1054. * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
  1055. * uses extended struct MR_FW_RAID_MAP_EXT
  1056. */
  1057. static int
  1058. megasas_get_ld_map_info(struct megasas_instance *instance)
  1059. {
  1060. int ret = 0;
  1061. struct megasas_cmd *cmd;
  1062. struct megasas_dcmd_frame *dcmd;
  1063. void *ci;
  1064. dma_addr_t ci_h = 0;
  1065. u32 size_map_info;
  1066. struct fusion_context *fusion;
  1067. cmd = megasas_get_cmd(instance);
  1068. if (!cmd) {
  1069. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
  1070. return -ENOMEM;
  1071. }
  1072. fusion = instance->ctrl_context;
  1073. if (!fusion) {
  1074. megasas_return_cmd(instance, cmd);
  1075. return -ENXIO;
  1076. }
  1077. dcmd = &cmd->frame->dcmd;
  1078. size_map_info = fusion->current_map_sz;
  1079. ci = (void *) fusion->ld_map[(instance->map_id & 1)];
  1080. ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
  1081. if (!ci) {
  1082. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
  1083. megasas_return_cmd(instance, cmd);
  1084. return -ENOMEM;
  1085. }
  1086. memset(ci, 0, fusion->max_map_sz);
  1087. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1088. dcmd->cmd = MFI_CMD_DCMD;
  1089. dcmd->cmd_status = 0xFF;
  1090. dcmd->sge_count = 1;
  1091. dcmd->flags = MFI_FRAME_DIR_READ;
  1092. dcmd->timeout = 0;
  1093. dcmd->pad_0 = 0;
  1094. dcmd->data_xfer_len = cpu_to_le32(size_map_info);
  1095. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
  1096. megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
  1097. if (!instance->mask_interrupts)
  1098. ret = megasas_issue_blocked_cmd(instance, cmd,
  1099. MFI_IO_TIMEOUT_SECS);
  1100. else
  1101. ret = megasas_issue_polled(instance, cmd);
  1102. if (ret == DCMD_TIMEOUT)
  1103. megaraid_sas_kill_hba(instance);
  1104. megasas_return_cmd(instance, cmd);
  1105. return ret;
  1106. }
  1107. u8
  1108. megasas_get_map_info(struct megasas_instance *instance)
  1109. {
  1110. struct fusion_context *fusion = instance->ctrl_context;
  1111. fusion->fast_path_io = 0;
  1112. if (!megasas_get_ld_map_info(instance)) {
  1113. if (MR_ValidateMapInfo(instance, instance->map_id)) {
  1114. fusion->fast_path_io = 1;
  1115. return 0;
  1116. }
  1117. }
  1118. return 1;
  1119. }
  1120. /*
  1121. * megasas_sync_map_info - Returns FW's ld_map structure
  1122. * @instance: Adapter soft state
  1123. *
  1124. * Issues an internal command (DCMD) to get the FW's controller PD
  1125. * list structure. This information is mainly used to find out SYSTEM
  1126. * supported by the FW.
  1127. */
  1128. int
  1129. megasas_sync_map_info(struct megasas_instance *instance)
  1130. {
  1131. int i;
  1132. struct megasas_cmd *cmd;
  1133. struct megasas_dcmd_frame *dcmd;
  1134. u16 num_lds;
  1135. u32 size_sync_info;
  1136. struct fusion_context *fusion;
  1137. struct MR_LD_TARGET_SYNC *ci = NULL;
  1138. struct MR_DRV_RAID_MAP_ALL *map;
  1139. struct MR_LD_RAID *raid;
  1140. struct MR_LD_TARGET_SYNC *ld_sync;
  1141. dma_addr_t ci_h = 0;
  1142. u32 size_map_info;
  1143. cmd = megasas_get_cmd(instance);
  1144. if (!cmd) {
  1145. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
  1146. return -ENOMEM;
  1147. }
  1148. fusion = instance->ctrl_context;
  1149. if (!fusion) {
  1150. megasas_return_cmd(instance, cmd);
  1151. return 1;
  1152. }
  1153. map = fusion->ld_drv_map[instance->map_id & 1];
  1154. num_lds = le16_to_cpu(map->raidMap.ldCount);
  1155. dcmd = &cmd->frame->dcmd;
  1156. size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
  1157. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1158. ci = (struct MR_LD_TARGET_SYNC *)
  1159. fusion->ld_map[(instance->map_id - 1) & 1];
  1160. memset(ci, 0, fusion->max_map_sz);
  1161. ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
  1162. ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
  1163. for (i = 0; i < num_lds; i++, ld_sync++) {
  1164. raid = MR_LdRaidGet(i, map);
  1165. ld_sync->targetId = MR_GetLDTgtId(i, map);
  1166. ld_sync->seqNum = raid->seqNum;
  1167. }
  1168. size_map_info = fusion->current_map_sz;
  1169. dcmd->cmd = MFI_CMD_DCMD;
  1170. dcmd->cmd_status = 0xFF;
  1171. dcmd->sge_count = 1;
  1172. dcmd->flags = MFI_FRAME_DIR_WRITE;
  1173. dcmd->timeout = 0;
  1174. dcmd->pad_0 = 0;
  1175. dcmd->data_xfer_len = cpu_to_le32(size_map_info);
  1176. dcmd->mbox.b[0] = num_lds;
  1177. dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
  1178. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
  1179. megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
  1180. instance->map_update_cmd = cmd;
  1181. instance->instancet->issue_dcmd(instance, cmd);
  1182. return 0;
  1183. }
  1184. /*
  1185. * meagasas_display_intel_branding - Display branding string
  1186. * @instance: per adapter object
  1187. *
  1188. * Return nothing.
  1189. */
  1190. static void
  1191. megasas_display_intel_branding(struct megasas_instance *instance)
  1192. {
  1193. if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  1194. return;
  1195. switch (instance->pdev->device) {
  1196. case PCI_DEVICE_ID_LSI_INVADER:
  1197. switch (instance->pdev->subsystem_device) {
  1198. case MEGARAID_INTEL_RS3DC080_SSDID:
  1199. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1200. instance->host->host_no,
  1201. MEGARAID_INTEL_RS3DC080_BRANDING);
  1202. break;
  1203. case MEGARAID_INTEL_RS3DC040_SSDID:
  1204. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1205. instance->host->host_no,
  1206. MEGARAID_INTEL_RS3DC040_BRANDING);
  1207. break;
  1208. case MEGARAID_INTEL_RS3SC008_SSDID:
  1209. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1210. instance->host->host_no,
  1211. MEGARAID_INTEL_RS3SC008_BRANDING);
  1212. break;
  1213. case MEGARAID_INTEL_RS3MC044_SSDID:
  1214. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1215. instance->host->host_no,
  1216. MEGARAID_INTEL_RS3MC044_BRANDING);
  1217. break;
  1218. default:
  1219. break;
  1220. }
  1221. break;
  1222. case PCI_DEVICE_ID_LSI_FURY:
  1223. switch (instance->pdev->subsystem_device) {
  1224. case MEGARAID_INTEL_RS3WC080_SSDID:
  1225. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1226. instance->host->host_no,
  1227. MEGARAID_INTEL_RS3WC080_BRANDING);
  1228. break;
  1229. case MEGARAID_INTEL_RS3WC040_SSDID:
  1230. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1231. instance->host->host_no,
  1232. MEGARAID_INTEL_RS3WC040_BRANDING);
  1233. break;
  1234. default:
  1235. break;
  1236. }
  1237. break;
  1238. case PCI_DEVICE_ID_LSI_CUTLASS_52:
  1239. case PCI_DEVICE_ID_LSI_CUTLASS_53:
  1240. switch (instance->pdev->subsystem_device) {
  1241. case MEGARAID_INTEL_RMS3BC160_SSDID:
  1242. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1243. instance->host->host_no,
  1244. MEGARAID_INTEL_RMS3BC160_BRANDING);
  1245. break;
  1246. default:
  1247. break;
  1248. }
  1249. break;
  1250. default:
  1251. break;
  1252. }
  1253. }
  1254. /**
  1255. * megasas_allocate_raid_maps - Allocate memory for RAID maps
  1256. * @instance: Adapter soft state
  1257. *
  1258. * return: if success: return 0
  1259. * failed: return -ENOMEM
  1260. */
  1261. static inline int megasas_allocate_raid_maps(struct megasas_instance *instance)
  1262. {
  1263. struct fusion_context *fusion;
  1264. int i = 0;
  1265. fusion = instance->ctrl_context;
  1266. fusion->drv_map_pages = get_order(fusion->drv_map_sz);
  1267. for (i = 0; i < 2; i++) {
  1268. fusion->ld_map[i] = NULL;
  1269. fusion->ld_drv_map[i] = (void *)
  1270. __get_free_pages(__GFP_ZERO | GFP_KERNEL,
  1271. fusion->drv_map_pages);
  1272. if (!fusion->ld_drv_map[i]) {
  1273. fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz);
  1274. if (!fusion->ld_drv_map[i]) {
  1275. dev_err(&instance->pdev->dev,
  1276. "Could not allocate memory for local map"
  1277. " size requested: %d\n",
  1278. fusion->drv_map_sz);
  1279. goto ld_drv_map_alloc_fail;
  1280. }
  1281. }
  1282. }
  1283. for (i = 0; i < 2; i++) {
  1284. fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
  1285. fusion->max_map_sz,
  1286. &fusion->ld_map_phys[i],
  1287. GFP_KERNEL);
  1288. if (!fusion->ld_map[i]) {
  1289. dev_err(&instance->pdev->dev,
  1290. "Could not allocate memory for map info %s:%d\n",
  1291. __func__, __LINE__);
  1292. goto ld_map_alloc_fail;
  1293. }
  1294. }
  1295. return 0;
  1296. ld_map_alloc_fail:
  1297. for (i = 0; i < 2; i++) {
  1298. if (fusion->ld_map[i])
  1299. dma_free_coherent(&instance->pdev->dev,
  1300. fusion->max_map_sz,
  1301. fusion->ld_map[i],
  1302. fusion->ld_map_phys[i]);
  1303. }
  1304. ld_drv_map_alloc_fail:
  1305. for (i = 0; i < 2; i++) {
  1306. if (fusion->ld_drv_map[i]) {
  1307. if (is_vmalloc_addr(fusion->ld_drv_map[i]))
  1308. vfree(fusion->ld_drv_map[i]);
  1309. else
  1310. free_pages((ulong)fusion->ld_drv_map[i],
  1311. fusion->drv_map_pages);
  1312. }
  1313. }
  1314. return -ENOMEM;
  1315. }
  1316. /**
  1317. * megasas_configure_queue_sizes - Calculate size of request desc queue,
  1318. * reply desc queue,
  1319. * IO request frame queue, set can_queue.
  1320. * @instance: Adapter soft state
  1321. * @return: void
  1322. */
  1323. static inline
  1324. void megasas_configure_queue_sizes(struct megasas_instance *instance)
  1325. {
  1326. struct fusion_context *fusion;
  1327. u16 max_cmd;
  1328. fusion = instance->ctrl_context;
  1329. max_cmd = instance->max_fw_cmds;
  1330. if (instance->adapter_type == VENTURA_SERIES)
  1331. instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS;
  1332. else
  1333. instance->max_mpt_cmds = instance->max_fw_cmds;
  1334. instance->max_scsi_cmds = instance->max_fw_cmds -
  1335. (MEGASAS_FUSION_INTERNAL_CMDS +
  1336. MEGASAS_FUSION_IOCTL_CMDS);
  1337. instance->cur_can_queue = instance->max_scsi_cmds;
  1338. instance->host->can_queue = instance->cur_can_queue;
  1339. fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
  1340. fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
  1341. instance->max_mpt_cmds;
  1342. fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
  1343. (fusion->reply_q_depth);
  1344. fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
  1345. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
  1346. * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
  1347. }
  1348. static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
  1349. {
  1350. struct fusion_context *fusion;
  1351. struct megasas_cmd *cmd;
  1352. fusion = instance->ctrl_context;
  1353. cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL);
  1354. if (!cmd) {
  1355. dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
  1356. __func__, __LINE__);
  1357. return -ENOMEM;
  1358. }
  1359. cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
  1360. IOC_INIT_FRAME_SIZE,
  1361. &cmd->frame_phys_addr, GFP_KERNEL);
  1362. if (!cmd->frame) {
  1363. dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
  1364. __func__, __LINE__);
  1365. kfree(cmd);
  1366. return -ENOMEM;
  1367. }
  1368. fusion->ioc_init_cmd = cmd;
  1369. return 0;
  1370. }
  1371. /**
  1372. * megasas_free_ioc_init_cmd - Free IOC INIT command frame
  1373. * @instance: Adapter soft state
  1374. */
  1375. static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
  1376. {
  1377. struct fusion_context *fusion;
  1378. fusion = instance->ctrl_context;
  1379. if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
  1380. dma_free_coherent(&instance->pdev->dev,
  1381. IOC_INIT_FRAME_SIZE,
  1382. fusion->ioc_init_cmd->frame,
  1383. fusion->ioc_init_cmd->frame_phys_addr);
  1384. if (fusion->ioc_init_cmd)
  1385. kfree(fusion->ioc_init_cmd);
  1386. }
  1387. /**
  1388. * megasas_init_adapter_fusion - Initializes the FW
  1389. * @instance: Adapter soft state
  1390. *
  1391. * This is the main function for initializing firmware.
  1392. */
  1393. u32
  1394. megasas_init_adapter_fusion(struct megasas_instance *instance)
  1395. {
  1396. struct megasas_register_set __iomem *reg_set;
  1397. struct fusion_context *fusion;
  1398. u32 scratch_pad_2;
  1399. int i = 0, count;
  1400. fusion = instance->ctrl_context;
  1401. reg_set = instance->reg_set;
  1402. megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
  1403. /*
  1404. * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
  1405. */
  1406. instance->max_mfi_cmds =
  1407. MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
  1408. megasas_configure_queue_sizes(instance);
  1409. scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
  1410. /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
  1411. * Firmware support extended IO chain frame which is 4 times more than
  1412. * legacy Firmware.
  1413. * Legacy Firmware - Frame size is (8 * 128) = 1K
  1414. * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
  1415. */
  1416. if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
  1417. instance->max_chain_frame_sz =
  1418. ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
  1419. MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
  1420. else
  1421. instance->max_chain_frame_sz =
  1422. ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
  1423. MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
  1424. if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
  1425. dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
  1426. instance->max_chain_frame_sz,
  1427. MEGASAS_CHAIN_FRAME_SZ_MIN);
  1428. instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
  1429. }
  1430. fusion->max_sge_in_main_msg =
  1431. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
  1432. - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
  1433. fusion->max_sge_in_chain =
  1434. instance->max_chain_frame_sz
  1435. / sizeof(union MPI2_SGE_IO_UNION);
  1436. instance->max_num_sge =
  1437. rounddown_pow_of_two(fusion->max_sge_in_main_msg
  1438. + fusion->max_sge_in_chain - 2);
  1439. /* Used for pass thru MFI frame (DCMD) */
  1440. fusion->chain_offset_mfi_pthru =
  1441. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
  1442. fusion->chain_offset_io_request =
  1443. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
  1444. sizeof(union MPI2_SGE_IO_UNION))/16;
  1445. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  1446. for (i = 0 ; i < count; i++)
  1447. fusion->last_reply_idx[i] = 0;
  1448. /*
  1449. * For fusion adapters, 3 commands for IOCTL and 8 commands
  1450. * for driver's internal DCMDs.
  1451. */
  1452. instance->max_scsi_cmds = instance->max_fw_cmds -
  1453. (MEGASAS_FUSION_INTERNAL_CMDS +
  1454. MEGASAS_FUSION_IOCTL_CMDS);
  1455. sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
  1456. if (megasas_alloc_ioc_init_frame(instance))
  1457. return 1;
  1458. /*
  1459. * Allocate memory for descriptors
  1460. * Create a pool of commands
  1461. */
  1462. if (megasas_alloc_cmds(instance))
  1463. goto fail_alloc_mfi_cmds;
  1464. if (megasas_alloc_cmds_fusion(instance))
  1465. goto fail_alloc_cmds;
  1466. if (megasas_ioc_init_fusion(instance))
  1467. goto fail_ioc_init;
  1468. megasas_display_intel_branding(instance);
  1469. if (megasas_get_ctrl_info(instance)) {
  1470. dev_err(&instance->pdev->dev,
  1471. "Could not get controller info. Fail from %s %d\n",
  1472. __func__, __LINE__);
  1473. goto fail_ioc_init;
  1474. }
  1475. instance->flag_ieee = 1;
  1476. instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
  1477. fusion->fast_path_io = 0;
  1478. if (megasas_allocate_raid_maps(instance))
  1479. goto fail_ioc_init;
  1480. if (!megasas_get_map_info(instance))
  1481. megasas_sync_map_info(instance);
  1482. return 0;
  1483. fail_ioc_init:
  1484. megasas_free_cmds_fusion(instance);
  1485. fail_alloc_cmds:
  1486. megasas_free_cmds(instance);
  1487. fail_alloc_mfi_cmds:
  1488. megasas_free_ioc_init_cmd(instance);
  1489. return 1;
  1490. }
  1491. /**
  1492. * map_cmd_status - Maps FW cmd status to OS cmd status
  1493. * @cmd : Pointer to cmd
  1494. * @status : status of cmd returned by FW
  1495. * @ext_status : ext status of cmd returned by FW
  1496. */
  1497. void
  1498. map_cmd_status(struct fusion_context *fusion,
  1499. struct scsi_cmnd *scmd, u8 status, u8 ext_status,
  1500. u32 data_length, u8 *sense)
  1501. {
  1502. u8 cmd_type;
  1503. int resid;
  1504. cmd_type = megasas_cmd_type(scmd);
  1505. switch (status) {
  1506. case MFI_STAT_OK:
  1507. scmd->result = DID_OK << 16;
  1508. break;
  1509. case MFI_STAT_SCSI_IO_FAILED:
  1510. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1511. scmd->result = (DID_ERROR << 16) | ext_status;
  1512. break;
  1513. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1514. scmd->result = (DID_OK << 16) | ext_status;
  1515. if (ext_status == SAM_STAT_CHECK_CONDITION) {
  1516. memset(scmd->sense_buffer, 0,
  1517. SCSI_SENSE_BUFFERSIZE);
  1518. memcpy(scmd->sense_buffer, sense,
  1519. SCSI_SENSE_BUFFERSIZE);
  1520. scmd->result |= DRIVER_SENSE << 24;
  1521. }
  1522. /*
  1523. * If the IO request is partially completed, then MR FW will
  1524. * update "io_request->DataLength" field with actual number of
  1525. * bytes transferred.Driver will set residual bytes count in
  1526. * SCSI command structure.
  1527. */
  1528. resid = (scsi_bufflen(scmd) - data_length);
  1529. scsi_set_resid(scmd, resid);
  1530. if (resid &&
  1531. ((cmd_type == READ_WRITE_LDIO) ||
  1532. (cmd_type == READ_WRITE_SYSPDIO)))
  1533. scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
  1534. " requested/completed 0x%x/0x%x\n",
  1535. status, scsi_bufflen(scmd), data_length);
  1536. break;
  1537. case MFI_STAT_LD_OFFLINE:
  1538. case MFI_STAT_DEVICE_NOT_FOUND:
  1539. scmd->result = DID_BAD_TARGET << 16;
  1540. break;
  1541. case MFI_STAT_CONFIG_SEQ_MISMATCH:
  1542. scmd->result = DID_IMM_RETRY << 16;
  1543. break;
  1544. default:
  1545. scmd->result = DID_ERROR << 16;
  1546. break;
  1547. }
  1548. }
  1549. /**
  1550. * megasas_is_prp_possible -
  1551. * Checks if native NVMe PRPs can be built for the IO
  1552. *
  1553. * @instance: Adapter soft state
  1554. * @scmd: SCSI command from the mid-layer
  1555. * @sge_count: scatter gather element count.
  1556. *
  1557. * Returns: true: PRPs can be built
  1558. * false: IEEE SGLs needs to be built
  1559. */
  1560. static bool
  1561. megasas_is_prp_possible(struct megasas_instance *instance,
  1562. struct scsi_cmnd *scmd, int sge_count)
  1563. {
  1564. struct fusion_context *fusion;
  1565. int i;
  1566. u32 data_length = 0;
  1567. struct scatterlist *sg_scmd;
  1568. bool build_prp = false;
  1569. u32 mr_nvme_pg_size;
  1570. mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
  1571. MR_DEFAULT_NVME_PAGE_SIZE);
  1572. fusion = instance->ctrl_context;
  1573. data_length = scsi_bufflen(scmd);
  1574. sg_scmd = scsi_sglist(scmd);
  1575. /*
  1576. * NVMe uses one PRP for each page (or part of a page)
  1577. * look at the data length - if 4 pages or less then IEEE is OK
  1578. * if > 5 pages then we need to build a native SGL
  1579. * if > 4 and <= 5 pages, then check physical address of 1st SG entry
  1580. * if this first size in the page is >= the residual beyond 4 pages
  1581. * then use IEEE, otherwise use native SGL
  1582. */
  1583. if (data_length > (mr_nvme_pg_size * 5)) {
  1584. build_prp = true;
  1585. } else if ((data_length > (mr_nvme_pg_size * 4)) &&
  1586. (data_length <= (mr_nvme_pg_size * 5))) {
  1587. /* check if 1st SG entry size is < residual beyond 4 pages */
  1588. if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
  1589. build_prp = true;
  1590. }
  1591. /*
  1592. * Below code detects gaps/holes in IO data buffers.
  1593. * What does holes/gaps mean?
  1594. * Any SGE except first one in a SGL starts at non NVME page size
  1595. * aligned address OR Any SGE except last one in a SGL ends at
  1596. * non NVME page size boundary.
  1597. *
  1598. * Driver has already informed block layer by setting boundary rules for
  1599. * bio merging done at NVME page size boundary calling kernel API
  1600. * blk_queue_virt_boundary inside slave_config.
  1601. * Still there is possibility of IO coming with holes to driver because of
  1602. * IO merging done by IO scheduler.
  1603. *
  1604. * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
  1605. * IO scheduling so no IO merging.
  1606. *
  1607. * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
  1608. * then sending IOs with holes.
  1609. *
  1610. * Though driver can request block layer to disable IO merging by calling-
  1611. * blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
  1612. * user may tune sysfs parameter- nomerges again to 0 or 1.
  1613. *
  1614. * If in future IO scheduling is enabled with SCSI BLK MQ,
  1615. * this algorithm to detect holes will be required in driver
  1616. * for SCSI BLK MQ enabled case as well.
  1617. *
  1618. *
  1619. */
  1620. scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
  1621. if ((i != 0) && (i != (sge_count - 1))) {
  1622. if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
  1623. mega_mod64(sg_dma_address(sg_scmd),
  1624. mr_nvme_pg_size)) {
  1625. build_prp = false;
  1626. atomic_inc(&instance->sge_holes_type1);
  1627. break;
  1628. }
  1629. }
  1630. if ((sge_count > 1) && (i == 0)) {
  1631. if ((mega_mod64((sg_dma_address(sg_scmd) +
  1632. sg_dma_len(sg_scmd)),
  1633. mr_nvme_pg_size))) {
  1634. build_prp = false;
  1635. atomic_inc(&instance->sge_holes_type2);
  1636. break;
  1637. }
  1638. }
  1639. if ((sge_count > 1) && (i == (sge_count - 1))) {
  1640. if (mega_mod64(sg_dma_address(sg_scmd),
  1641. mr_nvme_pg_size)) {
  1642. build_prp = false;
  1643. atomic_inc(&instance->sge_holes_type3);
  1644. break;
  1645. }
  1646. }
  1647. }
  1648. return build_prp;
  1649. }
  1650. /**
  1651. * megasas_make_prp_nvme -
  1652. * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
  1653. *
  1654. * @instance: Adapter soft state
  1655. * @scmd: SCSI command from the mid-layer
  1656. * @sgl_ptr: SGL to be filled in
  1657. * @cmd: Fusion command frame
  1658. * @sge_count: scatter gather element count.
  1659. *
  1660. * Returns: true: PRPs are built
  1661. * false: IEEE SGLs needs to be built
  1662. */
  1663. static bool
  1664. megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
  1665. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
  1666. struct megasas_cmd_fusion *cmd, int sge_count)
  1667. {
  1668. int sge_len, offset, num_prp_in_chain = 0;
  1669. struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
  1670. u64 *ptr_sgl;
  1671. dma_addr_t ptr_sgl_phys;
  1672. u64 sge_addr;
  1673. u32 page_mask, page_mask_result;
  1674. struct scatterlist *sg_scmd;
  1675. u32 first_prp_len;
  1676. bool build_prp = false;
  1677. int data_len = scsi_bufflen(scmd);
  1678. struct fusion_context *fusion;
  1679. u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
  1680. MR_DEFAULT_NVME_PAGE_SIZE);
  1681. fusion = instance->ctrl_context;
  1682. build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
  1683. if (!build_prp)
  1684. return false;
  1685. /*
  1686. * Nvme has a very convoluted prp format. One prp is required
  1687. * for each page or partial page. Driver need to split up OS sg_list
  1688. * entries if it is longer than one page or cross a page
  1689. * boundary. Driver also have to insert a PRP list pointer entry as
  1690. * the last entry in each physical page of the PRP list.
  1691. *
  1692. * NOTE: The first PRP "entry" is actually placed in the first
  1693. * SGL entry in the main message as IEEE 64 format. The 2nd
  1694. * entry in the main message is the chain element, and the rest
  1695. * of the PRP entries are built in the contiguous pcie buffer.
  1696. */
  1697. page_mask = mr_nvme_pg_size - 1;
  1698. ptr_sgl = (u64 *)cmd->sg_frame;
  1699. ptr_sgl_phys = cmd->sg_frame_phys_addr;
  1700. memset(ptr_sgl, 0, instance->max_chain_frame_sz);
  1701. /* Build chain frame element which holds all prps except first*/
  1702. main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
  1703. ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
  1704. main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
  1705. main_chain_element->NextChainOffset = 0;
  1706. main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1707. IEEE_SGE_FLAGS_SYSTEM_ADDR |
  1708. MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
  1709. /* Build first prp, sge need not to be page aligned*/
  1710. ptr_first_sgl = sgl_ptr;
  1711. sg_scmd = scsi_sglist(scmd);
  1712. sge_addr = sg_dma_address(sg_scmd);
  1713. sge_len = sg_dma_len(sg_scmd);
  1714. offset = (u32)(sge_addr & page_mask);
  1715. first_prp_len = mr_nvme_pg_size - offset;
  1716. ptr_first_sgl->Address = cpu_to_le64(sge_addr);
  1717. ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
  1718. data_len -= first_prp_len;
  1719. if (sge_len > first_prp_len) {
  1720. sge_addr += first_prp_len;
  1721. sge_len -= first_prp_len;
  1722. } else if (sge_len == first_prp_len) {
  1723. sg_scmd = sg_next(sg_scmd);
  1724. sge_addr = sg_dma_address(sg_scmd);
  1725. sge_len = sg_dma_len(sg_scmd);
  1726. }
  1727. for (;;) {
  1728. offset = (u32)(sge_addr & page_mask);
  1729. /* Put PRP pointer due to page boundary*/
  1730. page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
  1731. if (unlikely(!page_mask_result)) {
  1732. scmd_printk(KERN_NOTICE,
  1733. scmd, "page boundary ptr_sgl: 0x%p\n",
  1734. ptr_sgl);
  1735. ptr_sgl_phys += 8;
  1736. *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
  1737. ptr_sgl++;
  1738. num_prp_in_chain++;
  1739. }
  1740. *ptr_sgl = cpu_to_le64(sge_addr);
  1741. ptr_sgl++;
  1742. ptr_sgl_phys += 8;
  1743. num_prp_in_chain++;
  1744. sge_addr += mr_nvme_pg_size;
  1745. sge_len -= mr_nvme_pg_size;
  1746. data_len -= mr_nvme_pg_size;
  1747. if (data_len <= 0)
  1748. break;
  1749. if (sge_len > 0)
  1750. continue;
  1751. sg_scmd = sg_next(sg_scmd);
  1752. sge_addr = sg_dma_address(sg_scmd);
  1753. sge_len = sg_dma_len(sg_scmd);
  1754. }
  1755. main_chain_element->Length =
  1756. cpu_to_le32(num_prp_in_chain * sizeof(u64));
  1757. atomic_inc(&instance->prp_sgl);
  1758. return build_prp;
  1759. }
  1760. /**
  1761. * megasas_make_sgl_fusion - Prepares 32-bit SGL
  1762. * @instance: Adapter soft state
  1763. * @scp: SCSI command from the mid-layer
  1764. * @sgl_ptr: SGL to be filled in
  1765. * @cmd: cmd we are working on
  1766. * @sge_count sge count
  1767. *
  1768. */
  1769. static void
  1770. megasas_make_sgl_fusion(struct megasas_instance *instance,
  1771. struct scsi_cmnd *scp,
  1772. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
  1773. struct megasas_cmd_fusion *cmd, int sge_count)
  1774. {
  1775. int i, sg_processed;
  1776. struct scatterlist *os_sgl;
  1777. struct fusion_context *fusion;
  1778. fusion = instance->ctrl_context;
  1779. if (instance->adapter_type >= INVADER_SERIES) {
  1780. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
  1781. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  1782. sgl_ptr_end->Flags = 0;
  1783. }
  1784. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1785. sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
  1786. sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
  1787. sgl_ptr->Flags = 0;
  1788. if (instance->adapter_type >= INVADER_SERIES)
  1789. if (i == sge_count - 1)
  1790. sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
  1791. sgl_ptr++;
  1792. sg_processed = i + 1;
  1793. if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
  1794. (sge_count > fusion->max_sge_in_main_msg)) {
  1795. struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
  1796. if (instance->adapter_type >= INVADER_SERIES) {
  1797. if ((le16_to_cpu(cmd->io_request->IoFlags) &
  1798. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
  1799. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
  1800. cmd->io_request->ChainOffset =
  1801. fusion->
  1802. chain_offset_io_request;
  1803. else
  1804. cmd->io_request->ChainOffset = 0;
  1805. } else
  1806. cmd->io_request->ChainOffset =
  1807. fusion->chain_offset_io_request;
  1808. sg_chain = sgl_ptr;
  1809. /* Prepare chain element */
  1810. sg_chain->NextChainOffset = 0;
  1811. if (instance->adapter_type >= INVADER_SERIES)
  1812. sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
  1813. else
  1814. sg_chain->Flags =
  1815. (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1816. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
  1817. sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
  1818. sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
  1819. sgl_ptr =
  1820. (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
  1821. memset(sgl_ptr, 0, instance->max_chain_frame_sz);
  1822. }
  1823. }
  1824. atomic_inc(&instance->ieee_sgl);
  1825. }
  1826. /**
  1827. * megasas_make_sgl - Build Scatter Gather List(SGLs)
  1828. * @scp: SCSI command pointer
  1829. * @instance: Soft instance of controller
  1830. * @cmd: Fusion command pointer
  1831. *
  1832. * This function will build sgls based on device type.
  1833. * For nvme drives, there is different way of building sgls in nvme native
  1834. * format- PRPs(Physical Region Page).
  1835. *
  1836. * Returns the number of sg lists actually used, zero if the sg lists
  1837. * is NULL, or -ENOMEM if the mapping failed
  1838. */
  1839. static
  1840. int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1841. struct megasas_cmd_fusion *cmd)
  1842. {
  1843. int sge_count;
  1844. bool build_prp = false;
  1845. struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
  1846. sge_count = scsi_dma_map(scp);
  1847. if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
  1848. return sge_count;
  1849. sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
  1850. if ((le16_to_cpu(cmd->io_request->IoFlags) &
  1851. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
  1852. (cmd->pd_interface == NVME_PD))
  1853. build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
  1854. cmd, sge_count);
  1855. if (!build_prp)
  1856. megasas_make_sgl_fusion(instance, scp, sgl_chain64,
  1857. cmd, sge_count);
  1858. return sge_count;
  1859. }
  1860. /**
  1861. * megasas_set_pd_lba - Sets PD LBA
  1862. * @cdb: CDB
  1863. * @cdb_len: cdb length
  1864. * @start_blk: Start block of IO
  1865. *
  1866. * Used to set the PD LBA in CDB for FP IOs
  1867. */
  1868. void
  1869. megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
  1870. struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
  1871. struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
  1872. {
  1873. struct MR_LD_RAID *raid;
  1874. u16 ld;
  1875. u64 start_blk = io_info->pdBlock;
  1876. u8 *cdb = io_request->CDB.CDB32;
  1877. u32 num_blocks = io_info->numBlocks;
  1878. u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
  1879. /* Check if T10 PI (DIF) is enabled for this LD */
  1880. ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
  1881. raid = MR_LdRaidGet(ld, local_map_ptr);
  1882. if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
  1883. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1884. cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
  1885. cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
  1886. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1887. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
  1888. else
  1889. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
  1890. cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
  1891. /* LBA */
  1892. cdb[12] = (u8)((start_blk >> 56) & 0xff);
  1893. cdb[13] = (u8)((start_blk >> 48) & 0xff);
  1894. cdb[14] = (u8)((start_blk >> 40) & 0xff);
  1895. cdb[15] = (u8)((start_blk >> 32) & 0xff);
  1896. cdb[16] = (u8)((start_blk >> 24) & 0xff);
  1897. cdb[17] = (u8)((start_blk >> 16) & 0xff);
  1898. cdb[18] = (u8)((start_blk >> 8) & 0xff);
  1899. cdb[19] = (u8)(start_blk & 0xff);
  1900. /* Logical block reference tag */
  1901. io_request->CDB.EEDP32.PrimaryReferenceTag =
  1902. cpu_to_be32(ref_tag);
  1903. io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
  1904. io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
  1905. /* Transfer length */
  1906. cdb[28] = (u8)((num_blocks >> 24) & 0xff);
  1907. cdb[29] = (u8)((num_blocks >> 16) & 0xff);
  1908. cdb[30] = (u8)((num_blocks >> 8) & 0xff);
  1909. cdb[31] = (u8)(num_blocks & 0xff);
  1910. /* set SCSI IO EEDPFlags */
  1911. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
  1912. io_request->EEDPFlags = cpu_to_le16(
  1913. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1914. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  1915. MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
  1916. MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
  1917. MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
  1918. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
  1919. } else {
  1920. io_request->EEDPFlags = cpu_to_le16(
  1921. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1922. MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
  1923. }
  1924. io_request->Control |= cpu_to_le32((0x4 << 26));
  1925. io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
  1926. } else {
  1927. /* Some drives don't support 16/12 byte CDB's, convert to 10 */
  1928. if (((cdb_len == 12) || (cdb_len == 16)) &&
  1929. (start_blk <= 0xffffffff)) {
  1930. if (cdb_len == 16) {
  1931. opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
  1932. flagvals = cdb[1];
  1933. groupnum = cdb[14];
  1934. control = cdb[15];
  1935. } else {
  1936. opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
  1937. flagvals = cdb[1];
  1938. groupnum = cdb[10];
  1939. control = cdb[11];
  1940. }
  1941. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1942. cdb[0] = opcode;
  1943. cdb[1] = flagvals;
  1944. cdb[6] = groupnum;
  1945. cdb[9] = control;
  1946. /* Transfer length */
  1947. cdb[8] = (u8)(num_blocks & 0xff);
  1948. cdb[7] = (u8)((num_blocks >> 8) & 0xff);
  1949. io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
  1950. cdb_len = 10;
  1951. } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
  1952. /* Convert to 16 byte CDB for large LBA's */
  1953. switch (cdb_len) {
  1954. case 6:
  1955. opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
  1956. control = cdb[5];
  1957. break;
  1958. case 10:
  1959. opcode =
  1960. cdb[0] == READ_10 ? READ_16 : WRITE_16;
  1961. flagvals = cdb[1];
  1962. groupnum = cdb[6];
  1963. control = cdb[9];
  1964. break;
  1965. case 12:
  1966. opcode =
  1967. cdb[0] == READ_12 ? READ_16 : WRITE_16;
  1968. flagvals = cdb[1];
  1969. groupnum = cdb[10];
  1970. control = cdb[11];
  1971. break;
  1972. }
  1973. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1974. cdb[0] = opcode;
  1975. cdb[1] = flagvals;
  1976. cdb[14] = groupnum;
  1977. cdb[15] = control;
  1978. /* Transfer length */
  1979. cdb[13] = (u8)(num_blocks & 0xff);
  1980. cdb[12] = (u8)((num_blocks >> 8) & 0xff);
  1981. cdb[11] = (u8)((num_blocks >> 16) & 0xff);
  1982. cdb[10] = (u8)((num_blocks >> 24) & 0xff);
  1983. io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
  1984. cdb_len = 16;
  1985. }
  1986. /* Normal case, just load LBA here */
  1987. switch (cdb_len) {
  1988. case 6:
  1989. {
  1990. u8 val = cdb[1] & 0xE0;
  1991. cdb[3] = (u8)(start_blk & 0xff);
  1992. cdb[2] = (u8)((start_blk >> 8) & 0xff);
  1993. cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
  1994. break;
  1995. }
  1996. case 10:
  1997. cdb[5] = (u8)(start_blk & 0xff);
  1998. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  1999. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  2000. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  2001. break;
  2002. case 12:
  2003. cdb[5] = (u8)(start_blk & 0xff);
  2004. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  2005. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  2006. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  2007. break;
  2008. case 16:
  2009. cdb[9] = (u8)(start_blk & 0xff);
  2010. cdb[8] = (u8)((start_blk >> 8) & 0xff);
  2011. cdb[7] = (u8)((start_blk >> 16) & 0xff);
  2012. cdb[6] = (u8)((start_blk >> 24) & 0xff);
  2013. cdb[5] = (u8)((start_blk >> 32) & 0xff);
  2014. cdb[4] = (u8)((start_blk >> 40) & 0xff);
  2015. cdb[3] = (u8)((start_blk >> 48) & 0xff);
  2016. cdb[2] = (u8)((start_blk >> 56) & 0xff);
  2017. break;
  2018. }
  2019. }
  2020. }
  2021. /**
  2022. * megasas_stream_detect - stream detection on read and and write IOs
  2023. * @instance: Adapter soft state
  2024. * @cmd: Command to be prepared
  2025. * @io_info: IO Request info
  2026. *
  2027. */
  2028. /** stream detection on read and and write IOs */
  2029. static void megasas_stream_detect(struct megasas_instance *instance,
  2030. struct megasas_cmd_fusion *cmd,
  2031. struct IO_REQUEST_INFO *io_info)
  2032. {
  2033. struct fusion_context *fusion = instance->ctrl_context;
  2034. u32 device_id = io_info->ldTgtId;
  2035. struct LD_STREAM_DETECT *current_ld_sd
  2036. = fusion->stream_detect_by_ld[device_id];
  2037. u32 *track_stream = &current_ld_sd->mru_bit_map, stream_num;
  2038. u32 shifted_values, unshifted_values;
  2039. u32 index_value_mask, shifted_values_mask;
  2040. int i;
  2041. bool is_read_ahead = false;
  2042. struct STREAM_DETECT *current_sd;
  2043. /* find possible stream */
  2044. for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
  2045. stream_num = (*track_stream >>
  2046. (i * BITS_PER_INDEX_STREAM)) &
  2047. STREAM_MASK;
  2048. current_sd = &current_ld_sd->stream_track[stream_num];
  2049. /* if we found a stream, update the raid
  2050. * context and also update the mruBitMap
  2051. */
  2052. /* boundary condition */
  2053. if ((current_sd->next_seq_lba) &&
  2054. (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
  2055. (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
  2056. (current_sd->is_read == io_info->isRead)) {
  2057. if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
  2058. ((!io_info->isRead) || (!is_read_ahead)))
  2059. /*
  2060. * Once the API availible we need to change this.
  2061. * At this point we are not allowing any gap
  2062. */
  2063. continue;
  2064. SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
  2065. current_sd->next_seq_lba =
  2066. io_info->ldStartBlock + io_info->numBlocks;
  2067. /*
  2068. * update the mruBitMap LRU
  2069. */
  2070. shifted_values_mask =
  2071. (1 << i * BITS_PER_INDEX_STREAM) - 1;
  2072. shifted_values = ((*track_stream & shifted_values_mask)
  2073. << BITS_PER_INDEX_STREAM);
  2074. index_value_mask =
  2075. STREAM_MASK << i * BITS_PER_INDEX_STREAM;
  2076. unshifted_values =
  2077. *track_stream & ~(shifted_values_mask |
  2078. index_value_mask);
  2079. *track_stream =
  2080. unshifted_values | shifted_values | stream_num;
  2081. return;
  2082. }
  2083. }
  2084. /*
  2085. * if we did not find any stream, create a new one
  2086. * from the least recently used
  2087. */
  2088. stream_num = (*track_stream >>
  2089. ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
  2090. STREAM_MASK;
  2091. current_sd = &current_ld_sd->stream_track[stream_num];
  2092. current_sd->is_read = io_info->isRead;
  2093. current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
  2094. *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
  2095. return;
  2096. }
  2097. /**
  2098. * megasas_set_raidflag_cpu_affinity - This function sets the cpu
  2099. * affinity (cpu of the controller) and raid_flags in the raid context
  2100. * based on IO type.
  2101. *
  2102. * @praid_context: IO RAID context
  2103. * @raid: LD raid map
  2104. * @fp_possible: Is fast path possible?
  2105. * @is_read: Is read IO?
  2106. *
  2107. */
  2108. static void
  2109. megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context,
  2110. struct MR_LD_RAID *raid, bool fp_possible,
  2111. u8 is_read, u32 scsi_buff_len)
  2112. {
  2113. u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
  2114. struct RAID_CONTEXT_G35 *rctx_g35;
  2115. rctx_g35 = &praid_context->raid_context_g35;
  2116. if (fp_possible) {
  2117. if (is_read) {
  2118. if ((raid->cpuAffinity.pdRead.cpu0) &&
  2119. (raid->cpuAffinity.pdRead.cpu1))
  2120. cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
  2121. else if (raid->cpuAffinity.pdRead.cpu1)
  2122. cpu_sel = MR_RAID_CTX_CPUSEL_1;
  2123. } else {
  2124. if ((raid->cpuAffinity.pdWrite.cpu0) &&
  2125. (raid->cpuAffinity.pdWrite.cpu1))
  2126. cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
  2127. else if (raid->cpuAffinity.pdWrite.cpu1)
  2128. cpu_sel = MR_RAID_CTX_CPUSEL_1;
  2129. /* Fast path cache by pass capable R0/R1 VD */
  2130. if ((raid->level <= 1) &&
  2131. (raid->capability.fp_cache_bypass_capable)) {
  2132. rctx_g35->routing_flags |=
  2133. (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
  2134. rctx_g35->raid_flags =
  2135. (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
  2136. << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
  2137. }
  2138. }
  2139. } else {
  2140. if (is_read) {
  2141. if ((raid->cpuAffinity.ldRead.cpu0) &&
  2142. (raid->cpuAffinity.ldRead.cpu1))
  2143. cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
  2144. else if (raid->cpuAffinity.ldRead.cpu1)
  2145. cpu_sel = MR_RAID_CTX_CPUSEL_1;
  2146. } else {
  2147. if ((raid->cpuAffinity.ldWrite.cpu0) &&
  2148. (raid->cpuAffinity.ldWrite.cpu1))
  2149. cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
  2150. else if (raid->cpuAffinity.ldWrite.cpu1)
  2151. cpu_sel = MR_RAID_CTX_CPUSEL_1;
  2152. if (is_stream_detected(rctx_g35) &&
  2153. ((raid->level == 5) || (raid->level == 6)) &&
  2154. (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
  2155. (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
  2156. cpu_sel = MR_RAID_CTX_CPUSEL_0;
  2157. }
  2158. }
  2159. rctx_g35->routing_flags |=
  2160. (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
  2161. /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
  2162. * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
  2163. * IO Subtype is not bitmap.
  2164. */
  2165. if ((raid->level == 1) && (!is_read)) {
  2166. if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
  2167. praid_context->raid_context_g35.raid_flags =
  2168. (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
  2169. << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
  2170. }
  2171. }
  2172. /**
  2173. * megasas_build_ldio_fusion - Prepares IOs to devices
  2174. * @instance: Adapter soft state
  2175. * @scp: SCSI command
  2176. * @cmd: Command to be prepared
  2177. *
  2178. * Prepares the io_request and chain elements (sg_frame) for IO
  2179. * The IO can be for PD (Fast Path) or LD
  2180. */
  2181. void
  2182. megasas_build_ldio_fusion(struct megasas_instance *instance,
  2183. struct scsi_cmnd *scp,
  2184. struct megasas_cmd_fusion *cmd)
  2185. {
  2186. bool fp_possible;
  2187. u16 ld;
  2188. u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
  2189. u32 scsi_buff_len;
  2190. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  2191. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2192. struct IO_REQUEST_INFO io_info;
  2193. struct fusion_context *fusion;
  2194. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  2195. u8 *raidLUN;
  2196. unsigned long spinlock_flags;
  2197. union RAID_CONTEXT_UNION *praid_context;
  2198. struct MR_LD_RAID *raid = NULL;
  2199. struct MR_PRIV_DEVICE *mrdev_priv;
  2200. device_id = MEGASAS_DEV_INDEX(scp);
  2201. fusion = instance->ctrl_context;
  2202. io_request = cmd->io_request;
  2203. io_request->RaidContext.raid_context.virtual_disk_tgt_id =
  2204. cpu_to_le16(device_id);
  2205. io_request->RaidContext.raid_context.status = 0;
  2206. io_request->RaidContext.raid_context.ex_status = 0;
  2207. req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
  2208. start_lba_lo = 0;
  2209. start_lba_hi = 0;
  2210. fp_possible = false;
  2211. /*
  2212. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  2213. */
  2214. if (scp->cmd_len == 6) {
  2215. datalength = (u32) scp->cmnd[4];
  2216. start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  2217. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  2218. start_lba_lo &= 0x1FFFFF;
  2219. }
  2220. /*
  2221. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  2222. */
  2223. else if (scp->cmd_len == 10) {
  2224. datalength = (u32) scp->cmnd[8] |
  2225. ((u32) scp->cmnd[7] << 8);
  2226. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  2227. ((u32) scp->cmnd[3] << 16) |
  2228. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  2229. }
  2230. /*
  2231. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  2232. */
  2233. else if (scp->cmd_len == 12) {
  2234. datalength = ((u32) scp->cmnd[6] << 24) |
  2235. ((u32) scp->cmnd[7] << 16) |
  2236. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  2237. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  2238. ((u32) scp->cmnd[3] << 16) |
  2239. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  2240. }
  2241. /*
  2242. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  2243. */
  2244. else if (scp->cmd_len == 16) {
  2245. datalength = ((u32) scp->cmnd[10] << 24) |
  2246. ((u32) scp->cmnd[11] << 16) |
  2247. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  2248. start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  2249. ((u32) scp->cmnd[7] << 16) |
  2250. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  2251. start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  2252. ((u32) scp->cmnd[3] << 16) |
  2253. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  2254. }
  2255. memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
  2256. io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
  2257. io_info.numBlocks = datalength;
  2258. io_info.ldTgtId = device_id;
  2259. io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
  2260. scsi_buff_len = scsi_bufflen(scp);
  2261. io_request->DataLength = cpu_to_le32(scsi_buff_len);
  2262. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  2263. io_info.isRead = 1;
  2264. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  2265. ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
  2266. if (ld < instance->fw_supported_vd_count)
  2267. raid = MR_LdRaidGet(ld, local_map_ptr);
  2268. if (!raid || (!fusion->fast_path_io)) {
  2269. io_request->RaidContext.raid_context.reg_lock_flags = 0;
  2270. fp_possible = false;
  2271. } else {
  2272. if (MR_BuildRaidContext(instance, &io_info,
  2273. &io_request->RaidContext.raid_context,
  2274. local_map_ptr, &raidLUN))
  2275. fp_possible = (io_info.fpOkForIo > 0) ? true : false;
  2276. }
  2277. cmd->request_desc->SCSIIO.MSIxIndex =
  2278. instance->reply_map[raw_smp_processor_id()];
  2279. praid_context = &io_request->RaidContext;
  2280. if (instance->adapter_type == VENTURA_SERIES) {
  2281. /* FP for Optimal raid level 1.
  2282. * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
  2283. * are built by the driver as LD I/Os.
  2284. * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
  2285. * (there is never a reason to process these as buffered writes)
  2286. * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
  2287. * with the SLD bit asserted.
  2288. */
  2289. if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
  2290. mrdev_priv = scp->device->hostdata;
  2291. if (atomic_inc_return(&instance->fw_outstanding) >
  2292. (instance->host->can_queue)) {
  2293. fp_possible = false;
  2294. atomic_dec(&instance->fw_outstanding);
  2295. } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
  2296. (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0)) {
  2297. fp_possible = false;
  2298. atomic_dec(&instance->fw_outstanding);
  2299. if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
  2300. atomic_set(&mrdev_priv->r1_ldio_hint,
  2301. instance->r1_ldio_hint_default);
  2302. }
  2303. }
  2304. if (!fp_possible ||
  2305. (io_info.isRead && io_info.ra_capable)) {
  2306. spin_lock_irqsave(&instance->stream_lock,
  2307. spinlock_flags);
  2308. megasas_stream_detect(instance, cmd, &io_info);
  2309. spin_unlock_irqrestore(&instance->stream_lock,
  2310. spinlock_flags);
  2311. /* In ventura if stream detected for a read and it is
  2312. * read ahead capable make this IO as LDIO
  2313. */
  2314. if (is_stream_detected(&io_request->RaidContext.raid_context_g35))
  2315. fp_possible = false;
  2316. }
  2317. /* If raid is NULL, set CPU affinity to default CPU0 */
  2318. if (raid)
  2319. megasas_set_raidflag_cpu_affinity(praid_context,
  2320. raid, fp_possible, io_info.isRead,
  2321. scsi_buff_len);
  2322. else
  2323. praid_context->raid_context_g35.routing_flags |=
  2324. (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
  2325. }
  2326. if (fp_possible) {
  2327. megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
  2328. local_map_ptr, start_lba_lo);
  2329. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2330. cmd->request_desc->SCSIIO.RequestFlags =
  2331. (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
  2332. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2333. if (instance->adapter_type == INVADER_SERIES) {
  2334. if (io_request->RaidContext.raid_context.reg_lock_flags ==
  2335. REGION_TYPE_UNUSED)
  2336. cmd->request_desc->SCSIIO.RequestFlags =
  2337. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  2338. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2339. io_request->RaidContext.raid_context.type
  2340. = MPI2_TYPE_CUDA;
  2341. io_request->RaidContext.raid_context.nseg = 0x1;
  2342. io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
  2343. io_request->RaidContext.raid_context.reg_lock_flags |=
  2344. (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
  2345. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  2346. } else if (instance->adapter_type == VENTURA_SERIES) {
  2347. io_request->RaidContext.raid_context_g35.nseg_type |=
  2348. (1 << RAID_CONTEXT_NSEG_SHIFT);
  2349. io_request->RaidContext.raid_context_g35.nseg_type |=
  2350. (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
  2351. io_request->RaidContext.raid_context_g35.routing_flags |=
  2352. (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
  2353. io_request->IoFlags |=
  2354. cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
  2355. }
  2356. if (fusion->load_balance_info &&
  2357. (fusion->load_balance_info[device_id].loadBalanceFlag) &&
  2358. (io_info.isRead)) {
  2359. io_info.devHandle =
  2360. get_updated_dev_handle(instance,
  2361. &fusion->load_balance_info[device_id],
  2362. &io_info, local_map_ptr);
  2363. scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
  2364. cmd->pd_r1_lb = io_info.pd_after_lb;
  2365. if (instance->adapter_type == VENTURA_SERIES)
  2366. io_request->RaidContext.raid_context_g35.span_arm
  2367. = io_info.span_arm;
  2368. else
  2369. io_request->RaidContext.raid_context.span_arm
  2370. = io_info.span_arm;
  2371. } else
  2372. scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
  2373. if (instance->adapter_type == VENTURA_SERIES)
  2374. cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
  2375. else
  2376. cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
  2377. if ((raidLUN[0] == 1) &&
  2378. (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
  2379. instance->dev_handle = !(instance->dev_handle);
  2380. io_info.devHandle =
  2381. local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
  2382. }
  2383. cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
  2384. io_request->DevHandle = io_info.devHandle;
  2385. cmd->pd_interface = io_info.pd_interface;
  2386. /* populate the LUN field */
  2387. memcpy(io_request->LUN, raidLUN, 8);
  2388. } else {
  2389. io_request->RaidContext.raid_context.timeout_value =
  2390. cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
  2391. cmd->request_desc->SCSIIO.RequestFlags =
  2392. (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
  2393. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2394. if (instance->adapter_type == INVADER_SERIES) {
  2395. if (io_info.do_fp_rlbypass ||
  2396. (io_request->RaidContext.raid_context.reg_lock_flags
  2397. == REGION_TYPE_UNUSED))
  2398. cmd->request_desc->SCSIIO.RequestFlags =
  2399. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  2400. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2401. io_request->RaidContext.raid_context.type
  2402. = MPI2_TYPE_CUDA;
  2403. io_request->RaidContext.raid_context.reg_lock_flags |=
  2404. (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
  2405. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  2406. io_request->RaidContext.raid_context.nseg = 0x1;
  2407. } else if (instance->adapter_type == VENTURA_SERIES) {
  2408. io_request->RaidContext.raid_context_g35.routing_flags |=
  2409. (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
  2410. io_request->RaidContext.raid_context_g35.nseg_type |=
  2411. (1 << RAID_CONTEXT_NSEG_SHIFT);
  2412. io_request->RaidContext.raid_context_g35.nseg_type |=
  2413. (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
  2414. }
  2415. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  2416. io_request->DevHandle = cpu_to_le16(device_id);
  2417. } /* Not FP */
  2418. }
  2419. /**
  2420. * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
  2421. * @instance: Adapter soft state
  2422. * @scp: SCSI command
  2423. * @cmd: Command to be prepared
  2424. *
  2425. * Prepares the io_request frame for non-rw io cmds for vd.
  2426. */
  2427. static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
  2428. struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
  2429. {
  2430. u32 device_id;
  2431. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  2432. u16 ld;
  2433. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  2434. struct fusion_context *fusion = instance->ctrl_context;
  2435. u8 span, physArm;
  2436. __le16 devHandle;
  2437. u32 arRef, pd;
  2438. struct MR_LD_RAID *raid;
  2439. struct RAID_CONTEXT *pRAID_Context;
  2440. u8 fp_possible = 1;
  2441. io_request = cmd->io_request;
  2442. device_id = MEGASAS_DEV_INDEX(scmd);
  2443. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  2444. io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  2445. /* get RAID_Context pointer */
  2446. pRAID_Context = &io_request->RaidContext.raid_context;
  2447. /* Check with FW team */
  2448. pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
  2449. pRAID_Context->reg_lock_row_lba = 0;
  2450. pRAID_Context->reg_lock_length = 0;
  2451. if (fusion->fast_path_io && (
  2452. device_id < instance->fw_supported_vd_count)) {
  2453. ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
  2454. if (ld >= instance->fw_supported_vd_count)
  2455. fp_possible = 0;
  2456. else {
  2457. raid = MR_LdRaidGet(ld, local_map_ptr);
  2458. if (!(raid->capability.fpNonRWCapable))
  2459. fp_possible = 0;
  2460. }
  2461. } else
  2462. fp_possible = 0;
  2463. if (!fp_possible) {
  2464. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  2465. io_request->DevHandle = cpu_to_le16(device_id);
  2466. io_request->LUN[1] = scmd->device->lun;
  2467. pRAID_Context->timeout_value =
  2468. cpu_to_le16 (scmd->request->timeout / HZ);
  2469. cmd->request_desc->SCSIIO.RequestFlags =
  2470. (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  2471. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2472. } else {
  2473. /* set RAID context values */
  2474. pRAID_Context->config_seq_num = raid->seqNum;
  2475. if (instance->adapter_type != VENTURA_SERIES)
  2476. pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
  2477. pRAID_Context->timeout_value =
  2478. cpu_to_le16(raid->fpIoTimeoutForLd);
  2479. /* get the DevHandle for the PD (since this is
  2480. fpNonRWCapable, this is a single disk RAID0) */
  2481. span = physArm = 0;
  2482. arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
  2483. pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
  2484. devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
  2485. /* build request descriptor */
  2486. cmd->request_desc->SCSIIO.RequestFlags =
  2487. (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
  2488. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2489. cmd->request_desc->SCSIIO.DevHandle = devHandle;
  2490. /* populate the LUN field */
  2491. memcpy(io_request->LUN, raid->LUN, 8);
  2492. /* build the raidScsiIO structure */
  2493. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2494. io_request->DevHandle = devHandle;
  2495. }
  2496. }
  2497. /**
  2498. * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
  2499. * @instance: Adapter soft state
  2500. * @scp: SCSI command
  2501. * @cmd: Command to be prepared
  2502. * @fp_possible: parameter to detect fast path or firmware path io.
  2503. *
  2504. * Prepares the io_request frame for rw/non-rw io cmds for syspds
  2505. */
  2506. static void
  2507. megasas_build_syspd_fusion(struct megasas_instance *instance,
  2508. struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
  2509. bool fp_possible)
  2510. {
  2511. u32 device_id;
  2512. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  2513. u16 pd_index = 0;
  2514. u16 os_timeout_value;
  2515. u16 timeout_limit;
  2516. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  2517. struct RAID_CONTEXT *pRAID_Context;
  2518. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  2519. struct MR_PRIV_DEVICE *mr_device_priv_data;
  2520. struct fusion_context *fusion = instance->ctrl_context;
  2521. pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
  2522. device_id = MEGASAS_DEV_INDEX(scmd);
  2523. pd_index = MEGASAS_PD_INDEX(scmd);
  2524. os_timeout_value = scmd->request->timeout / HZ;
  2525. mr_device_priv_data = scmd->device->hostdata;
  2526. cmd->pd_interface = mr_device_priv_data->interface_type;
  2527. io_request = cmd->io_request;
  2528. /* get RAID_Context pointer */
  2529. pRAID_Context = &io_request->RaidContext.raid_context;
  2530. pRAID_Context->reg_lock_flags = 0;
  2531. pRAID_Context->reg_lock_row_lba = 0;
  2532. pRAID_Context->reg_lock_length = 0;
  2533. io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  2534. io_request->LUN[1] = scmd->device->lun;
  2535. pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
  2536. << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
  2537. /* If FW supports PD sequence number */
  2538. if (instance->use_seqnum_jbod_fp &&
  2539. instance->pd_list[pd_index].driveType == TYPE_DISK) {
  2540. /* TgtId must be incremented by 255 as jbod seq number is index
  2541. * below raid map
  2542. */
  2543. /* More than 256 PD/JBOD support for Ventura */
  2544. if (instance->support_morethan256jbod)
  2545. pRAID_Context->virtual_disk_tgt_id =
  2546. pd_sync->seq[pd_index].pd_target_id;
  2547. else
  2548. pRAID_Context->virtual_disk_tgt_id =
  2549. cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
  2550. pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
  2551. io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
  2552. if (instance->adapter_type == VENTURA_SERIES) {
  2553. io_request->RaidContext.raid_context_g35.routing_flags |=
  2554. (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
  2555. io_request->RaidContext.raid_context_g35.nseg_type |=
  2556. (1 << RAID_CONTEXT_NSEG_SHIFT);
  2557. io_request->RaidContext.raid_context_g35.nseg_type |=
  2558. (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
  2559. } else {
  2560. pRAID_Context->type = MPI2_TYPE_CUDA;
  2561. pRAID_Context->nseg = 0x1;
  2562. pRAID_Context->reg_lock_flags |=
  2563. (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
  2564. }
  2565. } else if (fusion->fast_path_io) {
  2566. pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
  2567. pRAID_Context->config_seq_num = 0;
  2568. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  2569. io_request->DevHandle =
  2570. local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
  2571. } else {
  2572. /* Want to send all IO via FW path */
  2573. pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
  2574. pRAID_Context->config_seq_num = 0;
  2575. io_request->DevHandle = cpu_to_le16(0xFFFF);
  2576. }
  2577. cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
  2578. cmd->request_desc->SCSIIO.MSIxIndex =
  2579. instance->reply_map[raw_smp_processor_id()];
  2580. if (!fp_possible) {
  2581. /* system pd firmware path */
  2582. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  2583. cmd->request_desc->SCSIIO.RequestFlags =
  2584. (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  2585. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2586. pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
  2587. pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
  2588. } else {
  2589. if (os_timeout_value)
  2590. os_timeout_value++;
  2591. /* system pd Fast Path */
  2592. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2593. timeout_limit = (scmd->device->type == TYPE_DISK) ?
  2594. 255 : 0xFFFF;
  2595. pRAID_Context->timeout_value =
  2596. cpu_to_le16((os_timeout_value > timeout_limit) ?
  2597. timeout_limit : os_timeout_value);
  2598. if (instance->adapter_type >= INVADER_SERIES)
  2599. io_request->IoFlags |=
  2600. cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
  2601. cmd->request_desc->SCSIIO.RequestFlags =
  2602. (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
  2603. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2604. }
  2605. }
  2606. /**
  2607. * megasas_build_io_fusion - Prepares IOs to devices
  2608. * @instance: Adapter soft state
  2609. * @scp: SCSI command
  2610. * @cmd: Command to be prepared
  2611. *
  2612. * Invokes helper functions to prepare request frames
  2613. * and sets flags appropriate for IO/Non-IO cmd
  2614. */
  2615. int
  2616. megasas_build_io_fusion(struct megasas_instance *instance,
  2617. struct scsi_cmnd *scp,
  2618. struct megasas_cmd_fusion *cmd)
  2619. {
  2620. int sge_count;
  2621. u8 cmd_type;
  2622. struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
  2623. struct MR_PRIV_DEVICE *mr_device_priv_data;
  2624. mr_device_priv_data = scp->device->hostdata;
  2625. /* Zero out some fields so they don't get reused */
  2626. memset(io_request->LUN, 0x0, 8);
  2627. io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
  2628. io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
  2629. io_request->EEDPFlags = 0;
  2630. io_request->Control = 0;
  2631. io_request->EEDPBlockSize = 0;
  2632. io_request->ChainOffset = 0;
  2633. io_request->RaidContext.raid_context.raid_flags = 0;
  2634. io_request->RaidContext.raid_context.type = 0;
  2635. io_request->RaidContext.raid_context.nseg = 0;
  2636. memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
  2637. /*
  2638. * Just the CDB length,rest of the Flags are zero
  2639. * This will be modified for FP in build_ldio_fusion
  2640. */
  2641. io_request->IoFlags = cpu_to_le16(scp->cmd_len);
  2642. switch (cmd_type = megasas_cmd_type(scp)) {
  2643. case READ_WRITE_LDIO:
  2644. megasas_build_ldio_fusion(instance, scp, cmd);
  2645. break;
  2646. case NON_READ_WRITE_LDIO:
  2647. megasas_build_ld_nonrw_fusion(instance, scp, cmd);
  2648. break;
  2649. case READ_WRITE_SYSPDIO:
  2650. megasas_build_syspd_fusion(instance, scp, cmd, true);
  2651. break;
  2652. case NON_READ_WRITE_SYSPDIO:
  2653. if (instance->secure_jbod_support ||
  2654. mr_device_priv_data->is_tm_capable)
  2655. megasas_build_syspd_fusion(instance, scp, cmd, false);
  2656. else
  2657. megasas_build_syspd_fusion(instance, scp, cmd, true);
  2658. break;
  2659. default:
  2660. break;
  2661. }
  2662. /*
  2663. * Construct SGL
  2664. */
  2665. sge_count = megasas_make_sgl(instance, scp, cmd);
  2666. if (sge_count > instance->max_num_sge || (sge_count < 0)) {
  2667. dev_err(&instance->pdev->dev,
  2668. "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
  2669. __func__, __LINE__, sge_count, instance->max_num_sge);
  2670. return 1;
  2671. }
  2672. if (instance->adapter_type == VENTURA_SERIES) {
  2673. set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
  2674. cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
  2675. cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
  2676. } else {
  2677. /* numSGE store lower 8 bit of sge_count.
  2678. * numSGEExt store higher 8 bit of sge_count
  2679. */
  2680. io_request->RaidContext.raid_context.num_sge = sge_count;
  2681. io_request->RaidContext.raid_context.num_sge_ext =
  2682. (u8)(sge_count >> 8);
  2683. }
  2684. io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
  2685. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  2686. io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
  2687. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  2688. io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
  2689. io_request->SGLOffset0 =
  2690. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
  2691. io_request->SenseBufferLowAddress =
  2692. cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
  2693. io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  2694. cmd->scmd = scp;
  2695. scp->SCp.ptr = (char *)cmd;
  2696. return 0;
  2697. }
  2698. static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  2699. megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
  2700. {
  2701. u8 *p;
  2702. struct fusion_context *fusion;
  2703. fusion = instance->ctrl_context;
  2704. p = fusion->req_frames_desc +
  2705. sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
  2706. return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
  2707. }
  2708. /* megasas_prepate_secondRaid1_IO
  2709. * It prepares the raid 1 second IO
  2710. */
  2711. void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
  2712. struct megasas_cmd_fusion *cmd,
  2713. struct megasas_cmd_fusion *r1_cmd)
  2714. {
  2715. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
  2716. struct fusion_context *fusion;
  2717. fusion = instance->ctrl_context;
  2718. req_desc = cmd->request_desc;
  2719. /* copy the io request frame as well as 8 SGEs data for r1 command*/
  2720. memcpy(r1_cmd->io_request, cmd->io_request,
  2721. (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
  2722. memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
  2723. (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
  2724. /*sense buffer is different for r1 command*/
  2725. r1_cmd->io_request->SenseBufferLowAddress =
  2726. cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
  2727. r1_cmd->scmd = cmd->scmd;
  2728. req_desc2 = megasas_get_request_descriptor(instance,
  2729. (r1_cmd->index - 1));
  2730. req_desc2->Words = 0;
  2731. r1_cmd->request_desc = req_desc2;
  2732. req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
  2733. req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
  2734. r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
  2735. r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
  2736. r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
  2737. cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
  2738. cpu_to_le16(r1_cmd->index);
  2739. r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
  2740. cpu_to_le16(cmd->index);
  2741. /*MSIxIndex of both commands request descriptors should be same*/
  2742. r1_cmd->request_desc->SCSIIO.MSIxIndex =
  2743. cmd->request_desc->SCSIIO.MSIxIndex;
  2744. /*span arm is different for r1 cmd*/
  2745. r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
  2746. cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
  2747. }
  2748. /**
  2749. * megasas_build_and_issue_cmd_fusion -Main routine for building and
  2750. * issuing non IOCTL cmd
  2751. * @instance: Adapter soft state
  2752. * @scmd: pointer to scsi cmd from OS
  2753. */
  2754. static u32
  2755. megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
  2756. struct scsi_cmnd *scmd)
  2757. {
  2758. struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
  2759. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2760. u32 index;
  2761. struct fusion_context *fusion;
  2762. fusion = instance->ctrl_context;
  2763. if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
  2764. instance->ldio_threshold &&
  2765. (atomic_inc_return(&instance->ldio_outstanding) >
  2766. instance->ldio_threshold)) {
  2767. atomic_dec(&instance->ldio_outstanding);
  2768. return SCSI_MLQUEUE_DEVICE_BUSY;
  2769. }
  2770. if (atomic_inc_return(&instance->fw_outstanding) >
  2771. instance->host->can_queue) {
  2772. atomic_dec(&instance->fw_outstanding);
  2773. return SCSI_MLQUEUE_HOST_BUSY;
  2774. }
  2775. cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
  2776. if (!cmd) {
  2777. atomic_dec(&instance->fw_outstanding);
  2778. return SCSI_MLQUEUE_HOST_BUSY;
  2779. }
  2780. index = cmd->index;
  2781. req_desc = megasas_get_request_descriptor(instance, index-1);
  2782. req_desc->Words = 0;
  2783. cmd->request_desc = req_desc;
  2784. if (megasas_build_io_fusion(instance, scmd, cmd)) {
  2785. megasas_return_cmd_fusion(instance, cmd);
  2786. dev_err(&instance->pdev->dev, "Error building command\n");
  2787. cmd->request_desc = NULL;
  2788. atomic_dec(&instance->fw_outstanding);
  2789. return SCSI_MLQUEUE_HOST_BUSY;
  2790. }
  2791. req_desc = cmd->request_desc;
  2792. req_desc->SCSIIO.SMID = cpu_to_le16(index);
  2793. if (cmd->io_request->ChainOffset != 0 &&
  2794. cmd->io_request->ChainOffset != 0xF)
  2795. dev_err(&instance->pdev->dev, "The chain offset value is not "
  2796. "correct : %x\n", cmd->io_request->ChainOffset);
  2797. /*
  2798. * if it is raid 1/10 fp write capable.
  2799. * try to get second command from pool and construct it.
  2800. * From FW, it has confirmed that lba values of two PDs
  2801. * corresponds to single R1/10 LD are always same
  2802. *
  2803. */
  2804. /* driver side count always should be less than max_fw_cmds
  2805. * to get new command
  2806. */
  2807. if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
  2808. r1_cmd = megasas_get_cmd_fusion(instance,
  2809. (scmd->request->tag + instance->max_fw_cmds));
  2810. megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
  2811. }
  2812. /*
  2813. * Issue the command to the FW
  2814. */
  2815. megasas_fire_cmd_fusion(instance, req_desc);
  2816. if (r1_cmd)
  2817. megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
  2818. return 0;
  2819. }
  2820. /**
  2821. * megasas_complete_r1_command -
  2822. * completes R1 FP write commands which has valid peer smid
  2823. * @instance: Adapter soft state
  2824. * @cmd_fusion: MPT command frame
  2825. *
  2826. */
  2827. static inline void
  2828. megasas_complete_r1_command(struct megasas_instance *instance,
  2829. struct megasas_cmd_fusion *cmd)
  2830. {
  2831. u8 *sense, status, ex_status;
  2832. u32 data_length;
  2833. u16 peer_smid;
  2834. struct fusion_context *fusion;
  2835. struct megasas_cmd_fusion *r1_cmd = NULL;
  2836. struct scsi_cmnd *scmd_local = NULL;
  2837. struct RAID_CONTEXT_G35 *rctx_g35;
  2838. rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
  2839. fusion = instance->ctrl_context;
  2840. peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid);
  2841. r1_cmd = fusion->cmd_list[peer_smid - 1];
  2842. scmd_local = cmd->scmd;
  2843. status = rctx_g35->status;
  2844. ex_status = rctx_g35->ex_status;
  2845. data_length = cmd->io_request->DataLength;
  2846. sense = cmd->sense;
  2847. cmd->cmd_completed = true;
  2848. /* Check if peer command is completed or not*/
  2849. if (r1_cmd->cmd_completed) {
  2850. rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
  2851. if (rctx_g35->status != MFI_STAT_OK) {
  2852. status = rctx_g35->status;
  2853. ex_status = rctx_g35->ex_status;
  2854. data_length = r1_cmd->io_request->DataLength;
  2855. sense = r1_cmd->sense;
  2856. }
  2857. megasas_return_cmd_fusion(instance, r1_cmd);
  2858. map_cmd_status(fusion, scmd_local, status, ex_status,
  2859. le32_to_cpu(data_length), sense);
  2860. if (instance->ldio_threshold &&
  2861. megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
  2862. atomic_dec(&instance->ldio_outstanding);
  2863. scmd_local->SCp.ptr = NULL;
  2864. megasas_return_cmd_fusion(instance, cmd);
  2865. scsi_dma_unmap(scmd_local);
  2866. scmd_local->scsi_done(scmd_local);
  2867. }
  2868. }
  2869. /**
  2870. * complete_cmd_fusion - Completes command
  2871. * @instance: Adapter soft state
  2872. * Completes all commands that is in reply descriptor queue
  2873. */
  2874. int
  2875. complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
  2876. {
  2877. union MPI2_REPLY_DESCRIPTORS_UNION *desc;
  2878. struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
  2879. struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
  2880. struct fusion_context *fusion;
  2881. struct megasas_cmd *cmd_mfi;
  2882. struct megasas_cmd_fusion *cmd_fusion;
  2883. u16 smid, num_completed;
  2884. u8 reply_descript_type, *sense, status, extStatus;
  2885. u32 device_id, data_length;
  2886. union desc_value d_val;
  2887. struct LD_LOAD_BALANCE_INFO *lbinfo;
  2888. int threshold_reply_count = 0;
  2889. struct scsi_cmnd *scmd_local = NULL;
  2890. struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
  2891. struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
  2892. fusion = instance->ctrl_context;
  2893. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  2894. return IRQ_HANDLED;
  2895. desc = fusion->reply_frames_desc[MSIxIndex] +
  2896. fusion->last_reply_idx[MSIxIndex];
  2897. reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  2898. d_val.word = desc->Words;
  2899. reply_descript_type = reply_desc->ReplyFlags &
  2900. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  2901. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  2902. return IRQ_NONE;
  2903. num_completed = 0;
  2904. while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
  2905. d_val.u.high != cpu_to_le32(UINT_MAX)) {
  2906. smid = le16_to_cpu(reply_desc->SMID);
  2907. cmd_fusion = fusion->cmd_list[smid - 1];
  2908. scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
  2909. cmd_fusion->io_request;
  2910. scmd_local = cmd_fusion->scmd;
  2911. status = scsi_io_req->RaidContext.raid_context.status;
  2912. extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
  2913. sense = cmd_fusion->sense;
  2914. data_length = scsi_io_req->DataLength;
  2915. switch (scsi_io_req->Function) {
  2916. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  2917. mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
  2918. cmd_fusion->io_request;
  2919. mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
  2920. &mr_tm_req->TmRequest;
  2921. dev_dbg(&instance->pdev->dev, "TM completion:"
  2922. "type: 0x%x TaskMID: 0x%x\n",
  2923. mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
  2924. complete(&cmd_fusion->done);
  2925. break;
  2926. case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
  2927. /* Update load balancing info */
  2928. if (fusion->load_balance_info &&
  2929. (cmd_fusion->scmd->SCp.Status &
  2930. MEGASAS_LOAD_BALANCE_FLAG)) {
  2931. device_id = MEGASAS_DEV_INDEX(scmd_local);
  2932. lbinfo = &fusion->load_balance_info[device_id];
  2933. atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
  2934. cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
  2935. }
  2936. //Fall thru and complete IO
  2937. case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
  2938. atomic_dec(&instance->fw_outstanding);
  2939. if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
  2940. map_cmd_status(fusion, scmd_local, status,
  2941. extStatus, le32_to_cpu(data_length),
  2942. sense);
  2943. if (instance->ldio_threshold &&
  2944. (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
  2945. atomic_dec(&instance->ldio_outstanding);
  2946. scmd_local->SCp.ptr = NULL;
  2947. megasas_return_cmd_fusion(instance, cmd_fusion);
  2948. scsi_dma_unmap(scmd_local);
  2949. scmd_local->scsi_done(scmd_local);
  2950. } else /* Optimal VD - R1 FP command completion. */
  2951. megasas_complete_r1_command(instance, cmd_fusion);
  2952. break;
  2953. case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
  2954. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  2955. /* Poll mode. Dummy free.
  2956. * In case of Interrupt mode, caller has reverse check.
  2957. */
  2958. if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
  2959. cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
  2960. megasas_return_cmd(instance, cmd_mfi);
  2961. } else
  2962. megasas_complete_cmd(instance, cmd_mfi, DID_OK);
  2963. break;
  2964. }
  2965. fusion->last_reply_idx[MSIxIndex]++;
  2966. if (fusion->last_reply_idx[MSIxIndex] >=
  2967. fusion->reply_q_depth)
  2968. fusion->last_reply_idx[MSIxIndex] = 0;
  2969. desc->Words = cpu_to_le64(ULLONG_MAX);
  2970. num_completed++;
  2971. threshold_reply_count++;
  2972. /* Get the next reply descriptor */
  2973. if (!fusion->last_reply_idx[MSIxIndex])
  2974. desc = fusion->reply_frames_desc[MSIxIndex];
  2975. else
  2976. desc++;
  2977. reply_desc =
  2978. (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  2979. d_val.word = desc->Words;
  2980. reply_descript_type = reply_desc->ReplyFlags &
  2981. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  2982. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  2983. break;
  2984. /*
  2985. * Write to reply post host index register after completing threshold
  2986. * number of reply counts and still there are more replies in reply queue
  2987. * pending to be completed
  2988. */
  2989. if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
  2990. if (instance->msix_combined)
  2991. writel(((MSIxIndex & 0x7) << 24) |
  2992. fusion->last_reply_idx[MSIxIndex],
  2993. instance->reply_post_host_index_addr[MSIxIndex/8]);
  2994. else
  2995. writel((MSIxIndex << 24) |
  2996. fusion->last_reply_idx[MSIxIndex],
  2997. instance->reply_post_host_index_addr[0]);
  2998. threshold_reply_count = 0;
  2999. }
  3000. }
  3001. if (!num_completed)
  3002. return IRQ_NONE;
  3003. wmb();
  3004. if (instance->msix_combined)
  3005. writel(((MSIxIndex & 0x7) << 24) |
  3006. fusion->last_reply_idx[MSIxIndex],
  3007. instance->reply_post_host_index_addr[MSIxIndex/8]);
  3008. else
  3009. writel((MSIxIndex << 24) |
  3010. fusion->last_reply_idx[MSIxIndex],
  3011. instance->reply_post_host_index_addr[0]);
  3012. megasas_check_and_restore_queue_depth(instance);
  3013. return IRQ_HANDLED;
  3014. }
  3015. /**
  3016. * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
  3017. * @instance: Adapter soft state
  3018. */
  3019. void megasas_sync_irqs(unsigned long instance_addr)
  3020. {
  3021. u32 count, i;
  3022. struct megasas_instance *instance =
  3023. (struct megasas_instance *)instance_addr;
  3024. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  3025. for (i = 0; i < count; i++)
  3026. synchronize_irq(pci_irq_vector(instance->pdev, i));
  3027. }
  3028. /**
  3029. * megasas_complete_cmd_dpc_fusion - Completes command
  3030. * @instance: Adapter soft state
  3031. *
  3032. * Tasklet to complete cmds
  3033. */
  3034. void
  3035. megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
  3036. {
  3037. struct megasas_instance *instance =
  3038. (struct megasas_instance *)instance_addr;
  3039. unsigned long flags;
  3040. u32 count, MSIxIndex;
  3041. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  3042. /* If we have already declared adapter dead, donot complete cmds */
  3043. spin_lock_irqsave(&instance->hba_lock, flags);
  3044. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  3045. spin_unlock_irqrestore(&instance->hba_lock, flags);
  3046. return;
  3047. }
  3048. spin_unlock_irqrestore(&instance->hba_lock, flags);
  3049. for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
  3050. complete_cmd_fusion(instance, MSIxIndex);
  3051. }
  3052. /**
  3053. * megasas_isr_fusion - isr entry point
  3054. */
  3055. irqreturn_t megasas_isr_fusion(int irq, void *devp)
  3056. {
  3057. struct megasas_irq_context *irq_context = devp;
  3058. struct megasas_instance *instance = irq_context->instance;
  3059. u32 mfiStatus, fw_state, dma_state;
  3060. if (instance->mask_interrupts)
  3061. return IRQ_NONE;
  3062. if (!instance->msix_vectors) {
  3063. mfiStatus = instance->instancet->clear_intr(instance->reg_set);
  3064. if (!mfiStatus)
  3065. return IRQ_NONE;
  3066. }
  3067. /* If we are resetting, bail */
  3068. if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
  3069. instance->instancet->clear_intr(instance->reg_set);
  3070. return IRQ_HANDLED;
  3071. }
  3072. if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
  3073. instance->instancet->clear_intr(instance->reg_set);
  3074. /* If we didn't complete any commands, check for FW fault */
  3075. fw_state = instance->instancet->read_fw_status_reg(
  3076. instance->reg_set) & MFI_STATE_MASK;
  3077. dma_state = instance->instancet->read_fw_status_reg
  3078. (instance->reg_set) & MFI_STATE_DMADONE;
  3079. if (instance->crash_dump_drv_support &&
  3080. instance->crash_dump_app_support) {
  3081. /* Start collecting crash, if DMA bit is done */
  3082. if ((fw_state == MFI_STATE_FAULT) && dma_state)
  3083. schedule_work(&instance->crash_init);
  3084. else if (fw_state == MFI_STATE_FAULT) {
  3085. if (instance->unload == 0)
  3086. schedule_work(&instance->work_init);
  3087. }
  3088. } else if (fw_state == MFI_STATE_FAULT) {
  3089. dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
  3090. "for scsi%d\n", instance->host->host_no);
  3091. if (instance->unload == 0)
  3092. schedule_work(&instance->work_init);
  3093. }
  3094. }
  3095. return IRQ_HANDLED;
  3096. }
  3097. /**
  3098. * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
  3099. * @instance: Adapter soft state
  3100. * mfi_cmd: megasas_cmd pointer
  3101. *
  3102. */
  3103. void
  3104. build_mpt_mfi_pass_thru(struct megasas_instance *instance,
  3105. struct megasas_cmd *mfi_cmd)
  3106. {
  3107. struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
  3108. struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
  3109. struct megasas_cmd_fusion *cmd;
  3110. struct fusion_context *fusion;
  3111. struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
  3112. fusion = instance->ctrl_context;
  3113. cmd = megasas_get_cmd_fusion(instance,
  3114. instance->max_scsi_cmds + mfi_cmd->index);
  3115. /* Save the smid. To be used for returning the cmd */
  3116. mfi_cmd->context.smid = cmd->index;
  3117. /*
  3118. * For cmds where the flag is set, store the flag and check
  3119. * on completion. For cmds with this flag, don't call
  3120. * megasas_complete_cmd
  3121. */
  3122. if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
  3123. mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
  3124. io_req = cmd->io_request;
  3125. if (instance->adapter_type >= INVADER_SERIES) {
  3126. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
  3127. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
  3128. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  3129. sgl_ptr_end->Flags = 0;
  3130. }
  3131. mpi25_ieee_chain =
  3132. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
  3133. io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
  3134. io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
  3135. SGL) / 4;
  3136. io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
  3137. mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
  3138. mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  3139. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
  3140. mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
  3141. }
  3142. /**
  3143. * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
  3144. * @instance: Adapter soft state
  3145. * @cmd: mfi cmd to build
  3146. *
  3147. */
  3148. union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  3149. build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  3150. {
  3151. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
  3152. u16 index;
  3153. build_mpt_mfi_pass_thru(instance, cmd);
  3154. index = cmd->context.smid;
  3155. req_desc = megasas_get_request_descriptor(instance, index - 1);
  3156. req_desc->Words = 0;
  3157. req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  3158. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  3159. req_desc->SCSIIO.SMID = cpu_to_le16(index);
  3160. return req_desc;
  3161. }
  3162. /**
  3163. * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
  3164. * @instance: Adapter soft state
  3165. * @cmd: mfi cmd pointer
  3166. *
  3167. */
  3168. void
  3169. megasas_issue_dcmd_fusion(struct megasas_instance *instance,
  3170. struct megasas_cmd *cmd)
  3171. {
  3172. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  3173. req_desc = build_mpt_cmd(instance, cmd);
  3174. megasas_fire_cmd_fusion(instance, req_desc);
  3175. return;
  3176. }
  3177. /**
  3178. * megasas_release_fusion - Reverses the FW initialization
  3179. * @instance: Adapter soft state
  3180. */
  3181. void
  3182. megasas_release_fusion(struct megasas_instance *instance)
  3183. {
  3184. megasas_free_ioc_init_cmd(instance);
  3185. megasas_free_cmds(instance);
  3186. megasas_free_cmds_fusion(instance);
  3187. iounmap(instance->reg_set);
  3188. pci_release_selected_regions(instance->pdev, 1<<instance->bar);
  3189. }
  3190. /**
  3191. * megasas_read_fw_status_reg_fusion - returns the current FW status value
  3192. * @regs: MFI register set
  3193. */
  3194. static u32
  3195. megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
  3196. {
  3197. return readl(&(regs)->outbound_scratch_pad);
  3198. }
  3199. /**
  3200. * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
  3201. * @instance: Controller's soft instance
  3202. * return: Number of allocated host crash buffers
  3203. */
  3204. static void
  3205. megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
  3206. {
  3207. unsigned int i;
  3208. for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
  3209. instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
  3210. if (!instance->crash_buf[i]) {
  3211. dev_info(&instance->pdev->dev, "Firmware crash dump "
  3212. "memory allocation failed at index %d\n", i);
  3213. break;
  3214. }
  3215. }
  3216. instance->drv_buf_alloc = i;
  3217. }
  3218. /**
  3219. * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
  3220. * @instance: Controller's soft instance
  3221. */
  3222. void
  3223. megasas_free_host_crash_buffer(struct megasas_instance *instance)
  3224. {
  3225. unsigned int i;
  3226. for (i = 0; i < instance->drv_buf_alloc; i++) {
  3227. if (instance->crash_buf[i])
  3228. vfree(instance->crash_buf[i]);
  3229. }
  3230. instance->drv_buf_index = 0;
  3231. instance->drv_buf_alloc = 0;
  3232. instance->fw_crash_state = UNAVAILABLE;
  3233. instance->fw_crash_buffer_size = 0;
  3234. }
  3235. /**
  3236. * megasas_adp_reset_fusion - For controller reset
  3237. * @regs: MFI register set
  3238. */
  3239. static int
  3240. megasas_adp_reset_fusion(struct megasas_instance *instance,
  3241. struct megasas_register_set __iomem *regs)
  3242. {
  3243. u32 host_diag, abs_state, retry;
  3244. /* Now try to reset the chip */
  3245. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  3246. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  3247. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  3248. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  3249. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  3250. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  3251. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  3252. /* Check that the diag write enable (DRWE) bit is on */
  3253. host_diag = readl(&instance->reg_set->fusion_host_diag);
  3254. retry = 0;
  3255. while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
  3256. msleep(100);
  3257. host_diag = readl(&instance->reg_set->fusion_host_diag);
  3258. if (retry++ == 100) {
  3259. dev_warn(&instance->pdev->dev,
  3260. "Host diag unlock failed from %s %d\n",
  3261. __func__, __LINE__);
  3262. break;
  3263. }
  3264. }
  3265. if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
  3266. return -1;
  3267. /* Send chip reset command */
  3268. writel(host_diag | HOST_DIAG_RESET_ADAPTER,
  3269. &instance->reg_set->fusion_host_diag);
  3270. msleep(3000);
  3271. /* Make sure reset adapter bit is cleared */
  3272. host_diag = readl(&instance->reg_set->fusion_host_diag);
  3273. retry = 0;
  3274. while (host_diag & HOST_DIAG_RESET_ADAPTER) {
  3275. msleep(100);
  3276. host_diag = readl(&instance->reg_set->fusion_host_diag);
  3277. if (retry++ == 1000) {
  3278. dev_warn(&instance->pdev->dev,
  3279. "Diag reset adapter never cleared %s %d\n",
  3280. __func__, __LINE__);
  3281. break;
  3282. }
  3283. }
  3284. if (host_diag & HOST_DIAG_RESET_ADAPTER)
  3285. return -1;
  3286. abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
  3287. & MFI_STATE_MASK;
  3288. retry = 0;
  3289. while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
  3290. msleep(100);
  3291. abs_state = instance->instancet->
  3292. read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  3293. }
  3294. if (abs_state <= MFI_STATE_FW_INIT) {
  3295. dev_warn(&instance->pdev->dev,
  3296. "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
  3297. abs_state, __func__, __LINE__);
  3298. return -1;
  3299. }
  3300. return 0;
  3301. }
  3302. /**
  3303. * megasas_check_reset_fusion - For controller reset check
  3304. * @regs: MFI register set
  3305. */
  3306. static int
  3307. megasas_check_reset_fusion(struct megasas_instance *instance,
  3308. struct megasas_register_set __iomem *regs)
  3309. {
  3310. return 0;
  3311. }
  3312. /* This function waits for outstanding commands on fusion to complete */
  3313. int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
  3314. int reason, int *convert)
  3315. {
  3316. int i, outstanding, retval = 0, hb_seconds_missed = 0;
  3317. u32 fw_state;
  3318. for (i = 0; i < resetwaittime; i++) {
  3319. /* Check if firmware is in fault state */
  3320. fw_state = instance->instancet->read_fw_status_reg(
  3321. instance->reg_set) & MFI_STATE_MASK;
  3322. if (fw_state == MFI_STATE_FAULT) {
  3323. dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
  3324. " will reset adapter scsi%d.\n",
  3325. instance->host->host_no);
  3326. megasas_complete_cmd_dpc_fusion((unsigned long)instance);
  3327. if (instance->requestorId && reason) {
  3328. dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
  3329. " state while polling during"
  3330. " I/O timeout handling for %d\n",
  3331. instance->host->host_no);
  3332. *convert = 1;
  3333. }
  3334. retval = 1;
  3335. goto out;
  3336. }
  3337. if (reason == MFI_IO_TIMEOUT_OCR) {
  3338. dev_info(&instance->pdev->dev,
  3339. "MFI IO is timed out, initiating OCR\n");
  3340. megasas_complete_cmd_dpc_fusion((unsigned long)instance);
  3341. retval = 1;
  3342. goto out;
  3343. }
  3344. /* If SR-IOV VF mode & heartbeat timeout, don't wait */
  3345. if (instance->requestorId && !reason) {
  3346. retval = 1;
  3347. goto out;
  3348. }
  3349. /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
  3350. if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
  3351. if (instance->hb_host_mem->HB.fwCounter !=
  3352. instance->hb_host_mem->HB.driverCounter) {
  3353. instance->hb_host_mem->HB.driverCounter =
  3354. instance->hb_host_mem->HB.fwCounter;
  3355. hb_seconds_missed = 0;
  3356. } else {
  3357. hb_seconds_missed++;
  3358. if (hb_seconds_missed ==
  3359. (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
  3360. dev_warn(&instance->pdev->dev, "SR-IOV:"
  3361. " Heartbeat never completed "
  3362. " while polling during I/O "
  3363. " timeout handling for "
  3364. "scsi%d.\n",
  3365. instance->host->host_no);
  3366. *convert = 1;
  3367. retval = 1;
  3368. goto out;
  3369. }
  3370. }
  3371. }
  3372. megasas_complete_cmd_dpc_fusion((unsigned long)instance);
  3373. outstanding = atomic_read(&instance->fw_outstanding);
  3374. if (!outstanding)
  3375. goto out;
  3376. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  3377. dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
  3378. "commands to complete for scsi%d\n", i,
  3379. outstanding, instance->host->host_no);
  3380. }
  3381. msleep(1000);
  3382. }
  3383. if (atomic_read(&instance->fw_outstanding)) {
  3384. dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
  3385. "will reset adapter scsi%d.\n",
  3386. instance->host->host_no);
  3387. *convert = 1;
  3388. retval = 1;
  3389. }
  3390. out:
  3391. return retval;
  3392. }
  3393. void megasas_reset_reply_desc(struct megasas_instance *instance)
  3394. {
  3395. int i, j, count;
  3396. struct fusion_context *fusion;
  3397. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  3398. fusion = instance->ctrl_context;
  3399. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  3400. for (i = 0 ; i < count ; i++) {
  3401. fusion->last_reply_idx[i] = 0;
  3402. reply_desc = fusion->reply_frames_desc[i];
  3403. for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
  3404. reply_desc->Words = cpu_to_le64(ULLONG_MAX);
  3405. }
  3406. }
  3407. /*
  3408. * megasas_refire_mgmt_cmd : Re-fire management commands
  3409. * @instance: Controller's soft instance
  3410. */
  3411. void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
  3412. {
  3413. int j;
  3414. struct megasas_cmd_fusion *cmd_fusion;
  3415. struct fusion_context *fusion;
  3416. struct megasas_cmd *cmd_mfi;
  3417. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  3418. u16 smid;
  3419. bool refire_cmd = 0;
  3420. u8 result;
  3421. u32 opcode = 0;
  3422. fusion = instance->ctrl_context;
  3423. /* Re-fire management commands.
  3424. * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
  3425. */
  3426. for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
  3427. cmd_fusion = fusion->cmd_list[j];
  3428. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  3429. smid = le16_to_cpu(cmd_mfi->context.smid);
  3430. result = REFIRE_CMD;
  3431. if (!smid)
  3432. continue;
  3433. req_desc = megasas_get_request_descriptor(instance, smid - 1);
  3434. switch (cmd_mfi->frame->hdr.cmd) {
  3435. case MFI_CMD_DCMD:
  3436. opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
  3437. /* Do not refire shutdown command */
  3438. if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
  3439. cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
  3440. result = COMPLETE_CMD;
  3441. break;
  3442. }
  3443. refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
  3444. (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
  3445. !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
  3446. if (!refire_cmd)
  3447. result = RETURN_CMD;
  3448. break;
  3449. case MFI_CMD_NVME:
  3450. if (!instance->support_nvme_passthru) {
  3451. cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
  3452. result = COMPLETE_CMD;
  3453. }
  3454. break;
  3455. default:
  3456. break;
  3457. }
  3458. switch (result) {
  3459. case REFIRE_CMD:
  3460. megasas_fire_cmd_fusion(instance, req_desc);
  3461. break;
  3462. case RETURN_CMD:
  3463. megasas_return_cmd(instance, cmd_mfi);
  3464. break;
  3465. case COMPLETE_CMD:
  3466. megasas_complete_cmd(instance, cmd_mfi, DID_OK);
  3467. break;
  3468. }
  3469. }
  3470. }
  3471. /*
  3472. * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
  3473. * @instance: per adapter struct
  3474. * @channel: the channel assigned by the OS
  3475. * @id: the id assigned by the OS
  3476. *
  3477. * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
  3478. */
  3479. static int megasas_track_scsiio(struct megasas_instance *instance,
  3480. int id, int channel)
  3481. {
  3482. int i, found = 0;
  3483. struct megasas_cmd_fusion *cmd_fusion;
  3484. struct fusion_context *fusion;
  3485. fusion = instance->ctrl_context;
  3486. for (i = 0 ; i < instance->max_scsi_cmds; i++) {
  3487. cmd_fusion = fusion->cmd_list[i];
  3488. if (cmd_fusion->scmd &&
  3489. (cmd_fusion->scmd->device->id == id &&
  3490. cmd_fusion->scmd->device->channel == channel)) {
  3491. dev_info(&instance->pdev->dev,
  3492. "SCSI commands pending to target"
  3493. "channel %d id %d \tSMID: 0x%x\n",
  3494. channel, id, cmd_fusion->index);
  3495. scsi_print_command(cmd_fusion->scmd);
  3496. found = 1;
  3497. break;
  3498. }
  3499. }
  3500. return found ? FAILED : SUCCESS;
  3501. }
  3502. /**
  3503. * megasas_tm_response_code - translation of device response code
  3504. * @ioc: per adapter object
  3505. * @mpi_reply: MPI reply returned by firmware
  3506. *
  3507. * Return nothing.
  3508. */
  3509. static void
  3510. megasas_tm_response_code(struct megasas_instance *instance,
  3511. struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
  3512. {
  3513. char *desc;
  3514. switch (mpi_reply->ResponseCode) {
  3515. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  3516. desc = "task management request completed";
  3517. break;
  3518. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  3519. desc = "invalid frame";
  3520. break;
  3521. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  3522. desc = "task management request not supported";
  3523. break;
  3524. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  3525. desc = "task management request failed";
  3526. break;
  3527. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  3528. desc = "task management request succeeded";
  3529. break;
  3530. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  3531. desc = "invalid lun";
  3532. break;
  3533. case 0xA:
  3534. desc = "overlapped tag attempted";
  3535. break;
  3536. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  3537. desc = "task queued, however not sent to target";
  3538. break;
  3539. default:
  3540. desc = "unknown";
  3541. break;
  3542. }
  3543. dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
  3544. mpi_reply->ResponseCode, desc);
  3545. dev_dbg(&instance->pdev->dev,
  3546. "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
  3547. " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
  3548. mpi_reply->TerminationCount, mpi_reply->DevHandle,
  3549. mpi_reply->Function, mpi_reply->TaskType,
  3550. mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
  3551. }
  3552. /**
  3553. * megasas_issue_tm - main routine for sending tm requests
  3554. * @instance: per adapter struct
  3555. * @device_handle: device handle
  3556. * @channel: the channel assigned by the OS
  3557. * @id: the id assigned by the OS
  3558. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
  3559. * @smid_task: smid assigned to the task
  3560. * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
  3561. * Context: user
  3562. *
  3563. * MegaRaid use MPT interface for Task Magement request.
  3564. * A generic API for sending task management requests to firmware.
  3565. *
  3566. * Return SUCCESS or FAILED.
  3567. */
  3568. static int
  3569. megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
  3570. uint channel, uint id, u16 smid_task, u8 type)
  3571. {
  3572. struct MR_TASK_MANAGE_REQUEST *mr_request;
  3573. struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
  3574. unsigned long timeleft;
  3575. struct megasas_cmd_fusion *cmd_fusion;
  3576. struct megasas_cmd *cmd_mfi;
  3577. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  3578. struct fusion_context *fusion = NULL;
  3579. struct megasas_cmd_fusion *scsi_lookup;
  3580. int rc;
  3581. struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
  3582. fusion = instance->ctrl_context;
  3583. cmd_mfi = megasas_get_cmd(instance);
  3584. if (!cmd_mfi) {
  3585. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  3586. __func__, __LINE__);
  3587. return -ENOMEM;
  3588. }
  3589. cmd_fusion = megasas_get_cmd_fusion(instance,
  3590. instance->max_scsi_cmds + cmd_mfi->index);
  3591. /* Save the smid. To be used for returning the cmd */
  3592. cmd_mfi->context.smid = cmd_fusion->index;
  3593. req_desc = megasas_get_request_descriptor(instance,
  3594. (cmd_fusion->index - 1));
  3595. cmd_fusion->request_desc = req_desc;
  3596. req_desc->Words = 0;
  3597. mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
  3598. memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
  3599. mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
  3600. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  3601. mpi_request->DevHandle = cpu_to_le16(device_handle);
  3602. mpi_request->TaskType = type;
  3603. mpi_request->TaskMID = cpu_to_le16(smid_task);
  3604. mpi_request->LUN[1] = 0;
  3605. req_desc = cmd_fusion->request_desc;
  3606. req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
  3607. req_desc->HighPriority.RequestFlags =
  3608. (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
  3609. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  3610. req_desc->HighPriority.MSIxIndex = 0;
  3611. req_desc->HighPriority.LMID = 0;
  3612. req_desc->HighPriority.Reserved1 = 0;
  3613. if (channel < MEGASAS_MAX_PD_CHANNELS)
  3614. mr_request->tmReqFlags.isTMForPD = 1;
  3615. else
  3616. mr_request->tmReqFlags.isTMForLD = 1;
  3617. init_completion(&cmd_fusion->done);
  3618. megasas_fire_cmd_fusion(instance, req_desc);
  3619. timeleft = wait_for_completion_timeout(&cmd_fusion->done, 50 * HZ);
  3620. if (!timeleft) {
  3621. dev_err(&instance->pdev->dev,
  3622. "task mgmt type 0x%x timed out\n", type);
  3623. cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
  3624. mutex_unlock(&instance->reset_mutex);
  3625. rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
  3626. mutex_lock(&instance->reset_mutex);
  3627. return rc;
  3628. }
  3629. mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
  3630. megasas_tm_response_code(instance, mpi_reply);
  3631. megasas_return_cmd(instance, cmd_mfi);
  3632. rc = SUCCESS;
  3633. switch (type) {
  3634. case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
  3635. scsi_lookup = fusion->cmd_list[smid_task - 1];
  3636. if (scsi_lookup->scmd == NULL)
  3637. break;
  3638. else {
  3639. instance->instancet->disable_intr(instance);
  3640. megasas_sync_irqs((unsigned long)instance);
  3641. instance->instancet->enable_intr(instance);
  3642. if (scsi_lookup->scmd == NULL)
  3643. break;
  3644. }
  3645. rc = FAILED;
  3646. break;
  3647. case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
  3648. if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
  3649. break;
  3650. instance->instancet->disable_intr(instance);
  3651. megasas_sync_irqs((unsigned long)instance);
  3652. rc = megasas_track_scsiio(instance, id, channel);
  3653. instance->instancet->enable_intr(instance);
  3654. break;
  3655. case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
  3656. case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
  3657. break;
  3658. default:
  3659. rc = FAILED;
  3660. break;
  3661. }
  3662. return rc;
  3663. }
  3664. /*
  3665. * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
  3666. * @instance: per adapter struct
  3667. *
  3668. * Return Non Zero index, if SMID found in outstanding commands
  3669. */
  3670. static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
  3671. {
  3672. int i, ret = 0;
  3673. struct megasas_instance *instance;
  3674. struct megasas_cmd_fusion *cmd_fusion;
  3675. struct fusion_context *fusion;
  3676. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  3677. fusion = instance->ctrl_context;
  3678. for (i = 0; i < instance->max_scsi_cmds; i++) {
  3679. cmd_fusion = fusion->cmd_list[i];
  3680. if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
  3681. scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
  3682. " SMID: %d\n", cmd_fusion->index);
  3683. ret = cmd_fusion->index;
  3684. break;
  3685. }
  3686. }
  3687. return ret;
  3688. }
  3689. /*
  3690. * megasas_get_tm_devhandle - Get devhandle for TM request
  3691. * @sdev- OS provided scsi device
  3692. *
  3693. * Returns- devhandle/targetID of SCSI device
  3694. */
  3695. static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
  3696. {
  3697. u16 pd_index = 0;
  3698. u32 device_id;
  3699. struct megasas_instance *instance;
  3700. struct fusion_context *fusion;
  3701. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  3702. u16 devhandle = (u16)ULONG_MAX;
  3703. instance = (struct megasas_instance *)sdev->host->hostdata;
  3704. fusion = instance->ctrl_context;
  3705. if (!MEGASAS_IS_LOGICAL(sdev)) {
  3706. if (instance->use_seqnum_jbod_fp) {
  3707. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
  3708. + sdev->id;
  3709. pd_sync = (void *)fusion->pd_seq_sync
  3710. [(instance->pd_seq_map_id - 1) & 1];
  3711. devhandle = pd_sync->seq[pd_index].devHandle;
  3712. } else
  3713. sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
  3714. " without JBOD MAP support from %s %d\n", __func__, __LINE__);
  3715. } else {
  3716. device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
  3717. + sdev->id;
  3718. devhandle = device_id;
  3719. }
  3720. return devhandle;
  3721. }
  3722. /*
  3723. * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
  3724. * @scmd : pointer to scsi command object
  3725. *
  3726. * Return SUCCESS, if command aborted else FAILED
  3727. */
  3728. int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
  3729. {
  3730. struct megasas_instance *instance;
  3731. u16 smid, devhandle;
  3732. struct fusion_context *fusion;
  3733. int ret;
  3734. struct MR_PRIV_DEVICE *mr_device_priv_data;
  3735. mr_device_priv_data = scmd->device->hostdata;
  3736. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  3737. fusion = instance->ctrl_context;
  3738. scmd_printk(KERN_INFO, scmd, "task abort called for scmd(%p)\n", scmd);
  3739. scsi_print_command(scmd);
  3740. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  3741. dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
  3742. "SCSI host:%d\n", instance->host->host_no);
  3743. ret = FAILED;
  3744. return ret;
  3745. }
  3746. if (!mr_device_priv_data) {
  3747. sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
  3748. "scmd(%p)\n", scmd);
  3749. scmd->result = DID_NO_CONNECT << 16;
  3750. ret = SUCCESS;
  3751. goto out;
  3752. }
  3753. if (!mr_device_priv_data->is_tm_capable) {
  3754. ret = FAILED;
  3755. goto out;
  3756. }
  3757. mutex_lock(&instance->reset_mutex);
  3758. smid = megasas_fusion_smid_lookup(scmd);
  3759. if (!smid) {
  3760. ret = SUCCESS;
  3761. scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
  3762. " issued is not found in oustanding commands\n");
  3763. mutex_unlock(&instance->reset_mutex);
  3764. goto out;
  3765. }
  3766. devhandle = megasas_get_tm_devhandle(scmd->device);
  3767. if (devhandle == (u16)ULONG_MAX) {
  3768. ret = SUCCESS;
  3769. sdev_printk(KERN_INFO, scmd->device,
  3770. "task abort issued for invalid devhandle\n");
  3771. mutex_unlock(&instance->reset_mutex);
  3772. goto out;
  3773. }
  3774. sdev_printk(KERN_INFO, scmd->device,
  3775. "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
  3776. scmd, devhandle);
  3777. mr_device_priv_data->tm_busy = 1;
  3778. ret = megasas_issue_tm(instance, devhandle,
  3779. scmd->device->channel, scmd->device->id, smid,
  3780. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK);
  3781. mr_device_priv_data->tm_busy = 0;
  3782. mutex_unlock(&instance->reset_mutex);
  3783. out:
  3784. sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
  3785. ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  3786. return ret;
  3787. }
  3788. /*
  3789. * megasas_reset_target_fusion : target reset function for fusion adapters
  3790. * scmd: SCSI command pointer
  3791. *
  3792. * Returns SUCCESS if all commands associated with target aborted else FAILED
  3793. */
  3794. int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
  3795. {
  3796. struct megasas_instance *instance;
  3797. int ret = FAILED;
  3798. u16 devhandle;
  3799. struct fusion_context *fusion;
  3800. struct MR_PRIV_DEVICE *mr_device_priv_data;
  3801. mr_device_priv_data = scmd->device->hostdata;
  3802. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  3803. fusion = instance->ctrl_context;
  3804. sdev_printk(KERN_INFO, scmd->device,
  3805. "target reset called for scmd(%p)\n", scmd);
  3806. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  3807. dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
  3808. "SCSI host:%d\n", instance->host->host_no);
  3809. ret = FAILED;
  3810. return ret;
  3811. }
  3812. if (!mr_device_priv_data) {
  3813. sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
  3814. "scmd(%p)\n", scmd);
  3815. scmd->result = DID_NO_CONNECT << 16;
  3816. ret = SUCCESS;
  3817. goto out;
  3818. }
  3819. if (!mr_device_priv_data->is_tm_capable) {
  3820. ret = FAILED;
  3821. goto out;
  3822. }
  3823. mutex_lock(&instance->reset_mutex);
  3824. devhandle = megasas_get_tm_devhandle(scmd->device);
  3825. if (devhandle == (u16)ULONG_MAX) {
  3826. ret = SUCCESS;
  3827. sdev_printk(KERN_INFO, scmd->device,
  3828. "target reset issued for invalid devhandle\n");
  3829. mutex_unlock(&instance->reset_mutex);
  3830. goto out;
  3831. }
  3832. sdev_printk(KERN_INFO, scmd->device,
  3833. "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
  3834. scmd, devhandle);
  3835. mr_device_priv_data->tm_busy = 1;
  3836. ret = megasas_issue_tm(instance, devhandle,
  3837. scmd->device->channel, scmd->device->id, 0,
  3838. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
  3839. mr_device_priv_data->tm_busy = 0;
  3840. mutex_unlock(&instance->reset_mutex);
  3841. out:
  3842. scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
  3843. (ret == SUCCESS) ? "SUCCESS" : "FAILED");
  3844. return ret;
  3845. }
  3846. /*SRIOV get other instance in cluster if any*/
  3847. struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
  3848. {
  3849. int i;
  3850. for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
  3851. if (megasas_mgmt_info.instance[i] &&
  3852. (megasas_mgmt_info.instance[i] != instance) &&
  3853. megasas_mgmt_info.instance[i]->requestorId &&
  3854. megasas_mgmt_info.instance[i]->peerIsPresent &&
  3855. (memcmp((megasas_mgmt_info.instance[i]->clusterId),
  3856. instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
  3857. return megasas_mgmt_info.instance[i];
  3858. }
  3859. return NULL;
  3860. }
  3861. /* Check for a second path that is currently UP */
  3862. int megasas_check_mpio_paths(struct megasas_instance *instance,
  3863. struct scsi_cmnd *scmd)
  3864. {
  3865. struct megasas_instance *peer_instance = NULL;
  3866. int retval = (DID_REQUEUE << 16);
  3867. if (instance->peerIsPresent) {
  3868. peer_instance = megasas_get_peer_instance(instance);
  3869. if ((peer_instance) &&
  3870. (atomic_read(&peer_instance->adprecovery) ==
  3871. MEGASAS_HBA_OPERATIONAL))
  3872. retval = (DID_NO_CONNECT << 16);
  3873. }
  3874. return retval;
  3875. }
  3876. /* Core fusion reset function */
  3877. int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
  3878. {
  3879. int retval = SUCCESS, i, j, convert = 0;
  3880. struct megasas_instance *instance;
  3881. struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
  3882. struct fusion_context *fusion;
  3883. u32 abs_state, status_reg, reset_adapter;
  3884. u32 io_timeout_in_crash_mode = 0;
  3885. struct scsi_cmnd *scmd_local = NULL;
  3886. struct scsi_device *sdev;
  3887. instance = (struct megasas_instance *)shost->hostdata;
  3888. fusion = instance->ctrl_context;
  3889. mutex_lock(&instance->reset_mutex);
  3890. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  3891. dev_warn(&instance->pdev->dev, "Hardware critical error, "
  3892. "returning FAILED for scsi%d.\n",
  3893. instance->host->host_no);
  3894. mutex_unlock(&instance->reset_mutex);
  3895. return FAILED;
  3896. }
  3897. status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
  3898. abs_state = status_reg & MFI_STATE_MASK;
  3899. /* IO timeout detected, forcibly put FW in FAULT state */
  3900. if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
  3901. instance->crash_dump_app_support && reason) {
  3902. dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
  3903. "forcibly FAULT Firmware\n");
  3904. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  3905. status_reg = readl(&instance->reg_set->doorbell);
  3906. writel(status_reg | MFI_STATE_FORCE_OCR,
  3907. &instance->reg_set->doorbell);
  3908. readl(&instance->reg_set->doorbell);
  3909. mutex_unlock(&instance->reset_mutex);
  3910. do {
  3911. ssleep(3);
  3912. io_timeout_in_crash_mode++;
  3913. dev_dbg(&instance->pdev->dev, "waiting for [%d] "
  3914. "seconds for crash dump collection and OCR "
  3915. "to be done\n", (io_timeout_in_crash_mode * 3));
  3916. } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
  3917. (io_timeout_in_crash_mode < 80));
  3918. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
  3919. dev_info(&instance->pdev->dev, "OCR done for IO "
  3920. "timeout case\n");
  3921. retval = SUCCESS;
  3922. } else {
  3923. dev_info(&instance->pdev->dev, "Controller is not "
  3924. "operational after 240 seconds wait for IO "
  3925. "timeout case in FW crash dump mode\n do "
  3926. "OCR/kill adapter\n");
  3927. retval = megasas_reset_fusion(shost, 0);
  3928. }
  3929. return retval;
  3930. }
  3931. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  3932. del_timer_sync(&instance->sriov_heartbeat_timer);
  3933. set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  3934. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
  3935. instance->instancet->disable_intr(instance);
  3936. megasas_sync_irqs((unsigned long)instance);
  3937. /* First try waiting for commands to complete */
  3938. if (megasas_wait_for_outstanding_fusion(instance, reason,
  3939. &convert)) {
  3940. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  3941. dev_warn(&instance->pdev->dev, "resetting fusion "
  3942. "adapter scsi%d.\n", instance->host->host_no);
  3943. if (convert)
  3944. reason = 0;
  3945. if (megasas_dbg_lvl & OCR_LOGS)
  3946. dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
  3947. /* Now return commands back to the OS */
  3948. for (i = 0 ; i < instance->max_scsi_cmds; i++) {
  3949. cmd_fusion = fusion->cmd_list[i];
  3950. /*check for extra commands issued by driver*/
  3951. if (instance->adapter_type == VENTURA_SERIES) {
  3952. r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
  3953. megasas_return_cmd_fusion(instance, r1_cmd);
  3954. }
  3955. scmd_local = cmd_fusion->scmd;
  3956. if (cmd_fusion->scmd) {
  3957. if (megasas_dbg_lvl & OCR_LOGS) {
  3958. sdev_printk(KERN_INFO,
  3959. cmd_fusion->scmd->device, "SMID: 0x%x\n",
  3960. cmd_fusion->index);
  3961. scsi_print_command(cmd_fusion->scmd);
  3962. }
  3963. scmd_local->result =
  3964. megasas_check_mpio_paths(instance,
  3965. scmd_local);
  3966. if (instance->ldio_threshold &&
  3967. megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
  3968. atomic_dec(&instance->ldio_outstanding);
  3969. megasas_return_cmd_fusion(instance, cmd_fusion);
  3970. scsi_dma_unmap(scmd_local);
  3971. scmd_local->scsi_done(scmd_local);
  3972. }
  3973. }
  3974. atomic_set(&instance->fw_outstanding, 0);
  3975. status_reg = instance->instancet->read_fw_status_reg(
  3976. instance->reg_set);
  3977. abs_state = status_reg & MFI_STATE_MASK;
  3978. reset_adapter = status_reg & MFI_RESET_ADAPTER;
  3979. if (instance->disableOnlineCtrlReset ||
  3980. (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
  3981. /* Reset not supported, kill adapter */
  3982. dev_warn(&instance->pdev->dev, "Reset not supported"
  3983. ", killing adapter scsi%d.\n",
  3984. instance->host->host_no);
  3985. megaraid_sas_kill_hba(instance);
  3986. instance->skip_heartbeat_timer_del = 1;
  3987. retval = FAILED;
  3988. goto out;
  3989. }
  3990. /* Let SR-IOV VF & PF sync up if there was a HB failure */
  3991. if (instance->requestorId && !reason) {
  3992. msleep(MEGASAS_OCR_SETTLE_TIME_VF);
  3993. goto transition_to_ready;
  3994. }
  3995. /* Now try to reset the chip */
  3996. for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
  3997. if (instance->instancet->adp_reset
  3998. (instance, instance->reg_set))
  3999. continue;
  4000. transition_to_ready:
  4001. /* Wait for FW to become ready */
  4002. if (megasas_transition_to_ready(instance, 1)) {
  4003. dev_warn(&instance->pdev->dev,
  4004. "Failed to transition controller to ready for "
  4005. "scsi%d.\n", instance->host->host_no);
  4006. if (instance->requestorId && !reason)
  4007. goto fail_kill_adapter;
  4008. else
  4009. continue;
  4010. }
  4011. megasas_reset_reply_desc(instance);
  4012. megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
  4013. if (megasas_ioc_init_fusion(instance)) {
  4014. if (instance->requestorId && !reason)
  4015. goto fail_kill_adapter;
  4016. else
  4017. continue;
  4018. }
  4019. if (megasas_get_ctrl_info(instance)) {
  4020. dev_info(&instance->pdev->dev,
  4021. "Failed from %s %d\n",
  4022. __func__, __LINE__);
  4023. megaraid_sas_kill_hba(instance);
  4024. retval = FAILED;
  4025. goto out;
  4026. }
  4027. megasas_refire_mgmt_cmd(instance);
  4028. /* Reset load balance info */
  4029. if (fusion->load_balance_info)
  4030. memset(fusion->load_balance_info, 0,
  4031. (sizeof(struct LD_LOAD_BALANCE_INFO) *
  4032. MAX_LOGICAL_DRIVES_EXT));
  4033. if (!megasas_get_map_info(instance))
  4034. megasas_sync_map_info(instance);
  4035. megasas_setup_jbod_map(instance);
  4036. shost_for_each_device(sdev, shost)
  4037. megasas_set_dynamic_target_properties(sdev);
  4038. /* reset stream detection array */
  4039. if (instance->adapter_type == VENTURA_SERIES) {
  4040. for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
  4041. memset(fusion->stream_detect_by_ld[j],
  4042. 0, sizeof(struct LD_STREAM_DETECT));
  4043. fusion->stream_detect_by_ld[j]->mru_bit_map
  4044. = MR_STREAM_BITMAP;
  4045. }
  4046. }
  4047. clear_bit(MEGASAS_FUSION_IN_RESET,
  4048. &instance->reset_flags);
  4049. instance->instancet->enable_intr(instance);
  4050. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  4051. dev_info(&instance->pdev->dev, "Interrupts are enabled and"
  4052. " controller is OPERATIONAL for scsi:%d\n",
  4053. instance->host->host_no);
  4054. /* Restart SR-IOV heartbeat */
  4055. if (instance->requestorId) {
  4056. if (!megasas_sriov_start_heartbeat(instance, 0))
  4057. megasas_start_timer(instance);
  4058. else
  4059. instance->skip_heartbeat_timer_del = 1;
  4060. }
  4061. if (instance->crash_dump_drv_support &&
  4062. instance->crash_dump_app_support)
  4063. megasas_set_crash_dump_params(instance,
  4064. MR_CRASH_BUF_TURN_ON);
  4065. else
  4066. megasas_set_crash_dump_params(instance,
  4067. MR_CRASH_BUF_TURN_OFF);
  4068. retval = SUCCESS;
  4069. /* Adapter reset completed successfully */
  4070. dev_warn(&instance->pdev->dev,
  4071. "Reset successful for scsi%d.\n",
  4072. instance->host->host_no);
  4073. goto out;
  4074. }
  4075. fail_kill_adapter:
  4076. /* Reset failed, kill the adapter */
  4077. dev_warn(&instance->pdev->dev, "Reset failed, killing "
  4078. "adapter scsi%d.\n", instance->host->host_no);
  4079. megaraid_sas_kill_hba(instance);
  4080. instance->skip_heartbeat_timer_del = 1;
  4081. retval = FAILED;
  4082. } else {
  4083. /* For VF: Restart HB timer if we didn't OCR */
  4084. if (instance->requestorId) {
  4085. megasas_start_timer(instance);
  4086. }
  4087. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  4088. instance->instancet->enable_intr(instance);
  4089. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  4090. }
  4091. out:
  4092. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  4093. mutex_unlock(&instance->reset_mutex);
  4094. return retval;
  4095. }
  4096. /* Fusion Crash dump collection work queue */
  4097. void megasas_fusion_crash_dump_wq(struct work_struct *work)
  4098. {
  4099. struct megasas_instance *instance =
  4100. container_of(work, struct megasas_instance, crash_init);
  4101. u32 status_reg;
  4102. u8 partial_copy = 0;
  4103. status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
  4104. /*
  4105. * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
  4106. * to host crash buffers
  4107. */
  4108. if (instance->drv_buf_index == 0) {
  4109. /* Buffer is already allocated for old Crash dump.
  4110. * Do OCR and do not wait for crash dump collection
  4111. */
  4112. if (instance->drv_buf_alloc) {
  4113. dev_info(&instance->pdev->dev, "earlier crash dump is "
  4114. "not yet copied by application, ignoring this "
  4115. "crash dump and initiating OCR\n");
  4116. status_reg |= MFI_STATE_CRASH_DUMP_DONE;
  4117. writel(status_reg,
  4118. &instance->reg_set->outbound_scratch_pad);
  4119. readl(&instance->reg_set->outbound_scratch_pad);
  4120. return;
  4121. }
  4122. megasas_alloc_host_crash_buffer(instance);
  4123. dev_info(&instance->pdev->dev, "Number of host crash buffers "
  4124. "allocated: %d\n", instance->drv_buf_alloc);
  4125. }
  4126. /*
  4127. * Driver has allocated max buffers, which can be allocated
  4128. * and FW has more crash dump data, then driver will
  4129. * ignore the data.
  4130. */
  4131. if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
  4132. dev_info(&instance->pdev->dev, "Driver is done copying "
  4133. "the buffer: %d\n", instance->drv_buf_alloc);
  4134. status_reg |= MFI_STATE_CRASH_DUMP_DONE;
  4135. partial_copy = 1;
  4136. } else {
  4137. memcpy(instance->crash_buf[instance->drv_buf_index],
  4138. instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
  4139. instance->drv_buf_index++;
  4140. status_reg &= ~MFI_STATE_DMADONE;
  4141. }
  4142. if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
  4143. dev_info(&instance->pdev->dev, "Crash Dump is available,number "
  4144. "of copied buffers: %d\n", instance->drv_buf_index);
  4145. instance->fw_crash_buffer_size = instance->drv_buf_index;
  4146. instance->fw_crash_state = AVAILABLE;
  4147. instance->drv_buf_index = 0;
  4148. writel(status_reg, &instance->reg_set->outbound_scratch_pad);
  4149. readl(&instance->reg_set->outbound_scratch_pad);
  4150. if (!partial_copy)
  4151. megasas_reset_fusion(instance->host, 0);
  4152. } else {
  4153. writel(status_reg, &instance->reg_set->outbound_scratch_pad);
  4154. readl(&instance->reg_set->outbound_scratch_pad);
  4155. }
  4156. }
  4157. /* Fusion OCR work queue */
  4158. void megasas_fusion_ocr_wq(struct work_struct *work)
  4159. {
  4160. struct megasas_instance *instance =
  4161. container_of(work, struct megasas_instance, work_init);
  4162. megasas_reset_fusion(instance->host, 0);
  4163. }
  4164. /* Allocate fusion context */
  4165. int
  4166. megasas_alloc_fusion_context(struct megasas_instance *instance)
  4167. {
  4168. struct fusion_context *fusion;
  4169. instance->ctrl_context = kzalloc(sizeof(struct fusion_context),
  4170. GFP_KERNEL);
  4171. if (!instance->ctrl_context) {
  4172. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  4173. __func__, __LINE__);
  4174. return -ENOMEM;
  4175. }
  4176. fusion = instance->ctrl_context;
  4177. fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
  4178. sizeof(LD_SPAN_INFO));
  4179. fusion->log_to_span =
  4180. (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  4181. fusion->log_to_span_pages);
  4182. if (!fusion->log_to_span) {
  4183. fusion->log_to_span = vzalloc(MAX_LOGICAL_DRIVES_EXT *
  4184. sizeof(LD_SPAN_INFO));
  4185. if (!fusion->log_to_span) {
  4186. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  4187. __func__, __LINE__);
  4188. return -ENOMEM;
  4189. }
  4190. }
  4191. fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
  4192. sizeof(struct LD_LOAD_BALANCE_INFO));
  4193. fusion->load_balance_info =
  4194. (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  4195. fusion->load_balance_info_pages);
  4196. if (!fusion->load_balance_info) {
  4197. fusion->load_balance_info = vzalloc(MAX_LOGICAL_DRIVES_EXT *
  4198. sizeof(struct LD_LOAD_BALANCE_INFO));
  4199. if (!fusion->load_balance_info)
  4200. dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
  4201. "continuing without Load Balance support\n");
  4202. }
  4203. return 0;
  4204. }
  4205. void
  4206. megasas_free_fusion_context(struct megasas_instance *instance)
  4207. {
  4208. struct fusion_context *fusion = instance->ctrl_context;
  4209. if (fusion) {
  4210. if (fusion->load_balance_info) {
  4211. if (is_vmalloc_addr(fusion->load_balance_info))
  4212. vfree(fusion->load_balance_info);
  4213. else
  4214. free_pages((ulong)fusion->load_balance_info,
  4215. fusion->load_balance_info_pages);
  4216. }
  4217. if (fusion->log_to_span) {
  4218. if (is_vmalloc_addr(fusion->log_to_span))
  4219. vfree(fusion->log_to_span);
  4220. else
  4221. free_pages((ulong)fusion->log_to_span,
  4222. fusion->log_to_span_pages);
  4223. }
  4224. kfree(fusion);
  4225. }
  4226. }
  4227. struct megasas_instance_template megasas_instance_template_fusion = {
  4228. .enable_intr = megasas_enable_intr_fusion,
  4229. .disable_intr = megasas_disable_intr_fusion,
  4230. .clear_intr = megasas_clear_intr_fusion,
  4231. .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
  4232. .adp_reset = megasas_adp_reset_fusion,
  4233. .check_reset = megasas_check_reset_fusion,
  4234. .service_isr = megasas_isr_fusion,
  4235. .tasklet = megasas_complete_cmd_dpc_fusion,
  4236. .init_adapter = megasas_init_adapter_fusion,
  4237. .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
  4238. .issue_dcmd = megasas_issue_dcmd_fusion,
  4239. };