ql4_os.c 151 KB

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