ql4_os.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/slab.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/iscsi_boot_sysfs.h>
  11. #include <linux/inet.h>
  12. #include <scsi/scsi_tcq.h>
  13. #include <scsi/scsicam.h>
  14. #include "ql4_def.h"
  15. #include "ql4_version.h"
  16. #include "ql4_glbl.h"
  17. #include "ql4_dbg.h"
  18. #include "ql4_inline.h"
  19. /*
  20. * Driver version
  21. */
  22. static char qla4xxx_version_str[40];
  23. /*
  24. * SRB allocation cache
  25. */
  26. static struct kmem_cache *srb_cachep;
  27. /*
  28. * Module parameter information and variables
  29. */
  30. static int ql4xdisablesysfsboot = 1;
  31. module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
  32. MODULE_PARM_DESC(ql4xdisablesysfsboot,
  33. " Set to disable exporting boot targets to sysfs.\n"
  34. "\t\t 0 - Export boot targets\n"
  35. "\t\t 1 - Do not export boot targets (Default)");
  36. int ql4xdontresethba;
  37. module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  38. MODULE_PARM_DESC(ql4xdontresethba,
  39. " Don't reset the HBA for driver recovery.\n"
  40. "\t\t 0 - It will reset HBA (Default)\n"
  41. "\t\t 1 - It will NOT reset HBA");
  42. int ql4xextended_error_logging;
  43. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  44. MODULE_PARM_DESC(ql4xextended_error_logging,
  45. " Option to enable extended error logging.\n"
  46. "\t\t 0 - no logging (Default)\n"
  47. "\t\t 2 - debug logging");
  48. int ql4xenablemsix = 1;
  49. module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  50. MODULE_PARM_DESC(ql4xenablemsix,
  51. " Set to enable MSI or MSI-X interrupt mechanism.\n"
  52. "\t\t 0 = enable INTx interrupt mechanism.\n"
  53. "\t\t 1 = enable MSI-X interrupt mechanism (Default).\n"
  54. "\t\t 2 = enable MSI interrupt mechanism.");
  55. #define QL4_DEF_QDEPTH 32
  56. static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
  57. module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
  58. MODULE_PARM_DESC(ql4xmaxqdepth,
  59. " Maximum queue depth to report for target devices.\n"
  60. "\t\t Default: 32.");
  61. static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
  62. module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
  63. MODULE_PARM_DESC(ql4xsess_recovery_tmo,
  64. " Target Session Recovery Timeout.\n"
  65. "\t\t Default: 120 sec.");
  66. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
  67. /*
  68. * SCSI host template entry points
  69. */
  70. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  71. /*
  72. * iSCSI template entry points
  73. */
  74. static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
  75. enum iscsi_param param, char *buf);
  76. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  77. enum iscsi_param param, char *buf);
  78. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  79. enum iscsi_host_param param, char *buf);
  80. static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
  81. uint32_t len);
  82. static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  83. enum iscsi_param_type param_type,
  84. int param, char *buf);
  85. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
  86. static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
  87. struct sockaddr *dst_addr,
  88. int non_blocking);
  89. static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
  90. static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
  91. static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
  92. enum iscsi_param param, char *buf);
  93. static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
  94. static struct iscsi_cls_conn *
  95. qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
  96. static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  97. struct iscsi_cls_conn *cls_conn,
  98. uint64_t transport_fd, int is_leading);
  99. static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
  100. static struct iscsi_cls_session *
  101. qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  102. uint16_t qdepth, uint32_t initial_cmdsn);
  103. static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
  104. static void qla4xxx_task_work(struct work_struct *wdata);
  105. static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
  106. static int qla4xxx_task_xmit(struct iscsi_task *);
  107. static void qla4xxx_task_cleanup(struct iscsi_task *);
  108. static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
  109. static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  110. struct iscsi_stats *stats);
  111. static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
  112. uint32_t iface_type, uint32_t payload_size,
  113. uint32_t pid, struct sockaddr *dst_addr);
  114. static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
  115. uint32_t *num_entries, char *buf);
  116. static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx);
  117. /*
  118. * SCSI host template entry points
  119. */
  120. static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  121. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  122. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  123. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  124. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  125. static int qla4xxx_slave_alloc(struct scsi_device *device);
  126. static int qla4xxx_slave_configure(struct scsi_device *device);
  127. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  128. static umode_t ql4_attr_is_visible(int param_type, int param);
  129. static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
  130. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  131. QLA82XX_LEGACY_INTR_CONFIG;
  132. static struct scsi_host_template qla4xxx_driver_template = {
  133. .module = THIS_MODULE,
  134. .name = DRIVER_NAME,
  135. .proc_name = DRIVER_NAME,
  136. .queuecommand = qla4xxx_queuecommand,
  137. .eh_abort_handler = qla4xxx_eh_abort,
  138. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  139. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  140. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  141. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  142. .slave_configure = qla4xxx_slave_configure,
  143. .slave_alloc = qla4xxx_slave_alloc,
  144. .slave_destroy = qla4xxx_slave_destroy,
  145. .this_id = -1,
  146. .cmd_per_lun = 3,
  147. .use_clustering = ENABLE_CLUSTERING,
  148. .sg_tablesize = SG_ALL,
  149. .max_sectors = 0xFFFF,
  150. .shost_attrs = qla4xxx_host_attrs,
  151. .host_reset = qla4xxx_host_reset,
  152. .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
  153. };
  154. static struct iscsi_transport qla4xxx_iscsi_transport = {
  155. .owner = THIS_MODULE,
  156. .name = DRIVER_NAME,
  157. .caps = CAP_TEXT_NEGO |
  158. CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
  159. CAP_DATADGST | CAP_LOGIN_OFFLOAD |
  160. CAP_MULTI_R2T,
  161. .attr_is_visible = ql4_attr_is_visible,
  162. .create_session = qla4xxx_session_create,
  163. .destroy_session = qla4xxx_session_destroy,
  164. .start_conn = qla4xxx_conn_start,
  165. .create_conn = qla4xxx_conn_create,
  166. .bind_conn = qla4xxx_conn_bind,
  167. .stop_conn = iscsi_conn_stop,
  168. .destroy_conn = qla4xxx_conn_destroy,
  169. .set_param = iscsi_set_param,
  170. .get_conn_param = qla4xxx_conn_get_param,
  171. .get_session_param = qla4xxx_session_get_param,
  172. .get_ep_param = qla4xxx_get_ep_param,
  173. .ep_connect = qla4xxx_ep_connect,
  174. .ep_poll = qla4xxx_ep_poll,
  175. .ep_disconnect = qla4xxx_ep_disconnect,
  176. .get_stats = qla4xxx_conn_get_stats,
  177. .send_pdu = iscsi_conn_send_pdu,
  178. .xmit_task = qla4xxx_task_xmit,
  179. .cleanup_task = qla4xxx_task_cleanup,
  180. .alloc_pdu = qla4xxx_alloc_pdu,
  181. .get_host_param = qla4xxx_host_get_param,
  182. .set_iface_param = qla4xxx_iface_set_param,
  183. .get_iface_param = qla4xxx_get_iface_param,
  184. .bsg_request = qla4xxx_bsg_request,
  185. .send_ping = qla4xxx_send_ping,
  186. .get_chap = qla4xxx_get_chap_list,
  187. .delete_chap = qla4xxx_delete_chap,
  188. };
  189. static struct scsi_transport_template *qla4xxx_scsi_transport;
  190. static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
  191. uint32_t iface_type, uint32_t payload_size,
  192. uint32_t pid, struct sockaddr *dst_addr)
  193. {
  194. struct scsi_qla_host *ha = to_qla_host(shost);
  195. struct sockaddr_in *addr;
  196. struct sockaddr_in6 *addr6;
  197. uint32_t options = 0;
  198. uint8_t ipaddr[IPv6_ADDR_LEN];
  199. int rval;
  200. memset(ipaddr, 0, IPv6_ADDR_LEN);
  201. /* IPv4 to IPv4 */
  202. if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
  203. (dst_addr->sa_family == AF_INET)) {
  204. addr = (struct sockaddr_in *)dst_addr;
  205. memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
  206. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
  207. "dest: %pI4\n", __func__,
  208. &ha->ip_config.ip_address, ipaddr));
  209. rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
  210. ipaddr);
  211. if (rval)
  212. rval = -EINVAL;
  213. } else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
  214. (dst_addr->sa_family == AF_INET6)) {
  215. /* IPv6 to IPv6 */
  216. addr6 = (struct sockaddr_in6 *)dst_addr;
  217. memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
  218. options |= PING_IPV6_PROTOCOL_ENABLE;
  219. /* Ping using LinkLocal address */
  220. if ((iface_num == 0) || (iface_num == 1)) {
  221. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
  222. "src: %pI6 dest: %pI6\n", __func__,
  223. &ha->ip_config.ipv6_link_local_addr,
  224. ipaddr));
  225. options |= PING_IPV6_LINKLOCAL_ADDR;
  226. rval = qla4xxx_ping_iocb(ha, options, payload_size,
  227. pid, ipaddr);
  228. } else {
  229. ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
  230. "not supported\n", __func__, iface_num);
  231. rval = -ENOSYS;
  232. goto exit_send_ping;
  233. }
  234. /*
  235. * If ping using LinkLocal address fails, try ping using
  236. * IPv6 address
  237. */
  238. if (rval != QLA_SUCCESS) {
  239. options &= ~PING_IPV6_LINKLOCAL_ADDR;
  240. if (iface_num == 0) {
  241. options |= PING_IPV6_ADDR0;
  242. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
  243. "Ping src: %pI6 "
  244. "dest: %pI6\n", __func__,
  245. &ha->ip_config.ipv6_addr0,
  246. ipaddr));
  247. } else if (iface_num == 1) {
  248. options |= PING_IPV6_ADDR1;
  249. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
  250. "Ping src: %pI6 "
  251. "dest: %pI6\n", __func__,
  252. &ha->ip_config.ipv6_addr1,
  253. ipaddr));
  254. }
  255. rval = qla4xxx_ping_iocb(ha, options, payload_size,
  256. pid, ipaddr);
  257. if (rval)
  258. rval = -EINVAL;
  259. }
  260. } else
  261. rval = -ENOSYS;
  262. exit_send_ping:
  263. return rval;
  264. }
  265. static umode_t ql4_attr_is_visible(int param_type, int param)
  266. {
  267. switch (param_type) {
  268. case ISCSI_HOST_PARAM:
  269. switch (param) {
  270. case ISCSI_HOST_PARAM_HWADDRESS:
  271. case ISCSI_HOST_PARAM_IPADDRESS:
  272. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  273. case ISCSI_HOST_PARAM_PORT_STATE:
  274. case ISCSI_HOST_PARAM_PORT_SPEED:
  275. return S_IRUGO;
  276. default:
  277. return 0;
  278. }
  279. case ISCSI_PARAM:
  280. switch (param) {
  281. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  282. case ISCSI_PARAM_PERSISTENT_PORT:
  283. case ISCSI_PARAM_CONN_ADDRESS:
  284. case ISCSI_PARAM_CONN_PORT:
  285. case ISCSI_PARAM_TARGET_NAME:
  286. case ISCSI_PARAM_TPGT:
  287. case ISCSI_PARAM_TARGET_ALIAS:
  288. case ISCSI_PARAM_MAX_BURST:
  289. case ISCSI_PARAM_MAX_R2T:
  290. case ISCSI_PARAM_FIRST_BURST:
  291. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  292. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  293. case ISCSI_PARAM_IFACE_NAME:
  294. case ISCSI_PARAM_CHAP_OUT_IDX:
  295. case ISCSI_PARAM_CHAP_IN_IDX:
  296. case ISCSI_PARAM_USERNAME:
  297. case ISCSI_PARAM_PASSWORD:
  298. case ISCSI_PARAM_USERNAME_IN:
  299. case ISCSI_PARAM_PASSWORD_IN:
  300. return S_IRUGO;
  301. default:
  302. return 0;
  303. }
  304. case ISCSI_NET_PARAM:
  305. switch (param) {
  306. case ISCSI_NET_PARAM_IPV4_ADDR:
  307. case ISCSI_NET_PARAM_IPV4_SUBNET:
  308. case ISCSI_NET_PARAM_IPV4_GW:
  309. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  310. case ISCSI_NET_PARAM_IFACE_ENABLE:
  311. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  312. case ISCSI_NET_PARAM_IPV6_ADDR:
  313. case ISCSI_NET_PARAM_IPV6_ROUTER:
  314. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  315. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  316. case ISCSI_NET_PARAM_VLAN_ID:
  317. case ISCSI_NET_PARAM_VLAN_PRIORITY:
  318. case ISCSI_NET_PARAM_VLAN_ENABLED:
  319. case ISCSI_NET_PARAM_MTU:
  320. case ISCSI_NET_PARAM_PORT:
  321. return S_IRUGO;
  322. default:
  323. return 0;
  324. }
  325. }
  326. return 0;
  327. }
  328. static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
  329. uint32_t *num_entries, char *buf)
  330. {
  331. struct scsi_qla_host *ha = to_qla_host(shost);
  332. struct ql4_chap_table *chap_table;
  333. struct iscsi_chap_rec *chap_rec;
  334. int max_chap_entries = 0;
  335. int valid_chap_entries = 0;
  336. int ret = 0, i;
  337. if (is_qla8022(ha))
  338. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  339. sizeof(struct ql4_chap_table);
  340. else
  341. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  342. ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
  343. __func__, *num_entries, chap_tbl_idx);
  344. if (!buf) {
  345. ret = -ENOMEM;
  346. goto exit_get_chap_list;
  347. }
  348. chap_rec = (struct iscsi_chap_rec *) buf;
  349. mutex_lock(&ha->chap_sem);
  350. for (i = chap_tbl_idx; i < max_chap_entries; i++) {
  351. chap_table = (struct ql4_chap_table *)ha->chap_list + i;
  352. if (chap_table->cookie !=
  353. __constant_cpu_to_le16(CHAP_VALID_COOKIE))
  354. continue;
  355. chap_rec->chap_tbl_idx = i;
  356. strncpy(chap_rec->username, chap_table->name,
  357. ISCSI_CHAP_AUTH_NAME_MAX_LEN);
  358. strncpy(chap_rec->password, chap_table->secret,
  359. QL4_CHAP_MAX_SECRET_LEN);
  360. chap_rec->password_length = chap_table->secret_len;
  361. if (chap_table->flags & BIT_7) /* local */
  362. chap_rec->chap_type = CHAP_TYPE_OUT;
  363. if (chap_table->flags & BIT_6) /* peer */
  364. chap_rec->chap_type = CHAP_TYPE_IN;
  365. chap_rec++;
  366. valid_chap_entries++;
  367. if (valid_chap_entries == *num_entries)
  368. break;
  369. else
  370. continue;
  371. }
  372. mutex_unlock(&ha->chap_sem);
  373. exit_get_chap_list:
  374. ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
  375. __func__, valid_chap_entries);
  376. *num_entries = valid_chap_entries;
  377. return ret;
  378. }
  379. static int __qla4xxx_is_chap_active(struct device *dev, void *data)
  380. {
  381. int ret = 0;
  382. uint16_t *chap_tbl_idx = (uint16_t *) data;
  383. struct iscsi_cls_session *cls_session;
  384. struct iscsi_session *sess;
  385. struct ddb_entry *ddb_entry;
  386. if (!iscsi_is_session_dev(dev))
  387. goto exit_is_chap_active;
  388. cls_session = iscsi_dev_to_session(dev);
  389. sess = cls_session->dd_data;
  390. ddb_entry = sess->dd_data;
  391. if (iscsi_session_chkready(cls_session))
  392. goto exit_is_chap_active;
  393. if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
  394. ret = 1;
  395. exit_is_chap_active:
  396. return ret;
  397. }
  398. static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
  399. uint16_t chap_tbl_idx)
  400. {
  401. int ret = 0;
  402. ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
  403. __qla4xxx_is_chap_active);
  404. return ret;
  405. }
  406. static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
  407. {
  408. struct scsi_qla_host *ha = to_qla_host(shost);
  409. struct ql4_chap_table *chap_table;
  410. dma_addr_t chap_dma;
  411. int max_chap_entries = 0;
  412. uint32_t offset = 0;
  413. uint32_t chap_size;
  414. int ret = 0;
  415. chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
  416. if (chap_table == NULL)
  417. return -ENOMEM;
  418. memset(chap_table, 0, sizeof(struct ql4_chap_table));
  419. if (is_qla8022(ha))
  420. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  421. sizeof(struct ql4_chap_table);
  422. else
  423. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  424. if (chap_tbl_idx > max_chap_entries) {
  425. ret = -EINVAL;
  426. goto exit_delete_chap;
  427. }
  428. /* Check if chap index is in use.
  429. * If chap is in use don't delet chap entry */
  430. ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
  431. if (ret) {
  432. ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
  433. "delete from flash\n", chap_tbl_idx);
  434. ret = -EBUSY;
  435. goto exit_delete_chap;
  436. }
  437. chap_size = sizeof(struct ql4_chap_table);
  438. if (is_qla40XX(ha))
  439. offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
  440. else {
  441. offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
  442. /* flt_chap_size is CHAP table size for both ports
  443. * so divide it by 2 to calculate the offset for second port
  444. */
  445. if (ha->port_num == 1)
  446. offset += (ha->hw.flt_chap_size / 2);
  447. offset += (chap_tbl_idx * chap_size);
  448. }
  449. ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
  450. if (ret != QLA_SUCCESS) {
  451. ret = -EINVAL;
  452. goto exit_delete_chap;
  453. }
  454. DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
  455. __le16_to_cpu(chap_table->cookie)));
  456. if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
  457. ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
  458. goto exit_delete_chap;
  459. }
  460. chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
  461. offset = FLASH_CHAP_OFFSET |
  462. (chap_tbl_idx * sizeof(struct ql4_chap_table));
  463. ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
  464. FLASH_OPT_RMW_COMMIT);
  465. if (ret == QLA_SUCCESS && ha->chap_list) {
  466. mutex_lock(&ha->chap_sem);
  467. /* Update ha chap_list cache */
  468. memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
  469. chap_table, sizeof(struct ql4_chap_table));
  470. mutex_unlock(&ha->chap_sem);
  471. }
  472. if (ret != QLA_SUCCESS)
  473. ret = -EINVAL;
  474. exit_delete_chap:
  475. dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
  476. return ret;
  477. }
  478. static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  479. enum iscsi_param_type param_type,
  480. int param, char *buf)
  481. {
  482. struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
  483. struct scsi_qla_host *ha = to_qla_host(shost);
  484. int len = -ENOSYS;
  485. if (param_type != ISCSI_NET_PARAM)
  486. return -ENOSYS;
  487. switch (param) {
  488. case ISCSI_NET_PARAM_IPV4_ADDR:
  489. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  490. break;
  491. case ISCSI_NET_PARAM_IPV4_SUBNET:
  492. len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
  493. break;
  494. case ISCSI_NET_PARAM_IPV4_GW:
  495. len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
  496. break;
  497. case ISCSI_NET_PARAM_IFACE_ENABLE:
  498. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  499. len = sprintf(buf, "%s\n",
  500. (ha->ip_config.ipv4_options &
  501. IPOPT_IPV4_PROTOCOL_ENABLE) ?
  502. "enabled" : "disabled");
  503. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  504. len = sprintf(buf, "%s\n",
  505. (ha->ip_config.ipv6_options &
  506. IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
  507. "enabled" : "disabled");
  508. break;
  509. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  510. len = sprintf(buf, "%s\n",
  511. (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
  512. "dhcp" : "static");
  513. break;
  514. case ISCSI_NET_PARAM_IPV6_ADDR:
  515. if (iface->iface_num == 0)
  516. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
  517. if (iface->iface_num == 1)
  518. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
  519. break;
  520. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  521. len = sprintf(buf, "%pI6\n",
  522. &ha->ip_config.ipv6_link_local_addr);
  523. break;
  524. case ISCSI_NET_PARAM_IPV6_ROUTER:
  525. len = sprintf(buf, "%pI6\n",
  526. &ha->ip_config.ipv6_default_router_addr);
  527. break;
  528. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  529. len = sprintf(buf, "%s\n",
  530. (ha->ip_config.ipv6_addl_options &
  531. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
  532. "nd" : "static");
  533. break;
  534. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  535. len = sprintf(buf, "%s\n",
  536. (ha->ip_config.ipv6_addl_options &
  537. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
  538. "auto" : "static");
  539. break;
  540. case ISCSI_NET_PARAM_VLAN_ID:
  541. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  542. len = sprintf(buf, "%d\n",
  543. (ha->ip_config.ipv4_vlan_tag &
  544. ISCSI_MAX_VLAN_ID));
  545. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  546. len = sprintf(buf, "%d\n",
  547. (ha->ip_config.ipv6_vlan_tag &
  548. ISCSI_MAX_VLAN_ID));
  549. break;
  550. case ISCSI_NET_PARAM_VLAN_PRIORITY:
  551. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  552. len = sprintf(buf, "%d\n",
  553. ((ha->ip_config.ipv4_vlan_tag >> 13) &
  554. ISCSI_MAX_VLAN_PRIORITY));
  555. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  556. len = sprintf(buf, "%d\n",
  557. ((ha->ip_config.ipv6_vlan_tag >> 13) &
  558. ISCSI_MAX_VLAN_PRIORITY));
  559. break;
  560. case ISCSI_NET_PARAM_VLAN_ENABLED:
  561. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  562. len = sprintf(buf, "%s\n",
  563. (ha->ip_config.ipv4_options &
  564. IPOPT_VLAN_TAGGING_ENABLE) ?
  565. "enabled" : "disabled");
  566. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  567. len = sprintf(buf, "%s\n",
  568. (ha->ip_config.ipv6_options &
  569. IPV6_OPT_VLAN_TAGGING_ENABLE) ?
  570. "enabled" : "disabled");
  571. break;
  572. case ISCSI_NET_PARAM_MTU:
  573. len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
  574. break;
  575. case ISCSI_NET_PARAM_PORT:
  576. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  577. len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
  578. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  579. len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
  580. break;
  581. default:
  582. len = -ENOSYS;
  583. }
  584. return len;
  585. }
  586. static struct iscsi_endpoint *
  587. qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  588. int non_blocking)
  589. {
  590. int ret;
  591. struct iscsi_endpoint *ep;
  592. struct qla_endpoint *qla_ep;
  593. struct scsi_qla_host *ha;
  594. struct sockaddr_in *addr;
  595. struct sockaddr_in6 *addr6;
  596. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  597. if (!shost) {
  598. ret = -ENXIO;
  599. printk(KERN_ERR "%s: shost is NULL\n",
  600. __func__);
  601. return ERR_PTR(ret);
  602. }
  603. ha = iscsi_host_priv(shost);
  604. ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
  605. if (!ep) {
  606. ret = -ENOMEM;
  607. return ERR_PTR(ret);
  608. }
  609. qla_ep = ep->dd_data;
  610. memset(qla_ep, 0, sizeof(struct qla_endpoint));
  611. if (dst_addr->sa_family == AF_INET) {
  612. memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
  613. addr = (struct sockaddr_in *)&qla_ep->dst_addr;
  614. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
  615. (char *)&addr->sin_addr));
  616. } else if (dst_addr->sa_family == AF_INET6) {
  617. memcpy(&qla_ep->dst_addr, dst_addr,
  618. sizeof(struct sockaddr_in6));
  619. addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
  620. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
  621. (char *)&addr6->sin6_addr));
  622. }
  623. qla_ep->host = shost;
  624. return ep;
  625. }
  626. static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  627. {
  628. struct qla_endpoint *qla_ep;
  629. struct scsi_qla_host *ha;
  630. int ret = 0;
  631. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  632. qla_ep = ep->dd_data;
  633. ha = to_qla_host(qla_ep->host);
  634. if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
  635. ret = 1;
  636. return ret;
  637. }
  638. static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
  639. {
  640. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  641. iscsi_destroy_endpoint(ep);
  642. }
  643. static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
  644. enum iscsi_param param,
  645. char *buf)
  646. {
  647. struct qla_endpoint *qla_ep = ep->dd_data;
  648. struct sockaddr *dst_addr;
  649. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  650. switch (param) {
  651. case ISCSI_PARAM_CONN_PORT:
  652. case ISCSI_PARAM_CONN_ADDRESS:
  653. if (!qla_ep)
  654. return -ENOTCONN;
  655. dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  656. if (!dst_addr)
  657. return -ENOTCONN;
  658. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  659. &qla_ep->dst_addr, param, buf);
  660. default:
  661. return -ENOSYS;
  662. }
  663. }
  664. static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  665. struct iscsi_stats *stats)
  666. {
  667. struct iscsi_session *sess;
  668. struct iscsi_cls_session *cls_sess;
  669. struct ddb_entry *ddb_entry;
  670. struct scsi_qla_host *ha;
  671. struct ql_iscsi_stats *ql_iscsi_stats;
  672. int stats_size;
  673. int ret;
  674. dma_addr_t iscsi_stats_dma;
  675. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  676. cls_sess = iscsi_conn_to_session(cls_conn);
  677. sess = cls_sess->dd_data;
  678. ddb_entry = sess->dd_data;
  679. ha = ddb_entry->ha;
  680. stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
  681. /* Allocate memory */
  682. ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
  683. &iscsi_stats_dma, GFP_KERNEL);
  684. if (!ql_iscsi_stats) {
  685. ql4_printk(KERN_ERR, ha,
  686. "Unable to allocate memory for iscsi stats\n");
  687. goto exit_get_stats;
  688. }
  689. ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
  690. iscsi_stats_dma);
  691. if (ret != QLA_SUCCESS) {
  692. ql4_printk(KERN_ERR, ha,
  693. "Unable to retreive iscsi stats\n");
  694. goto free_stats;
  695. }
  696. /* octets */
  697. stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
  698. stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
  699. /* xmit pdus */
  700. stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
  701. stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
  702. stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
  703. stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
  704. stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
  705. stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
  706. stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
  707. stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
  708. /* recv pdus */
  709. stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
  710. stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
  711. stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
  712. stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
  713. stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
  714. stats->logoutrsp_pdus =
  715. le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
  716. stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
  717. stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
  718. stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
  719. free_stats:
  720. dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
  721. iscsi_stats_dma);
  722. exit_get_stats:
  723. return;
  724. }
  725. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  726. {
  727. struct iscsi_cls_session *session;
  728. struct iscsi_session *sess;
  729. unsigned long flags;
  730. enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
  731. session = starget_to_session(scsi_target(sc->device));
  732. sess = session->dd_data;
  733. spin_lock_irqsave(&session->lock, flags);
  734. if (session->state == ISCSI_SESSION_FAILED)
  735. ret = BLK_EH_RESET_TIMER;
  736. spin_unlock_irqrestore(&session->lock, flags);
  737. return ret;
  738. }
  739. static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
  740. {
  741. struct scsi_qla_host *ha = to_qla_host(shost);
  742. struct iscsi_cls_host *ihost = shost->shost_data;
  743. uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
  744. qla4xxx_get_firmware_state(ha);
  745. switch (ha->addl_fw_state & 0x0F00) {
  746. case FW_ADDSTATE_LINK_SPEED_10MBPS:
  747. speed = ISCSI_PORT_SPEED_10MBPS;
  748. break;
  749. case FW_ADDSTATE_LINK_SPEED_100MBPS:
  750. speed = ISCSI_PORT_SPEED_100MBPS;
  751. break;
  752. case FW_ADDSTATE_LINK_SPEED_1GBPS:
  753. speed = ISCSI_PORT_SPEED_1GBPS;
  754. break;
  755. case FW_ADDSTATE_LINK_SPEED_10GBPS:
  756. speed = ISCSI_PORT_SPEED_10GBPS;
  757. break;
  758. }
  759. ihost->port_speed = speed;
  760. }
  761. static void qla4xxx_set_port_state(struct Scsi_Host *shost)
  762. {
  763. struct scsi_qla_host *ha = to_qla_host(shost);
  764. struct iscsi_cls_host *ihost = shost->shost_data;
  765. uint32_t state = ISCSI_PORT_STATE_DOWN;
  766. if (test_bit(AF_LINK_UP, &ha->flags))
  767. state = ISCSI_PORT_STATE_UP;
  768. ihost->port_state = state;
  769. }
  770. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  771. enum iscsi_host_param param, char *buf)
  772. {
  773. struct scsi_qla_host *ha = to_qla_host(shost);
  774. int len;
  775. switch (param) {
  776. case ISCSI_HOST_PARAM_HWADDRESS:
  777. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  778. break;
  779. case ISCSI_HOST_PARAM_IPADDRESS:
  780. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  781. break;
  782. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  783. len = sprintf(buf, "%s\n", ha->name_string);
  784. break;
  785. case ISCSI_HOST_PARAM_PORT_STATE:
  786. qla4xxx_set_port_state(shost);
  787. len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
  788. break;
  789. case ISCSI_HOST_PARAM_PORT_SPEED:
  790. qla4xxx_set_port_speed(shost);
  791. len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
  792. break;
  793. default:
  794. return -ENOSYS;
  795. }
  796. return len;
  797. }
  798. static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
  799. {
  800. if (ha->iface_ipv4)
  801. return;
  802. /* IPv4 */
  803. ha->iface_ipv4 = iscsi_create_iface(ha->host,
  804. &qla4xxx_iscsi_transport,
  805. ISCSI_IFACE_TYPE_IPV4, 0, 0);
  806. if (!ha->iface_ipv4)
  807. ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
  808. "iface0.\n");
  809. }
  810. static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
  811. {
  812. if (!ha->iface_ipv6_0)
  813. /* IPv6 iface-0 */
  814. ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
  815. &qla4xxx_iscsi_transport,
  816. ISCSI_IFACE_TYPE_IPV6, 0,
  817. 0);
  818. if (!ha->iface_ipv6_0)
  819. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  820. "iface0.\n");
  821. if (!ha->iface_ipv6_1)
  822. /* IPv6 iface-1 */
  823. ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
  824. &qla4xxx_iscsi_transport,
  825. ISCSI_IFACE_TYPE_IPV6, 1,
  826. 0);
  827. if (!ha->iface_ipv6_1)
  828. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  829. "iface1.\n");
  830. }
  831. static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
  832. {
  833. if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
  834. qla4xxx_create_ipv4_iface(ha);
  835. if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
  836. qla4xxx_create_ipv6_iface(ha);
  837. }
  838. static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
  839. {
  840. if (ha->iface_ipv4) {
  841. iscsi_destroy_iface(ha->iface_ipv4);
  842. ha->iface_ipv4 = NULL;
  843. }
  844. }
  845. static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
  846. {
  847. if (ha->iface_ipv6_0) {
  848. iscsi_destroy_iface(ha->iface_ipv6_0);
  849. ha->iface_ipv6_0 = NULL;
  850. }
  851. if (ha->iface_ipv6_1) {
  852. iscsi_destroy_iface(ha->iface_ipv6_1);
  853. ha->iface_ipv6_1 = NULL;
  854. }
  855. }
  856. static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
  857. {
  858. qla4xxx_destroy_ipv4_iface(ha);
  859. qla4xxx_destroy_ipv6_iface(ha);
  860. }
  861. static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
  862. struct iscsi_iface_param_info *iface_param,
  863. struct addr_ctrl_blk *init_fw_cb)
  864. {
  865. /*
  866. * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
  867. * iface_num 1 is valid only for IPv6 Addr.
  868. */
  869. switch (iface_param->param) {
  870. case ISCSI_NET_PARAM_IPV6_ADDR:
  871. if (iface_param->iface_num & 0x1)
  872. /* IPv6 Addr 1 */
  873. memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
  874. sizeof(init_fw_cb->ipv6_addr1));
  875. else
  876. /* IPv6 Addr 0 */
  877. memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
  878. sizeof(init_fw_cb->ipv6_addr0));
  879. break;
  880. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  881. if (iface_param->iface_num & 0x1)
  882. break;
  883. memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
  884. sizeof(init_fw_cb->ipv6_if_id));
  885. break;
  886. case ISCSI_NET_PARAM_IPV6_ROUTER:
  887. if (iface_param->iface_num & 0x1)
  888. break;
  889. memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
  890. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  891. break;
  892. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  893. /* Autocfg applies to even interface */
  894. if (iface_param->iface_num & 0x1)
  895. break;
  896. if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
  897. init_fw_cb->ipv6_addtl_opts &=
  898. cpu_to_le16(
  899. ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  900. else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
  901. init_fw_cb->ipv6_addtl_opts |=
  902. cpu_to_le16(
  903. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  904. else
  905. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  906. "IPv6 addr\n");
  907. break;
  908. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  909. /* Autocfg applies to even interface */
  910. if (iface_param->iface_num & 0x1)
  911. break;
  912. if (iface_param->value[0] ==
  913. ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
  914. init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
  915. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  916. else if (iface_param->value[0] ==
  917. ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
  918. init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
  919. ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  920. else
  921. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  922. "IPv6 linklocal addr\n");
  923. break;
  924. case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
  925. /* Autocfg applies to even interface */
  926. if (iface_param->iface_num & 0x1)
  927. break;
  928. if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
  929. memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
  930. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  931. break;
  932. case ISCSI_NET_PARAM_IFACE_ENABLE:
  933. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  934. init_fw_cb->ipv6_opts |=
  935. cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
  936. qla4xxx_create_ipv6_iface(ha);
  937. } else {
  938. init_fw_cb->ipv6_opts &=
  939. cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
  940. 0xFFFF);
  941. qla4xxx_destroy_ipv6_iface(ha);
  942. }
  943. break;
  944. case ISCSI_NET_PARAM_VLAN_TAG:
  945. if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
  946. break;
  947. init_fw_cb->ipv6_vlan_tag =
  948. cpu_to_be16(*(uint16_t *)iface_param->value);
  949. break;
  950. case ISCSI_NET_PARAM_VLAN_ENABLED:
  951. if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  952. init_fw_cb->ipv6_opts |=
  953. cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
  954. else
  955. init_fw_cb->ipv6_opts &=
  956. cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
  957. break;
  958. case ISCSI_NET_PARAM_MTU:
  959. init_fw_cb->eth_mtu_size =
  960. cpu_to_le16(*(uint16_t *)iface_param->value);
  961. break;
  962. case ISCSI_NET_PARAM_PORT:
  963. /* Autocfg applies to even interface */
  964. if (iface_param->iface_num & 0x1)
  965. break;
  966. init_fw_cb->ipv6_port =
  967. cpu_to_le16(*(uint16_t *)iface_param->value);
  968. break;
  969. default:
  970. ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
  971. iface_param->param);
  972. break;
  973. }
  974. }
  975. static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
  976. struct iscsi_iface_param_info *iface_param,
  977. struct addr_ctrl_blk *init_fw_cb)
  978. {
  979. switch (iface_param->param) {
  980. case ISCSI_NET_PARAM_IPV4_ADDR:
  981. memcpy(init_fw_cb->ipv4_addr, iface_param->value,
  982. sizeof(init_fw_cb->ipv4_addr));
  983. break;
  984. case ISCSI_NET_PARAM_IPV4_SUBNET:
  985. memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
  986. sizeof(init_fw_cb->ipv4_subnet));
  987. break;
  988. case ISCSI_NET_PARAM_IPV4_GW:
  989. memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
  990. sizeof(init_fw_cb->ipv4_gw_addr));
  991. break;
  992. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  993. if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
  994. init_fw_cb->ipv4_tcp_opts |=
  995. cpu_to_le16(TCPOPT_DHCP_ENABLE);
  996. else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
  997. init_fw_cb->ipv4_tcp_opts &=
  998. cpu_to_le16(~TCPOPT_DHCP_ENABLE);
  999. else
  1000. ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
  1001. break;
  1002. case ISCSI_NET_PARAM_IFACE_ENABLE:
  1003. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  1004. init_fw_cb->ipv4_ip_opts |=
  1005. cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
  1006. qla4xxx_create_ipv4_iface(ha);
  1007. } else {
  1008. init_fw_cb->ipv4_ip_opts &=
  1009. cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
  1010. 0xFFFF);
  1011. qla4xxx_destroy_ipv4_iface(ha);
  1012. }
  1013. break;
  1014. case ISCSI_NET_PARAM_VLAN_TAG:
  1015. if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
  1016. break;
  1017. init_fw_cb->ipv4_vlan_tag =
  1018. cpu_to_be16(*(uint16_t *)iface_param->value);
  1019. break;
  1020. case ISCSI_NET_PARAM_VLAN_ENABLED:
  1021. if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  1022. init_fw_cb->ipv4_ip_opts |=
  1023. cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
  1024. else
  1025. init_fw_cb->ipv4_ip_opts &=
  1026. cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
  1027. break;
  1028. case ISCSI_NET_PARAM_MTU:
  1029. init_fw_cb->eth_mtu_size =
  1030. cpu_to_le16(*(uint16_t *)iface_param->value);
  1031. break;
  1032. case ISCSI_NET_PARAM_PORT:
  1033. init_fw_cb->ipv4_port =
  1034. cpu_to_le16(*(uint16_t *)iface_param->value);
  1035. break;
  1036. default:
  1037. ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
  1038. iface_param->param);
  1039. break;
  1040. }
  1041. }
  1042. static void
  1043. qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
  1044. {
  1045. struct addr_ctrl_blk_def *acb;
  1046. acb = (struct addr_ctrl_blk_def *)init_fw_cb;
  1047. memset(acb->reserved1, 0, sizeof(acb->reserved1));
  1048. memset(acb->reserved2, 0, sizeof(acb->reserved2));
  1049. memset(acb->reserved3, 0, sizeof(acb->reserved3));
  1050. memset(acb->reserved4, 0, sizeof(acb->reserved4));
  1051. memset(acb->reserved5, 0, sizeof(acb->reserved5));
  1052. memset(acb->reserved6, 0, sizeof(acb->reserved6));
  1053. memset(acb->reserved7, 0, sizeof(acb->reserved7));
  1054. memset(acb->reserved8, 0, sizeof(acb->reserved8));
  1055. memset(acb->reserved9, 0, sizeof(acb->reserved9));
  1056. memset(acb->reserved10, 0, sizeof(acb->reserved10));
  1057. memset(acb->reserved11, 0, sizeof(acb->reserved11));
  1058. memset(acb->reserved12, 0, sizeof(acb->reserved12));
  1059. memset(acb->reserved13, 0, sizeof(acb->reserved13));
  1060. memset(acb->reserved14, 0, sizeof(acb->reserved14));
  1061. memset(acb->reserved15, 0, sizeof(acb->reserved15));
  1062. }
  1063. static int
  1064. qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
  1065. {
  1066. struct scsi_qla_host *ha = to_qla_host(shost);
  1067. int rval = 0;
  1068. struct iscsi_iface_param_info *iface_param = NULL;
  1069. struct addr_ctrl_blk *init_fw_cb = NULL;
  1070. dma_addr_t init_fw_cb_dma;
  1071. uint32_t mbox_cmd[MBOX_REG_COUNT];
  1072. uint32_t mbox_sts[MBOX_REG_COUNT];
  1073. uint32_t rem = len;
  1074. struct nlattr *attr;
  1075. init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
  1076. sizeof(struct addr_ctrl_blk),
  1077. &init_fw_cb_dma, GFP_KERNEL);
  1078. if (!init_fw_cb) {
  1079. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
  1080. __func__);
  1081. return -ENOMEM;
  1082. }
  1083. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  1084. memset(&mbox_cmd, 0, sizeof(mbox_cmd));
  1085. memset(&mbox_sts, 0, sizeof(mbox_sts));
  1086. if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
  1087. ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
  1088. rval = -EIO;
  1089. goto exit_init_fw_cb;
  1090. }
  1091. nla_for_each_attr(attr, data, len, rem) {
  1092. iface_param = nla_data(attr);
  1093. if (iface_param->param_type != ISCSI_NET_PARAM)
  1094. continue;
  1095. switch (iface_param->iface_type) {
  1096. case ISCSI_IFACE_TYPE_IPV4:
  1097. switch (iface_param->iface_num) {
  1098. case 0:
  1099. qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
  1100. break;
  1101. default:
  1102. /* Cannot have more than one IPv4 interface */
  1103. ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
  1104. "number = %d\n",
  1105. iface_param->iface_num);
  1106. break;
  1107. }
  1108. break;
  1109. case ISCSI_IFACE_TYPE_IPV6:
  1110. switch (iface_param->iface_num) {
  1111. case 0:
  1112. case 1:
  1113. qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
  1114. break;
  1115. default:
  1116. /* Cannot have more than two IPv6 interface */
  1117. ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
  1118. "number = %d\n",
  1119. iface_param->iface_num);
  1120. break;
  1121. }
  1122. break;
  1123. default:
  1124. ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
  1125. break;
  1126. }
  1127. }
  1128. init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
  1129. rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
  1130. sizeof(struct addr_ctrl_blk),
  1131. FLASH_OPT_RMW_COMMIT);
  1132. if (rval != QLA_SUCCESS) {
  1133. ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
  1134. __func__);
  1135. rval = -EIO;
  1136. goto exit_init_fw_cb;
  1137. }
  1138. rval = qla4xxx_disable_acb(ha);
  1139. if (rval != QLA_SUCCESS) {
  1140. ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
  1141. __func__);
  1142. rval = -EIO;
  1143. goto exit_init_fw_cb;
  1144. }
  1145. wait_for_completion_timeout(&ha->disable_acb_comp,
  1146. DISABLE_ACB_TOV * HZ);
  1147. qla4xxx_initcb_to_acb(init_fw_cb);
  1148. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
  1149. if (rval != QLA_SUCCESS) {
  1150. ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
  1151. __func__);
  1152. rval = -EIO;
  1153. goto exit_init_fw_cb;
  1154. }
  1155. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  1156. qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
  1157. init_fw_cb_dma);
  1158. exit_init_fw_cb:
  1159. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
  1160. init_fw_cb, init_fw_cb_dma);
  1161. return rval;
  1162. }
  1163. static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
  1164. enum iscsi_param param, char *buf)
  1165. {
  1166. struct iscsi_session *sess = cls_sess->dd_data;
  1167. struct ddb_entry *ddb_entry = sess->dd_data;
  1168. struct scsi_qla_host *ha = ddb_entry->ha;
  1169. int rval, len;
  1170. uint16_t idx;
  1171. switch (param) {
  1172. case ISCSI_PARAM_CHAP_IN_IDX:
  1173. rval = qla4xxx_get_chap_index(ha, sess->username_in,
  1174. sess->password_in, BIDI_CHAP,
  1175. &idx);
  1176. if (rval)
  1177. return -EINVAL;
  1178. len = sprintf(buf, "%hu\n", idx);
  1179. break;
  1180. case ISCSI_PARAM_CHAP_OUT_IDX:
  1181. rval = qla4xxx_get_chap_index(ha, sess->username,
  1182. sess->password, LOCAL_CHAP,
  1183. &idx);
  1184. if (rval)
  1185. return -EINVAL;
  1186. len = sprintf(buf, "%hu\n", idx);
  1187. break;
  1188. default:
  1189. return iscsi_session_get_param(cls_sess, param, buf);
  1190. }
  1191. return len;
  1192. }
  1193. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1194. enum iscsi_param param, char *buf)
  1195. {
  1196. struct iscsi_conn *conn;
  1197. struct qla_conn *qla_conn;
  1198. struct sockaddr *dst_addr;
  1199. int len = 0;
  1200. conn = cls_conn->dd_data;
  1201. qla_conn = conn->dd_data;
  1202. dst_addr = &qla_conn->qla_ep->dst_addr;
  1203. switch (param) {
  1204. case ISCSI_PARAM_CONN_PORT:
  1205. case ISCSI_PARAM_CONN_ADDRESS:
  1206. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  1207. dst_addr, param, buf);
  1208. default:
  1209. return iscsi_conn_get_param(cls_conn, param, buf);
  1210. }
  1211. return len;
  1212. }
  1213. int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
  1214. {
  1215. uint32_t mbx_sts = 0;
  1216. uint16_t tmp_ddb_index;
  1217. int ret;
  1218. get_ddb_index:
  1219. tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
  1220. if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
  1221. DEBUG2(ql4_printk(KERN_INFO, ha,
  1222. "Free DDB index not available\n"));
  1223. ret = QLA_ERROR;
  1224. goto exit_get_ddb_index;
  1225. }
  1226. if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
  1227. goto get_ddb_index;
  1228. DEBUG2(ql4_printk(KERN_INFO, ha,
  1229. "Found a free DDB index at %d\n", tmp_ddb_index));
  1230. ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
  1231. if (ret == QLA_ERROR) {
  1232. if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
  1233. ql4_printk(KERN_INFO, ha,
  1234. "DDB index = %d not available trying next\n",
  1235. tmp_ddb_index);
  1236. goto get_ddb_index;
  1237. }
  1238. DEBUG2(ql4_printk(KERN_INFO, ha,
  1239. "Free FW DDB not available\n"));
  1240. }
  1241. *ddb_index = tmp_ddb_index;
  1242. exit_get_ddb_index:
  1243. return ret;
  1244. }
  1245. static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
  1246. struct ddb_entry *ddb_entry,
  1247. char *existing_ipaddr,
  1248. char *user_ipaddr)
  1249. {
  1250. uint8_t dst_ipaddr[IPv6_ADDR_LEN];
  1251. char formatted_ipaddr[DDB_IPADDR_LEN];
  1252. int status = QLA_SUCCESS, ret = 0;
  1253. if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
  1254. ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
  1255. '\0', NULL);
  1256. if (ret == 0) {
  1257. status = QLA_ERROR;
  1258. goto out_match;
  1259. }
  1260. ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
  1261. } else {
  1262. ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
  1263. '\0', NULL);
  1264. if (ret == 0) {
  1265. status = QLA_ERROR;
  1266. goto out_match;
  1267. }
  1268. ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
  1269. }
  1270. if (strcmp(existing_ipaddr, formatted_ipaddr))
  1271. status = QLA_ERROR;
  1272. out_match:
  1273. return status;
  1274. }
  1275. static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
  1276. struct iscsi_cls_conn *cls_conn)
  1277. {
  1278. int idx = 0, max_ddbs, rval;
  1279. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1280. struct iscsi_session *sess, *existing_sess;
  1281. struct iscsi_conn *conn, *existing_conn;
  1282. struct ddb_entry *ddb_entry;
  1283. sess = cls_sess->dd_data;
  1284. conn = cls_conn->dd_data;
  1285. if (sess->targetname == NULL ||
  1286. conn->persistent_address == NULL ||
  1287. conn->persistent_port == 0)
  1288. return QLA_ERROR;
  1289. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  1290. MAX_DEV_DB_ENTRIES;
  1291. for (idx = 0; idx < max_ddbs; idx++) {
  1292. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  1293. if (ddb_entry == NULL)
  1294. continue;
  1295. if (ddb_entry->ddb_type != FLASH_DDB)
  1296. continue;
  1297. existing_sess = ddb_entry->sess->dd_data;
  1298. existing_conn = ddb_entry->conn->dd_data;
  1299. if (existing_sess->targetname == NULL ||
  1300. existing_conn->persistent_address == NULL ||
  1301. existing_conn->persistent_port == 0)
  1302. continue;
  1303. DEBUG2(ql4_printk(KERN_INFO, ha,
  1304. "IQN = %s User IQN = %s\n",
  1305. existing_sess->targetname,
  1306. sess->targetname));
  1307. DEBUG2(ql4_printk(KERN_INFO, ha,
  1308. "IP = %s User IP = %s\n",
  1309. existing_conn->persistent_address,
  1310. conn->persistent_address));
  1311. DEBUG2(ql4_printk(KERN_INFO, ha,
  1312. "Port = %d User Port = %d\n",
  1313. existing_conn->persistent_port,
  1314. conn->persistent_port));
  1315. if (strcmp(existing_sess->targetname, sess->targetname))
  1316. continue;
  1317. rval = qla4xxx_match_ipaddress(ha, ddb_entry,
  1318. existing_conn->persistent_address,
  1319. conn->persistent_address);
  1320. if (rval == QLA_ERROR)
  1321. continue;
  1322. if (existing_conn->persistent_port != conn->persistent_port)
  1323. continue;
  1324. break;
  1325. }
  1326. if (idx == max_ddbs)
  1327. return QLA_ERROR;
  1328. DEBUG2(ql4_printk(KERN_INFO, ha,
  1329. "Match found in fwdb sessions\n"));
  1330. return QLA_SUCCESS;
  1331. }
  1332. static struct iscsi_cls_session *
  1333. qla4xxx_session_create(struct iscsi_endpoint *ep,
  1334. uint16_t cmds_max, uint16_t qdepth,
  1335. uint32_t initial_cmdsn)
  1336. {
  1337. struct iscsi_cls_session *cls_sess;
  1338. struct scsi_qla_host *ha;
  1339. struct qla_endpoint *qla_ep;
  1340. struct ddb_entry *ddb_entry;
  1341. uint16_t ddb_index;
  1342. struct iscsi_session *sess;
  1343. struct sockaddr *dst_addr;
  1344. int ret;
  1345. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1346. if (!ep) {
  1347. printk(KERN_ERR "qla4xxx: missing ep.\n");
  1348. return NULL;
  1349. }
  1350. qla_ep = ep->dd_data;
  1351. dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  1352. ha = to_qla_host(qla_ep->host);
  1353. ret = qla4xxx_get_ddb_index(ha, &ddb_index);
  1354. if (ret == QLA_ERROR)
  1355. return NULL;
  1356. cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
  1357. cmds_max, sizeof(struct ddb_entry),
  1358. sizeof(struct ql4_task_data),
  1359. initial_cmdsn, ddb_index);
  1360. if (!cls_sess)
  1361. return NULL;
  1362. sess = cls_sess->dd_data;
  1363. ddb_entry = sess->dd_data;
  1364. ddb_entry->fw_ddb_index = ddb_index;
  1365. ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  1366. ddb_entry->ha = ha;
  1367. ddb_entry->sess = cls_sess;
  1368. ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
  1369. ddb_entry->ddb_change = qla4xxx_ddb_change;
  1370. cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  1371. ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
  1372. ha->tot_ddbs++;
  1373. return cls_sess;
  1374. }
  1375. static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
  1376. {
  1377. struct iscsi_session *sess;
  1378. struct ddb_entry *ddb_entry;
  1379. struct scsi_qla_host *ha;
  1380. unsigned long flags, wtime;
  1381. struct dev_db_entry *fw_ddb_entry = NULL;
  1382. dma_addr_t fw_ddb_entry_dma;
  1383. uint32_t ddb_state;
  1384. int ret;
  1385. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1386. sess = cls_sess->dd_data;
  1387. ddb_entry = sess->dd_data;
  1388. ha = ddb_entry->ha;
  1389. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1390. &fw_ddb_entry_dma, GFP_KERNEL);
  1391. if (!fw_ddb_entry) {
  1392. ql4_printk(KERN_ERR, ha,
  1393. "%s: Unable to allocate dma buffer\n", __func__);
  1394. goto destroy_session;
  1395. }
  1396. wtime = jiffies + (HZ * LOGOUT_TOV);
  1397. do {
  1398. ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
  1399. fw_ddb_entry, fw_ddb_entry_dma,
  1400. NULL, NULL, &ddb_state, NULL,
  1401. NULL, NULL);
  1402. if (ret == QLA_ERROR)
  1403. goto destroy_session;
  1404. if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
  1405. (ddb_state == DDB_DS_SESSION_FAILED))
  1406. goto destroy_session;
  1407. schedule_timeout_uninterruptible(HZ);
  1408. } while ((time_after(wtime, jiffies)));
  1409. destroy_session:
  1410. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  1411. spin_lock_irqsave(&ha->hardware_lock, flags);
  1412. qla4xxx_free_ddb(ha, ddb_entry);
  1413. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1414. iscsi_session_teardown(cls_sess);
  1415. if (fw_ddb_entry)
  1416. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1417. fw_ddb_entry, fw_ddb_entry_dma);
  1418. }
  1419. static struct iscsi_cls_conn *
  1420. qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
  1421. {
  1422. struct iscsi_cls_conn *cls_conn;
  1423. struct iscsi_session *sess;
  1424. struct ddb_entry *ddb_entry;
  1425. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1426. cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
  1427. conn_idx);
  1428. if (!cls_conn)
  1429. return NULL;
  1430. sess = cls_sess->dd_data;
  1431. ddb_entry = sess->dd_data;
  1432. ddb_entry->conn = cls_conn;
  1433. return cls_conn;
  1434. }
  1435. static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  1436. struct iscsi_cls_conn *cls_conn,
  1437. uint64_t transport_fd, int is_leading)
  1438. {
  1439. struct iscsi_conn *conn;
  1440. struct qla_conn *qla_conn;
  1441. struct iscsi_endpoint *ep;
  1442. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1443. if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  1444. return -EINVAL;
  1445. ep = iscsi_lookup_endpoint(transport_fd);
  1446. conn = cls_conn->dd_data;
  1447. qla_conn = conn->dd_data;
  1448. qla_conn->qla_ep = ep->dd_data;
  1449. return 0;
  1450. }
  1451. static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
  1452. {
  1453. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1454. struct iscsi_session *sess;
  1455. struct ddb_entry *ddb_entry;
  1456. struct scsi_qla_host *ha;
  1457. struct dev_db_entry *fw_ddb_entry = NULL;
  1458. dma_addr_t fw_ddb_entry_dma;
  1459. uint32_t mbx_sts = 0;
  1460. int ret = 0;
  1461. int status = QLA_SUCCESS;
  1462. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1463. sess = cls_sess->dd_data;
  1464. ddb_entry = sess->dd_data;
  1465. ha = ddb_entry->ha;
  1466. /* Check if we have matching FW DDB, if yes then do not
  1467. * login to this target. This could cause target to logout previous
  1468. * connection
  1469. */
  1470. ret = qla4xxx_match_fwdb_session(ha, cls_conn);
  1471. if (ret == QLA_SUCCESS) {
  1472. ql4_printk(KERN_INFO, ha,
  1473. "Session already exist in FW.\n");
  1474. ret = -EEXIST;
  1475. goto exit_conn_start;
  1476. }
  1477. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1478. &fw_ddb_entry_dma, GFP_KERNEL);
  1479. if (!fw_ddb_entry) {
  1480. ql4_printk(KERN_ERR, ha,
  1481. "%s: Unable to allocate dma buffer\n", __func__);
  1482. ret = -ENOMEM;
  1483. goto exit_conn_start;
  1484. }
  1485. ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
  1486. if (ret) {
  1487. /* If iscsid is stopped and started then no need to do
  1488. * set param again since ddb state will be already
  1489. * active and FW does not allow set ddb to an
  1490. * active session.
  1491. */
  1492. if (mbx_sts)
  1493. if (ddb_entry->fw_ddb_device_state ==
  1494. DDB_DS_SESSION_ACTIVE) {
  1495. ddb_entry->unblock_sess(ddb_entry->sess);
  1496. goto exit_set_param;
  1497. }
  1498. ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
  1499. __func__, ddb_entry->fw_ddb_index);
  1500. goto exit_conn_start;
  1501. }
  1502. status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
  1503. if (status == QLA_ERROR) {
  1504. ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
  1505. sess->targetname);
  1506. ret = -EINVAL;
  1507. goto exit_conn_start;
  1508. }
  1509. if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
  1510. ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
  1511. DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
  1512. ddb_entry->fw_ddb_device_state));
  1513. exit_set_param:
  1514. ret = 0;
  1515. exit_conn_start:
  1516. if (fw_ddb_entry)
  1517. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1518. fw_ddb_entry, fw_ddb_entry_dma);
  1519. return ret;
  1520. }
  1521. static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1522. {
  1523. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1524. struct iscsi_session *sess;
  1525. struct scsi_qla_host *ha;
  1526. struct ddb_entry *ddb_entry;
  1527. int options;
  1528. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1529. sess = cls_sess->dd_data;
  1530. ddb_entry = sess->dd_data;
  1531. ha = ddb_entry->ha;
  1532. options = LOGOUT_OPTION_CLOSE_SESSION;
  1533. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
  1534. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
  1535. }
  1536. static void qla4xxx_task_work(struct work_struct *wdata)
  1537. {
  1538. struct ql4_task_data *task_data;
  1539. struct scsi_qla_host *ha;
  1540. struct passthru_status *sts;
  1541. struct iscsi_task *task;
  1542. struct iscsi_hdr *hdr;
  1543. uint8_t *data;
  1544. uint32_t data_len;
  1545. struct iscsi_conn *conn;
  1546. int hdr_len;
  1547. itt_t itt;
  1548. task_data = container_of(wdata, struct ql4_task_data, task_work);
  1549. ha = task_data->ha;
  1550. task = task_data->task;
  1551. sts = &task_data->sts;
  1552. hdr_len = sizeof(struct iscsi_hdr);
  1553. DEBUG3(printk(KERN_INFO "Status returned\n"));
  1554. DEBUG3(qla4xxx_dump_buffer(sts, 64));
  1555. DEBUG3(printk(KERN_INFO "Response buffer"));
  1556. DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
  1557. conn = task->conn;
  1558. switch (sts->completionStatus) {
  1559. case PASSTHRU_STATUS_COMPLETE:
  1560. hdr = (struct iscsi_hdr *)task_data->resp_buffer;
  1561. /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
  1562. itt = sts->handle;
  1563. hdr->itt = itt;
  1564. data = task_data->resp_buffer + hdr_len;
  1565. data_len = task_data->resp_len - hdr_len;
  1566. iscsi_complete_pdu(conn, hdr, data, data_len);
  1567. break;
  1568. default:
  1569. ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
  1570. sts->completionStatus);
  1571. break;
  1572. }
  1573. return;
  1574. }
  1575. static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  1576. {
  1577. struct ql4_task_data *task_data;
  1578. struct iscsi_session *sess;
  1579. struct ddb_entry *ddb_entry;
  1580. struct scsi_qla_host *ha;
  1581. int hdr_len;
  1582. sess = task->conn->session;
  1583. ddb_entry = sess->dd_data;
  1584. ha = ddb_entry->ha;
  1585. task_data = task->dd_data;
  1586. memset(task_data, 0, sizeof(struct ql4_task_data));
  1587. if (task->sc) {
  1588. ql4_printk(KERN_INFO, ha,
  1589. "%s: SCSI Commands not implemented\n", __func__);
  1590. return -EINVAL;
  1591. }
  1592. hdr_len = sizeof(struct iscsi_hdr);
  1593. task_data->ha = ha;
  1594. task_data->task = task;
  1595. if (task->data_count) {
  1596. task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
  1597. task->data_count,
  1598. PCI_DMA_TODEVICE);
  1599. }
  1600. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1601. __func__, task->conn->max_recv_dlength, hdr_len));
  1602. task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
  1603. task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1604. task_data->resp_len,
  1605. &task_data->resp_dma,
  1606. GFP_ATOMIC);
  1607. if (!task_data->resp_buffer)
  1608. goto exit_alloc_pdu;
  1609. task_data->req_len = task->data_count + hdr_len;
  1610. task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1611. task_data->req_len,
  1612. &task_data->req_dma,
  1613. GFP_ATOMIC);
  1614. if (!task_data->req_buffer)
  1615. goto exit_alloc_pdu;
  1616. task->hdr = task_data->req_buffer;
  1617. INIT_WORK(&task_data->task_work, qla4xxx_task_work);
  1618. return 0;
  1619. exit_alloc_pdu:
  1620. if (task_data->resp_buffer)
  1621. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1622. task_data->resp_buffer, task_data->resp_dma);
  1623. if (task_data->req_buffer)
  1624. dma_free_coherent(&ha->pdev->dev, task_data->req_len,
  1625. task_data->req_buffer, task_data->req_dma);
  1626. return -ENOMEM;
  1627. }
  1628. static void qla4xxx_task_cleanup(struct iscsi_task *task)
  1629. {
  1630. struct ql4_task_data *task_data;
  1631. struct iscsi_session *sess;
  1632. struct ddb_entry *ddb_entry;
  1633. struct scsi_qla_host *ha;
  1634. int hdr_len;
  1635. hdr_len = sizeof(struct iscsi_hdr);
  1636. sess = task->conn->session;
  1637. ddb_entry = sess->dd_data;
  1638. ha = ddb_entry->ha;
  1639. task_data = task->dd_data;
  1640. if (task->data_count) {
  1641. dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
  1642. task->data_count, PCI_DMA_TODEVICE);
  1643. }
  1644. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1645. __func__, task->conn->max_recv_dlength, hdr_len));
  1646. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1647. task_data->resp_buffer, task_data->resp_dma);
  1648. dma_free_coherent(&ha->pdev->dev, task_data->req_len,
  1649. task_data->req_buffer, task_data->req_dma);
  1650. return;
  1651. }
  1652. static int qla4xxx_task_xmit(struct iscsi_task *task)
  1653. {
  1654. struct scsi_cmnd *sc = task->sc;
  1655. struct iscsi_session *sess = task->conn->session;
  1656. struct ddb_entry *ddb_entry = sess->dd_data;
  1657. struct scsi_qla_host *ha = ddb_entry->ha;
  1658. if (!sc)
  1659. return qla4xxx_send_passthru0(task);
  1660. ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
  1661. __func__);
  1662. return -ENOSYS;
  1663. }
  1664. static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
  1665. struct dev_db_entry *fw_ddb_entry,
  1666. struct iscsi_cls_session *cls_sess,
  1667. struct iscsi_cls_conn *cls_conn)
  1668. {
  1669. int buflen = 0;
  1670. struct iscsi_session *sess;
  1671. struct ddb_entry *ddb_entry;
  1672. struct iscsi_conn *conn;
  1673. char ip_addr[DDB_IPADDR_LEN];
  1674. uint16_t options = 0;
  1675. sess = cls_sess->dd_data;
  1676. ddb_entry = sess->dd_data;
  1677. conn = cls_conn->dd_data;
  1678. ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  1679. conn->max_recv_dlength = BYTE_UNITS *
  1680. le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  1681. conn->max_xmit_dlength = BYTE_UNITS *
  1682. le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  1683. sess->initial_r2t_en =
  1684. (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1685. sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  1686. sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1687. sess->first_burst = BYTE_UNITS *
  1688. le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  1689. sess->max_burst = BYTE_UNITS *
  1690. le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  1691. sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  1692. sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  1693. conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
  1694. sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  1695. options = le16_to_cpu(fw_ddb_entry->options);
  1696. if (options & DDB_OPT_IPV6_DEVICE)
  1697. sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  1698. else
  1699. sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  1700. iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
  1701. (char *)fw_ddb_entry->iscsi_name, buflen);
  1702. iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
  1703. (char *)ha->name_string, buflen);
  1704. iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
  1705. (char *)ip_addr, buflen);
  1706. iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
  1707. (char *)fw_ddb_entry->iscsi_alias, buflen);
  1708. }
  1709. void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
  1710. struct ddb_entry *ddb_entry)
  1711. {
  1712. struct iscsi_cls_session *cls_sess;
  1713. struct iscsi_cls_conn *cls_conn;
  1714. uint32_t ddb_state;
  1715. dma_addr_t fw_ddb_entry_dma;
  1716. struct dev_db_entry *fw_ddb_entry;
  1717. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1718. &fw_ddb_entry_dma, GFP_KERNEL);
  1719. if (!fw_ddb_entry) {
  1720. ql4_printk(KERN_ERR, ha,
  1721. "%s: Unable to allocate dma buffer\n", __func__);
  1722. goto exit_session_conn_fwddb_param;
  1723. }
  1724. if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  1725. fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  1726. NULL, NULL, NULL) == QLA_ERROR) {
  1727. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  1728. "get_ddb_entry for fw_ddb_index %d\n",
  1729. ha->host_no, __func__,
  1730. ddb_entry->fw_ddb_index));
  1731. goto exit_session_conn_fwddb_param;
  1732. }
  1733. cls_sess = ddb_entry->sess;
  1734. cls_conn = ddb_entry->conn;
  1735. /* Update params */
  1736. qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
  1737. exit_session_conn_fwddb_param:
  1738. if (fw_ddb_entry)
  1739. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1740. fw_ddb_entry, fw_ddb_entry_dma);
  1741. }
  1742. void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
  1743. struct ddb_entry *ddb_entry)
  1744. {
  1745. struct iscsi_cls_session *cls_sess;
  1746. struct iscsi_cls_conn *cls_conn;
  1747. struct iscsi_session *sess;
  1748. struct iscsi_conn *conn;
  1749. uint32_t ddb_state;
  1750. dma_addr_t fw_ddb_entry_dma;
  1751. struct dev_db_entry *fw_ddb_entry;
  1752. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1753. &fw_ddb_entry_dma, GFP_KERNEL);
  1754. if (!fw_ddb_entry) {
  1755. ql4_printk(KERN_ERR, ha,
  1756. "%s: Unable to allocate dma buffer\n", __func__);
  1757. goto exit_session_conn_param;
  1758. }
  1759. if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  1760. fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  1761. NULL, NULL, NULL) == QLA_ERROR) {
  1762. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  1763. "get_ddb_entry for fw_ddb_index %d\n",
  1764. ha->host_no, __func__,
  1765. ddb_entry->fw_ddb_index));
  1766. goto exit_session_conn_param;
  1767. }
  1768. cls_sess = ddb_entry->sess;
  1769. sess = cls_sess->dd_data;
  1770. cls_conn = ddb_entry->conn;
  1771. conn = cls_conn->dd_data;
  1772. /* Update timers after login */
  1773. ddb_entry->default_relogin_timeout =
  1774. (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
  1775. (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
  1776. le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
  1777. ddb_entry->default_time2wait =
  1778. le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  1779. /* Update params */
  1780. ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  1781. conn->max_recv_dlength = BYTE_UNITS *
  1782. le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  1783. conn->max_xmit_dlength = BYTE_UNITS *
  1784. le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  1785. sess->initial_r2t_en =
  1786. (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1787. sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  1788. sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1789. sess->first_burst = BYTE_UNITS *
  1790. le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  1791. sess->max_burst = BYTE_UNITS *
  1792. le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  1793. sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  1794. sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  1795. sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  1796. memcpy(sess->initiatorname, ha->name_string,
  1797. min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
  1798. iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
  1799. (char *)fw_ddb_entry->iscsi_alias, 0);
  1800. exit_session_conn_param:
  1801. if (fw_ddb_entry)
  1802. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1803. fw_ddb_entry, fw_ddb_entry_dma);
  1804. }
  1805. /*
  1806. * Timer routines
  1807. */
  1808. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  1809. unsigned long interval)
  1810. {
  1811. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  1812. __func__, ha->host->host_no));
  1813. init_timer(&ha->timer);
  1814. ha->timer.expires = jiffies + interval * HZ;
  1815. ha->timer.data = (unsigned long)ha;
  1816. ha->timer.function = (void (*)(unsigned long))func;
  1817. add_timer(&ha->timer);
  1818. ha->timer_active = 1;
  1819. }
  1820. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  1821. {
  1822. del_timer_sync(&ha->timer);
  1823. ha->timer_active = 0;
  1824. }
  1825. /***
  1826. * qla4xxx_mark_device_missing - blocks the session
  1827. * @cls_session: Pointer to the session to be blocked
  1828. * @ddb_entry: Pointer to device database entry
  1829. *
  1830. * This routine marks a device missing and close connection.
  1831. **/
  1832. void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
  1833. {
  1834. iscsi_block_session(cls_session);
  1835. }
  1836. /**
  1837. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  1838. * @ha: Pointer to host adapter structure.
  1839. *
  1840. * This routine marks a device missing and resets the relogin retry count.
  1841. **/
  1842. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  1843. {
  1844. iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
  1845. }
  1846. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  1847. struct ddb_entry *ddb_entry,
  1848. struct scsi_cmnd *cmd)
  1849. {
  1850. struct srb *srb;
  1851. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  1852. if (!srb)
  1853. return srb;
  1854. kref_init(&srb->srb_ref);
  1855. srb->ha = ha;
  1856. srb->ddb = ddb_entry;
  1857. srb->cmd = cmd;
  1858. srb->flags = 0;
  1859. CMD_SP(cmd) = (void *)srb;
  1860. return srb;
  1861. }
  1862. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  1863. {
  1864. struct scsi_cmnd *cmd = srb->cmd;
  1865. if (srb->flags & SRB_DMA_VALID) {
  1866. scsi_dma_unmap(cmd);
  1867. srb->flags &= ~SRB_DMA_VALID;
  1868. }
  1869. CMD_SP(cmd) = NULL;
  1870. }
  1871. void qla4xxx_srb_compl(struct kref *ref)
  1872. {
  1873. struct srb *srb = container_of(ref, struct srb, srb_ref);
  1874. struct scsi_cmnd *cmd = srb->cmd;
  1875. struct scsi_qla_host *ha = srb->ha;
  1876. qla4xxx_srb_free_dma(ha, srb);
  1877. mempool_free(srb, ha->srb_mempool);
  1878. cmd->scsi_done(cmd);
  1879. }
  1880. /**
  1881. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  1882. * @host: scsi host
  1883. * @cmd: Pointer to Linux's SCSI command structure
  1884. *
  1885. * Remarks:
  1886. * This routine is invoked by Linux to send a SCSI command to the driver.
  1887. * The mid-level driver tries to ensure that queuecommand never gets
  1888. * invoked concurrently with itself or the interrupt handler (although
  1889. * the interrupt handler may call this routine as part of request-
  1890. * completion handling). Unfortunely, it sometimes calls the scheduler
  1891. * in interrupt context which is a big NO! NO!.
  1892. **/
  1893. static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  1894. {
  1895. struct scsi_qla_host *ha = to_qla_host(host);
  1896. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1897. struct iscsi_cls_session *sess = ddb_entry->sess;
  1898. struct srb *srb;
  1899. int rval;
  1900. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1901. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  1902. cmd->result = DID_NO_CONNECT << 16;
  1903. else
  1904. cmd->result = DID_REQUEUE << 16;
  1905. goto qc_fail_command;
  1906. }
  1907. if (!sess) {
  1908. cmd->result = DID_IMM_RETRY << 16;
  1909. goto qc_fail_command;
  1910. }
  1911. rval = iscsi_session_chkready(sess);
  1912. if (rval) {
  1913. cmd->result = rval;
  1914. goto qc_fail_command;
  1915. }
  1916. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1917. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  1918. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1919. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  1920. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  1921. !test_bit(AF_ONLINE, &ha->flags) ||
  1922. !test_bit(AF_LINK_UP, &ha->flags) ||
  1923. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  1924. goto qc_host_busy;
  1925. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
  1926. if (!srb)
  1927. goto qc_host_busy;
  1928. rval = qla4xxx_send_command_to_isp(ha, srb);
  1929. if (rval != QLA_SUCCESS)
  1930. goto qc_host_busy_free_sp;
  1931. return 0;
  1932. qc_host_busy_free_sp:
  1933. qla4xxx_srb_free_dma(ha, srb);
  1934. mempool_free(srb, ha->srb_mempool);
  1935. qc_host_busy:
  1936. return SCSI_MLQUEUE_HOST_BUSY;
  1937. qc_fail_command:
  1938. cmd->scsi_done(cmd);
  1939. return 0;
  1940. }
  1941. /**
  1942. * qla4xxx_mem_free - frees memory allocated to adapter
  1943. * @ha: Pointer to host adapter structure.
  1944. *
  1945. * Frees memory previously allocated by qla4xxx_mem_alloc
  1946. **/
  1947. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  1948. {
  1949. if (ha->queues)
  1950. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  1951. ha->queues_dma);
  1952. ha->queues_len = 0;
  1953. ha->queues = NULL;
  1954. ha->queues_dma = 0;
  1955. ha->request_ring = NULL;
  1956. ha->request_dma = 0;
  1957. ha->response_ring = NULL;
  1958. ha->response_dma = 0;
  1959. ha->shadow_regs = NULL;
  1960. ha->shadow_regs_dma = 0;
  1961. /* Free srb pool. */
  1962. if (ha->srb_mempool)
  1963. mempool_destroy(ha->srb_mempool);
  1964. ha->srb_mempool = NULL;
  1965. if (ha->chap_dma_pool)
  1966. dma_pool_destroy(ha->chap_dma_pool);
  1967. if (ha->chap_list)
  1968. vfree(ha->chap_list);
  1969. ha->chap_list = NULL;
  1970. if (ha->fw_ddb_dma_pool)
  1971. dma_pool_destroy(ha->fw_ddb_dma_pool);
  1972. /* release io space registers */
  1973. if (is_qla8022(ha)) {
  1974. if (ha->nx_pcibase)
  1975. iounmap(
  1976. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  1977. } else if (ha->reg)
  1978. iounmap(ha->reg);
  1979. pci_release_regions(ha->pdev);
  1980. }
  1981. /**
  1982. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  1983. * @ha: Pointer to host adapter structure
  1984. *
  1985. * Allocates DMA memory for request and response queues. Also allocates memory
  1986. * for srbs.
  1987. **/
  1988. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  1989. {
  1990. unsigned long align;
  1991. /* Allocate contiguous block of DMA memory for queues. */
  1992. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  1993. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  1994. sizeof(struct shadow_regs) +
  1995. MEM_ALIGN_VALUE +
  1996. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  1997. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  1998. &ha->queues_dma, GFP_KERNEL);
  1999. if (ha->queues == NULL) {
  2000. ql4_printk(KERN_WARNING, ha,
  2001. "Memory Allocation failed - queues.\n");
  2002. goto mem_alloc_error_exit;
  2003. }
  2004. memset(ha->queues, 0, ha->queues_len);
  2005. /*
  2006. * As per RISC alignment requirements -- the bus-address must be a
  2007. * multiple of the request-ring size (in bytes).
  2008. */
  2009. align = 0;
  2010. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  2011. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  2012. (MEM_ALIGN_VALUE - 1));
  2013. /* Update request and response queue pointers. */
  2014. ha->request_dma = ha->queues_dma + align;
  2015. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  2016. ha->response_dma = ha->queues_dma + align +
  2017. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  2018. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  2019. (REQUEST_QUEUE_DEPTH *
  2020. QUEUE_SIZE));
  2021. ha->shadow_regs_dma = ha->queues_dma + align +
  2022. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  2023. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  2024. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  2025. (REQUEST_QUEUE_DEPTH *
  2026. QUEUE_SIZE) +
  2027. (RESPONSE_QUEUE_DEPTH *
  2028. QUEUE_SIZE));
  2029. /* Allocate memory for srb pool. */
  2030. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  2031. mempool_free_slab, srb_cachep);
  2032. if (ha->srb_mempool == NULL) {
  2033. ql4_printk(KERN_WARNING, ha,
  2034. "Memory Allocation failed - SRB Pool.\n");
  2035. goto mem_alloc_error_exit;
  2036. }
  2037. ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
  2038. CHAP_DMA_BLOCK_SIZE, 8, 0);
  2039. if (ha->chap_dma_pool == NULL) {
  2040. ql4_printk(KERN_WARNING, ha,
  2041. "%s: chap_dma_pool allocation failed..\n", __func__);
  2042. goto mem_alloc_error_exit;
  2043. }
  2044. ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
  2045. DDB_DMA_BLOCK_SIZE, 8, 0);
  2046. if (ha->fw_ddb_dma_pool == NULL) {
  2047. ql4_printk(KERN_WARNING, ha,
  2048. "%s: fw_ddb_dma_pool allocation failed..\n",
  2049. __func__);
  2050. goto mem_alloc_error_exit;
  2051. }
  2052. return QLA_SUCCESS;
  2053. mem_alloc_error_exit:
  2054. qla4xxx_mem_free(ha);
  2055. return QLA_ERROR;
  2056. }
  2057. /**
  2058. * qla4_8xxx_check_temp - Check the ISP82XX temperature.
  2059. * @ha: adapter block pointer.
  2060. *
  2061. * Note: The caller should not hold the idc lock.
  2062. **/
  2063. static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
  2064. {
  2065. uint32_t temp, temp_state, temp_val;
  2066. int status = QLA_SUCCESS;
  2067. temp = qla4_8xxx_rd_32(ha, CRB_TEMP_STATE);
  2068. temp_state = qla82xx_get_temp_state(temp);
  2069. temp_val = qla82xx_get_temp_val(temp);
  2070. if (temp_state == QLA82XX_TEMP_PANIC) {
  2071. ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
  2072. " exceeds maximum allowed. Hardware has been shut"
  2073. " down.\n", temp_val);
  2074. status = QLA_ERROR;
  2075. } else if (temp_state == QLA82XX_TEMP_WARN) {
  2076. if (ha->temperature == QLA82XX_TEMP_NORMAL)
  2077. ql4_printk(KERN_WARNING, ha, "Device temperature %d"
  2078. " degrees C exceeds operating range."
  2079. " Immediate action needed.\n", temp_val);
  2080. } else {
  2081. if (ha->temperature == QLA82XX_TEMP_WARN)
  2082. ql4_printk(KERN_INFO, ha, "Device temperature is"
  2083. " now %d degrees C in normal range.\n",
  2084. temp_val);
  2085. }
  2086. ha->temperature = temp_state;
  2087. return status;
  2088. }
  2089. /**
  2090. * qla4_8xxx_check_fw_alive - Check firmware health
  2091. * @ha: Pointer to host adapter structure.
  2092. *
  2093. * Context: Interrupt
  2094. **/
  2095. static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  2096. {
  2097. uint32_t fw_heartbeat_counter;
  2098. int status = QLA_SUCCESS;
  2099. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  2100. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  2101. if (fw_heartbeat_counter == 0xffffffff) {
  2102. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  2103. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  2104. ha->host_no, __func__));
  2105. return status;
  2106. }
  2107. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  2108. ha->seconds_since_last_heartbeat++;
  2109. /* FW not alive after 2 seconds */
  2110. if (ha->seconds_since_last_heartbeat == 2) {
  2111. ha->seconds_since_last_heartbeat = 0;
  2112. ql4_printk(KERN_INFO, ha,
  2113. "scsi(%ld): %s, Dumping hw/fw registers:\n "
  2114. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  2115. " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  2116. " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  2117. " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
  2118. ha->host_no, __func__,
  2119. qla4_8xxx_rd_32(ha,
  2120. QLA82XX_PEG_HALT_STATUS1),
  2121. qla4_8xxx_rd_32(ha,
  2122. QLA82XX_PEG_HALT_STATUS2),
  2123. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  2124. 0x3c),
  2125. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  2126. 0x3c),
  2127. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  2128. 0x3c),
  2129. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  2130. 0x3c),
  2131. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  2132. 0x3c));
  2133. status = QLA_ERROR;
  2134. }
  2135. } else
  2136. ha->seconds_since_last_heartbeat = 0;
  2137. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  2138. return status;
  2139. }
  2140. /**
  2141. * qla4_8xxx_watchdog - Poll dev state
  2142. * @ha: Pointer to host adapter structure.
  2143. *
  2144. * Context: Interrupt
  2145. **/
  2146. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  2147. {
  2148. uint32_t dev_state, halt_status;
  2149. /* don't poll if reset is going on */
  2150. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  2151. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2152. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
  2153. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  2154. if (qla4_8xxx_check_temp(ha)) {
  2155. ql4_printk(KERN_INFO, ha, "disabling pause"
  2156. " transmit on port 0 & 1.\n");
  2157. qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
  2158. CRB_NIU_XG_PAUSE_CTL_P0 |
  2159. CRB_NIU_XG_PAUSE_CTL_P1);
  2160. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  2161. qla4xxx_wake_dpc(ha);
  2162. } else if (dev_state == QLA82XX_DEV_NEED_RESET &&
  2163. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  2164. if (!ql4xdontresethba) {
  2165. ql4_printk(KERN_INFO, ha, "%s: HW State: "
  2166. "NEED RESET!\n", __func__);
  2167. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2168. qla4xxx_wake_dpc(ha);
  2169. }
  2170. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  2171. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  2172. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  2173. __func__);
  2174. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  2175. qla4xxx_wake_dpc(ha);
  2176. } else {
  2177. /* Check firmware health */
  2178. if (qla4_8xxx_check_fw_alive(ha)) {
  2179. ql4_printk(KERN_INFO, ha, "disabling pause"
  2180. " transmit on port 0 & 1.\n");
  2181. qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
  2182. CRB_NIU_XG_PAUSE_CTL_P0 |
  2183. CRB_NIU_XG_PAUSE_CTL_P1);
  2184. halt_status = qla4_8xxx_rd_32(ha,
  2185. QLA82XX_PEG_HALT_STATUS1);
  2186. if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
  2187. ql4_printk(KERN_ERR, ha, "%s:"
  2188. " Firmware aborted with"
  2189. " error code 0x00006700."
  2190. " Device is being reset\n",
  2191. __func__);
  2192. /* Since we cannot change dev_state in interrupt
  2193. * context, set appropriate DPC flag then wakeup
  2194. * DPC */
  2195. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  2196. set_bit(DPC_HA_UNRECOVERABLE,
  2197. &ha->dpc_flags);
  2198. else {
  2199. ql4_printk(KERN_INFO, ha, "%s: detect "
  2200. "abort needed!\n", __func__);
  2201. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2202. }
  2203. qla4xxx_mailbox_premature_completion(ha);
  2204. qla4xxx_wake_dpc(ha);
  2205. }
  2206. }
  2207. }
  2208. }
  2209. static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  2210. {
  2211. struct iscsi_session *sess;
  2212. struct ddb_entry *ddb_entry;
  2213. struct scsi_qla_host *ha;
  2214. sess = cls_sess->dd_data;
  2215. ddb_entry = sess->dd_data;
  2216. ha = ddb_entry->ha;
  2217. if (!(ddb_entry->ddb_type == FLASH_DDB))
  2218. return;
  2219. if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  2220. !iscsi_is_session_online(cls_sess)) {
  2221. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  2222. INVALID_ENTRY) {
  2223. if (atomic_read(&ddb_entry->retry_relogin_timer) ==
  2224. 0) {
  2225. atomic_set(&ddb_entry->retry_relogin_timer,
  2226. INVALID_ENTRY);
  2227. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  2228. set_bit(DF_RELOGIN, &ddb_entry->flags);
  2229. DEBUG2(ql4_printk(KERN_INFO, ha,
  2230. "%s: index [%d] login device\n",
  2231. __func__, ddb_entry->fw_ddb_index));
  2232. } else
  2233. atomic_dec(&ddb_entry->retry_relogin_timer);
  2234. }
  2235. }
  2236. /* Wait for relogin to timeout */
  2237. if (atomic_read(&ddb_entry->relogin_timer) &&
  2238. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  2239. /*
  2240. * If the relogin times out and the device is
  2241. * still NOT ONLINE then try and relogin again.
  2242. */
  2243. if (!iscsi_is_session_online(cls_sess)) {
  2244. /* Reset retry relogin timer */
  2245. atomic_inc(&ddb_entry->relogin_retry_count);
  2246. DEBUG2(ql4_printk(KERN_INFO, ha,
  2247. "%s: index[%d] relogin timed out-retrying"
  2248. " relogin (%d), retry (%d)\n", __func__,
  2249. ddb_entry->fw_ddb_index,
  2250. atomic_read(&ddb_entry->relogin_retry_count),
  2251. ddb_entry->default_time2wait + 4));
  2252. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  2253. atomic_set(&ddb_entry->retry_relogin_timer,
  2254. ddb_entry->default_time2wait + 4);
  2255. }
  2256. }
  2257. }
  2258. /**
  2259. * qla4xxx_timer - checks every second for work to do.
  2260. * @ha: Pointer to host adapter structure.
  2261. **/
  2262. static void qla4xxx_timer(struct scsi_qla_host *ha)
  2263. {
  2264. int start_dpc = 0;
  2265. uint16_t w;
  2266. iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
  2267. /* If we are in the middle of AER/EEH processing
  2268. * skip any processing and reschedule the timer
  2269. */
  2270. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  2271. mod_timer(&ha->timer, jiffies + HZ);
  2272. return;
  2273. }
  2274. /* Hardware read to trigger an EEH error during mailbox waits. */
  2275. if (!pci_channel_offline(ha->pdev))
  2276. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  2277. if (is_qla8022(ha)) {
  2278. qla4_8xxx_watchdog(ha);
  2279. }
  2280. if (!is_qla8022(ha)) {
  2281. /* Check for heartbeat interval. */
  2282. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  2283. ha->heartbeat_interval != 0) {
  2284. ha->seconds_since_last_heartbeat++;
  2285. if (ha->seconds_since_last_heartbeat >
  2286. ha->heartbeat_interval + 2)
  2287. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2288. }
  2289. }
  2290. /* Process any deferred work. */
  2291. if (!list_empty(&ha->work_list))
  2292. start_dpc++;
  2293. /* Wakeup the dpc routine for this adapter, if needed. */
  2294. if (start_dpc ||
  2295. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2296. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  2297. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  2298. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  2299. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  2300. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  2301. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  2302. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  2303. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  2304. test_bit(DPC_AEN, &ha->dpc_flags)) {
  2305. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  2306. " - dpc flags = 0x%lx\n",
  2307. ha->host_no, __func__, ha->dpc_flags));
  2308. qla4xxx_wake_dpc(ha);
  2309. }
  2310. /* Reschedule timer thread to call us back in one second */
  2311. mod_timer(&ha->timer, jiffies + HZ);
  2312. DEBUG2(ha->seconds_since_last_intr++);
  2313. }
  2314. /**
  2315. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  2316. * @ha: Pointer to host adapter structure.
  2317. *
  2318. * This routine stalls the driver until all outstanding commands are returned.
  2319. * Caller must release the Hardware Lock prior to calling this routine.
  2320. **/
  2321. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  2322. {
  2323. uint32_t index = 0;
  2324. unsigned long flags;
  2325. struct scsi_cmnd *cmd;
  2326. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  2327. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  2328. "complete\n", WAIT_CMD_TOV));
  2329. while (!time_after_eq(jiffies, wtime)) {
  2330. spin_lock_irqsave(&ha->hardware_lock, flags);
  2331. /* Find a command that hasn't completed. */
  2332. for (index = 0; index < ha->host->can_queue; index++) {
  2333. cmd = scsi_host_find_tag(ha->host, index);
  2334. /*
  2335. * We cannot just check if the index is valid,
  2336. * becase if we are run from the scsi eh, then
  2337. * the scsi/block layer is going to prevent
  2338. * the tag from being released.
  2339. */
  2340. if (cmd != NULL && CMD_SP(cmd))
  2341. break;
  2342. }
  2343. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2344. /* If No Commands are pending, wait is complete */
  2345. if (index == ha->host->can_queue)
  2346. return QLA_SUCCESS;
  2347. msleep(1000);
  2348. }
  2349. /* If we timed out on waiting for commands to come back
  2350. * return ERROR. */
  2351. return QLA_ERROR;
  2352. }
  2353. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  2354. {
  2355. uint32_t ctrl_status;
  2356. unsigned long flags = 0;
  2357. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  2358. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  2359. return QLA_ERROR;
  2360. spin_lock_irqsave(&ha->hardware_lock, flags);
  2361. /*
  2362. * If the SCSI Reset Interrupt bit is set, clear it.
  2363. * Otherwise, the Soft Reset won't work.
  2364. */
  2365. ctrl_status = readw(&ha->reg->ctrl_status);
  2366. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  2367. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  2368. /* Issue Soft Reset */
  2369. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  2370. readl(&ha->reg->ctrl_status);
  2371. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2372. return QLA_SUCCESS;
  2373. }
  2374. /**
  2375. * qla4xxx_soft_reset - performs soft reset.
  2376. * @ha: Pointer to host adapter structure.
  2377. **/
  2378. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  2379. {
  2380. uint32_t max_wait_time;
  2381. unsigned long flags = 0;
  2382. int status;
  2383. uint32_t ctrl_status;
  2384. status = qla4xxx_hw_reset(ha);
  2385. if (status != QLA_SUCCESS)
  2386. return status;
  2387. status = QLA_ERROR;
  2388. /* Wait until the Network Reset Intr bit is cleared */
  2389. max_wait_time = RESET_INTR_TOV;
  2390. do {
  2391. spin_lock_irqsave(&ha->hardware_lock, flags);
  2392. ctrl_status = readw(&ha->reg->ctrl_status);
  2393. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2394. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  2395. break;
  2396. msleep(1000);
  2397. } while ((--max_wait_time));
  2398. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  2399. DEBUG2(printk(KERN_WARNING
  2400. "scsi%ld: Network Reset Intr not cleared by "
  2401. "Network function, clearing it now!\n",
  2402. ha->host_no));
  2403. spin_lock_irqsave(&ha->hardware_lock, flags);
  2404. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  2405. readl(&ha->reg->ctrl_status);
  2406. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2407. }
  2408. /* Wait until the firmware tells us the Soft Reset is done */
  2409. max_wait_time = SOFT_RESET_TOV;
  2410. do {
  2411. spin_lock_irqsave(&ha->hardware_lock, flags);
  2412. ctrl_status = readw(&ha->reg->ctrl_status);
  2413. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2414. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  2415. status = QLA_SUCCESS;
  2416. break;
  2417. }
  2418. msleep(1000);
  2419. } while ((--max_wait_time));
  2420. /*
  2421. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  2422. * after the soft reset has taken place.
  2423. */
  2424. spin_lock_irqsave(&ha->hardware_lock, flags);
  2425. ctrl_status = readw(&ha->reg->ctrl_status);
  2426. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  2427. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  2428. readl(&ha->reg->ctrl_status);
  2429. }
  2430. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2431. /* If soft reset fails then most probably the bios on other
  2432. * function is also enabled.
  2433. * Since the initialization is sequential the other fn
  2434. * wont be able to acknowledge the soft reset.
  2435. * Issue a force soft reset to workaround this scenario.
  2436. */
  2437. if (max_wait_time == 0) {
  2438. /* Issue Force Soft Reset */
  2439. spin_lock_irqsave(&ha->hardware_lock, flags);
  2440. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  2441. readl(&ha->reg->ctrl_status);
  2442. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2443. /* Wait until the firmware tells us the Soft Reset is done */
  2444. max_wait_time = SOFT_RESET_TOV;
  2445. do {
  2446. spin_lock_irqsave(&ha->hardware_lock, flags);
  2447. ctrl_status = readw(&ha->reg->ctrl_status);
  2448. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2449. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  2450. status = QLA_SUCCESS;
  2451. break;
  2452. }
  2453. msleep(1000);
  2454. } while ((--max_wait_time));
  2455. }
  2456. return status;
  2457. }
  2458. /**
  2459. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  2460. * @ha: Pointer to host adapter structure.
  2461. * @res: returned scsi status
  2462. *
  2463. * This routine is called just prior to a HARD RESET to return all
  2464. * outstanding commands back to the Operating System.
  2465. * Caller should make sure that the following locks are released
  2466. * before this calling routine: Hardware lock, and io_request_lock.
  2467. **/
  2468. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  2469. {
  2470. struct srb *srb;
  2471. int i;
  2472. unsigned long flags;
  2473. spin_lock_irqsave(&ha->hardware_lock, flags);
  2474. for (i = 0; i < ha->host->can_queue; i++) {
  2475. srb = qla4xxx_del_from_active_array(ha, i);
  2476. if (srb != NULL) {
  2477. srb->cmd->result = res;
  2478. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  2479. }
  2480. }
  2481. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2482. }
  2483. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  2484. {
  2485. clear_bit(AF_ONLINE, &ha->flags);
  2486. /* Disable the board */
  2487. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  2488. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2489. qla4xxx_mark_all_devices_missing(ha);
  2490. clear_bit(AF_INIT_DONE, &ha->flags);
  2491. }
  2492. static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
  2493. {
  2494. struct iscsi_session *sess;
  2495. struct ddb_entry *ddb_entry;
  2496. sess = cls_session->dd_data;
  2497. ddb_entry = sess->dd_data;
  2498. ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
  2499. if (ddb_entry->ddb_type == FLASH_DDB)
  2500. iscsi_block_session(ddb_entry->sess);
  2501. else
  2502. iscsi_session_failure(cls_session->dd_data,
  2503. ISCSI_ERR_CONN_FAILED);
  2504. }
  2505. /**
  2506. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  2507. * @ha: Pointer to host adapter structure.
  2508. **/
  2509. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  2510. {
  2511. int status = QLA_ERROR;
  2512. uint8_t reset_chip = 0;
  2513. uint32_t dev_state;
  2514. unsigned long wait;
  2515. /* Stall incoming I/O until we are done */
  2516. scsi_block_requests(ha->host);
  2517. clear_bit(AF_ONLINE, &ha->flags);
  2518. clear_bit(AF_LINK_UP, &ha->flags);
  2519. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  2520. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2521. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  2522. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  2523. reset_chip = 1;
  2524. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  2525. * do not reset adapter, jump to initialize_adapter */
  2526. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2527. status = QLA_SUCCESS;
  2528. goto recover_ha_init_adapter;
  2529. }
  2530. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  2531. * from eh_host_reset or ioctl module */
  2532. if (is_qla8022(ha) && !reset_chip &&
  2533. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  2534. DEBUG2(ql4_printk(KERN_INFO, ha,
  2535. "scsi%ld: %s - Performing stop_firmware...\n",
  2536. ha->host_no, __func__));
  2537. status = ha->isp_ops->reset_firmware(ha);
  2538. if (status == QLA_SUCCESS) {
  2539. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  2540. qla4xxx_cmd_wait(ha);
  2541. ha->isp_ops->disable_intrs(ha);
  2542. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2543. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2544. } else {
  2545. /* If the stop_firmware fails then
  2546. * reset the entire chip */
  2547. reset_chip = 1;
  2548. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2549. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2550. }
  2551. }
  2552. /* Issue full chip reset if recovering from a catastrophic error,
  2553. * or if stop_firmware fails for ISP-82xx.
  2554. * This is the default case for ISP-4xxx */
  2555. if (!is_qla8022(ha) || reset_chip) {
  2556. if (!is_qla8022(ha))
  2557. goto chip_reset;
  2558. /* Check if 82XX firmware is alive or not
  2559. * We may have arrived here from NEED_RESET
  2560. * detection only */
  2561. if (test_bit(AF_FW_RECOVERY, &ha->flags))
  2562. goto chip_reset;
  2563. wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
  2564. while (time_before(jiffies, wait)) {
  2565. if (qla4_8xxx_check_fw_alive(ha)) {
  2566. qla4xxx_mailbox_premature_completion(ha);
  2567. break;
  2568. }
  2569. set_current_state(TASK_UNINTERRUPTIBLE);
  2570. schedule_timeout(HZ);
  2571. }
  2572. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  2573. qla4xxx_cmd_wait(ha);
  2574. chip_reset:
  2575. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2576. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2577. DEBUG2(ql4_printk(KERN_INFO, ha,
  2578. "scsi%ld: %s - Performing chip reset..\n",
  2579. ha->host_no, __func__));
  2580. status = ha->isp_ops->reset_chip(ha);
  2581. }
  2582. /* Flush any pending ddb changed AENs */
  2583. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2584. recover_ha_init_adapter:
  2585. /* Upon successful firmware/chip reset, re-initialize the adapter */
  2586. if (status == QLA_SUCCESS) {
  2587. /* For ISP-4xxx, force function 1 to always initialize
  2588. * before function 3 to prevent both funcions from
  2589. * stepping on top of the other */
  2590. if (!is_qla8022(ha) && (ha->mac_index == 3))
  2591. ssleep(6);
  2592. /* NOTE: AF_ONLINE flag set upon successful completion of
  2593. * qla4xxx_initialize_adapter */
  2594. status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  2595. }
  2596. /* Retry failed adapter initialization, if necessary
  2597. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  2598. * case to prevent ping-pong resets between functions */
  2599. if (!test_bit(AF_ONLINE, &ha->flags) &&
  2600. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2601. /* Adapter initialization failed, see if we can retry
  2602. * resetting the ha.
  2603. * Since we don't want to block the DPC for too long
  2604. * with multiple resets in the same thread,
  2605. * utilize DPC to retry */
  2606. if (is_qla8022(ha)) {
  2607. qla4_8xxx_idc_lock(ha);
  2608. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  2609. qla4_8xxx_idc_unlock(ha);
  2610. if (dev_state == QLA82XX_DEV_FAILED) {
  2611. ql4_printk(KERN_INFO, ha, "%s: don't retry "
  2612. "recover adapter. H/W is in Failed "
  2613. "state\n", __func__);
  2614. qla4xxx_dead_adapter_cleanup(ha);
  2615. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2616. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2617. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  2618. &ha->dpc_flags);
  2619. status = QLA_ERROR;
  2620. goto exit_recover;
  2621. }
  2622. }
  2623. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  2624. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  2625. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  2626. "(%d) more times\n", ha->host_no,
  2627. ha->retry_reset_ha_cnt));
  2628. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2629. status = QLA_ERROR;
  2630. } else {
  2631. if (ha->retry_reset_ha_cnt > 0) {
  2632. /* Schedule another Reset HA--DPC will retry */
  2633. ha->retry_reset_ha_cnt--;
  2634. DEBUG2(printk("scsi%ld: recover adapter - "
  2635. "retry remaining %d\n",
  2636. ha->host_no,
  2637. ha->retry_reset_ha_cnt));
  2638. status = QLA_ERROR;
  2639. }
  2640. if (ha->retry_reset_ha_cnt == 0) {
  2641. /* Recover adapter retries have been exhausted.
  2642. * Adapter DEAD */
  2643. DEBUG2(printk("scsi%ld: recover adapter "
  2644. "failed - board disabled\n",
  2645. ha->host_no));
  2646. qla4xxx_dead_adapter_cleanup(ha);
  2647. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2648. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2649. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  2650. &ha->dpc_flags);
  2651. status = QLA_ERROR;
  2652. }
  2653. }
  2654. } else {
  2655. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2656. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2657. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2658. }
  2659. exit_recover:
  2660. ha->adapter_error_count++;
  2661. if (test_bit(AF_ONLINE, &ha->flags))
  2662. ha->isp_ops->enable_intrs(ha);
  2663. scsi_unblock_requests(ha->host);
  2664. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2665. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  2666. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  2667. return status;
  2668. }
  2669. static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
  2670. {
  2671. struct iscsi_session *sess;
  2672. struct ddb_entry *ddb_entry;
  2673. struct scsi_qla_host *ha;
  2674. sess = cls_session->dd_data;
  2675. ddb_entry = sess->dd_data;
  2676. ha = ddb_entry->ha;
  2677. if (!iscsi_is_session_online(cls_session)) {
  2678. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  2679. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2680. " unblock session\n", ha->host_no, __func__,
  2681. ddb_entry->fw_ddb_index);
  2682. iscsi_unblock_session(ddb_entry->sess);
  2683. } else {
  2684. /* Trigger relogin */
  2685. if (ddb_entry->ddb_type == FLASH_DDB) {
  2686. if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
  2687. qla4xxx_arm_relogin_timer(ddb_entry);
  2688. } else
  2689. iscsi_session_failure(cls_session->dd_data,
  2690. ISCSI_ERR_CONN_FAILED);
  2691. }
  2692. }
  2693. }
  2694. int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
  2695. {
  2696. struct iscsi_session *sess;
  2697. struct ddb_entry *ddb_entry;
  2698. struct scsi_qla_host *ha;
  2699. sess = cls_session->dd_data;
  2700. ddb_entry = sess->dd_data;
  2701. ha = ddb_entry->ha;
  2702. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2703. " unblock session\n", ha->host_no, __func__,
  2704. ddb_entry->fw_ddb_index);
  2705. iscsi_unblock_session(ddb_entry->sess);
  2706. /* Start scan target */
  2707. if (test_bit(AF_ONLINE, &ha->flags)) {
  2708. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2709. " start scan\n", ha->host_no, __func__,
  2710. ddb_entry->fw_ddb_index);
  2711. scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
  2712. }
  2713. return QLA_SUCCESS;
  2714. }
  2715. int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
  2716. {
  2717. struct iscsi_session *sess;
  2718. struct ddb_entry *ddb_entry;
  2719. struct scsi_qla_host *ha;
  2720. sess = cls_session->dd_data;
  2721. ddb_entry = sess->dd_data;
  2722. ha = ddb_entry->ha;
  2723. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2724. " unblock user space session\n", ha->host_no, __func__,
  2725. ddb_entry->fw_ddb_index);
  2726. iscsi_conn_start(ddb_entry->conn);
  2727. iscsi_conn_login_event(ddb_entry->conn,
  2728. ISCSI_CONN_STATE_LOGGED_IN);
  2729. return QLA_SUCCESS;
  2730. }
  2731. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  2732. {
  2733. iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
  2734. }
  2735. static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  2736. {
  2737. uint16_t relogin_timer;
  2738. struct iscsi_session *sess;
  2739. struct ddb_entry *ddb_entry;
  2740. struct scsi_qla_host *ha;
  2741. sess = cls_sess->dd_data;
  2742. ddb_entry = sess->dd_data;
  2743. ha = ddb_entry->ha;
  2744. relogin_timer = max(ddb_entry->default_relogin_timeout,
  2745. (uint16_t)RELOGIN_TOV);
  2746. atomic_set(&ddb_entry->relogin_timer, relogin_timer);
  2747. DEBUG2(ql4_printk(KERN_INFO, ha,
  2748. "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
  2749. ddb_entry->fw_ddb_index, relogin_timer));
  2750. qla4xxx_login_flash_ddb(cls_sess);
  2751. }
  2752. static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
  2753. {
  2754. struct iscsi_session *sess;
  2755. struct ddb_entry *ddb_entry;
  2756. struct scsi_qla_host *ha;
  2757. sess = cls_sess->dd_data;
  2758. ddb_entry = sess->dd_data;
  2759. ha = ddb_entry->ha;
  2760. if (!(ddb_entry->ddb_type == FLASH_DDB))
  2761. return;
  2762. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  2763. !iscsi_is_session_online(cls_sess)) {
  2764. DEBUG2(ql4_printk(KERN_INFO, ha,
  2765. "relogin issued\n"));
  2766. qla4xxx_relogin_flash_ddb(cls_sess);
  2767. }
  2768. }
  2769. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  2770. {
  2771. if (ha->dpc_thread)
  2772. queue_work(ha->dpc_thread, &ha->dpc_work);
  2773. }
  2774. static struct qla4_work_evt *
  2775. qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
  2776. enum qla4_work_type type)
  2777. {
  2778. struct qla4_work_evt *e;
  2779. uint32_t size = sizeof(struct qla4_work_evt) + data_size;
  2780. e = kzalloc(size, GFP_ATOMIC);
  2781. if (!e)
  2782. return NULL;
  2783. INIT_LIST_HEAD(&e->list);
  2784. e->type = type;
  2785. return e;
  2786. }
  2787. static void qla4xxx_post_work(struct scsi_qla_host *ha,
  2788. struct qla4_work_evt *e)
  2789. {
  2790. unsigned long flags;
  2791. spin_lock_irqsave(&ha->work_lock, flags);
  2792. list_add_tail(&e->list, &ha->work_list);
  2793. spin_unlock_irqrestore(&ha->work_lock, flags);
  2794. qla4xxx_wake_dpc(ha);
  2795. }
  2796. int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
  2797. enum iscsi_host_event_code aen_code,
  2798. uint32_t data_size, uint8_t *data)
  2799. {
  2800. struct qla4_work_evt *e;
  2801. e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
  2802. if (!e)
  2803. return QLA_ERROR;
  2804. e->u.aen.code = aen_code;
  2805. e->u.aen.data_size = data_size;
  2806. memcpy(e->u.aen.data, data, data_size);
  2807. qla4xxx_post_work(ha, e);
  2808. return QLA_SUCCESS;
  2809. }
  2810. int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
  2811. uint32_t status, uint32_t pid,
  2812. uint32_t data_size, uint8_t *data)
  2813. {
  2814. struct qla4_work_evt *e;
  2815. e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
  2816. if (!e)
  2817. return QLA_ERROR;
  2818. e->u.ping.status = status;
  2819. e->u.ping.pid = pid;
  2820. e->u.ping.data_size = data_size;
  2821. memcpy(e->u.ping.data, data, data_size);
  2822. qla4xxx_post_work(ha, e);
  2823. return QLA_SUCCESS;
  2824. }
  2825. static void qla4xxx_do_work(struct scsi_qla_host *ha)
  2826. {
  2827. struct qla4_work_evt *e, *tmp;
  2828. unsigned long flags;
  2829. LIST_HEAD(work);
  2830. spin_lock_irqsave(&ha->work_lock, flags);
  2831. list_splice_init(&ha->work_list, &work);
  2832. spin_unlock_irqrestore(&ha->work_lock, flags);
  2833. list_for_each_entry_safe(e, tmp, &work, list) {
  2834. list_del_init(&e->list);
  2835. switch (e->type) {
  2836. case QLA4_EVENT_AEN:
  2837. iscsi_post_host_event(ha->host_no,
  2838. &qla4xxx_iscsi_transport,
  2839. e->u.aen.code,
  2840. e->u.aen.data_size,
  2841. e->u.aen.data);
  2842. break;
  2843. case QLA4_EVENT_PING_STATUS:
  2844. iscsi_ping_comp_event(ha->host_no,
  2845. &qla4xxx_iscsi_transport,
  2846. e->u.ping.status,
  2847. e->u.ping.pid,
  2848. e->u.ping.data_size,
  2849. e->u.ping.data);
  2850. break;
  2851. default:
  2852. ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
  2853. "supported", e->type);
  2854. }
  2855. kfree(e);
  2856. }
  2857. }
  2858. /**
  2859. * qla4xxx_do_dpc - dpc routine
  2860. * @data: in our case pointer to adapter structure
  2861. *
  2862. * This routine is a task that is schedule by the interrupt handler
  2863. * to perform the background processing for interrupts. We put it
  2864. * on a task queue that is consumed whenever the scheduler runs; that's
  2865. * so you can do anything (i.e. put the process to sleep etc). In fact,
  2866. * the mid-level tries to sleep when it reaches the driver threshold
  2867. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  2868. **/
  2869. static void qla4xxx_do_dpc(struct work_struct *work)
  2870. {
  2871. struct scsi_qla_host *ha =
  2872. container_of(work, struct scsi_qla_host, dpc_work);
  2873. int status = QLA_ERROR;
  2874. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  2875. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  2876. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  2877. /* Initialization not yet finished. Don't do anything yet. */
  2878. if (!test_bit(AF_INIT_DONE, &ha->flags))
  2879. return;
  2880. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  2881. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  2882. ha->host_no, __func__, ha->flags));
  2883. return;
  2884. }
  2885. /* post events to application */
  2886. qla4xxx_do_work(ha);
  2887. if (is_qla8022(ha)) {
  2888. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  2889. qla4_8xxx_idc_lock(ha);
  2890. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2891. QLA82XX_DEV_FAILED);
  2892. qla4_8xxx_idc_unlock(ha);
  2893. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  2894. qla4_8xxx_device_state_handler(ha);
  2895. }
  2896. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  2897. qla4_8xxx_need_qsnt_handler(ha);
  2898. }
  2899. }
  2900. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  2901. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2902. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  2903. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  2904. if (ql4xdontresethba) {
  2905. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  2906. ha->host_no, __func__));
  2907. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2908. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2909. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2910. goto dpc_post_reset_ha;
  2911. }
  2912. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  2913. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  2914. qla4xxx_recover_adapter(ha);
  2915. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2916. uint8_t wait_time = RESET_INTR_TOV;
  2917. while ((readw(&ha->reg->ctrl_status) &
  2918. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  2919. if (--wait_time == 0)
  2920. break;
  2921. msleep(1000);
  2922. }
  2923. if (wait_time == 0)
  2924. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  2925. "bit not cleared-- resetting\n",
  2926. ha->host_no, __func__));
  2927. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2928. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  2929. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2930. status = qla4xxx_recover_adapter(ha);
  2931. }
  2932. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2933. if (status == QLA_SUCCESS)
  2934. ha->isp_ops->enable_intrs(ha);
  2935. }
  2936. }
  2937. dpc_post_reset_ha:
  2938. /* ---- process AEN? --- */
  2939. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  2940. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  2941. /* ---- Get DHCP IP Address? --- */
  2942. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  2943. qla4xxx_get_dhcp_ip_address(ha);
  2944. /* ---- relogin device? --- */
  2945. if (adapter_up(ha) &&
  2946. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  2947. iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
  2948. }
  2949. /* ---- link change? --- */
  2950. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  2951. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  2952. /* ---- link down? --- */
  2953. qla4xxx_mark_all_devices_missing(ha);
  2954. } else {
  2955. /* ---- link up? --- *
  2956. * F/W will auto login to all devices ONLY ONCE after
  2957. * link up during driver initialization and runtime
  2958. * fatal error recovery. Therefore, the driver must
  2959. * manually relogin to devices when recovering from
  2960. * connection failures, logouts, expired KATO, etc. */
  2961. if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
  2962. qla4xxx_build_ddb_list(ha, ha->is_reset);
  2963. iscsi_host_for_each_session(ha->host,
  2964. qla4xxx_login_flash_ddb);
  2965. } else
  2966. qla4xxx_relogin_all_devices(ha);
  2967. }
  2968. }
  2969. }
  2970. /**
  2971. * qla4xxx_free_adapter - release the adapter
  2972. * @ha: pointer to adapter structure
  2973. **/
  2974. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  2975. {
  2976. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2977. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  2978. /* Turn-off interrupts on the card. */
  2979. ha->isp_ops->disable_intrs(ha);
  2980. }
  2981. /* Remove timer thread, if present */
  2982. if (ha->timer_active)
  2983. qla4xxx_stop_timer(ha);
  2984. /* Kill the kernel thread for this host */
  2985. if (ha->dpc_thread)
  2986. destroy_workqueue(ha->dpc_thread);
  2987. /* Kill the kernel thread for this host */
  2988. if (ha->task_wq)
  2989. destroy_workqueue(ha->task_wq);
  2990. /* Put firmware in known state */
  2991. ha->isp_ops->reset_firmware(ha);
  2992. if (is_qla8022(ha)) {
  2993. qla4_8xxx_idc_lock(ha);
  2994. qla4_8xxx_clear_drv_active(ha);
  2995. qla4_8xxx_idc_unlock(ha);
  2996. }
  2997. /* Detach interrupts */
  2998. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  2999. qla4xxx_free_irqs(ha);
  3000. /* free extra memory */
  3001. qla4xxx_mem_free(ha);
  3002. }
  3003. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  3004. {
  3005. int status = 0;
  3006. unsigned long mem_base, mem_len, db_base, db_len;
  3007. struct pci_dev *pdev = ha->pdev;
  3008. status = pci_request_regions(pdev, DRIVER_NAME);
  3009. if (status) {
  3010. printk(KERN_WARNING
  3011. "scsi(%ld) Failed to reserve PIO regions (%s) "
  3012. "status=%d\n", ha->host_no, pci_name(pdev), status);
  3013. goto iospace_error_exit;
  3014. }
  3015. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  3016. __func__, pdev->revision));
  3017. ha->revision_id = pdev->revision;
  3018. /* remap phys address */
  3019. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  3020. mem_len = pci_resource_len(pdev, 0);
  3021. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  3022. __func__, mem_base, mem_len));
  3023. /* mapping of pcibase pointer */
  3024. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  3025. if (!ha->nx_pcibase) {
  3026. printk(KERN_ERR
  3027. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  3028. pci_release_regions(ha->pdev);
  3029. goto iospace_error_exit;
  3030. }
  3031. /* Mapping of IO base pointer, door bell read and write pointer */
  3032. /* mapping of IO base pointer */
  3033. ha->qla4_8xxx_reg =
  3034. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  3035. 0xbc000 + (ha->pdev->devfn << 11));
  3036. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  3037. db_len = pci_resource_len(pdev, 4);
  3038. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  3039. QLA82XX_CAM_RAM_DB2);
  3040. return 0;
  3041. iospace_error_exit:
  3042. return -ENOMEM;
  3043. }
  3044. /***
  3045. * qla4xxx_iospace_config - maps registers
  3046. * @ha: pointer to adapter structure
  3047. *
  3048. * This routines maps HBA's registers from the pci address space
  3049. * into the kernel virtual address space for memory mapped i/o.
  3050. **/
  3051. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  3052. {
  3053. unsigned long pio, pio_len, pio_flags;
  3054. unsigned long mmio, mmio_len, mmio_flags;
  3055. pio = pci_resource_start(ha->pdev, 0);
  3056. pio_len = pci_resource_len(ha->pdev, 0);
  3057. pio_flags = pci_resource_flags(ha->pdev, 0);
  3058. if (pio_flags & IORESOURCE_IO) {
  3059. if (pio_len < MIN_IOBASE_LEN) {
  3060. ql4_printk(KERN_WARNING, ha,
  3061. "Invalid PCI I/O region size\n");
  3062. pio = 0;
  3063. }
  3064. } else {
  3065. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  3066. pio = 0;
  3067. }
  3068. /* Use MMIO operations for all accesses. */
  3069. mmio = pci_resource_start(ha->pdev, 1);
  3070. mmio_len = pci_resource_len(ha->pdev, 1);
  3071. mmio_flags = pci_resource_flags(ha->pdev, 1);
  3072. if (!(mmio_flags & IORESOURCE_MEM)) {
  3073. ql4_printk(KERN_ERR, ha,
  3074. "region #0 not an MMIO resource, aborting\n");
  3075. goto iospace_error_exit;
  3076. }
  3077. if (mmio_len < MIN_IOBASE_LEN) {
  3078. ql4_printk(KERN_ERR, ha,
  3079. "Invalid PCI mem region size, aborting\n");
  3080. goto iospace_error_exit;
  3081. }
  3082. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  3083. ql4_printk(KERN_WARNING, ha,
  3084. "Failed to reserve PIO/MMIO regions\n");
  3085. goto iospace_error_exit;
  3086. }
  3087. ha->pio_address = pio;
  3088. ha->pio_length = pio_len;
  3089. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  3090. if (!ha->reg) {
  3091. ql4_printk(KERN_ERR, ha,
  3092. "cannot remap MMIO, aborting\n");
  3093. goto iospace_error_exit;
  3094. }
  3095. return 0;
  3096. iospace_error_exit:
  3097. return -ENOMEM;
  3098. }
  3099. static struct isp_operations qla4xxx_isp_ops = {
  3100. .iospace_config = qla4xxx_iospace_config,
  3101. .pci_config = qla4xxx_pci_config,
  3102. .disable_intrs = qla4xxx_disable_intrs,
  3103. .enable_intrs = qla4xxx_enable_intrs,
  3104. .start_firmware = qla4xxx_start_firmware,
  3105. .intr_handler = qla4xxx_intr_handler,
  3106. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  3107. .reset_chip = qla4xxx_soft_reset,
  3108. .reset_firmware = qla4xxx_hw_reset,
  3109. .queue_iocb = qla4xxx_queue_iocb,
  3110. .complete_iocb = qla4xxx_complete_iocb,
  3111. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  3112. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  3113. .get_sys_info = qla4xxx_get_sys_info,
  3114. };
  3115. static struct isp_operations qla4_8xxx_isp_ops = {
  3116. .iospace_config = qla4_8xxx_iospace_config,
  3117. .pci_config = qla4_8xxx_pci_config,
  3118. .disable_intrs = qla4_8xxx_disable_intrs,
  3119. .enable_intrs = qla4_8xxx_enable_intrs,
  3120. .start_firmware = qla4_8xxx_load_risc,
  3121. .intr_handler = qla4_8xxx_intr_handler,
  3122. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  3123. .reset_chip = qla4_8xxx_isp_reset,
  3124. .reset_firmware = qla4_8xxx_stop_firmware,
  3125. .queue_iocb = qla4_8xxx_queue_iocb,
  3126. .complete_iocb = qla4_8xxx_complete_iocb,
  3127. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  3128. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  3129. .get_sys_info = qla4_8xxx_get_sys_info,
  3130. };
  3131. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  3132. {
  3133. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  3134. }
  3135. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  3136. {
  3137. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  3138. }
  3139. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  3140. {
  3141. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  3142. }
  3143. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  3144. {
  3145. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  3146. }
  3147. static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
  3148. {
  3149. struct scsi_qla_host *ha = data;
  3150. char *str = buf;
  3151. int rc;
  3152. switch (type) {
  3153. case ISCSI_BOOT_ETH_FLAGS:
  3154. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  3155. break;
  3156. case ISCSI_BOOT_ETH_INDEX:
  3157. rc = sprintf(str, "0\n");
  3158. break;
  3159. case ISCSI_BOOT_ETH_MAC:
  3160. rc = sysfs_format_mac(str, ha->my_mac,
  3161. MAC_ADDR_LEN);
  3162. break;
  3163. default:
  3164. rc = -ENOSYS;
  3165. break;
  3166. }
  3167. return rc;
  3168. }
  3169. static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
  3170. {
  3171. int rc;
  3172. switch (type) {
  3173. case ISCSI_BOOT_ETH_FLAGS:
  3174. case ISCSI_BOOT_ETH_MAC:
  3175. case ISCSI_BOOT_ETH_INDEX:
  3176. rc = S_IRUGO;
  3177. break;
  3178. default:
  3179. rc = 0;
  3180. break;
  3181. }
  3182. return rc;
  3183. }
  3184. static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
  3185. {
  3186. struct scsi_qla_host *ha = data;
  3187. char *str = buf;
  3188. int rc;
  3189. switch (type) {
  3190. case ISCSI_BOOT_INI_INITIATOR_NAME:
  3191. rc = sprintf(str, "%s\n", ha->name_string);
  3192. break;
  3193. default:
  3194. rc = -ENOSYS;
  3195. break;
  3196. }
  3197. return rc;
  3198. }
  3199. static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
  3200. {
  3201. int rc;
  3202. switch (type) {
  3203. case ISCSI_BOOT_INI_INITIATOR_NAME:
  3204. rc = S_IRUGO;
  3205. break;
  3206. default:
  3207. rc = 0;
  3208. break;
  3209. }
  3210. return rc;
  3211. }
  3212. static ssize_t
  3213. qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
  3214. char *buf)
  3215. {
  3216. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  3217. char *str = buf;
  3218. int rc;
  3219. switch (type) {
  3220. case ISCSI_BOOT_TGT_NAME:
  3221. rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
  3222. break;
  3223. case ISCSI_BOOT_TGT_IP_ADDR:
  3224. if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
  3225. rc = sprintf(buf, "%pI4\n",
  3226. &boot_conn->dest_ipaddr.ip_address);
  3227. else
  3228. rc = sprintf(str, "%pI6\n",
  3229. &boot_conn->dest_ipaddr.ip_address);
  3230. break;
  3231. case ISCSI_BOOT_TGT_PORT:
  3232. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  3233. break;
  3234. case ISCSI_BOOT_TGT_CHAP_NAME:
  3235. rc = sprintf(str, "%.*s\n",
  3236. boot_conn->chap.target_chap_name_length,
  3237. (char *)&boot_conn->chap.target_chap_name);
  3238. break;
  3239. case ISCSI_BOOT_TGT_CHAP_SECRET:
  3240. rc = sprintf(str, "%.*s\n",
  3241. boot_conn->chap.target_secret_length,
  3242. (char *)&boot_conn->chap.target_secret);
  3243. break;
  3244. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  3245. rc = sprintf(str, "%.*s\n",
  3246. boot_conn->chap.intr_chap_name_length,
  3247. (char *)&boot_conn->chap.intr_chap_name);
  3248. break;
  3249. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  3250. rc = sprintf(str, "%.*s\n",
  3251. boot_conn->chap.intr_secret_length,
  3252. (char *)&boot_conn->chap.intr_secret);
  3253. break;
  3254. case ISCSI_BOOT_TGT_FLAGS:
  3255. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  3256. break;
  3257. case ISCSI_BOOT_TGT_NIC_ASSOC:
  3258. rc = sprintf(str, "0\n");
  3259. break;
  3260. default:
  3261. rc = -ENOSYS;
  3262. break;
  3263. }
  3264. return rc;
  3265. }
  3266. static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
  3267. {
  3268. struct scsi_qla_host *ha = data;
  3269. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
  3270. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  3271. }
  3272. static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
  3273. {
  3274. struct scsi_qla_host *ha = data;
  3275. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
  3276. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  3277. }
  3278. static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
  3279. {
  3280. int rc;
  3281. switch (type) {
  3282. case ISCSI_BOOT_TGT_NAME:
  3283. case ISCSI_BOOT_TGT_IP_ADDR:
  3284. case ISCSI_BOOT_TGT_PORT:
  3285. case ISCSI_BOOT_TGT_CHAP_NAME:
  3286. case ISCSI_BOOT_TGT_CHAP_SECRET:
  3287. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  3288. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  3289. case ISCSI_BOOT_TGT_NIC_ASSOC:
  3290. case ISCSI_BOOT_TGT_FLAGS:
  3291. rc = S_IRUGO;
  3292. break;
  3293. default:
  3294. rc = 0;
  3295. break;
  3296. }
  3297. return rc;
  3298. }
  3299. static void qla4xxx_boot_release(void *data)
  3300. {
  3301. struct scsi_qla_host *ha = data;
  3302. scsi_host_put(ha->host);
  3303. }
  3304. static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
  3305. {
  3306. dma_addr_t buf_dma;
  3307. uint32_t addr, pri_addr, sec_addr;
  3308. uint32_t offset;
  3309. uint16_t func_num;
  3310. uint8_t val;
  3311. uint8_t *buf = NULL;
  3312. size_t size = 13 * sizeof(uint8_t);
  3313. int ret = QLA_SUCCESS;
  3314. func_num = PCI_FUNC(ha->pdev->devfn);
  3315. ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
  3316. __func__, ha->pdev->device, func_num);
  3317. if (is_qla40XX(ha)) {
  3318. if (func_num == 1) {
  3319. addr = NVRAM_PORT0_BOOT_MODE;
  3320. pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
  3321. sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
  3322. } else if (func_num == 3) {
  3323. addr = NVRAM_PORT1_BOOT_MODE;
  3324. pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
  3325. sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
  3326. } else {
  3327. ret = QLA_ERROR;
  3328. goto exit_boot_info;
  3329. }
  3330. /* Check Boot Mode */
  3331. val = rd_nvram_byte(ha, addr);
  3332. if (!(val & 0x07)) {
  3333. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
  3334. "options : 0x%x\n", __func__, val));
  3335. ret = QLA_ERROR;
  3336. goto exit_boot_info;
  3337. }
  3338. /* get primary valid target index */
  3339. val = rd_nvram_byte(ha, pri_addr);
  3340. if (val & BIT_7)
  3341. ddb_index[0] = (val & 0x7f);
  3342. /* get secondary valid target index */
  3343. val = rd_nvram_byte(ha, sec_addr);
  3344. if (val & BIT_7)
  3345. ddb_index[1] = (val & 0x7f);
  3346. } else if (is_qla8022(ha)) {
  3347. buf = dma_alloc_coherent(&ha->pdev->dev, size,
  3348. &buf_dma, GFP_KERNEL);
  3349. if (!buf) {
  3350. DEBUG2(ql4_printk(KERN_ERR, ha,
  3351. "%s: Unable to allocate dma buffer\n",
  3352. __func__));
  3353. ret = QLA_ERROR;
  3354. goto exit_boot_info;
  3355. }
  3356. if (ha->port_num == 0)
  3357. offset = BOOT_PARAM_OFFSET_PORT0;
  3358. else if (ha->port_num == 1)
  3359. offset = BOOT_PARAM_OFFSET_PORT1;
  3360. else {
  3361. ret = QLA_ERROR;
  3362. goto exit_boot_info_free;
  3363. }
  3364. addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
  3365. offset;
  3366. if (qla4xxx_get_flash(ha, buf_dma, addr,
  3367. 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
  3368. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
  3369. " failed\n", ha->host_no, __func__));
  3370. ret = QLA_ERROR;
  3371. goto exit_boot_info_free;
  3372. }
  3373. /* Check Boot Mode */
  3374. if (!(buf[1] & 0x07)) {
  3375. DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
  3376. " : 0x%x\n", buf[1]));
  3377. ret = QLA_ERROR;
  3378. goto exit_boot_info_free;
  3379. }
  3380. /* get primary valid target index */
  3381. if (buf[2] & BIT_7)
  3382. ddb_index[0] = buf[2] & 0x7f;
  3383. /* get secondary valid target index */
  3384. if (buf[11] & BIT_7)
  3385. ddb_index[1] = buf[11] & 0x7f;
  3386. } else {
  3387. ret = QLA_ERROR;
  3388. goto exit_boot_info;
  3389. }
  3390. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
  3391. " target ID %d\n", __func__, ddb_index[0],
  3392. ddb_index[1]));
  3393. exit_boot_info_free:
  3394. dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
  3395. exit_boot_info:
  3396. ha->pri_ddb_idx = ddb_index[0];
  3397. ha->sec_ddb_idx = ddb_index[1];
  3398. return ret;
  3399. }
  3400. /**
  3401. * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
  3402. * @ha: pointer to adapter structure
  3403. * @username: CHAP username to be returned
  3404. * @password: CHAP password to be returned
  3405. *
  3406. * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
  3407. * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
  3408. * So from the CHAP cache find the first BIDI CHAP entry and set it
  3409. * to the boot record in sysfs.
  3410. **/
  3411. static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
  3412. char *password)
  3413. {
  3414. int i, ret = -EINVAL;
  3415. int max_chap_entries = 0;
  3416. struct ql4_chap_table *chap_table;
  3417. if (is_qla8022(ha))
  3418. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  3419. sizeof(struct ql4_chap_table);
  3420. else
  3421. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  3422. if (!ha->chap_list) {
  3423. ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
  3424. return ret;
  3425. }
  3426. mutex_lock(&ha->chap_sem);
  3427. for (i = 0; i < max_chap_entries; i++) {
  3428. chap_table = (struct ql4_chap_table *)ha->chap_list + i;
  3429. if (chap_table->cookie !=
  3430. __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
  3431. continue;
  3432. }
  3433. if (chap_table->flags & BIT_7) /* local */
  3434. continue;
  3435. if (!(chap_table->flags & BIT_6)) /* Not BIDI */
  3436. continue;
  3437. strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
  3438. strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
  3439. ret = 0;
  3440. break;
  3441. }
  3442. mutex_unlock(&ha->chap_sem);
  3443. return ret;
  3444. }
  3445. static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
  3446. struct ql4_boot_session_info *boot_sess,
  3447. uint16_t ddb_index)
  3448. {
  3449. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  3450. struct dev_db_entry *fw_ddb_entry;
  3451. dma_addr_t fw_ddb_entry_dma;
  3452. uint16_t idx;
  3453. uint16_t options;
  3454. int ret = QLA_SUCCESS;
  3455. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  3456. &fw_ddb_entry_dma, GFP_KERNEL);
  3457. if (!fw_ddb_entry) {
  3458. DEBUG2(ql4_printk(KERN_ERR, ha,
  3459. "%s: Unable to allocate dma buffer.\n",
  3460. __func__));
  3461. ret = QLA_ERROR;
  3462. return ret;
  3463. }
  3464. if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
  3465. fw_ddb_entry_dma, ddb_index)) {
  3466. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
  3467. "index [%d]\n", __func__, ddb_index));
  3468. ret = QLA_ERROR;
  3469. goto exit_boot_target;
  3470. }
  3471. /* Update target name and IP from DDB */
  3472. memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
  3473. min(sizeof(boot_sess->target_name),
  3474. sizeof(fw_ddb_entry->iscsi_name)));
  3475. options = le16_to_cpu(fw_ddb_entry->options);
  3476. if (options & DDB_OPT_IPV6_DEVICE) {
  3477. memcpy(&boot_conn->dest_ipaddr.ip_address,
  3478. &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
  3479. } else {
  3480. boot_conn->dest_ipaddr.ip_type = 0x1;
  3481. memcpy(&boot_conn->dest_ipaddr.ip_address,
  3482. &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
  3483. }
  3484. boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
  3485. /* update chap information */
  3486. idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  3487. if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  3488. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
  3489. ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
  3490. target_chap_name,
  3491. (char *)&boot_conn->chap.target_secret,
  3492. idx);
  3493. if (ret) {
  3494. ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
  3495. ret = QLA_ERROR;
  3496. goto exit_boot_target;
  3497. }
  3498. boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  3499. boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  3500. }
  3501. if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  3502. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
  3503. ret = qla4xxx_get_bidi_chap(ha,
  3504. (char *)&boot_conn->chap.intr_chap_name,
  3505. (char *)&boot_conn->chap.intr_secret);
  3506. if (ret) {
  3507. ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
  3508. ret = QLA_ERROR;
  3509. goto exit_boot_target;
  3510. }
  3511. boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  3512. boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  3513. }
  3514. exit_boot_target:
  3515. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  3516. fw_ddb_entry, fw_ddb_entry_dma);
  3517. return ret;
  3518. }
  3519. static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
  3520. {
  3521. uint16_t ddb_index[2];
  3522. int ret = QLA_ERROR;
  3523. int rval;
  3524. memset(ddb_index, 0, sizeof(ddb_index));
  3525. ddb_index[0] = 0xffff;
  3526. ddb_index[1] = 0xffff;
  3527. ret = get_fw_boot_info(ha, ddb_index);
  3528. if (ret != QLA_SUCCESS) {
  3529. DEBUG2(ql4_printk(KERN_INFO, ha,
  3530. "%s: No boot target configured.\n", __func__));
  3531. return ret;
  3532. }
  3533. if (ql4xdisablesysfsboot)
  3534. return QLA_SUCCESS;
  3535. if (ddb_index[0] == 0xffff)
  3536. goto sec_target;
  3537. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
  3538. ddb_index[0]);
  3539. if (rval != QLA_SUCCESS) {
  3540. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
  3541. "configured\n", __func__));
  3542. } else
  3543. ret = QLA_SUCCESS;
  3544. sec_target:
  3545. if (ddb_index[1] == 0xffff)
  3546. goto exit_get_boot_info;
  3547. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
  3548. ddb_index[1]);
  3549. if (rval != QLA_SUCCESS) {
  3550. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
  3551. " configured\n", __func__));
  3552. } else
  3553. ret = QLA_SUCCESS;
  3554. exit_get_boot_info:
  3555. return ret;
  3556. }
  3557. static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
  3558. {
  3559. struct iscsi_boot_kobj *boot_kobj;
  3560. if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
  3561. return QLA_ERROR;
  3562. if (ql4xdisablesysfsboot) {
  3563. ql4_printk(KERN_INFO, ha,
  3564. "%s: syfsboot disabled - driver will trigger login "
  3565. "and publish session for discovery .\n", __func__);
  3566. return QLA_SUCCESS;
  3567. }
  3568. ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
  3569. if (!ha->boot_kset)
  3570. goto kset_free;
  3571. if (!scsi_host_get(ha->host))
  3572. goto kset_free;
  3573. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
  3574. qla4xxx_show_boot_tgt_pri_info,
  3575. qla4xxx_tgt_get_attr_visibility,
  3576. qla4xxx_boot_release);
  3577. if (!boot_kobj)
  3578. goto put_host;
  3579. if (!scsi_host_get(ha->host))
  3580. goto kset_free;
  3581. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
  3582. qla4xxx_show_boot_tgt_sec_info,
  3583. qla4xxx_tgt_get_attr_visibility,
  3584. qla4xxx_boot_release);
  3585. if (!boot_kobj)
  3586. goto put_host;
  3587. if (!scsi_host_get(ha->host))
  3588. goto kset_free;
  3589. boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
  3590. qla4xxx_show_boot_ini_info,
  3591. qla4xxx_ini_get_attr_visibility,
  3592. qla4xxx_boot_release);
  3593. if (!boot_kobj)
  3594. goto put_host;
  3595. if (!scsi_host_get(ha->host))
  3596. goto kset_free;
  3597. boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
  3598. qla4xxx_show_boot_eth_info,
  3599. qla4xxx_eth_get_attr_visibility,
  3600. qla4xxx_boot_release);
  3601. if (!boot_kobj)
  3602. goto put_host;
  3603. return QLA_SUCCESS;
  3604. put_host:
  3605. scsi_host_put(ha->host);
  3606. kset_free:
  3607. iscsi_boot_destroy_kset(ha->boot_kset);
  3608. return -ENOMEM;
  3609. }
  3610. /**
  3611. * qla4xxx_create chap_list - Create CHAP list from FLASH
  3612. * @ha: pointer to adapter structure
  3613. *
  3614. * Read flash and make a list of CHAP entries, during login when a CHAP entry
  3615. * is received, it will be checked in this list. If entry exist then the CHAP
  3616. * entry index is set in the DDB. If CHAP entry does not exist in this list
  3617. * then a new entry is added in FLASH in CHAP table and the index obtained is
  3618. * used in the DDB.
  3619. **/
  3620. static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
  3621. {
  3622. int rval = 0;
  3623. uint8_t *chap_flash_data = NULL;
  3624. uint32_t offset;
  3625. dma_addr_t chap_dma;
  3626. uint32_t chap_size = 0;
  3627. if (is_qla40XX(ha))
  3628. chap_size = MAX_CHAP_ENTRIES_40XX *
  3629. sizeof(struct ql4_chap_table);
  3630. else /* Single region contains CHAP info for both
  3631. * ports which is divided into half for each port.
  3632. */
  3633. chap_size = ha->hw.flt_chap_size / 2;
  3634. chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
  3635. &chap_dma, GFP_KERNEL);
  3636. if (!chap_flash_data) {
  3637. ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
  3638. return;
  3639. }
  3640. if (is_qla40XX(ha))
  3641. offset = FLASH_CHAP_OFFSET;
  3642. else {
  3643. offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
  3644. if (ha->port_num == 1)
  3645. offset += chap_size;
  3646. }
  3647. rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
  3648. if (rval != QLA_SUCCESS)
  3649. goto exit_chap_list;
  3650. if (ha->chap_list == NULL)
  3651. ha->chap_list = vmalloc(chap_size);
  3652. if (ha->chap_list == NULL) {
  3653. ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
  3654. goto exit_chap_list;
  3655. }
  3656. memcpy(ha->chap_list, chap_flash_data, chap_size);
  3657. exit_chap_list:
  3658. dma_free_coherent(&ha->pdev->dev, chap_size,
  3659. chap_flash_data, chap_dma);
  3660. }
  3661. static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
  3662. struct ql4_tuple_ddb *tddb)
  3663. {
  3664. struct scsi_qla_host *ha;
  3665. struct iscsi_cls_session *cls_sess;
  3666. struct iscsi_cls_conn *cls_conn;
  3667. struct iscsi_session *sess;
  3668. struct iscsi_conn *conn;
  3669. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  3670. ha = ddb_entry->ha;
  3671. cls_sess = ddb_entry->sess;
  3672. sess = cls_sess->dd_data;
  3673. cls_conn = ddb_entry->conn;
  3674. conn = cls_conn->dd_data;
  3675. tddb->tpgt = sess->tpgt;
  3676. tddb->port = conn->persistent_port;
  3677. strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
  3678. strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
  3679. }
  3680. static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
  3681. struct ql4_tuple_ddb *tddb)
  3682. {
  3683. uint16_t options = 0;
  3684. tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  3685. memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
  3686. min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
  3687. options = le16_to_cpu(fw_ddb_entry->options);
  3688. if (options & DDB_OPT_IPV6_DEVICE)
  3689. sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  3690. else
  3691. sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  3692. tddb->port = le16_to_cpu(fw_ddb_entry->port);
  3693. memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0], sizeof(tddb->isid));
  3694. }
  3695. static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
  3696. struct ql4_tuple_ddb *old_tddb,
  3697. struct ql4_tuple_ddb *new_tddb,
  3698. uint8_t is_isid_compare)
  3699. {
  3700. if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
  3701. return QLA_ERROR;
  3702. if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
  3703. return QLA_ERROR;
  3704. if (old_tddb->port != new_tddb->port)
  3705. return QLA_ERROR;
  3706. /* For multi sessions, driver generates the ISID, so do not compare
  3707. * ISID in reset path since it would be a comparision between the
  3708. * driver generated ISID and firmware generated ISID. This could
  3709. * lead to adding duplicated DDBs in the list as driver generated
  3710. * ISID would not match firmware generated ISID.
  3711. */
  3712. if (is_isid_compare) {
  3713. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: old ISID [%02x%02x%02x"
  3714. "%02x%02x%02x] New ISID [%02x%02x%02x%02x%02x%02x]\n",
  3715. __func__, old_tddb->isid[5], old_tddb->isid[4],
  3716. old_tddb->isid[3], old_tddb->isid[2], old_tddb->isid[1],
  3717. old_tddb->isid[0], new_tddb->isid[5], new_tddb->isid[4],
  3718. new_tddb->isid[3], new_tddb->isid[2], new_tddb->isid[1],
  3719. new_tddb->isid[0]));
  3720. if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
  3721. sizeof(old_tddb->isid)))
  3722. return QLA_ERROR;
  3723. }
  3724. DEBUG2(ql4_printk(KERN_INFO, ha,
  3725. "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
  3726. old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
  3727. old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
  3728. new_tddb->ip_addr, new_tddb->iscsi_name));
  3729. return QLA_SUCCESS;
  3730. }
  3731. static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
  3732. struct dev_db_entry *fw_ddb_entry)
  3733. {
  3734. struct ddb_entry *ddb_entry;
  3735. struct ql4_tuple_ddb *fw_tddb = NULL;
  3736. struct ql4_tuple_ddb *tmp_tddb = NULL;
  3737. int idx;
  3738. int ret = QLA_ERROR;
  3739. fw_tddb = vzalloc(sizeof(*fw_tddb));
  3740. if (!fw_tddb) {
  3741. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3742. "Memory Allocation failed.\n"));
  3743. ret = QLA_SUCCESS;
  3744. goto exit_check;
  3745. }
  3746. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  3747. if (!tmp_tddb) {
  3748. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3749. "Memory Allocation failed.\n"));
  3750. ret = QLA_SUCCESS;
  3751. goto exit_check;
  3752. }
  3753. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
  3754. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  3755. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  3756. if (ddb_entry == NULL)
  3757. continue;
  3758. qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
  3759. if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
  3760. ret = QLA_SUCCESS; /* found */
  3761. goto exit_check;
  3762. }
  3763. }
  3764. exit_check:
  3765. if (fw_tddb)
  3766. vfree(fw_tddb);
  3767. if (tmp_tddb)
  3768. vfree(tmp_tddb);
  3769. return ret;
  3770. }
  3771. static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
  3772. struct list_head *list_nt,
  3773. struct dev_db_entry *fw_ddb_entry)
  3774. {
  3775. struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
  3776. struct ql4_tuple_ddb *fw_tddb = NULL;
  3777. struct ql4_tuple_ddb *tmp_tddb = NULL;
  3778. int ret = QLA_ERROR;
  3779. fw_tddb = vzalloc(sizeof(*fw_tddb));
  3780. if (!fw_tddb) {
  3781. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3782. "Memory Allocation failed.\n"));
  3783. ret = QLA_SUCCESS;
  3784. goto exit_check;
  3785. }
  3786. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  3787. if (!tmp_tddb) {
  3788. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3789. "Memory Allocation failed.\n"));
  3790. ret = QLA_SUCCESS;
  3791. goto exit_check;
  3792. }
  3793. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
  3794. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  3795. qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
  3796. if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true)) {
  3797. ret = QLA_SUCCESS; /* found */
  3798. goto exit_check;
  3799. }
  3800. }
  3801. exit_check:
  3802. if (fw_tddb)
  3803. vfree(fw_tddb);
  3804. if (tmp_tddb)
  3805. vfree(tmp_tddb);
  3806. return ret;
  3807. }
  3808. static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
  3809. {
  3810. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  3811. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
  3812. list_del_init(&ddb_idx->list);
  3813. vfree(ddb_idx);
  3814. }
  3815. }
  3816. static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
  3817. struct dev_db_entry *fw_ddb_entry)
  3818. {
  3819. struct iscsi_endpoint *ep;
  3820. struct sockaddr_in *addr;
  3821. struct sockaddr_in6 *addr6;
  3822. struct sockaddr *dst_addr;
  3823. char *ip;
  3824. /* TODO: need to destroy on unload iscsi_endpoint*/
  3825. dst_addr = vmalloc(sizeof(*dst_addr));
  3826. if (!dst_addr)
  3827. return NULL;
  3828. if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
  3829. dst_addr->sa_family = AF_INET6;
  3830. addr6 = (struct sockaddr_in6 *)dst_addr;
  3831. ip = (char *)&addr6->sin6_addr;
  3832. memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
  3833. addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
  3834. } else {
  3835. dst_addr->sa_family = AF_INET;
  3836. addr = (struct sockaddr_in *)dst_addr;
  3837. ip = (char *)&addr->sin_addr;
  3838. memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
  3839. addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
  3840. }
  3841. ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
  3842. vfree(dst_addr);
  3843. return ep;
  3844. }
  3845. static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
  3846. {
  3847. if (ql4xdisablesysfsboot)
  3848. return QLA_SUCCESS;
  3849. if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
  3850. return QLA_ERROR;
  3851. return QLA_SUCCESS;
  3852. }
  3853. static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
  3854. struct ddb_entry *ddb_entry)
  3855. {
  3856. uint16_t def_timeout;
  3857. ddb_entry->ddb_type = FLASH_DDB;
  3858. ddb_entry->fw_ddb_index = INVALID_ENTRY;
  3859. ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  3860. ddb_entry->ha = ha;
  3861. ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
  3862. ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
  3863. atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  3864. atomic_set(&ddb_entry->relogin_timer, 0);
  3865. atomic_set(&ddb_entry->relogin_retry_count, 0);
  3866. def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
  3867. ddb_entry->default_relogin_timeout =
  3868. (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
  3869. def_timeout : LOGIN_TOV;
  3870. ddb_entry->default_time2wait =
  3871. le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
  3872. }
  3873. static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
  3874. {
  3875. uint32_t idx = 0;
  3876. uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
  3877. uint32_t sts[MBOX_REG_COUNT];
  3878. uint32_t ip_state;
  3879. unsigned long wtime;
  3880. int ret;
  3881. wtime = jiffies + (HZ * IP_CONFIG_TOV);
  3882. do {
  3883. for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
  3884. if (ip_idx[idx] == -1)
  3885. continue;
  3886. ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
  3887. if (ret == QLA_ERROR) {
  3888. ip_idx[idx] = -1;
  3889. continue;
  3890. }
  3891. ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
  3892. DEBUG2(ql4_printk(KERN_INFO, ha,
  3893. "Waiting for IP state for idx = %d, state = 0x%x\n",
  3894. ip_idx[idx], ip_state));
  3895. if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
  3896. ip_state == IP_ADDRSTATE_INVALID ||
  3897. ip_state == IP_ADDRSTATE_PREFERRED ||
  3898. ip_state == IP_ADDRSTATE_DEPRICATED ||
  3899. ip_state == IP_ADDRSTATE_DISABLING)
  3900. ip_idx[idx] = -1;
  3901. }
  3902. /* Break if all IP states checked */
  3903. if ((ip_idx[0] == -1) &&
  3904. (ip_idx[1] == -1) &&
  3905. (ip_idx[2] == -1) &&
  3906. (ip_idx[3] == -1))
  3907. break;
  3908. schedule_timeout_uninterruptible(HZ);
  3909. } while (time_after(wtime, jiffies));
  3910. }
  3911. static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
  3912. struct list_head *list_st)
  3913. {
  3914. struct qla_ddb_index *st_ddb_idx;
  3915. int max_ddbs;
  3916. int fw_idx_size;
  3917. struct dev_db_entry *fw_ddb_entry;
  3918. dma_addr_t fw_ddb_dma;
  3919. int ret;
  3920. uint32_t idx = 0, next_idx = 0;
  3921. uint32_t state = 0, conn_err = 0;
  3922. uint16_t conn_id = 0;
  3923. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  3924. &fw_ddb_dma);
  3925. if (fw_ddb_entry == NULL) {
  3926. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  3927. goto exit_st_list;
  3928. }
  3929. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  3930. MAX_DEV_DB_ENTRIES;
  3931. fw_idx_size = sizeof(struct qla_ddb_index);
  3932. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  3933. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
  3934. NULL, &next_idx, &state,
  3935. &conn_err, NULL, &conn_id);
  3936. if (ret == QLA_ERROR)
  3937. break;
  3938. /* Ignore DDB if invalid state (unassigned) */
  3939. if (state == DDB_DS_UNASSIGNED)
  3940. goto continue_next_st;
  3941. /* Check if ST, add to the list_st */
  3942. if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
  3943. goto continue_next_st;
  3944. st_ddb_idx = vzalloc(fw_idx_size);
  3945. if (!st_ddb_idx)
  3946. break;
  3947. st_ddb_idx->fw_ddb_idx = idx;
  3948. list_add_tail(&st_ddb_idx->list, list_st);
  3949. continue_next_st:
  3950. if (next_idx == 0)
  3951. break;
  3952. }
  3953. exit_st_list:
  3954. if (fw_ddb_entry)
  3955. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  3956. }
  3957. /**
  3958. * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
  3959. * @ha: pointer to adapter structure
  3960. * @list_ddb: List from which failed ddb to be removed
  3961. *
  3962. * Iterate over the list of DDBs and find and remove DDBs that are either in
  3963. * no connection active state or failed state
  3964. **/
  3965. static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
  3966. struct list_head *list_ddb)
  3967. {
  3968. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  3969. uint32_t next_idx = 0;
  3970. uint32_t state = 0, conn_err = 0;
  3971. int ret;
  3972. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
  3973. ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
  3974. NULL, 0, NULL, &next_idx, &state,
  3975. &conn_err, NULL, NULL);
  3976. if (ret == QLA_ERROR)
  3977. continue;
  3978. if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  3979. state == DDB_DS_SESSION_FAILED) {
  3980. list_del_init(&ddb_idx->list);
  3981. vfree(ddb_idx);
  3982. }
  3983. }
  3984. }
  3985. static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
  3986. struct dev_db_entry *fw_ddb_entry,
  3987. int is_reset)
  3988. {
  3989. struct iscsi_cls_session *cls_sess;
  3990. struct iscsi_session *sess;
  3991. struct iscsi_cls_conn *cls_conn;
  3992. struct iscsi_endpoint *ep;
  3993. uint16_t cmds_max = 32;
  3994. uint16_t conn_id = 0;
  3995. uint32_t initial_cmdsn = 0;
  3996. int ret = QLA_SUCCESS;
  3997. struct ddb_entry *ddb_entry = NULL;
  3998. /* Create session object, with INVALID_ENTRY,
  3999. * the targer_id would get set when we issue the login
  4000. */
  4001. cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
  4002. cmds_max, sizeof(struct ddb_entry),
  4003. sizeof(struct ql4_task_data),
  4004. initial_cmdsn, INVALID_ENTRY);
  4005. if (!cls_sess) {
  4006. ret = QLA_ERROR;
  4007. goto exit_setup;
  4008. }
  4009. /*
  4010. * so calling module_put function to decrement the
  4011. * reference count.
  4012. **/
  4013. module_put(qla4xxx_iscsi_transport.owner);
  4014. sess = cls_sess->dd_data;
  4015. ddb_entry = sess->dd_data;
  4016. ddb_entry->sess = cls_sess;
  4017. cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  4018. memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
  4019. sizeof(struct dev_db_entry));
  4020. qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
  4021. cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
  4022. if (!cls_conn) {
  4023. ret = QLA_ERROR;
  4024. goto exit_setup;
  4025. }
  4026. ddb_entry->conn = cls_conn;
  4027. /* Setup ep, for displaying attributes in sysfs */
  4028. ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
  4029. if (ep) {
  4030. ep->conn = cls_conn;
  4031. cls_conn->ep = ep;
  4032. } else {
  4033. DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
  4034. ret = QLA_ERROR;
  4035. goto exit_setup;
  4036. }
  4037. /* Update sess/conn params */
  4038. qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
  4039. if (is_reset == RESET_ADAPTER) {
  4040. iscsi_block_session(cls_sess);
  4041. /* Use the relogin path to discover new devices
  4042. * by short-circuting the logic of setting
  4043. * timer to relogin - instead set the flags
  4044. * to initiate login right away.
  4045. */
  4046. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  4047. set_bit(DF_RELOGIN, &ddb_entry->flags);
  4048. }
  4049. exit_setup:
  4050. return ret;
  4051. }
  4052. static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
  4053. struct list_head *list_nt, int is_reset)
  4054. {
  4055. struct dev_db_entry *fw_ddb_entry;
  4056. dma_addr_t fw_ddb_dma;
  4057. int max_ddbs;
  4058. int fw_idx_size;
  4059. int ret;
  4060. uint32_t idx = 0, next_idx = 0;
  4061. uint32_t state = 0, conn_err = 0;
  4062. uint16_t conn_id = 0;
  4063. struct qla_ddb_index *nt_ddb_idx;
  4064. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  4065. &fw_ddb_dma);
  4066. if (fw_ddb_entry == NULL) {
  4067. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  4068. goto exit_nt_list;
  4069. }
  4070. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  4071. MAX_DEV_DB_ENTRIES;
  4072. fw_idx_size = sizeof(struct qla_ddb_index);
  4073. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  4074. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
  4075. NULL, &next_idx, &state,
  4076. &conn_err, NULL, &conn_id);
  4077. if (ret == QLA_ERROR)
  4078. break;
  4079. if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
  4080. goto continue_next_nt;
  4081. /* Check if NT, then add to list it */
  4082. if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
  4083. goto continue_next_nt;
  4084. if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
  4085. state == DDB_DS_SESSION_FAILED))
  4086. goto continue_next_nt;
  4087. DEBUG2(ql4_printk(KERN_INFO, ha,
  4088. "Adding DDB to session = 0x%x\n", idx));
  4089. if (is_reset == INIT_ADAPTER) {
  4090. nt_ddb_idx = vmalloc(fw_idx_size);
  4091. if (!nt_ddb_idx)
  4092. break;
  4093. nt_ddb_idx->fw_ddb_idx = idx;
  4094. memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
  4095. sizeof(struct dev_db_entry));
  4096. if (qla4xxx_is_flash_ddb_exists(ha, list_nt,
  4097. fw_ddb_entry) == QLA_SUCCESS) {
  4098. vfree(nt_ddb_idx);
  4099. goto continue_next_nt;
  4100. }
  4101. list_add_tail(&nt_ddb_idx->list, list_nt);
  4102. } else if (is_reset == RESET_ADAPTER) {
  4103. if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
  4104. QLA_SUCCESS)
  4105. goto continue_next_nt;
  4106. }
  4107. ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
  4108. if (ret == QLA_ERROR)
  4109. goto exit_nt_list;
  4110. continue_next_nt:
  4111. if (next_idx == 0)
  4112. break;
  4113. }
  4114. exit_nt_list:
  4115. if (fw_ddb_entry)
  4116. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  4117. }
  4118. /**
  4119. * qla4xxx_build_ddb_list - Build ddb list and setup sessions
  4120. * @ha: pointer to adapter structure
  4121. * @is_reset: Is this init path or reset path
  4122. *
  4123. * Create a list of sendtargets (st) from firmware DDBs, issue send targets
  4124. * using connection open, then create the list of normal targets (nt)
  4125. * from firmware DDBs. Based on the list of nt setup session and connection
  4126. * objects.
  4127. **/
  4128. void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
  4129. {
  4130. uint16_t tmo = 0;
  4131. struct list_head list_st, list_nt;
  4132. struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
  4133. unsigned long wtime;
  4134. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  4135. set_bit(AF_BUILD_DDB_LIST, &ha->flags);
  4136. ha->is_reset = is_reset;
  4137. return;
  4138. }
  4139. INIT_LIST_HEAD(&list_st);
  4140. INIT_LIST_HEAD(&list_nt);
  4141. qla4xxx_build_st_list(ha, &list_st);
  4142. /* Before issuing conn open mbox, ensure all IPs states are configured
  4143. * Note, conn open fails if IPs are not configured
  4144. */
  4145. qla4xxx_wait_for_ip_configuration(ha);
  4146. /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
  4147. list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
  4148. qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
  4149. }
  4150. /* Wait to ensure all sendtargets are done for min 12 sec wait */
  4151. tmo = ((ha->def_timeout > LOGIN_TOV) &&
  4152. (ha->def_timeout < LOGIN_TOV * 10) ?
  4153. ha->def_timeout : LOGIN_TOV);
  4154. DEBUG2(ql4_printk(KERN_INFO, ha,
  4155. "Default time to wait for build ddb %d\n", tmo));
  4156. wtime = jiffies + (HZ * tmo);
  4157. do {
  4158. if (list_empty(&list_st))
  4159. break;
  4160. qla4xxx_remove_failed_ddb(ha, &list_st);
  4161. schedule_timeout_uninterruptible(HZ / 10);
  4162. } while (time_after(wtime, jiffies));
  4163. /* Free up the sendtargets list */
  4164. qla4xxx_free_ddb_list(&list_st);
  4165. qla4xxx_build_nt_list(ha, &list_nt, is_reset);
  4166. qla4xxx_free_ddb_list(&list_nt);
  4167. qla4xxx_free_ddb_index(ha);
  4168. }
  4169. /**
  4170. * qla4xxx_probe_adapter - callback function to probe HBA
  4171. * @pdev: pointer to pci_dev structure
  4172. * @pci_device_id: pointer to pci_device entry
  4173. *
  4174. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  4175. * It returns zero if successful. It also initializes all data necessary for
  4176. * the driver.
  4177. **/
  4178. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  4179. const struct pci_device_id *ent)
  4180. {
  4181. int ret = -ENODEV, status;
  4182. struct Scsi_Host *host;
  4183. struct scsi_qla_host *ha;
  4184. uint8_t init_retry_count = 0;
  4185. char buf[34];
  4186. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  4187. uint32_t dev_state;
  4188. if (pci_enable_device(pdev))
  4189. return -1;
  4190. host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
  4191. if (host == NULL) {
  4192. printk(KERN_WARNING
  4193. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  4194. goto probe_disable_device;
  4195. }
  4196. /* Clear our data area */
  4197. ha = to_qla_host(host);
  4198. memset(ha, 0, sizeof(*ha));
  4199. /* Save the information from PCI BIOS. */
  4200. ha->pdev = pdev;
  4201. ha->host = host;
  4202. ha->host_no = host->host_no;
  4203. pci_enable_pcie_error_reporting(pdev);
  4204. /* Setup Runtime configurable options */
  4205. if (is_qla8022(ha)) {
  4206. ha->isp_ops = &qla4_8xxx_isp_ops;
  4207. rwlock_init(&ha->hw_lock);
  4208. ha->qdr_sn_window = -1;
  4209. ha->ddr_mn_window = -1;
  4210. ha->curr_window = 255;
  4211. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  4212. nx_legacy_intr = &legacy_intr[ha->func_num];
  4213. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  4214. ha->nx_legacy_intr.tgt_status_reg =
  4215. nx_legacy_intr->tgt_status_reg;
  4216. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  4217. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  4218. } else {
  4219. ha->isp_ops = &qla4xxx_isp_ops;
  4220. }
  4221. /* Set EEH reset type to fundamental if required by hba */
  4222. if (is_qla8022(ha))
  4223. pdev->needs_freset = 1;
  4224. /* Configure PCI I/O space. */
  4225. ret = ha->isp_ops->iospace_config(ha);
  4226. if (ret)
  4227. goto probe_failed_ioconfig;
  4228. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  4229. pdev->device, pdev->irq, ha->reg);
  4230. qla4xxx_config_dma_addressing(ha);
  4231. /* Initialize lists and spinlocks. */
  4232. INIT_LIST_HEAD(&ha->free_srb_q);
  4233. mutex_init(&ha->mbox_sem);
  4234. mutex_init(&ha->chap_sem);
  4235. init_completion(&ha->mbx_intr_comp);
  4236. init_completion(&ha->disable_acb_comp);
  4237. spin_lock_init(&ha->hardware_lock);
  4238. /* Initialize work list */
  4239. INIT_LIST_HEAD(&ha->work_list);
  4240. /* Allocate dma buffers */
  4241. if (qla4xxx_mem_alloc(ha)) {
  4242. ql4_printk(KERN_WARNING, ha,
  4243. "[ERROR] Failed to allocate memory for adapter\n");
  4244. ret = -ENOMEM;
  4245. goto probe_failed;
  4246. }
  4247. host->cmd_per_lun = 3;
  4248. host->max_channel = 0;
  4249. host->max_lun = MAX_LUNS - 1;
  4250. host->max_id = MAX_TARGETS;
  4251. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  4252. host->can_queue = MAX_SRBS ;
  4253. host->transportt = qla4xxx_scsi_transport;
  4254. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  4255. if (ret) {
  4256. ql4_printk(KERN_WARNING, ha,
  4257. "%s: scsi_init_shared_tag_map failed\n", __func__);
  4258. goto probe_failed;
  4259. }
  4260. pci_set_drvdata(pdev, ha);
  4261. ret = scsi_add_host(host, &pdev->dev);
  4262. if (ret)
  4263. goto probe_failed;
  4264. if (is_qla8022(ha))
  4265. (void) qla4_8xxx_get_flash_info(ha);
  4266. /*
  4267. * Initialize the Host adapter request/response queues and
  4268. * firmware
  4269. * NOTE: interrupts enabled upon successful completion
  4270. */
  4271. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  4272. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  4273. init_retry_count++ < MAX_INIT_RETRIES) {
  4274. if (is_qla8022(ha)) {
  4275. qla4_8xxx_idc_lock(ha);
  4276. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  4277. qla4_8xxx_idc_unlock(ha);
  4278. if (dev_state == QLA82XX_DEV_FAILED) {
  4279. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  4280. "initialize adapter. H/W is in failed state\n",
  4281. __func__);
  4282. break;
  4283. }
  4284. }
  4285. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  4286. "(%d)\n", __func__, init_retry_count));
  4287. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  4288. continue;
  4289. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  4290. }
  4291. if (!test_bit(AF_ONLINE, &ha->flags)) {
  4292. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  4293. if (is_qla8022(ha) && ql4xdontresethba) {
  4294. /* Put the device in failed state. */
  4295. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  4296. qla4_8xxx_idc_lock(ha);
  4297. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  4298. QLA82XX_DEV_FAILED);
  4299. qla4_8xxx_idc_unlock(ha);
  4300. }
  4301. ret = -ENODEV;
  4302. goto remove_host;
  4303. }
  4304. /* Startup the kernel thread for this host adapter. */
  4305. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  4306. "qla4xxx_dpc\n", __func__));
  4307. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  4308. ha->dpc_thread = create_singlethread_workqueue(buf);
  4309. if (!ha->dpc_thread) {
  4310. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  4311. ret = -ENODEV;
  4312. goto remove_host;
  4313. }
  4314. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  4315. sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
  4316. ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
  4317. if (!ha->task_wq) {
  4318. ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
  4319. ret = -ENODEV;
  4320. goto remove_host;
  4321. }
  4322. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  4323. * (which is called indirectly by qla4xxx_initialize_adapter),
  4324. * so that irqs will be registered after crbinit but before
  4325. * mbx_intr_enable.
  4326. */
  4327. if (!is_qla8022(ha)) {
  4328. ret = qla4xxx_request_irqs(ha);
  4329. if (ret) {
  4330. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  4331. "interrupt %d already in use.\n", pdev->irq);
  4332. goto remove_host;
  4333. }
  4334. }
  4335. pci_save_state(ha->pdev);
  4336. ha->isp_ops->enable_intrs(ha);
  4337. /* Start timer thread. */
  4338. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  4339. set_bit(AF_INIT_DONE, &ha->flags);
  4340. printk(KERN_INFO
  4341. " QLogic iSCSI HBA Driver version: %s\n"
  4342. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  4343. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  4344. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  4345. ha->patch_number, ha->build_number);
  4346. if (qla4xxx_setup_boot_info(ha))
  4347. ql4_printk(KERN_ERR, ha,
  4348. "%s: No iSCSI boot target configured\n", __func__);
  4349. /* Perform the build ddb list and login to each */
  4350. qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
  4351. iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
  4352. qla4xxx_create_chap_list(ha);
  4353. qla4xxx_create_ifaces(ha);
  4354. return 0;
  4355. remove_host:
  4356. scsi_remove_host(ha->host);
  4357. probe_failed:
  4358. qla4xxx_free_adapter(ha);
  4359. probe_failed_ioconfig:
  4360. pci_disable_pcie_error_reporting(pdev);
  4361. scsi_host_put(ha->host);
  4362. probe_disable_device:
  4363. pci_disable_device(pdev);
  4364. return ret;
  4365. }
  4366. /**
  4367. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  4368. * @ha: pointer to adapter structure
  4369. *
  4370. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  4371. * so that the other port will not re-initialize while in the process of
  4372. * removing the ha due to driver unload or hba hotplug.
  4373. **/
  4374. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  4375. {
  4376. struct scsi_qla_host *other_ha = NULL;
  4377. struct pci_dev *other_pdev = NULL;
  4378. int fn = ISP4XXX_PCI_FN_2;
  4379. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  4380. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  4381. fn = ISP4XXX_PCI_FN_1;
  4382. other_pdev =
  4383. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  4384. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  4385. fn));
  4386. /* Get other_ha if other_pdev is valid and state is enable*/
  4387. if (other_pdev) {
  4388. if (atomic_read(&other_pdev->enable_cnt)) {
  4389. other_ha = pci_get_drvdata(other_pdev);
  4390. if (other_ha) {
  4391. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  4392. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  4393. "Prevent %s reinit\n", __func__,
  4394. dev_name(&other_ha->pdev->dev)));
  4395. }
  4396. }
  4397. pci_dev_put(other_pdev);
  4398. }
  4399. }
  4400. static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
  4401. {
  4402. struct ddb_entry *ddb_entry;
  4403. int options;
  4404. int idx;
  4405. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  4406. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  4407. if ((ddb_entry != NULL) &&
  4408. (ddb_entry->ddb_type == FLASH_DDB)) {
  4409. options = LOGOUT_OPTION_CLOSE_SESSION;
  4410. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
  4411. == QLA_ERROR)
  4412. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
  4413. __func__);
  4414. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  4415. /*
  4416. * we have decremented the reference count of the driver
  4417. * when we setup the session to have the driver unload
  4418. * to be seamless without actually destroying the
  4419. * session
  4420. **/
  4421. try_module_get(qla4xxx_iscsi_transport.owner);
  4422. iscsi_destroy_endpoint(ddb_entry->conn->ep);
  4423. qla4xxx_free_ddb(ha, ddb_entry);
  4424. iscsi_session_teardown(ddb_entry->sess);
  4425. }
  4426. }
  4427. }
  4428. /**
  4429. * qla4xxx_remove_adapter - calback function to remove adapter.
  4430. * @pci_dev: PCI device pointer
  4431. **/
  4432. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  4433. {
  4434. struct scsi_qla_host *ha;
  4435. ha = pci_get_drvdata(pdev);
  4436. if (!is_qla8022(ha))
  4437. qla4xxx_prevent_other_port_reinit(ha);
  4438. /* destroy iface from sysfs */
  4439. qla4xxx_destroy_ifaces(ha);
  4440. if ((!ql4xdisablesysfsboot) && ha->boot_kset)
  4441. iscsi_boot_destroy_kset(ha->boot_kset);
  4442. qla4xxx_destroy_fw_ddb_session(ha);
  4443. scsi_remove_host(ha->host);
  4444. qla4xxx_free_adapter(ha);
  4445. scsi_host_put(ha->host);
  4446. pci_disable_pcie_error_reporting(pdev);
  4447. pci_disable_device(pdev);
  4448. pci_set_drvdata(pdev, NULL);
  4449. }
  4450. /**
  4451. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  4452. * @ha: HA context
  4453. *
  4454. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  4455. * supported addressing method.
  4456. */
  4457. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  4458. {
  4459. int retval;
  4460. /* Update our PCI device dma_mask for full 64 bit mask */
  4461. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  4462. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  4463. dev_dbg(&ha->pdev->dev,
  4464. "Failed to set 64 bit PCI consistent mask; "
  4465. "using 32 bit.\n");
  4466. retval = pci_set_consistent_dma_mask(ha->pdev,
  4467. DMA_BIT_MASK(32));
  4468. }
  4469. } else
  4470. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  4471. }
  4472. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  4473. {
  4474. struct iscsi_cls_session *cls_sess;
  4475. struct iscsi_session *sess;
  4476. struct ddb_entry *ddb;
  4477. int queue_depth = QL4_DEF_QDEPTH;
  4478. cls_sess = starget_to_session(sdev->sdev_target);
  4479. sess = cls_sess->dd_data;
  4480. ddb = sess->dd_data;
  4481. sdev->hostdata = ddb;
  4482. sdev->tagged_supported = 1;
  4483. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  4484. queue_depth = ql4xmaxqdepth;
  4485. scsi_activate_tcq(sdev, queue_depth);
  4486. return 0;
  4487. }
  4488. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  4489. {
  4490. sdev->tagged_supported = 1;
  4491. return 0;
  4492. }
  4493. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  4494. {
  4495. scsi_deactivate_tcq(sdev, 1);
  4496. }
  4497. /**
  4498. * qla4xxx_del_from_active_array - returns an active srb
  4499. * @ha: Pointer to host adapter structure.
  4500. * @index: index into the active_array
  4501. *
  4502. * This routine removes and returns the srb at the specified index
  4503. **/
  4504. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  4505. uint32_t index)
  4506. {
  4507. struct srb *srb = NULL;
  4508. struct scsi_cmnd *cmd = NULL;
  4509. cmd = scsi_host_find_tag(ha->host, index);
  4510. if (!cmd)
  4511. return srb;
  4512. srb = (struct srb *)CMD_SP(cmd);
  4513. if (!srb)
  4514. return srb;
  4515. /* update counters */
  4516. if (srb->flags & SRB_DMA_VALID) {
  4517. ha->req_q_count += srb->iocb_cnt;
  4518. ha->iocb_cnt -= srb->iocb_cnt;
  4519. if (srb->cmd)
  4520. srb->cmd->host_scribble =
  4521. (unsigned char *)(unsigned long) MAX_SRBS;
  4522. }
  4523. return srb;
  4524. }
  4525. /**
  4526. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  4527. * @ha: Pointer to host adapter structure.
  4528. * @cmd: Scsi Command to wait on.
  4529. *
  4530. * This routine waits for the command to be returned by the Firmware
  4531. * for some max time.
  4532. **/
  4533. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  4534. struct scsi_cmnd *cmd)
  4535. {
  4536. int done = 0;
  4537. struct srb *rp;
  4538. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  4539. int ret = SUCCESS;
  4540. /* Dont wait on command if PCI error is being handled
  4541. * by PCI AER driver
  4542. */
  4543. if (unlikely(pci_channel_offline(ha->pdev)) ||
  4544. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  4545. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  4546. ha->host_no, __func__);
  4547. return ret;
  4548. }
  4549. do {
  4550. /* Checking to see if its returned to OS */
  4551. rp = (struct srb *) CMD_SP(cmd);
  4552. if (rp == NULL) {
  4553. done++;
  4554. break;
  4555. }
  4556. msleep(2000);
  4557. } while (max_wait_time--);
  4558. return done;
  4559. }
  4560. /**
  4561. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  4562. * @ha: Pointer to host adapter structure
  4563. **/
  4564. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  4565. {
  4566. unsigned long wait_online;
  4567. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  4568. while (time_before(jiffies, wait_online)) {
  4569. if (adapter_up(ha))
  4570. return QLA_SUCCESS;
  4571. msleep(2000);
  4572. }
  4573. return QLA_ERROR;
  4574. }
  4575. /**
  4576. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  4577. * @ha: pointer to HBA
  4578. * @t: target id
  4579. * @l: lun id
  4580. *
  4581. * This function waits for all outstanding commands to a lun to complete. It
  4582. * returns 0 if all pending commands are returned and 1 otherwise.
  4583. **/
  4584. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  4585. struct scsi_target *stgt,
  4586. struct scsi_device *sdev)
  4587. {
  4588. int cnt;
  4589. int status = 0;
  4590. struct scsi_cmnd *cmd;
  4591. /*
  4592. * Waiting for all commands for the designated target or dev
  4593. * in the active array
  4594. */
  4595. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  4596. cmd = scsi_host_find_tag(ha->host, cnt);
  4597. if (cmd && stgt == scsi_target(cmd->device) &&
  4598. (!sdev || sdev == cmd->device)) {
  4599. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  4600. status++;
  4601. break;
  4602. }
  4603. }
  4604. }
  4605. return status;
  4606. }
  4607. /**
  4608. * qla4xxx_eh_abort - callback for abort task.
  4609. * @cmd: Pointer to Linux's SCSI command structure
  4610. *
  4611. * This routine is called by the Linux OS to abort the specified
  4612. * command.
  4613. **/
  4614. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  4615. {
  4616. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4617. unsigned int id = cmd->device->id;
  4618. unsigned int lun = cmd->device->lun;
  4619. unsigned long flags;
  4620. struct srb *srb = NULL;
  4621. int ret = SUCCESS;
  4622. int wait = 0;
  4623. ql4_printk(KERN_INFO, ha,
  4624. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  4625. ha->host_no, id, lun, cmd);
  4626. spin_lock_irqsave(&ha->hardware_lock, flags);
  4627. srb = (struct srb *) CMD_SP(cmd);
  4628. if (!srb) {
  4629. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4630. return SUCCESS;
  4631. }
  4632. kref_get(&srb->srb_ref);
  4633. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4634. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  4635. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  4636. ha->host_no, id, lun));
  4637. ret = FAILED;
  4638. } else {
  4639. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  4640. ha->host_no, id, lun));
  4641. wait = 1;
  4642. }
  4643. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  4644. /* Wait for command to complete */
  4645. if (wait) {
  4646. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  4647. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  4648. ha->host_no, id, lun));
  4649. ret = FAILED;
  4650. }
  4651. }
  4652. ql4_printk(KERN_INFO, ha,
  4653. "scsi%ld:%d:%d: Abort command - %s\n",
  4654. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  4655. return ret;
  4656. }
  4657. /**
  4658. * qla4xxx_eh_device_reset - callback for target reset.
  4659. * @cmd: Pointer to Linux's SCSI command structure
  4660. *
  4661. * This routine is called by the Linux OS to reset all luns on the
  4662. * specified target.
  4663. **/
  4664. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  4665. {
  4666. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4667. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  4668. int ret = FAILED, stat;
  4669. if (!ddb_entry)
  4670. return ret;
  4671. ret = iscsi_block_scsi_eh(cmd);
  4672. if (ret)
  4673. return ret;
  4674. ret = FAILED;
  4675. ql4_printk(KERN_INFO, ha,
  4676. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  4677. cmd->device->channel, cmd->device->id, cmd->device->lun);
  4678. DEBUG2(printk(KERN_INFO
  4679. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  4680. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  4681. cmd, jiffies, cmd->request->timeout / HZ,
  4682. ha->dpc_flags, cmd->result, cmd->allowed));
  4683. /* FIXME: wait for hba to go online */
  4684. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  4685. if (stat != QLA_SUCCESS) {
  4686. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  4687. goto eh_dev_reset_done;
  4688. }
  4689. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  4690. cmd->device)) {
  4691. ql4_printk(KERN_INFO, ha,
  4692. "DEVICE RESET FAILED - waiting for "
  4693. "commands.\n");
  4694. goto eh_dev_reset_done;
  4695. }
  4696. /* Send marker. */
  4697. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  4698. MM_LUN_RESET) != QLA_SUCCESS)
  4699. goto eh_dev_reset_done;
  4700. ql4_printk(KERN_INFO, ha,
  4701. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  4702. ha->host_no, cmd->device->channel, cmd->device->id,
  4703. cmd->device->lun);
  4704. ret = SUCCESS;
  4705. eh_dev_reset_done:
  4706. return ret;
  4707. }
  4708. /**
  4709. * qla4xxx_eh_target_reset - callback for target reset.
  4710. * @cmd: Pointer to Linux's SCSI command structure
  4711. *
  4712. * This routine is called by the Linux OS to reset the target.
  4713. **/
  4714. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  4715. {
  4716. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4717. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  4718. int stat, ret;
  4719. if (!ddb_entry)
  4720. return FAILED;
  4721. ret = iscsi_block_scsi_eh(cmd);
  4722. if (ret)
  4723. return ret;
  4724. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4725. "WARM TARGET RESET ISSUED.\n");
  4726. DEBUG2(printk(KERN_INFO
  4727. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  4728. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  4729. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  4730. ha->dpc_flags, cmd->result, cmd->allowed));
  4731. stat = qla4xxx_reset_target(ha, ddb_entry);
  4732. if (stat != QLA_SUCCESS) {
  4733. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4734. "WARM TARGET RESET FAILED.\n");
  4735. return FAILED;
  4736. }
  4737. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  4738. NULL)) {
  4739. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4740. "WARM TARGET DEVICE RESET FAILED - "
  4741. "waiting for commands.\n");
  4742. return FAILED;
  4743. }
  4744. /* Send marker. */
  4745. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  4746. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  4747. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4748. "WARM TARGET DEVICE RESET FAILED - "
  4749. "marker iocb failed.\n");
  4750. return FAILED;
  4751. }
  4752. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4753. "WARM TARGET RESET SUCCEEDED.\n");
  4754. return SUCCESS;
  4755. }
  4756. /**
  4757. * qla4xxx_is_eh_active - check if error handler is running
  4758. * @shost: Pointer to SCSI Host struct
  4759. *
  4760. * This routine finds that if reset host is called in EH
  4761. * scenario or from some application like sg_reset
  4762. **/
  4763. static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
  4764. {
  4765. if (shost->shost_state == SHOST_RECOVERY)
  4766. return 1;
  4767. return 0;
  4768. }
  4769. /**
  4770. * qla4xxx_eh_host_reset - kernel callback
  4771. * @cmd: Pointer to Linux's SCSI command structure
  4772. *
  4773. * This routine is invoked by the Linux kernel to perform fatal error
  4774. * recovery on the specified adapter.
  4775. **/
  4776. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  4777. {
  4778. int return_status = FAILED;
  4779. struct scsi_qla_host *ha;
  4780. ha = to_qla_host(cmd->device->host);
  4781. if (ql4xdontresethba) {
  4782. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  4783. ha->host_no, __func__));
  4784. /* Clear outstanding srb in queues */
  4785. if (qla4xxx_is_eh_active(cmd->device->host))
  4786. qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
  4787. return FAILED;
  4788. }
  4789. ql4_printk(KERN_INFO, ha,
  4790. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  4791. cmd->device->channel, cmd->device->id, cmd->device->lun);
  4792. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  4793. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  4794. "DEAD.\n", ha->host_no, cmd->device->channel,
  4795. __func__));
  4796. return FAILED;
  4797. }
  4798. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  4799. if (is_qla8022(ha))
  4800. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  4801. else
  4802. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  4803. }
  4804. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  4805. return_status = SUCCESS;
  4806. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  4807. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  4808. return return_status;
  4809. }
  4810. static int qla4xxx_context_reset(struct scsi_qla_host *ha)
  4811. {
  4812. uint32_t mbox_cmd[MBOX_REG_COUNT];
  4813. uint32_t mbox_sts[MBOX_REG_COUNT];
  4814. struct addr_ctrl_blk_def *acb = NULL;
  4815. uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
  4816. int rval = QLA_SUCCESS;
  4817. dma_addr_t acb_dma;
  4818. acb = dma_alloc_coherent(&ha->pdev->dev,
  4819. sizeof(struct addr_ctrl_blk_def),
  4820. &acb_dma, GFP_KERNEL);
  4821. if (!acb) {
  4822. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
  4823. __func__);
  4824. rval = -ENOMEM;
  4825. goto exit_port_reset;
  4826. }
  4827. memset(acb, 0, acb_len);
  4828. rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
  4829. if (rval != QLA_SUCCESS) {
  4830. rval = -EIO;
  4831. goto exit_free_acb;
  4832. }
  4833. rval = qla4xxx_disable_acb(ha);
  4834. if (rval != QLA_SUCCESS) {
  4835. rval = -EIO;
  4836. goto exit_free_acb;
  4837. }
  4838. wait_for_completion_timeout(&ha->disable_acb_comp,
  4839. DISABLE_ACB_TOV * HZ);
  4840. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
  4841. if (rval != QLA_SUCCESS) {
  4842. rval = -EIO;
  4843. goto exit_free_acb;
  4844. }
  4845. exit_free_acb:
  4846. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
  4847. acb, acb_dma);
  4848. exit_port_reset:
  4849. DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
  4850. rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
  4851. return rval;
  4852. }
  4853. static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
  4854. {
  4855. struct scsi_qla_host *ha = to_qla_host(shost);
  4856. int rval = QLA_SUCCESS;
  4857. if (ql4xdontresethba) {
  4858. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
  4859. __func__));
  4860. rval = -EPERM;
  4861. goto exit_host_reset;
  4862. }
  4863. rval = qla4xxx_wait_for_hba_online(ha);
  4864. if (rval != QLA_SUCCESS) {
  4865. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
  4866. "adapter\n", __func__));
  4867. rval = -EIO;
  4868. goto exit_host_reset;
  4869. }
  4870. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  4871. goto recover_adapter;
  4872. switch (reset_type) {
  4873. case SCSI_ADAPTER_RESET:
  4874. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  4875. break;
  4876. case SCSI_FIRMWARE_RESET:
  4877. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  4878. if (is_qla8022(ha))
  4879. /* set firmware context reset */
  4880. set_bit(DPC_RESET_HA_FW_CONTEXT,
  4881. &ha->dpc_flags);
  4882. else {
  4883. rval = qla4xxx_context_reset(ha);
  4884. goto exit_host_reset;
  4885. }
  4886. }
  4887. break;
  4888. }
  4889. recover_adapter:
  4890. rval = qla4xxx_recover_adapter(ha);
  4891. if (rval != QLA_SUCCESS) {
  4892. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
  4893. __func__));
  4894. rval = -EIO;
  4895. }
  4896. exit_host_reset:
  4897. return rval;
  4898. }
  4899. /* PCI AER driver recovers from all correctable errors w/o
  4900. * driver intervention. For uncorrectable errors PCI AER
  4901. * driver calls the following device driver's callbacks
  4902. *
  4903. * - Fatal Errors - link_reset
  4904. * - Non-Fatal Errors - driver's pci_error_detected() which
  4905. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  4906. *
  4907. * PCI AER driver calls
  4908. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  4909. * returns RECOVERED or NEED_RESET if fw_hung
  4910. * NEED_RESET - driver's slot_reset()
  4911. * DISCONNECT - device is dead & cannot recover
  4912. * RECOVERED - driver's pci_resume()
  4913. */
  4914. static pci_ers_result_t
  4915. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  4916. {
  4917. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  4918. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  4919. ha->host_no, __func__, state);
  4920. if (!is_aer_supported(ha))
  4921. return PCI_ERS_RESULT_NONE;
  4922. switch (state) {
  4923. case pci_channel_io_normal:
  4924. clear_bit(AF_EEH_BUSY, &ha->flags);
  4925. return PCI_ERS_RESULT_CAN_RECOVER;
  4926. case pci_channel_io_frozen:
  4927. set_bit(AF_EEH_BUSY, &ha->flags);
  4928. qla4xxx_mailbox_premature_completion(ha);
  4929. qla4xxx_free_irqs(ha);
  4930. pci_disable_device(pdev);
  4931. /* Return back all IOs */
  4932. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  4933. return PCI_ERS_RESULT_NEED_RESET;
  4934. case pci_channel_io_perm_failure:
  4935. set_bit(AF_EEH_BUSY, &ha->flags);
  4936. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  4937. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  4938. return PCI_ERS_RESULT_DISCONNECT;
  4939. }
  4940. return PCI_ERS_RESULT_NEED_RESET;
  4941. }
  4942. /**
  4943. * qla4xxx_pci_mmio_enabled() gets called if
  4944. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  4945. * and read/write to the device still works.
  4946. **/
  4947. static pci_ers_result_t
  4948. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  4949. {
  4950. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  4951. if (!is_aer_supported(ha))
  4952. return PCI_ERS_RESULT_NONE;
  4953. return PCI_ERS_RESULT_RECOVERED;
  4954. }
  4955. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  4956. {
  4957. uint32_t rval = QLA_ERROR;
  4958. uint32_t ret = 0;
  4959. int fn;
  4960. struct pci_dev *other_pdev = NULL;
  4961. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  4962. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  4963. if (test_bit(AF_ONLINE, &ha->flags)) {
  4964. clear_bit(AF_ONLINE, &ha->flags);
  4965. clear_bit(AF_LINK_UP, &ha->flags);
  4966. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  4967. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  4968. }
  4969. fn = PCI_FUNC(ha->pdev->devfn);
  4970. while (fn > 0) {
  4971. fn--;
  4972. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  4973. "func %x\n", ha->host_no, __func__, fn);
  4974. /* Get the pci device given the domain, bus,
  4975. * slot/function number */
  4976. other_pdev =
  4977. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  4978. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  4979. fn));
  4980. if (!other_pdev)
  4981. continue;
  4982. if (atomic_read(&other_pdev->enable_cnt)) {
  4983. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  4984. "func in enabled state%x\n", ha->host_no,
  4985. __func__, fn);
  4986. pci_dev_put(other_pdev);
  4987. break;
  4988. }
  4989. pci_dev_put(other_pdev);
  4990. }
  4991. /* The first function on the card, the reset owner will
  4992. * start & initialize the firmware. The other functions
  4993. * on the card will reset the firmware context
  4994. */
  4995. if (!fn) {
  4996. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  4997. "0x%x is the owner\n", ha->host_no, __func__,
  4998. ha->pdev->devfn);
  4999. qla4_8xxx_idc_lock(ha);
  5000. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  5001. QLA82XX_DEV_COLD);
  5002. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  5003. QLA82XX_IDC_VERSION);
  5004. qla4_8xxx_idc_unlock(ha);
  5005. clear_bit(AF_FW_RECOVERY, &ha->flags);
  5006. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  5007. qla4_8xxx_idc_lock(ha);
  5008. if (rval != QLA_SUCCESS) {
  5009. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  5010. "FAILED\n", ha->host_no, __func__);
  5011. qla4_8xxx_clear_drv_active(ha);
  5012. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  5013. QLA82XX_DEV_FAILED);
  5014. } else {
  5015. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  5016. "READY\n", ha->host_no, __func__);
  5017. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  5018. QLA82XX_DEV_READY);
  5019. /* Clear driver state register */
  5020. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  5021. qla4_8xxx_set_drv_active(ha);
  5022. ret = qla4xxx_request_irqs(ha);
  5023. if (ret) {
  5024. ql4_printk(KERN_WARNING, ha, "Failed to "
  5025. "reserve interrupt %d already in use.\n",
  5026. ha->pdev->irq);
  5027. rval = QLA_ERROR;
  5028. } else {
  5029. ha->isp_ops->enable_intrs(ha);
  5030. rval = QLA_SUCCESS;
  5031. }
  5032. }
  5033. qla4_8xxx_idc_unlock(ha);
  5034. } else {
  5035. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  5036. "the reset owner\n", ha->host_no, __func__,
  5037. ha->pdev->devfn);
  5038. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  5039. QLA82XX_DEV_READY)) {
  5040. clear_bit(AF_FW_RECOVERY, &ha->flags);
  5041. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  5042. if (rval == QLA_SUCCESS) {
  5043. ret = qla4xxx_request_irqs(ha);
  5044. if (ret) {
  5045. ql4_printk(KERN_WARNING, ha, "Failed to"
  5046. " reserve interrupt %d already in"
  5047. " use.\n", ha->pdev->irq);
  5048. rval = QLA_ERROR;
  5049. } else {
  5050. ha->isp_ops->enable_intrs(ha);
  5051. rval = QLA_SUCCESS;
  5052. }
  5053. }
  5054. qla4_8xxx_idc_lock(ha);
  5055. qla4_8xxx_set_drv_active(ha);
  5056. qla4_8xxx_idc_unlock(ha);
  5057. }
  5058. }
  5059. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  5060. return rval;
  5061. }
  5062. static pci_ers_result_t
  5063. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  5064. {
  5065. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  5066. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  5067. int rc;
  5068. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  5069. ha->host_no, __func__);
  5070. if (!is_aer_supported(ha))
  5071. return PCI_ERS_RESULT_NONE;
  5072. /* Restore the saved state of PCIe device -
  5073. * BAR registers, PCI Config space, PCIX, MSI,
  5074. * IOV states
  5075. */
  5076. pci_restore_state(pdev);
  5077. /* pci_restore_state() clears the saved_state flag of the device
  5078. * save restored state which resets saved_state flag
  5079. */
  5080. pci_save_state(pdev);
  5081. /* Initialize device or resume if in suspended state */
  5082. rc = pci_enable_device(pdev);
  5083. if (rc) {
  5084. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  5085. "device after reset\n", ha->host_no, __func__);
  5086. goto exit_slot_reset;
  5087. }
  5088. ha->isp_ops->disable_intrs(ha);
  5089. if (is_qla8022(ha)) {
  5090. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  5091. ret = PCI_ERS_RESULT_RECOVERED;
  5092. goto exit_slot_reset;
  5093. } else
  5094. goto exit_slot_reset;
  5095. }
  5096. exit_slot_reset:
  5097. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  5098. "device after reset\n", ha->host_no, __func__, ret);
  5099. return ret;
  5100. }
  5101. static void
  5102. qla4xxx_pci_resume(struct pci_dev *pdev)
  5103. {
  5104. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  5105. int ret;
  5106. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  5107. ha->host_no, __func__);
  5108. ret = qla4xxx_wait_for_hba_online(ha);
  5109. if (ret != QLA_SUCCESS) {
  5110. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  5111. "resume I/O from slot/link_reset\n", ha->host_no,
  5112. __func__);
  5113. }
  5114. pci_cleanup_aer_uncorrect_error_status(pdev);
  5115. clear_bit(AF_EEH_BUSY, &ha->flags);
  5116. }
  5117. static struct pci_error_handlers qla4xxx_err_handler = {
  5118. .error_detected = qla4xxx_pci_error_detected,
  5119. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  5120. .slot_reset = qla4xxx_pci_slot_reset,
  5121. .resume = qla4xxx_pci_resume,
  5122. };
  5123. static struct pci_device_id qla4xxx_pci_tbl[] = {
  5124. {
  5125. .vendor = PCI_VENDOR_ID_QLOGIC,
  5126. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  5127. .subvendor = PCI_ANY_ID,
  5128. .subdevice = PCI_ANY_ID,
  5129. },
  5130. {
  5131. .vendor = PCI_VENDOR_ID_QLOGIC,
  5132. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  5133. .subvendor = PCI_ANY_ID,
  5134. .subdevice = PCI_ANY_ID,
  5135. },
  5136. {
  5137. .vendor = PCI_VENDOR_ID_QLOGIC,
  5138. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  5139. .subvendor = PCI_ANY_ID,
  5140. .subdevice = PCI_ANY_ID,
  5141. },
  5142. {
  5143. .vendor = PCI_VENDOR_ID_QLOGIC,
  5144. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  5145. .subvendor = PCI_ANY_ID,
  5146. .subdevice = PCI_ANY_ID,
  5147. },
  5148. {0, 0},
  5149. };
  5150. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  5151. static struct pci_driver qla4xxx_pci_driver = {
  5152. .name = DRIVER_NAME,
  5153. .id_table = qla4xxx_pci_tbl,
  5154. .probe = qla4xxx_probe_adapter,
  5155. .remove = qla4xxx_remove_adapter,
  5156. .err_handler = &qla4xxx_err_handler,
  5157. };
  5158. static int __init qla4xxx_module_init(void)
  5159. {
  5160. int ret;
  5161. /* Allocate cache for SRBs. */
  5162. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  5163. SLAB_HWCACHE_ALIGN, NULL);
  5164. if (srb_cachep == NULL) {
  5165. printk(KERN_ERR
  5166. "%s: Unable to allocate SRB cache..."
  5167. "Failing load!\n", DRIVER_NAME);
  5168. ret = -ENOMEM;
  5169. goto no_srp_cache;
  5170. }
  5171. /* Derive version string. */
  5172. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  5173. if (ql4xextended_error_logging)
  5174. strcat(qla4xxx_version_str, "-debug");
  5175. qla4xxx_scsi_transport =
  5176. iscsi_register_transport(&qla4xxx_iscsi_transport);
  5177. if (!qla4xxx_scsi_transport){
  5178. ret = -ENODEV;
  5179. goto release_srb_cache;
  5180. }
  5181. ret = pci_register_driver(&qla4xxx_pci_driver);
  5182. if (ret)
  5183. goto unregister_transport;
  5184. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  5185. return 0;
  5186. unregister_transport:
  5187. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  5188. release_srb_cache:
  5189. kmem_cache_destroy(srb_cachep);
  5190. no_srp_cache:
  5191. return ret;
  5192. }
  5193. static void __exit qla4xxx_module_exit(void)
  5194. {
  5195. pci_unregister_driver(&qla4xxx_pci_driver);
  5196. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  5197. kmem_cache_destroy(srb_cachep);
  5198. }
  5199. module_init(qla4xxx_module_init);
  5200. module_exit(qla4xxx_module_exit);
  5201. MODULE_AUTHOR("QLogic Corporation");
  5202. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  5203. MODULE_LICENSE("GPL");
  5204. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);