ql4_os.c 165 KB

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