ql4_os.c 176 KB

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