mpt2sas_base.c 137 KB

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