ql4_os.c 155 KB

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