ql4_os.c 165 KB

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