ql4_os.c 152 KB

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