qla_nx.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2011 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/delay.h>
  9. #include <linux/pci.h>
  10. #include <linux/ratelimit.h>
  11. #include <linux/vmalloc.h>
  12. #include <scsi/scsi_tcq.h>
  13. #define MASK(n) ((1ULL<<(n))-1)
  14. #define MN_WIN(addr) (((addr & 0x1fc0000) >> 1) | \
  15. ((addr >> 25) & 0x3ff))
  16. #define OCM_WIN(addr) (((addr & 0x1ff0000) >> 1) | \
  17. ((addr >> 25) & 0x3ff))
  18. #define MS_WIN(addr) (addr & 0x0ffc0000)
  19. #define QLA82XX_PCI_MN_2M (0)
  20. #define QLA82XX_PCI_MS_2M (0x80000)
  21. #define QLA82XX_PCI_OCM0_2M (0xc0000)
  22. #define VALID_OCM_ADDR(addr) (((addr) & 0x3f800) != 0x3f800)
  23. #define GET_MEM_OFFS_2M(addr) (addr & MASK(18))
  24. #define BLOCK_PROTECT_BITS 0x0F
  25. /* CRB window related */
  26. #define CRB_BLK(off) ((off >> 20) & 0x3f)
  27. #define CRB_SUBBLK(off) ((off >> 16) & 0xf)
  28. #define CRB_WINDOW_2M (0x130060)
  29. #define QLA82XX_PCI_CAMQM_2M_END (0x04800800UL)
  30. #define CRB_HI(off) ((qla82xx_crb_hub_agt[CRB_BLK(off)] << 20) | \
  31. ((off) & 0xf0000))
  32. #define QLA82XX_PCI_CAMQM_2M_BASE (0x000ff800UL)
  33. #define CRB_INDIRECT_2M (0x1e0000UL)
  34. #define MAX_CRB_XFORM 60
  35. static unsigned long crb_addr_xform[MAX_CRB_XFORM];
  36. int qla82xx_crb_table_initialized;
  37. #define qla82xx_crb_addr_transform(name) \
  38. (crb_addr_xform[QLA82XX_HW_PX_MAP_CRB_##name] = \
  39. QLA82XX_HW_CRB_HUB_AGT_ADR_##name << 20)
  40. static void qla82xx_crb_addr_transform_setup(void)
  41. {
  42. qla82xx_crb_addr_transform(XDMA);
  43. qla82xx_crb_addr_transform(TIMR);
  44. qla82xx_crb_addr_transform(SRE);
  45. qla82xx_crb_addr_transform(SQN3);
  46. qla82xx_crb_addr_transform(SQN2);
  47. qla82xx_crb_addr_transform(SQN1);
  48. qla82xx_crb_addr_transform(SQN0);
  49. qla82xx_crb_addr_transform(SQS3);
  50. qla82xx_crb_addr_transform(SQS2);
  51. qla82xx_crb_addr_transform(SQS1);
  52. qla82xx_crb_addr_transform(SQS0);
  53. qla82xx_crb_addr_transform(RPMX7);
  54. qla82xx_crb_addr_transform(RPMX6);
  55. qla82xx_crb_addr_transform(RPMX5);
  56. qla82xx_crb_addr_transform(RPMX4);
  57. qla82xx_crb_addr_transform(RPMX3);
  58. qla82xx_crb_addr_transform(RPMX2);
  59. qla82xx_crb_addr_transform(RPMX1);
  60. qla82xx_crb_addr_transform(RPMX0);
  61. qla82xx_crb_addr_transform(ROMUSB);
  62. qla82xx_crb_addr_transform(SN);
  63. qla82xx_crb_addr_transform(QMN);
  64. qla82xx_crb_addr_transform(QMS);
  65. qla82xx_crb_addr_transform(PGNI);
  66. qla82xx_crb_addr_transform(PGND);
  67. qla82xx_crb_addr_transform(PGN3);
  68. qla82xx_crb_addr_transform(PGN2);
  69. qla82xx_crb_addr_transform(PGN1);
  70. qla82xx_crb_addr_transform(PGN0);
  71. qla82xx_crb_addr_transform(PGSI);
  72. qla82xx_crb_addr_transform(PGSD);
  73. qla82xx_crb_addr_transform(PGS3);
  74. qla82xx_crb_addr_transform(PGS2);
  75. qla82xx_crb_addr_transform(PGS1);
  76. qla82xx_crb_addr_transform(PGS0);
  77. qla82xx_crb_addr_transform(PS);
  78. qla82xx_crb_addr_transform(PH);
  79. qla82xx_crb_addr_transform(NIU);
  80. qla82xx_crb_addr_transform(I2Q);
  81. qla82xx_crb_addr_transform(EG);
  82. qla82xx_crb_addr_transform(MN);
  83. qla82xx_crb_addr_transform(MS);
  84. qla82xx_crb_addr_transform(CAS2);
  85. qla82xx_crb_addr_transform(CAS1);
  86. qla82xx_crb_addr_transform(CAS0);
  87. qla82xx_crb_addr_transform(CAM);
  88. qla82xx_crb_addr_transform(C2C1);
  89. qla82xx_crb_addr_transform(C2C0);
  90. qla82xx_crb_addr_transform(SMB);
  91. qla82xx_crb_addr_transform(OCM0);
  92. /*
  93. * Used only in P3 just define it for P2 also.
  94. */
  95. qla82xx_crb_addr_transform(I2C0);
  96. qla82xx_crb_table_initialized = 1;
  97. }
  98. struct crb_128M_2M_block_map crb_128M_2M_map[64] = {
  99. {{{0, 0, 0, 0} } },
  100. {{{1, 0x0100000, 0x0102000, 0x120000},
  101. {1, 0x0110000, 0x0120000, 0x130000},
  102. {1, 0x0120000, 0x0122000, 0x124000},
  103. {1, 0x0130000, 0x0132000, 0x126000},
  104. {1, 0x0140000, 0x0142000, 0x128000},
  105. {1, 0x0150000, 0x0152000, 0x12a000},
  106. {1, 0x0160000, 0x0170000, 0x110000},
  107. {1, 0x0170000, 0x0172000, 0x12e000},
  108. {0, 0x0000000, 0x0000000, 0x000000},
  109. {0, 0x0000000, 0x0000000, 0x000000},
  110. {0, 0x0000000, 0x0000000, 0x000000},
  111. {0, 0x0000000, 0x0000000, 0x000000},
  112. {0, 0x0000000, 0x0000000, 0x000000},
  113. {0, 0x0000000, 0x0000000, 0x000000},
  114. {1, 0x01e0000, 0x01e0800, 0x122000},
  115. {0, 0x0000000, 0x0000000, 0x000000} } } ,
  116. {{{1, 0x0200000, 0x0210000, 0x180000} } },
  117. {{{0, 0, 0, 0} } },
  118. {{{1, 0x0400000, 0x0401000, 0x169000} } },
  119. {{{1, 0x0500000, 0x0510000, 0x140000} } },
  120. {{{1, 0x0600000, 0x0610000, 0x1c0000} } },
  121. {{{1, 0x0700000, 0x0704000, 0x1b8000} } },
  122. {{{1, 0x0800000, 0x0802000, 0x170000},
  123. {0, 0x0000000, 0x0000000, 0x000000},
  124. {0, 0x0000000, 0x0000000, 0x000000},
  125. {0, 0x0000000, 0x0000000, 0x000000},
  126. {0, 0x0000000, 0x0000000, 0x000000},
  127. {0, 0x0000000, 0x0000000, 0x000000},
  128. {0, 0x0000000, 0x0000000, 0x000000},
  129. {0, 0x0000000, 0x0000000, 0x000000},
  130. {0, 0x0000000, 0x0000000, 0x000000},
  131. {0, 0x0000000, 0x0000000, 0x000000},
  132. {0, 0x0000000, 0x0000000, 0x000000},
  133. {0, 0x0000000, 0x0000000, 0x000000},
  134. {0, 0x0000000, 0x0000000, 0x000000},
  135. {0, 0x0000000, 0x0000000, 0x000000},
  136. {0, 0x0000000, 0x0000000, 0x000000},
  137. {1, 0x08f0000, 0x08f2000, 0x172000} } },
  138. {{{1, 0x0900000, 0x0902000, 0x174000},
  139. {0, 0x0000000, 0x0000000, 0x000000},
  140. {0, 0x0000000, 0x0000000, 0x000000},
  141. {0, 0x0000000, 0x0000000, 0x000000},
  142. {0, 0x0000000, 0x0000000, 0x000000},
  143. {0, 0x0000000, 0x0000000, 0x000000},
  144. {0, 0x0000000, 0x0000000, 0x000000},
  145. {0, 0x0000000, 0x0000000, 0x000000},
  146. {0, 0x0000000, 0x0000000, 0x000000},
  147. {0, 0x0000000, 0x0000000, 0x000000},
  148. {0, 0x0000000, 0x0000000, 0x000000},
  149. {0, 0x0000000, 0x0000000, 0x000000},
  150. {0, 0x0000000, 0x0000000, 0x000000},
  151. {0, 0x0000000, 0x0000000, 0x000000},
  152. {0, 0x0000000, 0x0000000, 0x000000},
  153. {1, 0x09f0000, 0x09f2000, 0x176000} } },
  154. {{{0, 0x0a00000, 0x0a02000, 0x178000},
  155. {0, 0x0000000, 0x0000000, 0x000000},
  156. {0, 0x0000000, 0x0000000, 0x000000},
  157. {0, 0x0000000, 0x0000000, 0x000000},
  158. {0, 0x0000000, 0x0000000, 0x000000},
  159. {0, 0x0000000, 0x0000000, 0x000000},
  160. {0, 0x0000000, 0x0000000, 0x000000},
  161. {0, 0x0000000, 0x0000000, 0x000000},
  162. {0, 0x0000000, 0x0000000, 0x000000},
  163. {0, 0x0000000, 0x0000000, 0x000000},
  164. {0, 0x0000000, 0x0000000, 0x000000},
  165. {0, 0x0000000, 0x0000000, 0x000000},
  166. {0, 0x0000000, 0x0000000, 0x000000},
  167. {0, 0x0000000, 0x0000000, 0x000000},
  168. {0, 0x0000000, 0x0000000, 0x000000},
  169. {1, 0x0af0000, 0x0af2000, 0x17a000} } },
  170. {{{0, 0x0b00000, 0x0b02000, 0x17c000},
  171. {0, 0x0000000, 0x0000000, 0x000000},
  172. {0, 0x0000000, 0x0000000, 0x000000},
  173. {0, 0x0000000, 0x0000000, 0x000000},
  174. {0, 0x0000000, 0x0000000, 0x000000},
  175. {0, 0x0000000, 0x0000000, 0x000000},
  176. {0, 0x0000000, 0x0000000, 0x000000},
  177. {0, 0x0000000, 0x0000000, 0x000000},
  178. {0, 0x0000000, 0x0000000, 0x000000},
  179. {0, 0x0000000, 0x0000000, 0x000000},
  180. {0, 0x0000000, 0x0000000, 0x000000},
  181. {0, 0x0000000, 0x0000000, 0x000000},
  182. {0, 0x0000000, 0x0000000, 0x000000},
  183. {0, 0x0000000, 0x0000000, 0x000000},
  184. {0, 0x0000000, 0x0000000, 0x000000},
  185. {1, 0x0bf0000, 0x0bf2000, 0x17e000} } },
  186. {{{1, 0x0c00000, 0x0c04000, 0x1d4000} } },
  187. {{{1, 0x0d00000, 0x0d04000, 0x1a4000} } },
  188. {{{1, 0x0e00000, 0x0e04000, 0x1a0000} } },
  189. {{{1, 0x0f00000, 0x0f01000, 0x164000} } },
  190. {{{0, 0x1000000, 0x1004000, 0x1a8000} } },
  191. {{{1, 0x1100000, 0x1101000, 0x160000} } },
  192. {{{1, 0x1200000, 0x1201000, 0x161000} } },
  193. {{{1, 0x1300000, 0x1301000, 0x162000} } },
  194. {{{1, 0x1400000, 0x1401000, 0x163000} } },
  195. {{{1, 0x1500000, 0x1501000, 0x165000} } },
  196. {{{1, 0x1600000, 0x1601000, 0x166000} } },
  197. {{{0, 0, 0, 0} } },
  198. {{{0, 0, 0, 0} } },
  199. {{{0, 0, 0, 0} } },
  200. {{{0, 0, 0, 0} } },
  201. {{{0, 0, 0, 0} } },
  202. {{{0, 0, 0, 0} } },
  203. {{{1, 0x1d00000, 0x1d10000, 0x190000} } },
  204. {{{1, 0x1e00000, 0x1e01000, 0x16a000} } },
  205. {{{1, 0x1f00000, 0x1f10000, 0x150000} } },
  206. {{{0} } },
  207. {{{1, 0x2100000, 0x2102000, 0x120000},
  208. {1, 0x2110000, 0x2120000, 0x130000},
  209. {1, 0x2120000, 0x2122000, 0x124000},
  210. {1, 0x2130000, 0x2132000, 0x126000},
  211. {1, 0x2140000, 0x2142000, 0x128000},
  212. {1, 0x2150000, 0x2152000, 0x12a000},
  213. {1, 0x2160000, 0x2170000, 0x110000},
  214. {1, 0x2170000, 0x2172000, 0x12e000},
  215. {0, 0x0000000, 0x0000000, 0x000000},
  216. {0, 0x0000000, 0x0000000, 0x000000},
  217. {0, 0x0000000, 0x0000000, 0x000000},
  218. {0, 0x0000000, 0x0000000, 0x000000},
  219. {0, 0x0000000, 0x0000000, 0x000000},
  220. {0, 0x0000000, 0x0000000, 0x000000},
  221. {0, 0x0000000, 0x0000000, 0x000000},
  222. {0, 0x0000000, 0x0000000, 0x000000} } },
  223. {{{1, 0x2200000, 0x2204000, 0x1b0000} } },
  224. {{{0} } },
  225. {{{0} } },
  226. {{{0} } },
  227. {{{0} } },
  228. {{{0} } },
  229. {{{1, 0x2800000, 0x2804000, 0x1a4000} } },
  230. {{{1, 0x2900000, 0x2901000, 0x16b000} } },
  231. {{{1, 0x2a00000, 0x2a00400, 0x1ac400} } },
  232. {{{1, 0x2b00000, 0x2b00400, 0x1ac800} } },
  233. {{{1, 0x2c00000, 0x2c00400, 0x1acc00} } },
  234. {{{1, 0x2d00000, 0x2d00400, 0x1ad000} } },
  235. {{{1, 0x2e00000, 0x2e00400, 0x1ad400} } },
  236. {{{1, 0x2f00000, 0x2f00400, 0x1ad800} } },
  237. {{{1, 0x3000000, 0x3000400, 0x1adc00} } },
  238. {{{0, 0x3100000, 0x3104000, 0x1a8000} } },
  239. {{{1, 0x3200000, 0x3204000, 0x1d4000} } },
  240. {{{1, 0x3300000, 0x3304000, 0x1a0000} } },
  241. {{{0} } },
  242. {{{1, 0x3500000, 0x3500400, 0x1ac000} } },
  243. {{{1, 0x3600000, 0x3600400, 0x1ae000} } },
  244. {{{1, 0x3700000, 0x3700400, 0x1ae400} } },
  245. {{{1, 0x3800000, 0x3804000, 0x1d0000} } },
  246. {{{1, 0x3900000, 0x3904000, 0x1b4000} } },
  247. {{{1, 0x3a00000, 0x3a04000, 0x1d8000} } },
  248. {{{0} } },
  249. {{{0} } },
  250. {{{1, 0x3d00000, 0x3d04000, 0x1dc000} } },
  251. {{{1, 0x3e00000, 0x3e01000, 0x167000} } },
  252. {{{1, 0x3f00000, 0x3f01000, 0x168000} } }
  253. };
  254. /*
  255. * top 12 bits of crb internal address (hub, agent)
  256. */
  257. unsigned qla82xx_crb_hub_agt[64] = {
  258. 0,
  259. QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
  260. QLA82XX_HW_CRB_HUB_AGT_ADR_MN,
  261. QLA82XX_HW_CRB_HUB_AGT_ADR_MS,
  262. 0,
  263. QLA82XX_HW_CRB_HUB_AGT_ADR_SRE,
  264. QLA82XX_HW_CRB_HUB_AGT_ADR_NIU,
  265. QLA82XX_HW_CRB_HUB_AGT_ADR_QMN,
  266. QLA82XX_HW_CRB_HUB_AGT_ADR_SQN0,
  267. QLA82XX_HW_CRB_HUB_AGT_ADR_SQN1,
  268. QLA82XX_HW_CRB_HUB_AGT_ADR_SQN2,
  269. QLA82XX_HW_CRB_HUB_AGT_ADR_SQN3,
  270. QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
  271. QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
  272. QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
  273. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN4,
  274. QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
  275. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN0,
  276. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN1,
  277. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN2,
  278. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN3,
  279. QLA82XX_HW_CRB_HUB_AGT_ADR_PGND,
  280. QLA82XX_HW_CRB_HUB_AGT_ADR_PGNI,
  281. QLA82XX_HW_CRB_HUB_AGT_ADR_PGS0,
  282. QLA82XX_HW_CRB_HUB_AGT_ADR_PGS1,
  283. QLA82XX_HW_CRB_HUB_AGT_ADR_PGS2,
  284. QLA82XX_HW_CRB_HUB_AGT_ADR_PGS3,
  285. 0,
  286. QLA82XX_HW_CRB_HUB_AGT_ADR_PGSI,
  287. QLA82XX_HW_CRB_HUB_AGT_ADR_SN,
  288. 0,
  289. QLA82XX_HW_CRB_HUB_AGT_ADR_EG,
  290. 0,
  291. QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
  292. QLA82XX_HW_CRB_HUB_AGT_ADR_CAM,
  293. 0,
  294. 0,
  295. 0,
  296. 0,
  297. 0,
  298. QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
  299. 0,
  300. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX1,
  301. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX2,
  302. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX3,
  303. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX4,
  304. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX5,
  305. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX6,
  306. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX7,
  307. QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
  308. QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
  309. QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
  310. 0,
  311. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX0,
  312. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX8,
  313. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX9,
  314. QLA82XX_HW_CRB_HUB_AGT_ADR_OCM0,
  315. 0,
  316. QLA82XX_HW_CRB_HUB_AGT_ADR_SMB,
  317. QLA82XX_HW_CRB_HUB_AGT_ADR_I2C0,
  318. QLA82XX_HW_CRB_HUB_AGT_ADR_I2C1,
  319. 0,
  320. QLA82XX_HW_CRB_HUB_AGT_ADR_PGNC,
  321. 0,
  322. };
  323. /* Device states */
  324. char *q_dev_state[] = {
  325. "Unknown",
  326. "Cold",
  327. "Initializing",
  328. "Ready",
  329. "Need Reset",
  330. "Need Quiescent",
  331. "Failed",
  332. "Quiescent",
  333. };
  334. char *qdev_state(uint32_t dev_state)
  335. {
  336. return q_dev_state[dev_state];
  337. }
  338. /*
  339. * In: 'off' is offset from CRB space in 128M pci map
  340. * Out: 'off' is 2M pci map addr
  341. * side effect: lock crb window
  342. */
  343. static void
  344. qla82xx_pci_set_crbwindow_2M(struct qla_hw_data *ha, ulong *off)
  345. {
  346. u32 win_read;
  347. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  348. ha->crb_win = CRB_HI(*off);
  349. writel(ha->crb_win,
  350. (void *)(CRB_WINDOW_2M + ha->nx_pcibase));
  351. /* Read back value to make sure write has gone through before trying
  352. * to use it.
  353. */
  354. win_read = RD_REG_DWORD((void *)(CRB_WINDOW_2M + ha->nx_pcibase));
  355. if (win_read != ha->crb_win) {
  356. ql_dbg(ql_dbg_p3p, vha, 0xb000,
  357. "%s: Written crbwin (0x%x) "
  358. "!= Read crbwin (0x%x), off=0x%lx.\n",
  359. ha->crb_win, win_read, *off);
  360. }
  361. *off = (*off & MASK(16)) + CRB_INDIRECT_2M + ha->nx_pcibase;
  362. }
  363. static inline unsigned long
  364. qla82xx_pci_set_crbwindow(struct qla_hw_data *ha, u64 off)
  365. {
  366. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  367. /* See if we are currently pointing to the region we want to use next */
  368. if ((off >= QLA82XX_CRB_PCIX_HOST) && (off < QLA82XX_CRB_DDR_NET)) {
  369. /* No need to change window. PCIX and PCIEregs are in both
  370. * regs are in both windows.
  371. */
  372. return off;
  373. }
  374. if ((off >= QLA82XX_CRB_PCIX_HOST) && (off < QLA82XX_CRB_PCIX_HOST2)) {
  375. /* We are in first CRB window */
  376. if (ha->curr_window != 0)
  377. WARN_ON(1);
  378. return off;
  379. }
  380. if ((off > QLA82XX_CRB_PCIX_HOST2) && (off < QLA82XX_CRB_MAX)) {
  381. /* We are in second CRB window */
  382. off = off - QLA82XX_CRB_PCIX_HOST2 + QLA82XX_CRB_PCIX_HOST;
  383. if (ha->curr_window != 1)
  384. return off;
  385. /* We are in the QM or direct access
  386. * register region - do nothing
  387. */
  388. if ((off >= QLA82XX_PCI_DIRECT_CRB) &&
  389. (off < QLA82XX_PCI_CAMQM_MAX))
  390. return off;
  391. }
  392. /* strange address given */
  393. ql_dbg(ql_dbg_p3p, vha, 0xb001,
  394. "%x: Warning: unm_nic_pci_set_crbwindow "
  395. "called with an unknown address(%llx).\n",
  396. QLA2XXX_DRIVER_NAME, off);
  397. return off;
  398. }
  399. static int
  400. qla82xx_pci_get_crb_addr_2M(struct qla_hw_data *ha, ulong *off)
  401. {
  402. struct crb_128M_2M_sub_block_map *m;
  403. if (*off >= QLA82XX_CRB_MAX)
  404. return -1;
  405. if (*off >= QLA82XX_PCI_CAMQM && (*off < QLA82XX_PCI_CAMQM_2M_END)) {
  406. *off = (*off - QLA82XX_PCI_CAMQM) +
  407. QLA82XX_PCI_CAMQM_2M_BASE + ha->nx_pcibase;
  408. return 0;
  409. }
  410. if (*off < QLA82XX_PCI_CRBSPACE)
  411. return -1;
  412. *off -= QLA82XX_PCI_CRBSPACE;
  413. /* Try direct map */
  414. m = &crb_128M_2M_map[CRB_BLK(*off)].sub_block[CRB_SUBBLK(*off)];
  415. if (m->valid && (m->start_128M <= *off) && (m->end_128M > *off)) {
  416. *off = *off + m->start_2M - m->start_128M + ha->nx_pcibase;
  417. return 0;
  418. }
  419. /* Not in direct map, use crb window */
  420. return 1;
  421. }
  422. #define CRB_WIN_LOCK_TIMEOUT 100000000
  423. static int qla82xx_crb_win_lock(struct qla_hw_data *ha)
  424. {
  425. int done = 0, timeout = 0;
  426. while (!done) {
  427. /* acquire semaphore3 from PCI HW block */
  428. done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_LOCK));
  429. if (done == 1)
  430. break;
  431. if (timeout >= CRB_WIN_LOCK_TIMEOUT)
  432. return -1;
  433. timeout++;
  434. }
  435. qla82xx_wr_32(ha, QLA82XX_CRB_WIN_LOCK_ID, ha->portnum);
  436. return 0;
  437. }
  438. int
  439. qla82xx_wr_32(struct qla_hw_data *ha, ulong off, u32 data)
  440. {
  441. unsigned long flags = 0;
  442. int rv;
  443. rv = qla82xx_pci_get_crb_addr_2M(ha, &off);
  444. BUG_ON(rv == -1);
  445. if (rv == 1) {
  446. write_lock_irqsave(&ha->hw_lock, flags);
  447. qla82xx_crb_win_lock(ha);
  448. qla82xx_pci_set_crbwindow_2M(ha, &off);
  449. }
  450. writel(data, (void __iomem *)off);
  451. if (rv == 1) {
  452. qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
  453. write_unlock_irqrestore(&ha->hw_lock, flags);
  454. }
  455. return 0;
  456. }
  457. int
  458. qla82xx_rd_32(struct qla_hw_data *ha, ulong off)
  459. {
  460. unsigned long flags = 0;
  461. int rv;
  462. u32 data;
  463. rv = qla82xx_pci_get_crb_addr_2M(ha, &off);
  464. BUG_ON(rv == -1);
  465. if (rv == 1) {
  466. write_lock_irqsave(&ha->hw_lock, flags);
  467. qla82xx_crb_win_lock(ha);
  468. qla82xx_pci_set_crbwindow_2M(ha, &off);
  469. }
  470. data = RD_REG_DWORD((void __iomem *)off);
  471. if (rv == 1) {
  472. qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
  473. write_unlock_irqrestore(&ha->hw_lock, flags);
  474. }
  475. return data;
  476. }
  477. #define IDC_LOCK_TIMEOUT 100000000
  478. int qla82xx_idc_lock(struct qla_hw_data *ha)
  479. {
  480. int i;
  481. int done = 0, timeout = 0;
  482. while (!done) {
  483. /* acquire semaphore5 from PCI HW block */
  484. done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_LOCK));
  485. if (done == 1)
  486. break;
  487. if (timeout >= IDC_LOCK_TIMEOUT)
  488. return -1;
  489. timeout++;
  490. /* Yield CPU */
  491. if (!in_interrupt())
  492. schedule();
  493. else {
  494. for (i = 0; i < 20; i++)
  495. cpu_relax();
  496. }
  497. }
  498. return 0;
  499. }
  500. void qla82xx_idc_unlock(struct qla_hw_data *ha)
  501. {
  502. qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_UNLOCK));
  503. }
  504. /* PCI Windowing for DDR regions. */
  505. #define QLA82XX_ADDR_IN_RANGE(addr, low, high) \
  506. (((addr) <= (high)) && ((addr) >= (low)))
  507. /*
  508. * check memory access boundary.
  509. * used by test agent. support ddr access only for now
  510. */
  511. static unsigned long
  512. qla82xx_pci_mem_bound_check(struct qla_hw_data *ha,
  513. unsigned long long addr, int size)
  514. {
  515. if (!QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_DDR_NET,
  516. QLA82XX_ADDR_DDR_NET_MAX) ||
  517. !QLA82XX_ADDR_IN_RANGE(addr + size - 1, QLA82XX_ADDR_DDR_NET,
  518. QLA82XX_ADDR_DDR_NET_MAX) ||
  519. ((size != 1) && (size != 2) && (size != 4) && (size != 8)))
  520. return 0;
  521. else
  522. return 1;
  523. }
  524. int qla82xx_pci_set_window_warning_count;
  525. static unsigned long
  526. qla82xx_pci_set_window(struct qla_hw_data *ha, unsigned long long addr)
  527. {
  528. int window;
  529. u32 win_read;
  530. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  531. if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_DDR_NET,
  532. QLA82XX_ADDR_DDR_NET_MAX)) {
  533. /* DDR network side */
  534. window = MN_WIN(addr);
  535. ha->ddr_mn_window = window;
  536. qla82xx_wr_32(ha,
  537. ha->mn_win_crb | QLA82XX_PCI_CRBSPACE, window);
  538. win_read = qla82xx_rd_32(ha,
  539. ha->mn_win_crb | QLA82XX_PCI_CRBSPACE);
  540. if ((win_read << 17) != window) {
  541. ql_dbg(ql_dbg_p3p, vha, 0xb003,
  542. "%s: Written MNwin (0x%x) != Read MNwin (0x%x).\n",
  543. __func__, window, win_read);
  544. }
  545. addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_DDR_NET;
  546. } else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_OCM0,
  547. QLA82XX_ADDR_OCM0_MAX)) {
  548. unsigned int temp1;
  549. if ((addr & 0x00ff800) == 0xff800) {
  550. ql_log(ql_log_warn, vha, 0xb004,
  551. "%s: QM access not handled.\n", __func__);
  552. addr = -1UL;
  553. }
  554. window = OCM_WIN(addr);
  555. ha->ddr_mn_window = window;
  556. qla82xx_wr_32(ha,
  557. ha->mn_win_crb | QLA82XX_PCI_CRBSPACE, window);
  558. win_read = qla82xx_rd_32(ha,
  559. ha->mn_win_crb | QLA82XX_PCI_CRBSPACE);
  560. temp1 = ((window & 0x1FF) << 7) |
  561. ((window & 0x0FFFE0000) >> 17);
  562. if (win_read != temp1) {
  563. ql_log(ql_log_warn, vha, 0xb005,
  564. "%s: Written OCMwin (0x%x) != Read OCMwin (0x%x).\n",
  565. __func__, temp1, win_read);
  566. }
  567. addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_OCM0_2M;
  568. } else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_QDR_NET,
  569. QLA82XX_P3_ADDR_QDR_NET_MAX)) {
  570. /* QDR network side */
  571. window = MS_WIN(addr);
  572. ha->qdr_sn_window = window;
  573. qla82xx_wr_32(ha,
  574. ha->ms_win_crb | QLA82XX_PCI_CRBSPACE, window);
  575. win_read = qla82xx_rd_32(ha,
  576. ha->ms_win_crb | QLA82XX_PCI_CRBSPACE);
  577. if (win_read != window) {
  578. ql_log(ql_log_warn, vha, 0xb006,
  579. "%s: Written MSwin (0x%x) != Read MSwin (0x%x).\n",
  580. __func__, window, win_read);
  581. }
  582. addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_QDR_NET;
  583. } else {
  584. /*
  585. * peg gdb frequently accesses memory that doesn't exist,
  586. * this limits the chit chat so debugging isn't slowed down.
  587. */
  588. if ((qla82xx_pci_set_window_warning_count++ < 8) ||
  589. (qla82xx_pci_set_window_warning_count%64 == 0)) {
  590. ql_log(ql_log_warn, vha, 0xb007,
  591. "%s: Warning:%s Unknown address range!.\n",
  592. __func__, QLA2XXX_DRIVER_NAME);
  593. }
  594. addr = -1UL;
  595. }
  596. return addr;
  597. }
  598. /* check if address is in the same windows as the previous access */
  599. static int qla82xx_pci_is_same_window(struct qla_hw_data *ha,
  600. unsigned long long addr)
  601. {
  602. int window;
  603. unsigned long long qdr_max;
  604. qdr_max = QLA82XX_P3_ADDR_QDR_NET_MAX;
  605. /* DDR network side */
  606. if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_DDR_NET,
  607. QLA82XX_ADDR_DDR_NET_MAX))
  608. BUG();
  609. else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_OCM0,
  610. QLA82XX_ADDR_OCM0_MAX))
  611. return 1;
  612. else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_OCM1,
  613. QLA82XX_ADDR_OCM1_MAX))
  614. return 1;
  615. else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_QDR_NET, qdr_max)) {
  616. /* QDR network side */
  617. window = ((addr - QLA82XX_ADDR_QDR_NET) >> 22) & 0x3f;
  618. if (ha->qdr_sn_window == window)
  619. return 1;
  620. }
  621. return 0;
  622. }
  623. static int qla82xx_pci_mem_read_direct(struct qla_hw_data *ha,
  624. u64 off, void *data, int size)
  625. {
  626. unsigned long flags;
  627. void *addr = NULL;
  628. int ret = 0;
  629. u64 start;
  630. uint8_t *mem_ptr = NULL;
  631. unsigned long mem_base;
  632. unsigned long mem_page;
  633. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  634. write_lock_irqsave(&ha->hw_lock, flags);
  635. /*
  636. * If attempting to access unknown address or straddle hw windows,
  637. * do not access.
  638. */
  639. start = qla82xx_pci_set_window(ha, off);
  640. if ((start == -1UL) ||
  641. (qla82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
  642. write_unlock_irqrestore(&ha->hw_lock, flags);
  643. ql_log(ql_log_fatal, vha, 0xb008,
  644. "%s out of bound pci memory "
  645. "access, offset is 0x%llx.\n",
  646. QLA2XXX_DRIVER_NAME, off);
  647. return -1;
  648. }
  649. write_unlock_irqrestore(&ha->hw_lock, flags);
  650. mem_base = pci_resource_start(ha->pdev, 0);
  651. mem_page = start & PAGE_MASK;
  652. /* Map two pages whenever user tries to access addresses in two
  653. * consecutive pages.
  654. */
  655. if (mem_page != ((start + size - 1) & PAGE_MASK))
  656. mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE * 2);
  657. else
  658. mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
  659. if (mem_ptr == 0UL) {
  660. *(u8 *)data = 0;
  661. return -1;
  662. }
  663. addr = mem_ptr;
  664. addr += start & (PAGE_SIZE - 1);
  665. write_lock_irqsave(&ha->hw_lock, flags);
  666. switch (size) {
  667. case 1:
  668. *(u8 *)data = readb(addr);
  669. break;
  670. case 2:
  671. *(u16 *)data = readw(addr);
  672. break;
  673. case 4:
  674. *(u32 *)data = readl(addr);
  675. break;
  676. case 8:
  677. *(u64 *)data = readq(addr);
  678. break;
  679. default:
  680. ret = -1;
  681. break;
  682. }
  683. write_unlock_irqrestore(&ha->hw_lock, flags);
  684. if (mem_ptr)
  685. iounmap(mem_ptr);
  686. return ret;
  687. }
  688. static int
  689. qla82xx_pci_mem_write_direct(struct qla_hw_data *ha,
  690. u64 off, void *data, int size)
  691. {
  692. unsigned long flags;
  693. void *addr = NULL;
  694. int ret = 0;
  695. u64 start;
  696. uint8_t *mem_ptr = NULL;
  697. unsigned long mem_base;
  698. unsigned long mem_page;
  699. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  700. write_lock_irqsave(&ha->hw_lock, flags);
  701. /*
  702. * If attempting to access unknown address or straddle hw windows,
  703. * do not access.
  704. */
  705. start = qla82xx_pci_set_window(ha, off);
  706. if ((start == -1UL) ||
  707. (qla82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
  708. write_unlock_irqrestore(&ha->hw_lock, flags);
  709. ql_log(ql_log_fatal, vha, 0xb009,
  710. "%s out of bount memory "
  711. "access, offset is 0x%llx.\n",
  712. QLA2XXX_DRIVER_NAME, off);
  713. return -1;
  714. }
  715. write_unlock_irqrestore(&ha->hw_lock, flags);
  716. mem_base = pci_resource_start(ha->pdev, 0);
  717. mem_page = start & PAGE_MASK;
  718. /* Map two pages whenever user tries to access addresses in two
  719. * consecutive pages.
  720. */
  721. if (mem_page != ((start + size - 1) & PAGE_MASK))
  722. mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE*2);
  723. else
  724. mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
  725. if (mem_ptr == 0UL)
  726. return -1;
  727. addr = mem_ptr;
  728. addr += start & (PAGE_SIZE - 1);
  729. write_lock_irqsave(&ha->hw_lock, flags);
  730. switch (size) {
  731. case 1:
  732. writeb(*(u8 *)data, addr);
  733. break;
  734. case 2:
  735. writew(*(u16 *)data, addr);
  736. break;
  737. case 4:
  738. writel(*(u32 *)data, addr);
  739. break;
  740. case 8:
  741. writeq(*(u64 *)data, addr);
  742. break;
  743. default:
  744. ret = -1;
  745. break;
  746. }
  747. write_unlock_irqrestore(&ha->hw_lock, flags);
  748. if (mem_ptr)
  749. iounmap(mem_ptr);
  750. return ret;
  751. }
  752. #define MTU_FUDGE_FACTOR 100
  753. static unsigned long
  754. qla82xx_decode_crb_addr(unsigned long addr)
  755. {
  756. int i;
  757. unsigned long base_addr, offset, pci_base;
  758. if (!qla82xx_crb_table_initialized)
  759. qla82xx_crb_addr_transform_setup();
  760. pci_base = ADDR_ERROR;
  761. base_addr = addr & 0xfff00000;
  762. offset = addr & 0x000fffff;
  763. for (i = 0; i < MAX_CRB_XFORM; i++) {
  764. if (crb_addr_xform[i] == base_addr) {
  765. pci_base = i << 20;
  766. break;
  767. }
  768. }
  769. if (pci_base == ADDR_ERROR)
  770. return pci_base;
  771. return pci_base + offset;
  772. }
  773. static long rom_max_timeout = 100;
  774. static long qla82xx_rom_lock_timeout = 100;
  775. static int
  776. qla82xx_rom_lock(struct qla_hw_data *ha)
  777. {
  778. int done = 0, timeout = 0;
  779. while (!done) {
  780. /* acquire semaphore2 from PCI HW block */
  781. done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_LOCK));
  782. if (done == 1)
  783. break;
  784. if (timeout >= qla82xx_rom_lock_timeout)
  785. return -1;
  786. timeout++;
  787. }
  788. qla82xx_wr_32(ha, QLA82XX_ROM_LOCK_ID, ROM_LOCK_DRIVER);
  789. return 0;
  790. }
  791. static void
  792. qla82xx_rom_unlock(struct qla_hw_data *ha)
  793. {
  794. qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_UNLOCK));
  795. }
  796. static int
  797. qla82xx_wait_rom_busy(struct qla_hw_data *ha)
  798. {
  799. long timeout = 0;
  800. long done = 0 ;
  801. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  802. while (done == 0) {
  803. done = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
  804. done &= 4;
  805. timeout++;
  806. if (timeout >= rom_max_timeout) {
  807. ql_dbg(ql_dbg_p3p, vha, 0xb00a,
  808. "%s: Timeout reached waiting for rom busy.\n",
  809. QLA2XXX_DRIVER_NAME);
  810. return -1;
  811. }
  812. }
  813. return 0;
  814. }
  815. static int
  816. qla82xx_wait_rom_done(struct qla_hw_data *ha)
  817. {
  818. long timeout = 0;
  819. long done = 0 ;
  820. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  821. while (done == 0) {
  822. done = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
  823. done &= 2;
  824. timeout++;
  825. if (timeout >= rom_max_timeout) {
  826. ql_dbg(ql_dbg_p3p, vha, 0xb00b,
  827. "%s: Timeout reached waiting for rom done.\n",
  828. QLA2XXX_DRIVER_NAME);
  829. return -1;
  830. }
  831. }
  832. return 0;
  833. }
  834. static int
  835. qla82xx_do_rom_fast_read(struct qla_hw_data *ha, int addr, int *valp)
  836. {
  837. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  838. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, addr);
  839. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
  840. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
  841. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, 0xb);
  842. qla82xx_wait_rom_busy(ha);
  843. if (qla82xx_wait_rom_done(ha)) {
  844. ql_log(ql_log_fatal, vha, 0x00ba,
  845. "Error waiting for rom done.\n");
  846. return -1;
  847. }
  848. /* Reset abyte_cnt and dummy_byte_cnt */
  849. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
  850. udelay(10);
  851. cond_resched();
  852. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
  853. *valp = qla82xx_rd_32(ha, QLA82XX_ROMUSB_ROM_RDATA);
  854. return 0;
  855. }
  856. static int
  857. qla82xx_rom_fast_read(struct qla_hw_data *ha, int addr, int *valp)
  858. {
  859. int ret, loops = 0;
  860. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  861. while ((qla82xx_rom_lock(ha) != 0) && (loops < 50000)) {
  862. udelay(100);
  863. schedule();
  864. loops++;
  865. }
  866. if (loops >= 50000) {
  867. ql_log(ql_log_fatal, vha, 0x00b9,
  868. "Failed to aquire SEM2 lock.\n");
  869. return -1;
  870. }
  871. ret = qla82xx_do_rom_fast_read(ha, addr, valp);
  872. qla82xx_rom_unlock(ha);
  873. return ret;
  874. }
  875. static int
  876. qla82xx_read_status_reg(struct qla_hw_data *ha, uint32_t *val)
  877. {
  878. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  879. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_RDSR);
  880. qla82xx_wait_rom_busy(ha);
  881. if (qla82xx_wait_rom_done(ha)) {
  882. ql_log(ql_log_warn, vha, 0xb00c,
  883. "Error waiting for rom done.\n");
  884. return -1;
  885. }
  886. *val = qla82xx_rd_32(ha, QLA82XX_ROMUSB_ROM_RDATA);
  887. return 0;
  888. }
  889. static int
  890. qla82xx_flash_wait_write_finish(struct qla_hw_data *ha)
  891. {
  892. long timeout = 0;
  893. uint32_t done = 1 ;
  894. uint32_t val;
  895. int ret = 0;
  896. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  897. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
  898. while ((done != 0) && (ret == 0)) {
  899. ret = qla82xx_read_status_reg(ha, &val);
  900. done = val & 1;
  901. timeout++;
  902. udelay(10);
  903. cond_resched();
  904. if (timeout >= 50000) {
  905. ql_log(ql_log_warn, vha, 0xb00d,
  906. "Timeout reached waiting for write finish.\n");
  907. return -1;
  908. }
  909. }
  910. return ret;
  911. }
  912. static int
  913. qla82xx_flash_set_write_enable(struct qla_hw_data *ha)
  914. {
  915. uint32_t val;
  916. qla82xx_wait_rom_busy(ha);
  917. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
  918. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_WREN);
  919. qla82xx_wait_rom_busy(ha);
  920. if (qla82xx_wait_rom_done(ha))
  921. return -1;
  922. if (qla82xx_read_status_reg(ha, &val) != 0)
  923. return -1;
  924. if ((val & 2) != 2)
  925. return -1;
  926. return 0;
  927. }
  928. static int
  929. qla82xx_write_status_reg(struct qla_hw_data *ha, uint32_t val)
  930. {
  931. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  932. if (qla82xx_flash_set_write_enable(ha))
  933. return -1;
  934. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_WDATA, val);
  935. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, 0x1);
  936. if (qla82xx_wait_rom_done(ha)) {
  937. ql_log(ql_log_warn, vha, 0xb00e,
  938. "Error waiting for rom done.\n");
  939. return -1;
  940. }
  941. return qla82xx_flash_wait_write_finish(ha);
  942. }
  943. static int
  944. qla82xx_write_disable_flash(struct qla_hw_data *ha)
  945. {
  946. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  947. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_WRDI);
  948. if (qla82xx_wait_rom_done(ha)) {
  949. ql_log(ql_log_warn, vha, 0xb00f,
  950. "Error waiting for rom done.\n");
  951. return -1;
  952. }
  953. return 0;
  954. }
  955. static int
  956. ql82xx_rom_lock_d(struct qla_hw_data *ha)
  957. {
  958. int loops = 0;
  959. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  960. while ((qla82xx_rom_lock(ha) != 0) && (loops < 50000)) {
  961. udelay(100);
  962. cond_resched();
  963. loops++;
  964. }
  965. if (loops >= 50000) {
  966. ql_log(ql_log_warn, vha, 0xb010,
  967. "ROM lock failed.\n");
  968. return -1;
  969. }
  970. return 0;;
  971. }
  972. static int
  973. qla82xx_write_flash_dword(struct qla_hw_data *ha, uint32_t flashaddr,
  974. uint32_t data)
  975. {
  976. int ret = 0;
  977. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  978. ret = ql82xx_rom_lock_d(ha);
  979. if (ret < 0) {
  980. ql_log(ql_log_warn, vha, 0xb011,
  981. "ROM lock failed.\n");
  982. return ret;
  983. }
  984. if (qla82xx_flash_set_write_enable(ha))
  985. goto done_write;
  986. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_WDATA, data);
  987. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, flashaddr);
  988. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
  989. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_PP);
  990. qla82xx_wait_rom_busy(ha);
  991. if (qla82xx_wait_rom_done(ha)) {
  992. ql_log(ql_log_warn, vha, 0xb012,
  993. "Error waiting for rom done.\n");
  994. ret = -1;
  995. goto done_write;
  996. }
  997. ret = qla82xx_flash_wait_write_finish(ha);
  998. done_write:
  999. qla82xx_rom_unlock(ha);
  1000. return ret;
  1001. }
  1002. /* This routine does CRB initialize sequence
  1003. * to put the ISP into operational state
  1004. */
  1005. static int
  1006. qla82xx_pinit_from_rom(scsi_qla_host_t *vha)
  1007. {
  1008. int addr, val;
  1009. int i ;
  1010. struct crb_addr_pair *buf;
  1011. unsigned long off;
  1012. unsigned offset, n;
  1013. struct qla_hw_data *ha = vha->hw;
  1014. struct crb_addr_pair {
  1015. long addr;
  1016. long data;
  1017. };
  1018. /* Halt all the indiviual PEGs and other blocks of the ISP */
  1019. qla82xx_rom_lock(ha);
  1020. /* disable all I2Q */
  1021. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x10, 0x0);
  1022. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x14, 0x0);
  1023. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x18, 0x0);
  1024. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x1c, 0x0);
  1025. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x20, 0x0);
  1026. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x24, 0x0);
  1027. /* disable all niu interrupts */
  1028. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x40, 0xff);
  1029. /* disable xge rx/tx */
  1030. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x70000, 0x00);
  1031. /* disable xg1 rx/tx */
  1032. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x80000, 0x00);
  1033. /* disable sideband mac */
  1034. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x90000, 0x00);
  1035. /* disable ap0 mac */
  1036. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xa0000, 0x00);
  1037. /* disable ap1 mac */
  1038. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xb0000, 0x00);
  1039. /* halt sre */
  1040. val = qla82xx_rd_32(ha, QLA82XX_CRB_SRE + 0x1000);
  1041. qla82xx_wr_32(ha, QLA82XX_CRB_SRE + 0x1000, val & (~(0x1)));
  1042. /* halt epg */
  1043. qla82xx_wr_32(ha, QLA82XX_CRB_EPG + 0x1300, 0x1);
  1044. /* halt timers */
  1045. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x0, 0x0);
  1046. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x8, 0x0);
  1047. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x10, 0x0);
  1048. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x18, 0x0);
  1049. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x100, 0x0);
  1050. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x200, 0x0);
  1051. /* halt pegs */
  1052. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x3c, 1);
  1053. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1 + 0x3c, 1);
  1054. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2 + 0x3c, 1);
  1055. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3 + 0x3c, 1);
  1056. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_4 + 0x3c, 1);
  1057. msleep(20);
  1058. /* big hammer */
  1059. if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
  1060. /* don't reset CAM block on reset */
  1061. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xfeffffff);
  1062. else
  1063. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xffffffff);
  1064. /* reset ms */
  1065. val = qla82xx_rd_32(ha, QLA82XX_CRB_QDR_NET + 0xe4);
  1066. val |= (1 << 1);
  1067. qla82xx_wr_32(ha, QLA82XX_CRB_QDR_NET + 0xe4, val);
  1068. msleep(20);
  1069. /* unreset ms */
  1070. val = qla82xx_rd_32(ha, QLA82XX_CRB_QDR_NET + 0xe4);
  1071. val &= ~(1 << 1);
  1072. qla82xx_wr_32(ha, QLA82XX_CRB_QDR_NET + 0xe4, val);
  1073. msleep(20);
  1074. qla82xx_rom_unlock(ha);
  1075. /* Read the signature value from the flash.
  1076. * Offset 0: Contain signature (0xcafecafe)
  1077. * Offset 4: Offset and number of addr/value pairs
  1078. * that present in CRB initialize sequence
  1079. */
  1080. if (qla82xx_rom_fast_read(ha, 0, &n) != 0 || n != 0xcafecafeUL ||
  1081. qla82xx_rom_fast_read(ha, 4, &n) != 0) {
  1082. ql_log(ql_log_fatal, vha, 0x006e,
  1083. "Error Reading crb_init area: n: %08x.\n", n);
  1084. return -1;
  1085. }
  1086. /* Offset in flash = lower 16 bits
  1087. * Number of enteries = upper 16 bits
  1088. */
  1089. offset = n & 0xffffU;
  1090. n = (n >> 16) & 0xffffU;
  1091. /* number of addr/value pair should not exceed 1024 enteries */
  1092. if (n >= 1024) {
  1093. ql_log(ql_log_fatal, vha, 0x0071,
  1094. "Card flash not initialized:n=0x%x.\n", n);
  1095. return -1;
  1096. }
  1097. ql_log(ql_log_info, vha, 0x0072,
  1098. "%d CRB init values found in ROM.\n", n);
  1099. buf = kmalloc(n * sizeof(struct crb_addr_pair), GFP_KERNEL);
  1100. if (buf == NULL) {
  1101. ql_log(ql_log_fatal, vha, 0x010c,
  1102. "Unable to allocate memory.\n");
  1103. return -1;
  1104. }
  1105. for (i = 0; i < n; i++) {
  1106. if (qla82xx_rom_fast_read(ha, 8*i + 4*offset, &val) != 0 ||
  1107. qla82xx_rom_fast_read(ha, 8*i + 4*offset + 4, &addr) != 0) {
  1108. kfree(buf);
  1109. return -1;
  1110. }
  1111. buf[i].addr = addr;
  1112. buf[i].data = val;
  1113. }
  1114. for (i = 0; i < n; i++) {
  1115. /* Translate internal CRB initialization
  1116. * address to PCI bus address
  1117. */
  1118. off = qla82xx_decode_crb_addr((unsigned long)buf[i].addr) +
  1119. QLA82XX_PCI_CRBSPACE;
  1120. /* Not all CRB addr/value pair to be written,
  1121. * some of them are skipped
  1122. */
  1123. /* skipping cold reboot MAGIC */
  1124. if (off == QLA82XX_CAM_RAM(0x1fc))
  1125. continue;
  1126. /* do not reset PCI */
  1127. if (off == (ROMUSB_GLB + 0xbc))
  1128. continue;
  1129. /* skip core clock, so that firmware can increase the clock */
  1130. if (off == (ROMUSB_GLB + 0xc8))
  1131. continue;
  1132. /* skip the function enable register */
  1133. if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION))
  1134. continue;
  1135. if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION2))
  1136. continue;
  1137. if ((off & 0x0ff00000) == QLA82XX_CRB_SMB)
  1138. continue;
  1139. if ((off & 0x0ff00000) == QLA82XX_CRB_DDR_NET)
  1140. continue;
  1141. if (off == ADDR_ERROR) {
  1142. ql_log(ql_log_fatal, vha, 0x0116,
  1143. "Unknow addr: 0x%08lx.\n", buf[i].addr);
  1144. continue;
  1145. }
  1146. qla82xx_wr_32(ha, off, buf[i].data);
  1147. /* ISP requires much bigger delay to settle down,
  1148. * else crb_window returns 0xffffffff
  1149. */
  1150. if (off == QLA82XX_ROMUSB_GLB_SW_RESET)
  1151. msleep(1000);
  1152. /* ISP requires millisec delay between
  1153. * successive CRB register updation
  1154. */
  1155. msleep(1);
  1156. }
  1157. kfree(buf);
  1158. /* Resetting the data and instruction cache */
  1159. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0xec, 0x1e);
  1160. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0x4c, 8);
  1161. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_I+0x4c, 8);
  1162. /* Clear all protocol processing engines */
  1163. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0x8, 0);
  1164. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0xc, 0);
  1165. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0x8, 0);
  1166. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0xc, 0);
  1167. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0x8, 0);
  1168. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0xc, 0);
  1169. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0x8, 0);
  1170. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0xc, 0);
  1171. return 0;
  1172. }
  1173. static int
  1174. qla82xx_pci_mem_write_2M(struct qla_hw_data *ha,
  1175. u64 off, void *data, int size)
  1176. {
  1177. int i, j, ret = 0, loop, sz[2], off0;
  1178. int scale, shift_amount, startword;
  1179. uint32_t temp;
  1180. uint64_t off8, mem_crb, tmpw, word[2] = {0, 0};
  1181. /*
  1182. * If not MN, go check for MS or invalid.
  1183. */
  1184. if (off >= QLA82XX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
  1185. mem_crb = QLA82XX_CRB_QDR_NET;
  1186. else {
  1187. mem_crb = QLA82XX_CRB_DDR_NET;
  1188. if (qla82xx_pci_mem_bound_check(ha, off, size) == 0)
  1189. return qla82xx_pci_mem_write_direct(ha,
  1190. off, data, size);
  1191. }
  1192. off0 = off & 0x7;
  1193. sz[0] = (size < (8 - off0)) ? size : (8 - off0);
  1194. sz[1] = size - sz[0];
  1195. off8 = off & 0xfffffff0;
  1196. loop = (((off & 0xf) + size - 1) >> 4) + 1;
  1197. shift_amount = 4;
  1198. scale = 2;
  1199. startword = (off & 0xf)/8;
  1200. for (i = 0; i < loop; i++) {
  1201. if (qla82xx_pci_mem_read_2M(ha, off8 +
  1202. (i << shift_amount), &word[i * scale], 8))
  1203. return -1;
  1204. }
  1205. switch (size) {
  1206. case 1:
  1207. tmpw = *((uint8_t *)data);
  1208. break;
  1209. case 2:
  1210. tmpw = *((uint16_t *)data);
  1211. break;
  1212. case 4:
  1213. tmpw = *((uint32_t *)data);
  1214. break;
  1215. case 8:
  1216. default:
  1217. tmpw = *((uint64_t *)data);
  1218. break;
  1219. }
  1220. if (sz[0] == 8) {
  1221. word[startword] = tmpw;
  1222. } else {
  1223. word[startword] &=
  1224. ~((~(~0ULL << (sz[0] * 8))) << (off0 * 8));
  1225. word[startword] |= tmpw << (off0 * 8);
  1226. }
  1227. if (sz[1] != 0) {
  1228. word[startword+1] &= ~(~0ULL << (sz[1] * 8));
  1229. word[startword+1] |= tmpw >> (sz[0] * 8);
  1230. }
  1231. for (i = 0; i < loop; i++) {
  1232. temp = off8 + (i << shift_amount);
  1233. qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_LO, temp);
  1234. temp = 0;
  1235. qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_HI, temp);
  1236. temp = word[i * scale] & 0xffffffff;
  1237. qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_LO, temp);
  1238. temp = (word[i * scale] >> 32) & 0xffffffff;
  1239. qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_HI, temp);
  1240. temp = word[i*scale + 1] & 0xffffffff;
  1241. qla82xx_wr_32(ha, mem_crb +
  1242. MIU_TEST_AGT_WRDATA_UPPER_LO, temp);
  1243. temp = (word[i*scale + 1] >> 32) & 0xffffffff;
  1244. qla82xx_wr_32(ha, mem_crb +
  1245. MIU_TEST_AGT_WRDATA_UPPER_HI, temp);
  1246. temp = MIU_TA_CTL_ENABLE | MIU_TA_CTL_WRITE;
  1247. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
  1248. temp = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE | MIU_TA_CTL_WRITE;
  1249. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
  1250. for (j = 0; j < MAX_CTL_CHECK; j++) {
  1251. temp = qla82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
  1252. if ((temp & MIU_TA_CTL_BUSY) == 0)
  1253. break;
  1254. }
  1255. if (j >= MAX_CTL_CHECK) {
  1256. if (printk_ratelimit())
  1257. dev_err(&ha->pdev->dev,
  1258. "failed to write through agent.\n");
  1259. ret = -1;
  1260. break;
  1261. }
  1262. }
  1263. return ret;
  1264. }
  1265. static int
  1266. qla82xx_fw_load_from_flash(struct qla_hw_data *ha)
  1267. {
  1268. int i;
  1269. long size = 0;
  1270. long flashaddr = ha->flt_region_bootload << 2;
  1271. long memaddr = BOOTLD_START;
  1272. u64 data;
  1273. u32 high, low;
  1274. size = (IMAGE_START - BOOTLD_START) / 8;
  1275. for (i = 0; i < size; i++) {
  1276. if ((qla82xx_rom_fast_read(ha, flashaddr, (int *)&low)) ||
  1277. (qla82xx_rom_fast_read(ha, flashaddr + 4, (int *)&high))) {
  1278. return -1;
  1279. }
  1280. data = ((u64)high << 32) | low ;
  1281. qla82xx_pci_mem_write_2M(ha, memaddr, &data, 8);
  1282. flashaddr += 8;
  1283. memaddr += 8;
  1284. if (i % 0x1000 == 0)
  1285. msleep(1);
  1286. }
  1287. udelay(100);
  1288. read_lock(&ha->hw_lock);
  1289. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
  1290. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
  1291. read_unlock(&ha->hw_lock);
  1292. return 0;
  1293. }
  1294. int
  1295. qla82xx_pci_mem_read_2M(struct qla_hw_data *ha,
  1296. u64 off, void *data, int size)
  1297. {
  1298. int i, j = 0, k, start, end, loop, sz[2], off0[2];
  1299. int shift_amount;
  1300. uint32_t temp;
  1301. uint64_t off8, val, mem_crb, word[2] = {0, 0};
  1302. /*
  1303. * If not MN, go check for MS or invalid.
  1304. */
  1305. if (off >= QLA82XX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
  1306. mem_crb = QLA82XX_CRB_QDR_NET;
  1307. else {
  1308. mem_crb = QLA82XX_CRB_DDR_NET;
  1309. if (qla82xx_pci_mem_bound_check(ha, off, size) == 0)
  1310. return qla82xx_pci_mem_read_direct(ha,
  1311. off, data, size);
  1312. }
  1313. off8 = off & 0xfffffff0;
  1314. off0[0] = off & 0xf;
  1315. sz[0] = (size < (16 - off0[0])) ? size : (16 - off0[0]);
  1316. shift_amount = 4;
  1317. loop = ((off0[0] + size - 1) >> shift_amount) + 1;
  1318. off0[1] = 0;
  1319. sz[1] = size - sz[0];
  1320. for (i = 0; i < loop; i++) {
  1321. temp = off8 + (i << shift_amount);
  1322. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_LO, temp);
  1323. temp = 0;
  1324. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_HI, temp);
  1325. temp = MIU_TA_CTL_ENABLE;
  1326. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
  1327. temp = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE;
  1328. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
  1329. for (j = 0; j < MAX_CTL_CHECK; j++) {
  1330. temp = qla82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
  1331. if ((temp & MIU_TA_CTL_BUSY) == 0)
  1332. break;
  1333. }
  1334. if (j >= MAX_CTL_CHECK) {
  1335. if (printk_ratelimit())
  1336. dev_err(&ha->pdev->dev,
  1337. "failed to read through agent.\n");
  1338. break;
  1339. }
  1340. start = off0[i] >> 2;
  1341. end = (off0[i] + sz[i] - 1) >> 2;
  1342. for (k = start; k <= end; k++) {
  1343. temp = qla82xx_rd_32(ha,
  1344. mem_crb + MIU_TEST_AGT_RDDATA(k));
  1345. word[i] |= ((uint64_t)temp << (32 * (k & 1)));
  1346. }
  1347. }
  1348. if (j >= MAX_CTL_CHECK)
  1349. return -1;
  1350. if ((off0[0] & 7) == 0) {
  1351. val = word[0];
  1352. } else {
  1353. val = ((word[0] >> (off0[0] * 8)) & (~(~0ULL << (sz[0] * 8)))) |
  1354. ((word[1] & (~(~0ULL << (sz[1] * 8)))) << (sz[0] * 8));
  1355. }
  1356. switch (size) {
  1357. case 1:
  1358. *(uint8_t *)data = val;
  1359. break;
  1360. case 2:
  1361. *(uint16_t *)data = val;
  1362. break;
  1363. case 4:
  1364. *(uint32_t *)data = val;
  1365. break;
  1366. case 8:
  1367. *(uint64_t *)data = val;
  1368. break;
  1369. }
  1370. return 0;
  1371. }
  1372. static struct qla82xx_uri_table_desc *
  1373. qla82xx_get_table_desc(const u8 *unirom, int section)
  1374. {
  1375. uint32_t i;
  1376. struct qla82xx_uri_table_desc *directory =
  1377. (struct qla82xx_uri_table_desc *)&unirom[0];
  1378. __le32 offset;
  1379. __le32 tab_type;
  1380. __le32 entries = cpu_to_le32(directory->num_entries);
  1381. for (i = 0; i < entries; i++) {
  1382. offset = cpu_to_le32(directory->findex) +
  1383. (i * cpu_to_le32(directory->entry_size));
  1384. tab_type = cpu_to_le32(*((u32 *)&unirom[offset] + 8));
  1385. if (tab_type == section)
  1386. return (struct qla82xx_uri_table_desc *)&unirom[offset];
  1387. }
  1388. return NULL;
  1389. }
  1390. static struct qla82xx_uri_data_desc *
  1391. qla82xx_get_data_desc(struct qla_hw_data *ha,
  1392. u32 section, u32 idx_offset)
  1393. {
  1394. const u8 *unirom = ha->hablob->fw->data;
  1395. int idx = cpu_to_le32(*((int *)&unirom[ha->file_prd_off] + idx_offset));
  1396. struct qla82xx_uri_table_desc *tab_desc = NULL;
  1397. __le32 offset;
  1398. tab_desc = qla82xx_get_table_desc(unirom, section);
  1399. if (!tab_desc)
  1400. return NULL;
  1401. offset = cpu_to_le32(tab_desc->findex) +
  1402. (cpu_to_le32(tab_desc->entry_size) * idx);
  1403. return (struct qla82xx_uri_data_desc *)&unirom[offset];
  1404. }
  1405. static u8 *
  1406. qla82xx_get_bootld_offset(struct qla_hw_data *ha)
  1407. {
  1408. u32 offset = BOOTLD_START;
  1409. struct qla82xx_uri_data_desc *uri_desc = NULL;
  1410. if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
  1411. uri_desc = qla82xx_get_data_desc(ha,
  1412. QLA82XX_URI_DIR_SECT_BOOTLD, QLA82XX_URI_BOOTLD_IDX_OFF);
  1413. if (uri_desc)
  1414. offset = cpu_to_le32(uri_desc->findex);
  1415. }
  1416. return (u8 *)&ha->hablob->fw->data[offset];
  1417. }
  1418. static __le32
  1419. qla82xx_get_fw_size(struct qla_hw_data *ha)
  1420. {
  1421. struct qla82xx_uri_data_desc *uri_desc = NULL;
  1422. if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
  1423. uri_desc = qla82xx_get_data_desc(ha, QLA82XX_URI_DIR_SECT_FW,
  1424. QLA82XX_URI_FIRMWARE_IDX_OFF);
  1425. if (uri_desc)
  1426. return cpu_to_le32(uri_desc->size);
  1427. }
  1428. return cpu_to_le32(*(u32 *)&ha->hablob->fw->data[FW_SIZE_OFFSET]);
  1429. }
  1430. static u8 *
  1431. qla82xx_get_fw_offs(struct qla_hw_data *ha)
  1432. {
  1433. u32 offset = IMAGE_START;
  1434. struct qla82xx_uri_data_desc *uri_desc = NULL;
  1435. if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
  1436. uri_desc = qla82xx_get_data_desc(ha, QLA82XX_URI_DIR_SECT_FW,
  1437. QLA82XX_URI_FIRMWARE_IDX_OFF);
  1438. if (uri_desc)
  1439. offset = cpu_to_le32(uri_desc->findex);
  1440. }
  1441. return (u8 *)&ha->hablob->fw->data[offset];
  1442. }
  1443. /* PCI related functions */
  1444. char *
  1445. qla82xx_pci_info_str(struct scsi_qla_host *vha, char *str)
  1446. {
  1447. int pcie_reg;
  1448. struct qla_hw_data *ha = vha->hw;
  1449. char lwstr[6];
  1450. uint16_t lnk;
  1451. pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  1452. pci_read_config_word(ha->pdev, pcie_reg + PCI_EXP_LNKSTA, &lnk);
  1453. ha->link_width = (lnk >> 4) & 0x3f;
  1454. strcpy(str, "PCIe (");
  1455. strcat(str, "2.5Gb/s ");
  1456. snprintf(lwstr, sizeof(lwstr), "x%d)", ha->link_width);
  1457. strcat(str, lwstr);
  1458. return str;
  1459. }
  1460. int qla82xx_pci_region_offset(struct pci_dev *pdev, int region)
  1461. {
  1462. unsigned long val = 0;
  1463. u32 control;
  1464. switch (region) {
  1465. case 0:
  1466. val = 0;
  1467. break;
  1468. case 1:
  1469. pci_read_config_dword(pdev, QLA82XX_PCI_REG_MSIX_TBL, &control);
  1470. val = control + QLA82XX_MSIX_TBL_SPACE;
  1471. break;
  1472. }
  1473. return val;
  1474. }
  1475. int
  1476. qla82xx_iospace_config(struct qla_hw_data *ha)
  1477. {
  1478. uint32_t len = 0;
  1479. if (pci_request_regions(ha->pdev, QLA2XXX_DRIVER_NAME)) {
  1480. ql_log_pci(ql_log_fatal, ha->pdev, 0x000c,
  1481. "Failed to reserver selected regions.\n");
  1482. goto iospace_error_exit;
  1483. }
  1484. /* Use MMIO operations for all accesses. */
  1485. if (!(pci_resource_flags(ha->pdev, 0) & IORESOURCE_MEM)) {
  1486. ql_log_pci(ql_log_fatal, ha->pdev, 0x000d,
  1487. "Region #0 not an MMIO resource, aborting.\n");
  1488. goto iospace_error_exit;
  1489. }
  1490. len = pci_resource_len(ha->pdev, 0);
  1491. ha->nx_pcibase =
  1492. (unsigned long)ioremap(pci_resource_start(ha->pdev, 0), len);
  1493. if (!ha->nx_pcibase) {
  1494. ql_log_pci(ql_log_fatal, ha->pdev, 0x000e,
  1495. "Cannot remap pcibase MMIO, aborting.\n");
  1496. pci_release_regions(ha->pdev);
  1497. goto iospace_error_exit;
  1498. }
  1499. /* Mapping of IO base pointer */
  1500. ha->iobase = (device_reg_t __iomem *)((uint8_t *)ha->nx_pcibase +
  1501. 0xbc000 + (ha->pdev->devfn << 11));
  1502. if (!ql2xdbwr) {
  1503. ha->nxdb_wr_ptr =
  1504. (unsigned long)ioremap((pci_resource_start(ha->pdev, 4) +
  1505. (ha->pdev->devfn << 12)), 4);
  1506. if (!ha->nxdb_wr_ptr) {
  1507. ql_log_pci(ql_log_fatal, ha->pdev, 0x000f,
  1508. "Cannot remap MMIO, aborting.\n");
  1509. pci_release_regions(ha->pdev);
  1510. goto iospace_error_exit;
  1511. }
  1512. /* Mapping of IO base pointer,
  1513. * door bell read and write pointer
  1514. */
  1515. ha->nxdb_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
  1516. (ha->pdev->devfn * 8);
  1517. } else {
  1518. ha->nxdb_wr_ptr = (ha->pdev->devfn == 6 ?
  1519. QLA82XX_CAMRAM_DB1 :
  1520. QLA82XX_CAMRAM_DB2);
  1521. }
  1522. ha->max_req_queues = ha->max_rsp_queues = 1;
  1523. ha->msix_count = ha->max_rsp_queues + 1;
  1524. ql_dbg_pci(ql_dbg_multiq, ha->pdev, 0xc006,
  1525. "nx_pci_base=%p iobase=%p "
  1526. "max_req_queues=%d msix_count=%d.\n",
  1527. ha->nx_pcibase, ha->iobase,
  1528. ha->max_req_queues, ha->msix_count);
  1529. ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0010,
  1530. "nx_pci_base=%p iobase=%p "
  1531. "max_req_queues=%d msix_count=%d.\n",
  1532. ha->nx_pcibase, ha->iobase,
  1533. ha->max_req_queues, ha->msix_count);
  1534. return 0;
  1535. iospace_error_exit:
  1536. return -ENOMEM;
  1537. }
  1538. /* GS related functions */
  1539. /* Initialization related functions */
  1540. /**
  1541. * qla82xx_pci_config() - Setup ISP82xx PCI configuration registers.
  1542. * @ha: HA context
  1543. *
  1544. * Returns 0 on success.
  1545. */
  1546. int
  1547. qla82xx_pci_config(scsi_qla_host_t *vha)
  1548. {
  1549. struct qla_hw_data *ha = vha->hw;
  1550. int ret;
  1551. pci_set_master(ha->pdev);
  1552. ret = pci_set_mwi(ha->pdev);
  1553. ha->chip_revision = ha->pdev->revision;
  1554. ql_dbg(ql_dbg_init, vha, 0x0043,
  1555. "Chip revision:%ld.\n",
  1556. ha->chip_revision);
  1557. return 0;
  1558. }
  1559. /**
  1560. * qla82xx_reset_chip() - Setup ISP82xx PCI configuration registers.
  1561. * @ha: HA context
  1562. *
  1563. * Returns 0 on success.
  1564. */
  1565. void
  1566. qla82xx_reset_chip(scsi_qla_host_t *vha)
  1567. {
  1568. struct qla_hw_data *ha = vha->hw;
  1569. ha->isp_ops->disable_intrs(ha);
  1570. }
  1571. void qla82xx_config_rings(struct scsi_qla_host *vha)
  1572. {
  1573. struct qla_hw_data *ha = vha->hw;
  1574. struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
  1575. struct init_cb_81xx *icb;
  1576. struct req_que *req = ha->req_q_map[0];
  1577. struct rsp_que *rsp = ha->rsp_q_map[0];
  1578. /* Setup ring parameters in initialization control block. */
  1579. icb = (struct init_cb_81xx *)ha->init_cb;
  1580. icb->request_q_outpointer = __constant_cpu_to_le16(0);
  1581. icb->response_q_inpointer = __constant_cpu_to_le16(0);
  1582. icb->request_q_length = cpu_to_le16(req->length);
  1583. icb->response_q_length = cpu_to_le16(rsp->length);
  1584. icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
  1585. icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
  1586. icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
  1587. icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
  1588. WRT_REG_DWORD((unsigned long __iomem *)&reg->req_q_out[0], 0);
  1589. WRT_REG_DWORD((unsigned long __iomem *)&reg->rsp_q_in[0], 0);
  1590. WRT_REG_DWORD((unsigned long __iomem *)&reg->rsp_q_out[0], 0);
  1591. }
  1592. void qla82xx_reset_adapter(struct scsi_qla_host *vha)
  1593. {
  1594. struct qla_hw_data *ha = vha->hw;
  1595. vha->flags.online = 0;
  1596. qla2x00_try_to_stop_firmware(vha);
  1597. ha->isp_ops->disable_intrs(ha);
  1598. }
  1599. static int
  1600. qla82xx_fw_load_from_blob(struct qla_hw_data *ha)
  1601. {
  1602. u64 *ptr64;
  1603. u32 i, flashaddr, size;
  1604. __le64 data;
  1605. size = (IMAGE_START - BOOTLD_START) / 8;
  1606. ptr64 = (u64 *)qla82xx_get_bootld_offset(ha);
  1607. flashaddr = BOOTLD_START;
  1608. for (i = 0; i < size; i++) {
  1609. data = cpu_to_le64(ptr64[i]);
  1610. if (qla82xx_pci_mem_write_2M(ha, flashaddr, &data, 8))
  1611. return -EIO;
  1612. flashaddr += 8;
  1613. }
  1614. flashaddr = FLASH_ADDR_START;
  1615. size = (__force u32)qla82xx_get_fw_size(ha) / 8;
  1616. ptr64 = (u64 *)qla82xx_get_fw_offs(ha);
  1617. for (i = 0; i < size; i++) {
  1618. data = cpu_to_le64(ptr64[i]);
  1619. if (qla82xx_pci_mem_write_2M(ha, flashaddr, &data, 8))
  1620. return -EIO;
  1621. flashaddr += 8;
  1622. }
  1623. udelay(100);
  1624. /* Write a magic value to CAMRAM register
  1625. * at a specified offset to indicate
  1626. * that all data is written and
  1627. * ready for firmware to initialize.
  1628. */
  1629. qla82xx_wr_32(ha, QLA82XX_CAM_RAM(0x1fc), QLA82XX_BDINFO_MAGIC);
  1630. read_lock(&ha->hw_lock);
  1631. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
  1632. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
  1633. read_unlock(&ha->hw_lock);
  1634. return 0;
  1635. }
  1636. static int
  1637. qla82xx_set_product_offset(struct qla_hw_data *ha)
  1638. {
  1639. struct qla82xx_uri_table_desc *ptab_desc = NULL;
  1640. const uint8_t *unirom = ha->hablob->fw->data;
  1641. uint32_t i;
  1642. __le32 entries;
  1643. __le32 flags, file_chiprev, offset;
  1644. uint8_t chiprev = ha->chip_revision;
  1645. /* Hardcoding mn_present flag for P3P */
  1646. int mn_present = 0;
  1647. uint32_t flagbit;
  1648. ptab_desc = qla82xx_get_table_desc(unirom,
  1649. QLA82XX_URI_DIR_SECT_PRODUCT_TBL);
  1650. if (!ptab_desc)
  1651. return -1;
  1652. entries = cpu_to_le32(ptab_desc->num_entries);
  1653. for (i = 0; i < entries; i++) {
  1654. offset = cpu_to_le32(ptab_desc->findex) +
  1655. (i * cpu_to_le32(ptab_desc->entry_size));
  1656. flags = cpu_to_le32(*((int *)&unirom[offset] +
  1657. QLA82XX_URI_FLAGS_OFF));
  1658. file_chiprev = cpu_to_le32(*((int *)&unirom[offset] +
  1659. QLA82XX_URI_CHIP_REV_OFF));
  1660. flagbit = mn_present ? 1 : 2;
  1661. if ((chiprev == file_chiprev) && ((1ULL << flagbit) & flags)) {
  1662. ha->file_prd_off = offset;
  1663. return 0;
  1664. }
  1665. }
  1666. return -1;
  1667. }
  1668. int
  1669. qla82xx_validate_firmware_blob(scsi_qla_host_t *vha, uint8_t fw_type)
  1670. {
  1671. __le32 val;
  1672. uint32_t min_size;
  1673. struct qla_hw_data *ha = vha->hw;
  1674. const struct firmware *fw = ha->hablob->fw;
  1675. ha->fw_type = fw_type;
  1676. if (fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
  1677. if (qla82xx_set_product_offset(ha))
  1678. return -EINVAL;
  1679. min_size = QLA82XX_URI_FW_MIN_SIZE;
  1680. } else {
  1681. val = cpu_to_le32(*(u32 *)&fw->data[QLA82XX_FW_MAGIC_OFFSET]);
  1682. if ((__force u32)val != QLA82XX_BDINFO_MAGIC)
  1683. return -EINVAL;
  1684. min_size = QLA82XX_FW_MIN_SIZE;
  1685. }
  1686. if (fw->size < min_size)
  1687. return -EINVAL;
  1688. return 0;
  1689. }
  1690. static int
  1691. qla82xx_check_cmdpeg_state(struct qla_hw_data *ha)
  1692. {
  1693. u32 val = 0;
  1694. int retries = 60;
  1695. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  1696. do {
  1697. read_lock(&ha->hw_lock);
  1698. val = qla82xx_rd_32(ha, CRB_CMDPEG_STATE);
  1699. read_unlock(&ha->hw_lock);
  1700. switch (val) {
  1701. case PHAN_INITIALIZE_COMPLETE:
  1702. case PHAN_INITIALIZE_ACK:
  1703. return QLA_SUCCESS;
  1704. case PHAN_INITIALIZE_FAILED:
  1705. break;
  1706. default:
  1707. break;
  1708. }
  1709. ql_log(ql_log_info, vha, 0x00a8,
  1710. "CRB_CMDPEG_STATE: 0x%x and retries:0x%x.\n",
  1711. val, retries);
  1712. msleep(500);
  1713. } while (--retries);
  1714. ql_log(ql_log_fatal, vha, 0x00a9,
  1715. "Cmd Peg initialization failed: 0x%x.\n", val);
  1716. val = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_PEGTUNE_DONE);
  1717. read_lock(&ha->hw_lock);
  1718. qla82xx_wr_32(ha, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
  1719. read_unlock(&ha->hw_lock);
  1720. return QLA_FUNCTION_FAILED;
  1721. }
  1722. static int
  1723. qla82xx_check_rcvpeg_state(struct qla_hw_data *ha)
  1724. {
  1725. u32 val = 0;
  1726. int retries = 60;
  1727. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  1728. do {
  1729. read_lock(&ha->hw_lock);
  1730. val = qla82xx_rd_32(ha, CRB_RCVPEG_STATE);
  1731. read_unlock(&ha->hw_lock);
  1732. switch (val) {
  1733. case PHAN_INITIALIZE_COMPLETE:
  1734. case PHAN_INITIALIZE_ACK:
  1735. return QLA_SUCCESS;
  1736. case PHAN_INITIALIZE_FAILED:
  1737. break;
  1738. default:
  1739. break;
  1740. }
  1741. ql_log(ql_log_info, vha, 0x00ab,
  1742. "CRB_RCVPEG_STATE: 0x%x and retries: 0x%x.\n",
  1743. val, retries);
  1744. msleep(500);
  1745. } while (--retries);
  1746. ql_log(ql_log_fatal, vha, 0x00ac,
  1747. "Rcv Peg initializatin failed: 0x%x.\n", val);
  1748. read_lock(&ha->hw_lock);
  1749. qla82xx_wr_32(ha, CRB_RCVPEG_STATE, PHAN_INITIALIZE_FAILED);
  1750. read_unlock(&ha->hw_lock);
  1751. return QLA_FUNCTION_FAILED;
  1752. }
  1753. /* ISR related functions */
  1754. uint32_t qla82xx_isr_int_target_mask_enable[8] = {
  1755. ISR_INT_TARGET_MASK, ISR_INT_TARGET_MASK_F1,
  1756. ISR_INT_TARGET_MASK_F2, ISR_INT_TARGET_MASK_F3,
  1757. ISR_INT_TARGET_MASK_F4, ISR_INT_TARGET_MASK_F5,
  1758. ISR_INT_TARGET_MASK_F7, ISR_INT_TARGET_MASK_F7
  1759. };
  1760. uint32_t qla82xx_isr_int_target_status[8] = {
  1761. ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
  1762. ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
  1763. ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
  1764. ISR_INT_TARGET_STATUS_F7, ISR_INT_TARGET_STATUS_F7
  1765. };
  1766. static struct qla82xx_legacy_intr_set legacy_intr[] = \
  1767. QLA82XX_LEGACY_INTR_CONFIG;
  1768. /*
  1769. * qla82xx_mbx_completion() - Process mailbox command completions.
  1770. * @ha: SCSI driver HA context
  1771. * @mb0: Mailbox0 register
  1772. */
  1773. static void
  1774. qla82xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
  1775. {
  1776. uint16_t cnt;
  1777. uint16_t __iomem *wptr;
  1778. struct qla_hw_data *ha = vha->hw;
  1779. struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
  1780. wptr = (uint16_t __iomem *)&reg->mailbox_out[1];
  1781. /* Load return mailbox registers. */
  1782. ha->flags.mbox_int = 1;
  1783. ha->mailbox_out[0] = mb0;
  1784. for (cnt = 1; cnt < ha->mbx_count; cnt++) {
  1785. ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
  1786. wptr++;
  1787. }
  1788. if (ha->mcp) {
  1789. ql_dbg(ql_dbg_async, vha, 0x5052,
  1790. "Got mailbox completion. cmd=%x.\n", ha->mcp->mb[0]);
  1791. } else {
  1792. ql_dbg(ql_dbg_async, vha, 0x5053,
  1793. "MBX pointer ERROR.\n");
  1794. }
  1795. }
  1796. /*
  1797. * qla82xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
  1798. * @irq:
  1799. * @dev_id: SCSI driver HA context
  1800. * @regs:
  1801. *
  1802. * Called by system whenever the host adapter generates an interrupt.
  1803. *
  1804. * Returns handled flag.
  1805. */
  1806. irqreturn_t
  1807. qla82xx_intr_handler(int irq, void *dev_id)
  1808. {
  1809. scsi_qla_host_t *vha;
  1810. struct qla_hw_data *ha;
  1811. struct rsp_que *rsp;
  1812. struct device_reg_82xx __iomem *reg;
  1813. int status = 0, status1 = 0;
  1814. unsigned long flags;
  1815. unsigned long iter;
  1816. uint32_t stat = 0;
  1817. uint16_t mb[4];
  1818. rsp = (struct rsp_que *) dev_id;
  1819. if (!rsp) {
  1820. printk(KERN_INFO
  1821. "%s(): NULL response queue pointer.\n", __func__);
  1822. return IRQ_NONE;
  1823. }
  1824. ha = rsp->hw;
  1825. if (!ha->flags.msi_enabled) {
  1826. status = qla82xx_rd_32(ha, ISR_INT_VECTOR);
  1827. if (!(status & ha->nx_legacy_intr.int_vec_bit))
  1828. return IRQ_NONE;
  1829. status1 = qla82xx_rd_32(ha, ISR_INT_STATE_REG);
  1830. if (!ISR_IS_LEGACY_INTR_TRIGGERED(status1))
  1831. return IRQ_NONE;
  1832. }
  1833. /* clear the interrupt */
  1834. qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
  1835. /* read twice to ensure write is flushed */
  1836. qla82xx_rd_32(ha, ISR_INT_VECTOR);
  1837. qla82xx_rd_32(ha, ISR_INT_VECTOR);
  1838. reg = &ha->iobase->isp82;
  1839. spin_lock_irqsave(&ha->hardware_lock, flags);
  1840. vha = pci_get_drvdata(ha->pdev);
  1841. for (iter = 1; iter--; ) {
  1842. if (RD_REG_DWORD(&reg->host_int)) {
  1843. stat = RD_REG_DWORD(&reg->host_status);
  1844. switch (stat & 0xff) {
  1845. case 0x1:
  1846. case 0x2:
  1847. case 0x10:
  1848. case 0x11:
  1849. qla82xx_mbx_completion(vha, MSW(stat));
  1850. status |= MBX_INTERRUPT;
  1851. break;
  1852. case 0x12:
  1853. mb[0] = MSW(stat);
  1854. mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
  1855. mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
  1856. mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
  1857. qla2x00_async_event(vha, rsp, mb);
  1858. break;
  1859. case 0x13:
  1860. qla24xx_process_response_queue(vha, rsp);
  1861. break;
  1862. default:
  1863. ql_dbg(ql_dbg_async, vha, 0x5054,
  1864. "Unrecognized interrupt type (%d).\n",
  1865. stat & 0xff);
  1866. break;
  1867. }
  1868. }
  1869. WRT_REG_DWORD(&reg->host_int, 0);
  1870. }
  1871. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1872. if (!ha->flags.msi_enabled)
  1873. qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
  1874. #ifdef QL_DEBUG_LEVEL_17
  1875. if (!irq && ha->flags.eeh_busy)
  1876. ql_log(ql_log_warn, vha, 0x503d,
  1877. "isr:status %x, cmd_flags %lx, mbox_int %x, stat %x.\n",
  1878. status, ha->mbx_cmd_flags, ha->flags.mbox_int, stat);
  1879. #endif
  1880. if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
  1881. (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
  1882. set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
  1883. complete(&ha->mbx_intr_comp);
  1884. }
  1885. return IRQ_HANDLED;
  1886. }
  1887. irqreturn_t
  1888. qla82xx_msix_default(int irq, void *dev_id)
  1889. {
  1890. scsi_qla_host_t *vha;
  1891. struct qla_hw_data *ha;
  1892. struct rsp_que *rsp;
  1893. struct device_reg_82xx __iomem *reg;
  1894. int status = 0;
  1895. unsigned long flags;
  1896. uint32_t stat = 0;
  1897. uint16_t mb[4];
  1898. rsp = (struct rsp_que *) dev_id;
  1899. if (!rsp) {
  1900. printk(KERN_INFO
  1901. "%s(): NULL response queue pointer.\n", __func__);
  1902. return IRQ_NONE;
  1903. }
  1904. ha = rsp->hw;
  1905. reg = &ha->iobase->isp82;
  1906. spin_lock_irqsave(&ha->hardware_lock, flags);
  1907. vha = pci_get_drvdata(ha->pdev);
  1908. do {
  1909. if (RD_REG_DWORD(&reg->host_int)) {
  1910. stat = RD_REG_DWORD(&reg->host_status);
  1911. switch (stat & 0xff) {
  1912. case 0x1:
  1913. case 0x2:
  1914. case 0x10:
  1915. case 0x11:
  1916. qla82xx_mbx_completion(vha, MSW(stat));
  1917. status |= MBX_INTERRUPT;
  1918. break;
  1919. case 0x12:
  1920. mb[0] = MSW(stat);
  1921. mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
  1922. mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
  1923. mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
  1924. qla2x00_async_event(vha, rsp, mb);
  1925. break;
  1926. case 0x13:
  1927. qla24xx_process_response_queue(vha, rsp);
  1928. break;
  1929. default:
  1930. ql_dbg(ql_dbg_async, vha, 0x5041,
  1931. "Unrecognized interrupt type (%d).\n",
  1932. stat & 0xff);
  1933. break;
  1934. }
  1935. }
  1936. WRT_REG_DWORD(&reg->host_int, 0);
  1937. } while (0);
  1938. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1939. #ifdef QL_DEBUG_LEVEL_17
  1940. if (!irq && ha->flags.eeh_busy)
  1941. ql_log(ql_log_warn, vha, 0x5044,
  1942. "isr:status %x, cmd_flags %lx, mbox_int %x, stat %x.\n",
  1943. status, ha->mbx_cmd_flags, ha->flags.mbox_int, stat);
  1944. #endif
  1945. if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
  1946. (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
  1947. set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
  1948. complete(&ha->mbx_intr_comp);
  1949. }
  1950. return IRQ_HANDLED;
  1951. }
  1952. irqreturn_t
  1953. qla82xx_msix_rsp_q(int irq, void *dev_id)
  1954. {
  1955. scsi_qla_host_t *vha;
  1956. struct qla_hw_data *ha;
  1957. struct rsp_que *rsp;
  1958. struct device_reg_82xx __iomem *reg;
  1959. unsigned long flags;
  1960. rsp = (struct rsp_que *) dev_id;
  1961. if (!rsp) {
  1962. printk(KERN_INFO
  1963. "%s(): NULL response queue pointer.\n", __func__);
  1964. return IRQ_NONE;
  1965. }
  1966. ha = rsp->hw;
  1967. reg = &ha->iobase->isp82;
  1968. spin_lock_irqsave(&ha->hardware_lock, flags);
  1969. vha = pci_get_drvdata(ha->pdev);
  1970. qla24xx_process_response_queue(vha, rsp);
  1971. WRT_REG_DWORD(&reg->host_int, 0);
  1972. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1973. return IRQ_HANDLED;
  1974. }
  1975. void
  1976. qla82xx_poll(int irq, void *dev_id)
  1977. {
  1978. scsi_qla_host_t *vha;
  1979. struct qla_hw_data *ha;
  1980. struct rsp_que *rsp;
  1981. struct device_reg_82xx __iomem *reg;
  1982. int status = 0;
  1983. uint32_t stat;
  1984. uint16_t mb[4];
  1985. unsigned long flags;
  1986. rsp = (struct rsp_que *) dev_id;
  1987. if (!rsp) {
  1988. printk(KERN_INFO
  1989. "%s(): NULL response queue pointer.\n", __func__);
  1990. return;
  1991. }
  1992. ha = rsp->hw;
  1993. reg = &ha->iobase->isp82;
  1994. spin_lock_irqsave(&ha->hardware_lock, flags);
  1995. vha = pci_get_drvdata(ha->pdev);
  1996. if (RD_REG_DWORD(&reg->host_int)) {
  1997. stat = RD_REG_DWORD(&reg->host_status);
  1998. switch (stat & 0xff) {
  1999. case 0x1:
  2000. case 0x2:
  2001. case 0x10:
  2002. case 0x11:
  2003. qla82xx_mbx_completion(vha, MSW(stat));
  2004. status |= MBX_INTERRUPT;
  2005. break;
  2006. case 0x12:
  2007. mb[0] = MSW(stat);
  2008. mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
  2009. mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
  2010. mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
  2011. qla2x00_async_event(vha, rsp, mb);
  2012. break;
  2013. case 0x13:
  2014. qla24xx_process_response_queue(vha, rsp);
  2015. break;
  2016. default:
  2017. ql_dbg(ql_dbg_p3p, vha, 0xb013,
  2018. "Unrecognized interrupt type (%d).\n",
  2019. stat * 0xff);
  2020. break;
  2021. }
  2022. }
  2023. WRT_REG_DWORD(&reg->host_int, 0);
  2024. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2025. }
  2026. void
  2027. qla82xx_enable_intrs(struct qla_hw_data *ha)
  2028. {
  2029. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2030. qla82xx_mbx_intr_enable(vha);
  2031. spin_lock_irq(&ha->hardware_lock);
  2032. qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
  2033. spin_unlock_irq(&ha->hardware_lock);
  2034. ha->interrupts_on = 1;
  2035. }
  2036. void
  2037. qla82xx_disable_intrs(struct qla_hw_data *ha)
  2038. {
  2039. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2040. qla82xx_mbx_intr_disable(vha);
  2041. spin_lock_irq(&ha->hardware_lock);
  2042. qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0x0400);
  2043. spin_unlock_irq(&ha->hardware_lock);
  2044. ha->interrupts_on = 0;
  2045. }
  2046. void qla82xx_init_flags(struct qla_hw_data *ha)
  2047. {
  2048. struct qla82xx_legacy_intr_set *nx_legacy_intr;
  2049. /* ISP 8021 initializations */
  2050. rwlock_init(&ha->hw_lock);
  2051. ha->qdr_sn_window = -1;
  2052. ha->ddr_mn_window = -1;
  2053. ha->curr_window = 255;
  2054. ha->portnum = PCI_FUNC(ha->pdev->devfn);
  2055. nx_legacy_intr = &legacy_intr[ha->portnum];
  2056. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  2057. ha->nx_legacy_intr.tgt_status_reg = nx_legacy_intr->tgt_status_reg;
  2058. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  2059. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  2060. }
  2061. inline void
  2062. qla82xx_set_drv_active(scsi_qla_host_t *vha)
  2063. {
  2064. uint32_t drv_active;
  2065. struct qla_hw_data *ha = vha->hw;
  2066. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  2067. /* If reset value is all FF's, initialize DRV_ACTIVE */
  2068. if (drv_active == 0xffffffff) {
  2069. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE,
  2070. QLA82XX_DRV_NOT_ACTIVE);
  2071. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  2072. }
  2073. drv_active |= (QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
  2074. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
  2075. }
  2076. inline void
  2077. qla82xx_clear_drv_active(struct qla_hw_data *ha)
  2078. {
  2079. uint32_t drv_active;
  2080. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  2081. drv_active &= ~(QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
  2082. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
  2083. }
  2084. static inline int
  2085. qla82xx_need_reset(struct qla_hw_data *ha)
  2086. {
  2087. uint32_t drv_state;
  2088. int rval;
  2089. if (ha->flags.isp82xx_reset_owner)
  2090. return 1;
  2091. else {
  2092. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2093. rval = drv_state & (QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
  2094. return rval;
  2095. }
  2096. }
  2097. static inline void
  2098. qla82xx_set_rst_ready(struct qla_hw_data *ha)
  2099. {
  2100. uint32_t drv_state;
  2101. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2102. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2103. /* If reset value is all FF's, initialize DRV_STATE */
  2104. if (drv_state == 0xffffffff) {
  2105. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, QLA82XX_DRVST_NOT_RDY);
  2106. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2107. }
  2108. drv_state |= (QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
  2109. ql_dbg(ql_dbg_init, vha, 0x00bb,
  2110. "drv_state = 0x%08x.\n", drv_state);
  2111. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
  2112. }
  2113. static inline void
  2114. qla82xx_clear_rst_ready(struct qla_hw_data *ha)
  2115. {
  2116. uint32_t drv_state;
  2117. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2118. drv_state &= ~(QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
  2119. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
  2120. }
  2121. static inline void
  2122. qla82xx_set_qsnt_ready(struct qla_hw_data *ha)
  2123. {
  2124. uint32_t qsnt_state;
  2125. qsnt_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2126. qsnt_state |= (QLA82XX_DRVST_QSNT_RDY << (ha->portnum * 4));
  2127. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, qsnt_state);
  2128. }
  2129. void
  2130. qla82xx_clear_qsnt_ready(scsi_qla_host_t *vha)
  2131. {
  2132. struct qla_hw_data *ha = vha->hw;
  2133. uint32_t qsnt_state;
  2134. qsnt_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2135. qsnt_state &= ~(QLA82XX_DRVST_QSNT_RDY << (ha->portnum * 4));
  2136. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, qsnt_state);
  2137. }
  2138. static int
  2139. qla82xx_load_fw(scsi_qla_host_t *vha)
  2140. {
  2141. int rst;
  2142. struct fw_blob *blob;
  2143. struct qla_hw_data *ha = vha->hw;
  2144. if (qla82xx_pinit_from_rom(vha) != QLA_SUCCESS) {
  2145. ql_log(ql_log_fatal, vha, 0x009f,
  2146. "Error during CRB initialization.\n");
  2147. return QLA_FUNCTION_FAILED;
  2148. }
  2149. udelay(500);
  2150. /* Bring QM and CAMRAM out of reset */
  2151. rst = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET);
  2152. rst &= ~((1 << 28) | (1 << 24));
  2153. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, rst);
  2154. /*
  2155. * FW Load priority:
  2156. * 1) Operational firmware residing in flash.
  2157. * 2) Firmware via request-firmware interface (.bin file).
  2158. */
  2159. if (ql2xfwloadbin == 2)
  2160. goto try_blob_fw;
  2161. ql_log(ql_log_info, vha, 0x00a0,
  2162. "Attempting to load firmware from flash.\n");
  2163. if (qla82xx_fw_load_from_flash(ha) == QLA_SUCCESS) {
  2164. ql_log(ql_log_info, vha, 0x00a1,
  2165. "Firmware loaded successully from flash.\n");
  2166. return QLA_SUCCESS;
  2167. } else {
  2168. ql_log(ql_log_warn, vha, 0x0108,
  2169. "Firmware load from flash failed.\n");
  2170. }
  2171. try_blob_fw:
  2172. ql_log(ql_log_info, vha, 0x00a2,
  2173. "Attempting to load firmware from blob.\n");
  2174. /* Load firmware blob. */
  2175. blob = ha->hablob = qla2x00_request_firmware(vha);
  2176. if (!blob) {
  2177. ql_log(ql_log_fatal, vha, 0x00a3,
  2178. "Firmware image not preset.\n");
  2179. goto fw_load_failed;
  2180. }
  2181. /* Validating firmware blob */
  2182. if (qla82xx_validate_firmware_blob(vha,
  2183. QLA82XX_FLASH_ROMIMAGE)) {
  2184. /* Fallback to URI format */
  2185. if (qla82xx_validate_firmware_blob(vha,
  2186. QLA82XX_UNIFIED_ROMIMAGE)) {
  2187. ql_log(ql_log_fatal, vha, 0x00a4,
  2188. "No valid firmware image found.\n");
  2189. return QLA_FUNCTION_FAILED;
  2190. }
  2191. }
  2192. if (qla82xx_fw_load_from_blob(ha) == QLA_SUCCESS) {
  2193. ql_log(ql_log_info, vha, 0x00a5,
  2194. "Firmware loaded successfully from binary blob.\n");
  2195. return QLA_SUCCESS;
  2196. } else {
  2197. ql_log(ql_log_fatal, vha, 0x00a6,
  2198. "Firmware load failed for binary blob.\n");
  2199. blob->fw = NULL;
  2200. blob = NULL;
  2201. goto fw_load_failed;
  2202. }
  2203. return QLA_SUCCESS;
  2204. fw_load_failed:
  2205. return QLA_FUNCTION_FAILED;
  2206. }
  2207. int
  2208. qla82xx_start_firmware(scsi_qla_host_t *vha)
  2209. {
  2210. int pcie_cap;
  2211. uint16_t lnk;
  2212. struct qla_hw_data *ha = vha->hw;
  2213. /* scrub dma mask expansion register */
  2214. qla82xx_wr_32(ha, CRB_DMA_SHIFT, QLA82XX_DMA_SHIFT_VALUE);
  2215. /* Put both the PEG CMD and RCV PEG to default state
  2216. * of 0 before resetting the hardware
  2217. */
  2218. qla82xx_wr_32(ha, CRB_CMDPEG_STATE, 0);
  2219. qla82xx_wr_32(ha, CRB_RCVPEG_STATE, 0);
  2220. /* Overwrite stale initialization register values */
  2221. qla82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS1, 0);
  2222. qla82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS2, 0);
  2223. if (qla82xx_load_fw(vha) != QLA_SUCCESS) {
  2224. ql_log(ql_log_fatal, vha, 0x00a7,
  2225. "Error trying to start fw.\n");
  2226. return QLA_FUNCTION_FAILED;
  2227. }
  2228. /* Handshake with the card before we register the devices. */
  2229. if (qla82xx_check_cmdpeg_state(ha) != QLA_SUCCESS) {
  2230. ql_log(ql_log_fatal, vha, 0x00aa,
  2231. "Error during card handshake.\n");
  2232. return QLA_FUNCTION_FAILED;
  2233. }
  2234. /* Negotiated Link width */
  2235. pcie_cap = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  2236. pci_read_config_word(ha->pdev, pcie_cap + PCI_EXP_LNKSTA, &lnk);
  2237. ha->link_width = (lnk >> 4) & 0x3f;
  2238. /* Synchronize with Receive peg */
  2239. return qla82xx_check_rcvpeg_state(ha);
  2240. }
  2241. static inline int
  2242. qla2xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
  2243. uint16_t tot_dsds)
  2244. {
  2245. uint32_t *cur_dsd = NULL;
  2246. scsi_qla_host_t *vha;
  2247. struct qla_hw_data *ha;
  2248. struct scsi_cmnd *cmd;
  2249. struct scatterlist *cur_seg;
  2250. uint32_t *dsd_seg;
  2251. void *next_dsd;
  2252. uint8_t avail_dsds;
  2253. uint8_t first_iocb = 1;
  2254. uint32_t dsd_list_len;
  2255. struct dsd_dma *dsd_ptr;
  2256. struct ct6_dsd *ctx;
  2257. cmd = sp->cmd;
  2258. /* Update entry type to indicate Command Type 3 IOCB */
  2259. *((uint32_t *)(&cmd_pkt->entry_type)) =
  2260. __constant_cpu_to_le32(COMMAND_TYPE_6);
  2261. /* No data transfer */
  2262. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  2263. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  2264. return 0;
  2265. }
  2266. vha = sp->fcport->vha;
  2267. ha = vha->hw;
  2268. /* Set transfer direction */
  2269. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  2270. cmd_pkt->control_flags =
  2271. __constant_cpu_to_le16(CF_WRITE_DATA);
  2272. ha->qla_stats.output_bytes += scsi_bufflen(cmd);
  2273. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  2274. cmd_pkt->control_flags =
  2275. __constant_cpu_to_le16(CF_READ_DATA);
  2276. ha->qla_stats.input_bytes += scsi_bufflen(cmd);
  2277. }
  2278. cur_seg = scsi_sglist(cmd);
  2279. ctx = sp->ctx;
  2280. while (tot_dsds) {
  2281. avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
  2282. QLA_DSDS_PER_IOCB : tot_dsds;
  2283. tot_dsds -= avail_dsds;
  2284. dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
  2285. dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
  2286. struct dsd_dma, list);
  2287. next_dsd = dsd_ptr->dsd_addr;
  2288. list_del(&dsd_ptr->list);
  2289. ha->gbl_dsd_avail--;
  2290. list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
  2291. ctx->dsd_use_cnt++;
  2292. ha->gbl_dsd_inuse++;
  2293. if (first_iocb) {
  2294. first_iocb = 0;
  2295. dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
  2296. *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  2297. *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  2298. cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
  2299. } else {
  2300. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  2301. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  2302. *cur_dsd++ = cpu_to_le32(dsd_list_len);
  2303. }
  2304. cur_dsd = (uint32_t *)next_dsd;
  2305. while (avail_dsds) {
  2306. dma_addr_t sle_dma;
  2307. sle_dma = sg_dma_address(cur_seg);
  2308. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  2309. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  2310. *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
  2311. cur_seg = sg_next(cur_seg);
  2312. avail_dsds--;
  2313. }
  2314. }
  2315. /* Null termination */
  2316. *cur_dsd++ = 0;
  2317. *cur_dsd++ = 0;
  2318. *cur_dsd++ = 0;
  2319. cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
  2320. return 0;
  2321. }
  2322. /*
  2323. * qla82xx_calc_dsd_lists() - Determine number of DSD list required
  2324. * for Command Type 6.
  2325. *
  2326. * @dsds: number of data segment decriptors needed
  2327. *
  2328. * Returns the number of dsd list needed to store @dsds.
  2329. */
  2330. inline uint16_t
  2331. qla82xx_calc_dsd_lists(uint16_t dsds)
  2332. {
  2333. uint16_t dsd_lists = 0;
  2334. dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
  2335. if (dsds % QLA_DSDS_PER_IOCB)
  2336. dsd_lists++;
  2337. return dsd_lists;
  2338. }
  2339. /*
  2340. * qla82xx_start_scsi() - Send a SCSI command to the ISP
  2341. * @sp: command to send to the ISP
  2342. *
  2343. * Returns non-zero if a failure occurred, else zero.
  2344. */
  2345. int
  2346. qla82xx_start_scsi(srb_t *sp)
  2347. {
  2348. int ret, nseg;
  2349. unsigned long flags;
  2350. struct scsi_cmnd *cmd;
  2351. uint32_t *clr_ptr;
  2352. uint32_t index;
  2353. uint32_t handle;
  2354. uint16_t cnt;
  2355. uint16_t req_cnt;
  2356. uint16_t tot_dsds;
  2357. struct device_reg_82xx __iomem *reg;
  2358. uint32_t dbval;
  2359. uint32_t *fcp_dl;
  2360. uint8_t additional_cdb_len;
  2361. struct ct6_dsd *ctx;
  2362. struct scsi_qla_host *vha = sp->fcport->vha;
  2363. struct qla_hw_data *ha = vha->hw;
  2364. struct req_que *req = NULL;
  2365. struct rsp_que *rsp = NULL;
  2366. char tag[2];
  2367. /* Setup device pointers. */
  2368. ret = 0;
  2369. reg = &ha->iobase->isp82;
  2370. cmd = sp->cmd;
  2371. req = vha->req;
  2372. rsp = ha->rsp_q_map[0];
  2373. /* So we know we haven't pci_map'ed anything yet */
  2374. tot_dsds = 0;
  2375. dbval = 0x04 | (ha->portnum << 5);
  2376. /* Send marker if required */
  2377. if (vha->marker_needed != 0) {
  2378. if (qla2x00_marker(vha, req,
  2379. rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
  2380. ql_log(ql_log_warn, vha, 0x300c,
  2381. "qla2x00_marker failed for cmd=%p.\n", cmd);
  2382. return QLA_FUNCTION_FAILED;
  2383. }
  2384. vha->marker_needed = 0;
  2385. }
  2386. /* Acquire ring specific lock */
  2387. spin_lock_irqsave(&ha->hardware_lock, flags);
  2388. /* Check for room in outstanding command list. */
  2389. handle = req->current_outstanding_cmd;
  2390. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  2391. handle++;
  2392. if (handle == MAX_OUTSTANDING_COMMANDS)
  2393. handle = 1;
  2394. if (!req->outstanding_cmds[handle])
  2395. break;
  2396. }
  2397. if (index == MAX_OUTSTANDING_COMMANDS)
  2398. goto queuing_error;
  2399. /* Map the sg table so we have an accurate count of sg entries needed */
  2400. if (scsi_sg_count(cmd)) {
  2401. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  2402. scsi_sg_count(cmd), cmd->sc_data_direction);
  2403. if (unlikely(!nseg))
  2404. goto queuing_error;
  2405. } else
  2406. nseg = 0;
  2407. tot_dsds = nseg;
  2408. if (tot_dsds > ql2xshiftctondsd) {
  2409. struct cmd_type_6 *cmd_pkt;
  2410. uint16_t more_dsd_lists = 0;
  2411. struct dsd_dma *dsd_ptr;
  2412. uint16_t i;
  2413. more_dsd_lists = qla82xx_calc_dsd_lists(tot_dsds);
  2414. if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
  2415. ql_dbg(ql_dbg_io, vha, 0x300d,
  2416. "Num of DSD list %d is than %d for cmd=%p.\n",
  2417. more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
  2418. cmd);
  2419. goto queuing_error;
  2420. }
  2421. if (more_dsd_lists <= ha->gbl_dsd_avail)
  2422. goto sufficient_dsds;
  2423. else
  2424. more_dsd_lists -= ha->gbl_dsd_avail;
  2425. for (i = 0; i < more_dsd_lists; i++) {
  2426. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  2427. if (!dsd_ptr) {
  2428. ql_log(ql_log_fatal, vha, 0x300e,
  2429. "Failed to allocate memory for dsd_dma "
  2430. "for cmd=%p.\n", cmd);
  2431. goto queuing_error;
  2432. }
  2433. dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
  2434. GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
  2435. if (!dsd_ptr->dsd_addr) {
  2436. kfree(dsd_ptr);
  2437. ql_log(ql_log_fatal, vha, 0x300f,
  2438. "Failed to allocate memory for dsd_addr "
  2439. "for cmd=%p.\n", cmd);
  2440. goto queuing_error;
  2441. }
  2442. list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
  2443. ha->gbl_dsd_avail++;
  2444. }
  2445. sufficient_dsds:
  2446. req_cnt = 1;
  2447. if (req->cnt < (req_cnt + 2)) {
  2448. cnt = (uint16_t)RD_REG_DWORD_RELAXED(
  2449. &reg->req_q_out[0]);
  2450. if (req->ring_index < cnt)
  2451. req->cnt = cnt - req->ring_index;
  2452. else
  2453. req->cnt = req->length -
  2454. (req->ring_index - cnt);
  2455. }
  2456. if (req->cnt < (req_cnt + 2))
  2457. goto queuing_error;
  2458. ctx = sp->ctx = mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
  2459. if (!sp->ctx) {
  2460. ql_log(ql_log_fatal, vha, 0x3010,
  2461. "Failed to allocate ctx for cmd=%p.\n", cmd);
  2462. goto queuing_error;
  2463. }
  2464. memset(ctx, 0, sizeof(struct ct6_dsd));
  2465. ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
  2466. GFP_ATOMIC, &ctx->fcp_cmnd_dma);
  2467. if (!ctx->fcp_cmnd) {
  2468. ql_log(ql_log_fatal, vha, 0x3011,
  2469. "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
  2470. goto queuing_error_fcp_cmnd;
  2471. }
  2472. /* Initialize the DSD list and dma handle */
  2473. INIT_LIST_HEAD(&ctx->dsd_list);
  2474. ctx->dsd_use_cnt = 0;
  2475. if (cmd->cmd_len > 16) {
  2476. additional_cdb_len = cmd->cmd_len - 16;
  2477. if ((cmd->cmd_len % 4) != 0) {
  2478. /* SCSI command bigger than 16 bytes must be
  2479. * multiple of 4
  2480. */
  2481. ql_log(ql_log_warn, vha, 0x3012,
  2482. "scsi cmd len %d not multiple of 4 "
  2483. "for cmd=%p.\n", cmd->cmd_len, cmd);
  2484. goto queuing_error_fcp_cmnd;
  2485. }
  2486. ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  2487. } else {
  2488. additional_cdb_len = 0;
  2489. ctx->fcp_cmnd_len = 12 + 16 + 4;
  2490. }
  2491. cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
  2492. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  2493. /* Zero out remaining portion of packet. */
  2494. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  2495. clr_ptr = (uint32_t *)cmd_pkt + 2;
  2496. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  2497. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  2498. /* Set NPORT-ID and LUN number*/
  2499. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  2500. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  2501. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  2502. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  2503. cmd_pkt->vp_index = sp->fcport->vp_idx;
  2504. /* Build IOCB segments */
  2505. if (qla2xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
  2506. goto queuing_error_fcp_cmnd;
  2507. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  2508. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  2509. /* build FCP_CMND IU */
  2510. memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  2511. int_to_scsilun(sp->cmd->device->lun, &ctx->fcp_cmnd->lun);
  2512. ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
  2513. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  2514. ctx->fcp_cmnd->additional_cdb_len |= 1;
  2515. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  2516. ctx->fcp_cmnd->additional_cdb_len |= 2;
  2517. /*
  2518. * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
  2519. */
  2520. if (scsi_populate_tag_msg(cmd, tag)) {
  2521. switch (tag[0]) {
  2522. case HEAD_OF_QUEUE_TAG:
  2523. ctx->fcp_cmnd->task_attribute =
  2524. TSK_HEAD_OF_QUEUE;
  2525. break;
  2526. case ORDERED_QUEUE_TAG:
  2527. ctx->fcp_cmnd->task_attribute =
  2528. TSK_ORDERED;
  2529. break;
  2530. }
  2531. }
  2532. memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  2533. fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
  2534. additional_cdb_len);
  2535. *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
  2536. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
  2537. cmd_pkt->fcp_cmnd_dseg_address[0] =
  2538. cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
  2539. cmd_pkt->fcp_cmnd_dseg_address[1] =
  2540. cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
  2541. sp->flags |= SRB_FCP_CMND_DMA_VALID;
  2542. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  2543. /* Set total data segment count. */
  2544. cmd_pkt->entry_count = (uint8_t)req_cnt;
  2545. /* Specify response queue number where
  2546. * completion should happen
  2547. */
  2548. cmd_pkt->entry_status = (uint8_t) rsp->id;
  2549. } else {
  2550. struct cmd_type_7 *cmd_pkt;
  2551. req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
  2552. if (req->cnt < (req_cnt + 2)) {
  2553. cnt = (uint16_t)RD_REG_DWORD_RELAXED(
  2554. &reg->req_q_out[0]);
  2555. if (req->ring_index < cnt)
  2556. req->cnt = cnt - req->ring_index;
  2557. else
  2558. req->cnt = req->length -
  2559. (req->ring_index - cnt);
  2560. }
  2561. if (req->cnt < (req_cnt + 2))
  2562. goto queuing_error;
  2563. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  2564. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  2565. /* Zero out remaining portion of packet. */
  2566. /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
  2567. clr_ptr = (uint32_t *)cmd_pkt + 2;
  2568. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  2569. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  2570. /* Set NPORT-ID and LUN number*/
  2571. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  2572. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  2573. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  2574. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  2575. cmd_pkt->vp_index = sp->fcport->vp_idx;
  2576. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  2577. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
  2578. sizeof(cmd_pkt->lun));
  2579. /*
  2580. * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
  2581. */
  2582. if (scsi_populate_tag_msg(cmd, tag)) {
  2583. switch (tag[0]) {
  2584. case HEAD_OF_QUEUE_TAG:
  2585. cmd_pkt->task = TSK_HEAD_OF_QUEUE;
  2586. break;
  2587. case ORDERED_QUEUE_TAG:
  2588. cmd_pkt->task = TSK_ORDERED;
  2589. break;
  2590. }
  2591. }
  2592. /* Load SCSI command packet. */
  2593. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  2594. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  2595. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  2596. /* Build IOCB segments */
  2597. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  2598. /* Set total data segment count. */
  2599. cmd_pkt->entry_count = (uint8_t)req_cnt;
  2600. /* Specify response queue number where
  2601. * completion should happen.
  2602. */
  2603. cmd_pkt->entry_status = (uint8_t) rsp->id;
  2604. }
  2605. /* Build command packet. */
  2606. req->current_outstanding_cmd = handle;
  2607. req->outstanding_cmds[handle] = sp;
  2608. sp->handle = handle;
  2609. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  2610. req->cnt -= req_cnt;
  2611. wmb();
  2612. /* Adjust ring index. */
  2613. req->ring_index++;
  2614. if (req->ring_index == req->length) {
  2615. req->ring_index = 0;
  2616. req->ring_ptr = req->ring;
  2617. } else
  2618. req->ring_ptr++;
  2619. sp->flags |= SRB_DMA_VALID;
  2620. /* Set chip new ring index. */
  2621. /* write, read and verify logic */
  2622. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  2623. if (ql2xdbwr)
  2624. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  2625. else {
  2626. WRT_REG_DWORD(
  2627. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  2628. dbval);
  2629. wmb();
  2630. while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
  2631. WRT_REG_DWORD(
  2632. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  2633. dbval);
  2634. wmb();
  2635. }
  2636. }
  2637. /* Manage unprocessed RIO/ZIO commands in response queue. */
  2638. if (vha->flags.process_response_queue &&
  2639. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  2640. qla24xx_process_response_queue(vha, rsp);
  2641. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2642. return QLA_SUCCESS;
  2643. queuing_error_fcp_cmnd:
  2644. dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
  2645. queuing_error:
  2646. if (tot_dsds)
  2647. scsi_dma_unmap(cmd);
  2648. if (sp->ctx) {
  2649. mempool_free(sp->ctx, ha->ctx_mempool);
  2650. sp->ctx = NULL;
  2651. }
  2652. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2653. return QLA_FUNCTION_FAILED;
  2654. }
  2655. static uint32_t *
  2656. qla82xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
  2657. uint32_t length)
  2658. {
  2659. uint32_t i;
  2660. uint32_t val;
  2661. struct qla_hw_data *ha = vha->hw;
  2662. /* Dword reads to flash. */
  2663. for (i = 0; i < length/4; i++, faddr += 4) {
  2664. if (qla82xx_rom_fast_read(ha, faddr, &val)) {
  2665. ql_log(ql_log_warn, vha, 0x0106,
  2666. "Do ROM fast read failed.\n");
  2667. goto done_read;
  2668. }
  2669. dwptr[i] = __constant_cpu_to_le32(val);
  2670. }
  2671. done_read:
  2672. return dwptr;
  2673. }
  2674. static int
  2675. qla82xx_unprotect_flash(struct qla_hw_data *ha)
  2676. {
  2677. int ret;
  2678. uint32_t val;
  2679. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2680. ret = ql82xx_rom_lock_d(ha);
  2681. if (ret < 0) {
  2682. ql_log(ql_log_warn, vha, 0xb014,
  2683. "ROM Lock failed.\n");
  2684. return ret;
  2685. }
  2686. ret = qla82xx_read_status_reg(ha, &val);
  2687. if (ret < 0)
  2688. goto done_unprotect;
  2689. val &= ~(BLOCK_PROTECT_BITS << 2);
  2690. ret = qla82xx_write_status_reg(ha, val);
  2691. if (ret < 0) {
  2692. val |= (BLOCK_PROTECT_BITS << 2);
  2693. qla82xx_write_status_reg(ha, val);
  2694. }
  2695. if (qla82xx_write_disable_flash(ha) != 0)
  2696. ql_log(ql_log_warn, vha, 0xb015,
  2697. "Write disable failed.\n");
  2698. done_unprotect:
  2699. qla82xx_rom_unlock(ha);
  2700. return ret;
  2701. }
  2702. static int
  2703. qla82xx_protect_flash(struct qla_hw_data *ha)
  2704. {
  2705. int ret;
  2706. uint32_t val;
  2707. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2708. ret = ql82xx_rom_lock_d(ha);
  2709. if (ret < 0) {
  2710. ql_log(ql_log_warn, vha, 0xb016,
  2711. "ROM Lock failed.\n");
  2712. return ret;
  2713. }
  2714. ret = qla82xx_read_status_reg(ha, &val);
  2715. if (ret < 0)
  2716. goto done_protect;
  2717. val |= (BLOCK_PROTECT_BITS << 2);
  2718. /* LOCK all sectors */
  2719. ret = qla82xx_write_status_reg(ha, val);
  2720. if (ret < 0)
  2721. ql_log(ql_log_warn, vha, 0xb017,
  2722. "Write status register failed.\n");
  2723. if (qla82xx_write_disable_flash(ha) != 0)
  2724. ql_log(ql_log_warn, vha, 0xb018,
  2725. "Write disable failed.\n");
  2726. done_protect:
  2727. qla82xx_rom_unlock(ha);
  2728. return ret;
  2729. }
  2730. static int
  2731. qla82xx_erase_sector(struct qla_hw_data *ha, int addr)
  2732. {
  2733. int ret = 0;
  2734. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2735. ret = ql82xx_rom_lock_d(ha);
  2736. if (ret < 0) {
  2737. ql_log(ql_log_warn, vha, 0xb019,
  2738. "ROM Lock failed.\n");
  2739. return ret;
  2740. }
  2741. qla82xx_flash_set_write_enable(ha);
  2742. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, addr);
  2743. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
  2744. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_SE);
  2745. if (qla82xx_wait_rom_done(ha)) {
  2746. ql_log(ql_log_warn, vha, 0xb01a,
  2747. "Error waiting for rom done.\n");
  2748. ret = -1;
  2749. goto done;
  2750. }
  2751. ret = qla82xx_flash_wait_write_finish(ha);
  2752. done:
  2753. qla82xx_rom_unlock(ha);
  2754. return ret;
  2755. }
  2756. /*
  2757. * Address and length are byte address
  2758. */
  2759. uint8_t *
  2760. qla82xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  2761. uint32_t offset, uint32_t length)
  2762. {
  2763. scsi_block_requests(vha->host);
  2764. qla82xx_read_flash_data(vha, (uint32_t *)buf, offset, length);
  2765. scsi_unblock_requests(vha->host);
  2766. return buf;
  2767. }
  2768. static int
  2769. qla82xx_write_flash_data(struct scsi_qla_host *vha, uint32_t *dwptr,
  2770. uint32_t faddr, uint32_t dwords)
  2771. {
  2772. int ret;
  2773. uint32_t liter;
  2774. uint32_t sec_mask, rest_addr;
  2775. dma_addr_t optrom_dma;
  2776. void *optrom = NULL;
  2777. int page_mode = 0;
  2778. struct qla_hw_data *ha = vha->hw;
  2779. ret = -1;
  2780. /* Prepare burst-capable write on supported ISPs. */
  2781. if (page_mode && !(faddr & 0xfff) &&
  2782. dwords > OPTROM_BURST_DWORDS) {
  2783. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  2784. &optrom_dma, GFP_KERNEL);
  2785. if (!optrom) {
  2786. ql_log(ql_log_warn, vha, 0xb01b,
  2787. "Unable to allocate memory "
  2788. "for optron burst write (%x KB).\n",
  2789. OPTROM_BURST_SIZE / 1024);
  2790. }
  2791. }
  2792. rest_addr = ha->fdt_block_size - 1;
  2793. sec_mask = ~rest_addr;
  2794. ret = qla82xx_unprotect_flash(ha);
  2795. if (ret) {
  2796. ql_log(ql_log_warn, vha, 0xb01c,
  2797. "Unable to unprotect flash for update.\n");
  2798. goto write_done;
  2799. }
  2800. for (liter = 0; liter < dwords; liter++, faddr += 4, dwptr++) {
  2801. /* Are we at the beginning of a sector? */
  2802. if ((faddr & rest_addr) == 0) {
  2803. ret = qla82xx_erase_sector(ha, faddr);
  2804. if (ret) {
  2805. ql_log(ql_log_warn, vha, 0xb01d,
  2806. "Unable to erase sector: address=%x.\n",
  2807. faddr);
  2808. break;
  2809. }
  2810. }
  2811. /* Go with burst-write. */
  2812. if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
  2813. /* Copy data to DMA'ble buffer. */
  2814. memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
  2815. ret = qla2x00_load_ram(vha, optrom_dma,
  2816. (ha->flash_data_off | faddr),
  2817. OPTROM_BURST_DWORDS);
  2818. if (ret != QLA_SUCCESS) {
  2819. ql_log(ql_log_warn, vha, 0xb01e,
  2820. "Unable to burst-write optrom segment "
  2821. "(%x/%x/%llx).\n", ret,
  2822. (ha->flash_data_off | faddr),
  2823. (unsigned long long)optrom_dma);
  2824. ql_log(ql_log_warn, vha, 0xb01f,
  2825. "Reverting to slow-write.\n");
  2826. dma_free_coherent(&ha->pdev->dev,
  2827. OPTROM_BURST_SIZE, optrom, optrom_dma);
  2828. optrom = NULL;
  2829. } else {
  2830. liter += OPTROM_BURST_DWORDS - 1;
  2831. faddr += OPTROM_BURST_DWORDS - 1;
  2832. dwptr += OPTROM_BURST_DWORDS - 1;
  2833. continue;
  2834. }
  2835. }
  2836. ret = qla82xx_write_flash_dword(ha, faddr,
  2837. cpu_to_le32(*dwptr));
  2838. if (ret) {
  2839. ql_dbg(ql_dbg_p3p, vha, 0xb020,
  2840. "Unable to program flash address=%x data=%x.\n",
  2841. faddr, *dwptr);
  2842. break;
  2843. }
  2844. }
  2845. ret = qla82xx_protect_flash(ha);
  2846. if (ret)
  2847. ql_log(ql_log_warn, vha, 0xb021,
  2848. "Unable to protect flash after update.\n");
  2849. write_done:
  2850. if (optrom)
  2851. dma_free_coherent(&ha->pdev->dev,
  2852. OPTROM_BURST_SIZE, optrom, optrom_dma);
  2853. return ret;
  2854. }
  2855. int
  2856. qla82xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  2857. uint32_t offset, uint32_t length)
  2858. {
  2859. int rval;
  2860. /* Suspend HBA. */
  2861. scsi_block_requests(vha->host);
  2862. rval = qla82xx_write_flash_data(vha, (uint32_t *)buf, offset,
  2863. length >> 2);
  2864. scsi_unblock_requests(vha->host);
  2865. /* Convert return ISP82xx to generic */
  2866. if (rval)
  2867. rval = QLA_FUNCTION_FAILED;
  2868. else
  2869. rval = QLA_SUCCESS;
  2870. return rval;
  2871. }
  2872. void
  2873. qla82xx_start_iocbs(srb_t *sp)
  2874. {
  2875. struct qla_hw_data *ha = sp->fcport->vha->hw;
  2876. struct req_que *req = ha->req_q_map[0];
  2877. struct device_reg_82xx __iomem *reg;
  2878. uint32_t dbval;
  2879. /* Adjust ring index. */
  2880. req->ring_index++;
  2881. if (req->ring_index == req->length) {
  2882. req->ring_index = 0;
  2883. req->ring_ptr = req->ring;
  2884. } else
  2885. req->ring_ptr++;
  2886. reg = &ha->iobase->isp82;
  2887. dbval = 0x04 | (ha->portnum << 5);
  2888. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  2889. if (ql2xdbwr)
  2890. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  2891. else {
  2892. WRT_REG_DWORD((unsigned long __iomem *)ha->nxdb_wr_ptr, dbval);
  2893. wmb();
  2894. while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
  2895. WRT_REG_DWORD((unsigned long __iomem *)ha->nxdb_wr_ptr,
  2896. dbval);
  2897. wmb();
  2898. }
  2899. }
  2900. }
  2901. void qla82xx_rom_lock_recovery(struct qla_hw_data *ha)
  2902. {
  2903. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2904. if (qla82xx_rom_lock(ha))
  2905. /* Someone else is holding the lock. */
  2906. ql_log(ql_log_info, vha, 0xb022,
  2907. "Resetting rom_lock.\n");
  2908. /*
  2909. * Either we got the lock, or someone
  2910. * else died while holding it.
  2911. * In either case, unlock.
  2912. */
  2913. qla82xx_rom_unlock(ha);
  2914. }
  2915. /*
  2916. * qla82xx_device_bootstrap
  2917. * Initialize device, set DEV_READY, start fw
  2918. *
  2919. * Note:
  2920. * IDC lock must be held upon entry
  2921. *
  2922. * Return:
  2923. * Success : 0
  2924. * Failed : 1
  2925. */
  2926. static int
  2927. qla82xx_device_bootstrap(scsi_qla_host_t *vha)
  2928. {
  2929. int rval = QLA_SUCCESS;
  2930. int i, timeout;
  2931. uint32_t old_count, count;
  2932. struct qla_hw_data *ha = vha->hw;
  2933. int need_reset = 0, peg_stuck = 1;
  2934. need_reset = qla82xx_need_reset(ha);
  2935. old_count = qla82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  2936. for (i = 0; i < 10; i++) {
  2937. timeout = msleep_interruptible(200);
  2938. if (timeout) {
  2939. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2940. QLA82XX_DEV_FAILED);
  2941. return QLA_FUNCTION_FAILED;
  2942. }
  2943. count = qla82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  2944. if (count != old_count)
  2945. peg_stuck = 0;
  2946. }
  2947. if (need_reset) {
  2948. /* We are trying to perform a recovery here. */
  2949. if (peg_stuck)
  2950. qla82xx_rom_lock_recovery(ha);
  2951. goto dev_initialize;
  2952. } else {
  2953. /* Start of day for this ha context. */
  2954. if (peg_stuck) {
  2955. /* Either we are the first or recovery in progress. */
  2956. qla82xx_rom_lock_recovery(ha);
  2957. goto dev_initialize;
  2958. } else
  2959. /* Firmware already running. */
  2960. goto dev_ready;
  2961. }
  2962. return rval;
  2963. dev_initialize:
  2964. /* set to DEV_INITIALIZING */
  2965. ql_log(ql_log_info, vha, 0x009e,
  2966. "HW State: INITIALIZING.\n");
  2967. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_INITIALIZING);
  2968. /* Driver that sets device state to initializating sets IDC version */
  2969. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION, QLA82XX_IDC_VERSION);
  2970. qla82xx_idc_unlock(ha);
  2971. rval = qla82xx_start_firmware(vha);
  2972. qla82xx_idc_lock(ha);
  2973. if (rval != QLA_SUCCESS) {
  2974. ql_log(ql_log_fatal, vha, 0x00ad,
  2975. "HW State: FAILED.\n");
  2976. qla82xx_clear_drv_active(ha);
  2977. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_FAILED);
  2978. return rval;
  2979. }
  2980. dev_ready:
  2981. ql_log(ql_log_info, vha, 0x00ae,
  2982. "HW State: READY.\n");
  2983. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_READY);
  2984. return QLA_SUCCESS;
  2985. }
  2986. /*
  2987. * qla82xx_need_qsnt_handler
  2988. * Code to start quiescence sequence
  2989. *
  2990. * Note:
  2991. * IDC lock must be held upon entry
  2992. *
  2993. * Return: void
  2994. */
  2995. static void
  2996. qla82xx_need_qsnt_handler(scsi_qla_host_t *vha)
  2997. {
  2998. struct qla_hw_data *ha = vha->hw;
  2999. uint32_t dev_state, drv_state, drv_active;
  3000. unsigned long reset_timeout;
  3001. if (vha->flags.online) {
  3002. /*Block any further I/O and wait for pending cmnds to complete*/
  3003. qla82xx_quiescent_state_cleanup(vha);
  3004. }
  3005. /* Set the quiescence ready bit */
  3006. qla82xx_set_qsnt_ready(ha);
  3007. /*wait for 30 secs for other functions to ack */
  3008. reset_timeout = jiffies + (30 * HZ);
  3009. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  3010. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3011. /* Its 2 that is written when qsnt is acked, moving one bit */
  3012. drv_active = drv_active << 0x01;
  3013. while (drv_state != drv_active) {
  3014. if (time_after_eq(jiffies, reset_timeout)) {
  3015. /* quiescence timeout, other functions didn't ack
  3016. * changing the state to DEV_READY
  3017. */
  3018. ql_log(ql_log_info, vha, 0xb023,
  3019. "%s : QUIESCENT TIMEOUT.\n", QLA2XXX_DRIVER_NAME);
  3020. ql_log(ql_log_info, vha, 0xb024,
  3021. "DRV_ACTIVE:%d DRV_STATE:%d.\n",
  3022. drv_active, drv_state);
  3023. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  3024. QLA82XX_DEV_READY);
  3025. ql_log(ql_log_info, vha, 0xb025,
  3026. "HW State: DEV_READY.\n");
  3027. qla82xx_idc_unlock(ha);
  3028. qla2x00_perform_loop_resync(vha);
  3029. qla82xx_idc_lock(ha);
  3030. qla82xx_clear_qsnt_ready(vha);
  3031. return;
  3032. }
  3033. qla82xx_idc_unlock(ha);
  3034. msleep(1000);
  3035. qla82xx_idc_lock(ha);
  3036. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  3037. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3038. drv_active = drv_active << 0x01;
  3039. }
  3040. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3041. /* everyone acked so set the state to DEV_QUIESCENCE */
  3042. if (dev_state == QLA82XX_DEV_NEED_QUIESCENT) {
  3043. ql_log(ql_log_info, vha, 0xb026,
  3044. "HW State: DEV_QUIESCENT.\n");
  3045. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_QUIESCENT);
  3046. }
  3047. }
  3048. /*
  3049. * qla82xx_wait_for_state_change
  3050. * Wait for device state to change from given current state
  3051. *
  3052. * Note:
  3053. * IDC lock must not be held upon entry
  3054. *
  3055. * Return:
  3056. * Changed device state.
  3057. */
  3058. uint32_t
  3059. qla82xx_wait_for_state_change(scsi_qla_host_t *vha, uint32_t curr_state)
  3060. {
  3061. struct qla_hw_data *ha = vha->hw;
  3062. uint32_t dev_state;
  3063. do {
  3064. msleep(1000);
  3065. qla82xx_idc_lock(ha);
  3066. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3067. qla82xx_idc_unlock(ha);
  3068. } while (dev_state == curr_state);
  3069. return dev_state;
  3070. }
  3071. static void
  3072. qla82xx_dev_failed_handler(scsi_qla_host_t *vha)
  3073. {
  3074. struct qla_hw_data *ha = vha->hw;
  3075. /* Disable the board */
  3076. ql_log(ql_log_fatal, vha, 0x00b8,
  3077. "Disabling the board.\n");
  3078. qla82xx_idc_lock(ha);
  3079. qla82xx_clear_drv_active(ha);
  3080. qla82xx_idc_unlock(ha);
  3081. /* Set DEV_FAILED flag to disable timer */
  3082. vha->device_flags |= DFLG_DEV_FAILED;
  3083. qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
  3084. qla2x00_mark_all_devices_lost(vha, 0);
  3085. vha->flags.online = 0;
  3086. vha->flags.init_done = 0;
  3087. }
  3088. /*
  3089. * qla82xx_need_reset_handler
  3090. * Code to start reset sequence
  3091. *
  3092. * Note:
  3093. * IDC lock must be held upon entry
  3094. *
  3095. * Return:
  3096. * Success : 0
  3097. * Failed : 1
  3098. */
  3099. static void
  3100. qla82xx_need_reset_handler(scsi_qla_host_t *vha)
  3101. {
  3102. uint32_t dev_state, drv_state, drv_active;
  3103. uint32_t active_mask = 0;
  3104. unsigned long reset_timeout;
  3105. struct qla_hw_data *ha = vha->hw;
  3106. struct req_que *req = ha->req_q_map[0];
  3107. if (vha->flags.online) {
  3108. qla82xx_idc_unlock(ha);
  3109. qla2x00_abort_isp_cleanup(vha);
  3110. ha->isp_ops->get_flash_version(vha, req->ring);
  3111. ha->isp_ops->nvram_config(vha);
  3112. qla82xx_idc_lock(ha);
  3113. }
  3114. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3115. if (!ha->flags.isp82xx_reset_owner) {
  3116. ql_dbg(ql_dbg_p3p, vha, 0xb028,
  3117. "reset_acknowledged by 0x%x\n", ha->portnum);
  3118. qla82xx_set_rst_ready(ha);
  3119. } else {
  3120. active_mask = ~(QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
  3121. drv_active &= active_mask;
  3122. ql_dbg(ql_dbg_p3p, vha, 0xb029,
  3123. "active_mask: 0x%08x\n", active_mask);
  3124. }
  3125. /* wait for 10 seconds for reset ack from all functions */
  3126. reset_timeout = jiffies + (ha->nx_reset_timeout * HZ);
  3127. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  3128. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3129. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3130. ql_dbg(ql_dbg_p3p, vha, 0xb02a,
  3131. "drv_state: 0x%08x, drv_active: 0x%08x, "
  3132. "dev_state: 0x%08x, active_mask: 0x%08x\n",
  3133. drv_state, drv_active, dev_state, active_mask);
  3134. while (drv_state != drv_active &&
  3135. dev_state != QLA82XX_DEV_INITIALIZING) {
  3136. if (time_after_eq(jiffies, reset_timeout)) {
  3137. ql_log(ql_log_warn, vha, 0x00b5,
  3138. "Reset timeout.\n");
  3139. break;
  3140. }
  3141. qla82xx_idc_unlock(ha);
  3142. msleep(1000);
  3143. qla82xx_idc_lock(ha);
  3144. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  3145. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3146. if (ha->flags.isp82xx_reset_owner)
  3147. drv_active &= active_mask;
  3148. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3149. }
  3150. ql_dbg(ql_dbg_p3p, vha, 0xb02b,
  3151. "drv_state: 0x%08x, drv_active: 0x%08x, "
  3152. "dev_state: 0x%08x, active_mask: 0x%08x\n",
  3153. drv_state, drv_active, dev_state, active_mask);
  3154. ql_log(ql_log_info, vha, 0x00b6,
  3155. "Device state is 0x%x = %s.\n",
  3156. dev_state,
  3157. dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
  3158. /* Force to DEV_COLD unless someone else is starting a reset */
  3159. if (dev_state != QLA82XX_DEV_INITIALIZING &&
  3160. dev_state != QLA82XX_DEV_COLD) {
  3161. ql_log(ql_log_info, vha, 0x00b7,
  3162. "HW State: COLD/RE-INIT.\n");
  3163. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_COLD);
  3164. if (ql2xmdenable) {
  3165. if (qla82xx_md_collect(vha))
  3166. ql_log(ql_log_warn, vha, 0xb02c,
  3167. "Not able to collect minidump.\n");
  3168. } else
  3169. ql_log(ql_log_warn, vha, 0xb04f,
  3170. "Minidump disabled.\n");
  3171. }
  3172. }
  3173. int
  3174. qla82xx_check_md_needed(scsi_qla_host_t *vha)
  3175. {
  3176. struct qla_hw_data *ha = vha->hw;
  3177. uint16_t fw_major_version, fw_minor_version, fw_subminor_version;
  3178. int rval = QLA_SUCCESS;
  3179. fw_major_version = ha->fw_major_version;
  3180. fw_minor_version = ha->fw_minor_version;
  3181. fw_subminor_version = ha->fw_subminor_version;
  3182. rval = qla2x00_get_fw_version(vha, &ha->fw_major_version,
  3183. &ha->fw_minor_version, &ha->fw_subminor_version,
  3184. &ha->fw_attributes, &ha->fw_memory_size,
  3185. ha->mpi_version, &ha->mpi_capabilities,
  3186. ha->phy_version);
  3187. if (rval != QLA_SUCCESS)
  3188. return rval;
  3189. if (ql2xmdenable) {
  3190. if (!ha->fw_dumped) {
  3191. if (fw_major_version != ha->fw_major_version ||
  3192. fw_minor_version != ha->fw_minor_version ||
  3193. fw_subminor_version != ha->fw_subminor_version) {
  3194. ql_log(ql_log_info, vha, 0xb02d,
  3195. "Firmware version differs "
  3196. "Previous version: %d:%d:%d - "
  3197. "New version: %d:%d:%d\n",
  3198. ha->fw_major_version,
  3199. ha->fw_minor_version,
  3200. ha->fw_subminor_version,
  3201. fw_major_version, fw_minor_version,
  3202. fw_subminor_version);
  3203. /* Release MiniDump resources */
  3204. qla82xx_md_free(vha);
  3205. /* ALlocate MiniDump resources */
  3206. qla82xx_md_prep(vha);
  3207. } else
  3208. ql_log(ql_log_info, vha, 0xb02e,
  3209. "Firmware dump available to retrieve\n",
  3210. vha->host_no);
  3211. }
  3212. }
  3213. return rval;
  3214. }
  3215. int
  3216. qla82xx_check_fw_alive(scsi_qla_host_t *vha)
  3217. {
  3218. uint32_t fw_heartbeat_counter;
  3219. int status = 0;
  3220. fw_heartbeat_counter = qla82xx_rd_32(vha->hw,
  3221. QLA82XX_PEG_ALIVE_COUNTER);
  3222. /* all 0xff, assume AER/EEH in progress, ignore */
  3223. if (fw_heartbeat_counter == 0xffffffff) {
  3224. ql_dbg(ql_dbg_timer, vha, 0x6003,
  3225. "FW heartbeat counter is 0xffffffff, "
  3226. "returning status=%d.\n", status);
  3227. return status;
  3228. }
  3229. if (vha->fw_heartbeat_counter == fw_heartbeat_counter) {
  3230. vha->seconds_since_last_heartbeat++;
  3231. /* FW not alive after 2 seconds */
  3232. if (vha->seconds_since_last_heartbeat == 2) {
  3233. vha->seconds_since_last_heartbeat = 0;
  3234. status = 1;
  3235. }
  3236. } else
  3237. vha->seconds_since_last_heartbeat = 0;
  3238. vha->fw_heartbeat_counter = fw_heartbeat_counter;
  3239. if (status)
  3240. ql_dbg(ql_dbg_timer, vha, 0x6004,
  3241. "Returning status=%d.\n", status);
  3242. return status;
  3243. }
  3244. /*
  3245. * qla82xx_device_state_handler
  3246. * Main state handler
  3247. *
  3248. * Note:
  3249. * IDC lock must be held upon entry
  3250. *
  3251. * Return:
  3252. * Success : 0
  3253. * Failed : 1
  3254. */
  3255. int
  3256. qla82xx_device_state_handler(scsi_qla_host_t *vha)
  3257. {
  3258. uint32_t dev_state;
  3259. uint32_t old_dev_state;
  3260. int rval = QLA_SUCCESS;
  3261. unsigned long dev_init_timeout;
  3262. struct qla_hw_data *ha = vha->hw;
  3263. int loopcount = 0;
  3264. qla82xx_idc_lock(ha);
  3265. if (!vha->flags.init_done)
  3266. qla82xx_set_drv_active(vha);
  3267. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3268. old_dev_state = dev_state;
  3269. ql_log(ql_log_info, vha, 0x009b,
  3270. "Device state is 0x%x = %s.\n",
  3271. dev_state,
  3272. dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
  3273. /* wait for 30 seconds for device to go ready */
  3274. dev_init_timeout = jiffies + (ha->nx_dev_init_timeout * HZ);
  3275. while (1) {
  3276. if (time_after_eq(jiffies, dev_init_timeout)) {
  3277. ql_log(ql_log_fatal, vha, 0x009c,
  3278. "Device init failed.\n");
  3279. rval = QLA_FUNCTION_FAILED;
  3280. break;
  3281. }
  3282. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3283. if (old_dev_state != dev_state) {
  3284. loopcount = 0;
  3285. old_dev_state = dev_state;
  3286. }
  3287. if (loopcount < 5) {
  3288. ql_log(ql_log_info, vha, 0x009d,
  3289. "Device state is 0x%x = %s.\n",
  3290. dev_state,
  3291. dev_state < MAX_STATES ? qdev_state(dev_state) :
  3292. "Unknown");
  3293. }
  3294. switch (dev_state) {
  3295. case QLA82XX_DEV_READY:
  3296. qla82xx_check_md_needed(vha);
  3297. ha->flags.isp82xx_reset_owner = 0;
  3298. goto exit;
  3299. case QLA82XX_DEV_COLD:
  3300. rval = qla82xx_device_bootstrap(vha);
  3301. break;
  3302. case QLA82XX_DEV_INITIALIZING:
  3303. qla82xx_idc_unlock(ha);
  3304. msleep(1000);
  3305. qla82xx_idc_lock(ha);
  3306. break;
  3307. case QLA82XX_DEV_NEED_RESET:
  3308. if (!ql2xdontresethba)
  3309. qla82xx_need_reset_handler(vha);
  3310. else {
  3311. qla82xx_idc_unlock(ha);
  3312. msleep(1000);
  3313. qla82xx_idc_lock(ha);
  3314. }
  3315. dev_init_timeout = jiffies +
  3316. (ha->nx_dev_init_timeout * HZ);
  3317. break;
  3318. case QLA82XX_DEV_NEED_QUIESCENT:
  3319. qla82xx_need_qsnt_handler(vha);
  3320. /* Reset timeout value after quiescence handler */
  3321. dev_init_timeout = jiffies + (ha->nx_dev_init_timeout\
  3322. * HZ);
  3323. break;
  3324. case QLA82XX_DEV_QUIESCENT:
  3325. /* Owner will exit and other will wait for the state
  3326. * to get changed
  3327. */
  3328. if (ha->flags.quiesce_owner)
  3329. goto exit;
  3330. qla82xx_idc_unlock(ha);
  3331. msleep(1000);
  3332. qla82xx_idc_lock(ha);
  3333. /* Reset timeout value after quiescence handler */
  3334. dev_init_timeout = jiffies + (ha->nx_dev_init_timeout\
  3335. * HZ);
  3336. break;
  3337. case QLA82XX_DEV_FAILED:
  3338. qla82xx_dev_failed_handler(vha);
  3339. rval = QLA_FUNCTION_FAILED;
  3340. goto exit;
  3341. default:
  3342. qla82xx_idc_unlock(ha);
  3343. msleep(1000);
  3344. qla82xx_idc_lock(ha);
  3345. }
  3346. loopcount++;
  3347. }
  3348. exit:
  3349. qla82xx_idc_unlock(ha);
  3350. return rval;
  3351. }
  3352. void qla82xx_watchdog(scsi_qla_host_t *vha)
  3353. {
  3354. uint32_t dev_state, halt_status;
  3355. struct qla_hw_data *ha = vha->hw;
  3356. /* don't poll if reset is going on */
  3357. if (!ha->flags.isp82xx_reset_hdlr_active) {
  3358. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3359. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  3360. !test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags)) {
  3361. ql_log(ql_log_warn, vha, 0x6001,
  3362. "Adapter reset needed.\n");
  3363. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  3364. qla2xxx_wake_dpc(vha);
  3365. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  3366. !test_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags)) {
  3367. ql_log(ql_log_warn, vha, 0x6002,
  3368. "Quiescent needed.\n");
  3369. set_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags);
  3370. qla2xxx_wake_dpc(vha);
  3371. } else {
  3372. if (qla82xx_check_fw_alive(vha)) {
  3373. halt_status = qla82xx_rd_32(ha,
  3374. QLA82XX_PEG_HALT_STATUS1);
  3375. ql_dbg(ql_dbg_timer, vha, 0x6005,
  3376. "dumping hw/fw registers:.\n "
  3377. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,.\n "
  3378. " PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,.\n "
  3379. " PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,.\n "
  3380. " PEG_NET_4_PC: 0x%x.\n", halt_status,
  3381. qla82xx_rd_32(ha, QLA82XX_PEG_HALT_STATUS2),
  3382. qla82xx_rd_32(ha,
  3383. QLA82XX_CRB_PEG_NET_0 + 0x3c),
  3384. qla82xx_rd_32(ha,
  3385. QLA82XX_CRB_PEG_NET_1 + 0x3c),
  3386. qla82xx_rd_32(ha,
  3387. QLA82XX_CRB_PEG_NET_2 + 0x3c),
  3388. qla82xx_rd_32(ha,
  3389. QLA82XX_CRB_PEG_NET_3 + 0x3c),
  3390. qla82xx_rd_32(ha,
  3391. QLA82XX_CRB_PEG_NET_4 + 0x3c));
  3392. if (halt_status & HALT_STATUS_UNRECOVERABLE) {
  3393. set_bit(ISP_UNRECOVERABLE,
  3394. &vha->dpc_flags);
  3395. } else {
  3396. ql_log(ql_log_info, vha, 0x6006,
  3397. "Detect abort needed.\n");
  3398. set_bit(ISP_ABORT_NEEDED,
  3399. &vha->dpc_flags);
  3400. }
  3401. qla2xxx_wake_dpc(vha);
  3402. ha->flags.isp82xx_fw_hung = 1;
  3403. if (ha->flags.mbox_busy) {
  3404. ha->flags.mbox_int = 1;
  3405. ql_log(ql_log_warn, vha, 0x6007,
  3406. "Due to FW hung, doing "
  3407. "premature completion of mbx "
  3408. "command.\n");
  3409. if (test_bit(MBX_INTR_WAIT,
  3410. &ha->mbx_cmd_flags))
  3411. complete(&ha->mbx_intr_comp);
  3412. }
  3413. }
  3414. }
  3415. }
  3416. }
  3417. int qla82xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
  3418. {
  3419. int rval;
  3420. rval = qla82xx_device_state_handler(vha);
  3421. return rval;
  3422. }
  3423. void
  3424. qla82xx_set_reset_owner(scsi_qla_host_t *vha)
  3425. {
  3426. struct qla_hw_data *ha = vha->hw;
  3427. uint32_t dev_state;
  3428. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3429. if (dev_state == QLA82XX_DEV_READY) {
  3430. ql_log(ql_log_info, vha, 0xb02f,
  3431. "HW State: NEED RESET\n");
  3432. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  3433. QLA82XX_DEV_NEED_RESET);
  3434. ha->flags.isp82xx_reset_owner = 1;
  3435. ql_dbg(ql_dbg_p3p, vha, 0xb030,
  3436. "reset_owner is 0x%x\n", ha->portnum);
  3437. } else
  3438. ql_log(ql_log_info, vha, 0xb031,
  3439. "Device state is 0x%x = %s.\n",
  3440. dev_state,
  3441. dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
  3442. }
  3443. /*
  3444. * qla82xx_abort_isp
  3445. * Resets ISP and aborts all outstanding commands.
  3446. *
  3447. * Input:
  3448. * ha = adapter block pointer.
  3449. *
  3450. * Returns:
  3451. * 0 = success
  3452. */
  3453. int
  3454. qla82xx_abort_isp(scsi_qla_host_t *vha)
  3455. {
  3456. int rval;
  3457. struct qla_hw_data *ha = vha->hw;
  3458. if (vha->device_flags & DFLG_DEV_FAILED) {
  3459. ql_log(ql_log_warn, vha, 0x8024,
  3460. "Device in failed state, exiting.\n");
  3461. return QLA_SUCCESS;
  3462. }
  3463. ha->flags.isp82xx_reset_hdlr_active = 1;
  3464. qla82xx_idc_lock(ha);
  3465. qla82xx_set_reset_owner(vha);
  3466. qla82xx_idc_unlock(ha);
  3467. rval = qla82xx_device_state_handler(vha);
  3468. qla82xx_idc_lock(ha);
  3469. qla82xx_clear_rst_ready(ha);
  3470. qla82xx_idc_unlock(ha);
  3471. if (rval == QLA_SUCCESS) {
  3472. ha->flags.isp82xx_fw_hung = 0;
  3473. ha->flags.isp82xx_reset_hdlr_active = 0;
  3474. qla82xx_restart_isp(vha);
  3475. }
  3476. if (rval) {
  3477. vha->flags.online = 1;
  3478. if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
  3479. if (ha->isp_abort_cnt == 0) {
  3480. ql_log(ql_log_warn, vha, 0x8027,
  3481. "ISP error recover failed - board "
  3482. "disabled.\n");
  3483. /*
  3484. * The next call disables the board
  3485. * completely.
  3486. */
  3487. ha->isp_ops->reset_adapter(vha);
  3488. vha->flags.online = 0;
  3489. clear_bit(ISP_ABORT_RETRY,
  3490. &vha->dpc_flags);
  3491. rval = QLA_SUCCESS;
  3492. } else { /* schedule another ISP abort */
  3493. ha->isp_abort_cnt--;
  3494. ql_log(ql_log_warn, vha, 0x8036,
  3495. "ISP abort - retry remaining %d.\n",
  3496. ha->isp_abort_cnt);
  3497. rval = QLA_FUNCTION_FAILED;
  3498. }
  3499. } else {
  3500. ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
  3501. ql_dbg(ql_dbg_taskm, vha, 0x8029,
  3502. "ISP error recovery - retrying (%d) more times.\n",
  3503. ha->isp_abort_cnt);
  3504. set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
  3505. rval = QLA_FUNCTION_FAILED;
  3506. }
  3507. }
  3508. return rval;
  3509. }
  3510. /*
  3511. * qla82xx_fcoe_ctx_reset
  3512. * Perform a quick reset and aborts all outstanding commands.
  3513. * This will only perform an FCoE context reset and avoids a full blown
  3514. * chip reset.
  3515. *
  3516. * Input:
  3517. * ha = adapter block pointer.
  3518. * is_reset_path = flag for identifying the reset path.
  3519. *
  3520. * Returns:
  3521. * 0 = success
  3522. */
  3523. int qla82xx_fcoe_ctx_reset(scsi_qla_host_t *vha)
  3524. {
  3525. int rval = QLA_FUNCTION_FAILED;
  3526. if (vha->flags.online) {
  3527. /* Abort all outstanding commands, so as to be requeued later */
  3528. qla2x00_abort_isp_cleanup(vha);
  3529. }
  3530. /* Stop currently executing firmware.
  3531. * This will destroy existing FCoE context at the F/W end.
  3532. */
  3533. qla2x00_try_to_stop_firmware(vha);
  3534. /* Restart. Creates a new FCoE context on INIT_FIRMWARE. */
  3535. rval = qla82xx_restart_isp(vha);
  3536. return rval;
  3537. }
  3538. /*
  3539. * qla2x00_wait_for_fcoe_ctx_reset
  3540. * Wait till the FCoE context is reset.
  3541. *
  3542. * Note:
  3543. * Does context switching here.
  3544. * Release SPIN_LOCK (if any) before calling this routine.
  3545. *
  3546. * Return:
  3547. * Success (fcoe_ctx reset is done) : 0
  3548. * Failed (fcoe_ctx reset not completed within max loop timout ) : 1
  3549. */
  3550. int qla2x00_wait_for_fcoe_ctx_reset(scsi_qla_host_t *vha)
  3551. {
  3552. int status = QLA_FUNCTION_FAILED;
  3553. unsigned long wait_reset;
  3554. wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  3555. while ((test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
  3556. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
  3557. && time_before(jiffies, wait_reset)) {
  3558. set_current_state(TASK_UNINTERRUPTIBLE);
  3559. schedule_timeout(HZ);
  3560. if (!test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) &&
  3561. !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
  3562. status = QLA_SUCCESS;
  3563. break;
  3564. }
  3565. }
  3566. ql_dbg(ql_dbg_p3p, vha, 0xb027,
  3567. "%s status=%d.\n", status);
  3568. return status;
  3569. }
  3570. void
  3571. qla82xx_chip_reset_cleanup(scsi_qla_host_t *vha)
  3572. {
  3573. int i;
  3574. unsigned long flags;
  3575. struct qla_hw_data *ha = vha->hw;
  3576. /* Check if 82XX firmware is alive or not
  3577. * We may have arrived here from NEED_RESET
  3578. * detection only
  3579. */
  3580. if (!ha->flags.isp82xx_fw_hung) {
  3581. for (i = 0; i < 2; i++) {
  3582. msleep(1000);
  3583. if (qla82xx_check_fw_alive(vha)) {
  3584. ha->flags.isp82xx_fw_hung = 1;
  3585. if (ha->flags.mbox_busy) {
  3586. ha->flags.mbox_int = 1;
  3587. complete(&ha->mbx_intr_comp);
  3588. }
  3589. break;
  3590. }
  3591. }
  3592. }
  3593. ql_dbg(ql_dbg_init, vha, 0x00b0,
  3594. "Entered %s fw_hung=%d.\n",
  3595. __func__, ha->flags.isp82xx_fw_hung);
  3596. /* Abort all commands gracefully if fw NOT hung */
  3597. if (!ha->flags.isp82xx_fw_hung) {
  3598. int cnt, que;
  3599. srb_t *sp;
  3600. struct req_que *req;
  3601. spin_lock_irqsave(&ha->hardware_lock, flags);
  3602. for (que = 0; que < ha->max_req_queues; que++) {
  3603. req = ha->req_q_map[que];
  3604. if (!req)
  3605. continue;
  3606. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  3607. sp = req->outstanding_cmds[cnt];
  3608. if (sp) {
  3609. if (!sp->ctx ||
  3610. (sp->flags & SRB_FCP_CMND_DMA_VALID)) {
  3611. spin_unlock_irqrestore(
  3612. &ha->hardware_lock, flags);
  3613. if (ha->isp_ops->abort_command(sp)) {
  3614. ql_log(ql_log_info, vha,
  3615. 0x00b1,
  3616. "mbx abort failed.\n");
  3617. } else {
  3618. ql_log(ql_log_info, vha,
  3619. 0x00b2,
  3620. "mbx abort success.\n");
  3621. }
  3622. spin_lock_irqsave(&ha->hardware_lock, flags);
  3623. }
  3624. }
  3625. }
  3626. }
  3627. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3628. /* Wait for pending cmds (physical and virtual) to complete */
  3629. if (!qla2x00_eh_wait_for_pending_commands(vha, 0, 0,
  3630. WAIT_HOST) == QLA_SUCCESS) {
  3631. ql_dbg(ql_dbg_init, vha, 0x00b3,
  3632. "Done wait for "
  3633. "pending commands.\n");
  3634. }
  3635. }
  3636. }
  3637. /* Minidump related functions */
  3638. int
  3639. qla82xx_md_rw_32(struct qla_hw_data *ha, uint32_t off, u32 data, uint8_t flag)
  3640. {
  3641. uint32_t off_value, rval = 0;
  3642. WRT_REG_DWORD((void *)(CRB_WINDOW_2M + ha->nx_pcibase),
  3643. (off & 0xFFFF0000));
  3644. /* Read back value to make sure write has gone through */
  3645. RD_REG_DWORD((void *)(CRB_WINDOW_2M + ha->nx_pcibase));
  3646. off_value = (off & 0x0000FFFF);
  3647. if (flag)
  3648. WRT_REG_DWORD((void *)
  3649. (off_value + CRB_INDIRECT_2M + ha->nx_pcibase),
  3650. data);
  3651. else
  3652. rval = RD_REG_DWORD((void *)
  3653. (off_value + CRB_INDIRECT_2M + ha->nx_pcibase));
  3654. return rval;
  3655. }
  3656. static int
  3657. qla82xx_minidump_process_control(scsi_qla_host_t *vha,
  3658. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3659. {
  3660. struct qla_hw_data *ha = vha->hw;
  3661. struct qla82xx_md_entry_crb *crb_entry;
  3662. uint32_t read_value, opcode, poll_time;
  3663. uint32_t addr, index, crb_addr;
  3664. unsigned long wtime;
  3665. struct qla82xx_md_template_hdr *tmplt_hdr;
  3666. uint32_t rval = QLA_SUCCESS;
  3667. int i;
  3668. tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
  3669. crb_entry = (struct qla82xx_md_entry_crb *)entry_hdr;
  3670. crb_addr = crb_entry->addr;
  3671. for (i = 0; i < crb_entry->op_count; i++) {
  3672. opcode = crb_entry->crb_ctrl.opcode;
  3673. if (opcode & QLA82XX_DBG_OPCODE_WR) {
  3674. qla82xx_md_rw_32(ha, crb_addr,
  3675. crb_entry->value_1, 1);
  3676. opcode &= ~QLA82XX_DBG_OPCODE_WR;
  3677. }
  3678. if (opcode & QLA82XX_DBG_OPCODE_RW) {
  3679. read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
  3680. qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
  3681. opcode &= ~QLA82XX_DBG_OPCODE_RW;
  3682. }
  3683. if (opcode & QLA82XX_DBG_OPCODE_AND) {
  3684. read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
  3685. read_value &= crb_entry->value_2;
  3686. opcode &= ~QLA82XX_DBG_OPCODE_AND;
  3687. if (opcode & QLA82XX_DBG_OPCODE_OR) {
  3688. read_value |= crb_entry->value_3;
  3689. opcode &= ~QLA82XX_DBG_OPCODE_OR;
  3690. }
  3691. qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
  3692. }
  3693. if (opcode & QLA82XX_DBG_OPCODE_OR) {
  3694. read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
  3695. read_value |= crb_entry->value_3;
  3696. qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
  3697. opcode &= ~QLA82XX_DBG_OPCODE_OR;
  3698. }
  3699. if (opcode & QLA82XX_DBG_OPCODE_POLL) {
  3700. poll_time = crb_entry->crb_strd.poll_timeout;
  3701. wtime = jiffies + poll_time;
  3702. read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
  3703. do {
  3704. if ((read_value & crb_entry->value_2)
  3705. == crb_entry->value_1)
  3706. break;
  3707. else if (time_after_eq(jiffies, wtime)) {
  3708. /* capturing dump failed */
  3709. rval = QLA_FUNCTION_FAILED;
  3710. break;
  3711. } else
  3712. read_value = qla82xx_md_rw_32(ha,
  3713. crb_addr, 0, 0);
  3714. } while (1);
  3715. opcode &= ~QLA82XX_DBG_OPCODE_POLL;
  3716. }
  3717. if (opcode & QLA82XX_DBG_OPCODE_RDSTATE) {
  3718. if (crb_entry->crb_strd.state_index_a) {
  3719. index = crb_entry->crb_strd.state_index_a;
  3720. addr = tmplt_hdr->saved_state_array[index];
  3721. } else
  3722. addr = crb_addr;
  3723. read_value = qla82xx_md_rw_32(ha, addr, 0, 0);
  3724. index = crb_entry->crb_ctrl.state_index_v;
  3725. tmplt_hdr->saved_state_array[index] = read_value;
  3726. opcode &= ~QLA82XX_DBG_OPCODE_RDSTATE;
  3727. }
  3728. if (opcode & QLA82XX_DBG_OPCODE_WRSTATE) {
  3729. if (crb_entry->crb_strd.state_index_a) {
  3730. index = crb_entry->crb_strd.state_index_a;
  3731. addr = tmplt_hdr->saved_state_array[index];
  3732. } else
  3733. addr = crb_addr;
  3734. if (crb_entry->crb_ctrl.state_index_v) {
  3735. index = crb_entry->crb_ctrl.state_index_v;
  3736. read_value =
  3737. tmplt_hdr->saved_state_array[index];
  3738. } else
  3739. read_value = crb_entry->value_1;
  3740. qla82xx_md_rw_32(ha, addr, read_value, 1);
  3741. opcode &= ~QLA82XX_DBG_OPCODE_WRSTATE;
  3742. }
  3743. if (opcode & QLA82XX_DBG_OPCODE_MDSTATE) {
  3744. index = crb_entry->crb_ctrl.state_index_v;
  3745. read_value = tmplt_hdr->saved_state_array[index];
  3746. read_value <<= crb_entry->crb_ctrl.shl;
  3747. read_value >>= crb_entry->crb_ctrl.shr;
  3748. if (crb_entry->value_2)
  3749. read_value &= crb_entry->value_2;
  3750. read_value |= crb_entry->value_3;
  3751. read_value += crb_entry->value_1;
  3752. tmplt_hdr->saved_state_array[index] = read_value;
  3753. opcode &= ~QLA82XX_DBG_OPCODE_MDSTATE;
  3754. }
  3755. crb_addr += crb_entry->crb_strd.addr_stride;
  3756. }
  3757. return rval;
  3758. }
  3759. static void
  3760. qla82xx_minidump_process_rdocm(scsi_qla_host_t *vha,
  3761. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3762. {
  3763. struct qla_hw_data *ha = vha->hw;
  3764. uint32_t r_addr, r_stride, loop_cnt, i, r_value;
  3765. struct qla82xx_md_entry_rdocm *ocm_hdr;
  3766. uint32_t *data_ptr = *d_ptr;
  3767. ocm_hdr = (struct qla82xx_md_entry_rdocm *)entry_hdr;
  3768. r_addr = ocm_hdr->read_addr;
  3769. r_stride = ocm_hdr->read_addr_stride;
  3770. loop_cnt = ocm_hdr->op_count;
  3771. for (i = 0; i < loop_cnt; i++) {
  3772. r_value = RD_REG_DWORD((void *)(r_addr + ha->nx_pcibase));
  3773. *data_ptr++ = cpu_to_le32(r_value);
  3774. r_addr += r_stride;
  3775. }
  3776. *d_ptr = data_ptr;
  3777. }
  3778. static void
  3779. qla82xx_minidump_process_rdmux(scsi_qla_host_t *vha,
  3780. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3781. {
  3782. struct qla_hw_data *ha = vha->hw;
  3783. uint32_t r_addr, s_stride, s_addr, s_value, loop_cnt, i, r_value;
  3784. struct qla82xx_md_entry_mux *mux_hdr;
  3785. uint32_t *data_ptr = *d_ptr;
  3786. mux_hdr = (struct qla82xx_md_entry_mux *)entry_hdr;
  3787. r_addr = mux_hdr->read_addr;
  3788. s_addr = mux_hdr->select_addr;
  3789. s_stride = mux_hdr->select_value_stride;
  3790. s_value = mux_hdr->select_value;
  3791. loop_cnt = mux_hdr->op_count;
  3792. for (i = 0; i < loop_cnt; i++) {
  3793. qla82xx_md_rw_32(ha, s_addr, s_value, 1);
  3794. r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
  3795. *data_ptr++ = cpu_to_le32(s_value);
  3796. *data_ptr++ = cpu_to_le32(r_value);
  3797. s_value += s_stride;
  3798. }
  3799. *d_ptr = data_ptr;
  3800. }
  3801. static void
  3802. qla82xx_minidump_process_rdcrb(scsi_qla_host_t *vha,
  3803. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3804. {
  3805. struct qla_hw_data *ha = vha->hw;
  3806. uint32_t r_addr, r_stride, loop_cnt, i, r_value;
  3807. struct qla82xx_md_entry_crb *crb_hdr;
  3808. uint32_t *data_ptr = *d_ptr;
  3809. crb_hdr = (struct qla82xx_md_entry_crb *)entry_hdr;
  3810. r_addr = crb_hdr->addr;
  3811. r_stride = crb_hdr->crb_strd.addr_stride;
  3812. loop_cnt = crb_hdr->op_count;
  3813. for (i = 0; i < loop_cnt; i++) {
  3814. r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
  3815. *data_ptr++ = cpu_to_le32(r_addr);
  3816. *data_ptr++ = cpu_to_le32(r_value);
  3817. r_addr += r_stride;
  3818. }
  3819. *d_ptr = data_ptr;
  3820. }
  3821. static int
  3822. qla82xx_minidump_process_l2tag(scsi_qla_host_t *vha,
  3823. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3824. {
  3825. struct qla_hw_data *ha = vha->hw;
  3826. uint32_t addr, r_addr, c_addr, t_r_addr;
  3827. uint32_t i, k, loop_count, t_value, r_cnt, r_value;
  3828. unsigned long p_wait, w_time, p_mask;
  3829. uint32_t c_value_w, c_value_r;
  3830. struct qla82xx_md_entry_cache *cache_hdr;
  3831. int rval = QLA_FUNCTION_FAILED;
  3832. uint32_t *data_ptr = *d_ptr;
  3833. cache_hdr = (struct qla82xx_md_entry_cache *)entry_hdr;
  3834. loop_count = cache_hdr->op_count;
  3835. r_addr = cache_hdr->read_addr;
  3836. c_addr = cache_hdr->control_addr;
  3837. c_value_w = cache_hdr->cache_ctrl.write_value;
  3838. t_r_addr = cache_hdr->tag_reg_addr;
  3839. t_value = cache_hdr->addr_ctrl.init_tag_value;
  3840. r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
  3841. p_wait = cache_hdr->cache_ctrl.poll_wait;
  3842. p_mask = cache_hdr->cache_ctrl.poll_mask;
  3843. for (i = 0; i < loop_count; i++) {
  3844. qla82xx_md_rw_32(ha, t_r_addr, t_value, 1);
  3845. if (c_value_w)
  3846. qla82xx_md_rw_32(ha, c_addr, c_value_w, 1);
  3847. if (p_mask) {
  3848. w_time = jiffies + p_wait;
  3849. do {
  3850. c_value_r = qla82xx_md_rw_32(ha, c_addr, 0, 0);
  3851. if ((c_value_r & p_mask) == 0)
  3852. break;
  3853. else if (time_after_eq(jiffies, w_time)) {
  3854. /* capturing dump failed */
  3855. ql_dbg(ql_dbg_p3p, vha, 0xb032,
  3856. "c_value_r: 0x%x, poll_mask: 0x%lx, "
  3857. "w_time: 0x%lx\n",
  3858. c_value_r, p_mask, w_time);
  3859. return rval;
  3860. }
  3861. } while (1);
  3862. }
  3863. addr = r_addr;
  3864. for (k = 0; k < r_cnt; k++) {
  3865. r_value = qla82xx_md_rw_32(ha, addr, 0, 0);
  3866. *data_ptr++ = cpu_to_le32(r_value);
  3867. addr += cache_hdr->read_ctrl.read_addr_stride;
  3868. }
  3869. t_value += cache_hdr->addr_ctrl.tag_value_stride;
  3870. }
  3871. *d_ptr = data_ptr;
  3872. return QLA_SUCCESS;
  3873. }
  3874. static void
  3875. qla82xx_minidump_process_l1cache(scsi_qla_host_t *vha,
  3876. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3877. {
  3878. struct qla_hw_data *ha = vha->hw;
  3879. uint32_t addr, r_addr, c_addr, t_r_addr;
  3880. uint32_t i, k, loop_count, t_value, r_cnt, r_value;
  3881. uint32_t c_value_w;
  3882. struct qla82xx_md_entry_cache *cache_hdr;
  3883. uint32_t *data_ptr = *d_ptr;
  3884. cache_hdr = (struct qla82xx_md_entry_cache *)entry_hdr;
  3885. loop_count = cache_hdr->op_count;
  3886. r_addr = cache_hdr->read_addr;
  3887. c_addr = cache_hdr->control_addr;
  3888. c_value_w = cache_hdr->cache_ctrl.write_value;
  3889. t_r_addr = cache_hdr->tag_reg_addr;
  3890. t_value = cache_hdr->addr_ctrl.init_tag_value;
  3891. r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
  3892. for (i = 0; i < loop_count; i++) {
  3893. qla82xx_md_rw_32(ha, t_r_addr, t_value, 1);
  3894. qla82xx_md_rw_32(ha, c_addr, c_value_w, 1);
  3895. addr = r_addr;
  3896. for (k = 0; k < r_cnt; k++) {
  3897. r_value = qla82xx_md_rw_32(ha, addr, 0, 0);
  3898. *data_ptr++ = cpu_to_le32(r_value);
  3899. addr += cache_hdr->read_ctrl.read_addr_stride;
  3900. }
  3901. t_value += cache_hdr->addr_ctrl.tag_value_stride;
  3902. }
  3903. *d_ptr = data_ptr;
  3904. }
  3905. static void
  3906. qla82xx_minidump_process_queue(scsi_qla_host_t *vha,
  3907. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3908. {
  3909. struct qla_hw_data *ha = vha->hw;
  3910. uint32_t s_addr, r_addr;
  3911. uint32_t r_stride, r_value, r_cnt, qid = 0;
  3912. uint32_t i, k, loop_cnt;
  3913. struct qla82xx_md_entry_queue *q_hdr;
  3914. uint32_t *data_ptr = *d_ptr;
  3915. q_hdr = (struct qla82xx_md_entry_queue *)entry_hdr;
  3916. s_addr = q_hdr->select_addr;
  3917. r_cnt = q_hdr->rd_strd.read_addr_cnt;
  3918. r_stride = q_hdr->rd_strd.read_addr_stride;
  3919. loop_cnt = q_hdr->op_count;
  3920. for (i = 0; i < loop_cnt; i++) {
  3921. qla82xx_md_rw_32(ha, s_addr, qid, 1);
  3922. r_addr = q_hdr->read_addr;
  3923. for (k = 0; k < r_cnt; k++) {
  3924. r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
  3925. *data_ptr++ = cpu_to_le32(r_value);
  3926. r_addr += r_stride;
  3927. }
  3928. qid += q_hdr->q_strd.queue_id_stride;
  3929. }
  3930. *d_ptr = data_ptr;
  3931. }
  3932. static void
  3933. qla82xx_minidump_process_rdrom(scsi_qla_host_t *vha,
  3934. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3935. {
  3936. struct qla_hw_data *ha = vha->hw;
  3937. uint32_t r_addr, r_value;
  3938. uint32_t i, loop_cnt;
  3939. struct qla82xx_md_entry_rdrom *rom_hdr;
  3940. uint32_t *data_ptr = *d_ptr;
  3941. rom_hdr = (struct qla82xx_md_entry_rdrom *)entry_hdr;
  3942. r_addr = rom_hdr->read_addr;
  3943. loop_cnt = rom_hdr->read_data_size/sizeof(uint32_t);
  3944. for (i = 0; i < loop_cnt; i++) {
  3945. qla82xx_md_rw_32(ha, MD_DIRECT_ROM_WINDOW,
  3946. (r_addr & 0xFFFF0000), 1);
  3947. r_value = qla82xx_md_rw_32(ha,
  3948. MD_DIRECT_ROM_READ_BASE +
  3949. (r_addr & 0x0000FFFF), 0, 0);
  3950. *data_ptr++ = cpu_to_le32(r_value);
  3951. r_addr += sizeof(uint32_t);
  3952. }
  3953. *d_ptr = data_ptr;
  3954. }
  3955. static int
  3956. qla82xx_minidump_process_rdmem(scsi_qla_host_t *vha,
  3957. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3958. {
  3959. struct qla_hw_data *ha = vha->hw;
  3960. uint32_t r_addr, r_value, r_data;
  3961. uint32_t i, j, loop_cnt;
  3962. struct qla82xx_md_entry_rdmem *m_hdr;
  3963. unsigned long flags;
  3964. int rval = QLA_FUNCTION_FAILED;
  3965. uint32_t *data_ptr = *d_ptr;
  3966. m_hdr = (struct qla82xx_md_entry_rdmem *)entry_hdr;
  3967. r_addr = m_hdr->read_addr;
  3968. loop_cnt = m_hdr->read_data_size/16;
  3969. if (r_addr & 0xf) {
  3970. ql_log(ql_log_warn, vha, 0xb033,
  3971. "Read addr 0x%x not 16 bytes alligned\n", r_addr);
  3972. return rval;
  3973. }
  3974. if (m_hdr->read_data_size % 16) {
  3975. ql_log(ql_log_warn, vha, 0xb034,
  3976. "Read data[0x%x] not multiple of 16 bytes\n",
  3977. m_hdr->read_data_size);
  3978. return rval;
  3979. }
  3980. ql_dbg(ql_dbg_p3p, vha, 0xb035,
  3981. "[%s]: rdmem_addr: 0x%x, read_data_size: 0x%x, loop_cnt: 0x%x\n",
  3982. __func__, r_addr, m_hdr->read_data_size, loop_cnt);
  3983. write_lock_irqsave(&ha->hw_lock, flags);
  3984. for (i = 0; i < loop_cnt; i++) {
  3985. qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_LO, r_addr, 1);
  3986. r_value = 0;
  3987. qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_HI, r_value, 1);
  3988. r_value = MIU_TA_CTL_ENABLE;
  3989. qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
  3990. r_value = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE;
  3991. qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
  3992. for (j = 0; j < MAX_CTL_CHECK; j++) {
  3993. r_value = qla82xx_md_rw_32(ha,
  3994. MD_MIU_TEST_AGT_CTRL, 0, 0);
  3995. if ((r_value & MIU_TA_CTL_BUSY) == 0)
  3996. break;
  3997. }
  3998. if (j >= MAX_CTL_CHECK) {
  3999. printk_ratelimited(KERN_ERR
  4000. "failed to read through agent\n");
  4001. write_unlock_irqrestore(&ha->hw_lock, flags);
  4002. return rval;
  4003. }
  4004. for (j = 0; j < 4; j++) {
  4005. r_data = qla82xx_md_rw_32(ha,
  4006. MD_MIU_TEST_AGT_RDDATA[j], 0, 0);
  4007. *data_ptr++ = cpu_to_le32(r_data);
  4008. }
  4009. r_addr += 16;
  4010. }
  4011. write_unlock_irqrestore(&ha->hw_lock, flags);
  4012. *d_ptr = data_ptr;
  4013. return QLA_SUCCESS;
  4014. }
  4015. static int
  4016. qla82xx_validate_template_chksum(scsi_qla_host_t *vha)
  4017. {
  4018. struct qla_hw_data *ha = vha->hw;
  4019. uint64_t chksum = 0;
  4020. uint32_t *d_ptr = (uint32_t *)ha->md_tmplt_hdr;
  4021. int count = ha->md_template_size/sizeof(uint32_t);
  4022. while (count-- > 0)
  4023. chksum += *d_ptr++;
  4024. while (chksum >> 32)
  4025. chksum = (chksum & 0xFFFFFFFF) + (chksum >> 32);
  4026. return ~chksum;
  4027. }
  4028. static void
  4029. qla82xx_mark_entry_skipped(scsi_qla_host_t *vha,
  4030. qla82xx_md_entry_hdr_t *entry_hdr, int index)
  4031. {
  4032. entry_hdr->d_ctrl.driver_flags |= QLA82XX_DBG_SKIPPED_FLAG;
  4033. ql_dbg(ql_dbg_p3p, vha, 0xb036,
  4034. "Skipping entry[%d]: "
  4035. "ETYPE[0x%x]-ELEVEL[0x%x]\n",
  4036. index, entry_hdr->entry_type,
  4037. entry_hdr->d_ctrl.entry_capture_mask);
  4038. }
  4039. int
  4040. qla82xx_md_collect(scsi_qla_host_t *vha)
  4041. {
  4042. struct qla_hw_data *ha = vha->hw;
  4043. int no_entry_hdr = 0;
  4044. qla82xx_md_entry_hdr_t *entry_hdr;
  4045. struct qla82xx_md_template_hdr *tmplt_hdr;
  4046. uint32_t *data_ptr;
  4047. uint32_t total_data_size = 0, f_capture_mask, data_collected = 0;
  4048. int i = 0, rval = QLA_FUNCTION_FAILED;
  4049. tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
  4050. data_ptr = (uint32_t *)ha->md_dump;
  4051. if (ha->fw_dumped) {
  4052. ql_log(ql_log_info, vha, 0xb037,
  4053. "Firmware dump available to retrive\n");
  4054. goto md_failed;
  4055. }
  4056. ha->fw_dumped = 0;
  4057. if (!ha->md_tmplt_hdr || !ha->md_dump) {
  4058. ql_log(ql_log_warn, vha, 0xb038,
  4059. "Memory not allocated for minidump capture\n");
  4060. goto md_failed;
  4061. }
  4062. if (qla82xx_validate_template_chksum(vha)) {
  4063. ql_log(ql_log_info, vha, 0xb039,
  4064. "Template checksum validation error\n");
  4065. goto md_failed;
  4066. }
  4067. no_entry_hdr = tmplt_hdr->num_of_entries;
  4068. ql_dbg(ql_dbg_p3p, vha, 0xb03a,
  4069. "No of entry headers in Template: 0x%x\n", no_entry_hdr);
  4070. ql_dbg(ql_dbg_p3p, vha, 0xb03b,
  4071. "Capture Mask obtained: 0x%x\n", tmplt_hdr->capture_debug_level);
  4072. f_capture_mask = tmplt_hdr->capture_debug_level & 0xFF;
  4073. /* Validate whether required debug level is set */
  4074. if ((f_capture_mask & 0x3) != 0x3) {
  4075. ql_log(ql_log_warn, vha, 0xb03c,
  4076. "Minimum required capture mask[0x%x] level not set\n",
  4077. f_capture_mask);
  4078. goto md_failed;
  4079. }
  4080. tmplt_hdr->driver_capture_mask = ql2xmdcapmask;
  4081. tmplt_hdr->driver_info[0] = vha->host_no;
  4082. tmplt_hdr->driver_info[1] = (QLA_DRIVER_MAJOR_VER << 24) |
  4083. (QLA_DRIVER_MINOR_VER << 16) | (QLA_DRIVER_PATCH_VER << 8) |
  4084. QLA_DRIVER_BETA_VER;
  4085. total_data_size = ha->md_dump_size;
  4086. ql_dbg(ql_log_info, vha, 0xb03d,
  4087. "Total minidump data_size 0x%x to be captured\n", total_data_size);
  4088. /* Check whether template obtained is valid */
  4089. if (tmplt_hdr->entry_type != QLA82XX_TLHDR) {
  4090. ql_log(ql_log_warn, vha, 0xb04e,
  4091. "Bad template header entry type: 0x%x obtained\n",
  4092. tmplt_hdr->entry_type);
  4093. goto md_failed;
  4094. }
  4095. entry_hdr = (qla82xx_md_entry_hdr_t *) \
  4096. (((uint8_t *)ha->md_tmplt_hdr) + tmplt_hdr->first_entry_offset);
  4097. /* Walk through the entry headers */
  4098. for (i = 0; i < no_entry_hdr; i++) {
  4099. if (data_collected > total_data_size) {
  4100. ql_log(ql_log_warn, vha, 0xb03e,
  4101. "More MiniDump data collected: [0x%x]\n",
  4102. data_collected);
  4103. goto md_failed;
  4104. }
  4105. if (!(entry_hdr->d_ctrl.entry_capture_mask &
  4106. ql2xmdcapmask)) {
  4107. entry_hdr->d_ctrl.driver_flags |=
  4108. QLA82XX_DBG_SKIPPED_FLAG;
  4109. ql_dbg(ql_dbg_p3p, vha, 0xb03f,
  4110. "Skipping entry[%d]: "
  4111. "ETYPE[0x%x]-ELEVEL[0x%x]\n",
  4112. i, entry_hdr->entry_type,
  4113. entry_hdr->d_ctrl.entry_capture_mask);
  4114. goto skip_nxt_entry;
  4115. }
  4116. ql_dbg(ql_dbg_p3p, vha, 0xb040,
  4117. "[%s]: data ptr[%d]: %p, entry_hdr: %p\n"
  4118. "entry_type: 0x%x, captrue_mask: 0x%x\n",
  4119. __func__, i, data_ptr, entry_hdr,
  4120. entry_hdr->entry_type,
  4121. entry_hdr->d_ctrl.entry_capture_mask);
  4122. ql_dbg(ql_dbg_p3p, vha, 0xb041,
  4123. "Data collected: [0x%x], Dump size left:[0x%x]\n",
  4124. data_collected, (ha->md_dump_size - data_collected));
  4125. /* Decode the entry type and take
  4126. * required action to capture debug data */
  4127. switch (entry_hdr->entry_type) {
  4128. case QLA82XX_RDEND:
  4129. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4130. break;
  4131. case QLA82XX_CNTRL:
  4132. rval = qla82xx_minidump_process_control(vha,
  4133. entry_hdr, &data_ptr);
  4134. if (rval != QLA_SUCCESS) {
  4135. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4136. goto md_failed;
  4137. }
  4138. break;
  4139. case QLA82XX_RDCRB:
  4140. qla82xx_minidump_process_rdcrb(vha,
  4141. entry_hdr, &data_ptr);
  4142. break;
  4143. case QLA82XX_RDMEM:
  4144. rval = qla82xx_minidump_process_rdmem(vha,
  4145. entry_hdr, &data_ptr);
  4146. if (rval != QLA_SUCCESS) {
  4147. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4148. goto md_failed;
  4149. }
  4150. break;
  4151. case QLA82XX_BOARD:
  4152. case QLA82XX_RDROM:
  4153. qla82xx_minidump_process_rdrom(vha,
  4154. entry_hdr, &data_ptr);
  4155. break;
  4156. case QLA82XX_L2DTG:
  4157. case QLA82XX_L2ITG:
  4158. case QLA82XX_L2DAT:
  4159. case QLA82XX_L2INS:
  4160. rval = qla82xx_minidump_process_l2tag(vha,
  4161. entry_hdr, &data_ptr);
  4162. if (rval != QLA_SUCCESS) {
  4163. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4164. goto md_failed;
  4165. }
  4166. break;
  4167. case QLA82XX_L1DAT:
  4168. case QLA82XX_L1INS:
  4169. qla82xx_minidump_process_l1cache(vha,
  4170. entry_hdr, &data_ptr);
  4171. break;
  4172. case QLA82XX_RDOCM:
  4173. qla82xx_minidump_process_rdocm(vha,
  4174. entry_hdr, &data_ptr);
  4175. break;
  4176. case QLA82XX_RDMUX:
  4177. qla82xx_minidump_process_rdmux(vha,
  4178. entry_hdr, &data_ptr);
  4179. break;
  4180. case QLA82XX_QUEUE:
  4181. qla82xx_minidump_process_queue(vha,
  4182. entry_hdr, &data_ptr);
  4183. break;
  4184. case QLA82XX_RDNOP:
  4185. default:
  4186. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4187. break;
  4188. }
  4189. ql_dbg(ql_dbg_p3p, vha, 0xb042,
  4190. "[%s]: data ptr[%d]: %p\n", __func__, i, data_ptr);
  4191. data_collected = (uint8_t *)data_ptr -
  4192. (uint8_t *)ha->md_dump;
  4193. skip_nxt_entry:
  4194. entry_hdr = (qla82xx_md_entry_hdr_t *) \
  4195. (((uint8_t *)entry_hdr) + entry_hdr->entry_size);
  4196. }
  4197. if (data_collected != total_data_size) {
  4198. ql_dbg(ql_log_warn, vha, 0xb043,
  4199. "MiniDump data mismatch: Data collected: [0x%x],"
  4200. "total_data_size:[0x%x]\n",
  4201. data_collected, total_data_size);
  4202. goto md_failed;
  4203. }
  4204. ql_log(ql_log_info, vha, 0xb044,
  4205. "Firmware dump saved to temp buffer (%ld/%p %ld/%p).\n",
  4206. vha->host_no, ha->md_tmplt_hdr, vha->host_no, ha->md_dump);
  4207. ha->fw_dumped = 1;
  4208. qla2x00_post_uevent_work(vha, QLA_UEVENT_CODE_FW_DUMP);
  4209. md_failed:
  4210. return rval;
  4211. }
  4212. int
  4213. qla82xx_md_alloc(scsi_qla_host_t *vha)
  4214. {
  4215. struct qla_hw_data *ha = vha->hw;
  4216. int i, k;
  4217. struct qla82xx_md_template_hdr *tmplt_hdr;
  4218. tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
  4219. if (ql2xmdcapmask < 0x3 || ql2xmdcapmask > 0x7F) {
  4220. ql2xmdcapmask = tmplt_hdr->capture_debug_level & 0xFF;
  4221. ql_log(ql_log_info, vha, 0xb045,
  4222. "Forcing driver capture mask to firmware default capture mask: 0x%x.\n",
  4223. ql2xmdcapmask);
  4224. }
  4225. for (i = 0x2, k = 1; (i & QLA82XX_DEFAULT_CAP_MASK); i <<= 1, k++) {
  4226. if (i & ql2xmdcapmask)
  4227. ha->md_dump_size += tmplt_hdr->capture_size_array[k];
  4228. }
  4229. if (ha->md_dump) {
  4230. ql_log(ql_log_warn, vha, 0xb046,
  4231. "Firmware dump previously allocated.\n");
  4232. return 1;
  4233. }
  4234. ha->md_dump = vmalloc(ha->md_dump_size);
  4235. if (ha->md_dump == NULL) {
  4236. ql_log(ql_log_warn, vha, 0xb047,
  4237. "Unable to allocate memory for Minidump size "
  4238. "(0x%x).\n", ha->md_dump_size);
  4239. return 1;
  4240. }
  4241. return 0;
  4242. }
  4243. void
  4244. qla82xx_md_free(scsi_qla_host_t *vha)
  4245. {
  4246. struct qla_hw_data *ha = vha->hw;
  4247. /* Release the template header allocated */
  4248. if (ha->md_tmplt_hdr) {
  4249. ql_log(ql_log_info, vha, 0xb048,
  4250. "Free MiniDump template: %p, size (%d KB)\n",
  4251. ha->md_tmplt_hdr, ha->md_template_size / 1024);
  4252. dma_free_coherent(&ha->pdev->dev, ha->md_template_size,
  4253. ha->md_tmplt_hdr, ha->md_tmplt_hdr_dma);
  4254. ha->md_tmplt_hdr = 0;
  4255. }
  4256. /* Release the template data buffer allocated */
  4257. if (ha->md_dump) {
  4258. ql_log(ql_log_info, vha, 0xb049,
  4259. "Free MiniDump memory: %p, size (%d KB)\n",
  4260. ha->md_dump, ha->md_dump_size / 1024);
  4261. vfree(ha->md_dump);
  4262. ha->md_dump_size = 0;
  4263. ha->md_dump = 0;
  4264. }
  4265. }
  4266. void
  4267. qla82xx_md_prep(scsi_qla_host_t *vha)
  4268. {
  4269. struct qla_hw_data *ha = vha->hw;
  4270. int rval;
  4271. /* Get Minidump template size */
  4272. rval = qla82xx_md_get_template_size(vha);
  4273. if (rval == QLA_SUCCESS) {
  4274. ql_log(ql_log_info, vha, 0xb04a,
  4275. "MiniDump Template size obtained (%d KB)\n",
  4276. ha->md_template_size / 1024);
  4277. /* Get Minidump template */
  4278. rval = qla82xx_md_get_template(vha);
  4279. if (rval == QLA_SUCCESS) {
  4280. ql_dbg(ql_dbg_p3p, vha, 0xb04b,
  4281. "MiniDump Template obtained\n");
  4282. /* Allocate memory for minidump */
  4283. rval = qla82xx_md_alloc(vha);
  4284. if (rval == QLA_SUCCESS)
  4285. ql_log(ql_log_info, vha, 0xb04c,
  4286. "MiniDump memory allocated (%d KB)\n",
  4287. ha->md_dump_size / 1024);
  4288. else {
  4289. ql_log(ql_log_info, vha, 0xb04d,
  4290. "Free MiniDump template: %p, size: (%d KB)\n",
  4291. ha->md_tmplt_hdr,
  4292. ha->md_template_size / 1024);
  4293. dma_free_coherent(&ha->pdev->dev,
  4294. ha->md_template_size,
  4295. ha->md_tmplt_hdr, ha->md_tmplt_hdr_dma);
  4296. ha->md_tmplt_hdr = 0;
  4297. }
  4298. }
  4299. }
  4300. }
  4301. int
  4302. qla82xx_beacon_on(struct scsi_qla_host *vha)
  4303. {
  4304. int rval;
  4305. struct qla_hw_data *ha = vha->hw;
  4306. qla82xx_idc_lock(ha);
  4307. rval = qla82xx_mbx_beacon_ctl(vha, 1);
  4308. if (rval) {
  4309. ql_log(ql_log_warn, vha, 0xb050,
  4310. "mbx set led config failed in %s\n", __func__);
  4311. goto exit;
  4312. }
  4313. ha->beacon_blink_led = 1;
  4314. exit:
  4315. qla82xx_idc_unlock(ha);
  4316. return rval;
  4317. }
  4318. int
  4319. qla82xx_beacon_off(struct scsi_qla_host *vha)
  4320. {
  4321. int rval;
  4322. struct qla_hw_data *ha = vha->hw;
  4323. qla82xx_idc_lock(ha);
  4324. rval = qla82xx_mbx_beacon_ctl(vha, 0);
  4325. if (rval) {
  4326. ql_log(ql_log_warn, vha, 0xb051,
  4327. "mbx set led config failed in %s\n", __func__);
  4328. goto exit;
  4329. }
  4330. ha->beacon_blink_led = 0;
  4331. exit:
  4332. qla82xx_idc_unlock(ha);
  4333. return rval;
  4334. }