megaraid_sas_fusion.c 133 KB

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