qla_nx.c 116 KB

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