ql4_os.c 151 KB

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