ql4_os.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369
  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 void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  1712. {
  1713. uint32_t fw_heartbeat_counter, halt_status;
  1714. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  1715. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  1716. if (fw_heartbeat_counter == 0xffffffff) {
  1717. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  1718. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  1719. ha->host_no, __func__));
  1720. return;
  1721. }
  1722. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  1723. ha->seconds_since_last_heartbeat++;
  1724. /* FW not alive after 2 seconds */
  1725. if (ha->seconds_since_last_heartbeat == 2) {
  1726. ha->seconds_since_last_heartbeat = 0;
  1727. halt_status = qla4_8xxx_rd_32(ha,
  1728. QLA82XX_PEG_HALT_STATUS1);
  1729. ql4_printk(KERN_INFO, ha,
  1730. "scsi(%ld): %s, Dumping hw/fw registers:\n "
  1731. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  1732. " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  1733. " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  1734. " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
  1735. ha->host_no, __func__, halt_status,
  1736. qla4_8xxx_rd_32(ha,
  1737. QLA82XX_PEG_HALT_STATUS2),
  1738. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  1739. 0x3c),
  1740. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  1741. 0x3c),
  1742. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  1743. 0x3c),
  1744. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  1745. 0x3c),
  1746. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  1747. 0x3c));
  1748. /* Since we cannot change dev_state in interrupt
  1749. * context, set appropriate DPC flag then wakeup
  1750. * DPC */
  1751. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  1752. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  1753. else {
  1754. printk("scsi%ld: %s: detect abort needed!\n",
  1755. ha->host_no, __func__);
  1756. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1757. }
  1758. qla4xxx_wake_dpc(ha);
  1759. qla4xxx_mailbox_premature_completion(ha);
  1760. }
  1761. } else
  1762. ha->seconds_since_last_heartbeat = 0;
  1763. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  1764. }
  1765. /**
  1766. * qla4_8xxx_watchdog - Poll dev state
  1767. * @ha: Pointer to host adapter structure.
  1768. *
  1769. * Context: Interrupt
  1770. **/
  1771. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  1772. {
  1773. uint32_t dev_state;
  1774. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  1775. /* don't poll if reset is going on */
  1776. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  1777. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1778. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
  1779. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  1780. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1781. if (!ql4xdontresethba) {
  1782. ql4_printk(KERN_INFO, ha, "%s: HW State: "
  1783. "NEED RESET!\n", __func__);
  1784. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1785. qla4xxx_wake_dpc(ha);
  1786. qla4xxx_mailbox_premature_completion(ha);
  1787. }
  1788. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  1789. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1790. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  1791. __func__);
  1792. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  1793. qla4xxx_wake_dpc(ha);
  1794. } else {
  1795. /* Check firmware health */
  1796. qla4_8xxx_check_fw_alive(ha);
  1797. }
  1798. }
  1799. }
  1800. void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  1801. {
  1802. struct iscsi_session *sess;
  1803. struct ddb_entry *ddb_entry;
  1804. struct scsi_qla_host *ha;
  1805. sess = cls_sess->dd_data;
  1806. ddb_entry = sess->dd_data;
  1807. ha = ddb_entry->ha;
  1808. if (!(ddb_entry->ddb_type == FLASH_DDB))
  1809. return;
  1810. if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1811. !iscsi_is_session_online(cls_sess)) {
  1812. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  1813. INVALID_ENTRY) {
  1814. if (atomic_read(&ddb_entry->retry_relogin_timer) ==
  1815. 0) {
  1816. atomic_set(&ddb_entry->retry_relogin_timer,
  1817. INVALID_ENTRY);
  1818. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  1819. set_bit(DF_RELOGIN, &ddb_entry->flags);
  1820. DEBUG2(ql4_printk(KERN_INFO, ha,
  1821. "%s: index [%d] login device\n",
  1822. __func__, ddb_entry->fw_ddb_index));
  1823. } else
  1824. atomic_dec(&ddb_entry->retry_relogin_timer);
  1825. }
  1826. }
  1827. /* Wait for relogin to timeout */
  1828. if (atomic_read(&ddb_entry->relogin_timer) &&
  1829. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  1830. /*
  1831. * If the relogin times out and the device is
  1832. * still NOT ONLINE then try and relogin again.
  1833. */
  1834. if (!iscsi_is_session_online(cls_sess)) {
  1835. /* Reset retry relogin timer */
  1836. atomic_inc(&ddb_entry->relogin_retry_count);
  1837. DEBUG2(ql4_printk(KERN_INFO, ha,
  1838. "%s: index[%d] relogin timed out-retrying"
  1839. " relogin (%d), retry (%d)\n", __func__,
  1840. ddb_entry->fw_ddb_index,
  1841. atomic_read(&ddb_entry->relogin_retry_count),
  1842. ddb_entry->default_time2wait + 4));
  1843. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  1844. atomic_set(&ddb_entry->retry_relogin_timer,
  1845. ddb_entry->default_time2wait + 4);
  1846. }
  1847. }
  1848. }
  1849. /**
  1850. * qla4xxx_timer - checks every second for work to do.
  1851. * @ha: Pointer to host adapter structure.
  1852. **/
  1853. static void qla4xxx_timer(struct scsi_qla_host *ha)
  1854. {
  1855. int start_dpc = 0;
  1856. uint16_t w;
  1857. iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
  1858. /* If we are in the middle of AER/EEH processing
  1859. * skip any processing and reschedule the timer
  1860. */
  1861. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1862. mod_timer(&ha->timer, jiffies + HZ);
  1863. return;
  1864. }
  1865. /* Hardware read to trigger an EEH error during mailbox waits. */
  1866. if (!pci_channel_offline(ha->pdev))
  1867. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  1868. if (is_qla8022(ha)) {
  1869. qla4_8xxx_watchdog(ha);
  1870. }
  1871. if (!is_qla8022(ha)) {
  1872. /* Check for heartbeat interval. */
  1873. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  1874. ha->heartbeat_interval != 0) {
  1875. ha->seconds_since_last_heartbeat++;
  1876. if (ha->seconds_since_last_heartbeat >
  1877. ha->heartbeat_interval + 2)
  1878. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1879. }
  1880. }
  1881. /* Wakeup the dpc routine for this adapter, if needed. */
  1882. if (start_dpc ||
  1883. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1884. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  1885. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  1886. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1887. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1888. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  1889. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  1890. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  1891. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  1892. test_bit(DPC_AEN, &ha->dpc_flags)) {
  1893. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  1894. " - dpc flags = 0x%lx\n",
  1895. ha->host_no, __func__, ha->dpc_flags));
  1896. qla4xxx_wake_dpc(ha);
  1897. }
  1898. /* Reschedule timer thread to call us back in one second */
  1899. mod_timer(&ha->timer, jiffies + HZ);
  1900. DEBUG2(ha->seconds_since_last_intr++);
  1901. }
  1902. /**
  1903. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  1904. * @ha: Pointer to host adapter structure.
  1905. *
  1906. * This routine stalls the driver until all outstanding commands are returned.
  1907. * Caller must release the Hardware Lock prior to calling this routine.
  1908. **/
  1909. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  1910. {
  1911. uint32_t index = 0;
  1912. unsigned long flags;
  1913. struct scsi_cmnd *cmd;
  1914. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  1915. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  1916. "complete\n", WAIT_CMD_TOV));
  1917. while (!time_after_eq(jiffies, wtime)) {
  1918. spin_lock_irqsave(&ha->hardware_lock, flags);
  1919. /* Find a command that hasn't completed. */
  1920. for (index = 0; index < ha->host->can_queue; index++) {
  1921. cmd = scsi_host_find_tag(ha->host, index);
  1922. /*
  1923. * We cannot just check if the index is valid,
  1924. * becase if we are run from the scsi eh, then
  1925. * the scsi/block layer is going to prevent
  1926. * the tag from being released.
  1927. */
  1928. if (cmd != NULL && CMD_SP(cmd))
  1929. break;
  1930. }
  1931. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1932. /* If No Commands are pending, wait is complete */
  1933. if (index == ha->host->can_queue)
  1934. return QLA_SUCCESS;
  1935. msleep(1000);
  1936. }
  1937. /* If we timed out on waiting for commands to come back
  1938. * return ERROR. */
  1939. return QLA_ERROR;
  1940. }
  1941. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  1942. {
  1943. uint32_t ctrl_status;
  1944. unsigned long flags = 0;
  1945. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  1946. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1947. return QLA_ERROR;
  1948. spin_lock_irqsave(&ha->hardware_lock, flags);
  1949. /*
  1950. * If the SCSI Reset Interrupt bit is set, clear it.
  1951. * Otherwise, the Soft Reset won't work.
  1952. */
  1953. ctrl_status = readw(&ha->reg->ctrl_status);
  1954. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  1955. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  1956. /* Issue Soft Reset */
  1957. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  1958. readl(&ha->reg->ctrl_status);
  1959. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1960. return QLA_SUCCESS;
  1961. }
  1962. /**
  1963. * qla4xxx_soft_reset - performs soft reset.
  1964. * @ha: Pointer to host adapter structure.
  1965. **/
  1966. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  1967. {
  1968. uint32_t max_wait_time;
  1969. unsigned long flags = 0;
  1970. int status;
  1971. uint32_t ctrl_status;
  1972. status = qla4xxx_hw_reset(ha);
  1973. if (status != QLA_SUCCESS)
  1974. return status;
  1975. status = QLA_ERROR;
  1976. /* Wait until the Network Reset Intr bit is cleared */
  1977. max_wait_time = RESET_INTR_TOV;
  1978. do {
  1979. spin_lock_irqsave(&ha->hardware_lock, flags);
  1980. ctrl_status = readw(&ha->reg->ctrl_status);
  1981. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1982. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  1983. break;
  1984. msleep(1000);
  1985. } while ((--max_wait_time));
  1986. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  1987. DEBUG2(printk(KERN_WARNING
  1988. "scsi%ld: Network Reset Intr not cleared by "
  1989. "Network function, clearing it now!\n",
  1990. ha->host_no));
  1991. spin_lock_irqsave(&ha->hardware_lock, flags);
  1992. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  1993. readl(&ha->reg->ctrl_status);
  1994. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1995. }
  1996. /* Wait until the firmware tells us the Soft Reset is done */
  1997. max_wait_time = SOFT_RESET_TOV;
  1998. do {
  1999. spin_lock_irqsave(&ha->hardware_lock, flags);
  2000. ctrl_status = readw(&ha->reg->ctrl_status);
  2001. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2002. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  2003. status = QLA_SUCCESS;
  2004. break;
  2005. }
  2006. msleep(1000);
  2007. } while ((--max_wait_time));
  2008. /*
  2009. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  2010. * after the soft reset has taken place.
  2011. */
  2012. spin_lock_irqsave(&ha->hardware_lock, flags);
  2013. ctrl_status = readw(&ha->reg->ctrl_status);
  2014. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  2015. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  2016. readl(&ha->reg->ctrl_status);
  2017. }
  2018. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2019. /* If soft reset fails then most probably the bios on other
  2020. * function is also enabled.
  2021. * Since the initialization is sequential the other fn
  2022. * wont be able to acknowledge the soft reset.
  2023. * Issue a force soft reset to workaround this scenario.
  2024. */
  2025. if (max_wait_time == 0) {
  2026. /* Issue Force Soft Reset */
  2027. spin_lock_irqsave(&ha->hardware_lock, flags);
  2028. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  2029. readl(&ha->reg->ctrl_status);
  2030. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2031. /* Wait until the firmware tells us the Soft Reset is done */
  2032. max_wait_time = SOFT_RESET_TOV;
  2033. do {
  2034. spin_lock_irqsave(&ha->hardware_lock, flags);
  2035. ctrl_status = readw(&ha->reg->ctrl_status);
  2036. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2037. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  2038. status = QLA_SUCCESS;
  2039. break;
  2040. }
  2041. msleep(1000);
  2042. } while ((--max_wait_time));
  2043. }
  2044. return status;
  2045. }
  2046. /**
  2047. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  2048. * @ha: Pointer to host adapter structure.
  2049. * @res: returned scsi status
  2050. *
  2051. * This routine is called just prior to a HARD RESET to return all
  2052. * outstanding commands back to the Operating System.
  2053. * Caller should make sure that the following locks are released
  2054. * before this calling routine: Hardware lock, and io_request_lock.
  2055. **/
  2056. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  2057. {
  2058. struct srb *srb;
  2059. int i;
  2060. unsigned long flags;
  2061. spin_lock_irqsave(&ha->hardware_lock, flags);
  2062. for (i = 0; i < ha->host->can_queue; i++) {
  2063. srb = qla4xxx_del_from_active_array(ha, i);
  2064. if (srb != NULL) {
  2065. srb->cmd->result = res;
  2066. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  2067. }
  2068. }
  2069. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2070. }
  2071. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  2072. {
  2073. clear_bit(AF_ONLINE, &ha->flags);
  2074. /* Disable the board */
  2075. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  2076. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2077. qla4xxx_mark_all_devices_missing(ha);
  2078. clear_bit(AF_INIT_DONE, &ha->flags);
  2079. }
  2080. static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
  2081. {
  2082. struct iscsi_session *sess;
  2083. struct ddb_entry *ddb_entry;
  2084. sess = cls_session->dd_data;
  2085. ddb_entry = sess->dd_data;
  2086. ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
  2087. if (ddb_entry->ddb_type == FLASH_DDB)
  2088. iscsi_block_session(ddb_entry->sess);
  2089. else
  2090. iscsi_session_failure(cls_session->dd_data,
  2091. ISCSI_ERR_CONN_FAILED);
  2092. }
  2093. /**
  2094. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  2095. * @ha: Pointer to host adapter structure.
  2096. **/
  2097. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  2098. {
  2099. int status = QLA_ERROR;
  2100. uint8_t reset_chip = 0;
  2101. uint32_t dev_state;
  2102. /* Stall incoming I/O until we are done */
  2103. scsi_block_requests(ha->host);
  2104. clear_bit(AF_ONLINE, &ha->flags);
  2105. clear_bit(AF_LINK_UP, &ha->flags);
  2106. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  2107. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2108. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  2109. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  2110. reset_chip = 1;
  2111. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  2112. * do not reset adapter, jump to initialize_adapter */
  2113. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2114. status = QLA_SUCCESS;
  2115. goto recover_ha_init_adapter;
  2116. }
  2117. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  2118. * from eh_host_reset or ioctl module */
  2119. if (is_qla8022(ha) && !reset_chip &&
  2120. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  2121. DEBUG2(ql4_printk(KERN_INFO, ha,
  2122. "scsi%ld: %s - Performing stop_firmware...\n",
  2123. ha->host_no, __func__));
  2124. status = ha->isp_ops->reset_firmware(ha);
  2125. if (status == QLA_SUCCESS) {
  2126. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  2127. qla4xxx_cmd_wait(ha);
  2128. ha->isp_ops->disable_intrs(ha);
  2129. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2130. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2131. } else {
  2132. /* If the stop_firmware fails then
  2133. * reset the entire chip */
  2134. reset_chip = 1;
  2135. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2136. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2137. }
  2138. }
  2139. /* Issue full chip reset if recovering from a catastrophic error,
  2140. * or if stop_firmware fails for ISP-82xx.
  2141. * This is the default case for ISP-4xxx */
  2142. if (!is_qla8022(ha) || reset_chip) {
  2143. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  2144. qla4xxx_cmd_wait(ha);
  2145. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2146. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2147. DEBUG2(ql4_printk(KERN_INFO, ha,
  2148. "scsi%ld: %s - Performing chip reset..\n",
  2149. ha->host_no, __func__));
  2150. status = ha->isp_ops->reset_chip(ha);
  2151. }
  2152. /* Flush any pending ddb changed AENs */
  2153. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2154. recover_ha_init_adapter:
  2155. /* Upon successful firmware/chip reset, re-initialize the adapter */
  2156. if (status == QLA_SUCCESS) {
  2157. /* For ISP-4xxx, force function 1 to always initialize
  2158. * before function 3 to prevent both funcions from
  2159. * stepping on top of the other */
  2160. if (!is_qla8022(ha) && (ha->mac_index == 3))
  2161. ssleep(6);
  2162. /* NOTE: AF_ONLINE flag set upon successful completion of
  2163. * qla4xxx_initialize_adapter */
  2164. status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  2165. }
  2166. /* Retry failed adapter initialization, if necessary
  2167. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  2168. * case to prevent ping-pong resets between functions */
  2169. if (!test_bit(AF_ONLINE, &ha->flags) &&
  2170. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2171. /* Adapter initialization failed, see if we can retry
  2172. * resetting the ha.
  2173. * Since we don't want to block the DPC for too long
  2174. * with multiple resets in the same thread,
  2175. * utilize DPC to retry */
  2176. if (is_qla8022(ha)) {
  2177. qla4_8xxx_idc_lock(ha);
  2178. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  2179. qla4_8xxx_idc_unlock(ha);
  2180. if (dev_state == QLA82XX_DEV_FAILED) {
  2181. ql4_printk(KERN_INFO, ha, "%s: don't retry "
  2182. "recover adapter. H/W is in Failed "
  2183. "state\n", __func__);
  2184. qla4xxx_dead_adapter_cleanup(ha);
  2185. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2186. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2187. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  2188. &ha->dpc_flags);
  2189. status = QLA_ERROR;
  2190. goto exit_recover;
  2191. }
  2192. }
  2193. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  2194. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  2195. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  2196. "(%d) more times\n", ha->host_no,
  2197. ha->retry_reset_ha_cnt));
  2198. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2199. status = QLA_ERROR;
  2200. } else {
  2201. if (ha->retry_reset_ha_cnt > 0) {
  2202. /* Schedule another Reset HA--DPC will retry */
  2203. ha->retry_reset_ha_cnt--;
  2204. DEBUG2(printk("scsi%ld: recover adapter - "
  2205. "retry remaining %d\n",
  2206. ha->host_no,
  2207. ha->retry_reset_ha_cnt));
  2208. status = QLA_ERROR;
  2209. }
  2210. if (ha->retry_reset_ha_cnt == 0) {
  2211. /* Recover adapter retries have been exhausted.
  2212. * Adapter DEAD */
  2213. DEBUG2(printk("scsi%ld: recover adapter "
  2214. "failed - board disabled\n",
  2215. ha->host_no));
  2216. qla4xxx_dead_adapter_cleanup(ha);
  2217. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2218. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2219. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  2220. &ha->dpc_flags);
  2221. status = QLA_ERROR;
  2222. }
  2223. }
  2224. } else {
  2225. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2226. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2227. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  2228. }
  2229. exit_recover:
  2230. ha->adapter_error_count++;
  2231. if (test_bit(AF_ONLINE, &ha->flags))
  2232. ha->isp_ops->enable_intrs(ha);
  2233. scsi_unblock_requests(ha->host);
  2234. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2235. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  2236. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  2237. return status;
  2238. }
  2239. static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
  2240. {
  2241. struct iscsi_session *sess;
  2242. struct ddb_entry *ddb_entry;
  2243. struct scsi_qla_host *ha;
  2244. sess = cls_session->dd_data;
  2245. ddb_entry = sess->dd_data;
  2246. ha = ddb_entry->ha;
  2247. if (!iscsi_is_session_online(cls_session)) {
  2248. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  2249. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2250. " unblock session\n", ha->host_no, __func__,
  2251. ddb_entry->fw_ddb_index);
  2252. iscsi_unblock_session(ddb_entry->sess);
  2253. } else {
  2254. /* Trigger relogin */
  2255. if (ddb_entry->ddb_type == FLASH_DDB) {
  2256. if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
  2257. qla4xxx_arm_relogin_timer(ddb_entry);
  2258. } else
  2259. iscsi_session_failure(cls_session->dd_data,
  2260. ISCSI_ERR_CONN_FAILED);
  2261. }
  2262. }
  2263. }
  2264. int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
  2265. {
  2266. struct iscsi_session *sess;
  2267. struct ddb_entry *ddb_entry;
  2268. struct scsi_qla_host *ha;
  2269. sess = cls_session->dd_data;
  2270. ddb_entry = sess->dd_data;
  2271. ha = ddb_entry->ha;
  2272. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2273. " unblock session\n", ha->host_no, __func__,
  2274. ddb_entry->fw_ddb_index);
  2275. iscsi_unblock_session(ddb_entry->sess);
  2276. /* Start scan target */
  2277. if (test_bit(AF_ONLINE, &ha->flags)) {
  2278. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2279. " start scan\n", ha->host_no, __func__,
  2280. ddb_entry->fw_ddb_index);
  2281. scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
  2282. }
  2283. return QLA_SUCCESS;
  2284. }
  2285. int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
  2286. {
  2287. struct iscsi_session *sess;
  2288. struct ddb_entry *ddb_entry;
  2289. struct scsi_qla_host *ha;
  2290. sess = cls_session->dd_data;
  2291. ddb_entry = sess->dd_data;
  2292. ha = ddb_entry->ha;
  2293. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  2294. " unblock user space session\n", ha->host_no, __func__,
  2295. ddb_entry->fw_ddb_index);
  2296. iscsi_conn_start(ddb_entry->conn);
  2297. iscsi_conn_login_event(ddb_entry->conn,
  2298. ISCSI_CONN_STATE_LOGGED_IN);
  2299. return QLA_SUCCESS;
  2300. }
  2301. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  2302. {
  2303. iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
  2304. }
  2305. static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  2306. {
  2307. uint16_t relogin_timer;
  2308. struct iscsi_session *sess;
  2309. struct ddb_entry *ddb_entry;
  2310. struct scsi_qla_host *ha;
  2311. sess = cls_sess->dd_data;
  2312. ddb_entry = sess->dd_data;
  2313. ha = ddb_entry->ha;
  2314. relogin_timer = max(ddb_entry->default_relogin_timeout,
  2315. (uint16_t)RELOGIN_TOV);
  2316. atomic_set(&ddb_entry->relogin_timer, relogin_timer);
  2317. DEBUG2(ql4_printk(KERN_INFO, ha,
  2318. "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
  2319. ddb_entry->fw_ddb_index, relogin_timer));
  2320. qla4xxx_login_flash_ddb(cls_sess);
  2321. }
  2322. static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
  2323. {
  2324. struct iscsi_session *sess;
  2325. struct ddb_entry *ddb_entry;
  2326. struct scsi_qla_host *ha;
  2327. sess = cls_sess->dd_data;
  2328. ddb_entry = sess->dd_data;
  2329. ha = ddb_entry->ha;
  2330. if (!(ddb_entry->ddb_type == FLASH_DDB))
  2331. return;
  2332. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  2333. !iscsi_is_session_online(cls_sess)) {
  2334. DEBUG2(ql4_printk(KERN_INFO, ha,
  2335. "relogin issued\n"));
  2336. qla4xxx_relogin_flash_ddb(cls_sess);
  2337. }
  2338. }
  2339. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  2340. {
  2341. if (ha->dpc_thread)
  2342. queue_work(ha->dpc_thread, &ha->dpc_work);
  2343. }
  2344. /**
  2345. * qla4xxx_do_dpc - dpc routine
  2346. * @data: in our case pointer to adapter structure
  2347. *
  2348. * This routine is a task that is schedule by the interrupt handler
  2349. * to perform the background processing for interrupts. We put it
  2350. * on a task queue that is consumed whenever the scheduler runs; that's
  2351. * so you can do anything (i.e. put the process to sleep etc). In fact,
  2352. * the mid-level tries to sleep when it reaches the driver threshold
  2353. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  2354. **/
  2355. static void qla4xxx_do_dpc(struct work_struct *work)
  2356. {
  2357. struct scsi_qla_host *ha =
  2358. container_of(work, struct scsi_qla_host, dpc_work);
  2359. int status = QLA_ERROR;
  2360. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  2361. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  2362. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  2363. /* Initialization not yet finished. Don't do anything yet. */
  2364. if (!test_bit(AF_INIT_DONE, &ha->flags))
  2365. return;
  2366. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  2367. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  2368. ha->host_no, __func__, ha->flags));
  2369. return;
  2370. }
  2371. if (is_qla8022(ha)) {
  2372. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  2373. qla4_8xxx_idc_lock(ha);
  2374. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2375. QLA82XX_DEV_FAILED);
  2376. qla4_8xxx_idc_unlock(ha);
  2377. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  2378. qla4_8xxx_device_state_handler(ha);
  2379. }
  2380. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  2381. qla4_8xxx_need_qsnt_handler(ha);
  2382. }
  2383. }
  2384. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  2385. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  2386. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  2387. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  2388. if (ql4xdontresethba) {
  2389. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  2390. ha->host_no, __func__));
  2391. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  2392. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2393. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2394. goto dpc_post_reset_ha;
  2395. }
  2396. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  2397. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  2398. qla4xxx_recover_adapter(ha);
  2399. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  2400. uint8_t wait_time = RESET_INTR_TOV;
  2401. while ((readw(&ha->reg->ctrl_status) &
  2402. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  2403. if (--wait_time == 0)
  2404. break;
  2405. msleep(1000);
  2406. }
  2407. if (wait_time == 0)
  2408. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  2409. "bit not cleared-- resetting\n",
  2410. ha->host_no, __func__));
  2411. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2412. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  2413. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2414. status = qla4xxx_recover_adapter(ha);
  2415. }
  2416. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2417. if (status == QLA_SUCCESS)
  2418. ha->isp_ops->enable_intrs(ha);
  2419. }
  2420. }
  2421. dpc_post_reset_ha:
  2422. /* ---- process AEN? --- */
  2423. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  2424. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  2425. /* ---- Get DHCP IP Address? --- */
  2426. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  2427. qla4xxx_get_dhcp_ip_address(ha);
  2428. /* ---- relogin device? --- */
  2429. if (adapter_up(ha) &&
  2430. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  2431. iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
  2432. }
  2433. /* ---- link change? --- */
  2434. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  2435. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  2436. /* ---- link down? --- */
  2437. qla4xxx_mark_all_devices_missing(ha);
  2438. } else {
  2439. /* ---- link up? --- *
  2440. * F/W will auto login to all devices ONLY ONCE after
  2441. * link up during driver initialization and runtime
  2442. * fatal error recovery. Therefore, the driver must
  2443. * manually relogin to devices when recovering from
  2444. * connection failures, logouts, expired KATO, etc. */
  2445. if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
  2446. qla4xxx_build_ddb_list(ha, ha->is_reset);
  2447. iscsi_host_for_each_session(ha->host,
  2448. qla4xxx_login_flash_ddb);
  2449. } else
  2450. qla4xxx_relogin_all_devices(ha);
  2451. }
  2452. }
  2453. }
  2454. /**
  2455. * qla4xxx_free_adapter - release the adapter
  2456. * @ha: pointer to adapter structure
  2457. **/
  2458. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  2459. {
  2460. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2461. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  2462. /* Turn-off interrupts on the card. */
  2463. ha->isp_ops->disable_intrs(ha);
  2464. }
  2465. /* Remove timer thread, if present */
  2466. if (ha->timer_active)
  2467. qla4xxx_stop_timer(ha);
  2468. /* Kill the kernel thread for this host */
  2469. if (ha->dpc_thread)
  2470. destroy_workqueue(ha->dpc_thread);
  2471. /* Kill the kernel thread for this host */
  2472. if (ha->task_wq)
  2473. destroy_workqueue(ha->task_wq);
  2474. /* Put firmware in known state */
  2475. ha->isp_ops->reset_firmware(ha);
  2476. if (is_qla8022(ha)) {
  2477. qla4_8xxx_idc_lock(ha);
  2478. qla4_8xxx_clear_drv_active(ha);
  2479. qla4_8xxx_idc_unlock(ha);
  2480. }
  2481. /* Detach interrupts */
  2482. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  2483. qla4xxx_free_irqs(ha);
  2484. /* free extra memory */
  2485. qla4xxx_mem_free(ha);
  2486. }
  2487. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  2488. {
  2489. int status = 0;
  2490. uint8_t revision_id;
  2491. unsigned long mem_base, mem_len, db_base, db_len;
  2492. struct pci_dev *pdev = ha->pdev;
  2493. status = pci_request_regions(pdev, DRIVER_NAME);
  2494. if (status) {
  2495. printk(KERN_WARNING
  2496. "scsi(%ld) Failed to reserve PIO regions (%s) "
  2497. "status=%d\n", ha->host_no, pci_name(pdev), status);
  2498. goto iospace_error_exit;
  2499. }
  2500. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  2501. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  2502. __func__, revision_id));
  2503. ha->revision_id = revision_id;
  2504. /* remap phys address */
  2505. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  2506. mem_len = pci_resource_len(pdev, 0);
  2507. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  2508. __func__, mem_base, mem_len));
  2509. /* mapping of pcibase pointer */
  2510. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  2511. if (!ha->nx_pcibase) {
  2512. printk(KERN_ERR
  2513. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  2514. pci_release_regions(ha->pdev);
  2515. goto iospace_error_exit;
  2516. }
  2517. /* Mapping of IO base pointer, door bell read and write pointer */
  2518. /* mapping of IO base pointer */
  2519. ha->qla4_8xxx_reg =
  2520. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  2521. 0xbc000 + (ha->pdev->devfn << 11));
  2522. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  2523. db_len = pci_resource_len(pdev, 4);
  2524. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  2525. QLA82XX_CAM_RAM_DB2);
  2526. return 0;
  2527. iospace_error_exit:
  2528. return -ENOMEM;
  2529. }
  2530. /***
  2531. * qla4xxx_iospace_config - maps registers
  2532. * @ha: pointer to adapter structure
  2533. *
  2534. * This routines maps HBA's registers from the pci address space
  2535. * into the kernel virtual address space for memory mapped i/o.
  2536. **/
  2537. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  2538. {
  2539. unsigned long pio, pio_len, pio_flags;
  2540. unsigned long mmio, mmio_len, mmio_flags;
  2541. pio = pci_resource_start(ha->pdev, 0);
  2542. pio_len = pci_resource_len(ha->pdev, 0);
  2543. pio_flags = pci_resource_flags(ha->pdev, 0);
  2544. if (pio_flags & IORESOURCE_IO) {
  2545. if (pio_len < MIN_IOBASE_LEN) {
  2546. ql4_printk(KERN_WARNING, ha,
  2547. "Invalid PCI I/O region size\n");
  2548. pio = 0;
  2549. }
  2550. } else {
  2551. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  2552. pio = 0;
  2553. }
  2554. /* Use MMIO operations for all accesses. */
  2555. mmio = pci_resource_start(ha->pdev, 1);
  2556. mmio_len = pci_resource_len(ha->pdev, 1);
  2557. mmio_flags = pci_resource_flags(ha->pdev, 1);
  2558. if (!(mmio_flags & IORESOURCE_MEM)) {
  2559. ql4_printk(KERN_ERR, ha,
  2560. "region #0 not an MMIO resource, aborting\n");
  2561. goto iospace_error_exit;
  2562. }
  2563. if (mmio_len < MIN_IOBASE_LEN) {
  2564. ql4_printk(KERN_ERR, ha,
  2565. "Invalid PCI mem region size, aborting\n");
  2566. goto iospace_error_exit;
  2567. }
  2568. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  2569. ql4_printk(KERN_WARNING, ha,
  2570. "Failed to reserve PIO/MMIO regions\n");
  2571. goto iospace_error_exit;
  2572. }
  2573. ha->pio_address = pio;
  2574. ha->pio_length = pio_len;
  2575. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  2576. if (!ha->reg) {
  2577. ql4_printk(KERN_ERR, ha,
  2578. "cannot remap MMIO, aborting\n");
  2579. goto iospace_error_exit;
  2580. }
  2581. return 0;
  2582. iospace_error_exit:
  2583. return -ENOMEM;
  2584. }
  2585. static struct isp_operations qla4xxx_isp_ops = {
  2586. .iospace_config = qla4xxx_iospace_config,
  2587. .pci_config = qla4xxx_pci_config,
  2588. .disable_intrs = qla4xxx_disable_intrs,
  2589. .enable_intrs = qla4xxx_enable_intrs,
  2590. .start_firmware = qla4xxx_start_firmware,
  2591. .intr_handler = qla4xxx_intr_handler,
  2592. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  2593. .reset_chip = qla4xxx_soft_reset,
  2594. .reset_firmware = qla4xxx_hw_reset,
  2595. .queue_iocb = qla4xxx_queue_iocb,
  2596. .complete_iocb = qla4xxx_complete_iocb,
  2597. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  2598. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  2599. .get_sys_info = qla4xxx_get_sys_info,
  2600. };
  2601. static struct isp_operations qla4_8xxx_isp_ops = {
  2602. .iospace_config = qla4_8xxx_iospace_config,
  2603. .pci_config = qla4_8xxx_pci_config,
  2604. .disable_intrs = qla4_8xxx_disable_intrs,
  2605. .enable_intrs = qla4_8xxx_enable_intrs,
  2606. .start_firmware = qla4_8xxx_load_risc,
  2607. .intr_handler = qla4_8xxx_intr_handler,
  2608. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  2609. .reset_chip = qla4_8xxx_isp_reset,
  2610. .reset_firmware = qla4_8xxx_stop_firmware,
  2611. .queue_iocb = qla4_8xxx_queue_iocb,
  2612. .complete_iocb = qla4_8xxx_complete_iocb,
  2613. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  2614. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  2615. .get_sys_info = qla4_8xxx_get_sys_info,
  2616. };
  2617. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  2618. {
  2619. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  2620. }
  2621. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  2622. {
  2623. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  2624. }
  2625. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  2626. {
  2627. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  2628. }
  2629. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  2630. {
  2631. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  2632. }
  2633. static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
  2634. {
  2635. struct scsi_qla_host *ha = data;
  2636. char *str = buf;
  2637. int rc;
  2638. switch (type) {
  2639. case ISCSI_BOOT_ETH_FLAGS:
  2640. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  2641. break;
  2642. case ISCSI_BOOT_ETH_INDEX:
  2643. rc = sprintf(str, "0\n");
  2644. break;
  2645. case ISCSI_BOOT_ETH_MAC:
  2646. rc = sysfs_format_mac(str, ha->my_mac,
  2647. MAC_ADDR_LEN);
  2648. break;
  2649. default:
  2650. rc = -ENOSYS;
  2651. break;
  2652. }
  2653. return rc;
  2654. }
  2655. static mode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
  2656. {
  2657. int rc;
  2658. switch (type) {
  2659. case ISCSI_BOOT_ETH_FLAGS:
  2660. case ISCSI_BOOT_ETH_MAC:
  2661. case ISCSI_BOOT_ETH_INDEX:
  2662. rc = S_IRUGO;
  2663. break;
  2664. default:
  2665. rc = 0;
  2666. break;
  2667. }
  2668. return rc;
  2669. }
  2670. static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
  2671. {
  2672. struct scsi_qla_host *ha = data;
  2673. char *str = buf;
  2674. int rc;
  2675. switch (type) {
  2676. case ISCSI_BOOT_INI_INITIATOR_NAME:
  2677. rc = sprintf(str, "%s\n", ha->name_string);
  2678. break;
  2679. default:
  2680. rc = -ENOSYS;
  2681. break;
  2682. }
  2683. return rc;
  2684. }
  2685. static mode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
  2686. {
  2687. int rc;
  2688. switch (type) {
  2689. case ISCSI_BOOT_INI_INITIATOR_NAME:
  2690. rc = S_IRUGO;
  2691. break;
  2692. default:
  2693. rc = 0;
  2694. break;
  2695. }
  2696. return rc;
  2697. }
  2698. static ssize_t
  2699. qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
  2700. char *buf)
  2701. {
  2702. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  2703. char *str = buf;
  2704. int rc;
  2705. switch (type) {
  2706. case ISCSI_BOOT_TGT_NAME:
  2707. rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
  2708. break;
  2709. case ISCSI_BOOT_TGT_IP_ADDR:
  2710. if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
  2711. rc = sprintf(buf, "%pI4\n",
  2712. &boot_conn->dest_ipaddr.ip_address);
  2713. else
  2714. rc = sprintf(str, "%pI6\n",
  2715. &boot_conn->dest_ipaddr.ip_address);
  2716. break;
  2717. case ISCSI_BOOT_TGT_PORT:
  2718. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  2719. break;
  2720. case ISCSI_BOOT_TGT_CHAP_NAME:
  2721. rc = sprintf(str, "%.*s\n",
  2722. boot_conn->chap.target_chap_name_length,
  2723. (char *)&boot_conn->chap.target_chap_name);
  2724. break;
  2725. case ISCSI_BOOT_TGT_CHAP_SECRET:
  2726. rc = sprintf(str, "%.*s\n",
  2727. boot_conn->chap.target_secret_length,
  2728. (char *)&boot_conn->chap.target_secret);
  2729. break;
  2730. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  2731. rc = sprintf(str, "%.*s\n",
  2732. boot_conn->chap.intr_chap_name_length,
  2733. (char *)&boot_conn->chap.intr_chap_name);
  2734. break;
  2735. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  2736. rc = sprintf(str, "%.*s\n",
  2737. boot_conn->chap.intr_secret_length,
  2738. (char *)&boot_conn->chap.intr_secret);
  2739. break;
  2740. case ISCSI_BOOT_TGT_FLAGS:
  2741. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  2742. break;
  2743. case ISCSI_BOOT_TGT_NIC_ASSOC:
  2744. rc = sprintf(str, "0\n");
  2745. break;
  2746. default:
  2747. rc = -ENOSYS;
  2748. break;
  2749. }
  2750. return rc;
  2751. }
  2752. static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
  2753. {
  2754. struct scsi_qla_host *ha = data;
  2755. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
  2756. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  2757. }
  2758. static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
  2759. {
  2760. struct scsi_qla_host *ha = data;
  2761. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
  2762. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  2763. }
  2764. static mode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
  2765. {
  2766. int rc;
  2767. switch (type) {
  2768. case ISCSI_BOOT_TGT_NAME:
  2769. case ISCSI_BOOT_TGT_IP_ADDR:
  2770. case ISCSI_BOOT_TGT_PORT:
  2771. case ISCSI_BOOT_TGT_CHAP_NAME:
  2772. case ISCSI_BOOT_TGT_CHAP_SECRET:
  2773. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  2774. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  2775. case ISCSI_BOOT_TGT_NIC_ASSOC:
  2776. case ISCSI_BOOT_TGT_FLAGS:
  2777. rc = S_IRUGO;
  2778. break;
  2779. default:
  2780. rc = 0;
  2781. break;
  2782. }
  2783. return rc;
  2784. }
  2785. static void qla4xxx_boot_release(void *data)
  2786. {
  2787. struct scsi_qla_host *ha = data;
  2788. scsi_host_put(ha->host);
  2789. }
  2790. static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
  2791. {
  2792. dma_addr_t buf_dma;
  2793. uint32_t addr, pri_addr, sec_addr;
  2794. uint32_t offset;
  2795. uint16_t func_num;
  2796. uint8_t val;
  2797. uint8_t *buf = NULL;
  2798. size_t size = 13 * sizeof(uint8_t);
  2799. int ret = QLA_SUCCESS;
  2800. func_num = PCI_FUNC(ha->pdev->devfn);
  2801. ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
  2802. __func__, ha->pdev->device, func_num);
  2803. if (is_qla40XX(ha)) {
  2804. if (func_num == 1) {
  2805. addr = NVRAM_PORT0_BOOT_MODE;
  2806. pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
  2807. sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
  2808. } else if (func_num == 3) {
  2809. addr = NVRAM_PORT1_BOOT_MODE;
  2810. pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
  2811. sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
  2812. } else {
  2813. ret = QLA_ERROR;
  2814. goto exit_boot_info;
  2815. }
  2816. /* Check Boot Mode */
  2817. val = rd_nvram_byte(ha, addr);
  2818. if (!(val & 0x07)) {
  2819. DEBUG2(ql4_printk(KERN_ERR, ha,
  2820. "%s: Failed Boot options : 0x%x\n",
  2821. __func__, val));
  2822. ret = QLA_ERROR;
  2823. goto exit_boot_info;
  2824. }
  2825. /* get primary valid target index */
  2826. val = rd_nvram_byte(ha, pri_addr);
  2827. if (val & BIT_7)
  2828. ddb_index[0] = (val & 0x7f);
  2829. /* get secondary valid target index */
  2830. val = rd_nvram_byte(ha, sec_addr);
  2831. if (val & BIT_7)
  2832. ddb_index[1] = (val & 0x7f);
  2833. } else if (is_qla8022(ha)) {
  2834. buf = dma_alloc_coherent(&ha->pdev->dev, size,
  2835. &buf_dma, GFP_KERNEL);
  2836. if (!buf) {
  2837. DEBUG2(ql4_printk(KERN_ERR, ha,
  2838. "%s: Unable to allocate dma buffer\n",
  2839. __func__));
  2840. ret = QLA_ERROR;
  2841. goto exit_boot_info;
  2842. }
  2843. if (ha->port_num == 0)
  2844. offset = BOOT_PARAM_OFFSET_PORT0;
  2845. else if (ha->port_num == 1)
  2846. offset = BOOT_PARAM_OFFSET_PORT1;
  2847. else {
  2848. ret = QLA_ERROR;
  2849. goto exit_boot_info_free;
  2850. }
  2851. addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
  2852. offset;
  2853. if (qla4xxx_get_flash(ha, buf_dma, addr,
  2854. 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
  2855. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
  2856. "failed\n", ha->host_no, __func__));
  2857. ret = QLA_ERROR;
  2858. goto exit_boot_info_free;
  2859. }
  2860. /* Check Boot Mode */
  2861. if (!(buf[1] & 0x07)) {
  2862. DEBUG2(ql4_printk(KERN_INFO, ha,
  2863. "Failed: Boot options : 0x%x\n",
  2864. buf[1]));
  2865. ret = QLA_ERROR;
  2866. goto exit_boot_info_free;
  2867. }
  2868. /* get primary valid target index */
  2869. if (buf[2] & BIT_7)
  2870. ddb_index[0] = buf[2] & 0x7f;
  2871. /* get secondary valid target index */
  2872. if (buf[11] & BIT_7)
  2873. ddb_index[1] = buf[11] & 0x7f;
  2874. } else {
  2875. ret = QLA_ERROR;
  2876. goto exit_boot_info;
  2877. }
  2878. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
  2879. " target ID %d\n", __func__, ddb_index[0],
  2880. ddb_index[1]));
  2881. ha->pri_ddb_idx = ddb_index[0];
  2882. ha->sec_ddb_idx = ddb_index[1];
  2883. exit_boot_info_free:
  2884. dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
  2885. exit_boot_info:
  2886. return ret;
  2887. }
  2888. /**
  2889. * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
  2890. * @ha: pointer to adapter structure
  2891. * @username: CHAP username to be returned
  2892. * @password: CHAP password to be returned
  2893. *
  2894. * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
  2895. * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
  2896. * So from the CHAP cache find the first BIDI CHAP entry and set it
  2897. * to the boot record in sysfs.
  2898. **/
  2899. static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
  2900. char *password)
  2901. {
  2902. int i, ret = -EINVAL;
  2903. int max_chap_entries = 0;
  2904. struct ql4_chap_table *chap_table;
  2905. if (is_qla8022(ha))
  2906. max_chap_entries = (ha->hw.flt_chap_size / 2) /
  2907. sizeof(struct ql4_chap_table);
  2908. else
  2909. max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  2910. if (!ha->chap_list) {
  2911. ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
  2912. return ret;
  2913. }
  2914. mutex_lock(&ha->chap_sem);
  2915. for (i = 0; i < max_chap_entries; i++) {
  2916. chap_table = (struct ql4_chap_table *)ha->chap_list + i;
  2917. if (chap_table->cookie !=
  2918. __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
  2919. continue;
  2920. }
  2921. if (chap_table->flags & BIT_7) /* local */
  2922. continue;
  2923. if (!(chap_table->flags & BIT_6)) /* Not BIDI */
  2924. continue;
  2925. strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
  2926. strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
  2927. ret = 0;
  2928. break;
  2929. }
  2930. mutex_unlock(&ha->chap_sem);
  2931. return ret;
  2932. }
  2933. static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
  2934. struct ql4_boot_session_info *boot_sess,
  2935. uint16_t ddb_index)
  2936. {
  2937. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  2938. struct dev_db_entry *fw_ddb_entry;
  2939. dma_addr_t fw_ddb_entry_dma;
  2940. uint16_t idx;
  2941. uint16_t options;
  2942. int ret = QLA_SUCCESS;
  2943. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  2944. &fw_ddb_entry_dma, GFP_KERNEL);
  2945. if (!fw_ddb_entry) {
  2946. DEBUG2(ql4_printk(KERN_ERR, ha,
  2947. "%s: Unable to allocate dma buffer.\n",
  2948. __func__));
  2949. ret = QLA_ERROR;
  2950. return ret;
  2951. }
  2952. if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
  2953. fw_ddb_entry_dma, ddb_index)) {
  2954. DEBUG2(ql4_printk(KERN_ERR, ha,
  2955. "%s: Flash DDB read Failed\n", __func__));
  2956. ret = QLA_ERROR;
  2957. goto exit_boot_target;
  2958. }
  2959. /* Update target name and IP from DDB */
  2960. memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
  2961. min(sizeof(boot_sess->target_name),
  2962. sizeof(fw_ddb_entry->iscsi_name)));
  2963. options = le16_to_cpu(fw_ddb_entry->options);
  2964. if (options & DDB_OPT_IPV6_DEVICE) {
  2965. memcpy(&boot_conn->dest_ipaddr.ip_address,
  2966. &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
  2967. } else {
  2968. boot_conn->dest_ipaddr.ip_type = 0x1;
  2969. memcpy(&boot_conn->dest_ipaddr.ip_address,
  2970. &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
  2971. }
  2972. boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
  2973. /* update chap information */
  2974. idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  2975. if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  2976. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
  2977. ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
  2978. target_chap_name,
  2979. (char *)&boot_conn->chap.target_secret,
  2980. idx);
  2981. if (ret) {
  2982. ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
  2983. ret = QLA_ERROR;
  2984. goto exit_boot_target;
  2985. }
  2986. boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  2987. boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  2988. }
  2989. if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  2990. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
  2991. ret = qla4xxx_get_bidi_chap(ha,
  2992. (char *)&boot_conn->chap.intr_chap_name,
  2993. (char *)&boot_conn->chap.intr_secret);
  2994. if (ret) {
  2995. ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
  2996. ret = QLA_ERROR;
  2997. goto exit_boot_target;
  2998. }
  2999. boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  3000. boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  3001. }
  3002. exit_boot_target:
  3003. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  3004. fw_ddb_entry, fw_ddb_entry_dma);
  3005. return ret;
  3006. }
  3007. static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
  3008. {
  3009. uint16_t ddb_index[2];
  3010. int ret = QLA_ERROR;
  3011. int rval;
  3012. memset(ddb_index, 0, sizeof(ddb_index));
  3013. ddb_index[0] = 0xffff;
  3014. ddb_index[1] = 0xffff;
  3015. ret = get_fw_boot_info(ha, ddb_index);
  3016. if (ret != QLA_SUCCESS) {
  3017. DEBUG2(ql4_printk(KERN_ERR, ha,
  3018. "%s: Failed to set boot info.\n", __func__));
  3019. return ret;
  3020. }
  3021. if (ql4xdisablesysfsboot)
  3022. return QLA_SUCCESS;
  3023. if (ddb_index[0] == 0xffff)
  3024. goto sec_target;
  3025. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
  3026. ddb_index[0]);
  3027. if (rval != QLA_SUCCESS) {
  3028. DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
  3029. "primary target\n", __func__));
  3030. } else
  3031. ret = QLA_SUCCESS;
  3032. sec_target:
  3033. if (ddb_index[1] == 0xffff)
  3034. goto exit_get_boot_info;
  3035. rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
  3036. ddb_index[1]);
  3037. if (rval != QLA_SUCCESS) {
  3038. DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
  3039. "secondary target\n", __func__));
  3040. } else
  3041. ret = QLA_SUCCESS;
  3042. exit_get_boot_info:
  3043. return ret;
  3044. }
  3045. static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
  3046. {
  3047. struct iscsi_boot_kobj *boot_kobj;
  3048. if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
  3049. return QLA_ERROR;
  3050. if (ql4xdisablesysfsboot) {
  3051. ql4_printk(KERN_INFO, ha,
  3052. "%s: syfsboot disabled - driver will trigger login"
  3053. "and publish session for discovery .\n", __func__);
  3054. return QLA_SUCCESS;
  3055. }
  3056. ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
  3057. if (!ha->boot_kset)
  3058. goto kset_free;
  3059. if (!scsi_host_get(ha->host))
  3060. goto kset_free;
  3061. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
  3062. qla4xxx_show_boot_tgt_pri_info,
  3063. qla4xxx_tgt_get_attr_visibility,
  3064. qla4xxx_boot_release);
  3065. if (!boot_kobj)
  3066. goto put_host;
  3067. if (!scsi_host_get(ha->host))
  3068. goto kset_free;
  3069. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
  3070. qla4xxx_show_boot_tgt_sec_info,
  3071. qla4xxx_tgt_get_attr_visibility,
  3072. qla4xxx_boot_release);
  3073. if (!boot_kobj)
  3074. goto put_host;
  3075. if (!scsi_host_get(ha->host))
  3076. goto kset_free;
  3077. boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
  3078. qla4xxx_show_boot_ini_info,
  3079. qla4xxx_ini_get_attr_visibility,
  3080. qla4xxx_boot_release);
  3081. if (!boot_kobj)
  3082. goto put_host;
  3083. if (!scsi_host_get(ha->host))
  3084. goto kset_free;
  3085. boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
  3086. qla4xxx_show_boot_eth_info,
  3087. qla4xxx_eth_get_attr_visibility,
  3088. qla4xxx_boot_release);
  3089. if (!boot_kobj)
  3090. goto put_host;
  3091. return QLA_SUCCESS;
  3092. put_host:
  3093. scsi_host_put(ha->host);
  3094. kset_free:
  3095. iscsi_boot_destroy_kset(ha->boot_kset);
  3096. return -ENOMEM;
  3097. }
  3098. /**
  3099. * qla4xxx_create chap_list - Create CHAP list from FLASH
  3100. * @ha: pointer to adapter structure
  3101. *
  3102. * Read flash and make a list of CHAP entries, during login when a CHAP entry
  3103. * is received, it will be checked in this list. If entry exist then the CHAP
  3104. * entry index is set in the DDB. If CHAP entry does not exist in this list
  3105. * then a new entry is added in FLASH in CHAP table and the index obtained is
  3106. * used in the DDB.
  3107. **/
  3108. static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
  3109. {
  3110. int rval = 0;
  3111. uint8_t *chap_flash_data = NULL;
  3112. uint32_t offset;
  3113. dma_addr_t chap_dma;
  3114. uint32_t chap_size = 0;
  3115. if (is_qla40XX(ha))
  3116. chap_size = MAX_CHAP_ENTRIES_40XX *
  3117. sizeof(struct ql4_chap_table);
  3118. else /* Single region contains CHAP info for both
  3119. * ports which is divided into half for each port.
  3120. */
  3121. chap_size = ha->hw.flt_chap_size / 2;
  3122. chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
  3123. &chap_dma, GFP_KERNEL);
  3124. if (!chap_flash_data) {
  3125. ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
  3126. return;
  3127. }
  3128. if (is_qla40XX(ha))
  3129. offset = FLASH_CHAP_OFFSET;
  3130. else {
  3131. offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
  3132. if (ha->port_num == 1)
  3133. offset += chap_size;
  3134. }
  3135. rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
  3136. if (rval != QLA_SUCCESS)
  3137. goto exit_chap_list;
  3138. if (ha->chap_list == NULL)
  3139. ha->chap_list = vmalloc(chap_size);
  3140. if (ha->chap_list == NULL) {
  3141. ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
  3142. goto exit_chap_list;
  3143. }
  3144. memcpy(ha->chap_list, chap_flash_data, chap_size);
  3145. exit_chap_list:
  3146. dma_free_coherent(&ha->pdev->dev, chap_size,
  3147. chap_flash_data, chap_dma);
  3148. }
  3149. static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
  3150. struct ql4_tuple_ddb *tddb)
  3151. {
  3152. struct scsi_qla_host *ha;
  3153. struct iscsi_cls_session *cls_sess;
  3154. struct iscsi_cls_conn *cls_conn;
  3155. struct iscsi_session *sess;
  3156. struct iscsi_conn *conn;
  3157. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  3158. ha = ddb_entry->ha;
  3159. cls_sess = ddb_entry->sess;
  3160. sess = cls_sess->dd_data;
  3161. cls_conn = ddb_entry->conn;
  3162. conn = cls_conn->dd_data;
  3163. tddb->tpgt = sess->tpgt;
  3164. tddb->port = conn->persistent_port;
  3165. strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
  3166. strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
  3167. }
  3168. static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
  3169. struct ql4_tuple_ddb *tddb)
  3170. {
  3171. uint16_t options = 0;
  3172. tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  3173. memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
  3174. min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
  3175. options = le16_to_cpu(fw_ddb_entry->options);
  3176. if (options & DDB_OPT_IPV6_DEVICE)
  3177. sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  3178. else
  3179. sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  3180. tddb->port = le16_to_cpu(fw_ddb_entry->port);
  3181. }
  3182. static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
  3183. struct ql4_tuple_ddb *old_tddb,
  3184. struct ql4_tuple_ddb *new_tddb)
  3185. {
  3186. if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
  3187. return QLA_ERROR;
  3188. if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
  3189. return QLA_ERROR;
  3190. if (old_tddb->port != new_tddb->port)
  3191. return QLA_ERROR;
  3192. DEBUG2(ql4_printk(KERN_INFO, ha,
  3193. "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
  3194. old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
  3195. old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
  3196. new_tddb->ip_addr, new_tddb->iscsi_name));
  3197. return QLA_SUCCESS;
  3198. }
  3199. static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
  3200. struct dev_db_entry *fw_ddb_entry)
  3201. {
  3202. struct ddb_entry *ddb_entry;
  3203. struct ql4_tuple_ddb *fw_tddb = NULL;
  3204. struct ql4_tuple_ddb *tmp_tddb = NULL;
  3205. int idx;
  3206. int ret = QLA_ERROR;
  3207. fw_tddb = vzalloc(sizeof(*fw_tddb));
  3208. if (!fw_tddb) {
  3209. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3210. "Memory Allocation failed.\n"));
  3211. ret = QLA_SUCCESS;
  3212. goto exit_check;
  3213. }
  3214. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  3215. if (!tmp_tddb) {
  3216. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3217. "Memory Allocation failed.\n"));
  3218. ret = QLA_SUCCESS;
  3219. goto exit_check;
  3220. }
  3221. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
  3222. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  3223. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  3224. if (ddb_entry == NULL)
  3225. continue;
  3226. qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
  3227. if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
  3228. ret = QLA_SUCCESS; /* found */
  3229. goto exit_check;
  3230. }
  3231. }
  3232. exit_check:
  3233. if (fw_tddb)
  3234. vfree(fw_tddb);
  3235. if (tmp_tddb)
  3236. vfree(tmp_tddb);
  3237. return ret;
  3238. }
  3239. static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
  3240. struct list_head *list_nt,
  3241. struct dev_db_entry *fw_ddb_entry)
  3242. {
  3243. struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
  3244. struct ql4_tuple_ddb *fw_tddb = NULL;
  3245. struct ql4_tuple_ddb *tmp_tddb = NULL;
  3246. int ret = QLA_ERROR;
  3247. fw_tddb = vzalloc(sizeof(*fw_tddb));
  3248. if (!fw_tddb) {
  3249. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3250. "Memory Allocation failed.\n"));
  3251. ret = QLA_SUCCESS;
  3252. goto exit_check;
  3253. }
  3254. tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  3255. if (!tmp_tddb) {
  3256. DEBUG2(ql4_printk(KERN_WARNING, ha,
  3257. "Memory Allocation failed.\n"));
  3258. ret = QLA_SUCCESS;
  3259. goto exit_check;
  3260. }
  3261. qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
  3262. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  3263. qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
  3264. if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
  3265. ret = QLA_SUCCESS; /* found */
  3266. goto exit_check;
  3267. }
  3268. }
  3269. exit_check:
  3270. if (fw_tddb)
  3271. vfree(fw_tddb);
  3272. if (tmp_tddb)
  3273. vfree(tmp_tddb);
  3274. return ret;
  3275. }
  3276. static void qla4xxx_free_nt_list(struct list_head *list_nt)
  3277. {
  3278. struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
  3279. /* Free up the normaltargets list */
  3280. list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  3281. list_del_init(&nt_ddb_idx->list);
  3282. vfree(nt_ddb_idx);
  3283. }
  3284. }
  3285. static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
  3286. struct dev_db_entry *fw_ddb_entry)
  3287. {
  3288. struct iscsi_endpoint *ep;
  3289. struct sockaddr_in *addr;
  3290. struct sockaddr_in6 *addr6;
  3291. struct sockaddr *dst_addr;
  3292. char *ip;
  3293. /* TODO: need to destroy on unload iscsi_endpoint*/
  3294. dst_addr = vmalloc(sizeof(*dst_addr));
  3295. if (!dst_addr)
  3296. return NULL;
  3297. if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
  3298. dst_addr->sa_family = AF_INET6;
  3299. addr6 = (struct sockaddr_in6 *)dst_addr;
  3300. ip = (char *)&addr6->sin6_addr;
  3301. memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
  3302. addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
  3303. } else {
  3304. dst_addr->sa_family = AF_INET;
  3305. addr = (struct sockaddr_in *)dst_addr;
  3306. ip = (char *)&addr->sin_addr;
  3307. memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
  3308. addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
  3309. }
  3310. ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
  3311. vfree(dst_addr);
  3312. return ep;
  3313. }
  3314. static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
  3315. {
  3316. if (ql4xdisablesysfsboot)
  3317. return QLA_SUCCESS;
  3318. if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
  3319. return QLA_ERROR;
  3320. return QLA_SUCCESS;
  3321. }
  3322. static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
  3323. struct ddb_entry *ddb_entry)
  3324. {
  3325. ddb_entry->ddb_type = FLASH_DDB;
  3326. ddb_entry->fw_ddb_index = INVALID_ENTRY;
  3327. ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  3328. ddb_entry->ha = ha;
  3329. ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
  3330. ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
  3331. atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  3332. atomic_set(&ddb_entry->relogin_timer, 0);
  3333. atomic_set(&ddb_entry->relogin_retry_count, 0);
  3334. ddb_entry->default_relogin_timeout =
  3335. le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
  3336. ddb_entry->default_time2wait =
  3337. le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
  3338. }
  3339. static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
  3340. {
  3341. uint32_t idx = 0;
  3342. uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
  3343. uint32_t sts[MBOX_REG_COUNT];
  3344. uint32_t ip_state;
  3345. unsigned long wtime;
  3346. int ret;
  3347. wtime = jiffies + (HZ * IP_CONFIG_TOV);
  3348. do {
  3349. for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
  3350. if (ip_idx[idx] == -1)
  3351. continue;
  3352. ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
  3353. if (ret == QLA_ERROR) {
  3354. ip_idx[idx] = -1;
  3355. continue;
  3356. }
  3357. ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
  3358. DEBUG2(ql4_printk(KERN_INFO, ha,
  3359. "Waiting for IP state for idx = %d, state = 0x%x\n",
  3360. ip_idx[idx], ip_state));
  3361. if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
  3362. ip_state == IP_ADDRSTATE_INVALID ||
  3363. ip_state == IP_ADDRSTATE_PREFERRED ||
  3364. ip_state == IP_ADDRSTATE_DEPRICATED ||
  3365. ip_state == IP_ADDRSTATE_DISABLING)
  3366. ip_idx[idx] = -1;
  3367. }
  3368. /* Break if all IP states checked */
  3369. if ((ip_idx[0] == -1) &&
  3370. (ip_idx[1] == -1) &&
  3371. (ip_idx[2] == -1) &&
  3372. (ip_idx[3] == -1))
  3373. break;
  3374. schedule_timeout_uninterruptible(HZ);
  3375. } while (time_after(wtime, jiffies));
  3376. }
  3377. void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
  3378. {
  3379. int max_ddbs;
  3380. int ret;
  3381. uint32_t idx = 0, next_idx = 0;
  3382. uint32_t state = 0, conn_err = 0;
  3383. uint16_t conn_id;
  3384. struct dev_db_entry *fw_ddb_entry;
  3385. struct ddb_entry *ddb_entry = NULL;
  3386. dma_addr_t fw_ddb_dma;
  3387. struct iscsi_cls_session *cls_sess;
  3388. struct iscsi_session *sess;
  3389. struct iscsi_cls_conn *cls_conn;
  3390. struct iscsi_endpoint *ep;
  3391. uint16_t cmds_max = 32, tmo = 0;
  3392. uint32_t initial_cmdsn = 0;
  3393. struct list_head list_st, list_nt; /* List of sendtargets */
  3394. struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
  3395. int fw_idx_size;
  3396. unsigned long wtime;
  3397. struct qla_ddb_index *nt_ddb_idx;
  3398. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  3399. set_bit(AF_BUILD_DDB_LIST, &ha->flags);
  3400. ha->is_reset = is_reset;
  3401. return;
  3402. }
  3403. max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  3404. MAX_DEV_DB_ENTRIES;
  3405. fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  3406. &fw_ddb_dma);
  3407. if (fw_ddb_entry == NULL) {
  3408. DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
  3409. goto exit_ddb_list;
  3410. }
  3411. INIT_LIST_HEAD(&list_st);
  3412. INIT_LIST_HEAD(&list_nt);
  3413. fw_idx_size = sizeof(struct qla_ddb_index);
  3414. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  3415. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
  3416. fw_ddb_dma, NULL,
  3417. &next_idx, &state, &conn_err,
  3418. NULL, &conn_id);
  3419. if (ret == QLA_ERROR)
  3420. break;
  3421. if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
  3422. goto continue_next_st;
  3423. /* Check if ST, add to the list_st */
  3424. if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
  3425. goto continue_next_st;
  3426. st_ddb_idx = vzalloc(fw_idx_size);
  3427. if (!st_ddb_idx)
  3428. break;
  3429. st_ddb_idx->fw_ddb_idx = idx;
  3430. list_add_tail(&st_ddb_idx->list, &list_st);
  3431. continue_next_st:
  3432. if (next_idx == 0)
  3433. break;
  3434. }
  3435. /* Before issuing conn open mbox, ensure all IPs states are configured
  3436. * Note, conn open fails if IPs are not configured
  3437. */
  3438. qla4xxx_wait_for_ip_configuration(ha);
  3439. /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
  3440. list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
  3441. qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
  3442. }
  3443. /* Wait to ensure all sendtargets are done for min 12 sec wait */
  3444. tmo = ((ha->def_timeout < LOGIN_TOV) ? LOGIN_TOV : ha->def_timeout);
  3445. DEBUG2(ql4_printk(KERN_INFO, ha,
  3446. "Default time to wait for build ddb %d\n", tmo));
  3447. wtime = jiffies + (HZ * tmo);
  3448. do {
  3449. list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st,
  3450. list) {
  3451. ret = qla4xxx_get_fwddb_entry(ha,
  3452. st_ddb_idx->fw_ddb_idx,
  3453. NULL, 0, NULL, &next_idx,
  3454. &state, &conn_err, NULL,
  3455. NULL);
  3456. if (ret == QLA_ERROR)
  3457. continue;
  3458. if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  3459. state == DDB_DS_SESSION_FAILED) {
  3460. list_del_init(&st_ddb_idx->list);
  3461. vfree(st_ddb_idx);
  3462. }
  3463. }
  3464. schedule_timeout_uninterruptible(HZ / 10);
  3465. } while (time_after(wtime, jiffies));
  3466. /* Free up the sendtargets list */
  3467. list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
  3468. list_del_init(&st_ddb_idx->list);
  3469. vfree(st_ddb_idx);
  3470. }
  3471. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  3472. ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
  3473. fw_ddb_dma, NULL,
  3474. &next_idx, &state, &conn_err,
  3475. NULL, &conn_id);
  3476. if (ret == QLA_ERROR)
  3477. break;
  3478. if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
  3479. goto continue_next_nt;
  3480. /* Check if NT, then add to list it */
  3481. if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
  3482. goto continue_next_nt;
  3483. if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  3484. state == DDB_DS_SESSION_FAILED) {
  3485. DEBUG2(ql4_printk(KERN_INFO, ha,
  3486. "Adding DDB to session = 0x%x\n",
  3487. idx));
  3488. if (is_reset == INIT_ADAPTER) {
  3489. nt_ddb_idx = vmalloc(fw_idx_size);
  3490. if (!nt_ddb_idx)
  3491. break;
  3492. nt_ddb_idx->fw_ddb_idx = idx;
  3493. memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
  3494. sizeof(struct dev_db_entry));
  3495. if (qla4xxx_is_flash_ddb_exists(ha, &list_nt,
  3496. fw_ddb_entry) == QLA_SUCCESS) {
  3497. vfree(nt_ddb_idx);
  3498. goto continue_next_nt;
  3499. }
  3500. list_add_tail(&nt_ddb_idx->list, &list_nt);
  3501. } else if (is_reset == RESET_ADAPTER) {
  3502. if (qla4xxx_is_session_exists(ha,
  3503. fw_ddb_entry) == QLA_SUCCESS)
  3504. goto continue_next_nt;
  3505. }
  3506. /* Create session object, with INVALID_ENTRY,
  3507. * the targer_id would get set when we issue the login
  3508. */
  3509. cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport,
  3510. ha->host, cmds_max,
  3511. sizeof(struct ddb_entry),
  3512. sizeof(struct ql4_task_data),
  3513. initial_cmdsn, INVALID_ENTRY);
  3514. if (!cls_sess)
  3515. goto exit_ddb_list;
  3516. /*
  3517. * iscsi_session_setup increments the driver reference
  3518. * count which wouldn't let the driver to be unloaded.
  3519. * so calling module_put function to decrement the
  3520. * reference count.
  3521. **/
  3522. module_put(qla4xxx_iscsi_transport.owner);
  3523. sess = cls_sess->dd_data;
  3524. ddb_entry = sess->dd_data;
  3525. ddb_entry->sess = cls_sess;
  3526. cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  3527. memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
  3528. sizeof(struct dev_db_entry));
  3529. qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
  3530. cls_conn = iscsi_conn_setup(cls_sess,
  3531. sizeof(struct qla_conn),
  3532. conn_id);
  3533. if (!cls_conn)
  3534. goto exit_ddb_list;
  3535. ddb_entry->conn = cls_conn;
  3536. /* Setup ep, for displaying attributes in sysfs */
  3537. ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
  3538. if (ep) {
  3539. ep->conn = cls_conn;
  3540. cls_conn->ep = ep;
  3541. } else {
  3542. DEBUG2(ql4_printk(KERN_ERR, ha,
  3543. "Unable to get ep\n"));
  3544. }
  3545. /* Update sess/conn params */
  3546. qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess,
  3547. cls_conn);
  3548. if (is_reset == RESET_ADAPTER) {
  3549. iscsi_block_session(cls_sess);
  3550. /* Use the relogin path to discover new devices
  3551. * by short-circuting the logic of setting
  3552. * timer to relogin - instead set the flags
  3553. * to initiate login right away.
  3554. */
  3555. set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  3556. set_bit(DF_RELOGIN, &ddb_entry->flags);
  3557. }
  3558. }
  3559. continue_next_nt:
  3560. if (next_idx == 0)
  3561. break;
  3562. }
  3563. exit_ddb_list:
  3564. qla4xxx_free_nt_list(&list_nt);
  3565. if (fw_ddb_entry)
  3566. dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  3567. qla4xxx_free_ddb_index(ha);
  3568. }
  3569. /**
  3570. * qla4xxx_probe_adapter - callback function to probe HBA
  3571. * @pdev: pointer to pci_dev structure
  3572. * @pci_device_id: pointer to pci_device entry
  3573. *
  3574. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  3575. * It returns zero if successful. It also initializes all data necessary for
  3576. * the driver.
  3577. **/
  3578. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  3579. const struct pci_device_id *ent)
  3580. {
  3581. int ret = -ENODEV, status;
  3582. struct Scsi_Host *host;
  3583. struct scsi_qla_host *ha;
  3584. uint8_t init_retry_count = 0;
  3585. char buf[34];
  3586. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  3587. uint32_t dev_state;
  3588. if (pci_enable_device(pdev))
  3589. return -1;
  3590. host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
  3591. if (host == NULL) {
  3592. printk(KERN_WARNING
  3593. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  3594. goto probe_disable_device;
  3595. }
  3596. /* Clear our data area */
  3597. ha = to_qla_host(host);
  3598. memset(ha, 0, sizeof(*ha));
  3599. /* Save the information from PCI BIOS. */
  3600. ha->pdev = pdev;
  3601. ha->host = host;
  3602. ha->host_no = host->host_no;
  3603. pci_enable_pcie_error_reporting(pdev);
  3604. /* Setup Runtime configurable options */
  3605. if (is_qla8022(ha)) {
  3606. ha->isp_ops = &qla4_8xxx_isp_ops;
  3607. rwlock_init(&ha->hw_lock);
  3608. ha->qdr_sn_window = -1;
  3609. ha->ddr_mn_window = -1;
  3610. ha->curr_window = 255;
  3611. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  3612. nx_legacy_intr = &legacy_intr[ha->func_num];
  3613. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  3614. ha->nx_legacy_intr.tgt_status_reg =
  3615. nx_legacy_intr->tgt_status_reg;
  3616. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  3617. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  3618. } else {
  3619. ha->isp_ops = &qla4xxx_isp_ops;
  3620. }
  3621. /* Set EEH reset type to fundamental if required by hba */
  3622. if (is_qla8022(ha))
  3623. pdev->needs_freset = 1;
  3624. /* Configure PCI I/O space. */
  3625. ret = ha->isp_ops->iospace_config(ha);
  3626. if (ret)
  3627. goto probe_failed_ioconfig;
  3628. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  3629. pdev->device, pdev->irq, ha->reg);
  3630. qla4xxx_config_dma_addressing(ha);
  3631. /* Initialize lists and spinlocks. */
  3632. INIT_LIST_HEAD(&ha->free_srb_q);
  3633. mutex_init(&ha->mbox_sem);
  3634. mutex_init(&ha->chap_sem);
  3635. init_completion(&ha->mbx_intr_comp);
  3636. init_completion(&ha->disable_acb_comp);
  3637. spin_lock_init(&ha->hardware_lock);
  3638. /* Allocate dma buffers */
  3639. if (qla4xxx_mem_alloc(ha)) {
  3640. ql4_printk(KERN_WARNING, ha,
  3641. "[ERROR] Failed to allocate memory for adapter\n");
  3642. ret = -ENOMEM;
  3643. goto probe_failed;
  3644. }
  3645. host->cmd_per_lun = 3;
  3646. host->max_channel = 0;
  3647. host->max_lun = MAX_LUNS - 1;
  3648. host->max_id = MAX_TARGETS;
  3649. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  3650. host->can_queue = MAX_SRBS ;
  3651. host->transportt = qla4xxx_scsi_transport;
  3652. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  3653. if (ret) {
  3654. ql4_printk(KERN_WARNING, ha,
  3655. "%s: scsi_init_shared_tag_map failed\n", __func__);
  3656. goto probe_failed;
  3657. }
  3658. pci_set_drvdata(pdev, ha);
  3659. ret = scsi_add_host(host, &pdev->dev);
  3660. if (ret)
  3661. goto probe_failed;
  3662. if (is_qla8022(ha))
  3663. (void) qla4_8xxx_get_flash_info(ha);
  3664. /*
  3665. * Initialize the Host adapter request/response queues and
  3666. * firmware
  3667. * NOTE: interrupts enabled upon successful completion
  3668. */
  3669. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  3670. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  3671. init_retry_count++ < MAX_INIT_RETRIES) {
  3672. if (is_qla8022(ha)) {
  3673. qla4_8xxx_idc_lock(ha);
  3674. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3675. qla4_8xxx_idc_unlock(ha);
  3676. if (dev_state == QLA82XX_DEV_FAILED) {
  3677. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  3678. "initialize adapter. H/W is in failed state\n",
  3679. __func__);
  3680. break;
  3681. }
  3682. }
  3683. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  3684. "(%d)\n", __func__, init_retry_count));
  3685. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  3686. continue;
  3687. status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
  3688. }
  3689. if (!test_bit(AF_ONLINE, &ha->flags)) {
  3690. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  3691. if (is_qla8022(ha) && ql4xdontresethba) {
  3692. /* Put the device in failed state. */
  3693. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  3694. qla4_8xxx_idc_lock(ha);
  3695. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  3696. QLA82XX_DEV_FAILED);
  3697. qla4_8xxx_idc_unlock(ha);
  3698. }
  3699. ret = -ENODEV;
  3700. goto remove_host;
  3701. }
  3702. /* Startup the kernel thread for this host adapter. */
  3703. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  3704. "qla4xxx_dpc\n", __func__));
  3705. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  3706. ha->dpc_thread = create_singlethread_workqueue(buf);
  3707. if (!ha->dpc_thread) {
  3708. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  3709. ret = -ENODEV;
  3710. goto remove_host;
  3711. }
  3712. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  3713. sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
  3714. ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
  3715. if (!ha->task_wq) {
  3716. ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
  3717. ret = -ENODEV;
  3718. goto remove_host;
  3719. }
  3720. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  3721. * (which is called indirectly by qla4xxx_initialize_adapter),
  3722. * so that irqs will be registered after crbinit but before
  3723. * mbx_intr_enable.
  3724. */
  3725. if (!is_qla8022(ha)) {
  3726. ret = qla4xxx_request_irqs(ha);
  3727. if (ret) {
  3728. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  3729. "interrupt %d already in use.\n", pdev->irq);
  3730. goto remove_host;
  3731. }
  3732. }
  3733. pci_save_state(ha->pdev);
  3734. ha->isp_ops->enable_intrs(ha);
  3735. /* Start timer thread. */
  3736. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  3737. set_bit(AF_INIT_DONE, &ha->flags);
  3738. printk(KERN_INFO
  3739. " QLogic iSCSI HBA Driver version: %s\n"
  3740. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  3741. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  3742. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  3743. ha->patch_number, ha->build_number);
  3744. if (qla4xxx_setup_boot_info(ha))
  3745. ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
  3746. __func__);
  3747. /* Perform the build ddb list and login to each */
  3748. qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
  3749. iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
  3750. qla4xxx_create_chap_list(ha);
  3751. qla4xxx_create_ifaces(ha);
  3752. return 0;
  3753. remove_host:
  3754. scsi_remove_host(ha->host);
  3755. probe_failed:
  3756. qla4xxx_free_adapter(ha);
  3757. probe_failed_ioconfig:
  3758. pci_disable_pcie_error_reporting(pdev);
  3759. scsi_host_put(ha->host);
  3760. probe_disable_device:
  3761. pci_disable_device(pdev);
  3762. return ret;
  3763. }
  3764. /**
  3765. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  3766. * @ha: pointer to adapter structure
  3767. *
  3768. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  3769. * so that the other port will not re-initialize while in the process of
  3770. * removing the ha due to driver unload or hba hotplug.
  3771. **/
  3772. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  3773. {
  3774. struct scsi_qla_host *other_ha = NULL;
  3775. struct pci_dev *other_pdev = NULL;
  3776. int fn = ISP4XXX_PCI_FN_2;
  3777. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  3778. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  3779. fn = ISP4XXX_PCI_FN_1;
  3780. other_pdev =
  3781. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  3782. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  3783. fn));
  3784. /* Get other_ha if other_pdev is valid and state is enable*/
  3785. if (other_pdev) {
  3786. if (atomic_read(&other_pdev->enable_cnt)) {
  3787. other_ha = pci_get_drvdata(other_pdev);
  3788. if (other_ha) {
  3789. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  3790. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  3791. "Prevent %s reinit\n", __func__,
  3792. dev_name(&other_ha->pdev->dev)));
  3793. }
  3794. }
  3795. pci_dev_put(other_pdev);
  3796. }
  3797. }
  3798. static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
  3799. {
  3800. struct ddb_entry *ddb_entry;
  3801. int options;
  3802. int idx;
  3803. for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  3804. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  3805. if ((ddb_entry != NULL) &&
  3806. (ddb_entry->ddb_type == FLASH_DDB)) {
  3807. options = LOGOUT_OPTION_CLOSE_SESSION;
  3808. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
  3809. == QLA_ERROR)
  3810. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
  3811. __func__);
  3812. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  3813. /*
  3814. * we have decremented the reference count of the driver
  3815. * when we setup the session to have the driver unload
  3816. * to be seamless without actually destroying the
  3817. * session
  3818. **/
  3819. try_module_get(qla4xxx_iscsi_transport.owner);
  3820. iscsi_destroy_endpoint(ddb_entry->conn->ep);
  3821. qla4xxx_free_ddb(ha, ddb_entry);
  3822. iscsi_session_teardown(ddb_entry->sess);
  3823. }
  3824. }
  3825. }
  3826. /**
  3827. * qla4xxx_remove_adapter - calback function to remove adapter.
  3828. * @pci_dev: PCI device pointer
  3829. **/
  3830. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  3831. {
  3832. struct scsi_qla_host *ha;
  3833. ha = pci_get_drvdata(pdev);
  3834. if (!is_qla8022(ha))
  3835. qla4xxx_prevent_other_port_reinit(ha);
  3836. /* destroy iface from sysfs */
  3837. qla4xxx_destroy_ifaces(ha);
  3838. if ((!ql4xdisablesysfsboot) && ha->boot_kset)
  3839. iscsi_boot_destroy_kset(ha->boot_kset);
  3840. qla4xxx_destroy_fw_ddb_session(ha);
  3841. scsi_remove_host(ha->host);
  3842. qla4xxx_free_adapter(ha);
  3843. scsi_host_put(ha->host);
  3844. pci_disable_pcie_error_reporting(pdev);
  3845. pci_disable_device(pdev);
  3846. pci_set_drvdata(pdev, NULL);
  3847. }
  3848. /**
  3849. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  3850. * @ha: HA context
  3851. *
  3852. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  3853. * supported addressing method.
  3854. */
  3855. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  3856. {
  3857. int retval;
  3858. /* Update our PCI device dma_mask for full 64 bit mask */
  3859. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  3860. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  3861. dev_dbg(&ha->pdev->dev,
  3862. "Failed to set 64 bit PCI consistent mask; "
  3863. "using 32 bit.\n");
  3864. retval = pci_set_consistent_dma_mask(ha->pdev,
  3865. DMA_BIT_MASK(32));
  3866. }
  3867. } else
  3868. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  3869. }
  3870. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  3871. {
  3872. struct iscsi_cls_session *cls_sess;
  3873. struct iscsi_session *sess;
  3874. struct ddb_entry *ddb;
  3875. int queue_depth = QL4_DEF_QDEPTH;
  3876. cls_sess = starget_to_session(sdev->sdev_target);
  3877. sess = cls_sess->dd_data;
  3878. ddb = sess->dd_data;
  3879. sdev->hostdata = ddb;
  3880. sdev->tagged_supported = 1;
  3881. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  3882. queue_depth = ql4xmaxqdepth;
  3883. scsi_activate_tcq(sdev, queue_depth);
  3884. return 0;
  3885. }
  3886. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  3887. {
  3888. sdev->tagged_supported = 1;
  3889. return 0;
  3890. }
  3891. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  3892. {
  3893. scsi_deactivate_tcq(sdev, 1);
  3894. }
  3895. /**
  3896. * qla4xxx_del_from_active_array - returns an active srb
  3897. * @ha: Pointer to host adapter structure.
  3898. * @index: index into the active_array
  3899. *
  3900. * This routine removes and returns the srb at the specified index
  3901. **/
  3902. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  3903. uint32_t index)
  3904. {
  3905. struct srb *srb = NULL;
  3906. struct scsi_cmnd *cmd = NULL;
  3907. cmd = scsi_host_find_tag(ha->host, index);
  3908. if (!cmd)
  3909. return srb;
  3910. srb = (struct srb *)CMD_SP(cmd);
  3911. if (!srb)
  3912. return srb;
  3913. /* update counters */
  3914. if (srb->flags & SRB_DMA_VALID) {
  3915. ha->req_q_count += srb->iocb_cnt;
  3916. ha->iocb_cnt -= srb->iocb_cnt;
  3917. if (srb->cmd)
  3918. srb->cmd->host_scribble =
  3919. (unsigned char *)(unsigned long) MAX_SRBS;
  3920. }
  3921. return srb;
  3922. }
  3923. /**
  3924. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  3925. * @ha: Pointer to host adapter structure.
  3926. * @cmd: Scsi Command to wait on.
  3927. *
  3928. * This routine waits for the command to be returned by the Firmware
  3929. * for some max time.
  3930. **/
  3931. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  3932. struct scsi_cmnd *cmd)
  3933. {
  3934. int done = 0;
  3935. struct srb *rp;
  3936. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  3937. int ret = SUCCESS;
  3938. /* Dont wait on command if PCI error is being handled
  3939. * by PCI AER driver
  3940. */
  3941. if (unlikely(pci_channel_offline(ha->pdev)) ||
  3942. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  3943. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  3944. ha->host_no, __func__);
  3945. return ret;
  3946. }
  3947. do {
  3948. /* Checking to see if its returned to OS */
  3949. rp = (struct srb *) CMD_SP(cmd);
  3950. if (rp == NULL) {
  3951. done++;
  3952. break;
  3953. }
  3954. msleep(2000);
  3955. } while (max_wait_time--);
  3956. return done;
  3957. }
  3958. /**
  3959. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  3960. * @ha: Pointer to host adapter structure
  3961. **/
  3962. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  3963. {
  3964. unsigned long wait_online;
  3965. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  3966. while (time_before(jiffies, wait_online)) {
  3967. if (adapter_up(ha))
  3968. return QLA_SUCCESS;
  3969. msleep(2000);
  3970. }
  3971. return QLA_ERROR;
  3972. }
  3973. /**
  3974. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  3975. * @ha: pointer to HBA
  3976. * @t: target id
  3977. * @l: lun id
  3978. *
  3979. * This function waits for all outstanding commands to a lun to complete. It
  3980. * returns 0 if all pending commands are returned and 1 otherwise.
  3981. **/
  3982. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  3983. struct scsi_target *stgt,
  3984. struct scsi_device *sdev)
  3985. {
  3986. int cnt;
  3987. int status = 0;
  3988. struct scsi_cmnd *cmd;
  3989. /*
  3990. * Waiting for all commands for the designated target or dev
  3991. * in the active array
  3992. */
  3993. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  3994. cmd = scsi_host_find_tag(ha->host, cnt);
  3995. if (cmd && stgt == scsi_target(cmd->device) &&
  3996. (!sdev || sdev == cmd->device)) {
  3997. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  3998. status++;
  3999. break;
  4000. }
  4001. }
  4002. }
  4003. return status;
  4004. }
  4005. /**
  4006. * qla4xxx_eh_abort - callback for abort task.
  4007. * @cmd: Pointer to Linux's SCSI command structure
  4008. *
  4009. * This routine is called by the Linux OS to abort the specified
  4010. * command.
  4011. **/
  4012. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  4013. {
  4014. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4015. unsigned int id = cmd->device->id;
  4016. unsigned int lun = cmd->device->lun;
  4017. unsigned long flags;
  4018. struct srb *srb = NULL;
  4019. int ret = SUCCESS;
  4020. int wait = 0;
  4021. ql4_printk(KERN_INFO, ha,
  4022. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  4023. ha->host_no, id, lun, cmd);
  4024. spin_lock_irqsave(&ha->hardware_lock, flags);
  4025. srb = (struct srb *) CMD_SP(cmd);
  4026. if (!srb) {
  4027. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4028. return SUCCESS;
  4029. }
  4030. kref_get(&srb->srb_ref);
  4031. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  4032. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  4033. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  4034. ha->host_no, id, lun));
  4035. ret = FAILED;
  4036. } else {
  4037. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  4038. ha->host_no, id, lun));
  4039. wait = 1;
  4040. }
  4041. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  4042. /* Wait for command to complete */
  4043. if (wait) {
  4044. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  4045. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  4046. ha->host_no, id, lun));
  4047. ret = FAILED;
  4048. }
  4049. }
  4050. ql4_printk(KERN_INFO, ha,
  4051. "scsi%ld:%d:%d: Abort command - %s\n",
  4052. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  4053. return ret;
  4054. }
  4055. /**
  4056. * qla4xxx_eh_device_reset - callback for target reset.
  4057. * @cmd: Pointer to Linux's SCSI command structure
  4058. *
  4059. * This routine is called by the Linux OS to reset all luns on the
  4060. * specified target.
  4061. **/
  4062. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  4063. {
  4064. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4065. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  4066. int ret = FAILED, stat;
  4067. if (!ddb_entry)
  4068. return ret;
  4069. ret = iscsi_block_scsi_eh(cmd);
  4070. if (ret)
  4071. return ret;
  4072. ret = FAILED;
  4073. ql4_printk(KERN_INFO, ha,
  4074. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  4075. cmd->device->channel, cmd->device->id, cmd->device->lun);
  4076. DEBUG2(printk(KERN_INFO
  4077. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  4078. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  4079. cmd, jiffies, cmd->request->timeout / HZ,
  4080. ha->dpc_flags, cmd->result, cmd->allowed));
  4081. /* FIXME: wait for hba to go online */
  4082. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  4083. if (stat != QLA_SUCCESS) {
  4084. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  4085. goto eh_dev_reset_done;
  4086. }
  4087. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  4088. cmd->device)) {
  4089. ql4_printk(KERN_INFO, ha,
  4090. "DEVICE RESET FAILED - waiting for "
  4091. "commands.\n");
  4092. goto eh_dev_reset_done;
  4093. }
  4094. /* Send marker. */
  4095. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  4096. MM_LUN_RESET) != QLA_SUCCESS)
  4097. goto eh_dev_reset_done;
  4098. ql4_printk(KERN_INFO, ha,
  4099. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  4100. ha->host_no, cmd->device->channel, cmd->device->id,
  4101. cmd->device->lun);
  4102. ret = SUCCESS;
  4103. eh_dev_reset_done:
  4104. return ret;
  4105. }
  4106. /**
  4107. * qla4xxx_eh_target_reset - callback for target reset.
  4108. * @cmd: Pointer to Linux's SCSI command structure
  4109. *
  4110. * This routine is called by the Linux OS to reset the target.
  4111. **/
  4112. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  4113. {
  4114. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  4115. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  4116. int stat, ret;
  4117. if (!ddb_entry)
  4118. return FAILED;
  4119. ret = iscsi_block_scsi_eh(cmd);
  4120. if (ret)
  4121. return ret;
  4122. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4123. "WARM TARGET RESET ISSUED.\n");
  4124. DEBUG2(printk(KERN_INFO
  4125. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  4126. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  4127. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  4128. ha->dpc_flags, cmd->result, cmd->allowed));
  4129. stat = qla4xxx_reset_target(ha, ddb_entry);
  4130. if (stat != QLA_SUCCESS) {
  4131. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4132. "WARM TARGET RESET FAILED.\n");
  4133. return FAILED;
  4134. }
  4135. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  4136. NULL)) {
  4137. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4138. "WARM TARGET DEVICE RESET FAILED - "
  4139. "waiting for commands.\n");
  4140. return FAILED;
  4141. }
  4142. /* Send marker. */
  4143. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  4144. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  4145. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4146. "WARM TARGET DEVICE RESET FAILED - "
  4147. "marker iocb failed.\n");
  4148. return FAILED;
  4149. }
  4150. starget_printk(KERN_INFO, scsi_target(cmd->device),
  4151. "WARM TARGET RESET SUCCEEDED.\n");
  4152. return SUCCESS;
  4153. }
  4154. /**
  4155. * qla4xxx_is_eh_active - check if error handler is running
  4156. * @shost: Pointer to SCSI Host struct
  4157. *
  4158. * This routine finds that if reset host is called in EH
  4159. * scenario or from some application like sg_reset
  4160. **/
  4161. static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
  4162. {
  4163. if (shost->shost_state == SHOST_RECOVERY)
  4164. return 1;
  4165. return 0;
  4166. }
  4167. /**
  4168. * qla4xxx_eh_host_reset - kernel callback
  4169. * @cmd: Pointer to Linux's SCSI command structure
  4170. *
  4171. * This routine is invoked by the Linux kernel to perform fatal error
  4172. * recovery on the specified adapter.
  4173. **/
  4174. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  4175. {
  4176. int return_status = FAILED;
  4177. struct scsi_qla_host *ha;
  4178. ha = to_qla_host(cmd->device->host);
  4179. if (ql4xdontresethba) {
  4180. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  4181. ha->host_no, __func__));
  4182. /* Clear outstanding srb in queues */
  4183. if (qla4xxx_is_eh_active(cmd->device->host))
  4184. qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
  4185. return FAILED;
  4186. }
  4187. ql4_printk(KERN_INFO, ha,
  4188. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  4189. cmd->device->channel, cmd->device->id, cmd->device->lun);
  4190. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  4191. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  4192. "DEAD.\n", ha->host_no, cmd->device->channel,
  4193. __func__));
  4194. return FAILED;
  4195. }
  4196. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  4197. if (is_qla8022(ha))
  4198. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  4199. else
  4200. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  4201. }
  4202. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  4203. return_status = SUCCESS;
  4204. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  4205. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  4206. return return_status;
  4207. }
  4208. static int qla4xxx_context_reset(struct scsi_qla_host *ha)
  4209. {
  4210. uint32_t mbox_cmd[MBOX_REG_COUNT];
  4211. uint32_t mbox_sts[MBOX_REG_COUNT];
  4212. struct addr_ctrl_blk_def *acb = NULL;
  4213. uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
  4214. int rval = QLA_SUCCESS;
  4215. dma_addr_t acb_dma;
  4216. acb = dma_alloc_coherent(&ha->pdev->dev,
  4217. sizeof(struct addr_ctrl_blk_def),
  4218. &acb_dma, GFP_KERNEL);
  4219. if (!acb) {
  4220. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
  4221. __func__);
  4222. rval = -ENOMEM;
  4223. goto exit_port_reset;
  4224. }
  4225. memset(acb, 0, acb_len);
  4226. rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
  4227. if (rval != QLA_SUCCESS) {
  4228. rval = -EIO;
  4229. goto exit_free_acb;
  4230. }
  4231. rval = qla4xxx_disable_acb(ha);
  4232. if (rval != QLA_SUCCESS) {
  4233. rval = -EIO;
  4234. goto exit_free_acb;
  4235. }
  4236. wait_for_completion_timeout(&ha->disable_acb_comp,
  4237. DISABLE_ACB_TOV * HZ);
  4238. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
  4239. if (rval != QLA_SUCCESS) {
  4240. rval = -EIO;
  4241. goto exit_free_acb;
  4242. }
  4243. exit_free_acb:
  4244. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
  4245. acb, acb_dma);
  4246. exit_port_reset:
  4247. DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
  4248. rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
  4249. return rval;
  4250. }
  4251. static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
  4252. {
  4253. struct scsi_qla_host *ha = to_qla_host(shost);
  4254. int rval = QLA_SUCCESS;
  4255. if (ql4xdontresethba) {
  4256. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
  4257. __func__));
  4258. rval = -EPERM;
  4259. goto exit_host_reset;
  4260. }
  4261. rval = qla4xxx_wait_for_hba_online(ha);
  4262. if (rval != QLA_SUCCESS) {
  4263. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
  4264. "adapter\n", __func__));
  4265. rval = -EIO;
  4266. goto exit_host_reset;
  4267. }
  4268. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  4269. goto recover_adapter;
  4270. switch (reset_type) {
  4271. case SCSI_ADAPTER_RESET:
  4272. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  4273. break;
  4274. case SCSI_FIRMWARE_RESET:
  4275. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  4276. if (is_qla8022(ha))
  4277. /* set firmware context reset */
  4278. set_bit(DPC_RESET_HA_FW_CONTEXT,
  4279. &ha->dpc_flags);
  4280. else {
  4281. rval = qla4xxx_context_reset(ha);
  4282. goto exit_host_reset;
  4283. }
  4284. }
  4285. break;
  4286. }
  4287. recover_adapter:
  4288. rval = qla4xxx_recover_adapter(ha);
  4289. if (rval != QLA_SUCCESS) {
  4290. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
  4291. __func__));
  4292. rval = -EIO;
  4293. }
  4294. exit_host_reset:
  4295. return rval;
  4296. }
  4297. /* PCI AER driver recovers from all correctable errors w/o
  4298. * driver intervention. For uncorrectable errors PCI AER
  4299. * driver calls the following device driver's callbacks
  4300. *
  4301. * - Fatal Errors - link_reset
  4302. * - Non-Fatal Errors - driver's pci_error_detected() which
  4303. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  4304. *
  4305. * PCI AER driver calls
  4306. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  4307. * returns RECOVERED or NEED_RESET if fw_hung
  4308. * NEED_RESET - driver's slot_reset()
  4309. * DISCONNECT - device is dead & cannot recover
  4310. * RECOVERED - driver's pci_resume()
  4311. */
  4312. static pci_ers_result_t
  4313. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  4314. {
  4315. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  4316. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  4317. ha->host_no, __func__, state);
  4318. if (!is_aer_supported(ha))
  4319. return PCI_ERS_RESULT_NONE;
  4320. switch (state) {
  4321. case pci_channel_io_normal:
  4322. clear_bit(AF_EEH_BUSY, &ha->flags);
  4323. return PCI_ERS_RESULT_CAN_RECOVER;
  4324. case pci_channel_io_frozen:
  4325. set_bit(AF_EEH_BUSY, &ha->flags);
  4326. qla4xxx_mailbox_premature_completion(ha);
  4327. qla4xxx_free_irqs(ha);
  4328. pci_disable_device(pdev);
  4329. /* Return back all IOs */
  4330. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  4331. return PCI_ERS_RESULT_NEED_RESET;
  4332. case pci_channel_io_perm_failure:
  4333. set_bit(AF_EEH_BUSY, &ha->flags);
  4334. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  4335. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  4336. return PCI_ERS_RESULT_DISCONNECT;
  4337. }
  4338. return PCI_ERS_RESULT_NEED_RESET;
  4339. }
  4340. /**
  4341. * qla4xxx_pci_mmio_enabled() gets called if
  4342. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  4343. * and read/write to the device still works.
  4344. **/
  4345. static pci_ers_result_t
  4346. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  4347. {
  4348. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  4349. if (!is_aer_supported(ha))
  4350. return PCI_ERS_RESULT_NONE;
  4351. return PCI_ERS_RESULT_RECOVERED;
  4352. }
  4353. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  4354. {
  4355. uint32_t rval = QLA_ERROR;
  4356. uint32_t ret = 0;
  4357. int fn;
  4358. struct pci_dev *other_pdev = NULL;
  4359. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  4360. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  4361. if (test_bit(AF_ONLINE, &ha->flags)) {
  4362. clear_bit(AF_ONLINE, &ha->flags);
  4363. clear_bit(AF_LINK_UP, &ha->flags);
  4364. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  4365. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  4366. }
  4367. fn = PCI_FUNC(ha->pdev->devfn);
  4368. while (fn > 0) {
  4369. fn--;
  4370. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  4371. "func %x\n", ha->host_no, __func__, fn);
  4372. /* Get the pci device given the domain, bus,
  4373. * slot/function number */
  4374. other_pdev =
  4375. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  4376. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  4377. fn));
  4378. if (!other_pdev)
  4379. continue;
  4380. if (atomic_read(&other_pdev->enable_cnt)) {
  4381. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  4382. "func in enabled state%x\n", ha->host_no,
  4383. __func__, fn);
  4384. pci_dev_put(other_pdev);
  4385. break;
  4386. }
  4387. pci_dev_put(other_pdev);
  4388. }
  4389. /* The first function on the card, the reset owner will
  4390. * start & initialize the firmware. The other functions
  4391. * on the card will reset the firmware context
  4392. */
  4393. if (!fn) {
  4394. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  4395. "0x%x is the owner\n", ha->host_no, __func__,
  4396. ha->pdev->devfn);
  4397. qla4_8xxx_idc_lock(ha);
  4398. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  4399. QLA82XX_DEV_COLD);
  4400. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  4401. QLA82XX_IDC_VERSION);
  4402. qla4_8xxx_idc_unlock(ha);
  4403. clear_bit(AF_FW_RECOVERY, &ha->flags);
  4404. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  4405. qla4_8xxx_idc_lock(ha);
  4406. if (rval != QLA_SUCCESS) {
  4407. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  4408. "FAILED\n", ha->host_no, __func__);
  4409. qla4_8xxx_clear_drv_active(ha);
  4410. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  4411. QLA82XX_DEV_FAILED);
  4412. } else {
  4413. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  4414. "READY\n", ha->host_no, __func__);
  4415. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  4416. QLA82XX_DEV_READY);
  4417. /* Clear driver state register */
  4418. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  4419. qla4_8xxx_set_drv_active(ha);
  4420. ret = qla4xxx_request_irqs(ha);
  4421. if (ret) {
  4422. ql4_printk(KERN_WARNING, ha, "Failed to "
  4423. "reserve interrupt %d already in use.\n",
  4424. ha->pdev->irq);
  4425. rval = QLA_ERROR;
  4426. } else {
  4427. ha->isp_ops->enable_intrs(ha);
  4428. rval = QLA_SUCCESS;
  4429. }
  4430. }
  4431. qla4_8xxx_idc_unlock(ha);
  4432. } else {
  4433. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  4434. "the reset owner\n", ha->host_no, __func__,
  4435. ha->pdev->devfn);
  4436. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  4437. QLA82XX_DEV_READY)) {
  4438. clear_bit(AF_FW_RECOVERY, &ha->flags);
  4439. rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
  4440. if (rval == QLA_SUCCESS) {
  4441. ret = qla4xxx_request_irqs(ha);
  4442. if (ret) {
  4443. ql4_printk(KERN_WARNING, ha, "Failed to"
  4444. " reserve interrupt %d already in"
  4445. " use.\n", ha->pdev->irq);
  4446. rval = QLA_ERROR;
  4447. } else {
  4448. ha->isp_ops->enable_intrs(ha);
  4449. rval = QLA_SUCCESS;
  4450. }
  4451. }
  4452. qla4_8xxx_idc_lock(ha);
  4453. qla4_8xxx_set_drv_active(ha);
  4454. qla4_8xxx_idc_unlock(ha);
  4455. }
  4456. }
  4457. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  4458. return rval;
  4459. }
  4460. static pci_ers_result_t
  4461. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  4462. {
  4463. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  4464. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  4465. int rc;
  4466. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  4467. ha->host_no, __func__);
  4468. if (!is_aer_supported(ha))
  4469. return PCI_ERS_RESULT_NONE;
  4470. /* Restore the saved state of PCIe device -
  4471. * BAR registers, PCI Config space, PCIX, MSI,
  4472. * IOV states
  4473. */
  4474. pci_restore_state(pdev);
  4475. /* pci_restore_state() clears the saved_state flag of the device
  4476. * save restored state which resets saved_state flag
  4477. */
  4478. pci_save_state(pdev);
  4479. /* Initialize device or resume if in suspended state */
  4480. rc = pci_enable_device(pdev);
  4481. if (rc) {
  4482. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  4483. "device after reset\n", ha->host_no, __func__);
  4484. goto exit_slot_reset;
  4485. }
  4486. ha->isp_ops->disable_intrs(ha);
  4487. if (is_qla8022(ha)) {
  4488. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  4489. ret = PCI_ERS_RESULT_RECOVERED;
  4490. goto exit_slot_reset;
  4491. } else
  4492. goto exit_slot_reset;
  4493. }
  4494. exit_slot_reset:
  4495. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  4496. "device after reset\n", ha->host_no, __func__, ret);
  4497. return ret;
  4498. }
  4499. static void
  4500. qla4xxx_pci_resume(struct pci_dev *pdev)
  4501. {
  4502. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  4503. int ret;
  4504. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  4505. ha->host_no, __func__);
  4506. ret = qla4xxx_wait_for_hba_online(ha);
  4507. if (ret != QLA_SUCCESS) {
  4508. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  4509. "resume I/O from slot/link_reset\n", ha->host_no,
  4510. __func__);
  4511. }
  4512. pci_cleanup_aer_uncorrect_error_status(pdev);
  4513. clear_bit(AF_EEH_BUSY, &ha->flags);
  4514. }
  4515. static struct pci_error_handlers qla4xxx_err_handler = {
  4516. .error_detected = qla4xxx_pci_error_detected,
  4517. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  4518. .slot_reset = qla4xxx_pci_slot_reset,
  4519. .resume = qla4xxx_pci_resume,
  4520. };
  4521. static struct pci_device_id qla4xxx_pci_tbl[] = {
  4522. {
  4523. .vendor = PCI_VENDOR_ID_QLOGIC,
  4524. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  4525. .subvendor = PCI_ANY_ID,
  4526. .subdevice = PCI_ANY_ID,
  4527. },
  4528. {
  4529. .vendor = PCI_VENDOR_ID_QLOGIC,
  4530. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  4531. .subvendor = PCI_ANY_ID,
  4532. .subdevice = PCI_ANY_ID,
  4533. },
  4534. {
  4535. .vendor = PCI_VENDOR_ID_QLOGIC,
  4536. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  4537. .subvendor = PCI_ANY_ID,
  4538. .subdevice = PCI_ANY_ID,
  4539. },
  4540. {
  4541. .vendor = PCI_VENDOR_ID_QLOGIC,
  4542. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  4543. .subvendor = PCI_ANY_ID,
  4544. .subdevice = PCI_ANY_ID,
  4545. },
  4546. {0, 0},
  4547. };
  4548. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  4549. static struct pci_driver qla4xxx_pci_driver = {
  4550. .name = DRIVER_NAME,
  4551. .id_table = qla4xxx_pci_tbl,
  4552. .probe = qla4xxx_probe_adapter,
  4553. .remove = qla4xxx_remove_adapter,
  4554. .err_handler = &qla4xxx_err_handler,
  4555. };
  4556. static int __init qla4xxx_module_init(void)
  4557. {
  4558. int ret;
  4559. /* Allocate cache for SRBs. */
  4560. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  4561. SLAB_HWCACHE_ALIGN, NULL);
  4562. if (srb_cachep == NULL) {
  4563. printk(KERN_ERR
  4564. "%s: Unable to allocate SRB cache..."
  4565. "Failing load!\n", DRIVER_NAME);
  4566. ret = -ENOMEM;
  4567. goto no_srp_cache;
  4568. }
  4569. /* Derive version string. */
  4570. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  4571. if (ql4xextended_error_logging)
  4572. strcat(qla4xxx_version_str, "-debug");
  4573. qla4xxx_scsi_transport =
  4574. iscsi_register_transport(&qla4xxx_iscsi_transport);
  4575. if (!qla4xxx_scsi_transport){
  4576. ret = -ENODEV;
  4577. goto release_srb_cache;
  4578. }
  4579. ret = pci_register_driver(&qla4xxx_pci_driver);
  4580. if (ret)
  4581. goto unregister_transport;
  4582. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  4583. return 0;
  4584. unregister_transport:
  4585. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  4586. release_srb_cache:
  4587. kmem_cache_destroy(srb_cachep);
  4588. no_srp_cache:
  4589. return ret;
  4590. }
  4591. static void __exit qla4xxx_module_exit(void)
  4592. {
  4593. pci_unregister_driver(&qla4xxx_pci_driver);
  4594. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  4595. kmem_cache_destroy(srb_cachep);
  4596. }
  4597. module_init(qla4xxx_module_init);
  4598. module_exit(qla4xxx_module_exit);
  4599. MODULE_AUTHOR("QLogic Corporation");
  4600. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  4601. MODULE_LICENSE("GPL");
  4602. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);