ql4_os.c 176 KB

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