ql4_os.c 153 KB

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