qla_nx.c 115 KB

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