qla_sup.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2008 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/delay.h>
  9. #include <linux/vmalloc.h>
  10. #include <asm/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, uint16_t 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. qla2x00_nv_write(ha, NVR_DATA_OUT);
  171. qla2x00_nv_write(ha, 0);
  172. qla2x00_nv_write(ha, 0);
  173. for (word = 0; word < 8; word++)
  174. qla2x00_nv_write(ha, NVR_DATA_OUT);
  175. qla2x00_nv_deselect(ha);
  176. /* Write data */
  177. nv_cmd = (addr << 16) | NV_WRITE_OP;
  178. nv_cmd |= data;
  179. nv_cmd <<= 5;
  180. for (count = 0; count < 27; count++) {
  181. if (nv_cmd & BIT_31)
  182. qla2x00_nv_write(ha, NVR_DATA_OUT);
  183. else
  184. qla2x00_nv_write(ha, 0);
  185. nv_cmd <<= 1;
  186. }
  187. qla2x00_nv_deselect(ha);
  188. /* Wait for NVRAM to become ready */
  189. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  190. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  191. wait_cnt = NVR_WAIT_CNT;
  192. do {
  193. if (!--wait_cnt) {
  194. DEBUG9_10(qla_printk(KERN_WARNING, ha,
  195. "NVRAM didn't go ready...\n"));
  196. break;
  197. }
  198. NVRAM_DELAY();
  199. word = RD_REG_WORD(&reg->nvram);
  200. } while ((word & NVR_DATA_IN) == 0);
  201. qla2x00_nv_deselect(ha);
  202. /* Disable writes */
  203. qla2x00_nv_write(ha, NVR_DATA_OUT);
  204. for (count = 0; count < 10; count++)
  205. qla2x00_nv_write(ha, 0);
  206. qla2x00_nv_deselect(ha);
  207. }
  208. static int
  209. qla2x00_write_nvram_word_tmo(struct qla_hw_data *ha, uint32_t addr,
  210. uint16_t data, uint32_t tmo)
  211. {
  212. int ret, count;
  213. uint16_t word;
  214. uint32_t nv_cmd;
  215. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  216. ret = QLA_SUCCESS;
  217. qla2x00_nv_write(ha, NVR_DATA_OUT);
  218. qla2x00_nv_write(ha, 0);
  219. qla2x00_nv_write(ha, 0);
  220. for (word = 0; word < 8; word++)
  221. qla2x00_nv_write(ha, NVR_DATA_OUT);
  222. qla2x00_nv_deselect(ha);
  223. /* Write data */
  224. nv_cmd = (addr << 16) | NV_WRITE_OP;
  225. nv_cmd |= data;
  226. nv_cmd <<= 5;
  227. for (count = 0; count < 27; count++) {
  228. if (nv_cmd & BIT_31)
  229. qla2x00_nv_write(ha, NVR_DATA_OUT);
  230. else
  231. qla2x00_nv_write(ha, 0);
  232. nv_cmd <<= 1;
  233. }
  234. qla2x00_nv_deselect(ha);
  235. /* Wait for NVRAM to become ready */
  236. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  237. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  238. do {
  239. NVRAM_DELAY();
  240. word = RD_REG_WORD(&reg->nvram);
  241. if (!--tmo) {
  242. ret = QLA_FUNCTION_FAILED;
  243. break;
  244. }
  245. } while ((word & NVR_DATA_IN) == 0);
  246. qla2x00_nv_deselect(ha);
  247. /* Disable writes */
  248. qla2x00_nv_write(ha, NVR_DATA_OUT);
  249. for (count = 0; count < 10; count++)
  250. qla2x00_nv_write(ha, 0);
  251. qla2x00_nv_deselect(ha);
  252. return ret;
  253. }
  254. /**
  255. * qla2x00_clear_nvram_protection() -
  256. * @ha: HA context
  257. */
  258. static int
  259. qla2x00_clear_nvram_protection(struct qla_hw_data *ha)
  260. {
  261. int ret, stat;
  262. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  263. uint32_t word, wait_cnt;
  264. uint16_t wprot, wprot_old;
  265. /* Clear NVRAM write protection. */
  266. ret = QLA_FUNCTION_FAILED;
  267. wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  268. stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
  269. __constant_cpu_to_le16(0x1234), 100000);
  270. wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  271. if (stat != QLA_SUCCESS || wprot != 0x1234) {
  272. /* Write enable. */
  273. qla2x00_nv_write(ha, NVR_DATA_OUT);
  274. qla2x00_nv_write(ha, 0);
  275. qla2x00_nv_write(ha, 0);
  276. for (word = 0; word < 8; word++)
  277. qla2x00_nv_write(ha, NVR_DATA_OUT);
  278. qla2x00_nv_deselect(ha);
  279. /* Enable protection register. */
  280. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  281. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  282. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  283. for (word = 0; word < 8; word++)
  284. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  285. qla2x00_nv_deselect(ha);
  286. /* Clear protection register (ffff is cleared). */
  287. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  288. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  289. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  290. for (word = 0; word < 8; word++)
  291. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  292. qla2x00_nv_deselect(ha);
  293. /* Wait for NVRAM to become ready. */
  294. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  295. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  296. wait_cnt = NVR_WAIT_CNT;
  297. do {
  298. if (!--wait_cnt) {
  299. DEBUG9_10(qla_printk(KERN_WARNING, ha,
  300. "NVRAM didn't go ready...\n"));
  301. break;
  302. }
  303. NVRAM_DELAY();
  304. word = RD_REG_WORD(&reg->nvram);
  305. } while ((word & NVR_DATA_IN) == 0);
  306. if (wait_cnt)
  307. ret = QLA_SUCCESS;
  308. } else
  309. qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
  310. return ret;
  311. }
  312. static void
  313. qla2x00_set_nvram_protection(struct qla_hw_data *ha, int stat)
  314. {
  315. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  316. uint32_t word, wait_cnt;
  317. if (stat != QLA_SUCCESS)
  318. return;
  319. /* Set NVRAM write protection. */
  320. /* Write enable. */
  321. qla2x00_nv_write(ha, NVR_DATA_OUT);
  322. qla2x00_nv_write(ha, 0);
  323. qla2x00_nv_write(ha, 0);
  324. for (word = 0; word < 8; word++)
  325. qla2x00_nv_write(ha, NVR_DATA_OUT);
  326. qla2x00_nv_deselect(ha);
  327. /* Enable protection register. */
  328. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  329. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  330. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  331. for (word = 0; word < 8; word++)
  332. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  333. qla2x00_nv_deselect(ha);
  334. /* Enable protection register. */
  335. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  336. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  337. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  338. for (word = 0; word < 8; word++)
  339. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  340. qla2x00_nv_deselect(ha);
  341. /* Wait for NVRAM to become ready. */
  342. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  343. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  344. wait_cnt = NVR_WAIT_CNT;
  345. do {
  346. if (!--wait_cnt) {
  347. DEBUG9_10(qla_printk(KERN_WARNING, ha,
  348. "NVRAM didn't go ready...\n"));
  349. break;
  350. }
  351. NVRAM_DELAY();
  352. word = RD_REG_WORD(&reg->nvram);
  353. } while ((word & NVR_DATA_IN) == 0);
  354. }
  355. /*****************************************************************************/
  356. /* Flash Manipulation Routines */
  357. /*****************************************************************************/
  358. #define OPTROM_BURST_SIZE 0x1000
  359. #define OPTROM_BURST_DWORDS (OPTROM_BURST_SIZE / 4)
  360. static inline uint32_t
  361. flash_conf_addr(struct qla_hw_data *ha, uint32_t faddr)
  362. {
  363. return ha->flash_conf_off | faddr;
  364. }
  365. static inline uint32_t
  366. flash_data_addr(struct qla_hw_data *ha, uint32_t faddr)
  367. {
  368. return ha->flash_data_off | faddr;
  369. }
  370. static inline uint32_t
  371. nvram_conf_addr(struct qla_hw_data *ha, uint32_t naddr)
  372. {
  373. return ha->nvram_conf_off | naddr;
  374. }
  375. static inline uint32_t
  376. nvram_data_addr(struct qla_hw_data *ha, uint32_t naddr)
  377. {
  378. return ha->nvram_data_off | naddr;
  379. }
  380. static uint32_t
  381. qla24xx_read_flash_dword(struct qla_hw_data *ha, uint32_t addr)
  382. {
  383. int rval;
  384. uint32_t cnt, data;
  385. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  386. WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  387. /* Wait for READ cycle to complete. */
  388. rval = QLA_SUCCESS;
  389. for (cnt = 3000;
  390. (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
  391. rval == QLA_SUCCESS; cnt--) {
  392. if (cnt)
  393. udelay(10);
  394. else
  395. rval = QLA_FUNCTION_TIMEOUT;
  396. cond_resched();
  397. }
  398. /* TODO: What happens if we time out? */
  399. data = 0xDEADDEAD;
  400. if (rval == QLA_SUCCESS)
  401. data = RD_REG_DWORD(&reg->flash_data);
  402. return data;
  403. }
  404. uint32_t *
  405. qla24xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
  406. uint32_t dwords)
  407. {
  408. uint32_t i;
  409. struct qla_hw_data *ha = vha->hw;
  410. /* Dword reads to flash. */
  411. for (i = 0; i < dwords; i++, faddr++)
  412. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  413. flash_data_addr(ha, faddr)));
  414. return dwptr;
  415. }
  416. static int
  417. qla24xx_write_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t data)
  418. {
  419. int rval;
  420. uint32_t cnt;
  421. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  422. WRT_REG_DWORD(&reg->flash_data, data);
  423. RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
  424. WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
  425. /* Wait for Write cycle to complete. */
  426. rval = QLA_SUCCESS;
  427. for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
  428. rval == QLA_SUCCESS; cnt--) {
  429. if (cnt)
  430. udelay(10);
  431. else
  432. rval = QLA_FUNCTION_TIMEOUT;
  433. cond_resched();
  434. }
  435. return rval;
  436. }
  437. static void
  438. qla24xx_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
  439. uint8_t *flash_id)
  440. {
  441. uint32_t ids;
  442. ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x03ab));
  443. *man_id = LSB(ids);
  444. *flash_id = MSB(ids);
  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. ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x009f));
  454. *man_id = LSB(ids);
  455. *flash_id = MSB(ids);
  456. }
  457. }
  458. static int
  459. qla2xxx_find_flt_start(scsi_qla_host_t *vha, uint32_t *start)
  460. {
  461. const char *loc, *locations[] = { "DEF", "PCI" };
  462. uint32_t pcihdr, pcids;
  463. uint32_t *dcode;
  464. uint8_t *buf, *bcode, last_image;
  465. uint16_t cnt, chksum, *wptr;
  466. struct qla_flt_location *fltl;
  467. struct qla_hw_data *ha = vha->hw;
  468. struct req_que *req = ha->req_q_map[0];
  469. /*
  470. * FLT-location structure resides after the last PCI region.
  471. */
  472. /* Begin with sane defaults. */
  473. loc = locations[0];
  474. *start = 0;
  475. if (IS_QLA24XX_TYPE(ha))
  476. *start = FA_FLASH_LAYOUT_ADDR_24;
  477. else if (IS_QLA25XX(ha))
  478. *start = FA_FLASH_LAYOUT_ADDR;
  479. else if (IS_QLA81XX(ha))
  480. *start = FA_FLASH_LAYOUT_ADDR_81;
  481. /* Begin with first PCI expansion ROM header. */
  482. buf = (uint8_t *)req->ring;
  483. dcode = (uint32_t *)req->ring;
  484. pcihdr = 0;
  485. last_image = 1;
  486. do {
  487. /* Verify PCI expansion ROM header. */
  488. qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
  489. bcode = buf + (pcihdr % 4);
  490. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa)
  491. goto end;
  492. /* Locate PCI data structure. */
  493. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  494. qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
  495. bcode = buf + (pcihdr % 4);
  496. /* Validate signature of PCI data structure. */
  497. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  498. bcode[0x2] != 'I' || bcode[0x3] != 'R')
  499. goto end;
  500. last_image = bcode[0x15] & BIT_7;
  501. /* Locate next PCI expansion ROM. */
  502. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  503. } while (!last_image);
  504. /* Now verify FLT-location structure. */
  505. fltl = (struct qla_flt_location *)req->ring;
  506. qla24xx_read_flash_data(vha, dcode, pcihdr >> 2,
  507. sizeof(struct qla_flt_location) >> 2);
  508. if (fltl->sig[0] != 'Q' || fltl->sig[1] != 'F' ||
  509. fltl->sig[2] != 'L' || fltl->sig[3] != 'T')
  510. goto end;
  511. wptr = (uint16_t *)req->ring;
  512. cnt = sizeof(struct qla_flt_location) >> 1;
  513. for (chksum = 0; cnt; cnt--)
  514. chksum += le16_to_cpu(*wptr++);
  515. if (chksum) {
  516. qla_printk(KERN_ERR, ha,
  517. "Inconsistent FLTL detected: checksum=0x%x.\n", chksum);
  518. qla2x00_dump_buffer(buf, sizeof(struct qla_flt_location));
  519. return QLA_FUNCTION_FAILED;
  520. }
  521. /* Good data. Use specified location. */
  522. loc = locations[1];
  523. *start = (le16_to_cpu(fltl->start_hi) << 16 |
  524. le16_to_cpu(fltl->start_lo)) >> 2;
  525. end:
  526. DEBUG2(qla_printk(KERN_DEBUG, ha, "FLTL[%s] = 0x%x.\n", loc, *start));
  527. return QLA_SUCCESS;
  528. }
  529. static void
  530. qla2xxx_get_flt_info(scsi_qla_host_t *vha, uint32_t flt_addr)
  531. {
  532. const char *loc, *locations[] = { "DEF", "FLT" };
  533. const uint32_t def_fw[] =
  534. { FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR_81 };
  535. const uint32_t def_boot[] =
  536. { FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR_81 };
  537. const uint32_t def_vpd_nvram[] =
  538. { FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR_81 };
  539. const uint32_t def_vpd0[] =
  540. { 0, 0, FA_VPD0_ADDR_81 };
  541. const uint32_t def_vpd1[] =
  542. { 0, 0, FA_VPD1_ADDR_81 };
  543. const uint32_t def_nvram0[] =
  544. { 0, 0, FA_NVRAM0_ADDR_81 };
  545. const uint32_t def_nvram1[] =
  546. { 0, 0, FA_NVRAM1_ADDR_81 };
  547. const uint32_t def_fdt[] =
  548. { FA_FLASH_DESCR_ADDR_24, FA_FLASH_DESCR_ADDR,
  549. FA_FLASH_DESCR_ADDR_81 };
  550. const uint32_t def_npiv_conf0[] =
  551. { FA_NPIV_CONF0_ADDR_24, FA_NPIV_CONF0_ADDR,
  552. FA_NPIV_CONF0_ADDR_81 };
  553. const uint32_t def_npiv_conf1[] =
  554. { FA_NPIV_CONF1_ADDR_24, FA_NPIV_CONF1_ADDR,
  555. FA_NPIV_CONF1_ADDR_81 };
  556. uint32_t def;
  557. uint16_t *wptr;
  558. uint16_t cnt, chksum;
  559. uint32_t start;
  560. struct qla_flt_header *flt;
  561. struct qla_flt_region *region;
  562. struct qla_hw_data *ha = vha->hw;
  563. struct req_que *req = ha->req_q_map[0];
  564. ha->flt_region_flt = flt_addr;
  565. wptr = (uint16_t *)req->ring;
  566. flt = (struct qla_flt_header *)req->ring;
  567. region = (struct qla_flt_region *)&flt[1];
  568. ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
  569. flt_addr << 2, OPTROM_BURST_SIZE);
  570. if (*wptr == __constant_cpu_to_le16(0xffff))
  571. goto no_flash_data;
  572. if (flt->version != __constant_cpu_to_le16(1)) {
  573. DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported FLT detected: "
  574. "version=0x%x length=0x%x checksum=0x%x.\n",
  575. le16_to_cpu(flt->version), le16_to_cpu(flt->length),
  576. le16_to_cpu(flt->checksum)));
  577. goto no_flash_data;
  578. }
  579. cnt = (sizeof(struct qla_flt_header) + le16_to_cpu(flt->length)) >> 1;
  580. for (chksum = 0; cnt; cnt--)
  581. chksum += le16_to_cpu(*wptr++);
  582. if (chksum) {
  583. DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FLT detected: "
  584. "version=0x%x length=0x%x checksum=0x%x.\n",
  585. le16_to_cpu(flt->version), le16_to_cpu(flt->length),
  586. chksum));
  587. goto no_flash_data;
  588. }
  589. loc = locations[1];
  590. cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
  591. for ( ; cnt; cnt--, region++) {
  592. /* Store addresses as DWORD offsets. */
  593. start = le32_to_cpu(region->start) >> 2;
  594. DEBUG3(qla_printk(KERN_DEBUG, ha, "FLT[%02x]: start=0x%x "
  595. "end=0x%x size=0x%x.\n", le32_to_cpu(region->code), start,
  596. le32_to_cpu(region->end) >> 2, le32_to_cpu(region->size)));
  597. switch (le32_to_cpu(region->code) & 0xff) {
  598. case FLT_REG_FW:
  599. ha->flt_region_fw = start;
  600. break;
  601. case FLT_REG_BOOT_CODE:
  602. ha->flt_region_boot = start;
  603. break;
  604. case FLT_REG_VPD_0:
  605. ha->flt_region_vpd_nvram = start;
  606. if (!(PCI_FUNC(ha->pdev->devfn) & 1))
  607. ha->flt_region_vpd = start;
  608. break;
  609. case FLT_REG_VPD_1:
  610. if (PCI_FUNC(ha->pdev->devfn) & 1)
  611. ha->flt_region_vpd = start;
  612. break;
  613. case FLT_REG_NVRAM_0:
  614. if (!(PCI_FUNC(ha->pdev->devfn) & 1))
  615. ha->flt_region_nvram = start;
  616. break;
  617. case FLT_REG_NVRAM_1:
  618. if (PCI_FUNC(ha->pdev->devfn) & 1)
  619. ha->flt_region_nvram = start;
  620. break;
  621. case FLT_REG_FDT:
  622. ha->flt_region_fdt = start;
  623. break;
  624. case FLT_REG_NPIV_CONF_0:
  625. if (!(PCI_FUNC(ha->pdev->devfn) & 1))
  626. ha->flt_region_npiv_conf = start;
  627. break;
  628. case FLT_REG_NPIV_CONF_1:
  629. if (PCI_FUNC(ha->pdev->devfn) & 1)
  630. ha->flt_region_npiv_conf = start;
  631. break;
  632. }
  633. }
  634. goto done;
  635. no_flash_data:
  636. /* Use hardcoded defaults. */
  637. loc = locations[0];
  638. def = 0;
  639. if (IS_QLA24XX_TYPE(ha))
  640. def = 0;
  641. else if (IS_QLA25XX(ha))
  642. def = 1;
  643. else if (IS_QLA81XX(ha))
  644. def = 2;
  645. ha->flt_region_fw = def_fw[def];
  646. ha->flt_region_boot = def_boot[def];
  647. ha->flt_region_vpd_nvram = def_vpd_nvram[def];
  648. ha->flt_region_vpd = !(PCI_FUNC(ha->pdev->devfn) & 1) ?
  649. def_vpd0[def]: def_vpd1[def];
  650. ha->flt_region_nvram = !(PCI_FUNC(ha->pdev->devfn) & 1) ?
  651. def_nvram0[def]: def_nvram1[def];
  652. ha->flt_region_fdt = def_fdt[def];
  653. ha->flt_region_npiv_conf = !(PCI_FUNC(ha->pdev->devfn) & 1) ?
  654. def_npiv_conf0[def]: def_npiv_conf1[def];
  655. done:
  656. DEBUG2(qla_printk(KERN_DEBUG, ha, "FLT[%s]: boot=0x%x fw=0x%x "
  657. "vpd_nvram=0x%x vpd=0x%x nvram=0x%x fdt=0x%x flt=0x%x "
  658. "npiv=0x%x.\n", loc, ha->flt_region_boot, ha->flt_region_fw,
  659. ha->flt_region_vpd_nvram, ha->flt_region_vpd, ha->flt_region_nvram,
  660. ha->flt_region_fdt, ha->flt_region_flt, ha->flt_region_npiv_conf));
  661. }
  662. static void
  663. qla2xxx_get_fdt_info(scsi_qla_host_t *vha)
  664. {
  665. #define FLASH_BLK_SIZE_4K 0x1000
  666. #define FLASH_BLK_SIZE_32K 0x8000
  667. #define FLASH_BLK_SIZE_64K 0x10000
  668. const char *loc, *locations[] = { "MID", "FDT" };
  669. uint16_t cnt, chksum;
  670. uint16_t *wptr;
  671. struct qla_fdt_layout *fdt;
  672. uint8_t man_id, flash_id;
  673. uint16_t mid, fid;
  674. struct qla_hw_data *ha = vha->hw;
  675. struct req_que *req = ha->req_q_map[0];
  676. wptr = (uint16_t *)req->ring;
  677. fdt = (struct qla_fdt_layout *)req->ring;
  678. ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
  679. ha->flt_region_fdt << 2, OPTROM_BURST_SIZE);
  680. if (*wptr == __constant_cpu_to_le16(0xffff))
  681. goto no_flash_data;
  682. if (fdt->sig[0] != 'Q' || fdt->sig[1] != 'L' || fdt->sig[2] != 'I' ||
  683. fdt->sig[3] != 'D')
  684. goto no_flash_data;
  685. for (cnt = 0, chksum = 0; cnt < sizeof(struct qla_fdt_layout) >> 1;
  686. cnt++)
  687. chksum += le16_to_cpu(*wptr++);
  688. if (chksum) {
  689. DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FDT detected: "
  690. "checksum=0x%x id=%c version=0x%x.\n", chksum, fdt->sig[0],
  691. le16_to_cpu(fdt->version)));
  692. DEBUG9(qla2x00_dump_buffer((uint8_t *)fdt, sizeof(*fdt)));
  693. goto no_flash_data;
  694. }
  695. loc = locations[1];
  696. mid = le16_to_cpu(fdt->man_id);
  697. fid = le16_to_cpu(fdt->id);
  698. ha->fdt_wrt_disable = fdt->wrt_disable_bits;
  699. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0300 | fdt->erase_cmd);
  700. ha->fdt_block_size = le32_to_cpu(fdt->block_size);
  701. if (fdt->unprotect_sec_cmd) {
  702. ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0300 |
  703. fdt->unprotect_sec_cmd);
  704. ha->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
  705. flash_conf_addr(ha, 0x0300 | fdt->protect_sec_cmd):
  706. flash_conf_addr(ha, 0x0336);
  707. }
  708. goto done;
  709. no_flash_data:
  710. loc = locations[0];
  711. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  712. mid = man_id;
  713. fid = flash_id;
  714. ha->fdt_wrt_disable = 0x9c;
  715. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x03d8);
  716. switch (man_id) {
  717. case 0xbf: /* STT flash. */
  718. if (flash_id == 0x8e)
  719. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  720. else
  721. ha->fdt_block_size = FLASH_BLK_SIZE_32K;
  722. if (flash_id == 0x80)
  723. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0352);
  724. break;
  725. case 0x13: /* ST M25P80. */
  726. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  727. break;
  728. case 0x1f: /* Atmel 26DF081A. */
  729. ha->fdt_block_size = FLASH_BLK_SIZE_4K;
  730. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0320);
  731. ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0339);
  732. ha->fdt_protect_sec_cmd = flash_conf_addr(ha, 0x0336);
  733. break;
  734. default:
  735. /* Default to 64 kb sector size. */
  736. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  737. break;
  738. }
  739. done:
  740. DEBUG2(qla_printk(KERN_DEBUG, ha, "FDT[%s]: (0x%x/0x%x) erase=0x%x "
  741. "pro=%x upro=%x wrtd=0x%x blk=0x%x.\n", loc, mid, fid,
  742. ha->fdt_erase_cmd, ha->fdt_protect_sec_cmd,
  743. ha->fdt_unprotect_sec_cmd, ha->fdt_wrt_disable,
  744. ha->fdt_block_size));
  745. }
  746. int
  747. qla2xxx_get_flash_info(scsi_qla_host_t *vha)
  748. {
  749. int ret;
  750. uint32_t flt_addr;
  751. struct qla_hw_data *ha = vha->hw;
  752. if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA81XX(ha))
  753. return QLA_SUCCESS;
  754. ret = qla2xxx_find_flt_start(vha, &flt_addr);
  755. if (ret != QLA_SUCCESS)
  756. return ret;
  757. qla2xxx_get_flt_info(vha, flt_addr);
  758. qla2xxx_get_fdt_info(vha);
  759. return QLA_SUCCESS;
  760. }
  761. void
  762. qla2xxx_flash_npiv_conf(scsi_qla_host_t *vha)
  763. {
  764. #define NPIV_CONFIG_SIZE (16*1024)
  765. void *data;
  766. uint16_t *wptr;
  767. uint16_t cnt, chksum;
  768. int i;
  769. struct qla_npiv_header hdr;
  770. struct qla_npiv_entry *entry;
  771. struct qla_hw_data *ha = vha->hw;
  772. if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA81XX(ha))
  773. return;
  774. ha->isp_ops->read_optrom(vha, (uint8_t *)&hdr,
  775. ha->flt_region_npiv_conf << 2, sizeof(struct qla_npiv_header));
  776. if (hdr.version == __constant_cpu_to_le16(0xffff))
  777. return;
  778. if (hdr.version != __constant_cpu_to_le16(1)) {
  779. DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported NPIV-Config "
  780. "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
  781. le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
  782. le16_to_cpu(hdr.checksum)));
  783. return;
  784. }
  785. data = kmalloc(NPIV_CONFIG_SIZE, GFP_KERNEL);
  786. if (!data) {
  787. DEBUG2(qla_printk(KERN_INFO, ha, "NPIV-Config: Unable to "
  788. "allocate memory.\n"));
  789. return;
  790. }
  791. ha->isp_ops->read_optrom(vha, (uint8_t *)data,
  792. ha->flt_region_npiv_conf << 2, NPIV_CONFIG_SIZE);
  793. cnt = (sizeof(struct qla_npiv_header) + le16_to_cpu(hdr.entries) *
  794. sizeof(struct qla_npiv_entry)) >> 1;
  795. for (wptr = data, chksum = 0; cnt; cnt--)
  796. chksum += le16_to_cpu(*wptr++);
  797. if (chksum) {
  798. DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent NPIV-Config "
  799. "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
  800. le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
  801. chksum));
  802. goto done;
  803. }
  804. entry = data + sizeof(struct qla_npiv_header);
  805. cnt = le16_to_cpu(hdr.entries);
  806. ha->flex_port_count = cnt;
  807. for (i = 0; cnt; cnt--, entry++, i++) {
  808. uint16_t flags;
  809. struct fc_vport_identifiers vid;
  810. struct fc_vport *vport;
  811. flags = le16_to_cpu(entry->flags);
  812. if (flags == 0xffff)
  813. continue;
  814. if ((flags & BIT_0) == 0)
  815. continue;
  816. memset(&vid, 0, sizeof(vid));
  817. vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
  818. vid.vport_type = FC_PORTTYPE_NPIV;
  819. vid.disable = false;
  820. vid.port_name = wwn_to_u64(entry->port_name);
  821. vid.node_name = wwn_to_u64(entry->node_name);
  822. memcpy(&ha->npiv_info[i], entry, sizeof(struct qla_npiv_entry));
  823. DEBUG2(qla_printk(KERN_DEBUG, ha, "NPIV[%02x]: wwpn=%llx "
  824. "wwnn=%llx vf_id=0x%x Q_qos=0x%x F_qos=0x%x.\n", cnt,
  825. vid.port_name, vid.node_name, le16_to_cpu(entry->vf_id),
  826. entry->q_qos, entry->f_qos));
  827. if (i < QLA_PRECONFIG_VPORTS) {
  828. vport = fc_vport_create(vha->host, 0, &vid);
  829. if (!vport)
  830. qla_printk(KERN_INFO, ha,
  831. "NPIV-Config: Failed to create vport [%02x]: "
  832. "wwpn=%llx wwnn=%llx.\n", cnt,
  833. vid.port_name, vid.node_name);
  834. }
  835. }
  836. done:
  837. kfree(data);
  838. ha->npiv_info = NULL;
  839. }
  840. static int
  841. qla24xx_unprotect_flash(scsi_qla_host_t *vha)
  842. {
  843. struct qla_hw_data *ha = vha->hw;
  844. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  845. if (ha->flags.fac_supported)
  846. return qla81xx_fac_do_write_enable(vha, 1);
  847. /* Enable flash write. */
  848. WRT_REG_DWORD(&reg->ctrl_status,
  849. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  850. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  851. if (!ha->fdt_wrt_disable)
  852. goto done;
  853. /* Disable flash write-protection, first clear SR protection bit */
  854. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
  855. /* Then write zero again to clear remaining SR bits.*/
  856. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
  857. done:
  858. return QLA_SUCCESS;
  859. }
  860. static int
  861. qla24xx_protect_flash(scsi_qla_host_t *vha)
  862. {
  863. uint32_t cnt;
  864. struct qla_hw_data *ha = vha->hw;
  865. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  866. if (ha->flags.fac_supported)
  867. return qla81xx_fac_do_write_enable(vha, 0);
  868. if (!ha->fdt_wrt_disable)
  869. goto skip_wrt_protect;
  870. /* Enable flash write-protection and wait for completion. */
  871. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101),
  872. ha->fdt_wrt_disable);
  873. for (cnt = 300; cnt &&
  874. qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x005)) & BIT_0;
  875. cnt--) {
  876. udelay(10);
  877. }
  878. skip_wrt_protect:
  879. /* Disable flash write. */
  880. WRT_REG_DWORD(&reg->ctrl_status,
  881. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  882. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  883. return QLA_SUCCESS;
  884. }
  885. static int
  886. qla24xx_erase_sector(scsi_qla_host_t *vha, uint32_t fdata)
  887. {
  888. struct qla_hw_data *ha = vha->hw;
  889. uint32_t start, finish;
  890. if (ha->flags.fac_supported) {
  891. start = fdata >> 2;
  892. finish = start + (ha->fdt_block_size >> 2) - 1;
  893. return qla81xx_fac_erase_sector(vha, flash_data_addr(ha,
  894. start), flash_data_addr(ha, finish));
  895. }
  896. return qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
  897. (fdata & 0xff00) | ((fdata << 16) & 0xff0000) |
  898. ((fdata >> 16) & 0xff));
  899. }
  900. static int
  901. qla24xx_write_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
  902. uint32_t dwords)
  903. {
  904. int ret;
  905. uint32_t liter;
  906. uint32_t sec_mask, rest_addr;
  907. uint32_t fdata;
  908. dma_addr_t optrom_dma;
  909. void *optrom = NULL;
  910. struct qla_hw_data *ha = vha->hw;
  911. /* Prepare burst-capable write on supported ISPs. */
  912. if ((IS_QLA25XX(ha) || IS_QLA81XX(ha)) && !(faddr & 0xfff) &&
  913. dwords > OPTROM_BURST_DWORDS) {
  914. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  915. &optrom_dma, GFP_KERNEL);
  916. if (!optrom) {
  917. qla_printk(KERN_DEBUG, ha,
  918. "Unable to allocate memory for optrom burst write "
  919. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  920. }
  921. }
  922. rest_addr = (ha->fdt_block_size >> 2) - 1;
  923. sec_mask = ~rest_addr;
  924. ret = qla24xx_unprotect_flash(vha);
  925. if (ret != QLA_SUCCESS) {
  926. qla_printk(KERN_WARNING, ha,
  927. "Unable to unprotect flash for update.\n");
  928. goto done;
  929. }
  930. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  931. fdata = (faddr & sec_mask) << 2;
  932. /* Are we at the beginning of a sector? */
  933. if ((faddr & rest_addr) == 0) {
  934. /* Do sector unprotect. */
  935. if (ha->fdt_unprotect_sec_cmd)
  936. qla24xx_write_flash_dword(ha,
  937. ha->fdt_unprotect_sec_cmd,
  938. (fdata & 0xff00) | ((fdata << 16) &
  939. 0xff0000) | ((fdata >> 16) & 0xff));
  940. ret = qla24xx_erase_sector(vha, fdata);
  941. if (ret != QLA_SUCCESS) {
  942. DEBUG9(qla_printk(KERN_WARNING, ha,
  943. "Unable to erase sector: address=%x.\n",
  944. faddr));
  945. break;
  946. }
  947. }
  948. /* Go with burst-write. */
  949. if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
  950. /* Copy data to DMA'ble buffer. */
  951. memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
  952. ret = qla2x00_load_ram(vha, optrom_dma,
  953. flash_data_addr(ha, faddr),
  954. OPTROM_BURST_DWORDS);
  955. if (ret != QLA_SUCCESS) {
  956. qla_printk(KERN_WARNING, ha,
  957. "Unable to burst-write optrom segment "
  958. "(%x/%x/%llx).\n", ret,
  959. flash_data_addr(ha, faddr),
  960. (unsigned long long)optrom_dma);
  961. qla_printk(KERN_WARNING, ha,
  962. "Reverting to slow-write.\n");
  963. dma_free_coherent(&ha->pdev->dev,
  964. OPTROM_BURST_SIZE, optrom, optrom_dma);
  965. optrom = NULL;
  966. } else {
  967. liter += OPTROM_BURST_DWORDS - 1;
  968. faddr += OPTROM_BURST_DWORDS - 1;
  969. dwptr += OPTROM_BURST_DWORDS - 1;
  970. continue;
  971. }
  972. }
  973. ret = qla24xx_write_flash_dword(ha,
  974. flash_data_addr(ha, faddr), cpu_to_le32(*dwptr));
  975. if (ret != QLA_SUCCESS) {
  976. DEBUG9(printk("%s(%ld) Unable to program flash "
  977. "address=%x data=%x.\n", __func__,
  978. vha->host_no, faddr, *dwptr));
  979. break;
  980. }
  981. /* Do sector protect. */
  982. if (ha->fdt_unprotect_sec_cmd &&
  983. ((faddr & rest_addr) == rest_addr))
  984. qla24xx_write_flash_dword(ha,
  985. ha->fdt_protect_sec_cmd,
  986. (fdata & 0xff00) | ((fdata << 16) &
  987. 0xff0000) | ((fdata >> 16) & 0xff));
  988. }
  989. ret = qla24xx_protect_flash(vha);
  990. if (ret != QLA_SUCCESS)
  991. qla_printk(KERN_WARNING, ha,
  992. "Unable to protect flash after update.\n");
  993. done:
  994. if (optrom)
  995. dma_free_coherent(&ha->pdev->dev,
  996. OPTROM_BURST_SIZE, optrom, optrom_dma);
  997. return ret;
  998. }
  999. uint8_t *
  1000. qla2x00_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  1001. uint32_t bytes)
  1002. {
  1003. uint32_t i;
  1004. uint16_t *wptr;
  1005. struct qla_hw_data *ha = vha->hw;
  1006. /* Word reads to NVRAM via registers. */
  1007. wptr = (uint16_t *)buf;
  1008. qla2x00_lock_nvram_access(ha);
  1009. for (i = 0; i < bytes >> 1; i++, naddr++)
  1010. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  1011. naddr));
  1012. qla2x00_unlock_nvram_access(ha);
  1013. return buf;
  1014. }
  1015. uint8_t *
  1016. qla24xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  1017. uint32_t bytes)
  1018. {
  1019. uint32_t i;
  1020. uint32_t *dwptr;
  1021. struct qla_hw_data *ha = vha->hw;
  1022. /* Dword reads to flash. */
  1023. dwptr = (uint32_t *)buf;
  1024. for (i = 0; i < bytes >> 2; i++, naddr++)
  1025. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  1026. nvram_data_addr(ha, naddr)));
  1027. return buf;
  1028. }
  1029. int
  1030. qla2x00_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  1031. uint32_t bytes)
  1032. {
  1033. int ret, stat;
  1034. uint32_t i;
  1035. uint16_t *wptr;
  1036. unsigned long flags;
  1037. struct qla_hw_data *ha = vha->hw;
  1038. ret = QLA_SUCCESS;
  1039. spin_lock_irqsave(&ha->hardware_lock, flags);
  1040. qla2x00_lock_nvram_access(ha);
  1041. /* Disable NVRAM write-protection. */
  1042. stat = qla2x00_clear_nvram_protection(ha);
  1043. wptr = (uint16_t *)buf;
  1044. for (i = 0; i < bytes >> 1; i++, naddr++) {
  1045. qla2x00_write_nvram_word(ha, naddr,
  1046. cpu_to_le16(*wptr));
  1047. wptr++;
  1048. }
  1049. /* Enable NVRAM write-protection. */
  1050. qla2x00_set_nvram_protection(ha, stat);
  1051. qla2x00_unlock_nvram_access(ha);
  1052. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1053. return ret;
  1054. }
  1055. int
  1056. qla24xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  1057. uint32_t bytes)
  1058. {
  1059. int ret;
  1060. uint32_t i;
  1061. uint32_t *dwptr;
  1062. struct qla_hw_data *ha = vha->hw;
  1063. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1064. ret = QLA_SUCCESS;
  1065. /* Enable flash write. */
  1066. WRT_REG_DWORD(&reg->ctrl_status,
  1067. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  1068. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  1069. /* Disable NVRAM write-protection. */
  1070. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
  1071. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
  1072. /* Dword writes to flash. */
  1073. dwptr = (uint32_t *)buf;
  1074. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  1075. ret = qla24xx_write_flash_dword(ha,
  1076. nvram_data_addr(ha, naddr), cpu_to_le32(*dwptr));
  1077. if (ret != QLA_SUCCESS) {
  1078. DEBUG9(qla_printk(KERN_WARNING, ha,
  1079. "Unable to program nvram address=%x data=%x.\n",
  1080. naddr, *dwptr));
  1081. break;
  1082. }
  1083. }
  1084. /* Enable NVRAM write-protection. */
  1085. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0x8c);
  1086. /* Disable flash write. */
  1087. WRT_REG_DWORD(&reg->ctrl_status,
  1088. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  1089. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  1090. return ret;
  1091. }
  1092. uint8_t *
  1093. qla25xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  1094. uint32_t bytes)
  1095. {
  1096. uint32_t i;
  1097. uint32_t *dwptr;
  1098. struct qla_hw_data *ha = vha->hw;
  1099. /* Dword reads to flash. */
  1100. dwptr = (uint32_t *)buf;
  1101. for (i = 0; i < bytes >> 2; i++, naddr++)
  1102. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  1103. flash_data_addr(ha, ha->flt_region_vpd_nvram | naddr)));
  1104. return buf;
  1105. }
  1106. int
  1107. qla25xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  1108. uint32_t bytes)
  1109. {
  1110. struct qla_hw_data *ha = vha->hw;
  1111. #define RMW_BUFFER_SIZE (64 * 1024)
  1112. uint8_t *dbuf;
  1113. dbuf = vmalloc(RMW_BUFFER_SIZE);
  1114. if (!dbuf)
  1115. return QLA_MEMORY_ALLOC_FAILED;
  1116. ha->isp_ops->read_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
  1117. RMW_BUFFER_SIZE);
  1118. memcpy(dbuf + (naddr << 2), buf, bytes);
  1119. ha->isp_ops->write_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
  1120. RMW_BUFFER_SIZE);
  1121. vfree(dbuf);
  1122. return QLA_SUCCESS;
  1123. }
  1124. static inline void
  1125. qla2x00_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
  1126. {
  1127. if (IS_QLA2322(ha)) {
  1128. /* Flip all colors. */
  1129. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  1130. /* Turn off. */
  1131. ha->beacon_color_state = 0;
  1132. *pflags = GPIO_LED_ALL_OFF;
  1133. } else {
  1134. /* Turn on. */
  1135. ha->beacon_color_state = QLA_LED_ALL_ON;
  1136. *pflags = GPIO_LED_RGA_ON;
  1137. }
  1138. } else {
  1139. /* Flip green led only. */
  1140. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  1141. /* Turn off. */
  1142. ha->beacon_color_state = 0;
  1143. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  1144. } else {
  1145. /* Turn on. */
  1146. ha->beacon_color_state = QLA_LED_GRN_ON;
  1147. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  1148. }
  1149. }
  1150. }
  1151. #define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
  1152. void
  1153. qla2x00_beacon_blink(struct scsi_qla_host *vha)
  1154. {
  1155. uint16_t gpio_enable;
  1156. uint16_t gpio_data;
  1157. uint16_t led_color = 0;
  1158. unsigned long flags;
  1159. struct qla_hw_data *ha = vha->hw;
  1160. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1161. spin_lock_irqsave(&ha->hardware_lock, flags);
  1162. /* Save the Original GPIOE. */
  1163. if (ha->pio_address) {
  1164. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  1165. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  1166. } else {
  1167. gpio_enable = RD_REG_WORD(&reg->gpioe);
  1168. gpio_data = RD_REG_WORD(&reg->gpiod);
  1169. }
  1170. /* Set the modified gpio_enable values */
  1171. gpio_enable |= GPIO_LED_MASK;
  1172. if (ha->pio_address) {
  1173. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  1174. } else {
  1175. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  1176. RD_REG_WORD(&reg->gpioe);
  1177. }
  1178. qla2x00_flip_colors(ha, &led_color);
  1179. /* Clear out any previously set LED color. */
  1180. gpio_data &= ~GPIO_LED_MASK;
  1181. /* Set the new input LED color to GPIOD. */
  1182. gpio_data |= led_color;
  1183. /* Set the modified gpio_data values */
  1184. if (ha->pio_address) {
  1185. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  1186. } else {
  1187. WRT_REG_WORD(&reg->gpiod, gpio_data);
  1188. RD_REG_WORD(&reg->gpiod);
  1189. }
  1190. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1191. }
  1192. int
  1193. qla2x00_beacon_on(struct scsi_qla_host *vha)
  1194. {
  1195. uint16_t gpio_enable;
  1196. uint16_t gpio_data;
  1197. unsigned long flags;
  1198. struct qla_hw_data *ha = vha->hw;
  1199. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1200. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  1201. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  1202. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1203. qla_printk(KERN_WARNING, ha,
  1204. "Unable to update fw options (beacon on).\n");
  1205. return QLA_FUNCTION_FAILED;
  1206. }
  1207. /* Turn off LEDs. */
  1208. spin_lock_irqsave(&ha->hardware_lock, flags);
  1209. if (ha->pio_address) {
  1210. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  1211. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  1212. } else {
  1213. gpio_enable = RD_REG_WORD(&reg->gpioe);
  1214. gpio_data = RD_REG_WORD(&reg->gpiod);
  1215. }
  1216. gpio_enable |= GPIO_LED_MASK;
  1217. /* Set the modified gpio_enable values. */
  1218. if (ha->pio_address) {
  1219. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  1220. } else {
  1221. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  1222. RD_REG_WORD(&reg->gpioe);
  1223. }
  1224. /* Clear out previously set LED colour. */
  1225. gpio_data &= ~GPIO_LED_MASK;
  1226. if (ha->pio_address) {
  1227. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  1228. } else {
  1229. WRT_REG_WORD(&reg->gpiod, gpio_data);
  1230. RD_REG_WORD(&reg->gpiod);
  1231. }
  1232. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1233. /*
  1234. * Let the per HBA timer kick off the blinking process based on
  1235. * the following flags. No need to do anything else now.
  1236. */
  1237. ha->beacon_blink_led = 1;
  1238. ha->beacon_color_state = 0;
  1239. return QLA_SUCCESS;
  1240. }
  1241. int
  1242. qla2x00_beacon_off(struct scsi_qla_host *vha)
  1243. {
  1244. int rval = QLA_SUCCESS;
  1245. struct qla_hw_data *ha = vha->hw;
  1246. ha->beacon_blink_led = 0;
  1247. /* Set the on flag so when it gets flipped it will be off. */
  1248. if (IS_QLA2322(ha))
  1249. ha->beacon_color_state = QLA_LED_ALL_ON;
  1250. else
  1251. ha->beacon_color_state = QLA_LED_GRN_ON;
  1252. ha->isp_ops->beacon_blink(vha); /* This turns green LED off */
  1253. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  1254. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  1255. rval = qla2x00_set_fw_options(vha, ha->fw_options);
  1256. if (rval != QLA_SUCCESS)
  1257. qla_printk(KERN_WARNING, ha,
  1258. "Unable to update fw options (beacon off).\n");
  1259. return rval;
  1260. }
  1261. static inline void
  1262. qla24xx_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
  1263. {
  1264. /* Flip all colors. */
  1265. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  1266. /* Turn off. */
  1267. ha->beacon_color_state = 0;
  1268. *pflags = 0;
  1269. } else {
  1270. /* Turn on. */
  1271. ha->beacon_color_state = QLA_LED_ALL_ON;
  1272. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  1273. }
  1274. }
  1275. void
  1276. qla24xx_beacon_blink(struct scsi_qla_host *vha)
  1277. {
  1278. uint16_t led_color = 0;
  1279. uint32_t gpio_data;
  1280. unsigned long flags;
  1281. struct qla_hw_data *ha = vha->hw;
  1282. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1283. /* Save the Original GPIOD. */
  1284. spin_lock_irqsave(&ha->hardware_lock, flags);
  1285. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1286. /* Enable the gpio_data reg for update. */
  1287. gpio_data |= GPDX_LED_UPDATE_MASK;
  1288. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1289. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1290. /* Set the color bits. */
  1291. qla24xx_flip_colors(ha, &led_color);
  1292. /* Clear out any previously set LED color. */
  1293. gpio_data &= ~GPDX_LED_COLOR_MASK;
  1294. /* Set the new input LED color to GPIOD. */
  1295. gpio_data |= led_color;
  1296. /* Set the modified gpio_data values. */
  1297. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1298. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1299. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1300. }
  1301. int
  1302. qla24xx_beacon_on(struct scsi_qla_host *vha)
  1303. {
  1304. uint32_t gpio_data;
  1305. unsigned long flags;
  1306. struct qla_hw_data *ha = vha->hw;
  1307. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1308. if (ha->beacon_blink_led == 0) {
  1309. /* Enable firmware for update */
  1310. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1311. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS)
  1312. return QLA_FUNCTION_FAILED;
  1313. if (qla2x00_get_fw_options(vha, ha->fw_options) !=
  1314. QLA_SUCCESS) {
  1315. qla_printk(KERN_WARNING, ha,
  1316. "Unable to update fw options (beacon on).\n");
  1317. return QLA_FUNCTION_FAILED;
  1318. }
  1319. spin_lock_irqsave(&ha->hardware_lock, flags);
  1320. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1321. /* Enable the gpio_data reg for update. */
  1322. gpio_data |= GPDX_LED_UPDATE_MASK;
  1323. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1324. RD_REG_DWORD(&reg->gpiod);
  1325. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1326. }
  1327. /* So all colors blink together. */
  1328. ha->beacon_color_state = 0;
  1329. /* Let the per HBA timer kick off the blinking process. */
  1330. ha->beacon_blink_led = 1;
  1331. return QLA_SUCCESS;
  1332. }
  1333. int
  1334. qla24xx_beacon_off(struct scsi_qla_host *vha)
  1335. {
  1336. uint32_t gpio_data;
  1337. unsigned long flags;
  1338. struct qla_hw_data *ha = vha->hw;
  1339. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1340. ha->beacon_blink_led = 0;
  1341. ha->beacon_color_state = QLA_LED_ALL_ON;
  1342. ha->isp_ops->beacon_blink(vha); /* Will flip to all off. */
  1343. /* Give control back to firmware. */
  1344. spin_lock_irqsave(&ha->hardware_lock, flags);
  1345. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1346. /* Disable the gpio_data reg for update. */
  1347. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  1348. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1349. RD_REG_DWORD(&reg->gpiod);
  1350. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1351. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1352. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1353. qla_printk(KERN_WARNING, ha,
  1354. "Unable to update fw options (beacon off).\n");
  1355. return QLA_FUNCTION_FAILED;
  1356. }
  1357. if (qla2x00_get_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1358. qla_printk(KERN_WARNING, ha,
  1359. "Unable to get fw options (beacon off).\n");
  1360. return QLA_FUNCTION_FAILED;
  1361. }
  1362. return QLA_SUCCESS;
  1363. }
  1364. /*
  1365. * Flash support routines
  1366. */
  1367. /**
  1368. * qla2x00_flash_enable() - Setup flash for reading and writing.
  1369. * @ha: HA context
  1370. */
  1371. static void
  1372. qla2x00_flash_enable(struct qla_hw_data *ha)
  1373. {
  1374. uint16_t data;
  1375. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1376. data = RD_REG_WORD(&reg->ctrl_status);
  1377. data |= CSR_FLASH_ENABLE;
  1378. WRT_REG_WORD(&reg->ctrl_status, data);
  1379. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1380. }
  1381. /**
  1382. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  1383. * @ha: HA context
  1384. */
  1385. static void
  1386. qla2x00_flash_disable(struct qla_hw_data *ha)
  1387. {
  1388. uint16_t data;
  1389. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1390. data = RD_REG_WORD(&reg->ctrl_status);
  1391. data &= ~(CSR_FLASH_ENABLE);
  1392. WRT_REG_WORD(&reg->ctrl_status, data);
  1393. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1394. }
  1395. /**
  1396. * qla2x00_read_flash_byte() - Reads a byte from flash
  1397. * @ha: HA context
  1398. * @addr: Address in flash to read
  1399. *
  1400. * A word is read from the chip, but, only the lower byte is valid.
  1401. *
  1402. * Returns the byte read from flash @addr.
  1403. */
  1404. static uint8_t
  1405. qla2x00_read_flash_byte(struct qla_hw_data *ha, uint32_t addr)
  1406. {
  1407. uint16_t data;
  1408. uint16_t bank_select;
  1409. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1410. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1411. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1412. /* Specify 64K address range: */
  1413. /* clear out Module Select and Flash Address bits [19:16]. */
  1414. bank_select &= ~0xf8;
  1415. bank_select |= addr >> 12 & 0xf0;
  1416. bank_select |= CSR_FLASH_64K_BANK;
  1417. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1418. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1419. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1420. data = RD_REG_WORD(&reg->flash_data);
  1421. return (uint8_t)data;
  1422. }
  1423. /* Setup bit 16 of flash address. */
  1424. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1425. bank_select |= CSR_FLASH_64K_BANK;
  1426. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1427. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1428. } else if (((addr & BIT_16) == 0) &&
  1429. (bank_select & CSR_FLASH_64K_BANK)) {
  1430. bank_select &= ~(CSR_FLASH_64K_BANK);
  1431. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1432. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1433. }
  1434. /* Always perform IO mapped accesses to the FLASH registers. */
  1435. if (ha->pio_address) {
  1436. uint16_t data2;
  1437. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1438. do {
  1439. data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1440. barrier();
  1441. cpu_relax();
  1442. data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1443. } while (data != data2);
  1444. } else {
  1445. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1446. data = qla2x00_debounce_register(&reg->flash_data);
  1447. }
  1448. return (uint8_t)data;
  1449. }
  1450. /**
  1451. * qla2x00_write_flash_byte() - Write a byte to flash
  1452. * @ha: HA context
  1453. * @addr: Address in flash to write
  1454. * @data: Data to write
  1455. */
  1456. static void
  1457. qla2x00_write_flash_byte(struct qla_hw_data *ha, uint32_t addr, uint8_t data)
  1458. {
  1459. uint16_t bank_select;
  1460. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1461. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1462. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1463. /* Specify 64K address range: */
  1464. /* clear out Module Select and Flash Address bits [19:16]. */
  1465. bank_select &= ~0xf8;
  1466. bank_select |= addr >> 12 & 0xf0;
  1467. bank_select |= CSR_FLASH_64K_BANK;
  1468. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1469. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1470. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1471. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1472. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1473. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1474. return;
  1475. }
  1476. /* Setup bit 16 of flash address. */
  1477. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1478. bank_select |= CSR_FLASH_64K_BANK;
  1479. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1480. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1481. } else if (((addr & BIT_16) == 0) &&
  1482. (bank_select & CSR_FLASH_64K_BANK)) {
  1483. bank_select &= ~(CSR_FLASH_64K_BANK);
  1484. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1485. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1486. }
  1487. /* Always perform IO mapped accesses to the FLASH registers. */
  1488. if (ha->pio_address) {
  1489. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1490. WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
  1491. } else {
  1492. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1493. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1494. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1495. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1496. }
  1497. }
  1498. /**
  1499. * qla2x00_poll_flash() - Polls flash for completion.
  1500. * @ha: HA context
  1501. * @addr: Address in flash to poll
  1502. * @poll_data: Data to be polled
  1503. * @man_id: Flash manufacturer ID
  1504. * @flash_id: Flash ID
  1505. *
  1506. * This function polls the device until bit 7 of what is read matches data
  1507. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1508. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1509. * reading bit 5 as a 1.
  1510. *
  1511. * Returns 0 on success, else non-zero.
  1512. */
  1513. static int
  1514. qla2x00_poll_flash(struct qla_hw_data *ha, uint32_t addr, uint8_t poll_data,
  1515. uint8_t man_id, uint8_t flash_id)
  1516. {
  1517. int status;
  1518. uint8_t flash_data;
  1519. uint32_t cnt;
  1520. status = 1;
  1521. /* Wait for 30 seconds for command to finish. */
  1522. poll_data &= BIT_7;
  1523. for (cnt = 3000000; cnt; cnt--) {
  1524. flash_data = qla2x00_read_flash_byte(ha, addr);
  1525. if ((flash_data & BIT_7) == poll_data) {
  1526. status = 0;
  1527. break;
  1528. }
  1529. if (man_id != 0x40 && man_id != 0xda) {
  1530. if ((flash_data & BIT_5) && cnt > 2)
  1531. cnt = 2;
  1532. }
  1533. udelay(10);
  1534. barrier();
  1535. cond_resched();
  1536. }
  1537. return status;
  1538. }
  1539. /**
  1540. * qla2x00_program_flash_address() - Programs a flash address
  1541. * @ha: HA context
  1542. * @addr: Address in flash to program
  1543. * @data: Data to be written in flash
  1544. * @man_id: Flash manufacturer ID
  1545. * @flash_id: Flash ID
  1546. *
  1547. * Returns 0 on success, else non-zero.
  1548. */
  1549. static int
  1550. qla2x00_program_flash_address(struct qla_hw_data *ha, uint32_t addr,
  1551. uint8_t data, uint8_t man_id, uint8_t flash_id)
  1552. {
  1553. /* Write Program Command Sequence. */
  1554. if (IS_OEM_001(ha)) {
  1555. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1556. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1557. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1558. qla2x00_write_flash_byte(ha, addr, data);
  1559. } else {
  1560. if (man_id == 0xda && flash_id == 0xc1) {
  1561. qla2x00_write_flash_byte(ha, addr, data);
  1562. if (addr & 0x7e)
  1563. return 0;
  1564. } else {
  1565. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1566. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1567. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1568. qla2x00_write_flash_byte(ha, addr, data);
  1569. }
  1570. }
  1571. udelay(150);
  1572. /* Wait for write to complete. */
  1573. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1574. }
  1575. /**
  1576. * qla2x00_erase_flash() - Erase the flash.
  1577. * @ha: HA context
  1578. * @man_id: Flash manufacturer ID
  1579. * @flash_id: Flash ID
  1580. *
  1581. * Returns 0 on success, else non-zero.
  1582. */
  1583. static int
  1584. qla2x00_erase_flash(struct qla_hw_data *ha, uint8_t man_id, uint8_t flash_id)
  1585. {
  1586. /* Individual Sector Erase Command Sequence */
  1587. if (IS_OEM_001(ha)) {
  1588. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1589. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1590. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1591. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1592. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1593. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1594. } else {
  1595. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1596. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1597. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1598. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1599. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1600. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1601. }
  1602. udelay(150);
  1603. /* Wait for erase to complete. */
  1604. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1605. }
  1606. /**
  1607. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1608. * @ha: HA context
  1609. * @addr: Flash sector to erase
  1610. * @sec_mask: Sector address mask
  1611. * @man_id: Flash manufacturer ID
  1612. * @flash_id: Flash ID
  1613. *
  1614. * Returns 0 on success, else non-zero.
  1615. */
  1616. static int
  1617. qla2x00_erase_flash_sector(struct qla_hw_data *ha, uint32_t addr,
  1618. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1619. {
  1620. /* Individual Sector Erase Command Sequence */
  1621. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1622. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1623. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1624. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1625. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1626. if (man_id == 0x1f && flash_id == 0x13)
  1627. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1628. else
  1629. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1630. udelay(150);
  1631. /* Wait for erase to complete. */
  1632. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1633. }
  1634. /**
  1635. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1636. * @man_id: Flash manufacturer ID
  1637. * @flash_id: Flash ID
  1638. */
  1639. static void
  1640. qla2x00_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
  1641. uint8_t *flash_id)
  1642. {
  1643. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1644. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1645. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1646. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1647. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1648. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1649. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1650. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1651. }
  1652. static void
  1653. qla2x00_read_flash_data(struct qla_hw_data *ha, uint8_t *tmp_buf,
  1654. uint32_t saddr, uint32_t length)
  1655. {
  1656. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1657. uint32_t midpoint, ilength;
  1658. uint8_t data;
  1659. midpoint = length / 2;
  1660. WRT_REG_WORD(&reg->nvram, 0);
  1661. RD_REG_WORD(&reg->nvram);
  1662. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1663. if (ilength == midpoint) {
  1664. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1665. RD_REG_WORD(&reg->nvram);
  1666. }
  1667. data = qla2x00_read_flash_byte(ha, saddr);
  1668. if (saddr % 100)
  1669. udelay(10);
  1670. *tmp_buf = data;
  1671. cond_resched();
  1672. }
  1673. }
  1674. static inline void
  1675. qla2x00_suspend_hba(struct scsi_qla_host *vha)
  1676. {
  1677. int cnt;
  1678. unsigned long flags;
  1679. struct qla_hw_data *ha = vha->hw;
  1680. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1681. /* Suspend HBA. */
  1682. scsi_block_requests(vha->host);
  1683. ha->isp_ops->disable_intrs(ha);
  1684. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1685. /* Pause RISC. */
  1686. spin_lock_irqsave(&ha->hardware_lock, flags);
  1687. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1688. RD_REG_WORD(&reg->hccr);
  1689. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1690. for (cnt = 0; cnt < 30000; cnt++) {
  1691. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1692. break;
  1693. udelay(100);
  1694. }
  1695. } else {
  1696. udelay(10);
  1697. }
  1698. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1699. }
  1700. static inline void
  1701. qla2x00_resume_hba(struct scsi_qla_host *vha)
  1702. {
  1703. struct qla_hw_data *ha = vha->hw;
  1704. /* Resume HBA. */
  1705. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1706. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  1707. qla2xxx_wake_dpc(vha);
  1708. qla2x00_wait_for_chip_reset(vha);
  1709. scsi_unblock_requests(vha->host);
  1710. }
  1711. uint8_t *
  1712. qla2x00_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1713. uint32_t offset, uint32_t length)
  1714. {
  1715. uint32_t addr, midpoint;
  1716. uint8_t *data;
  1717. struct qla_hw_data *ha = vha->hw;
  1718. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1719. /* Suspend HBA. */
  1720. qla2x00_suspend_hba(vha);
  1721. /* Go with read. */
  1722. midpoint = ha->optrom_size / 2;
  1723. qla2x00_flash_enable(ha);
  1724. WRT_REG_WORD(&reg->nvram, 0);
  1725. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1726. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1727. if (addr == midpoint) {
  1728. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1729. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1730. }
  1731. *data = qla2x00_read_flash_byte(ha, addr);
  1732. }
  1733. qla2x00_flash_disable(ha);
  1734. /* Resume HBA. */
  1735. qla2x00_resume_hba(vha);
  1736. return buf;
  1737. }
  1738. int
  1739. qla2x00_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1740. uint32_t offset, uint32_t length)
  1741. {
  1742. int rval;
  1743. uint8_t man_id, flash_id, sec_number, data;
  1744. uint16_t wd;
  1745. uint32_t addr, liter, sec_mask, rest_addr;
  1746. struct qla_hw_data *ha = vha->hw;
  1747. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1748. /* Suspend HBA. */
  1749. qla2x00_suspend_hba(vha);
  1750. rval = QLA_SUCCESS;
  1751. sec_number = 0;
  1752. /* Reset ISP chip. */
  1753. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1754. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1755. /* Go with write. */
  1756. qla2x00_flash_enable(ha);
  1757. do { /* Loop once to provide quick error exit */
  1758. /* Structure of flash memory based on manufacturer */
  1759. if (IS_OEM_001(ha)) {
  1760. /* OEM variant with special flash part. */
  1761. man_id = flash_id = 0;
  1762. rest_addr = 0xffff;
  1763. sec_mask = 0x10000;
  1764. goto update_flash;
  1765. }
  1766. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1767. switch (man_id) {
  1768. case 0x20: /* ST flash. */
  1769. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1770. /*
  1771. * ST m29w008at part - 64kb sector size with
  1772. * 32kb,8kb,8kb,16kb sectors at memory address
  1773. * 0xf0000.
  1774. */
  1775. rest_addr = 0xffff;
  1776. sec_mask = 0x10000;
  1777. break;
  1778. }
  1779. /*
  1780. * ST m29w010b part - 16kb sector size
  1781. * Default to 16kb sectors
  1782. */
  1783. rest_addr = 0x3fff;
  1784. sec_mask = 0x1c000;
  1785. break;
  1786. case 0x40: /* Mostel flash. */
  1787. /* Mostel v29c51001 part - 512 byte sector size. */
  1788. rest_addr = 0x1ff;
  1789. sec_mask = 0x1fe00;
  1790. break;
  1791. case 0xbf: /* SST flash. */
  1792. /* SST39sf10 part - 4kb sector size. */
  1793. rest_addr = 0xfff;
  1794. sec_mask = 0x1f000;
  1795. break;
  1796. case 0xda: /* Winbond flash. */
  1797. /* Winbond W29EE011 part - 256 byte sector size. */
  1798. rest_addr = 0x7f;
  1799. sec_mask = 0x1ff80;
  1800. break;
  1801. case 0xc2: /* Macronix flash. */
  1802. /* 64k sector size. */
  1803. if (flash_id == 0x38 || flash_id == 0x4f) {
  1804. rest_addr = 0xffff;
  1805. sec_mask = 0x10000;
  1806. break;
  1807. }
  1808. /* Fall through... */
  1809. case 0x1f: /* Atmel flash. */
  1810. /* 512k sector size. */
  1811. if (flash_id == 0x13) {
  1812. rest_addr = 0x7fffffff;
  1813. sec_mask = 0x80000000;
  1814. break;
  1815. }
  1816. /* Fall through... */
  1817. case 0x01: /* AMD flash. */
  1818. if (flash_id == 0x38 || flash_id == 0x40 ||
  1819. flash_id == 0x4f) {
  1820. /* Am29LV081 part - 64kb sector size. */
  1821. /* Am29LV002BT part - 64kb sector size. */
  1822. rest_addr = 0xffff;
  1823. sec_mask = 0x10000;
  1824. break;
  1825. } else if (flash_id == 0x3e) {
  1826. /*
  1827. * Am29LV008b part - 64kb sector size with
  1828. * 32kb,8kb,8kb,16kb sector at memory address
  1829. * h0xf0000.
  1830. */
  1831. rest_addr = 0xffff;
  1832. sec_mask = 0x10000;
  1833. break;
  1834. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1835. /*
  1836. * Am29LV010 part or AM29f010 - 16kb sector
  1837. * size.
  1838. */
  1839. rest_addr = 0x3fff;
  1840. sec_mask = 0x1c000;
  1841. break;
  1842. } else if (flash_id == 0x6d) {
  1843. /* Am29LV001 part - 8kb sector size. */
  1844. rest_addr = 0x1fff;
  1845. sec_mask = 0x1e000;
  1846. break;
  1847. }
  1848. default:
  1849. /* Default to 16 kb sector size. */
  1850. rest_addr = 0x3fff;
  1851. sec_mask = 0x1c000;
  1852. break;
  1853. }
  1854. update_flash:
  1855. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1856. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1857. rval = QLA_FUNCTION_FAILED;
  1858. break;
  1859. }
  1860. }
  1861. for (addr = offset, liter = 0; liter < length; liter++,
  1862. addr++) {
  1863. data = buf[liter];
  1864. /* Are we at the beginning of a sector? */
  1865. if ((addr & rest_addr) == 0) {
  1866. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1867. if (addr >= 0x10000UL) {
  1868. if (((addr >> 12) & 0xf0) &&
  1869. ((man_id == 0x01 &&
  1870. flash_id == 0x3e) ||
  1871. (man_id == 0x20 &&
  1872. flash_id == 0xd2))) {
  1873. sec_number++;
  1874. if (sec_number == 1) {
  1875. rest_addr =
  1876. 0x7fff;
  1877. sec_mask =
  1878. 0x18000;
  1879. } else if (
  1880. sec_number == 2 ||
  1881. sec_number == 3) {
  1882. rest_addr =
  1883. 0x1fff;
  1884. sec_mask =
  1885. 0x1e000;
  1886. } else if (
  1887. sec_number == 4) {
  1888. rest_addr =
  1889. 0x3fff;
  1890. sec_mask =
  1891. 0x1c000;
  1892. }
  1893. }
  1894. }
  1895. } else if (addr == ha->optrom_size / 2) {
  1896. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1897. RD_REG_WORD(&reg->nvram);
  1898. }
  1899. if (flash_id == 0xda && man_id == 0xc1) {
  1900. qla2x00_write_flash_byte(ha, 0x5555,
  1901. 0xaa);
  1902. qla2x00_write_flash_byte(ha, 0x2aaa,
  1903. 0x55);
  1904. qla2x00_write_flash_byte(ha, 0x5555,
  1905. 0xa0);
  1906. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1907. /* Then erase it */
  1908. if (qla2x00_erase_flash_sector(ha,
  1909. addr, sec_mask, man_id,
  1910. flash_id)) {
  1911. rval = QLA_FUNCTION_FAILED;
  1912. break;
  1913. }
  1914. if (man_id == 0x01 && flash_id == 0x6d)
  1915. sec_number++;
  1916. }
  1917. }
  1918. if (man_id == 0x01 && flash_id == 0x6d) {
  1919. if (sec_number == 1 &&
  1920. addr == (rest_addr - 1)) {
  1921. rest_addr = 0x0fff;
  1922. sec_mask = 0x1f000;
  1923. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1924. rest_addr = 0x3fff;
  1925. sec_mask = 0x1c000;
  1926. }
  1927. }
  1928. if (qla2x00_program_flash_address(ha, addr, data,
  1929. man_id, flash_id)) {
  1930. rval = QLA_FUNCTION_FAILED;
  1931. break;
  1932. }
  1933. cond_resched();
  1934. }
  1935. } while (0);
  1936. qla2x00_flash_disable(ha);
  1937. /* Resume HBA. */
  1938. qla2x00_resume_hba(vha);
  1939. return rval;
  1940. }
  1941. uint8_t *
  1942. qla24xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1943. uint32_t offset, uint32_t length)
  1944. {
  1945. struct qla_hw_data *ha = vha->hw;
  1946. /* Suspend HBA. */
  1947. scsi_block_requests(vha->host);
  1948. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1949. /* Go with read. */
  1950. qla24xx_read_flash_data(vha, (uint32_t *)buf, offset >> 2, length >> 2);
  1951. /* Resume HBA. */
  1952. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1953. scsi_unblock_requests(vha->host);
  1954. return buf;
  1955. }
  1956. int
  1957. qla24xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1958. uint32_t offset, uint32_t length)
  1959. {
  1960. int rval;
  1961. struct qla_hw_data *ha = vha->hw;
  1962. /* Suspend HBA. */
  1963. scsi_block_requests(vha->host);
  1964. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1965. /* Go with write. */
  1966. rval = qla24xx_write_flash_data(vha, (uint32_t *)buf, offset >> 2,
  1967. length >> 2);
  1968. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1969. scsi_unblock_requests(vha->host);
  1970. return rval;
  1971. }
  1972. uint8_t *
  1973. qla25xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1974. uint32_t offset, uint32_t length)
  1975. {
  1976. int rval;
  1977. dma_addr_t optrom_dma;
  1978. void *optrom;
  1979. uint8_t *pbuf;
  1980. uint32_t faddr, left, burst;
  1981. struct qla_hw_data *ha = vha->hw;
  1982. if (offset & 0xfff)
  1983. goto slow_read;
  1984. if (length < OPTROM_BURST_SIZE)
  1985. goto slow_read;
  1986. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1987. &optrom_dma, GFP_KERNEL);
  1988. if (!optrom) {
  1989. qla_printk(KERN_DEBUG, ha,
  1990. "Unable to allocate memory for optrom burst read "
  1991. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  1992. goto slow_read;
  1993. }
  1994. pbuf = buf;
  1995. faddr = offset >> 2;
  1996. left = length >> 2;
  1997. burst = OPTROM_BURST_DWORDS;
  1998. while (left != 0) {
  1999. if (burst > left)
  2000. burst = left;
  2001. rval = qla2x00_dump_ram(vha, optrom_dma,
  2002. flash_data_addr(ha, faddr), burst);
  2003. if (rval) {
  2004. qla_printk(KERN_WARNING, ha,
  2005. "Unable to burst-read optrom segment "
  2006. "(%x/%x/%llx).\n", rval,
  2007. flash_data_addr(ha, faddr),
  2008. (unsigned long long)optrom_dma);
  2009. qla_printk(KERN_WARNING, ha,
  2010. "Reverting to slow-read.\n");
  2011. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  2012. optrom, optrom_dma);
  2013. goto slow_read;
  2014. }
  2015. memcpy(pbuf, optrom, burst * 4);
  2016. left -= burst;
  2017. faddr += burst;
  2018. pbuf += burst * 4;
  2019. }
  2020. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
  2021. optrom_dma);
  2022. return buf;
  2023. slow_read:
  2024. return qla24xx_read_optrom_data(vha, buf, offset, length);
  2025. }
  2026. /**
  2027. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  2028. * @ha: HA context
  2029. * @pcids: Pointer to the FCODE PCI data structure
  2030. *
  2031. * The process of retrieving the FCODE version information is at best
  2032. * described as interesting.
  2033. *
  2034. * Within the first 100h bytes of the image an ASCII string is present
  2035. * which contains several pieces of information including the FCODE
  2036. * version. Unfortunately it seems the only reliable way to retrieve
  2037. * the version is by scanning for another sentinel within the string,
  2038. * the FCODE build date:
  2039. *
  2040. * ... 2.00.02 10/17/02 ...
  2041. *
  2042. * Returns QLA_SUCCESS on successful retrieval of version.
  2043. */
  2044. static void
  2045. qla2x00_get_fcode_version(struct qla_hw_data *ha, uint32_t pcids)
  2046. {
  2047. int ret = QLA_FUNCTION_FAILED;
  2048. uint32_t istart, iend, iter, vend;
  2049. uint8_t do_next, rbyte, *vbyte;
  2050. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2051. /* Skip the PCI data structure. */
  2052. istart = pcids +
  2053. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  2054. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  2055. iend = istart + 0x100;
  2056. do {
  2057. /* Scan for the sentinel date string...eeewww. */
  2058. do_next = 0;
  2059. iter = istart;
  2060. while ((iter < iend) && !do_next) {
  2061. iter++;
  2062. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  2063. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  2064. '/')
  2065. do_next++;
  2066. else if (qla2x00_read_flash_byte(ha,
  2067. iter + 3) == '/')
  2068. do_next++;
  2069. }
  2070. }
  2071. if (!do_next)
  2072. break;
  2073. /* Backtrack to previous ' ' (space). */
  2074. do_next = 0;
  2075. while ((iter > istart) && !do_next) {
  2076. iter--;
  2077. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  2078. do_next++;
  2079. }
  2080. if (!do_next)
  2081. break;
  2082. /*
  2083. * Mark end of version tag, and find previous ' ' (space) or
  2084. * string length (recent FCODE images -- major hack ahead!!!).
  2085. */
  2086. vend = iter - 1;
  2087. do_next = 0;
  2088. while ((iter > istart) && !do_next) {
  2089. iter--;
  2090. rbyte = qla2x00_read_flash_byte(ha, iter);
  2091. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  2092. do_next++;
  2093. }
  2094. if (!do_next)
  2095. break;
  2096. /* Mark beginning of version tag, and copy data. */
  2097. iter++;
  2098. if ((vend - iter) &&
  2099. ((vend - iter) < sizeof(ha->fcode_revision))) {
  2100. vbyte = ha->fcode_revision;
  2101. while (iter <= vend) {
  2102. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  2103. iter++;
  2104. }
  2105. ret = QLA_SUCCESS;
  2106. }
  2107. } while (0);
  2108. if (ret != QLA_SUCCESS)
  2109. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2110. }
  2111. int
  2112. qla2x00_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2113. {
  2114. int ret = QLA_SUCCESS;
  2115. uint8_t code_type, last_image;
  2116. uint32_t pcihdr, pcids;
  2117. uint8_t *dbyte;
  2118. uint16_t *dcode;
  2119. struct qla_hw_data *ha = vha->hw;
  2120. if (!ha->pio_address || !mbuf)
  2121. return QLA_FUNCTION_FAILED;
  2122. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2123. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2124. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2125. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2126. qla2x00_flash_enable(ha);
  2127. /* Begin with first PCI expansion ROM header. */
  2128. pcihdr = 0;
  2129. last_image = 1;
  2130. do {
  2131. /* Verify PCI expansion ROM header. */
  2132. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  2133. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  2134. /* No signature */
  2135. DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
  2136. "signature.\n"));
  2137. ret = QLA_FUNCTION_FAILED;
  2138. break;
  2139. }
  2140. /* Locate PCI data structure. */
  2141. pcids = pcihdr +
  2142. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  2143. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  2144. /* Validate signature of PCI data structure. */
  2145. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  2146. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  2147. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  2148. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  2149. /* Incorrect header. */
  2150. DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
  2151. "found pcir_adr=%x.\n", pcids));
  2152. ret = QLA_FUNCTION_FAILED;
  2153. break;
  2154. }
  2155. /* Read version */
  2156. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  2157. switch (code_type) {
  2158. case ROM_CODE_TYPE_BIOS:
  2159. /* Intel x86, PC-AT compatible. */
  2160. ha->bios_revision[0] =
  2161. qla2x00_read_flash_byte(ha, pcids + 0x12);
  2162. ha->bios_revision[1] =
  2163. qla2x00_read_flash_byte(ha, pcids + 0x13);
  2164. DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
  2165. ha->bios_revision[1], ha->bios_revision[0]));
  2166. break;
  2167. case ROM_CODE_TYPE_FCODE:
  2168. /* Open Firmware standard for PCI (FCode). */
  2169. /* Eeeewww... */
  2170. qla2x00_get_fcode_version(ha, pcids);
  2171. break;
  2172. case ROM_CODE_TYPE_EFI:
  2173. /* Extensible Firmware Interface (EFI). */
  2174. ha->efi_revision[0] =
  2175. qla2x00_read_flash_byte(ha, pcids + 0x12);
  2176. ha->efi_revision[1] =
  2177. qla2x00_read_flash_byte(ha, pcids + 0x13);
  2178. DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
  2179. ha->efi_revision[1], ha->efi_revision[0]));
  2180. break;
  2181. default:
  2182. DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
  2183. "type %x at pcids %x.\n", code_type, pcids));
  2184. break;
  2185. }
  2186. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  2187. /* Locate next PCI expansion ROM. */
  2188. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  2189. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  2190. } while (!last_image);
  2191. if (IS_QLA2322(ha)) {
  2192. /* Read firmware image information. */
  2193. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2194. dbyte = mbuf;
  2195. memset(dbyte, 0, 8);
  2196. dcode = (uint16_t *)dbyte;
  2197. qla2x00_read_flash_data(ha, dbyte, ha->flt_region_fw * 4 + 10,
  2198. 8);
  2199. DEBUG3(qla_printk(KERN_DEBUG, ha, "dumping fw ver from "
  2200. "flash:\n"));
  2201. DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
  2202. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  2203. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  2204. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  2205. dcode[3] == 0)) {
  2206. DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
  2207. "revision at %x.\n", ha->flt_region_fw * 4));
  2208. } else {
  2209. /* values are in big endian */
  2210. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  2211. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  2212. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  2213. }
  2214. }
  2215. qla2x00_flash_disable(ha);
  2216. return ret;
  2217. }
  2218. int
  2219. qla24xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2220. {
  2221. int ret = QLA_SUCCESS;
  2222. uint32_t pcihdr, pcids;
  2223. uint32_t *dcode;
  2224. uint8_t *bcode;
  2225. uint8_t code_type, last_image;
  2226. int i;
  2227. struct qla_hw_data *ha = vha->hw;
  2228. if (!mbuf)
  2229. return QLA_FUNCTION_FAILED;
  2230. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2231. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2232. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2233. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2234. dcode = mbuf;
  2235. /* Begin with first PCI expansion ROM header. */
  2236. pcihdr = ha->flt_region_boot << 2;
  2237. last_image = 1;
  2238. do {
  2239. /* Verify PCI expansion ROM header. */
  2240. qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
  2241. bcode = mbuf + (pcihdr % 4);
  2242. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
  2243. /* No signature */
  2244. DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
  2245. "signature.\n"));
  2246. ret = QLA_FUNCTION_FAILED;
  2247. break;
  2248. }
  2249. /* Locate PCI data structure. */
  2250. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  2251. qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
  2252. bcode = mbuf + (pcihdr % 4);
  2253. /* Validate signature of PCI data structure. */
  2254. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  2255. bcode[0x2] != 'I' || bcode[0x3] != 'R') {
  2256. /* Incorrect header. */
  2257. DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
  2258. "found pcir_adr=%x.\n", pcids));
  2259. ret = QLA_FUNCTION_FAILED;
  2260. break;
  2261. }
  2262. /* Read version */
  2263. code_type = bcode[0x14];
  2264. switch (code_type) {
  2265. case ROM_CODE_TYPE_BIOS:
  2266. /* Intel x86, PC-AT compatible. */
  2267. ha->bios_revision[0] = bcode[0x12];
  2268. ha->bios_revision[1] = bcode[0x13];
  2269. DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
  2270. ha->bios_revision[1], ha->bios_revision[0]));
  2271. break;
  2272. case ROM_CODE_TYPE_FCODE:
  2273. /* Open Firmware standard for PCI (FCode). */
  2274. ha->fcode_revision[0] = bcode[0x12];
  2275. ha->fcode_revision[1] = bcode[0x13];
  2276. DEBUG3(qla_printk(KERN_DEBUG, ha, "read FCODE %d.%d.\n",
  2277. ha->fcode_revision[1], ha->fcode_revision[0]));
  2278. break;
  2279. case ROM_CODE_TYPE_EFI:
  2280. /* Extensible Firmware Interface (EFI). */
  2281. ha->efi_revision[0] = bcode[0x12];
  2282. ha->efi_revision[1] = bcode[0x13];
  2283. DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
  2284. ha->efi_revision[1], ha->efi_revision[0]));
  2285. break;
  2286. default:
  2287. DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
  2288. "type %x at pcids %x.\n", code_type, pcids));
  2289. break;
  2290. }
  2291. last_image = bcode[0x15] & BIT_7;
  2292. /* Locate next PCI expansion ROM. */
  2293. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  2294. } while (!last_image);
  2295. /* Read firmware image information. */
  2296. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2297. dcode = mbuf;
  2298. qla24xx_read_flash_data(vha, dcode, ha->flt_region_fw + 4, 4);
  2299. for (i = 0; i < 4; i++)
  2300. dcode[i] = be32_to_cpu(dcode[i]);
  2301. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  2302. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  2303. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  2304. dcode[3] == 0)) {
  2305. DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
  2306. "revision at %x.\n", ha->flt_region_fw * 4));
  2307. } else {
  2308. ha->fw_revision[0] = dcode[0];
  2309. ha->fw_revision[1] = dcode[1];
  2310. ha->fw_revision[2] = dcode[2];
  2311. ha->fw_revision[3] = dcode[3];
  2312. }
  2313. return ret;
  2314. }
  2315. static int
  2316. qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
  2317. {
  2318. if (pos >= end || *pos != 0x82)
  2319. return 0;
  2320. pos += 3 + pos[1];
  2321. if (pos >= end || *pos != 0x90)
  2322. return 0;
  2323. pos += 3 + pos[1];
  2324. if (pos >= end || *pos != 0x78)
  2325. return 0;
  2326. return 1;
  2327. }
  2328. int
  2329. qla2xxx_get_vpd_field(scsi_qla_host_t *vha, char *key, char *str, size_t size)
  2330. {
  2331. struct qla_hw_data *ha = vha->hw;
  2332. uint8_t *pos = ha->vpd;
  2333. uint8_t *end = pos + ha->vpd_size;
  2334. int len = 0;
  2335. if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
  2336. return 0;
  2337. while (pos < end && *pos != 0x78) {
  2338. len = (*pos == 0x82) ? pos[1] : pos[2];
  2339. if (!strncmp(pos, key, strlen(key)))
  2340. break;
  2341. if (*pos != 0x90 && *pos != 0x91)
  2342. pos += len;
  2343. pos += 3;
  2344. }
  2345. if (pos < end - len && *pos != 0x78)
  2346. return snprintf(str, size, "%.*s", len, pos + 3);
  2347. return 0;
  2348. }