mpt3sas_base.c 139 KB

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