ql4_os.c 153 KB

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