megaraid_sas_fusion.c 144 KB

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