megaraid_sas_fusion.c 155 KB

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