qla_sup.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QLogic Fibre Channel HBA Driver
  4. * Copyright (c) 2003-2014 QLogic Corporation
  5. */
  6. #include "qla_def.h"
  7. #include <linux/delay.h>
  8. #include <linux/slab.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/uaccess.h>
  11. /*
  12. * NVRAM support routines
  13. */
  14. /**
  15. * qla2x00_lock_nvram_access() -
  16. * @ha: HA context
  17. */
  18. static void
  19. qla2x00_lock_nvram_access(struct qla_hw_data *ha)
  20. {
  21. uint16_t data;
  22. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  23. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  24. data = rd_reg_word(&reg->nvram);
  25. while (data & NVR_BUSY) {
  26. udelay(100);
  27. data = rd_reg_word(&reg->nvram);
  28. }
  29. /* Lock resource */
  30. wrt_reg_word(&reg->u.isp2300.host_semaphore, 0x1);
  31. rd_reg_word(&reg->u.isp2300.host_semaphore);
  32. udelay(5);
  33. data = rd_reg_word(&reg->u.isp2300.host_semaphore);
  34. while ((data & BIT_0) == 0) {
  35. /* Lock failed */
  36. udelay(100);
  37. wrt_reg_word(&reg->u.isp2300.host_semaphore, 0x1);
  38. rd_reg_word(&reg->u.isp2300.host_semaphore);
  39. udelay(5);
  40. data = rd_reg_word(&reg->u.isp2300.host_semaphore);
  41. }
  42. }
  43. }
  44. /**
  45. * qla2x00_unlock_nvram_access() -
  46. * @ha: HA context
  47. */
  48. static void
  49. qla2x00_unlock_nvram_access(struct qla_hw_data *ha)
  50. {
  51. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  52. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  53. wrt_reg_word(&reg->u.isp2300.host_semaphore, 0);
  54. rd_reg_word(&reg->u.isp2300.host_semaphore);
  55. }
  56. }
  57. /**
  58. * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
  59. * @ha: HA context
  60. * @data: Serial interface selector
  61. */
  62. static void
  63. qla2x00_nv_write(struct qla_hw_data *ha, uint16_t data)
  64. {
  65. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  66. wrt_reg_word(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  67. rd_reg_word(&reg->nvram); /* PCI Posting. */
  68. NVRAM_DELAY();
  69. wrt_reg_word(&reg->nvram, data | NVR_SELECT | NVR_CLOCK |
  70. NVR_WRT_ENABLE);
  71. rd_reg_word(&reg->nvram); /* PCI Posting. */
  72. NVRAM_DELAY();
  73. wrt_reg_word(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  74. rd_reg_word(&reg->nvram); /* PCI Posting. */
  75. NVRAM_DELAY();
  76. }
  77. /**
  78. * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
  79. * NVRAM.
  80. * @ha: HA context
  81. * @nv_cmd: NVRAM command
  82. *
  83. * Bit definitions for NVRAM command:
  84. *
  85. * Bit 26 = start bit
  86. * Bit 25, 24 = opcode
  87. * Bit 23-16 = address
  88. * Bit 15-0 = write data
  89. *
  90. * Returns the word read from nvram @addr.
  91. */
  92. static uint16_t
  93. qla2x00_nvram_request(struct qla_hw_data *ha, uint32_t nv_cmd)
  94. {
  95. uint8_t cnt;
  96. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  97. uint16_t data = 0;
  98. uint16_t reg_data;
  99. /* Send command to NVRAM. */
  100. nv_cmd <<= 5;
  101. for (cnt = 0; cnt < 11; cnt++) {
  102. if (nv_cmd & BIT_31)
  103. qla2x00_nv_write(ha, NVR_DATA_OUT);
  104. else
  105. qla2x00_nv_write(ha, 0);
  106. nv_cmd <<= 1;
  107. }
  108. /* Read data from NVRAM. */
  109. for (cnt = 0; cnt < 16; cnt++) {
  110. wrt_reg_word(&reg->nvram, NVR_SELECT | NVR_CLOCK);
  111. rd_reg_word(&reg->nvram); /* PCI Posting. */
  112. NVRAM_DELAY();
  113. data <<= 1;
  114. reg_data = rd_reg_word(&reg->nvram);
  115. if (reg_data & NVR_DATA_IN)
  116. data |= BIT_0;
  117. wrt_reg_word(&reg->nvram, NVR_SELECT);
  118. rd_reg_word(&reg->nvram); /* PCI Posting. */
  119. NVRAM_DELAY();
  120. }
  121. /* Deselect chip. */
  122. wrt_reg_word(&reg->nvram, NVR_DESELECT);
  123. rd_reg_word(&reg->nvram); /* PCI Posting. */
  124. NVRAM_DELAY();
  125. return data;
  126. }
  127. /**
  128. * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
  129. * request routine to get the word from NVRAM.
  130. * @ha: HA context
  131. * @addr: Address in NVRAM to read
  132. *
  133. * Returns the word read from nvram @addr.
  134. */
  135. static uint16_t
  136. qla2x00_get_nvram_word(struct qla_hw_data *ha, uint32_t addr)
  137. {
  138. uint16_t data;
  139. uint32_t nv_cmd;
  140. nv_cmd = addr << 16;
  141. nv_cmd |= NV_READ_OP;
  142. data = qla2x00_nvram_request(ha, nv_cmd);
  143. return (data);
  144. }
  145. /**
  146. * qla2x00_nv_deselect() - Deselect NVRAM operations.
  147. * @ha: HA context
  148. */
  149. static void
  150. qla2x00_nv_deselect(struct qla_hw_data *ha)
  151. {
  152. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  153. wrt_reg_word(&reg->nvram, NVR_DESELECT);
  154. rd_reg_word(&reg->nvram); /* PCI Posting. */
  155. NVRAM_DELAY();
  156. }
  157. /**
  158. * qla2x00_write_nvram_word() - Write NVRAM data.
  159. * @ha: HA context
  160. * @addr: Address in NVRAM to write
  161. * @data: word to program
  162. */
  163. static void
  164. qla2x00_write_nvram_word(struct qla_hw_data *ha, uint32_t addr, __le16 data)
  165. {
  166. int count;
  167. uint16_t word;
  168. uint32_t nv_cmd, wait_cnt;
  169. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  170. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  171. qla2x00_nv_write(ha, NVR_DATA_OUT);
  172. qla2x00_nv_write(ha, 0);
  173. qla2x00_nv_write(ha, 0);
  174. for (word = 0; word < 8; word++)
  175. qla2x00_nv_write(ha, NVR_DATA_OUT);
  176. qla2x00_nv_deselect(ha);
  177. /* Write data */
  178. nv_cmd = (addr << 16) | NV_WRITE_OP;
  179. nv_cmd |= (__force u16)data;
  180. nv_cmd <<= 5;
  181. for (count = 0; count < 27; count++) {
  182. if (nv_cmd & BIT_31)
  183. qla2x00_nv_write(ha, NVR_DATA_OUT);
  184. else
  185. qla2x00_nv_write(ha, 0);
  186. nv_cmd <<= 1;
  187. }
  188. qla2x00_nv_deselect(ha);
  189. /* Wait for NVRAM to become ready */
  190. wrt_reg_word(&reg->nvram, NVR_SELECT);
  191. rd_reg_word(&reg->nvram); /* PCI Posting. */
  192. wait_cnt = NVR_WAIT_CNT;
  193. do {
  194. if (!--wait_cnt) {
  195. ql_dbg(ql_dbg_user, vha, 0x708d,
  196. "NVRAM didn't go ready...\n");
  197. break;
  198. }
  199. NVRAM_DELAY();
  200. word = rd_reg_word(&reg->nvram);
  201. } while ((word & NVR_DATA_IN) == 0);
  202. qla2x00_nv_deselect(ha);
  203. /* Disable writes */
  204. qla2x00_nv_write(ha, NVR_DATA_OUT);
  205. for (count = 0; count < 10; count++)
  206. qla2x00_nv_write(ha, 0);
  207. qla2x00_nv_deselect(ha);
  208. }
  209. static int
  210. qla2x00_write_nvram_word_tmo(struct qla_hw_data *ha, uint32_t addr,
  211. __le16 data, uint32_t tmo)
  212. {
  213. int ret, count;
  214. uint16_t word;
  215. uint32_t nv_cmd;
  216. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  217. ret = QLA_SUCCESS;
  218. qla2x00_nv_write(ha, NVR_DATA_OUT);
  219. qla2x00_nv_write(ha, 0);
  220. qla2x00_nv_write(ha, 0);
  221. for (word = 0; word < 8; word++)
  222. qla2x00_nv_write(ha, NVR_DATA_OUT);
  223. qla2x00_nv_deselect(ha);
  224. /* Write data */
  225. nv_cmd = (addr << 16) | NV_WRITE_OP;
  226. nv_cmd |= (__force u16)data;
  227. nv_cmd <<= 5;
  228. for (count = 0; count < 27; count++) {
  229. if (nv_cmd & BIT_31)
  230. qla2x00_nv_write(ha, NVR_DATA_OUT);
  231. else
  232. qla2x00_nv_write(ha, 0);
  233. nv_cmd <<= 1;
  234. }
  235. qla2x00_nv_deselect(ha);
  236. /* Wait for NVRAM to become ready */
  237. wrt_reg_word(&reg->nvram, NVR_SELECT);
  238. rd_reg_word(&reg->nvram); /* PCI Posting. */
  239. do {
  240. NVRAM_DELAY();
  241. word = rd_reg_word(&reg->nvram);
  242. if (!--tmo) {
  243. ret = QLA_FUNCTION_FAILED;
  244. break;
  245. }
  246. } while ((word & NVR_DATA_IN) == 0);
  247. qla2x00_nv_deselect(ha);
  248. /* Disable writes */
  249. qla2x00_nv_write(ha, NVR_DATA_OUT);
  250. for (count = 0; count < 10; count++)
  251. qla2x00_nv_write(ha, 0);
  252. qla2x00_nv_deselect(ha);
  253. return ret;
  254. }
  255. /**
  256. * qla2x00_clear_nvram_protection() -
  257. * @ha: HA context
  258. */
  259. static int
  260. qla2x00_clear_nvram_protection(struct qla_hw_data *ha)
  261. {
  262. int ret, stat;
  263. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  264. uint32_t word, wait_cnt;
  265. __le16 wprot, wprot_old;
  266. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  267. /* Clear NVRAM write protection. */
  268. ret = QLA_FUNCTION_FAILED;
  269. wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  270. stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
  271. cpu_to_le16(0x1234), 100000);
  272. wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  273. if (stat != QLA_SUCCESS || wprot != cpu_to_le16(0x1234)) {
  274. /* Write enable. */
  275. qla2x00_nv_write(ha, NVR_DATA_OUT);
  276. qla2x00_nv_write(ha, 0);
  277. qla2x00_nv_write(ha, 0);
  278. for (word = 0; word < 8; word++)
  279. qla2x00_nv_write(ha, NVR_DATA_OUT);
  280. qla2x00_nv_deselect(ha);
  281. /* Enable protection register. */
  282. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  283. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  284. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  285. for (word = 0; word < 8; word++)
  286. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  287. qla2x00_nv_deselect(ha);
  288. /* Clear protection register (ffff is cleared). */
  289. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  290. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  291. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  292. for (word = 0; word < 8; word++)
  293. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  294. qla2x00_nv_deselect(ha);
  295. /* Wait for NVRAM to become ready. */
  296. wrt_reg_word(&reg->nvram, NVR_SELECT);
  297. rd_reg_word(&reg->nvram); /* PCI Posting. */
  298. wait_cnt = NVR_WAIT_CNT;
  299. do {
  300. if (!--wait_cnt) {
  301. ql_dbg(ql_dbg_user, vha, 0x708e,
  302. "NVRAM didn't go ready...\n");
  303. break;
  304. }
  305. NVRAM_DELAY();
  306. word = rd_reg_word(&reg->nvram);
  307. } while ((word & NVR_DATA_IN) == 0);
  308. if (wait_cnt)
  309. ret = QLA_SUCCESS;
  310. } else
  311. qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
  312. return ret;
  313. }
  314. static void
  315. qla2x00_set_nvram_protection(struct qla_hw_data *ha, int stat)
  316. {
  317. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  318. uint32_t word, wait_cnt;
  319. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  320. if (stat != QLA_SUCCESS)
  321. return;
  322. /* Set NVRAM write protection. */
  323. /* Write enable. */
  324. qla2x00_nv_write(ha, NVR_DATA_OUT);
  325. qla2x00_nv_write(ha, 0);
  326. qla2x00_nv_write(ha, 0);
  327. for (word = 0; word < 8; word++)
  328. qla2x00_nv_write(ha, NVR_DATA_OUT);
  329. qla2x00_nv_deselect(ha);
  330. /* Enable protection register. */
  331. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  332. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  333. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  334. for (word = 0; word < 8; word++)
  335. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  336. qla2x00_nv_deselect(ha);
  337. /* Enable protection register. */
  338. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  339. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  340. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  341. for (word = 0; word < 8; word++)
  342. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  343. qla2x00_nv_deselect(ha);
  344. /* Wait for NVRAM to become ready. */
  345. wrt_reg_word(&reg->nvram, NVR_SELECT);
  346. rd_reg_word(&reg->nvram); /* PCI Posting. */
  347. wait_cnt = NVR_WAIT_CNT;
  348. do {
  349. if (!--wait_cnt) {
  350. ql_dbg(ql_dbg_user, vha, 0x708f,
  351. "NVRAM didn't go ready...\n");
  352. break;
  353. }
  354. NVRAM_DELAY();
  355. word = rd_reg_word(&reg->nvram);
  356. } while ((word & NVR_DATA_IN) == 0);
  357. }
  358. /*****************************************************************************/
  359. /* Flash Manipulation Routines */
  360. /*****************************************************************************/
  361. static inline uint32_t
  362. flash_conf_addr(struct qla_hw_data *ha, uint32_t faddr)
  363. {
  364. return ha->flash_conf_off + faddr;
  365. }
  366. static inline uint32_t
  367. flash_data_addr(struct qla_hw_data *ha, uint32_t faddr)
  368. {
  369. return ha->flash_data_off + faddr;
  370. }
  371. static inline uint32_t
  372. nvram_conf_addr(struct qla_hw_data *ha, uint32_t naddr)
  373. {
  374. return ha->nvram_conf_off + naddr;
  375. }
  376. static inline uint32_t
  377. nvram_data_addr(struct qla_hw_data *ha, uint32_t naddr)
  378. {
  379. return ha->nvram_data_off + naddr;
  380. }
  381. static int
  382. qla24xx_read_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t *data)
  383. {
  384. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  385. ulong cnt = 30000;
  386. wrt_reg_dword(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  387. while (cnt--) {
  388. if (rd_reg_dword(&reg->flash_addr) & FARX_DATA_FLAG) {
  389. *data = rd_reg_dword(&reg->flash_data);
  390. return QLA_SUCCESS;
  391. }
  392. udelay(10);
  393. cond_resched();
  394. }
  395. ql_log(ql_log_warn, pci_get_drvdata(ha->pdev), 0x7090,
  396. "Flash read dword at %x timeout.\n", addr);
  397. *data = 0xDEADDEAD;
  398. return QLA_FUNCTION_TIMEOUT;
  399. }
  400. int
  401. qla24xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
  402. uint32_t dwords)
  403. {
  404. ulong i;
  405. int ret = QLA_SUCCESS;
  406. struct qla_hw_data *ha = vha->hw;
  407. /* Dword reads to flash. */
  408. faddr = flash_data_addr(ha, faddr);
  409. for (i = 0; i < dwords; i++, faddr++, dwptr++) {
  410. ret = qla24xx_read_flash_dword(ha, faddr, dwptr);
  411. if (ret != QLA_SUCCESS)
  412. break;
  413. cpu_to_le32s(dwptr);
  414. }
  415. return ret;
  416. }
  417. static int
  418. qla24xx_write_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t data)
  419. {
  420. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  421. ulong cnt = 500000;
  422. wrt_reg_dword(&reg->flash_data, data);
  423. wrt_reg_dword(&reg->flash_addr, addr | FARX_DATA_FLAG);
  424. while (cnt--) {
  425. if (!(rd_reg_dword(&reg->flash_addr) & FARX_DATA_FLAG))
  426. return QLA_SUCCESS;
  427. udelay(10);
  428. cond_resched();
  429. }
  430. ql_log(ql_log_warn, pci_get_drvdata(ha->pdev), 0x7090,
  431. "Flash write dword at %x timeout.\n", addr);
  432. return QLA_FUNCTION_TIMEOUT;
  433. }
  434. static void
  435. qla24xx_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
  436. uint8_t *flash_id)
  437. {
  438. uint32_t faddr, ids = 0;
  439. *man_id = *flash_id = 0;
  440. faddr = flash_conf_addr(ha, 0x03ab);
  441. if (!qla24xx_read_flash_dword(ha, faddr, &ids)) {
  442. *man_id = LSB(ids);
  443. *flash_id = MSB(ids);
  444. }
  445. /* Check if man_id and flash_id are valid. */
  446. if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
  447. /* Read information using 0x9f opcode
  448. * Device ID, Mfg ID would be read in the format:
  449. * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
  450. * Example: ATMEL 0x00 01 45 1F
  451. * Extract MFG and Dev ID from last two bytes.
  452. */
  453. faddr = flash_conf_addr(ha, 0x009f);
  454. if (!qla24xx_read_flash_dword(ha, faddr, &ids)) {
  455. *man_id = LSB(ids);
  456. *flash_id = MSB(ids);
  457. }
  458. }
  459. }
  460. static int
  461. qla2xxx_find_flt_start(scsi_qla_host_t *vha, uint32_t *start)
  462. {
  463. const char *loc, *locations[] = { "DEF", "PCI" };
  464. uint32_t pcihdr, pcids;
  465. uint16_t cnt, chksum;
  466. __le16 *wptr;
  467. struct qla_hw_data *ha = vha->hw;
  468. struct req_que *req = ha->req_q_map[0];
  469. struct qla_flt_location *fltl = (void *)req->ring;
  470. uint32_t *dcode = (uint32_t *)req->ring;
  471. uint8_t *buf = (void *)req->ring, *bcode, last_image;
  472. /*
  473. * FLT-location structure resides after the last PCI region.
  474. */
  475. /* Begin with sane defaults. */
  476. loc = locations[0];
  477. *start = 0;
  478. if (IS_QLA24XX_TYPE(ha))
  479. *start = FA_FLASH_LAYOUT_ADDR_24;
  480. else if (IS_QLA25XX(ha))
  481. *start = FA_FLASH_LAYOUT_ADDR;
  482. else if (IS_QLA81XX(ha))
  483. *start = FA_FLASH_LAYOUT_ADDR_81;
  484. else if (IS_P3P_TYPE(ha)) {
  485. *start = FA_FLASH_LAYOUT_ADDR_82;
  486. goto end;
  487. } else if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
  488. *start = FA_FLASH_LAYOUT_ADDR_83;
  489. goto end;
  490. } else if (IS_QLA28XX(ha)) {
  491. *start = FA_FLASH_LAYOUT_ADDR_28;
  492. goto end;
  493. }
  494. /* Begin with first PCI expansion ROM header. */
  495. pcihdr = 0;
  496. do {
  497. /* Verify PCI expansion ROM header. */
  498. qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
  499. bcode = buf + (pcihdr % 4);
  500. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa)
  501. goto end;
  502. /* Locate PCI data structure. */
  503. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  504. qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
  505. bcode = buf + (pcihdr % 4);
  506. /* Validate signature of PCI data structure. */
  507. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  508. bcode[0x2] != 'I' || bcode[0x3] != 'R')
  509. goto end;
  510. last_image = bcode[0x15] & BIT_7;
  511. /* Locate next PCI expansion ROM. */
  512. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  513. } while (!last_image);
  514. /* Now verify FLT-location structure. */
  515. qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, sizeof(*fltl) >> 2);
  516. if (memcmp(fltl->sig, "QFLT", 4))
  517. goto end;
  518. wptr = (__force __le16 *)req->ring;
  519. cnt = sizeof(*fltl) / sizeof(*wptr);
  520. for (chksum = 0; cnt--; wptr++)
  521. chksum += le16_to_cpu(*wptr);
  522. if (chksum) {
  523. ql_log(ql_log_fatal, vha, 0x0045,
  524. "Inconsistent FLTL detected: checksum=0x%x.\n", chksum);
  525. ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010e,
  526. fltl, sizeof(*fltl));
  527. return QLA_FUNCTION_FAILED;
  528. }
  529. /* Good data. Use specified location. */
  530. loc = locations[1];
  531. *start = (le16_to_cpu(fltl->start_hi) << 16 |
  532. le16_to_cpu(fltl->start_lo)) >> 2;
  533. end:
  534. ql_dbg(ql_dbg_init, vha, 0x0046,
  535. "FLTL[%s] = 0x%x.\n",
  536. loc, *start);
  537. return QLA_SUCCESS;
  538. }
  539. static void
  540. qla2xxx_get_flt_info(scsi_qla_host_t *vha, uint32_t flt_addr)
  541. {
  542. const char *locations[] = { "DEF", "FLT" }, *loc = locations[1];
  543. const uint32_t def_fw[] =
  544. { FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR_81 };
  545. const uint32_t def_boot[] =
  546. { FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR_81 };
  547. const uint32_t def_vpd_nvram[] =
  548. { FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR_81 };
  549. const uint32_t def_vpd0[] =
  550. { 0, 0, FA_VPD0_ADDR_81 };
  551. const uint32_t def_vpd1[] =
  552. { 0, 0, FA_VPD1_ADDR_81 };
  553. const uint32_t def_nvram0[] =
  554. { 0, 0, FA_NVRAM0_ADDR_81 };
  555. const uint32_t def_nvram1[] =
  556. { 0, 0, FA_NVRAM1_ADDR_81 };
  557. const uint32_t def_fdt[] =
  558. { FA_FLASH_DESCR_ADDR_24, FA_FLASH_DESCR_ADDR,
  559. FA_FLASH_DESCR_ADDR_81 };
  560. const uint32_t def_npiv_conf0[] =
  561. { FA_NPIV_CONF0_ADDR_24, FA_NPIV_CONF0_ADDR,
  562. FA_NPIV_CONF0_ADDR_81 };
  563. const uint32_t def_npiv_conf1[] =
  564. { FA_NPIV_CONF1_ADDR_24, FA_NPIV_CONF1_ADDR,
  565. FA_NPIV_CONF1_ADDR_81 };
  566. const uint32_t fcp_prio_cfg0[] =
  567. { FA_FCP_PRIO0_ADDR, FA_FCP_PRIO0_ADDR_25,
  568. 0 };
  569. const uint32_t fcp_prio_cfg1[] =
  570. { FA_FCP_PRIO1_ADDR, FA_FCP_PRIO1_ADDR_25,
  571. 0 };
  572. struct qla_hw_data *ha = vha->hw;
  573. uint32_t def = IS_QLA81XX(ha) ? 2 : IS_QLA25XX(ha) ? 1 : 0;
  574. struct qla_flt_header *flt = ha->flt;
  575. struct qla_flt_region *region = &flt->region[0];
  576. __le16 *wptr;
  577. uint16_t cnt, chksum;
  578. uint32_t start;
  579. /* Assign FCP prio region since older adapters may not have FLT, or
  580. FCP prio region in it's FLT.
  581. */
  582. ha->flt_region_fcp_prio = (ha->port_no == 0) ?
  583. fcp_prio_cfg0[def] : fcp_prio_cfg1[def];
  584. ha->flt_region_flt = flt_addr;
  585. wptr = (__force __le16 *)ha->flt;
  586. ha->isp_ops->read_optrom(vha, flt, flt_addr << 2,
  587. (sizeof(struct qla_flt_header) + FLT_REGIONS_SIZE));
  588. if (le16_to_cpu(*wptr) == 0xffff)
  589. goto no_flash_data;
  590. if (flt->version != cpu_to_le16(1)) {
  591. ql_log(ql_log_warn, vha, 0x0047,
  592. "Unsupported FLT detected: version=0x%x length=0x%x checksum=0x%x.\n",
  593. le16_to_cpu(flt->version), le16_to_cpu(flt->length),
  594. le16_to_cpu(flt->checksum));
  595. goto no_flash_data;
  596. }
  597. cnt = (sizeof(*flt) + le16_to_cpu(flt->length)) / sizeof(*wptr);
  598. for (chksum = 0; cnt--; wptr++)
  599. chksum += le16_to_cpu(*wptr);
  600. if (chksum) {
  601. ql_log(ql_log_fatal, vha, 0x0048,
  602. "Inconsistent FLT detected: version=0x%x length=0x%x checksum=0x%x.\n",
  603. le16_to_cpu(flt->version), le16_to_cpu(flt->length),
  604. le16_to_cpu(flt->checksum));
  605. goto no_flash_data;
  606. }
  607. cnt = le16_to_cpu(flt->length) / sizeof(*region);
  608. for ( ; cnt; cnt--, region++) {
  609. /* Store addresses as DWORD offsets. */
  610. start = le32_to_cpu(region->start) >> 2;
  611. ql_dbg(ql_dbg_init, vha, 0x0049,
  612. "FLT[%#x]: start=%#x end=%#x size=%#x.\n",
  613. le16_to_cpu(region->code), start,
  614. le32_to_cpu(region->end) >> 2,
  615. le32_to_cpu(region->size) >> 2);
  616. if (region->attribute)
  617. ql_log(ql_dbg_init, vha, 0xffff,
  618. "Region %x is secure\n", region->code);
  619. switch (le16_to_cpu(region->code)) {
  620. case FLT_REG_FCOE_FW:
  621. if (!IS_QLA8031(ha))
  622. break;
  623. ha->flt_region_fw = start;
  624. break;
  625. case FLT_REG_FW:
  626. if (IS_QLA8031(ha))
  627. break;
  628. ha->flt_region_fw = start;
  629. break;
  630. case FLT_REG_BOOT_CODE:
  631. ha->flt_region_boot = start;
  632. break;
  633. case FLT_REG_VPD_0:
  634. if (IS_QLA8031(ha))
  635. break;
  636. ha->flt_region_vpd_nvram = start;
  637. if (IS_P3P_TYPE(ha))
  638. break;
  639. if (ha->port_no == 0)
  640. ha->flt_region_vpd = start;
  641. break;
  642. case FLT_REG_VPD_1:
  643. if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
  644. break;
  645. if (ha->port_no == 1)
  646. ha->flt_region_vpd = start;
  647. break;
  648. case FLT_REG_VPD_2:
  649. if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  650. break;
  651. if (ha->port_no == 2)
  652. ha->flt_region_vpd = start;
  653. break;
  654. case FLT_REG_VPD_3:
  655. if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  656. break;
  657. if (ha->port_no == 3)
  658. ha->flt_region_vpd = start;
  659. break;
  660. case FLT_REG_NVRAM_0:
  661. if (IS_QLA8031(ha))
  662. break;
  663. if (ha->port_no == 0)
  664. ha->flt_region_nvram = start;
  665. break;
  666. case FLT_REG_NVRAM_1:
  667. if (IS_QLA8031(ha))
  668. break;
  669. if (ha->port_no == 1)
  670. ha->flt_region_nvram = start;
  671. break;
  672. case FLT_REG_NVRAM_2:
  673. if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  674. break;
  675. if (ha->port_no == 2)
  676. ha->flt_region_nvram = start;
  677. break;
  678. case FLT_REG_NVRAM_3:
  679. if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  680. break;
  681. if (ha->port_no == 3)
  682. ha->flt_region_nvram = start;
  683. break;
  684. case FLT_REG_FDT:
  685. ha->flt_region_fdt = start;
  686. break;
  687. case FLT_REG_NPIV_CONF_0:
  688. if (ha->port_no == 0)
  689. ha->flt_region_npiv_conf = start;
  690. break;
  691. case FLT_REG_NPIV_CONF_1:
  692. if (ha->port_no == 1)
  693. ha->flt_region_npiv_conf = start;
  694. break;
  695. case FLT_REG_GOLD_FW:
  696. ha->flt_region_gold_fw = start;
  697. break;
  698. case FLT_REG_FCP_PRIO_0:
  699. if (ha->port_no == 0)
  700. ha->flt_region_fcp_prio = start;
  701. break;
  702. case FLT_REG_FCP_PRIO_1:
  703. if (ha->port_no == 1)
  704. ha->flt_region_fcp_prio = start;
  705. break;
  706. case FLT_REG_BOOT_CODE_82XX:
  707. ha->flt_region_boot = start;
  708. break;
  709. case FLT_REG_BOOT_CODE_8044:
  710. if (IS_QLA8044(ha))
  711. ha->flt_region_boot = start;
  712. break;
  713. case FLT_REG_FW_82XX:
  714. ha->flt_region_fw = start;
  715. break;
  716. case FLT_REG_CNA_FW:
  717. if (IS_CNA_CAPABLE(ha))
  718. ha->flt_region_fw = start;
  719. break;
  720. case FLT_REG_GOLD_FW_82XX:
  721. ha->flt_region_gold_fw = start;
  722. break;
  723. case FLT_REG_BOOTLOAD_82XX:
  724. ha->flt_region_bootload = start;
  725. break;
  726. case FLT_REG_VPD_8XXX:
  727. if (IS_CNA_CAPABLE(ha))
  728. ha->flt_region_vpd = start;
  729. break;
  730. case FLT_REG_FCOE_NVRAM_0:
  731. if (!(IS_QLA8031(ha) || IS_QLA8044(ha)))
  732. break;
  733. if (ha->port_no == 0)
  734. ha->flt_region_nvram = start;
  735. break;
  736. case FLT_REG_FCOE_NVRAM_1:
  737. if (!(IS_QLA8031(ha) || IS_QLA8044(ha)))
  738. break;
  739. if (ha->port_no == 1)
  740. ha->flt_region_nvram = start;
  741. break;
  742. case FLT_REG_IMG_PRI_27XX:
  743. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  744. ha->flt_region_img_status_pri = start;
  745. break;
  746. case FLT_REG_IMG_SEC_27XX:
  747. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  748. ha->flt_region_img_status_sec = start;
  749. break;
  750. case FLT_REG_FW_SEC_27XX:
  751. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  752. ha->flt_region_fw_sec = start;
  753. break;
  754. case FLT_REG_BOOTLOAD_SEC_27XX:
  755. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  756. ha->flt_region_boot_sec = start;
  757. break;
  758. case FLT_REG_AUX_IMG_PRI_28XX:
  759. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  760. ha->flt_region_aux_img_status_pri = start;
  761. break;
  762. case FLT_REG_AUX_IMG_SEC_28XX:
  763. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  764. ha->flt_region_aux_img_status_sec = start;
  765. break;
  766. case FLT_REG_NVRAM_SEC_28XX_0:
  767. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  768. if (ha->port_no == 0)
  769. ha->flt_region_nvram_sec = start;
  770. break;
  771. case FLT_REG_NVRAM_SEC_28XX_1:
  772. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  773. if (ha->port_no == 1)
  774. ha->flt_region_nvram_sec = start;
  775. break;
  776. case FLT_REG_NVRAM_SEC_28XX_2:
  777. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  778. if (ha->port_no == 2)
  779. ha->flt_region_nvram_sec = start;
  780. break;
  781. case FLT_REG_NVRAM_SEC_28XX_3:
  782. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  783. if (ha->port_no == 3)
  784. ha->flt_region_nvram_sec = start;
  785. break;
  786. case FLT_REG_VPD_SEC_27XX_0:
  787. case FLT_REG_VPD_SEC_28XX_0:
  788. if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
  789. ha->flt_region_vpd_nvram_sec = start;
  790. if (ha->port_no == 0)
  791. ha->flt_region_vpd_sec = start;
  792. }
  793. break;
  794. case FLT_REG_VPD_SEC_27XX_1:
  795. case FLT_REG_VPD_SEC_28XX_1:
  796. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  797. if (ha->port_no == 1)
  798. ha->flt_region_vpd_sec = start;
  799. break;
  800. case FLT_REG_VPD_SEC_27XX_2:
  801. case FLT_REG_VPD_SEC_28XX_2:
  802. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  803. if (ha->port_no == 2)
  804. ha->flt_region_vpd_sec = start;
  805. break;
  806. case FLT_REG_VPD_SEC_27XX_3:
  807. case FLT_REG_VPD_SEC_28XX_3:
  808. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  809. if (ha->port_no == 3)
  810. ha->flt_region_vpd_sec = start;
  811. break;
  812. }
  813. }
  814. goto done;
  815. no_flash_data:
  816. /* Use hardcoded defaults. */
  817. loc = locations[0];
  818. ha->flt_region_fw = def_fw[def];
  819. ha->flt_region_boot = def_boot[def];
  820. ha->flt_region_vpd_nvram = def_vpd_nvram[def];
  821. ha->flt_region_vpd = (ha->port_no == 0) ?
  822. def_vpd0[def] : def_vpd1[def];
  823. ha->flt_region_nvram = (ha->port_no == 0) ?
  824. def_nvram0[def] : def_nvram1[def];
  825. ha->flt_region_fdt = def_fdt[def];
  826. ha->flt_region_npiv_conf = (ha->port_no == 0) ?
  827. def_npiv_conf0[def] : def_npiv_conf1[def];
  828. done:
  829. ql_dbg(ql_dbg_init, vha, 0x004a,
  830. "FLT[%s]: boot=0x%x fw=0x%x vpd_nvram=0x%x vpd=0x%x nvram=0x%x "
  831. "fdt=0x%x flt=0x%x npiv=0x%x fcp_prif_cfg=0x%x.\n",
  832. loc, ha->flt_region_boot, ha->flt_region_fw,
  833. ha->flt_region_vpd_nvram, ha->flt_region_vpd, ha->flt_region_nvram,
  834. ha->flt_region_fdt, ha->flt_region_flt, ha->flt_region_npiv_conf,
  835. ha->flt_region_fcp_prio);
  836. }
  837. static void
  838. qla2xxx_get_fdt_info(scsi_qla_host_t *vha)
  839. {
  840. #define FLASH_BLK_SIZE_4K 0x1000
  841. #define FLASH_BLK_SIZE_32K 0x8000
  842. #define FLASH_BLK_SIZE_64K 0x10000
  843. const char *loc, *locations[] = { "MID", "FDT" };
  844. struct qla_hw_data *ha = vha->hw;
  845. struct req_que *req = ha->req_q_map[0];
  846. uint16_t cnt, chksum;
  847. __le16 *wptr = (__force __le16 *)req->ring;
  848. struct qla_fdt_layout *fdt = (struct qla_fdt_layout *)req->ring;
  849. uint8_t man_id, flash_id;
  850. uint16_t mid = 0, fid = 0;
  851. ha->isp_ops->read_optrom(vha, fdt, ha->flt_region_fdt << 2,
  852. OPTROM_BURST_DWORDS);
  853. if (le16_to_cpu(*wptr) == 0xffff)
  854. goto no_flash_data;
  855. if (memcmp(fdt->sig, "QLID", 4))
  856. goto no_flash_data;
  857. for (cnt = 0, chksum = 0; cnt < sizeof(*fdt) >> 1; cnt++, wptr++)
  858. chksum += le16_to_cpu(*wptr);
  859. if (chksum) {
  860. ql_dbg(ql_dbg_init, vha, 0x004c,
  861. "Inconsistent FDT detected:"
  862. " checksum=0x%x id=%c version0x%x.\n", chksum,
  863. fdt->sig[0], le16_to_cpu(fdt->version));
  864. ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0113,
  865. fdt, sizeof(*fdt));
  866. goto no_flash_data;
  867. }
  868. loc = locations[1];
  869. mid = le16_to_cpu(fdt->man_id);
  870. fid = le16_to_cpu(fdt->id);
  871. ha->fdt_wrt_disable = fdt->wrt_disable_bits;
  872. ha->fdt_wrt_enable = fdt->wrt_enable_bits;
  873. ha->fdt_wrt_sts_reg_cmd = fdt->wrt_sts_reg_cmd;
  874. if (IS_QLA8044(ha))
  875. ha->fdt_erase_cmd = fdt->erase_cmd;
  876. else
  877. ha->fdt_erase_cmd =
  878. flash_conf_addr(ha, 0x0300 | fdt->erase_cmd);
  879. ha->fdt_block_size = le32_to_cpu(fdt->block_size);
  880. if (fdt->unprotect_sec_cmd) {
  881. ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0300 |
  882. fdt->unprotect_sec_cmd);
  883. ha->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
  884. flash_conf_addr(ha, 0x0300 | fdt->protect_sec_cmd) :
  885. flash_conf_addr(ha, 0x0336);
  886. }
  887. goto done;
  888. no_flash_data:
  889. loc = locations[0];
  890. if (IS_P3P_TYPE(ha)) {
  891. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  892. goto done;
  893. }
  894. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  895. mid = man_id;
  896. fid = flash_id;
  897. ha->fdt_wrt_disable = 0x9c;
  898. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x03d8);
  899. switch (man_id) {
  900. case 0xbf: /* STT flash. */
  901. if (flash_id == 0x8e)
  902. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  903. else
  904. ha->fdt_block_size = FLASH_BLK_SIZE_32K;
  905. if (flash_id == 0x80)
  906. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0352);
  907. break;
  908. case 0x13: /* ST M25P80. */
  909. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  910. break;
  911. case 0x1f: /* Atmel 26DF081A. */
  912. ha->fdt_block_size = FLASH_BLK_SIZE_4K;
  913. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0320);
  914. ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0339);
  915. ha->fdt_protect_sec_cmd = flash_conf_addr(ha, 0x0336);
  916. break;
  917. default:
  918. /* Default to 64 kb sector size. */
  919. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  920. break;
  921. }
  922. done:
  923. ql_dbg(ql_dbg_init, vha, 0x004d,
  924. "FDT[%s]: (0x%x/0x%x) erase=0x%x "
  925. "pr=%x wrtd=0x%x blk=0x%x.\n",
  926. loc, mid, fid,
  927. ha->fdt_erase_cmd, ha->fdt_protect_sec_cmd,
  928. ha->fdt_wrt_disable, ha->fdt_block_size);
  929. }
  930. static void
  931. qla2xxx_get_idc_param(scsi_qla_host_t *vha)
  932. {
  933. #define QLA82XX_IDC_PARAM_ADDR 0x003e885c
  934. __le32 *wptr;
  935. struct qla_hw_data *ha = vha->hw;
  936. struct req_que *req = ha->req_q_map[0];
  937. if (!(IS_P3P_TYPE(ha)))
  938. return;
  939. wptr = (__force __le32 *)req->ring;
  940. ha->isp_ops->read_optrom(vha, req->ring, QLA82XX_IDC_PARAM_ADDR, 8);
  941. if (*wptr == cpu_to_le32(0xffffffff)) {
  942. ha->fcoe_dev_init_timeout = QLA82XX_ROM_DEV_INIT_TIMEOUT;
  943. ha->fcoe_reset_timeout = QLA82XX_ROM_DRV_RESET_ACK_TIMEOUT;
  944. } else {
  945. ha->fcoe_dev_init_timeout = le32_to_cpu(*wptr);
  946. wptr++;
  947. ha->fcoe_reset_timeout = le32_to_cpu(*wptr);
  948. }
  949. ql_dbg(ql_dbg_init, vha, 0x004e,
  950. "fcoe_dev_init_timeout=%d "
  951. "fcoe_reset_timeout=%d.\n", ha->fcoe_dev_init_timeout,
  952. ha->fcoe_reset_timeout);
  953. return;
  954. }
  955. int
  956. qla2xxx_get_flash_info(scsi_qla_host_t *vha)
  957. {
  958. int ret;
  959. uint32_t flt_addr;
  960. struct qla_hw_data *ha = vha->hw;
  961. if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
  962. !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha) &&
  963. !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  964. return QLA_SUCCESS;
  965. ret = qla2xxx_find_flt_start(vha, &flt_addr);
  966. if (ret != QLA_SUCCESS)
  967. return ret;
  968. qla2xxx_get_flt_info(vha, flt_addr);
  969. qla2xxx_get_fdt_info(vha);
  970. qla2xxx_get_idc_param(vha);
  971. return QLA_SUCCESS;
  972. }
  973. void
  974. qla2xxx_flash_npiv_conf(scsi_qla_host_t *vha)
  975. {
  976. #define NPIV_CONFIG_SIZE (16*1024)
  977. void *data;
  978. __le16 *wptr;
  979. uint16_t cnt, chksum;
  980. int i;
  981. struct qla_npiv_header hdr;
  982. struct qla_npiv_entry *entry;
  983. struct qla_hw_data *ha = vha->hw;
  984. if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
  985. !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha))
  986. return;
  987. if (ha->flags.nic_core_reset_hdlr_active)
  988. return;
  989. if (IS_QLA8044(ha))
  990. return;
  991. ha->isp_ops->read_optrom(vha, &hdr, ha->flt_region_npiv_conf << 2,
  992. sizeof(struct qla_npiv_header));
  993. if (hdr.version == cpu_to_le16(0xffff))
  994. return;
  995. if (hdr.version != cpu_to_le16(1)) {
  996. ql_dbg(ql_dbg_user, vha, 0x7090,
  997. "Unsupported NPIV-Config "
  998. "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
  999. le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
  1000. le16_to_cpu(hdr.checksum));
  1001. return;
  1002. }
  1003. data = kmalloc(NPIV_CONFIG_SIZE, GFP_KERNEL);
  1004. if (!data) {
  1005. ql_log(ql_log_warn, vha, 0x7091,
  1006. "Unable to allocate memory for data.\n");
  1007. return;
  1008. }
  1009. ha->isp_ops->read_optrom(vha, data, ha->flt_region_npiv_conf << 2,
  1010. NPIV_CONFIG_SIZE);
  1011. cnt = (sizeof(hdr) + le16_to_cpu(hdr.entries) * sizeof(*entry)) >> 1;
  1012. for (wptr = data, chksum = 0; cnt--; wptr++)
  1013. chksum += le16_to_cpu(*wptr);
  1014. if (chksum) {
  1015. ql_dbg(ql_dbg_user, vha, 0x7092,
  1016. "Inconsistent NPIV-Config "
  1017. "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
  1018. le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
  1019. le16_to_cpu(hdr.checksum));
  1020. goto done;
  1021. }
  1022. entry = data + sizeof(struct qla_npiv_header);
  1023. cnt = le16_to_cpu(hdr.entries);
  1024. for (i = 0; cnt; cnt--, entry++, i++) {
  1025. uint16_t flags;
  1026. struct fc_vport_identifiers vid;
  1027. struct fc_vport *vport;
  1028. memcpy(&ha->npiv_info[i], entry, sizeof(struct qla_npiv_entry));
  1029. flags = le16_to_cpu(entry->flags);
  1030. if (flags == 0xffff)
  1031. continue;
  1032. if ((flags & BIT_0) == 0)
  1033. continue;
  1034. memset(&vid, 0, sizeof(vid));
  1035. vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
  1036. vid.vport_type = FC_PORTTYPE_NPIV;
  1037. vid.disable = false;
  1038. vid.port_name = wwn_to_u64(entry->port_name);
  1039. vid.node_name = wwn_to_u64(entry->node_name);
  1040. ql_dbg(ql_dbg_user, vha, 0x7093,
  1041. "NPIV[%02x]: wwpn=%llx wwnn=%llx vf_id=%#x Q_qos=%#x F_qos=%#x.\n",
  1042. cnt, vid.port_name, vid.node_name,
  1043. le16_to_cpu(entry->vf_id),
  1044. entry->q_qos, entry->f_qos);
  1045. if (i < QLA_PRECONFIG_VPORTS) {
  1046. vport = fc_vport_create(vha->host, 0, &vid);
  1047. if (!vport)
  1048. ql_log(ql_log_warn, vha, 0x7094,
  1049. "NPIV-Config Failed to create vport [%02x]: wwpn=%llx wwnn=%llx.\n",
  1050. cnt, vid.port_name, vid.node_name);
  1051. }
  1052. }
  1053. done:
  1054. kfree(data);
  1055. }
  1056. static int
  1057. qla24xx_unprotect_flash(scsi_qla_host_t *vha)
  1058. {
  1059. struct qla_hw_data *ha = vha->hw;
  1060. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1061. if (ha->flags.fac_supported)
  1062. return qla81xx_fac_do_write_enable(vha, 1);
  1063. /* Enable flash write. */
  1064. wrt_reg_dword(&reg->ctrl_status,
  1065. rd_reg_dword(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  1066. rd_reg_dword(&reg->ctrl_status); /* PCI Posting. */
  1067. if (!ha->fdt_wrt_disable)
  1068. goto done;
  1069. /* Disable flash write-protection, first clear SR protection bit */
  1070. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
  1071. /* Then write zero again to clear remaining SR bits.*/
  1072. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
  1073. done:
  1074. return QLA_SUCCESS;
  1075. }
  1076. static int
  1077. qla24xx_protect_flash(scsi_qla_host_t *vha)
  1078. {
  1079. struct qla_hw_data *ha = vha->hw;
  1080. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1081. ulong cnt = 300;
  1082. uint32_t faddr, dword;
  1083. if (ha->flags.fac_supported)
  1084. return qla81xx_fac_do_write_enable(vha, 0);
  1085. if (!ha->fdt_wrt_disable)
  1086. goto skip_wrt_protect;
  1087. /* Enable flash write-protection and wait for completion. */
  1088. faddr = flash_conf_addr(ha, 0x101);
  1089. qla24xx_write_flash_dword(ha, faddr, ha->fdt_wrt_disable);
  1090. faddr = flash_conf_addr(ha, 0x5);
  1091. while (cnt--) {
  1092. if (!qla24xx_read_flash_dword(ha, faddr, &dword)) {
  1093. if (!(dword & BIT_0))
  1094. break;
  1095. }
  1096. udelay(10);
  1097. }
  1098. skip_wrt_protect:
  1099. /* Disable flash write. */
  1100. wrt_reg_dword(&reg->ctrl_status,
  1101. rd_reg_dword(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  1102. return QLA_SUCCESS;
  1103. }
  1104. static int
  1105. qla24xx_erase_sector(scsi_qla_host_t *vha, uint32_t fdata)
  1106. {
  1107. struct qla_hw_data *ha = vha->hw;
  1108. uint32_t start, finish;
  1109. if (ha->flags.fac_supported) {
  1110. start = fdata >> 2;
  1111. finish = start + (ha->fdt_block_size >> 2) - 1;
  1112. return qla81xx_fac_erase_sector(vha, flash_data_addr(ha,
  1113. start), flash_data_addr(ha, finish));
  1114. }
  1115. return qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
  1116. (fdata & 0xff00) | ((fdata << 16) & 0xff0000) |
  1117. ((fdata >> 16) & 0xff));
  1118. }
  1119. static int
  1120. qla24xx_write_flash_data(scsi_qla_host_t *vha, __le32 *dwptr, uint32_t faddr,
  1121. uint32_t dwords)
  1122. {
  1123. int ret;
  1124. ulong liter;
  1125. ulong dburst = OPTROM_BURST_DWORDS; /* burst size in dwords */
  1126. uint32_t sec_mask, rest_addr, fdata;
  1127. dma_addr_t optrom_dma;
  1128. void *optrom = NULL;
  1129. struct qla_hw_data *ha = vha->hw;
  1130. if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
  1131. !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  1132. goto next;
  1133. /* Allocate dma buffer for burst write */
  1134. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1135. &optrom_dma, GFP_KERNEL);
  1136. if (!optrom) {
  1137. ql_log(ql_log_warn, vha, 0x7095,
  1138. "Failed allocate burst (%x bytes)\n", OPTROM_BURST_SIZE);
  1139. }
  1140. next:
  1141. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  1142. "Unprotect flash...\n");
  1143. ret = qla24xx_unprotect_flash(vha);
  1144. if (ret) {
  1145. ql_log(ql_log_warn, vha, 0x7096,
  1146. "Failed to unprotect flash.\n");
  1147. goto done;
  1148. }
  1149. rest_addr = (ha->fdt_block_size >> 2) - 1;
  1150. sec_mask = ~rest_addr;
  1151. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  1152. fdata = (faddr & sec_mask) << 2;
  1153. /* Are we at the beginning of a sector? */
  1154. if (!(faddr & rest_addr)) {
  1155. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  1156. "Erase sector %#x...\n", faddr);
  1157. ret = qla24xx_erase_sector(vha, fdata);
  1158. if (ret) {
  1159. ql_dbg(ql_dbg_user, vha, 0x7007,
  1160. "Failed to erase sector %x.\n", faddr);
  1161. break;
  1162. }
  1163. }
  1164. if (optrom) {
  1165. /* If smaller than a burst remaining */
  1166. if (dwords - liter < dburst)
  1167. dburst = dwords - liter;
  1168. /* Copy to dma buffer */
  1169. memcpy(optrom, dwptr, dburst << 2);
  1170. /* Burst write */
  1171. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  1172. "Write burst (%#lx dwords)...\n", dburst);
  1173. ret = qla2x00_load_ram(vha, optrom_dma,
  1174. flash_data_addr(ha, faddr), dburst);
  1175. if (!ret) {
  1176. liter += dburst - 1;
  1177. faddr += dburst - 1;
  1178. dwptr += dburst - 1;
  1179. continue;
  1180. }
  1181. ql_log(ql_log_warn, vha, 0x7097,
  1182. "Failed burst-write at %x (%p/%#llx)....\n",
  1183. flash_data_addr(ha, faddr), optrom,
  1184. (u64)optrom_dma);
  1185. dma_free_coherent(&ha->pdev->dev,
  1186. OPTROM_BURST_SIZE, optrom, optrom_dma);
  1187. optrom = NULL;
  1188. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  1189. break;
  1190. ql_log(ql_log_warn, vha, 0x7098,
  1191. "Reverting to slow write...\n");
  1192. }
  1193. /* Slow write */
  1194. ret = qla24xx_write_flash_dword(ha,
  1195. flash_data_addr(ha, faddr), le32_to_cpu(*dwptr));
  1196. if (ret) {
  1197. ql_dbg(ql_dbg_user, vha, 0x7006,
  1198. "Failed slow write %x (%x)\n", faddr, *dwptr);
  1199. break;
  1200. }
  1201. }
  1202. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  1203. "Protect flash...\n");
  1204. ret = qla24xx_protect_flash(vha);
  1205. if (ret)
  1206. ql_log(ql_log_warn, vha, 0x7099,
  1207. "Failed to protect flash\n");
  1208. done:
  1209. if (optrom)
  1210. dma_free_coherent(&ha->pdev->dev,
  1211. OPTROM_BURST_SIZE, optrom, optrom_dma);
  1212. return ret;
  1213. }
  1214. uint8_t *
  1215. qla2x00_read_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1216. uint32_t bytes)
  1217. {
  1218. uint32_t i;
  1219. __le16 *wptr;
  1220. struct qla_hw_data *ha = vha->hw;
  1221. /* Word reads to NVRAM via registers. */
  1222. wptr = buf;
  1223. qla2x00_lock_nvram_access(ha);
  1224. for (i = 0; i < bytes >> 1; i++, naddr++)
  1225. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  1226. naddr));
  1227. qla2x00_unlock_nvram_access(ha);
  1228. return buf;
  1229. }
  1230. uint8_t *
  1231. qla24xx_read_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1232. uint32_t bytes)
  1233. {
  1234. struct qla_hw_data *ha = vha->hw;
  1235. uint32_t *dwptr = buf;
  1236. uint32_t i;
  1237. if (IS_P3P_TYPE(ha))
  1238. return buf;
  1239. /* Dword reads to flash. */
  1240. naddr = nvram_data_addr(ha, naddr);
  1241. bytes >>= 2;
  1242. for (i = 0; i < bytes; i++, naddr++, dwptr++) {
  1243. if (qla24xx_read_flash_dword(ha, naddr, dwptr))
  1244. break;
  1245. cpu_to_le32s(dwptr);
  1246. }
  1247. return buf;
  1248. }
  1249. int
  1250. qla2x00_write_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1251. uint32_t bytes)
  1252. {
  1253. int ret, stat;
  1254. uint32_t i;
  1255. uint16_t *wptr;
  1256. unsigned long flags;
  1257. struct qla_hw_data *ha = vha->hw;
  1258. ret = QLA_SUCCESS;
  1259. spin_lock_irqsave(&ha->hardware_lock, flags);
  1260. qla2x00_lock_nvram_access(ha);
  1261. /* Disable NVRAM write-protection. */
  1262. stat = qla2x00_clear_nvram_protection(ha);
  1263. wptr = (uint16_t *)buf;
  1264. for (i = 0; i < bytes >> 1; i++, naddr++) {
  1265. qla2x00_write_nvram_word(ha, naddr,
  1266. cpu_to_le16(*wptr));
  1267. wptr++;
  1268. }
  1269. /* Enable NVRAM write-protection. */
  1270. qla2x00_set_nvram_protection(ha, stat);
  1271. qla2x00_unlock_nvram_access(ha);
  1272. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1273. return ret;
  1274. }
  1275. int
  1276. qla24xx_write_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1277. uint32_t bytes)
  1278. {
  1279. struct qla_hw_data *ha = vha->hw;
  1280. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1281. __le32 *dwptr = buf;
  1282. uint32_t i;
  1283. int ret;
  1284. ret = QLA_SUCCESS;
  1285. if (IS_P3P_TYPE(ha))
  1286. return ret;
  1287. /* Enable flash write. */
  1288. wrt_reg_dword(&reg->ctrl_status,
  1289. rd_reg_dword(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  1290. rd_reg_dword(&reg->ctrl_status); /* PCI Posting. */
  1291. /* Disable NVRAM write-protection. */
  1292. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
  1293. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
  1294. /* Dword writes to flash. */
  1295. naddr = nvram_data_addr(ha, naddr);
  1296. bytes >>= 2;
  1297. for (i = 0; i < bytes; i++, naddr++, dwptr++) {
  1298. if (qla24xx_write_flash_dword(ha, naddr, le32_to_cpu(*dwptr))) {
  1299. ql_dbg(ql_dbg_user, vha, 0x709a,
  1300. "Unable to program nvram address=%x data=%x.\n",
  1301. naddr, *dwptr);
  1302. break;
  1303. }
  1304. }
  1305. /* Enable NVRAM write-protection. */
  1306. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0x8c);
  1307. /* Disable flash write. */
  1308. wrt_reg_dword(&reg->ctrl_status,
  1309. rd_reg_dword(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  1310. rd_reg_dword(&reg->ctrl_status); /* PCI Posting. */
  1311. return ret;
  1312. }
  1313. uint8_t *
  1314. qla25xx_read_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1315. uint32_t bytes)
  1316. {
  1317. struct qla_hw_data *ha = vha->hw;
  1318. uint32_t *dwptr = buf;
  1319. uint32_t i;
  1320. /* Dword reads to flash. */
  1321. naddr = flash_data_addr(ha, ha->flt_region_vpd_nvram | naddr);
  1322. bytes >>= 2;
  1323. for (i = 0; i < bytes; i++, naddr++, dwptr++) {
  1324. if (qla24xx_read_flash_dword(ha, naddr, dwptr))
  1325. break;
  1326. cpu_to_le32s(dwptr);
  1327. }
  1328. return buf;
  1329. }
  1330. #define RMW_BUFFER_SIZE (64 * 1024)
  1331. int
  1332. qla25xx_write_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1333. uint32_t bytes)
  1334. {
  1335. struct qla_hw_data *ha = vha->hw;
  1336. uint8_t *dbuf = vmalloc(RMW_BUFFER_SIZE);
  1337. if (!dbuf)
  1338. return QLA_MEMORY_ALLOC_FAILED;
  1339. ha->isp_ops->read_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
  1340. RMW_BUFFER_SIZE);
  1341. memcpy(dbuf + (naddr << 2), buf, bytes);
  1342. ha->isp_ops->write_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
  1343. RMW_BUFFER_SIZE);
  1344. vfree(dbuf);
  1345. return QLA_SUCCESS;
  1346. }
  1347. static inline void
  1348. qla2x00_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
  1349. {
  1350. if (IS_QLA2322(ha)) {
  1351. /* Flip all colors. */
  1352. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  1353. /* Turn off. */
  1354. ha->beacon_color_state = 0;
  1355. *pflags = GPIO_LED_ALL_OFF;
  1356. } else {
  1357. /* Turn on. */
  1358. ha->beacon_color_state = QLA_LED_ALL_ON;
  1359. *pflags = GPIO_LED_RGA_ON;
  1360. }
  1361. } else {
  1362. /* Flip green led only. */
  1363. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  1364. /* Turn off. */
  1365. ha->beacon_color_state = 0;
  1366. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  1367. } else {
  1368. /* Turn on. */
  1369. ha->beacon_color_state = QLA_LED_GRN_ON;
  1370. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  1371. }
  1372. }
  1373. }
  1374. #define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
  1375. void
  1376. qla2x00_beacon_blink(struct scsi_qla_host *vha)
  1377. {
  1378. uint16_t gpio_enable;
  1379. uint16_t gpio_data;
  1380. uint16_t led_color = 0;
  1381. unsigned long flags;
  1382. struct qla_hw_data *ha = vha->hw;
  1383. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1384. if (IS_P3P_TYPE(ha))
  1385. return;
  1386. spin_lock_irqsave(&ha->hardware_lock, flags);
  1387. /* Save the Original GPIOE. */
  1388. if (ha->pio_address) {
  1389. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  1390. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  1391. } else {
  1392. gpio_enable = rd_reg_word(&reg->gpioe);
  1393. gpio_data = rd_reg_word(&reg->gpiod);
  1394. }
  1395. /* Set the modified gpio_enable values */
  1396. gpio_enable |= GPIO_LED_MASK;
  1397. if (ha->pio_address) {
  1398. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  1399. } else {
  1400. wrt_reg_word(&reg->gpioe, gpio_enable);
  1401. rd_reg_word(&reg->gpioe);
  1402. }
  1403. qla2x00_flip_colors(ha, &led_color);
  1404. /* Clear out any previously set LED color. */
  1405. gpio_data &= ~GPIO_LED_MASK;
  1406. /* Set the new input LED color to GPIOD. */
  1407. gpio_data |= led_color;
  1408. /* Set the modified gpio_data values */
  1409. if (ha->pio_address) {
  1410. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  1411. } else {
  1412. wrt_reg_word(&reg->gpiod, gpio_data);
  1413. rd_reg_word(&reg->gpiod);
  1414. }
  1415. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1416. }
  1417. int
  1418. qla2x00_beacon_on(struct scsi_qla_host *vha)
  1419. {
  1420. uint16_t gpio_enable;
  1421. uint16_t gpio_data;
  1422. unsigned long flags;
  1423. struct qla_hw_data *ha = vha->hw;
  1424. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1425. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  1426. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  1427. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1428. ql_log(ql_log_warn, vha, 0x709b,
  1429. "Unable to update fw options (beacon on).\n");
  1430. return QLA_FUNCTION_FAILED;
  1431. }
  1432. /* Turn off LEDs. */
  1433. spin_lock_irqsave(&ha->hardware_lock, flags);
  1434. if (ha->pio_address) {
  1435. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  1436. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  1437. } else {
  1438. gpio_enable = rd_reg_word(&reg->gpioe);
  1439. gpio_data = rd_reg_word(&reg->gpiod);
  1440. }
  1441. gpio_enable |= GPIO_LED_MASK;
  1442. /* Set the modified gpio_enable values. */
  1443. if (ha->pio_address) {
  1444. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  1445. } else {
  1446. wrt_reg_word(&reg->gpioe, gpio_enable);
  1447. rd_reg_word(&reg->gpioe);
  1448. }
  1449. /* Clear out previously set LED colour. */
  1450. gpio_data &= ~GPIO_LED_MASK;
  1451. if (ha->pio_address) {
  1452. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  1453. } else {
  1454. wrt_reg_word(&reg->gpiod, gpio_data);
  1455. rd_reg_word(&reg->gpiod);
  1456. }
  1457. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1458. /*
  1459. * Let the per HBA timer kick off the blinking process based on
  1460. * the following flags. No need to do anything else now.
  1461. */
  1462. ha->beacon_blink_led = 1;
  1463. ha->beacon_color_state = 0;
  1464. return QLA_SUCCESS;
  1465. }
  1466. int
  1467. qla2x00_beacon_off(struct scsi_qla_host *vha)
  1468. {
  1469. int rval = QLA_SUCCESS;
  1470. struct qla_hw_data *ha = vha->hw;
  1471. ha->beacon_blink_led = 0;
  1472. /* Set the on flag so when it gets flipped it will be off. */
  1473. if (IS_QLA2322(ha))
  1474. ha->beacon_color_state = QLA_LED_ALL_ON;
  1475. else
  1476. ha->beacon_color_state = QLA_LED_GRN_ON;
  1477. ha->isp_ops->beacon_blink(vha); /* This turns green LED off */
  1478. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  1479. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  1480. rval = qla2x00_set_fw_options(vha, ha->fw_options);
  1481. if (rval != QLA_SUCCESS)
  1482. ql_log(ql_log_warn, vha, 0x709c,
  1483. "Unable to update fw options (beacon off).\n");
  1484. return rval;
  1485. }
  1486. static inline void
  1487. qla24xx_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
  1488. {
  1489. /* Flip all colors. */
  1490. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  1491. /* Turn off. */
  1492. ha->beacon_color_state = 0;
  1493. *pflags = 0;
  1494. } else {
  1495. /* Turn on. */
  1496. ha->beacon_color_state = QLA_LED_ALL_ON;
  1497. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  1498. }
  1499. }
  1500. void
  1501. qla24xx_beacon_blink(struct scsi_qla_host *vha)
  1502. {
  1503. uint16_t led_color = 0;
  1504. uint32_t gpio_data;
  1505. unsigned long flags;
  1506. struct qla_hw_data *ha = vha->hw;
  1507. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1508. /* Save the Original GPIOD. */
  1509. spin_lock_irqsave(&ha->hardware_lock, flags);
  1510. gpio_data = rd_reg_dword(&reg->gpiod);
  1511. /* Enable the gpio_data reg for update. */
  1512. gpio_data |= GPDX_LED_UPDATE_MASK;
  1513. wrt_reg_dword(&reg->gpiod, gpio_data);
  1514. gpio_data = rd_reg_dword(&reg->gpiod);
  1515. /* Set the color bits. */
  1516. qla24xx_flip_colors(ha, &led_color);
  1517. /* Clear out any previously set LED color. */
  1518. gpio_data &= ~GPDX_LED_COLOR_MASK;
  1519. /* Set the new input LED color to GPIOD. */
  1520. gpio_data |= led_color;
  1521. /* Set the modified gpio_data values. */
  1522. wrt_reg_dword(&reg->gpiod, gpio_data);
  1523. gpio_data = rd_reg_dword(&reg->gpiod);
  1524. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1525. }
  1526. static uint32_t
  1527. qla83xx_select_led_port(struct qla_hw_data *ha)
  1528. {
  1529. uint32_t led_select_value = 0;
  1530. if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  1531. goto out;
  1532. if (ha->port_no == 0)
  1533. led_select_value = QLA83XX_LED_PORT0;
  1534. else
  1535. led_select_value = QLA83XX_LED_PORT1;
  1536. out:
  1537. return led_select_value;
  1538. }
  1539. void
  1540. qla83xx_beacon_blink(struct scsi_qla_host *vha)
  1541. {
  1542. uint32_t led_select_value;
  1543. struct qla_hw_data *ha = vha->hw;
  1544. uint16_t led_cfg[6];
  1545. uint16_t orig_led_cfg[6];
  1546. uint32_t led_10_value, led_43_value;
  1547. if (!IS_QLA83XX(ha) && !IS_QLA81XX(ha) && !IS_QLA27XX(ha) &&
  1548. !IS_QLA28XX(ha))
  1549. return;
  1550. if (!ha->beacon_blink_led)
  1551. return;
  1552. if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
  1553. qla2x00_write_ram_word(vha, 0x1003, 0x40000230);
  1554. qla2x00_write_ram_word(vha, 0x1004, 0x40000230);
  1555. } else if (IS_QLA2031(ha)) {
  1556. led_select_value = qla83xx_select_led_port(ha);
  1557. qla83xx_wr_reg(vha, led_select_value, 0x40000230);
  1558. qla83xx_wr_reg(vha, led_select_value + 4, 0x40000230);
  1559. } else if (IS_QLA8031(ha)) {
  1560. led_select_value = qla83xx_select_led_port(ha);
  1561. qla83xx_rd_reg(vha, led_select_value, &led_10_value);
  1562. qla83xx_rd_reg(vha, led_select_value + 0x10, &led_43_value);
  1563. qla83xx_wr_reg(vha, led_select_value, 0x01f44000);
  1564. msleep(500);
  1565. qla83xx_wr_reg(vha, led_select_value, 0x400001f4);
  1566. msleep(1000);
  1567. qla83xx_wr_reg(vha, led_select_value, led_10_value);
  1568. qla83xx_wr_reg(vha, led_select_value + 0x10, led_43_value);
  1569. } else if (IS_QLA81XX(ha)) {
  1570. int rval;
  1571. /* Save Current */
  1572. rval = qla81xx_get_led_config(vha, orig_led_cfg);
  1573. /* Do the blink */
  1574. if (rval == QLA_SUCCESS) {
  1575. if (IS_QLA81XX(ha)) {
  1576. led_cfg[0] = 0x4000;
  1577. led_cfg[1] = 0x2000;
  1578. led_cfg[2] = 0;
  1579. led_cfg[3] = 0;
  1580. led_cfg[4] = 0;
  1581. led_cfg[5] = 0;
  1582. } else {
  1583. led_cfg[0] = 0x4000;
  1584. led_cfg[1] = 0x4000;
  1585. led_cfg[2] = 0x4000;
  1586. led_cfg[3] = 0x2000;
  1587. led_cfg[4] = 0;
  1588. led_cfg[5] = 0x2000;
  1589. }
  1590. rval = qla81xx_set_led_config(vha, led_cfg);
  1591. msleep(1000);
  1592. if (IS_QLA81XX(ha)) {
  1593. led_cfg[0] = 0x4000;
  1594. led_cfg[1] = 0x2000;
  1595. led_cfg[2] = 0;
  1596. } else {
  1597. led_cfg[0] = 0x4000;
  1598. led_cfg[1] = 0x2000;
  1599. led_cfg[2] = 0x4000;
  1600. led_cfg[3] = 0x4000;
  1601. led_cfg[4] = 0;
  1602. led_cfg[5] = 0x2000;
  1603. }
  1604. rval = qla81xx_set_led_config(vha, led_cfg);
  1605. }
  1606. /* On exit, restore original (presumes no status change) */
  1607. qla81xx_set_led_config(vha, orig_led_cfg);
  1608. }
  1609. }
  1610. int
  1611. qla24xx_beacon_on(struct scsi_qla_host *vha)
  1612. {
  1613. uint32_t gpio_data;
  1614. unsigned long flags;
  1615. struct qla_hw_data *ha = vha->hw;
  1616. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1617. if (IS_P3P_TYPE(ha))
  1618. return QLA_SUCCESS;
  1619. if (IS_QLA8031(ha) || IS_QLA81XX(ha))
  1620. goto skip_gpio; /* let blink handle it */
  1621. if (ha->beacon_blink_led == 0) {
  1622. /* Enable firmware for update */
  1623. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1624. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS)
  1625. return QLA_FUNCTION_FAILED;
  1626. if (qla2x00_get_fw_options(vha, ha->fw_options) !=
  1627. QLA_SUCCESS) {
  1628. ql_log(ql_log_warn, vha, 0x7009,
  1629. "Unable to update fw options (beacon on).\n");
  1630. return QLA_FUNCTION_FAILED;
  1631. }
  1632. if (IS_QLA2031(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
  1633. goto skip_gpio;
  1634. spin_lock_irqsave(&ha->hardware_lock, flags);
  1635. gpio_data = rd_reg_dword(&reg->gpiod);
  1636. /* Enable the gpio_data reg for update. */
  1637. gpio_data |= GPDX_LED_UPDATE_MASK;
  1638. wrt_reg_dword(&reg->gpiod, gpio_data);
  1639. rd_reg_dword(&reg->gpiod);
  1640. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1641. }
  1642. /* So all colors blink together. */
  1643. ha->beacon_color_state = 0;
  1644. skip_gpio:
  1645. /* Let the per HBA timer kick off the blinking process. */
  1646. ha->beacon_blink_led = 1;
  1647. return QLA_SUCCESS;
  1648. }
  1649. int
  1650. qla24xx_beacon_off(struct scsi_qla_host *vha)
  1651. {
  1652. uint32_t gpio_data;
  1653. unsigned long flags;
  1654. struct qla_hw_data *ha = vha->hw;
  1655. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1656. if (IS_P3P_TYPE(ha))
  1657. return QLA_SUCCESS;
  1658. if (!ha->flags.fw_started)
  1659. return QLA_SUCCESS;
  1660. ha->beacon_blink_led = 0;
  1661. if (IS_QLA2031(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
  1662. goto set_fw_options;
  1663. if (IS_QLA8031(ha) || IS_QLA81XX(ha))
  1664. return QLA_SUCCESS;
  1665. ha->beacon_color_state = QLA_LED_ALL_ON;
  1666. ha->isp_ops->beacon_blink(vha); /* Will flip to all off. */
  1667. /* Give control back to firmware. */
  1668. spin_lock_irqsave(&ha->hardware_lock, flags);
  1669. gpio_data = rd_reg_dword(&reg->gpiod);
  1670. /* Disable the gpio_data reg for update. */
  1671. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  1672. wrt_reg_dword(&reg->gpiod, gpio_data);
  1673. rd_reg_dword(&reg->gpiod);
  1674. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1675. set_fw_options:
  1676. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1677. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1678. ql_log(ql_log_warn, vha, 0x704d,
  1679. "Unable to update fw options (beacon on).\n");
  1680. return QLA_FUNCTION_FAILED;
  1681. }
  1682. if (qla2x00_get_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1683. ql_log(ql_log_warn, vha, 0x704e,
  1684. "Unable to update fw options (beacon on).\n");
  1685. return QLA_FUNCTION_FAILED;
  1686. }
  1687. return QLA_SUCCESS;
  1688. }
  1689. /*
  1690. * Flash support routines
  1691. */
  1692. /**
  1693. * qla2x00_flash_enable() - Setup flash for reading and writing.
  1694. * @ha: HA context
  1695. */
  1696. static void
  1697. qla2x00_flash_enable(struct qla_hw_data *ha)
  1698. {
  1699. uint16_t data;
  1700. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1701. data = rd_reg_word(&reg->ctrl_status);
  1702. data |= CSR_FLASH_ENABLE;
  1703. wrt_reg_word(&reg->ctrl_status, data);
  1704. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1705. }
  1706. /**
  1707. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  1708. * @ha: HA context
  1709. */
  1710. static void
  1711. qla2x00_flash_disable(struct qla_hw_data *ha)
  1712. {
  1713. uint16_t data;
  1714. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1715. data = rd_reg_word(&reg->ctrl_status);
  1716. data &= ~(CSR_FLASH_ENABLE);
  1717. wrt_reg_word(&reg->ctrl_status, data);
  1718. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1719. }
  1720. /**
  1721. * qla2x00_read_flash_byte() - Reads a byte from flash
  1722. * @ha: HA context
  1723. * @addr: Address in flash to read
  1724. *
  1725. * A word is read from the chip, but, only the lower byte is valid.
  1726. *
  1727. * Returns the byte read from flash @addr.
  1728. */
  1729. static uint8_t
  1730. qla2x00_read_flash_byte(struct qla_hw_data *ha, uint32_t addr)
  1731. {
  1732. uint16_t data;
  1733. uint16_t bank_select;
  1734. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1735. bank_select = rd_reg_word(&reg->ctrl_status);
  1736. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1737. /* Specify 64K address range: */
  1738. /* clear out Module Select and Flash Address bits [19:16]. */
  1739. bank_select &= ~0xf8;
  1740. bank_select |= addr >> 12 & 0xf0;
  1741. bank_select |= CSR_FLASH_64K_BANK;
  1742. wrt_reg_word(&reg->ctrl_status, bank_select);
  1743. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1744. wrt_reg_word(&reg->flash_address, (uint16_t)addr);
  1745. data = rd_reg_word(&reg->flash_data);
  1746. return (uint8_t)data;
  1747. }
  1748. /* Setup bit 16 of flash address. */
  1749. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1750. bank_select |= CSR_FLASH_64K_BANK;
  1751. wrt_reg_word(&reg->ctrl_status, bank_select);
  1752. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1753. } else if (((addr & BIT_16) == 0) &&
  1754. (bank_select & CSR_FLASH_64K_BANK)) {
  1755. bank_select &= ~(CSR_FLASH_64K_BANK);
  1756. wrt_reg_word(&reg->ctrl_status, bank_select);
  1757. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1758. }
  1759. /* Always perform IO mapped accesses to the FLASH registers. */
  1760. if (ha->pio_address) {
  1761. uint16_t data2;
  1762. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1763. do {
  1764. data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1765. barrier();
  1766. cpu_relax();
  1767. data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1768. } while (data != data2);
  1769. } else {
  1770. wrt_reg_word(&reg->flash_address, (uint16_t)addr);
  1771. data = qla2x00_debounce_register(&reg->flash_data);
  1772. }
  1773. return (uint8_t)data;
  1774. }
  1775. /**
  1776. * qla2x00_write_flash_byte() - Write a byte to flash
  1777. * @ha: HA context
  1778. * @addr: Address in flash to write
  1779. * @data: Data to write
  1780. */
  1781. static void
  1782. qla2x00_write_flash_byte(struct qla_hw_data *ha, uint32_t addr, uint8_t data)
  1783. {
  1784. uint16_t bank_select;
  1785. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1786. bank_select = rd_reg_word(&reg->ctrl_status);
  1787. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1788. /* Specify 64K address range: */
  1789. /* clear out Module Select and Flash Address bits [19:16]. */
  1790. bank_select &= ~0xf8;
  1791. bank_select |= addr >> 12 & 0xf0;
  1792. bank_select |= CSR_FLASH_64K_BANK;
  1793. wrt_reg_word(&reg->ctrl_status, bank_select);
  1794. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1795. wrt_reg_word(&reg->flash_address, (uint16_t)addr);
  1796. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1797. wrt_reg_word(&reg->flash_data, (uint16_t)data);
  1798. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1799. return;
  1800. }
  1801. /* Setup bit 16 of flash address. */
  1802. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1803. bank_select |= CSR_FLASH_64K_BANK;
  1804. wrt_reg_word(&reg->ctrl_status, bank_select);
  1805. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1806. } else if (((addr & BIT_16) == 0) &&
  1807. (bank_select & CSR_FLASH_64K_BANK)) {
  1808. bank_select &= ~(CSR_FLASH_64K_BANK);
  1809. wrt_reg_word(&reg->ctrl_status, bank_select);
  1810. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1811. }
  1812. /* Always perform IO mapped accesses to the FLASH registers. */
  1813. if (ha->pio_address) {
  1814. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1815. WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
  1816. } else {
  1817. wrt_reg_word(&reg->flash_address, (uint16_t)addr);
  1818. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1819. wrt_reg_word(&reg->flash_data, (uint16_t)data);
  1820. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1821. }
  1822. }
  1823. /**
  1824. * qla2x00_poll_flash() - Polls flash for completion.
  1825. * @ha: HA context
  1826. * @addr: Address in flash to poll
  1827. * @poll_data: Data to be polled
  1828. * @man_id: Flash manufacturer ID
  1829. * @flash_id: Flash ID
  1830. *
  1831. * This function polls the device until bit 7 of what is read matches data
  1832. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1833. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1834. * reading bit 5 as a 1.
  1835. *
  1836. * Returns 0 on success, else non-zero.
  1837. */
  1838. static int
  1839. qla2x00_poll_flash(struct qla_hw_data *ha, uint32_t addr, uint8_t poll_data,
  1840. uint8_t man_id, uint8_t flash_id)
  1841. {
  1842. int status;
  1843. uint8_t flash_data;
  1844. uint32_t cnt;
  1845. status = 1;
  1846. /* Wait for 30 seconds for command to finish. */
  1847. poll_data &= BIT_7;
  1848. for (cnt = 3000000; cnt; cnt--) {
  1849. flash_data = qla2x00_read_flash_byte(ha, addr);
  1850. if ((flash_data & BIT_7) == poll_data) {
  1851. status = 0;
  1852. break;
  1853. }
  1854. if (man_id != 0x40 && man_id != 0xda) {
  1855. if ((flash_data & BIT_5) && cnt > 2)
  1856. cnt = 2;
  1857. }
  1858. udelay(10);
  1859. barrier();
  1860. cond_resched();
  1861. }
  1862. return status;
  1863. }
  1864. /**
  1865. * qla2x00_program_flash_address() - Programs a flash address
  1866. * @ha: HA context
  1867. * @addr: Address in flash to program
  1868. * @data: Data to be written in flash
  1869. * @man_id: Flash manufacturer ID
  1870. * @flash_id: Flash ID
  1871. *
  1872. * Returns 0 on success, else non-zero.
  1873. */
  1874. static int
  1875. qla2x00_program_flash_address(struct qla_hw_data *ha, uint32_t addr,
  1876. uint8_t data, uint8_t man_id, uint8_t flash_id)
  1877. {
  1878. /* Write Program Command Sequence. */
  1879. if (IS_OEM_001(ha)) {
  1880. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1881. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1882. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1883. qla2x00_write_flash_byte(ha, addr, data);
  1884. } else {
  1885. if (man_id == 0xda && flash_id == 0xc1) {
  1886. qla2x00_write_flash_byte(ha, addr, data);
  1887. if (addr & 0x7e)
  1888. return 0;
  1889. } else {
  1890. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1891. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1892. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1893. qla2x00_write_flash_byte(ha, addr, data);
  1894. }
  1895. }
  1896. udelay(150);
  1897. /* Wait for write to complete. */
  1898. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1899. }
  1900. /**
  1901. * qla2x00_erase_flash() - Erase the flash.
  1902. * @ha: HA context
  1903. * @man_id: Flash manufacturer ID
  1904. * @flash_id: Flash ID
  1905. *
  1906. * Returns 0 on success, else non-zero.
  1907. */
  1908. static int
  1909. qla2x00_erase_flash(struct qla_hw_data *ha, uint8_t man_id, uint8_t flash_id)
  1910. {
  1911. /* Individual Sector Erase Command Sequence */
  1912. if (IS_OEM_001(ha)) {
  1913. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1914. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1915. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1916. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1917. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1918. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1919. } else {
  1920. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1921. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1922. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1923. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1924. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1925. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1926. }
  1927. udelay(150);
  1928. /* Wait for erase to complete. */
  1929. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1930. }
  1931. /**
  1932. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1933. * @ha: HA context
  1934. * @addr: Flash sector to erase
  1935. * @sec_mask: Sector address mask
  1936. * @man_id: Flash manufacturer ID
  1937. * @flash_id: Flash ID
  1938. *
  1939. * Returns 0 on success, else non-zero.
  1940. */
  1941. static int
  1942. qla2x00_erase_flash_sector(struct qla_hw_data *ha, uint32_t addr,
  1943. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1944. {
  1945. /* Individual Sector Erase Command Sequence */
  1946. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1947. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1948. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1949. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1950. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1951. if (man_id == 0x1f && flash_id == 0x13)
  1952. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1953. else
  1954. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1955. udelay(150);
  1956. /* Wait for erase to complete. */
  1957. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1958. }
  1959. /**
  1960. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1961. * @ha: host adapter
  1962. * @man_id: Flash manufacturer ID
  1963. * @flash_id: Flash ID
  1964. */
  1965. static void
  1966. qla2x00_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
  1967. uint8_t *flash_id)
  1968. {
  1969. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1970. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1971. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1972. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1973. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1974. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1975. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1976. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1977. }
  1978. static void
  1979. qla2x00_read_flash_data(struct qla_hw_data *ha, uint8_t *tmp_buf,
  1980. uint32_t saddr, uint32_t length)
  1981. {
  1982. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1983. uint32_t midpoint, ilength;
  1984. uint8_t data;
  1985. midpoint = length / 2;
  1986. wrt_reg_word(&reg->nvram, 0);
  1987. rd_reg_word(&reg->nvram);
  1988. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1989. if (ilength == midpoint) {
  1990. wrt_reg_word(&reg->nvram, NVR_SELECT);
  1991. rd_reg_word(&reg->nvram);
  1992. }
  1993. data = qla2x00_read_flash_byte(ha, saddr);
  1994. if (saddr % 100)
  1995. udelay(10);
  1996. *tmp_buf = data;
  1997. cond_resched();
  1998. }
  1999. }
  2000. static inline void
  2001. qla2x00_suspend_hba(struct scsi_qla_host *vha)
  2002. {
  2003. int cnt;
  2004. unsigned long flags;
  2005. struct qla_hw_data *ha = vha->hw;
  2006. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2007. /* Suspend HBA. */
  2008. scsi_block_requests(vha->host);
  2009. ha->isp_ops->disable_intrs(ha);
  2010. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2011. /* Pause RISC. */
  2012. spin_lock_irqsave(&ha->hardware_lock, flags);
  2013. wrt_reg_word(&reg->hccr, HCCR_PAUSE_RISC);
  2014. rd_reg_word(&reg->hccr);
  2015. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  2016. for (cnt = 0; cnt < 30000; cnt++) {
  2017. if ((rd_reg_word(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  2018. break;
  2019. udelay(100);
  2020. }
  2021. } else {
  2022. udelay(10);
  2023. }
  2024. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2025. }
  2026. static inline void
  2027. qla2x00_resume_hba(struct scsi_qla_host *vha)
  2028. {
  2029. struct qla_hw_data *ha = vha->hw;
  2030. /* Resume HBA. */
  2031. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2032. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  2033. qla2xxx_wake_dpc(vha);
  2034. qla2x00_wait_for_chip_reset(vha);
  2035. scsi_unblock_requests(vha->host);
  2036. }
  2037. void *
  2038. qla2x00_read_optrom_data(struct scsi_qla_host *vha, void *buf,
  2039. uint32_t offset, uint32_t length)
  2040. {
  2041. uint32_t addr, midpoint;
  2042. uint8_t *data;
  2043. struct qla_hw_data *ha = vha->hw;
  2044. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2045. /* Suspend HBA. */
  2046. qla2x00_suspend_hba(vha);
  2047. /* Go with read. */
  2048. midpoint = ha->optrom_size / 2;
  2049. qla2x00_flash_enable(ha);
  2050. wrt_reg_word(&reg->nvram, 0);
  2051. rd_reg_word(&reg->nvram); /* PCI Posting. */
  2052. for (addr = offset, data = buf; addr < length; addr++, data++) {
  2053. if (addr == midpoint) {
  2054. wrt_reg_word(&reg->nvram, NVR_SELECT);
  2055. rd_reg_word(&reg->nvram); /* PCI Posting. */
  2056. }
  2057. *data = qla2x00_read_flash_byte(ha, addr);
  2058. }
  2059. qla2x00_flash_disable(ha);
  2060. /* Resume HBA. */
  2061. qla2x00_resume_hba(vha);
  2062. return buf;
  2063. }
  2064. int
  2065. qla2x00_write_optrom_data(struct scsi_qla_host *vha, void *buf,
  2066. uint32_t offset, uint32_t length)
  2067. {
  2068. int rval;
  2069. uint8_t man_id, flash_id, sec_number, *data;
  2070. uint16_t wd;
  2071. uint32_t addr, liter, sec_mask, rest_addr;
  2072. struct qla_hw_data *ha = vha->hw;
  2073. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2074. /* Suspend HBA. */
  2075. qla2x00_suspend_hba(vha);
  2076. rval = QLA_SUCCESS;
  2077. sec_number = 0;
  2078. /* Reset ISP chip. */
  2079. wrt_reg_word(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  2080. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  2081. /* Go with write. */
  2082. qla2x00_flash_enable(ha);
  2083. do { /* Loop once to provide quick error exit */
  2084. /* Structure of flash memory based on manufacturer */
  2085. if (IS_OEM_001(ha)) {
  2086. /* OEM variant with special flash part. */
  2087. man_id = flash_id = 0;
  2088. rest_addr = 0xffff;
  2089. sec_mask = 0x10000;
  2090. goto update_flash;
  2091. }
  2092. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  2093. switch (man_id) {
  2094. case 0x20: /* ST flash. */
  2095. if (flash_id == 0xd2 || flash_id == 0xe3) {
  2096. /*
  2097. * ST m29w008at part - 64kb sector size with
  2098. * 32kb,8kb,8kb,16kb sectors at memory address
  2099. * 0xf0000.
  2100. */
  2101. rest_addr = 0xffff;
  2102. sec_mask = 0x10000;
  2103. break;
  2104. }
  2105. /*
  2106. * ST m29w010b part - 16kb sector size
  2107. * Default to 16kb sectors
  2108. */
  2109. rest_addr = 0x3fff;
  2110. sec_mask = 0x1c000;
  2111. break;
  2112. case 0x40: /* Mostel flash. */
  2113. /* Mostel v29c51001 part - 512 byte sector size. */
  2114. rest_addr = 0x1ff;
  2115. sec_mask = 0x1fe00;
  2116. break;
  2117. case 0xbf: /* SST flash. */
  2118. /* SST39sf10 part - 4kb sector size. */
  2119. rest_addr = 0xfff;
  2120. sec_mask = 0x1f000;
  2121. break;
  2122. case 0xda: /* Winbond flash. */
  2123. /* Winbond W29EE011 part - 256 byte sector size. */
  2124. rest_addr = 0x7f;
  2125. sec_mask = 0x1ff80;
  2126. break;
  2127. case 0xc2: /* Macronix flash. */
  2128. /* 64k sector size. */
  2129. if (flash_id == 0x38 || flash_id == 0x4f) {
  2130. rest_addr = 0xffff;
  2131. sec_mask = 0x10000;
  2132. break;
  2133. }
  2134. fallthrough;
  2135. case 0x1f: /* Atmel flash. */
  2136. /* 512k sector size. */
  2137. if (flash_id == 0x13) {
  2138. rest_addr = 0x7fffffff;
  2139. sec_mask = 0x80000000;
  2140. break;
  2141. }
  2142. fallthrough;
  2143. case 0x01: /* AMD flash. */
  2144. if (flash_id == 0x38 || flash_id == 0x40 ||
  2145. flash_id == 0x4f) {
  2146. /* Am29LV081 part - 64kb sector size. */
  2147. /* Am29LV002BT part - 64kb sector size. */
  2148. rest_addr = 0xffff;
  2149. sec_mask = 0x10000;
  2150. break;
  2151. } else if (flash_id == 0x3e) {
  2152. /*
  2153. * Am29LV008b part - 64kb sector size with
  2154. * 32kb,8kb,8kb,16kb sector at memory address
  2155. * h0xf0000.
  2156. */
  2157. rest_addr = 0xffff;
  2158. sec_mask = 0x10000;
  2159. break;
  2160. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  2161. /*
  2162. * Am29LV010 part or AM29f010 - 16kb sector
  2163. * size.
  2164. */
  2165. rest_addr = 0x3fff;
  2166. sec_mask = 0x1c000;
  2167. break;
  2168. } else if (flash_id == 0x6d) {
  2169. /* Am29LV001 part - 8kb sector size. */
  2170. rest_addr = 0x1fff;
  2171. sec_mask = 0x1e000;
  2172. break;
  2173. }
  2174. fallthrough;
  2175. default:
  2176. /* Default to 16 kb sector size. */
  2177. rest_addr = 0x3fff;
  2178. sec_mask = 0x1c000;
  2179. break;
  2180. }
  2181. update_flash:
  2182. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  2183. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  2184. rval = QLA_FUNCTION_FAILED;
  2185. break;
  2186. }
  2187. }
  2188. for (addr = offset, liter = 0; liter < length; liter++,
  2189. addr++) {
  2190. data = buf + liter;
  2191. /* Are we at the beginning of a sector? */
  2192. if ((addr & rest_addr) == 0) {
  2193. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  2194. if (addr >= 0x10000UL) {
  2195. if (((addr >> 12) & 0xf0) &&
  2196. ((man_id == 0x01 &&
  2197. flash_id == 0x3e) ||
  2198. (man_id == 0x20 &&
  2199. flash_id == 0xd2))) {
  2200. sec_number++;
  2201. if (sec_number == 1) {
  2202. rest_addr =
  2203. 0x7fff;
  2204. sec_mask =
  2205. 0x18000;
  2206. } else if (
  2207. sec_number == 2 ||
  2208. sec_number == 3) {
  2209. rest_addr =
  2210. 0x1fff;
  2211. sec_mask =
  2212. 0x1e000;
  2213. } else if (
  2214. sec_number == 4) {
  2215. rest_addr =
  2216. 0x3fff;
  2217. sec_mask =
  2218. 0x1c000;
  2219. }
  2220. }
  2221. }
  2222. } else if (addr == ha->optrom_size / 2) {
  2223. wrt_reg_word(&reg->nvram, NVR_SELECT);
  2224. rd_reg_word(&reg->nvram);
  2225. }
  2226. if (flash_id == 0xda && man_id == 0xc1) {
  2227. qla2x00_write_flash_byte(ha, 0x5555,
  2228. 0xaa);
  2229. qla2x00_write_flash_byte(ha, 0x2aaa,
  2230. 0x55);
  2231. qla2x00_write_flash_byte(ha, 0x5555,
  2232. 0xa0);
  2233. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  2234. /* Then erase it */
  2235. if (qla2x00_erase_flash_sector(ha,
  2236. addr, sec_mask, man_id,
  2237. flash_id)) {
  2238. rval = QLA_FUNCTION_FAILED;
  2239. break;
  2240. }
  2241. if (man_id == 0x01 && flash_id == 0x6d)
  2242. sec_number++;
  2243. }
  2244. }
  2245. if (man_id == 0x01 && flash_id == 0x6d) {
  2246. if (sec_number == 1 &&
  2247. addr == (rest_addr - 1)) {
  2248. rest_addr = 0x0fff;
  2249. sec_mask = 0x1f000;
  2250. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  2251. rest_addr = 0x3fff;
  2252. sec_mask = 0x1c000;
  2253. }
  2254. }
  2255. if (qla2x00_program_flash_address(ha, addr, *data,
  2256. man_id, flash_id)) {
  2257. rval = QLA_FUNCTION_FAILED;
  2258. break;
  2259. }
  2260. cond_resched();
  2261. }
  2262. } while (0);
  2263. qla2x00_flash_disable(ha);
  2264. /* Resume HBA. */
  2265. qla2x00_resume_hba(vha);
  2266. return rval;
  2267. }
  2268. void *
  2269. qla24xx_read_optrom_data(struct scsi_qla_host *vha, void *buf,
  2270. uint32_t offset, uint32_t length)
  2271. {
  2272. struct qla_hw_data *ha = vha->hw;
  2273. /* Suspend HBA. */
  2274. scsi_block_requests(vha->host);
  2275. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2276. /* Go with read. */
  2277. qla24xx_read_flash_data(vha, buf, offset >> 2, length >> 2);
  2278. /* Resume HBA. */
  2279. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2280. scsi_unblock_requests(vha->host);
  2281. return buf;
  2282. }
  2283. static int
  2284. qla28xx_extract_sfub_and_verify(struct scsi_qla_host *vha, __le32 *buf,
  2285. uint32_t len, uint32_t buf_size_without_sfub, uint8_t *sfub_buf)
  2286. {
  2287. uint32_t check_sum = 0;
  2288. __le32 *p;
  2289. int i;
  2290. p = buf + buf_size_without_sfub;
  2291. /* Extract SFUB from end of file */
  2292. memcpy(sfub_buf, (uint8_t *)p,
  2293. sizeof(struct secure_flash_update_block));
  2294. for (i = 0; i < (sizeof(struct secure_flash_update_block) >> 2); i++)
  2295. check_sum += le32_to_cpu(p[i]);
  2296. check_sum = (~check_sum) + 1;
  2297. if (check_sum != le32_to_cpu(p[i])) {
  2298. ql_log(ql_log_warn, vha, 0x7097,
  2299. "SFUB checksum failed, 0x%x, 0x%x\n",
  2300. check_sum, le32_to_cpu(p[i]));
  2301. return QLA_COMMAND_ERROR;
  2302. }
  2303. return QLA_SUCCESS;
  2304. }
  2305. static int
  2306. qla28xx_get_flash_region(struct scsi_qla_host *vha, uint32_t start,
  2307. struct qla_flt_region *region)
  2308. {
  2309. struct qla_hw_data *ha = vha->hw;
  2310. struct qla_flt_header *flt = ha->flt;
  2311. struct qla_flt_region *flt_reg = &flt->region[0];
  2312. uint16_t cnt;
  2313. int rval = QLA_FUNCTION_FAILED;
  2314. if (!ha->flt)
  2315. return QLA_FUNCTION_FAILED;
  2316. cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
  2317. for (; cnt; cnt--, flt_reg++) {
  2318. if (le32_to_cpu(flt_reg->start) == start) {
  2319. memcpy((uint8_t *)region, flt_reg,
  2320. sizeof(struct qla_flt_region));
  2321. rval = QLA_SUCCESS;
  2322. break;
  2323. }
  2324. }
  2325. return rval;
  2326. }
  2327. static int
  2328. qla28xx_write_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
  2329. uint32_t dwords)
  2330. {
  2331. struct qla_hw_data *ha = vha->hw;
  2332. ulong liter;
  2333. ulong dburst = OPTROM_BURST_DWORDS; /* burst size in dwords */
  2334. uint32_t sec_mask, rest_addr, fdata;
  2335. void *optrom = NULL;
  2336. dma_addr_t optrom_dma;
  2337. int rval, ret;
  2338. struct secure_flash_update_block *sfub;
  2339. dma_addr_t sfub_dma;
  2340. uint32_t offset = faddr << 2;
  2341. uint32_t buf_size_without_sfub = 0;
  2342. struct qla_flt_region region;
  2343. bool reset_to_rom = false;
  2344. uint32_t risc_size, risc_attr = 0;
  2345. __be32 *fw_array = NULL;
  2346. /* Retrieve region info - must be a start address passed in */
  2347. rval = qla28xx_get_flash_region(vha, offset, &region);
  2348. if (rval != QLA_SUCCESS) {
  2349. ql_log(ql_log_warn, vha, 0xffff,
  2350. "Invalid address %x - not a region start address\n",
  2351. offset);
  2352. goto done;
  2353. }
  2354. /* Allocate dma buffer for burst write */
  2355. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  2356. &optrom_dma, GFP_KERNEL);
  2357. if (!optrom) {
  2358. ql_log(ql_log_warn, vha, 0x7095,
  2359. "Failed allocate burst (%x bytes)\n", OPTROM_BURST_SIZE);
  2360. rval = QLA_COMMAND_ERROR;
  2361. goto done;
  2362. }
  2363. /*
  2364. * If adapter supports secure flash and region is secure
  2365. * extract secure flash update block (SFUB) and verify
  2366. */
  2367. if (ha->flags.secure_adapter && region.attribute) {
  2368. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2369. "Region %x is secure\n", le16_to_cpu(region.code));
  2370. switch (le16_to_cpu(region.code)) {
  2371. case FLT_REG_FW:
  2372. case FLT_REG_FW_SEC_27XX:
  2373. case FLT_REG_MPI_PRI_28XX:
  2374. case FLT_REG_MPI_SEC_28XX:
  2375. fw_array = (__force __be32 *)dwptr;
  2376. /* 1st fw array */
  2377. risc_size = be32_to_cpu(fw_array[3]);
  2378. risc_attr = be32_to_cpu(fw_array[9]);
  2379. buf_size_without_sfub = risc_size;
  2380. fw_array += risc_size;
  2381. /* 2nd fw array */
  2382. risc_size = be32_to_cpu(fw_array[3]);
  2383. buf_size_without_sfub += risc_size;
  2384. fw_array += risc_size;
  2385. /* 1st dump template */
  2386. risc_size = be32_to_cpu(fw_array[2]);
  2387. /* skip header and ignore checksum */
  2388. buf_size_without_sfub += risc_size;
  2389. fw_array += risc_size;
  2390. if (risc_attr & BIT_9) {
  2391. /* 2nd dump template */
  2392. risc_size = be32_to_cpu(fw_array[2]);
  2393. /* skip header and ignore checksum */
  2394. buf_size_without_sfub += risc_size;
  2395. fw_array += risc_size;
  2396. }
  2397. break;
  2398. case FLT_REG_PEP_PRI_28XX:
  2399. case FLT_REG_PEP_SEC_28XX:
  2400. fw_array = (__force __be32 *)dwptr;
  2401. /* 1st fw array */
  2402. risc_size = be32_to_cpu(fw_array[3]);
  2403. risc_attr = be32_to_cpu(fw_array[9]);
  2404. buf_size_without_sfub = risc_size;
  2405. fw_array += risc_size;
  2406. break;
  2407. default:
  2408. ql_log(ql_log_warn + ql_dbg_verbose, vha,
  2409. 0xffff, "Secure region %x not supported\n",
  2410. le16_to_cpu(region.code));
  2411. rval = QLA_COMMAND_ERROR;
  2412. goto done;
  2413. }
  2414. sfub = dma_alloc_coherent(&ha->pdev->dev,
  2415. sizeof(struct secure_flash_update_block), &sfub_dma,
  2416. GFP_KERNEL);
  2417. if (!sfub) {
  2418. ql_log(ql_log_warn, vha, 0xffff,
  2419. "Unable to allocate memory for SFUB\n");
  2420. rval = QLA_COMMAND_ERROR;
  2421. goto done;
  2422. }
  2423. rval = qla28xx_extract_sfub_and_verify(vha, (__le32 *)dwptr,
  2424. dwords, buf_size_without_sfub, (uint8_t *)sfub);
  2425. if (rval != QLA_SUCCESS)
  2426. goto done;
  2427. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2428. "SFUB extract and verify successful\n");
  2429. }
  2430. rest_addr = (ha->fdt_block_size >> 2) - 1;
  2431. sec_mask = ~rest_addr;
  2432. /* Lock semaphore */
  2433. rval = qla81xx_fac_semaphore_access(vha, FAC_SEMAPHORE_LOCK);
  2434. if (rval != QLA_SUCCESS) {
  2435. ql_log(ql_log_warn, vha, 0xffff,
  2436. "Unable to lock flash semaphore.");
  2437. goto done;
  2438. }
  2439. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  2440. "Unprotect flash...\n");
  2441. rval = qla24xx_unprotect_flash(vha);
  2442. if (rval) {
  2443. qla81xx_fac_semaphore_access(vha, FAC_SEMAPHORE_UNLOCK);
  2444. ql_log(ql_log_warn, vha, 0x7096, "Failed unprotect flash\n");
  2445. goto done;
  2446. }
  2447. for (liter = 0; liter < dwords; liter++, faddr++) {
  2448. fdata = (faddr & sec_mask) << 2;
  2449. /* If start of sector */
  2450. if (!(faddr & rest_addr)) {
  2451. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  2452. "Erase sector %#x...\n", faddr);
  2453. rval = qla24xx_erase_sector(vha, fdata);
  2454. if (rval) {
  2455. ql_dbg(ql_dbg_user, vha, 0x7007,
  2456. "Failed erase sector %#x\n", faddr);
  2457. goto write_protect;
  2458. }
  2459. }
  2460. }
  2461. if (ha->flags.secure_adapter) {
  2462. /*
  2463. * If adapter supports secure flash but FW doesn't,
  2464. * disable write protect, release semaphore and reset
  2465. * chip to execute ROM code in order to update region securely
  2466. */
  2467. if (!ha->flags.secure_fw) {
  2468. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2469. "Disable Write and Release Semaphore.");
  2470. rval = qla24xx_protect_flash(vha);
  2471. if (rval != QLA_SUCCESS) {
  2472. qla81xx_fac_semaphore_access(vha,
  2473. FAC_SEMAPHORE_UNLOCK);
  2474. ql_log(ql_log_warn, vha, 0xffff,
  2475. "Unable to protect flash.");
  2476. goto done;
  2477. }
  2478. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2479. "Reset chip to ROM.");
  2480. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  2481. set_bit(ISP_ABORT_TO_ROM, &vha->dpc_flags);
  2482. qla2xxx_wake_dpc(vha);
  2483. rval = qla2x00_wait_for_chip_reset(vha);
  2484. if (rval != QLA_SUCCESS) {
  2485. ql_log(ql_log_warn, vha, 0xffff,
  2486. "Unable to reset to ROM code.");
  2487. goto done;
  2488. }
  2489. reset_to_rom = true;
  2490. ha->flags.fac_supported = 0;
  2491. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2492. "Lock Semaphore");
  2493. rval = qla2xxx_write_remote_register(vha,
  2494. FLASH_SEMAPHORE_REGISTER_ADDR, 0x00020002);
  2495. if (rval != QLA_SUCCESS) {
  2496. ql_log(ql_log_warn, vha, 0xffff,
  2497. "Unable to lock flash semaphore.");
  2498. goto done;
  2499. }
  2500. /* Unprotect flash */
  2501. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2502. "Enable Write.");
  2503. rval = qla2x00_write_ram_word(vha, 0x7ffd0101, 0);
  2504. if (rval) {
  2505. ql_log(ql_log_warn, vha, 0x7096,
  2506. "Failed unprotect flash\n");
  2507. goto done;
  2508. }
  2509. }
  2510. /* If region is secure, send Secure Flash MB Cmd */
  2511. if (region.attribute && buf_size_without_sfub) {
  2512. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2513. "Sending Secure Flash MB Cmd\n");
  2514. rval = qla28xx_secure_flash_update(vha, 0,
  2515. le16_to_cpu(region.code),
  2516. buf_size_without_sfub, sfub_dma,
  2517. sizeof(struct secure_flash_update_block) >> 2);
  2518. if (rval != QLA_SUCCESS) {
  2519. ql_log(ql_log_warn, vha, 0xffff,
  2520. "Secure Flash MB Cmd failed %x.", rval);
  2521. goto write_protect;
  2522. }
  2523. }
  2524. }
  2525. /* re-init flash offset */
  2526. faddr = offset >> 2;
  2527. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  2528. fdata = (faddr & sec_mask) << 2;
  2529. /* If smaller than a burst remaining */
  2530. if (dwords - liter < dburst)
  2531. dburst = dwords - liter;
  2532. /* Copy to dma buffer */
  2533. memcpy(optrom, dwptr, dburst << 2);
  2534. /* Burst write */
  2535. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  2536. "Write burst (%#lx dwords)...\n", dburst);
  2537. rval = qla2x00_load_ram(vha, optrom_dma,
  2538. flash_data_addr(ha, faddr), dburst);
  2539. if (rval != QLA_SUCCESS) {
  2540. ql_log(ql_log_warn, vha, 0x7097,
  2541. "Failed burst write at %x (%p/%#llx)...\n",
  2542. flash_data_addr(ha, faddr), optrom,
  2543. (u64)optrom_dma);
  2544. break;
  2545. }
  2546. liter += dburst - 1;
  2547. faddr += dburst - 1;
  2548. dwptr += dburst - 1;
  2549. }
  2550. write_protect:
  2551. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  2552. "Protect flash...\n");
  2553. ret = qla24xx_protect_flash(vha);
  2554. if (ret) {
  2555. qla81xx_fac_semaphore_access(vha, FAC_SEMAPHORE_UNLOCK);
  2556. ql_log(ql_log_warn, vha, 0x7099,
  2557. "Failed protect flash\n");
  2558. rval = QLA_COMMAND_ERROR;
  2559. }
  2560. if (reset_to_rom == true) {
  2561. /* Schedule DPC to restart the RISC */
  2562. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  2563. qla2xxx_wake_dpc(vha);
  2564. ret = qla2x00_wait_for_hba_online(vha);
  2565. if (ret != QLA_SUCCESS) {
  2566. ql_log(ql_log_warn, vha, 0xffff,
  2567. "Adapter did not come out of reset\n");
  2568. rval = QLA_COMMAND_ERROR;
  2569. }
  2570. }
  2571. done:
  2572. if (optrom)
  2573. dma_free_coherent(&ha->pdev->dev,
  2574. OPTROM_BURST_SIZE, optrom, optrom_dma);
  2575. return rval;
  2576. }
  2577. int
  2578. qla24xx_write_optrom_data(struct scsi_qla_host *vha, void *buf,
  2579. uint32_t offset, uint32_t length)
  2580. {
  2581. int rval;
  2582. struct qla_hw_data *ha = vha->hw;
  2583. /* Suspend HBA. */
  2584. scsi_block_requests(vha->host);
  2585. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2586. /* Go with write. */
  2587. if (IS_QLA28XX(ha))
  2588. rval = qla28xx_write_flash_data(vha, buf, offset >> 2,
  2589. length >> 2);
  2590. else
  2591. rval = qla24xx_write_flash_data(vha, buf, offset >> 2,
  2592. length >> 2);
  2593. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2594. scsi_unblock_requests(vha->host);
  2595. return rval;
  2596. }
  2597. void *
  2598. qla25xx_read_optrom_data(struct scsi_qla_host *vha, void *buf,
  2599. uint32_t offset, uint32_t length)
  2600. {
  2601. int rval;
  2602. dma_addr_t optrom_dma;
  2603. void *optrom;
  2604. uint8_t *pbuf;
  2605. uint32_t faddr, left, burst;
  2606. struct qla_hw_data *ha = vha->hw;
  2607. if (IS_QLA25XX(ha) || IS_QLA81XX(ha) || IS_QLA83XX(ha) ||
  2608. IS_QLA27XX(ha) || IS_QLA28XX(ha))
  2609. goto try_fast;
  2610. if (offset & 0xfff)
  2611. goto slow_read;
  2612. if (length < OPTROM_BURST_SIZE)
  2613. goto slow_read;
  2614. try_fast:
  2615. if (offset & 0xff)
  2616. goto slow_read;
  2617. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  2618. &optrom_dma, GFP_KERNEL);
  2619. if (!optrom) {
  2620. ql_log(ql_log_warn, vha, 0x00cc,
  2621. "Unable to allocate memory for optrom burst read (%x KB).\n",
  2622. OPTROM_BURST_SIZE / 1024);
  2623. goto slow_read;
  2624. }
  2625. pbuf = buf;
  2626. faddr = offset >> 2;
  2627. left = length >> 2;
  2628. burst = OPTROM_BURST_DWORDS;
  2629. while (left != 0) {
  2630. if (burst > left)
  2631. burst = left;
  2632. rval = qla2x00_dump_ram(vha, optrom_dma,
  2633. flash_data_addr(ha, faddr), burst);
  2634. if (rval) {
  2635. ql_log(ql_log_warn, vha, 0x00f5,
  2636. "Unable to burst-read optrom segment (%x/%x/%llx).\n",
  2637. rval, flash_data_addr(ha, faddr),
  2638. (unsigned long long)optrom_dma);
  2639. ql_log(ql_log_warn, vha, 0x00f6,
  2640. "Reverting to slow-read.\n");
  2641. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  2642. optrom, optrom_dma);
  2643. goto slow_read;
  2644. }
  2645. memcpy(pbuf, optrom, burst * 4);
  2646. left -= burst;
  2647. faddr += burst;
  2648. pbuf += burst * 4;
  2649. }
  2650. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
  2651. optrom_dma);
  2652. return buf;
  2653. slow_read:
  2654. return qla24xx_read_optrom_data(vha, buf, offset, length);
  2655. }
  2656. /**
  2657. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  2658. * @ha: HA context
  2659. * @pcids: Pointer to the FCODE PCI data structure
  2660. *
  2661. * The process of retrieving the FCODE version information is at best
  2662. * described as interesting.
  2663. *
  2664. * Within the first 100h bytes of the image an ASCII string is present
  2665. * which contains several pieces of information including the FCODE
  2666. * version. Unfortunately it seems the only reliable way to retrieve
  2667. * the version is by scanning for another sentinel within the string,
  2668. * the FCODE build date:
  2669. *
  2670. * ... 2.00.02 10/17/02 ...
  2671. *
  2672. * Returns QLA_SUCCESS on successful retrieval of version.
  2673. */
  2674. static void
  2675. qla2x00_get_fcode_version(struct qla_hw_data *ha, uint32_t pcids)
  2676. {
  2677. int ret = QLA_FUNCTION_FAILED;
  2678. uint32_t istart, iend, iter, vend;
  2679. uint8_t do_next, rbyte, *vbyte;
  2680. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2681. /* Skip the PCI data structure. */
  2682. istart = pcids +
  2683. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  2684. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  2685. iend = istart + 0x100;
  2686. do {
  2687. /* Scan for the sentinel date string...eeewww. */
  2688. do_next = 0;
  2689. iter = istart;
  2690. while ((iter < iend) && !do_next) {
  2691. iter++;
  2692. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  2693. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  2694. '/')
  2695. do_next++;
  2696. else if (qla2x00_read_flash_byte(ha,
  2697. iter + 3) == '/')
  2698. do_next++;
  2699. }
  2700. }
  2701. if (!do_next)
  2702. break;
  2703. /* Backtrack to previous ' ' (space). */
  2704. do_next = 0;
  2705. while ((iter > istart) && !do_next) {
  2706. iter--;
  2707. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  2708. do_next++;
  2709. }
  2710. if (!do_next)
  2711. break;
  2712. /*
  2713. * Mark end of version tag, and find previous ' ' (space) or
  2714. * string length (recent FCODE images -- major hack ahead!!!).
  2715. */
  2716. vend = iter - 1;
  2717. do_next = 0;
  2718. while ((iter > istart) && !do_next) {
  2719. iter--;
  2720. rbyte = qla2x00_read_flash_byte(ha, iter);
  2721. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  2722. do_next++;
  2723. }
  2724. if (!do_next)
  2725. break;
  2726. /* Mark beginning of version tag, and copy data. */
  2727. iter++;
  2728. if ((vend - iter) &&
  2729. ((vend - iter) < sizeof(ha->fcode_revision))) {
  2730. vbyte = ha->fcode_revision;
  2731. while (iter <= vend) {
  2732. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  2733. iter++;
  2734. }
  2735. ret = QLA_SUCCESS;
  2736. }
  2737. } while (0);
  2738. if (ret != QLA_SUCCESS)
  2739. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2740. }
  2741. int
  2742. qla2x00_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2743. {
  2744. int ret = QLA_SUCCESS;
  2745. uint8_t code_type, last_image;
  2746. uint32_t pcihdr, pcids;
  2747. uint8_t *dbyte;
  2748. uint16_t *dcode;
  2749. struct qla_hw_data *ha = vha->hw;
  2750. if (!ha->pio_address || !mbuf)
  2751. return QLA_FUNCTION_FAILED;
  2752. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2753. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2754. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2755. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2756. qla2x00_flash_enable(ha);
  2757. /* Begin with first PCI expansion ROM header. */
  2758. pcihdr = 0;
  2759. last_image = 1;
  2760. do {
  2761. /* Verify PCI expansion ROM header. */
  2762. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  2763. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  2764. /* No signature */
  2765. ql_log(ql_log_fatal, vha, 0x0050,
  2766. "No matching ROM signature.\n");
  2767. ret = QLA_FUNCTION_FAILED;
  2768. break;
  2769. }
  2770. /* Locate PCI data structure. */
  2771. pcids = pcihdr +
  2772. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  2773. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  2774. /* Validate signature of PCI data structure. */
  2775. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  2776. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  2777. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  2778. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  2779. /* Incorrect header. */
  2780. ql_log(ql_log_fatal, vha, 0x0051,
  2781. "PCI data struct not found pcir_adr=%x.\n", pcids);
  2782. ret = QLA_FUNCTION_FAILED;
  2783. break;
  2784. }
  2785. /* Read version */
  2786. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  2787. switch (code_type) {
  2788. case ROM_CODE_TYPE_BIOS:
  2789. /* Intel x86, PC-AT compatible. */
  2790. ha->bios_revision[0] =
  2791. qla2x00_read_flash_byte(ha, pcids + 0x12);
  2792. ha->bios_revision[1] =
  2793. qla2x00_read_flash_byte(ha, pcids + 0x13);
  2794. ql_dbg(ql_dbg_init, vha, 0x0052,
  2795. "Read BIOS %d.%d.\n",
  2796. ha->bios_revision[1], ha->bios_revision[0]);
  2797. break;
  2798. case ROM_CODE_TYPE_FCODE:
  2799. /* Open Firmware standard for PCI (FCode). */
  2800. /* Eeeewww... */
  2801. qla2x00_get_fcode_version(ha, pcids);
  2802. break;
  2803. case ROM_CODE_TYPE_EFI:
  2804. /* Extensible Firmware Interface (EFI). */
  2805. ha->efi_revision[0] =
  2806. qla2x00_read_flash_byte(ha, pcids + 0x12);
  2807. ha->efi_revision[1] =
  2808. qla2x00_read_flash_byte(ha, pcids + 0x13);
  2809. ql_dbg(ql_dbg_init, vha, 0x0053,
  2810. "Read EFI %d.%d.\n",
  2811. ha->efi_revision[1], ha->efi_revision[0]);
  2812. break;
  2813. default:
  2814. ql_log(ql_log_warn, vha, 0x0054,
  2815. "Unrecognized code type %x at pcids %x.\n",
  2816. code_type, pcids);
  2817. break;
  2818. }
  2819. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  2820. /* Locate next PCI expansion ROM. */
  2821. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  2822. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  2823. } while (!last_image);
  2824. if (IS_QLA2322(ha)) {
  2825. /* Read firmware image information. */
  2826. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2827. dbyte = mbuf;
  2828. memset(dbyte, 0, 8);
  2829. dcode = (uint16_t *)dbyte;
  2830. qla2x00_read_flash_data(ha, dbyte, ha->flt_region_fw * 4 + 10,
  2831. 8);
  2832. ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010a,
  2833. "Dumping fw "
  2834. "ver from flash:.\n");
  2835. ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010b,
  2836. dbyte, 32);
  2837. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  2838. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  2839. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  2840. dcode[3] == 0)) {
  2841. ql_log(ql_log_warn, vha, 0x0057,
  2842. "Unrecognized fw revision at %x.\n",
  2843. ha->flt_region_fw * 4);
  2844. } else {
  2845. /* values are in big endian */
  2846. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  2847. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  2848. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  2849. ql_dbg(ql_dbg_init, vha, 0x0058,
  2850. "FW Version: "
  2851. "%d.%d.%d.\n", ha->fw_revision[0],
  2852. ha->fw_revision[1], ha->fw_revision[2]);
  2853. }
  2854. }
  2855. qla2x00_flash_disable(ha);
  2856. return ret;
  2857. }
  2858. int
  2859. qla82xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2860. {
  2861. int ret = QLA_SUCCESS;
  2862. uint32_t pcihdr, pcids;
  2863. uint32_t *dcode = mbuf;
  2864. uint8_t *bcode = mbuf;
  2865. uint8_t code_type, last_image;
  2866. struct qla_hw_data *ha = vha->hw;
  2867. if (!mbuf)
  2868. return QLA_FUNCTION_FAILED;
  2869. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2870. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2871. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2872. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2873. /* Begin with first PCI expansion ROM header. */
  2874. pcihdr = ha->flt_region_boot << 2;
  2875. last_image = 1;
  2876. do {
  2877. /* Verify PCI expansion ROM header. */
  2878. ha->isp_ops->read_optrom(vha, dcode, pcihdr, 0x20 * 4);
  2879. bcode = mbuf + (pcihdr % 4);
  2880. if (memcmp(bcode, "\x55\xaa", 2)) {
  2881. /* No signature */
  2882. ql_log(ql_log_fatal, vha, 0x0154,
  2883. "No matching ROM signature.\n");
  2884. ret = QLA_FUNCTION_FAILED;
  2885. break;
  2886. }
  2887. /* Locate PCI data structure. */
  2888. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  2889. ha->isp_ops->read_optrom(vha, dcode, pcids, 0x20 * 4);
  2890. bcode = mbuf + (pcihdr % 4);
  2891. /* Validate signature of PCI data structure. */
  2892. if (memcmp(bcode, "PCIR", 4)) {
  2893. /* Incorrect header. */
  2894. ql_log(ql_log_fatal, vha, 0x0155,
  2895. "PCI data struct not found pcir_adr=%x.\n", pcids);
  2896. ret = QLA_FUNCTION_FAILED;
  2897. break;
  2898. }
  2899. /* Read version */
  2900. code_type = bcode[0x14];
  2901. switch (code_type) {
  2902. case ROM_CODE_TYPE_BIOS:
  2903. /* Intel x86, PC-AT compatible. */
  2904. ha->bios_revision[0] = bcode[0x12];
  2905. ha->bios_revision[1] = bcode[0x13];
  2906. ql_dbg(ql_dbg_init, vha, 0x0156,
  2907. "Read BIOS %d.%d.\n",
  2908. ha->bios_revision[1], ha->bios_revision[0]);
  2909. break;
  2910. case ROM_CODE_TYPE_FCODE:
  2911. /* Open Firmware standard for PCI (FCode). */
  2912. ha->fcode_revision[0] = bcode[0x12];
  2913. ha->fcode_revision[1] = bcode[0x13];
  2914. ql_dbg(ql_dbg_init, vha, 0x0157,
  2915. "Read FCODE %d.%d.\n",
  2916. ha->fcode_revision[1], ha->fcode_revision[0]);
  2917. break;
  2918. case ROM_CODE_TYPE_EFI:
  2919. /* Extensible Firmware Interface (EFI). */
  2920. ha->efi_revision[0] = bcode[0x12];
  2921. ha->efi_revision[1] = bcode[0x13];
  2922. ql_dbg(ql_dbg_init, vha, 0x0158,
  2923. "Read EFI %d.%d.\n",
  2924. ha->efi_revision[1], ha->efi_revision[0]);
  2925. break;
  2926. default:
  2927. ql_log(ql_log_warn, vha, 0x0159,
  2928. "Unrecognized code type %x at pcids %x.\n",
  2929. code_type, pcids);
  2930. break;
  2931. }
  2932. last_image = bcode[0x15] & BIT_7;
  2933. /* Locate next PCI expansion ROM. */
  2934. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  2935. } while (!last_image);
  2936. /* Read firmware image information. */
  2937. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2938. dcode = mbuf;
  2939. ha->isp_ops->read_optrom(vha, dcode, ha->flt_region_fw << 2, 0x20);
  2940. bcode = mbuf + (pcihdr % 4);
  2941. /* Validate signature of PCI data structure. */
  2942. if (bcode[0x0] == 0x3 && bcode[0x1] == 0x0 &&
  2943. bcode[0x2] == 0x40 && bcode[0x3] == 0x40) {
  2944. ha->fw_revision[0] = bcode[0x4];
  2945. ha->fw_revision[1] = bcode[0x5];
  2946. ha->fw_revision[2] = bcode[0x6];
  2947. ql_dbg(ql_dbg_init, vha, 0x0153,
  2948. "Firmware revision %d.%d.%d\n",
  2949. ha->fw_revision[0], ha->fw_revision[1],
  2950. ha->fw_revision[2]);
  2951. }
  2952. return ret;
  2953. }
  2954. int
  2955. qla24xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2956. {
  2957. int ret = QLA_SUCCESS;
  2958. uint32_t pcihdr = 0, pcids = 0;
  2959. uint32_t *dcode = mbuf;
  2960. uint8_t *bcode = mbuf;
  2961. uint8_t code_type, last_image;
  2962. int i;
  2963. struct qla_hw_data *ha = vha->hw;
  2964. uint32_t faddr = 0;
  2965. struct active_regions active_regions = { };
  2966. if (IS_P3P_TYPE(ha))
  2967. return ret;
  2968. if (!mbuf)
  2969. return QLA_FUNCTION_FAILED;
  2970. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2971. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2972. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2973. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2974. pcihdr = ha->flt_region_boot << 2;
  2975. if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
  2976. qla27xx_get_active_image(vha, &active_regions);
  2977. if (active_regions.global == QLA27XX_SECONDARY_IMAGE) {
  2978. pcihdr = ha->flt_region_boot_sec << 2;
  2979. }
  2980. }
  2981. do {
  2982. /* Verify PCI expansion ROM header. */
  2983. qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
  2984. bcode = mbuf + (pcihdr % 4);
  2985. if (memcmp(bcode, "\x55\xaa", 2)) {
  2986. /* No signature */
  2987. ql_log(ql_log_fatal, vha, 0x0059,
  2988. "No matching ROM signature.\n");
  2989. ret = QLA_FUNCTION_FAILED;
  2990. break;
  2991. }
  2992. /* Locate PCI data structure. */
  2993. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  2994. qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
  2995. bcode = mbuf + (pcihdr % 4);
  2996. /* Validate signature of PCI data structure. */
  2997. if (memcmp(bcode, "PCIR", 4)) {
  2998. /* Incorrect header. */
  2999. ql_log(ql_log_fatal, vha, 0x005a,
  3000. "PCI data struct not found pcir_adr=%x.\n", pcids);
  3001. ql_dump_buffer(ql_dbg_init, vha, 0x0059, dcode, 32);
  3002. ret = QLA_FUNCTION_FAILED;
  3003. break;
  3004. }
  3005. /* Read version */
  3006. code_type = bcode[0x14];
  3007. switch (code_type) {
  3008. case ROM_CODE_TYPE_BIOS:
  3009. /* Intel x86, PC-AT compatible. */
  3010. ha->bios_revision[0] = bcode[0x12];
  3011. ha->bios_revision[1] = bcode[0x13];
  3012. ql_dbg(ql_dbg_init, vha, 0x005b,
  3013. "Read BIOS %d.%d.\n",
  3014. ha->bios_revision[1], ha->bios_revision[0]);
  3015. break;
  3016. case ROM_CODE_TYPE_FCODE:
  3017. /* Open Firmware standard for PCI (FCode). */
  3018. ha->fcode_revision[0] = bcode[0x12];
  3019. ha->fcode_revision[1] = bcode[0x13];
  3020. ql_dbg(ql_dbg_init, vha, 0x005c,
  3021. "Read FCODE %d.%d.\n",
  3022. ha->fcode_revision[1], ha->fcode_revision[0]);
  3023. break;
  3024. case ROM_CODE_TYPE_EFI:
  3025. /* Extensible Firmware Interface (EFI). */
  3026. ha->efi_revision[0] = bcode[0x12];
  3027. ha->efi_revision[1] = bcode[0x13];
  3028. ql_dbg(ql_dbg_init, vha, 0x005d,
  3029. "Read EFI %d.%d.\n",
  3030. ha->efi_revision[1], ha->efi_revision[0]);
  3031. break;
  3032. default:
  3033. ql_log(ql_log_warn, vha, 0x005e,
  3034. "Unrecognized code type %x at pcids %x.\n",
  3035. code_type, pcids);
  3036. break;
  3037. }
  3038. last_image = bcode[0x15] & BIT_7;
  3039. /* Locate next PCI expansion ROM. */
  3040. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  3041. } while (!last_image);
  3042. /* Read firmware image information. */
  3043. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  3044. faddr = ha->flt_region_fw;
  3045. if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
  3046. qla27xx_get_active_image(vha, &active_regions);
  3047. if (active_regions.global == QLA27XX_SECONDARY_IMAGE)
  3048. faddr = ha->flt_region_fw_sec;
  3049. }
  3050. qla24xx_read_flash_data(vha, dcode, faddr, 8);
  3051. if (qla24xx_risc_firmware_invalid(dcode)) {
  3052. ql_log(ql_log_warn, vha, 0x005f,
  3053. "Unrecognized fw revision at %x.\n",
  3054. ha->flt_region_fw * 4);
  3055. ql_dump_buffer(ql_dbg_init, vha, 0x005f, dcode, 32);
  3056. } else {
  3057. for (i = 0; i < 4; i++)
  3058. ha->fw_revision[i] =
  3059. be32_to_cpu((__force __be32)dcode[4+i]);
  3060. ql_dbg(ql_dbg_init, vha, 0x0060,
  3061. "Firmware revision (flash) %u.%u.%u (%x).\n",
  3062. ha->fw_revision[0], ha->fw_revision[1],
  3063. ha->fw_revision[2], ha->fw_revision[3]);
  3064. }
  3065. /* Check for golden firmware and get version if available */
  3066. if (!IS_QLA81XX(ha)) {
  3067. /* Golden firmware is not present in non 81XX adapters */
  3068. return ret;
  3069. }
  3070. memset(ha->gold_fw_version, 0, sizeof(ha->gold_fw_version));
  3071. faddr = ha->flt_region_gold_fw;
  3072. qla24xx_read_flash_data(vha, dcode, ha->flt_region_gold_fw, 8);
  3073. if (qla24xx_risc_firmware_invalid(dcode)) {
  3074. ql_log(ql_log_warn, vha, 0x0056,
  3075. "Unrecognized golden fw at %#x.\n", faddr);
  3076. ql_dump_buffer(ql_dbg_init, vha, 0x0056, dcode, 32);
  3077. return ret;
  3078. }
  3079. for (i = 0; i < 4; i++)
  3080. ha->gold_fw_version[i] =
  3081. be32_to_cpu((__force __be32)dcode[4+i]);
  3082. return ret;
  3083. }
  3084. static int
  3085. qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
  3086. {
  3087. if (pos >= end || *pos != 0x82)
  3088. return 0;
  3089. pos += 3 + pos[1];
  3090. if (pos >= end || *pos != 0x90)
  3091. return 0;
  3092. pos += 3 + pos[1];
  3093. if (pos >= end || *pos != 0x78)
  3094. return 0;
  3095. return 1;
  3096. }
  3097. int
  3098. qla2xxx_get_vpd_field(scsi_qla_host_t *vha, char *key, char *str, size_t size)
  3099. {
  3100. struct qla_hw_data *ha = vha->hw;
  3101. uint8_t *pos = ha->vpd;
  3102. uint8_t *end = pos + ha->vpd_size;
  3103. int len = 0;
  3104. if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
  3105. return 0;
  3106. while (pos < end && *pos != 0x78) {
  3107. len = (*pos == 0x82) ? pos[1] : pos[2];
  3108. if (!strncmp(pos, key, strlen(key)))
  3109. break;
  3110. if (*pos != 0x90 && *pos != 0x91)
  3111. pos += len;
  3112. pos += 3;
  3113. }
  3114. if (pos < end - len && *pos != 0x78)
  3115. return scnprintf(str, size, "%.*s", len, pos + 3);
  3116. return 0;
  3117. }
  3118. int
  3119. qla24xx_read_fcp_prio_cfg(scsi_qla_host_t *vha)
  3120. {
  3121. int len, max_len;
  3122. uint32_t fcp_prio_addr;
  3123. struct qla_hw_data *ha = vha->hw;
  3124. if (!ha->fcp_prio_cfg) {
  3125. ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
  3126. if (!ha->fcp_prio_cfg) {
  3127. ql_log(ql_log_warn, vha, 0x00d5,
  3128. "Unable to allocate memory for fcp priority data (%x).\n",
  3129. FCP_PRIO_CFG_SIZE);
  3130. return QLA_FUNCTION_FAILED;
  3131. }
  3132. }
  3133. memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
  3134. fcp_prio_addr = ha->flt_region_fcp_prio;
  3135. /* first read the fcp priority data header from flash */
  3136. ha->isp_ops->read_optrom(vha, ha->fcp_prio_cfg,
  3137. fcp_prio_addr << 2, FCP_PRIO_CFG_HDR_SIZE);
  3138. if (!qla24xx_fcp_prio_cfg_valid(vha, ha->fcp_prio_cfg, 0))
  3139. goto fail;
  3140. /* read remaining FCP CMD config data from flash */
  3141. fcp_prio_addr += (FCP_PRIO_CFG_HDR_SIZE >> 2);
  3142. len = ha->fcp_prio_cfg->num_entries * sizeof(struct qla_fcp_prio_entry);
  3143. max_len = FCP_PRIO_CFG_SIZE - FCP_PRIO_CFG_HDR_SIZE;
  3144. ha->isp_ops->read_optrom(vha, &ha->fcp_prio_cfg->entry[0],
  3145. fcp_prio_addr << 2, (len < max_len ? len : max_len));
  3146. /* revalidate the entire FCP priority config data, including entries */
  3147. if (!qla24xx_fcp_prio_cfg_valid(vha, ha->fcp_prio_cfg, 1))
  3148. goto fail;
  3149. ha->flags.fcp_prio_enabled = 1;
  3150. return QLA_SUCCESS;
  3151. fail:
  3152. vfree(ha->fcp_prio_cfg);
  3153. ha->fcp_prio_cfg = NULL;
  3154. return QLA_FUNCTION_FAILED;
  3155. }