mpt2sas_base.c 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655
  1. /*
  2. * This is the Fusion MPT base driver providing common API layer interface
  3. * for access to MPT (Message Passing Technology) firmware.
  4. *
  5. * This code is based on drivers/scsi/mpt2sas/mpt2_base.c
  6. * Copyright (C) 2007-2012 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * NO WARRANTY
  20. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. * solely responsible for determining the appropriateness of using and
  25. * distributing the Program and assumes all risks associated with its
  26. * exercise of rights under this Agreement, including but not limited to
  27. * the risks and costs of program errors, damage to or loss of data,
  28. * programs or equipment, and unavailability or interruption of operations.
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  40. * USA.
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/init.h>
  46. #include <linux/slab.h>
  47. #include <linux/types.h>
  48. #include <linux/pci.h>
  49. #include <linux/kdev_t.h>
  50. #include <linux/blkdev.h>
  51. #include <linux/delay.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/dma-mapping.h>
  54. #include <linux/sort.h>
  55. #include <linux/io.h>
  56. #include <linux/time.h>
  57. #include <linux/kthread.h>
  58. #include <linux/aer.h>
  59. #include "mpt2sas_base.h"
  60. static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
  61. #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
  62. #define MAX_HBA_QUEUE_DEPTH 30000
  63. #define MAX_CHAIN_DEPTH 100000
  64. static int max_queue_depth = -1;
  65. module_param(max_queue_depth, int, 0);
  66. MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
  67. static int max_sgl_entries = -1;
  68. module_param(max_sgl_entries, int, 0);
  69. MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
  70. static int msix_disable = -1;
  71. module_param(msix_disable, int, 0);
  72. MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
  73. static int missing_delay[2] = {-1, -1};
  74. module_param_array(missing_delay, int, NULL, 0);
  75. MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
  76. static int mpt2sas_fwfault_debug;
  77. MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
  78. "and halt firmware - (default=0)");
  79. static int disable_discovery = -1;
  80. module_param(disable_discovery, int, 0);
  81. MODULE_PARM_DESC(disable_discovery, " disable discovery ");
  82. /**
  83. * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
  84. *
  85. */
  86. static int
  87. _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
  88. {
  89. int ret = param_set_int(val, kp);
  90. struct MPT2SAS_ADAPTER *ioc;
  91. if (ret)
  92. return ret;
  93. printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
  94. list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
  95. ioc->fwfault_debug = mpt2sas_fwfault_debug;
  96. return 0;
  97. }
  98. module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
  99. param_get_int, &mpt2sas_fwfault_debug, 0644);
  100. /**
  101. * mpt2sas_remove_dead_ioc_func - kthread context to remove dead ioc
  102. * @arg: input argument, used to derive ioc
  103. *
  104. * Return 0 if controller is removed from pci subsystem.
  105. * Return -1 for other case.
  106. */
  107. static int mpt2sas_remove_dead_ioc_func(void *arg)
  108. {
  109. struct MPT2SAS_ADAPTER *ioc = (struct MPT2SAS_ADAPTER *)arg;
  110. struct pci_dev *pdev;
  111. if ((ioc == NULL))
  112. return -1;
  113. pdev = ioc->pdev;
  114. if ((pdev == NULL))
  115. return -1;
  116. pci_stop_and_remove_bus_device(pdev);
  117. return 0;
  118. }
  119. /**
  120. * _base_fault_reset_work - workq handling ioc fault conditions
  121. * @work: input argument, used to derive ioc
  122. * Context: sleep.
  123. *
  124. * Return nothing.
  125. */
  126. static void
  127. _base_fault_reset_work(struct work_struct *work)
  128. {
  129. struct MPT2SAS_ADAPTER *ioc =
  130. container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
  131. unsigned long flags;
  132. u32 doorbell;
  133. int rc;
  134. struct task_struct *p;
  135. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  136. if (ioc->shost_recovery || ioc->pci_error_recovery)
  137. goto rearm_timer;
  138. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  139. doorbell = mpt2sas_base_get_iocstate(ioc, 0);
  140. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
  141. printk(MPT2SAS_INFO_FMT "%s : SAS host is non-operational !!!!\n",
  142. ioc->name, __func__);
  143. /* It may be possible that EEH recovery can resolve some of
  144. * pci bus failure issues rather removing the dead ioc function
  145. * by considering controller is in a non-operational state. So
  146. * here priority is given to the EEH recovery. If it doesn't
  147. * not resolve this issue, mpt2sas driver will consider this
  148. * controller to non-operational state and remove the dead ioc
  149. * function.
  150. */
  151. if (ioc->non_operational_loop++ < 5) {
  152. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
  153. flags);
  154. goto rearm_timer;
  155. }
  156. /*
  157. * Call _scsih_flush_pending_cmds callback so that we flush all
  158. * pending commands back to OS. This call is required to aovid
  159. * deadlock at block layer. Dead IOC will fail to do diag reset,
  160. * and this call is safe since dead ioc will never return any
  161. * command back from HW.
  162. */
  163. ioc->schedule_dead_ioc_flush_running_cmds(ioc);
  164. /*
  165. * Set remove_host flag early since kernel thread will
  166. * take some time to execute.
  167. */
  168. ioc->remove_host = 1;
  169. /*Remove the Dead Host */
  170. p = kthread_run(mpt2sas_remove_dead_ioc_func, ioc,
  171. "mpt2sas_dead_ioc_%d", ioc->id);
  172. if (IS_ERR(p)) {
  173. printk(MPT2SAS_ERR_FMT
  174. "%s: Running mpt2sas_dead_ioc thread failed !!!!\n",
  175. ioc->name, __func__);
  176. } else {
  177. printk(MPT2SAS_ERR_FMT
  178. "%s: Running mpt2sas_dead_ioc thread success !!!!\n",
  179. ioc->name, __func__);
  180. }
  181. return; /* don't rearm timer */
  182. }
  183. ioc->non_operational_loop = 0;
  184. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  185. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  186. FORCE_BIG_HAMMER);
  187. printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
  188. __func__, (rc == 0) ? "success" : "failed");
  189. doorbell = mpt2sas_base_get_iocstate(ioc, 0);
  190. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  191. mpt2sas_base_fault_info(ioc, doorbell &
  192. MPI2_DOORBELL_DATA_MASK);
  193. }
  194. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  195. rearm_timer:
  196. if (ioc->fault_reset_work_q)
  197. queue_delayed_work(ioc->fault_reset_work_q,
  198. &ioc->fault_reset_work,
  199. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  200. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  201. }
  202. /**
  203. * mpt2sas_base_start_watchdog - start the fault_reset_work_q
  204. * @ioc: per adapter object
  205. * Context: sleep.
  206. *
  207. * Return nothing.
  208. */
  209. void
  210. mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
  211. {
  212. unsigned long flags;
  213. if (ioc->fault_reset_work_q)
  214. return;
  215. /* initialize fault polling */
  216. INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
  217. snprintf(ioc->fault_reset_work_q_name,
  218. sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
  219. ioc->fault_reset_work_q =
  220. create_singlethread_workqueue(ioc->fault_reset_work_q_name);
  221. if (!ioc->fault_reset_work_q) {
  222. printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
  223. ioc->name, __func__, __LINE__);
  224. return;
  225. }
  226. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  227. if (ioc->fault_reset_work_q)
  228. queue_delayed_work(ioc->fault_reset_work_q,
  229. &ioc->fault_reset_work,
  230. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  231. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  232. }
  233. /**
  234. * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
  235. * @ioc: per adapter object
  236. * Context: sleep.
  237. *
  238. * Return nothing.
  239. */
  240. void
  241. mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
  242. {
  243. unsigned long flags;
  244. struct workqueue_struct *wq;
  245. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  246. wq = ioc->fault_reset_work_q;
  247. ioc->fault_reset_work_q = NULL;
  248. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  249. if (wq) {
  250. if (!cancel_delayed_work(&ioc->fault_reset_work))
  251. flush_workqueue(wq);
  252. destroy_workqueue(wq);
  253. }
  254. }
  255. /**
  256. * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
  257. * @ioc: per adapter object
  258. * @fault_code: fault code
  259. *
  260. * Return nothing.
  261. */
  262. void
  263. mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
  264. {
  265. printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
  266. ioc->name, fault_code);
  267. }
  268. /**
  269. * mpt2sas_halt_firmware - halt's mpt controller firmware
  270. * @ioc: per adapter object
  271. *
  272. * For debugging timeout related issues. Writing 0xCOFFEE00
  273. * to the doorbell register will halt controller firmware. With
  274. * the purpose to stop both driver and firmware, the enduser can
  275. * obtain a ring buffer from controller UART.
  276. */
  277. void
  278. mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
  279. {
  280. u32 doorbell;
  281. if (!ioc->fwfault_debug)
  282. return;
  283. dump_stack();
  284. doorbell = readl(&ioc->chip->Doorbell);
  285. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  286. mpt2sas_base_fault_info(ioc , doorbell);
  287. else {
  288. writel(0xC0FFEE00, &ioc->chip->Doorbell);
  289. printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
  290. "timeout\n", ioc->name);
  291. }
  292. panic("panic in %s\n", __func__);
  293. }
  294. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  295. /**
  296. * _base_sas_ioc_info - verbose translation of the ioc status
  297. * @ioc: per adapter object
  298. * @mpi_reply: reply mf payload returned from firmware
  299. * @request_hdr: request mf
  300. *
  301. * Return nothing.
  302. */
  303. static void
  304. _base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
  305. MPI2RequestHeader_t *request_hdr)
  306. {
  307. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  308. MPI2_IOCSTATUS_MASK;
  309. char *desc = NULL;
  310. u16 frame_sz;
  311. char *func_str = NULL;
  312. /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
  313. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  314. request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  315. request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
  316. return;
  317. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  318. return;
  319. switch (ioc_status) {
  320. /****************************************************************************
  321. * Common IOCStatus values for all replies
  322. ****************************************************************************/
  323. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  324. desc = "invalid function";
  325. break;
  326. case MPI2_IOCSTATUS_BUSY:
  327. desc = "busy";
  328. break;
  329. case MPI2_IOCSTATUS_INVALID_SGL:
  330. desc = "invalid sgl";
  331. break;
  332. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  333. desc = "internal error";
  334. break;
  335. case MPI2_IOCSTATUS_INVALID_VPID:
  336. desc = "invalid vpid";
  337. break;
  338. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  339. desc = "insufficient resources";
  340. break;
  341. case MPI2_IOCSTATUS_INVALID_FIELD:
  342. desc = "invalid field";
  343. break;
  344. case MPI2_IOCSTATUS_INVALID_STATE:
  345. desc = "invalid state";
  346. break;
  347. case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
  348. desc = "op state not supported";
  349. break;
  350. /****************************************************************************
  351. * Config IOCStatus values
  352. ****************************************************************************/
  353. case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
  354. desc = "config invalid action";
  355. break;
  356. case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
  357. desc = "config invalid type";
  358. break;
  359. case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
  360. desc = "config invalid page";
  361. break;
  362. case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
  363. desc = "config invalid data";
  364. break;
  365. case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
  366. desc = "config no defaults";
  367. break;
  368. case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
  369. desc = "config cant commit";
  370. break;
  371. /****************************************************************************
  372. * SCSI IO Reply
  373. ****************************************************************************/
  374. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  375. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  376. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  377. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  378. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  379. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  380. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  381. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  382. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  383. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  384. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  385. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  386. break;
  387. /****************************************************************************
  388. * For use by SCSI Initiator and SCSI Target end-to-end data protection
  389. ****************************************************************************/
  390. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  391. desc = "eedp guard error";
  392. break;
  393. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  394. desc = "eedp ref tag error";
  395. break;
  396. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  397. desc = "eedp app tag error";
  398. break;
  399. /****************************************************************************
  400. * SCSI Target values
  401. ****************************************************************************/
  402. case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
  403. desc = "target invalid io index";
  404. break;
  405. case MPI2_IOCSTATUS_TARGET_ABORTED:
  406. desc = "target aborted";
  407. break;
  408. case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
  409. desc = "target no conn retryable";
  410. break;
  411. case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
  412. desc = "target no connection";
  413. break;
  414. case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
  415. desc = "target xfer count mismatch";
  416. break;
  417. case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
  418. desc = "target data offset error";
  419. break;
  420. case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
  421. desc = "target too much write data";
  422. break;
  423. case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
  424. desc = "target iu too short";
  425. break;
  426. case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
  427. desc = "target ack nak timeout";
  428. break;
  429. case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
  430. desc = "target nak received";
  431. break;
  432. /****************************************************************************
  433. * Serial Attached SCSI values
  434. ****************************************************************************/
  435. case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
  436. desc = "smp request failed";
  437. break;
  438. case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
  439. desc = "smp data overrun";
  440. break;
  441. /****************************************************************************
  442. * Diagnostic Buffer Post / Diagnostic Release values
  443. ****************************************************************************/
  444. case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
  445. desc = "diagnostic released";
  446. break;
  447. default:
  448. break;
  449. }
  450. if (!desc)
  451. return;
  452. switch (request_hdr->Function) {
  453. case MPI2_FUNCTION_CONFIG:
  454. frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
  455. func_str = "config_page";
  456. break;
  457. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  458. frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
  459. func_str = "task_mgmt";
  460. break;
  461. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  462. frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
  463. func_str = "sas_iounit_ctl";
  464. break;
  465. case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  466. frame_sz = sizeof(Mpi2SepRequest_t);
  467. func_str = "enclosure";
  468. break;
  469. case MPI2_FUNCTION_IOC_INIT:
  470. frame_sz = sizeof(Mpi2IOCInitRequest_t);
  471. func_str = "ioc_init";
  472. break;
  473. case MPI2_FUNCTION_PORT_ENABLE:
  474. frame_sz = sizeof(Mpi2PortEnableRequest_t);
  475. func_str = "port_enable";
  476. break;
  477. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  478. frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
  479. func_str = "smp_passthru";
  480. break;
  481. default:
  482. frame_sz = 32;
  483. func_str = "unknown";
  484. break;
  485. }
  486. printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
  487. " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
  488. _debug_dump_mf(request_hdr, frame_sz/4);
  489. }
  490. /**
  491. * _base_display_event_data - verbose translation of firmware asyn events
  492. * @ioc: per adapter object
  493. * @mpi_reply: reply mf payload returned from firmware
  494. *
  495. * Return nothing.
  496. */
  497. static void
  498. _base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
  499. Mpi2EventNotificationReply_t *mpi_reply)
  500. {
  501. char *desc = NULL;
  502. u16 event;
  503. if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
  504. return;
  505. event = le16_to_cpu(mpi_reply->Event);
  506. switch (event) {
  507. case MPI2_EVENT_LOG_DATA:
  508. desc = "Log Data";
  509. break;
  510. case MPI2_EVENT_STATE_CHANGE:
  511. desc = "Status Change";
  512. break;
  513. case MPI2_EVENT_HARD_RESET_RECEIVED:
  514. desc = "Hard Reset Received";
  515. break;
  516. case MPI2_EVENT_EVENT_CHANGE:
  517. desc = "Event Change";
  518. break;
  519. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  520. desc = "Device Status Change";
  521. break;
  522. case MPI2_EVENT_IR_OPERATION_STATUS:
  523. if (!ioc->hide_ir_msg)
  524. desc = "IR Operation Status";
  525. break;
  526. case MPI2_EVENT_SAS_DISCOVERY:
  527. {
  528. Mpi2EventDataSasDiscovery_t *event_data =
  529. (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
  530. printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
  531. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  532. "start" : "stop");
  533. if (event_data->DiscoveryStatus)
  534. printk("discovery_status(0x%08x)",
  535. le32_to_cpu(event_data->DiscoveryStatus));
  536. printk("\n");
  537. return;
  538. }
  539. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  540. desc = "SAS Broadcast Primitive";
  541. break;
  542. case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  543. desc = "SAS Init Device Status Change";
  544. break;
  545. case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  546. desc = "SAS Init Table Overflow";
  547. break;
  548. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  549. desc = "SAS Topology Change List";
  550. break;
  551. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  552. desc = "SAS Enclosure Device Status Change";
  553. break;
  554. case MPI2_EVENT_IR_VOLUME:
  555. if (!ioc->hide_ir_msg)
  556. desc = "IR Volume";
  557. break;
  558. case MPI2_EVENT_IR_PHYSICAL_DISK:
  559. if (!ioc->hide_ir_msg)
  560. desc = "IR Physical Disk";
  561. break;
  562. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  563. if (!ioc->hide_ir_msg)
  564. desc = "IR Configuration Change List";
  565. break;
  566. case MPI2_EVENT_LOG_ENTRY_ADDED:
  567. if (!ioc->hide_ir_msg)
  568. desc = "Log Entry Added";
  569. break;
  570. }
  571. if (!desc)
  572. return;
  573. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
  574. }
  575. #endif
  576. /**
  577. * _base_sas_log_info - verbose translation of firmware log info
  578. * @ioc: per adapter object
  579. * @log_info: log info
  580. *
  581. * Return nothing.
  582. */
  583. static void
  584. _base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
  585. {
  586. union loginfo_type {
  587. u32 loginfo;
  588. struct {
  589. u32 subcode:16;
  590. u32 code:8;
  591. u32 originator:4;
  592. u32 bus_type:4;
  593. } dw;
  594. };
  595. union loginfo_type sas_loginfo;
  596. char *originator_str = NULL;
  597. sas_loginfo.loginfo = log_info;
  598. if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  599. return;
  600. /* each nexus loss loginfo */
  601. if (log_info == 0x31170000)
  602. return;
  603. /* eat the loginfos associated with task aborts */
  604. if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
  605. 0x31140000 || log_info == 0x31130000))
  606. return;
  607. switch (sas_loginfo.dw.originator) {
  608. case 0:
  609. originator_str = "IOP";
  610. break;
  611. case 1:
  612. originator_str = "PL";
  613. break;
  614. case 2:
  615. if (!ioc->hide_ir_msg)
  616. originator_str = "IR";
  617. else
  618. originator_str = "WarpDrive";
  619. break;
  620. }
  621. printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
  622. "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
  623. originator_str, sas_loginfo.dw.code,
  624. sas_loginfo.dw.subcode);
  625. }
  626. /**
  627. * _base_display_reply_info -
  628. * @ioc: per adapter object
  629. * @smid: system request message index
  630. * @msix_index: MSIX table index supplied by the OS
  631. * @reply: reply message frame(lower 32bit addr)
  632. *
  633. * Return nothing.
  634. */
  635. static void
  636. _base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  637. u32 reply)
  638. {
  639. MPI2DefaultReply_t *mpi_reply;
  640. u16 ioc_status;
  641. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  642. if (unlikely(!mpi_reply)) {
  643. printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  644. ioc->name, __FILE__, __LINE__, __func__);
  645. return;
  646. }
  647. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  648. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  649. if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
  650. (ioc->logging_level & MPT_DEBUG_REPLY)) {
  651. _base_sas_ioc_info(ioc , mpi_reply,
  652. mpt2sas_base_get_msg_frame(ioc, smid));
  653. }
  654. #endif
  655. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  656. _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
  657. }
  658. /**
  659. * mpt2sas_base_done - base internal command completion routine
  660. * @ioc: per adapter object
  661. * @smid: system request message index
  662. * @msix_index: MSIX table index supplied by the OS
  663. * @reply: reply message frame(lower 32bit addr)
  664. *
  665. * Return 1 meaning mf should be freed from _base_interrupt
  666. * 0 means the mf is freed from this function.
  667. */
  668. u8
  669. mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  670. u32 reply)
  671. {
  672. MPI2DefaultReply_t *mpi_reply;
  673. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  674. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  675. return 1;
  676. if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
  677. return 1;
  678. ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
  679. if (mpi_reply) {
  680. ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
  681. memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  682. }
  683. ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
  684. complete(&ioc->base_cmds.done);
  685. return 1;
  686. }
  687. /**
  688. * _base_async_event - main callback handler for firmware asyn events
  689. * @ioc: per adapter object
  690. * @msix_index: MSIX table index supplied by the OS
  691. * @reply: reply message frame(lower 32bit addr)
  692. *
  693. * Return 1 meaning mf should be freed from _base_interrupt
  694. * 0 means the mf is freed from this function.
  695. */
  696. static u8
  697. _base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
  698. {
  699. Mpi2EventNotificationReply_t *mpi_reply;
  700. Mpi2EventAckRequest_t *ack_request;
  701. u16 smid;
  702. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  703. if (!mpi_reply)
  704. return 1;
  705. if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
  706. return 1;
  707. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  708. _base_display_event_data(ioc, mpi_reply);
  709. #endif
  710. if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
  711. goto out;
  712. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  713. if (!smid) {
  714. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  715. ioc->name, __func__);
  716. goto out;
  717. }
  718. ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
  719. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  720. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  721. ack_request->Event = mpi_reply->Event;
  722. ack_request->EventContext = mpi_reply->EventContext;
  723. ack_request->VF_ID = 0; /* TODO */
  724. ack_request->VP_ID = 0;
  725. mpt2sas_base_put_smid_default(ioc, smid);
  726. out:
  727. /* scsih callback handler */
  728. mpt2sas_scsih_event_callback(ioc, msix_index, reply);
  729. /* ctl callback handler */
  730. mpt2sas_ctl_event_callback(ioc, msix_index, reply);
  731. return 1;
  732. }
  733. /**
  734. * _base_get_cb_idx - obtain the callback index
  735. * @ioc: per adapter object
  736. * @smid: system request message index
  737. *
  738. * Return callback index.
  739. */
  740. static u8
  741. _base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  742. {
  743. int i;
  744. u8 cb_idx;
  745. if (smid < ioc->hi_priority_smid) {
  746. i = smid - 1;
  747. cb_idx = ioc->scsi_lookup[i].cb_idx;
  748. } else if (smid < ioc->internal_smid) {
  749. i = smid - ioc->hi_priority_smid;
  750. cb_idx = ioc->hpr_lookup[i].cb_idx;
  751. } else if (smid <= ioc->hba_queue_depth) {
  752. i = smid - ioc->internal_smid;
  753. cb_idx = ioc->internal_lookup[i].cb_idx;
  754. } else
  755. cb_idx = 0xFF;
  756. return cb_idx;
  757. }
  758. /**
  759. * _base_mask_interrupts - disable interrupts
  760. * @ioc: per adapter object
  761. *
  762. * Disabling ResetIRQ, Reply and Doorbell Interrupts
  763. *
  764. * Return nothing.
  765. */
  766. static void
  767. _base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
  768. {
  769. u32 him_register;
  770. ioc->mask_interrupts = 1;
  771. him_register = readl(&ioc->chip->HostInterruptMask);
  772. him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
  773. writel(him_register, &ioc->chip->HostInterruptMask);
  774. readl(&ioc->chip->HostInterruptMask);
  775. }
  776. /**
  777. * _base_unmask_interrupts - enable interrupts
  778. * @ioc: per adapter object
  779. *
  780. * Enabling only Reply Interrupts
  781. *
  782. * Return nothing.
  783. */
  784. static void
  785. _base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
  786. {
  787. u32 him_register;
  788. him_register = readl(&ioc->chip->HostInterruptMask);
  789. him_register &= ~MPI2_HIM_RIM;
  790. writel(him_register, &ioc->chip->HostInterruptMask);
  791. ioc->mask_interrupts = 0;
  792. }
  793. union reply_descriptor {
  794. u64 word;
  795. struct {
  796. u32 low;
  797. u32 high;
  798. } u;
  799. };
  800. /**
  801. * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
  802. * @irq: irq number (not used)
  803. * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
  804. * @r: pt_regs pointer (not used)
  805. *
  806. * Return IRQ_HANDLE if processed, else IRQ_NONE.
  807. */
  808. static irqreturn_t
  809. _base_interrupt(int irq, void *bus_id)
  810. {
  811. struct adapter_reply_queue *reply_q = bus_id;
  812. union reply_descriptor rd;
  813. u32 completed_cmds;
  814. u8 request_desript_type;
  815. u16 smid;
  816. u8 cb_idx;
  817. u32 reply;
  818. u8 msix_index = reply_q->msix_index;
  819. struct MPT2SAS_ADAPTER *ioc = reply_q->ioc;
  820. Mpi2ReplyDescriptorsUnion_t *rpf;
  821. u8 rc;
  822. if (ioc->mask_interrupts)
  823. return IRQ_NONE;
  824. if (!atomic_add_unless(&reply_q->busy, 1, 1))
  825. return IRQ_NONE;
  826. rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
  827. request_desript_type = rpf->Default.ReplyFlags
  828. & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  829. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
  830. atomic_dec(&reply_q->busy);
  831. return IRQ_NONE;
  832. }
  833. completed_cmds = 0;
  834. cb_idx = 0xFF;
  835. do {
  836. rd.word = le64_to_cpu(rpf->Words);
  837. if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
  838. goto out;
  839. reply = 0;
  840. smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
  841. if (request_desript_type ==
  842. MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
  843. reply = le32_to_cpu
  844. (rpf->AddressReply.ReplyFrameAddress);
  845. if (reply > ioc->reply_dma_max_address ||
  846. reply < ioc->reply_dma_min_address)
  847. reply = 0;
  848. } else if (request_desript_type ==
  849. MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
  850. goto next;
  851. else if (request_desript_type ==
  852. MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
  853. goto next;
  854. if (smid) {
  855. cb_idx = _base_get_cb_idx(ioc, smid);
  856. if ((likely(cb_idx < MPT_MAX_CALLBACKS))
  857. && (likely(mpt_callbacks[cb_idx] != NULL))) {
  858. rc = mpt_callbacks[cb_idx](ioc, smid,
  859. msix_index, reply);
  860. if (reply)
  861. _base_display_reply_info(ioc, smid,
  862. msix_index, reply);
  863. if (rc)
  864. mpt2sas_base_free_smid(ioc, smid);
  865. }
  866. }
  867. if (!smid)
  868. _base_async_event(ioc, msix_index, reply);
  869. /* reply free queue handling */
  870. if (reply) {
  871. ioc->reply_free_host_index =
  872. (ioc->reply_free_host_index ==
  873. (ioc->reply_free_queue_depth - 1)) ?
  874. 0 : ioc->reply_free_host_index + 1;
  875. ioc->reply_free[ioc->reply_free_host_index] =
  876. cpu_to_le32(reply);
  877. wmb();
  878. writel(ioc->reply_free_host_index,
  879. &ioc->chip->ReplyFreeHostIndex);
  880. }
  881. next:
  882. rpf->Words = cpu_to_le64(ULLONG_MAX);
  883. reply_q->reply_post_host_index =
  884. (reply_q->reply_post_host_index ==
  885. (ioc->reply_post_queue_depth - 1)) ? 0 :
  886. reply_q->reply_post_host_index + 1;
  887. request_desript_type =
  888. reply_q->reply_post_free[reply_q->reply_post_host_index].
  889. Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  890. completed_cmds++;
  891. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  892. goto out;
  893. if (!reply_q->reply_post_host_index)
  894. rpf = reply_q->reply_post_free;
  895. else
  896. rpf++;
  897. } while (1);
  898. out:
  899. if (!completed_cmds) {
  900. atomic_dec(&reply_q->busy);
  901. return IRQ_NONE;
  902. }
  903. wmb();
  904. if (ioc->is_warpdrive) {
  905. writel(reply_q->reply_post_host_index,
  906. ioc->reply_post_host_index[msix_index]);
  907. atomic_dec(&reply_q->busy);
  908. return IRQ_HANDLED;
  909. }
  910. writel(reply_q->reply_post_host_index | (msix_index <<
  911. MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
  912. atomic_dec(&reply_q->busy);
  913. return IRQ_HANDLED;
  914. }
  915. /**
  916. * _base_is_controller_msix_enabled - is controller support muli-reply queues
  917. * @ioc: per adapter object
  918. *
  919. */
  920. static inline int
  921. _base_is_controller_msix_enabled(struct MPT2SAS_ADAPTER *ioc)
  922. {
  923. return (ioc->facts.IOCCapabilities &
  924. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
  925. }
  926. /**
  927. * mpt2sas_base_flush_reply_queues - flushing the MSIX reply queues
  928. * @ioc: per adapter object
  929. * Context: ISR conext
  930. *
  931. * Called when a Task Management request has completed. We want
  932. * to flush the other reply queues so all the outstanding IO has been
  933. * completed back to OS before we process the TM completetion.
  934. *
  935. * Return nothing.
  936. */
  937. void
  938. mpt2sas_base_flush_reply_queues(struct MPT2SAS_ADAPTER *ioc)
  939. {
  940. struct adapter_reply_queue *reply_q;
  941. /* If MSIX capability is turned off
  942. * then multi-queues are not enabled
  943. */
  944. if (!_base_is_controller_msix_enabled(ioc))
  945. return;
  946. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  947. if (ioc->shost_recovery)
  948. return;
  949. /* TMs are on msix_index == 0 */
  950. if (reply_q->msix_index == 0)
  951. continue;
  952. _base_interrupt(reply_q->vector, (void *)reply_q);
  953. }
  954. }
  955. /**
  956. * mpt2sas_base_release_callback_handler - clear interrupt callback handler
  957. * @cb_idx: callback index
  958. *
  959. * Return nothing.
  960. */
  961. void
  962. mpt2sas_base_release_callback_handler(u8 cb_idx)
  963. {
  964. mpt_callbacks[cb_idx] = NULL;
  965. }
  966. /**
  967. * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
  968. * @cb_func: callback function
  969. *
  970. * Returns cb_func.
  971. */
  972. u8
  973. mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
  974. {
  975. u8 cb_idx;
  976. for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  977. if (mpt_callbacks[cb_idx] == NULL)
  978. break;
  979. mpt_callbacks[cb_idx] = cb_func;
  980. return cb_idx;
  981. }
  982. /**
  983. * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
  984. *
  985. * Return nothing.
  986. */
  987. void
  988. mpt2sas_base_initialize_callback_handler(void)
  989. {
  990. u8 cb_idx;
  991. for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  992. mpt2sas_base_release_callback_handler(cb_idx);
  993. }
  994. /**
  995. * mpt2sas_base_build_zero_len_sge - build zero length sg entry
  996. * @ioc: per adapter object
  997. * @paddr: virtual address for SGE
  998. *
  999. * Create a zero length scatter gather entry to insure the IOCs hardware has
  1000. * something to use if the target device goes brain dead and tries
  1001. * to send data even when none is asked for.
  1002. *
  1003. * Return nothing.
  1004. */
  1005. void
  1006. mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
  1007. {
  1008. u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
  1009. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
  1010. MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
  1011. MPI2_SGE_FLAGS_SHIFT);
  1012. ioc->base_add_sg_single(paddr, flags_length, -1);
  1013. }
  1014. /**
  1015. * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
  1016. * @paddr: virtual address for SGE
  1017. * @flags_length: SGE flags and data transfer length
  1018. * @dma_addr: Physical address
  1019. *
  1020. * Return nothing.
  1021. */
  1022. static void
  1023. _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1024. {
  1025. Mpi2SGESimple32_t *sgel = paddr;
  1026. flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
  1027. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1028. sgel->FlagsLength = cpu_to_le32(flags_length);
  1029. sgel->Address = cpu_to_le32(dma_addr);
  1030. }
  1031. /**
  1032. * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
  1033. * @paddr: virtual address for SGE
  1034. * @flags_length: SGE flags and data transfer length
  1035. * @dma_addr: Physical address
  1036. *
  1037. * Return nothing.
  1038. */
  1039. static void
  1040. _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1041. {
  1042. Mpi2SGESimple64_t *sgel = paddr;
  1043. flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
  1044. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1045. sgel->FlagsLength = cpu_to_le32(flags_length);
  1046. sgel->Address = cpu_to_le64(dma_addr);
  1047. }
  1048. #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
  1049. /**
  1050. * _base_config_dma_addressing - set dma addressing
  1051. * @ioc: per adapter object
  1052. * @pdev: PCI device struct
  1053. *
  1054. * Returns 0 for success, non-zero for failure.
  1055. */
  1056. static int
  1057. _base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
  1058. {
  1059. struct sysinfo s;
  1060. char *desc = NULL;
  1061. if (sizeof(dma_addr_t) > 4) {
  1062. const uint64_t required_mask =
  1063. dma_get_required_mask(&pdev->dev);
  1064. if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
  1065. DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
  1066. DMA_BIT_MASK(64))) {
  1067. ioc->base_add_sg_single = &_base_add_sg_single_64;
  1068. ioc->sge_size = sizeof(Mpi2SGESimple64_t);
  1069. desc = "64";
  1070. goto out;
  1071. }
  1072. }
  1073. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  1074. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1075. ioc->base_add_sg_single = &_base_add_sg_single_32;
  1076. ioc->sge_size = sizeof(Mpi2SGESimple32_t);
  1077. desc = "32";
  1078. } else
  1079. return -ENODEV;
  1080. out:
  1081. si_meminfo(&s);
  1082. printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
  1083. "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
  1084. return 0;
  1085. }
  1086. /**
  1087. * _base_check_enable_msix - checks MSIX capabable.
  1088. * @ioc: per adapter object
  1089. *
  1090. * Check to see if card is capable of MSIX, and set number
  1091. * of available msix vectors
  1092. */
  1093. static int
  1094. _base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
  1095. {
  1096. int base;
  1097. u16 message_control;
  1098. /* Check whether controller SAS2008 B0 controller,
  1099. if it is SAS2008 B0 controller use IO-APIC instead of MSIX */
  1100. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
  1101. ioc->pdev->revision == 0x01) {
  1102. return -EINVAL;
  1103. }
  1104. base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
  1105. if (!base) {
  1106. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
  1107. "supported\n", ioc->name));
  1108. return -EINVAL;
  1109. }
  1110. /* get msix vector count */
  1111. /* NUMA_IO not supported for older controllers */
  1112. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
  1113. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
  1114. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
  1115. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
  1116. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
  1117. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
  1118. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
  1119. ioc->msix_vector_count = 1;
  1120. else {
  1121. pci_read_config_word(ioc->pdev, base + 2, &message_control);
  1122. ioc->msix_vector_count = (message_control & 0x3FF) + 1;
  1123. }
  1124. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
  1125. "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));
  1126. return 0;
  1127. }
  1128. /**
  1129. * _base_free_irq - free irq
  1130. * @ioc: per adapter object
  1131. *
  1132. * Freeing respective reply_queue from the list.
  1133. */
  1134. static void
  1135. _base_free_irq(struct MPT2SAS_ADAPTER *ioc)
  1136. {
  1137. struct adapter_reply_queue *reply_q, *next;
  1138. if (list_empty(&ioc->reply_queue_list))
  1139. return;
  1140. list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
  1141. list_del(&reply_q->list);
  1142. synchronize_irq(reply_q->vector);
  1143. free_irq(reply_q->vector, reply_q);
  1144. kfree(reply_q);
  1145. }
  1146. }
  1147. /**
  1148. * _base_request_irq - request irq
  1149. * @ioc: per adapter object
  1150. * @index: msix index into vector table
  1151. * @vector: irq vector
  1152. *
  1153. * Inserting respective reply_queue into the list.
  1154. */
  1155. static int
  1156. _base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
  1157. {
  1158. struct adapter_reply_queue *reply_q;
  1159. int r;
  1160. reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
  1161. if (!reply_q) {
  1162. printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
  1163. ioc->name, (int)sizeof(struct adapter_reply_queue));
  1164. return -ENOMEM;
  1165. }
  1166. reply_q->ioc = ioc;
  1167. reply_q->msix_index = index;
  1168. reply_q->vector = vector;
  1169. atomic_set(&reply_q->busy, 0);
  1170. if (ioc->msix_enable)
  1171. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
  1172. MPT2SAS_DRIVER_NAME, ioc->id, index);
  1173. else
  1174. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
  1175. MPT2SAS_DRIVER_NAME, ioc->id);
  1176. r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
  1177. reply_q);
  1178. if (r) {
  1179. printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
  1180. reply_q->name, vector);
  1181. kfree(reply_q);
  1182. return -EBUSY;
  1183. }
  1184. INIT_LIST_HEAD(&reply_q->list);
  1185. list_add_tail(&reply_q->list, &ioc->reply_queue_list);
  1186. return 0;
  1187. }
  1188. /**
  1189. * _base_assign_reply_queues - assigning msix index for each cpu
  1190. * @ioc: per adapter object
  1191. *
  1192. * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
  1193. *
  1194. * It would nice if we could call irq_set_affinity, however it is not
  1195. * an exported symbol
  1196. */
  1197. static void
  1198. _base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
  1199. {
  1200. struct adapter_reply_queue *reply_q;
  1201. int cpu_id;
  1202. int cpu_grouping, loop, grouping, grouping_mod;
  1203. if (!_base_is_controller_msix_enabled(ioc))
  1204. return;
  1205. memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
  1206. /* when there are more cpus than available msix vectors,
  1207. * then group cpus togeather on same irq
  1208. */
  1209. if (ioc->cpu_count > ioc->msix_vector_count) {
  1210. grouping = ioc->cpu_count / ioc->msix_vector_count;
  1211. grouping_mod = ioc->cpu_count % ioc->msix_vector_count;
  1212. if (grouping < 2 || (grouping == 2 && !grouping_mod))
  1213. cpu_grouping = 2;
  1214. else if (grouping < 4 || (grouping == 4 && !grouping_mod))
  1215. cpu_grouping = 4;
  1216. else if (grouping < 8 || (grouping == 8 && !grouping_mod))
  1217. cpu_grouping = 8;
  1218. else
  1219. cpu_grouping = 16;
  1220. } else
  1221. cpu_grouping = 0;
  1222. loop = 0;
  1223. reply_q = list_entry(ioc->reply_queue_list.next,
  1224. struct adapter_reply_queue, list);
  1225. for_each_online_cpu(cpu_id) {
  1226. if (!cpu_grouping) {
  1227. ioc->cpu_msix_table[cpu_id] = reply_q->msix_index;
  1228. reply_q = list_entry(reply_q->list.next,
  1229. struct adapter_reply_queue, list);
  1230. } else {
  1231. if (loop < cpu_grouping) {
  1232. ioc->cpu_msix_table[cpu_id] =
  1233. reply_q->msix_index;
  1234. loop++;
  1235. } else {
  1236. reply_q = list_entry(reply_q->list.next,
  1237. struct adapter_reply_queue, list);
  1238. ioc->cpu_msix_table[cpu_id] =
  1239. reply_q->msix_index;
  1240. loop = 1;
  1241. }
  1242. }
  1243. }
  1244. }
  1245. /**
  1246. * _base_disable_msix - disables msix
  1247. * @ioc: per adapter object
  1248. *
  1249. */
  1250. static void
  1251. _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
  1252. {
  1253. if (ioc->msix_enable) {
  1254. pci_disable_msix(ioc->pdev);
  1255. ioc->msix_enable = 0;
  1256. }
  1257. }
  1258. /**
  1259. * _base_enable_msix - enables msix, failback to io_apic
  1260. * @ioc: per adapter object
  1261. *
  1262. */
  1263. static int
  1264. _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
  1265. {
  1266. struct msix_entry *entries, *a;
  1267. int r;
  1268. int i;
  1269. u8 try_msix = 0;
  1270. INIT_LIST_HEAD(&ioc->reply_queue_list);
  1271. if (msix_disable == -1 || msix_disable == 0)
  1272. try_msix = 1;
  1273. if (!try_msix)
  1274. goto try_ioapic;
  1275. if (_base_check_enable_msix(ioc) != 0)
  1276. goto try_ioapic;
  1277. ioc->reply_queue_count = min_t(int, ioc->cpu_count,
  1278. ioc->msix_vector_count);
  1279. entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
  1280. GFP_KERNEL);
  1281. if (!entries) {
  1282. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
  1283. "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
  1284. __LINE__, __func__));
  1285. goto try_ioapic;
  1286. }
  1287. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
  1288. a->entry = i;
  1289. r = pci_enable_msix(ioc->pdev, entries, ioc->reply_queue_count);
  1290. if (r) {
  1291. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
  1292. "failed (r=%d) !!!\n", ioc->name, r));
  1293. kfree(entries);
  1294. goto try_ioapic;
  1295. }
  1296. ioc->msix_enable = 1;
  1297. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
  1298. r = _base_request_irq(ioc, i, a->vector);
  1299. if (r) {
  1300. _base_free_irq(ioc);
  1301. _base_disable_msix(ioc);
  1302. kfree(entries);
  1303. goto try_ioapic;
  1304. }
  1305. }
  1306. kfree(entries);
  1307. return 0;
  1308. /* failback to io_apic interrupt routing */
  1309. try_ioapic:
  1310. r = _base_request_irq(ioc, 0, ioc->pdev->irq);
  1311. return r;
  1312. }
  1313. /**
  1314. * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
  1315. * @ioc: per adapter object
  1316. *
  1317. * Returns 0 for success, non-zero for failure.
  1318. */
  1319. int
  1320. mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
  1321. {
  1322. struct pci_dev *pdev = ioc->pdev;
  1323. u32 memap_sz;
  1324. u32 pio_sz;
  1325. int i, r = 0;
  1326. u64 pio_chip = 0;
  1327. u64 chip_phys = 0;
  1328. struct adapter_reply_queue *reply_q;
  1329. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
  1330. ioc->name, __func__));
  1331. ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1332. if (pci_enable_device_mem(pdev)) {
  1333. printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
  1334. "failed\n", ioc->name);
  1335. return -ENODEV;
  1336. }
  1337. if (pci_request_selected_regions(pdev, ioc->bars,
  1338. MPT2SAS_DRIVER_NAME)) {
  1339. printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
  1340. "failed\n", ioc->name);
  1341. r = -ENODEV;
  1342. goto out_fail;
  1343. }
  1344. /* AER (Advanced Error Reporting) hooks */
  1345. pci_enable_pcie_error_reporting(pdev);
  1346. pci_set_master(pdev);
  1347. if (_base_config_dma_addressing(ioc, pdev) != 0) {
  1348. printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
  1349. ioc->name, pci_name(pdev));
  1350. r = -ENODEV;
  1351. goto out_fail;
  1352. }
  1353. for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
  1354. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  1355. if (pio_sz)
  1356. continue;
  1357. pio_chip = (u64)pci_resource_start(pdev, i);
  1358. pio_sz = pci_resource_len(pdev, i);
  1359. } else {
  1360. if (memap_sz)
  1361. continue;
  1362. /* verify memory resource is valid before using */
  1363. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  1364. ioc->chip_phys = pci_resource_start(pdev, i);
  1365. chip_phys = (u64)ioc->chip_phys;
  1366. memap_sz = pci_resource_len(pdev, i);
  1367. ioc->chip = ioremap(ioc->chip_phys, memap_sz);
  1368. if (ioc->chip == NULL) {
  1369. printk(MPT2SAS_ERR_FMT "unable to map "
  1370. "adapter memory!\n", ioc->name);
  1371. r = -EINVAL;
  1372. goto out_fail;
  1373. }
  1374. }
  1375. }
  1376. }
  1377. _base_mask_interrupts(ioc);
  1378. r = _base_enable_msix(ioc);
  1379. if (r)
  1380. goto out_fail;
  1381. list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
  1382. printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
  1383. reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
  1384. "IO-APIC enabled"), reply_q->vector);
  1385. printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
  1386. ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
  1387. printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
  1388. ioc->name, (unsigned long long)pio_chip, pio_sz);
  1389. /* Save PCI configuration state for recovery from PCI AER/EEH errors */
  1390. pci_save_state(pdev);
  1391. return 0;
  1392. out_fail:
  1393. if (ioc->chip_phys)
  1394. iounmap(ioc->chip);
  1395. ioc->chip_phys = 0;
  1396. pci_release_selected_regions(ioc->pdev, ioc->bars);
  1397. pci_disable_pcie_error_reporting(pdev);
  1398. pci_disable_device(pdev);
  1399. return r;
  1400. }
  1401. /**
  1402. * mpt2sas_base_get_msg_frame - obtain request mf pointer
  1403. * @ioc: per adapter object
  1404. * @smid: system request message index(smid zero is invalid)
  1405. *
  1406. * Returns virt pointer to message frame.
  1407. */
  1408. void *
  1409. mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1410. {
  1411. return (void *)(ioc->request + (smid * ioc->request_sz));
  1412. }
  1413. /**
  1414. * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
  1415. * @ioc: per adapter object
  1416. * @smid: system request message index
  1417. *
  1418. * Returns virt pointer to sense buffer.
  1419. */
  1420. void *
  1421. mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1422. {
  1423. return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1424. }
  1425. /**
  1426. * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
  1427. * @ioc: per adapter object
  1428. * @smid: system request message index
  1429. *
  1430. * Returns phys pointer to the low 32bit address of the sense buffer.
  1431. */
  1432. __le32
  1433. mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1434. {
  1435. return cpu_to_le32(ioc->sense_dma +
  1436. ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1437. }
  1438. /**
  1439. * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
  1440. * @ioc: per adapter object
  1441. * @phys_addr: lower 32 physical addr of the reply
  1442. *
  1443. * Converts 32bit lower physical addr into a virt address.
  1444. */
  1445. void *
  1446. mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
  1447. {
  1448. if (!phys_addr)
  1449. return NULL;
  1450. return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
  1451. }
  1452. /**
  1453. * mpt2sas_base_get_smid - obtain a free smid from internal queue
  1454. * @ioc: per adapter object
  1455. * @cb_idx: callback index
  1456. *
  1457. * Returns smid (zero is invalid)
  1458. */
  1459. u16
  1460. mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
  1461. {
  1462. unsigned long flags;
  1463. struct request_tracker *request;
  1464. u16 smid;
  1465. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1466. if (list_empty(&ioc->internal_free_list)) {
  1467. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1468. printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
  1469. ioc->name, __func__);
  1470. return 0;
  1471. }
  1472. request = list_entry(ioc->internal_free_list.next,
  1473. struct request_tracker, tracker_list);
  1474. request->cb_idx = cb_idx;
  1475. smid = request->smid;
  1476. list_del(&request->tracker_list);
  1477. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1478. return smid;
  1479. }
  1480. /**
  1481. * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
  1482. * @ioc: per adapter object
  1483. * @cb_idx: callback index
  1484. * @scmd: pointer to scsi command object
  1485. *
  1486. * Returns smid (zero is invalid)
  1487. */
  1488. u16
  1489. mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
  1490. struct scsi_cmnd *scmd)
  1491. {
  1492. unsigned long flags;
  1493. struct scsiio_tracker *request;
  1494. u16 smid;
  1495. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1496. if (list_empty(&ioc->free_list)) {
  1497. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1498. printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
  1499. ioc->name, __func__);
  1500. return 0;
  1501. }
  1502. request = list_entry(ioc->free_list.next,
  1503. struct scsiio_tracker, tracker_list);
  1504. request->scmd = scmd;
  1505. request->cb_idx = cb_idx;
  1506. smid = request->smid;
  1507. list_del(&request->tracker_list);
  1508. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1509. return smid;
  1510. }
  1511. /**
  1512. * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
  1513. * @ioc: per adapter object
  1514. * @cb_idx: callback index
  1515. *
  1516. * Returns smid (zero is invalid)
  1517. */
  1518. u16
  1519. mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
  1520. {
  1521. unsigned long flags;
  1522. struct request_tracker *request;
  1523. u16 smid;
  1524. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1525. if (list_empty(&ioc->hpr_free_list)) {
  1526. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1527. return 0;
  1528. }
  1529. request = list_entry(ioc->hpr_free_list.next,
  1530. struct request_tracker, tracker_list);
  1531. request->cb_idx = cb_idx;
  1532. smid = request->smid;
  1533. list_del(&request->tracker_list);
  1534. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1535. return smid;
  1536. }
  1537. /**
  1538. * mpt2sas_base_free_smid - put smid back on free_list
  1539. * @ioc: per adapter object
  1540. * @smid: system request message index
  1541. *
  1542. * Return nothing.
  1543. */
  1544. void
  1545. mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1546. {
  1547. unsigned long flags;
  1548. int i;
  1549. struct chain_tracker *chain_req, *next;
  1550. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1551. if (smid < ioc->hi_priority_smid) {
  1552. /* scsiio queue */
  1553. i = smid - 1;
  1554. if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
  1555. list_for_each_entry_safe(chain_req, next,
  1556. &ioc->scsi_lookup[i].chain_list, tracker_list) {
  1557. list_del_init(&chain_req->tracker_list);
  1558. list_add_tail(&chain_req->tracker_list,
  1559. &ioc->free_chain_list);
  1560. }
  1561. }
  1562. ioc->scsi_lookup[i].cb_idx = 0xFF;
  1563. ioc->scsi_lookup[i].scmd = NULL;
  1564. ioc->scsi_lookup[i].direct_io = 0;
  1565. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  1566. &ioc->free_list);
  1567. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1568. /*
  1569. * See _wait_for_commands_to_complete() call with regards
  1570. * to this code.
  1571. */
  1572. if (ioc->shost_recovery && ioc->pending_io_count) {
  1573. if (ioc->pending_io_count == 1)
  1574. wake_up(&ioc->reset_wq);
  1575. ioc->pending_io_count--;
  1576. }
  1577. return;
  1578. } else if (smid < ioc->internal_smid) {
  1579. /* hi-priority */
  1580. i = smid - ioc->hi_priority_smid;
  1581. ioc->hpr_lookup[i].cb_idx = 0xFF;
  1582. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  1583. &ioc->hpr_free_list);
  1584. } else if (smid <= ioc->hba_queue_depth) {
  1585. /* internal queue */
  1586. i = smid - ioc->internal_smid;
  1587. ioc->internal_lookup[i].cb_idx = 0xFF;
  1588. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  1589. &ioc->internal_free_list);
  1590. }
  1591. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1592. }
  1593. /**
  1594. * _base_writeq - 64 bit write to MMIO
  1595. * @ioc: per adapter object
  1596. * @b: data payload
  1597. * @addr: address in MMIO space
  1598. * @writeq_lock: spin lock
  1599. *
  1600. * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
  1601. * care of 32 bit environment where its not quarenteed to send the entire word
  1602. * in one transfer.
  1603. */
  1604. #ifndef writeq
  1605. static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
  1606. spinlock_t *writeq_lock)
  1607. {
  1608. unsigned long flags;
  1609. __u64 data_out = cpu_to_le64(b);
  1610. spin_lock_irqsave(writeq_lock, flags);
  1611. writel((u32)(data_out), addr);
  1612. writel((u32)(data_out >> 32), (addr + 4));
  1613. spin_unlock_irqrestore(writeq_lock, flags);
  1614. }
  1615. #else
  1616. static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
  1617. spinlock_t *writeq_lock)
  1618. {
  1619. writeq(cpu_to_le64(b), addr);
  1620. }
  1621. #endif
  1622. static inline u8
  1623. _base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
  1624. {
  1625. return ioc->cpu_msix_table[raw_smp_processor_id()];
  1626. }
  1627. /**
  1628. * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
  1629. * @ioc: per adapter object
  1630. * @smid: system request message index
  1631. * @handle: device handle
  1632. *
  1633. * Return nothing.
  1634. */
  1635. void
  1636. mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
  1637. {
  1638. Mpi2RequestDescriptorUnion_t descriptor;
  1639. u64 *request = (u64 *)&descriptor;
  1640. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  1641. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  1642. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  1643. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  1644. descriptor.SCSIIO.LMID = 0;
  1645. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1646. &ioc->scsi_lookup_lock);
  1647. }
  1648. /**
  1649. * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
  1650. * @ioc: per adapter object
  1651. * @smid: system request message index
  1652. *
  1653. * Return nothing.
  1654. */
  1655. void
  1656. mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1657. {
  1658. Mpi2RequestDescriptorUnion_t descriptor;
  1659. u64 *request = (u64 *)&descriptor;
  1660. descriptor.HighPriority.RequestFlags =
  1661. MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  1662. descriptor.HighPriority.MSIxIndex = 0;
  1663. descriptor.HighPriority.SMID = cpu_to_le16(smid);
  1664. descriptor.HighPriority.LMID = 0;
  1665. descriptor.HighPriority.Reserved1 = 0;
  1666. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1667. &ioc->scsi_lookup_lock);
  1668. }
  1669. /**
  1670. * mpt2sas_base_put_smid_default - Default, primarily used for config pages
  1671. * @ioc: per adapter object
  1672. * @smid: system request message index
  1673. *
  1674. * Return nothing.
  1675. */
  1676. void
  1677. mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1678. {
  1679. Mpi2RequestDescriptorUnion_t descriptor;
  1680. u64 *request = (u64 *)&descriptor;
  1681. descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  1682. descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
  1683. descriptor.Default.SMID = cpu_to_le16(smid);
  1684. descriptor.Default.LMID = 0;
  1685. descriptor.Default.DescriptorTypeDependent = 0;
  1686. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1687. &ioc->scsi_lookup_lock);
  1688. }
  1689. /**
  1690. * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
  1691. * @ioc: per adapter object
  1692. * @smid: system request message index
  1693. * @io_index: value used to track the IO
  1694. *
  1695. * Return nothing.
  1696. */
  1697. void
  1698. mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  1699. u16 io_index)
  1700. {
  1701. Mpi2RequestDescriptorUnion_t descriptor;
  1702. u64 *request = (u64 *)&descriptor;
  1703. descriptor.SCSITarget.RequestFlags =
  1704. MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
  1705. descriptor.SCSITarget.MSIxIndex = _base_get_msix_index(ioc);
  1706. descriptor.SCSITarget.SMID = cpu_to_le16(smid);
  1707. descriptor.SCSITarget.LMID = 0;
  1708. descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
  1709. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1710. &ioc->scsi_lookup_lock);
  1711. }
  1712. /**
  1713. * _base_display_dell_branding - Disply branding string
  1714. * @ioc: per adapter object
  1715. *
  1716. * Return nothing.
  1717. */
  1718. static void
  1719. _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
  1720. {
  1721. char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
  1722. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
  1723. return;
  1724. memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
  1725. switch (ioc->pdev->subsystem_device) {
  1726. case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
  1727. strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
  1728. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1729. break;
  1730. case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
  1731. strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
  1732. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1733. break;
  1734. case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
  1735. strncpy(dell_branding,
  1736. MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
  1737. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1738. break;
  1739. case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
  1740. strncpy(dell_branding,
  1741. MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
  1742. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1743. break;
  1744. case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
  1745. strncpy(dell_branding,
  1746. MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
  1747. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1748. break;
  1749. case MPT2SAS_DELL_PERC_H200_SSDID:
  1750. strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
  1751. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1752. break;
  1753. case MPT2SAS_DELL_6GBPS_SAS_SSDID:
  1754. strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
  1755. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1756. break;
  1757. default:
  1758. sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
  1759. break;
  1760. }
  1761. printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
  1762. " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
  1763. ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
  1764. ioc->pdev->subsystem_device);
  1765. }
  1766. /**
  1767. * _base_display_intel_branding - Display branding string
  1768. * @ioc: per adapter object
  1769. *
  1770. * Return nothing.
  1771. */
  1772. static void
  1773. _base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
  1774. {
  1775. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  1776. return;
  1777. switch (ioc->pdev->device) {
  1778. case MPI2_MFGPAGE_DEVID_SAS2008:
  1779. switch (ioc->pdev->subsystem_device) {
  1780. case MPT2SAS_INTEL_RMS2LL080_SSDID:
  1781. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1782. MPT2SAS_INTEL_RMS2LL080_BRANDING);
  1783. break;
  1784. case MPT2SAS_INTEL_RMS2LL040_SSDID:
  1785. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1786. MPT2SAS_INTEL_RMS2LL040_BRANDING);
  1787. break;
  1788. case MPT2SAS_INTEL_SSD910_SSDID:
  1789. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1790. MPT2SAS_INTEL_SSD910_BRANDING);
  1791. break;
  1792. default:
  1793. break;
  1794. }
  1795. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  1796. switch (ioc->pdev->subsystem_device) {
  1797. case MPT2SAS_INTEL_RS25GB008_SSDID:
  1798. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1799. MPT2SAS_INTEL_RS25GB008_BRANDING);
  1800. break;
  1801. case MPT2SAS_INTEL_RMS25JB080_SSDID:
  1802. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1803. MPT2SAS_INTEL_RMS25JB080_BRANDING);
  1804. break;
  1805. case MPT2SAS_INTEL_RMS25JB040_SSDID:
  1806. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1807. MPT2SAS_INTEL_RMS25JB040_BRANDING);
  1808. break;
  1809. case MPT2SAS_INTEL_RMS25KB080_SSDID:
  1810. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1811. MPT2SAS_INTEL_RMS25KB080_BRANDING);
  1812. break;
  1813. case MPT2SAS_INTEL_RMS25KB040_SSDID:
  1814. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1815. MPT2SAS_INTEL_RMS25KB040_BRANDING);
  1816. break;
  1817. default:
  1818. break;
  1819. }
  1820. default:
  1821. break;
  1822. }
  1823. }
  1824. /**
  1825. * _base_display_hp_branding - Display branding string
  1826. * @ioc: per adapter object
  1827. *
  1828. * Return nothing.
  1829. */
  1830. static void
  1831. _base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
  1832. {
  1833. if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
  1834. return;
  1835. switch (ioc->pdev->device) {
  1836. case MPI2_MFGPAGE_DEVID_SAS2004:
  1837. switch (ioc->pdev->subsystem_device) {
  1838. case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
  1839. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1840. MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
  1841. break;
  1842. default:
  1843. break;
  1844. }
  1845. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  1846. switch (ioc->pdev->subsystem_device) {
  1847. case MPT2SAS_HP_2_4_INTERNAL_SSDID:
  1848. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1849. MPT2SAS_HP_2_4_INTERNAL_BRANDING);
  1850. break;
  1851. case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
  1852. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1853. MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
  1854. break;
  1855. case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
  1856. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1857. MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
  1858. break;
  1859. case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
  1860. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1861. MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
  1862. break;
  1863. default:
  1864. break;
  1865. }
  1866. default:
  1867. break;
  1868. }
  1869. }
  1870. /**
  1871. * _base_display_ioc_capabilities - Disply IOC's capabilities.
  1872. * @ioc: per adapter object
  1873. *
  1874. * Return nothing.
  1875. */
  1876. static void
  1877. _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
  1878. {
  1879. int i = 0;
  1880. char desc[16];
  1881. u32 iounit_pg1_flags;
  1882. u32 bios_version;
  1883. bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  1884. strncpy(desc, ioc->manu_pg0.ChipName, 16);
  1885. printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
  1886. "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
  1887. ioc->name, desc,
  1888. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  1889. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  1890. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  1891. ioc->facts.FWVersion.Word & 0x000000FF,
  1892. ioc->pdev->revision,
  1893. (bios_version & 0xFF000000) >> 24,
  1894. (bios_version & 0x00FF0000) >> 16,
  1895. (bios_version & 0x0000FF00) >> 8,
  1896. bios_version & 0x000000FF);
  1897. _base_display_dell_branding(ioc);
  1898. _base_display_intel_branding(ioc);
  1899. _base_display_hp_branding(ioc);
  1900. printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
  1901. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
  1902. printk("Initiator");
  1903. i++;
  1904. }
  1905. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
  1906. printk("%sTarget", i ? "," : "");
  1907. i++;
  1908. }
  1909. i = 0;
  1910. printk("), ");
  1911. printk("Capabilities=(");
  1912. if (!ioc->hide_ir_msg) {
  1913. if (ioc->facts.IOCCapabilities &
  1914. MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
  1915. printk("Raid");
  1916. i++;
  1917. }
  1918. }
  1919. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
  1920. printk("%sTLR", i ? "," : "");
  1921. i++;
  1922. }
  1923. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
  1924. printk("%sMulticast", i ? "," : "");
  1925. i++;
  1926. }
  1927. if (ioc->facts.IOCCapabilities &
  1928. MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
  1929. printk("%sBIDI Target", i ? "," : "");
  1930. i++;
  1931. }
  1932. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
  1933. printk("%sEEDP", i ? "," : "");
  1934. i++;
  1935. }
  1936. if (ioc->facts.IOCCapabilities &
  1937. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
  1938. printk("%sSnapshot Buffer", i ? "," : "");
  1939. i++;
  1940. }
  1941. if (ioc->facts.IOCCapabilities &
  1942. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
  1943. printk("%sDiag Trace Buffer", i ? "," : "");
  1944. i++;
  1945. }
  1946. if (ioc->facts.IOCCapabilities &
  1947. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
  1948. printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
  1949. i++;
  1950. }
  1951. if (ioc->facts.IOCCapabilities &
  1952. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
  1953. printk("%sTask Set Full", i ? "," : "");
  1954. i++;
  1955. }
  1956. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  1957. if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
  1958. printk("%sNCQ", i ? "," : "");
  1959. i++;
  1960. }
  1961. printk(")\n");
  1962. }
  1963. /**
  1964. * _base_update_missing_delay - change the missing delay timers
  1965. * @ioc: per adapter object
  1966. * @device_missing_delay: amount of time till device is reported missing
  1967. * @io_missing_delay: interval IO is returned when there is a missing device
  1968. *
  1969. * Return nothing.
  1970. *
  1971. * Passed on the command line, this function will modify the device missing
  1972. * delay, as well as the io missing delay. This should be called at driver
  1973. * load time.
  1974. */
  1975. static void
  1976. _base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
  1977. u16 device_missing_delay, u8 io_missing_delay)
  1978. {
  1979. u16 dmd, dmd_new, dmd_orignal;
  1980. u8 io_missing_delay_original;
  1981. u16 sz;
  1982. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1983. Mpi2ConfigReply_t mpi_reply;
  1984. u8 num_phys = 0;
  1985. u16 ioc_status;
  1986. mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
  1987. if (!num_phys)
  1988. return;
  1989. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
  1990. sizeof(Mpi2SasIOUnit1PhyData_t));
  1991. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1992. if (!sas_iounit_pg1) {
  1993. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1994. ioc->name, __FILE__, __LINE__, __func__);
  1995. goto out;
  1996. }
  1997. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1998. sas_iounit_pg1, sz))) {
  1999. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2000. ioc->name, __FILE__, __LINE__, __func__);
  2001. goto out;
  2002. }
  2003. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2004. MPI2_IOCSTATUS_MASK;
  2005. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2006. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2007. ioc->name, __FILE__, __LINE__, __func__);
  2008. goto out;
  2009. }
  2010. /* device missing delay */
  2011. dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
  2012. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2013. dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2014. else
  2015. dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2016. dmd_orignal = dmd;
  2017. if (device_missing_delay > 0x7F) {
  2018. dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
  2019. device_missing_delay;
  2020. dmd = dmd / 16;
  2021. dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
  2022. } else
  2023. dmd = device_missing_delay;
  2024. sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
  2025. /* io missing delay */
  2026. io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
  2027. sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
  2028. if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  2029. sz)) {
  2030. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2031. dmd_new = (dmd &
  2032. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2033. else
  2034. dmd_new =
  2035. dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2036. printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
  2037. "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
  2038. printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
  2039. "new(%d)\n", ioc->name, io_missing_delay_original,
  2040. io_missing_delay);
  2041. ioc->device_missing_delay = dmd_new;
  2042. ioc->io_missing_delay = io_missing_delay;
  2043. }
  2044. out:
  2045. kfree(sas_iounit_pg1);
  2046. }
  2047. /**
  2048. * _base_static_config_pages - static start of day config pages
  2049. * @ioc: per adapter object
  2050. *
  2051. * Return nothing.
  2052. */
  2053. static void
  2054. _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
  2055. {
  2056. Mpi2ConfigReply_t mpi_reply;
  2057. u32 iounit_pg1_flags;
  2058. mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
  2059. if (ioc->ir_firmware)
  2060. mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
  2061. &ioc->manu_pg10);
  2062. mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
  2063. mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
  2064. mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
  2065. mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
  2066. mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2067. _base_display_ioc_capabilities(ioc);
  2068. /*
  2069. * Enable task_set_full handling in iounit_pg1 when the
  2070. * facts capabilities indicate that its supported.
  2071. */
  2072. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  2073. if ((ioc->facts.IOCCapabilities &
  2074. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
  2075. iounit_pg1_flags &=
  2076. ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2077. else
  2078. iounit_pg1_flags |=
  2079. MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2080. ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
  2081. mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2082. }
  2083. /**
  2084. * _base_release_memory_pools - release memory
  2085. * @ioc: per adapter object
  2086. *
  2087. * Free memory allocated from _base_allocate_memory_pools.
  2088. *
  2089. * Return nothing.
  2090. */
  2091. static void
  2092. _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
  2093. {
  2094. int i;
  2095. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2096. __func__));
  2097. if (ioc->request) {
  2098. pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
  2099. ioc->request, ioc->request_dma);
  2100. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
  2101. ": free\n", ioc->name, ioc->request));
  2102. ioc->request = NULL;
  2103. }
  2104. if (ioc->sense) {
  2105. pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  2106. if (ioc->sense_dma_pool)
  2107. pci_pool_destroy(ioc->sense_dma_pool);
  2108. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
  2109. ": free\n", ioc->name, ioc->sense));
  2110. ioc->sense = NULL;
  2111. }
  2112. if (ioc->reply) {
  2113. pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
  2114. if (ioc->reply_dma_pool)
  2115. pci_pool_destroy(ioc->reply_dma_pool);
  2116. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
  2117. ": free\n", ioc->name, ioc->reply));
  2118. ioc->reply = NULL;
  2119. }
  2120. if (ioc->reply_free) {
  2121. pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
  2122. ioc->reply_free_dma);
  2123. if (ioc->reply_free_dma_pool)
  2124. pci_pool_destroy(ioc->reply_free_dma_pool);
  2125. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
  2126. "(0x%p): free\n", ioc->name, ioc->reply_free));
  2127. ioc->reply_free = NULL;
  2128. }
  2129. if (ioc->reply_post_free) {
  2130. pci_pool_free(ioc->reply_post_free_dma_pool,
  2131. ioc->reply_post_free, ioc->reply_post_free_dma);
  2132. if (ioc->reply_post_free_dma_pool)
  2133. pci_pool_destroy(ioc->reply_post_free_dma_pool);
  2134. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
  2135. "reply_post_free_pool(0x%p): free\n", ioc->name,
  2136. ioc->reply_post_free));
  2137. ioc->reply_post_free = NULL;
  2138. }
  2139. if (ioc->config_page) {
  2140. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
  2141. "config_page(0x%p): free\n", ioc->name,
  2142. ioc->config_page));
  2143. pci_free_consistent(ioc->pdev, ioc->config_page_sz,
  2144. ioc->config_page, ioc->config_page_dma);
  2145. }
  2146. if (ioc->scsi_lookup) {
  2147. free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
  2148. ioc->scsi_lookup = NULL;
  2149. }
  2150. kfree(ioc->hpr_lookup);
  2151. kfree(ioc->internal_lookup);
  2152. if (ioc->chain_lookup) {
  2153. for (i = 0; i < ioc->chain_depth; i++) {
  2154. if (ioc->chain_lookup[i].chain_buffer)
  2155. pci_pool_free(ioc->chain_dma_pool,
  2156. ioc->chain_lookup[i].chain_buffer,
  2157. ioc->chain_lookup[i].chain_buffer_dma);
  2158. }
  2159. if (ioc->chain_dma_pool)
  2160. pci_pool_destroy(ioc->chain_dma_pool);
  2161. free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
  2162. ioc->chain_lookup = NULL;
  2163. }
  2164. }
  2165. /**
  2166. * _base_allocate_memory_pools - allocate start of day memory pools
  2167. * @ioc: per adapter object
  2168. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2169. *
  2170. * Returns 0 success, anything else error
  2171. */
  2172. static int
  2173. _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2174. {
  2175. struct mpt2sas_facts *facts;
  2176. u16 max_sge_elements;
  2177. u16 chains_needed_per_io;
  2178. u32 sz, total_sz, reply_post_free_sz;
  2179. u32 retry_sz;
  2180. u16 max_request_credit;
  2181. int i;
  2182. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2183. __func__));
  2184. retry_sz = 0;
  2185. facts = &ioc->facts;
  2186. /* command line tunables for max sgl entries */
  2187. if (max_sgl_entries != -1) {
  2188. ioc->shost->sg_tablesize = (max_sgl_entries <
  2189. MPT2SAS_SG_DEPTH) ? max_sgl_entries :
  2190. MPT2SAS_SG_DEPTH;
  2191. } else {
  2192. ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
  2193. }
  2194. /* command line tunables for max controller queue depth */
  2195. if (max_queue_depth != -1 && max_queue_depth != 0) {
  2196. max_request_credit = min_t(u16, max_queue_depth +
  2197. ioc->hi_priority_depth + ioc->internal_depth,
  2198. facts->RequestCredit);
  2199. if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
  2200. max_request_credit = MAX_HBA_QUEUE_DEPTH;
  2201. } else
  2202. max_request_credit = min_t(u16, facts->RequestCredit,
  2203. MAX_HBA_QUEUE_DEPTH);
  2204. ioc->hba_queue_depth = max_request_credit;
  2205. ioc->hi_priority_depth = facts->HighPriorityCredit;
  2206. ioc->internal_depth = ioc->hi_priority_depth + 5;
  2207. /* request frame size */
  2208. ioc->request_sz = facts->IOCRequestFrameSize * 4;
  2209. /* reply frame size */
  2210. ioc->reply_sz = facts->ReplyFrameSize * 4;
  2211. retry_allocation:
  2212. total_sz = 0;
  2213. /* calculate number of sg elements left over in the 1st frame */
  2214. max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
  2215. sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
  2216. ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
  2217. /* now do the same for a chain buffer */
  2218. max_sge_elements = ioc->request_sz - ioc->sge_size;
  2219. ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
  2220. ioc->chain_offset_value_for_main_message =
  2221. ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
  2222. (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
  2223. /*
  2224. * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
  2225. */
  2226. chains_needed_per_io = ((ioc->shost->sg_tablesize -
  2227. ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
  2228. + 1;
  2229. if (chains_needed_per_io > facts->MaxChainDepth) {
  2230. chains_needed_per_io = facts->MaxChainDepth;
  2231. ioc->shost->sg_tablesize = min_t(u16,
  2232. ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
  2233. * chains_needed_per_io), ioc->shost->sg_tablesize);
  2234. }
  2235. ioc->chains_needed_per_io = chains_needed_per_io;
  2236. /* reply free queue sizing - taking into account for 64 FW events */
  2237. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  2238. /* align the reply post queue on the next 16 count boundary */
  2239. if (!ioc->reply_free_queue_depth % 16)
  2240. ioc->reply_post_queue_depth = ioc->reply_free_queue_depth + 16;
  2241. else
  2242. ioc->reply_post_queue_depth = ioc->reply_free_queue_depth +
  2243. 32 - (ioc->reply_free_queue_depth % 16);
  2244. if (ioc->reply_post_queue_depth >
  2245. facts->MaxReplyDescriptorPostQueueDepth) {
  2246. ioc->reply_post_queue_depth = min_t(u16,
  2247. (facts->MaxReplyDescriptorPostQueueDepth -
  2248. (facts->MaxReplyDescriptorPostQueueDepth % 16)),
  2249. (ioc->hba_queue_depth - (ioc->hba_queue_depth % 16)));
  2250. ioc->reply_free_queue_depth = ioc->reply_post_queue_depth - 16;
  2251. ioc->hba_queue_depth = ioc->reply_free_queue_depth - 64;
  2252. }
  2253. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
  2254. "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
  2255. "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
  2256. ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
  2257. ioc->chains_needed_per_io));
  2258. ioc->scsiio_depth = ioc->hba_queue_depth -
  2259. ioc->hi_priority_depth - ioc->internal_depth;
  2260. /* set the scsi host can_queue depth
  2261. * with some internal commands that could be outstanding
  2262. */
  2263. ioc->shost->can_queue = ioc->scsiio_depth;
  2264. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
  2265. "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
  2266. /* contiguous pool for request and chains, 16 byte align, one extra "
  2267. * "frame for smid=0
  2268. */
  2269. ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
  2270. sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
  2271. /* hi-priority queue */
  2272. sz += (ioc->hi_priority_depth * ioc->request_sz);
  2273. /* internal queue */
  2274. sz += (ioc->internal_depth * ioc->request_sz);
  2275. ioc->request_dma_sz = sz;
  2276. ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
  2277. if (!ioc->request) {
  2278. printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
  2279. "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2280. "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
  2281. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2282. if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
  2283. goto out;
  2284. retry_sz += 64;
  2285. ioc->hba_queue_depth = max_request_credit - retry_sz;
  2286. goto retry_allocation;
  2287. }
  2288. if (retry_sz)
  2289. printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
  2290. "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2291. "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
  2292. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2293. /* hi-priority queue */
  2294. ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
  2295. ioc->request_sz);
  2296. ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
  2297. ioc->request_sz);
  2298. /* internal queue */
  2299. ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
  2300. ioc->request_sz);
  2301. ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
  2302. ioc->request_sz);
  2303. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
  2304. "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
  2305. ioc->request, ioc->hba_queue_depth, ioc->request_sz,
  2306. (ioc->hba_queue_depth * ioc->request_sz)/1024));
  2307. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
  2308. ioc->name, (unsigned long long) ioc->request_dma));
  2309. total_sz += sz;
  2310. sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
  2311. ioc->scsi_lookup_pages = get_order(sz);
  2312. ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
  2313. GFP_KERNEL, ioc->scsi_lookup_pages);
  2314. if (!ioc->scsi_lookup) {
  2315. printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
  2316. "sz(%d)\n", ioc->name, (int)sz);
  2317. goto out;
  2318. }
  2319. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
  2320. "depth(%d)\n", ioc->name, ioc->request,
  2321. ioc->scsiio_depth));
  2322. ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
  2323. sz = ioc->chain_depth * sizeof(struct chain_tracker);
  2324. ioc->chain_pages = get_order(sz);
  2325. ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
  2326. GFP_KERNEL, ioc->chain_pages);
  2327. if (!ioc->chain_lookup) {
  2328. printk(MPT2SAS_ERR_FMT "chain_lookup: get_free_pages failed, "
  2329. "sz(%d)\n", ioc->name, (int)sz);
  2330. goto out;
  2331. }
  2332. ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
  2333. ioc->request_sz, 16, 0);
  2334. if (!ioc->chain_dma_pool) {
  2335. printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
  2336. "failed\n", ioc->name);
  2337. goto out;
  2338. }
  2339. for (i = 0; i < ioc->chain_depth; i++) {
  2340. ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
  2341. ioc->chain_dma_pool , GFP_KERNEL,
  2342. &ioc->chain_lookup[i].chain_buffer_dma);
  2343. if (!ioc->chain_lookup[i].chain_buffer) {
  2344. ioc->chain_depth = i;
  2345. goto chain_done;
  2346. }
  2347. total_sz += ioc->request_sz;
  2348. }
  2349. chain_done:
  2350. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
  2351. "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
  2352. ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
  2353. ioc->request_sz))/1024));
  2354. /* initialize hi-priority queue smid's */
  2355. ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
  2356. sizeof(struct request_tracker), GFP_KERNEL);
  2357. if (!ioc->hpr_lookup) {
  2358. printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
  2359. ioc->name);
  2360. goto out;
  2361. }
  2362. ioc->hi_priority_smid = ioc->scsiio_depth + 1;
  2363. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
  2364. "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
  2365. ioc->hi_priority_depth, ioc->hi_priority_smid));
  2366. /* initialize internal queue smid's */
  2367. ioc->internal_lookup = kcalloc(ioc->internal_depth,
  2368. sizeof(struct request_tracker), GFP_KERNEL);
  2369. if (!ioc->internal_lookup) {
  2370. printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
  2371. ioc->name);
  2372. goto out;
  2373. }
  2374. ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
  2375. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
  2376. "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
  2377. ioc->internal_depth, ioc->internal_smid));
  2378. /* sense buffers, 4 byte align */
  2379. sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
  2380. ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
  2381. 0);
  2382. if (!ioc->sense_dma_pool) {
  2383. printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
  2384. ioc->name);
  2385. goto out;
  2386. }
  2387. ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
  2388. &ioc->sense_dma);
  2389. if (!ioc->sense) {
  2390. printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
  2391. ioc->name);
  2392. goto out;
  2393. }
  2394. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
  2395. "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
  2396. "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
  2397. SCSI_SENSE_BUFFERSIZE, sz/1024));
  2398. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
  2399. ioc->name, (unsigned long long)ioc->sense_dma));
  2400. total_sz += sz;
  2401. /* reply pool, 4 byte align */
  2402. sz = ioc->reply_free_queue_depth * ioc->reply_sz;
  2403. ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
  2404. 0);
  2405. if (!ioc->reply_dma_pool) {
  2406. printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
  2407. ioc->name);
  2408. goto out;
  2409. }
  2410. ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
  2411. &ioc->reply_dma);
  2412. if (!ioc->reply) {
  2413. printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
  2414. ioc->name);
  2415. goto out;
  2416. }
  2417. ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
  2418. ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
  2419. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
  2420. "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
  2421. ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
  2422. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
  2423. ioc->name, (unsigned long long)ioc->reply_dma));
  2424. total_sz += sz;
  2425. /* reply free queue, 16 byte align */
  2426. sz = ioc->reply_free_queue_depth * 4;
  2427. ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
  2428. ioc->pdev, sz, 16, 0);
  2429. if (!ioc->reply_free_dma_pool) {
  2430. printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
  2431. "failed\n", ioc->name);
  2432. goto out;
  2433. }
  2434. ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
  2435. &ioc->reply_free_dma);
  2436. if (!ioc->reply_free) {
  2437. printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
  2438. "failed\n", ioc->name);
  2439. goto out;
  2440. }
  2441. memset(ioc->reply_free, 0, sz);
  2442. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
  2443. "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
  2444. ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
  2445. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
  2446. "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
  2447. total_sz += sz;
  2448. /* reply post queue, 16 byte align */
  2449. reply_post_free_sz = ioc->reply_post_queue_depth *
  2450. sizeof(Mpi2DefaultReplyDescriptor_t);
  2451. if (_base_is_controller_msix_enabled(ioc))
  2452. sz = reply_post_free_sz * ioc->reply_queue_count;
  2453. else
  2454. sz = reply_post_free_sz;
  2455. ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
  2456. ioc->pdev, sz, 16, 0);
  2457. if (!ioc->reply_post_free_dma_pool) {
  2458. printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
  2459. "failed\n", ioc->name);
  2460. goto out;
  2461. }
  2462. ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
  2463. GFP_KERNEL, &ioc->reply_post_free_dma);
  2464. if (!ioc->reply_post_free) {
  2465. printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
  2466. "failed\n", ioc->name);
  2467. goto out;
  2468. }
  2469. memset(ioc->reply_post_free, 0, sz);
  2470. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
  2471. "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
  2472. ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
  2473. sz/1024));
  2474. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
  2475. "(0x%llx)\n", ioc->name, (unsigned long long)
  2476. ioc->reply_post_free_dma));
  2477. total_sz += sz;
  2478. ioc->config_page_sz = 512;
  2479. ioc->config_page = pci_alloc_consistent(ioc->pdev,
  2480. ioc->config_page_sz, &ioc->config_page_dma);
  2481. if (!ioc->config_page) {
  2482. printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
  2483. "failed\n", ioc->name);
  2484. goto out;
  2485. }
  2486. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
  2487. "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
  2488. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
  2489. "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
  2490. total_sz += ioc->config_page_sz;
  2491. printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
  2492. ioc->name, total_sz/1024);
  2493. printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
  2494. "Max Controller Queue Depth(%d)\n",
  2495. ioc->name, ioc->shost->can_queue, facts->RequestCredit);
  2496. printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
  2497. ioc->name, ioc->shost->sg_tablesize);
  2498. return 0;
  2499. out:
  2500. return -ENOMEM;
  2501. }
  2502. /**
  2503. * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
  2504. * @ioc: Pointer to MPT_ADAPTER structure
  2505. * @cooked: Request raw or cooked IOC state
  2506. *
  2507. * Returns all IOC Doorbell register bits if cooked==0, else just the
  2508. * Doorbell bits in MPI_IOC_STATE_MASK.
  2509. */
  2510. u32
  2511. mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
  2512. {
  2513. u32 s, sc;
  2514. s = readl(&ioc->chip->Doorbell);
  2515. sc = s & MPI2_IOC_STATE_MASK;
  2516. return cooked ? sc : s;
  2517. }
  2518. /**
  2519. * _base_wait_on_iocstate - waiting on a particular ioc state
  2520. * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
  2521. * @timeout: timeout in second
  2522. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2523. *
  2524. * Returns 0 for success, non-zero for failure.
  2525. */
  2526. static int
  2527. _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
  2528. int sleep_flag)
  2529. {
  2530. u32 count, cntdn;
  2531. u32 current_state;
  2532. count = 0;
  2533. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2534. do {
  2535. current_state = mpt2sas_base_get_iocstate(ioc, 1);
  2536. if (current_state == ioc_state)
  2537. return 0;
  2538. if (count && current_state == MPI2_IOC_STATE_FAULT)
  2539. break;
  2540. if (sleep_flag == CAN_SLEEP)
  2541. msleep(1);
  2542. else
  2543. udelay(500);
  2544. count++;
  2545. } while (--cntdn);
  2546. return current_state;
  2547. }
  2548. /**
  2549. * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
  2550. * a write to the doorbell)
  2551. * @ioc: per adapter object
  2552. * @timeout: timeout in second
  2553. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2554. *
  2555. * Returns 0 for success, non-zero for failure.
  2556. *
  2557. * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
  2558. */
  2559. static int
  2560. _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
  2561. int sleep_flag)
  2562. {
  2563. u32 cntdn, count;
  2564. u32 int_status;
  2565. count = 0;
  2566. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2567. do {
  2568. int_status = readl(&ioc->chip->HostInterruptStatus);
  2569. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2570. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  2571. "successful count(%d), timeout(%d)\n", ioc->name,
  2572. __func__, count, timeout));
  2573. return 0;
  2574. }
  2575. if (sleep_flag == CAN_SLEEP)
  2576. msleep(1);
  2577. else
  2578. udelay(500);
  2579. count++;
  2580. } while (--cntdn);
  2581. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  2582. "int_status(%x)!\n", ioc->name, __func__, count, int_status);
  2583. return -EFAULT;
  2584. }
  2585. /**
  2586. * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
  2587. * @ioc: per adapter object
  2588. * @timeout: timeout in second
  2589. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2590. *
  2591. * Returns 0 for success, non-zero for failure.
  2592. *
  2593. * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
  2594. * doorbell.
  2595. */
  2596. static int
  2597. _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
  2598. int sleep_flag)
  2599. {
  2600. u32 cntdn, count;
  2601. u32 int_status;
  2602. u32 doorbell;
  2603. count = 0;
  2604. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2605. do {
  2606. int_status = readl(&ioc->chip->HostInterruptStatus);
  2607. if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
  2608. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  2609. "successful count(%d), timeout(%d)\n", ioc->name,
  2610. __func__, count, timeout));
  2611. return 0;
  2612. } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2613. doorbell = readl(&ioc->chip->Doorbell);
  2614. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  2615. MPI2_IOC_STATE_FAULT) {
  2616. mpt2sas_base_fault_info(ioc , doorbell);
  2617. return -EFAULT;
  2618. }
  2619. } else if (int_status == 0xFFFFFFFF)
  2620. goto out;
  2621. if (sleep_flag == CAN_SLEEP)
  2622. msleep(1);
  2623. else
  2624. udelay(500);
  2625. count++;
  2626. } while (--cntdn);
  2627. out:
  2628. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  2629. "int_status(%x)!\n", ioc->name, __func__, count, int_status);
  2630. return -EFAULT;
  2631. }
  2632. /**
  2633. * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
  2634. * @ioc: per adapter object
  2635. * @timeout: timeout in second
  2636. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2637. *
  2638. * Returns 0 for success, non-zero for failure.
  2639. *
  2640. */
  2641. static int
  2642. _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
  2643. int sleep_flag)
  2644. {
  2645. u32 cntdn, count;
  2646. u32 doorbell_reg;
  2647. count = 0;
  2648. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2649. do {
  2650. doorbell_reg = readl(&ioc->chip->Doorbell);
  2651. if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
  2652. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  2653. "successful count(%d), timeout(%d)\n", ioc->name,
  2654. __func__, count, timeout));
  2655. return 0;
  2656. }
  2657. if (sleep_flag == CAN_SLEEP)
  2658. msleep(1);
  2659. else
  2660. udelay(500);
  2661. count++;
  2662. } while (--cntdn);
  2663. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  2664. "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
  2665. return -EFAULT;
  2666. }
  2667. /**
  2668. * _base_send_ioc_reset - send doorbell reset
  2669. * @ioc: per adapter object
  2670. * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
  2671. * @timeout: timeout in second
  2672. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2673. *
  2674. * Returns 0 for success, non-zero for failure.
  2675. */
  2676. static int
  2677. _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
  2678. int sleep_flag)
  2679. {
  2680. u32 ioc_state;
  2681. int r = 0;
  2682. if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
  2683. printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
  2684. ioc->name, __func__);
  2685. return -EFAULT;
  2686. }
  2687. if (!(ioc->facts.IOCCapabilities &
  2688. MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
  2689. return -EFAULT;
  2690. printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
  2691. writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
  2692. &ioc->chip->Doorbell);
  2693. if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
  2694. r = -EFAULT;
  2695. goto out;
  2696. }
  2697. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
  2698. timeout, sleep_flag);
  2699. if (ioc_state) {
  2700. printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
  2701. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  2702. r = -EFAULT;
  2703. goto out;
  2704. }
  2705. out:
  2706. printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
  2707. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  2708. return r;
  2709. }
  2710. /**
  2711. * _base_handshake_req_reply_wait - send request thru doorbell interface
  2712. * @ioc: per adapter object
  2713. * @request_bytes: request length
  2714. * @request: pointer having request payload
  2715. * @reply_bytes: reply length
  2716. * @reply: pointer to reply payload
  2717. * @timeout: timeout in second
  2718. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2719. *
  2720. * Returns 0 for success, non-zero for failure.
  2721. */
  2722. static int
  2723. _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
  2724. u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
  2725. {
  2726. MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
  2727. int i;
  2728. u8 failed;
  2729. u16 dummy;
  2730. __le32 *mfp;
  2731. /* make sure doorbell is not in use */
  2732. if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
  2733. printk(MPT2SAS_ERR_FMT "doorbell is in use "
  2734. " (line=%d)\n", ioc->name, __LINE__);
  2735. return -EFAULT;
  2736. }
  2737. /* clear pending doorbell interrupts from previous state changes */
  2738. if (readl(&ioc->chip->HostInterruptStatus) &
  2739. MPI2_HIS_IOC2SYS_DB_STATUS)
  2740. writel(0, &ioc->chip->HostInterruptStatus);
  2741. /* send message to ioc */
  2742. writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
  2743. ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
  2744. &ioc->chip->Doorbell);
  2745. if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
  2746. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2747. "int failed (line=%d)\n", ioc->name, __LINE__);
  2748. return -EFAULT;
  2749. }
  2750. writel(0, &ioc->chip->HostInterruptStatus);
  2751. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
  2752. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2753. "ack failed (line=%d)\n", ioc->name, __LINE__);
  2754. return -EFAULT;
  2755. }
  2756. /* send message 32-bits at a time */
  2757. for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
  2758. writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
  2759. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
  2760. failed = 1;
  2761. }
  2762. if (failed) {
  2763. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2764. "sending request failed (line=%d)\n", ioc->name, __LINE__);
  2765. return -EFAULT;
  2766. }
  2767. /* now wait for the reply */
  2768. if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
  2769. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2770. "int failed (line=%d)\n", ioc->name, __LINE__);
  2771. return -EFAULT;
  2772. }
  2773. /* read the first two 16-bits, it gives the total length of the reply */
  2774. reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2775. & MPI2_DOORBELL_DATA_MASK);
  2776. writel(0, &ioc->chip->HostInterruptStatus);
  2777. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2778. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2779. "int failed (line=%d)\n", ioc->name, __LINE__);
  2780. return -EFAULT;
  2781. }
  2782. reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2783. & MPI2_DOORBELL_DATA_MASK);
  2784. writel(0, &ioc->chip->HostInterruptStatus);
  2785. for (i = 2; i < default_reply->MsgLength * 2; i++) {
  2786. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2787. printk(MPT2SAS_ERR_FMT "doorbell "
  2788. "handshake int failed (line=%d)\n", ioc->name,
  2789. __LINE__);
  2790. return -EFAULT;
  2791. }
  2792. if (i >= reply_bytes/2) /* overflow case */
  2793. dummy = readl(&ioc->chip->Doorbell);
  2794. else
  2795. reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2796. & MPI2_DOORBELL_DATA_MASK);
  2797. writel(0, &ioc->chip->HostInterruptStatus);
  2798. }
  2799. _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
  2800. if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
  2801. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
  2802. " (line=%d)\n", ioc->name, __LINE__));
  2803. }
  2804. writel(0, &ioc->chip->HostInterruptStatus);
  2805. if (ioc->logging_level & MPT_DEBUG_INIT) {
  2806. mfp = (__le32 *)reply;
  2807. printk(KERN_INFO "\toffset:data\n");
  2808. for (i = 0; i < reply_bytes/4; i++)
  2809. printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
  2810. le32_to_cpu(mfp[i]));
  2811. }
  2812. return 0;
  2813. }
  2814. /**
  2815. * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
  2816. * @ioc: per adapter object
  2817. * @mpi_reply: the reply payload from FW
  2818. * @mpi_request: the request payload sent to FW
  2819. *
  2820. * The SAS IO Unit Control Request message allows the host to perform low-level
  2821. * operations, such as resets on the PHYs of the IO Unit, also allows the host
  2822. * to obtain the IOC assigned device handles for a device if it has other
  2823. * identifying information about the device, in addition allows the host to
  2824. * remove IOC resources associated with the device.
  2825. *
  2826. * Returns 0 for success, non-zero for failure.
  2827. */
  2828. int
  2829. mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
  2830. Mpi2SasIoUnitControlReply_t *mpi_reply,
  2831. Mpi2SasIoUnitControlRequest_t *mpi_request)
  2832. {
  2833. u16 smid;
  2834. u32 ioc_state;
  2835. unsigned long timeleft;
  2836. u8 issue_reset;
  2837. int rc;
  2838. void *request;
  2839. u16 wait_state_count;
  2840. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2841. __func__));
  2842. mutex_lock(&ioc->base_cmds.mutex);
  2843. if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
  2844. printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
  2845. ioc->name, __func__);
  2846. rc = -EAGAIN;
  2847. goto out;
  2848. }
  2849. wait_state_count = 0;
  2850. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2851. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2852. if (wait_state_count++ == 10) {
  2853. printk(MPT2SAS_ERR_FMT
  2854. "%s: failed due to ioc not operational\n",
  2855. ioc->name, __func__);
  2856. rc = -EFAULT;
  2857. goto out;
  2858. }
  2859. ssleep(1);
  2860. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2861. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  2862. "operational state(count=%d)\n", ioc->name,
  2863. __func__, wait_state_count);
  2864. }
  2865. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2866. if (!smid) {
  2867. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2868. ioc->name, __func__);
  2869. rc = -EAGAIN;
  2870. goto out;
  2871. }
  2872. rc = 0;
  2873. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2874. request = mpt2sas_base_get_msg_frame(ioc, smid);
  2875. ioc->base_cmds.smid = smid;
  2876. memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
  2877. if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  2878. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
  2879. ioc->ioc_link_reset_in_progress = 1;
  2880. init_completion(&ioc->base_cmds.done);
  2881. mpt2sas_base_put_smid_default(ioc, smid);
  2882. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2883. msecs_to_jiffies(10000));
  2884. if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  2885. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
  2886. ioc->ioc_link_reset_in_progress)
  2887. ioc->ioc_link_reset_in_progress = 0;
  2888. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2889. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2890. ioc->name, __func__);
  2891. _debug_dump_mf(mpi_request,
  2892. sizeof(Mpi2SasIoUnitControlRequest_t)/4);
  2893. if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
  2894. issue_reset = 1;
  2895. goto issue_host_reset;
  2896. }
  2897. if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
  2898. memcpy(mpi_reply, ioc->base_cmds.reply,
  2899. sizeof(Mpi2SasIoUnitControlReply_t));
  2900. else
  2901. memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  2902. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2903. goto out;
  2904. issue_host_reset:
  2905. if (issue_reset)
  2906. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2907. FORCE_BIG_HAMMER);
  2908. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2909. rc = -EFAULT;
  2910. out:
  2911. mutex_unlock(&ioc->base_cmds.mutex);
  2912. return rc;
  2913. }
  2914. /**
  2915. * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
  2916. * @ioc: per adapter object
  2917. * @mpi_reply: the reply payload from FW
  2918. * @mpi_request: the request payload sent to FW
  2919. *
  2920. * The SCSI Enclosure Processor request message causes the IOC to
  2921. * communicate with SES devices to control LED status signals.
  2922. *
  2923. * Returns 0 for success, non-zero for failure.
  2924. */
  2925. int
  2926. mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
  2927. Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
  2928. {
  2929. u16 smid;
  2930. u32 ioc_state;
  2931. unsigned long timeleft;
  2932. u8 issue_reset;
  2933. int rc;
  2934. void *request;
  2935. u16 wait_state_count;
  2936. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2937. __func__));
  2938. mutex_lock(&ioc->base_cmds.mutex);
  2939. if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
  2940. printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
  2941. ioc->name, __func__);
  2942. rc = -EAGAIN;
  2943. goto out;
  2944. }
  2945. wait_state_count = 0;
  2946. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2947. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2948. if (wait_state_count++ == 10) {
  2949. printk(MPT2SAS_ERR_FMT
  2950. "%s: failed due to ioc not operational\n",
  2951. ioc->name, __func__);
  2952. rc = -EFAULT;
  2953. goto out;
  2954. }
  2955. ssleep(1);
  2956. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2957. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  2958. "operational state(count=%d)\n", ioc->name,
  2959. __func__, wait_state_count);
  2960. }
  2961. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2962. if (!smid) {
  2963. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2964. ioc->name, __func__);
  2965. rc = -EAGAIN;
  2966. goto out;
  2967. }
  2968. rc = 0;
  2969. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2970. request = mpt2sas_base_get_msg_frame(ioc, smid);
  2971. ioc->base_cmds.smid = smid;
  2972. memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
  2973. init_completion(&ioc->base_cmds.done);
  2974. mpt2sas_base_put_smid_default(ioc, smid);
  2975. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2976. msecs_to_jiffies(10000));
  2977. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2978. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2979. ioc->name, __func__);
  2980. _debug_dump_mf(mpi_request,
  2981. sizeof(Mpi2SepRequest_t)/4);
  2982. if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
  2983. issue_reset = 1;
  2984. goto issue_host_reset;
  2985. }
  2986. if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
  2987. memcpy(mpi_reply, ioc->base_cmds.reply,
  2988. sizeof(Mpi2SepReply_t));
  2989. else
  2990. memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
  2991. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2992. goto out;
  2993. issue_host_reset:
  2994. if (issue_reset)
  2995. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2996. FORCE_BIG_HAMMER);
  2997. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2998. rc = -EFAULT;
  2999. out:
  3000. mutex_unlock(&ioc->base_cmds.mutex);
  3001. return rc;
  3002. }
  3003. /**
  3004. * _base_get_port_facts - obtain port facts reply and save in ioc
  3005. * @ioc: per adapter object
  3006. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3007. *
  3008. * Returns 0 for success, non-zero for failure.
  3009. */
  3010. static int
  3011. _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
  3012. {
  3013. Mpi2PortFactsRequest_t mpi_request;
  3014. Mpi2PortFactsReply_t mpi_reply;
  3015. struct mpt2sas_port_facts *pfacts;
  3016. int mpi_reply_sz, mpi_request_sz, r;
  3017. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3018. __func__));
  3019. mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
  3020. mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
  3021. memset(&mpi_request, 0, mpi_request_sz);
  3022. mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
  3023. mpi_request.PortNumber = port;
  3024. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3025. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  3026. if (r != 0) {
  3027. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  3028. ioc->name, __func__, r);
  3029. return r;
  3030. }
  3031. pfacts = &ioc->pfacts[port];
  3032. memset(pfacts, 0, sizeof(struct mpt2sas_port_facts));
  3033. pfacts->PortNumber = mpi_reply.PortNumber;
  3034. pfacts->VP_ID = mpi_reply.VP_ID;
  3035. pfacts->VF_ID = mpi_reply.VF_ID;
  3036. pfacts->MaxPostedCmdBuffers =
  3037. le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
  3038. return 0;
  3039. }
  3040. /**
  3041. * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
  3042. * @ioc: per adapter object
  3043. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3044. *
  3045. * Returns 0 for success, non-zero for failure.
  3046. */
  3047. static int
  3048. _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3049. {
  3050. Mpi2IOCFactsRequest_t mpi_request;
  3051. Mpi2IOCFactsReply_t mpi_reply;
  3052. struct mpt2sas_facts *facts;
  3053. int mpi_reply_sz, mpi_request_sz, r;
  3054. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3055. __func__));
  3056. mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
  3057. mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
  3058. memset(&mpi_request, 0, mpi_request_sz);
  3059. mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
  3060. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3061. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  3062. if (r != 0) {
  3063. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  3064. ioc->name, __func__, r);
  3065. return r;
  3066. }
  3067. facts = &ioc->facts;
  3068. memset(facts, 0, sizeof(struct mpt2sas_facts));
  3069. facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
  3070. facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
  3071. facts->VP_ID = mpi_reply.VP_ID;
  3072. facts->VF_ID = mpi_reply.VF_ID;
  3073. facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
  3074. facts->MaxChainDepth = mpi_reply.MaxChainDepth;
  3075. facts->WhoInit = mpi_reply.WhoInit;
  3076. facts->NumberOfPorts = mpi_reply.NumberOfPorts;
  3077. facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
  3078. facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
  3079. facts->MaxReplyDescriptorPostQueueDepth =
  3080. le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
  3081. facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
  3082. facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
  3083. if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
  3084. ioc->ir_firmware = 1;
  3085. facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
  3086. facts->IOCRequestFrameSize =
  3087. le16_to_cpu(mpi_reply.IOCRequestFrameSize);
  3088. facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
  3089. facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
  3090. ioc->shost->max_id = -1;
  3091. facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
  3092. facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
  3093. facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
  3094. facts->HighPriorityCredit =
  3095. le16_to_cpu(mpi_reply.HighPriorityCredit);
  3096. facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
  3097. facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
  3098. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
  3099. "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
  3100. facts->MaxChainDepth));
  3101. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
  3102. "reply frame size(%d)\n", ioc->name,
  3103. facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
  3104. return 0;
  3105. }
  3106. /**
  3107. * _base_send_ioc_init - send ioc_init to firmware
  3108. * @ioc: per adapter object
  3109. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3110. *
  3111. * Returns 0 for success, non-zero for failure.
  3112. */
  3113. static int
  3114. _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3115. {
  3116. Mpi2IOCInitRequest_t mpi_request;
  3117. Mpi2IOCInitReply_t mpi_reply;
  3118. int r;
  3119. struct timeval current_time;
  3120. u16 ioc_status;
  3121. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3122. __func__));
  3123. memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
  3124. mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
  3125. mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  3126. mpi_request.VF_ID = 0; /* TODO */
  3127. mpi_request.VP_ID = 0;
  3128. mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
  3129. mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  3130. if (_base_is_controller_msix_enabled(ioc))
  3131. mpi_request.HostMSIxVectors = ioc->reply_queue_count;
  3132. mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
  3133. mpi_request.ReplyDescriptorPostQueueDepth =
  3134. cpu_to_le16(ioc->reply_post_queue_depth);
  3135. mpi_request.ReplyFreeQueueDepth =
  3136. cpu_to_le16(ioc->reply_free_queue_depth);
  3137. mpi_request.SenseBufferAddressHigh =
  3138. cpu_to_le32((u64)ioc->sense_dma >> 32);
  3139. mpi_request.SystemReplyAddressHigh =
  3140. cpu_to_le32((u64)ioc->reply_dma >> 32);
  3141. mpi_request.SystemRequestFrameBaseAddress =
  3142. cpu_to_le64((u64)ioc->request_dma);
  3143. mpi_request.ReplyFreeQueueAddress =
  3144. cpu_to_le64((u64)ioc->reply_free_dma);
  3145. mpi_request.ReplyDescriptorPostQueueAddress =
  3146. cpu_to_le64((u64)ioc->reply_post_free_dma);
  3147. /* This time stamp specifies number of milliseconds
  3148. * since epoch ~ midnight January 1, 1970.
  3149. */
  3150. do_gettimeofday(&current_time);
  3151. mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
  3152. (current_time.tv_usec / 1000));
  3153. if (ioc->logging_level & MPT_DEBUG_INIT) {
  3154. __le32 *mfp;
  3155. int i;
  3156. mfp = (__le32 *)&mpi_request;
  3157. printk(KERN_INFO "\toffset:data\n");
  3158. for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
  3159. printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
  3160. le32_to_cpu(mfp[i]));
  3161. }
  3162. r = _base_handshake_req_reply_wait(ioc,
  3163. sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
  3164. sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
  3165. sleep_flag);
  3166. if (r != 0) {
  3167. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  3168. ioc->name, __func__, r);
  3169. return r;
  3170. }
  3171. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  3172. if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
  3173. mpi_reply.IOCLogInfo) {
  3174. printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
  3175. r = -EIO;
  3176. }
  3177. return 0;
  3178. }
  3179. /**
  3180. * mpt2sas_port_enable_done - command completion routine for port enable
  3181. * @ioc: per adapter object
  3182. * @smid: system request message index
  3183. * @msix_index: MSIX table index supplied by the OS
  3184. * @reply: reply message frame(lower 32bit addr)
  3185. *
  3186. * Return 1 meaning mf should be freed from _base_interrupt
  3187. * 0 means the mf is freed from this function.
  3188. */
  3189. u8
  3190. mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  3191. u32 reply)
  3192. {
  3193. MPI2DefaultReply_t *mpi_reply;
  3194. u16 ioc_status;
  3195. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  3196. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  3197. return 1;
  3198. if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
  3199. return 1;
  3200. ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
  3201. if (mpi_reply) {
  3202. ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
  3203. memcpy(ioc->port_enable_cmds.reply, mpi_reply,
  3204. mpi_reply->MsgLength*4);
  3205. }
  3206. ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;
  3207. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  3208. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  3209. ioc->port_enable_failed = 1;
  3210. if (ioc->is_driver_loading) {
  3211. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  3212. mpt2sas_port_enable_complete(ioc);
  3213. return 1;
  3214. } else {
  3215. ioc->start_scan_failed = ioc_status;
  3216. ioc->start_scan = 0;
  3217. return 1;
  3218. }
  3219. }
  3220. complete(&ioc->port_enable_cmds.done);
  3221. return 1;
  3222. }
  3223. /**
  3224. * _base_send_port_enable - send port_enable(discovery stuff) to firmware
  3225. * @ioc: per adapter object
  3226. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3227. *
  3228. * Returns 0 for success, non-zero for failure.
  3229. */
  3230. static int
  3231. _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3232. {
  3233. Mpi2PortEnableRequest_t *mpi_request;
  3234. Mpi2PortEnableReply_t *mpi_reply;
  3235. unsigned long timeleft;
  3236. int r = 0;
  3237. u16 smid;
  3238. u16 ioc_status;
  3239. printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
  3240. if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
  3241. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  3242. ioc->name, __func__);
  3243. return -EAGAIN;
  3244. }
  3245. smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  3246. if (!smid) {
  3247. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3248. ioc->name, __func__);
  3249. return -EAGAIN;
  3250. }
  3251. ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
  3252. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3253. ioc->port_enable_cmds.smid = smid;
  3254. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  3255. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  3256. init_completion(&ioc->port_enable_cmds.done);
  3257. mpt2sas_base_put_smid_default(ioc, smid);
  3258. timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
  3259. 300*HZ);
  3260. if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
  3261. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  3262. ioc->name, __func__);
  3263. _debug_dump_mf(mpi_request,
  3264. sizeof(Mpi2PortEnableRequest_t)/4);
  3265. if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
  3266. r = -EFAULT;
  3267. else
  3268. r = -ETIME;
  3269. goto out;
  3270. }
  3271. mpi_reply = ioc->port_enable_cmds.reply;
  3272. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  3273. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3274. printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
  3275. ioc->name, __func__, ioc_status);
  3276. r = -EFAULT;
  3277. goto out;
  3278. }
  3279. out:
  3280. ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
  3281. printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
  3282. "SUCCESS" : "FAILED"));
  3283. return r;
  3284. }
  3285. /**
  3286. * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
  3287. * @ioc: per adapter object
  3288. *
  3289. * Returns 0 for success, non-zero for failure.
  3290. */
  3291. int
  3292. mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
  3293. {
  3294. Mpi2PortEnableRequest_t *mpi_request;
  3295. u16 smid;
  3296. printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
  3297. if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
  3298. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  3299. ioc->name, __func__);
  3300. return -EAGAIN;
  3301. }
  3302. smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  3303. if (!smid) {
  3304. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3305. ioc->name, __func__);
  3306. return -EAGAIN;
  3307. }
  3308. ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
  3309. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3310. ioc->port_enable_cmds.smid = smid;
  3311. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  3312. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  3313. mpt2sas_base_put_smid_default(ioc, smid);
  3314. return 0;
  3315. }
  3316. /**
  3317. * _base_determine_wait_on_discovery - desposition
  3318. * @ioc: per adapter object
  3319. *
  3320. * Decide whether to wait on discovery to complete. Used to either
  3321. * locate boot device, or report volumes ahead of physical devices.
  3322. *
  3323. * Returns 1 for wait, 0 for don't wait
  3324. */
  3325. static int
  3326. _base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
  3327. {
  3328. /* We wait for discovery to complete if IR firmware is loaded.
  3329. * The sas topology events arrive before PD events, so we need time to
  3330. * turn on the bit in ioc->pd_handles to indicate PD
  3331. * Also, it maybe required to report Volumes ahead of physical
  3332. * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
  3333. */
  3334. if (ioc->ir_firmware)
  3335. return 1;
  3336. /* if no Bios, then we don't need to wait */
  3337. if (!ioc->bios_pg3.BiosVersion)
  3338. return 0;
  3339. /* Bios is present, then we drop down here.
  3340. *
  3341. * If there any entries in the Bios Page 2, then we wait
  3342. * for discovery to complete.
  3343. */
  3344. /* Current Boot Device */
  3345. if ((ioc->bios_pg2.CurrentBootDeviceForm &
  3346. MPI2_BIOSPAGE2_FORM_MASK) ==
  3347. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  3348. /* Request Boot Device */
  3349. (ioc->bios_pg2.ReqBootDeviceForm &
  3350. MPI2_BIOSPAGE2_FORM_MASK) ==
  3351. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  3352. /* Alternate Request Boot Device */
  3353. (ioc->bios_pg2.ReqAltBootDeviceForm &
  3354. MPI2_BIOSPAGE2_FORM_MASK) ==
  3355. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
  3356. return 0;
  3357. return 1;
  3358. }
  3359. /**
  3360. * _base_unmask_events - turn on notification for this event
  3361. * @ioc: per adapter object
  3362. * @event: firmware event
  3363. *
  3364. * The mask is stored in ioc->event_masks.
  3365. */
  3366. static void
  3367. _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
  3368. {
  3369. u32 desired_event;
  3370. if (event >= 128)
  3371. return;
  3372. desired_event = (1 << (event % 32));
  3373. if (event < 32)
  3374. ioc->event_masks[0] &= ~desired_event;
  3375. else if (event < 64)
  3376. ioc->event_masks[1] &= ~desired_event;
  3377. else if (event < 96)
  3378. ioc->event_masks[2] &= ~desired_event;
  3379. else if (event < 128)
  3380. ioc->event_masks[3] &= ~desired_event;
  3381. }
  3382. /**
  3383. * _base_event_notification - send event notification
  3384. * @ioc: per adapter object
  3385. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3386. *
  3387. * Returns 0 for success, non-zero for failure.
  3388. */
  3389. static int
  3390. _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3391. {
  3392. Mpi2EventNotificationRequest_t *mpi_request;
  3393. unsigned long timeleft;
  3394. u16 smid;
  3395. int r = 0;
  3396. int i;
  3397. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3398. __func__));
  3399. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  3400. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  3401. ioc->name, __func__);
  3402. return -EAGAIN;
  3403. }
  3404. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  3405. if (!smid) {
  3406. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3407. ioc->name, __func__);
  3408. return -EAGAIN;
  3409. }
  3410. ioc->base_cmds.status = MPT2_CMD_PENDING;
  3411. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3412. ioc->base_cmds.smid = smid;
  3413. memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
  3414. mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  3415. mpi_request->VF_ID = 0; /* TODO */
  3416. mpi_request->VP_ID = 0;
  3417. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  3418. mpi_request->EventMasks[i] =
  3419. cpu_to_le32(ioc->event_masks[i]);
  3420. init_completion(&ioc->base_cmds.done);
  3421. mpt2sas_base_put_smid_default(ioc, smid);
  3422. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
  3423. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  3424. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  3425. ioc->name, __func__);
  3426. _debug_dump_mf(mpi_request,
  3427. sizeof(Mpi2EventNotificationRequest_t)/4);
  3428. if (ioc->base_cmds.status & MPT2_CMD_RESET)
  3429. r = -EFAULT;
  3430. else
  3431. r = -ETIME;
  3432. } else
  3433. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
  3434. ioc->name, __func__));
  3435. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  3436. return r;
  3437. }
  3438. /**
  3439. * mpt2sas_base_validate_event_type - validating event types
  3440. * @ioc: per adapter object
  3441. * @event: firmware event
  3442. *
  3443. * This will turn on firmware event notification when application
  3444. * ask for that event. We don't mask events that are already enabled.
  3445. */
  3446. void
  3447. mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
  3448. {
  3449. int i, j;
  3450. u32 event_mask, desired_event;
  3451. u8 send_update_to_fw;
  3452. for (i = 0, send_update_to_fw = 0; i <
  3453. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
  3454. event_mask = ~event_type[i];
  3455. desired_event = 1;
  3456. for (j = 0; j < 32; j++) {
  3457. if (!(event_mask & desired_event) &&
  3458. (ioc->event_masks[i] & desired_event)) {
  3459. ioc->event_masks[i] &= ~desired_event;
  3460. send_update_to_fw = 1;
  3461. }
  3462. desired_event = (desired_event << 1);
  3463. }
  3464. }
  3465. if (!send_update_to_fw)
  3466. return;
  3467. mutex_lock(&ioc->base_cmds.mutex);
  3468. _base_event_notification(ioc, CAN_SLEEP);
  3469. mutex_unlock(&ioc->base_cmds.mutex);
  3470. }
  3471. /**
  3472. * _base_diag_reset - the "big hammer" start of day reset
  3473. * @ioc: per adapter object
  3474. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3475. *
  3476. * Returns 0 for success, non-zero for failure.
  3477. */
  3478. static int
  3479. _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3480. {
  3481. u32 host_diagnostic;
  3482. u32 ioc_state;
  3483. u32 count;
  3484. u32 hcb_size;
  3485. printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
  3486. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
  3487. ioc->name));
  3488. count = 0;
  3489. do {
  3490. /* Write magic sequence to WriteSequence register
  3491. * Loop until in diagnostic mode
  3492. */
  3493. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
  3494. "sequence\n", ioc->name));
  3495. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3496. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
  3497. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
  3498. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
  3499. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3500. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3501. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3502. /* wait 100 msec */
  3503. if (sleep_flag == CAN_SLEEP)
  3504. msleep(100);
  3505. else
  3506. mdelay(100);
  3507. if (count++ > 20)
  3508. goto out;
  3509. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3510. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
  3511. "sequence: count(%d), host_diagnostic(0x%08x)\n",
  3512. ioc->name, count, host_diagnostic));
  3513. } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
  3514. hcb_size = readl(&ioc->chip->HCBSize);
  3515. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
  3516. ioc->name));
  3517. writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
  3518. &ioc->chip->HostDiagnostic);
  3519. /* don't access any registers for 50 milliseconds */
  3520. msleep(50);
  3521. /* 300 second max wait */
  3522. for (count = 0; count < 3000000 ; count++) {
  3523. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3524. if (host_diagnostic == 0xFFFFFFFF)
  3525. goto out;
  3526. if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
  3527. break;
  3528. /* wait 100 msec */
  3529. if (sleep_flag == CAN_SLEEP)
  3530. msleep(1);
  3531. else
  3532. mdelay(1);
  3533. }
  3534. if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
  3535. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
  3536. "assuming the HCB Address points to good F/W\n",
  3537. ioc->name));
  3538. host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
  3539. host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
  3540. writel(host_diagnostic, &ioc->chip->HostDiagnostic);
  3541. drsprintk(ioc, printk(MPT2SAS_INFO_FMT
  3542. "re-enable the HCDW\n", ioc->name));
  3543. writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
  3544. &ioc->chip->HCBSize);
  3545. }
  3546. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
  3547. ioc->name));
  3548. writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
  3549. &ioc->chip->HostDiagnostic);
  3550. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
  3551. "diagnostic register\n", ioc->name));
  3552. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3553. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
  3554. "READY state\n", ioc->name));
  3555. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
  3556. sleep_flag);
  3557. if (ioc_state) {
  3558. printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
  3559. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  3560. goto out;
  3561. }
  3562. printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
  3563. return 0;
  3564. out:
  3565. printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
  3566. return -EFAULT;
  3567. }
  3568. /**
  3569. * _base_make_ioc_ready - put controller in READY state
  3570. * @ioc: per adapter object
  3571. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3572. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  3573. *
  3574. * Returns 0 for success, non-zero for failure.
  3575. */
  3576. static int
  3577. _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
  3578. enum reset_type type)
  3579. {
  3580. u32 ioc_state;
  3581. int rc;
  3582. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3583. __func__));
  3584. if (ioc->pci_error_recovery)
  3585. return 0;
  3586. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  3587. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
  3588. ioc->name, __func__, ioc_state));
  3589. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
  3590. return 0;
  3591. if (ioc_state & MPI2_DOORBELL_USED) {
  3592. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
  3593. "active!\n", ioc->name));
  3594. goto issue_diag_reset;
  3595. }
  3596. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  3597. mpt2sas_base_fault_info(ioc, ioc_state &
  3598. MPI2_DOORBELL_DATA_MASK);
  3599. goto issue_diag_reset;
  3600. }
  3601. if (type == FORCE_BIG_HAMMER)
  3602. goto issue_diag_reset;
  3603. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  3604. if (!(_base_send_ioc_reset(ioc,
  3605. MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
  3606. ioc->ioc_reset_count++;
  3607. return 0;
  3608. }
  3609. issue_diag_reset:
  3610. rc = _base_diag_reset(ioc, CAN_SLEEP);
  3611. ioc->ioc_reset_count++;
  3612. return rc;
  3613. }
  3614. /**
  3615. * _base_make_ioc_operational - put controller in OPERATIONAL state
  3616. * @ioc: per adapter object
  3617. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3618. *
  3619. * Returns 0 for success, non-zero for failure.
  3620. */
  3621. static int
  3622. _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3623. {
  3624. int r, i;
  3625. unsigned long flags;
  3626. u32 reply_address;
  3627. u16 smid;
  3628. struct _tr_list *delayed_tr, *delayed_tr_next;
  3629. u8 hide_flag;
  3630. struct adapter_reply_queue *reply_q;
  3631. long reply_post_free;
  3632. u32 reply_post_free_sz;
  3633. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3634. __func__));
  3635. /* clean the delayed target reset list */
  3636. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3637. &ioc->delayed_tr_list, list) {
  3638. list_del(&delayed_tr->list);
  3639. kfree(delayed_tr);
  3640. }
  3641. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3642. &ioc->delayed_tr_volume_list, list) {
  3643. list_del(&delayed_tr->list);
  3644. kfree(delayed_tr);
  3645. }
  3646. /* initialize the scsi lookup free list */
  3647. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3648. INIT_LIST_HEAD(&ioc->free_list);
  3649. smid = 1;
  3650. for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
  3651. INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
  3652. ioc->scsi_lookup[i].cb_idx = 0xFF;
  3653. ioc->scsi_lookup[i].smid = smid;
  3654. ioc->scsi_lookup[i].scmd = NULL;
  3655. ioc->scsi_lookup[i].direct_io = 0;
  3656. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  3657. &ioc->free_list);
  3658. }
  3659. /* hi-priority queue */
  3660. INIT_LIST_HEAD(&ioc->hpr_free_list);
  3661. smid = ioc->hi_priority_smid;
  3662. for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
  3663. ioc->hpr_lookup[i].cb_idx = 0xFF;
  3664. ioc->hpr_lookup[i].smid = smid;
  3665. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  3666. &ioc->hpr_free_list);
  3667. }
  3668. /* internal queue */
  3669. INIT_LIST_HEAD(&ioc->internal_free_list);
  3670. smid = ioc->internal_smid;
  3671. for (i = 0; i < ioc->internal_depth; i++, smid++) {
  3672. ioc->internal_lookup[i].cb_idx = 0xFF;
  3673. ioc->internal_lookup[i].smid = smid;
  3674. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  3675. &ioc->internal_free_list);
  3676. }
  3677. /* chain pool */
  3678. INIT_LIST_HEAD(&ioc->free_chain_list);
  3679. for (i = 0; i < ioc->chain_depth; i++)
  3680. list_add_tail(&ioc->chain_lookup[i].tracker_list,
  3681. &ioc->free_chain_list);
  3682. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3683. /* initialize Reply Free Queue */
  3684. for (i = 0, reply_address = (u32)ioc->reply_dma ;
  3685. i < ioc->reply_free_queue_depth ; i++, reply_address +=
  3686. ioc->reply_sz)
  3687. ioc->reply_free[i] = cpu_to_le32(reply_address);
  3688. /* initialize reply queues */
  3689. if (ioc->is_driver_loading)
  3690. _base_assign_reply_queues(ioc);
  3691. /* initialize Reply Post Free Queue */
  3692. reply_post_free = (long)ioc->reply_post_free;
  3693. reply_post_free_sz = ioc->reply_post_queue_depth *
  3694. sizeof(Mpi2DefaultReplyDescriptor_t);
  3695. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  3696. reply_q->reply_post_host_index = 0;
  3697. reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
  3698. reply_post_free;
  3699. for (i = 0; i < ioc->reply_post_queue_depth; i++)
  3700. reply_q->reply_post_free[i].Words =
  3701. cpu_to_le64(ULLONG_MAX);
  3702. if (!_base_is_controller_msix_enabled(ioc))
  3703. goto skip_init_reply_post_free_queue;
  3704. reply_post_free += reply_post_free_sz;
  3705. }
  3706. skip_init_reply_post_free_queue:
  3707. r = _base_send_ioc_init(ioc, sleep_flag);
  3708. if (r)
  3709. return r;
  3710. /* initialize reply free host index */
  3711. ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
  3712. writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
  3713. /* initialize reply post host index */
  3714. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  3715. writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
  3716. &ioc->chip->ReplyPostHostIndex);
  3717. if (!_base_is_controller_msix_enabled(ioc))
  3718. goto skip_init_reply_post_host_index;
  3719. }
  3720. skip_init_reply_post_host_index:
  3721. _base_unmask_interrupts(ioc);
  3722. r = _base_event_notification(ioc, sleep_flag);
  3723. if (r)
  3724. return r;
  3725. if (sleep_flag == CAN_SLEEP)
  3726. _base_static_config_pages(ioc);
  3727. if (ioc->is_driver_loading) {
  3728. if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
  3729. == 0x80) {
  3730. hide_flag = (u8) (
  3731. le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
  3732. MFG_PAGE10_HIDE_SSDS_MASK);
  3733. if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
  3734. ioc->mfg_pg10_hide_flag = hide_flag;
  3735. }
  3736. ioc->wait_for_discovery_to_complete =
  3737. _base_determine_wait_on_discovery(ioc);
  3738. return r; /* scan_start and scan_finished support */
  3739. }
  3740. r = _base_send_port_enable(ioc, sleep_flag);
  3741. if (r)
  3742. return r;
  3743. return r;
  3744. }
  3745. /**
  3746. * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
  3747. * @ioc: per adapter object
  3748. *
  3749. * Return nothing.
  3750. */
  3751. void
  3752. mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
  3753. {
  3754. struct pci_dev *pdev = ioc->pdev;
  3755. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3756. __func__));
  3757. _base_mask_interrupts(ioc);
  3758. ioc->shost_recovery = 1;
  3759. _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3760. ioc->shost_recovery = 0;
  3761. _base_free_irq(ioc);
  3762. _base_disable_msix(ioc);
  3763. if (ioc->chip_phys)
  3764. iounmap(ioc->chip);
  3765. ioc->chip_phys = 0;
  3766. pci_release_selected_regions(ioc->pdev, ioc->bars);
  3767. pci_disable_pcie_error_reporting(pdev);
  3768. pci_disable_device(pdev);
  3769. return;
  3770. }
  3771. /**
  3772. * mpt2sas_base_attach - attach controller instance
  3773. * @ioc: per adapter object
  3774. *
  3775. * Returns 0 for success, non-zero for failure.
  3776. */
  3777. int
  3778. mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
  3779. {
  3780. int r, i;
  3781. int cpu_id, last_cpu_id = 0;
  3782. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3783. __func__));
  3784. /* setup cpu_msix_table */
  3785. ioc->cpu_count = num_online_cpus();
  3786. for_each_online_cpu(cpu_id)
  3787. last_cpu_id = cpu_id;
  3788. ioc->cpu_msix_table_sz = last_cpu_id + 1;
  3789. ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
  3790. ioc->reply_queue_count = 1;
  3791. if (!ioc->cpu_msix_table) {
  3792. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
  3793. "cpu_msix_table failed!!!\n", ioc->name));
  3794. r = -ENOMEM;
  3795. goto out_free_resources;
  3796. }
  3797. if (ioc->is_warpdrive) {
  3798. ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
  3799. sizeof(resource_size_t *), GFP_KERNEL);
  3800. if (!ioc->reply_post_host_index) {
  3801. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
  3802. "for cpu_msix_table failed!!!\n", ioc->name));
  3803. r = -ENOMEM;
  3804. goto out_free_resources;
  3805. }
  3806. }
  3807. r = mpt2sas_base_map_resources(ioc);
  3808. if (r)
  3809. goto out_free_resources;
  3810. if (ioc->is_warpdrive) {
  3811. ioc->reply_post_host_index[0] =
  3812. (resource_size_t *)&ioc->chip->ReplyPostHostIndex;
  3813. for (i = 1; i < ioc->cpu_msix_table_sz; i++)
  3814. ioc->reply_post_host_index[i] = (resource_size_t *)
  3815. ((u8 *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
  3816. * 4)));
  3817. }
  3818. pci_set_drvdata(ioc->pdev, ioc->shost);
  3819. r = _base_get_ioc_facts(ioc, CAN_SLEEP);
  3820. if (r)
  3821. goto out_free_resources;
  3822. r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3823. if (r)
  3824. goto out_free_resources;
  3825. ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
  3826. sizeof(struct mpt2sas_port_facts), GFP_KERNEL);
  3827. if (!ioc->pfacts) {
  3828. r = -ENOMEM;
  3829. goto out_free_resources;
  3830. }
  3831. for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
  3832. r = _base_get_port_facts(ioc, i, CAN_SLEEP);
  3833. if (r)
  3834. goto out_free_resources;
  3835. }
  3836. r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
  3837. if (r)
  3838. goto out_free_resources;
  3839. init_waitqueue_head(&ioc->reset_wq);
  3840. /* allocate memory pd handle bitmask list */
  3841. ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
  3842. if (ioc->facts.MaxDevHandle % 8)
  3843. ioc->pd_handles_sz++;
  3844. ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
  3845. GFP_KERNEL);
  3846. if (!ioc->pd_handles) {
  3847. r = -ENOMEM;
  3848. goto out_free_resources;
  3849. }
  3850. ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
  3851. GFP_KERNEL);
  3852. if (!ioc->blocking_handles) {
  3853. r = -ENOMEM;
  3854. goto out_free_resources;
  3855. }
  3856. ioc->fwfault_debug = mpt2sas_fwfault_debug;
  3857. /* base internal command bits */
  3858. mutex_init(&ioc->base_cmds.mutex);
  3859. ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3860. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  3861. /* port_enable command bits */
  3862. ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3863. ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
  3864. /* transport internal command bits */
  3865. ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3866. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  3867. mutex_init(&ioc->transport_cmds.mutex);
  3868. /* scsih internal command bits */
  3869. ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3870. ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
  3871. mutex_init(&ioc->scsih_cmds.mutex);
  3872. /* task management internal command bits */
  3873. ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3874. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3875. mutex_init(&ioc->tm_cmds.mutex);
  3876. /* config page internal command bits */
  3877. ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3878. ioc->config_cmds.status = MPT2_CMD_NOT_USED;
  3879. mutex_init(&ioc->config_cmds.mutex);
  3880. /* ctl module internal command bits */
  3881. ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3882. ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  3883. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  3884. mutex_init(&ioc->ctl_cmds.mutex);
  3885. if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
  3886. !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
  3887. !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
  3888. !ioc->ctl_cmds.sense) {
  3889. r = -ENOMEM;
  3890. goto out_free_resources;
  3891. }
  3892. if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
  3893. !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
  3894. !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
  3895. r = -ENOMEM;
  3896. goto out_free_resources;
  3897. }
  3898. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  3899. ioc->event_masks[i] = -1;
  3900. /* here we enable the events we care about */
  3901. _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
  3902. _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
  3903. _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  3904. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  3905. _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
  3906. _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
  3907. _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
  3908. _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
  3909. _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
  3910. _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
  3911. r = _base_make_ioc_operational(ioc, CAN_SLEEP);
  3912. if (r)
  3913. goto out_free_resources;
  3914. if (missing_delay[0] != -1 && missing_delay[1] != -1)
  3915. _base_update_missing_delay(ioc, missing_delay[0],
  3916. missing_delay[1]);
  3917. ioc->non_operational_loop = 0;
  3918. return 0;
  3919. out_free_resources:
  3920. ioc->remove_host = 1;
  3921. mpt2sas_base_free_resources(ioc);
  3922. _base_release_memory_pools(ioc);
  3923. pci_set_drvdata(ioc->pdev, NULL);
  3924. kfree(ioc->cpu_msix_table);
  3925. if (ioc->is_warpdrive)
  3926. kfree(ioc->reply_post_host_index);
  3927. kfree(ioc->pd_handles);
  3928. kfree(ioc->blocking_handles);
  3929. kfree(ioc->tm_cmds.reply);
  3930. kfree(ioc->transport_cmds.reply);
  3931. kfree(ioc->scsih_cmds.reply);
  3932. kfree(ioc->config_cmds.reply);
  3933. kfree(ioc->base_cmds.reply);
  3934. kfree(ioc->port_enable_cmds.reply);
  3935. kfree(ioc->ctl_cmds.reply);
  3936. kfree(ioc->ctl_cmds.sense);
  3937. kfree(ioc->pfacts);
  3938. ioc->ctl_cmds.reply = NULL;
  3939. ioc->base_cmds.reply = NULL;
  3940. ioc->tm_cmds.reply = NULL;
  3941. ioc->scsih_cmds.reply = NULL;
  3942. ioc->transport_cmds.reply = NULL;
  3943. ioc->config_cmds.reply = NULL;
  3944. ioc->pfacts = NULL;
  3945. return r;
  3946. }
  3947. /**
  3948. * mpt2sas_base_detach - remove controller instance
  3949. * @ioc: per adapter object
  3950. *
  3951. * Return nothing.
  3952. */
  3953. void
  3954. mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
  3955. {
  3956. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3957. __func__));
  3958. mpt2sas_base_stop_watchdog(ioc);
  3959. mpt2sas_base_free_resources(ioc);
  3960. _base_release_memory_pools(ioc);
  3961. pci_set_drvdata(ioc->pdev, NULL);
  3962. kfree(ioc->cpu_msix_table);
  3963. if (ioc->is_warpdrive)
  3964. kfree(ioc->reply_post_host_index);
  3965. kfree(ioc->pd_handles);
  3966. kfree(ioc->blocking_handles);
  3967. kfree(ioc->pfacts);
  3968. kfree(ioc->ctl_cmds.reply);
  3969. kfree(ioc->ctl_cmds.sense);
  3970. kfree(ioc->base_cmds.reply);
  3971. kfree(ioc->port_enable_cmds.reply);
  3972. kfree(ioc->tm_cmds.reply);
  3973. kfree(ioc->transport_cmds.reply);
  3974. kfree(ioc->scsih_cmds.reply);
  3975. kfree(ioc->config_cmds.reply);
  3976. }
  3977. /**
  3978. * _base_reset_handler - reset callback handler (for base)
  3979. * @ioc: per adapter object
  3980. * @reset_phase: phase
  3981. *
  3982. * The handler for doing any required cleanup or initialization.
  3983. *
  3984. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  3985. * MPT2_IOC_DONE_RESET
  3986. *
  3987. * Return nothing.
  3988. */
  3989. static void
  3990. _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  3991. {
  3992. mpt2sas_scsih_reset_handler(ioc, reset_phase);
  3993. mpt2sas_ctl_reset_handler(ioc, reset_phase);
  3994. switch (reset_phase) {
  3995. case MPT2_IOC_PRE_RESET:
  3996. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  3997. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  3998. break;
  3999. case MPT2_IOC_AFTER_RESET:
  4000. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  4001. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  4002. if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
  4003. ioc->transport_cmds.status |= MPT2_CMD_RESET;
  4004. mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
  4005. complete(&ioc->transport_cmds.done);
  4006. }
  4007. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  4008. ioc->base_cmds.status |= MPT2_CMD_RESET;
  4009. mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
  4010. complete(&ioc->base_cmds.done);
  4011. }
  4012. if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
  4013. ioc->port_enable_failed = 1;
  4014. ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
  4015. mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
  4016. if (ioc->is_driver_loading) {
  4017. ioc->start_scan_failed =
  4018. MPI2_IOCSTATUS_INTERNAL_ERROR;
  4019. ioc->start_scan = 0;
  4020. ioc->port_enable_cmds.status =
  4021. MPT2_CMD_NOT_USED;
  4022. } else
  4023. complete(&ioc->port_enable_cmds.done);
  4024. }
  4025. if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
  4026. ioc->config_cmds.status |= MPT2_CMD_RESET;
  4027. mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
  4028. ioc->config_cmds.smid = USHRT_MAX;
  4029. complete(&ioc->config_cmds.done);
  4030. }
  4031. break;
  4032. case MPT2_IOC_DONE_RESET:
  4033. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  4034. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  4035. break;
  4036. }
  4037. }
  4038. /**
  4039. * _wait_for_commands_to_complete - reset controller
  4040. * @ioc: Pointer to MPT_ADAPTER structure
  4041. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  4042. *
  4043. * This function waiting(3s) for all pending commands to complete
  4044. * prior to putting controller in reset.
  4045. */
  4046. static void
  4047. _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  4048. {
  4049. u32 ioc_state;
  4050. unsigned long flags;
  4051. u16 i;
  4052. ioc->pending_io_count = 0;
  4053. if (sleep_flag != CAN_SLEEP)
  4054. return;
  4055. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  4056. if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
  4057. return;
  4058. /* pending command count */
  4059. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  4060. for (i = 0; i < ioc->scsiio_depth; i++)
  4061. if (ioc->scsi_lookup[i].cb_idx != 0xFF)
  4062. ioc->pending_io_count++;
  4063. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  4064. if (!ioc->pending_io_count)
  4065. return;
  4066. /* wait for pending commands to complete */
  4067. wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
  4068. }
  4069. /**
  4070. * mpt2sas_base_hard_reset_handler - reset controller
  4071. * @ioc: Pointer to MPT_ADAPTER structure
  4072. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  4073. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  4074. *
  4075. * Returns 0 for success, non-zero for failure.
  4076. */
  4077. int
  4078. mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
  4079. enum reset_type type)
  4080. {
  4081. int r;
  4082. unsigned long flags;
  4083. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  4084. __func__));
  4085. if (ioc->pci_error_recovery) {
  4086. printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
  4087. ioc->name, __func__);
  4088. r = 0;
  4089. goto out_unlocked;
  4090. }
  4091. if (mpt2sas_fwfault_debug)
  4092. mpt2sas_halt_firmware(ioc);
  4093. /* TODO - What we really should be doing is pulling
  4094. * out all the code associated with NO_SLEEP; its never used.
  4095. * That is legacy code from mpt fusion driver, ported over.
  4096. * I will leave this BUG_ON here for now till its been resolved.
  4097. */
  4098. BUG_ON(sleep_flag == NO_SLEEP);
  4099. /* wait for an active reset in progress to complete */
  4100. if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
  4101. do {
  4102. ssleep(1);
  4103. } while (ioc->shost_recovery == 1);
  4104. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
  4105. __func__));
  4106. return ioc->ioc_reset_in_progress_status;
  4107. }
  4108. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4109. ioc->shost_recovery = 1;
  4110. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4111. _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
  4112. _wait_for_commands_to_complete(ioc, sleep_flag);
  4113. _base_mask_interrupts(ioc);
  4114. r = _base_make_ioc_ready(ioc, sleep_flag, type);
  4115. if (r)
  4116. goto out;
  4117. _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
  4118. /* If this hard reset is called while port enable is active, then
  4119. * there is no reason to call make_ioc_operational
  4120. */
  4121. if (ioc->is_driver_loading && ioc->port_enable_failed) {
  4122. ioc->remove_host = 1;
  4123. r = -EFAULT;
  4124. goto out;
  4125. }
  4126. r = _base_make_ioc_operational(ioc, sleep_flag);
  4127. if (!r)
  4128. _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
  4129. out:
  4130. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
  4131. ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
  4132. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4133. ioc->ioc_reset_in_progress_status = r;
  4134. ioc->shost_recovery = 0;
  4135. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4136. mutex_unlock(&ioc->reset_in_progress_mutex);
  4137. out_unlocked:
  4138. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
  4139. __func__));
  4140. return r;
  4141. }