ql4_os.c 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045
  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;
  1381. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1382. sess = cls_sess->dd_data;
  1383. ddb_entry = sess->dd_data;
  1384. ha = ddb_entry->ha;
  1385. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  1386. spin_lock_irqsave(&ha->hardware_lock, flags);
  1387. qla4xxx_free_ddb(ha, ddb_entry);
  1388. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1389. iscsi_session_teardown(cls_sess);
  1390. }
  1391. static struct iscsi_cls_conn *
  1392. qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
  1393. {
  1394. struct iscsi_cls_conn *cls_conn;
  1395. struct iscsi_session *sess;
  1396. struct ddb_entry *ddb_entry;
  1397. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1398. cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
  1399. conn_idx);
  1400. if (!cls_conn)
  1401. return NULL;
  1402. sess = cls_sess->dd_data;
  1403. ddb_entry = sess->dd_data;
  1404. ddb_entry->conn = cls_conn;
  1405. return cls_conn;
  1406. }
  1407. static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  1408. struct iscsi_cls_conn *cls_conn,
  1409. uint64_t transport_fd, int is_leading)
  1410. {
  1411. struct iscsi_conn *conn;
  1412. struct qla_conn *qla_conn;
  1413. struct iscsi_endpoint *ep;
  1414. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1415. if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  1416. return -EINVAL;
  1417. ep = iscsi_lookup_endpoint(transport_fd);
  1418. conn = cls_conn->dd_data;
  1419. qla_conn = conn->dd_data;
  1420. qla_conn->qla_ep = ep->dd_data;
  1421. return 0;
  1422. }
  1423. static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
  1424. {
  1425. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1426. struct iscsi_session *sess;
  1427. struct ddb_entry *ddb_entry;
  1428. struct scsi_qla_host *ha;
  1429. struct dev_db_entry *fw_ddb_entry = NULL;
  1430. dma_addr_t fw_ddb_entry_dma;
  1431. uint32_t mbx_sts = 0;
  1432. int ret = 0;
  1433. int status = QLA_SUCCESS;
  1434. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1435. sess = cls_sess->dd_data;
  1436. ddb_entry = sess->dd_data;
  1437. ha = ddb_entry->ha;
  1438. /* Check if we have matching FW DDB, if yes then do not
  1439. * login to this target. This could cause target to logout previous
  1440. * connection
  1441. */
  1442. ret = qla4xxx_match_fwdb_session(ha, cls_conn);
  1443. if (ret == QLA_SUCCESS) {
  1444. ql4_printk(KERN_INFO, ha,
  1445. "Session already exist in FW.\n");
  1446. ret = -EEXIST;
  1447. goto exit_conn_start;
  1448. }
  1449. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1450. &fw_ddb_entry_dma, GFP_KERNEL);
  1451. if (!fw_ddb_entry) {
  1452. ql4_printk(KERN_ERR, ha,
  1453. "%s: Unable to allocate dma buffer\n", __func__);
  1454. ret = -ENOMEM;
  1455. goto exit_conn_start;
  1456. }
  1457. ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
  1458. if (ret) {
  1459. /* If iscsid is stopped and started then no need to do
  1460. * set param again since ddb state will be already
  1461. * active and FW does not allow set ddb to an
  1462. * active session.
  1463. */
  1464. if (mbx_sts)
  1465. if (ddb_entry->fw_ddb_device_state ==
  1466. DDB_DS_SESSION_ACTIVE) {
  1467. ddb_entry->unblock_sess(ddb_entry->sess);
  1468. goto exit_set_param;
  1469. }
  1470. ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
  1471. __func__, ddb_entry->fw_ddb_index);
  1472. goto exit_conn_start;
  1473. }
  1474. status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
  1475. if (status == QLA_ERROR) {
  1476. ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
  1477. sess->targetname);
  1478. ret = -EINVAL;
  1479. goto exit_conn_start;
  1480. }
  1481. if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
  1482. ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
  1483. DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
  1484. ddb_entry->fw_ddb_device_state));
  1485. exit_set_param:
  1486. ret = 0;
  1487. exit_conn_start:
  1488. if (fw_ddb_entry)
  1489. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1490. fw_ddb_entry, fw_ddb_entry_dma);
  1491. return ret;
  1492. }
  1493. static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1494. {
  1495. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1496. struct iscsi_session *sess;
  1497. struct scsi_qla_host *ha;
  1498. struct ddb_entry *ddb_entry;
  1499. int options;
  1500. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1501. sess = cls_sess->dd_data;
  1502. ddb_entry = sess->dd_data;
  1503. ha = ddb_entry->ha;
  1504. options = LOGOUT_OPTION_CLOSE_SESSION;
  1505. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
  1506. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
  1507. }
  1508. static void qla4xxx_task_work(struct work_struct *wdata)
  1509. {
  1510. struct ql4_task_data *task_data;
  1511. struct scsi_qla_host *ha;
  1512. struct passthru_status *sts;
  1513. struct iscsi_task *task;
  1514. struct iscsi_hdr *hdr;
  1515. uint8_t *data;
  1516. uint32_t data_len;
  1517. struct iscsi_conn *conn;
  1518. int hdr_len;
  1519. itt_t itt;
  1520. task_data = container_of(wdata, struct ql4_task_data, task_work);
  1521. ha = task_data->ha;
  1522. task = task_data->task;
  1523. sts = &task_data->sts;
  1524. hdr_len = sizeof(struct iscsi_hdr);
  1525. DEBUG3(printk(KERN_INFO "Status returned\n"));
  1526. DEBUG3(qla4xxx_dump_buffer(sts, 64));
  1527. DEBUG3(printk(KERN_INFO "Response buffer"));
  1528. DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
  1529. conn = task->conn;
  1530. switch (sts->completionStatus) {
  1531. case PASSTHRU_STATUS_COMPLETE:
  1532. hdr = (struct iscsi_hdr *)task_data->resp_buffer;
  1533. /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
  1534. itt = sts->handle;
  1535. hdr->itt = itt;
  1536. data = task_data->resp_buffer + hdr_len;
  1537. data_len = task_data->resp_len - hdr_len;
  1538. iscsi_complete_pdu(conn, hdr, data, data_len);
  1539. break;
  1540. default:
  1541. ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
  1542. sts->completionStatus);
  1543. break;
  1544. }
  1545. return;
  1546. }
  1547. static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  1548. {
  1549. struct ql4_task_data *task_data;
  1550. struct iscsi_session *sess;
  1551. struct ddb_entry *ddb_entry;
  1552. struct scsi_qla_host *ha;
  1553. int hdr_len;
  1554. sess = task->conn->session;
  1555. ddb_entry = sess->dd_data;
  1556. ha = ddb_entry->ha;
  1557. task_data = task->dd_data;
  1558. memset(task_data, 0, sizeof(struct ql4_task_data));
  1559. if (task->sc) {
  1560. ql4_printk(KERN_INFO, ha,
  1561. "%s: SCSI Commands not implemented\n", __func__);
  1562. return -EINVAL;
  1563. }
  1564. hdr_len = sizeof(struct iscsi_hdr);
  1565. task_data->ha = ha;
  1566. task_data->task = task;
  1567. if (task->data_count) {
  1568. task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
  1569. task->data_count,
  1570. PCI_DMA_TODEVICE);
  1571. }
  1572. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1573. __func__, task->conn->max_recv_dlength, hdr_len));
  1574. task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
  1575. task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1576. task_data->resp_len,
  1577. &task_data->resp_dma,
  1578. GFP_ATOMIC);
  1579. if (!task_data->resp_buffer)
  1580. goto exit_alloc_pdu;
  1581. task_data->req_len = task->data_count + hdr_len;
  1582. task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1583. task_data->req_len,
  1584. &task_data->req_dma,
  1585. GFP_ATOMIC);
  1586. if (!task_data->req_buffer)
  1587. goto exit_alloc_pdu;
  1588. task->hdr = task_data->req_buffer;
  1589. INIT_WORK(&task_data->task_work, qla4xxx_task_work);
  1590. return 0;
  1591. exit_alloc_pdu:
  1592. if (task_data->resp_buffer)
  1593. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1594. task_data->resp_buffer, task_data->resp_dma);
  1595. if (task_data->req_buffer)
  1596. dma_free_coherent(&ha->pdev->dev, task_data->req_len,
  1597. task_data->req_buffer, task_data->req_dma);
  1598. return -ENOMEM;
  1599. }
  1600. static void qla4xxx_task_cleanup(struct iscsi_task *task)
  1601. {
  1602. struct ql4_task_data *task_data;
  1603. struct iscsi_session *sess;
  1604. struct ddb_entry *ddb_entry;
  1605. struct scsi_qla_host *ha;
  1606. int hdr_len;
  1607. hdr_len = sizeof(struct iscsi_hdr);
  1608. sess = task->conn->session;
  1609. ddb_entry = sess->dd_data;
  1610. ha = ddb_entry->ha;
  1611. task_data = task->dd_data;
  1612. if (task->data_count) {
  1613. dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
  1614. task->data_count, PCI_DMA_TODEVICE);
  1615. }
  1616. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1617. __func__, task->conn->max_recv_dlength, hdr_len));
  1618. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1619. task_data->resp_buffer, task_data->resp_dma);
  1620. dma_free_coherent(&ha->pdev->dev, task_data->req_len,
  1621. task_data->req_buffer, task_data->req_dma);
  1622. return;
  1623. }
  1624. static int qla4xxx_task_xmit(struct iscsi_task *task)
  1625. {
  1626. struct scsi_cmnd *sc = task->sc;
  1627. struct iscsi_session *sess = task->conn->session;
  1628. struct ddb_entry *ddb_entry = sess->dd_data;
  1629. struct scsi_qla_host *ha = ddb_entry->ha;
  1630. if (!sc)
  1631. return qla4xxx_send_passthru0(task);
  1632. ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
  1633. __func__);
  1634. return -ENOSYS;
  1635. }
  1636. static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
  1637. struct dev_db_entry *fw_ddb_entry,
  1638. struct iscsi_cls_session *cls_sess,
  1639. struct iscsi_cls_conn *cls_conn)
  1640. {
  1641. int buflen = 0;
  1642. struct iscsi_session *sess;
  1643. struct ddb_entry *ddb_entry;
  1644. struct iscsi_conn *conn;
  1645. char ip_addr[DDB_IPADDR_LEN];
  1646. uint16_t options = 0;
  1647. sess = cls_sess->dd_data;
  1648. ddb_entry = sess->dd_data;
  1649. conn = cls_conn->dd_data;
  1650. ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  1651. conn->max_recv_dlength = BYTE_UNITS *
  1652. le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  1653. conn->max_xmit_dlength = BYTE_UNITS *
  1654. le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  1655. sess->initial_r2t_en =
  1656. (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1657. sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  1658. sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1659. sess->first_burst = BYTE_UNITS *
  1660. le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  1661. sess->max_burst = BYTE_UNITS *
  1662. le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  1663. sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  1664. sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  1665. conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
  1666. sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  1667. options = le16_to_cpu(fw_ddb_entry->options);
  1668. if (options & DDB_OPT_IPV6_DEVICE)
  1669. sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  1670. else
  1671. sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  1672. iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
  1673. (char *)fw_ddb_entry->iscsi_name, buflen);
  1674. iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
  1675. (char *)ha->name_string, buflen);
  1676. iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
  1677. (char *)ip_addr, buflen);
  1678. iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
  1679. (char *)fw_ddb_entry->iscsi_alias, buflen);
  1680. }
  1681. void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
  1682. struct ddb_entry *ddb_entry)
  1683. {
  1684. struct iscsi_cls_session *cls_sess;
  1685. struct iscsi_cls_conn *cls_conn;
  1686. uint32_t ddb_state;
  1687. dma_addr_t fw_ddb_entry_dma;
  1688. struct dev_db_entry *fw_ddb_entry;
  1689. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1690. &fw_ddb_entry_dma, GFP_KERNEL);
  1691. if (!fw_ddb_entry) {
  1692. ql4_printk(KERN_ERR, ha,
  1693. "%s: Unable to allocate dma buffer\n", __func__);
  1694. goto exit_session_conn_fwddb_param;
  1695. }
  1696. if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  1697. fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  1698. NULL, NULL, NULL) == QLA_ERROR) {
  1699. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  1700. "get_ddb_entry for fw_ddb_index %d\n",
  1701. ha->host_no, __func__,
  1702. ddb_entry->fw_ddb_index));
  1703. goto exit_session_conn_fwddb_param;
  1704. }
  1705. cls_sess = ddb_entry->sess;
  1706. cls_conn = ddb_entry->conn;
  1707. /* Update params */
  1708. qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
  1709. exit_session_conn_fwddb_param:
  1710. if (fw_ddb_entry)
  1711. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1712. fw_ddb_entry, fw_ddb_entry_dma);
  1713. }
  1714. void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
  1715. struct ddb_entry *ddb_entry)
  1716. {
  1717. struct iscsi_cls_session *cls_sess;
  1718. struct iscsi_cls_conn *cls_conn;
  1719. struct iscsi_session *sess;
  1720. struct iscsi_conn *conn;
  1721. uint32_t ddb_state;
  1722. dma_addr_t fw_ddb_entry_dma;
  1723. struct dev_db_entry *fw_ddb_entry;
  1724. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1725. &fw_ddb_entry_dma, GFP_KERNEL);
  1726. if (!fw_ddb_entry) {
  1727. ql4_printk(KERN_ERR, ha,
  1728. "%s: Unable to allocate dma buffer\n", __func__);
  1729. goto exit_session_conn_param;
  1730. }
  1731. if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  1732. fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  1733. NULL, NULL, NULL) == QLA_ERROR) {
  1734. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  1735. "get_ddb_entry for fw_ddb_index %d\n",
  1736. ha->host_no, __func__,
  1737. ddb_entry->fw_ddb_index));
  1738. goto exit_session_conn_param;
  1739. }
  1740. cls_sess = ddb_entry->sess;
  1741. sess = cls_sess->dd_data;
  1742. cls_conn = ddb_entry->conn;
  1743. conn = cls_conn->dd_data;
  1744. /* Update timers after login */
  1745. ddb_entry->default_relogin_timeout =
  1746. (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
  1747. (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
  1748. le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
  1749. ddb_entry->default_time2wait =
  1750. le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  1751. /* Update params */
  1752. ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  1753. conn->max_recv_dlength = BYTE_UNITS *
  1754. le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  1755. conn->max_xmit_dlength = BYTE_UNITS *
  1756. le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  1757. sess->initial_r2t_en =
  1758. (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1759. sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  1760. sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1761. sess->first_burst = BYTE_UNITS *
  1762. le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  1763. sess->max_burst = BYTE_UNITS *
  1764. le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  1765. sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  1766. sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  1767. sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  1768. memcpy(sess->initiatorname, ha->name_string,
  1769. min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
  1770. iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
  1771. (char *)fw_ddb_entry->iscsi_alias, 0);
  1772. exit_session_conn_param:
  1773. if (fw_ddb_entry)
  1774. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1775. fw_ddb_entry, fw_ddb_entry_dma);
  1776. }
  1777. /*
  1778. * Timer routines
  1779. */
  1780. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  1781. unsigned long interval)
  1782. {
  1783. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  1784. __func__, ha->host->host_no));
  1785. init_timer(&ha->timer);
  1786. ha->timer.expires = jiffies + interval * HZ;
  1787. ha->timer.data = (unsigned long)ha;
  1788. ha->timer.function = (void (*)(unsigned long))func;
  1789. add_timer(&ha->timer);
  1790. ha->timer_active = 1;
  1791. }
  1792. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  1793. {
  1794. del_timer_sync(&ha->timer);
  1795. ha->timer_active = 0;
  1796. }
  1797. /***
  1798. * qla4xxx_mark_device_missing - blocks the session
  1799. * @cls_session: Pointer to the session to be blocked
  1800. * @ddb_entry: Pointer to device database entry
  1801. *
  1802. * This routine marks a device missing and close connection.
  1803. **/
  1804. void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
  1805. {
  1806. iscsi_block_session(cls_session);
  1807. }
  1808. /**
  1809. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  1810. * @ha: Pointer to host adapter structure.
  1811. *
  1812. * This routine marks a device missing and resets the relogin retry count.
  1813. **/
  1814. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  1815. {
  1816. iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
  1817. }
  1818. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  1819. struct ddb_entry *ddb_entry,
  1820. struct scsi_cmnd *cmd)
  1821. {
  1822. struct srb *srb;
  1823. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  1824. if (!srb)
  1825. return srb;
  1826. kref_init(&srb->srb_ref);
  1827. srb->ha = ha;
  1828. srb->ddb = ddb_entry;
  1829. srb->cmd = cmd;
  1830. srb->flags = 0;
  1831. CMD_SP(cmd) = (void *)srb;
  1832. return srb;
  1833. }
  1834. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  1835. {
  1836. struct scsi_cmnd *cmd = srb->cmd;
  1837. if (srb->flags & SRB_DMA_VALID) {
  1838. scsi_dma_unmap(cmd);
  1839. srb->flags &= ~SRB_DMA_VALID;
  1840. }
  1841. CMD_SP(cmd) = NULL;
  1842. }
  1843. void qla4xxx_srb_compl(struct kref *ref)
  1844. {
  1845. struct srb *srb = container_of(ref, struct srb, srb_ref);
  1846. struct scsi_cmnd *cmd = srb->cmd;
  1847. struct scsi_qla_host *ha = srb->ha;
  1848. qla4xxx_srb_free_dma(ha, srb);
  1849. mempool_free(srb, ha->srb_mempool);
  1850. cmd->scsi_done(cmd);
  1851. }
  1852. /**
  1853. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  1854. * @host: scsi host
  1855. * @cmd: Pointer to Linux's SCSI command structure
  1856. *
  1857. * Remarks:
  1858. * This routine is invoked by Linux to send a SCSI command to the driver.
  1859. * The mid-level driver tries to ensure that queuecommand never gets
  1860. * invoked concurrently with itself or the interrupt handler (although
  1861. * the interrupt handler may call this routine as part of request-
  1862. * completion handling). Unfortunely, it sometimes calls the scheduler
  1863. * in interrupt context which is a big NO! NO!.
  1864. **/
  1865. static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  1866. {
  1867. struct scsi_qla_host *ha = to_qla_host(host);
  1868. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1869. struct iscsi_cls_session *sess = ddb_entry->sess;
  1870. struct srb *srb;
  1871. int rval;
  1872. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1873. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  1874. cmd->result = DID_NO_CONNECT << 16;
  1875. else
  1876. cmd->result = DID_REQUEUE << 16;
  1877. goto qc_fail_command;
  1878. }
  1879. if (!sess) {
  1880. cmd->result = DID_IMM_RETRY << 16;
  1881. goto qc_fail_command;
  1882. }
  1883. rval = iscsi_session_chkready(sess);
  1884. if (rval) {
  1885. cmd->result = rval;
  1886. goto qc_fail_command;
  1887. }
  1888. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1889. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  1890. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1891. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  1892. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  1893. !test_bit(AF_ONLINE, &ha->flags) ||
  1894. !test_bit(AF_LINK_UP, &ha->flags) ||
  1895. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  1896. goto qc_host_busy;
  1897. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
  1898. if (!srb)
  1899. goto qc_host_busy;
  1900. rval = qla4xxx_send_command_to_isp(ha, srb);
  1901. if (rval != QLA_SUCCESS)
  1902. goto qc_host_busy_free_sp;
  1903. return 0;
  1904. qc_host_busy_free_sp:
  1905. qla4xxx_srb_free_dma(ha, srb);
  1906. mempool_free(srb, ha->srb_mempool);
  1907. qc_host_busy:
  1908. return SCSI_MLQUEUE_HOST_BUSY;
  1909. qc_fail_command:
  1910. cmd->scsi_done(cmd);
  1911. return 0;
  1912. }
  1913. /**
  1914. * qla4xxx_mem_free - frees memory allocated to adapter
  1915. * @ha: Pointer to host adapter structure.
  1916. *
  1917. * Frees memory previously allocated by qla4xxx_mem_alloc
  1918. **/
  1919. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  1920. {
  1921. if (ha->queues)
  1922. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  1923. ha->queues_dma);
  1924. ha->queues_len = 0;
  1925. ha->queues = NULL;
  1926. ha->queues_dma = 0;
  1927. ha->request_ring = NULL;
  1928. ha->request_dma = 0;
  1929. ha->response_ring = NULL;
  1930. ha->response_dma = 0;
  1931. ha->shadow_regs = NULL;
  1932. ha->shadow_regs_dma = 0;
  1933. /* Free srb pool. */
  1934. if (ha->srb_mempool)
  1935. mempool_destroy(ha->srb_mempool);
  1936. ha->srb_mempool = NULL;
  1937. if (ha->chap_dma_pool)
  1938. dma_pool_destroy(ha->chap_dma_pool);
  1939. if (ha->chap_list)
  1940. vfree(ha->chap_list);
  1941. ha->chap_list = NULL;
  1942. if (ha->fw_ddb_dma_pool)
  1943. dma_pool_destroy(ha->fw_ddb_dma_pool);
  1944. /* release io space registers */
  1945. if (is_qla8022(ha)) {
  1946. if (ha->nx_pcibase)
  1947. iounmap(
  1948. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  1949. } else if (ha->reg)
  1950. iounmap(ha->reg);
  1951. pci_release_regions(ha->pdev);
  1952. }
  1953. /**
  1954. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  1955. * @ha: Pointer to host adapter structure
  1956. *
  1957. * Allocates DMA memory for request and response queues. Also allocates memory
  1958. * for srbs.
  1959. **/
  1960. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  1961. {
  1962. unsigned long align;
  1963. /* Allocate contiguous block of DMA memory for queues. */
  1964. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  1965. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  1966. sizeof(struct shadow_regs) +
  1967. MEM_ALIGN_VALUE +
  1968. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  1969. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  1970. &ha->queues_dma, GFP_KERNEL);
  1971. if (ha->queues == NULL) {
  1972. ql4_printk(KERN_WARNING, ha,
  1973. "Memory Allocation failed - queues.\n");
  1974. goto mem_alloc_error_exit;
  1975. }
  1976. memset(ha->queues, 0, ha->queues_len);
  1977. /*
  1978. * As per RISC alignment requirements -- the bus-address must be a
  1979. * multiple of the request-ring size (in bytes).
  1980. */
  1981. align = 0;
  1982. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  1983. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  1984. (MEM_ALIGN_VALUE - 1));
  1985. /* Update request and response queue pointers. */
  1986. ha->request_dma = ha->queues_dma + align;
  1987. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  1988. ha->response_dma = ha->queues_dma + align +
  1989. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  1990. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  1991. (REQUEST_QUEUE_DEPTH *
  1992. QUEUE_SIZE));
  1993. ha->shadow_regs_dma = ha->queues_dma + align +
  1994. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  1995. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  1996. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  1997. (REQUEST_QUEUE_DEPTH *
  1998. QUEUE_SIZE) +
  1999. (RESPONSE_QUEUE_DEPTH *
  2000. QUEUE_SIZE));
  2001. /* Allocate memory for srb pool. */
  2002. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  2003. mempool_free_slab, srb_cachep);
  2004. if (ha->srb_mempool == NULL) {
  2005. ql4_printk(KERN_WARNING, ha,
  2006. "Memory Allocation failed - SRB Pool.\n");
  2007. goto mem_alloc_error_exit;
  2008. }
  2009. ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
  2010. CHAP_DMA_BLOCK_SIZE, 8, 0);
  2011. if (ha->chap_dma_pool == NULL) {
  2012. ql4_printk(KERN_WARNING, ha,
  2013. "%s: chap_dma_pool allocation failed..\n", __func__);
  2014. goto mem_alloc_error_exit;
  2015. }
  2016. ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
  2017. DDB_DMA_BLOCK_SIZE, 8, 0);
  2018. if (ha->fw_ddb_dma_pool == NULL) {
  2019. ql4_printk(KERN_WARNING, ha,
  2020. "%s: fw_ddb_dma_pool allocation failed..\n",
  2021. __func__);
  2022. goto mem_alloc_error_exit;
  2023. }
  2024. return QLA_SUCCESS;
  2025. mem_alloc_error_exit:
  2026. qla4xxx_mem_free(ha);
  2027. return QLA_ERROR;
  2028. }
  2029. /**
  2030. * qla4_8xxx_check_temp - Check the ISP82XX temperature.
  2031. * @ha: adapter block pointer.
  2032. *
  2033. * Note: The caller should not hold the idc lock.
  2034. **/
  2035. static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
  2036. {
  2037. uint32_t temp, temp_state, temp_val;
  2038. int status = QLA_SUCCESS;
  2039. temp = qla4_8xxx_rd_32(ha, CRB_TEMP_STATE);
  2040. temp_state = qla82xx_get_temp_state(temp);
  2041. temp_val = qla82xx_get_temp_val(temp);
  2042. if (temp_state == QLA82XX_TEMP_PANIC) {
  2043. ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
  2044. " exceeds maximum allowed. Hardware has been shut"
  2045. " down.\n", temp_val);
  2046. status = QLA_ERROR;
  2047. } else if (temp_state == QLA82XX_TEMP_WARN) {
  2048. if (ha->temperature == QLA82XX_TEMP_NORMAL)
  2049. ql4_printk(KERN_WARNING, ha, "Device temperature %d"
  2050. " degrees C exceeds operating range."
  2051. " Immediate action needed.\n", temp_val);
  2052. } else {
  2053. if (ha->temperature == QLA82XX_TEMP_WARN)
  2054. ql4_printk(KERN_INFO, ha, "Device temperature is"
  2055. " now %d degrees C in normal range.\n",
  2056. temp_val);
  2057. }
  2058. ha->temperature = temp_state;
  2059. return status;
  2060. }
  2061. /**
  2062. * qla4_8xxx_check_fw_alive - Check firmware health
  2063. * @ha: Pointer to host adapter structure.
  2064. *
  2065. * Context: Interrupt
  2066. **/
  2067. static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  2068. {
  2069. uint32_t fw_heartbeat_counter;
  2070. int status = QLA_SUCCESS;
  2071. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  2072. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  2073. if (fw_heartbeat_counter == 0xffffffff) {
  2074. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  2075. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  2076. ha->host_no, __func__));
  2077. return status;
  2078. }
  2079. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  2080. ha->seconds_since_last_heartbeat++;
  2081. /* FW not alive after 2 seconds */
  2082. if (ha->seconds_since_last_heartbeat == 2) {
  2083. ha->seconds_since_last_heartbeat = 0;
  2084. ql4_printk(KERN_INFO, ha,
  2085. "scsi(%ld): %s, Dumping hw/fw registers:\n "
  2086. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  2087. " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  2088. " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  2089. " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
  2090. ha->host_no, __func__,
  2091. qla4_8xxx_rd_32(ha,
  2092. QLA82XX_PEG_HALT_STATUS1),
  2093. qla4_8xxx_rd_32(ha,
  2094. QLA82XX_PEG_HALT_STATUS2),
  2095. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  2096. 0x3c),
  2097. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  2098. 0x3c),
  2099. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  2100. 0x3c),
  2101. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  2102. 0x3c),
  2103. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  2104. 0x3c));
  2105. status = QLA_ERROR;
  2106. }
  2107. } else
  2108. ha->seconds_since_last_heartbeat = 0;
  2109. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  2110. return status;
  2111. }
  2112. /**
  2113. * qla4_8xxx_watchdog - Poll dev state
  2114. * @ha: Pointer to host adapter structure.
  2115. *
  2116. * Context: Interrupt
  2117. **/
  2118. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  2119. {
  2120. uint32_t dev_state, halt_status;
  2121. /* don't poll if reset is going on */
  2122. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  2123. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2124. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
  2125. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  2126. if (qla4_8xxx_check_temp(ha)) {
  2127. ql4_printk(KERN_INFO, ha, "disabling pause"
  2128. " transmit on port 0 & 1.\n");
  2129. qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
  2130. CRB_NIU_XG_PAUSE_CTL_P0 |
  2131. CRB_NIU_XG_PAUSE_CTL_P1);
  2132. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  2133. qla4xxx_wake_dpc(ha);
  2134. } else if (dev_state == QLA82XX_DEV_NEED_RESET &&
  2135. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  2136. if (!ql4xdontresethba) {
  2137. ql4_printk(KERN_INFO, ha, "%s: HW State: "
  2138. "NEED RESET!\n", __func__);
  2139. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2140. qla4xxx_wake_dpc(ha);
  2141. }
  2142. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  2143. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  2144. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  2145. __func__);
  2146. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  2147. qla4xxx_wake_dpc(ha);
  2148. } else {
  2149. /* Check firmware health */
  2150. if (qla4_8xxx_check_fw_alive(ha)) {
  2151. ql4_printk(KERN_INFO, ha, "disabling pause"
  2152. " transmit on port 0 & 1.\n");
  2153. qla4_8xxx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
  2154. CRB_NIU_XG_PAUSE_CTL_P0 |
  2155. CRB_NIU_XG_PAUSE_CTL_P1);
  2156. halt_status = qla4_8xxx_rd_32(ha,
  2157. QLA82XX_PEG_HALT_STATUS1);
  2158. if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
  2159. ql4_printk(KERN_ERR, ha, "%s:"
  2160. " Firmware aborted with"
  2161. " error code 0x00006700."
  2162. " Device is being reset\n",
  2163. __func__);
  2164. /* Since we cannot change dev_state in interrupt
  2165. * context, set appropriate DPC flag then wakeup
  2166. * DPC */
  2167. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  2168. set_bit(DPC_HA_UNRECOVERABLE,
  2169. &ha->dpc_flags);
  2170. else {
  2171. ql4_printk(KERN_INFO, ha, "%s: detect "
  2172. "abort needed!\n", __func__);
  2173. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2174. }
  2175. qla4xxx_mailbox_premature_completion(ha);
  2176. qla4xxx_wake_dpc(ha);
  2177. }
  2178. }
  2179. }
  2180. }
  2181. static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  2182. {
  2183. struct iscsi_session *sess;
  2184. struct ddb_entry *ddb_entry;
  2185. struct scsi_qla_host *ha;
  2186. sess = cls_sess->dd_data;
  2187. ddb_entry = sess->dd_data;
  2188. ha = ddb_entry->ha;
  2189. if (!(ddb_entry->ddb_type == FLASH_DDB))
  2190. return;
  2191. if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  2192. !iscsi_is_session_online(cls_sess)) {
  2193. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  2194. INVALID_ENTRY) {
  2195. if (atomic_read(&ddb_entry->retry_relogin_timer) ==
  2196. 0) {
  2197. atomic_set(&ddb_entry->retry_relogin_timer,
  2198. INVALID_ENTRY);
  2199. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  2200. set_bit(DF_RELOGIN, &ddb_entry->flags);
  2201. DEBUG2(ql4_printk(KERN_INFO, ha,
  2202. "%s: index [%d] login device\n",
  2203. __func__, ddb_entry->fw_ddb_index));
  2204. } else
  2205. atomic_dec(&ddb_entry->retry_relogin_timer);
  2206. }
  2207. }
  2208. /* Wait for relogin to timeout */
  2209. if (atomic_read(&ddb_entry->relogin_timer) &&
  2210. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  2211. /*
  2212. * If the relogin times out and the device is
  2213. * still NOT ONLINE then try and relogin again.
  2214. */
  2215. if (!iscsi_is_session_online(cls_sess)) {
  2216. /* Reset retry relogin timer */
  2217. atomic_inc(&ddb_entry->relogin_retry_count);
  2218. DEBUG2(ql4_printk(KERN_INFO, ha,
  2219. "%s: index[%d] relogin timed out-retrying"
  2220. " relogin (%d), retry (%d)\n", __func__,
  2221. ddb_entry->fw_ddb_index,
  2222. atomic_read(&ddb_entry->relogin_retry_count),
  2223. ddb_entry->default_time2wait + 4));
  2224. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  2225. atomic_set(&ddb_entry->retry_relogin_timer,
  2226. ddb_entry->default_time2wait + 4);
  2227. }
  2228. }
  2229. }
  2230. /**
  2231. * qla4xxx_timer - checks every second for work to do.
  2232. * @ha: Pointer to host adapter structure.
  2233. **/
  2234. static void qla4xxx_timer(struct scsi_qla_host *ha)
  2235. {
  2236. int start_dpc = 0;
  2237. uint16_t w;
  2238. iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
  2239. /* If we are in the middle of AER/EEH processing
  2240. * skip any processing and reschedule the timer
  2241. */
  2242. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  2243. mod_timer(&ha->timer, jiffies + HZ);
  2244. return;
  2245. }
  2246. /* Hardware read to trigger an EEH error during mailbox waits. */
  2247. if (!pci_channel_offline(ha->pdev))
  2248. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  2249. if (is_qla8022(ha)) {
  2250. qla4_8xxx_watchdog(ha);
  2251. }
  2252. if (!is_qla8022(ha)) {
  2253. /* Check for heartbeat interval. */
  2254. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  2255. ha->heartbeat_interval != 0) {
  2256. ha->seconds_since_last_heartbeat++;
  2257. if (ha->seconds_since_last_heartbeat >
  2258. ha->heartbeat_interval + 2)
  2259. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2260. }
  2261. }
  2262. /* Process any deferred work. */
  2263. if (!list_empty(&ha->work_list))
  2264. start_dpc++;
  2265. /* Wakeup the dpc routine for this adapter, if needed. */
  2266. if (start_dpc ||
  2267. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2268. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  2269. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  2270. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  2271. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  2272. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  2273. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  2274. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  2275. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  2276. test_bit(DPC_AEN, &ha->dpc_flags)) {
  2277. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  2278. " - dpc flags = 0x%lx\n",
  2279. ha->host_no, __func__, ha->dpc_flags));
  2280. qla4xxx_wake_dpc(ha);
  2281. }
  2282. /* Reschedule timer thread to call us back in one second */
  2283. mod_timer(&ha->timer, jiffies + HZ);
  2284. DEBUG2(ha->seconds_since_last_intr++);
  2285. }
  2286. /**
  2287. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  2288. * @ha: Pointer to host adapter structure.
  2289. *
  2290. * This routine stalls the driver until all outstanding commands are returned.
  2291. * Caller must release the Hardware Lock prior to calling this routine.
  2292. **/
  2293. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  2294. {
  2295. uint32_t index = 0;
  2296. unsigned long flags;
  2297. struct scsi_cmnd *cmd;
  2298. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  2299. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  2300. "complete\n", WAIT_CMD_TOV));
  2301. while (!time_after_eq(jiffies, wtime)) {
  2302. spin_lock_irqsave(&ha->hardware_lock, flags);
  2303. /* Find a command that hasn't completed. */
  2304. for (index = 0; index < ha->host->can_queue; index++) {
  2305. cmd = scsi_host_find_tag(ha->host, index);
  2306. /*
  2307. * We cannot just check if the index is valid,
  2308. * becase if we are run from the scsi eh, then
  2309. * the scsi/block layer is going to prevent
  2310. * the tag from being released.
  2311. */
  2312. if (cmd != NULL && CMD_SP(cmd))
  2313. break;
  2314. }
  2315. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2316. /* If No Commands are pending, wait is complete */
  2317. if (index == ha->host->can_queue)
  2318. return QLA_SUCCESS;
  2319. msleep(1000);
  2320. }
  2321. /* If we timed out on waiting for commands to come back
  2322. * return ERROR. */
  2323. return QLA_ERROR;
  2324. }
  2325. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  2326. {
  2327. uint32_t ctrl_status;
  2328. unsigned long flags = 0;
  2329. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  2330. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  2331. return QLA_ERROR;
  2332. spin_lock_irqsave(&ha->hardware_lock, flags);
  2333. /*
  2334. * If the SCSI Reset Interrupt bit is set, clear it.
  2335. * Otherwise, the Soft Reset won't work.
  2336. */
  2337. ctrl_status = readw(&ha->reg->ctrl_status);
  2338. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  2339. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  2340. /* Issue Soft Reset */
  2341. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  2342. readl(&ha->reg->ctrl_status);
  2343. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2344. return QLA_SUCCESS;
  2345. }
  2346. /**
  2347. * qla4xxx_soft_reset - performs soft reset.
  2348. * @ha: Pointer to host adapter structure.
  2349. **/
  2350. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  2351. {
  2352. uint32_t max_wait_time;
  2353. unsigned long flags = 0;
  2354. int status;
  2355. uint32_t ctrl_status;
  2356. status = qla4xxx_hw_reset(ha);
  2357. if (status != QLA_SUCCESS)
  2358. return status;
  2359. status = QLA_ERROR;
  2360. /* Wait until the Network Reset Intr bit is cleared */
  2361. max_wait_time = RESET_INTR_TOV;
  2362. do {
  2363. spin_lock_irqsave(&ha->hardware_lock, flags);
  2364. ctrl_status = readw(&ha->reg->ctrl_status);
  2365. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2366. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  2367. break;
  2368. msleep(1000);
  2369. } while ((--max_wait_time));
  2370. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  2371. DEBUG2(printk(KERN_WARNING
  2372. "scsi%ld: Network Reset Intr not cleared by "
  2373. "Network function, clearing it now!\n",
  2374. ha->host_no));
  2375. spin_lock_irqsave(&ha->hardware_lock, flags);
  2376. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  2377. readl(&ha->reg->ctrl_status);
  2378. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2379. }
  2380. /* Wait until the firmware tells us the Soft Reset is done */
  2381. max_wait_time = SOFT_RESET_TOV;
  2382. do {
  2383. spin_lock_irqsave(&ha->hardware_lock, flags);
  2384. ctrl_status = readw(&ha->reg->ctrl_status);
  2385. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2386. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  2387. status = QLA_SUCCESS;
  2388. break;
  2389. }
  2390. msleep(1000);
  2391. } while ((--max_wait_time));
  2392. /*
  2393. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  2394. * after the soft reset has taken place.
  2395. */
  2396. spin_lock_irqsave(&ha->hardware_lock, flags);
  2397. ctrl_status = readw(&ha->reg->ctrl_status);
  2398. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  2399. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  2400. readl(&ha->reg->ctrl_status);
  2401. }
  2402. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2403. /* If soft reset fails then most probably the bios on other
  2404. * function is also enabled.
  2405. * Since the initialization is sequential the other fn
  2406. * wont be able to acknowledge the soft reset.
  2407. * Issue a force soft reset to workaround this scenario.
  2408. */
  2409. if (max_wait_time == 0) {
  2410. /* Issue Force Soft Reset */
  2411. spin_lock_irqsave(&ha->hardware_lock, flags);
  2412. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  2413. readl(&ha->reg->ctrl_status);
  2414. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2415. /* Wait until the firmware tells us the Soft Reset is done */
  2416. max_wait_time = SOFT_RESET_TOV;
  2417. do {
  2418. spin_lock_irqsave(&ha->hardware_lock, flags);
  2419. ctrl_status = readw(&ha->reg->ctrl_status);
  2420. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2421. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  2422. status = QLA_SUCCESS;
  2423. break;
  2424. }
  2425. msleep(1000);
  2426. } while ((--max_wait_time));
  2427. }
  2428. return status;
  2429. }
  2430. /**
  2431. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  2432. * @ha: Pointer to host adapter structure.
  2433. * @res: returned scsi status
  2434. *
  2435. * This routine is called just prior to a HARD RESET to return all
  2436. * outstanding commands back to the Operating System.
  2437. * Caller should make sure that the following locks are released
  2438. * before this calling routine: Hardware lock, and io_request_lock.
  2439. **/
  2440. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  2441. {
  2442. struct srb *srb;
  2443. int i;
  2444. unsigned long flags;
  2445. spin_lock_irqsave(&ha->hardware_lock, flags);
  2446. for (i = 0; i < ha->host->can_queue; i++) {
  2447. srb = qla4xxx_del_from_active_array(ha, i);
  2448. if (srb != NULL) {
  2449. srb->cmd->result = res;
  2450. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  2451. }
  2452. }
  2453. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2454. }
  2455. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  2456. {
  2457. clear_bit(AF_ONLINE, &ha->flags);
  2458. /* Disable the board */
  2459. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  2460. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2461. qla4xxx_mark_all_devices_missing(ha);
  2462. clear_bit(AF_INIT_DONE, &ha->flags);
  2463. }
  2464. static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
  2465. {
  2466. struct iscsi_session *sess;
  2467. struct ddb_entry *ddb_entry;
  2468. sess = cls_session->dd_data;
  2469. ddb_entry = sess->dd_data;
  2470. ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
  2471. if (ddb_entry->ddb_type == FLASH_DDB)
  2472. iscsi_block_session(ddb_entry->sess);
  2473. else
  2474. iscsi_session_failure(cls_session->dd_data,
  2475. ISCSI_ERR_CONN_FAILED);
  2476. }
  2477. /**
  2478. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  2479. * @ha: Pointer to host adapter structure.
  2480. **/
  2481. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  2482. {
  2483. int status = QLA_ERROR;
  2484. uint8_t reset_chip = 0;
  2485. uint32_t dev_state;
  2486. unsigned long wait;
  2487. /* Stall incoming I/O until we are done */
  2488. scsi_block_requests(ha->host);
  2489. clear_bit(AF_ONLINE, &ha->flags);
  2490. clear_bit(AF_LINK_UP, &ha->flags);
  2491. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  2492. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2493. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  2494. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  2495. reset_chip = 1;
  2496. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  2497. * do not reset adapter, jump to initialize_adapter */
  2498. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2499. status = QLA_SUCCESS;
  2500. goto recover_ha_init_adapter;
  2501. }
  2502. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  2503. * from eh_host_reset or ioctl module */
  2504. if (is_qla8022(ha) && !reset_chip &&
  2505. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  2506. DEBUG2(ql4_printk(KERN_INFO, ha,
  2507. "scsi%ld: %s - Performing stop_firmware...\n",
  2508. ha->host_no, __func__));
  2509. status = ha->isp_ops->reset_firmware(ha);
  2510. if (status == QLA_SUCCESS) {
  2511. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  2512. qla4xxx_cmd_wait(ha);
  2513. ha->isp_ops->disable_intrs(ha);
  2514. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2515. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2516. } else {
  2517. /* If the stop_firmware fails then
  2518. * reset the entire chip */
  2519. reset_chip = 1;
  2520. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2521. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2522. }
  2523. }
  2524. /* Issue full chip reset if recovering from a catastrophic error,
  2525. * or if stop_firmware fails for ISP-82xx.
  2526. * This is the default case for ISP-4xxx */
  2527. if (!is_qla8022(ha) || reset_chip) {
  2528. if (!is_qla8022(ha))
  2529. goto chip_reset;
  2530. /* Check if 82XX firmware is alive or not
  2531. * We may have arrived here from NEED_RESET
  2532. * detection only */
  2533. if (test_bit(AF_FW_RECOVERY, &ha->flags))
  2534. goto chip_reset;
  2535. wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
  2536. while (time_before(jiffies, wait)) {
  2537. if (qla4_8xxx_check_fw_alive(ha)) {
  2538. qla4xxx_mailbox_premature_completion(ha);
  2539. break;
  2540. }
  2541. set_current_state(TASK_UNINTERRUPTIBLE);
  2542. schedule_timeout(HZ);
  2543. }
  2544. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  2545. qla4xxx_cmd_wait(ha);
  2546. chip_reset:
  2547. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2548. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2549. DEBUG2(ql4_printk(KERN_INFO, ha,
  2550. "scsi%ld: %s - Performing chip reset..\n",
  2551. ha->host_no, __func__));
  2552. status = ha->isp_ops->reset_chip(ha);
  2553. }
  2554. /* Flush any pending ddb changed AENs */
  2555. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2556. recover_ha_init_adapter:
  2557. /* Upon successful firmware/chip reset, re-initialize the adapter */
  2558. if (status == QLA_SUCCESS) {
  2559. /* For ISP-4xxx, force function 1 to always initialize
  2560. * before function 3 to prevent both funcions from
  2561. * stepping on top of the other */
  2562. if (!is_qla8022(ha) && (ha->mac_index == 3))
  2563. ssleep(6);
  2564. /* NOTE: AF_ONLINE flag set upon successful completion of
  2565. * qla4xxx_initialize_adapter */
  2566. status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  2567. }
  2568. /* Retry failed adapter initialization, if necessary
  2569. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  2570. * case to prevent ping-pong resets between functions */
  2571. if (!test_bit(AF_ONLINE, &ha->flags) &&
  2572. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2573. /* Adapter initialization failed, see if we can retry
  2574. * resetting the ha.
  2575. * Since we don't want to block the DPC for too long
  2576. * with multiple resets in the same thread,
  2577. * utilize DPC to retry */
  2578. if (is_qla8022(ha)) {
  2579. qla4_8xxx_idc_lock(ha);
  2580. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  2581. qla4_8xxx_idc_unlock(ha);
  2582. if (dev_state == QLA82XX_DEV_FAILED) {
  2583. ql4_printk(KERN_INFO, ha, "%s: don't retry "
  2584. "recover adapter. H/W is in Failed "
  2585. "state\n", __func__);
  2586. qla4xxx_dead_adapter_cleanup(ha);
  2587. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2588. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2589. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  2590. &ha->dpc_flags);
  2591. status = QLA_ERROR;
  2592. goto exit_recover;
  2593. }
  2594. }
  2595. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  2596. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  2597. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  2598. "(%d) more times\n", ha->host_no,
  2599. ha->retry_reset_ha_cnt));
  2600. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2601. status = QLA_ERROR;
  2602. } else {
  2603. if (ha->retry_reset_ha_cnt > 0) {
  2604. /* Schedule another Reset HA--DPC will retry */
  2605. ha->retry_reset_ha_cnt--;
  2606. DEBUG2(printk("scsi%ld: recover adapter - "
  2607. "retry remaining %d\n",
  2608. ha->host_no,
  2609. ha->retry_reset_ha_cnt));
  2610. status = QLA_ERROR;
  2611. }
  2612. if (ha->retry_reset_ha_cnt == 0) {
  2613. /* Recover adapter retries have been exhausted.
  2614. * Adapter DEAD */
  2615. DEBUG2(printk("scsi%ld: recover adapter "
  2616. "failed - board disabled\n",
  2617. ha->host_no));
  2618. qla4xxx_dead_adapter_cleanup(ha);
  2619. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2620. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2621. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  2622. &ha->dpc_flags);
  2623. status = QLA_ERROR;
  2624. }
  2625. }
  2626. } else {
  2627. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2628. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2629. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2630. }
  2631. exit_recover:
  2632. ha->adapter_error_count++;
  2633. if (test_bit(AF_ONLINE, &ha->flags))
  2634. ha->isp_ops->enable_intrs(ha);
  2635. scsi_unblock_requests(ha->host);
  2636. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2637. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  2638. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  2639. return status;
  2640. }
  2641. static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
  2642. {
  2643. struct iscsi_session *sess;
  2644. struct ddb_entry *ddb_entry;
  2645. struct scsi_qla_host *ha;
  2646. sess = cls_session->dd_data;
  2647. ddb_entry = sess->dd_data;
  2648. ha = ddb_entry->ha;
  2649. if (!iscsi_is_session_online(cls_session)) {
  2650. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  2651. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2652. " unblock session\n", ha->host_no, __func__,
  2653. ddb_entry->fw_ddb_index);
  2654. iscsi_unblock_session(ddb_entry->sess);
  2655. } else {
  2656. /* Trigger relogin */
  2657. if (ddb_entry->ddb_type == FLASH_DDB) {
  2658. if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
  2659. qla4xxx_arm_relogin_timer(ddb_entry);
  2660. } else
  2661. iscsi_session_failure(cls_session->dd_data,
  2662. ISCSI_ERR_CONN_FAILED);
  2663. }
  2664. }
  2665. }
  2666. int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
  2667. {
  2668. struct iscsi_session *sess;
  2669. struct ddb_entry *ddb_entry;
  2670. struct scsi_qla_host *ha;
  2671. sess = cls_session->dd_data;
  2672. ddb_entry = sess->dd_data;
  2673. ha = ddb_entry->ha;
  2674. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2675. " unblock session\n", ha->host_no, __func__,
  2676. ddb_entry->fw_ddb_index);
  2677. iscsi_unblock_session(ddb_entry->sess);
  2678. /* Start scan target */
  2679. if (test_bit(AF_ONLINE, &ha->flags)) {
  2680. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2681. " start scan\n", ha->host_no, __func__,
  2682. ddb_entry->fw_ddb_index);
  2683. scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
  2684. }
  2685. return QLA_SUCCESS;
  2686. }
  2687. int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
  2688. {
  2689. struct iscsi_session *sess;
  2690. struct ddb_entry *ddb_entry;
  2691. struct scsi_qla_host *ha;
  2692. sess = cls_session->dd_data;
  2693. ddb_entry = sess->dd_data;
  2694. ha = ddb_entry->ha;
  2695. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2696. " unblock user space session\n", ha->host_no, __func__,
  2697. ddb_entry->fw_ddb_index);
  2698. iscsi_conn_start(ddb_entry->conn);
  2699. iscsi_conn_login_event(ddb_entry->conn,
  2700. ISCSI_CONN_STATE_LOGGED_IN);
  2701. return QLA_SUCCESS;
  2702. }
  2703. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  2704. {
  2705. iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
  2706. }
  2707. static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  2708. {
  2709. uint16_t relogin_timer;
  2710. struct iscsi_session *sess;
  2711. struct ddb_entry *ddb_entry;
  2712. struct scsi_qla_host *ha;
  2713. sess = cls_sess->dd_data;
  2714. ddb_entry = sess->dd_data;
  2715. ha = ddb_entry->ha;
  2716. relogin_timer = max(ddb_entry->default_relogin_timeout,
  2717. (uint16_t)RELOGIN_TOV);
  2718. atomic_set(&ddb_entry->relogin_timer, relogin_timer);
  2719. DEBUG2(ql4_printk(KERN_INFO, ha,
  2720. "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
  2721. ddb_entry->fw_ddb_index, relogin_timer));
  2722. qla4xxx_login_flash_ddb(cls_sess);
  2723. }
  2724. static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
  2725. {
  2726. struct iscsi_session *sess;
  2727. struct ddb_entry *ddb_entry;
  2728. struct scsi_qla_host *ha;
  2729. sess = cls_sess->dd_data;
  2730. ddb_entry = sess->dd_data;
  2731. ha = ddb_entry->ha;
  2732. if (!(ddb_entry->ddb_type == FLASH_DDB))
  2733. return;
  2734. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  2735. !iscsi_is_session_online(cls_sess)) {
  2736. DEBUG2(ql4_printk(KERN_INFO, ha,
  2737. "relogin issued\n"));
  2738. qla4xxx_relogin_flash_ddb(cls_sess);
  2739. }
  2740. }
  2741. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  2742. {
  2743. if (ha->dpc_thread)
  2744. queue_work(ha->dpc_thread, &ha->dpc_work);
  2745. }
  2746. static struct qla4_work_evt *
  2747. qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
  2748. enum qla4_work_type type)
  2749. {
  2750. struct qla4_work_evt *e;
  2751. uint32_t size = sizeof(struct qla4_work_evt) + data_size;
  2752. e = kzalloc(size, GFP_ATOMIC);
  2753. if (!e)
  2754. return NULL;
  2755. INIT_LIST_HEAD(&e->list);
  2756. e->type = type;
  2757. return e;
  2758. }
  2759. static void qla4xxx_post_work(struct scsi_qla_host *ha,
  2760. struct qla4_work_evt *e)
  2761. {
  2762. unsigned long flags;
  2763. spin_lock_irqsave(&ha->work_lock, flags);
  2764. list_add_tail(&e->list, &ha->work_list);
  2765. spin_unlock_irqrestore(&ha->work_lock, flags);
  2766. qla4xxx_wake_dpc(ha);
  2767. }
  2768. int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
  2769. enum iscsi_host_event_code aen_code,
  2770. uint32_t data_size, uint8_t *data)
  2771. {
  2772. struct qla4_work_evt *e;
  2773. e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
  2774. if (!e)
  2775. return QLA_ERROR;
  2776. e->u.aen.code = aen_code;
  2777. e->u.aen.data_size = data_size;
  2778. memcpy(e->u.aen.data, data, data_size);
  2779. qla4xxx_post_work(ha, e);
  2780. return QLA_SUCCESS;
  2781. }
  2782. int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
  2783. uint32_t status, uint32_t pid,
  2784. uint32_t data_size, uint8_t *data)
  2785. {
  2786. struct qla4_work_evt *e;
  2787. e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
  2788. if (!e)
  2789. return QLA_ERROR;
  2790. e->u.ping.status = status;
  2791. e->u.ping.pid = pid;
  2792. e->u.ping.data_size = data_size;
  2793. memcpy(e->u.ping.data, data, data_size);
  2794. qla4xxx_post_work(ha, e);
  2795. return QLA_SUCCESS;
  2796. }
  2797. static void qla4xxx_do_work(struct scsi_qla_host *ha)
  2798. {
  2799. struct qla4_work_evt *e, *tmp;
  2800. unsigned long flags;
  2801. LIST_HEAD(work);
  2802. spin_lock_irqsave(&ha->work_lock, flags);
  2803. list_splice_init(&ha->work_list, &work);
  2804. spin_unlock_irqrestore(&ha->work_lock, flags);
  2805. list_for_each_entry_safe(e, tmp, &work, list) {
  2806. list_del_init(&e->list);
  2807. switch (e->type) {
  2808. case QLA4_EVENT_AEN:
  2809. iscsi_post_host_event(ha->host_no,
  2810. &qla4xxx_iscsi_transport,
  2811. e->u.aen.code,
  2812. e->u.aen.data_size,
  2813. e->u.aen.data);
  2814. break;
  2815. case QLA4_EVENT_PING_STATUS:
  2816. iscsi_ping_comp_event(ha->host_no,
  2817. &qla4xxx_iscsi_transport,
  2818. e->u.ping.status,
  2819. e->u.ping.pid,
  2820. e->u.ping.data_size,
  2821. e->u.ping.data);
  2822. break;
  2823. default:
  2824. ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
  2825. "supported", e->type);
  2826. }
  2827. kfree(e);
  2828. }
  2829. }
  2830. /**
  2831. * qla4xxx_do_dpc - dpc routine
  2832. * @data: in our case pointer to adapter structure
  2833. *
  2834. * This routine is a task that is schedule by the interrupt handler
  2835. * to perform the background processing for interrupts. We put it
  2836. * on a task queue that is consumed whenever the scheduler runs; that's
  2837. * so you can do anything (i.e. put the process to sleep etc). In fact,
  2838. * the mid-level tries to sleep when it reaches the driver threshold
  2839. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  2840. **/
  2841. static void qla4xxx_do_dpc(struct work_struct *work)
  2842. {
  2843. struct scsi_qla_host *ha =
  2844. container_of(work, struct scsi_qla_host, dpc_work);
  2845. int status = QLA_ERROR;
  2846. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  2847. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  2848. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  2849. /* Initialization not yet finished. Don't do anything yet. */
  2850. if (!test_bit(AF_INIT_DONE, &ha->flags))
  2851. return;
  2852. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  2853. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  2854. ha->host_no, __func__, ha->flags));
  2855. return;
  2856. }
  2857. /* post events to application */
  2858. qla4xxx_do_work(ha);
  2859. if (is_qla8022(ha)) {
  2860. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  2861. qla4_8xxx_idc_lock(ha);
  2862. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2863. QLA82XX_DEV_FAILED);
  2864. qla4_8xxx_idc_unlock(ha);
  2865. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  2866. qla4_8xxx_device_state_handler(ha);
  2867. }
  2868. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  2869. qla4_8xxx_need_qsnt_handler(ha);
  2870. }
  2871. }
  2872. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  2873. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2874. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  2875. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  2876. if (ql4xdontresethba) {
  2877. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  2878. ha->host_no, __func__));
  2879. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2880. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2881. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2882. goto dpc_post_reset_ha;
  2883. }
  2884. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  2885. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  2886. qla4xxx_recover_adapter(ha);
  2887. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2888. uint8_t wait_time = RESET_INTR_TOV;
  2889. while ((readw(&ha->reg->ctrl_status) &
  2890. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  2891. if (--wait_time == 0)
  2892. break;
  2893. msleep(1000);
  2894. }
  2895. if (wait_time == 0)
  2896. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  2897. "bit not cleared-- resetting\n",
  2898. ha->host_no, __func__));
  2899. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2900. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  2901. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2902. status = qla4xxx_recover_adapter(ha);
  2903. }
  2904. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2905. if (status == QLA_SUCCESS)
  2906. ha->isp_ops->enable_intrs(ha);
  2907. }
  2908. }
  2909. dpc_post_reset_ha:
  2910. /* ---- process AEN? --- */
  2911. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  2912. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  2913. /* ---- Get DHCP IP Address? --- */
  2914. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  2915. qla4xxx_get_dhcp_ip_address(ha);
  2916. /* ---- relogin device? --- */
  2917. if (adapter_up(ha) &&
  2918. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  2919. iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
  2920. }
  2921. /* ---- link change? --- */
  2922. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  2923. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  2924. /* ---- link down? --- */
  2925. qla4xxx_mark_all_devices_missing(ha);
  2926. } else {
  2927. /* ---- link up? --- *
  2928. * F/W will auto login to all devices ONLY ONCE after
  2929. * link up during driver initialization and runtime
  2930. * fatal error recovery. Therefore, the driver must
  2931. * manually relogin to devices when recovering from
  2932. * connection failures, logouts, expired KATO, etc. */
  2933. if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
  2934. qla4xxx_build_ddb_list(ha, ha->is_reset);
  2935. iscsi_host_for_each_session(ha->host,
  2936. qla4xxx_login_flash_ddb);
  2937. } else
  2938. qla4xxx_relogin_all_devices(ha);
  2939. }
  2940. }
  2941. }
  2942. /**
  2943. * qla4xxx_free_adapter - release the adapter
  2944. * @ha: pointer to adapter structure
  2945. **/
  2946. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  2947. {
  2948. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2949. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  2950. /* Turn-off interrupts on the card. */
  2951. ha->isp_ops->disable_intrs(ha);
  2952. }
  2953. /* Remove timer thread, if present */
  2954. if (ha->timer_active)
  2955. qla4xxx_stop_timer(ha);
  2956. /* Kill the kernel thread for this host */
  2957. if (ha->dpc_thread)
  2958. destroy_workqueue(ha->dpc_thread);
  2959. /* Kill the kernel thread for this host */
  2960. if (ha->task_wq)
  2961. destroy_workqueue(ha->task_wq);
  2962. /* Put firmware in known state */
  2963. ha->isp_ops->reset_firmware(ha);
  2964. if (is_qla8022(ha)) {
  2965. qla4_8xxx_idc_lock(ha);
  2966. qla4_8xxx_clear_drv_active(ha);
  2967. qla4_8xxx_idc_unlock(ha);
  2968. }
  2969. /* Detach interrupts */
  2970. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  2971. qla4xxx_free_irqs(ha);
  2972. /* free extra memory */
  2973. qla4xxx_mem_free(ha);
  2974. }
  2975. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  2976. {
  2977. int status = 0;
  2978. unsigned long mem_base, mem_len, db_base, db_len;
  2979. struct pci_dev *pdev = ha->pdev;
  2980. status = pci_request_regions(pdev, DRIVER_NAME);
  2981. if (status) {
  2982. printk(KERN_WARNING
  2983. "scsi(%ld) Failed to reserve PIO regions (%s) "
  2984. "status=%d\n", ha->host_no, pci_name(pdev), status);
  2985. goto iospace_error_exit;
  2986. }
  2987. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  2988. __func__, pdev->revision));
  2989. ha->revision_id = pdev->revision;
  2990. /* remap phys address */
  2991. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  2992. mem_len = pci_resource_len(pdev, 0);
  2993. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  2994. __func__, mem_base, mem_len));
  2995. /* mapping of pcibase pointer */
  2996. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  2997. if (!ha->nx_pcibase) {
  2998. printk(KERN_ERR
  2999. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  3000. pci_release_regions(ha->pdev);
  3001. goto iospace_error_exit;
  3002. }
  3003. /* Mapping of IO base pointer, door bell read and write pointer */
  3004. /* mapping of IO base pointer */
  3005. ha->qla4_8xxx_reg =
  3006. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  3007. 0xbc000 + (ha->pdev->devfn << 11));
  3008. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  3009. db_len = pci_resource_len(pdev, 4);
  3010. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  3011. QLA82XX_CAM_RAM_DB2);
  3012. return 0;
  3013. iospace_error_exit:
  3014. return -ENOMEM;
  3015. }
  3016. /***
  3017. * qla4xxx_iospace_config - maps registers
  3018. * @ha: pointer to adapter structure
  3019. *
  3020. * This routines maps HBA's registers from the pci address space
  3021. * into the kernel virtual address space for memory mapped i/o.
  3022. **/
  3023. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  3024. {
  3025. unsigned long pio, pio_len, pio_flags;
  3026. unsigned long mmio, mmio_len, mmio_flags;
  3027. pio = pci_resource_start(ha->pdev, 0);
  3028. pio_len = pci_resource_len(ha->pdev, 0);
  3029. pio_flags = pci_resource_flags(ha->pdev, 0);
  3030. if (pio_flags & IORESOURCE_IO) {
  3031. if (pio_len < MIN_IOBASE_LEN) {
  3032. ql4_printk(KERN_WARNING, ha,
  3033. "Invalid PCI I/O region size\n");
  3034. pio = 0;
  3035. }
  3036. } else {
  3037. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  3038. pio = 0;
  3039. }
  3040. /* Use MMIO operations for all accesses. */
  3041. mmio = pci_resource_start(ha->pdev, 1);
  3042. mmio_len = pci_resource_len(ha->pdev, 1);
  3043. mmio_flags = pci_resource_flags(ha->pdev, 1);
  3044. if (!(mmio_flags & IORESOURCE_MEM)) {
  3045. ql4_printk(KERN_ERR, ha,
  3046. "region #0 not an MMIO resource, aborting\n");
  3047. goto iospace_error_exit;
  3048. }
  3049. if (mmio_len < MIN_IOBASE_LEN) {
  3050. ql4_printk(KERN_ERR, ha,
  3051. "Invalid PCI mem region size, aborting\n");
  3052. goto iospace_error_exit;
  3053. }
  3054. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  3055. ql4_printk(KERN_WARNING, ha,
  3056. "Failed to reserve PIO/MMIO regions\n");
  3057. goto iospace_error_exit;
  3058. }
  3059. ha->pio_address = pio;
  3060. ha->pio_length = pio_len;
  3061. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  3062. if (!ha->reg) {
  3063. ql4_printk(KERN_ERR, ha,
  3064. "cannot remap MMIO, aborting\n");
  3065. goto iospace_error_exit;
  3066. }
  3067. return 0;
  3068. iospace_error_exit:
  3069. return -ENOMEM;
  3070. }
  3071. static struct isp_operations qla4xxx_isp_ops = {
  3072. .iospace_config = qla4xxx_iospace_config,
  3073. .pci_config = qla4xxx_pci_config,
  3074. .disable_intrs = qla4xxx_disable_intrs,
  3075. .enable_intrs = qla4xxx_enable_intrs,
  3076. .start_firmware = qla4xxx_start_firmware,
  3077. .intr_handler = qla4xxx_intr_handler,
  3078. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  3079. .reset_chip = qla4xxx_soft_reset,
  3080. .reset_firmware = qla4xxx_hw_reset,
  3081. .queue_iocb = qla4xxx_queue_iocb,
  3082. .complete_iocb = qla4xxx_complete_iocb,
  3083. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  3084. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  3085. .get_sys_info = qla4xxx_get_sys_info,
  3086. };
  3087. static struct isp_operations qla4_8xxx_isp_ops = {
  3088. .iospace_config = qla4_8xxx_iospace_config,
  3089. .pci_config = qla4_8xxx_pci_config,
  3090. .disable_intrs = qla4_8xxx_disable_intrs,
  3091. .enable_intrs = qla4_8xxx_enable_intrs,
  3092. .start_firmware = qla4_8xxx_load_risc,
  3093. .intr_handler = qla4_8xxx_intr_handler,
  3094. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  3095. .reset_chip = qla4_8xxx_isp_reset,
  3096. .reset_firmware = qla4_8xxx_stop_firmware,
  3097. .queue_iocb = qla4_8xxx_queue_iocb,
  3098. .complete_iocb = qla4_8xxx_complete_iocb,
  3099. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  3100. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  3101. .get_sys_info = qla4_8xxx_get_sys_info,
  3102. };
  3103. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  3104. {
  3105. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  3106. }
  3107. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  3108. {
  3109. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  3110. }
  3111. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  3112. {
  3113. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  3114. }
  3115. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  3116. {
  3117. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  3118. }
  3119. static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
  3120. {
  3121. struct scsi_qla_host *ha = data;
  3122. char *str = buf;
  3123. int rc;
  3124. switch (type) {
  3125. case ISCSI_BOOT_ETH_FLAGS:
  3126. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  3127. break;
  3128. case ISCSI_BOOT_ETH_INDEX:
  3129. rc = sprintf(str, "0\n");
  3130. break;
  3131. case ISCSI_BOOT_ETH_MAC:
  3132. rc = sysfs_format_mac(str, ha->my_mac,
  3133. MAC_ADDR_LEN);
  3134. break;
  3135. default:
  3136. rc = -ENOSYS;
  3137. break;
  3138. }
  3139. return rc;
  3140. }
  3141. static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
  3142. {
  3143. int rc;
  3144. switch (type) {
  3145. case ISCSI_BOOT_ETH_FLAGS:
  3146. case ISCSI_BOOT_ETH_MAC:
  3147. case ISCSI_BOOT_ETH_INDEX:
  3148. rc = S_IRUGO;
  3149. break;
  3150. default:
  3151. rc = 0;
  3152. break;
  3153. }
  3154. return rc;
  3155. }
  3156. static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
  3157. {
  3158. struct scsi_qla_host *ha = data;
  3159. char *str = buf;
  3160. int rc;
  3161. switch (type) {
  3162. case ISCSI_BOOT_INI_INITIATOR_NAME:
  3163. rc = sprintf(str, "%s\n", ha->name_string);
  3164. break;
  3165. default:
  3166. rc = -ENOSYS;
  3167. break;
  3168. }
  3169. return rc;
  3170. }
  3171. static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
  3172. {
  3173. int rc;
  3174. switch (type) {
  3175. case ISCSI_BOOT_INI_INITIATOR_NAME:
  3176. rc = S_IRUGO;
  3177. break;
  3178. default:
  3179. rc = 0;
  3180. break;
  3181. }
  3182. return rc;
  3183. }
  3184. static ssize_t
  3185. qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
  3186. char *buf)
  3187. {
  3188. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  3189. char *str = buf;
  3190. int rc;
  3191. switch (type) {
  3192. case ISCSI_BOOT_TGT_NAME:
  3193. rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
  3194. break;
  3195. case ISCSI_BOOT_TGT_IP_ADDR:
  3196. if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
  3197. rc = sprintf(buf, "%pI4\n",
  3198. &boot_conn->dest_ipaddr.ip_address);
  3199. else
  3200. rc = sprintf(str, "%pI6\n",
  3201. &boot_conn->dest_ipaddr.ip_address);
  3202. break;
  3203. case ISCSI_BOOT_TGT_PORT:
  3204. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  3205. break;
  3206. case ISCSI_BOOT_TGT_CHAP_NAME:
  3207. rc = sprintf(str, "%.*s\n",
  3208. boot_conn->chap.target_chap_name_length,
  3209. (char *)&boot_conn->chap.target_chap_name);
  3210. break;
  3211. case ISCSI_BOOT_TGT_CHAP_SECRET:
  3212. rc = sprintf(str, "%.*s\n",
  3213. boot_conn->chap.target_secret_length,
  3214. (char *)&boot_conn->chap.target_secret);
  3215. break;
  3216. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  3217. rc = sprintf(str, "%.*s\n",
  3218. boot_conn->chap.intr_chap_name_length,
  3219. (char *)&boot_conn->chap.intr_chap_name);
  3220. break;
  3221. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  3222. rc = sprintf(str, "%.*s\n",
  3223. boot_conn->chap.intr_secret_length,
  3224. (char *)&boot_conn->chap.intr_secret);
  3225. break;
  3226. case ISCSI_BOOT_TGT_FLAGS:
  3227. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  3228. break;
  3229. case ISCSI_BOOT_TGT_NIC_ASSOC:
  3230. rc = sprintf(str, "0\n");
  3231. break;
  3232. default:
  3233. rc = -ENOSYS;
  3234. break;
  3235. }
  3236. return rc;
  3237. }
  3238. static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
  3239. {
  3240. struct scsi_qla_host *ha = data;
  3241. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
  3242. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  3243. }
  3244. static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
  3245. {
  3246. struct scsi_qla_host *ha = data;
  3247. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
  3248. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  3249. }
  3250. static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
  3251. {
  3252. int rc;
  3253. switch (type) {
  3254. case ISCSI_BOOT_TGT_NAME:
  3255. case ISCSI_BOOT_TGT_IP_ADDR:
  3256. case ISCSI_BOOT_TGT_PORT:
  3257. case ISCSI_BOOT_TGT_CHAP_NAME:
  3258. case ISCSI_BOOT_TGT_CHAP_SECRET:
  3259. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  3260. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  3261. case ISCSI_BOOT_TGT_NIC_ASSOC:
  3262. case ISCSI_BOOT_TGT_FLAGS:
  3263. rc = S_IRUGO;
  3264. break;
  3265. default:
  3266. rc = 0;
  3267. break;
  3268. }
  3269. return rc;
  3270. }
  3271. static void qla4xxx_boot_release(void *data)
  3272. {
  3273. struct scsi_qla_host *ha = data;
  3274. scsi_host_put(ha->host);
  3275. }
  3276. static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
  3277. {
  3278. dma_addr_t buf_dma;
  3279. uint32_t addr, pri_addr, sec_addr;
  3280. uint32_t offset;
  3281. uint16_t func_num;
  3282. uint8_t val;
  3283. uint8_t *buf = NULL;
  3284. size_t size = 13 * sizeof(uint8_t);
  3285. int ret = QLA_SUCCESS;
  3286. func_num = PCI_FUNC(ha->pdev->devfn);
  3287. ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
  3288. __func__, ha->pdev->device, func_num);
  3289. if (is_qla40XX(ha)) {
  3290. if (func_num == 1) {
  3291. addr = NVRAM_PORT0_BOOT_MODE;
  3292. pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
  3293. sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
  3294. } else if (func_num == 3) {
  3295. addr = NVRAM_PORT1_BOOT_MODE;
  3296. pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
  3297. sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
  3298. } else {
  3299. ret = QLA_ERROR;
  3300. goto exit_boot_info;
  3301. }
  3302. /* Check Boot Mode */
  3303. val = rd_nvram_byte(ha, addr);
  3304. if (!(val & 0x07)) {
  3305. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
  3306. "options : 0x%x\n", __func__, val));
  3307. ret = QLA_ERROR;
  3308. goto exit_boot_info;
  3309. }
  3310. /* get primary valid target index */
  3311. val = rd_nvram_byte(ha, pri_addr);
  3312. if (val & BIT_7)
  3313. ddb_index[0] = (val & 0x7f);
  3314. /* get secondary valid target index */
  3315. val = rd_nvram_byte(ha, sec_addr);
  3316. if (val & BIT_7)
  3317. ddb_index[1] = (val & 0x7f);
  3318. } else if (is_qla8022(ha)) {
  3319. buf = dma_alloc_coherent(&ha->pdev->dev, size,
  3320. &buf_dma, GFP_KERNEL);
  3321. if (!buf) {
  3322. DEBUG2(ql4_printk(KERN_ERR, ha,
  3323. "%s: Unable to allocate dma buffer\n",
  3324. __func__));
  3325. ret = QLA_ERROR;
  3326. goto exit_boot_info;
  3327. }
  3328. if (ha->port_num == 0)
  3329. offset = BOOT_PARAM_OFFSET_PORT0;
  3330. else if (ha->port_num == 1)
  3331. offset = BOOT_PARAM_OFFSET_PORT1;
  3332. else {
  3333. ret = QLA_ERROR;
  3334. goto exit_boot_info_free;
  3335. }
  3336. addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
  3337. offset;
  3338. if (qla4xxx_get_flash(ha, buf_dma, addr,
  3339. 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
  3340. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
  3341. " failed\n", ha->host_no, __func__));
  3342. ret = QLA_ERROR;
  3343. goto exit_boot_info_free;
  3344. }
  3345. /* Check Boot Mode */
  3346. if (!(buf[1] & 0x07)) {
  3347. DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
  3348. " : 0x%x\n", buf[1]));
  3349. ret = QLA_ERROR;
  3350. goto exit_boot_info_free;
  3351. }
  3352. /* get primary valid target index */
  3353. if (buf[2] & BIT_7)
  3354. ddb_index[0] = buf[2] & 0x7f;
  3355. /* get secondary valid target index */
  3356. if (buf[11] & BIT_7)
  3357. ddb_index[1] = buf[11] & 0x7f;
  3358. } else {
  3359. ret = QLA_ERROR;
  3360. goto exit_boot_info;
  3361. }
  3362. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
  3363. " target ID %d\n", __func__, ddb_index[0],
  3364. ddb_index[1]));
  3365. exit_boot_info_free:
  3366. dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
  3367. exit_boot_info:
  3368. ha->pri_ddb_idx = ddb_index[0];
  3369. ha->sec_ddb_idx = ddb_index[1];
  3370. return ret;
  3371. }
  3372. /**
  3373. * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
  3374. * @ha: pointer to adapter structure
  3375. * @username: CHAP username to be returned
  3376. * @password: CHAP password to be returned
  3377. *
  3378. * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
  3379. * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
  3380. * So from the CHAP cache find the first BIDI CHAP entry and set it
  3381. * to the boot record in sysfs.
  3382. **/
  3383. static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
  3384. char *password)
  3385. {
  3386. int i, ret = -EINVAL;
  3387. int max_chap_entries = 0;
  3388. struct ql4_chap_table *chap_table;
  3389. if (is_qla8022(ha))
  3390. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  3391. sizeof(struct ql4_chap_table);
  3392. else
  3393. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  3394. if (!ha->chap_list) {
  3395. ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
  3396. return ret;
  3397. }
  3398. mutex_lock(&ha->chap_sem);
  3399. for (i = 0; i < max_chap_entries; i++) {
  3400. chap_table = (struct ql4_chap_table *)ha->chap_list + i;
  3401. if (chap_table->cookie !=
  3402. __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
  3403. continue;
  3404. }
  3405. if (chap_table->flags & BIT_7) /* local */
  3406. continue;
  3407. if (!(chap_table->flags & BIT_6)) /* Not BIDI */
  3408. continue;
  3409. strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
  3410. strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
  3411. ret = 0;
  3412. break;
  3413. }
  3414. mutex_unlock(&ha->chap_sem);
  3415. return ret;
  3416. }
  3417. static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
  3418. struct ql4_boot_session_info *boot_sess,
  3419. uint16_t ddb_index)
  3420. {
  3421. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  3422. struct dev_db_entry *fw_ddb_entry;
  3423. dma_addr_t fw_ddb_entry_dma;
  3424. uint16_t idx;
  3425. uint16_t options;
  3426. int ret = QLA_SUCCESS;
  3427. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  3428. &fw_ddb_entry_dma, GFP_KERNEL);
  3429. if (!fw_ddb_entry) {
  3430. DEBUG2(ql4_printk(KERN_ERR, ha,
  3431. "%s: Unable to allocate dma buffer.\n",
  3432. __func__));
  3433. ret = QLA_ERROR;
  3434. return ret;
  3435. }
  3436. if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
  3437. fw_ddb_entry_dma, ddb_index)) {
  3438. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
  3439. "index [%d]\n", __func__, ddb_index));
  3440. ret = QLA_ERROR;
  3441. goto exit_boot_target;
  3442. }
  3443. /* Update target name and IP from DDB */
  3444. memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
  3445. min(sizeof(boot_sess->target_name),
  3446. sizeof(fw_ddb_entry->iscsi_name)));
  3447. options = le16_to_cpu(fw_ddb_entry->options);
  3448. if (options & DDB_OPT_IPV6_DEVICE) {
  3449. memcpy(&boot_conn->dest_ipaddr.ip_address,
  3450. &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
  3451. } else {
  3452. boot_conn->dest_ipaddr.ip_type = 0x1;
  3453. memcpy(&boot_conn->dest_ipaddr.ip_address,
  3454. &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
  3455. }
  3456. boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
  3457. /* update chap information */
  3458. idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  3459. if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  3460. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
  3461. ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
  3462. target_chap_name,
  3463. (char *)&boot_conn->chap.target_secret,
  3464. idx);
  3465. if (ret) {
  3466. ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
  3467. ret = QLA_ERROR;
  3468. goto exit_boot_target;
  3469. }
  3470. boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  3471. boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  3472. }
  3473. if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  3474. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
  3475. ret = qla4xxx_get_bidi_chap(ha,
  3476. (char *)&boot_conn->chap.intr_chap_name,
  3477. (char *)&boot_conn->chap.intr_secret);
  3478. if (ret) {
  3479. ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
  3480. ret = QLA_ERROR;
  3481. goto exit_boot_target;
  3482. }
  3483. boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  3484. boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  3485. }
  3486. exit_boot_target:
  3487. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  3488. fw_ddb_entry, fw_ddb_entry_dma);
  3489. return ret;
  3490. }
  3491. static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
  3492. {
  3493. uint16_t ddb_index[2];
  3494. int ret = QLA_ERROR;
  3495. int rval;
  3496. memset(ddb_index, 0, sizeof(ddb_index));
  3497. ddb_index[0] = 0xffff;
  3498. ddb_index[1] = 0xffff;
  3499. ret = get_fw_boot_info(ha, ddb_index);
  3500. if (ret != QLA_SUCCESS) {
  3501. DEBUG2(ql4_printk(KERN_INFO, ha,
  3502. "%s: No boot target configured.\n", __func__));
  3503. return ret;
  3504. }
  3505. if (ql4xdisablesysfsboot)
  3506. return QLA_SUCCESS;
  3507. if (ddb_index[0] == 0xffff)
  3508. goto sec_target;
  3509. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
  3510. ddb_index[0]);
  3511. if (rval != QLA_SUCCESS) {
  3512. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
  3513. "configured\n", __func__));
  3514. } else
  3515. ret = QLA_SUCCESS;
  3516. sec_target:
  3517. if (ddb_index[1] == 0xffff)
  3518. goto exit_get_boot_info;
  3519. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
  3520. ddb_index[1]);
  3521. if (rval != QLA_SUCCESS) {
  3522. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
  3523. " configured\n", __func__));
  3524. } else
  3525. ret = QLA_SUCCESS;
  3526. exit_get_boot_info:
  3527. return ret;
  3528. }
  3529. static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
  3530. {
  3531. struct iscsi_boot_kobj *boot_kobj;
  3532. if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
  3533. return QLA_ERROR;
  3534. if (ql4xdisablesysfsboot) {
  3535. ql4_printk(KERN_INFO, ha,
  3536. "%s: syfsboot disabled - driver will trigger login "
  3537. "and publish session for discovery .\n", __func__);
  3538. return QLA_SUCCESS;
  3539. }
  3540. ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
  3541. if (!ha->boot_kset)
  3542. goto kset_free;
  3543. if (!scsi_host_get(ha->host))
  3544. goto kset_free;
  3545. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
  3546. qla4xxx_show_boot_tgt_pri_info,
  3547. qla4xxx_tgt_get_attr_visibility,
  3548. qla4xxx_boot_release);
  3549. if (!boot_kobj)
  3550. goto put_host;
  3551. if (!scsi_host_get(ha->host))
  3552. goto kset_free;
  3553. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
  3554. qla4xxx_show_boot_tgt_sec_info,
  3555. qla4xxx_tgt_get_attr_visibility,
  3556. qla4xxx_boot_release);
  3557. if (!boot_kobj)
  3558. goto put_host;
  3559. if (!scsi_host_get(ha->host))
  3560. goto kset_free;
  3561. boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
  3562. qla4xxx_show_boot_ini_info,
  3563. qla4xxx_ini_get_attr_visibility,
  3564. qla4xxx_boot_release);
  3565. if (!boot_kobj)
  3566. goto put_host;
  3567. if (!scsi_host_get(ha->host))
  3568. goto kset_free;
  3569. boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
  3570. qla4xxx_show_boot_eth_info,
  3571. qla4xxx_eth_get_attr_visibility,
  3572. qla4xxx_boot_release);
  3573. if (!boot_kobj)
  3574. goto put_host;
  3575. return QLA_SUCCESS;
  3576. put_host:
  3577. scsi_host_put(ha->host);
  3578. kset_free:
  3579. iscsi_boot_destroy_kset(ha->boot_kset);
  3580. return -ENOMEM;
  3581. }
  3582. /**
  3583. * qla4xxx_create chap_list - Create CHAP list from FLASH
  3584. * @ha: pointer to adapter structure
  3585. *
  3586. * Read flash and make a list of CHAP entries, during login when a CHAP entry
  3587. * is received, it will be checked in this list. If entry exist then the CHAP
  3588. * entry index is set in the DDB. If CHAP entry does not exist in this list
  3589. * then a new entry is added in FLASH in CHAP table and the index obtained is
  3590. * used in the DDB.
  3591. **/
  3592. static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
  3593. {
  3594. int rval = 0;
  3595. uint8_t *chap_flash_data = NULL;
  3596. uint32_t offset;
  3597. dma_addr_t chap_dma;
  3598. uint32_t chap_size = 0;
  3599. if (is_qla40XX(ha))
  3600. chap_size = MAX_CHAP_ENTRIES_40XX *
  3601. sizeof(struct ql4_chap_table);
  3602. else /* Single region contains CHAP info for both
  3603. * ports which is divided into half for each port.
  3604. */
  3605. chap_size = ha->hw.flt_chap_size / 2;
  3606. chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
  3607. &chap_dma, GFP_KERNEL);
  3608. if (!chap_flash_data) {
  3609. ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
  3610. return;
  3611. }
  3612. if (is_qla40XX(ha))
  3613. offset = FLASH_CHAP_OFFSET;
  3614. else {
  3615. offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
  3616. if (ha->port_num == 1)
  3617. offset += chap_size;
  3618. }
  3619. rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
  3620. if (rval != QLA_SUCCESS)
  3621. goto exit_chap_list;
  3622. if (ha->chap_list == NULL)
  3623. ha->chap_list = vmalloc(chap_size);
  3624. if (ha->chap_list == NULL) {
  3625. ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
  3626. goto exit_chap_list;
  3627. }
  3628. memcpy(ha->chap_list, chap_flash_data, chap_size);
  3629. exit_chap_list:
  3630. dma_free_coherent(&ha->pdev->dev, chap_size,
  3631. chap_flash_data, chap_dma);
  3632. }
  3633. static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
  3634. struct ql4_tuple_ddb *tddb)
  3635. {
  3636. struct scsi_qla_host *ha;
  3637. struct iscsi_cls_session *cls_sess;
  3638. struct iscsi_cls_conn *cls_conn;
  3639. struct iscsi_session *sess;
  3640. struct iscsi_conn *conn;
  3641. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  3642. ha = ddb_entry->ha;
  3643. cls_sess = ddb_entry->sess;
  3644. sess = cls_sess->dd_data;
  3645. cls_conn = ddb_entry->conn;
  3646. conn = cls_conn->dd_data;
  3647. tddb->tpgt = sess->tpgt;
  3648. tddb->port = conn->persistent_port;
  3649. strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
  3650. strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
  3651. }
  3652. static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
  3653. struct ql4_tuple_ddb *tddb)
  3654. {
  3655. uint16_t options = 0;
  3656. tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  3657. memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
  3658. min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
  3659. options = le16_to_cpu(fw_ddb_entry->options);
  3660. if (options & DDB_OPT_IPV6_DEVICE)
  3661. sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  3662. else
  3663. sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  3664. tddb->port = le16_to_cpu(fw_ddb_entry->port);
  3665. memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0], sizeof(tddb->isid));
  3666. }
  3667. static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
  3668. struct ql4_tuple_ddb *old_tddb,
  3669. struct ql4_tuple_ddb *new_tddb,
  3670. uint8_t is_isid_compare)
  3671. {
  3672. if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
  3673. return QLA_ERROR;
  3674. if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
  3675. return QLA_ERROR;
  3676. if (old_tddb->port != new_tddb->port)
  3677. return QLA_ERROR;
  3678. /* For multi sessions, driver generates the ISID, so do not compare
  3679. * ISID in reset path since it would be a comparision between the
  3680. * driver generated ISID and firmware generated ISID. This could
  3681. * lead to adding duplicated DDBs in the list as driver generated
  3682. * ISID would not match firmware generated ISID.
  3683. */
  3684. if (is_isid_compare) {
  3685. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: old ISID [%02x%02x%02x"
  3686. "%02x%02x%02x] New ISID [%02x%02x%02x%02x%02x%02x]\n",
  3687. __func__, old_tddb->isid[5], old_tddb->isid[4],
  3688. old_tddb->isid[3], old_tddb->isid[2], old_tddb->isid[1],
  3689. old_tddb->isid[0], new_tddb->isid[5], new_tddb->isid[4],
  3690. new_tddb->isid[3], new_tddb->isid[2], new_tddb->isid[1],
  3691. new_tddb->isid[0]));
  3692. if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
  3693. sizeof(old_tddb->isid)))
  3694. return QLA_ERROR;
  3695. }
  3696. DEBUG2(ql4_printk(KERN_INFO, ha,
  3697. "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
  3698. old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
  3699. old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
  3700. new_tddb->ip_addr, new_tddb->iscsi_name));
  3701. return QLA_SUCCESS;
  3702. }
  3703. static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
  3704. struct dev_db_entry *fw_ddb_entry)
  3705. {
  3706. struct ddb_entry *ddb_entry;
  3707. struct ql4_tuple_ddb *fw_tddb = NULL;
  3708. struct ql4_tuple_ddb *tmp_tddb = NULL;
  3709. int idx;
  3710. int ret = QLA_ERROR;
  3711. fw_tddb = vzalloc(sizeof(*fw_tddb));
  3712. if (!fw_tddb) {
  3713. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3714. "Memory Allocation failed.\n"));
  3715. ret = QLA_SUCCESS;
  3716. goto exit_check;
  3717. }
  3718. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  3719. if (!tmp_tddb) {
  3720. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3721. "Memory Allocation failed.\n"));
  3722. ret = QLA_SUCCESS;
  3723. goto exit_check;
  3724. }
  3725. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
  3726. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  3727. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  3728. if (ddb_entry == NULL)
  3729. continue;
  3730. qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
  3731. if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
  3732. ret = QLA_SUCCESS; /* found */
  3733. goto exit_check;
  3734. }
  3735. }
  3736. exit_check:
  3737. if (fw_tddb)
  3738. vfree(fw_tddb);
  3739. if (tmp_tddb)
  3740. vfree(tmp_tddb);
  3741. return ret;
  3742. }
  3743. static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
  3744. struct list_head *list_nt,
  3745. struct dev_db_entry *fw_ddb_entry)
  3746. {
  3747. struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
  3748. struct ql4_tuple_ddb *fw_tddb = NULL;
  3749. struct ql4_tuple_ddb *tmp_tddb = NULL;
  3750. int ret = QLA_ERROR;
  3751. fw_tddb = vzalloc(sizeof(*fw_tddb));
  3752. if (!fw_tddb) {
  3753. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3754. "Memory Allocation failed.\n"));
  3755. ret = QLA_SUCCESS;
  3756. goto exit_check;
  3757. }
  3758. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  3759. if (!tmp_tddb) {
  3760. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3761. "Memory Allocation failed.\n"));
  3762. ret = QLA_SUCCESS;
  3763. goto exit_check;
  3764. }
  3765. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
  3766. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  3767. qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
  3768. if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true)) {
  3769. ret = QLA_SUCCESS; /* found */
  3770. goto exit_check;
  3771. }
  3772. }
  3773. exit_check:
  3774. if (fw_tddb)
  3775. vfree(fw_tddb);
  3776. if (tmp_tddb)
  3777. vfree(tmp_tddb);
  3778. return ret;
  3779. }
  3780. static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
  3781. {
  3782. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  3783. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
  3784. list_del_init(&ddb_idx->list);
  3785. vfree(ddb_idx);
  3786. }
  3787. }
  3788. static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
  3789. struct dev_db_entry *fw_ddb_entry)
  3790. {
  3791. struct iscsi_endpoint *ep;
  3792. struct sockaddr_in *addr;
  3793. struct sockaddr_in6 *addr6;
  3794. struct sockaddr *dst_addr;
  3795. char *ip;
  3796. /* TODO: need to destroy on unload iscsi_endpoint*/
  3797. dst_addr = vmalloc(sizeof(*dst_addr));
  3798. if (!dst_addr)
  3799. return NULL;
  3800. if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
  3801. dst_addr->sa_family = AF_INET6;
  3802. addr6 = (struct sockaddr_in6 *)dst_addr;
  3803. ip = (char *)&addr6->sin6_addr;
  3804. memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
  3805. addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
  3806. } else {
  3807. dst_addr->sa_family = AF_INET;
  3808. addr = (struct sockaddr_in *)dst_addr;
  3809. ip = (char *)&addr->sin_addr;
  3810. memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
  3811. addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
  3812. }
  3813. ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
  3814. vfree(dst_addr);
  3815. return ep;
  3816. }
  3817. static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
  3818. {
  3819. if (ql4xdisablesysfsboot)
  3820. return QLA_SUCCESS;
  3821. if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
  3822. return QLA_ERROR;
  3823. return QLA_SUCCESS;
  3824. }
  3825. static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
  3826. struct ddb_entry *ddb_entry)
  3827. {
  3828. uint16_t def_timeout;
  3829. ddb_entry->ddb_type = FLASH_DDB;
  3830. ddb_entry->fw_ddb_index = INVALID_ENTRY;
  3831. ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  3832. ddb_entry->ha = ha;
  3833. ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
  3834. ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
  3835. atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  3836. atomic_set(&ddb_entry->relogin_timer, 0);
  3837. atomic_set(&ddb_entry->relogin_retry_count, 0);
  3838. def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
  3839. ddb_entry->default_relogin_timeout =
  3840. (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
  3841. def_timeout : LOGIN_TOV;
  3842. ddb_entry->default_time2wait =
  3843. le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
  3844. }
  3845. static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
  3846. {
  3847. uint32_t idx = 0;
  3848. uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
  3849. uint32_t sts[MBOX_REG_COUNT];
  3850. uint32_t ip_state;
  3851. unsigned long wtime;
  3852. int ret;
  3853. wtime = jiffies + (HZ * IP_CONFIG_TOV);
  3854. do {
  3855. for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
  3856. if (ip_idx[idx] == -1)
  3857. continue;
  3858. ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
  3859. if (ret == QLA_ERROR) {
  3860. ip_idx[idx] = -1;
  3861. continue;
  3862. }
  3863. ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
  3864. DEBUG2(ql4_printk(KERN_INFO, ha,
  3865. "Waiting for IP state for idx = %d, state = 0x%x\n",
  3866. ip_idx[idx], ip_state));
  3867. if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
  3868. ip_state == IP_ADDRSTATE_INVALID ||
  3869. ip_state == IP_ADDRSTATE_PREFERRED ||
  3870. ip_state == IP_ADDRSTATE_DEPRICATED ||
  3871. ip_state == IP_ADDRSTATE_DISABLING)
  3872. ip_idx[idx] = -1;
  3873. }
  3874. /* Break if all IP states checked */
  3875. if ((ip_idx[0] == -1) &&
  3876. (ip_idx[1] == -1) &&
  3877. (ip_idx[2] == -1) &&
  3878. (ip_idx[3] == -1))
  3879. break;
  3880. schedule_timeout_uninterruptible(HZ);
  3881. } while (time_after(wtime, jiffies));
  3882. }
  3883. static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
  3884. struct list_head *list_st)
  3885. {
  3886. struct qla_ddb_index *st_ddb_idx;
  3887. int max_ddbs;
  3888. int fw_idx_size;
  3889. struct dev_db_entry *fw_ddb_entry;
  3890. dma_addr_t fw_ddb_dma;
  3891. int ret;
  3892. uint32_t idx = 0, next_idx = 0;
  3893. uint32_t state = 0, conn_err = 0;
  3894. uint16_t conn_id = 0;
  3895. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  3896. &fw_ddb_dma);
  3897. if (fw_ddb_entry == NULL) {
  3898. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  3899. goto exit_st_list;
  3900. }
  3901. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  3902. MAX_DEV_DB_ENTRIES;
  3903. fw_idx_size = sizeof(struct qla_ddb_index);
  3904. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  3905. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
  3906. NULL, &next_idx, &state,
  3907. &conn_err, NULL, &conn_id);
  3908. if (ret == QLA_ERROR)
  3909. break;
  3910. /* Ignore DDB if invalid state (unassigned) */
  3911. if (state == DDB_DS_UNASSIGNED)
  3912. goto continue_next_st;
  3913. /* Check if ST, add to the list_st */
  3914. if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
  3915. goto continue_next_st;
  3916. st_ddb_idx = vzalloc(fw_idx_size);
  3917. if (!st_ddb_idx)
  3918. break;
  3919. st_ddb_idx->fw_ddb_idx = idx;
  3920. list_add_tail(&st_ddb_idx->list, list_st);
  3921. continue_next_st:
  3922. if (next_idx == 0)
  3923. break;
  3924. }
  3925. exit_st_list:
  3926. if (fw_ddb_entry)
  3927. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  3928. }
  3929. /**
  3930. * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
  3931. * @ha: pointer to adapter structure
  3932. * @list_ddb: List from which failed ddb to be removed
  3933. *
  3934. * Iterate over the list of DDBs and find and remove DDBs that are either in
  3935. * no connection active state or failed state
  3936. **/
  3937. static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
  3938. struct list_head *list_ddb)
  3939. {
  3940. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  3941. uint32_t next_idx = 0;
  3942. uint32_t state = 0, conn_err = 0;
  3943. int ret;
  3944. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
  3945. ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
  3946. NULL, 0, NULL, &next_idx, &state,
  3947. &conn_err, NULL, NULL);
  3948. if (ret == QLA_ERROR)
  3949. continue;
  3950. if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  3951. state == DDB_DS_SESSION_FAILED) {
  3952. list_del_init(&ddb_idx->list);
  3953. vfree(ddb_idx);
  3954. }
  3955. }
  3956. }
  3957. static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
  3958. struct dev_db_entry *fw_ddb_entry,
  3959. int is_reset)
  3960. {
  3961. struct iscsi_cls_session *cls_sess;
  3962. struct iscsi_session *sess;
  3963. struct iscsi_cls_conn *cls_conn;
  3964. struct iscsi_endpoint *ep;
  3965. uint16_t cmds_max = 32;
  3966. uint16_t conn_id = 0;
  3967. uint32_t initial_cmdsn = 0;
  3968. int ret = QLA_SUCCESS;
  3969. struct ddb_entry *ddb_entry = NULL;
  3970. /* Create session object, with INVALID_ENTRY,
  3971. * the targer_id would get set when we issue the login
  3972. */
  3973. cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
  3974. cmds_max, sizeof(struct ddb_entry),
  3975. sizeof(struct ql4_task_data),
  3976. initial_cmdsn, INVALID_ENTRY);
  3977. if (!cls_sess) {
  3978. ret = QLA_ERROR;
  3979. goto exit_setup;
  3980. }
  3981. /*
  3982. * so calling module_put function to decrement the
  3983. * reference count.
  3984. **/
  3985. module_put(qla4xxx_iscsi_transport.owner);
  3986. sess = cls_sess->dd_data;
  3987. ddb_entry = sess->dd_data;
  3988. ddb_entry->sess = cls_sess;
  3989. cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  3990. memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
  3991. sizeof(struct dev_db_entry));
  3992. qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
  3993. cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
  3994. if (!cls_conn) {
  3995. ret = QLA_ERROR;
  3996. goto exit_setup;
  3997. }
  3998. ddb_entry->conn = cls_conn;
  3999. /* Setup ep, for displaying attributes in sysfs */
  4000. ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
  4001. if (ep) {
  4002. ep->conn = cls_conn;
  4003. cls_conn->ep = ep;
  4004. } else {
  4005. DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
  4006. ret = QLA_ERROR;
  4007. goto exit_setup;
  4008. }
  4009. /* Update sess/conn params */
  4010. qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
  4011. if (is_reset == RESET_ADAPTER) {
  4012. iscsi_block_session(cls_sess);
  4013. /* Use the relogin path to discover new devices
  4014. * by short-circuting the logic of setting
  4015. * timer to relogin - instead set the flags
  4016. * to initiate login right away.
  4017. */
  4018. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  4019. set_bit(DF_RELOGIN, &ddb_entry->flags);
  4020. }
  4021. exit_setup:
  4022. return ret;
  4023. }
  4024. static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
  4025. struct list_head *list_nt, int is_reset)
  4026. {
  4027. struct dev_db_entry *fw_ddb_entry;
  4028. dma_addr_t fw_ddb_dma;
  4029. int max_ddbs;
  4030. int fw_idx_size;
  4031. int ret;
  4032. uint32_t idx = 0, next_idx = 0;
  4033. uint32_t state = 0, conn_err = 0;
  4034. uint16_t conn_id = 0;
  4035. struct qla_ddb_index *nt_ddb_idx;
  4036. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  4037. &fw_ddb_dma);
  4038. if (fw_ddb_entry == NULL) {
  4039. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  4040. goto exit_nt_list;
  4041. }
  4042. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  4043. MAX_DEV_DB_ENTRIES;
  4044. fw_idx_size = sizeof(struct qla_ddb_index);
  4045. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  4046. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
  4047. NULL, &next_idx, &state,
  4048. &conn_err, NULL, &conn_id);
  4049. if (ret == QLA_ERROR)
  4050. break;
  4051. if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
  4052. goto continue_next_nt;
  4053. /* Check if NT, then add to list it */
  4054. if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
  4055. goto continue_next_nt;
  4056. if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
  4057. state == DDB_DS_SESSION_FAILED))
  4058. goto continue_next_nt;
  4059. DEBUG2(ql4_printk(KERN_INFO, ha,
  4060. "Adding DDB to session = 0x%x\n", idx));
  4061. if (is_reset == INIT_ADAPTER) {
  4062. nt_ddb_idx = vmalloc(fw_idx_size);
  4063. if (!nt_ddb_idx)
  4064. break;
  4065. nt_ddb_idx->fw_ddb_idx = idx;
  4066. memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
  4067. sizeof(struct dev_db_entry));
  4068. if (qla4xxx_is_flash_ddb_exists(ha, list_nt,
  4069. fw_ddb_entry) == QLA_SUCCESS) {
  4070. vfree(nt_ddb_idx);
  4071. goto continue_next_nt;
  4072. }
  4073. list_add_tail(&nt_ddb_idx->list, list_nt);
  4074. } else if (is_reset == RESET_ADAPTER) {
  4075. if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
  4076. QLA_SUCCESS)
  4077. goto continue_next_nt;
  4078. }
  4079. ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
  4080. if (ret == QLA_ERROR)
  4081. goto exit_nt_list;
  4082. continue_next_nt:
  4083. if (next_idx == 0)
  4084. break;
  4085. }
  4086. exit_nt_list:
  4087. if (fw_ddb_entry)
  4088. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  4089. }
  4090. /**
  4091. * qla4xxx_build_ddb_list - Build ddb list and setup sessions
  4092. * @ha: pointer to adapter structure
  4093. * @is_reset: Is this init path or reset path
  4094. *
  4095. * Create a list of sendtargets (st) from firmware DDBs, issue send targets
  4096. * using connection open, then create the list of normal targets (nt)
  4097. * from firmware DDBs. Based on the list of nt setup session and connection
  4098. * objects.
  4099. **/
  4100. void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
  4101. {
  4102. uint16_t tmo = 0;
  4103. struct list_head list_st, list_nt;
  4104. struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
  4105. unsigned long wtime;
  4106. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  4107. set_bit(AF_BUILD_DDB_LIST, &ha->flags);
  4108. ha->is_reset = is_reset;
  4109. return;
  4110. }
  4111. INIT_LIST_HEAD(&list_st);
  4112. INIT_LIST_HEAD(&list_nt);
  4113. qla4xxx_build_st_list(ha, &list_st);
  4114. /* Before issuing conn open mbox, ensure all IPs states are configured
  4115. * Note, conn open fails if IPs are not configured
  4116. */
  4117. qla4xxx_wait_for_ip_configuration(ha);
  4118. /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
  4119. list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
  4120. qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
  4121. }
  4122. /* Wait to ensure all sendtargets are done for min 12 sec wait */
  4123. tmo = ((ha->def_timeout > LOGIN_TOV) &&
  4124. (ha->def_timeout < LOGIN_TOV * 10) ?
  4125. ha->def_timeout : LOGIN_TOV);
  4126. DEBUG2(ql4_printk(KERN_INFO, ha,
  4127. "Default time to wait for build ddb %d\n", tmo));
  4128. wtime = jiffies + (HZ * tmo);
  4129. do {
  4130. if (list_empty(&list_st))
  4131. break;
  4132. qla4xxx_remove_failed_ddb(ha, &list_st);
  4133. schedule_timeout_uninterruptible(HZ / 10);
  4134. } while (time_after(wtime, jiffies));
  4135. /* Free up the sendtargets list */
  4136. qla4xxx_free_ddb_list(&list_st);
  4137. qla4xxx_build_nt_list(ha, &list_nt, is_reset);
  4138. qla4xxx_free_ddb_list(&list_nt);
  4139. qla4xxx_free_ddb_index(ha);
  4140. }
  4141. /**
  4142. * qla4xxx_probe_adapter - callback function to probe HBA
  4143. * @pdev: pointer to pci_dev structure
  4144. * @pci_device_id: pointer to pci_device entry
  4145. *
  4146. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  4147. * It returns zero if successful. It also initializes all data necessary for
  4148. * the driver.
  4149. **/
  4150. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  4151. const struct pci_device_id *ent)
  4152. {
  4153. int ret = -ENODEV, status;
  4154. struct Scsi_Host *host;
  4155. struct scsi_qla_host *ha;
  4156. uint8_t init_retry_count = 0;
  4157. char buf[34];
  4158. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  4159. uint32_t dev_state;
  4160. if (pci_enable_device(pdev))
  4161. return -1;
  4162. host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
  4163. if (host == NULL) {
  4164. printk(KERN_WARNING
  4165. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  4166. goto probe_disable_device;
  4167. }
  4168. /* Clear our data area */
  4169. ha = to_qla_host(host);
  4170. memset(ha, 0, sizeof(*ha));
  4171. /* Save the information from PCI BIOS. */
  4172. ha->pdev = pdev;
  4173. ha->host = host;
  4174. ha->host_no = host->host_no;
  4175. pci_enable_pcie_error_reporting(pdev);
  4176. /* Setup Runtime configurable options */
  4177. if (is_qla8022(ha)) {
  4178. ha->isp_ops = &qla4_8xxx_isp_ops;
  4179. rwlock_init(&ha->hw_lock);
  4180. ha->qdr_sn_window = -1;
  4181. ha->ddr_mn_window = -1;
  4182. ha->curr_window = 255;
  4183. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  4184. nx_legacy_intr = &legacy_intr[ha->func_num];
  4185. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  4186. ha->nx_legacy_intr.tgt_status_reg =
  4187. nx_legacy_intr->tgt_status_reg;
  4188. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  4189. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  4190. } else {
  4191. ha->isp_ops = &qla4xxx_isp_ops;
  4192. }
  4193. /* Set EEH reset type to fundamental if required by hba */
  4194. if (is_qla8022(ha))
  4195. pdev->needs_freset = 1;
  4196. /* Configure PCI I/O space. */
  4197. ret = ha->isp_ops->iospace_config(ha);
  4198. if (ret)
  4199. goto probe_failed_ioconfig;
  4200. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  4201. pdev->device, pdev->irq, ha->reg);
  4202. qla4xxx_config_dma_addressing(ha);
  4203. /* Initialize lists and spinlocks. */
  4204. INIT_LIST_HEAD(&ha->free_srb_q);
  4205. mutex_init(&ha->mbox_sem);
  4206. mutex_init(&ha->chap_sem);
  4207. init_completion(&ha->mbx_intr_comp);
  4208. init_completion(&ha->disable_acb_comp);
  4209. spin_lock_init(&ha->hardware_lock);
  4210. /* Initialize work list */
  4211. INIT_LIST_HEAD(&ha->work_list);
  4212. /* Allocate dma buffers */
  4213. if (qla4xxx_mem_alloc(ha)) {
  4214. ql4_printk(KERN_WARNING, ha,
  4215. "[ERROR] Failed to allocate memory for adapter\n");
  4216. ret = -ENOMEM;
  4217. goto probe_failed;
  4218. }
  4219. host->cmd_per_lun = 3;
  4220. host->max_channel = 0;
  4221. host->max_lun = MAX_LUNS - 1;
  4222. host->max_id = MAX_TARGETS;
  4223. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  4224. host->can_queue = MAX_SRBS ;
  4225. host->transportt = qla4xxx_scsi_transport;
  4226. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  4227. if (ret) {
  4228. ql4_printk(KERN_WARNING, ha,
  4229. "%s: scsi_init_shared_tag_map failed\n", __func__);
  4230. goto probe_failed;
  4231. }
  4232. pci_set_drvdata(pdev, ha);
  4233. ret = scsi_add_host(host, &pdev->dev);
  4234. if (ret)
  4235. goto probe_failed;
  4236. if (is_qla8022(ha))
  4237. (void) qla4_8xxx_get_flash_info(ha);
  4238. /*
  4239. * Initialize the Host adapter request/response queues and
  4240. * firmware
  4241. * NOTE: interrupts enabled upon successful completion
  4242. */
  4243. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  4244. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  4245. init_retry_count++ < MAX_INIT_RETRIES) {
  4246. if (is_qla8022(ha)) {
  4247. qla4_8xxx_idc_lock(ha);
  4248. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  4249. qla4_8xxx_idc_unlock(ha);
  4250. if (dev_state == QLA82XX_DEV_FAILED) {
  4251. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  4252. "initialize adapter. H/W is in failed state\n",
  4253. __func__);
  4254. break;
  4255. }
  4256. }
  4257. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  4258. "(%d)\n", __func__, init_retry_count));
  4259. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  4260. continue;
  4261. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  4262. }
  4263. if (!test_bit(AF_ONLINE, &ha->flags)) {
  4264. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  4265. if (is_qla8022(ha) && ql4xdontresethba) {
  4266. /* Put the device in failed state. */
  4267. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  4268. qla4_8xxx_idc_lock(ha);
  4269. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  4270. QLA82XX_DEV_FAILED);
  4271. qla4_8xxx_idc_unlock(ha);
  4272. }
  4273. ret = -ENODEV;
  4274. goto remove_host;
  4275. }
  4276. /* Startup the kernel thread for this host adapter. */
  4277. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  4278. "qla4xxx_dpc\n", __func__));
  4279. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  4280. ha->dpc_thread = create_singlethread_workqueue(buf);
  4281. if (!ha->dpc_thread) {
  4282. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  4283. ret = -ENODEV;
  4284. goto remove_host;
  4285. }
  4286. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  4287. sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
  4288. ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
  4289. if (!ha->task_wq) {
  4290. ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
  4291. ret = -ENODEV;
  4292. goto remove_host;
  4293. }
  4294. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  4295. * (which is called indirectly by qla4xxx_initialize_adapter),
  4296. * so that irqs will be registered after crbinit but before
  4297. * mbx_intr_enable.
  4298. */
  4299. if (!is_qla8022(ha)) {
  4300. ret = qla4xxx_request_irqs(ha);
  4301. if (ret) {
  4302. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  4303. "interrupt %d already in use.\n", pdev->irq);
  4304. goto remove_host;
  4305. }
  4306. }
  4307. pci_save_state(ha->pdev);
  4308. ha->isp_ops->enable_intrs(ha);
  4309. /* Start timer thread. */
  4310. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  4311. set_bit(AF_INIT_DONE, &ha->flags);
  4312. printk(KERN_INFO
  4313. " QLogic iSCSI HBA Driver version: %s\n"
  4314. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  4315. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  4316. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  4317. ha->patch_number, ha->build_number);
  4318. if (qla4xxx_setup_boot_info(ha))
  4319. ql4_printk(KERN_ERR, ha,
  4320. "%s: No iSCSI boot target configured\n", __func__);
  4321. /* Perform the build ddb list and login to each */
  4322. qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
  4323. iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
  4324. qla4xxx_create_chap_list(ha);
  4325. qla4xxx_create_ifaces(ha);
  4326. return 0;
  4327. remove_host:
  4328. scsi_remove_host(ha->host);
  4329. probe_failed:
  4330. qla4xxx_free_adapter(ha);
  4331. probe_failed_ioconfig:
  4332. pci_disable_pcie_error_reporting(pdev);
  4333. scsi_host_put(ha->host);
  4334. probe_disable_device:
  4335. pci_disable_device(pdev);
  4336. return ret;
  4337. }
  4338. /**
  4339. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  4340. * @ha: pointer to adapter structure
  4341. *
  4342. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  4343. * so that the other port will not re-initialize while in the process of
  4344. * removing the ha due to driver unload or hba hotplug.
  4345. **/
  4346. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  4347. {
  4348. struct scsi_qla_host *other_ha = NULL;
  4349. struct pci_dev *other_pdev = NULL;
  4350. int fn = ISP4XXX_PCI_FN_2;
  4351. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  4352. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  4353. fn = ISP4XXX_PCI_FN_1;
  4354. other_pdev =
  4355. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  4356. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  4357. fn));
  4358. /* Get other_ha if other_pdev is valid and state is enable*/
  4359. if (other_pdev) {
  4360. if (atomic_read(&other_pdev->enable_cnt)) {
  4361. other_ha = pci_get_drvdata(other_pdev);
  4362. if (other_ha) {
  4363. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  4364. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  4365. "Prevent %s reinit\n", __func__,
  4366. dev_name(&other_ha->pdev->dev)));
  4367. }
  4368. }
  4369. pci_dev_put(other_pdev);
  4370. }
  4371. }
  4372. static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
  4373. {
  4374. struct ddb_entry *ddb_entry;
  4375. int options;
  4376. int idx;
  4377. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  4378. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  4379. if ((ddb_entry != NULL) &&
  4380. (ddb_entry->ddb_type == FLASH_DDB)) {
  4381. options = LOGOUT_OPTION_CLOSE_SESSION;
  4382. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
  4383. == QLA_ERROR)
  4384. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
  4385. __func__);
  4386. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  4387. /*
  4388. * we have decremented the reference count of the driver
  4389. * when we setup the session to have the driver unload
  4390. * to be seamless without actually destroying the
  4391. * session
  4392. **/
  4393. try_module_get(qla4xxx_iscsi_transport.owner);
  4394. iscsi_destroy_endpoint(ddb_entry->conn->ep);
  4395. qla4xxx_free_ddb(ha, ddb_entry);
  4396. iscsi_session_teardown(ddb_entry->sess);
  4397. }
  4398. }
  4399. }
  4400. /**
  4401. * qla4xxx_remove_adapter - calback function to remove adapter.
  4402. * @pci_dev: PCI device pointer
  4403. **/
  4404. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  4405. {
  4406. struct scsi_qla_host *ha;
  4407. ha = pci_get_drvdata(pdev);
  4408. if (!is_qla8022(ha))
  4409. qla4xxx_prevent_other_port_reinit(ha);
  4410. /* destroy iface from sysfs */
  4411. qla4xxx_destroy_ifaces(ha);
  4412. if ((!ql4xdisablesysfsboot) && ha->boot_kset)
  4413. iscsi_boot_destroy_kset(ha->boot_kset);
  4414. qla4xxx_destroy_fw_ddb_session(ha);
  4415. scsi_remove_host(ha->host);
  4416. qla4xxx_free_adapter(ha);
  4417. scsi_host_put(ha->host);
  4418. pci_disable_pcie_error_reporting(pdev);
  4419. pci_disable_device(pdev);
  4420. pci_set_drvdata(pdev, NULL);
  4421. }
  4422. /**
  4423. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  4424. * @ha: HA context
  4425. *
  4426. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  4427. * supported addressing method.
  4428. */
  4429. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  4430. {
  4431. int retval;
  4432. /* Update our PCI device dma_mask for full 64 bit mask */
  4433. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  4434. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  4435. dev_dbg(&ha->pdev->dev,
  4436. "Failed to set 64 bit PCI consistent mask; "
  4437. "using 32 bit.\n");
  4438. retval = pci_set_consistent_dma_mask(ha->pdev,
  4439. DMA_BIT_MASK(32));
  4440. }
  4441. } else
  4442. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  4443. }
  4444. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  4445. {
  4446. struct iscsi_cls_session *cls_sess;
  4447. struct iscsi_session *sess;
  4448. struct ddb_entry *ddb;
  4449. int queue_depth = QL4_DEF_QDEPTH;
  4450. cls_sess = starget_to_session(sdev->sdev_target);
  4451. sess = cls_sess->dd_data;
  4452. ddb = sess->dd_data;
  4453. sdev->hostdata = ddb;
  4454. sdev->tagged_supported = 1;
  4455. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  4456. queue_depth = ql4xmaxqdepth;
  4457. scsi_activate_tcq(sdev, queue_depth);
  4458. return 0;
  4459. }
  4460. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  4461. {
  4462. sdev->tagged_supported = 1;
  4463. return 0;
  4464. }
  4465. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  4466. {
  4467. scsi_deactivate_tcq(sdev, 1);
  4468. }
  4469. /**
  4470. * qla4xxx_del_from_active_array - returns an active srb
  4471. * @ha: Pointer to host adapter structure.
  4472. * @index: index into the active_array
  4473. *
  4474. * This routine removes and returns the srb at the specified index
  4475. **/
  4476. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  4477. uint32_t index)
  4478. {
  4479. struct srb *srb = NULL;
  4480. struct scsi_cmnd *cmd = NULL;
  4481. cmd = scsi_host_find_tag(ha->host, index);
  4482. if (!cmd)
  4483. return srb;
  4484. srb = (struct srb *)CMD_SP(cmd);
  4485. if (!srb)
  4486. return srb;
  4487. /* update counters */
  4488. if (srb->flags & SRB_DMA_VALID) {
  4489. ha->req_q_count += srb->iocb_cnt;
  4490. ha->iocb_cnt -= srb->iocb_cnt;
  4491. if (srb->cmd)
  4492. srb->cmd->host_scribble =
  4493. (unsigned char *)(unsigned long) MAX_SRBS;
  4494. }
  4495. return srb;
  4496. }
  4497. /**
  4498. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  4499. * @ha: Pointer to host adapter structure.
  4500. * @cmd: Scsi Command to wait on.
  4501. *
  4502. * This routine waits for the command to be returned by the Firmware
  4503. * for some max time.
  4504. **/
  4505. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  4506. struct scsi_cmnd *cmd)
  4507. {
  4508. int done = 0;
  4509. struct srb *rp;
  4510. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  4511. int ret = SUCCESS;
  4512. /* Dont wait on command if PCI error is being handled
  4513. * by PCI AER driver
  4514. */
  4515. if (unlikely(pci_channel_offline(ha->pdev)) ||
  4516. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  4517. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  4518. ha->host_no, __func__);
  4519. return ret;
  4520. }
  4521. do {
  4522. /* Checking to see if its returned to OS */
  4523. rp = (struct srb *) CMD_SP(cmd);
  4524. if (rp == NULL) {
  4525. done++;
  4526. break;
  4527. }
  4528. msleep(2000);
  4529. } while (max_wait_time--);
  4530. return done;
  4531. }
  4532. /**
  4533. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  4534. * @ha: Pointer to host adapter structure
  4535. **/
  4536. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  4537. {
  4538. unsigned long wait_online;
  4539. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  4540. while (time_before(jiffies, wait_online)) {
  4541. if (adapter_up(ha))
  4542. return QLA_SUCCESS;
  4543. msleep(2000);
  4544. }
  4545. return QLA_ERROR;
  4546. }
  4547. /**
  4548. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  4549. * @ha: pointer to HBA
  4550. * @t: target id
  4551. * @l: lun id
  4552. *
  4553. * This function waits for all outstanding commands to a lun to complete. It
  4554. * returns 0 if all pending commands are returned and 1 otherwise.
  4555. **/
  4556. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  4557. struct scsi_target *stgt,
  4558. struct scsi_device *sdev)
  4559. {
  4560. int cnt;
  4561. int status = 0;
  4562. struct scsi_cmnd *cmd;
  4563. /*
  4564. * Waiting for all commands for the designated target or dev
  4565. * in the active array
  4566. */
  4567. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  4568. cmd = scsi_host_find_tag(ha->host, cnt);
  4569. if (cmd && stgt == scsi_target(cmd->device) &&
  4570. (!sdev || sdev == cmd->device)) {
  4571. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  4572. status++;
  4573. break;
  4574. }
  4575. }
  4576. }
  4577. return status;
  4578. }
  4579. /**
  4580. * qla4xxx_eh_abort - callback for abort task.
  4581. * @cmd: Pointer to Linux's SCSI command structure
  4582. *
  4583. * This routine is called by the Linux OS to abort the specified
  4584. * command.
  4585. **/
  4586. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  4587. {
  4588. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4589. unsigned int id = cmd->device->id;
  4590. unsigned int lun = cmd->device->lun;
  4591. unsigned long flags;
  4592. struct srb *srb = NULL;
  4593. int ret = SUCCESS;
  4594. int wait = 0;
  4595. ql4_printk(KERN_INFO, ha,
  4596. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  4597. ha->host_no, id, lun, cmd);
  4598. spin_lock_irqsave(&ha->hardware_lock, flags);
  4599. srb = (struct srb *) CMD_SP(cmd);
  4600. if (!srb) {
  4601. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4602. return SUCCESS;
  4603. }
  4604. kref_get(&srb->srb_ref);
  4605. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4606. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  4607. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  4608. ha->host_no, id, lun));
  4609. ret = FAILED;
  4610. } else {
  4611. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  4612. ha->host_no, id, lun));
  4613. wait = 1;
  4614. }
  4615. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  4616. /* Wait for command to complete */
  4617. if (wait) {
  4618. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  4619. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  4620. ha->host_no, id, lun));
  4621. ret = FAILED;
  4622. }
  4623. }
  4624. ql4_printk(KERN_INFO, ha,
  4625. "scsi%ld:%d:%d: Abort command - %s\n",
  4626. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  4627. return ret;
  4628. }
  4629. /**
  4630. * qla4xxx_eh_device_reset - callback for target reset.
  4631. * @cmd: Pointer to Linux's SCSI command structure
  4632. *
  4633. * This routine is called by the Linux OS to reset all luns on the
  4634. * specified target.
  4635. **/
  4636. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  4637. {
  4638. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4639. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  4640. int ret = FAILED, stat;
  4641. if (!ddb_entry)
  4642. return ret;
  4643. ret = iscsi_block_scsi_eh(cmd);
  4644. if (ret)
  4645. return ret;
  4646. ret = FAILED;
  4647. ql4_printk(KERN_INFO, ha,
  4648. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  4649. cmd->device->channel, cmd->device->id, cmd->device->lun);
  4650. DEBUG2(printk(KERN_INFO
  4651. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  4652. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  4653. cmd, jiffies, cmd->request->timeout / HZ,
  4654. ha->dpc_flags, cmd->result, cmd->allowed));
  4655. /* FIXME: wait for hba to go online */
  4656. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  4657. if (stat != QLA_SUCCESS) {
  4658. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  4659. goto eh_dev_reset_done;
  4660. }
  4661. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  4662. cmd->device)) {
  4663. ql4_printk(KERN_INFO, ha,
  4664. "DEVICE RESET FAILED - waiting for "
  4665. "commands.\n");
  4666. goto eh_dev_reset_done;
  4667. }
  4668. /* Send marker. */
  4669. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  4670. MM_LUN_RESET) != QLA_SUCCESS)
  4671. goto eh_dev_reset_done;
  4672. ql4_printk(KERN_INFO, ha,
  4673. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  4674. ha->host_no, cmd->device->channel, cmd->device->id,
  4675. cmd->device->lun);
  4676. ret = SUCCESS;
  4677. eh_dev_reset_done:
  4678. return ret;
  4679. }
  4680. /**
  4681. * qla4xxx_eh_target_reset - callback for target reset.
  4682. * @cmd: Pointer to Linux's SCSI command structure
  4683. *
  4684. * This routine is called by the Linux OS to reset the target.
  4685. **/
  4686. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  4687. {
  4688. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4689. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  4690. int stat, ret;
  4691. if (!ddb_entry)
  4692. return FAILED;
  4693. ret = iscsi_block_scsi_eh(cmd);
  4694. if (ret)
  4695. return ret;
  4696. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4697. "WARM TARGET RESET ISSUED.\n");
  4698. DEBUG2(printk(KERN_INFO
  4699. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  4700. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  4701. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  4702. ha->dpc_flags, cmd->result, cmd->allowed));
  4703. stat = qla4xxx_reset_target(ha, ddb_entry);
  4704. if (stat != QLA_SUCCESS) {
  4705. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4706. "WARM TARGET RESET FAILED.\n");
  4707. return FAILED;
  4708. }
  4709. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  4710. NULL)) {
  4711. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4712. "WARM TARGET DEVICE RESET FAILED - "
  4713. "waiting for commands.\n");
  4714. return FAILED;
  4715. }
  4716. /* Send marker. */
  4717. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  4718. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  4719. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4720. "WARM TARGET DEVICE RESET FAILED - "
  4721. "marker iocb failed.\n");
  4722. return FAILED;
  4723. }
  4724. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4725. "WARM TARGET RESET SUCCEEDED.\n");
  4726. return SUCCESS;
  4727. }
  4728. /**
  4729. * qla4xxx_is_eh_active - check if error handler is running
  4730. * @shost: Pointer to SCSI Host struct
  4731. *
  4732. * This routine finds that if reset host is called in EH
  4733. * scenario or from some application like sg_reset
  4734. **/
  4735. static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
  4736. {
  4737. if (shost->shost_state == SHOST_RECOVERY)
  4738. return 1;
  4739. return 0;
  4740. }
  4741. /**
  4742. * qla4xxx_eh_host_reset - kernel callback
  4743. * @cmd: Pointer to Linux's SCSI command structure
  4744. *
  4745. * This routine is invoked by the Linux kernel to perform fatal error
  4746. * recovery on the specified adapter.
  4747. **/
  4748. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  4749. {
  4750. int return_status = FAILED;
  4751. struct scsi_qla_host *ha;
  4752. ha = to_qla_host(cmd->device->host);
  4753. if (ql4xdontresethba) {
  4754. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  4755. ha->host_no, __func__));
  4756. /* Clear outstanding srb in queues */
  4757. if (qla4xxx_is_eh_active(cmd->device->host))
  4758. qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
  4759. return FAILED;
  4760. }
  4761. ql4_printk(KERN_INFO, ha,
  4762. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  4763. cmd->device->channel, cmd->device->id, cmd->device->lun);
  4764. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  4765. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  4766. "DEAD.\n", ha->host_no, cmd->device->channel,
  4767. __func__));
  4768. return FAILED;
  4769. }
  4770. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  4771. if (is_qla8022(ha))
  4772. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  4773. else
  4774. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  4775. }
  4776. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  4777. return_status = SUCCESS;
  4778. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  4779. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  4780. return return_status;
  4781. }
  4782. static int qla4xxx_context_reset(struct scsi_qla_host *ha)
  4783. {
  4784. uint32_t mbox_cmd[MBOX_REG_COUNT];
  4785. uint32_t mbox_sts[MBOX_REG_COUNT];
  4786. struct addr_ctrl_blk_def *acb = NULL;
  4787. uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
  4788. int rval = QLA_SUCCESS;
  4789. dma_addr_t acb_dma;
  4790. acb = dma_alloc_coherent(&ha->pdev->dev,
  4791. sizeof(struct addr_ctrl_blk_def),
  4792. &acb_dma, GFP_KERNEL);
  4793. if (!acb) {
  4794. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
  4795. __func__);
  4796. rval = -ENOMEM;
  4797. goto exit_port_reset;
  4798. }
  4799. memset(acb, 0, acb_len);
  4800. rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
  4801. if (rval != QLA_SUCCESS) {
  4802. rval = -EIO;
  4803. goto exit_free_acb;
  4804. }
  4805. rval = qla4xxx_disable_acb(ha);
  4806. if (rval != QLA_SUCCESS) {
  4807. rval = -EIO;
  4808. goto exit_free_acb;
  4809. }
  4810. wait_for_completion_timeout(&ha->disable_acb_comp,
  4811. DISABLE_ACB_TOV * HZ);
  4812. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
  4813. if (rval != QLA_SUCCESS) {
  4814. rval = -EIO;
  4815. goto exit_free_acb;
  4816. }
  4817. exit_free_acb:
  4818. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
  4819. acb, acb_dma);
  4820. exit_port_reset:
  4821. DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
  4822. rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
  4823. return rval;
  4824. }
  4825. static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
  4826. {
  4827. struct scsi_qla_host *ha = to_qla_host(shost);
  4828. int rval = QLA_SUCCESS;
  4829. if (ql4xdontresethba) {
  4830. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
  4831. __func__));
  4832. rval = -EPERM;
  4833. goto exit_host_reset;
  4834. }
  4835. rval = qla4xxx_wait_for_hba_online(ha);
  4836. if (rval != QLA_SUCCESS) {
  4837. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
  4838. "adapter\n", __func__));
  4839. rval = -EIO;
  4840. goto exit_host_reset;
  4841. }
  4842. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  4843. goto recover_adapter;
  4844. switch (reset_type) {
  4845. case SCSI_ADAPTER_RESET:
  4846. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  4847. break;
  4848. case SCSI_FIRMWARE_RESET:
  4849. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  4850. if (is_qla8022(ha))
  4851. /* set firmware context reset */
  4852. set_bit(DPC_RESET_HA_FW_CONTEXT,
  4853. &ha->dpc_flags);
  4854. else {
  4855. rval = qla4xxx_context_reset(ha);
  4856. goto exit_host_reset;
  4857. }
  4858. }
  4859. break;
  4860. }
  4861. recover_adapter:
  4862. rval = qla4xxx_recover_adapter(ha);
  4863. if (rval != QLA_SUCCESS) {
  4864. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
  4865. __func__));
  4866. rval = -EIO;
  4867. }
  4868. exit_host_reset:
  4869. return rval;
  4870. }
  4871. /* PCI AER driver recovers from all correctable errors w/o
  4872. * driver intervention. For uncorrectable errors PCI AER
  4873. * driver calls the following device driver's callbacks
  4874. *
  4875. * - Fatal Errors - link_reset
  4876. * - Non-Fatal Errors - driver's pci_error_detected() which
  4877. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  4878. *
  4879. * PCI AER driver calls
  4880. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  4881. * returns RECOVERED or NEED_RESET if fw_hung
  4882. * NEED_RESET - driver's slot_reset()
  4883. * DISCONNECT - device is dead & cannot recover
  4884. * RECOVERED - driver's pci_resume()
  4885. */
  4886. static pci_ers_result_t
  4887. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  4888. {
  4889. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  4890. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  4891. ha->host_no, __func__, state);
  4892. if (!is_aer_supported(ha))
  4893. return PCI_ERS_RESULT_NONE;
  4894. switch (state) {
  4895. case pci_channel_io_normal:
  4896. clear_bit(AF_EEH_BUSY, &ha->flags);
  4897. return PCI_ERS_RESULT_CAN_RECOVER;
  4898. case pci_channel_io_frozen:
  4899. set_bit(AF_EEH_BUSY, &ha->flags);
  4900. qla4xxx_mailbox_premature_completion(ha);
  4901. qla4xxx_free_irqs(ha);
  4902. pci_disable_device(pdev);
  4903. /* Return back all IOs */
  4904. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  4905. return PCI_ERS_RESULT_NEED_RESET;
  4906. case pci_channel_io_perm_failure:
  4907. set_bit(AF_EEH_BUSY, &ha->flags);
  4908. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  4909. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  4910. return PCI_ERS_RESULT_DISCONNECT;
  4911. }
  4912. return PCI_ERS_RESULT_NEED_RESET;
  4913. }
  4914. /**
  4915. * qla4xxx_pci_mmio_enabled() gets called if
  4916. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  4917. * and read/write to the device still works.
  4918. **/
  4919. static pci_ers_result_t
  4920. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  4921. {
  4922. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  4923. if (!is_aer_supported(ha))
  4924. return PCI_ERS_RESULT_NONE;
  4925. return PCI_ERS_RESULT_RECOVERED;
  4926. }
  4927. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  4928. {
  4929. uint32_t rval = QLA_ERROR;
  4930. uint32_t ret = 0;
  4931. int fn;
  4932. struct pci_dev *other_pdev = NULL;
  4933. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  4934. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  4935. if (test_bit(AF_ONLINE, &ha->flags)) {
  4936. clear_bit(AF_ONLINE, &ha->flags);
  4937. clear_bit(AF_LINK_UP, &ha->flags);
  4938. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  4939. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  4940. }
  4941. fn = PCI_FUNC(ha->pdev->devfn);
  4942. while (fn > 0) {
  4943. fn--;
  4944. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  4945. "func %x\n", ha->host_no, __func__, fn);
  4946. /* Get the pci device given the domain, bus,
  4947. * slot/function number */
  4948. other_pdev =
  4949. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  4950. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  4951. fn));
  4952. if (!other_pdev)
  4953. continue;
  4954. if (atomic_read(&other_pdev->enable_cnt)) {
  4955. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  4956. "func in enabled state%x\n", ha->host_no,
  4957. __func__, fn);
  4958. pci_dev_put(other_pdev);
  4959. break;
  4960. }
  4961. pci_dev_put(other_pdev);
  4962. }
  4963. /* The first function on the card, the reset owner will
  4964. * start & initialize the firmware. The other functions
  4965. * on the card will reset the firmware context
  4966. */
  4967. if (!fn) {
  4968. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  4969. "0x%x is the owner\n", ha->host_no, __func__,
  4970. ha->pdev->devfn);
  4971. qla4_8xxx_idc_lock(ha);
  4972. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  4973. QLA82XX_DEV_COLD);
  4974. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  4975. QLA82XX_IDC_VERSION);
  4976. qla4_8xxx_idc_unlock(ha);
  4977. clear_bit(AF_FW_RECOVERY, &ha->flags);
  4978. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  4979. qla4_8xxx_idc_lock(ha);
  4980. if (rval != QLA_SUCCESS) {
  4981. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  4982. "FAILED\n", ha->host_no, __func__);
  4983. qla4_8xxx_clear_drv_active(ha);
  4984. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  4985. QLA82XX_DEV_FAILED);
  4986. } else {
  4987. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  4988. "READY\n", ha->host_no, __func__);
  4989. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  4990. QLA82XX_DEV_READY);
  4991. /* Clear driver state register */
  4992. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  4993. qla4_8xxx_set_drv_active(ha);
  4994. ret = qla4xxx_request_irqs(ha);
  4995. if (ret) {
  4996. ql4_printk(KERN_WARNING, ha, "Failed to "
  4997. "reserve interrupt %d already in use.\n",
  4998. ha->pdev->irq);
  4999. rval = QLA_ERROR;
  5000. } else {
  5001. ha->isp_ops->enable_intrs(ha);
  5002. rval = QLA_SUCCESS;
  5003. }
  5004. }
  5005. qla4_8xxx_idc_unlock(ha);
  5006. } else {
  5007. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  5008. "the reset owner\n", ha->host_no, __func__,
  5009. ha->pdev->devfn);
  5010. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  5011. QLA82XX_DEV_READY)) {
  5012. clear_bit(AF_FW_RECOVERY, &ha->flags);
  5013. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  5014. if (rval == QLA_SUCCESS) {
  5015. ret = qla4xxx_request_irqs(ha);
  5016. if (ret) {
  5017. ql4_printk(KERN_WARNING, ha, "Failed to"
  5018. " reserve interrupt %d already in"
  5019. " use.\n", ha->pdev->irq);
  5020. rval = QLA_ERROR;
  5021. } else {
  5022. ha->isp_ops->enable_intrs(ha);
  5023. rval = QLA_SUCCESS;
  5024. }
  5025. }
  5026. qla4_8xxx_idc_lock(ha);
  5027. qla4_8xxx_set_drv_active(ha);
  5028. qla4_8xxx_idc_unlock(ha);
  5029. }
  5030. }
  5031. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  5032. return rval;
  5033. }
  5034. static pci_ers_result_t
  5035. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  5036. {
  5037. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  5038. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  5039. int rc;
  5040. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  5041. ha->host_no, __func__);
  5042. if (!is_aer_supported(ha))
  5043. return PCI_ERS_RESULT_NONE;
  5044. /* Restore the saved state of PCIe device -
  5045. * BAR registers, PCI Config space, PCIX, MSI,
  5046. * IOV states
  5047. */
  5048. pci_restore_state(pdev);
  5049. /* pci_restore_state() clears the saved_state flag of the device
  5050. * save restored state which resets saved_state flag
  5051. */
  5052. pci_save_state(pdev);
  5053. /* Initialize device or resume if in suspended state */
  5054. rc = pci_enable_device(pdev);
  5055. if (rc) {
  5056. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  5057. "device after reset\n", ha->host_no, __func__);
  5058. goto exit_slot_reset;
  5059. }
  5060. ha->isp_ops->disable_intrs(ha);
  5061. if (is_qla8022(ha)) {
  5062. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  5063. ret = PCI_ERS_RESULT_RECOVERED;
  5064. goto exit_slot_reset;
  5065. } else
  5066. goto exit_slot_reset;
  5067. }
  5068. exit_slot_reset:
  5069. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  5070. "device after reset\n", ha->host_no, __func__, ret);
  5071. return ret;
  5072. }
  5073. static void
  5074. qla4xxx_pci_resume(struct pci_dev *pdev)
  5075. {
  5076. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  5077. int ret;
  5078. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  5079. ha->host_no, __func__);
  5080. ret = qla4xxx_wait_for_hba_online(ha);
  5081. if (ret != QLA_SUCCESS) {
  5082. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  5083. "resume I/O from slot/link_reset\n", ha->host_no,
  5084. __func__);
  5085. }
  5086. pci_cleanup_aer_uncorrect_error_status(pdev);
  5087. clear_bit(AF_EEH_BUSY, &ha->flags);
  5088. }
  5089. static struct pci_error_handlers qla4xxx_err_handler = {
  5090. .error_detected = qla4xxx_pci_error_detected,
  5091. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  5092. .slot_reset = qla4xxx_pci_slot_reset,
  5093. .resume = qla4xxx_pci_resume,
  5094. };
  5095. static struct pci_device_id qla4xxx_pci_tbl[] = {
  5096. {
  5097. .vendor = PCI_VENDOR_ID_QLOGIC,
  5098. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  5099. .subvendor = PCI_ANY_ID,
  5100. .subdevice = PCI_ANY_ID,
  5101. },
  5102. {
  5103. .vendor = PCI_VENDOR_ID_QLOGIC,
  5104. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  5105. .subvendor = PCI_ANY_ID,
  5106. .subdevice = PCI_ANY_ID,
  5107. },
  5108. {
  5109. .vendor = PCI_VENDOR_ID_QLOGIC,
  5110. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  5111. .subvendor = PCI_ANY_ID,
  5112. .subdevice = PCI_ANY_ID,
  5113. },
  5114. {
  5115. .vendor = PCI_VENDOR_ID_QLOGIC,
  5116. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  5117. .subvendor = PCI_ANY_ID,
  5118. .subdevice = PCI_ANY_ID,
  5119. },
  5120. {0, 0},
  5121. };
  5122. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  5123. static struct pci_driver qla4xxx_pci_driver = {
  5124. .name = DRIVER_NAME,
  5125. .id_table = qla4xxx_pci_tbl,
  5126. .probe = qla4xxx_probe_adapter,
  5127. .remove = qla4xxx_remove_adapter,
  5128. .err_handler = &qla4xxx_err_handler,
  5129. };
  5130. static int __init qla4xxx_module_init(void)
  5131. {
  5132. int ret;
  5133. /* Allocate cache for SRBs. */
  5134. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  5135. SLAB_HWCACHE_ALIGN, NULL);
  5136. if (srb_cachep == NULL) {
  5137. printk(KERN_ERR
  5138. "%s: Unable to allocate SRB cache..."
  5139. "Failing load!\n", DRIVER_NAME);
  5140. ret = -ENOMEM;
  5141. goto no_srp_cache;
  5142. }
  5143. /* Derive version string. */
  5144. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  5145. if (ql4xextended_error_logging)
  5146. strcat(qla4xxx_version_str, "-debug");
  5147. qla4xxx_scsi_transport =
  5148. iscsi_register_transport(&qla4xxx_iscsi_transport);
  5149. if (!qla4xxx_scsi_transport){
  5150. ret = -ENODEV;
  5151. goto release_srb_cache;
  5152. }
  5153. ret = pci_register_driver(&qla4xxx_pci_driver);
  5154. if (ret)
  5155. goto unregister_transport;
  5156. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  5157. return 0;
  5158. unregister_transport:
  5159. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  5160. release_srb_cache:
  5161. kmem_cache_destroy(srb_cachep);
  5162. no_srp_cache:
  5163. return ret;
  5164. }
  5165. static void __exit qla4xxx_module_exit(void)
  5166. {
  5167. pci_unregister_driver(&qla4xxx_pci_driver);
  5168. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  5169. kmem_cache_destroy(srb_cachep);
  5170. }
  5171. module_init(qla4xxx_module_init);
  5172. module_exit(qla4xxx_module_exit);
  5173. MODULE_AUTHOR("QLogic Corporation");
  5174. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  5175. MODULE_LICENSE("GPL");
  5176. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);