qla_nx.c 116 KB

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