lpfc_sli.c 166 KB

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