ql4_os.c 148 KB

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