ql4_os.c 176 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403
  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 retreive 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. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  2549. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  2550. reset_chip = 1;
  2551. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  2552. * do not reset adapter, jump to initialize_adapter */
  2553. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2554. status = QLA_SUCCESS;
  2555. goto recover_ha_init_adapter;
  2556. }
  2557. /* For the ISP-8xxx adapter, issue a stop_firmware if invoked
  2558. * from eh_host_reset or ioctl module */
  2559. if (is_qla80XX(ha) && !reset_chip &&
  2560. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  2561. DEBUG2(ql4_printk(KERN_INFO, ha,
  2562. "scsi%ld: %s - Performing stop_firmware...\n",
  2563. ha->host_no, __func__));
  2564. status = ha->isp_ops->reset_firmware(ha);
  2565. if (status == QLA_SUCCESS) {
  2566. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  2567. qla4xxx_cmd_wait(ha);
  2568. ha->isp_ops->disable_intrs(ha);
  2569. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2570. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2571. } else {
  2572. /* If the stop_firmware fails then
  2573. * reset the entire chip */
  2574. reset_chip = 1;
  2575. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2576. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2577. }
  2578. }
  2579. /* Issue full chip reset if recovering from a catastrophic error,
  2580. * or if stop_firmware fails for ISP-8xxx.
  2581. * This is the default case for ISP-4xxx */
  2582. if (is_qla40XX(ha) || reset_chip) {
  2583. if (is_qla40XX(ha))
  2584. goto chip_reset;
  2585. /* Check if 8XXX firmware is alive or not
  2586. * We may have arrived here from NEED_RESET
  2587. * detection only */
  2588. if (test_bit(AF_FW_RECOVERY, &ha->flags))
  2589. goto chip_reset;
  2590. wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
  2591. while (time_before(jiffies, wait)) {
  2592. if (qla4_8xxx_check_fw_alive(ha)) {
  2593. qla4xxx_mailbox_premature_completion(ha);
  2594. break;
  2595. }
  2596. set_current_state(TASK_UNINTERRUPTIBLE);
  2597. schedule_timeout(HZ);
  2598. }
  2599. chip_reset:
  2600. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  2601. qla4xxx_cmd_wait(ha);
  2602. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2603. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2604. DEBUG2(ql4_printk(KERN_INFO, ha,
  2605. "scsi%ld: %s - Performing chip reset..\n",
  2606. ha->host_no, __func__));
  2607. status = ha->isp_ops->reset_chip(ha);
  2608. }
  2609. /* Flush any pending ddb changed AENs */
  2610. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2611. recover_ha_init_adapter:
  2612. /* Upon successful firmware/chip reset, re-initialize the adapter */
  2613. if (status == QLA_SUCCESS) {
  2614. /* For ISP-4xxx, force function 1 to always initialize
  2615. * before function 3 to prevent both funcions from
  2616. * stepping on top of the other */
  2617. if (is_qla40XX(ha) && (ha->mac_index == 3))
  2618. ssleep(6);
  2619. /* NOTE: AF_ONLINE flag set upon successful completion of
  2620. * qla4xxx_initialize_adapter */
  2621. status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  2622. }
  2623. /* Retry failed adapter initialization, if necessary
  2624. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  2625. * case to prevent ping-pong resets between functions */
  2626. if (!test_bit(AF_ONLINE, &ha->flags) &&
  2627. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2628. /* Adapter initialization failed, see if we can retry
  2629. * resetting the ha.
  2630. * Since we don't want to block the DPC for too long
  2631. * with multiple resets in the same thread,
  2632. * utilize DPC to retry */
  2633. if (is_qla80XX(ha)) {
  2634. ha->isp_ops->idc_lock(ha);
  2635. dev_state = qla4_8xxx_rd_direct(ha,
  2636. QLA8XXX_CRB_DEV_STATE);
  2637. ha->isp_ops->idc_unlock(ha);
  2638. if (dev_state == QLA8XXX_DEV_FAILED) {
  2639. ql4_printk(KERN_INFO, ha, "%s: don't retry "
  2640. "recover adapter. H/W is in Failed "
  2641. "state\n", __func__);
  2642. qla4xxx_dead_adapter_cleanup(ha);
  2643. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2644. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2645. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  2646. &ha->dpc_flags);
  2647. status = QLA_ERROR;
  2648. goto exit_recover;
  2649. }
  2650. }
  2651. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  2652. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  2653. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  2654. "(%d) more times\n", ha->host_no,
  2655. ha->retry_reset_ha_cnt));
  2656. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2657. status = QLA_ERROR;
  2658. } else {
  2659. if (ha->retry_reset_ha_cnt > 0) {
  2660. /* Schedule another Reset HA--DPC will retry */
  2661. ha->retry_reset_ha_cnt--;
  2662. DEBUG2(printk("scsi%ld: recover adapter - "
  2663. "retry remaining %d\n",
  2664. ha->host_no,
  2665. ha->retry_reset_ha_cnt));
  2666. status = QLA_ERROR;
  2667. }
  2668. if (ha->retry_reset_ha_cnt == 0) {
  2669. /* Recover adapter retries have been exhausted.
  2670. * Adapter DEAD */
  2671. DEBUG2(printk("scsi%ld: recover adapter "
  2672. "failed - board disabled\n",
  2673. ha->host_no));
  2674. qla4xxx_dead_adapter_cleanup(ha);
  2675. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2676. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2677. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  2678. &ha->dpc_flags);
  2679. status = QLA_ERROR;
  2680. }
  2681. }
  2682. } else {
  2683. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2684. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2685. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2686. }
  2687. exit_recover:
  2688. ha->adapter_error_count++;
  2689. if (test_bit(AF_ONLINE, &ha->flags))
  2690. ha->isp_ops->enable_intrs(ha);
  2691. scsi_unblock_requests(ha->host);
  2692. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2693. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  2694. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  2695. return status;
  2696. }
  2697. static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
  2698. {
  2699. struct iscsi_session *sess;
  2700. struct ddb_entry *ddb_entry;
  2701. struct scsi_qla_host *ha;
  2702. sess = cls_session->dd_data;
  2703. ddb_entry = sess->dd_data;
  2704. ha = ddb_entry->ha;
  2705. if (!iscsi_is_session_online(cls_session)) {
  2706. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  2707. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2708. " unblock session\n", ha->host_no, __func__,
  2709. ddb_entry->fw_ddb_index);
  2710. iscsi_unblock_session(ddb_entry->sess);
  2711. } else {
  2712. /* Trigger relogin */
  2713. if (ddb_entry->ddb_type == FLASH_DDB) {
  2714. if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
  2715. qla4xxx_arm_relogin_timer(ddb_entry);
  2716. } else
  2717. iscsi_session_failure(cls_session->dd_data,
  2718. ISCSI_ERR_CONN_FAILED);
  2719. }
  2720. }
  2721. }
  2722. int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
  2723. {
  2724. struct iscsi_session *sess;
  2725. struct ddb_entry *ddb_entry;
  2726. struct scsi_qla_host *ha;
  2727. sess = cls_session->dd_data;
  2728. ddb_entry = sess->dd_data;
  2729. ha = ddb_entry->ha;
  2730. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2731. " unblock session\n", ha->host_no, __func__,
  2732. ddb_entry->fw_ddb_index);
  2733. iscsi_unblock_session(ddb_entry->sess);
  2734. /* Start scan target */
  2735. if (test_bit(AF_ONLINE, &ha->flags)) {
  2736. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2737. " start scan\n", ha->host_no, __func__,
  2738. ddb_entry->fw_ddb_index);
  2739. scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
  2740. }
  2741. return QLA_SUCCESS;
  2742. }
  2743. int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
  2744. {
  2745. struct iscsi_session *sess;
  2746. struct ddb_entry *ddb_entry;
  2747. struct scsi_qla_host *ha;
  2748. int status = QLA_SUCCESS;
  2749. sess = cls_session->dd_data;
  2750. ddb_entry = sess->dd_data;
  2751. ha = ddb_entry->ha;
  2752. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2753. " unblock user space session\n", ha->host_no, __func__,
  2754. ddb_entry->fw_ddb_index);
  2755. if (!iscsi_is_session_online(cls_session)) {
  2756. iscsi_conn_start(ddb_entry->conn);
  2757. iscsi_conn_login_event(ddb_entry->conn,
  2758. ISCSI_CONN_STATE_LOGGED_IN);
  2759. } else {
  2760. ql4_printk(KERN_INFO, ha,
  2761. "scsi%ld: %s: ddb[%d] session [%d] already logged in\n",
  2762. ha->host_no, __func__, ddb_entry->fw_ddb_index,
  2763. cls_session->sid);
  2764. status = QLA_ERROR;
  2765. }
  2766. return status;
  2767. }
  2768. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  2769. {
  2770. iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
  2771. }
  2772. static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  2773. {
  2774. uint16_t relogin_timer;
  2775. struct iscsi_session *sess;
  2776. struct ddb_entry *ddb_entry;
  2777. struct scsi_qla_host *ha;
  2778. sess = cls_sess->dd_data;
  2779. ddb_entry = sess->dd_data;
  2780. ha = ddb_entry->ha;
  2781. relogin_timer = max(ddb_entry->default_relogin_timeout,
  2782. (uint16_t)RELOGIN_TOV);
  2783. atomic_set(&ddb_entry->relogin_timer, relogin_timer);
  2784. DEBUG2(ql4_printk(KERN_INFO, ha,
  2785. "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
  2786. ddb_entry->fw_ddb_index, relogin_timer));
  2787. qla4xxx_login_flash_ddb(cls_sess);
  2788. }
  2789. static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
  2790. {
  2791. struct iscsi_session *sess;
  2792. struct ddb_entry *ddb_entry;
  2793. struct scsi_qla_host *ha;
  2794. sess = cls_sess->dd_data;
  2795. ddb_entry = sess->dd_data;
  2796. ha = ddb_entry->ha;
  2797. if (!(ddb_entry->ddb_type == FLASH_DDB))
  2798. return;
  2799. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  2800. !iscsi_is_session_online(cls_sess)) {
  2801. DEBUG2(ql4_printk(KERN_INFO, ha,
  2802. "relogin issued\n"));
  2803. qla4xxx_relogin_flash_ddb(cls_sess);
  2804. }
  2805. }
  2806. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  2807. {
  2808. if (ha->dpc_thread)
  2809. queue_work(ha->dpc_thread, &ha->dpc_work);
  2810. }
  2811. static struct qla4_work_evt *
  2812. qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
  2813. enum qla4_work_type type)
  2814. {
  2815. struct qla4_work_evt *e;
  2816. uint32_t size = sizeof(struct qla4_work_evt) + data_size;
  2817. e = kzalloc(size, GFP_ATOMIC);
  2818. if (!e)
  2819. return NULL;
  2820. INIT_LIST_HEAD(&e->list);
  2821. e->type = type;
  2822. return e;
  2823. }
  2824. static void qla4xxx_post_work(struct scsi_qla_host *ha,
  2825. struct qla4_work_evt *e)
  2826. {
  2827. unsigned long flags;
  2828. spin_lock_irqsave(&ha->work_lock, flags);
  2829. list_add_tail(&e->list, &ha->work_list);
  2830. spin_unlock_irqrestore(&ha->work_lock, flags);
  2831. qla4xxx_wake_dpc(ha);
  2832. }
  2833. int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
  2834. enum iscsi_host_event_code aen_code,
  2835. uint32_t data_size, uint8_t *data)
  2836. {
  2837. struct qla4_work_evt *e;
  2838. e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
  2839. if (!e)
  2840. return QLA_ERROR;
  2841. e->u.aen.code = aen_code;
  2842. e->u.aen.data_size = data_size;
  2843. memcpy(e->u.aen.data, data, data_size);
  2844. qla4xxx_post_work(ha, e);
  2845. return QLA_SUCCESS;
  2846. }
  2847. int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
  2848. uint32_t status, uint32_t pid,
  2849. uint32_t data_size, uint8_t *data)
  2850. {
  2851. struct qla4_work_evt *e;
  2852. e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
  2853. if (!e)
  2854. return QLA_ERROR;
  2855. e->u.ping.status = status;
  2856. e->u.ping.pid = pid;
  2857. e->u.ping.data_size = data_size;
  2858. memcpy(e->u.ping.data, data, data_size);
  2859. qla4xxx_post_work(ha, e);
  2860. return QLA_SUCCESS;
  2861. }
  2862. static void qla4xxx_do_work(struct scsi_qla_host *ha)
  2863. {
  2864. struct qla4_work_evt *e, *tmp;
  2865. unsigned long flags;
  2866. LIST_HEAD(work);
  2867. spin_lock_irqsave(&ha->work_lock, flags);
  2868. list_splice_init(&ha->work_list, &work);
  2869. spin_unlock_irqrestore(&ha->work_lock, flags);
  2870. list_for_each_entry_safe(e, tmp, &work, list) {
  2871. list_del_init(&e->list);
  2872. switch (e->type) {
  2873. case QLA4_EVENT_AEN:
  2874. iscsi_post_host_event(ha->host_no,
  2875. &qla4xxx_iscsi_transport,
  2876. e->u.aen.code,
  2877. e->u.aen.data_size,
  2878. e->u.aen.data);
  2879. break;
  2880. case QLA4_EVENT_PING_STATUS:
  2881. iscsi_ping_comp_event(ha->host_no,
  2882. &qla4xxx_iscsi_transport,
  2883. e->u.ping.status,
  2884. e->u.ping.pid,
  2885. e->u.ping.data_size,
  2886. e->u.ping.data);
  2887. break;
  2888. default:
  2889. ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
  2890. "supported", e->type);
  2891. }
  2892. kfree(e);
  2893. }
  2894. }
  2895. /**
  2896. * qla4xxx_do_dpc - dpc routine
  2897. * @data: in our case pointer to adapter structure
  2898. *
  2899. * This routine is a task that is schedule by the interrupt handler
  2900. * to perform the background processing for interrupts. We put it
  2901. * on a task queue that is consumed whenever the scheduler runs; that's
  2902. * so you can do anything (i.e. put the process to sleep etc). In fact,
  2903. * the mid-level tries to sleep when it reaches the driver threshold
  2904. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  2905. **/
  2906. static void qla4xxx_do_dpc(struct work_struct *work)
  2907. {
  2908. struct scsi_qla_host *ha =
  2909. container_of(work, struct scsi_qla_host, dpc_work);
  2910. int status = QLA_ERROR;
  2911. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  2912. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  2913. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  2914. /* Initialization not yet finished. Don't do anything yet. */
  2915. if (!test_bit(AF_INIT_DONE, &ha->flags))
  2916. return;
  2917. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  2918. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  2919. ha->host_no, __func__, ha->flags));
  2920. return;
  2921. }
  2922. /* post events to application */
  2923. qla4xxx_do_work(ha);
  2924. if (is_qla80XX(ha)) {
  2925. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  2926. ha->isp_ops->idc_lock(ha);
  2927. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  2928. QLA8XXX_DEV_FAILED);
  2929. ha->isp_ops->idc_unlock(ha);
  2930. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  2931. qla4_8xxx_device_state_handler(ha);
  2932. }
  2933. if (test_and_clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags))
  2934. qla4_83xx_post_idc_ack(ha);
  2935. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  2936. qla4_8xxx_need_qsnt_handler(ha);
  2937. }
  2938. }
  2939. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  2940. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2941. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  2942. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  2943. if ((is_qla8022(ha) && ql4xdontresethba) ||
  2944. (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
  2945. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  2946. ha->host_no, __func__));
  2947. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2948. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2949. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2950. goto dpc_post_reset_ha;
  2951. }
  2952. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  2953. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  2954. qla4xxx_recover_adapter(ha);
  2955. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2956. uint8_t wait_time = RESET_INTR_TOV;
  2957. while ((readw(&ha->reg->ctrl_status) &
  2958. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  2959. if (--wait_time == 0)
  2960. break;
  2961. msleep(1000);
  2962. }
  2963. if (wait_time == 0)
  2964. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  2965. "bit not cleared-- resetting\n",
  2966. ha->host_no, __func__));
  2967. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2968. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  2969. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2970. status = qla4xxx_recover_adapter(ha);
  2971. }
  2972. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2973. if (status == QLA_SUCCESS)
  2974. ha->isp_ops->enable_intrs(ha);
  2975. }
  2976. }
  2977. dpc_post_reset_ha:
  2978. /* ---- process AEN? --- */
  2979. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  2980. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  2981. /* ---- Get DHCP IP Address? --- */
  2982. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  2983. qla4xxx_get_dhcp_ip_address(ha);
  2984. /* ---- relogin device? --- */
  2985. if (adapter_up(ha) &&
  2986. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  2987. iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
  2988. }
  2989. /* ---- link change? --- */
  2990. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  2991. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  2992. /* ---- link down? --- */
  2993. qla4xxx_mark_all_devices_missing(ha);
  2994. } else {
  2995. /* ---- link up? --- *
  2996. * F/W will auto login to all devices ONLY ONCE after
  2997. * link up during driver initialization and runtime
  2998. * fatal error recovery. Therefore, the driver must
  2999. * manually relogin to devices when recovering from
  3000. * connection failures, logouts, expired KATO, etc. */
  3001. if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
  3002. qla4xxx_build_ddb_list(ha, ha->is_reset);
  3003. iscsi_host_for_each_session(ha->host,
  3004. qla4xxx_login_flash_ddb);
  3005. } else
  3006. qla4xxx_relogin_all_devices(ha);
  3007. }
  3008. }
  3009. }
  3010. /**
  3011. * qla4xxx_free_adapter - release the adapter
  3012. * @ha: pointer to adapter structure
  3013. **/
  3014. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  3015. {
  3016. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  3017. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  3018. /* Turn-off interrupts on the card. */
  3019. ha->isp_ops->disable_intrs(ha);
  3020. }
  3021. if (is_qla40XX(ha)) {
  3022. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  3023. &ha->reg->ctrl_status);
  3024. readl(&ha->reg->ctrl_status);
  3025. } else if (is_qla8022(ha)) {
  3026. writel(0, &ha->qla4_82xx_reg->host_int);
  3027. readl(&ha->qla4_82xx_reg->host_int);
  3028. } else if (is_qla8032(ha)) {
  3029. writel(0, &ha->qla4_83xx_reg->risc_intr);
  3030. readl(&ha->qla4_83xx_reg->risc_intr);
  3031. }
  3032. /* Remove timer thread, if present */
  3033. if (ha->timer_active)
  3034. qla4xxx_stop_timer(ha);
  3035. /* Kill the kernel thread for this host */
  3036. if (ha->dpc_thread)
  3037. destroy_workqueue(ha->dpc_thread);
  3038. /* Kill the kernel thread for this host */
  3039. if (ha->task_wq)
  3040. destroy_workqueue(ha->task_wq);
  3041. /* Put firmware in known state */
  3042. ha->isp_ops->reset_firmware(ha);
  3043. if (is_qla80XX(ha)) {
  3044. ha->isp_ops->idc_lock(ha);
  3045. qla4_8xxx_clear_drv_active(ha);
  3046. ha->isp_ops->idc_unlock(ha);
  3047. }
  3048. /* Detach interrupts */
  3049. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  3050. qla4xxx_free_irqs(ha);
  3051. /* free extra memory */
  3052. qla4xxx_mem_free(ha);
  3053. }
  3054. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  3055. {
  3056. int status = 0;
  3057. unsigned long mem_base, mem_len, db_base, db_len;
  3058. struct pci_dev *pdev = ha->pdev;
  3059. status = pci_request_regions(pdev, DRIVER_NAME);
  3060. if (status) {
  3061. printk(KERN_WARNING
  3062. "scsi(%ld) Failed to reserve PIO regions (%s) "
  3063. "status=%d\n", ha->host_no, pci_name(pdev), status);
  3064. goto iospace_error_exit;
  3065. }
  3066. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  3067. __func__, pdev->revision));
  3068. ha->revision_id = pdev->revision;
  3069. /* remap phys address */
  3070. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  3071. mem_len = pci_resource_len(pdev, 0);
  3072. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  3073. __func__, mem_base, mem_len));
  3074. /* mapping of pcibase pointer */
  3075. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  3076. if (!ha->nx_pcibase) {
  3077. printk(KERN_ERR
  3078. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  3079. pci_release_regions(ha->pdev);
  3080. goto iospace_error_exit;
  3081. }
  3082. /* Mapping of IO base pointer, door bell read and write pointer */
  3083. /* mapping of IO base pointer */
  3084. if (is_qla8022(ha)) {
  3085. ha->qla4_82xx_reg = (struct device_reg_82xx __iomem *)
  3086. ((uint8_t *)ha->nx_pcibase + 0xbc000 +
  3087. (ha->pdev->devfn << 11));
  3088. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  3089. QLA82XX_CAM_RAM_DB2);
  3090. } else if (is_qla8032(ha)) {
  3091. ha->qla4_83xx_reg = (struct device_reg_83xx __iomem *)
  3092. ((uint8_t *)ha->nx_pcibase);
  3093. }
  3094. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  3095. db_len = pci_resource_len(pdev, 4);
  3096. return 0;
  3097. iospace_error_exit:
  3098. return -ENOMEM;
  3099. }
  3100. /***
  3101. * qla4xxx_iospace_config - maps registers
  3102. * @ha: pointer to adapter structure
  3103. *
  3104. * This routines maps HBA's registers from the pci address space
  3105. * into the kernel virtual address space for memory mapped i/o.
  3106. **/
  3107. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  3108. {
  3109. unsigned long pio, pio_len, pio_flags;
  3110. unsigned long mmio, mmio_len, mmio_flags;
  3111. pio = pci_resource_start(ha->pdev, 0);
  3112. pio_len = pci_resource_len(ha->pdev, 0);
  3113. pio_flags = pci_resource_flags(ha->pdev, 0);
  3114. if (pio_flags & IORESOURCE_IO) {
  3115. if (pio_len < MIN_IOBASE_LEN) {
  3116. ql4_printk(KERN_WARNING, ha,
  3117. "Invalid PCI I/O region size\n");
  3118. pio = 0;
  3119. }
  3120. } else {
  3121. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  3122. pio = 0;
  3123. }
  3124. /* Use MMIO operations for all accesses. */
  3125. mmio = pci_resource_start(ha->pdev, 1);
  3126. mmio_len = pci_resource_len(ha->pdev, 1);
  3127. mmio_flags = pci_resource_flags(ha->pdev, 1);
  3128. if (!(mmio_flags & IORESOURCE_MEM)) {
  3129. ql4_printk(KERN_ERR, ha,
  3130. "region #0 not an MMIO resource, aborting\n");
  3131. goto iospace_error_exit;
  3132. }
  3133. if (mmio_len < MIN_IOBASE_LEN) {
  3134. ql4_printk(KERN_ERR, ha,
  3135. "Invalid PCI mem region size, aborting\n");
  3136. goto iospace_error_exit;
  3137. }
  3138. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  3139. ql4_printk(KERN_WARNING, ha,
  3140. "Failed to reserve PIO/MMIO regions\n");
  3141. goto iospace_error_exit;
  3142. }
  3143. ha->pio_address = pio;
  3144. ha->pio_length = pio_len;
  3145. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  3146. if (!ha->reg) {
  3147. ql4_printk(KERN_ERR, ha,
  3148. "cannot remap MMIO, aborting\n");
  3149. goto iospace_error_exit;
  3150. }
  3151. return 0;
  3152. iospace_error_exit:
  3153. return -ENOMEM;
  3154. }
  3155. static struct isp_operations qla4xxx_isp_ops = {
  3156. .iospace_config = qla4xxx_iospace_config,
  3157. .pci_config = qla4xxx_pci_config,
  3158. .disable_intrs = qla4xxx_disable_intrs,
  3159. .enable_intrs = qla4xxx_enable_intrs,
  3160. .start_firmware = qla4xxx_start_firmware,
  3161. .intr_handler = qla4xxx_intr_handler,
  3162. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  3163. .reset_chip = qla4xxx_soft_reset,
  3164. .reset_firmware = qla4xxx_hw_reset,
  3165. .queue_iocb = qla4xxx_queue_iocb,
  3166. .complete_iocb = qla4xxx_complete_iocb,
  3167. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  3168. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  3169. .get_sys_info = qla4xxx_get_sys_info,
  3170. .queue_mailbox_command = qla4xxx_queue_mbox_cmd,
  3171. .process_mailbox_interrupt = qla4xxx_process_mbox_intr,
  3172. };
  3173. static struct isp_operations qla4_82xx_isp_ops = {
  3174. .iospace_config = qla4_8xxx_iospace_config,
  3175. .pci_config = qla4_8xxx_pci_config,
  3176. .disable_intrs = qla4_82xx_disable_intrs,
  3177. .enable_intrs = qla4_82xx_enable_intrs,
  3178. .start_firmware = qla4_8xxx_load_risc,
  3179. .restart_firmware = qla4_82xx_try_start_fw,
  3180. .intr_handler = qla4_82xx_intr_handler,
  3181. .interrupt_service_routine = qla4_82xx_interrupt_service_routine,
  3182. .need_reset = qla4_8xxx_need_reset,
  3183. .reset_chip = qla4_82xx_isp_reset,
  3184. .reset_firmware = qla4_8xxx_stop_firmware,
  3185. .queue_iocb = qla4_82xx_queue_iocb,
  3186. .complete_iocb = qla4_82xx_complete_iocb,
  3187. .rd_shdw_req_q_out = qla4_82xx_rd_shdw_req_q_out,
  3188. .rd_shdw_rsp_q_in = qla4_82xx_rd_shdw_rsp_q_in,
  3189. .get_sys_info = qla4_8xxx_get_sys_info,
  3190. .rd_reg_direct = qla4_82xx_rd_32,
  3191. .wr_reg_direct = qla4_82xx_wr_32,
  3192. .rd_reg_indirect = qla4_82xx_md_rd_32,
  3193. .wr_reg_indirect = qla4_82xx_md_wr_32,
  3194. .idc_lock = qla4_82xx_idc_lock,
  3195. .idc_unlock = qla4_82xx_idc_unlock,
  3196. .rom_lock_recovery = qla4_82xx_rom_lock_recovery,
  3197. .queue_mailbox_command = qla4_82xx_queue_mbox_cmd,
  3198. .process_mailbox_interrupt = qla4_82xx_process_mbox_intr,
  3199. };
  3200. static struct isp_operations qla4_83xx_isp_ops = {
  3201. .iospace_config = qla4_8xxx_iospace_config,
  3202. .pci_config = qla4_8xxx_pci_config,
  3203. .disable_intrs = qla4_83xx_disable_intrs,
  3204. .enable_intrs = qla4_83xx_enable_intrs,
  3205. .start_firmware = qla4_8xxx_load_risc,
  3206. .restart_firmware = qla4_83xx_start_firmware,
  3207. .intr_handler = qla4_83xx_intr_handler,
  3208. .interrupt_service_routine = qla4_83xx_interrupt_service_routine,
  3209. .need_reset = qla4_8xxx_need_reset,
  3210. .reset_chip = qla4_83xx_isp_reset,
  3211. .reset_firmware = qla4_8xxx_stop_firmware,
  3212. .queue_iocb = qla4_83xx_queue_iocb,
  3213. .complete_iocb = qla4_83xx_complete_iocb,
  3214. .rd_shdw_req_q_out = qla4_83xx_rd_shdw_req_q_out,
  3215. .rd_shdw_rsp_q_in = qla4_83xx_rd_shdw_rsp_q_in,
  3216. .get_sys_info = qla4_8xxx_get_sys_info,
  3217. .rd_reg_direct = qla4_83xx_rd_reg,
  3218. .wr_reg_direct = qla4_83xx_wr_reg,
  3219. .rd_reg_indirect = qla4_83xx_rd_reg_indirect,
  3220. .wr_reg_indirect = qla4_83xx_wr_reg_indirect,
  3221. .idc_lock = qla4_83xx_drv_lock,
  3222. .idc_unlock = qla4_83xx_drv_unlock,
  3223. .rom_lock_recovery = qla4_83xx_rom_lock_recovery,
  3224. .queue_mailbox_command = qla4_83xx_queue_mbox_cmd,
  3225. .process_mailbox_interrupt = qla4_83xx_process_mbox_intr,
  3226. };
  3227. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  3228. {
  3229. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  3230. }
  3231. uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  3232. {
  3233. return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
  3234. }
  3235. uint16_t qla4_83xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  3236. {
  3237. return (uint16_t)le32_to_cpu(readl(&ha->qla4_83xx_reg->req_q_out));
  3238. }
  3239. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  3240. {
  3241. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  3242. }
  3243. uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  3244. {
  3245. return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
  3246. }
  3247. uint16_t qla4_83xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  3248. {
  3249. return (uint16_t)le32_to_cpu(readl(&ha->qla4_83xx_reg->rsp_q_in));
  3250. }
  3251. static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
  3252. {
  3253. struct scsi_qla_host *ha = data;
  3254. char *str = buf;
  3255. int rc;
  3256. switch (type) {
  3257. case ISCSI_BOOT_ETH_FLAGS:
  3258. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  3259. break;
  3260. case ISCSI_BOOT_ETH_INDEX:
  3261. rc = sprintf(str, "0\n");
  3262. break;
  3263. case ISCSI_BOOT_ETH_MAC:
  3264. rc = sysfs_format_mac(str, ha->my_mac,
  3265. MAC_ADDR_LEN);
  3266. break;
  3267. default:
  3268. rc = -ENOSYS;
  3269. break;
  3270. }
  3271. return rc;
  3272. }
  3273. static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
  3274. {
  3275. int rc;
  3276. switch (type) {
  3277. case ISCSI_BOOT_ETH_FLAGS:
  3278. case ISCSI_BOOT_ETH_MAC:
  3279. case ISCSI_BOOT_ETH_INDEX:
  3280. rc = S_IRUGO;
  3281. break;
  3282. default:
  3283. rc = 0;
  3284. break;
  3285. }
  3286. return rc;
  3287. }
  3288. static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
  3289. {
  3290. struct scsi_qla_host *ha = data;
  3291. char *str = buf;
  3292. int rc;
  3293. switch (type) {
  3294. case ISCSI_BOOT_INI_INITIATOR_NAME:
  3295. rc = sprintf(str, "%s\n", ha->name_string);
  3296. break;
  3297. default:
  3298. rc = -ENOSYS;
  3299. break;
  3300. }
  3301. return rc;
  3302. }
  3303. static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
  3304. {
  3305. int rc;
  3306. switch (type) {
  3307. case ISCSI_BOOT_INI_INITIATOR_NAME:
  3308. rc = S_IRUGO;
  3309. break;
  3310. default:
  3311. rc = 0;
  3312. break;
  3313. }
  3314. return rc;
  3315. }
  3316. static ssize_t
  3317. qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
  3318. char *buf)
  3319. {
  3320. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  3321. char *str = buf;
  3322. int rc;
  3323. switch (type) {
  3324. case ISCSI_BOOT_TGT_NAME:
  3325. rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
  3326. break;
  3327. case ISCSI_BOOT_TGT_IP_ADDR:
  3328. if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
  3329. rc = sprintf(buf, "%pI4\n",
  3330. &boot_conn->dest_ipaddr.ip_address);
  3331. else
  3332. rc = sprintf(str, "%pI6\n",
  3333. &boot_conn->dest_ipaddr.ip_address);
  3334. break;
  3335. case ISCSI_BOOT_TGT_PORT:
  3336. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  3337. break;
  3338. case ISCSI_BOOT_TGT_CHAP_NAME:
  3339. rc = sprintf(str, "%.*s\n",
  3340. boot_conn->chap.target_chap_name_length,
  3341. (char *)&boot_conn->chap.target_chap_name);
  3342. break;
  3343. case ISCSI_BOOT_TGT_CHAP_SECRET:
  3344. rc = sprintf(str, "%.*s\n",
  3345. boot_conn->chap.target_secret_length,
  3346. (char *)&boot_conn->chap.target_secret);
  3347. break;
  3348. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  3349. rc = sprintf(str, "%.*s\n",
  3350. boot_conn->chap.intr_chap_name_length,
  3351. (char *)&boot_conn->chap.intr_chap_name);
  3352. break;
  3353. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  3354. rc = sprintf(str, "%.*s\n",
  3355. boot_conn->chap.intr_secret_length,
  3356. (char *)&boot_conn->chap.intr_secret);
  3357. break;
  3358. case ISCSI_BOOT_TGT_FLAGS:
  3359. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  3360. break;
  3361. case ISCSI_BOOT_TGT_NIC_ASSOC:
  3362. rc = sprintf(str, "0\n");
  3363. break;
  3364. default:
  3365. rc = -ENOSYS;
  3366. break;
  3367. }
  3368. return rc;
  3369. }
  3370. static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
  3371. {
  3372. struct scsi_qla_host *ha = data;
  3373. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
  3374. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  3375. }
  3376. static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
  3377. {
  3378. struct scsi_qla_host *ha = data;
  3379. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
  3380. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  3381. }
  3382. static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
  3383. {
  3384. int rc;
  3385. switch (type) {
  3386. case ISCSI_BOOT_TGT_NAME:
  3387. case ISCSI_BOOT_TGT_IP_ADDR:
  3388. case ISCSI_BOOT_TGT_PORT:
  3389. case ISCSI_BOOT_TGT_CHAP_NAME:
  3390. case ISCSI_BOOT_TGT_CHAP_SECRET:
  3391. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  3392. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  3393. case ISCSI_BOOT_TGT_NIC_ASSOC:
  3394. case ISCSI_BOOT_TGT_FLAGS:
  3395. rc = S_IRUGO;
  3396. break;
  3397. default:
  3398. rc = 0;
  3399. break;
  3400. }
  3401. return rc;
  3402. }
  3403. static void qla4xxx_boot_release(void *data)
  3404. {
  3405. struct scsi_qla_host *ha = data;
  3406. scsi_host_put(ha->host);
  3407. }
  3408. static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
  3409. {
  3410. dma_addr_t buf_dma;
  3411. uint32_t addr, pri_addr, sec_addr;
  3412. uint32_t offset;
  3413. uint16_t func_num;
  3414. uint8_t val;
  3415. uint8_t *buf = NULL;
  3416. size_t size = 13 * sizeof(uint8_t);
  3417. int ret = QLA_SUCCESS;
  3418. func_num = PCI_FUNC(ha->pdev->devfn);
  3419. ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
  3420. __func__, ha->pdev->device, func_num);
  3421. if (is_qla40XX(ha)) {
  3422. if (func_num == 1) {
  3423. addr = NVRAM_PORT0_BOOT_MODE;
  3424. pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
  3425. sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
  3426. } else if (func_num == 3) {
  3427. addr = NVRAM_PORT1_BOOT_MODE;
  3428. pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
  3429. sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
  3430. } else {
  3431. ret = QLA_ERROR;
  3432. goto exit_boot_info;
  3433. }
  3434. /* Check Boot Mode */
  3435. val = rd_nvram_byte(ha, addr);
  3436. if (!(val & 0x07)) {
  3437. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
  3438. "options : 0x%x\n", __func__, val));
  3439. ret = QLA_ERROR;
  3440. goto exit_boot_info;
  3441. }
  3442. /* get primary valid target index */
  3443. val = rd_nvram_byte(ha, pri_addr);
  3444. if (val & BIT_7)
  3445. ddb_index[0] = (val & 0x7f);
  3446. /* get secondary valid target index */
  3447. val = rd_nvram_byte(ha, sec_addr);
  3448. if (val & BIT_7)
  3449. ddb_index[1] = (val & 0x7f);
  3450. } else if (is_qla8022(ha)) {
  3451. buf = dma_alloc_coherent(&ha->pdev->dev, size,
  3452. &buf_dma, GFP_KERNEL);
  3453. if (!buf) {
  3454. DEBUG2(ql4_printk(KERN_ERR, ha,
  3455. "%s: Unable to allocate dma buffer\n",
  3456. __func__));
  3457. ret = QLA_ERROR;
  3458. goto exit_boot_info;
  3459. }
  3460. if (ha->port_num == 0)
  3461. offset = BOOT_PARAM_OFFSET_PORT0;
  3462. else if (ha->port_num == 1)
  3463. offset = BOOT_PARAM_OFFSET_PORT1;
  3464. else {
  3465. ret = QLA_ERROR;
  3466. goto exit_boot_info_free;
  3467. }
  3468. addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
  3469. offset;
  3470. if (qla4xxx_get_flash(ha, buf_dma, addr,
  3471. 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
  3472. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
  3473. " failed\n", ha->host_no, __func__));
  3474. ret = QLA_ERROR;
  3475. goto exit_boot_info_free;
  3476. }
  3477. /* Check Boot Mode */
  3478. if (!(buf[1] & 0x07)) {
  3479. DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
  3480. " : 0x%x\n", buf[1]));
  3481. ret = QLA_ERROR;
  3482. goto exit_boot_info_free;
  3483. }
  3484. /* get primary valid target index */
  3485. if (buf[2] & BIT_7)
  3486. ddb_index[0] = buf[2] & 0x7f;
  3487. /* get secondary valid target index */
  3488. if (buf[11] & BIT_7)
  3489. ddb_index[1] = buf[11] & 0x7f;
  3490. } else {
  3491. ret = QLA_ERROR;
  3492. goto exit_boot_info;
  3493. }
  3494. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
  3495. " target ID %d\n", __func__, ddb_index[0],
  3496. ddb_index[1]));
  3497. exit_boot_info_free:
  3498. dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
  3499. exit_boot_info:
  3500. ha->pri_ddb_idx = ddb_index[0];
  3501. ha->sec_ddb_idx = ddb_index[1];
  3502. return ret;
  3503. }
  3504. /**
  3505. * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
  3506. * @ha: pointer to adapter structure
  3507. * @username: CHAP username to be returned
  3508. * @password: CHAP password to be returned
  3509. *
  3510. * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
  3511. * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
  3512. * So from the CHAP cache find the first BIDI CHAP entry and set it
  3513. * to the boot record in sysfs.
  3514. **/
  3515. static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
  3516. char *password)
  3517. {
  3518. int i, ret = -EINVAL;
  3519. int max_chap_entries = 0;
  3520. struct ql4_chap_table *chap_table;
  3521. if (is_qla8022(ha))
  3522. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  3523. sizeof(struct ql4_chap_table);
  3524. else
  3525. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  3526. if (!ha->chap_list) {
  3527. ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
  3528. return ret;
  3529. }
  3530. mutex_lock(&ha->chap_sem);
  3531. for (i = 0; i < max_chap_entries; i++) {
  3532. chap_table = (struct ql4_chap_table *)ha->chap_list + i;
  3533. if (chap_table->cookie !=
  3534. __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
  3535. continue;
  3536. }
  3537. if (chap_table->flags & BIT_7) /* local */
  3538. continue;
  3539. if (!(chap_table->flags & BIT_6)) /* Not BIDI */
  3540. continue;
  3541. strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
  3542. strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
  3543. ret = 0;
  3544. break;
  3545. }
  3546. mutex_unlock(&ha->chap_sem);
  3547. return ret;
  3548. }
  3549. static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
  3550. struct ql4_boot_session_info *boot_sess,
  3551. uint16_t ddb_index)
  3552. {
  3553. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  3554. struct dev_db_entry *fw_ddb_entry;
  3555. dma_addr_t fw_ddb_entry_dma;
  3556. uint16_t idx;
  3557. uint16_t options;
  3558. int ret = QLA_SUCCESS;
  3559. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  3560. &fw_ddb_entry_dma, GFP_KERNEL);
  3561. if (!fw_ddb_entry) {
  3562. DEBUG2(ql4_printk(KERN_ERR, ha,
  3563. "%s: Unable to allocate dma buffer.\n",
  3564. __func__));
  3565. ret = QLA_ERROR;
  3566. return ret;
  3567. }
  3568. if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
  3569. fw_ddb_entry_dma, ddb_index)) {
  3570. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
  3571. "index [%d]\n", __func__, ddb_index));
  3572. ret = QLA_ERROR;
  3573. goto exit_boot_target;
  3574. }
  3575. /* Update target name and IP from DDB */
  3576. memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
  3577. min(sizeof(boot_sess->target_name),
  3578. sizeof(fw_ddb_entry->iscsi_name)));
  3579. options = le16_to_cpu(fw_ddb_entry->options);
  3580. if (options & DDB_OPT_IPV6_DEVICE) {
  3581. memcpy(&boot_conn->dest_ipaddr.ip_address,
  3582. &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
  3583. } else {
  3584. boot_conn->dest_ipaddr.ip_type = 0x1;
  3585. memcpy(&boot_conn->dest_ipaddr.ip_address,
  3586. &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
  3587. }
  3588. boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
  3589. /* update chap information */
  3590. idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  3591. if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  3592. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
  3593. ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
  3594. target_chap_name,
  3595. (char *)&boot_conn->chap.target_secret,
  3596. idx);
  3597. if (ret) {
  3598. ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
  3599. ret = QLA_ERROR;
  3600. goto exit_boot_target;
  3601. }
  3602. boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  3603. boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  3604. }
  3605. if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  3606. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
  3607. ret = qla4xxx_get_bidi_chap(ha,
  3608. (char *)&boot_conn->chap.intr_chap_name,
  3609. (char *)&boot_conn->chap.intr_secret);
  3610. if (ret) {
  3611. ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
  3612. ret = QLA_ERROR;
  3613. goto exit_boot_target;
  3614. }
  3615. boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  3616. boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  3617. }
  3618. exit_boot_target:
  3619. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  3620. fw_ddb_entry, fw_ddb_entry_dma);
  3621. return ret;
  3622. }
  3623. static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
  3624. {
  3625. uint16_t ddb_index[2];
  3626. int ret = QLA_ERROR;
  3627. int rval;
  3628. memset(ddb_index, 0, sizeof(ddb_index));
  3629. ddb_index[0] = 0xffff;
  3630. ddb_index[1] = 0xffff;
  3631. ret = get_fw_boot_info(ha, ddb_index);
  3632. if (ret != QLA_SUCCESS) {
  3633. DEBUG2(ql4_printk(KERN_INFO, ha,
  3634. "%s: No boot target configured.\n", __func__));
  3635. return ret;
  3636. }
  3637. if (ql4xdisablesysfsboot)
  3638. return QLA_SUCCESS;
  3639. if (ddb_index[0] == 0xffff)
  3640. goto sec_target;
  3641. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
  3642. ddb_index[0]);
  3643. if (rval != QLA_SUCCESS) {
  3644. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
  3645. "configured\n", __func__));
  3646. } else
  3647. ret = QLA_SUCCESS;
  3648. sec_target:
  3649. if (ddb_index[1] == 0xffff)
  3650. goto exit_get_boot_info;
  3651. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
  3652. ddb_index[1]);
  3653. if (rval != QLA_SUCCESS) {
  3654. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
  3655. " configured\n", __func__));
  3656. } else
  3657. ret = QLA_SUCCESS;
  3658. exit_get_boot_info:
  3659. return ret;
  3660. }
  3661. static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
  3662. {
  3663. struct iscsi_boot_kobj *boot_kobj;
  3664. if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
  3665. return QLA_ERROR;
  3666. if (ql4xdisablesysfsboot) {
  3667. ql4_printk(KERN_INFO, ha,
  3668. "%s: syfsboot disabled - driver will trigger login "
  3669. "and publish session for discovery .\n", __func__);
  3670. return QLA_SUCCESS;
  3671. }
  3672. ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
  3673. if (!ha->boot_kset)
  3674. goto kset_free;
  3675. if (!scsi_host_get(ha->host))
  3676. goto kset_free;
  3677. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
  3678. qla4xxx_show_boot_tgt_pri_info,
  3679. qla4xxx_tgt_get_attr_visibility,
  3680. qla4xxx_boot_release);
  3681. if (!boot_kobj)
  3682. goto put_host;
  3683. if (!scsi_host_get(ha->host))
  3684. goto kset_free;
  3685. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
  3686. qla4xxx_show_boot_tgt_sec_info,
  3687. qla4xxx_tgt_get_attr_visibility,
  3688. qla4xxx_boot_release);
  3689. if (!boot_kobj)
  3690. goto put_host;
  3691. if (!scsi_host_get(ha->host))
  3692. goto kset_free;
  3693. boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
  3694. qla4xxx_show_boot_ini_info,
  3695. qla4xxx_ini_get_attr_visibility,
  3696. qla4xxx_boot_release);
  3697. if (!boot_kobj)
  3698. goto put_host;
  3699. if (!scsi_host_get(ha->host))
  3700. goto kset_free;
  3701. boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
  3702. qla4xxx_show_boot_eth_info,
  3703. qla4xxx_eth_get_attr_visibility,
  3704. qla4xxx_boot_release);
  3705. if (!boot_kobj)
  3706. goto put_host;
  3707. return QLA_SUCCESS;
  3708. put_host:
  3709. scsi_host_put(ha->host);
  3710. kset_free:
  3711. iscsi_boot_destroy_kset(ha->boot_kset);
  3712. return -ENOMEM;
  3713. }
  3714. /**
  3715. * qla4xxx_create chap_list - Create CHAP list from FLASH
  3716. * @ha: pointer to adapter structure
  3717. *
  3718. * Read flash and make a list of CHAP entries, during login when a CHAP entry
  3719. * is received, it will be checked in this list. If entry exist then the CHAP
  3720. * entry index is set in the DDB. If CHAP entry does not exist in this list
  3721. * then a new entry is added in FLASH in CHAP table and the index obtained is
  3722. * used in the DDB.
  3723. **/
  3724. static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
  3725. {
  3726. int rval = 0;
  3727. uint8_t *chap_flash_data = NULL;
  3728. uint32_t offset;
  3729. dma_addr_t chap_dma;
  3730. uint32_t chap_size = 0;
  3731. if (is_qla40XX(ha))
  3732. chap_size = MAX_CHAP_ENTRIES_40XX *
  3733. sizeof(struct ql4_chap_table);
  3734. else /* Single region contains CHAP info for both
  3735. * ports which is divided into half for each port.
  3736. */
  3737. chap_size = ha->hw.flt_chap_size / 2;
  3738. chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
  3739. &chap_dma, GFP_KERNEL);
  3740. if (!chap_flash_data) {
  3741. ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
  3742. return;
  3743. }
  3744. if (is_qla40XX(ha))
  3745. offset = FLASH_CHAP_OFFSET;
  3746. else {
  3747. offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
  3748. if (ha->port_num == 1)
  3749. offset += chap_size;
  3750. }
  3751. rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
  3752. if (rval != QLA_SUCCESS)
  3753. goto exit_chap_list;
  3754. if (ha->chap_list == NULL)
  3755. ha->chap_list = vmalloc(chap_size);
  3756. if (ha->chap_list == NULL) {
  3757. ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
  3758. goto exit_chap_list;
  3759. }
  3760. memcpy(ha->chap_list, chap_flash_data, chap_size);
  3761. exit_chap_list:
  3762. dma_free_coherent(&ha->pdev->dev, chap_size,
  3763. chap_flash_data, chap_dma);
  3764. }
  3765. static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
  3766. struct ql4_tuple_ddb *tddb)
  3767. {
  3768. struct scsi_qla_host *ha;
  3769. struct iscsi_cls_session *cls_sess;
  3770. struct iscsi_cls_conn *cls_conn;
  3771. struct iscsi_session *sess;
  3772. struct iscsi_conn *conn;
  3773. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  3774. ha = ddb_entry->ha;
  3775. cls_sess = ddb_entry->sess;
  3776. sess = cls_sess->dd_data;
  3777. cls_conn = ddb_entry->conn;
  3778. conn = cls_conn->dd_data;
  3779. tddb->tpgt = sess->tpgt;
  3780. tddb->port = conn->persistent_port;
  3781. strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
  3782. strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
  3783. }
  3784. static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
  3785. struct ql4_tuple_ddb *tddb,
  3786. uint8_t *flash_isid)
  3787. {
  3788. uint16_t options = 0;
  3789. tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  3790. memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
  3791. min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
  3792. options = le16_to_cpu(fw_ddb_entry->options);
  3793. if (options & DDB_OPT_IPV6_DEVICE)
  3794. sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  3795. else
  3796. sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  3797. tddb->port = le16_to_cpu(fw_ddb_entry->port);
  3798. if (flash_isid == NULL)
  3799. memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0],
  3800. sizeof(tddb->isid));
  3801. else
  3802. memcpy(&tddb->isid[0], &flash_isid[0], sizeof(tddb->isid));
  3803. }
  3804. static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
  3805. struct ql4_tuple_ddb *old_tddb,
  3806. struct ql4_tuple_ddb *new_tddb,
  3807. uint8_t is_isid_compare)
  3808. {
  3809. if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
  3810. return QLA_ERROR;
  3811. if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
  3812. return QLA_ERROR;
  3813. if (old_tddb->port != new_tddb->port)
  3814. return QLA_ERROR;
  3815. /* For multi sessions, driver generates the ISID, so do not compare
  3816. * ISID in reset path since it would be a comparision between the
  3817. * driver generated ISID and firmware generated ISID. This could
  3818. * lead to adding duplicated DDBs in the list as driver generated
  3819. * ISID would not match firmware generated ISID.
  3820. */
  3821. if (is_isid_compare) {
  3822. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: old ISID [%02x%02x%02x"
  3823. "%02x%02x%02x] New ISID [%02x%02x%02x%02x%02x%02x]\n",
  3824. __func__, old_tddb->isid[5], old_tddb->isid[4],
  3825. old_tddb->isid[3], old_tddb->isid[2], old_tddb->isid[1],
  3826. old_tddb->isid[0], new_tddb->isid[5], new_tddb->isid[4],
  3827. new_tddb->isid[3], new_tddb->isid[2], new_tddb->isid[1],
  3828. new_tddb->isid[0]));
  3829. if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
  3830. sizeof(old_tddb->isid)))
  3831. return QLA_ERROR;
  3832. }
  3833. DEBUG2(ql4_printk(KERN_INFO, ha,
  3834. "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
  3835. old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
  3836. old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
  3837. new_tddb->ip_addr, new_tddb->iscsi_name));
  3838. return QLA_SUCCESS;
  3839. }
  3840. static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
  3841. struct dev_db_entry *fw_ddb_entry)
  3842. {
  3843. struct ddb_entry *ddb_entry;
  3844. struct ql4_tuple_ddb *fw_tddb = NULL;
  3845. struct ql4_tuple_ddb *tmp_tddb = NULL;
  3846. int idx;
  3847. int ret = QLA_ERROR;
  3848. fw_tddb = vzalloc(sizeof(*fw_tddb));
  3849. if (!fw_tddb) {
  3850. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3851. "Memory Allocation failed.\n"));
  3852. ret = QLA_SUCCESS;
  3853. goto exit_check;
  3854. }
  3855. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  3856. if (!tmp_tddb) {
  3857. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3858. "Memory Allocation failed.\n"));
  3859. ret = QLA_SUCCESS;
  3860. goto exit_check;
  3861. }
  3862. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
  3863. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  3864. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  3865. if (ddb_entry == NULL)
  3866. continue;
  3867. qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
  3868. if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
  3869. ret = QLA_SUCCESS; /* found */
  3870. goto exit_check;
  3871. }
  3872. }
  3873. exit_check:
  3874. if (fw_tddb)
  3875. vfree(fw_tddb);
  3876. if (tmp_tddb)
  3877. vfree(tmp_tddb);
  3878. return ret;
  3879. }
  3880. /**
  3881. * qla4xxx_check_existing_isid - check if target with same isid exist
  3882. * in target list
  3883. * @list_nt: list of target
  3884. * @isid: isid to check
  3885. *
  3886. * This routine return QLA_SUCCESS if target with same isid exist
  3887. **/
  3888. static int qla4xxx_check_existing_isid(struct list_head *list_nt, uint8_t *isid)
  3889. {
  3890. struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
  3891. struct dev_db_entry *fw_ddb_entry;
  3892. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  3893. fw_ddb_entry = &nt_ddb_idx->fw_ddb;
  3894. if (memcmp(&fw_ddb_entry->isid[0], &isid[0],
  3895. sizeof(nt_ddb_idx->fw_ddb.isid)) == 0) {
  3896. return QLA_SUCCESS;
  3897. }
  3898. }
  3899. return QLA_ERROR;
  3900. }
  3901. /**
  3902. * qla4xxx_update_isid - compare ddbs and updated isid
  3903. * @ha: Pointer to host adapter structure.
  3904. * @list_nt: list of nt target
  3905. * @fw_ddb_entry: firmware ddb entry
  3906. *
  3907. * This routine update isid if ddbs have same iqn, same isid and
  3908. * different IP addr.
  3909. * Return QLA_SUCCESS if isid is updated.
  3910. **/
  3911. static int qla4xxx_update_isid(struct scsi_qla_host *ha,
  3912. struct list_head *list_nt,
  3913. struct dev_db_entry *fw_ddb_entry)
  3914. {
  3915. uint8_t base_value, i;
  3916. base_value = fw_ddb_entry->isid[1] & 0x1f;
  3917. for (i = 0; i < 8; i++) {
  3918. fw_ddb_entry->isid[1] = (base_value | (i << 5));
  3919. if (qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
  3920. break;
  3921. }
  3922. if (!qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
  3923. return QLA_ERROR;
  3924. return QLA_SUCCESS;
  3925. }
  3926. /**
  3927. * qla4xxx_should_update_isid - check if isid need to update
  3928. * @ha: Pointer to host adapter structure.
  3929. * @old_tddb: ddb tuple
  3930. * @new_tddb: ddb tuple
  3931. *
  3932. * Return QLA_SUCCESS if different IP, different PORT, same iqn,
  3933. * same isid
  3934. **/
  3935. static int qla4xxx_should_update_isid(struct scsi_qla_host *ha,
  3936. struct ql4_tuple_ddb *old_tddb,
  3937. struct ql4_tuple_ddb *new_tddb)
  3938. {
  3939. if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr) == 0) {
  3940. /* Same ip */
  3941. if (old_tddb->port == new_tddb->port)
  3942. return QLA_ERROR;
  3943. }
  3944. if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
  3945. /* different iqn */
  3946. return QLA_ERROR;
  3947. if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
  3948. sizeof(old_tddb->isid)))
  3949. /* different isid */
  3950. return QLA_ERROR;
  3951. return QLA_SUCCESS;
  3952. }
  3953. /**
  3954. * qla4xxx_is_flash_ddb_exists - check if fw_ddb_entry already exists in list_nt
  3955. * @ha: Pointer to host adapter structure.
  3956. * @list_nt: list of nt target.
  3957. * @fw_ddb_entry: firmware ddb entry.
  3958. *
  3959. * This routine check if fw_ddb_entry already exists in list_nt to avoid
  3960. * duplicate ddb in list_nt.
  3961. * Return QLA_SUCCESS if duplicate ddb exit in list_nl.
  3962. * Note: This function also update isid of DDB if required.
  3963. **/
  3964. static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
  3965. struct list_head *list_nt,
  3966. struct dev_db_entry *fw_ddb_entry)
  3967. {
  3968. struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
  3969. struct ql4_tuple_ddb *fw_tddb = NULL;
  3970. struct ql4_tuple_ddb *tmp_tddb = NULL;
  3971. int rval, ret = QLA_ERROR;
  3972. fw_tddb = vzalloc(sizeof(*fw_tddb));
  3973. if (!fw_tddb) {
  3974. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3975. "Memory Allocation failed.\n"));
  3976. ret = QLA_SUCCESS;
  3977. goto exit_check;
  3978. }
  3979. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  3980. if (!tmp_tddb) {
  3981. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3982. "Memory Allocation failed.\n"));
  3983. ret = QLA_SUCCESS;
  3984. goto exit_check;
  3985. }
  3986. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
  3987. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  3988. qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb,
  3989. nt_ddb_idx->flash_isid);
  3990. ret = qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true);
  3991. /* found duplicate ddb */
  3992. if (ret == QLA_SUCCESS)
  3993. goto exit_check;
  3994. }
  3995. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  3996. qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb, NULL);
  3997. ret = qla4xxx_should_update_isid(ha, tmp_tddb, fw_tddb);
  3998. if (ret == QLA_SUCCESS) {
  3999. rval = qla4xxx_update_isid(ha, list_nt, fw_ddb_entry);
  4000. if (rval == QLA_SUCCESS)
  4001. ret = QLA_ERROR;
  4002. else
  4003. ret = QLA_SUCCESS;
  4004. goto exit_check;
  4005. }
  4006. }
  4007. exit_check:
  4008. if (fw_tddb)
  4009. vfree(fw_tddb);
  4010. if (tmp_tddb)
  4011. vfree(tmp_tddb);
  4012. return ret;
  4013. }
  4014. static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
  4015. {
  4016. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  4017. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
  4018. list_del_init(&ddb_idx->list);
  4019. vfree(ddb_idx);
  4020. }
  4021. }
  4022. static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
  4023. struct dev_db_entry *fw_ddb_entry)
  4024. {
  4025. struct iscsi_endpoint *ep;
  4026. struct sockaddr_in *addr;
  4027. struct sockaddr_in6 *addr6;
  4028. struct sockaddr *dst_addr;
  4029. char *ip;
  4030. /* TODO: need to destroy on unload iscsi_endpoint*/
  4031. dst_addr = vmalloc(sizeof(*dst_addr));
  4032. if (!dst_addr)
  4033. return NULL;
  4034. if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
  4035. dst_addr->sa_family = AF_INET6;
  4036. addr6 = (struct sockaddr_in6 *)dst_addr;
  4037. ip = (char *)&addr6->sin6_addr;
  4038. memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
  4039. addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
  4040. } else {
  4041. dst_addr->sa_family = AF_INET;
  4042. addr = (struct sockaddr_in *)dst_addr;
  4043. ip = (char *)&addr->sin_addr;
  4044. memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
  4045. addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
  4046. }
  4047. ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
  4048. vfree(dst_addr);
  4049. return ep;
  4050. }
  4051. static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
  4052. {
  4053. if (ql4xdisablesysfsboot)
  4054. return QLA_SUCCESS;
  4055. if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
  4056. return QLA_ERROR;
  4057. return QLA_SUCCESS;
  4058. }
  4059. static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
  4060. struct ddb_entry *ddb_entry)
  4061. {
  4062. uint16_t def_timeout;
  4063. ddb_entry->ddb_type = FLASH_DDB;
  4064. ddb_entry->fw_ddb_index = INVALID_ENTRY;
  4065. ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  4066. ddb_entry->ha = ha;
  4067. ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
  4068. ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
  4069. atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  4070. atomic_set(&ddb_entry->relogin_timer, 0);
  4071. atomic_set(&ddb_entry->relogin_retry_count, 0);
  4072. def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
  4073. ddb_entry->default_relogin_timeout =
  4074. (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
  4075. def_timeout : LOGIN_TOV;
  4076. ddb_entry->default_time2wait =
  4077. le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
  4078. }
  4079. static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
  4080. {
  4081. uint32_t idx = 0;
  4082. uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
  4083. uint32_t sts[MBOX_REG_COUNT];
  4084. uint32_t ip_state;
  4085. unsigned long wtime;
  4086. int ret;
  4087. wtime = jiffies + (HZ * IP_CONFIG_TOV);
  4088. do {
  4089. for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
  4090. if (ip_idx[idx] == -1)
  4091. continue;
  4092. ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
  4093. if (ret == QLA_ERROR) {
  4094. ip_idx[idx] = -1;
  4095. continue;
  4096. }
  4097. ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
  4098. DEBUG2(ql4_printk(KERN_INFO, ha,
  4099. "Waiting for IP state for idx = %d, state = 0x%x\n",
  4100. ip_idx[idx], ip_state));
  4101. if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
  4102. ip_state == IP_ADDRSTATE_INVALID ||
  4103. ip_state == IP_ADDRSTATE_PREFERRED ||
  4104. ip_state == IP_ADDRSTATE_DEPRICATED ||
  4105. ip_state == IP_ADDRSTATE_DISABLING)
  4106. ip_idx[idx] = -1;
  4107. }
  4108. /* Break if all IP states checked */
  4109. if ((ip_idx[0] == -1) &&
  4110. (ip_idx[1] == -1) &&
  4111. (ip_idx[2] == -1) &&
  4112. (ip_idx[3] == -1))
  4113. break;
  4114. schedule_timeout_uninterruptible(HZ);
  4115. } while (time_after(wtime, jiffies));
  4116. }
  4117. static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
  4118. struct list_head *list_st)
  4119. {
  4120. struct qla_ddb_index *st_ddb_idx;
  4121. int max_ddbs;
  4122. int fw_idx_size;
  4123. struct dev_db_entry *fw_ddb_entry;
  4124. dma_addr_t fw_ddb_dma;
  4125. int ret;
  4126. uint32_t idx = 0, next_idx = 0;
  4127. uint32_t state = 0, conn_err = 0;
  4128. uint16_t conn_id = 0;
  4129. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  4130. &fw_ddb_dma);
  4131. if (fw_ddb_entry == NULL) {
  4132. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  4133. goto exit_st_list;
  4134. }
  4135. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  4136. MAX_DEV_DB_ENTRIES;
  4137. fw_idx_size = sizeof(struct qla_ddb_index);
  4138. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  4139. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
  4140. NULL, &next_idx, &state,
  4141. &conn_err, NULL, &conn_id);
  4142. if (ret == QLA_ERROR)
  4143. break;
  4144. /* Ignore DDB if invalid state (unassigned) */
  4145. if (state == DDB_DS_UNASSIGNED)
  4146. goto continue_next_st;
  4147. /* Check if ST, add to the list_st */
  4148. if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
  4149. goto continue_next_st;
  4150. st_ddb_idx = vzalloc(fw_idx_size);
  4151. if (!st_ddb_idx)
  4152. break;
  4153. st_ddb_idx->fw_ddb_idx = idx;
  4154. list_add_tail(&st_ddb_idx->list, list_st);
  4155. continue_next_st:
  4156. if (next_idx == 0)
  4157. break;
  4158. }
  4159. exit_st_list:
  4160. if (fw_ddb_entry)
  4161. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  4162. }
  4163. /**
  4164. * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
  4165. * @ha: pointer to adapter structure
  4166. * @list_ddb: List from which failed ddb to be removed
  4167. *
  4168. * Iterate over the list of DDBs and find and remove DDBs that are either in
  4169. * no connection active state or failed state
  4170. **/
  4171. static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
  4172. struct list_head *list_ddb)
  4173. {
  4174. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  4175. uint32_t next_idx = 0;
  4176. uint32_t state = 0, conn_err = 0;
  4177. int ret;
  4178. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
  4179. ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
  4180. NULL, 0, NULL, &next_idx, &state,
  4181. &conn_err, NULL, NULL);
  4182. if (ret == QLA_ERROR)
  4183. continue;
  4184. if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  4185. state == DDB_DS_SESSION_FAILED) {
  4186. list_del_init(&ddb_idx->list);
  4187. vfree(ddb_idx);
  4188. }
  4189. }
  4190. }
  4191. static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
  4192. struct dev_db_entry *fw_ddb_entry,
  4193. int is_reset)
  4194. {
  4195. struct iscsi_cls_session *cls_sess;
  4196. struct iscsi_session *sess;
  4197. struct iscsi_cls_conn *cls_conn;
  4198. struct iscsi_endpoint *ep;
  4199. uint16_t cmds_max = 32;
  4200. uint16_t conn_id = 0;
  4201. uint32_t initial_cmdsn = 0;
  4202. int ret = QLA_SUCCESS;
  4203. struct ddb_entry *ddb_entry = NULL;
  4204. /* Create session object, with INVALID_ENTRY,
  4205. * the targer_id would get set when we issue the login
  4206. */
  4207. cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
  4208. cmds_max, sizeof(struct ddb_entry),
  4209. sizeof(struct ql4_task_data),
  4210. initial_cmdsn, INVALID_ENTRY);
  4211. if (!cls_sess) {
  4212. ret = QLA_ERROR;
  4213. goto exit_setup;
  4214. }
  4215. /*
  4216. * so calling module_put function to decrement the
  4217. * reference count.
  4218. **/
  4219. module_put(qla4xxx_iscsi_transport.owner);
  4220. sess = cls_sess->dd_data;
  4221. ddb_entry = sess->dd_data;
  4222. ddb_entry->sess = cls_sess;
  4223. cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  4224. memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
  4225. sizeof(struct dev_db_entry));
  4226. qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
  4227. cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
  4228. if (!cls_conn) {
  4229. ret = QLA_ERROR;
  4230. goto exit_setup;
  4231. }
  4232. ddb_entry->conn = cls_conn;
  4233. /* Setup ep, for displaying attributes in sysfs */
  4234. ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
  4235. if (ep) {
  4236. ep->conn = cls_conn;
  4237. cls_conn->ep = ep;
  4238. } else {
  4239. DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
  4240. ret = QLA_ERROR;
  4241. goto exit_setup;
  4242. }
  4243. /* Update sess/conn params */
  4244. qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
  4245. if (is_reset == RESET_ADAPTER) {
  4246. iscsi_block_session(cls_sess);
  4247. /* Use the relogin path to discover new devices
  4248. * by short-circuting the logic of setting
  4249. * timer to relogin - instead set the flags
  4250. * to initiate login right away.
  4251. */
  4252. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  4253. set_bit(DF_RELOGIN, &ddb_entry->flags);
  4254. }
  4255. exit_setup:
  4256. return ret;
  4257. }
  4258. static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
  4259. struct list_head *list_nt, int is_reset)
  4260. {
  4261. struct dev_db_entry *fw_ddb_entry;
  4262. dma_addr_t fw_ddb_dma;
  4263. int max_ddbs;
  4264. int fw_idx_size;
  4265. int ret;
  4266. uint32_t idx = 0, next_idx = 0;
  4267. uint32_t state = 0, conn_err = 0;
  4268. uint16_t conn_id = 0;
  4269. struct qla_ddb_index *nt_ddb_idx;
  4270. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  4271. &fw_ddb_dma);
  4272. if (fw_ddb_entry == NULL) {
  4273. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  4274. goto exit_nt_list;
  4275. }
  4276. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  4277. MAX_DEV_DB_ENTRIES;
  4278. fw_idx_size = sizeof(struct qla_ddb_index);
  4279. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  4280. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
  4281. NULL, &next_idx, &state,
  4282. &conn_err, NULL, &conn_id);
  4283. if (ret == QLA_ERROR)
  4284. break;
  4285. if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
  4286. goto continue_next_nt;
  4287. /* Check if NT, then add to list it */
  4288. if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
  4289. goto continue_next_nt;
  4290. if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
  4291. state == DDB_DS_SESSION_FAILED))
  4292. goto continue_next_nt;
  4293. DEBUG2(ql4_printk(KERN_INFO, ha,
  4294. "Adding DDB to session = 0x%x\n", idx));
  4295. if (is_reset == INIT_ADAPTER) {
  4296. nt_ddb_idx = vmalloc(fw_idx_size);
  4297. if (!nt_ddb_idx)
  4298. break;
  4299. nt_ddb_idx->fw_ddb_idx = idx;
  4300. /* Copy original isid as it may get updated in function
  4301. * qla4xxx_update_isid(). We need original isid in
  4302. * function qla4xxx_compare_tuple_ddb to find duplicate
  4303. * target */
  4304. memcpy(&nt_ddb_idx->flash_isid[0],
  4305. &fw_ddb_entry->isid[0],
  4306. sizeof(nt_ddb_idx->flash_isid));
  4307. ret = qla4xxx_is_flash_ddb_exists(ha, list_nt,
  4308. fw_ddb_entry);
  4309. if (ret == QLA_SUCCESS) {
  4310. /* free nt_ddb_idx and do not add to list_nt */
  4311. vfree(nt_ddb_idx);
  4312. goto continue_next_nt;
  4313. }
  4314. /* Copy updated isid */
  4315. memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
  4316. sizeof(struct dev_db_entry));
  4317. list_add_tail(&nt_ddb_idx->list, list_nt);
  4318. } else if (is_reset == RESET_ADAPTER) {
  4319. if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
  4320. QLA_SUCCESS)
  4321. goto continue_next_nt;
  4322. }
  4323. ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
  4324. if (ret == QLA_ERROR)
  4325. goto exit_nt_list;
  4326. continue_next_nt:
  4327. if (next_idx == 0)
  4328. break;
  4329. }
  4330. exit_nt_list:
  4331. if (fw_ddb_entry)
  4332. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  4333. }
  4334. /**
  4335. * qla4xxx_build_ddb_list - Build ddb list and setup sessions
  4336. * @ha: pointer to adapter structure
  4337. * @is_reset: Is this init path or reset path
  4338. *
  4339. * Create a list of sendtargets (st) from firmware DDBs, issue send targets
  4340. * using connection open, then create the list of normal targets (nt)
  4341. * from firmware DDBs. Based on the list of nt setup session and connection
  4342. * objects.
  4343. **/
  4344. void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
  4345. {
  4346. uint16_t tmo = 0;
  4347. struct list_head list_st, list_nt;
  4348. struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
  4349. unsigned long wtime;
  4350. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  4351. set_bit(AF_BUILD_DDB_LIST, &ha->flags);
  4352. ha->is_reset = is_reset;
  4353. return;
  4354. }
  4355. INIT_LIST_HEAD(&list_st);
  4356. INIT_LIST_HEAD(&list_nt);
  4357. qla4xxx_build_st_list(ha, &list_st);
  4358. /* Before issuing conn open mbox, ensure all IPs states are configured
  4359. * Note, conn open fails if IPs are not configured
  4360. */
  4361. qla4xxx_wait_for_ip_configuration(ha);
  4362. /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
  4363. list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
  4364. qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
  4365. }
  4366. /* Wait to ensure all sendtargets are done for min 12 sec wait */
  4367. tmo = ((ha->def_timeout > LOGIN_TOV) &&
  4368. (ha->def_timeout < LOGIN_TOV * 10) ?
  4369. ha->def_timeout : LOGIN_TOV);
  4370. DEBUG2(ql4_printk(KERN_INFO, ha,
  4371. "Default time to wait for build ddb %d\n", tmo));
  4372. wtime = jiffies + (HZ * tmo);
  4373. do {
  4374. if (list_empty(&list_st))
  4375. break;
  4376. qla4xxx_remove_failed_ddb(ha, &list_st);
  4377. schedule_timeout_uninterruptible(HZ / 10);
  4378. } while (time_after(wtime, jiffies));
  4379. /* Free up the sendtargets list */
  4380. qla4xxx_free_ddb_list(&list_st);
  4381. qla4xxx_build_nt_list(ha, &list_nt, is_reset);
  4382. qla4xxx_free_ddb_list(&list_nt);
  4383. qla4xxx_free_ddb_index(ha);
  4384. }
  4385. /**
  4386. * qla4xxx_probe_adapter - callback function to probe HBA
  4387. * @pdev: pointer to pci_dev structure
  4388. * @pci_device_id: pointer to pci_device entry
  4389. *
  4390. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  4391. * It returns zero if successful. It also initializes all data necessary for
  4392. * the driver.
  4393. **/
  4394. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  4395. const struct pci_device_id *ent)
  4396. {
  4397. int ret = -ENODEV, status;
  4398. struct Scsi_Host *host;
  4399. struct scsi_qla_host *ha;
  4400. uint8_t init_retry_count = 0;
  4401. char buf[34];
  4402. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  4403. uint32_t dev_state;
  4404. if (pci_enable_device(pdev))
  4405. return -1;
  4406. host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
  4407. if (host == NULL) {
  4408. printk(KERN_WARNING
  4409. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  4410. goto probe_disable_device;
  4411. }
  4412. /* Clear our data area */
  4413. ha = to_qla_host(host);
  4414. memset(ha, 0, sizeof(*ha));
  4415. /* Save the information from PCI BIOS. */
  4416. ha->pdev = pdev;
  4417. ha->host = host;
  4418. ha->host_no = host->host_no;
  4419. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  4420. pci_enable_pcie_error_reporting(pdev);
  4421. /* Setup Runtime configurable options */
  4422. if (is_qla8022(ha)) {
  4423. ha->isp_ops = &qla4_82xx_isp_ops;
  4424. ha->reg_tbl = (uint32_t *) qla4_82xx_reg_tbl;
  4425. ha->qdr_sn_window = -1;
  4426. ha->ddr_mn_window = -1;
  4427. ha->curr_window = 255;
  4428. nx_legacy_intr = &legacy_intr[ha->func_num];
  4429. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  4430. ha->nx_legacy_intr.tgt_status_reg =
  4431. nx_legacy_intr->tgt_status_reg;
  4432. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  4433. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  4434. } else if (is_qla8032(ha)) {
  4435. ha->isp_ops = &qla4_83xx_isp_ops;
  4436. ha->reg_tbl = (uint32_t *)qla4_83xx_reg_tbl;
  4437. } else {
  4438. ha->isp_ops = &qla4xxx_isp_ops;
  4439. }
  4440. if (is_qla80XX(ha)) {
  4441. rwlock_init(&ha->hw_lock);
  4442. ha->pf_bit = ha->func_num << 16;
  4443. /* Set EEH reset type to fundamental if required by hba */
  4444. pdev->needs_freset = 1;
  4445. }
  4446. /* Configure PCI I/O space. */
  4447. ret = ha->isp_ops->iospace_config(ha);
  4448. if (ret)
  4449. goto probe_failed_ioconfig;
  4450. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  4451. pdev->device, pdev->irq, ha->reg);
  4452. qla4xxx_config_dma_addressing(ha);
  4453. /* Initialize lists and spinlocks. */
  4454. INIT_LIST_HEAD(&ha->free_srb_q);
  4455. mutex_init(&ha->mbox_sem);
  4456. mutex_init(&ha->chap_sem);
  4457. init_completion(&ha->mbx_intr_comp);
  4458. init_completion(&ha->disable_acb_comp);
  4459. spin_lock_init(&ha->hardware_lock);
  4460. spin_lock_init(&ha->work_lock);
  4461. /* Initialize work list */
  4462. INIT_LIST_HEAD(&ha->work_list);
  4463. /* Allocate dma buffers */
  4464. if (qla4xxx_mem_alloc(ha)) {
  4465. ql4_printk(KERN_WARNING, ha,
  4466. "[ERROR] Failed to allocate memory for adapter\n");
  4467. ret = -ENOMEM;
  4468. goto probe_failed;
  4469. }
  4470. host->cmd_per_lun = 3;
  4471. host->max_channel = 0;
  4472. host->max_lun = MAX_LUNS - 1;
  4473. host->max_id = MAX_TARGETS;
  4474. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  4475. host->can_queue = MAX_SRBS ;
  4476. host->transportt = qla4xxx_scsi_transport;
  4477. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  4478. if (ret) {
  4479. ql4_printk(KERN_WARNING, ha,
  4480. "%s: scsi_init_shared_tag_map failed\n", __func__);
  4481. goto probe_failed;
  4482. }
  4483. pci_set_drvdata(pdev, ha);
  4484. ret = scsi_add_host(host, &pdev->dev);
  4485. if (ret)
  4486. goto probe_failed;
  4487. if (is_qla80XX(ha))
  4488. qla4_8xxx_get_flash_info(ha);
  4489. if (is_qla8032(ha)) {
  4490. qla4_83xx_read_reset_template(ha);
  4491. /*
  4492. * NOTE: If ql4dontresethba==1, set IDC_CTRL DONTRESET_BIT0.
  4493. * If DONRESET_BIT0 is set, drivers should not set dev_state
  4494. * to NEED_RESET. But if NEED_RESET is set, drivers should
  4495. * should honor the reset.
  4496. */
  4497. if (ql4xdontresethba == 1)
  4498. qla4_83xx_set_idc_dontreset(ha);
  4499. }
  4500. /*
  4501. * Initialize the Host adapter request/response queues and
  4502. * firmware
  4503. * NOTE: interrupts enabled upon successful completion
  4504. */
  4505. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  4506. /* Dont retry adapter initialization if IRQ allocation failed */
  4507. if (!test_bit(AF_IRQ_ATTACHED, &ha->flags))
  4508. goto skip_retry_init;
  4509. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  4510. init_retry_count++ < MAX_INIT_RETRIES) {
  4511. if (is_qla80XX(ha)) {
  4512. ha->isp_ops->idc_lock(ha);
  4513. dev_state = qla4_8xxx_rd_direct(ha,
  4514. QLA82XX_CRB_DEV_STATE);
  4515. ha->isp_ops->idc_unlock(ha);
  4516. if (dev_state == QLA8XXX_DEV_FAILED) {
  4517. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  4518. "initialize adapter. H/W is in failed state\n",
  4519. __func__);
  4520. break;
  4521. }
  4522. }
  4523. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  4524. "(%d)\n", __func__, init_retry_count));
  4525. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  4526. continue;
  4527. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  4528. }
  4529. skip_retry_init:
  4530. if (!test_bit(AF_ONLINE, &ha->flags)) {
  4531. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  4532. if ((is_qla8022(ha) && ql4xdontresethba) ||
  4533. (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
  4534. /* Put the device in failed state. */
  4535. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  4536. ha->isp_ops->idc_lock(ha);
  4537. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  4538. QLA8XXX_DEV_FAILED);
  4539. ha->isp_ops->idc_unlock(ha);
  4540. }
  4541. ret = -ENODEV;
  4542. goto remove_host;
  4543. }
  4544. /* Startup the kernel thread for this host adapter. */
  4545. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  4546. "qla4xxx_dpc\n", __func__));
  4547. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  4548. ha->dpc_thread = create_singlethread_workqueue(buf);
  4549. if (!ha->dpc_thread) {
  4550. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  4551. ret = -ENODEV;
  4552. goto remove_host;
  4553. }
  4554. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  4555. sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
  4556. ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
  4557. if (!ha->task_wq) {
  4558. ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
  4559. ret = -ENODEV;
  4560. goto remove_host;
  4561. }
  4562. /*
  4563. * For ISP-8XXX, request_irqs is called in qla4_8xxx_load_risc
  4564. * (which is called indirectly by qla4xxx_initialize_adapter),
  4565. * so that irqs will be registered after crbinit but before
  4566. * mbx_intr_enable.
  4567. */
  4568. if (is_qla40XX(ha)) {
  4569. ret = qla4xxx_request_irqs(ha);
  4570. if (ret) {
  4571. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  4572. "interrupt %d already in use.\n", pdev->irq);
  4573. goto remove_host;
  4574. }
  4575. }
  4576. pci_save_state(ha->pdev);
  4577. ha->isp_ops->enable_intrs(ha);
  4578. /* Start timer thread. */
  4579. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  4580. set_bit(AF_INIT_DONE, &ha->flags);
  4581. qla4_8xxx_alloc_sysfs_attr(ha);
  4582. printk(KERN_INFO
  4583. " QLogic iSCSI HBA Driver version: %s\n"
  4584. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  4585. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  4586. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  4587. ha->patch_number, ha->build_number);
  4588. /* Set the driver version */
  4589. if (is_qla80XX(ha))
  4590. qla4_8xxx_set_param(ha, SET_DRVR_VERSION);
  4591. if (qla4xxx_setup_boot_info(ha))
  4592. ql4_printk(KERN_ERR, ha,
  4593. "%s: No iSCSI boot target configured\n", __func__);
  4594. /* Perform the build ddb list and login to each */
  4595. qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
  4596. iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
  4597. qla4xxx_create_chap_list(ha);
  4598. qla4xxx_create_ifaces(ha);
  4599. return 0;
  4600. remove_host:
  4601. scsi_remove_host(ha->host);
  4602. probe_failed:
  4603. qla4xxx_free_adapter(ha);
  4604. probe_failed_ioconfig:
  4605. pci_disable_pcie_error_reporting(pdev);
  4606. scsi_host_put(ha->host);
  4607. probe_disable_device:
  4608. pci_disable_device(pdev);
  4609. return ret;
  4610. }
  4611. /**
  4612. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  4613. * @ha: pointer to adapter structure
  4614. *
  4615. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  4616. * so that the other port will not re-initialize while in the process of
  4617. * removing the ha due to driver unload or hba hotplug.
  4618. **/
  4619. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  4620. {
  4621. struct scsi_qla_host *other_ha = NULL;
  4622. struct pci_dev *other_pdev = NULL;
  4623. int fn = ISP4XXX_PCI_FN_2;
  4624. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  4625. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  4626. fn = ISP4XXX_PCI_FN_1;
  4627. other_pdev =
  4628. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  4629. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  4630. fn));
  4631. /* Get other_ha if other_pdev is valid and state is enable*/
  4632. if (other_pdev) {
  4633. if (atomic_read(&other_pdev->enable_cnt)) {
  4634. other_ha = pci_get_drvdata(other_pdev);
  4635. if (other_ha) {
  4636. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  4637. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  4638. "Prevent %s reinit\n", __func__,
  4639. dev_name(&other_ha->pdev->dev)));
  4640. }
  4641. }
  4642. pci_dev_put(other_pdev);
  4643. }
  4644. }
  4645. static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
  4646. {
  4647. struct ddb_entry *ddb_entry;
  4648. int options;
  4649. int idx;
  4650. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  4651. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  4652. if ((ddb_entry != NULL) &&
  4653. (ddb_entry->ddb_type == FLASH_DDB)) {
  4654. options = LOGOUT_OPTION_CLOSE_SESSION;
  4655. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
  4656. == QLA_ERROR)
  4657. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
  4658. __func__);
  4659. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  4660. /*
  4661. * we have decremented the reference count of the driver
  4662. * when we setup the session to have the driver unload
  4663. * to be seamless without actually destroying the
  4664. * session
  4665. **/
  4666. try_module_get(qla4xxx_iscsi_transport.owner);
  4667. iscsi_destroy_endpoint(ddb_entry->conn->ep);
  4668. qla4xxx_free_ddb(ha, ddb_entry);
  4669. iscsi_session_teardown(ddb_entry->sess);
  4670. }
  4671. }
  4672. }
  4673. /**
  4674. * qla4xxx_remove_adapter - calback function to remove adapter.
  4675. * @pci_dev: PCI device pointer
  4676. **/
  4677. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  4678. {
  4679. struct scsi_qla_host *ha;
  4680. /*
  4681. * If the PCI device is disabled then it means probe_adapter had
  4682. * failed and resources already cleaned up on probe_adapter exit.
  4683. */
  4684. if (!pci_is_enabled(pdev))
  4685. return;
  4686. ha = pci_get_drvdata(pdev);
  4687. if (is_qla40XX(ha))
  4688. qla4xxx_prevent_other_port_reinit(ha);
  4689. /* destroy iface from sysfs */
  4690. qla4xxx_destroy_ifaces(ha);
  4691. if ((!ql4xdisablesysfsboot) && ha->boot_kset)
  4692. iscsi_boot_destroy_kset(ha->boot_kset);
  4693. qla4xxx_destroy_fw_ddb_session(ha);
  4694. qla4_8xxx_free_sysfs_attr(ha);
  4695. scsi_remove_host(ha->host);
  4696. qla4xxx_free_adapter(ha);
  4697. scsi_host_put(ha->host);
  4698. pci_disable_pcie_error_reporting(pdev);
  4699. pci_disable_device(pdev);
  4700. pci_set_drvdata(pdev, NULL);
  4701. }
  4702. /**
  4703. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  4704. * @ha: HA context
  4705. *
  4706. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  4707. * supported addressing method.
  4708. */
  4709. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  4710. {
  4711. int retval;
  4712. /* Update our PCI device dma_mask for full 64 bit mask */
  4713. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  4714. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  4715. dev_dbg(&ha->pdev->dev,
  4716. "Failed to set 64 bit PCI consistent mask; "
  4717. "using 32 bit.\n");
  4718. retval = pci_set_consistent_dma_mask(ha->pdev,
  4719. DMA_BIT_MASK(32));
  4720. }
  4721. } else
  4722. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  4723. }
  4724. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  4725. {
  4726. struct iscsi_cls_session *cls_sess;
  4727. struct iscsi_session *sess;
  4728. struct ddb_entry *ddb;
  4729. int queue_depth = QL4_DEF_QDEPTH;
  4730. cls_sess = starget_to_session(sdev->sdev_target);
  4731. sess = cls_sess->dd_data;
  4732. ddb = sess->dd_data;
  4733. sdev->hostdata = ddb;
  4734. sdev->tagged_supported = 1;
  4735. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  4736. queue_depth = ql4xmaxqdepth;
  4737. scsi_activate_tcq(sdev, queue_depth);
  4738. return 0;
  4739. }
  4740. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  4741. {
  4742. sdev->tagged_supported = 1;
  4743. return 0;
  4744. }
  4745. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  4746. {
  4747. scsi_deactivate_tcq(sdev, 1);
  4748. }
  4749. static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
  4750. int reason)
  4751. {
  4752. if (!ql4xqfulltracking)
  4753. return -EOPNOTSUPP;
  4754. return iscsi_change_queue_depth(sdev, qdepth, reason);
  4755. }
  4756. /**
  4757. * qla4xxx_del_from_active_array - returns an active srb
  4758. * @ha: Pointer to host adapter structure.
  4759. * @index: index into the active_array
  4760. *
  4761. * This routine removes and returns the srb at the specified index
  4762. **/
  4763. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  4764. uint32_t index)
  4765. {
  4766. struct srb *srb = NULL;
  4767. struct scsi_cmnd *cmd = NULL;
  4768. cmd = scsi_host_find_tag(ha->host, index);
  4769. if (!cmd)
  4770. return srb;
  4771. srb = (struct srb *)CMD_SP(cmd);
  4772. if (!srb)
  4773. return srb;
  4774. /* update counters */
  4775. if (srb->flags & SRB_DMA_VALID) {
  4776. ha->req_q_count += srb->iocb_cnt;
  4777. ha->iocb_cnt -= srb->iocb_cnt;
  4778. if (srb->cmd)
  4779. srb->cmd->host_scribble =
  4780. (unsigned char *)(unsigned long) MAX_SRBS;
  4781. }
  4782. return srb;
  4783. }
  4784. /**
  4785. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  4786. * @ha: Pointer to host adapter structure.
  4787. * @cmd: Scsi Command to wait on.
  4788. *
  4789. * This routine waits for the command to be returned by the Firmware
  4790. * for some max time.
  4791. **/
  4792. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  4793. struct scsi_cmnd *cmd)
  4794. {
  4795. int done = 0;
  4796. struct srb *rp;
  4797. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  4798. int ret = SUCCESS;
  4799. /* Dont wait on command if PCI error is being handled
  4800. * by PCI AER driver
  4801. */
  4802. if (unlikely(pci_channel_offline(ha->pdev)) ||
  4803. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  4804. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  4805. ha->host_no, __func__);
  4806. return ret;
  4807. }
  4808. do {
  4809. /* Checking to see if its returned to OS */
  4810. rp = (struct srb *) CMD_SP(cmd);
  4811. if (rp == NULL) {
  4812. done++;
  4813. break;
  4814. }
  4815. msleep(2000);
  4816. } while (max_wait_time--);
  4817. return done;
  4818. }
  4819. /**
  4820. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  4821. * @ha: Pointer to host adapter structure
  4822. **/
  4823. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  4824. {
  4825. unsigned long wait_online;
  4826. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  4827. while (time_before(jiffies, wait_online)) {
  4828. if (adapter_up(ha))
  4829. return QLA_SUCCESS;
  4830. msleep(2000);
  4831. }
  4832. return QLA_ERROR;
  4833. }
  4834. /**
  4835. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  4836. * @ha: pointer to HBA
  4837. * @t: target id
  4838. * @l: lun id
  4839. *
  4840. * This function waits for all outstanding commands to a lun to complete. It
  4841. * returns 0 if all pending commands are returned and 1 otherwise.
  4842. **/
  4843. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  4844. struct scsi_target *stgt,
  4845. struct scsi_device *sdev)
  4846. {
  4847. int cnt;
  4848. int status = 0;
  4849. struct scsi_cmnd *cmd;
  4850. /*
  4851. * Waiting for all commands for the designated target or dev
  4852. * in the active array
  4853. */
  4854. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  4855. cmd = scsi_host_find_tag(ha->host, cnt);
  4856. if (cmd && stgt == scsi_target(cmd->device) &&
  4857. (!sdev || sdev == cmd->device)) {
  4858. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  4859. status++;
  4860. break;
  4861. }
  4862. }
  4863. }
  4864. return status;
  4865. }
  4866. /**
  4867. * qla4xxx_eh_abort - callback for abort task.
  4868. * @cmd: Pointer to Linux's SCSI command structure
  4869. *
  4870. * This routine is called by the Linux OS to abort the specified
  4871. * command.
  4872. **/
  4873. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  4874. {
  4875. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4876. unsigned int id = cmd->device->id;
  4877. unsigned int lun = cmd->device->lun;
  4878. unsigned long flags;
  4879. struct srb *srb = NULL;
  4880. int ret = SUCCESS;
  4881. int wait = 0;
  4882. ql4_printk(KERN_INFO, ha,
  4883. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  4884. ha->host_no, id, lun, cmd);
  4885. spin_lock_irqsave(&ha->hardware_lock, flags);
  4886. srb = (struct srb *) CMD_SP(cmd);
  4887. if (!srb) {
  4888. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4889. return SUCCESS;
  4890. }
  4891. kref_get(&srb->srb_ref);
  4892. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4893. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  4894. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  4895. ha->host_no, id, lun));
  4896. ret = FAILED;
  4897. } else {
  4898. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  4899. ha->host_no, id, lun));
  4900. wait = 1;
  4901. }
  4902. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  4903. /* Wait for command to complete */
  4904. if (wait) {
  4905. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  4906. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  4907. ha->host_no, id, lun));
  4908. ret = FAILED;
  4909. }
  4910. }
  4911. ql4_printk(KERN_INFO, ha,
  4912. "scsi%ld:%d:%d: Abort command - %s\n",
  4913. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  4914. return ret;
  4915. }
  4916. /**
  4917. * qla4xxx_eh_device_reset - callback for target reset.
  4918. * @cmd: Pointer to Linux's SCSI command structure
  4919. *
  4920. * This routine is called by the Linux OS to reset all luns on the
  4921. * specified target.
  4922. **/
  4923. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  4924. {
  4925. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4926. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  4927. int ret = FAILED, stat;
  4928. if (!ddb_entry)
  4929. return ret;
  4930. ret = iscsi_block_scsi_eh(cmd);
  4931. if (ret)
  4932. return ret;
  4933. ret = FAILED;
  4934. ql4_printk(KERN_INFO, ha,
  4935. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  4936. cmd->device->channel, cmd->device->id, cmd->device->lun);
  4937. DEBUG2(printk(KERN_INFO
  4938. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  4939. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  4940. cmd, jiffies, cmd->request->timeout / HZ,
  4941. ha->dpc_flags, cmd->result, cmd->allowed));
  4942. /* FIXME: wait for hba to go online */
  4943. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  4944. if (stat != QLA_SUCCESS) {
  4945. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  4946. goto eh_dev_reset_done;
  4947. }
  4948. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  4949. cmd->device)) {
  4950. ql4_printk(KERN_INFO, ha,
  4951. "DEVICE RESET FAILED - waiting for "
  4952. "commands.\n");
  4953. goto eh_dev_reset_done;
  4954. }
  4955. /* Send marker. */
  4956. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  4957. MM_LUN_RESET) != QLA_SUCCESS)
  4958. goto eh_dev_reset_done;
  4959. ql4_printk(KERN_INFO, ha,
  4960. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  4961. ha->host_no, cmd->device->channel, cmd->device->id,
  4962. cmd->device->lun);
  4963. ret = SUCCESS;
  4964. eh_dev_reset_done:
  4965. return ret;
  4966. }
  4967. /**
  4968. * qla4xxx_eh_target_reset - callback for target reset.
  4969. * @cmd: Pointer to Linux's SCSI command structure
  4970. *
  4971. * This routine is called by the Linux OS to reset the target.
  4972. **/
  4973. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  4974. {
  4975. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4976. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  4977. int stat, ret;
  4978. if (!ddb_entry)
  4979. return FAILED;
  4980. ret = iscsi_block_scsi_eh(cmd);
  4981. if (ret)
  4982. return ret;
  4983. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4984. "WARM TARGET RESET ISSUED.\n");
  4985. DEBUG2(printk(KERN_INFO
  4986. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  4987. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  4988. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  4989. ha->dpc_flags, cmd->result, cmd->allowed));
  4990. stat = qla4xxx_reset_target(ha, ddb_entry);
  4991. if (stat != QLA_SUCCESS) {
  4992. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4993. "WARM TARGET RESET FAILED.\n");
  4994. return FAILED;
  4995. }
  4996. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  4997. NULL)) {
  4998. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4999. "WARM TARGET DEVICE RESET FAILED - "
  5000. "waiting for commands.\n");
  5001. return FAILED;
  5002. }
  5003. /* Send marker. */
  5004. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  5005. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  5006. starget_printk(KERN_INFO, scsi_target(cmd->device),
  5007. "WARM TARGET DEVICE RESET FAILED - "
  5008. "marker iocb failed.\n");
  5009. return FAILED;
  5010. }
  5011. starget_printk(KERN_INFO, scsi_target(cmd->device),
  5012. "WARM TARGET RESET SUCCEEDED.\n");
  5013. return SUCCESS;
  5014. }
  5015. /**
  5016. * qla4xxx_is_eh_active - check if error handler is running
  5017. * @shost: Pointer to SCSI Host struct
  5018. *
  5019. * This routine finds that if reset host is called in EH
  5020. * scenario or from some application like sg_reset
  5021. **/
  5022. static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
  5023. {
  5024. if (shost->shost_state == SHOST_RECOVERY)
  5025. return 1;
  5026. return 0;
  5027. }
  5028. /**
  5029. * qla4xxx_eh_host_reset - kernel callback
  5030. * @cmd: Pointer to Linux's SCSI command structure
  5031. *
  5032. * This routine is invoked by the Linux kernel to perform fatal error
  5033. * recovery on the specified adapter.
  5034. **/
  5035. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  5036. {
  5037. int return_status = FAILED;
  5038. struct scsi_qla_host *ha;
  5039. ha = to_qla_host(cmd->device->host);
  5040. if (is_qla8032(ha) && ql4xdontresethba)
  5041. qla4_83xx_set_idc_dontreset(ha);
  5042. /*
  5043. * For ISP8324, if IDC_CTRL DONTRESET_BIT0 is set by other
  5044. * protocol drivers, we should not set device_state to
  5045. * NEED_RESET
  5046. */
  5047. if (ql4xdontresethba ||
  5048. (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
  5049. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  5050. ha->host_no, __func__));
  5051. /* Clear outstanding srb in queues */
  5052. if (qla4xxx_is_eh_active(cmd->device->host))
  5053. qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
  5054. return FAILED;
  5055. }
  5056. ql4_printk(KERN_INFO, ha,
  5057. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  5058. cmd->device->channel, cmd->device->id, cmd->device->lun);
  5059. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  5060. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  5061. "DEAD.\n", ha->host_no, cmd->device->channel,
  5062. __func__));
  5063. return FAILED;
  5064. }
  5065. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  5066. if (is_qla80XX(ha))
  5067. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  5068. else
  5069. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  5070. }
  5071. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  5072. return_status = SUCCESS;
  5073. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  5074. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  5075. return return_status;
  5076. }
  5077. static int qla4xxx_context_reset(struct scsi_qla_host *ha)
  5078. {
  5079. uint32_t mbox_cmd[MBOX_REG_COUNT];
  5080. uint32_t mbox_sts[MBOX_REG_COUNT];
  5081. struct addr_ctrl_blk_def *acb = NULL;
  5082. uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
  5083. int rval = QLA_SUCCESS;
  5084. dma_addr_t acb_dma;
  5085. acb = dma_alloc_coherent(&ha->pdev->dev,
  5086. sizeof(struct addr_ctrl_blk_def),
  5087. &acb_dma, GFP_KERNEL);
  5088. if (!acb) {
  5089. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
  5090. __func__);
  5091. rval = -ENOMEM;
  5092. goto exit_port_reset;
  5093. }
  5094. memset(acb, 0, acb_len);
  5095. rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
  5096. if (rval != QLA_SUCCESS) {
  5097. rval = -EIO;
  5098. goto exit_free_acb;
  5099. }
  5100. rval = qla4xxx_disable_acb(ha);
  5101. if (rval != QLA_SUCCESS) {
  5102. rval = -EIO;
  5103. goto exit_free_acb;
  5104. }
  5105. wait_for_completion_timeout(&ha->disable_acb_comp,
  5106. DISABLE_ACB_TOV * HZ);
  5107. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
  5108. if (rval != QLA_SUCCESS) {
  5109. rval = -EIO;
  5110. goto exit_free_acb;
  5111. }
  5112. exit_free_acb:
  5113. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
  5114. acb, acb_dma);
  5115. exit_port_reset:
  5116. DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
  5117. rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
  5118. return rval;
  5119. }
  5120. static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
  5121. {
  5122. struct scsi_qla_host *ha = to_qla_host(shost);
  5123. int rval = QLA_SUCCESS;
  5124. if (ql4xdontresethba) {
  5125. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
  5126. __func__));
  5127. rval = -EPERM;
  5128. goto exit_host_reset;
  5129. }
  5130. rval = qla4xxx_wait_for_hba_online(ha);
  5131. if (rval != QLA_SUCCESS) {
  5132. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
  5133. "adapter\n", __func__));
  5134. rval = -EIO;
  5135. goto exit_host_reset;
  5136. }
  5137. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  5138. goto recover_adapter;
  5139. switch (reset_type) {
  5140. case SCSI_ADAPTER_RESET:
  5141. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  5142. break;
  5143. case SCSI_FIRMWARE_RESET:
  5144. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  5145. if (is_qla80XX(ha))
  5146. /* set firmware context reset */
  5147. set_bit(DPC_RESET_HA_FW_CONTEXT,
  5148. &ha->dpc_flags);
  5149. else {
  5150. rval = qla4xxx_context_reset(ha);
  5151. goto exit_host_reset;
  5152. }
  5153. }
  5154. break;
  5155. }
  5156. recover_adapter:
  5157. rval = qla4xxx_recover_adapter(ha);
  5158. if (rval != QLA_SUCCESS) {
  5159. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
  5160. __func__));
  5161. rval = -EIO;
  5162. }
  5163. exit_host_reset:
  5164. return rval;
  5165. }
  5166. /* PCI AER driver recovers from all correctable errors w/o
  5167. * driver intervention. For uncorrectable errors PCI AER
  5168. * driver calls the following device driver's callbacks
  5169. *
  5170. * - Fatal Errors - link_reset
  5171. * - Non-Fatal Errors - driver's pci_error_detected() which
  5172. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  5173. *
  5174. * PCI AER driver calls
  5175. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  5176. * returns RECOVERED or NEED_RESET if fw_hung
  5177. * NEED_RESET - driver's slot_reset()
  5178. * DISCONNECT - device is dead & cannot recover
  5179. * RECOVERED - driver's pci_resume()
  5180. */
  5181. static pci_ers_result_t
  5182. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  5183. {
  5184. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  5185. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  5186. ha->host_no, __func__, state);
  5187. if (!is_aer_supported(ha))
  5188. return PCI_ERS_RESULT_NONE;
  5189. switch (state) {
  5190. case pci_channel_io_normal:
  5191. clear_bit(AF_EEH_BUSY, &ha->flags);
  5192. return PCI_ERS_RESULT_CAN_RECOVER;
  5193. case pci_channel_io_frozen:
  5194. set_bit(AF_EEH_BUSY, &ha->flags);
  5195. qla4xxx_mailbox_premature_completion(ha);
  5196. qla4xxx_free_irqs(ha);
  5197. pci_disable_device(pdev);
  5198. /* Return back all IOs */
  5199. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  5200. return PCI_ERS_RESULT_NEED_RESET;
  5201. case pci_channel_io_perm_failure:
  5202. set_bit(AF_EEH_BUSY, &ha->flags);
  5203. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  5204. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  5205. return PCI_ERS_RESULT_DISCONNECT;
  5206. }
  5207. return PCI_ERS_RESULT_NEED_RESET;
  5208. }
  5209. /**
  5210. * qla4xxx_pci_mmio_enabled() gets called if
  5211. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  5212. * and read/write to the device still works.
  5213. **/
  5214. static pci_ers_result_t
  5215. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  5216. {
  5217. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  5218. if (!is_aer_supported(ha))
  5219. return PCI_ERS_RESULT_NONE;
  5220. return PCI_ERS_RESULT_RECOVERED;
  5221. }
  5222. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  5223. {
  5224. uint32_t rval = QLA_ERROR;
  5225. uint32_t ret = 0;
  5226. int fn;
  5227. struct pci_dev *other_pdev = NULL;
  5228. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  5229. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  5230. if (test_bit(AF_ONLINE, &ha->flags)) {
  5231. clear_bit(AF_ONLINE, &ha->flags);
  5232. clear_bit(AF_LINK_UP, &ha->flags);
  5233. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  5234. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  5235. }
  5236. fn = PCI_FUNC(ha->pdev->devfn);
  5237. while (fn > 0) {
  5238. fn--;
  5239. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  5240. "func %x\n", ha->host_no, __func__, fn);
  5241. /* Get the pci device given the domain, bus,
  5242. * slot/function number */
  5243. other_pdev =
  5244. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  5245. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  5246. fn));
  5247. if (!other_pdev)
  5248. continue;
  5249. if (atomic_read(&other_pdev->enable_cnt)) {
  5250. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  5251. "func in enabled state%x\n", ha->host_no,
  5252. __func__, fn);
  5253. pci_dev_put(other_pdev);
  5254. break;
  5255. }
  5256. pci_dev_put(other_pdev);
  5257. }
  5258. /* The first function on the card, the reset owner will
  5259. * start & initialize the firmware. The other functions
  5260. * on the card will reset the firmware context
  5261. */
  5262. if (!fn) {
  5263. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  5264. "0x%x is the owner\n", ha->host_no, __func__,
  5265. ha->pdev->devfn);
  5266. ha->isp_ops->idc_lock(ha);
  5267. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  5268. QLA8XXX_DEV_COLD);
  5269. ha->isp_ops->idc_unlock(ha);
  5270. rval = qla4_8xxx_update_idc_reg(ha);
  5271. if (rval == QLA_ERROR) {
  5272. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: FAILED\n",
  5273. ha->host_no, __func__);
  5274. ha->isp_ops->idc_lock(ha);
  5275. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  5276. QLA8XXX_DEV_FAILED);
  5277. ha->isp_ops->idc_unlock(ha);
  5278. goto exit_error_recovery;
  5279. }
  5280. clear_bit(AF_FW_RECOVERY, &ha->flags);
  5281. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  5282. if (rval != QLA_SUCCESS) {
  5283. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  5284. "FAILED\n", ha->host_no, __func__);
  5285. ha->isp_ops->idc_lock(ha);
  5286. qla4_8xxx_clear_drv_active(ha);
  5287. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  5288. QLA8XXX_DEV_FAILED);
  5289. ha->isp_ops->idc_unlock(ha);
  5290. } else {
  5291. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  5292. "READY\n", ha->host_no, __func__);
  5293. ha->isp_ops->idc_lock(ha);
  5294. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  5295. QLA8XXX_DEV_READY);
  5296. /* Clear driver state register */
  5297. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, 0);
  5298. qla4_8xxx_set_drv_active(ha);
  5299. ha->isp_ops->idc_unlock(ha);
  5300. ret = qla4xxx_request_irqs(ha);
  5301. if (ret) {
  5302. ql4_printk(KERN_WARNING, ha, "Failed to "
  5303. "reserve interrupt %d already in use.\n",
  5304. ha->pdev->irq);
  5305. rval = QLA_ERROR;
  5306. } else {
  5307. ha->isp_ops->enable_intrs(ha);
  5308. rval = QLA_SUCCESS;
  5309. }
  5310. }
  5311. } else {
  5312. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  5313. "the reset owner\n", ha->host_no, __func__,
  5314. ha->pdev->devfn);
  5315. if ((qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE) ==
  5316. QLA8XXX_DEV_READY)) {
  5317. clear_bit(AF_FW_RECOVERY, &ha->flags);
  5318. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  5319. if (rval == QLA_SUCCESS) {
  5320. ret = qla4xxx_request_irqs(ha);
  5321. if (ret) {
  5322. ql4_printk(KERN_WARNING, ha, "Failed to"
  5323. " reserve interrupt %d already in"
  5324. " use.\n", ha->pdev->irq);
  5325. rval = QLA_ERROR;
  5326. } else {
  5327. ha->isp_ops->enable_intrs(ha);
  5328. rval = QLA_SUCCESS;
  5329. }
  5330. }
  5331. ha->isp_ops->idc_lock(ha);
  5332. qla4_8xxx_set_drv_active(ha);
  5333. ha->isp_ops->idc_unlock(ha);
  5334. }
  5335. }
  5336. exit_error_recovery:
  5337. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  5338. return rval;
  5339. }
  5340. static pci_ers_result_t
  5341. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  5342. {
  5343. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  5344. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  5345. int rc;
  5346. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  5347. ha->host_no, __func__);
  5348. if (!is_aer_supported(ha))
  5349. return PCI_ERS_RESULT_NONE;
  5350. /* Restore the saved state of PCIe device -
  5351. * BAR registers, PCI Config space, PCIX, MSI,
  5352. * IOV states
  5353. */
  5354. pci_restore_state(pdev);
  5355. /* pci_restore_state() clears the saved_state flag of the device
  5356. * save restored state which resets saved_state flag
  5357. */
  5358. pci_save_state(pdev);
  5359. /* Initialize device or resume if in suspended state */
  5360. rc = pci_enable_device(pdev);
  5361. if (rc) {
  5362. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  5363. "device after reset\n", ha->host_no, __func__);
  5364. goto exit_slot_reset;
  5365. }
  5366. ha->isp_ops->disable_intrs(ha);
  5367. if (is_qla80XX(ha)) {
  5368. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  5369. ret = PCI_ERS_RESULT_RECOVERED;
  5370. goto exit_slot_reset;
  5371. } else
  5372. goto exit_slot_reset;
  5373. }
  5374. exit_slot_reset:
  5375. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  5376. "device after reset\n", ha->host_no, __func__, ret);
  5377. return ret;
  5378. }
  5379. static void
  5380. qla4xxx_pci_resume(struct pci_dev *pdev)
  5381. {
  5382. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  5383. int ret;
  5384. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  5385. ha->host_no, __func__);
  5386. ret = qla4xxx_wait_for_hba_online(ha);
  5387. if (ret != QLA_SUCCESS) {
  5388. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  5389. "resume I/O from slot/link_reset\n", ha->host_no,
  5390. __func__);
  5391. }
  5392. pci_cleanup_aer_uncorrect_error_status(pdev);
  5393. clear_bit(AF_EEH_BUSY, &ha->flags);
  5394. }
  5395. static struct pci_error_handlers qla4xxx_err_handler = {
  5396. .error_detected = qla4xxx_pci_error_detected,
  5397. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  5398. .slot_reset = qla4xxx_pci_slot_reset,
  5399. .resume = qla4xxx_pci_resume,
  5400. };
  5401. static struct pci_device_id qla4xxx_pci_tbl[] = {
  5402. {
  5403. .vendor = PCI_VENDOR_ID_QLOGIC,
  5404. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  5405. .subvendor = PCI_ANY_ID,
  5406. .subdevice = PCI_ANY_ID,
  5407. },
  5408. {
  5409. .vendor = PCI_VENDOR_ID_QLOGIC,
  5410. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  5411. .subvendor = PCI_ANY_ID,
  5412. .subdevice = PCI_ANY_ID,
  5413. },
  5414. {
  5415. .vendor = PCI_VENDOR_ID_QLOGIC,
  5416. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  5417. .subvendor = PCI_ANY_ID,
  5418. .subdevice = PCI_ANY_ID,
  5419. },
  5420. {
  5421. .vendor = PCI_VENDOR_ID_QLOGIC,
  5422. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  5423. .subvendor = PCI_ANY_ID,
  5424. .subdevice = PCI_ANY_ID,
  5425. },
  5426. {
  5427. .vendor = PCI_VENDOR_ID_QLOGIC,
  5428. .device = PCI_DEVICE_ID_QLOGIC_ISP8324,
  5429. .subvendor = PCI_ANY_ID,
  5430. .subdevice = PCI_ANY_ID,
  5431. },
  5432. {0, 0},
  5433. };
  5434. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  5435. static struct pci_driver qla4xxx_pci_driver = {
  5436. .name = DRIVER_NAME,
  5437. .id_table = qla4xxx_pci_tbl,
  5438. .probe = qla4xxx_probe_adapter,
  5439. .remove = qla4xxx_remove_adapter,
  5440. .err_handler = &qla4xxx_err_handler,
  5441. };
  5442. static int __init qla4xxx_module_init(void)
  5443. {
  5444. int ret;
  5445. /* Allocate cache for SRBs. */
  5446. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  5447. SLAB_HWCACHE_ALIGN, NULL);
  5448. if (srb_cachep == NULL) {
  5449. printk(KERN_ERR
  5450. "%s: Unable to allocate SRB cache..."
  5451. "Failing load!\n", DRIVER_NAME);
  5452. ret = -ENOMEM;
  5453. goto no_srp_cache;
  5454. }
  5455. /* Derive version string. */
  5456. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  5457. if (ql4xextended_error_logging)
  5458. strcat(qla4xxx_version_str, "-debug");
  5459. qla4xxx_scsi_transport =
  5460. iscsi_register_transport(&qla4xxx_iscsi_transport);
  5461. if (!qla4xxx_scsi_transport){
  5462. ret = -ENODEV;
  5463. goto release_srb_cache;
  5464. }
  5465. ret = pci_register_driver(&qla4xxx_pci_driver);
  5466. if (ret)
  5467. goto unregister_transport;
  5468. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  5469. return 0;
  5470. unregister_transport:
  5471. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  5472. release_srb_cache:
  5473. kmem_cache_destroy(srb_cachep);
  5474. no_srp_cache:
  5475. return ret;
  5476. }
  5477. static void __exit qla4xxx_module_exit(void)
  5478. {
  5479. pci_unregister_driver(&qla4xxx_pci_driver);
  5480. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  5481. kmem_cache_destroy(srb_cachep);
  5482. }
  5483. module_init(qla4xxx_module_init);
  5484. module_exit(qla4xxx_module_exit);
  5485. MODULE_AUTHOR("QLogic Corporation");
  5486. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  5487. MODULE_LICENSE("GPL");
  5488. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);