ql4_os.c 245 KB

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