ql4_os.c 244 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2013 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. static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void *data,
  138. int len);
  139. /*
  140. * SCSI host template entry points
  141. */
  142. static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  143. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  144. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  145. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  146. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  147. static int qla4xxx_slave_alloc(struct scsi_device *device);
  148. static int qla4xxx_slave_configure(struct scsi_device *device);
  149. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  150. static umode_t qla4_attr_is_visible(int param_type, int param);
  151. static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
  152. static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
  153. int reason);
  154. /*
  155. * iSCSI Flash DDB sysfs entry points
  156. */
  157. static int
  158. qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
  159. struct iscsi_bus_flash_conn *fnode_conn,
  160. void *data, int len);
  161. static int
  162. qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
  163. int param, char *buf);
  164. static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
  165. int len);
  166. static int
  167. qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess);
  168. static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
  169. struct iscsi_bus_flash_conn *fnode_conn);
  170. static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
  171. struct iscsi_bus_flash_conn *fnode_conn);
  172. static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess);
  173. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  174. QLA82XX_LEGACY_INTR_CONFIG;
  175. static struct scsi_host_template qla4xxx_driver_template = {
  176. .module = THIS_MODULE,
  177. .name = DRIVER_NAME,
  178. .proc_name = DRIVER_NAME,
  179. .queuecommand = qla4xxx_queuecommand,
  180. .eh_abort_handler = qla4xxx_eh_abort,
  181. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  182. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  183. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  184. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  185. .slave_configure = qla4xxx_slave_configure,
  186. .slave_alloc = qla4xxx_slave_alloc,
  187. .slave_destroy = qla4xxx_slave_destroy,
  188. .change_queue_depth = qla4xxx_change_queue_depth,
  189. .this_id = -1,
  190. .cmd_per_lun = 3,
  191. .use_clustering = ENABLE_CLUSTERING,
  192. .sg_tablesize = SG_ALL,
  193. .max_sectors = 0xFFFF,
  194. .shost_attrs = qla4xxx_host_attrs,
  195. .host_reset = qla4xxx_host_reset,
  196. .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
  197. };
  198. static struct iscsi_transport qla4xxx_iscsi_transport = {
  199. .owner = THIS_MODULE,
  200. .name = DRIVER_NAME,
  201. .caps = CAP_TEXT_NEGO |
  202. CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
  203. CAP_DATADGST | CAP_LOGIN_OFFLOAD |
  204. CAP_MULTI_R2T,
  205. .attr_is_visible = qla4_attr_is_visible,
  206. .create_session = qla4xxx_session_create,
  207. .destroy_session = qla4xxx_session_destroy,
  208. .start_conn = qla4xxx_conn_start,
  209. .create_conn = qla4xxx_conn_create,
  210. .bind_conn = qla4xxx_conn_bind,
  211. .stop_conn = iscsi_conn_stop,
  212. .destroy_conn = qla4xxx_conn_destroy,
  213. .set_param = iscsi_set_param,
  214. .get_conn_param = qla4xxx_conn_get_param,
  215. .get_session_param = qla4xxx_session_get_param,
  216. .get_ep_param = qla4xxx_get_ep_param,
  217. .ep_connect = qla4xxx_ep_connect,
  218. .ep_poll = qla4xxx_ep_poll,
  219. .ep_disconnect = qla4xxx_ep_disconnect,
  220. .get_stats = qla4xxx_conn_get_stats,
  221. .send_pdu = iscsi_conn_send_pdu,
  222. .xmit_task = qla4xxx_task_xmit,
  223. .cleanup_task = qla4xxx_task_cleanup,
  224. .alloc_pdu = qla4xxx_alloc_pdu,
  225. .get_host_param = qla4xxx_host_get_param,
  226. .set_iface_param = qla4xxx_iface_set_param,
  227. .get_iface_param = qla4xxx_get_iface_param,
  228. .bsg_request = qla4xxx_bsg_request,
  229. .send_ping = qla4xxx_send_ping,
  230. .get_chap = qla4xxx_get_chap_list,
  231. .delete_chap = qla4xxx_delete_chap,
  232. .set_chap = qla4xxx_set_chap_entry,
  233. .get_flashnode_param = qla4xxx_sysfs_ddb_get_param,
  234. .set_flashnode_param = qla4xxx_sysfs_ddb_set_param,
  235. .new_flashnode = qla4xxx_sysfs_ddb_add,
  236. .del_flashnode = qla4xxx_sysfs_ddb_delete,
  237. .login_flashnode = qla4xxx_sysfs_ddb_login,
  238. .logout_flashnode = qla4xxx_sysfs_ddb_logout,
  239. .logout_flashnode_sid = qla4xxx_sysfs_ddb_logout_sid,
  240. };
  241. static struct scsi_transport_template *qla4xxx_scsi_transport;
  242. static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
  243. uint32_t iface_type, uint32_t payload_size,
  244. uint32_t pid, struct sockaddr *dst_addr)
  245. {
  246. struct scsi_qla_host *ha = to_qla_host(shost);
  247. struct sockaddr_in *addr;
  248. struct sockaddr_in6 *addr6;
  249. uint32_t options = 0;
  250. uint8_t ipaddr[IPv6_ADDR_LEN];
  251. int rval;
  252. memset(ipaddr, 0, IPv6_ADDR_LEN);
  253. /* IPv4 to IPv4 */
  254. if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
  255. (dst_addr->sa_family == AF_INET)) {
  256. addr = (struct sockaddr_in *)dst_addr;
  257. memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
  258. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
  259. "dest: %pI4\n", __func__,
  260. &ha->ip_config.ip_address, ipaddr));
  261. rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
  262. ipaddr);
  263. if (rval)
  264. rval = -EINVAL;
  265. } else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
  266. (dst_addr->sa_family == AF_INET6)) {
  267. /* IPv6 to IPv6 */
  268. addr6 = (struct sockaddr_in6 *)dst_addr;
  269. memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
  270. options |= PING_IPV6_PROTOCOL_ENABLE;
  271. /* Ping using LinkLocal address */
  272. if ((iface_num == 0) || (iface_num == 1)) {
  273. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
  274. "src: %pI6 dest: %pI6\n", __func__,
  275. &ha->ip_config.ipv6_link_local_addr,
  276. ipaddr));
  277. options |= PING_IPV6_LINKLOCAL_ADDR;
  278. rval = qla4xxx_ping_iocb(ha, options, payload_size,
  279. pid, ipaddr);
  280. } else {
  281. ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
  282. "not supported\n", __func__, iface_num);
  283. rval = -ENOSYS;
  284. goto exit_send_ping;
  285. }
  286. /*
  287. * If ping using LinkLocal address fails, try ping using
  288. * IPv6 address
  289. */
  290. if (rval != QLA_SUCCESS) {
  291. options &= ~PING_IPV6_LINKLOCAL_ADDR;
  292. if (iface_num == 0) {
  293. options |= PING_IPV6_ADDR0;
  294. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
  295. "Ping src: %pI6 "
  296. "dest: %pI6\n", __func__,
  297. &ha->ip_config.ipv6_addr0,
  298. ipaddr));
  299. } else if (iface_num == 1) {
  300. options |= PING_IPV6_ADDR1;
  301. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
  302. "Ping src: %pI6 "
  303. "dest: %pI6\n", __func__,
  304. &ha->ip_config.ipv6_addr1,
  305. ipaddr));
  306. }
  307. rval = qla4xxx_ping_iocb(ha, options, payload_size,
  308. pid, ipaddr);
  309. if (rval)
  310. rval = -EINVAL;
  311. }
  312. } else
  313. rval = -ENOSYS;
  314. exit_send_ping:
  315. return rval;
  316. }
  317. static umode_t qla4_attr_is_visible(int param_type, int param)
  318. {
  319. switch (param_type) {
  320. case ISCSI_HOST_PARAM:
  321. switch (param) {
  322. case ISCSI_HOST_PARAM_HWADDRESS:
  323. case ISCSI_HOST_PARAM_IPADDRESS:
  324. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  325. case ISCSI_HOST_PARAM_PORT_STATE:
  326. case ISCSI_HOST_PARAM_PORT_SPEED:
  327. return S_IRUGO;
  328. default:
  329. return 0;
  330. }
  331. case ISCSI_PARAM:
  332. switch (param) {
  333. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  334. case ISCSI_PARAM_PERSISTENT_PORT:
  335. case ISCSI_PARAM_CONN_ADDRESS:
  336. case ISCSI_PARAM_CONN_PORT:
  337. case ISCSI_PARAM_TARGET_NAME:
  338. case ISCSI_PARAM_TPGT:
  339. case ISCSI_PARAM_TARGET_ALIAS:
  340. case ISCSI_PARAM_MAX_BURST:
  341. case ISCSI_PARAM_MAX_R2T:
  342. case ISCSI_PARAM_FIRST_BURST:
  343. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  344. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  345. case ISCSI_PARAM_IFACE_NAME:
  346. case ISCSI_PARAM_CHAP_OUT_IDX:
  347. case ISCSI_PARAM_CHAP_IN_IDX:
  348. case ISCSI_PARAM_USERNAME:
  349. case ISCSI_PARAM_PASSWORD:
  350. case ISCSI_PARAM_USERNAME_IN:
  351. case ISCSI_PARAM_PASSWORD_IN:
  352. case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
  353. case ISCSI_PARAM_DISCOVERY_SESS:
  354. case ISCSI_PARAM_PORTAL_TYPE:
  355. case ISCSI_PARAM_CHAP_AUTH_EN:
  356. case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
  357. case ISCSI_PARAM_BIDI_CHAP_EN:
  358. case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
  359. case ISCSI_PARAM_DEF_TIME2WAIT:
  360. case ISCSI_PARAM_DEF_TIME2RETAIN:
  361. case ISCSI_PARAM_HDRDGST_EN:
  362. case ISCSI_PARAM_DATADGST_EN:
  363. case ISCSI_PARAM_INITIAL_R2T_EN:
  364. case ISCSI_PARAM_IMM_DATA_EN:
  365. case ISCSI_PARAM_PDU_INORDER_EN:
  366. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  367. case ISCSI_PARAM_MAX_SEGMENT_SIZE:
  368. case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
  369. case ISCSI_PARAM_TCP_WSF_DISABLE:
  370. case ISCSI_PARAM_TCP_NAGLE_DISABLE:
  371. case ISCSI_PARAM_TCP_TIMER_SCALE:
  372. case ISCSI_PARAM_TCP_TIMESTAMP_EN:
  373. case ISCSI_PARAM_TCP_XMIT_WSF:
  374. case ISCSI_PARAM_TCP_RECV_WSF:
  375. case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
  376. case ISCSI_PARAM_IPV4_TOS:
  377. case ISCSI_PARAM_IPV6_TC:
  378. case ISCSI_PARAM_IPV6_FLOW_LABEL:
  379. case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
  380. case ISCSI_PARAM_KEEPALIVE_TMO:
  381. case ISCSI_PARAM_LOCAL_PORT:
  382. case ISCSI_PARAM_ISID:
  383. case ISCSI_PARAM_TSID:
  384. case ISCSI_PARAM_DEF_TASKMGMT_TMO:
  385. case ISCSI_PARAM_ERL:
  386. case ISCSI_PARAM_STATSN:
  387. case ISCSI_PARAM_EXP_STATSN:
  388. case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
  389. case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
  390. return S_IRUGO;
  391. default:
  392. return 0;
  393. }
  394. case ISCSI_NET_PARAM:
  395. switch (param) {
  396. case ISCSI_NET_PARAM_IPV4_ADDR:
  397. case ISCSI_NET_PARAM_IPV4_SUBNET:
  398. case ISCSI_NET_PARAM_IPV4_GW:
  399. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  400. case ISCSI_NET_PARAM_IFACE_ENABLE:
  401. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  402. case ISCSI_NET_PARAM_IPV6_ADDR:
  403. case ISCSI_NET_PARAM_IPV6_ROUTER:
  404. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  405. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  406. case ISCSI_NET_PARAM_VLAN_ID:
  407. case ISCSI_NET_PARAM_VLAN_PRIORITY:
  408. case ISCSI_NET_PARAM_VLAN_ENABLED:
  409. case ISCSI_NET_PARAM_MTU:
  410. case ISCSI_NET_PARAM_PORT:
  411. return S_IRUGO;
  412. default:
  413. return 0;
  414. }
  415. case ISCSI_FLASHNODE_PARAM:
  416. switch (param) {
  417. case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
  418. case ISCSI_FLASHNODE_PORTAL_TYPE:
  419. case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
  420. case ISCSI_FLASHNODE_DISCOVERY_SESS:
  421. case ISCSI_FLASHNODE_ENTRY_EN:
  422. case ISCSI_FLASHNODE_HDR_DGST_EN:
  423. case ISCSI_FLASHNODE_DATA_DGST_EN:
  424. case ISCSI_FLASHNODE_IMM_DATA_EN:
  425. case ISCSI_FLASHNODE_INITIAL_R2T_EN:
  426. case ISCSI_FLASHNODE_DATASEQ_INORDER:
  427. case ISCSI_FLASHNODE_PDU_INORDER:
  428. case ISCSI_FLASHNODE_CHAP_AUTH_EN:
  429. case ISCSI_FLASHNODE_SNACK_REQ_EN:
  430. case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
  431. case ISCSI_FLASHNODE_BIDI_CHAP_EN:
  432. case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
  433. case ISCSI_FLASHNODE_ERL:
  434. case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
  435. case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
  436. case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
  437. case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
  438. case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
  439. case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
  440. case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
  441. case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
  442. case ISCSI_FLASHNODE_FIRST_BURST:
  443. case ISCSI_FLASHNODE_DEF_TIME2WAIT:
  444. case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
  445. case ISCSI_FLASHNODE_MAX_R2T:
  446. case ISCSI_FLASHNODE_KEEPALIVE_TMO:
  447. case ISCSI_FLASHNODE_ISID:
  448. case ISCSI_FLASHNODE_TSID:
  449. case ISCSI_FLASHNODE_PORT:
  450. case ISCSI_FLASHNODE_MAX_BURST:
  451. case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
  452. case ISCSI_FLASHNODE_IPADDR:
  453. case ISCSI_FLASHNODE_ALIAS:
  454. case ISCSI_FLASHNODE_REDIRECT_IPADDR:
  455. case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
  456. case ISCSI_FLASHNODE_LOCAL_PORT:
  457. case ISCSI_FLASHNODE_IPV4_TOS:
  458. case ISCSI_FLASHNODE_IPV6_TC:
  459. case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
  460. case ISCSI_FLASHNODE_NAME:
  461. case ISCSI_FLASHNODE_TPGT:
  462. case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
  463. case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
  464. case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
  465. case ISCSI_FLASHNODE_TCP_XMIT_WSF:
  466. case ISCSI_FLASHNODE_TCP_RECV_WSF:
  467. case ISCSI_FLASHNODE_CHAP_OUT_IDX:
  468. case ISCSI_FLASHNODE_USERNAME:
  469. case ISCSI_FLASHNODE_PASSWORD:
  470. case ISCSI_FLASHNODE_STATSN:
  471. case ISCSI_FLASHNODE_EXP_STATSN:
  472. case ISCSI_FLASHNODE_IS_BOOT_TGT:
  473. return S_IRUGO;
  474. default:
  475. return 0;
  476. }
  477. }
  478. return 0;
  479. }
  480. static int qla4xxx_get_chap_by_index(struct scsi_qla_host *ha,
  481. int16_t chap_index,
  482. struct ql4_chap_table **chap_entry)
  483. {
  484. int rval = QLA_ERROR;
  485. int max_chap_entries;
  486. if (!ha->chap_list) {
  487. ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
  488. rval = QLA_ERROR;
  489. goto exit_get_chap;
  490. }
  491. if (is_qla80XX(ha))
  492. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  493. sizeof(struct ql4_chap_table);
  494. else
  495. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  496. if (chap_index > max_chap_entries) {
  497. ql4_printk(KERN_ERR, ha, "Invalid Chap index\n");
  498. rval = QLA_ERROR;
  499. goto exit_get_chap;
  500. }
  501. *chap_entry = (struct ql4_chap_table *)ha->chap_list + chap_index;
  502. if ((*chap_entry)->cookie !=
  503. __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
  504. rval = QLA_ERROR;
  505. *chap_entry = NULL;
  506. } else {
  507. rval = QLA_SUCCESS;
  508. }
  509. exit_get_chap:
  510. return rval;
  511. }
  512. /**
  513. * qla4xxx_find_free_chap_index - Find the first free chap index
  514. * @ha: pointer to adapter structure
  515. * @chap_index: CHAP index to be returned
  516. *
  517. * Find the first free chap index available in the chap table
  518. *
  519. * Note: Caller should acquire the chap lock before getting here.
  520. **/
  521. static int qla4xxx_find_free_chap_index(struct scsi_qla_host *ha,
  522. uint16_t *chap_index)
  523. {
  524. int i, rval;
  525. int free_index = -1;
  526. int max_chap_entries = 0;
  527. struct ql4_chap_table *chap_table;
  528. if (is_qla80XX(ha))
  529. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  530. sizeof(struct ql4_chap_table);
  531. else
  532. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  533. if (!ha->chap_list) {
  534. ql4_printk(KERN_ERR, ha, "CHAP table cache is empty!\n");
  535. rval = QLA_ERROR;
  536. goto exit_find_chap;
  537. }
  538. for (i = 0; i < max_chap_entries; i++) {
  539. chap_table = (struct ql4_chap_table *)ha->chap_list + i;
  540. if ((chap_table->cookie !=
  541. __constant_cpu_to_le16(CHAP_VALID_COOKIE)) &&
  542. (i > MAX_RESRV_CHAP_IDX)) {
  543. free_index = i;
  544. break;
  545. }
  546. }
  547. if (free_index != -1) {
  548. *chap_index = free_index;
  549. rval = QLA_SUCCESS;
  550. } else {
  551. rval = QLA_ERROR;
  552. }
  553. exit_find_chap:
  554. return rval;
  555. }
  556. static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
  557. uint32_t *num_entries, char *buf)
  558. {
  559. struct scsi_qla_host *ha = to_qla_host(shost);
  560. struct ql4_chap_table *chap_table;
  561. struct iscsi_chap_rec *chap_rec;
  562. int max_chap_entries = 0;
  563. int valid_chap_entries = 0;
  564. int ret = 0, i;
  565. if (is_qla80XX(ha))
  566. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  567. sizeof(struct ql4_chap_table);
  568. else
  569. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  570. ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
  571. __func__, *num_entries, chap_tbl_idx);
  572. if (!buf) {
  573. ret = -ENOMEM;
  574. goto exit_get_chap_list;
  575. }
  576. chap_rec = (struct iscsi_chap_rec *) buf;
  577. mutex_lock(&ha->chap_sem);
  578. for (i = chap_tbl_idx; i < max_chap_entries; i++) {
  579. chap_table = (struct ql4_chap_table *)ha->chap_list + i;
  580. if (chap_table->cookie !=
  581. __constant_cpu_to_le16(CHAP_VALID_COOKIE))
  582. continue;
  583. chap_rec->chap_tbl_idx = i;
  584. strncpy(chap_rec->username, chap_table->name,
  585. ISCSI_CHAP_AUTH_NAME_MAX_LEN);
  586. strncpy(chap_rec->password, chap_table->secret,
  587. QL4_CHAP_MAX_SECRET_LEN);
  588. chap_rec->password_length = chap_table->secret_len;
  589. if (chap_table->flags & BIT_7) /* local */
  590. chap_rec->chap_type = CHAP_TYPE_OUT;
  591. if (chap_table->flags & BIT_6) /* peer */
  592. chap_rec->chap_type = CHAP_TYPE_IN;
  593. chap_rec++;
  594. valid_chap_entries++;
  595. if (valid_chap_entries == *num_entries)
  596. break;
  597. else
  598. continue;
  599. }
  600. mutex_unlock(&ha->chap_sem);
  601. exit_get_chap_list:
  602. ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
  603. __func__, valid_chap_entries);
  604. *num_entries = valid_chap_entries;
  605. return ret;
  606. }
  607. static int __qla4xxx_is_chap_active(struct device *dev, void *data)
  608. {
  609. int ret = 0;
  610. uint16_t *chap_tbl_idx = (uint16_t *) data;
  611. struct iscsi_cls_session *cls_session;
  612. struct iscsi_session *sess;
  613. struct ddb_entry *ddb_entry;
  614. if (!iscsi_is_session_dev(dev))
  615. goto exit_is_chap_active;
  616. cls_session = iscsi_dev_to_session(dev);
  617. sess = cls_session->dd_data;
  618. ddb_entry = sess->dd_data;
  619. if (iscsi_session_chkready(cls_session))
  620. goto exit_is_chap_active;
  621. if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
  622. ret = 1;
  623. exit_is_chap_active:
  624. return ret;
  625. }
  626. static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
  627. uint16_t chap_tbl_idx)
  628. {
  629. int ret = 0;
  630. ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
  631. __qla4xxx_is_chap_active);
  632. return ret;
  633. }
  634. static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
  635. {
  636. struct scsi_qla_host *ha = to_qla_host(shost);
  637. struct ql4_chap_table *chap_table;
  638. dma_addr_t chap_dma;
  639. int max_chap_entries = 0;
  640. uint32_t offset = 0;
  641. uint32_t chap_size;
  642. int ret = 0;
  643. chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
  644. if (chap_table == NULL)
  645. return -ENOMEM;
  646. memset(chap_table, 0, sizeof(struct ql4_chap_table));
  647. if (is_qla80XX(ha))
  648. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  649. sizeof(struct ql4_chap_table);
  650. else
  651. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  652. if (chap_tbl_idx > max_chap_entries) {
  653. ret = -EINVAL;
  654. goto exit_delete_chap;
  655. }
  656. /* Check if chap index is in use.
  657. * If chap is in use don't delet chap entry */
  658. ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
  659. if (ret) {
  660. ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
  661. "delete from flash\n", chap_tbl_idx);
  662. ret = -EBUSY;
  663. goto exit_delete_chap;
  664. }
  665. chap_size = sizeof(struct ql4_chap_table);
  666. if (is_qla40XX(ha))
  667. offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
  668. else {
  669. offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
  670. /* flt_chap_size is CHAP table size for both ports
  671. * so divide it by 2 to calculate the offset for second port
  672. */
  673. if (ha->port_num == 1)
  674. offset += (ha->hw.flt_chap_size / 2);
  675. offset += (chap_tbl_idx * chap_size);
  676. }
  677. ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
  678. if (ret != QLA_SUCCESS) {
  679. ret = -EINVAL;
  680. goto exit_delete_chap;
  681. }
  682. DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
  683. __le16_to_cpu(chap_table->cookie)));
  684. if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
  685. ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
  686. goto exit_delete_chap;
  687. }
  688. chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
  689. offset = FLASH_CHAP_OFFSET |
  690. (chap_tbl_idx * sizeof(struct ql4_chap_table));
  691. ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
  692. FLASH_OPT_RMW_COMMIT);
  693. if (ret == QLA_SUCCESS && ha->chap_list) {
  694. mutex_lock(&ha->chap_sem);
  695. /* Update ha chap_list cache */
  696. memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
  697. chap_table, sizeof(struct ql4_chap_table));
  698. mutex_unlock(&ha->chap_sem);
  699. }
  700. if (ret != QLA_SUCCESS)
  701. ret = -EINVAL;
  702. exit_delete_chap:
  703. dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
  704. return ret;
  705. }
  706. /**
  707. * qla4xxx_set_chap_entry - Make chap entry with given information
  708. * @shost: pointer to host
  709. * @data: chap info - credentials, index and type to make chap entry
  710. * @len: length of data
  711. *
  712. * Add or update chap entry with the given information
  713. **/
  714. static int qla4xxx_set_chap_entry(struct Scsi_Host *shost, void *data, int len)
  715. {
  716. struct scsi_qla_host *ha = to_qla_host(shost);
  717. struct iscsi_chap_rec chap_rec;
  718. struct ql4_chap_table *chap_entry = NULL;
  719. struct iscsi_param_info *param_info;
  720. struct nlattr *attr;
  721. int max_chap_entries = 0;
  722. int type;
  723. int rem = len;
  724. int rc = 0;
  725. memset(&chap_rec, 0, sizeof(chap_rec));
  726. nla_for_each_attr(attr, data, len, rem) {
  727. param_info = nla_data(attr);
  728. switch (param_info->param) {
  729. case ISCSI_CHAP_PARAM_INDEX:
  730. chap_rec.chap_tbl_idx = *(uint16_t *)param_info->value;
  731. break;
  732. case ISCSI_CHAP_PARAM_CHAP_TYPE:
  733. chap_rec.chap_type = param_info->value[0];
  734. break;
  735. case ISCSI_CHAP_PARAM_USERNAME:
  736. memcpy(chap_rec.username, param_info->value,
  737. param_info->len);
  738. break;
  739. case ISCSI_CHAP_PARAM_PASSWORD:
  740. memcpy(chap_rec.password, param_info->value,
  741. param_info->len);
  742. break;
  743. case ISCSI_CHAP_PARAM_PASSWORD_LEN:
  744. chap_rec.password_length = param_info->value[0];
  745. break;
  746. default:
  747. ql4_printk(KERN_ERR, ha,
  748. "%s: No such sysfs attribute\n", __func__);
  749. rc = -ENOSYS;
  750. goto exit_set_chap;
  751. };
  752. }
  753. if (chap_rec.chap_type == CHAP_TYPE_IN)
  754. type = BIDI_CHAP;
  755. else
  756. type = LOCAL_CHAP;
  757. if (is_qla80XX(ha))
  758. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  759. sizeof(struct ql4_chap_table);
  760. else
  761. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  762. mutex_lock(&ha->chap_sem);
  763. if (chap_rec.chap_tbl_idx < max_chap_entries) {
  764. rc = qla4xxx_get_chap_by_index(ha, chap_rec.chap_tbl_idx,
  765. &chap_entry);
  766. if (!rc) {
  767. if (!(type == qla4xxx_get_chap_type(chap_entry))) {
  768. ql4_printk(KERN_INFO, ha,
  769. "Type mismatch for CHAP entry %d\n",
  770. chap_rec.chap_tbl_idx);
  771. rc = -EINVAL;
  772. goto exit_unlock_chap;
  773. }
  774. /* If chap index is in use then don't modify it */
  775. rc = qla4xxx_is_chap_active(shost,
  776. chap_rec.chap_tbl_idx);
  777. if (rc) {
  778. ql4_printk(KERN_INFO, ha,
  779. "CHAP entry %d is in use\n",
  780. chap_rec.chap_tbl_idx);
  781. rc = -EBUSY;
  782. goto exit_unlock_chap;
  783. }
  784. }
  785. } else {
  786. rc = qla4xxx_find_free_chap_index(ha, &chap_rec.chap_tbl_idx);
  787. if (rc) {
  788. ql4_printk(KERN_INFO, ha, "CHAP entry not available\n");
  789. rc = -EBUSY;
  790. goto exit_unlock_chap;
  791. }
  792. }
  793. rc = qla4xxx_set_chap(ha, chap_rec.username, chap_rec.password,
  794. chap_rec.chap_tbl_idx, type);
  795. exit_unlock_chap:
  796. mutex_unlock(&ha->chap_sem);
  797. exit_set_chap:
  798. return rc;
  799. }
  800. static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  801. enum iscsi_param_type param_type,
  802. int param, char *buf)
  803. {
  804. struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
  805. struct scsi_qla_host *ha = to_qla_host(shost);
  806. int len = -ENOSYS;
  807. if (param_type != ISCSI_NET_PARAM)
  808. return -ENOSYS;
  809. switch (param) {
  810. case ISCSI_NET_PARAM_IPV4_ADDR:
  811. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  812. break;
  813. case ISCSI_NET_PARAM_IPV4_SUBNET:
  814. len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
  815. break;
  816. case ISCSI_NET_PARAM_IPV4_GW:
  817. len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
  818. break;
  819. case ISCSI_NET_PARAM_IFACE_ENABLE:
  820. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  821. len = sprintf(buf, "%s\n",
  822. (ha->ip_config.ipv4_options &
  823. IPOPT_IPV4_PROTOCOL_ENABLE) ?
  824. "enabled" : "disabled");
  825. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  826. len = sprintf(buf, "%s\n",
  827. (ha->ip_config.ipv6_options &
  828. IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
  829. "enabled" : "disabled");
  830. break;
  831. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  832. len = sprintf(buf, "%s\n",
  833. (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
  834. "dhcp" : "static");
  835. break;
  836. case ISCSI_NET_PARAM_IPV6_ADDR:
  837. if (iface->iface_num == 0)
  838. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
  839. if (iface->iface_num == 1)
  840. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
  841. break;
  842. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  843. len = sprintf(buf, "%pI6\n",
  844. &ha->ip_config.ipv6_link_local_addr);
  845. break;
  846. case ISCSI_NET_PARAM_IPV6_ROUTER:
  847. len = sprintf(buf, "%pI6\n",
  848. &ha->ip_config.ipv6_default_router_addr);
  849. break;
  850. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  851. len = sprintf(buf, "%s\n",
  852. (ha->ip_config.ipv6_addl_options &
  853. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
  854. "nd" : "static");
  855. break;
  856. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  857. len = sprintf(buf, "%s\n",
  858. (ha->ip_config.ipv6_addl_options &
  859. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
  860. "auto" : "static");
  861. break;
  862. case ISCSI_NET_PARAM_VLAN_ID:
  863. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  864. len = sprintf(buf, "%d\n",
  865. (ha->ip_config.ipv4_vlan_tag &
  866. ISCSI_MAX_VLAN_ID));
  867. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  868. len = sprintf(buf, "%d\n",
  869. (ha->ip_config.ipv6_vlan_tag &
  870. ISCSI_MAX_VLAN_ID));
  871. break;
  872. case ISCSI_NET_PARAM_VLAN_PRIORITY:
  873. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  874. len = sprintf(buf, "%d\n",
  875. ((ha->ip_config.ipv4_vlan_tag >> 13) &
  876. ISCSI_MAX_VLAN_PRIORITY));
  877. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  878. len = sprintf(buf, "%d\n",
  879. ((ha->ip_config.ipv6_vlan_tag >> 13) &
  880. ISCSI_MAX_VLAN_PRIORITY));
  881. break;
  882. case ISCSI_NET_PARAM_VLAN_ENABLED:
  883. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  884. len = sprintf(buf, "%s\n",
  885. (ha->ip_config.ipv4_options &
  886. IPOPT_VLAN_TAGGING_ENABLE) ?
  887. "enabled" : "disabled");
  888. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  889. len = sprintf(buf, "%s\n",
  890. (ha->ip_config.ipv6_options &
  891. IPV6_OPT_VLAN_TAGGING_ENABLE) ?
  892. "enabled" : "disabled");
  893. break;
  894. case ISCSI_NET_PARAM_MTU:
  895. len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
  896. break;
  897. case ISCSI_NET_PARAM_PORT:
  898. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  899. len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
  900. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  901. len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
  902. break;
  903. default:
  904. len = -ENOSYS;
  905. }
  906. return len;
  907. }
  908. static struct iscsi_endpoint *
  909. qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  910. int non_blocking)
  911. {
  912. int ret;
  913. struct iscsi_endpoint *ep;
  914. struct qla_endpoint *qla_ep;
  915. struct scsi_qla_host *ha;
  916. struct sockaddr_in *addr;
  917. struct sockaddr_in6 *addr6;
  918. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  919. if (!shost) {
  920. ret = -ENXIO;
  921. printk(KERN_ERR "%s: shost is NULL\n",
  922. __func__);
  923. return ERR_PTR(ret);
  924. }
  925. ha = iscsi_host_priv(shost);
  926. ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
  927. if (!ep) {
  928. ret = -ENOMEM;
  929. return ERR_PTR(ret);
  930. }
  931. qla_ep = ep->dd_data;
  932. memset(qla_ep, 0, sizeof(struct qla_endpoint));
  933. if (dst_addr->sa_family == AF_INET) {
  934. memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
  935. addr = (struct sockaddr_in *)&qla_ep->dst_addr;
  936. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
  937. (char *)&addr->sin_addr));
  938. } else if (dst_addr->sa_family == AF_INET6) {
  939. memcpy(&qla_ep->dst_addr, dst_addr,
  940. sizeof(struct sockaddr_in6));
  941. addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
  942. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
  943. (char *)&addr6->sin6_addr));
  944. }
  945. qla_ep->host = shost;
  946. return ep;
  947. }
  948. static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  949. {
  950. struct qla_endpoint *qla_ep;
  951. struct scsi_qla_host *ha;
  952. int ret = 0;
  953. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  954. qla_ep = ep->dd_data;
  955. ha = to_qla_host(qla_ep->host);
  956. if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
  957. ret = 1;
  958. return ret;
  959. }
  960. static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
  961. {
  962. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  963. iscsi_destroy_endpoint(ep);
  964. }
  965. static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
  966. enum iscsi_param param,
  967. char *buf)
  968. {
  969. struct qla_endpoint *qla_ep = ep->dd_data;
  970. struct sockaddr *dst_addr;
  971. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  972. switch (param) {
  973. case ISCSI_PARAM_CONN_PORT:
  974. case ISCSI_PARAM_CONN_ADDRESS:
  975. if (!qla_ep)
  976. return -ENOTCONN;
  977. dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  978. if (!dst_addr)
  979. return -ENOTCONN;
  980. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  981. &qla_ep->dst_addr, param, buf);
  982. default:
  983. return -ENOSYS;
  984. }
  985. }
  986. static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  987. struct iscsi_stats *stats)
  988. {
  989. struct iscsi_session *sess;
  990. struct iscsi_cls_session *cls_sess;
  991. struct ddb_entry *ddb_entry;
  992. struct scsi_qla_host *ha;
  993. struct ql_iscsi_stats *ql_iscsi_stats;
  994. int stats_size;
  995. int ret;
  996. dma_addr_t iscsi_stats_dma;
  997. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  998. cls_sess = iscsi_conn_to_session(cls_conn);
  999. sess = cls_sess->dd_data;
  1000. ddb_entry = sess->dd_data;
  1001. ha = ddb_entry->ha;
  1002. stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
  1003. /* Allocate memory */
  1004. ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
  1005. &iscsi_stats_dma, GFP_KERNEL);
  1006. if (!ql_iscsi_stats) {
  1007. ql4_printk(KERN_ERR, ha,
  1008. "Unable to allocate memory for iscsi stats\n");
  1009. goto exit_get_stats;
  1010. }
  1011. ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
  1012. iscsi_stats_dma);
  1013. if (ret != QLA_SUCCESS) {
  1014. ql4_printk(KERN_ERR, ha,
  1015. "Unable to retrieve iscsi stats\n");
  1016. goto free_stats;
  1017. }
  1018. /* octets */
  1019. stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
  1020. stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
  1021. /* xmit pdus */
  1022. stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
  1023. stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
  1024. stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
  1025. stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
  1026. stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
  1027. stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
  1028. stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
  1029. stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
  1030. /* recv pdus */
  1031. stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
  1032. stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
  1033. stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
  1034. stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
  1035. stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
  1036. stats->logoutrsp_pdus =
  1037. le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
  1038. stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
  1039. stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
  1040. stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
  1041. free_stats:
  1042. dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
  1043. iscsi_stats_dma);
  1044. exit_get_stats:
  1045. return;
  1046. }
  1047. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  1048. {
  1049. struct iscsi_cls_session *session;
  1050. struct iscsi_session *sess;
  1051. unsigned long flags;
  1052. enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
  1053. session = starget_to_session(scsi_target(sc->device));
  1054. sess = session->dd_data;
  1055. spin_lock_irqsave(&session->lock, flags);
  1056. if (session->state == ISCSI_SESSION_FAILED)
  1057. ret = BLK_EH_RESET_TIMER;
  1058. spin_unlock_irqrestore(&session->lock, flags);
  1059. return ret;
  1060. }
  1061. static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
  1062. {
  1063. struct scsi_qla_host *ha = to_qla_host(shost);
  1064. struct iscsi_cls_host *ihost = shost->shost_data;
  1065. uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
  1066. qla4xxx_get_firmware_state(ha);
  1067. switch (ha->addl_fw_state & 0x0F00) {
  1068. case FW_ADDSTATE_LINK_SPEED_10MBPS:
  1069. speed = ISCSI_PORT_SPEED_10MBPS;
  1070. break;
  1071. case FW_ADDSTATE_LINK_SPEED_100MBPS:
  1072. speed = ISCSI_PORT_SPEED_100MBPS;
  1073. break;
  1074. case FW_ADDSTATE_LINK_SPEED_1GBPS:
  1075. speed = ISCSI_PORT_SPEED_1GBPS;
  1076. break;
  1077. case FW_ADDSTATE_LINK_SPEED_10GBPS:
  1078. speed = ISCSI_PORT_SPEED_10GBPS;
  1079. break;
  1080. }
  1081. ihost->port_speed = speed;
  1082. }
  1083. static void qla4xxx_set_port_state(struct Scsi_Host *shost)
  1084. {
  1085. struct scsi_qla_host *ha = to_qla_host(shost);
  1086. struct iscsi_cls_host *ihost = shost->shost_data;
  1087. uint32_t state = ISCSI_PORT_STATE_DOWN;
  1088. if (test_bit(AF_LINK_UP, &ha->flags))
  1089. state = ISCSI_PORT_STATE_UP;
  1090. ihost->port_state = state;
  1091. }
  1092. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  1093. enum iscsi_host_param param, char *buf)
  1094. {
  1095. struct scsi_qla_host *ha = to_qla_host(shost);
  1096. int len;
  1097. switch (param) {
  1098. case ISCSI_HOST_PARAM_HWADDRESS:
  1099. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  1100. break;
  1101. case ISCSI_HOST_PARAM_IPADDRESS:
  1102. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  1103. break;
  1104. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1105. len = sprintf(buf, "%s\n", ha->name_string);
  1106. break;
  1107. case ISCSI_HOST_PARAM_PORT_STATE:
  1108. qla4xxx_set_port_state(shost);
  1109. len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
  1110. break;
  1111. case ISCSI_HOST_PARAM_PORT_SPEED:
  1112. qla4xxx_set_port_speed(shost);
  1113. len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
  1114. break;
  1115. default:
  1116. return -ENOSYS;
  1117. }
  1118. return len;
  1119. }
  1120. static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
  1121. {
  1122. if (ha->iface_ipv4)
  1123. return;
  1124. /* IPv4 */
  1125. ha->iface_ipv4 = iscsi_create_iface(ha->host,
  1126. &qla4xxx_iscsi_transport,
  1127. ISCSI_IFACE_TYPE_IPV4, 0, 0);
  1128. if (!ha->iface_ipv4)
  1129. ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
  1130. "iface0.\n");
  1131. }
  1132. static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
  1133. {
  1134. if (!ha->iface_ipv6_0)
  1135. /* IPv6 iface-0 */
  1136. ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
  1137. &qla4xxx_iscsi_transport,
  1138. ISCSI_IFACE_TYPE_IPV6, 0,
  1139. 0);
  1140. if (!ha->iface_ipv6_0)
  1141. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  1142. "iface0.\n");
  1143. if (!ha->iface_ipv6_1)
  1144. /* IPv6 iface-1 */
  1145. ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
  1146. &qla4xxx_iscsi_transport,
  1147. ISCSI_IFACE_TYPE_IPV6, 1,
  1148. 0);
  1149. if (!ha->iface_ipv6_1)
  1150. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  1151. "iface1.\n");
  1152. }
  1153. static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
  1154. {
  1155. if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
  1156. qla4xxx_create_ipv4_iface(ha);
  1157. if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
  1158. qla4xxx_create_ipv6_iface(ha);
  1159. }
  1160. static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
  1161. {
  1162. if (ha->iface_ipv4) {
  1163. iscsi_destroy_iface(ha->iface_ipv4);
  1164. ha->iface_ipv4 = NULL;
  1165. }
  1166. }
  1167. static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
  1168. {
  1169. if (ha->iface_ipv6_0) {
  1170. iscsi_destroy_iface(ha->iface_ipv6_0);
  1171. ha->iface_ipv6_0 = NULL;
  1172. }
  1173. if (ha->iface_ipv6_1) {
  1174. iscsi_destroy_iface(ha->iface_ipv6_1);
  1175. ha->iface_ipv6_1 = NULL;
  1176. }
  1177. }
  1178. static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
  1179. {
  1180. qla4xxx_destroy_ipv4_iface(ha);
  1181. qla4xxx_destroy_ipv6_iface(ha);
  1182. }
  1183. static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
  1184. struct iscsi_iface_param_info *iface_param,
  1185. struct addr_ctrl_blk *init_fw_cb)
  1186. {
  1187. /*
  1188. * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
  1189. * iface_num 1 is valid only for IPv6 Addr.
  1190. */
  1191. switch (iface_param->param) {
  1192. case ISCSI_NET_PARAM_IPV6_ADDR:
  1193. if (iface_param->iface_num & 0x1)
  1194. /* IPv6 Addr 1 */
  1195. memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
  1196. sizeof(init_fw_cb->ipv6_addr1));
  1197. else
  1198. /* IPv6 Addr 0 */
  1199. memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
  1200. sizeof(init_fw_cb->ipv6_addr0));
  1201. break;
  1202. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  1203. if (iface_param->iface_num & 0x1)
  1204. break;
  1205. memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
  1206. sizeof(init_fw_cb->ipv6_if_id));
  1207. break;
  1208. case ISCSI_NET_PARAM_IPV6_ROUTER:
  1209. if (iface_param->iface_num & 0x1)
  1210. break;
  1211. memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
  1212. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  1213. break;
  1214. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  1215. /* Autocfg applies to even interface */
  1216. if (iface_param->iface_num & 0x1)
  1217. break;
  1218. if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
  1219. init_fw_cb->ipv6_addtl_opts &=
  1220. cpu_to_le16(
  1221. ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  1222. else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
  1223. init_fw_cb->ipv6_addtl_opts |=
  1224. cpu_to_le16(
  1225. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  1226. else
  1227. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  1228. "IPv6 addr\n");
  1229. break;
  1230. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  1231. /* Autocfg applies to even interface */
  1232. if (iface_param->iface_num & 0x1)
  1233. break;
  1234. if (iface_param->value[0] ==
  1235. ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
  1236. init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
  1237. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  1238. else if (iface_param->value[0] ==
  1239. ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
  1240. init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
  1241. ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  1242. else
  1243. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  1244. "IPv6 linklocal addr\n");
  1245. break;
  1246. case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
  1247. /* Autocfg applies to even interface */
  1248. if (iface_param->iface_num & 0x1)
  1249. break;
  1250. if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
  1251. memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
  1252. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  1253. break;
  1254. case ISCSI_NET_PARAM_IFACE_ENABLE:
  1255. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  1256. init_fw_cb->ipv6_opts |=
  1257. cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
  1258. qla4xxx_create_ipv6_iface(ha);
  1259. } else {
  1260. init_fw_cb->ipv6_opts &=
  1261. cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
  1262. 0xFFFF);
  1263. qla4xxx_destroy_ipv6_iface(ha);
  1264. }
  1265. break;
  1266. case ISCSI_NET_PARAM_VLAN_TAG:
  1267. if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
  1268. break;
  1269. init_fw_cb->ipv6_vlan_tag =
  1270. cpu_to_be16(*(uint16_t *)iface_param->value);
  1271. break;
  1272. case ISCSI_NET_PARAM_VLAN_ENABLED:
  1273. if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  1274. init_fw_cb->ipv6_opts |=
  1275. cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
  1276. else
  1277. init_fw_cb->ipv6_opts &=
  1278. cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
  1279. break;
  1280. case ISCSI_NET_PARAM_MTU:
  1281. init_fw_cb->eth_mtu_size =
  1282. cpu_to_le16(*(uint16_t *)iface_param->value);
  1283. break;
  1284. case ISCSI_NET_PARAM_PORT:
  1285. /* Autocfg applies to even interface */
  1286. if (iface_param->iface_num & 0x1)
  1287. break;
  1288. init_fw_cb->ipv6_port =
  1289. cpu_to_le16(*(uint16_t *)iface_param->value);
  1290. break;
  1291. default:
  1292. ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
  1293. iface_param->param);
  1294. break;
  1295. }
  1296. }
  1297. static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
  1298. struct iscsi_iface_param_info *iface_param,
  1299. struct addr_ctrl_blk *init_fw_cb)
  1300. {
  1301. switch (iface_param->param) {
  1302. case ISCSI_NET_PARAM_IPV4_ADDR:
  1303. memcpy(init_fw_cb->ipv4_addr, iface_param->value,
  1304. sizeof(init_fw_cb->ipv4_addr));
  1305. break;
  1306. case ISCSI_NET_PARAM_IPV4_SUBNET:
  1307. memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
  1308. sizeof(init_fw_cb->ipv4_subnet));
  1309. break;
  1310. case ISCSI_NET_PARAM_IPV4_GW:
  1311. memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
  1312. sizeof(init_fw_cb->ipv4_gw_addr));
  1313. break;
  1314. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  1315. if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
  1316. init_fw_cb->ipv4_tcp_opts |=
  1317. cpu_to_le16(TCPOPT_DHCP_ENABLE);
  1318. else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
  1319. init_fw_cb->ipv4_tcp_opts &=
  1320. cpu_to_le16(~TCPOPT_DHCP_ENABLE);
  1321. else
  1322. ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
  1323. break;
  1324. case ISCSI_NET_PARAM_IFACE_ENABLE:
  1325. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  1326. init_fw_cb->ipv4_ip_opts |=
  1327. cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
  1328. qla4xxx_create_ipv4_iface(ha);
  1329. } else {
  1330. init_fw_cb->ipv4_ip_opts &=
  1331. cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
  1332. 0xFFFF);
  1333. qla4xxx_destroy_ipv4_iface(ha);
  1334. }
  1335. break;
  1336. case ISCSI_NET_PARAM_VLAN_TAG:
  1337. if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
  1338. break;
  1339. init_fw_cb->ipv4_vlan_tag =
  1340. cpu_to_be16(*(uint16_t *)iface_param->value);
  1341. break;
  1342. case ISCSI_NET_PARAM_VLAN_ENABLED:
  1343. if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  1344. init_fw_cb->ipv4_ip_opts |=
  1345. cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
  1346. else
  1347. init_fw_cb->ipv4_ip_opts &=
  1348. cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
  1349. break;
  1350. case ISCSI_NET_PARAM_MTU:
  1351. init_fw_cb->eth_mtu_size =
  1352. cpu_to_le16(*(uint16_t *)iface_param->value);
  1353. break;
  1354. case ISCSI_NET_PARAM_PORT:
  1355. init_fw_cb->ipv4_port =
  1356. cpu_to_le16(*(uint16_t *)iface_param->value);
  1357. break;
  1358. default:
  1359. ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
  1360. iface_param->param);
  1361. break;
  1362. }
  1363. }
  1364. static void
  1365. qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
  1366. {
  1367. struct addr_ctrl_blk_def *acb;
  1368. acb = (struct addr_ctrl_blk_def *)init_fw_cb;
  1369. memset(acb->reserved1, 0, sizeof(acb->reserved1));
  1370. memset(acb->reserved2, 0, sizeof(acb->reserved2));
  1371. memset(acb->reserved3, 0, sizeof(acb->reserved3));
  1372. memset(acb->reserved4, 0, sizeof(acb->reserved4));
  1373. memset(acb->reserved5, 0, sizeof(acb->reserved5));
  1374. memset(acb->reserved6, 0, sizeof(acb->reserved6));
  1375. memset(acb->reserved7, 0, sizeof(acb->reserved7));
  1376. memset(acb->reserved8, 0, sizeof(acb->reserved8));
  1377. memset(acb->reserved9, 0, sizeof(acb->reserved9));
  1378. memset(acb->reserved10, 0, sizeof(acb->reserved10));
  1379. memset(acb->reserved11, 0, sizeof(acb->reserved11));
  1380. memset(acb->reserved12, 0, sizeof(acb->reserved12));
  1381. memset(acb->reserved13, 0, sizeof(acb->reserved13));
  1382. memset(acb->reserved14, 0, sizeof(acb->reserved14));
  1383. memset(acb->reserved15, 0, sizeof(acb->reserved15));
  1384. }
  1385. static int
  1386. qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
  1387. {
  1388. struct scsi_qla_host *ha = to_qla_host(shost);
  1389. int rval = 0;
  1390. struct iscsi_iface_param_info *iface_param = NULL;
  1391. struct addr_ctrl_blk *init_fw_cb = NULL;
  1392. dma_addr_t init_fw_cb_dma;
  1393. uint32_t mbox_cmd[MBOX_REG_COUNT];
  1394. uint32_t mbox_sts[MBOX_REG_COUNT];
  1395. uint32_t rem = len;
  1396. struct nlattr *attr;
  1397. init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
  1398. sizeof(struct addr_ctrl_blk),
  1399. &init_fw_cb_dma, GFP_KERNEL);
  1400. if (!init_fw_cb) {
  1401. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
  1402. __func__);
  1403. return -ENOMEM;
  1404. }
  1405. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  1406. memset(&mbox_cmd, 0, sizeof(mbox_cmd));
  1407. memset(&mbox_sts, 0, sizeof(mbox_sts));
  1408. if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
  1409. ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
  1410. rval = -EIO;
  1411. goto exit_init_fw_cb;
  1412. }
  1413. nla_for_each_attr(attr, data, len, rem) {
  1414. iface_param = nla_data(attr);
  1415. if (iface_param->param_type != ISCSI_NET_PARAM)
  1416. continue;
  1417. switch (iface_param->iface_type) {
  1418. case ISCSI_IFACE_TYPE_IPV4:
  1419. switch (iface_param->iface_num) {
  1420. case 0:
  1421. qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
  1422. break;
  1423. default:
  1424. /* Cannot have more than one IPv4 interface */
  1425. ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
  1426. "number = %d\n",
  1427. iface_param->iface_num);
  1428. break;
  1429. }
  1430. break;
  1431. case ISCSI_IFACE_TYPE_IPV6:
  1432. switch (iface_param->iface_num) {
  1433. case 0:
  1434. case 1:
  1435. qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
  1436. break;
  1437. default:
  1438. /* Cannot have more than two IPv6 interface */
  1439. ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
  1440. "number = %d\n",
  1441. iface_param->iface_num);
  1442. break;
  1443. }
  1444. break;
  1445. default:
  1446. ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
  1447. break;
  1448. }
  1449. }
  1450. init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
  1451. rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
  1452. sizeof(struct addr_ctrl_blk),
  1453. FLASH_OPT_RMW_COMMIT);
  1454. if (rval != QLA_SUCCESS) {
  1455. ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
  1456. __func__);
  1457. rval = -EIO;
  1458. goto exit_init_fw_cb;
  1459. }
  1460. rval = qla4xxx_disable_acb(ha);
  1461. if (rval != QLA_SUCCESS) {
  1462. ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
  1463. __func__);
  1464. rval = -EIO;
  1465. goto exit_init_fw_cb;
  1466. }
  1467. wait_for_completion_timeout(&ha->disable_acb_comp,
  1468. DISABLE_ACB_TOV * HZ);
  1469. qla4xxx_initcb_to_acb(init_fw_cb);
  1470. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
  1471. if (rval != QLA_SUCCESS) {
  1472. ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
  1473. __func__);
  1474. rval = -EIO;
  1475. goto exit_init_fw_cb;
  1476. }
  1477. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  1478. qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
  1479. init_fw_cb_dma);
  1480. exit_init_fw_cb:
  1481. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
  1482. init_fw_cb, init_fw_cb_dma);
  1483. return rval;
  1484. }
  1485. static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
  1486. enum iscsi_param param, char *buf)
  1487. {
  1488. struct iscsi_session *sess = cls_sess->dd_data;
  1489. struct ddb_entry *ddb_entry = sess->dd_data;
  1490. struct scsi_qla_host *ha = ddb_entry->ha;
  1491. int rval, len;
  1492. uint16_t idx;
  1493. switch (param) {
  1494. case ISCSI_PARAM_CHAP_IN_IDX:
  1495. rval = qla4xxx_get_chap_index(ha, sess->username_in,
  1496. sess->password_in, BIDI_CHAP,
  1497. &idx);
  1498. if (rval)
  1499. len = sprintf(buf, "\n");
  1500. else
  1501. len = sprintf(buf, "%hu\n", idx);
  1502. break;
  1503. case ISCSI_PARAM_CHAP_OUT_IDX:
  1504. rval = qla4xxx_get_chap_index(ha, sess->username,
  1505. sess->password, LOCAL_CHAP,
  1506. &idx);
  1507. if (rval)
  1508. len = sprintf(buf, "\n");
  1509. else
  1510. len = sprintf(buf, "%hu\n", idx);
  1511. break;
  1512. default:
  1513. return iscsi_session_get_param(cls_sess, param, buf);
  1514. }
  1515. return len;
  1516. }
  1517. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1518. enum iscsi_param param, char *buf)
  1519. {
  1520. struct iscsi_conn *conn;
  1521. struct qla_conn *qla_conn;
  1522. struct sockaddr *dst_addr;
  1523. int len = 0;
  1524. conn = cls_conn->dd_data;
  1525. qla_conn = conn->dd_data;
  1526. dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
  1527. switch (param) {
  1528. case ISCSI_PARAM_CONN_PORT:
  1529. case ISCSI_PARAM_CONN_ADDRESS:
  1530. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  1531. dst_addr, param, buf);
  1532. default:
  1533. return iscsi_conn_get_param(cls_conn, param, buf);
  1534. }
  1535. return len;
  1536. }
  1537. int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
  1538. {
  1539. uint32_t mbx_sts = 0;
  1540. uint16_t tmp_ddb_index;
  1541. int ret;
  1542. get_ddb_index:
  1543. tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
  1544. if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
  1545. DEBUG2(ql4_printk(KERN_INFO, ha,
  1546. "Free DDB index not available\n"));
  1547. ret = QLA_ERROR;
  1548. goto exit_get_ddb_index;
  1549. }
  1550. if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
  1551. goto get_ddb_index;
  1552. DEBUG2(ql4_printk(KERN_INFO, ha,
  1553. "Found a free DDB index at %d\n", tmp_ddb_index));
  1554. ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
  1555. if (ret == QLA_ERROR) {
  1556. if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
  1557. ql4_printk(KERN_INFO, ha,
  1558. "DDB index = %d not available trying next\n",
  1559. tmp_ddb_index);
  1560. goto get_ddb_index;
  1561. }
  1562. DEBUG2(ql4_printk(KERN_INFO, ha,
  1563. "Free FW DDB not available\n"));
  1564. }
  1565. *ddb_index = tmp_ddb_index;
  1566. exit_get_ddb_index:
  1567. return ret;
  1568. }
  1569. static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
  1570. struct ddb_entry *ddb_entry,
  1571. char *existing_ipaddr,
  1572. char *user_ipaddr)
  1573. {
  1574. uint8_t dst_ipaddr[IPv6_ADDR_LEN];
  1575. char formatted_ipaddr[DDB_IPADDR_LEN];
  1576. int status = QLA_SUCCESS, ret = 0;
  1577. if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
  1578. ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
  1579. '\0', NULL);
  1580. if (ret == 0) {
  1581. status = QLA_ERROR;
  1582. goto out_match;
  1583. }
  1584. ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
  1585. } else {
  1586. ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
  1587. '\0', NULL);
  1588. if (ret == 0) {
  1589. status = QLA_ERROR;
  1590. goto out_match;
  1591. }
  1592. ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
  1593. }
  1594. if (strcmp(existing_ipaddr, formatted_ipaddr))
  1595. status = QLA_ERROR;
  1596. out_match:
  1597. return status;
  1598. }
  1599. static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
  1600. struct iscsi_cls_conn *cls_conn)
  1601. {
  1602. int idx = 0, max_ddbs, rval;
  1603. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1604. struct iscsi_session *sess, *existing_sess;
  1605. struct iscsi_conn *conn, *existing_conn;
  1606. struct ddb_entry *ddb_entry;
  1607. sess = cls_sess->dd_data;
  1608. conn = cls_conn->dd_data;
  1609. if (sess->targetname == NULL ||
  1610. conn->persistent_address == NULL ||
  1611. conn->persistent_port == 0)
  1612. return QLA_ERROR;
  1613. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  1614. MAX_DEV_DB_ENTRIES;
  1615. for (idx = 0; idx < max_ddbs; idx++) {
  1616. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  1617. if (ddb_entry == NULL)
  1618. continue;
  1619. if (ddb_entry->ddb_type != FLASH_DDB)
  1620. continue;
  1621. existing_sess = ddb_entry->sess->dd_data;
  1622. existing_conn = ddb_entry->conn->dd_data;
  1623. if (existing_sess->targetname == NULL ||
  1624. existing_conn->persistent_address == NULL ||
  1625. existing_conn->persistent_port == 0)
  1626. continue;
  1627. DEBUG2(ql4_printk(KERN_INFO, ha,
  1628. "IQN = %s User IQN = %s\n",
  1629. existing_sess->targetname,
  1630. sess->targetname));
  1631. DEBUG2(ql4_printk(KERN_INFO, ha,
  1632. "IP = %s User IP = %s\n",
  1633. existing_conn->persistent_address,
  1634. conn->persistent_address));
  1635. DEBUG2(ql4_printk(KERN_INFO, ha,
  1636. "Port = %d User Port = %d\n",
  1637. existing_conn->persistent_port,
  1638. conn->persistent_port));
  1639. if (strcmp(existing_sess->targetname, sess->targetname))
  1640. continue;
  1641. rval = qla4xxx_match_ipaddress(ha, ddb_entry,
  1642. existing_conn->persistent_address,
  1643. conn->persistent_address);
  1644. if (rval == QLA_ERROR)
  1645. continue;
  1646. if (existing_conn->persistent_port != conn->persistent_port)
  1647. continue;
  1648. break;
  1649. }
  1650. if (idx == max_ddbs)
  1651. return QLA_ERROR;
  1652. DEBUG2(ql4_printk(KERN_INFO, ha,
  1653. "Match found in fwdb sessions\n"));
  1654. return QLA_SUCCESS;
  1655. }
  1656. static struct iscsi_cls_session *
  1657. qla4xxx_session_create(struct iscsi_endpoint *ep,
  1658. uint16_t cmds_max, uint16_t qdepth,
  1659. uint32_t initial_cmdsn)
  1660. {
  1661. struct iscsi_cls_session *cls_sess;
  1662. struct scsi_qla_host *ha;
  1663. struct qla_endpoint *qla_ep;
  1664. struct ddb_entry *ddb_entry;
  1665. uint16_t ddb_index;
  1666. struct iscsi_session *sess;
  1667. struct sockaddr *dst_addr;
  1668. int ret;
  1669. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1670. if (!ep) {
  1671. printk(KERN_ERR "qla4xxx: missing ep.\n");
  1672. return NULL;
  1673. }
  1674. qla_ep = ep->dd_data;
  1675. dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  1676. ha = to_qla_host(qla_ep->host);
  1677. ret = qla4xxx_get_ddb_index(ha, &ddb_index);
  1678. if (ret == QLA_ERROR)
  1679. return NULL;
  1680. cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
  1681. cmds_max, sizeof(struct ddb_entry),
  1682. sizeof(struct ql4_task_data),
  1683. initial_cmdsn, ddb_index);
  1684. if (!cls_sess)
  1685. return NULL;
  1686. sess = cls_sess->dd_data;
  1687. ddb_entry = sess->dd_data;
  1688. ddb_entry->fw_ddb_index = ddb_index;
  1689. ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  1690. ddb_entry->ha = ha;
  1691. ddb_entry->sess = cls_sess;
  1692. ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
  1693. ddb_entry->ddb_change = qla4xxx_ddb_change;
  1694. cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  1695. ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
  1696. ha->tot_ddbs++;
  1697. return cls_sess;
  1698. }
  1699. static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
  1700. {
  1701. struct iscsi_session *sess;
  1702. struct ddb_entry *ddb_entry;
  1703. struct scsi_qla_host *ha;
  1704. unsigned long flags, wtime;
  1705. struct dev_db_entry *fw_ddb_entry = NULL;
  1706. dma_addr_t fw_ddb_entry_dma;
  1707. uint32_t ddb_state;
  1708. int ret;
  1709. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1710. sess = cls_sess->dd_data;
  1711. ddb_entry = sess->dd_data;
  1712. ha = ddb_entry->ha;
  1713. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1714. &fw_ddb_entry_dma, GFP_KERNEL);
  1715. if (!fw_ddb_entry) {
  1716. ql4_printk(KERN_ERR, ha,
  1717. "%s: Unable to allocate dma buffer\n", __func__);
  1718. goto destroy_session;
  1719. }
  1720. wtime = jiffies + (HZ * LOGOUT_TOV);
  1721. do {
  1722. ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
  1723. fw_ddb_entry, fw_ddb_entry_dma,
  1724. NULL, NULL, &ddb_state, NULL,
  1725. NULL, NULL);
  1726. if (ret == QLA_ERROR)
  1727. goto destroy_session;
  1728. if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
  1729. (ddb_state == DDB_DS_SESSION_FAILED))
  1730. goto destroy_session;
  1731. schedule_timeout_uninterruptible(HZ);
  1732. } while ((time_after(wtime, jiffies)));
  1733. destroy_session:
  1734. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  1735. spin_lock_irqsave(&ha->hardware_lock, flags);
  1736. qla4xxx_free_ddb(ha, ddb_entry);
  1737. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1738. iscsi_session_teardown(cls_sess);
  1739. if (fw_ddb_entry)
  1740. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1741. fw_ddb_entry, fw_ddb_entry_dma);
  1742. }
  1743. static struct iscsi_cls_conn *
  1744. qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
  1745. {
  1746. struct iscsi_cls_conn *cls_conn;
  1747. struct iscsi_session *sess;
  1748. struct ddb_entry *ddb_entry;
  1749. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1750. cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
  1751. conn_idx);
  1752. if (!cls_conn)
  1753. return NULL;
  1754. sess = cls_sess->dd_data;
  1755. ddb_entry = sess->dd_data;
  1756. ddb_entry->conn = cls_conn;
  1757. return cls_conn;
  1758. }
  1759. static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  1760. struct iscsi_cls_conn *cls_conn,
  1761. uint64_t transport_fd, int is_leading)
  1762. {
  1763. struct iscsi_conn *conn;
  1764. struct qla_conn *qla_conn;
  1765. struct iscsi_endpoint *ep;
  1766. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1767. if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  1768. return -EINVAL;
  1769. ep = iscsi_lookup_endpoint(transport_fd);
  1770. conn = cls_conn->dd_data;
  1771. qla_conn = conn->dd_data;
  1772. qla_conn->qla_ep = ep->dd_data;
  1773. return 0;
  1774. }
  1775. static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
  1776. {
  1777. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1778. struct iscsi_session *sess;
  1779. struct ddb_entry *ddb_entry;
  1780. struct scsi_qla_host *ha;
  1781. struct dev_db_entry *fw_ddb_entry = NULL;
  1782. dma_addr_t fw_ddb_entry_dma;
  1783. uint32_t mbx_sts = 0;
  1784. int ret = 0;
  1785. int status = QLA_SUCCESS;
  1786. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1787. sess = cls_sess->dd_data;
  1788. ddb_entry = sess->dd_data;
  1789. ha = ddb_entry->ha;
  1790. /* Check if we have matching FW DDB, if yes then do not
  1791. * login to this target. This could cause target to logout previous
  1792. * connection
  1793. */
  1794. ret = qla4xxx_match_fwdb_session(ha, cls_conn);
  1795. if (ret == QLA_SUCCESS) {
  1796. ql4_printk(KERN_INFO, ha,
  1797. "Session already exist in FW.\n");
  1798. ret = -EEXIST;
  1799. goto exit_conn_start;
  1800. }
  1801. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1802. &fw_ddb_entry_dma, GFP_KERNEL);
  1803. if (!fw_ddb_entry) {
  1804. ql4_printk(KERN_ERR, ha,
  1805. "%s: Unable to allocate dma buffer\n", __func__);
  1806. ret = -ENOMEM;
  1807. goto exit_conn_start;
  1808. }
  1809. ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
  1810. if (ret) {
  1811. /* If iscsid is stopped and started then no need to do
  1812. * set param again since ddb state will be already
  1813. * active and FW does not allow set ddb to an
  1814. * active session.
  1815. */
  1816. if (mbx_sts)
  1817. if (ddb_entry->fw_ddb_device_state ==
  1818. DDB_DS_SESSION_ACTIVE) {
  1819. ddb_entry->unblock_sess(ddb_entry->sess);
  1820. goto exit_set_param;
  1821. }
  1822. ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
  1823. __func__, ddb_entry->fw_ddb_index);
  1824. goto exit_conn_start;
  1825. }
  1826. status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
  1827. if (status == QLA_ERROR) {
  1828. ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
  1829. sess->targetname);
  1830. ret = -EINVAL;
  1831. goto exit_conn_start;
  1832. }
  1833. if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
  1834. ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
  1835. DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
  1836. ddb_entry->fw_ddb_device_state));
  1837. exit_set_param:
  1838. ret = 0;
  1839. exit_conn_start:
  1840. if (fw_ddb_entry)
  1841. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1842. fw_ddb_entry, fw_ddb_entry_dma);
  1843. return ret;
  1844. }
  1845. static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1846. {
  1847. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1848. struct iscsi_session *sess;
  1849. struct scsi_qla_host *ha;
  1850. struct ddb_entry *ddb_entry;
  1851. int options;
  1852. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1853. sess = cls_sess->dd_data;
  1854. ddb_entry = sess->dd_data;
  1855. ha = ddb_entry->ha;
  1856. options = LOGOUT_OPTION_CLOSE_SESSION;
  1857. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
  1858. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
  1859. }
  1860. static void qla4xxx_task_work(struct work_struct *wdata)
  1861. {
  1862. struct ql4_task_data *task_data;
  1863. struct scsi_qla_host *ha;
  1864. struct passthru_status *sts;
  1865. struct iscsi_task *task;
  1866. struct iscsi_hdr *hdr;
  1867. uint8_t *data;
  1868. uint32_t data_len;
  1869. struct iscsi_conn *conn;
  1870. int hdr_len;
  1871. itt_t itt;
  1872. task_data = container_of(wdata, struct ql4_task_data, task_work);
  1873. ha = task_data->ha;
  1874. task = task_data->task;
  1875. sts = &task_data->sts;
  1876. hdr_len = sizeof(struct iscsi_hdr);
  1877. DEBUG3(printk(KERN_INFO "Status returned\n"));
  1878. DEBUG3(qla4xxx_dump_buffer(sts, 64));
  1879. DEBUG3(printk(KERN_INFO "Response buffer"));
  1880. DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
  1881. conn = task->conn;
  1882. switch (sts->completionStatus) {
  1883. case PASSTHRU_STATUS_COMPLETE:
  1884. hdr = (struct iscsi_hdr *)task_data->resp_buffer;
  1885. /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
  1886. itt = sts->handle;
  1887. hdr->itt = itt;
  1888. data = task_data->resp_buffer + hdr_len;
  1889. data_len = task_data->resp_len - hdr_len;
  1890. iscsi_complete_pdu(conn, hdr, data, data_len);
  1891. break;
  1892. default:
  1893. ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
  1894. sts->completionStatus);
  1895. break;
  1896. }
  1897. return;
  1898. }
  1899. static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  1900. {
  1901. struct ql4_task_data *task_data;
  1902. struct iscsi_session *sess;
  1903. struct ddb_entry *ddb_entry;
  1904. struct scsi_qla_host *ha;
  1905. int hdr_len;
  1906. sess = task->conn->session;
  1907. ddb_entry = sess->dd_data;
  1908. ha = ddb_entry->ha;
  1909. task_data = task->dd_data;
  1910. memset(task_data, 0, sizeof(struct ql4_task_data));
  1911. if (task->sc) {
  1912. ql4_printk(KERN_INFO, ha,
  1913. "%s: SCSI Commands not implemented\n", __func__);
  1914. return -EINVAL;
  1915. }
  1916. hdr_len = sizeof(struct iscsi_hdr);
  1917. task_data->ha = ha;
  1918. task_data->task = task;
  1919. if (task->data_count) {
  1920. task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
  1921. task->data_count,
  1922. PCI_DMA_TODEVICE);
  1923. }
  1924. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1925. __func__, task->conn->max_recv_dlength, hdr_len));
  1926. task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
  1927. task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1928. task_data->resp_len,
  1929. &task_data->resp_dma,
  1930. GFP_ATOMIC);
  1931. if (!task_data->resp_buffer)
  1932. goto exit_alloc_pdu;
  1933. task_data->req_len = task->data_count + hdr_len;
  1934. task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1935. task_data->req_len,
  1936. &task_data->req_dma,
  1937. GFP_ATOMIC);
  1938. if (!task_data->req_buffer)
  1939. goto exit_alloc_pdu;
  1940. task->hdr = task_data->req_buffer;
  1941. INIT_WORK(&task_data->task_work, qla4xxx_task_work);
  1942. return 0;
  1943. exit_alloc_pdu:
  1944. if (task_data->resp_buffer)
  1945. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1946. task_data->resp_buffer, task_data->resp_dma);
  1947. if (task_data->req_buffer)
  1948. dma_free_coherent(&ha->pdev->dev, task_data->req_len,
  1949. task_data->req_buffer, task_data->req_dma);
  1950. return -ENOMEM;
  1951. }
  1952. static void qla4xxx_task_cleanup(struct iscsi_task *task)
  1953. {
  1954. struct ql4_task_data *task_data;
  1955. struct iscsi_session *sess;
  1956. struct ddb_entry *ddb_entry;
  1957. struct scsi_qla_host *ha;
  1958. int hdr_len;
  1959. hdr_len = sizeof(struct iscsi_hdr);
  1960. sess = task->conn->session;
  1961. ddb_entry = sess->dd_data;
  1962. ha = ddb_entry->ha;
  1963. task_data = task->dd_data;
  1964. if (task->data_count) {
  1965. dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
  1966. task->data_count, PCI_DMA_TODEVICE);
  1967. }
  1968. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1969. __func__, task->conn->max_recv_dlength, hdr_len));
  1970. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1971. task_data->resp_buffer, task_data->resp_dma);
  1972. dma_free_coherent(&ha->pdev->dev, task_data->req_len,
  1973. task_data->req_buffer, task_data->req_dma);
  1974. return;
  1975. }
  1976. static int qla4xxx_task_xmit(struct iscsi_task *task)
  1977. {
  1978. struct scsi_cmnd *sc = task->sc;
  1979. struct iscsi_session *sess = task->conn->session;
  1980. struct ddb_entry *ddb_entry = sess->dd_data;
  1981. struct scsi_qla_host *ha = ddb_entry->ha;
  1982. if (!sc)
  1983. return qla4xxx_send_passthru0(task);
  1984. ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
  1985. __func__);
  1986. return -ENOSYS;
  1987. }
  1988. static int qla4xxx_copy_from_fwddb_param(struct iscsi_bus_flash_session *sess,
  1989. struct iscsi_bus_flash_conn *conn,
  1990. struct dev_db_entry *fw_ddb_entry)
  1991. {
  1992. unsigned long options = 0;
  1993. int rc = 0;
  1994. options = le16_to_cpu(fw_ddb_entry->options);
  1995. conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
  1996. if (test_bit(OPT_IPV6_DEVICE, &options)) {
  1997. rc = iscsi_switch_str_param(&sess->portal_type,
  1998. PORTAL_TYPE_IPV6);
  1999. if (rc)
  2000. goto exit_copy;
  2001. } else {
  2002. rc = iscsi_switch_str_param(&sess->portal_type,
  2003. PORTAL_TYPE_IPV4);
  2004. if (rc)
  2005. goto exit_copy;
  2006. }
  2007. sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
  2008. &options);
  2009. sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
  2010. sess->entry_state = test_bit(OPT_ENTRY_STATE, &options);
  2011. options = le16_to_cpu(fw_ddb_entry->iscsi_options);
  2012. conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
  2013. conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
  2014. sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
  2015. sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
  2016. sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
  2017. &options);
  2018. sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
  2019. sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
  2020. conn->snack_req_en = test_bit(ISCSIOPT_SNACK_REQ_EN, &options);
  2021. sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
  2022. &options);
  2023. sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
  2024. sess->discovery_auth_optional =
  2025. test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
  2026. if (test_bit(ISCSIOPT_ERL1, &options))
  2027. sess->erl |= BIT_1;
  2028. if (test_bit(ISCSIOPT_ERL0, &options))
  2029. sess->erl |= BIT_0;
  2030. options = le16_to_cpu(fw_ddb_entry->tcp_options);
  2031. conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
  2032. conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
  2033. conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
  2034. if (test_bit(TCPOPT_TIMER_SCALE3, &options))
  2035. conn->tcp_timer_scale |= BIT_3;
  2036. if (test_bit(TCPOPT_TIMER_SCALE2, &options))
  2037. conn->tcp_timer_scale |= BIT_2;
  2038. if (test_bit(TCPOPT_TIMER_SCALE1, &options))
  2039. conn->tcp_timer_scale |= BIT_1;
  2040. conn->tcp_timer_scale >>= 1;
  2041. conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
  2042. options = le16_to_cpu(fw_ddb_entry->ip_options);
  2043. conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
  2044. conn->max_recv_dlength = BYTE_UNITS *
  2045. le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  2046. conn->max_xmit_dlength = BYTE_UNITS *
  2047. le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  2048. sess->first_burst = BYTE_UNITS *
  2049. le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  2050. sess->max_burst = BYTE_UNITS *
  2051. le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  2052. sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  2053. sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  2054. sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  2055. sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  2056. conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
  2057. conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
  2058. conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
  2059. conn->ipv6_flow_label = le16_to_cpu(fw_ddb_entry->ipv6_flow_lbl);
  2060. conn->keepalive_timeout = le16_to_cpu(fw_ddb_entry->ka_timeout);
  2061. conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
  2062. conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
  2063. conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
  2064. sess->discovery_parent_idx = le16_to_cpu(fw_ddb_entry->ddb_link);
  2065. sess->discovery_parent_type = le16_to_cpu(fw_ddb_entry->ddb_link);
  2066. sess->chap_out_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  2067. sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
  2068. sess->default_taskmgmt_timeout =
  2069. le16_to_cpu(fw_ddb_entry->def_timeout);
  2070. conn->port = le16_to_cpu(fw_ddb_entry->port);
  2071. options = le16_to_cpu(fw_ddb_entry->options);
  2072. conn->ipaddress = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
  2073. if (!conn->ipaddress) {
  2074. rc = -ENOMEM;
  2075. goto exit_copy;
  2076. }
  2077. conn->redirect_ipaddr = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
  2078. if (!conn->redirect_ipaddr) {
  2079. rc = -ENOMEM;
  2080. goto exit_copy;
  2081. }
  2082. memcpy(conn->ipaddress, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
  2083. memcpy(conn->redirect_ipaddr, fw_ddb_entry->tgt_addr, IPv6_ADDR_LEN);
  2084. if (test_bit(OPT_IPV6_DEVICE, &options)) {
  2085. conn->ipv6_traffic_class = fw_ddb_entry->ipv4_tos;
  2086. conn->link_local_ipv6_addr = kzalloc(IPv6_ADDR_LEN, GFP_KERNEL);
  2087. if (!conn->link_local_ipv6_addr) {
  2088. rc = -ENOMEM;
  2089. goto exit_copy;
  2090. }
  2091. memcpy(conn->link_local_ipv6_addr,
  2092. fw_ddb_entry->link_local_ipv6_addr, IPv6_ADDR_LEN);
  2093. } else {
  2094. conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
  2095. }
  2096. if (fw_ddb_entry->iscsi_name[0]) {
  2097. rc = iscsi_switch_str_param(&sess->targetname,
  2098. (char *)fw_ddb_entry->iscsi_name);
  2099. if (rc)
  2100. goto exit_copy;
  2101. }
  2102. if (fw_ddb_entry->iscsi_alias[0]) {
  2103. rc = iscsi_switch_str_param(&sess->targetalias,
  2104. (char *)fw_ddb_entry->iscsi_alias);
  2105. if (rc)
  2106. goto exit_copy;
  2107. }
  2108. COPY_ISID(sess->isid, fw_ddb_entry->isid);
  2109. exit_copy:
  2110. return rc;
  2111. }
  2112. static int qla4xxx_copy_to_fwddb_param(struct iscsi_bus_flash_session *sess,
  2113. struct iscsi_bus_flash_conn *conn,
  2114. struct dev_db_entry *fw_ddb_entry)
  2115. {
  2116. uint16_t options;
  2117. int rc = 0;
  2118. options = le16_to_cpu(fw_ddb_entry->options);
  2119. SET_BITVAL(conn->is_fw_assigned_ipv6, options, BIT_11);
  2120. if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
  2121. options |= BIT_8;
  2122. else
  2123. options &= ~BIT_8;
  2124. SET_BITVAL(sess->auto_snd_tgt_disable, options, BIT_6);
  2125. SET_BITVAL(sess->discovery_sess, options, BIT_4);
  2126. SET_BITVAL(sess->entry_state, options, BIT_3);
  2127. fw_ddb_entry->options = cpu_to_le16(options);
  2128. options = le16_to_cpu(fw_ddb_entry->iscsi_options);
  2129. SET_BITVAL(conn->hdrdgst_en, options, BIT_13);
  2130. SET_BITVAL(conn->datadgst_en, options, BIT_12);
  2131. SET_BITVAL(sess->imm_data_en, options, BIT_11);
  2132. SET_BITVAL(sess->initial_r2t_en, options, BIT_10);
  2133. SET_BITVAL(sess->dataseq_inorder_en, options, BIT_9);
  2134. SET_BITVAL(sess->pdu_inorder_en, options, BIT_8);
  2135. SET_BITVAL(sess->chap_auth_en, options, BIT_7);
  2136. SET_BITVAL(conn->snack_req_en, options, BIT_6);
  2137. SET_BITVAL(sess->discovery_logout_en, options, BIT_5);
  2138. SET_BITVAL(sess->bidi_chap_en, options, BIT_4);
  2139. SET_BITVAL(sess->discovery_auth_optional, options, BIT_3);
  2140. SET_BITVAL(sess->erl & BIT_1, options, BIT_1);
  2141. SET_BITVAL(sess->erl & BIT_0, options, BIT_0);
  2142. fw_ddb_entry->iscsi_options = cpu_to_le16(options);
  2143. options = le16_to_cpu(fw_ddb_entry->tcp_options);
  2144. SET_BITVAL(conn->tcp_timestamp_stat, options, BIT_6);
  2145. SET_BITVAL(conn->tcp_nagle_disable, options, BIT_5);
  2146. SET_BITVAL(conn->tcp_wsf_disable, options, BIT_4);
  2147. SET_BITVAL(conn->tcp_timer_scale & BIT_2, options, BIT_3);
  2148. SET_BITVAL(conn->tcp_timer_scale & BIT_1, options, BIT_2);
  2149. SET_BITVAL(conn->tcp_timer_scale & BIT_0, options, BIT_1);
  2150. SET_BITVAL(conn->tcp_timestamp_en, options, BIT_0);
  2151. fw_ddb_entry->tcp_options = cpu_to_le16(options);
  2152. options = le16_to_cpu(fw_ddb_entry->ip_options);
  2153. SET_BITVAL(conn->fragment_disable, options, BIT_4);
  2154. fw_ddb_entry->ip_options = cpu_to_le16(options);
  2155. fw_ddb_entry->iscsi_max_outsnd_r2t = cpu_to_le16(sess->max_r2t);
  2156. fw_ddb_entry->iscsi_max_rcv_data_seg_len =
  2157. cpu_to_le16(conn->max_recv_dlength / BYTE_UNITS);
  2158. fw_ddb_entry->iscsi_max_snd_data_seg_len =
  2159. cpu_to_le16(conn->max_xmit_dlength / BYTE_UNITS);
  2160. fw_ddb_entry->iscsi_first_burst_len =
  2161. cpu_to_le16(sess->first_burst / BYTE_UNITS);
  2162. fw_ddb_entry->iscsi_max_burst_len = cpu_to_le16(sess->max_burst /
  2163. BYTE_UNITS);
  2164. fw_ddb_entry->iscsi_def_time2wait = cpu_to_le16(sess->time2wait);
  2165. fw_ddb_entry->iscsi_def_time2retain = cpu_to_le16(sess->time2retain);
  2166. fw_ddb_entry->tgt_portal_grp = cpu_to_le16(sess->tpgt);
  2167. fw_ddb_entry->mss = cpu_to_le16(conn->max_segment_size);
  2168. fw_ddb_entry->tcp_xmt_wsf = (uint8_t) cpu_to_le32(conn->tcp_xmit_wsf);
  2169. fw_ddb_entry->tcp_rcv_wsf = (uint8_t) cpu_to_le32(conn->tcp_recv_wsf);
  2170. fw_ddb_entry->ipv6_flow_lbl = cpu_to_le16(conn->ipv6_flow_label);
  2171. fw_ddb_entry->ka_timeout = cpu_to_le16(conn->keepalive_timeout);
  2172. fw_ddb_entry->lcl_port = cpu_to_le16(conn->local_port);
  2173. fw_ddb_entry->stat_sn = cpu_to_le32(conn->statsn);
  2174. fw_ddb_entry->exp_stat_sn = cpu_to_le32(conn->exp_statsn);
  2175. fw_ddb_entry->ddb_link = cpu_to_le16(sess->discovery_parent_idx);
  2176. fw_ddb_entry->chap_tbl_idx = cpu_to_le16(sess->chap_out_idx);
  2177. fw_ddb_entry->tsid = cpu_to_le16(sess->tsid);
  2178. fw_ddb_entry->port = cpu_to_le16(conn->port);
  2179. fw_ddb_entry->def_timeout =
  2180. cpu_to_le16(sess->default_taskmgmt_timeout);
  2181. if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
  2182. fw_ddb_entry->ipv4_tos = conn->ipv6_traffic_class;
  2183. else
  2184. fw_ddb_entry->ipv4_tos = conn->ipv4_tos;
  2185. if (conn->ipaddress)
  2186. memcpy(fw_ddb_entry->ip_addr, conn->ipaddress,
  2187. sizeof(fw_ddb_entry->ip_addr));
  2188. if (conn->redirect_ipaddr)
  2189. memcpy(fw_ddb_entry->tgt_addr, conn->redirect_ipaddr,
  2190. sizeof(fw_ddb_entry->tgt_addr));
  2191. if (conn->link_local_ipv6_addr)
  2192. memcpy(fw_ddb_entry->link_local_ipv6_addr,
  2193. conn->link_local_ipv6_addr,
  2194. sizeof(fw_ddb_entry->link_local_ipv6_addr));
  2195. if (sess->targetname)
  2196. memcpy(fw_ddb_entry->iscsi_name, sess->targetname,
  2197. sizeof(fw_ddb_entry->iscsi_name));
  2198. if (sess->targetalias)
  2199. memcpy(fw_ddb_entry->iscsi_alias, sess->targetalias,
  2200. sizeof(fw_ddb_entry->iscsi_alias));
  2201. COPY_ISID(fw_ddb_entry->isid, sess->isid);
  2202. return rc;
  2203. }
  2204. static void qla4xxx_copy_to_sess_conn_params(struct iscsi_conn *conn,
  2205. struct iscsi_session *sess,
  2206. struct dev_db_entry *fw_ddb_entry)
  2207. {
  2208. unsigned long options = 0;
  2209. uint16_t ddb_link;
  2210. uint16_t disc_parent;
  2211. options = le16_to_cpu(fw_ddb_entry->options);
  2212. conn->is_fw_assigned_ipv6 = test_bit(OPT_IS_FW_ASSIGNED_IPV6, &options);
  2213. sess->auto_snd_tgt_disable = test_bit(OPT_AUTO_SENDTGTS_DISABLE,
  2214. &options);
  2215. sess->discovery_sess = test_bit(OPT_DISC_SESSION, &options);
  2216. options = le16_to_cpu(fw_ddb_entry->iscsi_options);
  2217. conn->hdrdgst_en = test_bit(ISCSIOPT_HEADER_DIGEST_EN, &options);
  2218. conn->datadgst_en = test_bit(ISCSIOPT_DATA_DIGEST_EN, &options);
  2219. sess->imm_data_en = test_bit(ISCSIOPT_IMMEDIATE_DATA_EN, &options);
  2220. sess->initial_r2t_en = test_bit(ISCSIOPT_INITIAL_R2T_EN, &options);
  2221. sess->dataseq_inorder_en = test_bit(ISCSIOPT_DATA_SEQ_IN_ORDER,
  2222. &options);
  2223. sess->pdu_inorder_en = test_bit(ISCSIOPT_DATA_PDU_IN_ORDER, &options);
  2224. sess->chap_auth_en = test_bit(ISCSIOPT_CHAP_AUTH_EN, &options);
  2225. sess->discovery_logout_en = test_bit(ISCSIOPT_DISCOVERY_LOGOUT_EN,
  2226. &options);
  2227. sess->bidi_chap_en = test_bit(ISCSIOPT_BIDI_CHAP_EN, &options);
  2228. sess->discovery_auth_optional =
  2229. test_bit(ISCSIOPT_DISCOVERY_AUTH_OPTIONAL, &options);
  2230. if (test_bit(ISCSIOPT_ERL1, &options))
  2231. sess->erl |= BIT_1;
  2232. if (test_bit(ISCSIOPT_ERL0, &options))
  2233. sess->erl |= BIT_0;
  2234. options = le16_to_cpu(fw_ddb_entry->tcp_options);
  2235. conn->tcp_timestamp_stat = test_bit(TCPOPT_TIMESTAMP_STAT, &options);
  2236. conn->tcp_nagle_disable = test_bit(TCPOPT_NAGLE_DISABLE, &options);
  2237. conn->tcp_wsf_disable = test_bit(TCPOPT_WSF_DISABLE, &options);
  2238. if (test_bit(TCPOPT_TIMER_SCALE3, &options))
  2239. conn->tcp_timer_scale |= BIT_3;
  2240. if (test_bit(TCPOPT_TIMER_SCALE2, &options))
  2241. conn->tcp_timer_scale |= BIT_2;
  2242. if (test_bit(TCPOPT_TIMER_SCALE1, &options))
  2243. conn->tcp_timer_scale |= BIT_1;
  2244. conn->tcp_timer_scale >>= 1;
  2245. conn->tcp_timestamp_en = test_bit(TCPOPT_TIMESTAMP_EN, &options);
  2246. options = le16_to_cpu(fw_ddb_entry->ip_options);
  2247. conn->fragment_disable = test_bit(IPOPT_FRAGMENT_DISABLE, &options);
  2248. conn->max_recv_dlength = BYTE_UNITS *
  2249. le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  2250. conn->max_xmit_dlength = BYTE_UNITS *
  2251. le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  2252. sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  2253. sess->first_burst = BYTE_UNITS *
  2254. le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  2255. sess->max_burst = BYTE_UNITS *
  2256. le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  2257. sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  2258. sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  2259. sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  2260. conn->max_segment_size = le16_to_cpu(fw_ddb_entry->mss);
  2261. conn->tcp_xmit_wsf = fw_ddb_entry->tcp_xmt_wsf;
  2262. conn->tcp_recv_wsf = fw_ddb_entry->tcp_rcv_wsf;
  2263. conn->ipv4_tos = fw_ddb_entry->ipv4_tos;
  2264. conn->keepalive_tmo = le16_to_cpu(fw_ddb_entry->ka_timeout);
  2265. conn->local_port = le16_to_cpu(fw_ddb_entry->lcl_port);
  2266. conn->statsn = le32_to_cpu(fw_ddb_entry->stat_sn);
  2267. conn->exp_statsn = le32_to_cpu(fw_ddb_entry->exp_stat_sn);
  2268. sess->tsid = le16_to_cpu(fw_ddb_entry->tsid);
  2269. COPY_ISID(sess->isid, fw_ddb_entry->isid);
  2270. ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
  2271. if (ddb_link == DDB_ISNS)
  2272. disc_parent = ISCSI_DISC_PARENT_ISNS;
  2273. else if (ddb_link == DDB_NO_LINK)
  2274. disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
  2275. else if (ddb_link < MAX_DDB_ENTRIES)
  2276. disc_parent = ISCSI_DISC_PARENT_SENDTGT;
  2277. else
  2278. disc_parent = ISCSI_DISC_PARENT_UNKNOWN;
  2279. iscsi_set_param(conn->cls_conn, ISCSI_PARAM_DISCOVERY_PARENT_TYPE,
  2280. iscsi_get_discovery_parent_name(disc_parent), 0);
  2281. iscsi_set_param(conn->cls_conn, ISCSI_PARAM_TARGET_ALIAS,
  2282. (char *)fw_ddb_entry->iscsi_alias, 0);
  2283. }
  2284. static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
  2285. struct dev_db_entry *fw_ddb_entry,
  2286. struct iscsi_cls_session *cls_sess,
  2287. struct iscsi_cls_conn *cls_conn)
  2288. {
  2289. int buflen = 0;
  2290. struct iscsi_session *sess;
  2291. struct ddb_entry *ddb_entry;
  2292. struct ql4_chap_table chap_tbl;
  2293. struct iscsi_conn *conn;
  2294. char ip_addr[DDB_IPADDR_LEN];
  2295. uint16_t options = 0;
  2296. sess = cls_sess->dd_data;
  2297. ddb_entry = sess->dd_data;
  2298. conn = cls_conn->dd_data;
  2299. memset(&chap_tbl, 0, sizeof(chap_tbl));
  2300. ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  2301. qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
  2302. sess->def_taskmgmt_tmo = le16_to_cpu(fw_ddb_entry->def_timeout);
  2303. conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
  2304. memset(ip_addr, 0, sizeof(ip_addr));
  2305. options = le16_to_cpu(fw_ddb_entry->options);
  2306. if (options & DDB_OPT_IPV6_DEVICE) {
  2307. iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv6", 4);
  2308. memset(ip_addr, 0, sizeof(ip_addr));
  2309. sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  2310. } else {
  2311. iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv4", 4);
  2312. sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  2313. }
  2314. iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
  2315. (char *)ip_addr, buflen);
  2316. iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
  2317. (char *)fw_ddb_entry->iscsi_name, buflen);
  2318. iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
  2319. (char *)ha->name_string, buflen);
  2320. if (ddb_entry->chap_tbl_idx != INVALID_ENTRY) {
  2321. if (!qla4xxx_get_uni_chap_at_index(ha, chap_tbl.name,
  2322. chap_tbl.secret,
  2323. ddb_entry->chap_tbl_idx)) {
  2324. iscsi_set_param(cls_conn, ISCSI_PARAM_USERNAME,
  2325. (char *)chap_tbl.name,
  2326. strlen((char *)chap_tbl.name));
  2327. iscsi_set_param(cls_conn, ISCSI_PARAM_PASSWORD,
  2328. (char *)chap_tbl.secret,
  2329. chap_tbl.secret_len);
  2330. }
  2331. }
  2332. }
  2333. void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
  2334. struct ddb_entry *ddb_entry)
  2335. {
  2336. struct iscsi_cls_session *cls_sess;
  2337. struct iscsi_cls_conn *cls_conn;
  2338. uint32_t ddb_state;
  2339. dma_addr_t fw_ddb_entry_dma;
  2340. struct dev_db_entry *fw_ddb_entry;
  2341. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  2342. &fw_ddb_entry_dma, GFP_KERNEL);
  2343. if (!fw_ddb_entry) {
  2344. ql4_printk(KERN_ERR, ha,
  2345. "%s: Unable to allocate dma buffer\n", __func__);
  2346. goto exit_session_conn_fwddb_param;
  2347. }
  2348. if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  2349. fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  2350. NULL, NULL, NULL) == QLA_ERROR) {
  2351. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  2352. "get_ddb_entry for fw_ddb_index %d\n",
  2353. ha->host_no, __func__,
  2354. ddb_entry->fw_ddb_index));
  2355. goto exit_session_conn_fwddb_param;
  2356. }
  2357. cls_sess = ddb_entry->sess;
  2358. cls_conn = ddb_entry->conn;
  2359. /* Update params */
  2360. qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
  2361. exit_session_conn_fwddb_param:
  2362. if (fw_ddb_entry)
  2363. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  2364. fw_ddb_entry, fw_ddb_entry_dma);
  2365. }
  2366. void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
  2367. struct ddb_entry *ddb_entry)
  2368. {
  2369. struct iscsi_cls_session *cls_sess;
  2370. struct iscsi_cls_conn *cls_conn;
  2371. struct iscsi_session *sess;
  2372. struct iscsi_conn *conn;
  2373. uint32_t ddb_state;
  2374. dma_addr_t fw_ddb_entry_dma;
  2375. struct dev_db_entry *fw_ddb_entry;
  2376. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  2377. &fw_ddb_entry_dma, GFP_KERNEL);
  2378. if (!fw_ddb_entry) {
  2379. ql4_printk(KERN_ERR, ha,
  2380. "%s: Unable to allocate dma buffer\n", __func__);
  2381. goto exit_session_conn_param;
  2382. }
  2383. if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  2384. fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  2385. NULL, NULL, NULL) == QLA_ERROR) {
  2386. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  2387. "get_ddb_entry for fw_ddb_index %d\n",
  2388. ha->host_no, __func__,
  2389. ddb_entry->fw_ddb_index));
  2390. goto exit_session_conn_param;
  2391. }
  2392. cls_sess = ddb_entry->sess;
  2393. sess = cls_sess->dd_data;
  2394. cls_conn = ddb_entry->conn;
  2395. conn = cls_conn->dd_data;
  2396. /* Update timers after login */
  2397. ddb_entry->default_relogin_timeout =
  2398. (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
  2399. (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
  2400. le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
  2401. ddb_entry->default_time2wait =
  2402. le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  2403. /* Update params */
  2404. ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  2405. qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
  2406. memcpy(sess->initiatorname, ha->name_string,
  2407. min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
  2408. exit_session_conn_param:
  2409. if (fw_ddb_entry)
  2410. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  2411. fw_ddb_entry, fw_ddb_entry_dma);
  2412. }
  2413. /*
  2414. * Timer routines
  2415. */
  2416. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  2417. unsigned long interval)
  2418. {
  2419. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  2420. __func__, ha->host->host_no));
  2421. init_timer(&ha->timer);
  2422. ha->timer.expires = jiffies + interval * HZ;
  2423. ha->timer.data = (unsigned long)ha;
  2424. ha->timer.function = (void (*)(unsigned long))func;
  2425. add_timer(&ha->timer);
  2426. ha->timer_active = 1;
  2427. }
  2428. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  2429. {
  2430. del_timer_sync(&ha->timer);
  2431. ha->timer_active = 0;
  2432. }
  2433. /***
  2434. * qla4xxx_mark_device_missing - blocks the session
  2435. * @cls_session: Pointer to the session to be blocked
  2436. * @ddb_entry: Pointer to device database entry
  2437. *
  2438. * This routine marks a device missing and close connection.
  2439. **/
  2440. void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
  2441. {
  2442. iscsi_block_session(cls_session);
  2443. }
  2444. /**
  2445. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  2446. * @ha: Pointer to host adapter structure.
  2447. *
  2448. * This routine marks a device missing and resets the relogin retry count.
  2449. **/
  2450. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  2451. {
  2452. iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
  2453. }
  2454. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  2455. struct ddb_entry *ddb_entry,
  2456. struct scsi_cmnd *cmd)
  2457. {
  2458. struct srb *srb;
  2459. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  2460. if (!srb)
  2461. return srb;
  2462. kref_init(&srb->srb_ref);
  2463. srb->ha = ha;
  2464. srb->ddb = ddb_entry;
  2465. srb->cmd = cmd;
  2466. srb->flags = 0;
  2467. CMD_SP(cmd) = (void *)srb;
  2468. return srb;
  2469. }
  2470. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  2471. {
  2472. struct scsi_cmnd *cmd = srb->cmd;
  2473. if (srb->flags & SRB_DMA_VALID) {
  2474. scsi_dma_unmap(cmd);
  2475. srb->flags &= ~SRB_DMA_VALID;
  2476. }
  2477. CMD_SP(cmd) = NULL;
  2478. }
  2479. void qla4xxx_srb_compl(struct kref *ref)
  2480. {
  2481. struct srb *srb = container_of(ref, struct srb, srb_ref);
  2482. struct scsi_cmnd *cmd = srb->cmd;
  2483. struct scsi_qla_host *ha = srb->ha;
  2484. qla4xxx_srb_free_dma(ha, srb);
  2485. mempool_free(srb, ha->srb_mempool);
  2486. cmd->scsi_done(cmd);
  2487. }
  2488. /**
  2489. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  2490. * @host: scsi host
  2491. * @cmd: Pointer to Linux's SCSI command structure
  2492. *
  2493. * Remarks:
  2494. * This routine is invoked by Linux to send a SCSI command to the driver.
  2495. * The mid-level driver tries to ensure that queuecommand never gets
  2496. * invoked concurrently with itself or the interrupt handler (although
  2497. * the interrupt handler may call this routine as part of request-
  2498. * completion handling). Unfortunely, it sometimes calls the scheduler
  2499. * in interrupt context which is a big NO! NO!.
  2500. **/
  2501. static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  2502. {
  2503. struct scsi_qla_host *ha = to_qla_host(host);
  2504. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  2505. struct iscsi_cls_session *sess = ddb_entry->sess;
  2506. struct srb *srb;
  2507. int rval;
  2508. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  2509. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  2510. cmd->result = DID_NO_CONNECT << 16;
  2511. else
  2512. cmd->result = DID_REQUEUE << 16;
  2513. goto qc_fail_command;
  2514. }
  2515. if (!sess) {
  2516. cmd->result = DID_IMM_RETRY << 16;
  2517. goto qc_fail_command;
  2518. }
  2519. rval = iscsi_session_chkready(sess);
  2520. if (rval) {
  2521. cmd->result = rval;
  2522. goto qc_fail_command;
  2523. }
  2524. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  2525. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  2526. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2527. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  2528. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  2529. !test_bit(AF_ONLINE, &ha->flags) ||
  2530. !test_bit(AF_LINK_UP, &ha->flags) ||
  2531. test_bit(AF_LOOPBACK, &ha->flags) ||
  2532. test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags) ||
  2533. test_bit(DPC_RESTORE_ACB, &ha->dpc_flags) ||
  2534. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  2535. goto qc_host_busy;
  2536. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
  2537. if (!srb)
  2538. goto qc_host_busy;
  2539. rval = qla4xxx_send_command_to_isp(ha, srb);
  2540. if (rval != QLA_SUCCESS)
  2541. goto qc_host_busy_free_sp;
  2542. return 0;
  2543. qc_host_busy_free_sp:
  2544. qla4xxx_srb_free_dma(ha, srb);
  2545. mempool_free(srb, ha->srb_mempool);
  2546. qc_host_busy:
  2547. return SCSI_MLQUEUE_HOST_BUSY;
  2548. qc_fail_command:
  2549. cmd->scsi_done(cmd);
  2550. return 0;
  2551. }
  2552. /**
  2553. * qla4xxx_mem_free - frees memory allocated to adapter
  2554. * @ha: Pointer to host adapter structure.
  2555. *
  2556. * Frees memory previously allocated by qla4xxx_mem_alloc
  2557. **/
  2558. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  2559. {
  2560. if (ha->queues)
  2561. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  2562. ha->queues_dma);
  2563. if (ha->fw_dump)
  2564. vfree(ha->fw_dump);
  2565. ha->queues_len = 0;
  2566. ha->queues = NULL;
  2567. ha->queues_dma = 0;
  2568. ha->request_ring = NULL;
  2569. ha->request_dma = 0;
  2570. ha->response_ring = NULL;
  2571. ha->response_dma = 0;
  2572. ha->shadow_regs = NULL;
  2573. ha->shadow_regs_dma = 0;
  2574. ha->fw_dump = NULL;
  2575. ha->fw_dump_size = 0;
  2576. /* Free srb pool. */
  2577. if (ha->srb_mempool)
  2578. mempool_destroy(ha->srb_mempool);
  2579. ha->srb_mempool = NULL;
  2580. if (ha->chap_dma_pool)
  2581. dma_pool_destroy(ha->chap_dma_pool);
  2582. if (ha->chap_list)
  2583. vfree(ha->chap_list);
  2584. ha->chap_list = NULL;
  2585. if (ha->fw_ddb_dma_pool)
  2586. dma_pool_destroy(ha->fw_ddb_dma_pool);
  2587. /* release io space registers */
  2588. if (is_qla8022(ha)) {
  2589. if (ha->nx_pcibase)
  2590. iounmap(
  2591. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  2592. } else if (is_qla8032(ha) || is_qla8042(ha)) {
  2593. if (ha->nx_pcibase)
  2594. iounmap(
  2595. (struct device_reg_83xx __iomem *)ha->nx_pcibase);
  2596. } else if (ha->reg) {
  2597. iounmap(ha->reg);
  2598. }
  2599. if (ha->reset_tmplt.buff)
  2600. vfree(ha->reset_tmplt.buff);
  2601. pci_release_regions(ha->pdev);
  2602. }
  2603. /**
  2604. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  2605. * @ha: Pointer to host adapter structure
  2606. *
  2607. * Allocates DMA memory for request and response queues. Also allocates memory
  2608. * for srbs.
  2609. **/
  2610. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  2611. {
  2612. unsigned long align;
  2613. /* Allocate contiguous block of DMA memory for queues. */
  2614. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  2615. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  2616. sizeof(struct shadow_regs) +
  2617. MEM_ALIGN_VALUE +
  2618. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  2619. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  2620. &ha->queues_dma, GFP_KERNEL);
  2621. if (ha->queues == NULL) {
  2622. ql4_printk(KERN_WARNING, ha,
  2623. "Memory Allocation failed - queues.\n");
  2624. goto mem_alloc_error_exit;
  2625. }
  2626. memset(ha->queues, 0, ha->queues_len);
  2627. /*
  2628. * As per RISC alignment requirements -- the bus-address must be a
  2629. * multiple of the request-ring size (in bytes).
  2630. */
  2631. align = 0;
  2632. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  2633. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  2634. (MEM_ALIGN_VALUE - 1));
  2635. /* Update request and response queue pointers. */
  2636. ha->request_dma = ha->queues_dma + align;
  2637. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  2638. ha->response_dma = ha->queues_dma + align +
  2639. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  2640. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  2641. (REQUEST_QUEUE_DEPTH *
  2642. QUEUE_SIZE));
  2643. ha->shadow_regs_dma = ha->queues_dma + align +
  2644. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  2645. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  2646. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  2647. (REQUEST_QUEUE_DEPTH *
  2648. QUEUE_SIZE) +
  2649. (RESPONSE_QUEUE_DEPTH *
  2650. QUEUE_SIZE));
  2651. /* Allocate memory for srb pool. */
  2652. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  2653. mempool_free_slab, srb_cachep);
  2654. if (ha->srb_mempool == NULL) {
  2655. ql4_printk(KERN_WARNING, ha,
  2656. "Memory Allocation failed - SRB Pool.\n");
  2657. goto mem_alloc_error_exit;
  2658. }
  2659. ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
  2660. CHAP_DMA_BLOCK_SIZE, 8, 0);
  2661. if (ha->chap_dma_pool == NULL) {
  2662. ql4_printk(KERN_WARNING, ha,
  2663. "%s: chap_dma_pool allocation failed..\n", __func__);
  2664. goto mem_alloc_error_exit;
  2665. }
  2666. ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
  2667. DDB_DMA_BLOCK_SIZE, 8, 0);
  2668. if (ha->fw_ddb_dma_pool == NULL) {
  2669. ql4_printk(KERN_WARNING, ha,
  2670. "%s: fw_ddb_dma_pool allocation failed..\n",
  2671. __func__);
  2672. goto mem_alloc_error_exit;
  2673. }
  2674. return QLA_SUCCESS;
  2675. mem_alloc_error_exit:
  2676. qla4xxx_mem_free(ha);
  2677. return QLA_ERROR;
  2678. }
  2679. /**
  2680. * qla4_8xxx_check_temp - Check the ISP82XX temperature.
  2681. * @ha: adapter block pointer.
  2682. *
  2683. * Note: The caller should not hold the idc lock.
  2684. **/
  2685. static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
  2686. {
  2687. uint32_t temp, temp_state, temp_val;
  2688. int status = QLA_SUCCESS;
  2689. temp = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_TEMP_STATE);
  2690. temp_state = qla82xx_get_temp_state(temp);
  2691. temp_val = qla82xx_get_temp_val(temp);
  2692. if (temp_state == QLA82XX_TEMP_PANIC) {
  2693. ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
  2694. " exceeds maximum allowed. Hardware has been shut"
  2695. " down.\n", temp_val);
  2696. status = QLA_ERROR;
  2697. } else if (temp_state == QLA82XX_TEMP_WARN) {
  2698. if (ha->temperature == QLA82XX_TEMP_NORMAL)
  2699. ql4_printk(KERN_WARNING, ha, "Device temperature %d"
  2700. " degrees C exceeds operating range."
  2701. " Immediate action needed.\n", temp_val);
  2702. } else {
  2703. if (ha->temperature == QLA82XX_TEMP_WARN)
  2704. ql4_printk(KERN_INFO, ha, "Device temperature is"
  2705. " now %d degrees C in normal range.\n",
  2706. temp_val);
  2707. }
  2708. ha->temperature = temp_state;
  2709. return status;
  2710. }
  2711. /**
  2712. * qla4_8xxx_check_fw_alive - Check firmware health
  2713. * @ha: Pointer to host adapter structure.
  2714. *
  2715. * Context: Interrupt
  2716. **/
  2717. static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  2718. {
  2719. uint32_t fw_heartbeat_counter;
  2720. int status = QLA_SUCCESS;
  2721. fw_heartbeat_counter = qla4_8xxx_rd_direct(ha,
  2722. QLA8XXX_PEG_ALIVE_COUNTER);
  2723. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  2724. if (fw_heartbeat_counter == 0xffffffff) {
  2725. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  2726. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  2727. ha->host_no, __func__));
  2728. return status;
  2729. }
  2730. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  2731. ha->seconds_since_last_heartbeat++;
  2732. /* FW not alive after 2 seconds */
  2733. if (ha->seconds_since_last_heartbeat == 2) {
  2734. ha->seconds_since_last_heartbeat = 0;
  2735. qla4_8xxx_dump_peg_reg(ha);
  2736. status = QLA_ERROR;
  2737. }
  2738. } else
  2739. ha->seconds_since_last_heartbeat = 0;
  2740. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  2741. return status;
  2742. }
  2743. static void qla4_8xxx_process_fw_error(struct scsi_qla_host *ha)
  2744. {
  2745. uint32_t halt_status;
  2746. int halt_status_unrecoverable = 0;
  2747. halt_status = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_HALT_STATUS1);
  2748. if (is_qla8022(ha)) {
  2749. ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
  2750. __func__);
  2751. qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
  2752. CRB_NIU_XG_PAUSE_CTL_P0 |
  2753. CRB_NIU_XG_PAUSE_CTL_P1);
  2754. if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
  2755. ql4_printk(KERN_ERR, ha, "%s: Firmware aborted with error code 0x00006700. Device is being reset\n",
  2756. __func__);
  2757. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  2758. halt_status_unrecoverable = 1;
  2759. } else if (is_qla8032(ha) || is_qla8042(ha)) {
  2760. if (halt_status & QLA83XX_HALT_STATUS_FW_RESET)
  2761. ql4_printk(KERN_ERR, ha, "%s: Firmware error detected device is being reset\n",
  2762. __func__);
  2763. else if (halt_status & QLA83XX_HALT_STATUS_UNRECOVERABLE)
  2764. halt_status_unrecoverable = 1;
  2765. }
  2766. /*
  2767. * Since we cannot change dev_state in interrupt context,
  2768. * set appropriate DPC flag then wakeup DPC
  2769. */
  2770. if (halt_status_unrecoverable) {
  2771. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  2772. } else {
  2773. ql4_printk(KERN_INFO, ha, "%s: detect abort needed!\n",
  2774. __func__);
  2775. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2776. }
  2777. qla4xxx_mailbox_premature_completion(ha);
  2778. qla4xxx_wake_dpc(ha);
  2779. }
  2780. /**
  2781. * qla4_8xxx_watchdog - Poll dev state
  2782. * @ha: Pointer to host adapter structure.
  2783. *
  2784. * Context: Interrupt
  2785. **/
  2786. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  2787. {
  2788. uint32_t dev_state;
  2789. uint32_t idc_ctrl;
  2790. /* don't poll if reset is going on */
  2791. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  2792. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2793. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
  2794. dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
  2795. if (qla4_8xxx_check_temp(ha)) {
  2796. if (is_qla8022(ha)) {
  2797. ql4_printk(KERN_INFO, ha, "disabling pause transmit on port 0 & 1.\n");
  2798. qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
  2799. CRB_NIU_XG_PAUSE_CTL_P0 |
  2800. CRB_NIU_XG_PAUSE_CTL_P1);
  2801. }
  2802. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  2803. qla4xxx_wake_dpc(ha);
  2804. } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
  2805. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  2806. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED RESET!\n",
  2807. __func__);
  2808. if (is_qla8032(ha) || is_qla8042(ha)) {
  2809. idc_ctrl = qla4_83xx_rd_reg(ha,
  2810. QLA83XX_IDC_DRV_CTRL);
  2811. if (!(idc_ctrl & GRACEFUL_RESET_BIT1)) {
  2812. ql4_printk(KERN_INFO, ha, "%s: Graceful reset bit is not set\n",
  2813. __func__);
  2814. qla4xxx_mailbox_premature_completion(
  2815. ha);
  2816. }
  2817. }
  2818. if ((is_qla8032(ha) || is_qla8042(ha)) ||
  2819. (is_qla8022(ha) && !ql4xdontresethba)) {
  2820. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2821. qla4xxx_wake_dpc(ha);
  2822. }
  2823. } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
  2824. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  2825. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  2826. __func__);
  2827. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  2828. qla4xxx_wake_dpc(ha);
  2829. } else {
  2830. /* Check firmware health */
  2831. if (qla4_8xxx_check_fw_alive(ha))
  2832. qla4_8xxx_process_fw_error(ha);
  2833. }
  2834. }
  2835. }
  2836. static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  2837. {
  2838. struct iscsi_session *sess;
  2839. struct ddb_entry *ddb_entry;
  2840. struct scsi_qla_host *ha;
  2841. sess = cls_sess->dd_data;
  2842. ddb_entry = sess->dd_data;
  2843. ha = ddb_entry->ha;
  2844. if (!(ddb_entry->ddb_type == FLASH_DDB))
  2845. return;
  2846. if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  2847. !iscsi_is_session_online(cls_sess)) {
  2848. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  2849. INVALID_ENTRY) {
  2850. if (atomic_read(&ddb_entry->retry_relogin_timer) ==
  2851. 0) {
  2852. atomic_set(&ddb_entry->retry_relogin_timer,
  2853. INVALID_ENTRY);
  2854. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  2855. set_bit(DF_RELOGIN, &ddb_entry->flags);
  2856. DEBUG2(ql4_printk(KERN_INFO, ha,
  2857. "%s: index [%d] login device\n",
  2858. __func__, ddb_entry->fw_ddb_index));
  2859. } else
  2860. atomic_dec(&ddb_entry->retry_relogin_timer);
  2861. }
  2862. }
  2863. /* Wait for relogin to timeout */
  2864. if (atomic_read(&ddb_entry->relogin_timer) &&
  2865. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  2866. /*
  2867. * If the relogin times out and the device is
  2868. * still NOT ONLINE then try and relogin again.
  2869. */
  2870. if (!iscsi_is_session_online(cls_sess)) {
  2871. /* Reset retry relogin timer */
  2872. atomic_inc(&ddb_entry->relogin_retry_count);
  2873. DEBUG2(ql4_printk(KERN_INFO, ha,
  2874. "%s: index[%d] relogin timed out-retrying"
  2875. " relogin (%d), retry (%d)\n", __func__,
  2876. ddb_entry->fw_ddb_index,
  2877. atomic_read(&ddb_entry->relogin_retry_count),
  2878. ddb_entry->default_time2wait + 4));
  2879. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  2880. atomic_set(&ddb_entry->retry_relogin_timer,
  2881. ddb_entry->default_time2wait + 4);
  2882. }
  2883. }
  2884. }
  2885. /**
  2886. * qla4xxx_timer - checks every second for work to do.
  2887. * @ha: Pointer to host adapter structure.
  2888. **/
  2889. static void qla4xxx_timer(struct scsi_qla_host *ha)
  2890. {
  2891. int start_dpc = 0;
  2892. uint16_t w;
  2893. iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
  2894. /* If we are in the middle of AER/EEH processing
  2895. * skip any processing and reschedule the timer
  2896. */
  2897. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  2898. mod_timer(&ha->timer, jiffies + HZ);
  2899. return;
  2900. }
  2901. /* Hardware read to trigger an EEH error during mailbox waits. */
  2902. if (!pci_channel_offline(ha->pdev))
  2903. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  2904. if (is_qla80XX(ha))
  2905. qla4_8xxx_watchdog(ha);
  2906. if (is_qla40XX(ha)) {
  2907. /* Check for heartbeat interval. */
  2908. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  2909. ha->heartbeat_interval != 0) {
  2910. ha->seconds_since_last_heartbeat++;
  2911. if (ha->seconds_since_last_heartbeat >
  2912. ha->heartbeat_interval + 2)
  2913. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2914. }
  2915. }
  2916. /* Process any deferred work. */
  2917. if (!list_empty(&ha->work_list))
  2918. start_dpc++;
  2919. /* Wakeup the dpc routine for this adapter, if needed. */
  2920. if (start_dpc ||
  2921. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2922. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  2923. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  2924. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  2925. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  2926. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  2927. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  2928. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  2929. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  2930. test_bit(DPC_AEN, &ha->dpc_flags)) {
  2931. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  2932. " - dpc flags = 0x%lx\n",
  2933. ha->host_no, __func__, ha->dpc_flags));
  2934. qla4xxx_wake_dpc(ha);
  2935. }
  2936. /* Reschedule timer thread to call us back in one second */
  2937. mod_timer(&ha->timer, jiffies + HZ);
  2938. DEBUG2(ha->seconds_since_last_intr++);
  2939. }
  2940. /**
  2941. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  2942. * @ha: Pointer to host adapter structure.
  2943. *
  2944. * This routine stalls the driver until all outstanding commands are returned.
  2945. * Caller must release the Hardware Lock prior to calling this routine.
  2946. **/
  2947. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  2948. {
  2949. uint32_t index = 0;
  2950. unsigned long flags;
  2951. struct scsi_cmnd *cmd;
  2952. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  2953. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  2954. "complete\n", WAIT_CMD_TOV));
  2955. while (!time_after_eq(jiffies, wtime)) {
  2956. spin_lock_irqsave(&ha->hardware_lock, flags);
  2957. /* Find a command that hasn't completed. */
  2958. for (index = 0; index < ha->host->can_queue; index++) {
  2959. cmd = scsi_host_find_tag(ha->host, index);
  2960. /*
  2961. * We cannot just check if the index is valid,
  2962. * becase if we are run from the scsi eh, then
  2963. * the scsi/block layer is going to prevent
  2964. * the tag from being released.
  2965. */
  2966. if (cmd != NULL && CMD_SP(cmd))
  2967. break;
  2968. }
  2969. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2970. /* If No Commands are pending, wait is complete */
  2971. if (index == ha->host->can_queue)
  2972. return QLA_SUCCESS;
  2973. msleep(1000);
  2974. }
  2975. /* If we timed out on waiting for commands to come back
  2976. * return ERROR. */
  2977. return QLA_ERROR;
  2978. }
  2979. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  2980. {
  2981. uint32_t ctrl_status;
  2982. unsigned long flags = 0;
  2983. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  2984. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  2985. return QLA_ERROR;
  2986. spin_lock_irqsave(&ha->hardware_lock, flags);
  2987. /*
  2988. * If the SCSI Reset Interrupt bit is set, clear it.
  2989. * Otherwise, the Soft Reset won't work.
  2990. */
  2991. ctrl_status = readw(&ha->reg->ctrl_status);
  2992. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  2993. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  2994. /* Issue Soft Reset */
  2995. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  2996. readl(&ha->reg->ctrl_status);
  2997. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2998. return QLA_SUCCESS;
  2999. }
  3000. /**
  3001. * qla4xxx_soft_reset - performs soft reset.
  3002. * @ha: Pointer to host adapter structure.
  3003. **/
  3004. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  3005. {
  3006. uint32_t max_wait_time;
  3007. unsigned long flags = 0;
  3008. int status;
  3009. uint32_t ctrl_status;
  3010. status = qla4xxx_hw_reset(ha);
  3011. if (status != QLA_SUCCESS)
  3012. return status;
  3013. status = QLA_ERROR;
  3014. /* Wait until the Network Reset Intr bit is cleared */
  3015. max_wait_time = RESET_INTR_TOV;
  3016. do {
  3017. spin_lock_irqsave(&ha->hardware_lock, flags);
  3018. ctrl_status = readw(&ha->reg->ctrl_status);
  3019. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3020. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  3021. break;
  3022. msleep(1000);
  3023. } while ((--max_wait_time));
  3024. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  3025. DEBUG2(printk(KERN_WARNING
  3026. "scsi%ld: Network Reset Intr not cleared by "
  3027. "Network function, clearing it now!\n",
  3028. ha->host_no));
  3029. spin_lock_irqsave(&ha->hardware_lock, flags);
  3030. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  3031. readl(&ha->reg->ctrl_status);
  3032. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3033. }
  3034. /* Wait until the firmware tells us the Soft Reset is done */
  3035. max_wait_time = SOFT_RESET_TOV;
  3036. do {
  3037. spin_lock_irqsave(&ha->hardware_lock, flags);
  3038. ctrl_status = readw(&ha->reg->ctrl_status);
  3039. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3040. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  3041. status = QLA_SUCCESS;
  3042. break;
  3043. }
  3044. msleep(1000);
  3045. } while ((--max_wait_time));
  3046. /*
  3047. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  3048. * after the soft reset has taken place.
  3049. */
  3050. spin_lock_irqsave(&ha->hardware_lock, flags);
  3051. ctrl_status = readw(&ha->reg->ctrl_status);
  3052. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  3053. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  3054. readl(&ha->reg->ctrl_status);
  3055. }
  3056. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3057. /* If soft reset fails then most probably the bios on other
  3058. * function is also enabled.
  3059. * Since the initialization is sequential the other fn
  3060. * wont be able to acknowledge the soft reset.
  3061. * Issue a force soft reset to workaround this scenario.
  3062. */
  3063. if (max_wait_time == 0) {
  3064. /* Issue Force Soft Reset */
  3065. spin_lock_irqsave(&ha->hardware_lock, flags);
  3066. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  3067. readl(&ha->reg->ctrl_status);
  3068. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3069. /* Wait until the firmware tells us the Soft Reset is done */
  3070. max_wait_time = SOFT_RESET_TOV;
  3071. do {
  3072. spin_lock_irqsave(&ha->hardware_lock, flags);
  3073. ctrl_status = readw(&ha->reg->ctrl_status);
  3074. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3075. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  3076. status = QLA_SUCCESS;
  3077. break;
  3078. }
  3079. msleep(1000);
  3080. } while ((--max_wait_time));
  3081. }
  3082. return status;
  3083. }
  3084. /**
  3085. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  3086. * @ha: Pointer to host adapter structure.
  3087. * @res: returned scsi status
  3088. *
  3089. * This routine is called just prior to a HARD RESET to return all
  3090. * outstanding commands back to the Operating System.
  3091. * Caller should make sure that the following locks are released
  3092. * before this calling routine: Hardware lock, and io_request_lock.
  3093. **/
  3094. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  3095. {
  3096. struct srb *srb;
  3097. int i;
  3098. unsigned long flags;
  3099. spin_lock_irqsave(&ha->hardware_lock, flags);
  3100. for (i = 0; i < ha->host->can_queue; i++) {
  3101. srb = qla4xxx_del_from_active_array(ha, i);
  3102. if (srb != NULL) {
  3103. srb->cmd->result = res;
  3104. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  3105. }
  3106. }
  3107. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3108. }
  3109. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  3110. {
  3111. clear_bit(AF_ONLINE, &ha->flags);
  3112. /* Disable the board */
  3113. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  3114. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  3115. qla4xxx_mark_all_devices_missing(ha);
  3116. clear_bit(AF_INIT_DONE, &ha->flags);
  3117. }
  3118. static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
  3119. {
  3120. struct iscsi_session *sess;
  3121. struct ddb_entry *ddb_entry;
  3122. sess = cls_session->dd_data;
  3123. ddb_entry = sess->dd_data;
  3124. ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
  3125. if (ddb_entry->ddb_type == FLASH_DDB)
  3126. iscsi_block_session(ddb_entry->sess);
  3127. else
  3128. iscsi_session_failure(cls_session->dd_data,
  3129. ISCSI_ERR_CONN_FAILED);
  3130. }
  3131. /**
  3132. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  3133. * @ha: Pointer to host adapter structure.
  3134. **/
  3135. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  3136. {
  3137. int status = QLA_ERROR;
  3138. uint8_t reset_chip = 0;
  3139. uint32_t dev_state;
  3140. unsigned long wait;
  3141. /* Stall incoming I/O until we are done */
  3142. scsi_block_requests(ha->host);
  3143. clear_bit(AF_ONLINE, &ha->flags);
  3144. clear_bit(AF_LINK_UP, &ha->flags);
  3145. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  3146. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  3147. if ((is_qla8032(ha) || is_qla8042(ha)) &&
  3148. !test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  3149. ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
  3150. __func__);
  3151. /* disable pause frame for ISP83xx */
  3152. qla4_83xx_disable_pause(ha);
  3153. }
  3154. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  3155. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  3156. reset_chip = 1;
  3157. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  3158. * do not reset adapter, jump to initialize_adapter */
  3159. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  3160. status = QLA_SUCCESS;
  3161. goto recover_ha_init_adapter;
  3162. }
  3163. /* For the ISP-8xxx adapter, issue a stop_firmware if invoked
  3164. * from eh_host_reset or ioctl module */
  3165. if (is_qla80XX(ha) && !reset_chip &&
  3166. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  3167. DEBUG2(ql4_printk(KERN_INFO, ha,
  3168. "scsi%ld: %s - Performing stop_firmware...\n",
  3169. ha->host_no, __func__));
  3170. status = ha->isp_ops->reset_firmware(ha);
  3171. if (status == QLA_SUCCESS) {
  3172. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  3173. qla4xxx_cmd_wait(ha);
  3174. ha->isp_ops->disable_intrs(ha);
  3175. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  3176. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  3177. } else {
  3178. /* If the stop_firmware fails then
  3179. * reset the entire chip */
  3180. reset_chip = 1;
  3181. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  3182. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  3183. }
  3184. }
  3185. /* Issue full chip reset if recovering from a catastrophic error,
  3186. * or if stop_firmware fails for ISP-8xxx.
  3187. * This is the default case for ISP-4xxx */
  3188. if (is_qla40XX(ha) || reset_chip) {
  3189. if (is_qla40XX(ha))
  3190. goto chip_reset;
  3191. /* Check if 8XXX firmware is alive or not
  3192. * We may have arrived here from NEED_RESET
  3193. * detection only */
  3194. if (test_bit(AF_FW_RECOVERY, &ha->flags))
  3195. goto chip_reset;
  3196. wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
  3197. while (time_before(jiffies, wait)) {
  3198. if (qla4_8xxx_check_fw_alive(ha)) {
  3199. qla4xxx_mailbox_premature_completion(ha);
  3200. break;
  3201. }
  3202. set_current_state(TASK_UNINTERRUPTIBLE);
  3203. schedule_timeout(HZ);
  3204. }
  3205. chip_reset:
  3206. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  3207. qla4xxx_cmd_wait(ha);
  3208. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  3209. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  3210. DEBUG2(ql4_printk(KERN_INFO, ha,
  3211. "scsi%ld: %s - Performing chip reset..\n",
  3212. ha->host_no, __func__));
  3213. status = ha->isp_ops->reset_chip(ha);
  3214. }
  3215. /* Flush any pending ddb changed AENs */
  3216. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  3217. recover_ha_init_adapter:
  3218. /* Upon successful firmware/chip reset, re-initialize the adapter */
  3219. if (status == QLA_SUCCESS) {
  3220. /* For ISP-4xxx, force function 1 to always initialize
  3221. * before function 3 to prevent both funcions from
  3222. * stepping on top of the other */
  3223. if (is_qla40XX(ha) && (ha->mac_index == 3))
  3224. ssleep(6);
  3225. /* NOTE: AF_ONLINE flag set upon successful completion of
  3226. * qla4xxx_initialize_adapter */
  3227. status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  3228. }
  3229. /* Retry failed adapter initialization, if necessary
  3230. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  3231. * case to prevent ping-pong resets between functions */
  3232. if (!test_bit(AF_ONLINE, &ha->flags) &&
  3233. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  3234. /* Adapter initialization failed, see if we can retry
  3235. * resetting the ha.
  3236. * Since we don't want to block the DPC for too long
  3237. * with multiple resets in the same thread,
  3238. * utilize DPC to retry */
  3239. if (is_qla80XX(ha)) {
  3240. ha->isp_ops->idc_lock(ha);
  3241. dev_state = qla4_8xxx_rd_direct(ha,
  3242. QLA8XXX_CRB_DEV_STATE);
  3243. ha->isp_ops->idc_unlock(ha);
  3244. if (dev_state == QLA8XXX_DEV_FAILED) {
  3245. ql4_printk(KERN_INFO, ha, "%s: don't retry "
  3246. "recover adapter. H/W is in Failed "
  3247. "state\n", __func__);
  3248. qla4xxx_dead_adapter_cleanup(ha);
  3249. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  3250. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  3251. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  3252. &ha->dpc_flags);
  3253. status = QLA_ERROR;
  3254. goto exit_recover;
  3255. }
  3256. }
  3257. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  3258. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  3259. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  3260. "(%d) more times\n", ha->host_no,
  3261. ha->retry_reset_ha_cnt));
  3262. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  3263. status = QLA_ERROR;
  3264. } else {
  3265. if (ha->retry_reset_ha_cnt > 0) {
  3266. /* Schedule another Reset HA--DPC will retry */
  3267. ha->retry_reset_ha_cnt--;
  3268. DEBUG2(printk("scsi%ld: recover adapter - "
  3269. "retry remaining %d\n",
  3270. ha->host_no,
  3271. ha->retry_reset_ha_cnt));
  3272. status = QLA_ERROR;
  3273. }
  3274. if (ha->retry_reset_ha_cnt == 0) {
  3275. /* Recover adapter retries have been exhausted.
  3276. * Adapter DEAD */
  3277. DEBUG2(printk("scsi%ld: recover adapter "
  3278. "failed - board disabled\n",
  3279. ha->host_no));
  3280. qla4xxx_dead_adapter_cleanup(ha);
  3281. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  3282. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  3283. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  3284. &ha->dpc_flags);
  3285. status = QLA_ERROR;
  3286. }
  3287. }
  3288. } else {
  3289. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  3290. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  3291. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  3292. }
  3293. exit_recover:
  3294. ha->adapter_error_count++;
  3295. if (test_bit(AF_ONLINE, &ha->flags))
  3296. ha->isp_ops->enable_intrs(ha);
  3297. scsi_unblock_requests(ha->host);
  3298. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  3299. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  3300. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  3301. return status;
  3302. }
  3303. static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
  3304. {
  3305. struct iscsi_session *sess;
  3306. struct ddb_entry *ddb_entry;
  3307. struct scsi_qla_host *ha;
  3308. sess = cls_session->dd_data;
  3309. ddb_entry = sess->dd_data;
  3310. ha = ddb_entry->ha;
  3311. if (!iscsi_is_session_online(cls_session)) {
  3312. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  3313. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  3314. " unblock session\n", ha->host_no, __func__,
  3315. ddb_entry->fw_ddb_index);
  3316. iscsi_unblock_session(ddb_entry->sess);
  3317. } else {
  3318. /* Trigger relogin */
  3319. if (ddb_entry->ddb_type == FLASH_DDB) {
  3320. if (!(test_bit(DF_RELOGIN, &ddb_entry->flags) ||
  3321. test_bit(DF_DISABLE_RELOGIN,
  3322. &ddb_entry->flags)))
  3323. qla4xxx_arm_relogin_timer(ddb_entry);
  3324. } else
  3325. iscsi_session_failure(cls_session->dd_data,
  3326. ISCSI_ERR_CONN_FAILED);
  3327. }
  3328. }
  3329. }
  3330. int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
  3331. {
  3332. struct iscsi_session *sess;
  3333. struct ddb_entry *ddb_entry;
  3334. struct scsi_qla_host *ha;
  3335. sess = cls_session->dd_data;
  3336. ddb_entry = sess->dd_data;
  3337. ha = ddb_entry->ha;
  3338. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  3339. " unblock session\n", ha->host_no, __func__,
  3340. ddb_entry->fw_ddb_index);
  3341. iscsi_unblock_session(ddb_entry->sess);
  3342. /* Start scan target */
  3343. if (test_bit(AF_ONLINE, &ha->flags)) {
  3344. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  3345. " start scan\n", ha->host_no, __func__,
  3346. ddb_entry->fw_ddb_index);
  3347. scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
  3348. }
  3349. return QLA_SUCCESS;
  3350. }
  3351. int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
  3352. {
  3353. struct iscsi_session *sess;
  3354. struct ddb_entry *ddb_entry;
  3355. struct scsi_qla_host *ha;
  3356. int status = QLA_SUCCESS;
  3357. sess = cls_session->dd_data;
  3358. ddb_entry = sess->dd_data;
  3359. ha = ddb_entry->ha;
  3360. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  3361. " unblock user space session\n", ha->host_no, __func__,
  3362. ddb_entry->fw_ddb_index);
  3363. if (!iscsi_is_session_online(cls_session)) {
  3364. iscsi_conn_start(ddb_entry->conn);
  3365. iscsi_conn_login_event(ddb_entry->conn,
  3366. ISCSI_CONN_STATE_LOGGED_IN);
  3367. } else {
  3368. ql4_printk(KERN_INFO, ha,
  3369. "scsi%ld: %s: ddb[%d] session [%d] already logged in\n",
  3370. ha->host_no, __func__, ddb_entry->fw_ddb_index,
  3371. cls_session->sid);
  3372. status = QLA_ERROR;
  3373. }
  3374. return status;
  3375. }
  3376. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  3377. {
  3378. iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
  3379. }
  3380. static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  3381. {
  3382. uint16_t relogin_timer;
  3383. struct iscsi_session *sess;
  3384. struct ddb_entry *ddb_entry;
  3385. struct scsi_qla_host *ha;
  3386. sess = cls_sess->dd_data;
  3387. ddb_entry = sess->dd_data;
  3388. ha = ddb_entry->ha;
  3389. relogin_timer = max(ddb_entry->default_relogin_timeout,
  3390. (uint16_t)RELOGIN_TOV);
  3391. atomic_set(&ddb_entry->relogin_timer, relogin_timer);
  3392. DEBUG2(ql4_printk(KERN_INFO, ha,
  3393. "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
  3394. ddb_entry->fw_ddb_index, relogin_timer));
  3395. qla4xxx_login_flash_ddb(cls_sess);
  3396. }
  3397. static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
  3398. {
  3399. struct iscsi_session *sess;
  3400. struct ddb_entry *ddb_entry;
  3401. struct scsi_qla_host *ha;
  3402. sess = cls_sess->dd_data;
  3403. ddb_entry = sess->dd_data;
  3404. ha = ddb_entry->ha;
  3405. if (!(ddb_entry->ddb_type == FLASH_DDB))
  3406. return;
  3407. if (test_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
  3408. return;
  3409. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  3410. !iscsi_is_session_online(cls_sess)) {
  3411. DEBUG2(ql4_printk(KERN_INFO, ha,
  3412. "relogin issued\n"));
  3413. qla4xxx_relogin_flash_ddb(cls_sess);
  3414. }
  3415. }
  3416. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  3417. {
  3418. if (ha->dpc_thread)
  3419. queue_work(ha->dpc_thread, &ha->dpc_work);
  3420. }
  3421. static struct qla4_work_evt *
  3422. qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
  3423. enum qla4_work_type type)
  3424. {
  3425. struct qla4_work_evt *e;
  3426. uint32_t size = sizeof(struct qla4_work_evt) + data_size;
  3427. e = kzalloc(size, GFP_ATOMIC);
  3428. if (!e)
  3429. return NULL;
  3430. INIT_LIST_HEAD(&e->list);
  3431. e->type = type;
  3432. return e;
  3433. }
  3434. static void qla4xxx_post_work(struct scsi_qla_host *ha,
  3435. struct qla4_work_evt *e)
  3436. {
  3437. unsigned long flags;
  3438. spin_lock_irqsave(&ha->work_lock, flags);
  3439. list_add_tail(&e->list, &ha->work_list);
  3440. spin_unlock_irqrestore(&ha->work_lock, flags);
  3441. qla4xxx_wake_dpc(ha);
  3442. }
  3443. int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
  3444. enum iscsi_host_event_code aen_code,
  3445. uint32_t data_size, uint8_t *data)
  3446. {
  3447. struct qla4_work_evt *e;
  3448. e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
  3449. if (!e)
  3450. return QLA_ERROR;
  3451. e->u.aen.code = aen_code;
  3452. e->u.aen.data_size = data_size;
  3453. memcpy(e->u.aen.data, data, data_size);
  3454. qla4xxx_post_work(ha, e);
  3455. return QLA_SUCCESS;
  3456. }
  3457. int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
  3458. uint32_t status, uint32_t pid,
  3459. uint32_t data_size, uint8_t *data)
  3460. {
  3461. struct qla4_work_evt *e;
  3462. e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
  3463. if (!e)
  3464. return QLA_ERROR;
  3465. e->u.ping.status = status;
  3466. e->u.ping.pid = pid;
  3467. e->u.ping.data_size = data_size;
  3468. memcpy(e->u.ping.data, data, data_size);
  3469. qla4xxx_post_work(ha, e);
  3470. return QLA_SUCCESS;
  3471. }
  3472. static void qla4xxx_do_work(struct scsi_qla_host *ha)
  3473. {
  3474. struct qla4_work_evt *e, *tmp;
  3475. unsigned long flags;
  3476. LIST_HEAD(work);
  3477. spin_lock_irqsave(&ha->work_lock, flags);
  3478. list_splice_init(&ha->work_list, &work);
  3479. spin_unlock_irqrestore(&ha->work_lock, flags);
  3480. list_for_each_entry_safe(e, tmp, &work, list) {
  3481. list_del_init(&e->list);
  3482. switch (e->type) {
  3483. case QLA4_EVENT_AEN:
  3484. iscsi_post_host_event(ha->host_no,
  3485. &qla4xxx_iscsi_transport,
  3486. e->u.aen.code,
  3487. e->u.aen.data_size,
  3488. e->u.aen.data);
  3489. break;
  3490. case QLA4_EVENT_PING_STATUS:
  3491. iscsi_ping_comp_event(ha->host_no,
  3492. &qla4xxx_iscsi_transport,
  3493. e->u.ping.status,
  3494. e->u.ping.pid,
  3495. e->u.ping.data_size,
  3496. e->u.ping.data);
  3497. break;
  3498. default:
  3499. ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
  3500. "supported", e->type);
  3501. }
  3502. kfree(e);
  3503. }
  3504. }
  3505. /**
  3506. * qla4xxx_do_dpc - dpc routine
  3507. * @data: in our case pointer to adapter structure
  3508. *
  3509. * This routine is a task that is schedule by the interrupt handler
  3510. * to perform the background processing for interrupts. We put it
  3511. * on a task queue that is consumed whenever the scheduler runs; that's
  3512. * so you can do anything (i.e. put the process to sleep etc). In fact,
  3513. * the mid-level tries to sleep when it reaches the driver threshold
  3514. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  3515. **/
  3516. static void qla4xxx_do_dpc(struct work_struct *work)
  3517. {
  3518. struct scsi_qla_host *ha =
  3519. container_of(work, struct scsi_qla_host, dpc_work);
  3520. int status = QLA_ERROR;
  3521. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  3522. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  3523. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  3524. /* Initialization not yet finished. Don't do anything yet. */
  3525. if (!test_bit(AF_INIT_DONE, &ha->flags))
  3526. return;
  3527. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  3528. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  3529. ha->host_no, __func__, ha->flags));
  3530. return;
  3531. }
  3532. /* post events to application */
  3533. qla4xxx_do_work(ha);
  3534. if (is_qla80XX(ha)) {
  3535. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  3536. if (is_qla8032(ha) || is_qla8042(ha)) {
  3537. ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
  3538. __func__);
  3539. /* disable pause frame for ISP83xx */
  3540. qla4_83xx_disable_pause(ha);
  3541. }
  3542. ha->isp_ops->idc_lock(ha);
  3543. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  3544. QLA8XXX_DEV_FAILED);
  3545. ha->isp_ops->idc_unlock(ha);
  3546. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  3547. qla4_8xxx_device_state_handler(ha);
  3548. }
  3549. if (test_bit(DPC_POST_IDC_ACK, &ha->dpc_flags)) {
  3550. if (is_qla8042(ha)) {
  3551. if (ha->idc_info.info2 &
  3552. ENABLE_INTERNAL_LOOPBACK) {
  3553. ql4_printk(KERN_INFO, ha, "%s: Disabling ACB\n",
  3554. __func__);
  3555. status = qla4_84xx_config_acb(ha,
  3556. ACB_CONFIG_DISABLE);
  3557. if (status != QLA_SUCCESS) {
  3558. ql4_printk(KERN_INFO, ha, "%s: ACB config failed\n",
  3559. __func__);
  3560. }
  3561. }
  3562. }
  3563. qla4_83xx_post_idc_ack(ha);
  3564. clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags);
  3565. }
  3566. if (is_qla8042(ha) &&
  3567. test_bit(DPC_RESTORE_ACB, &ha->dpc_flags)) {
  3568. ql4_printk(KERN_INFO, ha, "%s: Restoring ACB\n",
  3569. __func__);
  3570. if (qla4_84xx_config_acb(ha, ACB_CONFIG_SET) !=
  3571. QLA_SUCCESS) {
  3572. ql4_printk(KERN_INFO, ha, "%s: ACB config failed ",
  3573. __func__);
  3574. }
  3575. clear_bit(DPC_RESTORE_ACB, &ha->dpc_flags);
  3576. }
  3577. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  3578. qla4_8xxx_need_qsnt_handler(ha);
  3579. }
  3580. }
  3581. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  3582. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  3583. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  3584. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  3585. if ((is_qla8022(ha) && ql4xdontresethba) ||
  3586. ((is_qla8032(ha) || is_qla8042(ha)) &&
  3587. qla4_83xx_idc_dontreset(ha))) {
  3588. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  3589. ha->host_no, __func__));
  3590. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  3591. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  3592. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  3593. goto dpc_post_reset_ha;
  3594. }
  3595. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  3596. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  3597. qla4xxx_recover_adapter(ha);
  3598. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  3599. uint8_t wait_time = RESET_INTR_TOV;
  3600. while ((readw(&ha->reg->ctrl_status) &
  3601. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  3602. if (--wait_time == 0)
  3603. break;
  3604. msleep(1000);
  3605. }
  3606. if (wait_time == 0)
  3607. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  3608. "bit not cleared-- resetting\n",
  3609. ha->host_no, __func__));
  3610. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  3611. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  3612. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  3613. status = qla4xxx_recover_adapter(ha);
  3614. }
  3615. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  3616. if (status == QLA_SUCCESS)
  3617. ha->isp_ops->enable_intrs(ha);
  3618. }
  3619. }
  3620. dpc_post_reset_ha:
  3621. /* ---- process AEN? --- */
  3622. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  3623. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  3624. /* ---- Get DHCP IP Address? --- */
  3625. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  3626. qla4xxx_get_dhcp_ip_address(ha);
  3627. /* ---- relogin device? --- */
  3628. if (adapter_up(ha) &&
  3629. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  3630. iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
  3631. }
  3632. /* ---- link change? --- */
  3633. if (!test_bit(AF_LOOPBACK, &ha->flags) &&
  3634. test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  3635. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  3636. /* ---- link down? --- */
  3637. qla4xxx_mark_all_devices_missing(ha);
  3638. } else {
  3639. /* ---- link up? --- *
  3640. * F/W will auto login to all devices ONLY ONCE after
  3641. * link up during driver initialization and runtime
  3642. * fatal error recovery. Therefore, the driver must
  3643. * manually relogin to devices when recovering from
  3644. * connection failures, logouts, expired KATO, etc. */
  3645. if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
  3646. qla4xxx_build_ddb_list(ha, ha->is_reset);
  3647. iscsi_host_for_each_session(ha->host,
  3648. qla4xxx_login_flash_ddb);
  3649. } else
  3650. qla4xxx_relogin_all_devices(ha);
  3651. }
  3652. }
  3653. }
  3654. /**
  3655. * qla4xxx_free_adapter - release the adapter
  3656. * @ha: pointer to adapter structure
  3657. **/
  3658. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  3659. {
  3660. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  3661. /* Turn-off interrupts on the card. */
  3662. ha->isp_ops->disable_intrs(ha);
  3663. if (is_qla40XX(ha)) {
  3664. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  3665. &ha->reg->ctrl_status);
  3666. readl(&ha->reg->ctrl_status);
  3667. } else if (is_qla8022(ha)) {
  3668. writel(0, &ha->qla4_82xx_reg->host_int);
  3669. readl(&ha->qla4_82xx_reg->host_int);
  3670. } else if (is_qla8032(ha) || is_qla8042(ha)) {
  3671. writel(0, &ha->qla4_83xx_reg->risc_intr);
  3672. readl(&ha->qla4_83xx_reg->risc_intr);
  3673. }
  3674. /* Remove timer thread, if present */
  3675. if (ha->timer_active)
  3676. qla4xxx_stop_timer(ha);
  3677. /* Kill the kernel thread for this host */
  3678. if (ha->dpc_thread)
  3679. destroy_workqueue(ha->dpc_thread);
  3680. /* Kill the kernel thread for this host */
  3681. if (ha->task_wq)
  3682. destroy_workqueue(ha->task_wq);
  3683. /* Put firmware in known state */
  3684. ha->isp_ops->reset_firmware(ha);
  3685. if (is_qla80XX(ha)) {
  3686. ha->isp_ops->idc_lock(ha);
  3687. qla4_8xxx_clear_drv_active(ha);
  3688. ha->isp_ops->idc_unlock(ha);
  3689. }
  3690. /* Detach interrupts */
  3691. qla4xxx_free_irqs(ha);
  3692. /* free extra memory */
  3693. qla4xxx_mem_free(ha);
  3694. }
  3695. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  3696. {
  3697. int status = 0;
  3698. unsigned long mem_base, mem_len, db_base, db_len;
  3699. struct pci_dev *pdev = ha->pdev;
  3700. status = pci_request_regions(pdev, DRIVER_NAME);
  3701. if (status) {
  3702. printk(KERN_WARNING
  3703. "scsi(%ld) Failed to reserve PIO regions (%s) "
  3704. "status=%d\n", ha->host_no, pci_name(pdev), status);
  3705. goto iospace_error_exit;
  3706. }
  3707. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  3708. __func__, pdev->revision));
  3709. ha->revision_id = pdev->revision;
  3710. /* remap phys address */
  3711. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  3712. mem_len = pci_resource_len(pdev, 0);
  3713. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  3714. __func__, mem_base, mem_len));
  3715. /* mapping of pcibase pointer */
  3716. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  3717. if (!ha->nx_pcibase) {
  3718. printk(KERN_ERR
  3719. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  3720. pci_release_regions(ha->pdev);
  3721. goto iospace_error_exit;
  3722. }
  3723. /* Mapping of IO base pointer, door bell read and write pointer */
  3724. /* mapping of IO base pointer */
  3725. if (is_qla8022(ha)) {
  3726. ha->qla4_82xx_reg = (struct device_reg_82xx __iomem *)
  3727. ((uint8_t *)ha->nx_pcibase + 0xbc000 +
  3728. (ha->pdev->devfn << 11));
  3729. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  3730. QLA82XX_CAM_RAM_DB2);
  3731. } else if (is_qla8032(ha) || is_qla8042(ha)) {
  3732. ha->qla4_83xx_reg = (struct device_reg_83xx __iomem *)
  3733. ((uint8_t *)ha->nx_pcibase);
  3734. }
  3735. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  3736. db_len = pci_resource_len(pdev, 4);
  3737. return 0;
  3738. iospace_error_exit:
  3739. return -ENOMEM;
  3740. }
  3741. /***
  3742. * qla4xxx_iospace_config - maps registers
  3743. * @ha: pointer to adapter structure
  3744. *
  3745. * This routines maps HBA's registers from the pci address space
  3746. * into the kernel virtual address space for memory mapped i/o.
  3747. **/
  3748. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  3749. {
  3750. unsigned long pio, pio_len, pio_flags;
  3751. unsigned long mmio, mmio_len, mmio_flags;
  3752. pio = pci_resource_start(ha->pdev, 0);
  3753. pio_len = pci_resource_len(ha->pdev, 0);
  3754. pio_flags = pci_resource_flags(ha->pdev, 0);
  3755. if (pio_flags & IORESOURCE_IO) {
  3756. if (pio_len < MIN_IOBASE_LEN) {
  3757. ql4_printk(KERN_WARNING, ha,
  3758. "Invalid PCI I/O region size\n");
  3759. pio = 0;
  3760. }
  3761. } else {
  3762. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  3763. pio = 0;
  3764. }
  3765. /* Use MMIO operations for all accesses. */
  3766. mmio = pci_resource_start(ha->pdev, 1);
  3767. mmio_len = pci_resource_len(ha->pdev, 1);
  3768. mmio_flags = pci_resource_flags(ha->pdev, 1);
  3769. if (!(mmio_flags & IORESOURCE_MEM)) {
  3770. ql4_printk(KERN_ERR, ha,
  3771. "region #0 not an MMIO resource, aborting\n");
  3772. goto iospace_error_exit;
  3773. }
  3774. if (mmio_len < MIN_IOBASE_LEN) {
  3775. ql4_printk(KERN_ERR, ha,
  3776. "Invalid PCI mem region size, aborting\n");
  3777. goto iospace_error_exit;
  3778. }
  3779. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  3780. ql4_printk(KERN_WARNING, ha,
  3781. "Failed to reserve PIO/MMIO regions\n");
  3782. goto iospace_error_exit;
  3783. }
  3784. ha->pio_address = pio;
  3785. ha->pio_length = pio_len;
  3786. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  3787. if (!ha->reg) {
  3788. ql4_printk(KERN_ERR, ha,
  3789. "cannot remap MMIO, aborting\n");
  3790. goto iospace_error_exit;
  3791. }
  3792. return 0;
  3793. iospace_error_exit:
  3794. return -ENOMEM;
  3795. }
  3796. static struct isp_operations qla4xxx_isp_ops = {
  3797. .iospace_config = qla4xxx_iospace_config,
  3798. .pci_config = qla4xxx_pci_config,
  3799. .disable_intrs = qla4xxx_disable_intrs,
  3800. .enable_intrs = qla4xxx_enable_intrs,
  3801. .start_firmware = qla4xxx_start_firmware,
  3802. .intr_handler = qla4xxx_intr_handler,
  3803. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  3804. .reset_chip = qla4xxx_soft_reset,
  3805. .reset_firmware = qla4xxx_hw_reset,
  3806. .queue_iocb = qla4xxx_queue_iocb,
  3807. .complete_iocb = qla4xxx_complete_iocb,
  3808. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  3809. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  3810. .get_sys_info = qla4xxx_get_sys_info,
  3811. .queue_mailbox_command = qla4xxx_queue_mbox_cmd,
  3812. .process_mailbox_interrupt = qla4xxx_process_mbox_intr,
  3813. };
  3814. static struct isp_operations qla4_82xx_isp_ops = {
  3815. .iospace_config = qla4_8xxx_iospace_config,
  3816. .pci_config = qla4_8xxx_pci_config,
  3817. .disable_intrs = qla4_82xx_disable_intrs,
  3818. .enable_intrs = qla4_82xx_enable_intrs,
  3819. .start_firmware = qla4_8xxx_load_risc,
  3820. .restart_firmware = qla4_82xx_try_start_fw,
  3821. .intr_handler = qla4_82xx_intr_handler,
  3822. .interrupt_service_routine = qla4_82xx_interrupt_service_routine,
  3823. .need_reset = qla4_8xxx_need_reset,
  3824. .reset_chip = qla4_82xx_isp_reset,
  3825. .reset_firmware = qla4_8xxx_stop_firmware,
  3826. .queue_iocb = qla4_82xx_queue_iocb,
  3827. .complete_iocb = qla4_82xx_complete_iocb,
  3828. .rd_shdw_req_q_out = qla4_82xx_rd_shdw_req_q_out,
  3829. .rd_shdw_rsp_q_in = qla4_82xx_rd_shdw_rsp_q_in,
  3830. .get_sys_info = qla4_8xxx_get_sys_info,
  3831. .rd_reg_direct = qla4_82xx_rd_32,
  3832. .wr_reg_direct = qla4_82xx_wr_32,
  3833. .rd_reg_indirect = qla4_82xx_md_rd_32,
  3834. .wr_reg_indirect = qla4_82xx_md_wr_32,
  3835. .idc_lock = qla4_82xx_idc_lock,
  3836. .idc_unlock = qla4_82xx_idc_unlock,
  3837. .rom_lock_recovery = qla4_82xx_rom_lock_recovery,
  3838. .queue_mailbox_command = qla4_82xx_queue_mbox_cmd,
  3839. .process_mailbox_interrupt = qla4_82xx_process_mbox_intr,
  3840. };
  3841. static struct isp_operations qla4_83xx_isp_ops = {
  3842. .iospace_config = qla4_8xxx_iospace_config,
  3843. .pci_config = qla4_8xxx_pci_config,
  3844. .disable_intrs = qla4_83xx_disable_intrs,
  3845. .enable_intrs = qla4_83xx_enable_intrs,
  3846. .start_firmware = qla4_8xxx_load_risc,
  3847. .restart_firmware = qla4_83xx_start_firmware,
  3848. .intr_handler = qla4_83xx_intr_handler,
  3849. .interrupt_service_routine = qla4_83xx_interrupt_service_routine,
  3850. .need_reset = qla4_8xxx_need_reset,
  3851. .reset_chip = qla4_83xx_isp_reset,
  3852. .reset_firmware = qla4_8xxx_stop_firmware,
  3853. .queue_iocb = qla4_83xx_queue_iocb,
  3854. .complete_iocb = qla4_83xx_complete_iocb,
  3855. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  3856. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  3857. .get_sys_info = qla4_8xxx_get_sys_info,
  3858. .rd_reg_direct = qla4_83xx_rd_reg,
  3859. .wr_reg_direct = qla4_83xx_wr_reg,
  3860. .rd_reg_indirect = qla4_83xx_rd_reg_indirect,
  3861. .wr_reg_indirect = qla4_83xx_wr_reg_indirect,
  3862. .idc_lock = qla4_83xx_drv_lock,
  3863. .idc_unlock = qla4_83xx_drv_unlock,
  3864. .rom_lock_recovery = qla4_83xx_rom_lock_recovery,
  3865. .queue_mailbox_command = qla4_83xx_queue_mbox_cmd,
  3866. .process_mailbox_interrupt = qla4_83xx_process_mbox_intr,
  3867. };
  3868. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  3869. {
  3870. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  3871. }
  3872. uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  3873. {
  3874. return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
  3875. }
  3876. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  3877. {
  3878. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  3879. }
  3880. uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  3881. {
  3882. return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
  3883. }
  3884. static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
  3885. {
  3886. struct scsi_qla_host *ha = data;
  3887. char *str = buf;
  3888. int rc;
  3889. switch (type) {
  3890. case ISCSI_BOOT_ETH_FLAGS:
  3891. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  3892. break;
  3893. case ISCSI_BOOT_ETH_INDEX:
  3894. rc = sprintf(str, "0\n");
  3895. break;
  3896. case ISCSI_BOOT_ETH_MAC:
  3897. rc = sysfs_format_mac(str, ha->my_mac,
  3898. MAC_ADDR_LEN);
  3899. break;
  3900. default:
  3901. rc = -ENOSYS;
  3902. break;
  3903. }
  3904. return rc;
  3905. }
  3906. static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
  3907. {
  3908. int rc;
  3909. switch (type) {
  3910. case ISCSI_BOOT_ETH_FLAGS:
  3911. case ISCSI_BOOT_ETH_MAC:
  3912. case ISCSI_BOOT_ETH_INDEX:
  3913. rc = S_IRUGO;
  3914. break;
  3915. default:
  3916. rc = 0;
  3917. break;
  3918. }
  3919. return rc;
  3920. }
  3921. static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
  3922. {
  3923. struct scsi_qla_host *ha = data;
  3924. char *str = buf;
  3925. int rc;
  3926. switch (type) {
  3927. case ISCSI_BOOT_INI_INITIATOR_NAME:
  3928. rc = sprintf(str, "%s\n", ha->name_string);
  3929. break;
  3930. default:
  3931. rc = -ENOSYS;
  3932. break;
  3933. }
  3934. return rc;
  3935. }
  3936. static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
  3937. {
  3938. int rc;
  3939. switch (type) {
  3940. case ISCSI_BOOT_INI_INITIATOR_NAME:
  3941. rc = S_IRUGO;
  3942. break;
  3943. default:
  3944. rc = 0;
  3945. break;
  3946. }
  3947. return rc;
  3948. }
  3949. static ssize_t
  3950. qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
  3951. char *buf)
  3952. {
  3953. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  3954. char *str = buf;
  3955. int rc;
  3956. switch (type) {
  3957. case ISCSI_BOOT_TGT_NAME:
  3958. rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
  3959. break;
  3960. case ISCSI_BOOT_TGT_IP_ADDR:
  3961. if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
  3962. rc = sprintf(buf, "%pI4\n",
  3963. &boot_conn->dest_ipaddr.ip_address);
  3964. else
  3965. rc = sprintf(str, "%pI6\n",
  3966. &boot_conn->dest_ipaddr.ip_address);
  3967. break;
  3968. case ISCSI_BOOT_TGT_PORT:
  3969. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  3970. break;
  3971. case ISCSI_BOOT_TGT_CHAP_NAME:
  3972. rc = sprintf(str, "%.*s\n",
  3973. boot_conn->chap.target_chap_name_length,
  3974. (char *)&boot_conn->chap.target_chap_name);
  3975. break;
  3976. case ISCSI_BOOT_TGT_CHAP_SECRET:
  3977. rc = sprintf(str, "%.*s\n",
  3978. boot_conn->chap.target_secret_length,
  3979. (char *)&boot_conn->chap.target_secret);
  3980. break;
  3981. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  3982. rc = sprintf(str, "%.*s\n",
  3983. boot_conn->chap.intr_chap_name_length,
  3984. (char *)&boot_conn->chap.intr_chap_name);
  3985. break;
  3986. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  3987. rc = sprintf(str, "%.*s\n",
  3988. boot_conn->chap.intr_secret_length,
  3989. (char *)&boot_conn->chap.intr_secret);
  3990. break;
  3991. case ISCSI_BOOT_TGT_FLAGS:
  3992. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  3993. break;
  3994. case ISCSI_BOOT_TGT_NIC_ASSOC:
  3995. rc = sprintf(str, "0\n");
  3996. break;
  3997. default:
  3998. rc = -ENOSYS;
  3999. break;
  4000. }
  4001. return rc;
  4002. }
  4003. static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
  4004. {
  4005. struct scsi_qla_host *ha = data;
  4006. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
  4007. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  4008. }
  4009. static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
  4010. {
  4011. struct scsi_qla_host *ha = data;
  4012. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
  4013. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  4014. }
  4015. static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
  4016. {
  4017. int rc;
  4018. switch (type) {
  4019. case ISCSI_BOOT_TGT_NAME:
  4020. case ISCSI_BOOT_TGT_IP_ADDR:
  4021. case ISCSI_BOOT_TGT_PORT:
  4022. case ISCSI_BOOT_TGT_CHAP_NAME:
  4023. case ISCSI_BOOT_TGT_CHAP_SECRET:
  4024. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  4025. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  4026. case ISCSI_BOOT_TGT_NIC_ASSOC:
  4027. case ISCSI_BOOT_TGT_FLAGS:
  4028. rc = S_IRUGO;
  4029. break;
  4030. default:
  4031. rc = 0;
  4032. break;
  4033. }
  4034. return rc;
  4035. }
  4036. static void qla4xxx_boot_release(void *data)
  4037. {
  4038. struct scsi_qla_host *ha = data;
  4039. scsi_host_put(ha->host);
  4040. }
  4041. static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
  4042. {
  4043. dma_addr_t buf_dma;
  4044. uint32_t addr, pri_addr, sec_addr;
  4045. uint32_t offset;
  4046. uint16_t func_num;
  4047. uint8_t val;
  4048. uint8_t *buf = NULL;
  4049. size_t size = 13 * sizeof(uint8_t);
  4050. int ret = QLA_SUCCESS;
  4051. func_num = PCI_FUNC(ha->pdev->devfn);
  4052. ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
  4053. __func__, ha->pdev->device, func_num);
  4054. if (is_qla40XX(ha)) {
  4055. if (func_num == 1) {
  4056. addr = NVRAM_PORT0_BOOT_MODE;
  4057. pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
  4058. sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
  4059. } else if (func_num == 3) {
  4060. addr = NVRAM_PORT1_BOOT_MODE;
  4061. pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
  4062. sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
  4063. } else {
  4064. ret = QLA_ERROR;
  4065. goto exit_boot_info;
  4066. }
  4067. /* Check Boot Mode */
  4068. val = rd_nvram_byte(ha, addr);
  4069. if (!(val & 0x07)) {
  4070. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
  4071. "options : 0x%x\n", __func__, val));
  4072. ret = QLA_ERROR;
  4073. goto exit_boot_info;
  4074. }
  4075. /* get primary valid target index */
  4076. val = rd_nvram_byte(ha, pri_addr);
  4077. if (val & BIT_7)
  4078. ddb_index[0] = (val & 0x7f);
  4079. /* get secondary valid target index */
  4080. val = rd_nvram_byte(ha, sec_addr);
  4081. if (val & BIT_7)
  4082. ddb_index[1] = (val & 0x7f);
  4083. } else if (is_qla80XX(ha)) {
  4084. buf = dma_alloc_coherent(&ha->pdev->dev, size,
  4085. &buf_dma, GFP_KERNEL);
  4086. if (!buf) {
  4087. DEBUG2(ql4_printk(KERN_ERR, ha,
  4088. "%s: Unable to allocate dma buffer\n",
  4089. __func__));
  4090. ret = QLA_ERROR;
  4091. goto exit_boot_info;
  4092. }
  4093. if (ha->port_num == 0)
  4094. offset = BOOT_PARAM_OFFSET_PORT0;
  4095. else if (ha->port_num == 1)
  4096. offset = BOOT_PARAM_OFFSET_PORT1;
  4097. else {
  4098. ret = QLA_ERROR;
  4099. goto exit_boot_info_free;
  4100. }
  4101. addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
  4102. offset;
  4103. if (qla4xxx_get_flash(ha, buf_dma, addr,
  4104. 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
  4105. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
  4106. " failed\n", ha->host_no, __func__));
  4107. ret = QLA_ERROR;
  4108. goto exit_boot_info_free;
  4109. }
  4110. /* Check Boot Mode */
  4111. if (!(buf[1] & 0x07)) {
  4112. DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
  4113. " : 0x%x\n", buf[1]));
  4114. ret = QLA_ERROR;
  4115. goto exit_boot_info_free;
  4116. }
  4117. /* get primary valid target index */
  4118. if (buf[2] & BIT_7)
  4119. ddb_index[0] = buf[2] & 0x7f;
  4120. /* get secondary valid target index */
  4121. if (buf[11] & BIT_7)
  4122. ddb_index[1] = buf[11] & 0x7f;
  4123. } else {
  4124. ret = QLA_ERROR;
  4125. goto exit_boot_info;
  4126. }
  4127. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
  4128. " target ID %d\n", __func__, ddb_index[0],
  4129. ddb_index[1]));
  4130. exit_boot_info_free:
  4131. dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
  4132. exit_boot_info:
  4133. ha->pri_ddb_idx = ddb_index[0];
  4134. ha->sec_ddb_idx = ddb_index[1];
  4135. return ret;
  4136. }
  4137. /**
  4138. * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
  4139. * @ha: pointer to adapter structure
  4140. * @username: CHAP username to be returned
  4141. * @password: CHAP password to be returned
  4142. *
  4143. * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
  4144. * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
  4145. * So from the CHAP cache find the first BIDI CHAP entry and set it
  4146. * to the boot record in sysfs.
  4147. **/
  4148. static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
  4149. char *password)
  4150. {
  4151. int i, ret = -EINVAL;
  4152. int max_chap_entries = 0;
  4153. struct ql4_chap_table *chap_table;
  4154. if (is_qla80XX(ha))
  4155. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  4156. sizeof(struct ql4_chap_table);
  4157. else
  4158. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  4159. if (!ha->chap_list) {
  4160. ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
  4161. return ret;
  4162. }
  4163. mutex_lock(&ha->chap_sem);
  4164. for (i = 0; i < max_chap_entries; i++) {
  4165. chap_table = (struct ql4_chap_table *)ha->chap_list + i;
  4166. if (chap_table->cookie !=
  4167. __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
  4168. continue;
  4169. }
  4170. if (chap_table->flags & BIT_7) /* local */
  4171. continue;
  4172. if (!(chap_table->flags & BIT_6)) /* Not BIDI */
  4173. continue;
  4174. strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
  4175. strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
  4176. ret = 0;
  4177. break;
  4178. }
  4179. mutex_unlock(&ha->chap_sem);
  4180. return ret;
  4181. }
  4182. static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
  4183. struct ql4_boot_session_info *boot_sess,
  4184. uint16_t ddb_index)
  4185. {
  4186. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  4187. struct dev_db_entry *fw_ddb_entry;
  4188. dma_addr_t fw_ddb_entry_dma;
  4189. uint16_t idx;
  4190. uint16_t options;
  4191. int ret = QLA_SUCCESS;
  4192. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  4193. &fw_ddb_entry_dma, GFP_KERNEL);
  4194. if (!fw_ddb_entry) {
  4195. DEBUG2(ql4_printk(KERN_ERR, ha,
  4196. "%s: Unable to allocate dma buffer.\n",
  4197. __func__));
  4198. ret = QLA_ERROR;
  4199. return ret;
  4200. }
  4201. if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
  4202. fw_ddb_entry_dma, ddb_index)) {
  4203. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
  4204. "index [%d]\n", __func__, ddb_index));
  4205. ret = QLA_ERROR;
  4206. goto exit_boot_target;
  4207. }
  4208. /* Update target name and IP from DDB */
  4209. memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
  4210. min(sizeof(boot_sess->target_name),
  4211. sizeof(fw_ddb_entry->iscsi_name)));
  4212. options = le16_to_cpu(fw_ddb_entry->options);
  4213. if (options & DDB_OPT_IPV6_DEVICE) {
  4214. memcpy(&boot_conn->dest_ipaddr.ip_address,
  4215. &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
  4216. } else {
  4217. boot_conn->dest_ipaddr.ip_type = 0x1;
  4218. memcpy(&boot_conn->dest_ipaddr.ip_address,
  4219. &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
  4220. }
  4221. boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
  4222. /* update chap information */
  4223. idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  4224. if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  4225. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
  4226. ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
  4227. target_chap_name,
  4228. (char *)&boot_conn->chap.target_secret,
  4229. idx);
  4230. if (ret) {
  4231. ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
  4232. ret = QLA_ERROR;
  4233. goto exit_boot_target;
  4234. }
  4235. boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  4236. boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  4237. }
  4238. if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  4239. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
  4240. ret = qla4xxx_get_bidi_chap(ha,
  4241. (char *)&boot_conn->chap.intr_chap_name,
  4242. (char *)&boot_conn->chap.intr_secret);
  4243. if (ret) {
  4244. ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
  4245. ret = QLA_ERROR;
  4246. goto exit_boot_target;
  4247. }
  4248. boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  4249. boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  4250. }
  4251. exit_boot_target:
  4252. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  4253. fw_ddb_entry, fw_ddb_entry_dma);
  4254. return ret;
  4255. }
  4256. static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
  4257. {
  4258. uint16_t ddb_index[2];
  4259. int ret = QLA_ERROR;
  4260. int rval;
  4261. memset(ddb_index, 0, sizeof(ddb_index));
  4262. ddb_index[0] = 0xffff;
  4263. ddb_index[1] = 0xffff;
  4264. ret = get_fw_boot_info(ha, ddb_index);
  4265. if (ret != QLA_SUCCESS) {
  4266. DEBUG2(ql4_printk(KERN_INFO, ha,
  4267. "%s: No boot target configured.\n", __func__));
  4268. return ret;
  4269. }
  4270. if (ql4xdisablesysfsboot)
  4271. return QLA_SUCCESS;
  4272. if (ddb_index[0] == 0xffff)
  4273. goto sec_target;
  4274. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
  4275. ddb_index[0]);
  4276. if (rval != QLA_SUCCESS) {
  4277. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
  4278. "configured\n", __func__));
  4279. } else
  4280. ret = QLA_SUCCESS;
  4281. sec_target:
  4282. if (ddb_index[1] == 0xffff)
  4283. goto exit_get_boot_info;
  4284. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
  4285. ddb_index[1]);
  4286. if (rval != QLA_SUCCESS) {
  4287. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
  4288. " configured\n", __func__));
  4289. } else
  4290. ret = QLA_SUCCESS;
  4291. exit_get_boot_info:
  4292. return ret;
  4293. }
  4294. static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
  4295. {
  4296. struct iscsi_boot_kobj *boot_kobj;
  4297. if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
  4298. return QLA_ERROR;
  4299. if (ql4xdisablesysfsboot) {
  4300. ql4_printk(KERN_INFO, ha,
  4301. "%s: syfsboot disabled - driver will trigger login "
  4302. "and publish session for discovery .\n", __func__);
  4303. return QLA_SUCCESS;
  4304. }
  4305. ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
  4306. if (!ha->boot_kset)
  4307. goto kset_free;
  4308. if (!scsi_host_get(ha->host))
  4309. goto kset_free;
  4310. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
  4311. qla4xxx_show_boot_tgt_pri_info,
  4312. qla4xxx_tgt_get_attr_visibility,
  4313. qla4xxx_boot_release);
  4314. if (!boot_kobj)
  4315. goto put_host;
  4316. if (!scsi_host_get(ha->host))
  4317. goto kset_free;
  4318. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
  4319. qla4xxx_show_boot_tgt_sec_info,
  4320. qla4xxx_tgt_get_attr_visibility,
  4321. qla4xxx_boot_release);
  4322. if (!boot_kobj)
  4323. goto put_host;
  4324. if (!scsi_host_get(ha->host))
  4325. goto kset_free;
  4326. boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
  4327. qla4xxx_show_boot_ini_info,
  4328. qla4xxx_ini_get_attr_visibility,
  4329. qla4xxx_boot_release);
  4330. if (!boot_kobj)
  4331. goto put_host;
  4332. if (!scsi_host_get(ha->host))
  4333. goto kset_free;
  4334. boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
  4335. qla4xxx_show_boot_eth_info,
  4336. qla4xxx_eth_get_attr_visibility,
  4337. qla4xxx_boot_release);
  4338. if (!boot_kobj)
  4339. goto put_host;
  4340. return QLA_SUCCESS;
  4341. put_host:
  4342. scsi_host_put(ha->host);
  4343. kset_free:
  4344. iscsi_boot_destroy_kset(ha->boot_kset);
  4345. return -ENOMEM;
  4346. }
  4347. /**
  4348. * qla4xxx_create chap_list - Create CHAP list from FLASH
  4349. * @ha: pointer to adapter structure
  4350. *
  4351. * Read flash and make a list of CHAP entries, during login when a CHAP entry
  4352. * is received, it will be checked in this list. If entry exist then the CHAP
  4353. * entry index is set in the DDB. If CHAP entry does not exist in this list
  4354. * then a new entry is added in FLASH in CHAP table and the index obtained is
  4355. * used in the DDB.
  4356. **/
  4357. static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
  4358. {
  4359. int rval = 0;
  4360. uint8_t *chap_flash_data = NULL;
  4361. uint32_t offset;
  4362. dma_addr_t chap_dma;
  4363. uint32_t chap_size = 0;
  4364. if (is_qla40XX(ha))
  4365. chap_size = MAX_CHAP_ENTRIES_40XX *
  4366. sizeof(struct ql4_chap_table);
  4367. else /* Single region contains CHAP info for both
  4368. * ports which is divided into half for each port.
  4369. */
  4370. chap_size = ha->hw.flt_chap_size / 2;
  4371. chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
  4372. &chap_dma, GFP_KERNEL);
  4373. if (!chap_flash_data) {
  4374. ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
  4375. return;
  4376. }
  4377. if (is_qla40XX(ha))
  4378. offset = FLASH_CHAP_OFFSET;
  4379. else {
  4380. offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
  4381. if (ha->port_num == 1)
  4382. offset += chap_size;
  4383. }
  4384. rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
  4385. if (rval != QLA_SUCCESS)
  4386. goto exit_chap_list;
  4387. if (ha->chap_list == NULL)
  4388. ha->chap_list = vmalloc(chap_size);
  4389. if (ha->chap_list == NULL) {
  4390. ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
  4391. goto exit_chap_list;
  4392. }
  4393. memcpy(ha->chap_list, chap_flash_data, chap_size);
  4394. exit_chap_list:
  4395. dma_free_coherent(&ha->pdev->dev, chap_size,
  4396. chap_flash_data, chap_dma);
  4397. }
  4398. static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
  4399. struct ql4_tuple_ddb *tddb)
  4400. {
  4401. struct scsi_qla_host *ha;
  4402. struct iscsi_cls_session *cls_sess;
  4403. struct iscsi_cls_conn *cls_conn;
  4404. struct iscsi_session *sess;
  4405. struct iscsi_conn *conn;
  4406. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  4407. ha = ddb_entry->ha;
  4408. cls_sess = ddb_entry->sess;
  4409. sess = cls_sess->dd_data;
  4410. cls_conn = ddb_entry->conn;
  4411. conn = cls_conn->dd_data;
  4412. tddb->tpgt = sess->tpgt;
  4413. tddb->port = conn->persistent_port;
  4414. strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
  4415. strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
  4416. }
  4417. static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
  4418. struct ql4_tuple_ddb *tddb,
  4419. uint8_t *flash_isid)
  4420. {
  4421. uint16_t options = 0;
  4422. tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  4423. memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
  4424. min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
  4425. options = le16_to_cpu(fw_ddb_entry->options);
  4426. if (options & DDB_OPT_IPV6_DEVICE)
  4427. sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  4428. else
  4429. sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  4430. tddb->port = le16_to_cpu(fw_ddb_entry->port);
  4431. if (flash_isid == NULL)
  4432. memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0],
  4433. sizeof(tddb->isid));
  4434. else
  4435. memcpy(&tddb->isid[0], &flash_isid[0], sizeof(tddb->isid));
  4436. }
  4437. static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
  4438. struct ql4_tuple_ddb *old_tddb,
  4439. struct ql4_tuple_ddb *new_tddb,
  4440. uint8_t is_isid_compare)
  4441. {
  4442. if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
  4443. return QLA_ERROR;
  4444. if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
  4445. return QLA_ERROR;
  4446. if (old_tddb->port != new_tddb->port)
  4447. return QLA_ERROR;
  4448. /* For multi sessions, driver generates the ISID, so do not compare
  4449. * ISID in reset path since it would be a comparison between the
  4450. * driver generated ISID and firmware generated ISID. This could
  4451. * lead to adding duplicated DDBs in the list as driver generated
  4452. * ISID would not match firmware generated ISID.
  4453. */
  4454. if (is_isid_compare) {
  4455. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: old ISID [%02x%02x%02x"
  4456. "%02x%02x%02x] New ISID [%02x%02x%02x%02x%02x%02x]\n",
  4457. __func__, old_tddb->isid[5], old_tddb->isid[4],
  4458. old_tddb->isid[3], old_tddb->isid[2], old_tddb->isid[1],
  4459. old_tddb->isid[0], new_tddb->isid[5], new_tddb->isid[4],
  4460. new_tddb->isid[3], new_tddb->isid[2], new_tddb->isid[1],
  4461. new_tddb->isid[0]));
  4462. if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
  4463. sizeof(old_tddb->isid)))
  4464. return QLA_ERROR;
  4465. }
  4466. DEBUG2(ql4_printk(KERN_INFO, ha,
  4467. "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
  4468. old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
  4469. old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
  4470. new_tddb->ip_addr, new_tddb->iscsi_name));
  4471. return QLA_SUCCESS;
  4472. }
  4473. static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
  4474. struct dev_db_entry *fw_ddb_entry,
  4475. uint32_t *index)
  4476. {
  4477. struct ddb_entry *ddb_entry;
  4478. struct ql4_tuple_ddb *fw_tddb = NULL;
  4479. struct ql4_tuple_ddb *tmp_tddb = NULL;
  4480. int idx;
  4481. int ret = QLA_ERROR;
  4482. fw_tddb = vzalloc(sizeof(*fw_tddb));
  4483. if (!fw_tddb) {
  4484. DEBUG2(ql4_printk(KERN_WARNING, ha,
  4485. "Memory Allocation failed.\n"));
  4486. ret = QLA_SUCCESS;
  4487. goto exit_check;
  4488. }
  4489. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  4490. if (!tmp_tddb) {
  4491. DEBUG2(ql4_printk(KERN_WARNING, ha,
  4492. "Memory Allocation failed.\n"));
  4493. ret = QLA_SUCCESS;
  4494. goto exit_check;
  4495. }
  4496. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
  4497. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  4498. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  4499. if (ddb_entry == NULL)
  4500. continue;
  4501. qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
  4502. if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
  4503. ret = QLA_SUCCESS; /* found */
  4504. if (index != NULL)
  4505. *index = idx;
  4506. goto exit_check;
  4507. }
  4508. }
  4509. exit_check:
  4510. if (fw_tddb)
  4511. vfree(fw_tddb);
  4512. if (tmp_tddb)
  4513. vfree(tmp_tddb);
  4514. return ret;
  4515. }
  4516. /**
  4517. * qla4xxx_check_existing_isid - check if target with same isid exist
  4518. * in target list
  4519. * @list_nt: list of target
  4520. * @isid: isid to check
  4521. *
  4522. * This routine return QLA_SUCCESS if target with same isid exist
  4523. **/
  4524. static int qla4xxx_check_existing_isid(struct list_head *list_nt, uint8_t *isid)
  4525. {
  4526. struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
  4527. struct dev_db_entry *fw_ddb_entry;
  4528. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  4529. fw_ddb_entry = &nt_ddb_idx->fw_ddb;
  4530. if (memcmp(&fw_ddb_entry->isid[0], &isid[0],
  4531. sizeof(nt_ddb_idx->fw_ddb.isid)) == 0) {
  4532. return QLA_SUCCESS;
  4533. }
  4534. }
  4535. return QLA_ERROR;
  4536. }
  4537. /**
  4538. * qla4xxx_update_isid - compare ddbs and updated isid
  4539. * @ha: Pointer to host adapter structure.
  4540. * @list_nt: list of nt target
  4541. * @fw_ddb_entry: firmware ddb entry
  4542. *
  4543. * This routine update isid if ddbs have same iqn, same isid and
  4544. * different IP addr.
  4545. * Return QLA_SUCCESS if isid is updated.
  4546. **/
  4547. static int qla4xxx_update_isid(struct scsi_qla_host *ha,
  4548. struct list_head *list_nt,
  4549. struct dev_db_entry *fw_ddb_entry)
  4550. {
  4551. uint8_t base_value, i;
  4552. base_value = fw_ddb_entry->isid[1] & 0x1f;
  4553. for (i = 0; i < 8; i++) {
  4554. fw_ddb_entry->isid[1] = (base_value | (i << 5));
  4555. if (qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
  4556. break;
  4557. }
  4558. if (!qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
  4559. return QLA_ERROR;
  4560. return QLA_SUCCESS;
  4561. }
  4562. /**
  4563. * qla4xxx_should_update_isid - check if isid need to update
  4564. * @ha: Pointer to host adapter structure.
  4565. * @old_tddb: ddb tuple
  4566. * @new_tddb: ddb tuple
  4567. *
  4568. * Return QLA_SUCCESS if different IP, different PORT, same iqn,
  4569. * same isid
  4570. **/
  4571. static int qla4xxx_should_update_isid(struct scsi_qla_host *ha,
  4572. struct ql4_tuple_ddb *old_tddb,
  4573. struct ql4_tuple_ddb *new_tddb)
  4574. {
  4575. if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr) == 0) {
  4576. /* Same ip */
  4577. if (old_tddb->port == new_tddb->port)
  4578. return QLA_ERROR;
  4579. }
  4580. if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
  4581. /* different iqn */
  4582. return QLA_ERROR;
  4583. if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
  4584. sizeof(old_tddb->isid)))
  4585. /* different isid */
  4586. return QLA_ERROR;
  4587. return QLA_SUCCESS;
  4588. }
  4589. /**
  4590. * qla4xxx_is_flash_ddb_exists - check if fw_ddb_entry already exists in list_nt
  4591. * @ha: Pointer to host adapter structure.
  4592. * @list_nt: list of nt target.
  4593. * @fw_ddb_entry: firmware ddb entry.
  4594. *
  4595. * This routine check if fw_ddb_entry already exists in list_nt to avoid
  4596. * duplicate ddb in list_nt.
  4597. * Return QLA_SUCCESS if duplicate ddb exit in list_nl.
  4598. * Note: This function also update isid of DDB if required.
  4599. **/
  4600. static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
  4601. struct list_head *list_nt,
  4602. struct dev_db_entry *fw_ddb_entry)
  4603. {
  4604. struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
  4605. struct ql4_tuple_ddb *fw_tddb = NULL;
  4606. struct ql4_tuple_ddb *tmp_tddb = NULL;
  4607. int rval, ret = QLA_ERROR;
  4608. fw_tddb = vzalloc(sizeof(*fw_tddb));
  4609. if (!fw_tddb) {
  4610. DEBUG2(ql4_printk(KERN_WARNING, ha,
  4611. "Memory Allocation failed.\n"));
  4612. ret = QLA_SUCCESS;
  4613. goto exit_check;
  4614. }
  4615. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  4616. if (!tmp_tddb) {
  4617. DEBUG2(ql4_printk(KERN_WARNING, ha,
  4618. "Memory Allocation failed.\n"));
  4619. ret = QLA_SUCCESS;
  4620. goto exit_check;
  4621. }
  4622. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
  4623. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  4624. qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb,
  4625. nt_ddb_idx->flash_isid);
  4626. ret = qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true);
  4627. /* found duplicate ddb */
  4628. if (ret == QLA_SUCCESS)
  4629. goto exit_check;
  4630. }
  4631. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  4632. qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb, NULL);
  4633. ret = qla4xxx_should_update_isid(ha, tmp_tddb, fw_tddb);
  4634. if (ret == QLA_SUCCESS) {
  4635. rval = qla4xxx_update_isid(ha, list_nt, fw_ddb_entry);
  4636. if (rval == QLA_SUCCESS)
  4637. ret = QLA_ERROR;
  4638. else
  4639. ret = QLA_SUCCESS;
  4640. goto exit_check;
  4641. }
  4642. }
  4643. exit_check:
  4644. if (fw_tddb)
  4645. vfree(fw_tddb);
  4646. if (tmp_tddb)
  4647. vfree(tmp_tddb);
  4648. return ret;
  4649. }
  4650. static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
  4651. {
  4652. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  4653. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
  4654. list_del_init(&ddb_idx->list);
  4655. vfree(ddb_idx);
  4656. }
  4657. }
  4658. static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
  4659. struct dev_db_entry *fw_ddb_entry)
  4660. {
  4661. struct iscsi_endpoint *ep;
  4662. struct sockaddr_in *addr;
  4663. struct sockaddr_in6 *addr6;
  4664. struct sockaddr *t_addr;
  4665. struct sockaddr_storage *dst_addr;
  4666. char *ip;
  4667. /* TODO: need to destroy on unload iscsi_endpoint*/
  4668. dst_addr = vmalloc(sizeof(*dst_addr));
  4669. if (!dst_addr)
  4670. return NULL;
  4671. if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
  4672. t_addr = (struct sockaddr *)dst_addr;
  4673. t_addr->sa_family = AF_INET6;
  4674. addr6 = (struct sockaddr_in6 *)dst_addr;
  4675. ip = (char *)&addr6->sin6_addr;
  4676. memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
  4677. addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
  4678. } else {
  4679. t_addr = (struct sockaddr *)dst_addr;
  4680. t_addr->sa_family = AF_INET;
  4681. addr = (struct sockaddr_in *)dst_addr;
  4682. ip = (char *)&addr->sin_addr;
  4683. memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
  4684. addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
  4685. }
  4686. ep = qla4xxx_ep_connect(ha->host, (struct sockaddr *)dst_addr, 0);
  4687. vfree(dst_addr);
  4688. return ep;
  4689. }
  4690. static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
  4691. {
  4692. if (ql4xdisablesysfsboot)
  4693. return QLA_SUCCESS;
  4694. if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
  4695. return QLA_ERROR;
  4696. return QLA_SUCCESS;
  4697. }
  4698. static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
  4699. struct ddb_entry *ddb_entry,
  4700. uint16_t idx)
  4701. {
  4702. uint16_t def_timeout;
  4703. ddb_entry->ddb_type = FLASH_DDB;
  4704. ddb_entry->fw_ddb_index = INVALID_ENTRY;
  4705. ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  4706. ddb_entry->ha = ha;
  4707. ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
  4708. ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
  4709. ddb_entry->chap_tbl_idx = INVALID_ENTRY;
  4710. atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  4711. atomic_set(&ddb_entry->relogin_timer, 0);
  4712. atomic_set(&ddb_entry->relogin_retry_count, 0);
  4713. def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
  4714. ddb_entry->default_relogin_timeout =
  4715. (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
  4716. def_timeout : LOGIN_TOV;
  4717. ddb_entry->default_time2wait =
  4718. le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
  4719. if (ql4xdisablesysfsboot &&
  4720. (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx))
  4721. set_bit(DF_BOOT_TGT, &ddb_entry->flags);
  4722. }
  4723. static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
  4724. {
  4725. uint32_t idx = 0;
  4726. uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
  4727. uint32_t sts[MBOX_REG_COUNT];
  4728. uint32_t ip_state;
  4729. unsigned long wtime;
  4730. int ret;
  4731. wtime = jiffies + (HZ * IP_CONFIG_TOV);
  4732. do {
  4733. for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
  4734. if (ip_idx[idx] == -1)
  4735. continue;
  4736. ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
  4737. if (ret == QLA_ERROR) {
  4738. ip_idx[idx] = -1;
  4739. continue;
  4740. }
  4741. ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
  4742. DEBUG2(ql4_printk(KERN_INFO, ha,
  4743. "Waiting for IP state for idx = %d, state = 0x%x\n",
  4744. ip_idx[idx], ip_state));
  4745. if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
  4746. ip_state == IP_ADDRSTATE_INVALID ||
  4747. ip_state == IP_ADDRSTATE_PREFERRED ||
  4748. ip_state == IP_ADDRSTATE_DEPRICATED ||
  4749. ip_state == IP_ADDRSTATE_DISABLING)
  4750. ip_idx[idx] = -1;
  4751. }
  4752. /* Break if all IP states checked */
  4753. if ((ip_idx[0] == -1) &&
  4754. (ip_idx[1] == -1) &&
  4755. (ip_idx[2] == -1) &&
  4756. (ip_idx[3] == -1))
  4757. break;
  4758. schedule_timeout_uninterruptible(HZ);
  4759. } while (time_after(wtime, jiffies));
  4760. }
  4761. static int qla4xxx_cmp_fw_stentry(struct dev_db_entry *fw_ddb_entry,
  4762. struct dev_db_entry *flash_ddb_entry)
  4763. {
  4764. uint16_t options = 0;
  4765. size_t ip_len = IP_ADDR_LEN;
  4766. options = le16_to_cpu(fw_ddb_entry->options);
  4767. if (options & DDB_OPT_IPV6_DEVICE)
  4768. ip_len = IPv6_ADDR_LEN;
  4769. if (memcmp(fw_ddb_entry->ip_addr, flash_ddb_entry->ip_addr, ip_len))
  4770. return QLA_ERROR;
  4771. if (memcmp(&fw_ddb_entry->isid[0], &flash_ddb_entry->isid[0],
  4772. sizeof(fw_ddb_entry->isid)))
  4773. return QLA_ERROR;
  4774. if (memcmp(&fw_ddb_entry->port, &flash_ddb_entry->port,
  4775. sizeof(fw_ddb_entry->port)))
  4776. return QLA_ERROR;
  4777. return QLA_SUCCESS;
  4778. }
  4779. static int qla4xxx_find_flash_st_idx(struct scsi_qla_host *ha,
  4780. struct dev_db_entry *fw_ddb_entry,
  4781. uint32_t fw_idx, uint32_t *flash_index)
  4782. {
  4783. struct dev_db_entry *flash_ddb_entry;
  4784. dma_addr_t flash_ddb_entry_dma;
  4785. uint32_t idx = 0;
  4786. int max_ddbs;
  4787. int ret = QLA_ERROR, status;
  4788. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  4789. MAX_DEV_DB_ENTRIES;
  4790. flash_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  4791. &flash_ddb_entry_dma);
  4792. if (flash_ddb_entry == NULL || fw_ddb_entry == NULL) {
  4793. ql4_printk(KERN_ERR, ha, "Out of memory\n");
  4794. goto exit_find_st_idx;
  4795. }
  4796. status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
  4797. flash_ddb_entry_dma, fw_idx);
  4798. if (status == QLA_SUCCESS) {
  4799. status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
  4800. if (status == QLA_SUCCESS) {
  4801. *flash_index = fw_idx;
  4802. ret = QLA_SUCCESS;
  4803. goto exit_find_st_idx;
  4804. }
  4805. }
  4806. for (idx = 0; idx < max_ddbs; idx++) {
  4807. status = qla4xxx_flashdb_by_index(ha, flash_ddb_entry,
  4808. flash_ddb_entry_dma, idx);
  4809. if (status == QLA_ERROR)
  4810. continue;
  4811. status = qla4xxx_cmp_fw_stentry(fw_ddb_entry, flash_ddb_entry);
  4812. if (status == QLA_SUCCESS) {
  4813. *flash_index = idx;
  4814. ret = QLA_SUCCESS;
  4815. goto exit_find_st_idx;
  4816. }
  4817. }
  4818. if (idx == max_ddbs)
  4819. ql4_printk(KERN_ERR, ha, "Failed to find ST [%d] in flash\n",
  4820. fw_idx);
  4821. exit_find_st_idx:
  4822. if (flash_ddb_entry)
  4823. dma_pool_free(ha->fw_ddb_dma_pool, flash_ddb_entry,
  4824. flash_ddb_entry_dma);
  4825. return ret;
  4826. }
  4827. static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
  4828. struct list_head *list_st)
  4829. {
  4830. struct qla_ddb_index *st_ddb_idx;
  4831. int max_ddbs;
  4832. int fw_idx_size;
  4833. struct dev_db_entry *fw_ddb_entry;
  4834. dma_addr_t fw_ddb_dma;
  4835. int ret;
  4836. uint32_t idx = 0, next_idx = 0;
  4837. uint32_t state = 0, conn_err = 0;
  4838. uint32_t flash_index = -1;
  4839. uint16_t conn_id = 0;
  4840. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  4841. &fw_ddb_dma);
  4842. if (fw_ddb_entry == NULL) {
  4843. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  4844. goto exit_st_list;
  4845. }
  4846. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  4847. MAX_DEV_DB_ENTRIES;
  4848. fw_idx_size = sizeof(struct qla_ddb_index);
  4849. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  4850. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
  4851. NULL, &next_idx, &state,
  4852. &conn_err, NULL, &conn_id);
  4853. if (ret == QLA_ERROR)
  4854. break;
  4855. /* Ignore DDB if invalid state (unassigned) */
  4856. if (state == DDB_DS_UNASSIGNED)
  4857. goto continue_next_st;
  4858. /* Check if ST, add to the list_st */
  4859. if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
  4860. goto continue_next_st;
  4861. st_ddb_idx = vzalloc(fw_idx_size);
  4862. if (!st_ddb_idx)
  4863. break;
  4864. ret = qla4xxx_find_flash_st_idx(ha, fw_ddb_entry, idx,
  4865. &flash_index);
  4866. if (ret == QLA_ERROR) {
  4867. ql4_printk(KERN_ERR, ha,
  4868. "No flash entry for ST at idx [%d]\n", idx);
  4869. st_ddb_idx->flash_ddb_idx = idx;
  4870. } else {
  4871. ql4_printk(KERN_INFO, ha,
  4872. "ST at idx [%d] is stored at flash [%d]\n",
  4873. idx, flash_index);
  4874. st_ddb_idx->flash_ddb_idx = flash_index;
  4875. }
  4876. st_ddb_idx->fw_ddb_idx = idx;
  4877. list_add_tail(&st_ddb_idx->list, list_st);
  4878. continue_next_st:
  4879. if (next_idx == 0)
  4880. break;
  4881. }
  4882. exit_st_list:
  4883. if (fw_ddb_entry)
  4884. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  4885. }
  4886. /**
  4887. * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
  4888. * @ha: pointer to adapter structure
  4889. * @list_ddb: List from which failed ddb to be removed
  4890. *
  4891. * Iterate over the list of DDBs and find and remove DDBs that are either in
  4892. * no connection active state or failed state
  4893. **/
  4894. static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
  4895. struct list_head *list_ddb)
  4896. {
  4897. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  4898. uint32_t next_idx = 0;
  4899. uint32_t state = 0, conn_err = 0;
  4900. int ret;
  4901. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
  4902. ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
  4903. NULL, 0, NULL, &next_idx, &state,
  4904. &conn_err, NULL, NULL);
  4905. if (ret == QLA_ERROR)
  4906. continue;
  4907. if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  4908. state == DDB_DS_SESSION_FAILED) {
  4909. list_del_init(&ddb_idx->list);
  4910. vfree(ddb_idx);
  4911. }
  4912. }
  4913. }
  4914. static void qla4xxx_update_sess_disc_idx(struct scsi_qla_host *ha,
  4915. struct ddb_entry *ddb_entry,
  4916. struct dev_db_entry *fw_ddb_entry)
  4917. {
  4918. struct iscsi_cls_session *cls_sess;
  4919. struct iscsi_session *sess;
  4920. uint32_t max_ddbs = 0;
  4921. uint16_t ddb_link = -1;
  4922. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  4923. MAX_DEV_DB_ENTRIES;
  4924. cls_sess = ddb_entry->sess;
  4925. sess = cls_sess->dd_data;
  4926. ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
  4927. if (ddb_link < max_ddbs)
  4928. sess->discovery_parent_idx = ddb_link;
  4929. else
  4930. sess->discovery_parent_idx = DDB_NO_LINK;
  4931. }
  4932. static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
  4933. struct dev_db_entry *fw_ddb_entry,
  4934. int is_reset, uint16_t idx)
  4935. {
  4936. struct iscsi_cls_session *cls_sess;
  4937. struct iscsi_session *sess;
  4938. struct iscsi_cls_conn *cls_conn;
  4939. struct iscsi_endpoint *ep;
  4940. uint16_t cmds_max = 32;
  4941. uint16_t conn_id = 0;
  4942. uint32_t initial_cmdsn = 0;
  4943. int ret = QLA_SUCCESS;
  4944. struct ddb_entry *ddb_entry = NULL;
  4945. /* Create session object, with INVALID_ENTRY,
  4946. * the targer_id would get set when we issue the login
  4947. */
  4948. cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
  4949. cmds_max, sizeof(struct ddb_entry),
  4950. sizeof(struct ql4_task_data),
  4951. initial_cmdsn, INVALID_ENTRY);
  4952. if (!cls_sess) {
  4953. ret = QLA_ERROR;
  4954. goto exit_setup;
  4955. }
  4956. /*
  4957. * so calling module_put function to decrement the
  4958. * reference count.
  4959. **/
  4960. module_put(qla4xxx_iscsi_transport.owner);
  4961. sess = cls_sess->dd_data;
  4962. ddb_entry = sess->dd_data;
  4963. ddb_entry->sess = cls_sess;
  4964. cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  4965. memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
  4966. sizeof(struct dev_db_entry));
  4967. qla4xxx_setup_flash_ddb_entry(ha, ddb_entry, idx);
  4968. cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
  4969. if (!cls_conn) {
  4970. ret = QLA_ERROR;
  4971. goto exit_setup;
  4972. }
  4973. ddb_entry->conn = cls_conn;
  4974. /* Setup ep, for displaying attributes in sysfs */
  4975. ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
  4976. if (ep) {
  4977. ep->conn = cls_conn;
  4978. cls_conn->ep = ep;
  4979. } else {
  4980. DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
  4981. ret = QLA_ERROR;
  4982. goto exit_setup;
  4983. }
  4984. /* Update sess/conn params */
  4985. qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
  4986. qla4xxx_update_sess_disc_idx(ha, ddb_entry, fw_ddb_entry);
  4987. if (is_reset == RESET_ADAPTER) {
  4988. iscsi_block_session(cls_sess);
  4989. /* Use the relogin path to discover new devices
  4990. * by short-circuting the logic of setting
  4991. * timer to relogin - instead set the flags
  4992. * to initiate login right away.
  4993. */
  4994. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  4995. set_bit(DF_RELOGIN, &ddb_entry->flags);
  4996. }
  4997. exit_setup:
  4998. return ret;
  4999. }
  5000. static void qla4xxx_update_fw_ddb_link(struct scsi_qla_host *ha,
  5001. struct list_head *list_ddb,
  5002. struct dev_db_entry *fw_ddb_entry)
  5003. {
  5004. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  5005. uint16_t ddb_link;
  5006. ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
  5007. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
  5008. if (ddb_idx->fw_ddb_idx == ddb_link) {
  5009. DEBUG2(ql4_printk(KERN_INFO, ha,
  5010. "Updating NT parent idx from [%d] to [%d]\n",
  5011. ddb_link, ddb_idx->flash_ddb_idx));
  5012. fw_ddb_entry->ddb_link =
  5013. cpu_to_le16(ddb_idx->flash_ddb_idx);
  5014. return;
  5015. }
  5016. }
  5017. }
  5018. static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
  5019. struct list_head *list_nt,
  5020. struct list_head *list_st,
  5021. int is_reset)
  5022. {
  5023. struct dev_db_entry *fw_ddb_entry;
  5024. struct ddb_entry *ddb_entry = NULL;
  5025. dma_addr_t fw_ddb_dma;
  5026. int max_ddbs;
  5027. int fw_idx_size;
  5028. int ret;
  5029. uint32_t idx = 0, next_idx = 0;
  5030. uint32_t state = 0, conn_err = 0;
  5031. uint32_t ddb_idx = -1;
  5032. uint16_t conn_id = 0;
  5033. uint16_t ddb_link = -1;
  5034. struct qla_ddb_index *nt_ddb_idx;
  5035. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  5036. &fw_ddb_dma);
  5037. if (fw_ddb_entry == NULL) {
  5038. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  5039. goto exit_nt_list;
  5040. }
  5041. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  5042. MAX_DEV_DB_ENTRIES;
  5043. fw_idx_size = sizeof(struct qla_ddb_index);
  5044. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  5045. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
  5046. NULL, &next_idx, &state,
  5047. &conn_err, NULL, &conn_id);
  5048. if (ret == QLA_ERROR)
  5049. break;
  5050. if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
  5051. goto continue_next_nt;
  5052. /* Check if NT, then add to list it */
  5053. if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
  5054. goto continue_next_nt;
  5055. ddb_link = le16_to_cpu(fw_ddb_entry->ddb_link);
  5056. if (ddb_link < max_ddbs)
  5057. qla4xxx_update_fw_ddb_link(ha, list_st, fw_ddb_entry);
  5058. if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
  5059. state == DDB_DS_SESSION_FAILED) &&
  5060. (is_reset == INIT_ADAPTER))
  5061. goto continue_next_nt;
  5062. DEBUG2(ql4_printk(KERN_INFO, ha,
  5063. "Adding DDB to session = 0x%x\n", idx));
  5064. if (is_reset == INIT_ADAPTER) {
  5065. nt_ddb_idx = vmalloc(fw_idx_size);
  5066. if (!nt_ddb_idx)
  5067. break;
  5068. nt_ddb_idx->fw_ddb_idx = idx;
  5069. /* Copy original isid as it may get updated in function
  5070. * qla4xxx_update_isid(). We need original isid in
  5071. * function qla4xxx_compare_tuple_ddb to find duplicate
  5072. * target */
  5073. memcpy(&nt_ddb_idx->flash_isid[0],
  5074. &fw_ddb_entry->isid[0],
  5075. sizeof(nt_ddb_idx->flash_isid));
  5076. ret = qla4xxx_is_flash_ddb_exists(ha, list_nt,
  5077. fw_ddb_entry);
  5078. if (ret == QLA_SUCCESS) {
  5079. /* free nt_ddb_idx and do not add to list_nt */
  5080. vfree(nt_ddb_idx);
  5081. goto continue_next_nt;
  5082. }
  5083. /* Copy updated isid */
  5084. memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
  5085. sizeof(struct dev_db_entry));
  5086. list_add_tail(&nt_ddb_idx->list, list_nt);
  5087. } else if (is_reset == RESET_ADAPTER) {
  5088. ret = qla4xxx_is_session_exists(ha, fw_ddb_entry,
  5089. &ddb_idx);
  5090. if (ret == QLA_SUCCESS) {
  5091. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
  5092. ddb_idx);
  5093. if (ddb_entry != NULL)
  5094. qla4xxx_update_sess_disc_idx(ha,
  5095. ddb_entry,
  5096. fw_ddb_entry);
  5097. goto continue_next_nt;
  5098. }
  5099. }
  5100. ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset, idx);
  5101. if (ret == QLA_ERROR)
  5102. goto exit_nt_list;
  5103. continue_next_nt:
  5104. if (next_idx == 0)
  5105. break;
  5106. }
  5107. exit_nt_list:
  5108. if (fw_ddb_entry)
  5109. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  5110. }
  5111. static void qla4xxx_build_new_nt_list(struct scsi_qla_host *ha,
  5112. struct list_head *list_nt,
  5113. uint16_t target_id)
  5114. {
  5115. struct dev_db_entry *fw_ddb_entry;
  5116. dma_addr_t fw_ddb_dma;
  5117. int max_ddbs;
  5118. int fw_idx_size;
  5119. int ret;
  5120. uint32_t idx = 0, next_idx = 0;
  5121. uint32_t state = 0, conn_err = 0;
  5122. uint16_t conn_id = 0;
  5123. struct qla_ddb_index *nt_ddb_idx;
  5124. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  5125. &fw_ddb_dma);
  5126. if (fw_ddb_entry == NULL) {
  5127. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  5128. goto exit_new_nt_list;
  5129. }
  5130. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  5131. MAX_DEV_DB_ENTRIES;
  5132. fw_idx_size = sizeof(struct qla_ddb_index);
  5133. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  5134. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
  5135. NULL, &next_idx, &state,
  5136. &conn_err, NULL, &conn_id);
  5137. if (ret == QLA_ERROR)
  5138. break;
  5139. /* Check if NT, then add it to list */
  5140. if (strlen((char *)fw_ddb_entry->iscsi_name) == 0)
  5141. goto continue_next_new_nt;
  5142. if (!(state == DDB_DS_NO_CONNECTION_ACTIVE))
  5143. goto continue_next_new_nt;
  5144. DEBUG2(ql4_printk(KERN_INFO, ha,
  5145. "Adding DDB to session = 0x%x\n", idx));
  5146. nt_ddb_idx = vmalloc(fw_idx_size);
  5147. if (!nt_ddb_idx)
  5148. break;
  5149. nt_ddb_idx->fw_ddb_idx = idx;
  5150. ret = qla4xxx_is_session_exists(ha, fw_ddb_entry, NULL);
  5151. if (ret == QLA_SUCCESS) {
  5152. /* free nt_ddb_idx and do not add to list_nt */
  5153. vfree(nt_ddb_idx);
  5154. goto continue_next_new_nt;
  5155. }
  5156. if (target_id < max_ddbs)
  5157. fw_ddb_entry->ddb_link = cpu_to_le16(target_id);
  5158. list_add_tail(&nt_ddb_idx->list, list_nt);
  5159. ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, RESET_ADAPTER,
  5160. idx);
  5161. if (ret == QLA_ERROR)
  5162. goto exit_new_nt_list;
  5163. continue_next_new_nt:
  5164. if (next_idx == 0)
  5165. break;
  5166. }
  5167. exit_new_nt_list:
  5168. if (fw_ddb_entry)
  5169. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  5170. }
  5171. /**
  5172. * qla4xxx_sysfs_ddb_is_non_persistent - check for non-persistence of ddb entry
  5173. * @dev: dev associated with the sysfs entry
  5174. * @data: pointer to flashnode session object
  5175. *
  5176. * Returns:
  5177. * 1: if flashnode entry is non-persistent
  5178. * 0: if flashnode entry is persistent
  5179. **/
  5180. static int qla4xxx_sysfs_ddb_is_non_persistent(struct device *dev, void *data)
  5181. {
  5182. struct iscsi_bus_flash_session *fnode_sess;
  5183. if (!iscsi_flashnode_bus_match(dev, NULL))
  5184. return 0;
  5185. fnode_sess = iscsi_dev_to_flash_session(dev);
  5186. return (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT);
  5187. }
  5188. /**
  5189. * qla4xxx_sysfs_ddb_tgt_create - Create sysfs entry for target
  5190. * @ha: pointer to host
  5191. * @fw_ddb_entry: flash ddb data
  5192. * @idx: target index
  5193. * @user: if set then this call is made from userland else from kernel
  5194. *
  5195. * Returns:
  5196. * On sucess: QLA_SUCCESS
  5197. * On failure: QLA_ERROR
  5198. *
  5199. * This create separate sysfs entries for session and connection attributes of
  5200. * the given fw ddb entry.
  5201. * If this is invoked as a result of a userspace call then the entry is marked
  5202. * as nonpersistent using flash_state field.
  5203. **/
  5204. static int qla4xxx_sysfs_ddb_tgt_create(struct scsi_qla_host *ha,
  5205. struct dev_db_entry *fw_ddb_entry,
  5206. uint16_t *idx, int user)
  5207. {
  5208. struct iscsi_bus_flash_session *fnode_sess = NULL;
  5209. struct iscsi_bus_flash_conn *fnode_conn = NULL;
  5210. int rc = QLA_ERROR;
  5211. fnode_sess = iscsi_create_flashnode_sess(ha->host, *idx,
  5212. &qla4xxx_iscsi_transport, 0);
  5213. if (!fnode_sess) {
  5214. ql4_printk(KERN_ERR, ha,
  5215. "%s: Unable to create session sysfs entry for flashnode %d of host%lu\n",
  5216. __func__, *idx, ha->host_no);
  5217. goto exit_tgt_create;
  5218. }
  5219. fnode_conn = iscsi_create_flashnode_conn(ha->host, fnode_sess,
  5220. &qla4xxx_iscsi_transport, 0);
  5221. if (!fnode_conn) {
  5222. ql4_printk(KERN_ERR, ha,
  5223. "%s: Unable to create conn sysfs entry for flashnode %d of host%lu\n",
  5224. __func__, *idx, ha->host_no);
  5225. goto free_sess;
  5226. }
  5227. if (user) {
  5228. fnode_sess->flash_state = DEV_DB_NON_PERSISTENT;
  5229. } else {
  5230. fnode_sess->flash_state = DEV_DB_PERSISTENT;
  5231. if (*idx == ha->pri_ddb_idx || *idx == ha->sec_ddb_idx)
  5232. fnode_sess->is_boot_target = 1;
  5233. else
  5234. fnode_sess->is_boot_target = 0;
  5235. }
  5236. rc = qla4xxx_copy_from_fwddb_param(fnode_sess, fnode_conn,
  5237. fw_ddb_entry);
  5238. ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
  5239. __func__, fnode_sess->dev.kobj.name);
  5240. ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
  5241. __func__, fnode_conn->dev.kobj.name);
  5242. return QLA_SUCCESS;
  5243. free_sess:
  5244. iscsi_destroy_flashnode_sess(fnode_sess);
  5245. exit_tgt_create:
  5246. return QLA_ERROR;
  5247. }
  5248. /**
  5249. * qla4xxx_sysfs_ddb_add - Add new ddb entry in flash
  5250. * @shost: pointer to host
  5251. * @buf: type of ddb entry (ipv4/ipv6)
  5252. * @len: length of buf
  5253. *
  5254. * This creates new ddb entry in the flash by finding first free index and
  5255. * storing default ddb there. And then create sysfs entry for the new ddb entry.
  5256. **/
  5257. static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
  5258. int len)
  5259. {
  5260. struct scsi_qla_host *ha = to_qla_host(shost);
  5261. struct dev_db_entry *fw_ddb_entry = NULL;
  5262. dma_addr_t fw_ddb_entry_dma;
  5263. struct device *dev;
  5264. uint16_t idx = 0;
  5265. uint16_t max_ddbs = 0;
  5266. uint32_t options = 0;
  5267. uint32_t rval = QLA_ERROR;
  5268. if (strncasecmp(PORTAL_TYPE_IPV4, buf, 4) &&
  5269. strncasecmp(PORTAL_TYPE_IPV6, buf, 4)) {
  5270. DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Invalid portal type\n",
  5271. __func__));
  5272. goto exit_ddb_add;
  5273. }
  5274. max_ddbs = is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
  5275. MAX_DEV_DB_ENTRIES;
  5276. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  5277. &fw_ddb_entry_dma, GFP_KERNEL);
  5278. if (!fw_ddb_entry) {
  5279. DEBUG2(ql4_printk(KERN_ERR, ha,
  5280. "%s: Unable to allocate dma buffer\n",
  5281. __func__));
  5282. goto exit_ddb_add;
  5283. }
  5284. dev = iscsi_find_flashnode_sess(ha->host, NULL,
  5285. qla4xxx_sysfs_ddb_is_non_persistent);
  5286. if (dev) {
  5287. ql4_printk(KERN_ERR, ha,
  5288. "%s: A non-persistent entry %s found\n",
  5289. __func__, dev->kobj.name);
  5290. put_device(dev);
  5291. goto exit_ddb_add;
  5292. }
  5293. /* Index 0 and 1 are reserved for boot target entries */
  5294. for (idx = 2; idx < max_ddbs; idx++) {
  5295. if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry,
  5296. fw_ddb_entry_dma, idx))
  5297. break;
  5298. }
  5299. if (idx == max_ddbs)
  5300. goto exit_ddb_add;
  5301. if (!strncasecmp("ipv6", buf, 4))
  5302. options |= IPV6_DEFAULT_DDB_ENTRY;
  5303. rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
  5304. if (rval == QLA_ERROR)
  5305. goto exit_ddb_add;
  5306. rval = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 1);
  5307. exit_ddb_add:
  5308. if (fw_ddb_entry)
  5309. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  5310. fw_ddb_entry, fw_ddb_entry_dma);
  5311. if (rval == QLA_SUCCESS)
  5312. return idx;
  5313. else
  5314. return -EIO;
  5315. }
  5316. /**
  5317. * qla4xxx_sysfs_ddb_apply - write the target ddb contents to Flash
  5318. * @fnode_sess: pointer to session attrs of flash ddb entry
  5319. * @fnode_conn: pointer to connection attrs of flash ddb entry
  5320. *
  5321. * This writes the contents of target ddb buffer to Flash with a valid cookie
  5322. * value in order to make the ddb entry persistent.
  5323. **/
  5324. static int qla4xxx_sysfs_ddb_apply(struct iscsi_bus_flash_session *fnode_sess,
  5325. struct iscsi_bus_flash_conn *fnode_conn)
  5326. {
  5327. struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
  5328. struct scsi_qla_host *ha = to_qla_host(shost);
  5329. uint32_t dev_db_start_offset = FLASH_OFFSET_DB_INFO;
  5330. struct dev_db_entry *fw_ddb_entry = NULL;
  5331. dma_addr_t fw_ddb_entry_dma;
  5332. uint32_t options = 0;
  5333. int rval = 0;
  5334. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  5335. &fw_ddb_entry_dma, GFP_KERNEL);
  5336. if (!fw_ddb_entry) {
  5337. DEBUG2(ql4_printk(KERN_ERR, ha,
  5338. "%s: Unable to allocate dma buffer\n",
  5339. __func__));
  5340. rval = -ENOMEM;
  5341. goto exit_ddb_apply;
  5342. }
  5343. if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
  5344. options |= IPV6_DEFAULT_DDB_ENTRY;
  5345. rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
  5346. if (rval == QLA_ERROR)
  5347. goto exit_ddb_apply;
  5348. dev_db_start_offset += (fnode_sess->target_id *
  5349. sizeof(*fw_ddb_entry));
  5350. qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
  5351. fw_ddb_entry->cookie = DDB_VALID_COOKIE;
  5352. rval = qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
  5353. sizeof(*fw_ddb_entry), FLASH_OPT_RMW_COMMIT);
  5354. if (rval == QLA_SUCCESS) {
  5355. fnode_sess->flash_state = DEV_DB_PERSISTENT;
  5356. ql4_printk(KERN_INFO, ha,
  5357. "%s: flash node %u of host %lu written to flash\n",
  5358. __func__, fnode_sess->target_id, ha->host_no);
  5359. } else {
  5360. rval = -EIO;
  5361. ql4_printk(KERN_ERR, ha,
  5362. "%s: Error while writing flash node %u of host %lu to flash\n",
  5363. __func__, fnode_sess->target_id, ha->host_no);
  5364. }
  5365. exit_ddb_apply:
  5366. if (fw_ddb_entry)
  5367. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  5368. fw_ddb_entry, fw_ddb_entry_dma);
  5369. return rval;
  5370. }
  5371. static ssize_t qla4xxx_sysfs_ddb_conn_open(struct scsi_qla_host *ha,
  5372. struct dev_db_entry *fw_ddb_entry,
  5373. uint16_t idx)
  5374. {
  5375. struct dev_db_entry *ddb_entry = NULL;
  5376. dma_addr_t ddb_entry_dma;
  5377. unsigned long wtime;
  5378. uint32_t mbx_sts = 0;
  5379. uint32_t state = 0, conn_err = 0;
  5380. uint16_t tmo = 0;
  5381. int ret = 0;
  5382. ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
  5383. &ddb_entry_dma, GFP_KERNEL);
  5384. if (!ddb_entry) {
  5385. DEBUG2(ql4_printk(KERN_ERR, ha,
  5386. "%s: Unable to allocate dma buffer\n",
  5387. __func__));
  5388. return QLA_ERROR;
  5389. }
  5390. memcpy(ddb_entry, fw_ddb_entry, sizeof(*ddb_entry));
  5391. ret = qla4xxx_set_ddb_entry(ha, idx, ddb_entry_dma, &mbx_sts);
  5392. if (ret != QLA_SUCCESS) {
  5393. DEBUG2(ql4_printk(KERN_ERR, ha,
  5394. "%s: Unable to set ddb entry for index %d\n",
  5395. __func__, idx));
  5396. goto exit_ddb_conn_open;
  5397. }
  5398. qla4xxx_conn_open(ha, idx);
  5399. /* To ensure that sendtargets is done, wait for at least 12 secs */
  5400. tmo = ((ha->def_timeout > LOGIN_TOV) &&
  5401. (ha->def_timeout < LOGIN_TOV * 10) ?
  5402. ha->def_timeout : LOGIN_TOV);
  5403. DEBUG2(ql4_printk(KERN_INFO, ha,
  5404. "Default time to wait for login to ddb %d\n", tmo));
  5405. wtime = jiffies + (HZ * tmo);
  5406. do {
  5407. ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
  5408. NULL, &state, &conn_err, NULL,
  5409. NULL);
  5410. if (ret == QLA_ERROR)
  5411. continue;
  5412. if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  5413. state == DDB_DS_SESSION_FAILED)
  5414. break;
  5415. schedule_timeout_uninterruptible(HZ / 10);
  5416. } while (time_after(wtime, jiffies));
  5417. exit_ddb_conn_open:
  5418. if (ddb_entry)
  5419. dma_free_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
  5420. ddb_entry, ddb_entry_dma);
  5421. return ret;
  5422. }
  5423. static int qla4xxx_ddb_login_st(struct scsi_qla_host *ha,
  5424. struct dev_db_entry *fw_ddb_entry,
  5425. uint16_t target_id)
  5426. {
  5427. struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
  5428. struct list_head list_nt;
  5429. uint16_t ddb_index;
  5430. int ret = 0;
  5431. if (test_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags)) {
  5432. ql4_printk(KERN_WARNING, ha,
  5433. "%s: A discovery already in progress!\n", __func__);
  5434. return QLA_ERROR;
  5435. }
  5436. INIT_LIST_HEAD(&list_nt);
  5437. set_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
  5438. ret = qla4xxx_get_ddb_index(ha, &ddb_index);
  5439. if (ret == QLA_ERROR)
  5440. goto exit_login_st_clr_bit;
  5441. ret = qla4xxx_sysfs_ddb_conn_open(ha, fw_ddb_entry, ddb_index);
  5442. if (ret == QLA_ERROR)
  5443. goto exit_login_st;
  5444. qla4xxx_build_new_nt_list(ha, &list_nt, target_id);
  5445. list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, &list_nt, list) {
  5446. list_del_init(&ddb_idx->list);
  5447. qla4xxx_clear_ddb_entry(ha, ddb_idx->fw_ddb_idx);
  5448. vfree(ddb_idx);
  5449. }
  5450. exit_login_st:
  5451. if (qla4xxx_clear_ddb_entry(ha, ddb_index) == QLA_ERROR) {
  5452. ql4_printk(KERN_ERR, ha,
  5453. "Unable to clear DDB index = 0x%x\n", ddb_index);
  5454. }
  5455. clear_bit(ddb_index, ha->ddb_idx_map);
  5456. exit_login_st_clr_bit:
  5457. clear_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
  5458. return ret;
  5459. }
  5460. static int qla4xxx_ddb_login_nt(struct scsi_qla_host *ha,
  5461. struct dev_db_entry *fw_ddb_entry,
  5462. uint16_t idx)
  5463. {
  5464. int ret = QLA_ERROR;
  5465. ret = qla4xxx_is_session_exists(ha, fw_ddb_entry, NULL);
  5466. if (ret != QLA_SUCCESS)
  5467. ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, RESET_ADAPTER,
  5468. idx);
  5469. else
  5470. ret = -EPERM;
  5471. return ret;
  5472. }
  5473. /**
  5474. * qla4xxx_sysfs_ddb_login - Login to the specified target
  5475. * @fnode_sess: pointer to session attrs of flash ddb entry
  5476. * @fnode_conn: pointer to connection attrs of flash ddb entry
  5477. *
  5478. * This logs in to the specified target
  5479. **/
  5480. static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
  5481. struct iscsi_bus_flash_conn *fnode_conn)
  5482. {
  5483. struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
  5484. struct scsi_qla_host *ha = to_qla_host(shost);
  5485. struct dev_db_entry *fw_ddb_entry = NULL;
  5486. dma_addr_t fw_ddb_entry_dma;
  5487. uint32_t options = 0;
  5488. int ret = 0;
  5489. if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT) {
  5490. ql4_printk(KERN_ERR, ha,
  5491. "%s: Target info is not persistent\n", __func__);
  5492. ret = -EIO;
  5493. goto exit_ddb_login;
  5494. }
  5495. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  5496. &fw_ddb_entry_dma, GFP_KERNEL);
  5497. if (!fw_ddb_entry) {
  5498. DEBUG2(ql4_printk(KERN_ERR, ha,
  5499. "%s: Unable to allocate dma buffer\n",
  5500. __func__));
  5501. ret = -ENOMEM;
  5502. goto exit_ddb_login;
  5503. }
  5504. if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
  5505. options |= IPV6_DEFAULT_DDB_ENTRY;
  5506. ret = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
  5507. if (ret == QLA_ERROR)
  5508. goto exit_ddb_login;
  5509. qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
  5510. fw_ddb_entry->cookie = DDB_VALID_COOKIE;
  5511. if (strlen((char *)fw_ddb_entry->iscsi_name) == 0)
  5512. ret = qla4xxx_ddb_login_st(ha, fw_ddb_entry,
  5513. fnode_sess->target_id);
  5514. else
  5515. ret = qla4xxx_ddb_login_nt(ha, fw_ddb_entry,
  5516. fnode_sess->target_id);
  5517. if (ret > 0)
  5518. ret = -EIO;
  5519. exit_ddb_login:
  5520. if (fw_ddb_entry)
  5521. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  5522. fw_ddb_entry, fw_ddb_entry_dma);
  5523. return ret;
  5524. }
  5525. /**
  5526. * qla4xxx_sysfs_ddb_logout_sid - Logout session for the specified target
  5527. * @cls_sess: pointer to session to be logged out
  5528. *
  5529. * This performs session log out from the specified target
  5530. **/
  5531. static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess)
  5532. {
  5533. struct iscsi_session *sess;
  5534. struct ddb_entry *ddb_entry = NULL;
  5535. struct scsi_qla_host *ha;
  5536. struct dev_db_entry *fw_ddb_entry = NULL;
  5537. dma_addr_t fw_ddb_entry_dma;
  5538. unsigned long flags;
  5539. unsigned long wtime;
  5540. uint32_t ddb_state;
  5541. int options;
  5542. int ret = 0;
  5543. sess = cls_sess->dd_data;
  5544. ddb_entry = sess->dd_data;
  5545. ha = ddb_entry->ha;
  5546. if (ddb_entry->ddb_type != FLASH_DDB) {
  5547. ql4_printk(KERN_ERR, ha, "%s: Not a flash node session\n",
  5548. __func__);
  5549. ret = -ENXIO;
  5550. goto exit_ddb_logout;
  5551. }
  5552. if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
  5553. ql4_printk(KERN_ERR, ha,
  5554. "%s: Logout from boot target entry is not permitted.\n",
  5555. __func__);
  5556. ret = -EPERM;
  5557. goto exit_ddb_logout;
  5558. }
  5559. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  5560. &fw_ddb_entry_dma, GFP_KERNEL);
  5561. if (!fw_ddb_entry) {
  5562. ql4_printk(KERN_ERR, ha,
  5563. "%s: Unable to allocate dma buffer\n", __func__);
  5564. ret = -ENOMEM;
  5565. goto exit_ddb_logout;
  5566. }
  5567. if (test_and_set_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
  5568. goto ddb_logout_init;
  5569. ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
  5570. fw_ddb_entry, fw_ddb_entry_dma,
  5571. NULL, NULL, &ddb_state, NULL,
  5572. NULL, NULL);
  5573. if (ret == QLA_ERROR)
  5574. goto ddb_logout_init;
  5575. if (ddb_state == DDB_DS_SESSION_ACTIVE)
  5576. goto ddb_logout_init;
  5577. /* wait until next relogin is triggered using DF_RELOGIN and
  5578. * clear DF_RELOGIN to avoid invocation of further relogin
  5579. */
  5580. wtime = jiffies + (HZ * RELOGIN_TOV);
  5581. do {
  5582. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags))
  5583. goto ddb_logout_init;
  5584. schedule_timeout_uninterruptible(HZ);
  5585. } while ((time_after(wtime, jiffies)));
  5586. ddb_logout_init:
  5587. atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  5588. atomic_set(&ddb_entry->relogin_timer, 0);
  5589. options = LOGOUT_OPTION_CLOSE_SESSION;
  5590. qla4xxx_session_logout_ddb(ha, ddb_entry, options);
  5591. memset(fw_ddb_entry, 0, sizeof(*fw_ddb_entry));
  5592. wtime = jiffies + (HZ * LOGOUT_TOV);
  5593. do {
  5594. ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
  5595. fw_ddb_entry, fw_ddb_entry_dma,
  5596. NULL, NULL, &ddb_state, NULL,
  5597. NULL, NULL);
  5598. if (ret == QLA_ERROR)
  5599. goto ddb_logout_clr_sess;
  5600. if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
  5601. (ddb_state == DDB_DS_SESSION_FAILED))
  5602. goto ddb_logout_clr_sess;
  5603. schedule_timeout_uninterruptible(HZ);
  5604. } while ((time_after(wtime, jiffies)));
  5605. ddb_logout_clr_sess:
  5606. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  5607. /*
  5608. * we have decremented the reference count of the driver
  5609. * when we setup the session to have the driver unload
  5610. * to be seamless without actually destroying the
  5611. * session
  5612. **/
  5613. try_module_get(qla4xxx_iscsi_transport.owner);
  5614. iscsi_destroy_endpoint(ddb_entry->conn->ep);
  5615. spin_lock_irqsave(&ha->hardware_lock, flags);
  5616. qla4xxx_free_ddb(ha, ddb_entry);
  5617. clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
  5618. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  5619. iscsi_session_teardown(ddb_entry->sess);
  5620. clear_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags);
  5621. ret = QLA_SUCCESS;
  5622. exit_ddb_logout:
  5623. if (fw_ddb_entry)
  5624. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  5625. fw_ddb_entry, fw_ddb_entry_dma);
  5626. return ret;
  5627. }
  5628. /**
  5629. * qla4xxx_sysfs_ddb_logout - Logout from the specified target
  5630. * @fnode_sess: pointer to session attrs of flash ddb entry
  5631. * @fnode_conn: pointer to connection attrs of flash ddb entry
  5632. *
  5633. * This performs log out from the specified target
  5634. **/
  5635. static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
  5636. struct iscsi_bus_flash_conn *fnode_conn)
  5637. {
  5638. struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
  5639. struct scsi_qla_host *ha = to_qla_host(shost);
  5640. struct ql4_tuple_ddb *flash_tddb = NULL;
  5641. struct ql4_tuple_ddb *tmp_tddb = NULL;
  5642. struct dev_db_entry *fw_ddb_entry = NULL;
  5643. struct ddb_entry *ddb_entry = NULL;
  5644. dma_addr_t fw_ddb_dma;
  5645. uint32_t next_idx = 0;
  5646. uint32_t state = 0, conn_err = 0;
  5647. uint16_t conn_id = 0;
  5648. int idx, index;
  5649. int status, ret = 0;
  5650. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  5651. &fw_ddb_dma);
  5652. if (fw_ddb_entry == NULL) {
  5653. ql4_printk(KERN_ERR, ha, "%s:Out of memory\n", __func__);
  5654. ret = -ENOMEM;
  5655. goto exit_ddb_logout;
  5656. }
  5657. flash_tddb = vzalloc(sizeof(*flash_tddb));
  5658. if (!flash_tddb) {
  5659. ql4_printk(KERN_WARNING, ha,
  5660. "%s:Memory Allocation failed.\n", __func__);
  5661. ret = -ENOMEM;
  5662. goto exit_ddb_logout;
  5663. }
  5664. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  5665. if (!tmp_tddb) {
  5666. ql4_printk(KERN_WARNING, ha,
  5667. "%s:Memory Allocation failed.\n", __func__);
  5668. ret = -ENOMEM;
  5669. goto exit_ddb_logout;
  5670. }
  5671. if (!fnode_sess->targetname) {
  5672. ql4_printk(KERN_ERR, ha,
  5673. "%s:Cannot logout from SendTarget entry\n",
  5674. __func__);
  5675. ret = -EPERM;
  5676. goto exit_ddb_logout;
  5677. }
  5678. if (fnode_sess->is_boot_target) {
  5679. ql4_printk(KERN_ERR, ha,
  5680. "%s: Logout from boot target entry is not permitted.\n",
  5681. __func__);
  5682. ret = -EPERM;
  5683. goto exit_ddb_logout;
  5684. }
  5685. strncpy(flash_tddb->iscsi_name, fnode_sess->targetname,
  5686. ISCSI_NAME_SIZE);
  5687. if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
  5688. sprintf(flash_tddb->ip_addr, "%pI6", fnode_conn->ipaddress);
  5689. else
  5690. sprintf(flash_tddb->ip_addr, "%pI4", fnode_conn->ipaddress);
  5691. flash_tddb->tpgt = fnode_sess->tpgt;
  5692. flash_tddb->port = fnode_conn->port;
  5693. COPY_ISID(flash_tddb->isid, fnode_sess->isid);
  5694. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  5695. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  5696. if (ddb_entry == NULL)
  5697. continue;
  5698. if (ddb_entry->ddb_type != FLASH_DDB)
  5699. continue;
  5700. index = ddb_entry->sess->target_id;
  5701. status = qla4xxx_get_fwddb_entry(ha, index, fw_ddb_entry,
  5702. fw_ddb_dma, NULL, &next_idx,
  5703. &state, &conn_err, NULL,
  5704. &conn_id);
  5705. if (status == QLA_ERROR) {
  5706. ret = -ENOMEM;
  5707. break;
  5708. }
  5709. qla4xxx_convert_param_ddb(fw_ddb_entry, tmp_tddb, NULL);
  5710. status = qla4xxx_compare_tuple_ddb(ha, flash_tddb, tmp_tddb,
  5711. true);
  5712. if (status == QLA_SUCCESS) {
  5713. ret = qla4xxx_sysfs_ddb_logout_sid(ddb_entry->sess);
  5714. break;
  5715. }
  5716. }
  5717. if (idx == MAX_DDB_ENTRIES)
  5718. ret = -ESRCH;
  5719. exit_ddb_logout:
  5720. if (flash_tddb)
  5721. vfree(flash_tddb);
  5722. if (tmp_tddb)
  5723. vfree(tmp_tddb);
  5724. if (fw_ddb_entry)
  5725. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  5726. return ret;
  5727. }
  5728. static int
  5729. qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
  5730. int param, char *buf)
  5731. {
  5732. struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
  5733. struct scsi_qla_host *ha = to_qla_host(shost);
  5734. struct iscsi_bus_flash_conn *fnode_conn;
  5735. struct ql4_chap_table chap_tbl;
  5736. struct device *dev;
  5737. int parent_type;
  5738. int rc = 0;
  5739. dev = iscsi_find_flashnode_conn(fnode_sess);
  5740. if (!dev)
  5741. return -EIO;
  5742. fnode_conn = iscsi_dev_to_flash_conn(dev);
  5743. switch (param) {
  5744. case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
  5745. rc = sprintf(buf, "%u\n", fnode_conn->is_fw_assigned_ipv6);
  5746. break;
  5747. case ISCSI_FLASHNODE_PORTAL_TYPE:
  5748. rc = sprintf(buf, "%s\n", fnode_sess->portal_type);
  5749. break;
  5750. case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
  5751. rc = sprintf(buf, "%u\n", fnode_sess->auto_snd_tgt_disable);
  5752. break;
  5753. case ISCSI_FLASHNODE_DISCOVERY_SESS:
  5754. rc = sprintf(buf, "%u\n", fnode_sess->discovery_sess);
  5755. break;
  5756. case ISCSI_FLASHNODE_ENTRY_EN:
  5757. rc = sprintf(buf, "%u\n", fnode_sess->entry_state);
  5758. break;
  5759. case ISCSI_FLASHNODE_HDR_DGST_EN:
  5760. rc = sprintf(buf, "%u\n", fnode_conn->hdrdgst_en);
  5761. break;
  5762. case ISCSI_FLASHNODE_DATA_DGST_EN:
  5763. rc = sprintf(buf, "%u\n", fnode_conn->datadgst_en);
  5764. break;
  5765. case ISCSI_FLASHNODE_IMM_DATA_EN:
  5766. rc = sprintf(buf, "%u\n", fnode_sess->imm_data_en);
  5767. break;
  5768. case ISCSI_FLASHNODE_INITIAL_R2T_EN:
  5769. rc = sprintf(buf, "%u\n", fnode_sess->initial_r2t_en);
  5770. break;
  5771. case ISCSI_FLASHNODE_DATASEQ_INORDER:
  5772. rc = sprintf(buf, "%u\n", fnode_sess->dataseq_inorder_en);
  5773. break;
  5774. case ISCSI_FLASHNODE_PDU_INORDER:
  5775. rc = sprintf(buf, "%u\n", fnode_sess->pdu_inorder_en);
  5776. break;
  5777. case ISCSI_FLASHNODE_CHAP_AUTH_EN:
  5778. rc = sprintf(buf, "%u\n", fnode_sess->chap_auth_en);
  5779. break;
  5780. case ISCSI_FLASHNODE_SNACK_REQ_EN:
  5781. rc = sprintf(buf, "%u\n", fnode_conn->snack_req_en);
  5782. break;
  5783. case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
  5784. rc = sprintf(buf, "%u\n", fnode_sess->discovery_logout_en);
  5785. break;
  5786. case ISCSI_FLASHNODE_BIDI_CHAP_EN:
  5787. rc = sprintf(buf, "%u\n", fnode_sess->bidi_chap_en);
  5788. break;
  5789. case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
  5790. rc = sprintf(buf, "%u\n", fnode_sess->discovery_auth_optional);
  5791. break;
  5792. case ISCSI_FLASHNODE_ERL:
  5793. rc = sprintf(buf, "%u\n", fnode_sess->erl);
  5794. break;
  5795. case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
  5796. rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_stat);
  5797. break;
  5798. case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
  5799. rc = sprintf(buf, "%u\n", fnode_conn->tcp_nagle_disable);
  5800. break;
  5801. case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
  5802. rc = sprintf(buf, "%u\n", fnode_conn->tcp_wsf_disable);
  5803. break;
  5804. case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
  5805. rc = sprintf(buf, "%u\n", fnode_conn->tcp_timer_scale);
  5806. break;
  5807. case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
  5808. rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_en);
  5809. break;
  5810. case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
  5811. rc = sprintf(buf, "%u\n", fnode_conn->fragment_disable);
  5812. break;
  5813. case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
  5814. rc = sprintf(buf, "%u\n", fnode_conn->max_recv_dlength);
  5815. break;
  5816. case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
  5817. rc = sprintf(buf, "%u\n", fnode_conn->max_xmit_dlength);
  5818. break;
  5819. case ISCSI_FLASHNODE_FIRST_BURST:
  5820. rc = sprintf(buf, "%u\n", fnode_sess->first_burst);
  5821. break;
  5822. case ISCSI_FLASHNODE_DEF_TIME2WAIT:
  5823. rc = sprintf(buf, "%u\n", fnode_sess->time2wait);
  5824. break;
  5825. case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
  5826. rc = sprintf(buf, "%u\n", fnode_sess->time2retain);
  5827. break;
  5828. case ISCSI_FLASHNODE_MAX_R2T:
  5829. rc = sprintf(buf, "%u\n", fnode_sess->max_r2t);
  5830. break;
  5831. case ISCSI_FLASHNODE_KEEPALIVE_TMO:
  5832. rc = sprintf(buf, "%u\n", fnode_conn->keepalive_timeout);
  5833. break;
  5834. case ISCSI_FLASHNODE_ISID:
  5835. rc = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
  5836. fnode_sess->isid[0], fnode_sess->isid[1],
  5837. fnode_sess->isid[2], fnode_sess->isid[3],
  5838. fnode_sess->isid[4], fnode_sess->isid[5]);
  5839. break;
  5840. case ISCSI_FLASHNODE_TSID:
  5841. rc = sprintf(buf, "%u\n", fnode_sess->tsid);
  5842. break;
  5843. case ISCSI_FLASHNODE_PORT:
  5844. rc = sprintf(buf, "%d\n", fnode_conn->port);
  5845. break;
  5846. case ISCSI_FLASHNODE_MAX_BURST:
  5847. rc = sprintf(buf, "%u\n", fnode_sess->max_burst);
  5848. break;
  5849. case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
  5850. rc = sprintf(buf, "%u\n",
  5851. fnode_sess->default_taskmgmt_timeout);
  5852. break;
  5853. case ISCSI_FLASHNODE_IPADDR:
  5854. if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
  5855. rc = sprintf(buf, "%pI6\n", fnode_conn->ipaddress);
  5856. else
  5857. rc = sprintf(buf, "%pI4\n", fnode_conn->ipaddress);
  5858. break;
  5859. case ISCSI_FLASHNODE_ALIAS:
  5860. if (fnode_sess->targetalias)
  5861. rc = sprintf(buf, "%s\n", fnode_sess->targetalias);
  5862. else
  5863. rc = sprintf(buf, "\n");
  5864. break;
  5865. case ISCSI_FLASHNODE_REDIRECT_IPADDR:
  5866. if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
  5867. rc = sprintf(buf, "%pI6\n",
  5868. fnode_conn->redirect_ipaddr);
  5869. else
  5870. rc = sprintf(buf, "%pI4\n",
  5871. fnode_conn->redirect_ipaddr);
  5872. break;
  5873. case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
  5874. rc = sprintf(buf, "%u\n", fnode_conn->max_segment_size);
  5875. break;
  5876. case ISCSI_FLASHNODE_LOCAL_PORT:
  5877. rc = sprintf(buf, "%u\n", fnode_conn->local_port);
  5878. break;
  5879. case ISCSI_FLASHNODE_IPV4_TOS:
  5880. rc = sprintf(buf, "%u\n", fnode_conn->ipv4_tos);
  5881. break;
  5882. case ISCSI_FLASHNODE_IPV6_TC:
  5883. if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
  5884. rc = sprintf(buf, "%u\n",
  5885. fnode_conn->ipv6_traffic_class);
  5886. else
  5887. rc = sprintf(buf, "\n");
  5888. break;
  5889. case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
  5890. rc = sprintf(buf, "%u\n", fnode_conn->ipv6_flow_label);
  5891. break;
  5892. case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
  5893. if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
  5894. rc = sprintf(buf, "%pI6\n",
  5895. fnode_conn->link_local_ipv6_addr);
  5896. else
  5897. rc = sprintf(buf, "\n");
  5898. break;
  5899. case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
  5900. rc = sprintf(buf, "%u\n", fnode_sess->discovery_parent_idx);
  5901. break;
  5902. case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
  5903. if (fnode_sess->discovery_parent_type == DDB_ISNS)
  5904. parent_type = ISCSI_DISC_PARENT_ISNS;
  5905. else if (fnode_sess->discovery_parent_type == DDB_NO_LINK)
  5906. parent_type = ISCSI_DISC_PARENT_UNKNOWN;
  5907. else if (fnode_sess->discovery_parent_type < MAX_DDB_ENTRIES)
  5908. parent_type = ISCSI_DISC_PARENT_SENDTGT;
  5909. else
  5910. parent_type = ISCSI_DISC_PARENT_UNKNOWN;
  5911. rc = sprintf(buf, "%s\n",
  5912. iscsi_get_discovery_parent_name(parent_type));
  5913. break;
  5914. case ISCSI_FLASHNODE_NAME:
  5915. if (fnode_sess->targetname)
  5916. rc = sprintf(buf, "%s\n", fnode_sess->targetname);
  5917. else
  5918. rc = sprintf(buf, "\n");
  5919. break;
  5920. case ISCSI_FLASHNODE_TPGT:
  5921. rc = sprintf(buf, "%u\n", fnode_sess->tpgt);
  5922. break;
  5923. case ISCSI_FLASHNODE_TCP_XMIT_WSF:
  5924. rc = sprintf(buf, "%u\n", fnode_conn->tcp_xmit_wsf);
  5925. break;
  5926. case ISCSI_FLASHNODE_TCP_RECV_WSF:
  5927. rc = sprintf(buf, "%u\n", fnode_conn->tcp_recv_wsf);
  5928. break;
  5929. case ISCSI_FLASHNODE_CHAP_OUT_IDX:
  5930. rc = sprintf(buf, "%u\n", fnode_sess->chap_out_idx);
  5931. break;
  5932. case ISCSI_FLASHNODE_USERNAME:
  5933. if (fnode_sess->chap_auth_en) {
  5934. qla4xxx_get_uni_chap_at_index(ha,
  5935. chap_tbl.name,
  5936. chap_tbl.secret,
  5937. fnode_sess->chap_out_idx);
  5938. rc = sprintf(buf, "%s\n", chap_tbl.name);
  5939. } else {
  5940. rc = sprintf(buf, "\n");
  5941. }
  5942. break;
  5943. case ISCSI_FLASHNODE_PASSWORD:
  5944. if (fnode_sess->chap_auth_en) {
  5945. qla4xxx_get_uni_chap_at_index(ha,
  5946. chap_tbl.name,
  5947. chap_tbl.secret,
  5948. fnode_sess->chap_out_idx);
  5949. rc = sprintf(buf, "%s\n", chap_tbl.secret);
  5950. } else {
  5951. rc = sprintf(buf, "\n");
  5952. }
  5953. break;
  5954. case ISCSI_FLASHNODE_STATSN:
  5955. rc = sprintf(buf, "%u\n", fnode_conn->statsn);
  5956. break;
  5957. case ISCSI_FLASHNODE_EXP_STATSN:
  5958. rc = sprintf(buf, "%u\n", fnode_conn->exp_statsn);
  5959. break;
  5960. case ISCSI_FLASHNODE_IS_BOOT_TGT:
  5961. rc = sprintf(buf, "%u\n", fnode_sess->is_boot_target);
  5962. break;
  5963. default:
  5964. rc = -ENOSYS;
  5965. break;
  5966. }
  5967. put_device(dev);
  5968. return rc;
  5969. }
  5970. /**
  5971. * qla4xxx_sysfs_ddb_set_param - Set parameter for firmware DDB entry
  5972. * @fnode_sess: pointer to session attrs of flash ddb entry
  5973. * @fnode_conn: pointer to connection attrs of flash ddb entry
  5974. * @data: Parameters and their values to update
  5975. * @len: len of data
  5976. *
  5977. * This sets the parameter of flash ddb entry and writes them to flash
  5978. **/
  5979. static int
  5980. qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
  5981. struct iscsi_bus_flash_conn *fnode_conn,
  5982. void *data, int len)
  5983. {
  5984. struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
  5985. struct scsi_qla_host *ha = to_qla_host(shost);
  5986. struct iscsi_flashnode_param_info *fnode_param;
  5987. struct ql4_chap_table chap_tbl;
  5988. struct nlattr *attr;
  5989. uint16_t chap_out_idx = INVALID_ENTRY;
  5990. int rc = QLA_ERROR;
  5991. uint32_t rem = len;
  5992. memset((void *)&chap_tbl, 0, sizeof(chap_tbl));
  5993. nla_for_each_attr(attr, data, len, rem) {
  5994. fnode_param = nla_data(attr);
  5995. switch (fnode_param->param) {
  5996. case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
  5997. fnode_conn->is_fw_assigned_ipv6 = fnode_param->value[0];
  5998. break;
  5999. case ISCSI_FLASHNODE_PORTAL_TYPE:
  6000. memcpy(fnode_sess->portal_type, fnode_param->value,
  6001. strlen(fnode_sess->portal_type));
  6002. break;
  6003. case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
  6004. fnode_sess->auto_snd_tgt_disable =
  6005. fnode_param->value[0];
  6006. break;
  6007. case ISCSI_FLASHNODE_DISCOVERY_SESS:
  6008. fnode_sess->discovery_sess = fnode_param->value[0];
  6009. break;
  6010. case ISCSI_FLASHNODE_ENTRY_EN:
  6011. fnode_sess->entry_state = fnode_param->value[0];
  6012. break;
  6013. case ISCSI_FLASHNODE_HDR_DGST_EN:
  6014. fnode_conn->hdrdgst_en = fnode_param->value[0];
  6015. break;
  6016. case ISCSI_FLASHNODE_DATA_DGST_EN:
  6017. fnode_conn->datadgst_en = fnode_param->value[0];
  6018. break;
  6019. case ISCSI_FLASHNODE_IMM_DATA_EN:
  6020. fnode_sess->imm_data_en = fnode_param->value[0];
  6021. break;
  6022. case ISCSI_FLASHNODE_INITIAL_R2T_EN:
  6023. fnode_sess->initial_r2t_en = fnode_param->value[0];
  6024. break;
  6025. case ISCSI_FLASHNODE_DATASEQ_INORDER:
  6026. fnode_sess->dataseq_inorder_en = fnode_param->value[0];
  6027. break;
  6028. case ISCSI_FLASHNODE_PDU_INORDER:
  6029. fnode_sess->pdu_inorder_en = fnode_param->value[0];
  6030. break;
  6031. case ISCSI_FLASHNODE_CHAP_AUTH_EN:
  6032. fnode_sess->chap_auth_en = fnode_param->value[0];
  6033. /* Invalidate chap index if chap auth is disabled */
  6034. if (!fnode_sess->chap_auth_en)
  6035. fnode_sess->chap_out_idx = INVALID_ENTRY;
  6036. break;
  6037. case ISCSI_FLASHNODE_SNACK_REQ_EN:
  6038. fnode_conn->snack_req_en = fnode_param->value[0];
  6039. break;
  6040. case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
  6041. fnode_sess->discovery_logout_en = fnode_param->value[0];
  6042. break;
  6043. case ISCSI_FLASHNODE_BIDI_CHAP_EN:
  6044. fnode_sess->bidi_chap_en = fnode_param->value[0];
  6045. break;
  6046. case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
  6047. fnode_sess->discovery_auth_optional =
  6048. fnode_param->value[0];
  6049. break;
  6050. case ISCSI_FLASHNODE_ERL:
  6051. fnode_sess->erl = fnode_param->value[0];
  6052. break;
  6053. case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
  6054. fnode_conn->tcp_timestamp_stat = fnode_param->value[0];
  6055. break;
  6056. case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
  6057. fnode_conn->tcp_nagle_disable = fnode_param->value[0];
  6058. break;
  6059. case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
  6060. fnode_conn->tcp_wsf_disable = fnode_param->value[0];
  6061. break;
  6062. case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
  6063. fnode_conn->tcp_timer_scale = fnode_param->value[0];
  6064. break;
  6065. case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
  6066. fnode_conn->tcp_timestamp_en = fnode_param->value[0];
  6067. break;
  6068. case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
  6069. fnode_conn->fragment_disable = fnode_param->value[0];
  6070. break;
  6071. case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
  6072. fnode_conn->max_recv_dlength =
  6073. *(unsigned *)fnode_param->value;
  6074. break;
  6075. case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
  6076. fnode_conn->max_xmit_dlength =
  6077. *(unsigned *)fnode_param->value;
  6078. break;
  6079. case ISCSI_FLASHNODE_FIRST_BURST:
  6080. fnode_sess->first_burst =
  6081. *(unsigned *)fnode_param->value;
  6082. break;
  6083. case ISCSI_FLASHNODE_DEF_TIME2WAIT:
  6084. fnode_sess->time2wait = *(uint16_t *)fnode_param->value;
  6085. break;
  6086. case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
  6087. fnode_sess->time2retain =
  6088. *(uint16_t *)fnode_param->value;
  6089. break;
  6090. case ISCSI_FLASHNODE_MAX_R2T:
  6091. fnode_sess->max_r2t =
  6092. *(uint16_t *)fnode_param->value;
  6093. break;
  6094. case ISCSI_FLASHNODE_KEEPALIVE_TMO:
  6095. fnode_conn->keepalive_timeout =
  6096. *(uint16_t *)fnode_param->value;
  6097. break;
  6098. case ISCSI_FLASHNODE_ISID:
  6099. memcpy(fnode_sess->isid, fnode_param->value,
  6100. sizeof(fnode_sess->isid));
  6101. break;
  6102. case ISCSI_FLASHNODE_TSID:
  6103. fnode_sess->tsid = *(uint16_t *)fnode_param->value;
  6104. break;
  6105. case ISCSI_FLASHNODE_PORT:
  6106. fnode_conn->port = *(uint16_t *)fnode_param->value;
  6107. break;
  6108. case ISCSI_FLASHNODE_MAX_BURST:
  6109. fnode_sess->max_burst = *(unsigned *)fnode_param->value;
  6110. break;
  6111. case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
  6112. fnode_sess->default_taskmgmt_timeout =
  6113. *(uint16_t *)fnode_param->value;
  6114. break;
  6115. case ISCSI_FLASHNODE_IPADDR:
  6116. memcpy(fnode_conn->ipaddress, fnode_param->value,
  6117. IPv6_ADDR_LEN);
  6118. break;
  6119. case ISCSI_FLASHNODE_ALIAS:
  6120. rc = iscsi_switch_str_param(&fnode_sess->targetalias,
  6121. (char *)fnode_param->value);
  6122. break;
  6123. case ISCSI_FLASHNODE_REDIRECT_IPADDR:
  6124. memcpy(fnode_conn->redirect_ipaddr, fnode_param->value,
  6125. IPv6_ADDR_LEN);
  6126. break;
  6127. case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
  6128. fnode_conn->max_segment_size =
  6129. *(unsigned *)fnode_param->value;
  6130. break;
  6131. case ISCSI_FLASHNODE_LOCAL_PORT:
  6132. fnode_conn->local_port =
  6133. *(uint16_t *)fnode_param->value;
  6134. break;
  6135. case ISCSI_FLASHNODE_IPV4_TOS:
  6136. fnode_conn->ipv4_tos = fnode_param->value[0];
  6137. break;
  6138. case ISCSI_FLASHNODE_IPV6_TC:
  6139. fnode_conn->ipv6_traffic_class = fnode_param->value[0];
  6140. break;
  6141. case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
  6142. fnode_conn->ipv6_flow_label = fnode_param->value[0];
  6143. break;
  6144. case ISCSI_FLASHNODE_NAME:
  6145. rc = iscsi_switch_str_param(&fnode_sess->targetname,
  6146. (char *)fnode_param->value);
  6147. break;
  6148. case ISCSI_FLASHNODE_TPGT:
  6149. fnode_sess->tpgt = *(uint16_t *)fnode_param->value;
  6150. break;
  6151. case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
  6152. memcpy(fnode_conn->link_local_ipv6_addr,
  6153. fnode_param->value, IPv6_ADDR_LEN);
  6154. break;
  6155. case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
  6156. fnode_sess->discovery_parent_idx =
  6157. *(uint16_t *)fnode_param->value;
  6158. break;
  6159. case ISCSI_FLASHNODE_TCP_XMIT_WSF:
  6160. fnode_conn->tcp_xmit_wsf =
  6161. *(uint8_t *)fnode_param->value;
  6162. break;
  6163. case ISCSI_FLASHNODE_TCP_RECV_WSF:
  6164. fnode_conn->tcp_recv_wsf =
  6165. *(uint8_t *)fnode_param->value;
  6166. break;
  6167. case ISCSI_FLASHNODE_STATSN:
  6168. fnode_conn->statsn = *(uint32_t *)fnode_param->value;
  6169. break;
  6170. case ISCSI_FLASHNODE_EXP_STATSN:
  6171. fnode_conn->exp_statsn =
  6172. *(uint32_t *)fnode_param->value;
  6173. break;
  6174. case ISCSI_FLASHNODE_CHAP_OUT_IDX:
  6175. chap_out_idx = *(uint16_t *)fnode_param->value;
  6176. if (!qla4xxx_get_uni_chap_at_index(ha,
  6177. chap_tbl.name,
  6178. chap_tbl.secret,
  6179. chap_out_idx)) {
  6180. fnode_sess->chap_out_idx = chap_out_idx;
  6181. /* Enable chap auth if chap index is valid */
  6182. fnode_sess->chap_auth_en = QL4_PARAM_ENABLE;
  6183. }
  6184. break;
  6185. default:
  6186. ql4_printk(KERN_ERR, ha,
  6187. "%s: No such sysfs attribute\n", __func__);
  6188. rc = -ENOSYS;
  6189. goto exit_set_param;
  6190. }
  6191. }
  6192. rc = qla4xxx_sysfs_ddb_apply(fnode_sess, fnode_conn);
  6193. exit_set_param:
  6194. return rc;
  6195. }
  6196. /**
  6197. * qla4xxx_sysfs_ddb_delete - Delete firmware DDB entry
  6198. * @fnode_sess: pointer to session attrs of flash ddb entry
  6199. *
  6200. * This invalidates the flash ddb entry at the given index
  6201. **/
  6202. static int qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess)
  6203. {
  6204. struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
  6205. struct scsi_qla_host *ha = to_qla_host(shost);
  6206. uint32_t dev_db_start_offset;
  6207. uint32_t dev_db_end_offset;
  6208. struct dev_db_entry *fw_ddb_entry = NULL;
  6209. dma_addr_t fw_ddb_entry_dma;
  6210. uint16_t *ddb_cookie = NULL;
  6211. size_t ddb_size = 0;
  6212. void *pddb = NULL;
  6213. int target_id;
  6214. int rc = 0;
  6215. if (fnode_sess->is_boot_target) {
  6216. rc = -EPERM;
  6217. DEBUG2(ql4_printk(KERN_ERR, ha,
  6218. "%s: Deletion of boot target entry is not permitted.\n",
  6219. __func__));
  6220. goto exit_ddb_del;
  6221. }
  6222. if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT)
  6223. goto sysfs_ddb_del;
  6224. if (is_qla40XX(ha)) {
  6225. dev_db_start_offset = FLASH_OFFSET_DB_INFO;
  6226. dev_db_end_offset = FLASH_OFFSET_DB_END;
  6227. dev_db_start_offset += (fnode_sess->target_id *
  6228. sizeof(*fw_ddb_entry));
  6229. ddb_size = sizeof(*fw_ddb_entry);
  6230. } else {
  6231. dev_db_start_offset = FLASH_RAW_ACCESS_ADDR +
  6232. (ha->hw.flt_region_ddb << 2);
  6233. /* flt_ddb_size is DDB table size for both ports
  6234. * so divide it by 2 to calculate the offset for second port
  6235. */
  6236. if (ha->port_num == 1)
  6237. dev_db_start_offset += (ha->hw.flt_ddb_size / 2);
  6238. dev_db_end_offset = dev_db_start_offset +
  6239. (ha->hw.flt_ddb_size / 2);
  6240. dev_db_start_offset += (fnode_sess->target_id *
  6241. sizeof(*fw_ddb_entry));
  6242. dev_db_start_offset += offsetof(struct dev_db_entry, cookie);
  6243. ddb_size = sizeof(*ddb_cookie);
  6244. }
  6245. DEBUG2(ql4_printk(KERN_ERR, ha, "%s: start offset=%u, end offset=%u\n",
  6246. __func__, dev_db_start_offset, dev_db_end_offset));
  6247. if (dev_db_start_offset > dev_db_end_offset) {
  6248. rc = -EIO;
  6249. DEBUG2(ql4_printk(KERN_ERR, ha, "%s:Invalid DDB index %u\n",
  6250. __func__, fnode_sess->target_id));
  6251. goto exit_ddb_del;
  6252. }
  6253. pddb = dma_alloc_coherent(&ha->pdev->dev, ddb_size,
  6254. &fw_ddb_entry_dma, GFP_KERNEL);
  6255. if (!pddb) {
  6256. rc = -ENOMEM;
  6257. DEBUG2(ql4_printk(KERN_ERR, ha,
  6258. "%s: Unable to allocate dma buffer\n",
  6259. __func__));
  6260. goto exit_ddb_del;
  6261. }
  6262. if (is_qla40XX(ha)) {
  6263. fw_ddb_entry = pddb;
  6264. memset(fw_ddb_entry, 0, ddb_size);
  6265. ddb_cookie = &fw_ddb_entry->cookie;
  6266. } else {
  6267. ddb_cookie = pddb;
  6268. }
  6269. /* invalidate the cookie */
  6270. *ddb_cookie = 0xFFEE;
  6271. qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
  6272. ddb_size, FLASH_OPT_RMW_COMMIT);
  6273. sysfs_ddb_del:
  6274. target_id = fnode_sess->target_id;
  6275. iscsi_destroy_flashnode_sess(fnode_sess);
  6276. ql4_printk(KERN_INFO, ha,
  6277. "%s: session and conn entries for flashnode %u of host %lu deleted\n",
  6278. __func__, target_id, ha->host_no);
  6279. exit_ddb_del:
  6280. if (pddb)
  6281. dma_free_coherent(&ha->pdev->dev, ddb_size, pddb,
  6282. fw_ddb_entry_dma);
  6283. return rc;
  6284. }
  6285. /**
  6286. * qla4xxx_sysfs_ddb_export - Create sysfs entries for firmware DDBs
  6287. * @ha: pointer to adapter structure
  6288. *
  6289. * Export the firmware DDB for all send targets and normal targets to sysfs.
  6290. **/
  6291. static int qla4xxx_sysfs_ddb_export(struct scsi_qla_host *ha)
  6292. {
  6293. struct dev_db_entry *fw_ddb_entry = NULL;
  6294. dma_addr_t fw_ddb_entry_dma;
  6295. uint16_t max_ddbs;
  6296. uint16_t idx = 0;
  6297. int ret = QLA_SUCCESS;
  6298. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
  6299. sizeof(*fw_ddb_entry),
  6300. &fw_ddb_entry_dma, GFP_KERNEL);
  6301. if (!fw_ddb_entry) {
  6302. DEBUG2(ql4_printk(KERN_ERR, ha,
  6303. "%s: Unable to allocate dma buffer\n",
  6304. __func__));
  6305. return -ENOMEM;
  6306. }
  6307. max_ddbs = is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
  6308. MAX_DEV_DB_ENTRIES;
  6309. for (idx = 0; idx < max_ddbs; idx++) {
  6310. if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry, fw_ddb_entry_dma,
  6311. idx))
  6312. continue;
  6313. ret = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 0);
  6314. if (ret) {
  6315. ret = -EIO;
  6316. break;
  6317. }
  6318. }
  6319. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
  6320. fw_ddb_entry_dma);
  6321. return ret;
  6322. }
  6323. static void qla4xxx_sysfs_ddb_remove(struct scsi_qla_host *ha)
  6324. {
  6325. iscsi_destroy_all_flashnode(ha->host);
  6326. }
  6327. /**
  6328. * qla4xxx_build_ddb_list - Build ddb list and setup sessions
  6329. * @ha: pointer to adapter structure
  6330. * @is_reset: Is this init path or reset path
  6331. *
  6332. * Create a list of sendtargets (st) from firmware DDBs, issue send targets
  6333. * using connection open, then create the list of normal targets (nt)
  6334. * from firmware DDBs. Based on the list of nt setup session and connection
  6335. * objects.
  6336. **/
  6337. void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
  6338. {
  6339. uint16_t tmo = 0;
  6340. struct list_head list_st, list_nt;
  6341. struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
  6342. unsigned long wtime;
  6343. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  6344. set_bit(AF_BUILD_DDB_LIST, &ha->flags);
  6345. ha->is_reset = is_reset;
  6346. return;
  6347. }
  6348. INIT_LIST_HEAD(&list_st);
  6349. INIT_LIST_HEAD(&list_nt);
  6350. qla4xxx_build_st_list(ha, &list_st);
  6351. /* Before issuing conn open mbox, ensure all IPs states are configured
  6352. * Note, conn open fails if IPs are not configured
  6353. */
  6354. qla4xxx_wait_for_ip_configuration(ha);
  6355. /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
  6356. list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
  6357. qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
  6358. }
  6359. /* Wait to ensure all sendtargets are done for min 12 sec wait */
  6360. tmo = ((ha->def_timeout > LOGIN_TOV) &&
  6361. (ha->def_timeout < LOGIN_TOV * 10) ?
  6362. ha->def_timeout : LOGIN_TOV);
  6363. DEBUG2(ql4_printk(KERN_INFO, ha,
  6364. "Default time to wait for build ddb %d\n", tmo));
  6365. wtime = jiffies + (HZ * tmo);
  6366. do {
  6367. if (list_empty(&list_st))
  6368. break;
  6369. qla4xxx_remove_failed_ddb(ha, &list_st);
  6370. schedule_timeout_uninterruptible(HZ / 10);
  6371. } while (time_after(wtime, jiffies));
  6372. qla4xxx_build_nt_list(ha, &list_nt, &list_st, is_reset);
  6373. qla4xxx_free_ddb_list(&list_st);
  6374. qla4xxx_free_ddb_list(&list_nt);
  6375. qla4xxx_free_ddb_index(ha);
  6376. }
  6377. /**
  6378. * qla4xxx_wait_login_resp_boot_tgt - Wait for iSCSI boot target login
  6379. * response.
  6380. * @ha: pointer to adapter structure
  6381. *
  6382. * When the boot entry is normal iSCSI target then DF_BOOT_TGT flag will be
  6383. * set in DDB and we will wait for login response of boot targets during
  6384. * probe.
  6385. **/
  6386. static void qla4xxx_wait_login_resp_boot_tgt(struct scsi_qla_host *ha)
  6387. {
  6388. struct ddb_entry *ddb_entry;
  6389. struct dev_db_entry *fw_ddb_entry = NULL;
  6390. dma_addr_t fw_ddb_entry_dma;
  6391. unsigned long wtime;
  6392. uint32_t ddb_state;
  6393. int max_ddbs, idx, ret;
  6394. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  6395. MAX_DEV_DB_ENTRIES;
  6396. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  6397. &fw_ddb_entry_dma, GFP_KERNEL);
  6398. if (!fw_ddb_entry) {
  6399. ql4_printk(KERN_ERR, ha,
  6400. "%s: Unable to allocate dma buffer\n", __func__);
  6401. goto exit_login_resp;
  6402. }
  6403. wtime = jiffies + (HZ * BOOT_LOGIN_RESP_TOV);
  6404. for (idx = 0; idx < max_ddbs; idx++) {
  6405. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  6406. if (ddb_entry == NULL)
  6407. continue;
  6408. if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
  6409. DEBUG2(ql4_printk(KERN_INFO, ha,
  6410. "%s: DDB index [%d]\n", __func__,
  6411. ddb_entry->fw_ddb_index));
  6412. do {
  6413. ret = qla4xxx_get_fwddb_entry(ha,
  6414. ddb_entry->fw_ddb_index,
  6415. fw_ddb_entry, fw_ddb_entry_dma,
  6416. NULL, NULL, &ddb_state, NULL,
  6417. NULL, NULL);
  6418. if (ret == QLA_ERROR)
  6419. goto exit_login_resp;
  6420. if ((ddb_state == DDB_DS_SESSION_ACTIVE) ||
  6421. (ddb_state == DDB_DS_SESSION_FAILED))
  6422. break;
  6423. schedule_timeout_uninterruptible(HZ);
  6424. } while ((time_after(wtime, jiffies)));
  6425. if (!time_after(wtime, jiffies)) {
  6426. DEBUG2(ql4_printk(KERN_INFO, ha,
  6427. "%s: Login response wait timer expired\n",
  6428. __func__));
  6429. goto exit_login_resp;
  6430. }
  6431. }
  6432. }
  6433. exit_login_resp:
  6434. if (fw_ddb_entry)
  6435. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  6436. fw_ddb_entry, fw_ddb_entry_dma);
  6437. }
  6438. /**
  6439. * qla4xxx_probe_adapter - callback function to probe HBA
  6440. * @pdev: pointer to pci_dev structure
  6441. * @pci_device_id: pointer to pci_device entry
  6442. *
  6443. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  6444. * It returns zero if successful. It also initializes all data necessary for
  6445. * the driver.
  6446. **/
  6447. static int qla4xxx_probe_adapter(struct pci_dev *pdev,
  6448. const struct pci_device_id *ent)
  6449. {
  6450. int ret = -ENODEV, status;
  6451. struct Scsi_Host *host;
  6452. struct scsi_qla_host *ha;
  6453. uint8_t init_retry_count = 0;
  6454. char buf[34];
  6455. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  6456. uint32_t dev_state;
  6457. if (pci_enable_device(pdev))
  6458. return -1;
  6459. host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
  6460. if (host == NULL) {
  6461. printk(KERN_WARNING
  6462. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  6463. goto probe_disable_device;
  6464. }
  6465. /* Clear our data area */
  6466. ha = to_qla_host(host);
  6467. memset(ha, 0, sizeof(*ha));
  6468. /* Save the information from PCI BIOS. */
  6469. ha->pdev = pdev;
  6470. ha->host = host;
  6471. ha->host_no = host->host_no;
  6472. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  6473. pci_enable_pcie_error_reporting(pdev);
  6474. /* Setup Runtime configurable options */
  6475. if (is_qla8022(ha)) {
  6476. ha->isp_ops = &qla4_82xx_isp_ops;
  6477. ha->reg_tbl = (uint32_t *) qla4_82xx_reg_tbl;
  6478. ha->qdr_sn_window = -1;
  6479. ha->ddr_mn_window = -1;
  6480. ha->curr_window = 255;
  6481. nx_legacy_intr = &legacy_intr[ha->func_num];
  6482. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  6483. ha->nx_legacy_intr.tgt_status_reg =
  6484. nx_legacy_intr->tgt_status_reg;
  6485. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  6486. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  6487. } else if (is_qla8032(ha) || is_qla8042(ha)) {
  6488. ha->isp_ops = &qla4_83xx_isp_ops;
  6489. ha->reg_tbl = (uint32_t *)qla4_83xx_reg_tbl;
  6490. } else {
  6491. ha->isp_ops = &qla4xxx_isp_ops;
  6492. }
  6493. if (is_qla80XX(ha)) {
  6494. rwlock_init(&ha->hw_lock);
  6495. ha->pf_bit = ha->func_num << 16;
  6496. /* Set EEH reset type to fundamental if required by hba */
  6497. pdev->needs_freset = 1;
  6498. }
  6499. /* Configure PCI I/O space. */
  6500. ret = ha->isp_ops->iospace_config(ha);
  6501. if (ret)
  6502. goto probe_failed_ioconfig;
  6503. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  6504. pdev->device, pdev->irq, ha->reg);
  6505. qla4xxx_config_dma_addressing(ha);
  6506. /* Initialize lists and spinlocks. */
  6507. INIT_LIST_HEAD(&ha->free_srb_q);
  6508. mutex_init(&ha->mbox_sem);
  6509. mutex_init(&ha->chap_sem);
  6510. init_completion(&ha->mbx_intr_comp);
  6511. init_completion(&ha->disable_acb_comp);
  6512. spin_lock_init(&ha->hardware_lock);
  6513. spin_lock_init(&ha->work_lock);
  6514. /* Initialize work list */
  6515. INIT_LIST_HEAD(&ha->work_list);
  6516. /* Allocate dma buffers */
  6517. if (qla4xxx_mem_alloc(ha)) {
  6518. ql4_printk(KERN_WARNING, ha,
  6519. "[ERROR] Failed to allocate memory for adapter\n");
  6520. ret = -ENOMEM;
  6521. goto probe_failed;
  6522. }
  6523. host->cmd_per_lun = 3;
  6524. host->max_channel = 0;
  6525. host->max_lun = MAX_LUNS - 1;
  6526. host->max_id = MAX_TARGETS;
  6527. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  6528. host->can_queue = MAX_SRBS ;
  6529. host->transportt = qla4xxx_scsi_transport;
  6530. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  6531. if (ret) {
  6532. ql4_printk(KERN_WARNING, ha,
  6533. "%s: scsi_init_shared_tag_map failed\n", __func__);
  6534. goto probe_failed;
  6535. }
  6536. pci_set_drvdata(pdev, ha);
  6537. ret = scsi_add_host(host, &pdev->dev);
  6538. if (ret)
  6539. goto probe_failed;
  6540. if (is_qla80XX(ha))
  6541. qla4_8xxx_get_flash_info(ha);
  6542. if (is_qla8032(ha) || is_qla8042(ha)) {
  6543. qla4_83xx_read_reset_template(ha);
  6544. /*
  6545. * NOTE: If ql4dontresethba==1, set IDC_CTRL DONTRESET_BIT0.
  6546. * If DONRESET_BIT0 is set, drivers should not set dev_state
  6547. * to NEED_RESET. But if NEED_RESET is set, drivers should
  6548. * should honor the reset.
  6549. */
  6550. if (ql4xdontresethba == 1)
  6551. qla4_83xx_set_idc_dontreset(ha);
  6552. }
  6553. /*
  6554. * Initialize the Host adapter request/response queues and
  6555. * firmware
  6556. * NOTE: interrupts enabled upon successful completion
  6557. */
  6558. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  6559. /* Dont retry adapter initialization if IRQ allocation failed */
  6560. if (is_qla80XX(ha) && !test_bit(AF_IRQ_ATTACHED, &ha->flags)) {
  6561. ql4_printk(KERN_WARNING, ha, "%s: Skipping retry of adapter initialization\n",
  6562. __func__);
  6563. goto skip_retry_init;
  6564. }
  6565. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  6566. init_retry_count++ < MAX_INIT_RETRIES) {
  6567. if (is_qla80XX(ha)) {
  6568. ha->isp_ops->idc_lock(ha);
  6569. dev_state = qla4_8xxx_rd_direct(ha,
  6570. QLA8XXX_CRB_DEV_STATE);
  6571. ha->isp_ops->idc_unlock(ha);
  6572. if (dev_state == QLA8XXX_DEV_FAILED) {
  6573. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  6574. "initialize adapter. H/W is in failed state\n",
  6575. __func__);
  6576. break;
  6577. }
  6578. }
  6579. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  6580. "(%d)\n", __func__, init_retry_count));
  6581. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  6582. continue;
  6583. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  6584. }
  6585. skip_retry_init:
  6586. if (!test_bit(AF_ONLINE, &ha->flags)) {
  6587. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  6588. if ((is_qla8022(ha) && ql4xdontresethba) ||
  6589. ((is_qla8032(ha) || is_qla8042(ha)) &&
  6590. qla4_83xx_idc_dontreset(ha))) {
  6591. /* Put the device in failed state. */
  6592. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  6593. ha->isp_ops->idc_lock(ha);
  6594. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  6595. QLA8XXX_DEV_FAILED);
  6596. ha->isp_ops->idc_unlock(ha);
  6597. }
  6598. ret = -ENODEV;
  6599. goto remove_host;
  6600. }
  6601. /* Startup the kernel thread for this host adapter. */
  6602. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  6603. "qla4xxx_dpc\n", __func__));
  6604. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  6605. ha->dpc_thread = create_singlethread_workqueue(buf);
  6606. if (!ha->dpc_thread) {
  6607. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  6608. ret = -ENODEV;
  6609. goto remove_host;
  6610. }
  6611. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  6612. ha->task_wq = alloc_workqueue("qla4xxx_%lu_task", WQ_MEM_RECLAIM, 1,
  6613. ha->host_no);
  6614. if (!ha->task_wq) {
  6615. ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
  6616. ret = -ENODEV;
  6617. goto remove_host;
  6618. }
  6619. /*
  6620. * For ISP-8XXX, request_irqs is called in qla4_8xxx_load_risc
  6621. * (which is called indirectly by qla4xxx_initialize_adapter),
  6622. * so that irqs will be registered after crbinit but before
  6623. * mbx_intr_enable.
  6624. */
  6625. if (is_qla40XX(ha)) {
  6626. ret = qla4xxx_request_irqs(ha);
  6627. if (ret) {
  6628. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  6629. "interrupt %d already in use.\n", pdev->irq);
  6630. goto remove_host;
  6631. }
  6632. }
  6633. pci_save_state(ha->pdev);
  6634. ha->isp_ops->enable_intrs(ha);
  6635. /* Start timer thread. */
  6636. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  6637. set_bit(AF_INIT_DONE, &ha->flags);
  6638. qla4_8xxx_alloc_sysfs_attr(ha);
  6639. printk(KERN_INFO
  6640. " QLogic iSCSI HBA Driver version: %s\n"
  6641. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  6642. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  6643. ha->host_no, ha->fw_info.fw_major, ha->fw_info.fw_minor,
  6644. ha->fw_info.fw_patch, ha->fw_info.fw_build);
  6645. /* Set the driver version */
  6646. if (is_qla80XX(ha))
  6647. qla4_8xxx_set_param(ha, SET_DRVR_VERSION);
  6648. if (qla4xxx_setup_boot_info(ha))
  6649. ql4_printk(KERN_ERR, ha,
  6650. "%s: No iSCSI boot target configured\n", __func__);
  6651. if (qla4xxx_sysfs_ddb_export(ha))
  6652. ql4_printk(KERN_ERR, ha,
  6653. "%s: Error exporting ddb to sysfs\n", __func__);
  6654. /* Perform the build ddb list and login to each */
  6655. qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
  6656. iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
  6657. qla4xxx_wait_login_resp_boot_tgt(ha);
  6658. qla4xxx_create_chap_list(ha);
  6659. qla4xxx_create_ifaces(ha);
  6660. return 0;
  6661. remove_host:
  6662. scsi_remove_host(ha->host);
  6663. probe_failed:
  6664. qla4xxx_free_adapter(ha);
  6665. probe_failed_ioconfig:
  6666. pci_disable_pcie_error_reporting(pdev);
  6667. scsi_host_put(ha->host);
  6668. probe_disable_device:
  6669. pci_disable_device(pdev);
  6670. return ret;
  6671. }
  6672. /**
  6673. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  6674. * @ha: pointer to adapter structure
  6675. *
  6676. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  6677. * so that the other port will not re-initialize while in the process of
  6678. * removing the ha due to driver unload or hba hotplug.
  6679. **/
  6680. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  6681. {
  6682. struct scsi_qla_host *other_ha = NULL;
  6683. struct pci_dev *other_pdev = NULL;
  6684. int fn = ISP4XXX_PCI_FN_2;
  6685. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  6686. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  6687. fn = ISP4XXX_PCI_FN_1;
  6688. other_pdev =
  6689. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  6690. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  6691. fn));
  6692. /* Get other_ha if other_pdev is valid and state is enable*/
  6693. if (other_pdev) {
  6694. if (atomic_read(&other_pdev->enable_cnt)) {
  6695. other_ha = pci_get_drvdata(other_pdev);
  6696. if (other_ha) {
  6697. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  6698. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  6699. "Prevent %s reinit\n", __func__,
  6700. dev_name(&other_ha->pdev->dev)));
  6701. }
  6702. }
  6703. pci_dev_put(other_pdev);
  6704. }
  6705. }
  6706. static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
  6707. {
  6708. struct ddb_entry *ddb_entry;
  6709. int options;
  6710. int idx;
  6711. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  6712. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  6713. if ((ddb_entry != NULL) &&
  6714. (ddb_entry->ddb_type == FLASH_DDB)) {
  6715. options = LOGOUT_OPTION_CLOSE_SESSION;
  6716. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
  6717. == QLA_ERROR)
  6718. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
  6719. __func__);
  6720. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  6721. /*
  6722. * we have decremented the reference count of the driver
  6723. * when we setup the session to have the driver unload
  6724. * to be seamless without actually destroying the
  6725. * session
  6726. **/
  6727. try_module_get(qla4xxx_iscsi_transport.owner);
  6728. iscsi_destroy_endpoint(ddb_entry->conn->ep);
  6729. qla4xxx_free_ddb(ha, ddb_entry);
  6730. iscsi_session_teardown(ddb_entry->sess);
  6731. }
  6732. }
  6733. }
  6734. /**
  6735. * qla4xxx_remove_adapter - callback function to remove adapter.
  6736. * @pci_dev: PCI device pointer
  6737. **/
  6738. static void qla4xxx_remove_adapter(struct pci_dev *pdev)
  6739. {
  6740. struct scsi_qla_host *ha;
  6741. /*
  6742. * If the PCI device is disabled then it means probe_adapter had
  6743. * failed and resources already cleaned up on probe_adapter exit.
  6744. */
  6745. if (!pci_is_enabled(pdev))
  6746. return;
  6747. ha = pci_get_drvdata(pdev);
  6748. if (is_qla40XX(ha))
  6749. qla4xxx_prevent_other_port_reinit(ha);
  6750. /* destroy iface from sysfs */
  6751. qla4xxx_destroy_ifaces(ha);
  6752. if ((!ql4xdisablesysfsboot) && ha->boot_kset)
  6753. iscsi_boot_destroy_kset(ha->boot_kset);
  6754. qla4xxx_destroy_fw_ddb_session(ha);
  6755. qla4_8xxx_free_sysfs_attr(ha);
  6756. qla4xxx_sysfs_ddb_remove(ha);
  6757. scsi_remove_host(ha->host);
  6758. qla4xxx_free_adapter(ha);
  6759. scsi_host_put(ha->host);
  6760. pci_disable_pcie_error_reporting(pdev);
  6761. pci_disable_device(pdev);
  6762. pci_set_drvdata(pdev, NULL);
  6763. }
  6764. /**
  6765. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  6766. * @ha: HA context
  6767. *
  6768. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  6769. * supported addressing method.
  6770. */
  6771. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  6772. {
  6773. int retval;
  6774. /* Update our PCI device dma_mask for full 64 bit mask */
  6775. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  6776. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  6777. dev_dbg(&ha->pdev->dev,
  6778. "Failed to set 64 bit PCI consistent mask; "
  6779. "using 32 bit.\n");
  6780. retval = pci_set_consistent_dma_mask(ha->pdev,
  6781. DMA_BIT_MASK(32));
  6782. }
  6783. } else
  6784. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  6785. }
  6786. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  6787. {
  6788. struct iscsi_cls_session *cls_sess;
  6789. struct iscsi_session *sess;
  6790. struct ddb_entry *ddb;
  6791. int queue_depth = QL4_DEF_QDEPTH;
  6792. cls_sess = starget_to_session(sdev->sdev_target);
  6793. sess = cls_sess->dd_data;
  6794. ddb = sess->dd_data;
  6795. sdev->hostdata = ddb;
  6796. sdev->tagged_supported = 1;
  6797. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  6798. queue_depth = ql4xmaxqdepth;
  6799. scsi_activate_tcq(sdev, queue_depth);
  6800. return 0;
  6801. }
  6802. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  6803. {
  6804. sdev->tagged_supported = 1;
  6805. return 0;
  6806. }
  6807. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  6808. {
  6809. scsi_deactivate_tcq(sdev, 1);
  6810. }
  6811. static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
  6812. int reason)
  6813. {
  6814. if (!ql4xqfulltracking)
  6815. return -EOPNOTSUPP;
  6816. return iscsi_change_queue_depth(sdev, qdepth, reason);
  6817. }
  6818. /**
  6819. * qla4xxx_del_from_active_array - returns an active srb
  6820. * @ha: Pointer to host adapter structure.
  6821. * @index: index into the active_array
  6822. *
  6823. * This routine removes and returns the srb at the specified index
  6824. **/
  6825. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  6826. uint32_t index)
  6827. {
  6828. struct srb *srb = NULL;
  6829. struct scsi_cmnd *cmd = NULL;
  6830. cmd = scsi_host_find_tag(ha->host, index);
  6831. if (!cmd)
  6832. return srb;
  6833. srb = (struct srb *)CMD_SP(cmd);
  6834. if (!srb)
  6835. return srb;
  6836. /* update counters */
  6837. if (srb->flags & SRB_DMA_VALID) {
  6838. ha->iocb_cnt -= srb->iocb_cnt;
  6839. if (srb->cmd)
  6840. srb->cmd->host_scribble =
  6841. (unsigned char *)(unsigned long) MAX_SRBS;
  6842. }
  6843. return srb;
  6844. }
  6845. /**
  6846. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  6847. * @ha: Pointer to host adapter structure.
  6848. * @cmd: Scsi Command to wait on.
  6849. *
  6850. * This routine waits for the command to be returned by the Firmware
  6851. * for some max time.
  6852. **/
  6853. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  6854. struct scsi_cmnd *cmd)
  6855. {
  6856. int done = 0;
  6857. struct srb *rp;
  6858. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  6859. int ret = SUCCESS;
  6860. /* Dont wait on command if PCI error is being handled
  6861. * by PCI AER driver
  6862. */
  6863. if (unlikely(pci_channel_offline(ha->pdev)) ||
  6864. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  6865. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  6866. ha->host_no, __func__);
  6867. return ret;
  6868. }
  6869. do {
  6870. /* Checking to see if its returned to OS */
  6871. rp = (struct srb *) CMD_SP(cmd);
  6872. if (rp == NULL) {
  6873. done++;
  6874. break;
  6875. }
  6876. msleep(2000);
  6877. } while (max_wait_time--);
  6878. return done;
  6879. }
  6880. /**
  6881. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  6882. * @ha: Pointer to host adapter structure
  6883. **/
  6884. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  6885. {
  6886. unsigned long wait_online;
  6887. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  6888. while (time_before(jiffies, wait_online)) {
  6889. if (adapter_up(ha))
  6890. return QLA_SUCCESS;
  6891. msleep(2000);
  6892. }
  6893. return QLA_ERROR;
  6894. }
  6895. /**
  6896. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  6897. * @ha: pointer to HBA
  6898. * @t: target id
  6899. * @l: lun id
  6900. *
  6901. * This function waits for all outstanding commands to a lun to complete. It
  6902. * returns 0 if all pending commands are returned and 1 otherwise.
  6903. **/
  6904. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  6905. struct scsi_target *stgt,
  6906. struct scsi_device *sdev)
  6907. {
  6908. int cnt;
  6909. int status = 0;
  6910. struct scsi_cmnd *cmd;
  6911. /*
  6912. * Waiting for all commands for the designated target or dev
  6913. * in the active array
  6914. */
  6915. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  6916. cmd = scsi_host_find_tag(ha->host, cnt);
  6917. if (cmd && stgt == scsi_target(cmd->device) &&
  6918. (!sdev || sdev == cmd->device)) {
  6919. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  6920. status++;
  6921. break;
  6922. }
  6923. }
  6924. }
  6925. return status;
  6926. }
  6927. /**
  6928. * qla4xxx_eh_abort - callback for abort task.
  6929. * @cmd: Pointer to Linux's SCSI command structure
  6930. *
  6931. * This routine is called by the Linux OS to abort the specified
  6932. * command.
  6933. **/
  6934. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  6935. {
  6936. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  6937. unsigned int id = cmd->device->id;
  6938. unsigned int lun = cmd->device->lun;
  6939. unsigned long flags;
  6940. struct srb *srb = NULL;
  6941. int ret = SUCCESS;
  6942. int wait = 0;
  6943. ql4_printk(KERN_INFO, ha,
  6944. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  6945. ha->host_no, id, lun, cmd);
  6946. spin_lock_irqsave(&ha->hardware_lock, flags);
  6947. srb = (struct srb *) CMD_SP(cmd);
  6948. if (!srb) {
  6949. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  6950. return SUCCESS;
  6951. }
  6952. kref_get(&srb->srb_ref);
  6953. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  6954. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  6955. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  6956. ha->host_no, id, lun));
  6957. ret = FAILED;
  6958. } else {
  6959. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  6960. ha->host_no, id, lun));
  6961. wait = 1;
  6962. }
  6963. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  6964. /* Wait for command to complete */
  6965. if (wait) {
  6966. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  6967. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  6968. ha->host_no, id, lun));
  6969. ret = FAILED;
  6970. }
  6971. }
  6972. ql4_printk(KERN_INFO, ha,
  6973. "scsi%ld:%d:%d: Abort command - %s\n",
  6974. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  6975. return ret;
  6976. }
  6977. /**
  6978. * qla4xxx_eh_device_reset - callback for target reset.
  6979. * @cmd: Pointer to Linux's SCSI command structure
  6980. *
  6981. * This routine is called by the Linux OS to reset all luns on the
  6982. * specified target.
  6983. **/
  6984. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  6985. {
  6986. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  6987. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  6988. int ret = FAILED, stat;
  6989. if (!ddb_entry)
  6990. return ret;
  6991. ret = iscsi_block_scsi_eh(cmd);
  6992. if (ret)
  6993. return ret;
  6994. ret = FAILED;
  6995. ql4_printk(KERN_INFO, ha,
  6996. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  6997. cmd->device->channel, cmd->device->id, cmd->device->lun);
  6998. DEBUG2(printk(KERN_INFO
  6999. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  7000. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  7001. cmd, jiffies, cmd->request->timeout / HZ,
  7002. ha->dpc_flags, cmd->result, cmd->allowed));
  7003. /* FIXME: wait for hba to go online */
  7004. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  7005. if (stat != QLA_SUCCESS) {
  7006. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  7007. goto eh_dev_reset_done;
  7008. }
  7009. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  7010. cmd->device)) {
  7011. ql4_printk(KERN_INFO, ha,
  7012. "DEVICE RESET FAILED - waiting for "
  7013. "commands.\n");
  7014. goto eh_dev_reset_done;
  7015. }
  7016. /* Send marker. */
  7017. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  7018. MM_LUN_RESET) != QLA_SUCCESS)
  7019. goto eh_dev_reset_done;
  7020. ql4_printk(KERN_INFO, ha,
  7021. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  7022. ha->host_no, cmd->device->channel, cmd->device->id,
  7023. cmd->device->lun);
  7024. ret = SUCCESS;
  7025. eh_dev_reset_done:
  7026. return ret;
  7027. }
  7028. /**
  7029. * qla4xxx_eh_target_reset - callback for target reset.
  7030. * @cmd: Pointer to Linux's SCSI command structure
  7031. *
  7032. * This routine is called by the Linux OS to reset the target.
  7033. **/
  7034. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  7035. {
  7036. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  7037. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  7038. int stat, ret;
  7039. if (!ddb_entry)
  7040. return FAILED;
  7041. ret = iscsi_block_scsi_eh(cmd);
  7042. if (ret)
  7043. return ret;
  7044. starget_printk(KERN_INFO, scsi_target(cmd->device),
  7045. "WARM TARGET RESET ISSUED.\n");
  7046. DEBUG2(printk(KERN_INFO
  7047. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  7048. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  7049. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  7050. ha->dpc_flags, cmd->result, cmd->allowed));
  7051. stat = qla4xxx_reset_target(ha, ddb_entry);
  7052. if (stat != QLA_SUCCESS) {
  7053. starget_printk(KERN_INFO, scsi_target(cmd->device),
  7054. "WARM TARGET RESET FAILED.\n");
  7055. return FAILED;
  7056. }
  7057. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  7058. NULL)) {
  7059. starget_printk(KERN_INFO, scsi_target(cmd->device),
  7060. "WARM TARGET DEVICE RESET FAILED - "
  7061. "waiting for commands.\n");
  7062. return FAILED;
  7063. }
  7064. /* Send marker. */
  7065. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  7066. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  7067. starget_printk(KERN_INFO, scsi_target(cmd->device),
  7068. "WARM TARGET DEVICE RESET FAILED - "
  7069. "marker iocb failed.\n");
  7070. return FAILED;
  7071. }
  7072. starget_printk(KERN_INFO, scsi_target(cmd->device),
  7073. "WARM TARGET RESET SUCCEEDED.\n");
  7074. return SUCCESS;
  7075. }
  7076. /**
  7077. * qla4xxx_is_eh_active - check if error handler is running
  7078. * @shost: Pointer to SCSI Host struct
  7079. *
  7080. * This routine finds that if reset host is called in EH
  7081. * scenario or from some application like sg_reset
  7082. **/
  7083. static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
  7084. {
  7085. if (shost->shost_state == SHOST_RECOVERY)
  7086. return 1;
  7087. return 0;
  7088. }
  7089. /**
  7090. * qla4xxx_eh_host_reset - kernel callback
  7091. * @cmd: Pointer to Linux's SCSI command structure
  7092. *
  7093. * This routine is invoked by the Linux kernel to perform fatal error
  7094. * recovery on the specified adapter.
  7095. **/
  7096. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  7097. {
  7098. int return_status = FAILED;
  7099. struct scsi_qla_host *ha;
  7100. ha = to_qla_host(cmd->device->host);
  7101. if ((is_qla8032(ha) || is_qla8042(ha)) && ql4xdontresethba)
  7102. qla4_83xx_set_idc_dontreset(ha);
  7103. /*
  7104. * For ISP8324 and ISP8042, if IDC_CTRL DONTRESET_BIT0 is set by other
  7105. * protocol drivers, we should not set device_state to NEED_RESET
  7106. */
  7107. if (ql4xdontresethba ||
  7108. ((is_qla8032(ha) || is_qla8042(ha)) &&
  7109. qla4_83xx_idc_dontreset(ha))) {
  7110. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  7111. ha->host_no, __func__));
  7112. /* Clear outstanding srb in queues */
  7113. if (qla4xxx_is_eh_active(cmd->device->host))
  7114. qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
  7115. return FAILED;
  7116. }
  7117. ql4_printk(KERN_INFO, ha,
  7118. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  7119. cmd->device->channel, cmd->device->id, cmd->device->lun);
  7120. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  7121. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  7122. "DEAD.\n", ha->host_no, cmd->device->channel,
  7123. __func__));
  7124. return FAILED;
  7125. }
  7126. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  7127. if (is_qla80XX(ha))
  7128. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  7129. else
  7130. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  7131. }
  7132. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  7133. return_status = SUCCESS;
  7134. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  7135. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  7136. return return_status;
  7137. }
  7138. static int qla4xxx_context_reset(struct scsi_qla_host *ha)
  7139. {
  7140. uint32_t mbox_cmd[MBOX_REG_COUNT];
  7141. uint32_t mbox_sts[MBOX_REG_COUNT];
  7142. struct addr_ctrl_blk_def *acb = NULL;
  7143. uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
  7144. int rval = QLA_SUCCESS;
  7145. dma_addr_t acb_dma;
  7146. acb = dma_alloc_coherent(&ha->pdev->dev,
  7147. sizeof(struct addr_ctrl_blk_def),
  7148. &acb_dma, GFP_KERNEL);
  7149. if (!acb) {
  7150. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
  7151. __func__);
  7152. rval = -ENOMEM;
  7153. goto exit_port_reset;
  7154. }
  7155. memset(acb, 0, acb_len);
  7156. rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
  7157. if (rval != QLA_SUCCESS) {
  7158. rval = -EIO;
  7159. goto exit_free_acb;
  7160. }
  7161. rval = qla4xxx_disable_acb(ha);
  7162. if (rval != QLA_SUCCESS) {
  7163. rval = -EIO;
  7164. goto exit_free_acb;
  7165. }
  7166. wait_for_completion_timeout(&ha->disable_acb_comp,
  7167. DISABLE_ACB_TOV * HZ);
  7168. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
  7169. if (rval != QLA_SUCCESS) {
  7170. rval = -EIO;
  7171. goto exit_free_acb;
  7172. }
  7173. exit_free_acb:
  7174. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
  7175. acb, acb_dma);
  7176. exit_port_reset:
  7177. DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
  7178. rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
  7179. return rval;
  7180. }
  7181. static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
  7182. {
  7183. struct scsi_qla_host *ha = to_qla_host(shost);
  7184. int rval = QLA_SUCCESS;
  7185. uint32_t idc_ctrl;
  7186. if (ql4xdontresethba) {
  7187. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
  7188. __func__));
  7189. rval = -EPERM;
  7190. goto exit_host_reset;
  7191. }
  7192. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  7193. goto recover_adapter;
  7194. switch (reset_type) {
  7195. case SCSI_ADAPTER_RESET:
  7196. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  7197. break;
  7198. case SCSI_FIRMWARE_RESET:
  7199. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  7200. if (is_qla80XX(ha))
  7201. /* set firmware context reset */
  7202. set_bit(DPC_RESET_HA_FW_CONTEXT,
  7203. &ha->dpc_flags);
  7204. else {
  7205. rval = qla4xxx_context_reset(ha);
  7206. goto exit_host_reset;
  7207. }
  7208. }
  7209. break;
  7210. }
  7211. recover_adapter:
  7212. /* For ISP8324 and ISP8042 set graceful reset bit in IDC_DRV_CTRL if
  7213. * reset is issued by application */
  7214. if ((is_qla8032(ha) || is_qla8042(ha)) &&
  7215. test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  7216. idc_ctrl = qla4_83xx_rd_reg(ha, QLA83XX_IDC_DRV_CTRL);
  7217. qla4_83xx_wr_reg(ha, QLA83XX_IDC_DRV_CTRL,
  7218. (idc_ctrl | GRACEFUL_RESET_BIT1));
  7219. }
  7220. rval = qla4xxx_recover_adapter(ha);
  7221. if (rval != QLA_SUCCESS) {
  7222. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
  7223. __func__));
  7224. rval = -EIO;
  7225. }
  7226. exit_host_reset:
  7227. return rval;
  7228. }
  7229. /* PCI AER driver recovers from all correctable errors w/o
  7230. * driver intervention. For uncorrectable errors PCI AER
  7231. * driver calls the following device driver's callbacks
  7232. *
  7233. * - Fatal Errors - link_reset
  7234. * - Non-Fatal Errors - driver's pci_error_detected() which
  7235. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  7236. *
  7237. * PCI AER driver calls
  7238. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  7239. * returns RECOVERED or NEED_RESET if fw_hung
  7240. * NEED_RESET - driver's slot_reset()
  7241. * DISCONNECT - device is dead & cannot recover
  7242. * RECOVERED - driver's pci_resume()
  7243. */
  7244. static pci_ers_result_t
  7245. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  7246. {
  7247. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  7248. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  7249. ha->host_no, __func__, state);
  7250. if (!is_aer_supported(ha))
  7251. return PCI_ERS_RESULT_NONE;
  7252. switch (state) {
  7253. case pci_channel_io_normal:
  7254. clear_bit(AF_EEH_BUSY, &ha->flags);
  7255. return PCI_ERS_RESULT_CAN_RECOVER;
  7256. case pci_channel_io_frozen:
  7257. set_bit(AF_EEH_BUSY, &ha->flags);
  7258. qla4xxx_mailbox_premature_completion(ha);
  7259. qla4xxx_free_irqs(ha);
  7260. pci_disable_device(pdev);
  7261. /* Return back all IOs */
  7262. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  7263. return PCI_ERS_RESULT_NEED_RESET;
  7264. case pci_channel_io_perm_failure:
  7265. set_bit(AF_EEH_BUSY, &ha->flags);
  7266. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  7267. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  7268. return PCI_ERS_RESULT_DISCONNECT;
  7269. }
  7270. return PCI_ERS_RESULT_NEED_RESET;
  7271. }
  7272. /**
  7273. * qla4xxx_pci_mmio_enabled() gets called if
  7274. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  7275. * and read/write to the device still works.
  7276. **/
  7277. static pci_ers_result_t
  7278. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  7279. {
  7280. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  7281. if (!is_aer_supported(ha))
  7282. return PCI_ERS_RESULT_NONE;
  7283. return PCI_ERS_RESULT_RECOVERED;
  7284. }
  7285. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  7286. {
  7287. uint32_t rval = QLA_ERROR;
  7288. int fn;
  7289. struct pci_dev *other_pdev = NULL;
  7290. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  7291. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  7292. if (test_bit(AF_ONLINE, &ha->flags)) {
  7293. clear_bit(AF_ONLINE, &ha->flags);
  7294. clear_bit(AF_LINK_UP, &ha->flags);
  7295. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  7296. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  7297. }
  7298. fn = PCI_FUNC(ha->pdev->devfn);
  7299. while (fn > 0) {
  7300. fn--;
  7301. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  7302. "func %x\n", ha->host_no, __func__, fn);
  7303. /* Get the pci device given the domain, bus,
  7304. * slot/function number */
  7305. other_pdev =
  7306. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  7307. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  7308. fn));
  7309. if (!other_pdev)
  7310. continue;
  7311. if (atomic_read(&other_pdev->enable_cnt)) {
  7312. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  7313. "func in enabled state%x\n", ha->host_no,
  7314. __func__, fn);
  7315. pci_dev_put(other_pdev);
  7316. break;
  7317. }
  7318. pci_dev_put(other_pdev);
  7319. }
  7320. /* The first function on the card, the reset owner will
  7321. * start & initialize the firmware. The other functions
  7322. * on the card will reset the firmware context
  7323. */
  7324. if (!fn) {
  7325. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  7326. "0x%x is the owner\n", ha->host_no, __func__,
  7327. ha->pdev->devfn);
  7328. ha->isp_ops->idc_lock(ha);
  7329. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  7330. QLA8XXX_DEV_COLD);
  7331. ha->isp_ops->idc_unlock(ha);
  7332. rval = qla4_8xxx_update_idc_reg(ha);
  7333. if (rval == QLA_ERROR) {
  7334. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: FAILED\n",
  7335. ha->host_no, __func__);
  7336. ha->isp_ops->idc_lock(ha);
  7337. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  7338. QLA8XXX_DEV_FAILED);
  7339. ha->isp_ops->idc_unlock(ha);
  7340. goto exit_error_recovery;
  7341. }
  7342. clear_bit(AF_FW_RECOVERY, &ha->flags);
  7343. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  7344. if (rval != QLA_SUCCESS) {
  7345. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  7346. "FAILED\n", ha->host_no, __func__);
  7347. ha->isp_ops->idc_lock(ha);
  7348. qla4_8xxx_clear_drv_active(ha);
  7349. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  7350. QLA8XXX_DEV_FAILED);
  7351. ha->isp_ops->idc_unlock(ha);
  7352. } else {
  7353. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  7354. "READY\n", ha->host_no, __func__);
  7355. ha->isp_ops->idc_lock(ha);
  7356. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
  7357. QLA8XXX_DEV_READY);
  7358. /* Clear driver state register */
  7359. qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, 0);
  7360. qla4_8xxx_set_drv_active(ha);
  7361. ha->isp_ops->idc_unlock(ha);
  7362. ha->isp_ops->enable_intrs(ha);
  7363. }
  7364. } else {
  7365. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  7366. "the reset owner\n", ha->host_no, __func__,
  7367. ha->pdev->devfn);
  7368. if ((qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE) ==
  7369. QLA8XXX_DEV_READY)) {
  7370. clear_bit(AF_FW_RECOVERY, &ha->flags);
  7371. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  7372. if (rval == QLA_SUCCESS)
  7373. ha->isp_ops->enable_intrs(ha);
  7374. ha->isp_ops->idc_lock(ha);
  7375. qla4_8xxx_set_drv_active(ha);
  7376. ha->isp_ops->idc_unlock(ha);
  7377. }
  7378. }
  7379. exit_error_recovery:
  7380. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  7381. return rval;
  7382. }
  7383. static pci_ers_result_t
  7384. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  7385. {
  7386. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  7387. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  7388. int rc;
  7389. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  7390. ha->host_no, __func__);
  7391. if (!is_aer_supported(ha))
  7392. return PCI_ERS_RESULT_NONE;
  7393. /* Restore the saved state of PCIe device -
  7394. * BAR registers, PCI Config space, PCIX, MSI,
  7395. * IOV states
  7396. */
  7397. pci_restore_state(pdev);
  7398. /* pci_restore_state() clears the saved_state flag of the device
  7399. * save restored state which resets saved_state flag
  7400. */
  7401. pci_save_state(pdev);
  7402. /* Initialize device or resume if in suspended state */
  7403. rc = pci_enable_device(pdev);
  7404. if (rc) {
  7405. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  7406. "device after reset\n", ha->host_no, __func__);
  7407. goto exit_slot_reset;
  7408. }
  7409. ha->isp_ops->disable_intrs(ha);
  7410. if (is_qla80XX(ha)) {
  7411. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  7412. ret = PCI_ERS_RESULT_RECOVERED;
  7413. goto exit_slot_reset;
  7414. } else
  7415. goto exit_slot_reset;
  7416. }
  7417. exit_slot_reset:
  7418. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  7419. "device after reset\n", ha->host_no, __func__, ret);
  7420. return ret;
  7421. }
  7422. static void
  7423. qla4xxx_pci_resume(struct pci_dev *pdev)
  7424. {
  7425. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  7426. int ret;
  7427. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  7428. ha->host_no, __func__);
  7429. ret = qla4xxx_wait_for_hba_online(ha);
  7430. if (ret != QLA_SUCCESS) {
  7431. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  7432. "resume I/O from slot/link_reset\n", ha->host_no,
  7433. __func__);
  7434. }
  7435. pci_cleanup_aer_uncorrect_error_status(pdev);
  7436. clear_bit(AF_EEH_BUSY, &ha->flags);
  7437. }
  7438. static const struct pci_error_handlers qla4xxx_err_handler = {
  7439. .error_detected = qla4xxx_pci_error_detected,
  7440. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  7441. .slot_reset = qla4xxx_pci_slot_reset,
  7442. .resume = qla4xxx_pci_resume,
  7443. };
  7444. static struct pci_device_id qla4xxx_pci_tbl[] = {
  7445. {
  7446. .vendor = PCI_VENDOR_ID_QLOGIC,
  7447. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  7448. .subvendor = PCI_ANY_ID,
  7449. .subdevice = PCI_ANY_ID,
  7450. },
  7451. {
  7452. .vendor = PCI_VENDOR_ID_QLOGIC,
  7453. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  7454. .subvendor = PCI_ANY_ID,
  7455. .subdevice = PCI_ANY_ID,
  7456. },
  7457. {
  7458. .vendor = PCI_VENDOR_ID_QLOGIC,
  7459. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  7460. .subvendor = PCI_ANY_ID,
  7461. .subdevice = PCI_ANY_ID,
  7462. },
  7463. {
  7464. .vendor = PCI_VENDOR_ID_QLOGIC,
  7465. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  7466. .subvendor = PCI_ANY_ID,
  7467. .subdevice = PCI_ANY_ID,
  7468. },
  7469. {
  7470. .vendor = PCI_VENDOR_ID_QLOGIC,
  7471. .device = PCI_DEVICE_ID_QLOGIC_ISP8324,
  7472. .subvendor = PCI_ANY_ID,
  7473. .subdevice = PCI_ANY_ID,
  7474. },
  7475. {
  7476. .vendor = PCI_VENDOR_ID_QLOGIC,
  7477. .device = PCI_DEVICE_ID_QLOGIC_ISP8042,
  7478. .subvendor = PCI_ANY_ID,
  7479. .subdevice = PCI_ANY_ID,
  7480. },
  7481. {0, 0},
  7482. };
  7483. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  7484. static struct pci_driver qla4xxx_pci_driver = {
  7485. .name = DRIVER_NAME,
  7486. .id_table = qla4xxx_pci_tbl,
  7487. .probe = qla4xxx_probe_adapter,
  7488. .remove = qla4xxx_remove_adapter,
  7489. .err_handler = &qla4xxx_err_handler,
  7490. };
  7491. static int __init qla4xxx_module_init(void)
  7492. {
  7493. int ret;
  7494. /* Allocate cache for SRBs. */
  7495. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  7496. SLAB_HWCACHE_ALIGN, NULL);
  7497. if (srb_cachep == NULL) {
  7498. printk(KERN_ERR
  7499. "%s: Unable to allocate SRB cache..."
  7500. "Failing load!\n", DRIVER_NAME);
  7501. ret = -ENOMEM;
  7502. goto no_srp_cache;
  7503. }
  7504. /* Derive version string. */
  7505. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  7506. if (ql4xextended_error_logging)
  7507. strcat(qla4xxx_version_str, "-debug");
  7508. qla4xxx_scsi_transport =
  7509. iscsi_register_transport(&qla4xxx_iscsi_transport);
  7510. if (!qla4xxx_scsi_transport){
  7511. ret = -ENODEV;
  7512. goto release_srb_cache;
  7513. }
  7514. ret = pci_register_driver(&qla4xxx_pci_driver);
  7515. if (ret)
  7516. goto unregister_transport;
  7517. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  7518. return 0;
  7519. unregister_transport:
  7520. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  7521. release_srb_cache:
  7522. kmem_cache_destroy(srb_cachep);
  7523. no_srp_cache:
  7524. return ret;
  7525. }
  7526. static void __exit qla4xxx_module_exit(void)
  7527. {
  7528. pci_unregister_driver(&qla4xxx_pci_driver);
  7529. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  7530. kmem_cache_destroy(srb_cachep);
  7531. }
  7532. module_init(qla4xxx_module_init);
  7533. module_exit(qla4xxx_module_exit);
  7534. MODULE_AUTHOR("QLogic Corporation");
  7535. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  7536. MODULE_LICENSE("GPL");
  7537. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);