lpfc_sli.c 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2008 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_transport_fc.h>
  30. #include "lpfc_hw.h"
  31. #include "lpfc_sli.h"
  32. #include "lpfc_nl.h"
  33. #include "lpfc_disc.h"
  34. #include "lpfc_scsi.h"
  35. #include "lpfc.h"
  36. #include "lpfc_crtn.h"
  37. #include "lpfc_logmsg.h"
  38. #include "lpfc_compat.h"
  39. #include "lpfc_debugfs.h"
  40. /*
  41. * Define macro to log: Mailbox command x%x cannot issue Data
  42. * This allows multiple uses of lpfc_msgBlk0311
  43. * w/o perturbing log msg utility.
  44. */
  45. #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
  46. lpfc_printf_log(phba, \
  47. KERN_INFO, \
  48. LOG_MBOX | LOG_SLI, \
  49. "(%d):0311 Mailbox command x%x cannot " \
  50. "issue Data: x%x x%x x%x\n", \
  51. pmbox->vport ? pmbox->vport->vpi : 0, \
  52. pmbox->mb.mbxCommand, \
  53. phba->pport->port_state, \
  54. psli->sli_flag, \
  55. flag)
  56. /* There are only four IOCB completion types. */
  57. typedef enum _lpfc_iocb_type {
  58. LPFC_UNKNOWN_IOCB,
  59. LPFC_UNSOL_IOCB,
  60. LPFC_SOL_IOCB,
  61. LPFC_ABORT_IOCB
  62. } lpfc_iocb_type;
  63. /**
  64. * lpfc_cmd_iocb: Get next command iocb entry in the ring.
  65. * @phba: Pointer to HBA context object.
  66. * @pring: Pointer to driver SLI ring object.
  67. *
  68. * This function returns pointer to next command iocb entry
  69. * in the command ring. The caller must hold hbalock to prevent
  70. * other threads consume the next command iocb.
  71. * SLI-2/SLI-3 provide different sized iocbs.
  72. **/
  73. static inline IOCB_t *
  74. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  75. {
  76. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  77. pring->cmdidx * phba->iocb_cmd_size);
  78. }
  79. /**
  80. * lpfc_resp_iocb: Get next response iocb entry in the ring.
  81. * @phba: Pointer to HBA context object.
  82. * @pring: Pointer to driver SLI ring object.
  83. *
  84. * This function returns pointer to next response iocb entry
  85. * in the response ring. The caller must hold hbalock to make sure
  86. * that no other thread consume the next response iocb.
  87. * SLI-2/SLI-3 provide different sized iocbs.
  88. **/
  89. static inline IOCB_t *
  90. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  91. {
  92. return (IOCB_t *) (((char *) pring->rspringaddr) +
  93. pring->rspidx * phba->iocb_rsp_size);
  94. }
  95. /**
  96. * __lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
  97. * @phba: Pointer to HBA context object.
  98. *
  99. * This function is called with hbalock held. This function
  100. * allocates a new driver iocb object from the iocb pool. If the
  101. * allocation is successful, it returns pointer to the newly
  102. * allocated iocb object else it returns NULL.
  103. **/
  104. static struct lpfc_iocbq *
  105. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  106. {
  107. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  108. struct lpfc_iocbq * iocbq = NULL;
  109. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  110. return iocbq;
  111. }
  112. /**
  113. * lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
  114. * @phba: Pointer to HBA context object.
  115. *
  116. * This function is called with no lock held. This function
  117. * allocates a new driver iocb object from the iocb pool. If the
  118. * allocation is successful, it returns pointer to the newly
  119. * allocated iocb object else it returns NULL.
  120. **/
  121. struct lpfc_iocbq *
  122. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  123. {
  124. struct lpfc_iocbq * iocbq = NULL;
  125. unsigned long iflags;
  126. spin_lock_irqsave(&phba->hbalock, iflags);
  127. iocbq = __lpfc_sli_get_iocbq(phba);
  128. spin_unlock_irqrestore(&phba->hbalock, iflags);
  129. return iocbq;
  130. }
  131. /**
  132. * __lpfc_sli_release_iocbq: Release iocb to the iocb pool.
  133. * @phba: Pointer to HBA context object.
  134. * @iocbq: Pointer to driver iocb object.
  135. *
  136. * This function is called with hbalock held to release driver
  137. * iocb object to the iocb pool. The iotag in the iocb object
  138. * does not change for each use of the iocb object. This function
  139. * clears all other fields of the iocb object when it is freed.
  140. **/
  141. static void
  142. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  143. {
  144. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  145. /*
  146. * Clean all volatile data fields, preserve iotag and node struct.
  147. */
  148. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  149. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  150. }
  151. /**
  152. * lpfc_sli_release_iocbq: Release iocb to the iocb pool.
  153. * @phba: Pointer to HBA context object.
  154. * @iocbq: Pointer to driver iocb object.
  155. *
  156. * This function is called with no lock held to release the iocb to
  157. * iocb pool.
  158. **/
  159. void
  160. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  161. {
  162. unsigned long iflags;
  163. /*
  164. * Clean all volatile data fields, preserve iotag and node struct.
  165. */
  166. spin_lock_irqsave(&phba->hbalock, iflags);
  167. __lpfc_sli_release_iocbq(phba, iocbq);
  168. spin_unlock_irqrestore(&phba->hbalock, iflags);
  169. }
  170. /**
  171. * lpfc_sli_iocb_cmd_type: Get the iocb type.
  172. * @iocb_cmnd : iocb command code.
  173. *
  174. * This function is called by ring event handler function to get the iocb type.
  175. * This function translates the iocb command to an iocb command type used to
  176. * decide the final disposition of each completed IOCB.
  177. * The function returns
  178. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  179. * LPFC_SOL_IOCB if it is a solicited iocb completion
  180. * LPFC_ABORT_IOCB if it is an abort iocb
  181. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  182. *
  183. * The caller is not required to hold any lock.
  184. **/
  185. static lpfc_iocb_type
  186. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  187. {
  188. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  189. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  190. return 0;
  191. switch (iocb_cmnd) {
  192. case CMD_XMIT_SEQUENCE_CR:
  193. case CMD_XMIT_SEQUENCE_CX:
  194. case CMD_XMIT_BCAST_CN:
  195. case CMD_XMIT_BCAST_CX:
  196. case CMD_ELS_REQUEST_CR:
  197. case CMD_ELS_REQUEST_CX:
  198. case CMD_CREATE_XRI_CR:
  199. case CMD_CREATE_XRI_CX:
  200. case CMD_GET_RPI_CN:
  201. case CMD_XMIT_ELS_RSP_CX:
  202. case CMD_GET_RPI_CR:
  203. case CMD_FCP_IWRITE_CR:
  204. case CMD_FCP_IWRITE_CX:
  205. case CMD_FCP_IREAD_CR:
  206. case CMD_FCP_IREAD_CX:
  207. case CMD_FCP_ICMND_CR:
  208. case CMD_FCP_ICMND_CX:
  209. case CMD_FCP_TSEND_CX:
  210. case CMD_FCP_TRSP_CX:
  211. case CMD_FCP_TRECEIVE_CX:
  212. case CMD_FCP_AUTO_TRSP_CX:
  213. case CMD_ADAPTER_MSG:
  214. case CMD_ADAPTER_DUMP:
  215. case CMD_XMIT_SEQUENCE64_CR:
  216. case CMD_XMIT_SEQUENCE64_CX:
  217. case CMD_XMIT_BCAST64_CN:
  218. case CMD_XMIT_BCAST64_CX:
  219. case CMD_ELS_REQUEST64_CR:
  220. case CMD_ELS_REQUEST64_CX:
  221. case CMD_FCP_IWRITE64_CR:
  222. case CMD_FCP_IWRITE64_CX:
  223. case CMD_FCP_IREAD64_CR:
  224. case CMD_FCP_IREAD64_CX:
  225. case CMD_FCP_ICMND64_CR:
  226. case CMD_FCP_ICMND64_CX:
  227. case CMD_FCP_TSEND64_CX:
  228. case CMD_FCP_TRSP64_CX:
  229. case CMD_FCP_TRECEIVE64_CX:
  230. case CMD_GEN_REQUEST64_CR:
  231. case CMD_GEN_REQUEST64_CX:
  232. case CMD_XMIT_ELS_RSP64_CX:
  233. type = LPFC_SOL_IOCB;
  234. break;
  235. case CMD_ABORT_XRI_CN:
  236. case CMD_ABORT_XRI_CX:
  237. case CMD_CLOSE_XRI_CN:
  238. case CMD_CLOSE_XRI_CX:
  239. case CMD_XRI_ABORTED_CX:
  240. case CMD_ABORT_MXRI64_CN:
  241. type = LPFC_ABORT_IOCB;
  242. break;
  243. case CMD_RCV_SEQUENCE_CX:
  244. case CMD_RCV_ELS_REQ_CX:
  245. case CMD_RCV_SEQUENCE64_CX:
  246. case CMD_RCV_ELS_REQ64_CX:
  247. case CMD_ASYNC_STATUS:
  248. case CMD_IOCB_RCV_SEQ64_CX:
  249. case CMD_IOCB_RCV_ELS64_CX:
  250. case CMD_IOCB_RCV_CONT64_CX:
  251. case CMD_IOCB_RET_XRI64_CX:
  252. type = LPFC_UNSOL_IOCB;
  253. break;
  254. case CMD_IOCB_XMIT_MSEQ64_CR:
  255. case CMD_IOCB_XMIT_MSEQ64_CX:
  256. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  257. case CMD_IOCB_RCV_ELS_LIST64_CX:
  258. case CMD_IOCB_CLOSE_EXTENDED_CN:
  259. case CMD_IOCB_ABORT_EXTENDED_CN:
  260. case CMD_IOCB_RET_HBQE64_CN:
  261. case CMD_IOCB_FCP_IBIDIR64_CR:
  262. case CMD_IOCB_FCP_IBIDIR64_CX:
  263. case CMD_IOCB_FCP_ITASKMGT64_CX:
  264. case CMD_IOCB_LOGENTRY_CN:
  265. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  266. printk("%s - Unhandled SLI-3 Command x%x\n",
  267. __func__, iocb_cmnd);
  268. type = LPFC_UNKNOWN_IOCB;
  269. break;
  270. default:
  271. type = LPFC_UNKNOWN_IOCB;
  272. break;
  273. }
  274. return type;
  275. }
  276. /**
  277. * lpfc_sli_ring_map: Issue config_ring mbox for all rings.
  278. * @phba: Pointer to HBA context object.
  279. *
  280. * This function is called from SLI initialization code
  281. * to configure every ring of the HBA's SLI interface. The
  282. * caller is not required to hold any lock. This function issues
  283. * a config_ring mailbox command for each ring.
  284. * This function returns zero if successful else returns a negative
  285. * error code.
  286. **/
  287. static int
  288. lpfc_sli_ring_map(struct lpfc_hba *phba)
  289. {
  290. struct lpfc_sli *psli = &phba->sli;
  291. LPFC_MBOXQ_t *pmb;
  292. MAILBOX_t *pmbox;
  293. int i, rc, ret = 0;
  294. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  295. if (!pmb)
  296. return -ENOMEM;
  297. pmbox = &pmb->mb;
  298. phba->link_state = LPFC_INIT_MBX_CMDS;
  299. for (i = 0; i < psli->num_rings; i++) {
  300. lpfc_config_ring(phba, i, pmb);
  301. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  302. if (rc != MBX_SUCCESS) {
  303. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  304. "0446 Adapter failed to init (%d), "
  305. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  306. "ring %d\n",
  307. rc, pmbox->mbxCommand,
  308. pmbox->mbxStatus, i);
  309. phba->link_state = LPFC_HBA_ERROR;
  310. ret = -ENXIO;
  311. break;
  312. }
  313. }
  314. mempool_free(pmb, phba->mbox_mem_pool);
  315. return ret;
  316. }
  317. /**
  318. * lpfc_sli_ringtxcmpl_put: Adds new iocb to the txcmplq.
  319. * @phba: Pointer to HBA context object.
  320. * @pring: Pointer to driver SLI ring object.
  321. * @piocb: Pointer to the driver iocb object.
  322. *
  323. * This function is called with hbalock held. The function adds the
  324. * new iocb to txcmplq of the given ring. This function always returns
  325. * 0. If this function is called for ELS ring, this function checks if
  326. * there is a vport associated with the ELS command. This function also
  327. * starts els_tmofunc timer if this is an ELS command.
  328. **/
  329. static int
  330. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  331. struct lpfc_iocbq *piocb)
  332. {
  333. list_add_tail(&piocb->list, &pring->txcmplq);
  334. pring->txcmplq_cnt++;
  335. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  336. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  337. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  338. if (!piocb->vport)
  339. BUG();
  340. else
  341. mod_timer(&piocb->vport->els_tmofunc,
  342. jiffies + HZ * (phba->fc_ratov << 1));
  343. }
  344. return 0;
  345. }
  346. /**
  347. * lpfc_sli_ringtx_get: Get first element of the txq.
  348. * @phba: Pointer to HBA context object.
  349. * @pring: Pointer to driver SLI ring object.
  350. *
  351. * This function is called with hbalock held to get next
  352. * iocb in txq of the given ring. If there is any iocb in
  353. * the txq, the function returns first iocb in the list after
  354. * removing the iocb from the list, else it returns NULL.
  355. **/
  356. static struct lpfc_iocbq *
  357. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  358. {
  359. struct lpfc_iocbq *cmd_iocb;
  360. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  361. if (cmd_iocb != NULL)
  362. pring->txq_cnt--;
  363. return cmd_iocb;
  364. }
  365. /**
  366. * lpfc_sli_next_iocb_slot: Get next iocb slot in the ring.
  367. * @phba: Pointer to HBA context object.
  368. * @pring: Pointer to driver SLI ring object.
  369. *
  370. * This function is called with hbalock held and the caller must post the
  371. * iocb without releasing the lock. If the caller releases the lock,
  372. * iocb slot returned by the function is not guaranteed to be available.
  373. * The function returns pointer to the next available iocb slot if there
  374. * is available slot in the ring, else it returns NULL.
  375. * If the get index of the ring is ahead of the put index, the function
  376. * will post an error attention event to the worker thread to take the
  377. * HBA to offline state.
  378. **/
  379. static IOCB_t *
  380. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  381. {
  382. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  383. uint32_t max_cmd_idx = pring->numCiocb;
  384. if ((pring->next_cmdidx == pring->cmdidx) &&
  385. (++pring->next_cmdidx >= max_cmd_idx))
  386. pring->next_cmdidx = 0;
  387. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  388. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  389. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  390. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  391. "0315 Ring %d issue: portCmdGet %d "
  392. "is bigger then cmd ring %d\n",
  393. pring->ringno,
  394. pring->local_getidx, max_cmd_idx);
  395. phba->link_state = LPFC_HBA_ERROR;
  396. /*
  397. * All error attention handlers are posted to
  398. * worker thread
  399. */
  400. phba->work_ha |= HA_ERATT;
  401. phba->work_hs = HS_FFER3;
  402. lpfc_worker_wake_up(phba);
  403. return NULL;
  404. }
  405. if (pring->local_getidx == pring->next_cmdidx)
  406. return NULL;
  407. }
  408. return lpfc_cmd_iocb(phba, pring);
  409. }
  410. /**
  411. * lpfc_sli_next_iotag: Get an iotag for the iocb.
  412. * @phba: Pointer to HBA context object.
  413. * @iocbq: Pointer to driver iocb object.
  414. *
  415. * This function gets an iotag for the iocb. If there is no unused iotag and
  416. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  417. * array and assigns a new iotag.
  418. * The function returns the allocated iotag if successful, else returns zero.
  419. * Zero is not a valid iotag.
  420. * The caller is not required to hold any lock.
  421. **/
  422. uint16_t
  423. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  424. {
  425. struct lpfc_iocbq **new_arr;
  426. struct lpfc_iocbq **old_arr;
  427. size_t new_len;
  428. struct lpfc_sli *psli = &phba->sli;
  429. uint16_t iotag;
  430. spin_lock_irq(&phba->hbalock);
  431. iotag = psli->last_iotag;
  432. if(++iotag < psli->iocbq_lookup_len) {
  433. psli->last_iotag = iotag;
  434. psli->iocbq_lookup[iotag] = iocbq;
  435. spin_unlock_irq(&phba->hbalock);
  436. iocbq->iotag = iotag;
  437. return iotag;
  438. } else if (psli->iocbq_lookup_len < (0xffff
  439. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  440. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  441. spin_unlock_irq(&phba->hbalock);
  442. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  443. GFP_KERNEL);
  444. if (new_arr) {
  445. spin_lock_irq(&phba->hbalock);
  446. old_arr = psli->iocbq_lookup;
  447. if (new_len <= psli->iocbq_lookup_len) {
  448. /* highly unprobable case */
  449. kfree(new_arr);
  450. iotag = psli->last_iotag;
  451. if(++iotag < psli->iocbq_lookup_len) {
  452. psli->last_iotag = iotag;
  453. psli->iocbq_lookup[iotag] = iocbq;
  454. spin_unlock_irq(&phba->hbalock);
  455. iocbq->iotag = iotag;
  456. return iotag;
  457. }
  458. spin_unlock_irq(&phba->hbalock);
  459. return 0;
  460. }
  461. if (psli->iocbq_lookup)
  462. memcpy(new_arr, old_arr,
  463. ((psli->last_iotag + 1) *
  464. sizeof (struct lpfc_iocbq *)));
  465. psli->iocbq_lookup = new_arr;
  466. psli->iocbq_lookup_len = new_len;
  467. psli->last_iotag = iotag;
  468. psli->iocbq_lookup[iotag] = iocbq;
  469. spin_unlock_irq(&phba->hbalock);
  470. iocbq->iotag = iotag;
  471. kfree(old_arr);
  472. return iotag;
  473. }
  474. } else
  475. spin_unlock_irq(&phba->hbalock);
  476. lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
  477. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  478. psli->last_iotag);
  479. return 0;
  480. }
  481. /**
  482. * lpfc_sli_submit_iocb: Submit an iocb to the firmware.
  483. * @phba: Pointer to HBA context object.
  484. * @pring: Pointer to driver SLI ring object.
  485. * @iocb: Pointer to iocb slot in the ring.
  486. * @nextiocb: Pointer to driver iocb object which need to be
  487. * posted to firmware.
  488. *
  489. * This function is called with hbalock held to post a new iocb to
  490. * the firmware. This function copies the new iocb to ring iocb slot and
  491. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  492. * a completion call back for this iocb else the function will free the
  493. * iocb object.
  494. **/
  495. static void
  496. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  497. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  498. {
  499. /*
  500. * Set up an iotag
  501. */
  502. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  503. if (pring->ringno == LPFC_ELS_RING) {
  504. lpfc_debugfs_slow_ring_trc(phba,
  505. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  506. *(((uint32_t *) &nextiocb->iocb) + 4),
  507. *(((uint32_t *) &nextiocb->iocb) + 6),
  508. *(((uint32_t *) &nextiocb->iocb) + 7));
  509. }
  510. /*
  511. * Issue iocb command to adapter
  512. */
  513. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  514. wmb();
  515. pring->stats.iocb_cmd++;
  516. /*
  517. * If there is no completion routine to call, we can release the
  518. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  519. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  520. */
  521. if (nextiocb->iocb_cmpl)
  522. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  523. else
  524. __lpfc_sli_release_iocbq(phba, nextiocb);
  525. /*
  526. * Let the HBA know what IOCB slot will be the next one the
  527. * driver will put a command into.
  528. */
  529. pring->cmdidx = pring->next_cmdidx;
  530. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  531. }
  532. /**
  533. * lpfc_sli_update_full_ring: Update the chip attention register.
  534. * @phba: Pointer to HBA context object.
  535. * @pring: Pointer to driver SLI ring object.
  536. *
  537. * The caller is not required to hold any lock for calling this function.
  538. * This function updates the chip attention bits for the ring to inform firmware
  539. * that there are pending work to be done for this ring and requests an
  540. * interrupt when there is space available in the ring. This function is
  541. * called when the driver is unable to post more iocbs to the ring due
  542. * to unavailability of space in the ring.
  543. **/
  544. static void
  545. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  546. {
  547. int ringno = pring->ringno;
  548. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  549. wmb();
  550. /*
  551. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  552. * The HBA will tell us when an IOCB entry is available.
  553. */
  554. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  555. readl(phba->CAregaddr); /* flush */
  556. pring->stats.iocb_cmd_full++;
  557. }
  558. /**
  559. * lpfc_sli_update_ring: Update chip attention register.
  560. * @phba: Pointer to HBA context object.
  561. * @pring: Pointer to driver SLI ring object.
  562. *
  563. * This function updates the chip attention register bit for the
  564. * given ring to inform HBA that there is more work to be done
  565. * in this ring. The caller is not required to hold any lock.
  566. **/
  567. static void
  568. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  569. {
  570. int ringno = pring->ringno;
  571. /*
  572. * Tell the HBA that there is work to do in this ring.
  573. */
  574. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  575. wmb();
  576. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  577. readl(phba->CAregaddr); /* flush */
  578. }
  579. }
  580. /**
  581. * lpfc_sli_resume_iocb: Process iocbs in the txq.
  582. * @phba: Pointer to HBA context object.
  583. * @pring: Pointer to driver SLI ring object.
  584. *
  585. * This function is called with hbalock held to post pending iocbs
  586. * in the txq to the firmware. This function is called when driver
  587. * detects space available in the ring.
  588. **/
  589. static void
  590. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  591. {
  592. IOCB_t *iocb;
  593. struct lpfc_iocbq *nextiocb;
  594. /*
  595. * Check to see if:
  596. * (a) there is anything on the txq to send
  597. * (b) link is up
  598. * (c) link attention events can be processed (fcp ring only)
  599. * (d) IOCB processing is not blocked by the outstanding mbox command.
  600. */
  601. if (pring->txq_cnt &&
  602. lpfc_is_link_up(phba) &&
  603. (pring->ringno != phba->sli.fcp_ring ||
  604. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  605. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  606. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  607. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  608. if (iocb)
  609. lpfc_sli_update_ring(phba, pring);
  610. else
  611. lpfc_sli_update_full_ring(phba, pring);
  612. }
  613. return;
  614. }
  615. /**
  616. * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
  617. * @phba: Pointer to HBA context object.
  618. * @hbqno: HBQ number.
  619. *
  620. * This function is called with hbalock held to get the next
  621. * available slot for the given HBQ. If there is free slot
  622. * available for the HBQ it will return pointer to the next available
  623. * HBQ entry else it will return NULL.
  624. **/
  625. static struct lpfc_hbq_entry *
  626. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  627. {
  628. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  629. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  630. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  631. hbqp->next_hbqPutIdx = 0;
  632. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  633. uint32_t raw_index = phba->hbq_get[hbqno];
  634. uint32_t getidx = le32_to_cpu(raw_index);
  635. hbqp->local_hbqGetIdx = getidx;
  636. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  637. lpfc_printf_log(phba, KERN_ERR,
  638. LOG_SLI | LOG_VPORT,
  639. "1802 HBQ %d: local_hbqGetIdx "
  640. "%u is > than hbqp->entry_count %u\n",
  641. hbqno, hbqp->local_hbqGetIdx,
  642. hbqp->entry_count);
  643. phba->link_state = LPFC_HBA_ERROR;
  644. return NULL;
  645. }
  646. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  647. return NULL;
  648. }
  649. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  650. hbqp->hbqPutIdx;
  651. }
  652. /**
  653. * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
  654. * @phba: Pointer to HBA context object.
  655. *
  656. * This function is called with no lock held to free all the
  657. * hbq buffers while uninitializing the SLI interface. It also
  658. * frees the HBQ buffers returned by the firmware but not yet
  659. * processed by the upper layers.
  660. **/
  661. void
  662. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  663. {
  664. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  665. struct hbq_dmabuf *hbq_buf;
  666. unsigned long flags;
  667. int i, hbq_count;
  668. uint32_t hbqno;
  669. hbq_count = lpfc_sli_hbq_count();
  670. /* Return all memory used by all HBQs */
  671. spin_lock_irqsave(&phba->hbalock, flags);
  672. for (i = 0; i < hbq_count; ++i) {
  673. list_for_each_entry_safe(dmabuf, next_dmabuf,
  674. &phba->hbqs[i].hbq_buffer_list, list) {
  675. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  676. list_del(&hbq_buf->dbuf.list);
  677. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  678. }
  679. phba->hbqs[i].buffer_count = 0;
  680. }
  681. /* Return all HBQ buffer that are in-fly */
  682. list_for_each_entry_safe(dmabuf, next_dmabuf,
  683. &phba->hbqbuf_in_list, list) {
  684. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  685. list_del(&hbq_buf->dbuf.list);
  686. if (hbq_buf->tag == -1) {
  687. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  688. (phba, hbq_buf);
  689. } else {
  690. hbqno = hbq_buf->tag >> 16;
  691. if (hbqno >= LPFC_MAX_HBQS)
  692. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  693. (phba, hbq_buf);
  694. else
  695. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  696. hbq_buf);
  697. }
  698. }
  699. /* Mark the HBQs not in use */
  700. phba->hbq_in_use = 0;
  701. spin_unlock_irqrestore(&phba->hbalock, flags);
  702. }
  703. /**
  704. * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
  705. * @phba: Pointer to HBA context object.
  706. * @hbqno: HBQ number.
  707. * @hbq_buf: Pointer to HBQ buffer.
  708. *
  709. * This function is called with the hbalock held to post a
  710. * hbq buffer to the firmware. If the function finds an empty
  711. * slot in the HBQ, it will post the buffer. The function will return
  712. * pointer to the hbq entry if it successfully post the buffer
  713. * else it will return NULL.
  714. **/
  715. static struct lpfc_hbq_entry *
  716. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  717. struct hbq_dmabuf *hbq_buf)
  718. {
  719. struct lpfc_hbq_entry *hbqe;
  720. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  721. /* Get next HBQ entry slot to use */
  722. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  723. if (hbqe) {
  724. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  725. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  726. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  727. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  728. hbqe->bde.tus.f.bdeFlags = 0;
  729. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  730. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  731. /* Sync SLIM */
  732. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  733. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  734. /* flush */
  735. readl(phba->hbq_put + hbqno);
  736. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  737. }
  738. return hbqe;
  739. }
  740. /* HBQ for ELS and CT traffic. */
  741. static struct lpfc_hbq_init lpfc_els_hbq = {
  742. .rn = 1,
  743. .entry_count = 200,
  744. .mask_count = 0,
  745. .profile = 0,
  746. .ring_mask = (1 << LPFC_ELS_RING),
  747. .buffer_count = 0,
  748. .init_count = 20,
  749. .add_count = 5,
  750. };
  751. /* HBQ for the extra ring if needed */
  752. static struct lpfc_hbq_init lpfc_extra_hbq = {
  753. .rn = 1,
  754. .entry_count = 200,
  755. .mask_count = 0,
  756. .profile = 0,
  757. .ring_mask = (1 << LPFC_EXTRA_RING),
  758. .buffer_count = 0,
  759. .init_count = 0,
  760. .add_count = 5,
  761. };
  762. /* Array of HBQs */
  763. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  764. &lpfc_els_hbq,
  765. &lpfc_extra_hbq,
  766. };
  767. /**
  768. * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
  769. * @phba: Pointer to HBA context object.
  770. * @hbqno: HBQ number.
  771. * @count: Number of HBQ buffers to be posted.
  772. *
  773. * This function is called with no lock held to post more hbq buffers to the
  774. * given HBQ. The function returns the number of HBQ buffers successfully
  775. * posted.
  776. **/
  777. static int
  778. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  779. {
  780. uint32_t i, posted = 0;
  781. unsigned long flags;
  782. struct hbq_dmabuf *hbq_buffer;
  783. LIST_HEAD(hbq_buf_list);
  784. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  785. return 0;
  786. if ((phba->hbqs[hbqno].buffer_count + count) >
  787. lpfc_hbq_defs[hbqno]->entry_count)
  788. count = lpfc_hbq_defs[hbqno]->entry_count -
  789. phba->hbqs[hbqno].buffer_count;
  790. if (!count)
  791. return 0;
  792. /* Allocate HBQ entries */
  793. for (i = 0; i < count; i++) {
  794. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  795. if (!hbq_buffer)
  796. break;
  797. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  798. }
  799. /* Check whether HBQ is still in use */
  800. spin_lock_irqsave(&phba->hbalock, flags);
  801. if (!phba->hbq_in_use)
  802. goto err;
  803. while (!list_empty(&hbq_buf_list)) {
  804. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  805. dbuf.list);
  806. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  807. (hbqno << 16));
  808. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  809. phba->hbqs[hbqno].buffer_count++;
  810. posted++;
  811. } else
  812. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  813. }
  814. spin_unlock_irqrestore(&phba->hbalock, flags);
  815. return posted;
  816. err:
  817. spin_unlock_irqrestore(&phba->hbalock, flags);
  818. while (!list_empty(&hbq_buf_list)) {
  819. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  820. dbuf.list);
  821. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  822. }
  823. return 0;
  824. }
  825. /**
  826. * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
  827. * @phba: Pointer to HBA context object.
  828. * @qno: HBQ number.
  829. *
  830. * This function posts more buffers to the HBQ. This function
  831. * is called with no lock held. The function returns the number of HBQ entries
  832. * successfully allocated.
  833. **/
  834. int
  835. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  836. {
  837. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  838. lpfc_hbq_defs[qno]->add_count));
  839. }
  840. /**
  841. * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
  842. * @phba: Pointer to HBA context object.
  843. * @qno: HBQ queue number.
  844. *
  845. * This function is called from SLI initialization code path with
  846. * no lock held to post initial HBQ buffers to firmware. The
  847. * function returns the number of HBQ entries successfully allocated.
  848. **/
  849. static int
  850. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  851. {
  852. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  853. lpfc_hbq_defs[qno]->init_count));
  854. }
  855. /**
  856. * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
  857. * @phba: Pointer to HBA context object.
  858. * @tag: Tag of the hbq buffer.
  859. *
  860. * This function is called with hbalock held. This function searches
  861. * for the hbq buffer associated with the given tag in the hbq buffer
  862. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  863. * it returns NULL.
  864. **/
  865. static struct hbq_dmabuf *
  866. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  867. {
  868. struct lpfc_dmabuf *d_buf;
  869. struct hbq_dmabuf *hbq_buf;
  870. uint32_t hbqno;
  871. hbqno = tag >> 16;
  872. if (hbqno >= LPFC_MAX_HBQS)
  873. return NULL;
  874. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  875. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  876. if (hbq_buf->tag == tag) {
  877. return hbq_buf;
  878. }
  879. }
  880. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  881. "1803 Bad hbq tag. Data: x%x x%x\n",
  882. tag, phba->hbqs[tag >> 16].buffer_count);
  883. return NULL;
  884. }
  885. /**
  886. * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
  887. * @phba: Pointer to HBA context object.
  888. * @hbq_buffer: Pointer to HBQ buffer.
  889. *
  890. * This function is called with hbalock. This function gives back
  891. * the hbq buffer to firmware. If the HBQ does not have space to
  892. * post the buffer, it will free the buffer.
  893. **/
  894. void
  895. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  896. {
  897. uint32_t hbqno;
  898. if (hbq_buffer) {
  899. hbqno = hbq_buffer->tag >> 16;
  900. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  901. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  902. }
  903. }
  904. }
  905. /**
  906. * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
  907. * @mbxCommand: mailbox command code.
  908. *
  909. * This function is called by the mailbox event handler function to verify
  910. * that the completed mailbox command is a legitimate mailbox command. If the
  911. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  912. * and the mailbox event handler will take the HBA offline.
  913. **/
  914. static int
  915. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  916. {
  917. uint8_t ret;
  918. switch (mbxCommand) {
  919. case MBX_LOAD_SM:
  920. case MBX_READ_NV:
  921. case MBX_WRITE_NV:
  922. case MBX_WRITE_VPARMS:
  923. case MBX_RUN_BIU_DIAG:
  924. case MBX_INIT_LINK:
  925. case MBX_DOWN_LINK:
  926. case MBX_CONFIG_LINK:
  927. case MBX_CONFIG_RING:
  928. case MBX_RESET_RING:
  929. case MBX_READ_CONFIG:
  930. case MBX_READ_RCONFIG:
  931. case MBX_READ_SPARM:
  932. case MBX_READ_STATUS:
  933. case MBX_READ_RPI:
  934. case MBX_READ_XRI:
  935. case MBX_READ_REV:
  936. case MBX_READ_LNK_STAT:
  937. case MBX_REG_LOGIN:
  938. case MBX_UNREG_LOGIN:
  939. case MBX_READ_LA:
  940. case MBX_CLEAR_LA:
  941. case MBX_DUMP_MEMORY:
  942. case MBX_DUMP_CONTEXT:
  943. case MBX_RUN_DIAGS:
  944. case MBX_RESTART:
  945. case MBX_UPDATE_CFG:
  946. case MBX_DOWN_LOAD:
  947. case MBX_DEL_LD_ENTRY:
  948. case MBX_RUN_PROGRAM:
  949. case MBX_SET_MASK:
  950. case MBX_SET_VARIABLE:
  951. case MBX_UNREG_D_ID:
  952. case MBX_KILL_BOARD:
  953. case MBX_CONFIG_FARP:
  954. case MBX_BEACON:
  955. case MBX_LOAD_AREA:
  956. case MBX_RUN_BIU_DIAG64:
  957. case MBX_CONFIG_PORT:
  958. case MBX_READ_SPARM64:
  959. case MBX_READ_RPI64:
  960. case MBX_REG_LOGIN64:
  961. case MBX_READ_LA64:
  962. case MBX_WRITE_WWN:
  963. case MBX_SET_DEBUG:
  964. case MBX_LOAD_EXP_ROM:
  965. case MBX_ASYNCEVT_ENABLE:
  966. case MBX_REG_VPI:
  967. case MBX_UNREG_VPI:
  968. case MBX_HEARTBEAT:
  969. case MBX_PORT_CAPABILITIES:
  970. case MBX_PORT_IOV_CONTROL:
  971. ret = mbxCommand;
  972. break;
  973. default:
  974. ret = MBX_SHUTDOWN;
  975. break;
  976. }
  977. return ret;
  978. }
  979. /**
  980. * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
  981. * lpfc_sli_issue_mbox_wait.
  982. * @phba: Pointer to HBA context object.
  983. * @pmboxq: Pointer to mailbox command.
  984. *
  985. * This is completion handler function for mailbox commands issued from
  986. * lpfc_sli_issue_mbox_wait function. This function is called by the
  987. * mailbox event handler function with no lock held. This function
  988. * will wake up thread waiting on the wait queue pointed by context1
  989. * of the mailbox.
  990. **/
  991. static void
  992. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  993. {
  994. wait_queue_head_t *pdone_q;
  995. unsigned long drvr_flag;
  996. /*
  997. * If pdone_q is empty, the driver thread gave up waiting and
  998. * continued running.
  999. */
  1000. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1001. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1002. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1003. if (pdone_q)
  1004. wake_up_interruptible(pdone_q);
  1005. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1006. return;
  1007. }
  1008. /**
  1009. * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
  1010. * @phba: Pointer to HBA context object.
  1011. * @pmb: Pointer to mailbox object.
  1012. *
  1013. * This function is the default mailbox completion handler. It
  1014. * frees the memory resources associated with the completed mailbox
  1015. * command. If the completed command is a REG_LOGIN mailbox command,
  1016. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1017. **/
  1018. void
  1019. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1020. {
  1021. struct lpfc_dmabuf *mp;
  1022. uint16_t rpi;
  1023. int rc;
  1024. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1025. if (mp) {
  1026. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1027. kfree(mp);
  1028. }
  1029. /*
  1030. * If a REG_LOGIN succeeded after node is destroyed or node
  1031. * is in re-discovery driver need to cleanup the RPI.
  1032. */
  1033. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1034. pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
  1035. !pmb->mb.mbxStatus) {
  1036. rpi = pmb->mb.un.varWords[0];
  1037. lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
  1038. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1039. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1040. if (rc != MBX_NOT_FINISHED)
  1041. return;
  1042. }
  1043. mempool_free(pmb, phba->mbox_mem_pool);
  1044. return;
  1045. }
  1046. /**
  1047. * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
  1048. * @phba: Pointer to HBA context object.
  1049. *
  1050. * This function is called with no lock held. This function processes all
  1051. * the completed mailbox commands and gives it to upper layers. The interrupt
  1052. * service routine processes mailbox completion interrupt and adds completed
  1053. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1054. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1055. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1056. * function returns the mailbox commands to the upper layer by calling the
  1057. * completion handler function of each mailbox.
  1058. **/
  1059. int
  1060. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1061. {
  1062. MAILBOX_t *pmbox;
  1063. LPFC_MBOXQ_t *pmb;
  1064. int rc;
  1065. LIST_HEAD(cmplq);
  1066. phba->sli.slistat.mbox_event++;
  1067. /* Get all completed mailboxe buffers into the cmplq */
  1068. spin_lock_irq(&phba->hbalock);
  1069. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  1070. spin_unlock_irq(&phba->hbalock);
  1071. /* Get a Mailbox buffer to setup mailbox commands for callback */
  1072. do {
  1073. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  1074. if (pmb == NULL)
  1075. break;
  1076. pmbox = &pmb->mb;
  1077. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  1078. if (pmb->vport) {
  1079. lpfc_debugfs_disc_trc(pmb->vport,
  1080. LPFC_DISC_TRC_MBOX_VPORT,
  1081. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  1082. (uint32_t)pmbox->mbxCommand,
  1083. pmbox->un.varWords[0],
  1084. pmbox->un.varWords[1]);
  1085. }
  1086. else {
  1087. lpfc_debugfs_disc_trc(phba->pport,
  1088. LPFC_DISC_TRC_MBOX,
  1089. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  1090. (uint32_t)pmbox->mbxCommand,
  1091. pmbox->un.varWords[0],
  1092. pmbox->un.varWords[1]);
  1093. }
  1094. }
  1095. /*
  1096. * It is a fatal error if unknown mbox command completion.
  1097. */
  1098. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  1099. MBX_SHUTDOWN) {
  1100. /* Unknow mailbox command compl */
  1101. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1102. "(%d):0323 Unknown Mailbox command "
  1103. "%x Cmpl\n",
  1104. pmb->vport ? pmb->vport->vpi : 0,
  1105. pmbox->mbxCommand);
  1106. phba->link_state = LPFC_HBA_ERROR;
  1107. phba->work_hs = HS_FFER3;
  1108. lpfc_handle_eratt(phba);
  1109. continue;
  1110. }
  1111. if (pmbox->mbxStatus) {
  1112. phba->sli.slistat.mbox_stat_err++;
  1113. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  1114. /* Mbox cmd cmpl error - RETRYing */
  1115. lpfc_printf_log(phba, KERN_INFO,
  1116. LOG_MBOX | LOG_SLI,
  1117. "(%d):0305 Mbox cmd cmpl "
  1118. "error - RETRYing Data: x%x "
  1119. "x%x x%x x%x\n",
  1120. pmb->vport ? pmb->vport->vpi :0,
  1121. pmbox->mbxCommand,
  1122. pmbox->mbxStatus,
  1123. pmbox->un.varWords[0],
  1124. pmb->vport->port_state);
  1125. pmbox->mbxStatus = 0;
  1126. pmbox->mbxOwner = OWN_HOST;
  1127. spin_lock_irq(&phba->hbalock);
  1128. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1129. spin_unlock_irq(&phba->hbalock);
  1130. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1131. if (rc == MBX_SUCCESS)
  1132. continue;
  1133. }
  1134. }
  1135. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  1136. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  1137. "(%d):0307 Mailbox cmd x%x Cmpl x%p "
  1138. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1139. pmb->vport ? pmb->vport->vpi : 0,
  1140. pmbox->mbxCommand,
  1141. pmb->mbox_cmpl,
  1142. *((uint32_t *) pmbox),
  1143. pmbox->un.varWords[0],
  1144. pmbox->un.varWords[1],
  1145. pmbox->un.varWords[2],
  1146. pmbox->un.varWords[3],
  1147. pmbox->un.varWords[4],
  1148. pmbox->un.varWords[5],
  1149. pmbox->un.varWords[6],
  1150. pmbox->un.varWords[7]);
  1151. if (pmb->mbox_cmpl)
  1152. pmb->mbox_cmpl(phba,pmb);
  1153. } while (1);
  1154. return 0;
  1155. }
  1156. /**
  1157. * lpfc_sli_replace_hbqbuff: Replace the HBQ buffer with a new buffer.
  1158. * @phba: Pointer to HBA context object.
  1159. * @tag: Tag for the HBQ buffer.
  1160. *
  1161. * This function is called from unsolicited event handler code path to get the
  1162. * HBQ buffer associated with an unsolicited iocb. This function is called with
  1163. * no lock held. It returns the buffer associated with the given tag and posts
  1164. * another buffer to the firmware. Note that the new buffer must be allocated
  1165. * before taking the hbalock and that the hba lock must be held until it is
  1166. * finished with the hbq entry swap.
  1167. **/
  1168. static struct lpfc_dmabuf *
  1169. lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
  1170. {
  1171. struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
  1172. uint32_t hbqno;
  1173. void *virt; /* virtual address ptr */
  1174. dma_addr_t phys; /* mapped address */
  1175. unsigned long flags;
  1176. hbqno = tag >> 16;
  1177. new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1178. /* Check whether HBQ is still in use */
  1179. spin_lock_irqsave(&phba->hbalock, flags);
  1180. if (!phba->hbq_in_use) {
  1181. if (new_hbq_entry)
  1182. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1183. new_hbq_entry);
  1184. spin_unlock_irqrestore(&phba->hbalock, flags);
  1185. return NULL;
  1186. }
  1187. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  1188. if (hbq_entry == NULL) {
  1189. if (new_hbq_entry)
  1190. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1191. new_hbq_entry);
  1192. spin_unlock_irqrestore(&phba->hbalock, flags);
  1193. return NULL;
  1194. }
  1195. list_del(&hbq_entry->dbuf.list);
  1196. if (new_hbq_entry == NULL) {
  1197. list_add_tail(&hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
  1198. spin_unlock_irqrestore(&phba->hbalock, flags);
  1199. return &hbq_entry->dbuf;
  1200. }
  1201. new_hbq_entry->tag = -1;
  1202. phys = new_hbq_entry->dbuf.phys;
  1203. virt = new_hbq_entry->dbuf.virt;
  1204. new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
  1205. new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
  1206. hbq_entry->dbuf.phys = phys;
  1207. hbq_entry->dbuf.virt = virt;
  1208. lpfc_sli_free_hbq(phba, hbq_entry);
  1209. list_add_tail(&new_hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
  1210. spin_unlock_irqrestore(&phba->hbalock, flags);
  1211. return &new_hbq_entry->dbuf;
  1212. }
  1213. /**
  1214. * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
  1215. * @phba: Pointer to HBA context object.
  1216. * @pring: Pointer to driver SLI ring object.
  1217. * @tag: buffer tag.
  1218. *
  1219. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  1220. * is set in the tag the buffer is posted for a particular exchange,
  1221. * the function will return the buffer without replacing the buffer.
  1222. * If the buffer is for unsolicited ELS or CT traffic, this function
  1223. * returns the buffer and also posts another buffer to the firmware.
  1224. **/
  1225. static struct lpfc_dmabuf *
  1226. lpfc_sli_get_buff(struct lpfc_hba *phba,
  1227. struct lpfc_sli_ring *pring,
  1228. uint32_t tag)
  1229. {
  1230. if (tag & QUE_BUFTAG_BIT)
  1231. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  1232. else
  1233. return lpfc_sli_replace_hbqbuff(phba, tag);
  1234. }
  1235. /**
  1236. * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
  1237. * @phba: Pointer to HBA context object.
  1238. * @pring: Pointer to driver SLI ring object.
  1239. * @saveq: Pointer to the unsolicited iocb.
  1240. *
  1241. * This function is called with no lock held by the ring event handler
  1242. * when there is an unsolicited iocb posted to the response ring by the
  1243. * firmware. This function gets the buffer associated with the iocbs
  1244. * and calls the event handler for the ring. This function handles both
  1245. * qring buffers and hbq buffers.
  1246. * When the function returns 1 the caller can free the iocb object otherwise
  1247. * upper layer functions will free the iocb objects.
  1248. **/
  1249. static int
  1250. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1251. struct lpfc_iocbq *saveq)
  1252. {
  1253. IOCB_t * irsp;
  1254. WORD5 * w5p;
  1255. uint32_t Rctl, Type;
  1256. uint32_t match, i;
  1257. struct lpfc_iocbq *iocbq;
  1258. struct lpfc_dmabuf *dmzbuf;
  1259. match = 0;
  1260. irsp = &(saveq->iocb);
  1261. if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
  1262. return 1;
  1263. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  1264. if (pring->lpfc_sli_rcv_async_status)
  1265. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  1266. else
  1267. lpfc_printf_log(phba,
  1268. KERN_WARNING,
  1269. LOG_SLI,
  1270. "0316 Ring %d handler: unexpected "
  1271. "ASYNC_STATUS iocb received evt_code "
  1272. "0x%x\n",
  1273. pring->ringno,
  1274. irsp->un.asyncstat.evt_code);
  1275. return 1;
  1276. }
  1277. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  1278. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  1279. if (irsp->ulpBdeCount > 0) {
  1280. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1281. irsp->un.ulpWord[3]);
  1282. lpfc_in_buf_free(phba, dmzbuf);
  1283. }
  1284. if (irsp->ulpBdeCount > 1) {
  1285. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1286. irsp->unsli3.sli3Words[3]);
  1287. lpfc_in_buf_free(phba, dmzbuf);
  1288. }
  1289. if (irsp->ulpBdeCount > 2) {
  1290. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1291. irsp->unsli3.sli3Words[7]);
  1292. lpfc_in_buf_free(phba, dmzbuf);
  1293. }
  1294. return 1;
  1295. }
  1296. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  1297. if (irsp->ulpBdeCount != 0) {
  1298. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  1299. irsp->un.ulpWord[3]);
  1300. if (!saveq->context2)
  1301. lpfc_printf_log(phba,
  1302. KERN_ERR,
  1303. LOG_SLI,
  1304. "0341 Ring %d Cannot find buffer for "
  1305. "an unsolicited iocb. tag 0x%x\n",
  1306. pring->ringno,
  1307. irsp->un.ulpWord[3]);
  1308. }
  1309. if (irsp->ulpBdeCount == 2) {
  1310. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  1311. irsp->unsli3.sli3Words[7]);
  1312. if (!saveq->context3)
  1313. lpfc_printf_log(phba,
  1314. KERN_ERR,
  1315. LOG_SLI,
  1316. "0342 Ring %d Cannot find buffer for an"
  1317. " unsolicited iocb. tag 0x%x\n",
  1318. pring->ringno,
  1319. irsp->unsli3.sli3Words[7]);
  1320. }
  1321. list_for_each_entry(iocbq, &saveq->list, list) {
  1322. irsp = &(iocbq->iocb);
  1323. if (irsp->ulpBdeCount != 0) {
  1324. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  1325. irsp->un.ulpWord[3]);
  1326. if (!iocbq->context2)
  1327. lpfc_printf_log(phba,
  1328. KERN_ERR,
  1329. LOG_SLI,
  1330. "0343 Ring %d Cannot find "
  1331. "buffer for an unsolicited iocb"
  1332. ". tag 0x%x\n", pring->ringno,
  1333. irsp->un.ulpWord[3]);
  1334. }
  1335. if (irsp->ulpBdeCount == 2) {
  1336. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  1337. irsp->unsli3.sli3Words[7]);
  1338. if (!iocbq->context3)
  1339. lpfc_printf_log(phba,
  1340. KERN_ERR,
  1341. LOG_SLI,
  1342. "0344 Ring %d Cannot find "
  1343. "buffer for an unsolicited "
  1344. "iocb. tag 0x%x\n",
  1345. pring->ringno,
  1346. irsp->unsli3.sli3Words[7]);
  1347. }
  1348. }
  1349. }
  1350. if (irsp->ulpBdeCount != 0 &&
  1351. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  1352. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  1353. int found = 0;
  1354. /* search continue save q for same XRI */
  1355. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  1356. if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
  1357. list_add_tail(&saveq->list, &iocbq->list);
  1358. found = 1;
  1359. break;
  1360. }
  1361. }
  1362. if (!found)
  1363. list_add_tail(&saveq->clist,
  1364. &pring->iocb_continue_saveq);
  1365. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  1366. list_del_init(&iocbq->clist);
  1367. saveq = iocbq;
  1368. irsp = &(saveq->iocb);
  1369. } else
  1370. return 0;
  1371. }
  1372. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  1373. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  1374. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  1375. Rctl = FC_ELS_REQ;
  1376. Type = FC_ELS_DATA;
  1377. } else {
  1378. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  1379. Rctl = w5p->hcsw.Rctl;
  1380. Type = w5p->hcsw.Type;
  1381. /* Firmware Workaround */
  1382. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  1383. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  1384. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  1385. Rctl = FC_ELS_REQ;
  1386. Type = FC_ELS_DATA;
  1387. w5p->hcsw.Rctl = Rctl;
  1388. w5p->hcsw.Type = Type;
  1389. }
  1390. }
  1391. /* unSolicited Responses */
  1392. if (pring->prt[0].profile) {
  1393. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  1394. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  1395. saveq);
  1396. match = 1;
  1397. } else {
  1398. /* We must search, based on rctl / type
  1399. for the right routine */
  1400. for (i = 0; i < pring->num_mask; i++) {
  1401. if ((pring->prt[i].rctl == Rctl)
  1402. && (pring->prt[i].type == Type)) {
  1403. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1404. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1405. (phba, pring, saveq);
  1406. match = 1;
  1407. break;
  1408. }
  1409. }
  1410. }
  1411. if (match == 0) {
  1412. /* Unexpected Rctl / Type received */
  1413. /* Ring <ringno> handler: unexpected
  1414. Rctl <Rctl> Type <Type> received */
  1415. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1416. "0313 Ring %d handler: unexpected Rctl x%x "
  1417. "Type x%x received\n",
  1418. pring->ringno, Rctl, Type);
  1419. }
  1420. return 1;
  1421. }
  1422. /**
  1423. * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
  1424. * @phba: Pointer to HBA context object.
  1425. * @pring: Pointer to driver SLI ring object.
  1426. * @prspiocb: Pointer to response iocb object.
  1427. *
  1428. * This function looks up the iocb_lookup table to get the command iocb
  1429. * corresponding to the given response iocb using the iotag of the
  1430. * response iocb. This function is called with the hbalock held.
  1431. * This function returns the command iocb object if it finds the command
  1432. * iocb else returns NULL.
  1433. **/
  1434. static struct lpfc_iocbq *
  1435. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  1436. struct lpfc_sli_ring *pring,
  1437. struct lpfc_iocbq *prspiocb)
  1438. {
  1439. struct lpfc_iocbq *cmd_iocb = NULL;
  1440. uint16_t iotag;
  1441. iotag = prspiocb->iocb.ulpIoTag;
  1442. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  1443. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  1444. list_del_init(&cmd_iocb->list);
  1445. pring->txcmplq_cnt--;
  1446. return cmd_iocb;
  1447. }
  1448. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1449. "0317 iotag x%x is out off "
  1450. "range: max iotag x%x wd0 x%x\n",
  1451. iotag, phba->sli.last_iotag,
  1452. *(((uint32_t *) &prspiocb->iocb) + 7));
  1453. return NULL;
  1454. }
  1455. /**
  1456. * lpfc_sli_process_sol_iocb: process solicited iocb completion.
  1457. * @phba: Pointer to HBA context object.
  1458. * @pring: Pointer to driver SLI ring object.
  1459. * @saveq: Pointer to the response iocb to be processed.
  1460. *
  1461. * This function is called by the ring event handler for non-fcp
  1462. * rings when there is a new response iocb in the response ring.
  1463. * The caller is not required to hold any locks. This function
  1464. * gets the command iocb associated with the response iocb and
  1465. * calls the completion handler for the command iocb. If there
  1466. * is no completion handler, the function will free the resources
  1467. * associated with command iocb. If the response iocb is for
  1468. * an already aborted command iocb, the status of the completion
  1469. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  1470. * This function always returns 1.
  1471. **/
  1472. static int
  1473. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1474. struct lpfc_iocbq *saveq)
  1475. {
  1476. struct lpfc_iocbq *cmdiocbp;
  1477. int rc = 1;
  1478. unsigned long iflag;
  1479. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  1480. spin_lock_irqsave(&phba->hbalock, iflag);
  1481. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  1482. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1483. if (cmdiocbp) {
  1484. if (cmdiocbp->iocb_cmpl) {
  1485. /*
  1486. * If an ELS command failed send an event to mgmt
  1487. * application.
  1488. */
  1489. if (saveq->iocb.ulpStatus &&
  1490. (pring->ringno == LPFC_ELS_RING) &&
  1491. (cmdiocbp->iocb.ulpCommand ==
  1492. CMD_ELS_REQUEST64_CR))
  1493. lpfc_send_els_failure_event(phba,
  1494. cmdiocbp, saveq);
  1495. /*
  1496. * Post all ELS completions to the worker thread.
  1497. * All other are passed to the completion callback.
  1498. */
  1499. if (pring->ringno == LPFC_ELS_RING) {
  1500. if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
  1501. cmdiocbp->iocb_flag &=
  1502. ~LPFC_DRIVER_ABORTED;
  1503. saveq->iocb.ulpStatus =
  1504. IOSTAT_LOCAL_REJECT;
  1505. saveq->iocb.un.ulpWord[4] =
  1506. IOERR_SLI_ABORTED;
  1507. /* Firmware could still be in progress
  1508. * of DMAing payload, so don't free data
  1509. * buffer till after a hbeat.
  1510. */
  1511. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  1512. }
  1513. }
  1514. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  1515. } else
  1516. lpfc_sli_release_iocbq(phba, cmdiocbp);
  1517. } else {
  1518. /*
  1519. * Unknown initiating command based on the response iotag.
  1520. * This could be the case on the ELS ring because of
  1521. * lpfc_els_abort().
  1522. */
  1523. if (pring->ringno != LPFC_ELS_RING) {
  1524. /*
  1525. * Ring <ringno> handler: unexpected completion IoTag
  1526. * <IoTag>
  1527. */
  1528. lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
  1529. "0322 Ring %d handler: "
  1530. "unexpected completion IoTag x%x "
  1531. "Data: x%x x%x x%x x%x\n",
  1532. pring->ringno,
  1533. saveq->iocb.ulpIoTag,
  1534. saveq->iocb.ulpStatus,
  1535. saveq->iocb.un.ulpWord[4],
  1536. saveq->iocb.ulpCommand,
  1537. saveq->iocb.ulpContext);
  1538. }
  1539. }
  1540. return rc;
  1541. }
  1542. /**
  1543. * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
  1544. * @phba: Pointer to HBA context object.
  1545. * @pring: Pointer to driver SLI ring object.
  1546. *
  1547. * This function is called from the iocb ring event handlers when
  1548. * put pointer is ahead of the get pointer for a ring. This function signal
  1549. * an error attention condition to the worker thread and the worker
  1550. * thread will transition the HBA to offline state.
  1551. **/
  1552. static void
  1553. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1554. {
  1555. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1556. /*
  1557. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  1558. * rsp ring <portRspMax>
  1559. */
  1560. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1561. "0312 Ring %d handler: portRspPut %d "
  1562. "is bigger then rsp ring %d\n",
  1563. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  1564. pring->numRiocb);
  1565. phba->link_state = LPFC_HBA_ERROR;
  1566. /*
  1567. * All error attention handlers are posted to
  1568. * worker thread
  1569. */
  1570. phba->work_ha |= HA_ERATT;
  1571. phba->work_hs = HS_FFER3;
  1572. lpfc_worker_wake_up(phba);
  1573. return;
  1574. }
  1575. /**
  1576. * lpfc_poll_eratt: Error attention polling timer timeout handler.
  1577. * @ptr: Pointer to address of HBA context object.
  1578. *
  1579. * This function is invoked by the Error Attention polling timer when the
  1580. * timer times out. It will check the SLI Error Attention register for
  1581. * possible attention events. If so, it will post an Error Attention event
  1582. * and wake up worker thread to process it. Otherwise, it will set up the
  1583. * Error Attention polling timer for the next poll.
  1584. **/
  1585. void lpfc_poll_eratt(unsigned long ptr)
  1586. {
  1587. struct lpfc_hba *phba;
  1588. uint32_t eratt = 0;
  1589. phba = (struct lpfc_hba *)ptr;
  1590. /* Check chip HA register for error event */
  1591. eratt = lpfc_sli_check_eratt(phba);
  1592. if (eratt)
  1593. /* Tell the worker thread there is work to do */
  1594. lpfc_worker_wake_up(phba);
  1595. else
  1596. /* Restart the timer for next eratt poll */
  1597. mod_timer(&phba->eratt_poll, jiffies +
  1598. HZ * LPFC_ERATT_POLL_INTERVAL);
  1599. return;
  1600. }
  1601. /**
  1602. * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
  1603. * @phba: Pointer to HBA context object.
  1604. *
  1605. * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
  1606. * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
  1607. * is enabled.
  1608. *
  1609. * The caller does not hold any lock.
  1610. * The function processes each response iocb in the response ring until it
  1611. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  1612. * LE bit set. The function will call the completion handler of the command iocb
  1613. * if the response iocb indicates a completion for a command iocb or it is
  1614. * an abort completion.
  1615. **/
  1616. void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
  1617. {
  1618. struct lpfc_sli *psli = &phba->sli;
  1619. struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
  1620. IOCB_t *irsp = NULL;
  1621. IOCB_t *entry = NULL;
  1622. struct lpfc_iocbq *cmdiocbq = NULL;
  1623. struct lpfc_iocbq rspiocbq;
  1624. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1625. uint32_t status;
  1626. uint32_t portRspPut, portRspMax;
  1627. int type;
  1628. uint32_t rsp_cmpl = 0;
  1629. uint32_t ha_copy;
  1630. unsigned long iflags;
  1631. pring->stats.iocb_event++;
  1632. /*
  1633. * The next available response entry should never exceed the maximum
  1634. * entries. If it does, treat it as an adapter hardware error.
  1635. */
  1636. portRspMax = pring->numRiocb;
  1637. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1638. if (unlikely(portRspPut >= portRspMax)) {
  1639. lpfc_sli_rsp_pointers_error(phba, pring);
  1640. return;
  1641. }
  1642. rmb();
  1643. while (pring->rspidx != portRspPut) {
  1644. entry = lpfc_resp_iocb(phba, pring);
  1645. if (++pring->rspidx >= portRspMax)
  1646. pring->rspidx = 0;
  1647. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  1648. (uint32_t *) &rspiocbq.iocb,
  1649. phba->iocb_rsp_size);
  1650. irsp = &rspiocbq.iocb;
  1651. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  1652. pring->stats.iocb_rsp++;
  1653. rsp_cmpl++;
  1654. if (unlikely(irsp->ulpStatus)) {
  1655. /* Rsp ring <ringno> error: IOCB */
  1656. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1657. "0326 Rsp Ring %d error: IOCB Data: "
  1658. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1659. pring->ringno,
  1660. irsp->un.ulpWord[0],
  1661. irsp->un.ulpWord[1],
  1662. irsp->un.ulpWord[2],
  1663. irsp->un.ulpWord[3],
  1664. irsp->un.ulpWord[4],
  1665. irsp->un.ulpWord[5],
  1666. *(uint32_t *)&irsp->un1,
  1667. *((uint32_t *)&irsp->un1 + 1));
  1668. }
  1669. switch (type) {
  1670. case LPFC_ABORT_IOCB:
  1671. case LPFC_SOL_IOCB:
  1672. /*
  1673. * Idle exchange closed via ABTS from port. No iocb
  1674. * resources need to be recovered.
  1675. */
  1676. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  1677. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1678. "0314 IOCB cmd 0x%x "
  1679. "processed. Skipping "
  1680. "completion",
  1681. irsp->ulpCommand);
  1682. break;
  1683. }
  1684. spin_lock_irqsave(&phba->hbalock, iflags);
  1685. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  1686. &rspiocbq);
  1687. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1688. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  1689. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1690. &rspiocbq);
  1691. }
  1692. break;
  1693. default:
  1694. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1695. char adaptermsg[LPFC_MAX_ADPTMSG];
  1696. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  1697. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1698. MAX_MSG_DATA);
  1699. dev_warn(&((phba->pcidev)->dev),
  1700. "lpfc%d: %s\n",
  1701. phba->brd_no, adaptermsg);
  1702. } else {
  1703. /* Unknown IOCB command */
  1704. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1705. "0321 Unknown IOCB command "
  1706. "Data: x%x, x%x x%x x%x x%x\n",
  1707. type, irsp->ulpCommand,
  1708. irsp->ulpStatus,
  1709. irsp->ulpIoTag,
  1710. irsp->ulpContext);
  1711. }
  1712. break;
  1713. }
  1714. /*
  1715. * The response IOCB has been processed. Update the ring
  1716. * pointer in SLIM. If the port response put pointer has not
  1717. * been updated, sync the pgp->rspPutInx and fetch the new port
  1718. * response put pointer.
  1719. */
  1720. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1721. if (pring->rspidx == portRspPut)
  1722. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1723. }
  1724. ha_copy = readl(phba->HAregaddr);
  1725. ha_copy >>= (LPFC_FCP_RING * 4);
  1726. if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
  1727. spin_lock_irqsave(&phba->hbalock, iflags);
  1728. pring->stats.iocb_rsp_full++;
  1729. status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
  1730. writel(status, phba->CAregaddr);
  1731. readl(phba->CAregaddr);
  1732. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1733. }
  1734. if ((ha_copy & HA_R0CE_RSP) &&
  1735. (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1736. spin_lock_irqsave(&phba->hbalock, iflags);
  1737. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1738. pring->stats.iocb_cmd_empty++;
  1739. /* Force update of the local copy of cmdGetInx */
  1740. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1741. lpfc_sli_resume_iocb(phba, pring);
  1742. if ((pring->lpfc_sli_cmd_available))
  1743. (pring->lpfc_sli_cmd_available) (phba, pring);
  1744. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1745. }
  1746. return;
  1747. }
  1748. /**
  1749. * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
  1750. * @phba: Pointer to HBA context object.
  1751. * @pring: Pointer to driver SLI ring object.
  1752. * @mask: Host attention register mask for this ring.
  1753. *
  1754. * This function is called from the interrupt context when there is a ring
  1755. * event for the fcp ring. The caller does not hold any lock.
  1756. * The function processes each response iocb in the response ring until it
  1757. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  1758. * LE bit set. The function will call the completion handler of the command iocb
  1759. * if the response iocb indicates a completion for a command iocb or it is
  1760. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  1761. * function if this is an unsolicited iocb.
  1762. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  1763. * to check it explicitly. This function always returns 1.
  1764. **/
  1765. static int
  1766. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  1767. struct lpfc_sli_ring *pring, uint32_t mask)
  1768. {
  1769. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1770. IOCB_t *irsp = NULL;
  1771. IOCB_t *entry = NULL;
  1772. struct lpfc_iocbq *cmdiocbq = NULL;
  1773. struct lpfc_iocbq rspiocbq;
  1774. uint32_t status;
  1775. uint32_t portRspPut, portRspMax;
  1776. int rc = 1;
  1777. lpfc_iocb_type type;
  1778. unsigned long iflag;
  1779. uint32_t rsp_cmpl = 0;
  1780. spin_lock_irqsave(&phba->hbalock, iflag);
  1781. pring->stats.iocb_event++;
  1782. /*
  1783. * The next available response entry should never exceed the maximum
  1784. * entries. If it does, treat it as an adapter hardware error.
  1785. */
  1786. portRspMax = pring->numRiocb;
  1787. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1788. if (unlikely(portRspPut >= portRspMax)) {
  1789. lpfc_sli_rsp_pointers_error(phba, pring);
  1790. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1791. return 1;
  1792. }
  1793. rmb();
  1794. while (pring->rspidx != portRspPut) {
  1795. /*
  1796. * Fetch an entry off the ring and copy it into a local data
  1797. * structure. The copy involves a byte-swap since the
  1798. * network byte order and pci byte orders are different.
  1799. */
  1800. entry = lpfc_resp_iocb(phba, pring);
  1801. phba->last_completion_time = jiffies;
  1802. if (++pring->rspidx >= portRspMax)
  1803. pring->rspidx = 0;
  1804. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  1805. (uint32_t *) &rspiocbq.iocb,
  1806. phba->iocb_rsp_size);
  1807. INIT_LIST_HEAD(&(rspiocbq.list));
  1808. irsp = &rspiocbq.iocb;
  1809. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  1810. pring->stats.iocb_rsp++;
  1811. rsp_cmpl++;
  1812. if (unlikely(irsp->ulpStatus)) {
  1813. /*
  1814. * If resource errors reported from HBA, reduce
  1815. * queuedepths of the SCSI device.
  1816. */
  1817. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  1818. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  1819. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1820. lpfc_rampdown_queue_depth(phba);
  1821. spin_lock_irqsave(&phba->hbalock, iflag);
  1822. }
  1823. /* Rsp ring <ringno> error: IOCB */
  1824. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1825. "0336 Rsp Ring %d error: IOCB Data: "
  1826. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1827. pring->ringno,
  1828. irsp->un.ulpWord[0],
  1829. irsp->un.ulpWord[1],
  1830. irsp->un.ulpWord[2],
  1831. irsp->un.ulpWord[3],
  1832. irsp->un.ulpWord[4],
  1833. irsp->un.ulpWord[5],
  1834. *(uint32_t *)&irsp->un1,
  1835. *((uint32_t *)&irsp->un1 + 1));
  1836. }
  1837. switch (type) {
  1838. case LPFC_ABORT_IOCB:
  1839. case LPFC_SOL_IOCB:
  1840. /*
  1841. * Idle exchange closed via ABTS from port. No iocb
  1842. * resources need to be recovered.
  1843. */
  1844. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  1845. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1846. "0333 IOCB cmd 0x%x"
  1847. " processed. Skipping"
  1848. " completion\n",
  1849. irsp->ulpCommand);
  1850. break;
  1851. }
  1852. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  1853. &rspiocbq);
  1854. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  1855. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1856. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1857. &rspiocbq);
  1858. } else {
  1859. spin_unlock_irqrestore(&phba->hbalock,
  1860. iflag);
  1861. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1862. &rspiocbq);
  1863. spin_lock_irqsave(&phba->hbalock,
  1864. iflag);
  1865. }
  1866. }
  1867. break;
  1868. case LPFC_UNSOL_IOCB:
  1869. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1870. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  1871. spin_lock_irqsave(&phba->hbalock, iflag);
  1872. break;
  1873. default:
  1874. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1875. char adaptermsg[LPFC_MAX_ADPTMSG];
  1876. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  1877. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1878. MAX_MSG_DATA);
  1879. dev_warn(&((phba->pcidev)->dev),
  1880. "lpfc%d: %s\n",
  1881. phba->brd_no, adaptermsg);
  1882. } else {
  1883. /* Unknown IOCB command */
  1884. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1885. "0334 Unknown IOCB command "
  1886. "Data: x%x, x%x x%x x%x x%x\n",
  1887. type, irsp->ulpCommand,
  1888. irsp->ulpStatus,
  1889. irsp->ulpIoTag,
  1890. irsp->ulpContext);
  1891. }
  1892. break;
  1893. }
  1894. /*
  1895. * The response IOCB has been processed. Update the ring
  1896. * pointer in SLIM. If the port response put pointer has not
  1897. * been updated, sync the pgp->rspPutInx and fetch the new port
  1898. * response put pointer.
  1899. */
  1900. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1901. if (pring->rspidx == portRspPut)
  1902. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1903. }
  1904. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  1905. pring->stats.iocb_rsp_full++;
  1906. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  1907. writel(status, phba->CAregaddr);
  1908. readl(phba->CAregaddr);
  1909. }
  1910. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1911. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1912. pring->stats.iocb_cmd_empty++;
  1913. /* Force update of the local copy of cmdGetInx */
  1914. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1915. lpfc_sli_resume_iocb(phba, pring);
  1916. if ((pring->lpfc_sli_cmd_available))
  1917. (pring->lpfc_sli_cmd_available) (phba, pring);
  1918. }
  1919. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1920. return rc;
  1921. }
  1922. /**
  1923. * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
  1924. * @phba: Pointer to HBA context object.
  1925. * @pring: Pointer to driver SLI ring object.
  1926. * @mask: Host attention register mask for this ring.
  1927. *
  1928. * This function is called from the worker thread when there is a ring
  1929. * event for non-fcp rings. The caller does not hold any lock .
  1930. * The function processes each response iocb in the response ring until it
  1931. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  1932. * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
  1933. * response iocb indicates a completion of a command iocb. The function
  1934. * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
  1935. * iocb. The function frees the resources or calls the completion handler if
  1936. * this iocb is an abort completion. The function returns 0 when the allocated
  1937. * iocbs are not freed, otherwise returns 1.
  1938. **/
  1939. int
  1940. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  1941. struct lpfc_sli_ring *pring, uint32_t mask)
  1942. {
  1943. struct lpfc_pgp *pgp;
  1944. IOCB_t *entry;
  1945. IOCB_t *irsp = NULL;
  1946. struct lpfc_iocbq *rspiocbp = NULL;
  1947. struct lpfc_iocbq *next_iocb;
  1948. struct lpfc_iocbq *cmdiocbp;
  1949. struct lpfc_iocbq *saveq;
  1950. uint8_t iocb_cmd_type;
  1951. lpfc_iocb_type type;
  1952. uint32_t status, free_saveq;
  1953. uint32_t portRspPut, portRspMax;
  1954. int rc = 1;
  1955. unsigned long iflag;
  1956. pgp = &phba->port_gp[pring->ringno];
  1957. spin_lock_irqsave(&phba->hbalock, iflag);
  1958. pring->stats.iocb_event++;
  1959. /*
  1960. * The next available response entry should never exceed the maximum
  1961. * entries. If it does, treat it as an adapter hardware error.
  1962. */
  1963. portRspMax = pring->numRiocb;
  1964. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1965. if (portRspPut >= portRspMax) {
  1966. /*
  1967. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  1968. * rsp ring <portRspMax>
  1969. */
  1970. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1971. "0303 Ring %d handler: portRspPut %d "
  1972. "is bigger then rsp ring %d\n",
  1973. pring->ringno, portRspPut, portRspMax);
  1974. phba->link_state = LPFC_HBA_ERROR;
  1975. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1976. phba->work_hs = HS_FFER3;
  1977. lpfc_handle_eratt(phba);
  1978. return 1;
  1979. }
  1980. rmb();
  1981. while (pring->rspidx != portRspPut) {
  1982. /*
  1983. * Build a completion list and call the appropriate handler.
  1984. * The process is to get the next available response iocb, get
  1985. * a free iocb from the list, copy the response data into the
  1986. * free iocb, insert to the continuation list, and update the
  1987. * next response index to slim. This process makes response
  1988. * iocb's in the ring available to DMA as fast as possible but
  1989. * pays a penalty for a copy operation. Since the iocb is
  1990. * only 32 bytes, this penalty is considered small relative to
  1991. * the PCI reads for register values and a slim write. When
  1992. * the ulpLe field is set, the entire Command has been
  1993. * received.
  1994. */
  1995. entry = lpfc_resp_iocb(phba, pring);
  1996. phba->last_completion_time = jiffies;
  1997. rspiocbp = __lpfc_sli_get_iocbq(phba);
  1998. if (rspiocbp == NULL) {
  1999. printk(KERN_ERR "%s: out of buffers! Failing "
  2000. "completion.\n", __func__);
  2001. break;
  2002. }
  2003. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  2004. phba->iocb_rsp_size);
  2005. irsp = &rspiocbp->iocb;
  2006. if (++pring->rspidx >= portRspMax)
  2007. pring->rspidx = 0;
  2008. if (pring->ringno == LPFC_ELS_RING) {
  2009. lpfc_debugfs_slow_ring_trc(phba,
  2010. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  2011. *(((uint32_t *) irsp) + 4),
  2012. *(((uint32_t *) irsp) + 6),
  2013. *(((uint32_t *) irsp) + 7));
  2014. }
  2015. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2016. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2017. pring->iocb_continueq_cnt++;
  2018. if (irsp->ulpLe) {
  2019. /*
  2020. * By default, the driver expects to free all resources
  2021. * associated with this iocb completion.
  2022. */
  2023. free_saveq = 1;
  2024. saveq = list_get_first(&pring->iocb_continueq,
  2025. struct lpfc_iocbq, list);
  2026. irsp = &(saveq->iocb);
  2027. list_del_init(&pring->iocb_continueq);
  2028. pring->iocb_continueq_cnt = 0;
  2029. pring->stats.iocb_rsp++;
  2030. /*
  2031. * If resource errors reported from HBA, reduce
  2032. * queuedepths of the SCSI device.
  2033. */
  2034. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2035. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2036. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2037. lpfc_rampdown_queue_depth(phba);
  2038. spin_lock_irqsave(&phba->hbalock, iflag);
  2039. }
  2040. if (irsp->ulpStatus) {
  2041. /* Rsp ring <ringno> error: IOCB */
  2042. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2043. "0328 Rsp Ring %d error: "
  2044. "IOCB Data: "
  2045. "x%x x%x x%x x%x "
  2046. "x%x x%x x%x x%x "
  2047. "x%x x%x x%x x%x "
  2048. "x%x x%x x%x x%x\n",
  2049. pring->ringno,
  2050. irsp->un.ulpWord[0],
  2051. irsp->un.ulpWord[1],
  2052. irsp->un.ulpWord[2],
  2053. irsp->un.ulpWord[3],
  2054. irsp->un.ulpWord[4],
  2055. irsp->un.ulpWord[5],
  2056. *(((uint32_t *) irsp) + 6),
  2057. *(((uint32_t *) irsp) + 7),
  2058. *(((uint32_t *) irsp) + 8),
  2059. *(((uint32_t *) irsp) + 9),
  2060. *(((uint32_t *) irsp) + 10),
  2061. *(((uint32_t *) irsp) + 11),
  2062. *(((uint32_t *) irsp) + 12),
  2063. *(((uint32_t *) irsp) + 13),
  2064. *(((uint32_t *) irsp) + 14),
  2065. *(((uint32_t *) irsp) + 15));
  2066. }
  2067. /*
  2068. * Fetch the IOCB command type and call the correct
  2069. * completion routine. Solicited and Unsolicited
  2070. * IOCBs on the ELS ring get freed back to the
  2071. * lpfc_iocb_list by the discovery kernel thread.
  2072. */
  2073. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2074. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2075. if (type == LPFC_SOL_IOCB) {
  2076. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2077. rc = lpfc_sli_process_sol_iocb(phba, pring,
  2078. saveq);
  2079. spin_lock_irqsave(&phba->hbalock, iflag);
  2080. } else if (type == LPFC_UNSOL_IOCB) {
  2081. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2082. rc = lpfc_sli_process_unsol_iocb(phba, pring,
  2083. saveq);
  2084. spin_lock_irqsave(&phba->hbalock, iflag);
  2085. if (!rc)
  2086. free_saveq = 0;
  2087. } else if (type == LPFC_ABORT_IOCB) {
  2088. if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
  2089. ((cmdiocbp =
  2090. lpfc_sli_iocbq_lookup(phba, pring,
  2091. saveq)))) {
  2092. /* Call the specified completion
  2093. routine */
  2094. if (cmdiocbp->iocb_cmpl) {
  2095. spin_unlock_irqrestore(
  2096. &phba->hbalock,
  2097. iflag);
  2098. (cmdiocbp->iocb_cmpl) (phba,
  2099. cmdiocbp, saveq);
  2100. spin_lock_irqsave(
  2101. &phba->hbalock,
  2102. iflag);
  2103. } else
  2104. __lpfc_sli_release_iocbq(phba,
  2105. cmdiocbp);
  2106. }
  2107. } else if (type == LPFC_UNKNOWN_IOCB) {
  2108. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2109. char adaptermsg[LPFC_MAX_ADPTMSG];
  2110. memset(adaptermsg, 0,
  2111. LPFC_MAX_ADPTMSG);
  2112. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2113. MAX_MSG_DATA);
  2114. dev_warn(&((phba->pcidev)->dev),
  2115. "lpfc%d: %s\n",
  2116. phba->brd_no, adaptermsg);
  2117. } else {
  2118. /* Unknown IOCB command */
  2119. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2120. "0335 Unknown IOCB "
  2121. "command Data: x%x "
  2122. "x%x x%x x%x\n",
  2123. irsp->ulpCommand,
  2124. irsp->ulpStatus,
  2125. irsp->ulpIoTag,
  2126. irsp->ulpContext);
  2127. }
  2128. }
  2129. if (free_saveq) {
  2130. list_for_each_entry_safe(rspiocbp, next_iocb,
  2131. &saveq->list, list) {
  2132. list_del(&rspiocbp->list);
  2133. __lpfc_sli_release_iocbq(phba,
  2134. rspiocbp);
  2135. }
  2136. __lpfc_sli_release_iocbq(phba, saveq);
  2137. }
  2138. rspiocbp = NULL;
  2139. }
  2140. /*
  2141. * If the port response put pointer has not been updated, sync
  2142. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  2143. * response put pointer.
  2144. */
  2145. if (pring->rspidx == portRspPut) {
  2146. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2147. }
  2148. } /* while (pring->rspidx != portRspPut) */
  2149. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  2150. /* At least one response entry has been freed */
  2151. pring->stats.iocb_rsp_full++;
  2152. /* SET RxRE_RSP in Chip Att register */
  2153. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2154. writel(status, phba->CAregaddr);
  2155. readl(phba->CAregaddr); /* flush */
  2156. }
  2157. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2158. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2159. pring->stats.iocb_cmd_empty++;
  2160. /* Force update of the local copy of cmdGetInx */
  2161. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2162. lpfc_sli_resume_iocb(phba, pring);
  2163. if ((pring->lpfc_sli_cmd_available))
  2164. (pring->lpfc_sli_cmd_available) (phba, pring);
  2165. }
  2166. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2167. return rc;
  2168. }
  2169. /**
  2170. * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
  2171. * @phba: Pointer to HBA context object.
  2172. * @pring: Pointer to driver SLI ring object.
  2173. *
  2174. * This function aborts all iocbs in the given ring and frees all the iocb
  2175. * objects in txq. This function issues an abort iocb for all the iocb commands
  2176. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  2177. * the return of this function. The caller is not required to hold any locks.
  2178. **/
  2179. void
  2180. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2181. {
  2182. LIST_HEAD(completions);
  2183. struct lpfc_iocbq *iocb, *next_iocb;
  2184. IOCB_t *cmd = NULL;
  2185. if (pring->ringno == LPFC_ELS_RING) {
  2186. lpfc_fabric_abort_hba(phba);
  2187. }
  2188. /* Error everything on txq and txcmplq
  2189. * First do the txq.
  2190. */
  2191. spin_lock_irq(&phba->hbalock);
  2192. list_splice_init(&pring->txq, &completions);
  2193. pring->txq_cnt = 0;
  2194. /* Next issue ABTS for everything on the txcmplq */
  2195. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  2196. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  2197. spin_unlock_irq(&phba->hbalock);
  2198. while (!list_empty(&completions)) {
  2199. iocb = list_get_first(&completions, struct lpfc_iocbq, list);
  2200. cmd = &iocb->iocb;
  2201. list_del_init(&iocb->list);
  2202. if (!iocb->iocb_cmpl)
  2203. lpfc_sli_release_iocbq(phba, iocb);
  2204. else {
  2205. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2206. cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  2207. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2208. }
  2209. }
  2210. }
  2211. /**
  2212. * lpfc_sli_flush_fcp_rings: flush all iocbs in the fcp ring.
  2213. * @phba: Pointer to HBA context object.
  2214. *
  2215. * This function flushes all iocbs in the fcp ring and frees all the iocb
  2216. * objects in txq and txcmplq. This function will not issue abort iocbs
  2217. * for all the iocb commands in txcmplq, they will just be returned with
  2218. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  2219. * slot has been permanently disabled.
  2220. **/
  2221. void
  2222. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  2223. {
  2224. LIST_HEAD(txq);
  2225. LIST_HEAD(txcmplq);
  2226. struct lpfc_iocbq *iocb;
  2227. IOCB_t *cmd = NULL;
  2228. struct lpfc_sli *psli = &phba->sli;
  2229. struct lpfc_sli_ring *pring;
  2230. /* Currently, only one fcp ring */
  2231. pring = &psli->ring[psli->fcp_ring];
  2232. spin_lock_irq(&phba->hbalock);
  2233. /* Retrieve everything on txq */
  2234. list_splice_init(&pring->txq, &txq);
  2235. pring->txq_cnt = 0;
  2236. /* Retrieve everything on the txcmplq */
  2237. list_splice_init(&pring->txcmplq, &txcmplq);
  2238. pring->txcmplq_cnt = 0;
  2239. spin_unlock_irq(&phba->hbalock);
  2240. /* Flush the txq */
  2241. while (!list_empty(&txq)) {
  2242. iocb = list_get_first(&txq, struct lpfc_iocbq, list);
  2243. cmd = &iocb->iocb;
  2244. list_del_init(&iocb->list);
  2245. if (!iocb->iocb_cmpl)
  2246. lpfc_sli_release_iocbq(phba, iocb);
  2247. else {
  2248. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2249. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  2250. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2251. }
  2252. }
  2253. /* Flush the txcmpq */
  2254. while (!list_empty(&txcmplq)) {
  2255. iocb = list_get_first(&txcmplq, struct lpfc_iocbq, list);
  2256. cmd = &iocb->iocb;
  2257. list_del_init(&iocb->list);
  2258. if (!iocb->iocb_cmpl)
  2259. lpfc_sli_release_iocbq(phba, iocb);
  2260. else {
  2261. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2262. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  2263. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2264. }
  2265. }
  2266. }
  2267. /**
  2268. * lpfc_sli_brdready: Check for host status bits.
  2269. * @phba: Pointer to HBA context object.
  2270. * @mask: Bit mask to be checked.
  2271. *
  2272. * This function reads the host status register and compares
  2273. * with the provided bit mask to check if HBA completed
  2274. * the restart. This function will wait in a loop for the
  2275. * HBA to complete restart. If the HBA does not restart within
  2276. * 15 iterations, the function will reset the HBA again. The
  2277. * function returns 1 when HBA fail to restart otherwise returns
  2278. * zero.
  2279. **/
  2280. int
  2281. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  2282. {
  2283. uint32_t status;
  2284. int i = 0;
  2285. int retval = 0;
  2286. /* Read the HBA Host Status Register */
  2287. status = readl(phba->HSregaddr);
  2288. /*
  2289. * Check status register every 100ms for 5 retries, then every
  2290. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  2291. * every 2.5 sec for 4.
  2292. * Break our of the loop if errors occurred during init.
  2293. */
  2294. while (((status & mask) != mask) &&
  2295. !(status & HS_FFERM) &&
  2296. i++ < 20) {
  2297. if (i <= 5)
  2298. msleep(10);
  2299. else if (i <= 10)
  2300. msleep(500);
  2301. else
  2302. msleep(2500);
  2303. if (i == 15) {
  2304. /* Do post */
  2305. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2306. lpfc_sli_brdrestart(phba);
  2307. }
  2308. /* Read the HBA Host Status Register */
  2309. status = readl(phba->HSregaddr);
  2310. }
  2311. /* Check to see if any errors occurred during init */
  2312. if ((status & HS_FFERM) || (i >= 20)) {
  2313. phba->link_state = LPFC_HBA_ERROR;
  2314. retval = 1;
  2315. }
  2316. return retval;
  2317. }
  2318. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  2319. /**
  2320. * lpfc_reset_barrier: Make HBA ready for HBA reset.
  2321. * @phba: Pointer to HBA context object.
  2322. *
  2323. * This function is called before resetting an HBA. This
  2324. * function requests HBA to quiesce DMAs before a reset.
  2325. **/
  2326. void lpfc_reset_barrier(struct lpfc_hba *phba)
  2327. {
  2328. uint32_t __iomem *resp_buf;
  2329. uint32_t __iomem *mbox_buf;
  2330. volatile uint32_t mbox;
  2331. uint32_t hc_copy;
  2332. int i;
  2333. uint8_t hdrtype;
  2334. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  2335. if (hdrtype != 0x80 ||
  2336. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  2337. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  2338. return;
  2339. /*
  2340. * Tell the other part of the chip to suspend temporarily all
  2341. * its DMA activity.
  2342. */
  2343. resp_buf = phba->MBslimaddr;
  2344. /* Disable the error attention */
  2345. hc_copy = readl(phba->HCregaddr);
  2346. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  2347. readl(phba->HCregaddr); /* flush */
  2348. phba->link_flag |= LS_IGNORE_ERATT;
  2349. if (readl(phba->HAregaddr) & HA_ERATT) {
  2350. /* Clear Chip error bit */
  2351. writel(HA_ERATT, phba->HAregaddr);
  2352. phba->pport->stopped = 1;
  2353. }
  2354. mbox = 0;
  2355. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  2356. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  2357. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  2358. mbox_buf = phba->MBslimaddr;
  2359. writel(mbox, mbox_buf);
  2360. for (i = 0;
  2361. readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
  2362. mdelay(1);
  2363. if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
  2364. if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
  2365. phba->pport->stopped)
  2366. goto restore_hc;
  2367. else
  2368. goto clear_errat;
  2369. }
  2370. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  2371. for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
  2372. mdelay(1);
  2373. clear_errat:
  2374. while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
  2375. mdelay(1);
  2376. if (readl(phba->HAregaddr) & HA_ERATT) {
  2377. writel(HA_ERATT, phba->HAregaddr);
  2378. phba->pport->stopped = 1;
  2379. }
  2380. restore_hc:
  2381. phba->link_flag &= ~LS_IGNORE_ERATT;
  2382. writel(hc_copy, phba->HCregaddr);
  2383. readl(phba->HCregaddr); /* flush */
  2384. }
  2385. /**
  2386. * lpfc_sli_brdkill: Issue a kill_board mailbox command.
  2387. * @phba: Pointer to HBA context object.
  2388. *
  2389. * This function issues a kill_board mailbox command and waits for
  2390. * the error attention interrupt. This function is called for stopping
  2391. * the firmware processing. The caller is not required to hold any
  2392. * locks. This function calls lpfc_hba_down_post function to free
  2393. * any pending commands after the kill. The function will return 1 when it
  2394. * fails to kill the board else will return 0.
  2395. **/
  2396. int
  2397. lpfc_sli_brdkill(struct lpfc_hba *phba)
  2398. {
  2399. struct lpfc_sli *psli;
  2400. LPFC_MBOXQ_t *pmb;
  2401. uint32_t status;
  2402. uint32_t ha_copy;
  2403. int retval;
  2404. int i = 0;
  2405. psli = &phba->sli;
  2406. /* Kill HBA */
  2407. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2408. "0329 Kill HBA Data: x%x x%x\n",
  2409. phba->pport->port_state, psli->sli_flag);
  2410. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2411. if (!pmb)
  2412. return 1;
  2413. /* Disable the error attention */
  2414. spin_lock_irq(&phba->hbalock);
  2415. status = readl(phba->HCregaddr);
  2416. status &= ~HC_ERINT_ENA;
  2417. writel(status, phba->HCregaddr);
  2418. readl(phba->HCregaddr); /* flush */
  2419. phba->link_flag |= LS_IGNORE_ERATT;
  2420. spin_unlock_irq(&phba->hbalock);
  2421. lpfc_kill_board(phba, pmb);
  2422. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2423. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2424. if (retval != MBX_SUCCESS) {
  2425. if (retval != MBX_BUSY)
  2426. mempool_free(pmb, phba->mbox_mem_pool);
  2427. spin_lock_irq(&phba->hbalock);
  2428. phba->link_flag &= ~LS_IGNORE_ERATT;
  2429. spin_unlock_irq(&phba->hbalock);
  2430. return 1;
  2431. }
  2432. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  2433. mempool_free(pmb, phba->mbox_mem_pool);
  2434. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  2435. * attention every 100ms for 3 seconds. If we don't get ERATT after
  2436. * 3 seconds we still set HBA_ERROR state because the status of the
  2437. * board is now undefined.
  2438. */
  2439. ha_copy = readl(phba->HAregaddr);
  2440. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  2441. mdelay(100);
  2442. ha_copy = readl(phba->HAregaddr);
  2443. }
  2444. del_timer_sync(&psli->mbox_tmo);
  2445. if (ha_copy & HA_ERATT) {
  2446. writel(HA_ERATT, phba->HAregaddr);
  2447. phba->pport->stopped = 1;
  2448. }
  2449. spin_lock_irq(&phba->hbalock);
  2450. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2451. phba->link_flag &= ~LS_IGNORE_ERATT;
  2452. spin_unlock_irq(&phba->hbalock);
  2453. psli->mbox_active = NULL;
  2454. lpfc_hba_down_post(phba);
  2455. phba->link_state = LPFC_HBA_ERROR;
  2456. return ha_copy & HA_ERATT ? 0 : 1;
  2457. }
  2458. /**
  2459. * lpfc_sli_brdreset: Reset the HBA.
  2460. * @phba: Pointer to HBA context object.
  2461. *
  2462. * This function resets the HBA by writing HC_INITFF to the control
  2463. * register. After the HBA resets, this function resets all the iocb ring
  2464. * indices. This function disables PCI layer parity checking during
  2465. * the reset.
  2466. * This function returns 0 always.
  2467. * The caller is not required to hold any locks.
  2468. **/
  2469. int
  2470. lpfc_sli_brdreset(struct lpfc_hba *phba)
  2471. {
  2472. struct lpfc_sli *psli;
  2473. struct lpfc_sli_ring *pring;
  2474. uint16_t cfg_value;
  2475. int i;
  2476. psli = &phba->sli;
  2477. /* Reset HBA */
  2478. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2479. "0325 Reset HBA Data: x%x x%x\n",
  2480. phba->pport->port_state, psli->sli_flag);
  2481. /* perform board reset */
  2482. phba->fc_eventTag = 0;
  2483. phba->pport->fc_myDID = 0;
  2484. phba->pport->fc_prevDID = 0;
  2485. /* Turn off parity checking and serr during the physical reset */
  2486. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  2487. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  2488. (cfg_value &
  2489. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  2490. psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
  2491. /* Now toggle INITFF bit in the Host Control Register */
  2492. writel(HC_INITFF, phba->HCregaddr);
  2493. mdelay(1);
  2494. readl(phba->HCregaddr); /* flush */
  2495. writel(0, phba->HCregaddr);
  2496. readl(phba->HCregaddr); /* flush */
  2497. /* Restore PCI cmd register */
  2498. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  2499. /* Initialize relevant SLI info */
  2500. for (i = 0; i < psli->num_rings; i++) {
  2501. pring = &psli->ring[i];
  2502. pring->flag = 0;
  2503. pring->rspidx = 0;
  2504. pring->next_cmdidx = 0;
  2505. pring->local_getidx = 0;
  2506. pring->cmdidx = 0;
  2507. pring->missbufcnt = 0;
  2508. }
  2509. phba->link_state = LPFC_WARM_START;
  2510. return 0;
  2511. }
  2512. /**
  2513. * lpfc_sli_brdrestart: Restart the HBA.
  2514. * @phba: Pointer to HBA context object.
  2515. *
  2516. * This function is called in the SLI initialization code path to
  2517. * restart the HBA. The caller is not required to hold any lock.
  2518. * This function writes MBX_RESTART mailbox command to the SLIM and
  2519. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  2520. * function to free any pending commands. The function enables
  2521. * POST only during the first initialization. The function returns zero.
  2522. * The function does not guarantee completion of MBX_RESTART mailbox
  2523. * command before the return of this function.
  2524. **/
  2525. int
  2526. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  2527. {
  2528. MAILBOX_t *mb;
  2529. struct lpfc_sli *psli;
  2530. volatile uint32_t word0;
  2531. void __iomem *to_slim;
  2532. spin_lock_irq(&phba->hbalock);
  2533. psli = &phba->sli;
  2534. /* Restart HBA */
  2535. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2536. "0337 Restart HBA Data: x%x x%x\n",
  2537. phba->pport->port_state, psli->sli_flag);
  2538. word0 = 0;
  2539. mb = (MAILBOX_t *) &word0;
  2540. mb->mbxCommand = MBX_RESTART;
  2541. mb->mbxHc = 1;
  2542. lpfc_reset_barrier(phba);
  2543. to_slim = phba->MBslimaddr;
  2544. writel(*(uint32_t *) mb, to_slim);
  2545. readl(to_slim); /* flush */
  2546. /* Only skip post after fc_ffinit is completed */
  2547. if (phba->pport->port_state)
  2548. word0 = 1; /* This is really setting up word1 */
  2549. else
  2550. word0 = 0; /* This is really setting up word1 */
  2551. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  2552. writel(*(uint32_t *) mb, to_slim);
  2553. readl(to_slim); /* flush */
  2554. lpfc_sli_brdreset(phba);
  2555. phba->pport->stopped = 0;
  2556. phba->link_state = LPFC_INIT_START;
  2557. spin_unlock_irq(&phba->hbalock);
  2558. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  2559. psli->stats_start = get_seconds();
  2560. /* Give the INITFF and Post time to settle. */
  2561. mdelay(100);
  2562. lpfc_hba_down_post(phba);
  2563. return 0;
  2564. }
  2565. /**
  2566. * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
  2567. * @phba: Pointer to HBA context object.
  2568. *
  2569. * This function is called after a HBA restart to wait for successful
  2570. * restart of the HBA. Successful restart of the HBA is indicated by
  2571. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  2572. * iteration, the function will restart the HBA again. The function returns
  2573. * zero if HBA successfully restarted else returns negative error code.
  2574. **/
  2575. static int
  2576. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  2577. {
  2578. uint32_t status, i = 0;
  2579. /* Read the HBA Host Status Register */
  2580. status = readl(phba->HSregaddr);
  2581. /* Check status register to see what current state is */
  2582. i = 0;
  2583. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  2584. /* Check every 100ms for 5 retries, then every 500ms for 5, then
  2585. * every 2.5 sec for 5, then reset board and every 2.5 sec for
  2586. * 4.
  2587. */
  2588. if (i++ >= 20) {
  2589. /* Adapter failed to init, timeout, status reg
  2590. <status> */
  2591. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2592. "0436 Adapter failed to init, "
  2593. "timeout, status reg x%x, "
  2594. "FW Data: A8 x%x AC x%x\n", status,
  2595. readl(phba->MBslimaddr + 0xa8),
  2596. readl(phba->MBslimaddr + 0xac));
  2597. phba->link_state = LPFC_HBA_ERROR;
  2598. return -ETIMEDOUT;
  2599. }
  2600. /* Check to see if any errors occurred during init */
  2601. if (status & HS_FFERM) {
  2602. /* ERROR: During chipset initialization */
  2603. /* Adapter failed to init, chipset, status reg
  2604. <status> */
  2605. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2606. "0437 Adapter failed to init, "
  2607. "chipset, status reg x%x, "
  2608. "FW Data: A8 x%x AC x%x\n", status,
  2609. readl(phba->MBslimaddr + 0xa8),
  2610. readl(phba->MBslimaddr + 0xac));
  2611. phba->link_state = LPFC_HBA_ERROR;
  2612. return -EIO;
  2613. }
  2614. if (i <= 5) {
  2615. msleep(10);
  2616. } else if (i <= 10) {
  2617. msleep(500);
  2618. } else {
  2619. msleep(2500);
  2620. }
  2621. if (i == 15) {
  2622. /* Do post */
  2623. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2624. lpfc_sli_brdrestart(phba);
  2625. }
  2626. /* Read the HBA Host Status Register */
  2627. status = readl(phba->HSregaddr);
  2628. }
  2629. /* Check to see if any errors occurred during init */
  2630. if (status & HS_FFERM) {
  2631. /* ERROR: During chipset initialization */
  2632. /* Adapter failed to init, chipset, status reg <status> */
  2633. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2634. "0438 Adapter failed to init, chipset, "
  2635. "status reg x%x, "
  2636. "FW Data: A8 x%x AC x%x\n", status,
  2637. readl(phba->MBslimaddr + 0xa8),
  2638. readl(phba->MBslimaddr + 0xac));
  2639. phba->link_state = LPFC_HBA_ERROR;
  2640. return -EIO;
  2641. }
  2642. /* Clear all interrupt enable conditions */
  2643. writel(0, phba->HCregaddr);
  2644. readl(phba->HCregaddr); /* flush */
  2645. /* setup host attn register */
  2646. writel(0xffffffff, phba->HAregaddr);
  2647. readl(phba->HAregaddr); /* flush */
  2648. return 0;
  2649. }
  2650. /**
  2651. * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
  2652. *
  2653. * This function calculates and returns the number of HBQs required to be
  2654. * configured.
  2655. **/
  2656. int
  2657. lpfc_sli_hbq_count(void)
  2658. {
  2659. return ARRAY_SIZE(lpfc_hbq_defs);
  2660. }
  2661. /**
  2662. * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
  2663. *
  2664. * This function adds the number of hbq entries in every HBQ to get
  2665. * the total number of hbq entries required for the HBA and returns
  2666. * the total count.
  2667. **/
  2668. static int
  2669. lpfc_sli_hbq_entry_count(void)
  2670. {
  2671. int hbq_count = lpfc_sli_hbq_count();
  2672. int count = 0;
  2673. int i;
  2674. for (i = 0; i < hbq_count; ++i)
  2675. count += lpfc_hbq_defs[i]->entry_count;
  2676. return count;
  2677. }
  2678. /**
  2679. * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
  2680. *
  2681. * This function calculates amount of memory required for all hbq entries
  2682. * to be configured and returns the total memory required.
  2683. **/
  2684. int
  2685. lpfc_sli_hbq_size(void)
  2686. {
  2687. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  2688. }
  2689. /**
  2690. * lpfc_sli_hbq_setup: configure and initialize HBQs.
  2691. * @phba: Pointer to HBA context object.
  2692. *
  2693. * This function is called during the SLI initialization to configure
  2694. * all the HBQs and post buffers to the HBQ. The caller is not
  2695. * required to hold any locks. This function will return zero if successful
  2696. * else it will return negative error code.
  2697. **/
  2698. static int
  2699. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  2700. {
  2701. int hbq_count = lpfc_sli_hbq_count();
  2702. LPFC_MBOXQ_t *pmb;
  2703. MAILBOX_t *pmbox;
  2704. uint32_t hbqno;
  2705. uint32_t hbq_entry_index;
  2706. /* Get a Mailbox buffer to setup mailbox
  2707. * commands for HBA initialization
  2708. */
  2709. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2710. if (!pmb)
  2711. return -ENOMEM;
  2712. pmbox = &pmb->mb;
  2713. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  2714. phba->link_state = LPFC_INIT_MBX_CMDS;
  2715. phba->hbq_in_use = 1;
  2716. hbq_entry_index = 0;
  2717. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  2718. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  2719. phba->hbqs[hbqno].hbqPutIdx = 0;
  2720. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  2721. phba->hbqs[hbqno].entry_count =
  2722. lpfc_hbq_defs[hbqno]->entry_count;
  2723. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  2724. hbq_entry_index, pmb);
  2725. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  2726. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  2727. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  2728. mbxStatus <status>, ring <num> */
  2729. lpfc_printf_log(phba, KERN_ERR,
  2730. LOG_SLI | LOG_VPORT,
  2731. "1805 Adapter failed to init. "
  2732. "Data: x%x x%x x%x\n",
  2733. pmbox->mbxCommand,
  2734. pmbox->mbxStatus, hbqno);
  2735. phba->link_state = LPFC_HBA_ERROR;
  2736. mempool_free(pmb, phba->mbox_mem_pool);
  2737. return ENXIO;
  2738. }
  2739. }
  2740. phba->hbq_count = hbq_count;
  2741. mempool_free(pmb, phba->mbox_mem_pool);
  2742. /* Initially populate or replenish the HBQs */
  2743. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  2744. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  2745. return 0;
  2746. }
  2747. /**
  2748. * lpfc_sli_config_port: Issue config port mailbox command.
  2749. * @phba: Pointer to HBA context object.
  2750. * @sli_mode: sli mode - 2/3
  2751. *
  2752. * This function is called by the sli intialization code path
  2753. * to issue config_port mailbox command. This function restarts the
  2754. * HBA firmware and issues a config_port mailbox command to configure
  2755. * the SLI interface in the sli mode specified by sli_mode
  2756. * variable. The caller is not required to hold any locks.
  2757. * The function returns 0 if successful, else returns negative error
  2758. * code.
  2759. **/
  2760. int
  2761. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  2762. {
  2763. LPFC_MBOXQ_t *pmb;
  2764. uint32_t resetcount = 0, rc = 0, done = 0;
  2765. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2766. if (!pmb) {
  2767. phba->link_state = LPFC_HBA_ERROR;
  2768. return -ENOMEM;
  2769. }
  2770. phba->sli_rev = sli_mode;
  2771. while (resetcount < 2 && !done) {
  2772. spin_lock_irq(&phba->hbalock);
  2773. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  2774. spin_unlock_irq(&phba->hbalock);
  2775. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2776. lpfc_sli_brdrestart(phba);
  2777. rc = lpfc_sli_chipset_init(phba);
  2778. if (rc)
  2779. break;
  2780. spin_lock_irq(&phba->hbalock);
  2781. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2782. spin_unlock_irq(&phba->hbalock);
  2783. resetcount++;
  2784. /* Call pre CONFIG_PORT mailbox command initialization. A
  2785. * value of 0 means the call was successful. Any other
  2786. * nonzero value is a failure, but if ERESTART is returned,
  2787. * the driver may reset the HBA and try again.
  2788. */
  2789. rc = lpfc_config_port_prep(phba);
  2790. if (rc == -ERESTART) {
  2791. phba->link_state = LPFC_LINK_UNKNOWN;
  2792. continue;
  2793. } else if (rc)
  2794. break;
  2795. phba->link_state = LPFC_INIT_MBX_CMDS;
  2796. lpfc_config_port(phba, pmb);
  2797. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  2798. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  2799. LPFC_SLI3_HBQ_ENABLED |
  2800. LPFC_SLI3_CRP_ENABLED |
  2801. LPFC_SLI3_INB_ENABLED);
  2802. if (rc != MBX_SUCCESS) {
  2803. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2804. "0442 Adapter failed to init, mbxCmd x%x "
  2805. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  2806. pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
  2807. spin_lock_irq(&phba->hbalock);
  2808. phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
  2809. spin_unlock_irq(&phba->hbalock);
  2810. rc = -ENXIO;
  2811. } else
  2812. done = 1;
  2813. }
  2814. if (!done) {
  2815. rc = -EINVAL;
  2816. goto do_prep_failed;
  2817. }
  2818. if (pmb->mb.un.varCfgPort.sli_mode == 3) {
  2819. if (!pmb->mb.un.varCfgPort.cMA) {
  2820. rc = -ENXIO;
  2821. goto do_prep_failed;
  2822. }
  2823. if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
  2824. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  2825. phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
  2826. } else
  2827. phba->max_vpi = 0;
  2828. if (pmb->mb.un.varCfgPort.gerbm)
  2829. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  2830. if (pmb->mb.un.varCfgPort.gcrp)
  2831. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  2832. if (pmb->mb.un.varCfgPort.ginb) {
  2833. phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
  2834. phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
  2835. phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
  2836. phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
  2837. phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
  2838. phba->inb_last_counter =
  2839. phba->mbox->us.s3_inb_pgp.counter;
  2840. } else {
  2841. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  2842. phba->port_gp = phba->mbox->us.s3_pgp.port;
  2843. phba->inb_ha_copy = NULL;
  2844. phba->inb_counter = NULL;
  2845. }
  2846. } else {
  2847. phba->hbq_get = NULL;
  2848. phba->port_gp = phba->mbox->us.s2.port;
  2849. phba->inb_ha_copy = NULL;
  2850. phba->inb_counter = NULL;
  2851. phba->max_vpi = 0;
  2852. }
  2853. do_prep_failed:
  2854. mempool_free(pmb, phba->mbox_mem_pool);
  2855. return rc;
  2856. }
  2857. /**
  2858. * lpfc_sli_hba_setup: SLI intialization function.
  2859. * @phba: Pointer to HBA context object.
  2860. *
  2861. * This function is the main SLI intialization function. This function
  2862. * is called by the HBA intialization code, HBA reset code and HBA
  2863. * error attention handler code. Caller is not required to hold any
  2864. * locks. This function issues config_port mailbox command to configure
  2865. * the SLI, setup iocb rings and HBQ rings. In the end the function
  2866. * calls the config_port_post function to issue init_link mailbox
  2867. * command and to start the discovery. The function will return zero
  2868. * if successful, else it will return negative error code.
  2869. **/
  2870. int
  2871. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  2872. {
  2873. uint32_t rc;
  2874. int mode = 3;
  2875. switch (lpfc_sli_mode) {
  2876. case 2:
  2877. if (phba->cfg_enable_npiv) {
  2878. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2879. "1824 NPIV enabled: Override lpfc_sli_mode "
  2880. "parameter (%d) to auto (0).\n",
  2881. lpfc_sli_mode);
  2882. break;
  2883. }
  2884. mode = 2;
  2885. break;
  2886. case 0:
  2887. case 3:
  2888. break;
  2889. default:
  2890. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2891. "1819 Unrecognized lpfc_sli_mode "
  2892. "parameter: %d.\n", lpfc_sli_mode);
  2893. break;
  2894. }
  2895. rc = lpfc_sli_config_port(phba, mode);
  2896. if (rc && lpfc_sli_mode == 3)
  2897. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2898. "1820 Unable to select SLI-3. "
  2899. "Not supported by adapter.\n");
  2900. if (rc && mode != 2)
  2901. rc = lpfc_sli_config_port(phba, 2);
  2902. if (rc)
  2903. goto lpfc_sli_hba_setup_error;
  2904. if (phba->sli_rev == 3) {
  2905. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  2906. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  2907. } else {
  2908. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  2909. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  2910. phba->sli3_options = 0;
  2911. }
  2912. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2913. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  2914. phba->sli_rev, phba->max_vpi);
  2915. rc = lpfc_sli_ring_map(phba);
  2916. if (rc)
  2917. goto lpfc_sli_hba_setup_error;
  2918. /* Init HBQs */
  2919. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2920. rc = lpfc_sli_hbq_setup(phba);
  2921. if (rc)
  2922. goto lpfc_sli_hba_setup_error;
  2923. }
  2924. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  2925. rc = lpfc_config_port_post(phba);
  2926. if (rc)
  2927. goto lpfc_sli_hba_setup_error;
  2928. return rc;
  2929. lpfc_sli_hba_setup_error:
  2930. phba->link_state = LPFC_HBA_ERROR;
  2931. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2932. "0445 Firmware initialization failed\n");
  2933. return rc;
  2934. }
  2935. /**
  2936. * lpfc_mbox_timeout: Timeout call back function for mbox timer.
  2937. * @ptr: context object - pointer to hba structure.
  2938. *
  2939. * This is the callback function for mailbox timer. The mailbox
  2940. * timer is armed when a new mailbox command is issued and the timer
  2941. * is deleted when the mailbox complete. The function is called by
  2942. * the kernel timer code when a mailbox does not complete within
  2943. * expected time. This function wakes up the worker thread to
  2944. * process the mailbox timeout and returns. All the processing is
  2945. * done by the worker thread function lpfc_mbox_timeout_handler.
  2946. **/
  2947. void
  2948. lpfc_mbox_timeout(unsigned long ptr)
  2949. {
  2950. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2951. unsigned long iflag;
  2952. uint32_t tmo_posted;
  2953. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  2954. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  2955. if (!tmo_posted)
  2956. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  2957. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  2958. if (!tmo_posted)
  2959. lpfc_worker_wake_up(phba);
  2960. return;
  2961. }
  2962. /**
  2963. * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
  2964. * @phba: Pointer to HBA context object.
  2965. *
  2966. * This function is called from worker thread when a mailbox command times out.
  2967. * The caller is not required to hold any locks. This function will reset the
  2968. * HBA and recover all the pending commands.
  2969. **/
  2970. void
  2971. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  2972. {
  2973. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  2974. MAILBOX_t *mb = &pmbox->mb;
  2975. struct lpfc_sli *psli = &phba->sli;
  2976. struct lpfc_sli_ring *pring;
  2977. /* Mbox cmd <mbxCommand> timeout */
  2978. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2979. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  2980. mb->mbxCommand,
  2981. phba->pport->port_state,
  2982. phba->sli.sli_flag,
  2983. phba->sli.mbox_active);
  2984. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  2985. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  2986. * it to fail all oustanding SCSI IO.
  2987. */
  2988. spin_lock_irq(&phba->pport->work_port_lock);
  2989. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  2990. spin_unlock_irq(&phba->pport->work_port_lock);
  2991. spin_lock_irq(&phba->hbalock);
  2992. phba->link_state = LPFC_LINK_UNKNOWN;
  2993. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  2994. spin_unlock_irq(&phba->hbalock);
  2995. pring = &psli->ring[psli->fcp_ring];
  2996. lpfc_sli_abort_iocb_ring(phba, pring);
  2997. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2998. "0345 Resetting board due to mailbox timeout\n");
  2999. /*
  3000. * lpfc_offline calls lpfc_sli_hba_down which will clean up
  3001. * on oustanding mailbox commands.
  3002. */
  3003. /* If resets are disabled then set error state and return. */
  3004. if (!phba->cfg_enable_hba_reset) {
  3005. phba->link_state = LPFC_HBA_ERROR;
  3006. return;
  3007. }
  3008. lpfc_offline_prep(phba);
  3009. lpfc_offline(phba);
  3010. lpfc_sli_brdrestart(phba);
  3011. lpfc_online(phba);
  3012. lpfc_unblock_mgmt_io(phba);
  3013. return;
  3014. }
  3015. /**
  3016. * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
  3017. * @phba: Pointer to HBA context object.
  3018. * @pmbox: Pointer to mailbox object.
  3019. * @flag: Flag indicating how the mailbox need to be processed.
  3020. *
  3021. * This function is called by discovery code and HBA management code
  3022. * to submit a mailbox command to firmware. This function gets the
  3023. * hbalock to protect the data structures.
  3024. * The mailbox command can be submitted in polling mode, in which case
  3025. * this function will wait in a polling loop for the completion of the
  3026. * mailbox.
  3027. * If the mailbox is submitted in no_wait mode (not polling) the
  3028. * function will submit the command and returns immediately without waiting
  3029. * for the mailbox completion. The no_wait is supported only when HBA
  3030. * is in SLI2/SLI3 mode - interrupts are enabled.
  3031. * The SLI interface allows only one mailbox pending at a time. If the
  3032. * mailbox is issued in polling mode and there is already a mailbox
  3033. * pending, then the function will return an error. If the mailbox is issued
  3034. * in NO_WAIT mode and there is a mailbox pending already, the function
  3035. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  3036. * The sli layer owns the mailbox object until the completion of mailbox
  3037. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  3038. * return codes the caller owns the mailbox command after the return of
  3039. * the function.
  3040. **/
  3041. int
  3042. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  3043. {
  3044. MAILBOX_t *mb;
  3045. struct lpfc_sli *psli = &phba->sli;
  3046. uint32_t status, evtctr;
  3047. uint32_t ha_copy;
  3048. int i;
  3049. unsigned long timeout;
  3050. unsigned long drvr_flag = 0;
  3051. uint32_t word0, ldata;
  3052. void __iomem *to_slim;
  3053. int processing_queue = 0;
  3054. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  3055. if (!pmbox) {
  3056. /* processing mbox queue from intr_handler */
  3057. processing_queue = 1;
  3058. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3059. pmbox = lpfc_mbox_get(phba);
  3060. if (!pmbox) {
  3061. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3062. return MBX_SUCCESS;
  3063. }
  3064. }
  3065. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  3066. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  3067. if(!pmbox->vport) {
  3068. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3069. lpfc_printf_log(phba, KERN_ERR,
  3070. LOG_MBOX | LOG_VPORT,
  3071. "1806 Mbox x%x failed. No vport\n",
  3072. pmbox->mb.mbxCommand);
  3073. dump_stack();
  3074. goto out_not_finished;
  3075. }
  3076. }
  3077. /* If the PCI channel is in offline state, do not post mbox. */
  3078. if (unlikely(pci_channel_offline(phba->pcidev))) {
  3079. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3080. goto out_not_finished;
  3081. }
  3082. psli = &phba->sli;
  3083. mb = &pmbox->mb;
  3084. status = MBX_SUCCESS;
  3085. if (phba->link_state == LPFC_HBA_ERROR) {
  3086. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3087. /* Mbox command <mbxCommand> cannot issue */
  3088. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3089. goto out_not_finished;
  3090. }
  3091. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
  3092. !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
  3093. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3094. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3095. goto out_not_finished;
  3096. }
  3097. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  3098. /* Polling for a mbox command when another one is already active
  3099. * is not allowed in SLI. Also, the driver must have established
  3100. * SLI2 mode to queue and process multiple mbox commands.
  3101. */
  3102. if (flag & MBX_POLL) {
  3103. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3104. /* Mbox command <mbxCommand> cannot issue */
  3105. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3106. goto out_not_finished;
  3107. }
  3108. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
  3109. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3110. /* Mbox command <mbxCommand> cannot issue */
  3111. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3112. goto out_not_finished;
  3113. }
  3114. /* Another mailbox command is still being processed, queue this
  3115. * command to be processed later.
  3116. */
  3117. lpfc_mbox_put(phba, pmbox);
  3118. /* Mbox cmd issue - BUSY */
  3119. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3120. "(%d):0308 Mbox cmd issue - BUSY Data: "
  3121. "x%x x%x x%x x%x\n",
  3122. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  3123. mb->mbxCommand, phba->pport->port_state,
  3124. psli->sli_flag, flag);
  3125. psli->slistat.mbox_busy++;
  3126. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3127. if (pmbox->vport) {
  3128. lpfc_debugfs_disc_trc(pmbox->vport,
  3129. LPFC_DISC_TRC_MBOX_VPORT,
  3130. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  3131. (uint32_t)mb->mbxCommand,
  3132. mb->un.varWords[0], mb->un.varWords[1]);
  3133. }
  3134. else {
  3135. lpfc_debugfs_disc_trc(phba->pport,
  3136. LPFC_DISC_TRC_MBOX,
  3137. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  3138. (uint32_t)mb->mbxCommand,
  3139. mb->un.varWords[0], mb->un.varWords[1]);
  3140. }
  3141. return MBX_BUSY;
  3142. }
  3143. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3144. /* If we are not polling, we MUST be in SLI2 mode */
  3145. if (flag != MBX_POLL) {
  3146. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
  3147. (mb->mbxCommand != MBX_KILL_BOARD)) {
  3148. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3149. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3150. /* Mbox command <mbxCommand> cannot issue */
  3151. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3152. goto out_not_finished;
  3153. }
  3154. /* timeout active mbox command */
  3155. mod_timer(&psli->mbox_tmo, (jiffies +
  3156. (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
  3157. }
  3158. /* Mailbox cmd <cmd> issue */
  3159. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3160. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  3161. "x%x\n",
  3162. pmbox->vport ? pmbox->vport->vpi : 0,
  3163. mb->mbxCommand, phba->pport->port_state,
  3164. psli->sli_flag, flag);
  3165. if (mb->mbxCommand != MBX_HEARTBEAT) {
  3166. if (pmbox->vport) {
  3167. lpfc_debugfs_disc_trc(pmbox->vport,
  3168. LPFC_DISC_TRC_MBOX_VPORT,
  3169. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  3170. (uint32_t)mb->mbxCommand,
  3171. mb->un.varWords[0], mb->un.varWords[1]);
  3172. }
  3173. else {
  3174. lpfc_debugfs_disc_trc(phba->pport,
  3175. LPFC_DISC_TRC_MBOX,
  3176. "MBOX Send: cmd:x%x mb:x%x x%x",
  3177. (uint32_t)mb->mbxCommand,
  3178. mb->un.varWords[0], mb->un.varWords[1]);
  3179. }
  3180. }
  3181. psli->slistat.mbox_cmd++;
  3182. evtctr = psli->slistat.mbox_event;
  3183. /* next set own bit for the adapter and copy over command word */
  3184. mb->mbxOwner = OWN_CHIP;
  3185. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3186. /* First copy command data to host SLIM area */
  3187. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  3188. } else {
  3189. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  3190. /* copy command data into host mbox for cmpl */
  3191. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  3192. }
  3193. /* First copy mbox command data to HBA SLIM, skip past first
  3194. word */
  3195. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3196. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  3197. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  3198. /* Next copy over first word, with mbxOwner set */
  3199. ldata = *((uint32_t *)mb);
  3200. to_slim = phba->MBslimaddr;
  3201. writel(ldata, to_slim);
  3202. readl(to_slim); /* flush */
  3203. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  3204. /* switch over to host mailbox */
  3205. psli->sli_flag |= LPFC_SLI2_ACTIVE;
  3206. }
  3207. }
  3208. wmb();
  3209. switch (flag) {
  3210. case MBX_NOWAIT:
  3211. /* Set up reference to mailbox command */
  3212. psli->mbox_active = pmbox;
  3213. /* Interrupt board to do it */
  3214. writel(CA_MBATT, phba->CAregaddr);
  3215. readl(phba->CAregaddr); /* flush */
  3216. /* Don't wait for it to finish, just return */
  3217. break;
  3218. case MBX_POLL:
  3219. /* Set up null reference to mailbox command */
  3220. psli->mbox_active = NULL;
  3221. /* Interrupt board to do it */
  3222. writel(CA_MBATT, phba->CAregaddr);
  3223. readl(phba->CAregaddr); /* flush */
  3224. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3225. /* First read mbox status word */
  3226. word0 = *((uint32_t *)phba->mbox);
  3227. word0 = le32_to_cpu(word0);
  3228. } else {
  3229. /* First read mbox status word */
  3230. word0 = readl(phba->MBslimaddr);
  3231. }
  3232. /* Read the HBA Host Attention Register */
  3233. ha_copy = readl(phba->HAregaddr);
  3234. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  3235. mb->mbxCommand) *
  3236. 1000) + jiffies;
  3237. i = 0;
  3238. /* Wait for command to complete */
  3239. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  3240. (!(ha_copy & HA_MBATT) &&
  3241. (phba->link_state > LPFC_WARM_START))) {
  3242. if (time_after(jiffies, timeout)) {
  3243. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3244. spin_unlock_irqrestore(&phba->hbalock,
  3245. drvr_flag);
  3246. goto out_not_finished;
  3247. }
  3248. /* Check if we took a mbox interrupt while we were
  3249. polling */
  3250. if (((word0 & OWN_CHIP) != OWN_CHIP)
  3251. && (evtctr != psli->slistat.mbox_event))
  3252. break;
  3253. if (i++ > 10) {
  3254. spin_unlock_irqrestore(&phba->hbalock,
  3255. drvr_flag);
  3256. msleep(1);
  3257. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  3258. }
  3259. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3260. /* First copy command data */
  3261. word0 = *((uint32_t *)phba->mbox);
  3262. word0 = le32_to_cpu(word0);
  3263. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  3264. MAILBOX_t *slimmb;
  3265. uint32_t slimword0;
  3266. /* Check real SLIM for any errors */
  3267. slimword0 = readl(phba->MBslimaddr);
  3268. slimmb = (MAILBOX_t *) & slimword0;
  3269. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  3270. && slimmb->mbxStatus) {
  3271. psli->sli_flag &=
  3272. ~LPFC_SLI2_ACTIVE;
  3273. word0 = slimword0;
  3274. }
  3275. }
  3276. } else {
  3277. /* First copy command data */
  3278. word0 = readl(phba->MBslimaddr);
  3279. }
  3280. /* Read the HBA Host Attention Register */
  3281. ha_copy = readl(phba->HAregaddr);
  3282. }
  3283. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3284. /* copy results back to user */
  3285. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  3286. } else {
  3287. /* First copy command data */
  3288. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  3289. MAILBOX_CMD_SIZE);
  3290. if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
  3291. pmbox->context2) {
  3292. lpfc_memcpy_from_slim((void *)pmbox->context2,
  3293. phba->MBslimaddr + DMP_RSP_OFFSET,
  3294. mb->un.varDmp.word_cnt);
  3295. }
  3296. }
  3297. writel(HA_MBATT, phba->HAregaddr);
  3298. readl(phba->HAregaddr); /* flush */
  3299. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3300. status = mb->mbxStatus;
  3301. }
  3302. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3303. return status;
  3304. out_not_finished:
  3305. if (processing_queue) {
  3306. pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
  3307. lpfc_mbox_cmpl_put(phba, pmbox);
  3308. }
  3309. return MBX_NOT_FINISHED;
  3310. }
  3311. /**
  3312. * __lpfc_sli_ringtx_put: Add an iocb to the txq.
  3313. * @phba: Pointer to HBA context object.
  3314. * @pring: Pointer to driver SLI ring object.
  3315. * @piocb: Pointer to address of newly added command iocb.
  3316. *
  3317. * This function is called with hbalock held to add a command
  3318. * iocb to the txq when SLI layer cannot submit the command iocb
  3319. * to the ring.
  3320. **/
  3321. static void
  3322. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3323. struct lpfc_iocbq *piocb)
  3324. {
  3325. /* Insert the caller's iocb in the txq tail for later processing. */
  3326. list_add_tail(&piocb->list, &pring->txq);
  3327. pring->txq_cnt++;
  3328. }
  3329. /**
  3330. * lpfc_sli_next_iocb: Get the next iocb in the txq.
  3331. * @phba: Pointer to HBA context object.
  3332. * @pring: Pointer to driver SLI ring object.
  3333. * @piocb: Pointer to address of newly added command iocb.
  3334. *
  3335. * This function is called with hbalock held before a new
  3336. * iocb is submitted to the firmware. This function checks
  3337. * txq to flush the iocbs in txq to Firmware before
  3338. * submitting new iocbs to the Firmware.
  3339. * If there are iocbs in the txq which need to be submitted
  3340. * to firmware, lpfc_sli_next_iocb returns the first element
  3341. * of the txq after dequeuing it from txq.
  3342. * If there is no iocb in the txq then the function will return
  3343. * *piocb and *piocb is set to NULL. Caller needs to check
  3344. * *piocb to find if there are more commands in the txq.
  3345. **/
  3346. static struct lpfc_iocbq *
  3347. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3348. struct lpfc_iocbq **piocb)
  3349. {
  3350. struct lpfc_iocbq * nextiocb;
  3351. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  3352. if (!nextiocb) {
  3353. nextiocb = *piocb;
  3354. *piocb = NULL;
  3355. }
  3356. return nextiocb;
  3357. }
  3358. /**
  3359. * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
  3360. * @phba: Pointer to HBA context object.
  3361. * @pring: Pointer to driver SLI ring object.
  3362. * @piocb: Pointer to command iocb.
  3363. * @flag: Flag indicating if this command can be put into txq.
  3364. *
  3365. * __lpfc_sli_issue_iocb is used by other functions in the driver
  3366. * to issue an iocb command to the HBA. If the PCI slot is recovering
  3367. * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  3368. * flag is turned on, the function returns IOCB_ERROR.
  3369. * When the link is down, this function allows only iocbs for
  3370. * posting buffers.
  3371. * This function finds next available slot in the command ring and
  3372. * posts the command to the available slot and writes the port
  3373. * attention register to request HBA start processing new iocb.
  3374. * If there is no slot available in the ring and
  3375. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
  3376. * txq, otherwise the function returns IOCB_BUSY.
  3377. *
  3378. * This function is called with hbalock held.
  3379. * The function will return success after it successfully submit the
  3380. * iocb to firmware or after adding to the txq.
  3381. **/
  3382. static int
  3383. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3384. struct lpfc_iocbq *piocb, uint32_t flag)
  3385. {
  3386. struct lpfc_iocbq *nextiocb;
  3387. IOCB_t *iocb;
  3388. if (piocb->iocb_cmpl && (!piocb->vport) &&
  3389. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  3390. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  3391. lpfc_printf_log(phba, KERN_ERR,
  3392. LOG_SLI | LOG_VPORT,
  3393. "1807 IOCB x%x failed. No vport\n",
  3394. piocb->iocb.ulpCommand);
  3395. dump_stack();
  3396. return IOCB_ERROR;
  3397. }
  3398. /* If the PCI channel is in offline state, do not post iocbs. */
  3399. if (unlikely(pci_channel_offline(phba->pcidev)))
  3400. return IOCB_ERROR;
  3401. /*
  3402. * We should never get an IOCB if we are in a < LINK_DOWN state
  3403. */
  3404. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  3405. return IOCB_ERROR;
  3406. /*
  3407. * Check to see if we are blocking IOCB processing because of a
  3408. * outstanding event.
  3409. */
  3410. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  3411. goto iocb_busy;
  3412. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  3413. /*
  3414. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  3415. * can be issued if the link is not up.
  3416. */
  3417. switch (piocb->iocb.ulpCommand) {
  3418. case CMD_GEN_REQUEST64_CR:
  3419. case CMD_GEN_REQUEST64_CX:
  3420. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  3421. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  3422. FC_FCP_CMND) ||
  3423. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  3424. MENLO_TRANSPORT_TYPE))
  3425. goto iocb_busy;
  3426. break;
  3427. case CMD_QUE_RING_BUF_CN:
  3428. case CMD_QUE_RING_BUF64_CN:
  3429. /*
  3430. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  3431. * completion, iocb_cmpl MUST be 0.
  3432. */
  3433. if (piocb->iocb_cmpl)
  3434. piocb->iocb_cmpl = NULL;
  3435. /*FALLTHROUGH*/
  3436. case CMD_CREATE_XRI_CR:
  3437. case CMD_CLOSE_XRI_CN:
  3438. case CMD_CLOSE_XRI_CX:
  3439. break;
  3440. default:
  3441. goto iocb_busy;
  3442. }
  3443. /*
  3444. * For FCP commands, we must be in a state where we can process link
  3445. * attention events.
  3446. */
  3447. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  3448. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  3449. goto iocb_busy;
  3450. }
  3451. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  3452. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  3453. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  3454. if (iocb)
  3455. lpfc_sli_update_ring(phba, pring);
  3456. else
  3457. lpfc_sli_update_full_ring(phba, pring);
  3458. if (!piocb)
  3459. return IOCB_SUCCESS;
  3460. goto out_busy;
  3461. iocb_busy:
  3462. pring->stats.iocb_cmd_delay++;
  3463. out_busy:
  3464. if (!(flag & SLI_IOCB_RET_IOCB)) {
  3465. __lpfc_sli_ringtx_put(phba, pring, piocb);
  3466. return IOCB_SUCCESS;
  3467. }
  3468. return IOCB_BUSY;
  3469. }
  3470. /**
  3471. * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
  3472. * @phba: Pointer to HBA context object.
  3473. * @pring: Pointer to driver SLI ring object.
  3474. * @piocb: Pointer to command iocb.
  3475. * @flag: Flag indicating if this command can be put into txq.
  3476. *
  3477. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  3478. * function. This function gets the hbalock and calls
  3479. * __lpfc_sli_issue_iocb function and will return the error returned
  3480. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  3481. * functions which do not hold hbalock.
  3482. **/
  3483. int
  3484. lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3485. struct lpfc_iocbq *piocb, uint32_t flag)
  3486. {
  3487. unsigned long iflags;
  3488. int rc;
  3489. spin_lock_irqsave(&phba->hbalock, iflags);
  3490. rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
  3491. spin_unlock_irqrestore(&phba->hbalock, iflags);
  3492. return rc;
  3493. }
  3494. /**
  3495. * lpfc_extra_ring_setup: Extra ring setup function.
  3496. * @phba: Pointer to HBA context object.
  3497. *
  3498. * This function is called while driver attaches with the
  3499. * HBA to setup the extra ring. The extra ring is used
  3500. * only when driver needs to support target mode functionality
  3501. * or IP over FC functionalities.
  3502. *
  3503. * This function is called with no lock held.
  3504. **/
  3505. static int
  3506. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  3507. {
  3508. struct lpfc_sli *psli;
  3509. struct lpfc_sli_ring *pring;
  3510. psli = &phba->sli;
  3511. /* Adjust cmd/rsp ring iocb entries more evenly */
  3512. /* Take some away from the FCP ring */
  3513. pring = &psli->ring[psli->fcp_ring];
  3514. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  3515. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  3516. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  3517. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  3518. /* and give them to the extra ring */
  3519. pring = &psli->ring[psli->extra_ring];
  3520. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  3521. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  3522. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  3523. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  3524. /* Setup default profile for this ring */
  3525. pring->iotag_max = 4096;
  3526. pring->num_mask = 1;
  3527. pring->prt[0].profile = 0; /* Mask 0 */
  3528. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  3529. pring->prt[0].type = phba->cfg_multi_ring_type;
  3530. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  3531. return 0;
  3532. }
  3533. /**
  3534. * lpfc_sli_async_event_handler: ASYNC iocb handler function.
  3535. * @phba: Pointer to HBA context object.
  3536. * @pring: Pointer to driver SLI ring object.
  3537. * @iocbq: Pointer to iocb object.
  3538. *
  3539. * This function is called by the slow ring event handler
  3540. * function when there is an ASYNC event iocb in the ring.
  3541. * This function is called with no lock held.
  3542. * Currently this function handles only temperature related
  3543. * ASYNC events. The function decodes the temperature sensor
  3544. * event message and posts events for the management applications.
  3545. **/
  3546. static void
  3547. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  3548. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  3549. {
  3550. IOCB_t *icmd;
  3551. uint16_t evt_code;
  3552. uint16_t temp;
  3553. struct temp_event temp_event_data;
  3554. struct Scsi_Host *shost;
  3555. icmd = &iocbq->iocb;
  3556. evt_code = icmd->un.asyncstat.evt_code;
  3557. temp = icmd->ulpContext;
  3558. if ((evt_code != ASYNC_TEMP_WARN) &&
  3559. (evt_code != ASYNC_TEMP_SAFE)) {
  3560. lpfc_printf_log(phba,
  3561. KERN_ERR,
  3562. LOG_SLI,
  3563. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  3564. " evt_code 0x%x\n",
  3565. pring->ringno,
  3566. icmd->un.asyncstat.evt_code);
  3567. return;
  3568. }
  3569. temp_event_data.data = (uint32_t)temp;
  3570. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  3571. if (evt_code == ASYNC_TEMP_WARN) {
  3572. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  3573. lpfc_printf_log(phba,
  3574. KERN_ERR,
  3575. LOG_TEMP,
  3576. "0347 Adapter is very hot, please take "
  3577. "corrective action. temperature : %d Celsius\n",
  3578. temp);
  3579. }
  3580. if (evt_code == ASYNC_TEMP_SAFE) {
  3581. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  3582. lpfc_printf_log(phba,
  3583. KERN_ERR,
  3584. LOG_TEMP,
  3585. "0340 Adapter temperature is OK now. "
  3586. "temperature : %d Celsius\n",
  3587. temp);
  3588. }
  3589. /* Send temperature change event to applications */
  3590. shost = lpfc_shost_from_vport(phba->pport);
  3591. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3592. sizeof(temp_event_data), (char *) &temp_event_data,
  3593. LPFC_NL_VENDOR_ID);
  3594. }
  3595. /**
  3596. * lpfc_sli_setup: SLI ring setup function.
  3597. * @phba: Pointer to HBA context object.
  3598. *
  3599. * lpfc_sli_setup sets up rings of the SLI interface with
  3600. * number of iocbs per ring and iotags. This function is
  3601. * called while driver attach to the HBA and before the
  3602. * interrupts are enabled. So there is no need for locking.
  3603. *
  3604. * This function always returns 0.
  3605. **/
  3606. int
  3607. lpfc_sli_setup(struct lpfc_hba *phba)
  3608. {
  3609. int i, totiocbsize = 0;
  3610. struct lpfc_sli *psli = &phba->sli;
  3611. struct lpfc_sli_ring *pring;
  3612. psli->num_rings = MAX_CONFIGURED_RINGS;
  3613. psli->sli_flag = 0;
  3614. psli->fcp_ring = LPFC_FCP_RING;
  3615. psli->next_ring = LPFC_FCP_NEXT_RING;
  3616. psli->extra_ring = LPFC_EXTRA_RING;
  3617. psli->iocbq_lookup = NULL;
  3618. psli->iocbq_lookup_len = 0;
  3619. psli->last_iotag = 0;
  3620. for (i = 0; i < psli->num_rings; i++) {
  3621. pring = &psli->ring[i];
  3622. switch (i) {
  3623. case LPFC_FCP_RING: /* ring 0 - FCP */
  3624. /* numCiocb and numRiocb are used in config_port */
  3625. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  3626. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  3627. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  3628. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  3629. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  3630. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  3631. pring->sizeCiocb = (phba->sli_rev == 3) ?
  3632. SLI3_IOCB_CMD_SIZE :
  3633. SLI2_IOCB_CMD_SIZE;
  3634. pring->sizeRiocb = (phba->sli_rev == 3) ?
  3635. SLI3_IOCB_RSP_SIZE :
  3636. SLI2_IOCB_RSP_SIZE;
  3637. pring->iotag_ctr = 0;
  3638. pring->iotag_max =
  3639. (phba->cfg_hba_queue_depth * 2);
  3640. pring->fast_iotag = pring->iotag_max;
  3641. pring->num_mask = 0;
  3642. break;
  3643. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  3644. /* numCiocb and numRiocb are used in config_port */
  3645. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  3646. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  3647. pring->sizeCiocb = (phba->sli_rev == 3) ?
  3648. SLI3_IOCB_CMD_SIZE :
  3649. SLI2_IOCB_CMD_SIZE;
  3650. pring->sizeRiocb = (phba->sli_rev == 3) ?
  3651. SLI3_IOCB_RSP_SIZE :
  3652. SLI2_IOCB_RSP_SIZE;
  3653. pring->iotag_max = phba->cfg_hba_queue_depth;
  3654. pring->num_mask = 0;
  3655. break;
  3656. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  3657. /* numCiocb and numRiocb are used in config_port */
  3658. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  3659. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  3660. pring->sizeCiocb = (phba->sli_rev == 3) ?
  3661. SLI3_IOCB_CMD_SIZE :
  3662. SLI2_IOCB_CMD_SIZE;
  3663. pring->sizeRiocb = (phba->sli_rev == 3) ?
  3664. SLI3_IOCB_RSP_SIZE :
  3665. SLI2_IOCB_RSP_SIZE;
  3666. pring->fast_iotag = 0;
  3667. pring->iotag_ctr = 0;
  3668. pring->iotag_max = 4096;
  3669. pring->lpfc_sli_rcv_async_status =
  3670. lpfc_sli_async_event_handler;
  3671. pring->num_mask = 4;
  3672. pring->prt[0].profile = 0; /* Mask 0 */
  3673. pring->prt[0].rctl = FC_ELS_REQ;
  3674. pring->prt[0].type = FC_ELS_DATA;
  3675. pring->prt[0].lpfc_sli_rcv_unsol_event =
  3676. lpfc_els_unsol_event;
  3677. pring->prt[1].profile = 0; /* Mask 1 */
  3678. pring->prt[1].rctl = FC_ELS_RSP;
  3679. pring->prt[1].type = FC_ELS_DATA;
  3680. pring->prt[1].lpfc_sli_rcv_unsol_event =
  3681. lpfc_els_unsol_event;
  3682. pring->prt[2].profile = 0; /* Mask 2 */
  3683. /* NameServer Inquiry */
  3684. pring->prt[2].rctl = FC_UNSOL_CTL;
  3685. /* NameServer */
  3686. pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
  3687. pring->prt[2].lpfc_sli_rcv_unsol_event =
  3688. lpfc_ct_unsol_event;
  3689. pring->prt[3].profile = 0; /* Mask 3 */
  3690. /* NameServer response */
  3691. pring->prt[3].rctl = FC_SOL_CTL;
  3692. /* NameServer */
  3693. pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
  3694. pring->prt[3].lpfc_sli_rcv_unsol_event =
  3695. lpfc_ct_unsol_event;
  3696. break;
  3697. }
  3698. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  3699. (pring->numRiocb * pring->sizeRiocb);
  3700. }
  3701. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  3702. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  3703. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  3704. "SLI2 SLIM Data: x%x x%lx\n",
  3705. phba->brd_no, totiocbsize,
  3706. (unsigned long) MAX_SLIM_IOCB_SIZE);
  3707. }
  3708. if (phba->cfg_multi_ring_support == 2)
  3709. lpfc_extra_ring_setup(phba);
  3710. return 0;
  3711. }
  3712. /**
  3713. * lpfc_sli_queue_setup: Queue initialization function.
  3714. * @phba: Pointer to HBA context object.
  3715. *
  3716. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  3717. * ring. This function also initializes ring indices of each ring.
  3718. * This function is called during the initialization of the SLI
  3719. * interface of an HBA.
  3720. * This function is called with no lock held and always returns
  3721. * 1.
  3722. **/
  3723. int
  3724. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  3725. {
  3726. struct lpfc_sli *psli;
  3727. struct lpfc_sli_ring *pring;
  3728. int i;
  3729. psli = &phba->sli;
  3730. spin_lock_irq(&phba->hbalock);
  3731. INIT_LIST_HEAD(&psli->mboxq);
  3732. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  3733. /* Initialize list headers for txq and txcmplq as double linked lists */
  3734. for (i = 0; i < psli->num_rings; i++) {
  3735. pring = &psli->ring[i];
  3736. pring->ringno = i;
  3737. pring->next_cmdidx = 0;
  3738. pring->local_getidx = 0;
  3739. pring->cmdidx = 0;
  3740. INIT_LIST_HEAD(&pring->txq);
  3741. INIT_LIST_HEAD(&pring->txcmplq);
  3742. INIT_LIST_HEAD(&pring->iocb_continueq);
  3743. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  3744. INIT_LIST_HEAD(&pring->postbufq);
  3745. }
  3746. spin_unlock_irq(&phba->hbalock);
  3747. return 1;
  3748. }
  3749. /**
  3750. * lpfc_sli_host_down: Vport cleanup function.
  3751. * @vport: Pointer to virtual port object.
  3752. *
  3753. * lpfc_sli_host_down is called to clean up the resources
  3754. * associated with a vport before destroying virtual
  3755. * port data structures.
  3756. * This function does following operations:
  3757. * - Free discovery resources associated with this virtual
  3758. * port.
  3759. * - Free iocbs associated with this virtual port in
  3760. * the txq.
  3761. * - Send abort for all iocb commands associated with this
  3762. * vport in txcmplq.
  3763. *
  3764. * This function is called with no lock held and always returns 1.
  3765. **/
  3766. int
  3767. lpfc_sli_host_down(struct lpfc_vport *vport)
  3768. {
  3769. LIST_HEAD(completions);
  3770. struct lpfc_hba *phba = vport->phba;
  3771. struct lpfc_sli *psli = &phba->sli;
  3772. struct lpfc_sli_ring *pring;
  3773. struct lpfc_iocbq *iocb, *next_iocb;
  3774. int i;
  3775. unsigned long flags = 0;
  3776. uint16_t prev_pring_flag;
  3777. lpfc_cleanup_discovery_resources(vport);
  3778. spin_lock_irqsave(&phba->hbalock, flags);
  3779. for (i = 0; i < psli->num_rings; i++) {
  3780. pring = &psli->ring[i];
  3781. prev_pring_flag = pring->flag;
  3782. /* Only slow rings */
  3783. if (pring->ringno == LPFC_ELS_RING) {
  3784. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  3785. /* Set the lpfc data pending flag */
  3786. set_bit(LPFC_DATA_READY, &phba->data_flags);
  3787. }
  3788. /*
  3789. * Error everything on the txq since these iocbs have not been
  3790. * given to the FW yet.
  3791. */
  3792. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  3793. if (iocb->vport != vport)
  3794. continue;
  3795. list_move_tail(&iocb->list, &completions);
  3796. pring->txq_cnt--;
  3797. }
  3798. /* Next issue ABTS for everything on the txcmplq */
  3799. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  3800. list) {
  3801. if (iocb->vport != vport)
  3802. continue;
  3803. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3804. }
  3805. pring->flag = prev_pring_flag;
  3806. }
  3807. spin_unlock_irqrestore(&phba->hbalock, flags);
  3808. while (!list_empty(&completions)) {
  3809. list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
  3810. if (!iocb->iocb_cmpl)
  3811. lpfc_sli_release_iocbq(phba, iocb);
  3812. else {
  3813. iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  3814. iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
  3815. (iocb->iocb_cmpl) (phba, iocb, iocb);
  3816. }
  3817. }
  3818. return 1;
  3819. }
  3820. /**
  3821. * lpfc_sli_hba_down: Resource cleanup function for the HBA.
  3822. * @phba: Pointer to HBA context object.
  3823. *
  3824. * This function cleans up all iocb, buffers, mailbox commands
  3825. * while shutting down the HBA. This function is called with no
  3826. * lock held and always returns 1.
  3827. * This function does the following to cleanup driver resources:
  3828. * - Free discovery resources for each virtual port
  3829. * - Cleanup any pending fabric iocbs
  3830. * - Iterate through the iocb txq and free each entry
  3831. * in the list.
  3832. * - Free up any buffer posted to the HBA
  3833. * - Free mailbox commands in the mailbox queue.
  3834. **/
  3835. int
  3836. lpfc_sli_hba_down(struct lpfc_hba *phba)
  3837. {
  3838. LIST_HEAD(completions);
  3839. struct lpfc_sli *psli = &phba->sli;
  3840. struct lpfc_sli_ring *pring;
  3841. struct lpfc_dmabuf *buf_ptr;
  3842. LPFC_MBOXQ_t *pmb;
  3843. struct lpfc_iocbq *iocb;
  3844. IOCB_t *cmd = NULL;
  3845. int i;
  3846. unsigned long flags = 0;
  3847. lpfc_hba_down_prep(phba);
  3848. lpfc_fabric_abort_hba(phba);
  3849. spin_lock_irqsave(&phba->hbalock, flags);
  3850. for (i = 0; i < psli->num_rings; i++) {
  3851. pring = &psli->ring[i];
  3852. /* Only slow rings */
  3853. if (pring->ringno == LPFC_ELS_RING) {
  3854. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  3855. /* Set the lpfc data pending flag */
  3856. set_bit(LPFC_DATA_READY, &phba->data_flags);
  3857. }
  3858. /*
  3859. * Error everything on the txq since these iocbs have not been
  3860. * given to the FW yet.
  3861. */
  3862. list_splice_init(&pring->txq, &completions);
  3863. pring->txq_cnt = 0;
  3864. }
  3865. spin_unlock_irqrestore(&phba->hbalock, flags);
  3866. while (!list_empty(&completions)) {
  3867. list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
  3868. cmd = &iocb->iocb;
  3869. if (!iocb->iocb_cmpl)
  3870. lpfc_sli_release_iocbq(phba, iocb);
  3871. else {
  3872. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  3873. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  3874. (iocb->iocb_cmpl) (phba, iocb, iocb);
  3875. }
  3876. }
  3877. spin_lock_irqsave(&phba->hbalock, flags);
  3878. list_splice_init(&phba->elsbuf, &completions);
  3879. phba->elsbuf_cnt = 0;
  3880. phba->elsbuf_prev_cnt = 0;
  3881. spin_unlock_irqrestore(&phba->hbalock, flags);
  3882. while (!list_empty(&completions)) {
  3883. list_remove_head(&completions, buf_ptr,
  3884. struct lpfc_dmabuf, list);
  3885. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  3886. kfree(buf_ptr);
  3887. }
  3888. /* Return any active mbox cmds */
  3889. del_timer_sync(&psli->mbox_tmo);
  3890. spin_lock_irqsave(&phba->hbalock, flags);
  3891. spin_lock(&phba->pport->work_port_lock);
  3892. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  3893. spin_unlock(&phba->pport->work_port_lock);
  3894. /* Return any pending or completed mbox cmds */
  3895. list_splice_init(&phba->sli.mboxq, &completions);
  3896. if (psli->mbox_active) {
  3897. list_add_tail(&psli->mbox_active->list, &completions);
  3898. psli->mbox_active = NULL;
  3899. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3900. }
  3901. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  3902. spin_unlock_irqrestore(&phba->hbalock, flags);
  3903. while (!list_empty(&completions)) {
  3904. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  3905. pmb->mb.mbxStatus = MBX_NOT_FINISHED;
  3906. if (pmb->mbox_cmpl)
  3907. pmb->mbox_cmpl(phba,pmb);
  3908. }
  3909. return 1;
  3910. }
  3911. /**
  3912. * lpfc_sli_pcimem_bcopy: SLI memory copy function.
  3913. * @srcp: Source memory pointer.
  3914. * @destp: Destination memory pointer.
  3915. * @cnt: Number of words required to be copied.
  3916. *
  3917. * This function is used for copying data between driver memory
  3918. * and the SLI memory. This function also changes the endianness
  3919. * of each word if native endianness is different from SLI
  3920. * endianness. This function can be called with or without
  3921. * lock.
  3922. **/
  3923. void
  3924. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  3925. {
  3926. uint32_t *src = srcp;
  3927. uint32_t *dest = destp;
  3928. uint32_t ldata;
  3929. int i;
  3930. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  3931. ldata = *src;
  3932. ldata = le32_to_cpu(ldata);
  3933. *dest = ldata;
  3934. src++;
  3935. dest++;
  3936. }
  3937. }
  3938. /**
  3939. * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
  3940. * @phba: Pointer to HBA context object.
  3941. * @pring: Pointer to driver SLI ring object.
  3942. * @mp: Pointer to driver buffer object.
  3943. *
  3944. * This function is called with no lock held.
  3945. * It always return zero after adding the buffer to the postbufq
  3946. * buffer list.
  3947. **/
  3948. int
  3949. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3950. struct lpfc_dmabuf *mp)
  3951. {
  3952. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  3953. later */
  3954. spin_lock_irq(&phba->hbalock);
  3955. list_add_tail(&mp->list, &pring->postbufq);
  3956. pring->postbufq_cnt++;
  3957. spin_unlock_irq(&phba->hbalock);
  3958. return 0;
  3959. }
  3960. /**
  3961. * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
  3962. * using CMD_QUE_XRI64_CX iocb.
  3963. * @phba: Pointer to HBA context object.
  3964. *
  3965. * When HBQ is enabled, buffers are searched based on tags. This function
  3966. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  3967. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  3968. * does not conflict with tags of buffer posted for unsolicited events.
  3969. * The function returns the allocated tag. The function is called with
  3970. * no locks held.
  3971. **/
  3972. uint32_t
  3973. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  3974. {
  3975. spin_lock_irq(&phba->hbalock);
  3976. phba->buffer_tag_count++;
  3977. /*
  3978. * Always set the QUE_BUFTAG_BIT to distiguish between
  3979. * a tag assigned by HBQ.
  3980. */
  3981. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  3982. spin_unlock_irq(&phba->hbalock);
  3983. return phba->buffer_tag_count;
  3984. }
  3985. /**
  3986. * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
  3987. * posted using CMD_QUE_XRI64_CX iocb.
  3988. * @phba: Pointer to HBA context object.
  3989. * @pring: Pointer to driver SLI ring object.
  3990. * @tag: Buffer tag.
  3991. *
  3992. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  3993. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  3994. * iocb is posted to the response ring with the tag of the buffer.
  3995. * This function searches the pring->postbufq list using the tag
  3996. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  3997. * iocb. If the buffer is found then lpfc_dmabuf object of the
  3998. * buffer is returned to the caller else NULL is returned.
  3999. * This function is called with no lock held.
  4000. **/
  4001. struct lpfc_dmabuf *
  4002. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  4003. uint32_t tag)
  4004. {
  4005. struct lpfc_dmabuf *mp, *next_mp;
  4006. struct list_head *slp = &pring->postbufq;
  4007. /* Search postbufq, from the begining, looking for a match on tag */
  4008. spin_lock_irq(&phba->hbalock);
  4009. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  4010. if (mp->buffer_tag == tag) {
  4011. list_del_init(&mp->list);
  4012. pring->postbufq_cnt--;
  4013. spin_unlock_irq(&phba->hbalock);
  4014. return mp;
  4015. }
  4016. }
  4017. spin_unlock_irq(&phba->hbalock);
  4018. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4019. "0402 Cannot find virtual addr for buffer tag on "
  4020. "ring %d Data x%lx x%p x%p x%x\n",
  4021. pring->ringno, (unsigned long) tag,
  4022. slp->next, slp->prev, pring->postbufq_cnt);
  4023. return NULL;
  4024. }
  4025. /**
  4026. * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
  4027. * unsolicited ct and els events.
  4028. * @phba: Pointer to HBA context object.
  4029. * @pring: Pointer to driver SLI ring object.
  4030. * @phys: DMA address of the buffer.
  4031. *
  4032. * This function searches the buffer list using the dma_address
  4033. * of unsolicited event to find the driver's lpfc_dmabuf object
  4034. * corresponding to the dma_address. The function returns the
  4035. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  4036. * This function is called by the ct and els unsolicited event
  4037. * handlers to get the buffer associated with the unsolicited
  4038. * event.
  4039. *
  4040. * This function is called with no lock held.
  4041. **/
  4042. struct lpfc_dmabuf *
  4043. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  4044. dma_addr_t phys)
  4045. {
  4046. struct lpfc_dmabuf *mp, *next_mp;
  4047. struct list_head *slp = &pring->postbufq;
  4048. /* Search postbufq, from the begining, looking for a match on phys */
  4049. spin_lock_irq(&phba->hbalock);
  4050. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  4051. if (mp->phys == phys) {
  4052. list_del_init(&mp->list);
  4053. pring->postbufq_cnt--;
  4054. spin_unlock_irq(&phba->hbalock);
  4055. return mp;
  4056. }
  4057. }
  4058. spin_unlock_irq(&phba->hbalock);
  4059. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4060. "0410 Cannot find virtual addr for mapped buf on "
  4061. "ring %d Data x%llx x%p x%p x%x\n",
  4062. pring->ringno, (unsigned long long)phys,
  4063. slp->next, slp->prev, pring->postbufq_cnt);
  4064. return NULL;
  4065. }
  4066. /**
  4067. * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
  4068. * @phba: Pointer to HBA context object.
  4069. * @cmdiocb: Pointer to driver command iocb object.
  4070. * @rspiocb: Pointer to driver response iocb object.
  4071. *
  4072. * This function is the completion handler for the abort iocbs for
  4073. * ELS commands. This function is called from the ELS ring event
  4074. * handler with no lock held. This function frees memory resources
  4075. * associated with the abort iocb.
  4076. **/
  4077. static void
  4078. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  4079. struct lpfc_iocbq *rspiocb)
  4080. {
  4081. IOCB_t *irsp = &rspiocb->iocb;
  4082. uint16_t abort_iotag, abort_context;
  4083. struct lpfc_iocbq *abort_iocb;
  4084. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  4085. abort_iocb = NULL;
  4086. if (irsp->ulpStatus) {
  4087. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  4088. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  4089. spin_lock_irq(&phba->hbalock);
  4090. if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
  4091. abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
  4092. lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
  4093. "0327 Cannot abort els iocb %p "
  4094. "with tag %x context %x, abort status %x, "
  4095. "abort code %x\n",
  4096. abort_iocb, abort_iotag, abort_context,
  4097. irsp->ulpStatus, irsp->un.ulpWord[4]);
  4098. /*
  4099. * If the iocb is not found in Firmware queue the iocb
  4100. * might have completed already. Do not free it again.
  4101. */
  4102. if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  4103. spin_unlock_irq(&phba->hbalock);
  4104. lpfc_sli_release_iocbq(phba, cmdiocb);
  4105. return;
  4106. }
  4107. /*
  4108. * make sure we have the right iocbq before taking it
  4109. * off the txcmplq and try to call completion routine.
  4110. */
  4111. if (!abort_iocb ||
  4112. abort_iocb->iocb.ulpContext != abort_context ||
  4113. (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
  4114. spin_unlock_irq(&phba->hbalock);
  4115. else {
  4116. list_del_init(&abort_iocb->list);
  4117. pring->txcmplq_cnt--;
  4118. spin_unlock_irq(&phba->hbalock);
  4119. /* Firmware could still be in progress of DMAing
  4120. * payload, so don't free data buffer till after
  4121. * a hbeat.
  4122. */
  4123. abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
  4124. abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  4125. abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  4126. abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
  4127. (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
  4128. }
  4129. }
  4130. lpfc_sli_release_iocbq(phba, cmdiocb);
  4131. return;
  4132. }
  4133. /**
  4134. * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
  4135. * @phba: Pointer to HBA context object.
  4136. * @cmdiocb: Pointer to driver command iocb object.
  4137. * @rspiocb: Pointer to driver response iocb object.
  4138. *
  4139. * The function is called from SLI ring event handler with no
  4140. * lock held. This function is the completion handler for ELS commands
  4141. * which are aborted. The function frees memory resources used for
  4142. * the aborted ELS commands.
  4143. **/
  4144. static void
  4145. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  4146. struct lpfc_iocbq *rspiocb)
  4147. {
  4148. IOCB_t *irsp = &rspiocb->iocb;
  4149. /* ELS cmd tag <ulpIoTag> completes */
  4150. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  4151. "0139 Ignoring ELS cmd tag x%x completion Data: "
  4152. "x%x x%x x%x\n",
  4153. irsp->ulpIoTag, irsp->ulpStatus,
  4154. irsp->un.ulpWord[4], irsp->ulpTimeout);
  4155. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  4156. lpfc_ct_free_iocb(phba, cmdiocb);
  4157. else
  4158. lpfc_els_free_iocb(phba, cmdiocb);
  4159. return;
  4160. }
  4161. /**
  4162. * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
  4163. * @phba: Pointer to HBA context object.
  4164. * @pring: Pointer to driver SLI ring object.
  4165. * @cmdiocb: Pointer to driver command iocb object.
  4166. *
  4167. * This function issues an abort iocb for the provided command
  4168. * iocb. This function is called with hbalock held.
  4169. * The function returns 0 when it fails due to memory allocation
  4170. * failure or when the command iocb is an abort request.
  4171. **/
  4172. int
  4173. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  4174. struct lpfc_iocbq *cmdiocb)
  4175. {
  4176. struct lpfc_vport *vport = cmdiocb->vport;
  4177. struct lpfc_iocbq *abtsiocbp;
  4178. IOCB_t *icmd = NULL;
  4179. IOCB_t *iabt = NULL;
  4180. int retval = IOCB_ERROR;
  4181. /*
  4182. * There are certain command types we don't want to abort. And we
  4183. * don't want to abort commands that are already in the process of
  4184. * being aborted.
  4185. */
  4186. icmd = &cmdiocb->iocb;
  4187. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  4188. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  4189. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  4190. return 0;
  4191. /* If we're unloading, don't abort iocb on the ELS ring, but change the
  4192. * callback so that nothing happens when it finishes.
  4193. */
  4194. if ((vport->load_flag & FC_UNLOADING) &&
  4195. (pring->ringno == LPFC_ELS_RING)) {
  4196. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  4197. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  4198. else
  4199. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  4200. goto abort_iotag_exit;
  4201. }
  4202. /* issue ABTS for this IOCB based on iotag */
  4203. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  4204. if (abtsiocbp == NULL)
  4205. return 0;
  4206. /* This signals the response to set the correct status
  4207. * before calling the completion handler.
  4208. */
  4209. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  4210. iabt = &abtsiocbp->iocb;
  4211. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  4212. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  4213. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  4214. iabt->ulpLe = 1;
  4215. iabt->ulpClass = icmd->ulpClass;
  4216. if (phba->link_state >= LPFC_LINK_UP)
  4217. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  4218. else
  4219. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  4220. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  4221. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  4222. "0339 Abort xri x%x, original iotag x%x, "
  4223. "abort cmd iotag x%x\n",
  4224. iabt->un.acxri.abortContextTag,
  4225. iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
  4226. retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
  4227. if (retval)
  4228. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  4229. abort_iotag_exit:
  4230. /*
  4231. * Caller to this routine should check for IOCB_ERROR
  4232. * and handle it properly. This routine no longer removes
  4233. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  4234. */
  4235. return retval;
  4236. }
  4237. /**
  4238. * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
  4239. * associated with a vport/SCSI target/lun.
  4240. * @iocbq: Pointer to driver iocb object.
  4241. * @vport: Pointer to driver virtual port object.
  4242. * @tgt_id: SCSI ID of the target.
  4243. * @lun_id: LUN ID of the scsi device.
  4244. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  4245. *
  4246. * This function acts as iocb filter for functions which abort or count
  4247. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  4248. * 0 if the filtering criteria is met for the given iocb and will return
  4249. * 1 if the filtering criteria is not met.
  4250. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  4251. * given iocb is for the SCSI device specified by vport, tgt_id and
  4252. * lun_id parameter.
  4253. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  4254. * given iocb is for the SCSI target specified by vport and tgt_id
  4255. * parameters.
  4256. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  4257. * given iocb is for the SCSI host associated with the given vport.
  4258. * This function is called with no locks held.
  4259. **/
  4260. static int
  4261. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  4262. uint16_t tgt_id, uint64_t lun_id,
  4263. lpfc_ctx_cmd ctx_cmd)
  4264. {
  4265. struct lpfc_scsi_buf *lpfc_cmd;
  4266. int rc = 1;
  4267. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  4268. return rc;
  4269. if (iocbq->vport != vport)
  4270. return rc;
  4271. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  4272. if (lpfc_cmd->pCmd == NULL)
  4273. return rc;
  4274. switch (ctx_cmd) {
  4275. case LPFC_CTX_LUN:
  4276. if ((lpfc_cmd->rdata->pnode) &&
  4277. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  4278. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  4279. rc = 0;
  4280. break;
  4281. case LPFC_CTX_TGT:
  4282. if ((lpfc_cmd->rdata->pnode) &&
  4283. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  4284. rc = 0;
  4285. break;
  4286. case LPFC_CTX_HOST:
  4287. rc = 0;
  4288. break;
  4289. default:
  4290. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  4291. __func__, ctx_cmd);
  4292. break;
  4293. }
  4294. return rc;
  4295. }
  4296. /**
  4297. * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
  4298. * @vport: Pointer to virtual port.
  4299. * @tgt_id: SCSI ID of the target.
  4300. * @lun_id: LUN ID of the scsi device.
  4301. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  4302. *
  4303. * This function returns number of FCP commands pending for the vport.
  4304. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  4305. * commands pending on the vport associated with SCSI device specified
  4306. * by tgt_id and lun_id parameters.
  4307. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  4308. * commands pending on the vport associated with SCSI target specified
  4309. * by tgt_id parameter.
  4310. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  4311. * commands pending on the vport.
  4312. * This function returns the number of iocbs which satisfy the filter.
  4313. * This function is called without any lock held.
  4314. **/
  4315. int
  4316. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  4317. lpfc_ctx_cmd ctx_cmd)
  4318. {
  4319. struct lpfc_hba *phba = vport->phba;
  4320. struct lpfc_iocbq *iocbq;
  4321. int sum, i;
  4322. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  4323. iocbq = phba->sli.iocbq_lookup[i];
  4324. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  4325. ctx_cmd) == 0)
  4326. sum++;
  4327. }
  4328. return sum;
  4329. }
  4330. /**
  4331. * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
  4332. * FCP iocb.
  4333. * @phba: Pointer to HBA context object
  4334. * @cmdiocb: Pointer to command iocb object.
  4335. * @rspiocb: Pointer to response iocb object.
  4336. *
  4337. * This function is called when an aborted FCP iocb completes. This
  4338. * function is called by the ring event handler with no lock held.
  4339. * This function frees the iocb.
  4340. **/
  4341. void
  4342. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  4343. struct lpfc_iocbq *rspiocb)
  4344. {
  4345. lpfc_sli_release_iocbq(phba, cmdiocb);
  4346. return;
  4347. }
  4348. /**
  4349. * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
  4350. * pending on a SCSI host(vport)/target/lun.
  4351. * @vport: Pointer to virtual port.
  4352. * @pring: Pointer to driver SLI ring object.
  4353. * @tgt_id: SCSI ID of the target.
  4354. * @lun_id: LUN ID of the scsi device.
  4355. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  4356. *
  4357. * This function sends an abort command for every SCSI command
  4358. * associated with the given virtual port pending on the ring
  4359. * filtered by lpfc_sli_validate_fcp_iocb function.
  4360. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  4361. * FCP iocbs associated with lun specified by tgt_id and lun_id
  4362. * parameters
  4363. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  4364. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  4365. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  4366. * FCP iocbs associated with virtual port.
  4367. * This function returns number of iocbs it failed to abort.
  4368. * This function is called with no locks held.
  4369. **/
  4370. int
  4371. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  4372. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  4373. {
  4374. struct lpfc_hba *phba = vport->phba;
  4375. struct lpfc_iocbq *iocbq;
  4376. struct lpfc_iocbq *abtsiocb;
  4377. IOCB_t *cmd = NULL;
  4378. int errcnt = 0, ret_val = 0;
  4379. int i;
  4380. for (i = 1; i <= phba->sli.last_iotag; i++) {
  4381. iocbq = phba->sli.iocbq_lookup[i];
  4382. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  4383. abort_cmd) != 0)
  4384. continue;
  4385. /* issue ABTS for this IOCB based on iotag */
  4386. abtsiocb = lpfc_sli_get_iocbq(phba);
  4387. if (abtsiocb == NULL) {
  4388. errcnt++;
  4389. continue;
  4390. }
  4391. cmd = &iocbq->iocb;
  4392. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  4393. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  4394. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  4395. abtsiocb->iocb.ulpLe = 1;
  4396. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  4397. abtsiocb->vport = phba->pport;
  4398. if (lpfc_is_link_up(phba))
  4399. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  4400. else
  4401. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  4402. /* Setup callback routine and issue the command. */
  4403. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  4404. ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
  4405. if (ret_val == IOCB_ERROR) {
  4406. lpfc_sli_release_iocbq(phba, abtsiocb);
  4407. errcnt++;
  4408. continue;
  4409. }
  4410. }
  4411. return errcnt;
  4412. }
  4413. /**
  4414. * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
  4415. * lpfc_sli_issue_iocb_wait.
  4416. * @phba: Pointer to HBA context object.
  4417. * @cmdiocbq: Pointer to command iocb.
  4418. * @rspiocbq: Pointer to response iocb.
  4419. *
  4420. * This function is the completion handler for iocbs issued using
  4421. * lpfc_sli_issue_iocb_wait function. This function is called by the
  4422. * ring event handler function without any lock held. This function
  4423. * can be called from both worker thread context and interrupt
  4424. * context. This function also can be called from other thread which
  4425. * cleans up the SLI layer objects.
  4426. * This function copy the contents of the response iocb to the
  4427. * response iocb memory object provided by the caller of
  4428. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  4429. * sleeps for the iocb completion.
  4430. **/
  4431. static void
  4432. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  4433. struct lpfc_iocbq *cmdiocbq,
  4434. struct lpfc_iocbq *rspiocbq)
  4435. {
  4436. wait_queue_head_t *pdone_q;
  4437. unsigned long iflags;
  4438. spin_lock_irqsave(&phba->hbalock, iflags);
  4439. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  4440. if (cmdiocbq->context2 && rspiocbq)
  4441. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  4442. &rspiocbq->iocb, sizeof(IOCB_t));
  4443. pdone_q = cmdiocbq->context_un.wait_queue;
  4444. if (pdone_q)
  4445. wake_up(pdone_q);
  4446. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4447. return;
  4448. }
  4449. /**
  4450. * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
  4451. * @phba: Pointer to HBA context object..
  4452. * @pring: Pointer to sli ring.
  4453. * @piocb: Pointer to command iocb.
  4454. * @prspiocbq: Pointer to response iocb.
  4455. * @timeout: Timeout in number of seconds.
  4456. *
  4457. * This function issues the iocb to firmware and waits for the
  4458. * iocb to complete. If the iocb command is not
  4459. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  4460. * Caller should not free the iocb resources if this function
  4461. * returns IOCB_TIMEDOUT.
  4462. * The function waits for the iocb completion using an
  4463. * non-interruptible wait.
  4464. * This function will sleep while waiting for iocb completion.
  4465. * So, this function should not be called from any context which
  4466. * does not allow sleeping. Due to the same reason, this function
  4467. * cannot be called with interrupt disabled.
  4468. * This function assumes that the iocb completions occur while
  4469. * this function sleep. So, this function cannot be called from
  4470. * the thread which process iocb completion for this ring.
  4471. * This function clears the iocb_flag of the iocb object before
  4472. * issuing the iocb and the iocb completion handler sets this
  4473. * flag and wakes this thread when the iocb completes.
  4474. * The contents of the response iocb will be copied to prspiocbq
  4475. * by the completion handler when the command completes.
  4476. * This function returns IOCB_SUCCESS when success.
  4477. * This function is called with no lock held.
  4478. **/
  4479. int
  4480. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  4481. struct lpfc_sli_ring *pring,
  4482. struct lpfc_iocbq *piocb,
  4483. struct lpfc_iocbq *prspiocbq,
  4484. uint32_t timeout)
  4485. {
  4486. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  4487. long timeleft, timeout_req = 0;
  4488. int retval = IOCB_SUCCESS;
  4489. uint32_t creg_val;
  4490. /*
  4491. * If the caller has provided a response iocbq buffer, then context2
  4492. * is NULL or its an error.
  4493. */
  4494. if (prspiocbq) {
  4495. if (piocb->context2)
  4496. return IOCB_ERROR;
  4497. piocb->context2 = prspiocbq;
  4498. }
  4499. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  4500. piocb->context_un.wait_queue = &done_q;
  4501. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  4502. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  4503. creg_val = readl(phba->HCregaddr);
  4504. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  4505. writel(creg_val, phba->HCregaddr);
  4506. readl(phba->HCregaddr); /* flush */
  4507. }
  4508. retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
  4509. if (retval == IOCB_SUCCESS) {
  4510. timeout_req = timeout * HZ;
  4511. timeleft = wait_event_timeout(done_q,
  4512. piocb->iocb_flag & LPFC_IO_WAKE,
  4513. timeout_req);
  4514. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  4515. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4516. "0331 IOCB wake signaled\n");
  4517. } else if (timeleft == 0) {
  4518. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4519. "0338 IOCB wait timeout error - no "
  4520. "wake response Data x%x\n", timeout);
  4521. retval = IOCB_TIMEDOUT;
  4522. } else {
  4523. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4524. "0330 IOCB wake NOT set, "
  4525. "Data x%x x%lx\n",
  4526. timeout, (timeleft / jiffies));
  4527. retval = IOCB_TIMEDOUT;
  4528. }
  4529. } else {
  4530. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4531. "0332 IOCB wait issue failed, Data x%x\n",
  4532. retval);
  4533. retval = IOCB_ERROR;
  4534. }
  4535. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  4536. creg_val = readl(phba->HCregaddr);
  4537. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  4538. writel(creg_val, phba->HCregaddr);
  4539. readl(phba->HCregaddr); /* flush */
  4540. }
  4541. if (prspiocbq)
  4542. piocb->context2 = NULL;
  4543. piocb->context_un.wait_queue = NULL;
  4544. piocb->iocb_cmpl = NULL;
  4545. return retval;
  4546. }
  4547. /**
  4548. * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
  4549. * @phba: Pointer to HBA context object.
  4550. * @pmboxq: Pointer to driver mailbox object.
  4551. * @timeout: Timeout in number of seconds.
  4552. *
  4553. * This function issues the mailbox to firmware and waits for the
  4554. * mailbox command to complete. If the mailbox command is not
  4555. * completed within timeout seconds, it returns MBX_TIMEOUT.
  4556. * The function waits for the mailbox completion using an
  4557. * interruptible wait. If the thread is woken up due to a
  4558. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  4559. * should not free the mailbox resources, if this function returns
  4560. * MBX_TIMEOUT.
  4561. * This function will sleep while waiting for mailbox completion.
  4562. * So, this function should not be called from any context which
  4563. * does not allow sleeping. Due to the same reason, this function
  4564. * cannot be called with interrupt disabled.
  4565. * This function assumes that the mailbox completion occurs while
  4566. * this function sleep. So, this function cannot be called from
  4567. * the worker thread which processes mailbox completion.
  4568. * This function is called in the context of HBA management
  4569. * applications.
  4570. * This function returns MBX_SUCCESS when successful.
  4571. * This function is called with no lock held.
  4572. **/
  4573. int
  4574. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  4575. uint32_t timeout)
  4576. {
  4577. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  4578. int retval;
  4579. unsigned long flag;
  4580. /* The caller must leave context1 empty. */
  4581. if (pmboxq->context1)
  4582. return MBX_NOT_FINISHED;
  4583. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  4584. /* setup wake call as IOCB callback */
  4585. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  4586. /* setup context field to pass wait_queue pointer to wake function */
  4587. pmboxq->context1 = &done_q;
  4588. /* now issue the command */
  4589. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  4590. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  4591. wait_event_interruptible_timeout(done_q,
  4592. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  4593. timeout * HZ);
  4594. spin_lock_irqsave(&phba->hbalock, flag);
  4595. pmboxq->context1 = NULL;
  4596. /*
  4597. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  4598. * else do not free the resources.
  4599. */
  4600. if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
  4601. retval = MBX_SUCCESS;
  4602. else {
  4603. retval = MBX_TIMEOUT;
  4604. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4605. }
  4606. spin_unlock_irqrestore(&phba->hbalock, flag);
  4607. }
  4608. return retval;
  4609. }
  4610. /**
  4611. * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
  4612. * @phba: Pointer to HBA context.
  4613. *
  4614. * This function is called to cleanup any pending mailbox
  4615. * objects in the driver queue before bringing the HBA offline.
  4616. * This function is called while resetting the HBA.
  4617. * The function is called without any lock held. The function
  4618. * takes hbalock to update SLI data structure.
  4619. * This function returns 1 when there is an active mailbox
  4620. * command pending else returns 0.
  4621. **/
  4622. int
  4623. lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
  4624. {
  4625. struct lpfc_vport *vport = phba->pport;
  4626. int i = 0;
  4627. uint32_t ha_copy;
  4628. while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
  4629. if (i++ > LPFC_MBOX_TMO * 1000)
  4630. return 1;
  4631. /*
  4632. * Call lpfc_sli_handle_mb_event only if a mailbox cmd
  4633. * did finish. This way we won't get the misleading
  4634. * "Stray Mailbox Interrupt" message.
  4635. */
  4636. spin_lock_irq(&phba->hbalock);
  4637. ha_copy = phba->work_ha;
  4638. phba->work_ha &= ~HA_MBATT;
  4639. spin_unlock_irq(&phba->hbalock);
  4640. if (ha_copy & HA_MBATT)
  4641. if (lpfc_sli_handle_mb_event(phba) == 0)
  4642. i = 0;
  4643. msleep(1);
  4644. }
  4645. return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
  4646. }
  4647. /**
  4648. * lpfc_sli_check_eratt: check error attention events
  4649. * @phba: Pointer to HBA context.
  4650. *
  4651. * This function is called form timer soft interrupt context to check HBA's
  4652. * error attention register bit for error attention events.
  4653. *
  4654. * This fucntion returns 1 when there is Error Attention in the Host Attention
  4655. * Register and returns 0 otherwise.
  4656. **/
  4657. int
  4658. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  4659. {
  4660. uint32_t ha_copy;
  4661. /* If PCI channel is offline, don't process it */
  4662. if (unlikely(pci_channel_offline(phba->pcidev)))
  4663. return 0;
  4664. /* If somebody is waiting to handle an eratt, don't process it
  4665. * here. The brdkill function will do this.
  4666. */
  4667. if (phba->link_flag & LS_IGNORE_ERATT)
  4668. return 0;
  4669. /* Check if interrupt handler handles this ERATT */
  4670. spin_lock_irq(&phba->hbalock);
  4671. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  4672. /* Interrupt handler has handled ERATT */
  4673. spin_unlock_irq(&phba->hbalock);
  4674. return 0;
  4675. }
  4676. /* Read chip Host Attention (HA) register */
  4677. ha_copy = readl(phba->HAregaddr);
  4678. if (ha_copy & HA_ERATT) {
  4679. /* Read host status register to retrieve error event */
  4680. lpfc_sli_read_hs(phba);
  4681. /* Set the driver HA work bitmap */
  4682. phba->work_ha |= HA_ERATT;
  4683. /* Indicate polling handles this ERATT */
  4684. phba->hba_flag |= HBA_ERATT_HANDLED;
  4685. spin_unlock_irq(&phba->hbalock);
  4686. return 1;
  4687. }
  4688. spin_unlock_irq(&phba->hbalock);
  4689. return 0;
  4690. }
  4691. /**
  4692. * lpfc_sp_intr_handler: The slow-path interrupt handler of lpfc driver.
  4693. * @irq: Interrupt number.
  4694. * @dev_id: The device context pointer.
  4695. *
  4696. * This function is directly called from the PCI layer as an interrupt
  4697. * service routine when the device is enabled with MSI-X multi-message
  4698. * interrupt mode and there are slow-path events in the HBA. However,
  4699. * when the device is enabled with either MSI or Pin-IRQ interrupt mode,
  4700. * this function is called as part of the device-level interrupt handler.
  4701. * When the PCI slot is in error recovery or the HBA is undergoing
  4702. * initialization, the interrupt handler will not process the interrupt.
  4703. * The link attention and ELS ring attention events are handled by the
  4704. * worker thread. The interrupt handler signals the worker thread and
  4705. * and returns for these events. This function is called without any
  4706. * lock held. It gets the hbalock to access and update SLI data
  4707. * structures.
  4708. *
  4709. * This function returns IRQ_HANDLED when interrupt is handled else it
  4710. * returns IRQ_NONE.
  4711. **/
  4712. irqreturn_t
  4713. lpfc_sp_intr_handler(int irq, void *dev_id)
  4714. {
  4715. struct lpfc_hba *phba;
  4716. uint32_t ha_copy;
  4717. uint32_t work_ha_copy;
  4718. unsigned long status;
  4719. unsigned long iflag;
  4720. uint32_t control;
  4721. MAILBOX_t *mbox, *pmbox;
  4722. struct lpfc_vport *vport;
  4723. struct lpfc_nodelist *ndlp;
  4724. struct lpfc_dmabuf *mp;
  4725. LPFC_MBOXQ_t *pmb;
  4726. int rc;
  4727. /*
  4728. * Get the driver's phba structure from the dev_id and
  4729. * assume the HBA is not interrupting.
  4730. */
  4731. phba = (struct lpfc_hba *)dev_id;
  4732. if (unlikely(!phba))
  4733. return IRQ_NONE;
  4734. /*
  4735. * Stuff needs to be attented to when this function is invoked as an
  4736. * individual interrupt handler in MSI-X multi-message interrupt mode
  4737. */
  4738. if (phba->intr_type == MSIX) {
  4739. /* If the pci channel is offline, ignore all the interrupts */
  4740. if (unlikely(pci_channel_offline(phba->pcidev)))
  4741. return IRQ_NONE;
  4742. /* Update device-level interrupt statistics */
  4743. phba->sli.slistat.sli_intr++;
  4744. /* Ignore all interrupts during initialization. */
  4745. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  4746. return IRQ_NONE;
  4747. /* Need to read HA REG for slow-path events */
  4748. spin_lock_irqsave(&phba->hbalock, iflag);
  4749. ha_copy = readl(phba->HAregaddr);
  4750. /* If somebody is waiting to handle an eratt don't process it
  4751. * here. The brdkill function will do this.
  4752. */
  4753. if (phba->link_flag & LS_IGNORE_ERATT)
  4754. ha_copy &= ~HA_ERATT;
  4755. /* Check the need for handling ERATT in interrupt handler */
  4756. if (ha_copy & HA_ERATT) {
  4757. if (phba->hba_flag & HBA_ERATT_HANDLED)
  4758. /* ERATT polling has handled ERATT */
  4759. ha_copy &= ~HA_ERATT;
  4760. else
  4761. /* Indicate interrupt handler handles ERATT */
  4762. phba->hba_flag |= HBA_ERATT_HANDLED;
  4763. }
  4764. /* Clear up only attention source related to slow-path */
  4765. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  4766. phba->HAregaddr);
  4767. readl(phba->HAregaddr); /* flush */
  4768. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4769. } else
  4770. ha_copy = phba->ha_copy;
  4771. work_ha_copy = ha_copy & phba->work_ha_mask;
  4772. if (work_ha_copy) {
  4773. if (work_ha_copy & HA_LATT) {
  4774. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  4775. /*
  4776. * Turn off Link Attention interrupts
  4777. * until CLEAR_LA done
  4778. */
  4779. spin_lock_irqsave(&phba->hbalock, iflag);
  4780. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  4781. control = readl(phba->HCregaddr);
  4782. control &= ~HC_LAINT_ENA;
  4783. writel(control, phba->HCregaddr);
  4784. readl(phba->HCregaddr); /* flush */
  4785. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4786. }
  4787. else
  4788. work_ha_copy &= ~HA_LATT;
  4789. }
  4790. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  4791. /*
  4792. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  4793. * the only slow ring.
  4794. */
  4795. status = (work_ha_copy &
  4796. (HA_RXMASK << (4*LPFC_ELS_RING)));
  4797. status >>= (4*LPFC_ELS_RING);
  4798. if (status & HA_RXMASK) {
  4799. spin_lock_irqsave(&phba->hbalock, iflag);
  4800. control = readl(phba->HCregaddr);
  4801. lpfc_debugfs_slow_ring_trc(phba,
  4802. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  4803. control, status,
  4804. (uint32_t)phba->sli.slistat.sli_intr);
  4805. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  4806. lpfc_debugfs_slow_ring_trc(phba,
  4807. "ISR Disable ring:"
  4808. "pwork:x%x hawork:x%x wait:x%x",
  4809. phba->work_ha, work_ha_copy,
  4810. (uint32_t)((unsigned long)
  4811. &phba->work_waitq));
  4812. control &=
  4813. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  4814. writel(control, phba->HCregaddr);
  4815. readl(phba->HCregaddr); /* flush */
  4816. }
  4817. else {
  4818. lpfc_debugfs_slow_ring_trc(phba,
  4819. "ISR slow ring: pwork:"
  4820. "x%x hawork:x%x wait:x%x",
  4821. phba->work_ha, work_ha_copy,
  4822. (uint32_t)((unsigned long)
  4823. &phba->work_waitq));
  4824. }
  4825. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4826. }
  4827. }
  4828. spin_lock_irqsave(&phba->hbalock, iflag);
  4829. if (work_ha_copy & HA_ERATT)
  4830. lpfc_sli_read_hs(phba);
  4831. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  4832. pmb = phba->sli.mbox_active;
  4833. pmbox = &pmb->mb;
  4834. mbox = phba->mbox;
  4835. vport = pmb->vport;
  4836. /* First check out the status word */
  4837. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  4838. if (pmbox->mbxOwner != OWN_HOST) {
  4839. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4840. /*
  4841. * Stray Mailbox Interrupt, mbxCommand <cmd>
  4842. * mbxStatus <status>
  4843. */
  4844. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  4845. LOG_SLI,
  4846. "(%d):0304 Stray Mailbox "
  4847. "Interrupt mbxCommand x%x "
  4848. "mbxStatus x%x\n",
  4849. (vport ? vport->vpi : 0),
  4850. pmbox->mbxCommand,
  4851. pmbox->mbxStatus);
  4852. /* clear mailbox attention bit */
  4853. work_ha_copy &= ~HA_MBATT;
  4854. } else {
  4855. phba->sli.mbox_active = NULL;
  4856. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4857. phba->last_completion_time = jiffies;
  4858. del_timer(&phba->sli.mbox_tmo);
  4859. if (pmb->mbox_cmpl) {
  4860. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  4861. MAILBOX_CMD_SIZE);
  4862. }
  4863. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  4864. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  4865. lpfc_debugfs_disc_trc(vport,
  4866. LPFC_DISC_TRC_MBOX_VPORT,
  4867. "MBOX dflt rpi: : "
  4868. "status:x%x rpi:x%x",
  4869. (uint32_t)pmbox->mbxStatus,
  4870. pmbox->un.varWords[0], 0);
  4871. if (!pmbox->mbxStatus) {
  4872. mp = (struct lpfc_dmabuf *)
  4873. (pmb->context1);
  4874. ndlp = (struct lpfc_nodelist *)
  4875. pmb->context2;
  4876. /* Reg_LOGIN of dflt RPI was
  4877. * successful. new lets get
  4878. * rid of the RPI using the
  4879. * same mbox buffer.
  4880. */
  4881. lpfc_unreg_login(phba,
  4882. vport->vpi,
  4883. pmbox->un.varWords[0],
  4884. pmb);
  4885. pmb->mbox_cmpl =
  4886. lpfc_mbx_cmpl_dflt_rpi;
  4887. pmb->context1 = mp;
  4888. pmb->context2 = ndlp;
  4889. pmb->vport = vport;
  4890. rc = lpfc_sli_issue_mbox(phba,
  4891. pmb,
  4892. MBX_NOWAIT);
  4893. if (rc != MBX_BUSY)
  4894. lpfc_printf_log(phba,
  4895. KERN_ERR,
  4896. LOG_MBOX | LOG_SLI,
  4897. "0350 rc should have"
  4898. "been MBX_BUSY");
  4899. goto send_current_mbox;
  4900. }
  4901. }
  4902. spin_lock_irqsave(
  4903. &phba->pport->work_port_lock,
  4904. iflag);
  4905. phba->pport->work_port_events &=
  4906. ~WORKER_MBOX_TMO;
  4907. spin_unlock_irqrestore(
  4908. &phba->pport->work_port_lock,
  4909. iflag);
  4910. lpfc_mbox_cmpl_put(phba, pmb);
  4911. }
  4912. } else
  4913. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4914. if ((work_ha_copy & HA_MBATT) &&
  4915. (phba->sli.mbox_active == NULL)) {
  4916. send_current_mbox:
  4917. /* Process next mailbox command if there is one */
  4918. do {
  4919. rc = lpfc_sli_issue_mbox(phba, NULL,
  4920. MBX_NOWAIT);
  4921. } while (rc == MBX_NOT_FINISHED);
  4922. if (rc != MBX_SUCCESS)
  4923. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  4924. LOG_SLI, "0349 rc should be "
  4925. "MBX_SUCCESS");
  4926. }
  4927. spin_lock_irqsave(&phba->hbalock, iflag);
  4928. phba->work_ha |= work_ha_copy;
  4929. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4930. lpfc_worker_wake_up(phba);
  4931. }
  4932. return IRQ_HANDLED;
  4933. } /* lpfc_sp_intr_handler */
  4934. /**
  4935. * lpfc_fp_intr_handler: The fast-path interrupt handler of lpfc driver.
  4936. * @irq: Interrupt number.
  4937. * @dev_id: The device context pointer.
  4938. *
  4939. * This function is directly called from the PCI layer as an interrupt
  4940. * service routine when the device is enabled with MSI-X multi-message
  4941. * interrupt mode and there is a fast-path FCP IOCB ring event in the
  4942. * HBA. However, when the device is enabled with either MSI or Pin-IRQ
  4943. * interrupt mode, this function is called as part of the device-level
  4944. * interrupt handler. When the PCI slot is in error recovery or the HBA
  4945. * is undergoing initialization, the interrupt handler will not process
  4946. * the interrupt. The SCSI FCP fast-path ring event are handled in the
  4947. * intrrupt context. This function is called without any lock held. It
  4948. * gets the hbalock to access and update SLI data structures.
  4949. *
  4950. * This function returns IRQ_HANDLED when interrupt is handled else it
  4951. * returns IRQ_NONE.
  4952. **/
  4953. irqreturn_t
  4954. lpfc_fp_intr_handler(int irq, void *dev_id)
  4955. {
  4956. struct lpfc_hba *phba;
  4957. uint32_t ha_copy;
  4958. unsigned long status;
  4959. unsigned long iflag;
  4960. /* Get the driver's phba structure from the dev_id and
  4961. * assume the HBA is not interrupting.
  4962. */
  4963. phba = (struct lpfc_hba *) dev_id;
  4964. if (unlikely(!phba))
  4965. return IRQ_NONE;
  4966. /*
  4967. * Stuff needs to be attented to when this function is invoked as an
  4968. * individual interrupt handler in MSI-X multi-message interrupt mode
  4969. */
  4970. if (phba->intr_type == MSIX) {
  4971. /* If pci channel is offline, ignore all the interrupts */
  4972. if (unlikely(pci_channel_offline(phba->pcidev)))
  4973. return IRQ_NONE;
  4974. /* Update device-level interrupt statistics */
  4975. phba->sli.slistat.sli_intr++;
  4976. /* Ignore all interrupts during initialization. */
  4977. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  4978. return IRQ_NONE;
  4979. /* Need to read HA REG for FCP ring and other ring events */
  4980. ha_copy = readl(phba->HAregaddr);
  4981. /* Clear up only attention source related to fast-path */
  4982. spin_lock_irqsave(&phba->hbalock, iflag);
  4983. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  4984. phba->HAregaddr);
  4985. readl(phba->HAregaddr); /* flush */
  4986. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4987. } else
  4988. ha_copy = phba->ha_copy;
  4989. /*
  4990. * Process all events on FCP ring. Take the optimized path for FCP IO.
  4991. */
  4992. ha_copy &= ~(phba->work_ha_mask);
  4993. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  4994. status >>= (4*LPFC_FCP_RING);
  4995. if (status & HA_RXMASK)
  4996. lpfc_sli_handle_fast_ring_event(phba,
  4997. &phba->sli.ring[LPFC_FCP_RING],
  4998. status);
  4999. if (phba->cfg_multi_ring_support == 2) {
  5000. /*
  5001. * Process all events on extra ring. Take the optimized path
  5002. * for extra ring IO.
  5003. */
  5004. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  5005. status >>= (4*LPFC_EXTRA_RING);
  5006. if (status & HA_RXMASK) {
  5007. lpfc_sli_handle_fast_ring_event(phba,
  5008. &phba->sli.ring[LPFC_EXTRA_RING],
  5009. status);
  5010. }
  5011. }
  5012. return IRQ_HANDLED;
  5013. } /* lpfc_fp_intr_handler */
  5014. /**
  5015. * lpfc_intr_handler: The device-level interrupt handler of lpfc driver.
  5016. * @irq: Interrupt number.
  5017. * @dev_id: The device context pointer.
  5018. *
  5019. * This function is the device-level interrupt handler called from the PCI
  5020. * layer when either MSI or Pin-IRQ interrupt mode is enabled and there is
  5021. * an event in the HBA which requires driver attention. This function
  5022. * invokes the slow-path interrupt attention handling function and fast-path
  5023. * interrupt attention handling function in turn to process the relevant
  5024. * HBA attention events. This function is called without any lock held. It
  5025. * gets the hbalock to access and update SLI data structures.
  5026. *
  5027. * This function returns IRQ_HANDLED when interrupt is handled, else it
  5028. * returns IRQ_NONE.
  5029. **/
  5030. irqreturn_t
  5031. lpfc_intr_handler(int irq, void *dev_id)
  5032. {
  5033. struct lpfc_hba *phba;
  5034. irqreturn_t sp_irq_rc, fp_irq_rc;
  5035. unsigned long status1, status2;
  5036. /*
  5037. * Get the driver's phba structure from the dev_id and
  5038. * assume the HBA is not interrupting.
  5039. */
  5040. phba = (struct lpfc_hba *) dev_id;
  5041. if (unlikely(!phba))
  5042. return IRQ_NONE;
  5043. /* If the pci channel is offline, ignore all the interrupts. */
  5044. if (unlikely(pci_channel_offline(phba->pcidev)))
  5045. return IRQ_NONE;
  5046. /* Update device level interrupt statistics */
  5047. phba->sli.slistat.sli_intr++;
  5048. /* Ignore all interrupts during initialization. */
  5049. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  5050. return IRQ_NONE;
  5051. spin_lock(&phba->hbalock);
  5052. phba->ha_copy = readl(phba->HAregaddr);
  5053. if (unlikely(!phba->ha_copy)) {
  5054. spin_unlock(&phba->hbalock);
  5055. return IRQ_NONE;
  5056. } else if (phba->ha_copy & HA_ERATT) {
  5057. if (phba->hba_flag & HBA_ERATT_HANDLED)
  5058. /* ERATT polling has handled ERATT */
  5059. phba->ha_copy &= ~HA_ERATT;
  5060. else
  5061. /* Indicate interrupt handler handles ERATT */
  5062. phba->hba_flag |= HBA_ERATT_HANDLED;
  5063. }
  5064. /* Clear attention sources except link and error attentions */
  5065. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  5066. readl(phba->HAregaddr); /* flush */
  5067. spin_unlock(&phba->hbalock);
  5068. /*
  5069. * Invokes slow-path host attention interrupt handling as appropriate.
  5070. */
  5071. /* status of events with mailbox and link attention */
  5072. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  5073. /* status of events with ELS ring */
  5074. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  5075. status2 >>= (4*LPFC_ELS_RING);
  5076. if (status1 || (status2 & HA_RXMASK))
  5077. sp_irq_rc = lpfc_sp_intr_handler(irq, dev_id);
  5078. else
  5079. sp_irq_rc = IRQ_NONE;
  5080. /*
  5081. * Invoke fast-path host attention interrupt handling as appropriate.
  5082. */
  5083. /* status of events with FCP ring */
  5084. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  5085. status1 >>= (4*LPFC_FCP_RING);
  5086. /* status of events with extra ring */
  5087. if (phba->cfg_multi_ring_support == 2) {
  5088. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  5089. status2 >>= (4*LPFC_EXTRA_RING);
  5090. } else
  5091. status2 = 0;
  5092. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  5093. fp_irq_rc = lpfc_fp_intr_handler(irq, dev_id);
  5094. else
  5095. fp_irq_rc = IRQ_NONE;
  5096. /* Return device-level interrupt handling status */
  5097. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  5098. } /* lpfc_intr_handler */