qla_sup.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2005 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 <asm/uaccess.h>
  10. static uint16_t qla2x00_nvram_request(scsi_qla_host_t *, uint32_t);
  11. static void qla2x00_nv_deselect(scsi_qla_host_t *);
  12. static void qla2x00_nv_write(scsi_qla_host_t *, uint16_t);
  13. /*
  14. * NVRAM support routines
  15. */
  16. /**
  17. * qla2x00_lock_nvram_access() -
  18. * @ha: HA context
  19. */
  20. void
  21. qla2x00_lock_nvram_access(scsi_qla_host_t *ha)
  22. {
  23. uint16_t data;
  24. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  25. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  26. data = RD_REG_WORD(&reg->nvram);
  27. while (data & NVR_BUSY) {
  28. udelay(100);
  29. data = RD_REG_WORD(&reg->nvram);
  30. }
  31. /* Lock resource */
  32. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
  33. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  34. udelay(5);
  35. data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  36. while ((data & BIT_0) == 0) {
  37. /* Lock failed */
  38. udelay(100);
  39. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
  40. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  41. udelay(5);
  42. data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  43. }
  44. }
  45. }
  46. /**
  47. * qla2x00_unlock_nvram_access() -
  48. * @ha: HA context
  49. */
  50. void
  51. qla2x00_unlock_nvram_access(scsi_qla_host_t *ha)
  52. {
  53. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  54. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  55. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0);
  56. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  57. }
  58. }
  59. /**
  60. * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
  61. * request routine to get the word from NVRAM.
  62. * @ha: HA context
  63. * @addr: Address in NVRAM to read
  64. *
  65. * Returns the word read from nvram @addr.
  66. */
  67. uint16_t
  68. qla2x00_get_nvram_word(scsi_qla_host_t *ha, uint32_t addr)
  69. {
  70. uint16_t data;
  71. uint32_t nv_cmd;
  72. nv_cmd = addr << 16;
  73. nv_cmd |= NV_READ_OP;
  74. data = qla2x00_nvram_request(ha, nv_cmd);
  75. return (data);
  76. }
  77. /**
  78. * qla2x00_write_nvram_word() - Write NVRAM data.
  79. * @ha: HA context
  80. * @addr: Address in NVRAM to write
  81. * @data: word to program
  82. */
  83. void
  84. qla2x00_write_nvram_word(scsi_qla_host_t *ha, uint32_t addr, uint16_t data)
  85. {
  86. int count;
  87. uint16_t word;
  88. uint32_t nv_cmd, wait_cnt;
  89. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  90. qla2x00_nv_write(ha, NVR_DATA_OUT);
  91. qla2x00_nv_write(ha, 0);
  92. qla2x00_nv_write(ha, 0);
  93. for (word = 0; word < 8; word++)
  94. qla2x00_nv_write(ha, NVR_DATA_OUT);
  95. qla2x00_nv_deselect(ha);
  96. /* Write data */
  97. nv_cmd = (addr << 16) | NV_WRITE_OP;
  98. nv_cmd |= data;
  99. nv_cmd <<= 5;
  100. for (count = 0; count < 27; count++) {
  101. if (nv_cmd & BIT_31)
  102. qla2x00_nv_write(ha, NVR_DATA_OUT);
  103. else
  104. qla2x00_nv_write(ha, 0);
  105. nv_cmd <<= 1;
  106. }
  107. qla2x00_nv_deselect(ha);
  108. /* Wait for NVRAM to become ready */
  109. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  110. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  111. wait_cnt = NVR_WAIT_CNT;
  112. do {
  113. if (!--wait_cnt) {
  114. DEBUG9_10(printk("%s(%ld): NVRAM didn't go ready...\n",
  115. __func__, ha->host_no));
  116. break;
  117. }
  118. NVRAM_DELAY();
  119. word = RD_REG_WORD(&reg->nvram);
  120. } while ((word & NVR_DATA_IN) == 0);
  121. qla2x00_nv_deselect(ha);
  122. /* Disable writes */
  123. qla2x00_nv_write(ha, NVR_DATA_OUT);
  124. for (count = 0; count < 10; count++)
  125. qla2x00_nv_write(ha, 0);
  126. qla2x00_nv_deselect(ha);
  127. }
  128. static int
  129. qla2x00_write_nvram_word_tmo(scsi_qla_host_t *ha, uint32_t addr, uint16_t data,
  130. uint32_t tmo)
  131. {
  132. int ret, count;
  133. uint16_t word;
  134. uint32_t nv_cmd;
  135. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  136. ret = QLA_SUCCESS;
  137. qla2x00_nv_write(ha, NVR_DATA_OUT);
  138. qla2x00_nv_write(ha, 0);
  139. qla2x00_nv_write(ha, 0);
  140. for (word = 0; word < 8; word++)
  141. qla2x00_nv_write(ha, NVR_DATA_OUT);
  142. qla2x00_nv_deselect(ha);
  143. /* Write data */
  144. nv_cmd = (addr << 16) | NV_WRITE_OP;
  145. nv_cmd |= data;
  146. nv_cmd <<= 5;
  147. for (count = 0; count < 27; count++) {
  148. if (nv_cmd & BIT_31)
  149. qla2x00_nv_write(ha, NVR_DATA_OUT);
  150. else
  151. qla2x00_nv_write(ha, 0);
  152. nv_cmd <<= 1;
  153. }
  154. qla2x00_nv_deselect(ha);
  155. /* Wait for NVRAM to become ready */
  156. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  157. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  158. do {
  159. NVRAM_DELAY();
  160. word = RD_REG_WORD(&reg->nvram);
  161. if (!--tmo) {
  162. ret = QLA_FUNCTION_FAILED;
  163. break;
  164. }
  165. } while ((word & NVR_DATA_IN) == 0);
  166. qla2x00_nv_deselect(ha);
  167. /* Disable writes */
  168. qla2x00_nv_write(ha, NVR_DATA_OUT);
  169. for (count = 0; count < 10; count++)
  170. qla2x00_nv_write(ha, 0);
  171. qla2x00_nv_deselect(ha);
  172. return ret;
  173. }
  174. /**
  175. * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
  176. * NVRAM.
  177. * @ha: HA context
  178. * @nv_cmd: NVRAM command
  179. *
  180. * Bit definitions for NVRAM command:
  181. *
  182. * Bit 26 = start bit
  183. * Bit 25, 24 = opcode
  184. * Bit 23-16 = address
  185. * Bit 15-0 = write data
  186. *
  187. * Returns the word read from nvram @addr.
  188. */
  189. static uint16_t
  190. qla2x00_nvram_request(scsi_qla_host_t *ha, uint32_t nv_cmd)
  191. {
  192. uint8_t cnt;
  193. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  194. uint16_t data = 0;
  195. uint16_t reg_data;
  196. /* Send command to NVRAM. */
  197. nv_cmd <<= 5;
  198. for (cnt = 0; cnt < 11; cnt++) {
  199. if (nv_cmd & BIT_31)
  200. qla2x00_nv_write(ha, NVR_DATA_OUT);
  201. else
  202. qla2x00_nv_write(ha, 0);
  203. nv_cmd <<= 1;
  204. }
  205. /* Read data from NVRAM. */
  206. for (cnt = 0; cnt < 16; cnt++) {
  207. WRT_REG_WORD(&reg->nvram, NVR_SELECT | NVR_CLOCK);
  208. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  209. NVRAM_DELAY();
  210. data <<= 1;
  211. reg_data = RD_REG_WORD(&reg->nvram);
  212. if (reg_data & NVR_DATA_IN)
  213. data |= BIT_0;
  214. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  215. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  216. NVRAM_DELAY();
  217. }
  218. /* Deselect chip. */
  219. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  220. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  221. NVRAM_DELAY();
  222. return (data);
  223. }
  224. /**
  225. * qla2x00_nv_write() - Clean NVRAM operations.
  226. * @ha: HA context
  227. */
  228. static void
  229. qla2x00_nv_deselect(scsi_qla_host_t *ha)
  230. {
  231. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  232. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  233. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  234. NVRAM_DELAY();
  235. }
  236. /**
  237. * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
  238. * @ha: HA context
  239. * @data: Serial interface selector
  240. */
  241. static void
  242. qla2x00_nv_write(scsi_qla_host_t *ha, uint16_t data)
  243. {
  244. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  245. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  246. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  247. NVRAM_DELAY();
  248. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT| NVR_CLOCK |
  249. NVR_WRT_ENABLE);
  250. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  251. NVRAM_DELAY();
  252. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  253. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  254. NVRAM_DELAY();
  255. }
  256. /**
  257. * qla2x00_clear_nvram_protection() -
  258. * @ha: HA context
  259. */
  260. static int
  261. qla2x00_clear_nvram_protection(scsi_qla_host_t *ha)
  262. {
  263. int ret, stat;
  264. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  265. uint32_t word, wait_cnt;
  266. uint16_t wprot, wprot_old;
  267. /* Clear NVRAM write protection. */
  268. ret = QLA_FUNCTION_FAILED;
  269. wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  270. stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
  271. __constant_cpu_to_le16(0x1234), 100000);
  272. wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  273. if (stat != QLA_SUCCESS || wprot != 0x1234) {
  274. /* Write enable. */
  275. qla2x00_nv_write(ha, NVR_DATA_OUT);
  276. qla2x00_nv_write(ha, 0);
  277. qla2x00_nv_write(ha, 0);
  278. for (word = 0; word < 8; word++)
  279. qla2x00_nv_write(ha, NVR_DATA_OUT);
  280. qla2x00_nv_deselect(ha);
  281. /* Enable protection register. */
  282. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  283. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  284. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  285. for (word = 0; word < 8; word++)
  286. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  287. qla2x00_nv_deselect(ha);
  288. /* Clear protection register (ffff is cleared). */
  289. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  290. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  291. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  292. for (word = 0; word < 8; word++)
  293. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  294. qla2x00_nv_deselect(ha);
  295. /* Wait for NVRAM to become ready. */
  296. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  297. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  298. wait_cnt = NVR_WAIT_CNT;
  299. do {
  300. if (!--wait_cnt) {
  301. DEBUG9_10(printk("%s(%ld): NVRAM didn't go "
  302. "ready...\n", __func__,
  303. ha->host_no));
  304. break;
  305. }
  306. NVRAM_DELAY();
  307. word = RD_REG_WORD(&reg->nvram);
  308. } while ((word & NVR_DATA_IN) == 0);
  309. if (wait_cnt)
  310. ret = QLA_SUCCESS;
  311. } else
  312. qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
  313. return ret;
  314. }
  315. static void
  316. qla2x00_set_nvram_protection(scsi_qla_host_t *ha, int stat)
  317. {
  318. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  319. uint32_t word, wait_cnt;
  320. if (stat != QLA_SUCCESS)
  321. return;
  322. /* Set NVRAM write protection. */
  323. /* Write enable. */
  324. qla2x00_nv_write(ha, NVR_DATA_OUT);
  325. qla2x00_nv_write(ha, 0);
  326. qla2x00_nv_write(ha, 0);
  327. for (word = 0; word < 8; word++)
  328. qla2x00_nv_write(ha, NVR_DATA_OUT);
  329. qla2x00_nv_deselect(ha);
  330. /* Enable protection register. */
  331. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  332. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  333. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  334. for (word = 0; word < 8; word++)
  335. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  336. qla2x00_nv_deselect(ha);
  337. /* Enable protection register. */
  338. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  339. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  340. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  341. for (word = 0; word < 8; word++)
  342. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  343. qla2x00_nv_deselect(ha);
  344. /* Wait for NVRAM to become ready. */
  345. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  346. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  347. wait_cnt = NVR_WAIT_CNT;
  348. do {
  349. if (!--wait_cnt) {
  350. DEBUG9_10(printk("%s(%ld): NVRAM didn't go ready...\n",
  351. __func__, ha->host_no));
  352. break;
  353. }
  354. NVRAM_DELAY();
  355. word = RD_REG_WORD(&reg->nvram);
  356. } while ((word & NVR_DATA_IN) == 0);
  357. }
  358. /*****************************************************************************/
  359. /* Flash Manipulation Routines */
  360. /*****************************************************************************/
  361. static inline uint32_t
  362. flash_conf_to_access_addr(uint32_t faddr)
  363. {
  364. return FARX_ACCESS_FLASH_CONF | faddr;
  365. }
  366. static inline uint32_t
  367. flash_data_to_access_addr(uint32_t faddr)
  368. {
  369. return FARX_ACCESS_FLASH_DATA | faddr;
  370. }
  371. static inline uint32_t
  372. nvram_conf_to_access_addr(uint32_t naddr)
  373. {
  374. return FARX_ACCESS_NVRAM_CONF | naddr;
  375. }
  376. static inline uint32_t
  377. nvram_data_to_access_addr(uint32_t naddr)
  378. {
  379. return FARX_ACCESS_NVRAM_DATA | naddr;
  380. }
  381. static uint32_t
  382. qla24xx_read_flash_dword(scsi_qla_host_t *ha, uint32_t addr)
  383. {
  384. int rval;
  385. uint32_t cnt, data;
  386. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  387. WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  388. /* Wait for READ cycle to complete. */
  389. rval = QLA_SUCCESS;
  390. for (cnt = 3000;
  391. (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
  392. rval == QLA_SUCCESS; cnt--) {
  393. if (cnt)
  394. udelay(10);
  395. else
  396. rval = QLA_FUNCTION_TIMEOUT;
  397. cond_resched();
  398. }
  399. /* TODO: What happens if we time out? */
  400. data = 0xDEADDEAD;
  401. if (rval == QLA_SUCCESS)
  402. data = RD_REG_DWORD(&reg->flash_data);
  403. return data;
  404. }
  405. uint32_t *
  406. qla24xx_read_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  407. uint32_t dwords)
  408. {
  409. uint32_t i;
  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_to_access_addr(faddr)));
  414. return dwptr;
  415. }
  416. static int
  417. qla24xx_write_flash_dword(scsi_qla_host_t *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(scsi_qla_host_t *ha, uint8_t *man_id,
  439. uint8_t *flash_id)
  440. {
  441. uint32_t ids;
  442. ids = qla24xx_read_flash_dword(ha, flash_data_to_access_addr(0xd03ab));
  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,
  454. flash_data_to_access_addr(0xd009f));
  455. *man_id = LSB(ids);
  456. *flash_id = MSB(ids);
  457. }
  458. }
  459. static int
  460. qla24xx_write_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  461. uint32_t dwords)
  462. {
  463. int ret;
  464. uint32_t liter;
  465. uint32_t sec_mask, rest_addr, conf_addr, sec_end_mask;
  466. uint32_t fdata, findex ;
  467. uint8_t man_id, flash_id;
  468. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  469. ret = QLA_SUCCESS;
  470. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  471. DEBUG9(printk("%s(%ld): Flash man_id=%d flash_id=%d\n", __func__,
  472. ha->host_no, man_id, flash_id));
  473. sec_end_mask = 0;
  474. conf_addr = flash_conf_to_access_addr(0x03d8);
  475. switch (man_id) {
  476. case 0xbf: /* STT flash. */
  477. rest_addr = 0x1fff;
  478. sec_mask = 0x3e000;
  479. if (flash_id == 0x80)
  480. conf_addr = flash_conf_to_access_addr(0x0352);
  481. break;
  482. case 0x13: /* ST M25P80. */
  483. rest_addr = 0x3fff;
  484. sec_mask = 0x3c000;
  485. break;
  486. case 0x1f: // Atmel 26DF081A
  487. rest_addr = 0x0fff;
  488. sec_mask = 0xff000;
  489. sec_end_mask = 0x003ff;
  490. conf_addr = flash_conf_to_access_addr(0x0320);
  491. break;
  492. default:
  493. /* Default to 64 kb sector size. */
  494. rest_addr = 0x3fff;
  495. sec_mask = 0x3c000;
  496. break;
  497. }
  498. /* Enable flash write. */
  499. WRT_REG_DWORD(&reg->ctrl_status,
  500. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  501. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  502. /* Disable flash write-protection. */
  503. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  504. /* Some flash parts need an additional zero-write to clear bits.*/
  505. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  506. do { /* Loop once to provide quick error exit. */
  507. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  508. if (man_id == 0x1f) {
  509. findex = faddr << 2;
  510. fdata = findex & sec_mask;
  511. } else {
  512. findex = faddr;
  513. fdata = (findex & sec_mask) << 2;
  514. }
  515. /* Are we at the beginning of a sector? */
  516. if ((findex & rest_addr) == 0) {
  517. /*
  518. * Do sector unprotect at 4K boundry for Atmel
  519. * part.
  520. */
  521. if (man_id == 0x1f)
  522. qla24xx_write_flash_dword(ha,
  523. flash_conf_to_access_addr(0x0339),
  524. (fdata & 0xff00) | ((fdata << 16) &
  525. 0xff0000) | ((fdata >> 16) & 0xff));
  526. ret = qla24xx_write_flash_dword(ha, conf_addr,
  527. (fdata & 0xff00) |((fdata << 16) &
  528. 0xff0000) | ((fdata >> 16) & 0xff));
  529. if (ret != QLA_SUCCESS) {
  530. DEBUG9(printk("%s(%ld) Unable to flash "
  531. "sector: address=%x.\n", __func__,
  532. ha->host_no, faddr));
  533. break;
  534. }
  535. }
  536. ret = qla24xx_write_flash_dword(ha,
  537. flash_data_to_access_addr(faddr),
  538. cpu_to_le32(*dwptr));
  539. if (ret != QLA_SUCCESS) {
  540. DEBUG9(printk("%s(%ld) Unable to program flash "
  541. "address=%x data=%x.\n", __func__,
  542. ha->host_no, faddr, *dwptr));
  543. break;
  544. }
  545. /* Do sector protect at 4K boundry for Atmel part. */
  546. if (man_id == 0x1f &&
  547. ((faddr & sec_end_mask) == 0x3ff))
  548. qla24xx_write_flash_dword(ha,
  549. flash_conf_to_access_addr(0x0336),
  550. (fdata & 0xff00) | ((fdata << 16) &
  551. 0xff0000) | ((fdata >> 16) & 0xff));
  552. }
  553. } while (0);
  554. /* Enable flash write-protection. */
  555. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0x9c);
  556. /* Disable flash write. */
  557. WRT_REG_DWORD(&reg->ctrl_status,
  558. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  559. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  560. return ret;
  561. }
  562. uint8_t *
  563. qla2x00_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  564. uint32_t bytes)
  565. {
  566. uint32_t i;
  567. uint16_t *wptr;
  568. /* Word reads to NVRAM via registers. */
  569. wptr = (uint16_t *)buf;
  570. qla2x00_lock_nvram_access(ha);
  571. for (i = 0; i < bytes >> 1; i++, naddr++)
  572. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  573. naddr));
  574. qla2x00_unlock_nvram_access(ha);
  575. return buf;
  576. }
  577. uint8_t *
  578. qla24xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  579. uint32_t bytes)
  580. {
  581. uint32_t i;
  582. uint32_t *dwptr;
  583. /* Dword reads to flash. */
  584. dwptr = (uint32_t *)buf;
  585. for (i = 0; i < bytes >> 2; i++, naddr++)
  586. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  587. nvram_data_to_access_addr(naddr)));
  588. return buf;
  589. }
  590. int
  591. qla2x00_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  592. uint32_t bytes)
  593. {
  594. int ret, stat;
  595. uint32_t i;
  596. uint16_t *wptr;
  597. ret = QLA_SUCCESS;
  598. qla2x00_lock_nvram_access(ha);
  599. /* Disable NVRAM write-protection. */
  600. stat = qla2x00_clear_nvram_protection(ha);
  601. wptr = (uint16_t *)buf;
  602. for (i = 0; i < bytes >> 1; i++, naddr++) {
  603. qla2x00_write_nvram_word(ha, naddr,
  604. cpu_to_le16(*wptr));
  605. wptr++;
  606. }
  607. /* Enable NVRAM write-protection. */
  608. qla2x00_set_nvram_protection(ha, stat);
  609. qla2x00_unlock_nvram_access(ha);
  610. return ret;
  611. }
  612. int
  613. qla24xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  614. uint32_t bytes)
  615. {
  616. int ret;
  617. uint32_t i;
  618. uint32_t *dwptr;
  619. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  620. ret = QLA_SUCCESS;
  621. /* Enable flash write. */
  622. WRT_REG_DWORD(&reg->ctrl_status,
  623. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  624. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  625. /* Disable NVRAM write-protection. */
  626. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  627. 0);
  628. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  629. 0);
  630. /* Dword writes to flash. */
  631. dwptr = (uint32_t *)buf;
  632. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  633. ret = qla24xx_write_flash_dword(ha,
  634. nvram_data_to_access_addr(naddr),
  635. cpu_to_le32(*dwptr));
  636. if (ret != QLA_SUCCESS) {
  637. DEBUG9(printk("%s(%ld) Unable to program "
  638. "nvram address=%x data=%x.\n", __func__,
  639. ha->host_no, naddr, *dwptr));
  640. break;
  641. }
  642. }
  643. /* Enable NVRAM write-protection. */
  644. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  645. 0x8c);
  646. /* Disable flash write. */
  647. WRT_REG_DWORD(&reg->ctrl_status,
  648. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  649. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  650. return ret;
  651. }
  652. uint8_t *
  653. qla25xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  654. uint32_t bytes)
  655. {
  656. uint32_t i;
  657. uint32_t *dwptr;
  658. /* Dword reads to flash. */
  659. dwptr = (uint32_t *)buf;
  660. for (i = 0; i < bytes >> 2; i++, naddr++)
  661. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  662. flash_data_to_access_addr(FA_VPD_NVRAM_ADDR | naddr)));
  663. return buf;
  664. }
  665. int
  666. qla25xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  667. uint32_t bytes)
  668. {
  669. return qla24xx_write_flash_data(ha, (uint32_t *)buf,
  670. FA_VPD_NVRAM_ADDR | naddr, bytes >> 2);
  671. }
  672. static inline void
  673. qla2x00_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  674. {
  675. if (IS_QLA2322(ha)) {
  676. /* Flip all colors. */
  677. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  678. /* Turn off. */
  679. ha->beacon_color_state = 0;
  680. *pflags = GPIO_LED_ALL_OFF;
  681. } else {
  682. /* Turn on. */
  683. ha->beacon_color_state = QLA_LED_ALL_ON;
  684. *pflags = GPIO_LED_RGA_ON;
  685. }
  686. } else {
  687. /* Flip green led only. */
  688. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  689. /* Turn off. */
  690. ha->beacon_color_state = 0;
  691. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  692. } else {
  693. /* Turn on. */
  694. ha->beacon_color_state = QLA_LED_GRN_ON;
  695. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  696. }
  697. }
  698. }
  699. void
  700. qla2x00_beacon_blink(struct scsi_qla_host *ha)
  701. {
  702. uint16_t gpio_enable;
  703. uint16_t gpio_data;
  704. uint16_t led_color = 0;
  705. unsigned long flags;
  706. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  707. if (ha->pio_address)
  708. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  709. spin_lock_irqsave(&ha->hardware_lock, flags);
  710. /* Save the Original GPIOE. */
  711. if (ha->pio_address) {
  712. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  713. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  714. } else {
  715. gpio_enable = RD_REG_WORD(&reg->gpioe);
  716. gpio_data = RD_REG_WORD(&reg->gpiod);
  717. }
  718. /* Set the modified gpio_enable values */
  719. gpio_enable |= GPIO_LED_MASK;
  720. if (ha->pio_address) {
  721. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  722. } else {
  723. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  724. RD_REG_WORD(&reg->gpioe);
  725. }
  726. qla2x00_flip_colors(ha, &led_color);
  727. /* Clear out any previously set LED color. */
  728. gpio_data &= ~GPIO_LED_MASK;
  729. /* Set the new input LED color to GPIOD. */
  730. gpio_data |= led_color;
  731. /* Set the modified gpio_data values */
  732. if (ha->pio_address) {
  733. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  734. } else {
  735. WRT_REG_WORD(&reg->gpiod, gpio_data);
  736. RD_REG_WORD(&reg->gpiod);
  737. }
  738. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  739. }
  740. int
  741. qla2x00_beacon_on(struct scsi_qla_host *ha)
  742. {
  743. uint16_t gpio_enable;
  744. uint16_t gpio_data;
  745. unsigned long flags;
  746. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  747. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  748. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  749. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  750. qla_printk(KERN_WARNING, ha,
  751. "Unable to update fw options (beacon on).\n");
  752. return QLA_FUNCTION_FAILED;
  753. }
  754. if (ha->pio_address)
  755. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  756. /* Turn off LEDs. */
  757. spin_lock_irqsave(&ha->hardware_lock, flags);
  758. if (ha->pio_address) {
  759. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  760. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  761. } else {
  762. gpio_enable = RD_REG_WORD(&reg->gpioe);
  763. gpio_data = RD_REG_WORD(&reg->gpiod);
  764. }
  765. gpio_enable |= GPIO_LED_MASK;
  766. /* Set the modified gpio_enable values. */
  767. if (ha->pio_address) {
  768. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  769. } else {
  770. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  771. RD_REG_WORD(&reg->gpioe);
  772. }
  773. /* Clear out previously set LED colour. */
  774. gpio_data &= ~GPIO_LED_MASK;
  775. if (ha->pio_address) {
  776. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  777. } else {
  778. WRT_REG_WORD(&reg->gpiod, gpio_data);
  779. RD_REG_WORD(&reg->gpiod);
  780. }
  781. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  782. /*
  783. * Let the per HBA timer kick off the blinking process based on
  784. * the following flags. No need to do anything else now.
  785. */
  786. ha->beacon_blink_led = 1;
  787. ha->beacon_color_state = 0;
  788. return QLA_SUCCESS;
  789. }
  790. int
  791. qla2x00_beacon_off(struct scsi_qla_host *ha)
  792. {
  793. int rval = QLA_SUCCESS;
  794. ha->beacon_blink_led = 0;
  795. /* Set the on flag so when it gets flipped it will be off. */
  796. if (IS_QLA2322(ha))
  797. ha->beacon_color_state = QLA_LED_ALL_ON;
  798. else
  799. ha->beacon_color_state = QLA_LED_GRN_ON;
  800. ha->isp_ops->beacon_blink(ha); /* This turns green LED off */
  801. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  802. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  803. rval = qla2x00_set_fw_options(ha, ha->fw_options);
  804. if (rval != QLA_SUCCESS)
  805. qla_printk(KERN_WARNING, ha,
  806. "Unable to update fw options (beacon off).\n");
  807. return rval;
  808. }
  809. static inline void
  810. qla24xx_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  811. {
  812. /* Flip all colors. */
  813. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  814. /* Turn off. */
  815. ha->beacon_color_state = 0;
  816. *pflags = 0;
  817. } else {
  818. /* Turn on. */
  819. ha->beacon_color_state = QLA_LED_ALL_ON;
  820. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  821. }
  822. }
  823. void
  824. qla24xx_beacon_blink(struct scsi_qla_host *ha)
  825. {
  826. uint16_t led_color = 0;
  827. uint32_t gpio_data;
  828. unsigned long flags;
  829. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  830. /* Save the Original GPIOD. */
  831. spin_lock_irqsave(&ha->hardware_lock, flags);
  832. gpio_data = RD_REG_DWORD(&reg->gpiod);
  833. /* Enable the gpio_data reg for update. */
  834. gpio_data |= GPDX_LED_UPDATE_MASK;
  835. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  836. gpio_data = RD_REG_DWORD(&reg->gpiod);
  837. /* Set the color bits. */
  838. qla24xx_flip_colors(ha, &led_color);
  839. /* Clear out any previously set LED color. */
  840. gpio_data &= ~GPDX_LED_COLOR_MASK;
  841. /* Set the new input LED color to GPIOD. */
  842. gpio_data |= led_color;
  843. /* Set the modified gpio_data values. */
  844. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  845. gpio_data = RD_REG_DWORD(&reg->gpiod);
  846. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  847. }
  848. int
  849. qla24xx_beacon_on(struct scsi_qla_host *ha)
  850. {
  851. uint32_t gpio_data;
  852. unsigned long flags;
  853. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  854. if (ha->beacon_blink_led == 0) {
  855. /* Enable firmware for update */
  856. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  857. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS)
  858. return QLA_FUNCTION_FAILED;
  859. if (qla2x00_get_fw_options(ha, ha->fw_options) !=
  860. QLA_SUCCESS) {
  861. qla_printk(KERN_WARNING, ha,
  862. "Unable to update fw options (beacon on).\n");
  863. return QLA_FUNCTION_FAILED;
  864. }
  865. spin_lock_irqsave(&ha->hardware_lock, flags);
  866. gpio_data = RD_REG_DWORD(&reg->gpiod);
  867. /* Enable the gpio_data reg for update. */
  868. gpio_data |= GPDX_LED_UPDATE_MASK;
  869. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  870. RD_REG_DWORD(&reg->gpiod);
  871. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  872. }
  873. /* So all colors blink together. */
  874. ha->beacon_color_state = 0;
  875. /* Let the per HBA timer kick off the blinking process. */
  876. ha->beacon_blink_led = 1;
  877. return QLA_SUCCESS;
  878. }
  879. int
  880. qla24xx_beacon_off(struct scsi_qla_host *ha)
  881. {
  882. uint32_t gpio_data;
  883. unsigned long flags;
  884. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  885. ha->beacon_blink_led = 0;
  886. ha->beacon_color_state = QLA_LED_ALL_ON;
  887. ha->isp_ops->beacon_blink(ha); /* Will flip to all off. */
  888. /* Give control back to firmware. */
  889. spin_lock_irqsave(&ha->hardware_lock, flags);
  890. gpio_data = RD_REG_DWORD(&reg->gpiod);
  891. /* Disable the gpio_data reg for update. */
  892. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  893. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  894. RD_REG_DWORD(&reg->gpiod);
  895. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  896. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  897. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  898. qla_printk(KERN_WARNING, ha,
  899. "Unable to update fw options (beacon off).\n");
  900. return QLA_FUNCTION_FAILED;
  901. }
  902. if (qla2x00_get_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  903. qla_printk(KERN_WARNING, ha,
  904. "Unable to get fw options (beacon off).\n");
  905. return QLA_FUNCTION_FAILED;
  906. }
  907. return QLA_SUCCESS;
  908. }
  909. /*
  910. * Flash support routines
  911. */
  912. /**
  913. * qla2x00_flash_enable() - Setup flash for reading and writing.
  914. * @ha: HA context
  915. */
  916. static void
  917. qla2x00_flash_enable(scsi_qla_host_t *ha)
  918. {
  919. uint16_t data;
  920. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  921. data = RD_REG_WORD(&reg->ctrl_status);
  922. data |= CSR_FLASH_ENABLE;
  923. WRT_REG_WORD(&reg->ctrl_status, data);
  924. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  925. }
  926. /**
  927. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  928. * @ha: HA context
  929. */
  930. static void
  931. qla2x00_flash_disable(scsi_qla_host_t *ha)
  932. {
  933. uint16_t data;
  934. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  935. data = RD_REG_WORD(&reg->ctrl_status);
  936. data &= ~(CSR_FLASH_ENABLE);
  937. WRT_REG_WORD(&reg->ctrl_status, data);
  938. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  939. }
  940. /**
  941. * qla2x00_read_flash_byte() - Reads a byte from flash
  942. * @ha: HA context
  943. * @addr: Address in flash to read
  944. *
  945. * A word is read from the chip, but, only the lower byte is valid.
  946. *
  947. * Returns the byte read from flash @addr.
  948. */
  949. static uint8_t
  950. qla2x00_read_flash_byte(scsi_qla_host_t *ha, uint32_t addr)
  951. {
  952. uint16_t data;
  953. uint16_t bank_select;
  954. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  955. bank_select = RD_REG_WORD(&reg->ctrl_status);
  956. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  957. /* Specify 64K address range: */
  958. /* clear out Module Select and Flash Address bits [19:16]. */
  959. bank_select &= ~0xf8;
  960. bank_select |= addr >> 12 & 0xf0;
  961. bank_select |= CSR_FLASH_64K_BANK;
  962. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  963. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  964. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  965. data = RD_REG_WORD(&reg->flash_data);
  966. return (uint8_t)data;
  967. }
  968. /* Setup bit 16 of flash address. */
  969. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  970. bank_select |= CSR_FLASH_64K_BANK;
  971. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  972. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  973. } else if (((addr & BIT_16) == 0) &&
  974. (bank_select & CSR_FLASH_64K_BANK)) {
  975. bank_select &= ~(CSR_FLASH_64K_BANK);
  976. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  977. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  978. }
  979. /* Always perform IO mapped accesses to the FLASH registers. */
  980. if (ha->pio_address) {
  981. uint16_t data2;
  982. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  983. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  984. do {
  985. data = RD_REG_WORD_PIO(&reg->flash_data);
  986. barrier();
  987. cpu_relax();
  988. data2 = RD_REG_WORD_PIO(&reg->flash_data);
  989. } while (data != data2);
  990. } else {
  991. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  992. data = qla2x00_debounce_register(&reg->flash_data);
  993. }
  994. return (uint8_t)data;
  995. }
  996. /**
  997. * qla2x00_write_flash_byte() - Write a byte to flash
  998. * @ha: HA context
  999. * @addr: Address in flash to write
  1000. * @data: Data to write
  1001. */
  1002. static void
  1003. qla2x00_write_flash_byte(scsi_qla_host_t *ha, uint32_t addr, uint8_t data)
  1004. {
  1005. uint16_t bank_select;
  1006. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1007. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1008. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1009. /* Specify 64K address range: */
  1010. /* clear out Module Select and Flash Address bits [19:16]. */
  1011. bank_select &= ~0xf8;
  1012. bank_select |= addr >> 12 & 0xf0;
  1013. bank_select |= CSR_FLASH_64K_BANK;
  1014. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1015. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1016. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1017. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1018. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1019. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1020. return;
  1021. }
  1022. /* Setup bit 16 of flash address. */
  1023. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1024. bank_select |= CSR_FLASH_64K_BANK;
  1025. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1026. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1027. } else if (((addr & BIT_16) == 0) &&
  1028. (bank_select & CSR_FLASH_64K_BANK)) {
  1029. bank_select &= ~(CSR_FLASH_64K_BANK);
  1030. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1031. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1032. }
  1033. /* Always perform IO mapped accesses to the FLASH registers. */
  1034. if (ha->pio_address) {
  1035. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  1036. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  1037. WRT_REG_WORD_PIO(&reg->flash_data, (uint16_t)data);
  1038. } else {
  1039. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1040. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1041. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1042. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1043. }
  1044. }
  1045. /**
  1046. * qla2x00_poll_flash() - Polls flash for completion.
  1047. * @ha: HA context
  1048. * @addr: Address in flash to poll
  1049. * @poll_data: Data to be polled
  1050. * @man_id: Flash manufacturer ID
  1051. * @flash_id: Flash ID
  1052. *
  1053. * This function polls the device until bit 7 of what is read matches data
  1054. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1055. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1056. * reading bit 5 as a 1.
  1057. *
  1058. * Returns 0 on success, else non-zero.
  1059. */
  1060. static int
  1061. qla2x00_poll_flash(scsi_qla_host_t *ha, uint32_t addr, uint8_t poll_data,
  1062. uint8_t man_id, uint8_t flash_id)
  1063. {
  1064. int status;
  1065. uint8_t flash_data;
  1066. uint32_t cnt;
  1067. status = 1;
  1068. /* Wait for 30 seconds for command to finish. */
  1069. poll_data &= BIT_7;
  1070. for (cnt = 3000000; cnt; cnt--) {
  1071. flash_data = qla2x00_read_flash_byte(ha, addr);
  1072. if ((flash_data & BIT_7) == poll_data) {
  1073. status = 0;
  1074. break;
  1075. }
  1076. if (man_id != 0x40 && man_id != 0xda) {
  1077. if ((flash_data & BIT_5) && cnt > 2)
  1078. cnt = 2;
  1079. }
  1080. udelay(10);
  1081. barrier();
  1082. cond_resched();
  1083. }
  1084. return status;
  1085. }
  1086. /**
  1087. * qla2x00_program_flash_address() - Programs a flash address
  1088. * @ha: HA context
  1089. * @addr: Address in flash to program
  1090. * @data: Data to be written in flash
  1091. * @man_id: Flash manufacturer ID
  1092. * @flash_id: Flash ID
  1093. *
  1094. * Returns 0 on success, else non-zero.
  1095. */
  1096. static int
  1097. qla2x00_program_flash_address(scsi_qla_host_t *ha, uint32_t addr, uint8_t data,
  1098. uint8_t man_id, uint8_t flash_id)
  1099. {
  1100. /* Write Program Command Sequence. */
  1101. if (IS_OEM_001(ha)) {
  1102. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1103. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1104. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1105. qla2x00_write_flash_byte(ha, addr, data);
  1106. } else {
  1107. if (man_id == 0xda && flash_id == 0xc1) {
  1108. qla2x00_write_flash_byte(ha, addr, data);
  1109. if (addr & 0x7e)
  1110. return 0;
  1111. } else {
  1112. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1113. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1114. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1115. qla2x00_write_flash_byte(ha, addr, data);
  1116. }
  1117. }
  1118. udelay(150);
  1119. /* Wait for write to complete. */
  1120. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1121. }
  1122. /**
  1123. * qla2x00_erase_flash() - Erase the flash.
  1124. * @ha: HA context
  1125. * @man_id: Flash manufacturer ID
  1126. * @flash_id: Flash ID
  1127. *
  1128. * Returns 0 on success, else non-zero.
  1129. */
  1130. static int
  1131. qla2x00_erase_flash(scsi_qla_host_t *ha, uint8_t man_id, uint8_t flash_id)
  1132. {
  1133. /* Individual Sector Erase Command Sequence */
  1134. if (IS_OEM_001(ha)) {
  1135. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1136. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1137. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1138. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1139. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1140. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1141. } else {
  1142. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1143. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1144. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1145. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1146. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1147. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1148. }
  1149. udelay(150);
  1150. /* Wait for erase to complete. */
  1151. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1152. }
  1153. /**
  1154. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1155. * @ha: HA context
  1156. * @addr: Flash sector to erase
  1157. * @sec_mask: Sector address mask
  1158. * @man_id: Flash manufacturer ID
  1159. * @flash_id: Flash ID
  1160. *
  1161. * Returns 0 on success, else non-zero.
  1162. */
  1163. static int
  1164. qla2x00_erase_flash_sector(scsi_qla_host_t *ha, uint32_t addr,
  1165. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1166. {
  1167. /* Individual Sector Erase Command Sequence */
  1168. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1169. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1170. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1171. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1172. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1173. if (man_id == 0x1f && flash_id == 0x13)
  1174. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1175. else
  1176. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1177. udelay(150);
  1178. /* Wait for erase to complete. */
  1179. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1180. }
  1181. /**
  1182. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1183. * @man_id: Flash manufacturer ID
  1184. * @flash_id: Flash ID
  1185. */
  1186. static void
  1187. qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  1188. uint8_t *flash_id)
  1189. {
  1190. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1191. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1192. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1193. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1194. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1195. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1196. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1197. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1198. }
  1199. static void
  1200. qla2x00_read_flash_data(scsi_qla_host_t *ha, uint8_t *tmp_buf, uint32_t saddr,
  1201. uint32_t length)
  1202. {
  1203. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1204. uint32_t midpoint, ilength;
  1205. uint8_t data;
  1206. midpoint = length / 2;
  1207. WRT_REG_WORD(&reg->nvram, 0);
  1208. RD_REG_WORD(&reg->nvram);
  1209. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1210. if (ilength == midpoint) {
  1211. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1212. RD_REG_WORD(&reg->nvram);
  1213. }
  1214. data = qla2x00_read_flash_byte(ha, saddr);
  1215. if (saddr % 100)
  1216. udelay(10);
  1217. *tmp_buf = data;
  1218. cond_resched();
  1219. }
  1220. }
  1221. static inline void
  1222. qla2x00_suspend_hba(struct scsi_qla_host *ha)
  1223. {
  1224. int cnt;
  1225. unsigned long flags;
  1226. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1227. /* Suspend HBA. */
  1228. scsi_block_requests(ha->host);
  1229. ha->isp_ops->disable_intrs(ha);
  1230. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1231. /* Pause RISC. */
  1232. spin_lock_irqsave(&ha->hardware_lock, flags);
  1233. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1234. RD_REG_WORD(&reg->hccr);
  1235. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1236. for (cnt = 0; cnt < 30000; cnt++) {
  1237. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1238. break;
  1239. udelay(100);
  1240. }
  1241. } else {
  1242. udelay(10);
  1243. }
  1244. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1245. }
  1246. static inline void
  1247. qla2x00_resume_hba(struct scsi_qla_host *ha)
  1248. {
  1249. /* Resume HBA. */
  1250. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1251. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1252. qla2xxx_wake_dpc(ha);
  1253. qla2x00_wait_for_hba_online(ha);
  1254. scsi_unblock_requests(ha->host);
  1255. }
  1256. uint8_t *
  1257. qla2x00_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1258. uint32_t offset, uint32_t length)
  1259. {
  1260. uint32_t addr, midpoint;
  1261. uint8_t *data;
  1262. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1263. /* Suspend HBA. */
  1264. qla2x00_suspend_hba(ha);
  1265. /* Go with read. */
  1266. midpoint = ha->optrom_size / 2;
  1267. qla2x00_flash_enable(ha);
  1268. WRT_REG_WORD(&reg->nvram, 0);
  1269. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1270. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1271. if (addr == midpoint) {
  1272. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1273. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1274. }
  1275. *data = qla2x00_read_flash_byte(ha, addr);
  1276. }
  1277. qla2x00_flash_disable(ha);
  1278. /* Resume HBA. */
  1279. qla2x00_resume_hba(ha);
  1280. return buf;
  1281. }
  1282. int
  1283. qla2x00_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1284. uint32_t offset, uint32_t length)
  1285. {
  1286. int rval;
  1287. uint8_t man_id, flash_id, sec_number, data;
  1288. uint16_t wd;
  1289. uint32_t addr, liter, sec_mask, rest_addr;
  1290. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1291. /* Suspend HBA. */
  1292. qla2x00_suspend_hba(ha);
  1293. rval = QLA_SUCCESS;
  1294. sec_number = 0;
  1295. /* Reset ISP chip. */
  1296. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1297. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1298. /* Go with write. */
  1299. qla2x00_flash_enable(ha);
  1300. do { /* Loop once to provide quick error exit */
  1301. /* Structure of flash memory based on manufacturer */
  1302. if (IS_OEM_001(ha)) {
  1303. /* OEM variant with special flash part. */
  1304. man_id = flash_id = 0;
  1305. rest_addr = 0xffff;
  1306. sec_mask = 0x10000;
  1307. goto update_flash;
  1308. }
  1309. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1310. switch (man_id) {
  1311. case 0x20: /* ST flash. */
  1312. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1313. /*
  1314. * ST m29w008at part - 64kb sector size with
  1315. * 32kb,8kb,8kb,16kb sectors at memory address
  1316. * 0xf0000.
  1317. */
  1318. rest_addr = 0xffff;
  1319. sec_mask = 0x10000;
  1320. break;
  1321. }
  1322. /*
  1323. * ST m29w010b part - 16kb sector size
  1324. * Default to 16kb sectors
  1325. */
  1326. rest_addr = 0x3fff;
  1327. sec_mask = 0x1c000;
  1328. break;
  1329. case 0x40: /* Mostel flash. */
  1330. /* Mostel v29c51001 part - 512 byte sector size. */
  1331. rest_addr = 0x1ff;
  1332. sec_mask = 0x1fe00;
  1333. break;
  1334. case 0xbf: /* SST flash. */
  1335. /* SST39sf10 part - 4kb sector size. */
  1336. rest_addr = 0xfff;
  1337. sec_mask = 0x1f000;
  1338. break;
  1339. case 0xda: /* Winbond flash. */
  1340. /* Winbond W29EE011 part - 256 byte sector size. */
  1341. rest_addr = 0x7f;
  1342. sec_mask = 0x1ff80;
  1343. break;
  1344. case 0xc2: /* Macronix flash. */
  1345. /* 64k sector size. */
  1346. if (flash_id == 0x38 || flash_id == 0x4f) {
  1347. rest_addr = 0xffff;
  1348. sec_mask = 0x10000;
  1349. break;
  1350. }
  1351. /* Fall through... */
  1352. case 0x1f: /* Atmel flash. */
  1353. /* 512k sector size. */
  1354. if (flash_id == 0x13) {
  1355. rest_addr = 0x7fffffff;
  1356. sec_mask = 0x80000000;
  1357. break;
  1358. }
  1359. /* Fall through... */
  1360. case 0x01: /* AMD flash. */
  1361. if (flash_id == 0x38 || flash_id == 0x40 ||
  1362. flash_id == 0x4f) {
  1363. /* Am29LV081 part - 64kb sector size. */
  1364. /* Am29LV002BT part - 64kb sector size. */
  1365. rest_addr = 0xffff;
  1366. sec_mask = 0x10000;
  1367. break;
  1368. } else if (flash_id == 0x3e) {
  1369. /*
  1370. * Am29LV008b part - 64kb sector size with
  1371. * 32kb,8kb,8kb,16kb sector at memory address
  1372. * h0xf0000.
  1373. */
  1374. rest_addr = 0xffff;
  1375. sec_mask = 0x10000;
  1376. break;
  1377. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1378. /*
  1379. * Am29LV010 part or AM29f010 - 16kb sector
  1380. * size.
  1381. */
  1382. rest_addr = 0x3fff;
  1383. sec_mask = 0x1c000;
  1384. break;
  1385. } else if (flash_id == 0x6d) {
  1386. /* Am29LV001 part - 8kb sector size. */
  1387. rest_addr = 0x1fff;
  1388. sec_mask = 0x1e000;
  1389. break;
  1390. }
  1391. default:
  1392. /* Default to 16 kb sector size. */
  1393. rest_addr = 0x3fff;
  1394. sec_mask = 0x1c000;
  1395. break;
  1396. }
  1397. update_flash:
  1398. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1399. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1400. rval = QLA_FUNCTION_FAILED;
  1401. break;
  1402. }
  1403. }
  1404. for (addr = offset, liter = 0; liter < length; liter++,
  1405. addr++) {
  1406. data = buf[liter];
  1407. /* Are we at the beginning of a sector? */
  1408. if ((addr & rest_addr) == 0) {
  1409. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1410. if (addr >= 0x10000UL) {
  1411. if (((addr >> 12) & 0xf0) &&
  1412. ((man_id == 0x01 &&
  1413. flash_id == 0x3e) ||
  1414. (man_id == 0x20 &&
  1415. flash_id == 0xd2))) {
  1416. sec_number++;
  1417. if (sec_number == 1) {
  1418. rest_addr =
  1419. 0x7fff;
  1420. sec_mask =
  1421. 0x18000;
  1422. } else if (
  1423. sec_number == 2 ||
  1424. sec_number == 3) {
  1425. rest_addr =
  1426. 0x1fff;
  1427. sec_mask =
  1428. 0x1e000;
  1429. } else if (
  1430. sec_number == 4) {
  1431. rest_addr =
  1432. 0x3fff;
  1433. sec_mask =
  1434. 0x1c000;
  1435. }
  1436. }
  1437. }
  1438. } else if (addr == ha->optrom_size / 2) {
  1439. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1440. RD_REG_WORD(&reg->nvram);
  1441. }
  1442. if (flash_id == 0xda && man_id == 0xc1) {
  1443. qla2x00_write_flash_byte(ha, 0x5555,
  1444. 0xaa);
  1445. qla2x00_write_flash_byte(ha, 0x2aaa,
  1446. 0x55);
  1447. qla2x00_write_flash_byte(ha, 0x5555,
  1448. 0xa0);
  1449. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1450. /* Then erase it */
  1451. if (qla2x00_erase_flash_sector(ha,
  1452. addr, sec_mask, man_id,
  1453. flash_id)) {
  1454. rval = QLA_FUNCTION_FAILED;
  1455. break;
  1456. }
  1457. if (man_id == 0x01 && flash_id == 0x6d)
  1458. sec_number++;
  1459. }
  1460. }
  1461. if (man_id == 0x01 && flash_id == 0x6d) {
  1462. if (sec_number == 1 &&
  1463. addr == (rest_addr - 1)) {
  1464. rest_addr = 0x0fff;
  1465. sec_mask = 0x1f000;
  1466. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1467. rest_addr = 0x3fff;
  1468. sec_mask = 0x1c000;
  1469. }
  1470. }
  1471. if (qla2x00_program_flash_address(ha, addr, data,
  1472. man_id, flash_id)) {
  1473. rval = QLA_FUNCTION_FAILED;
  1474. break;
  1475. }
  1476. cond_resched();
  1477. }
  1478. } while (0);
  1479. qla2x00_flash_disable(ha);
  1480. /* Resume HBA. */
  1481. qla2x00_resume_hba(ha);
  1482. return rval;
  1483. }
  1484. uint8_t *
  1485. qla24xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1486. uint32_t offset, uint32_t length)
  1487. {
  1488. /* Suspend HBA. */
  1489. scsi_block_requests(ha->host);
  1490. ha->isp_ops->disable_intrs(ha);
  1491. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1492. /* Go with read. */
  1493. qla24xx_read_flash_data(ha, (uint32_t *)buf, offset >> 2, length >> 2);
  1494. /* Resume HBA. */
  1495. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1496. ha->isp_ops->enable_intrs(ha);
  1497. scsi_unblock_requests(ha->host);
  1498. return buf;
  1499. }
  1500. int
  1501. qla24xx_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1502. uint32_t offset, uint32_t length)
  1503. {
  1504. int rval;
  1505. /* Suspend HBA. */
  1506. scsi_block_requests(ha->host);
  1507. ha->isp_ops->disable_intrs(ha);
  1508. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1509. /* Go with write. */
  1510. rval = qla24xx_write_flash_data(ha, (uint32_t *)buf, offset >> 2,
  1511. length >> 2);
  1512. /* Resume HBA -- RISC reset needed. */
  1513. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1514. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1515. qla2xxx_wake_dpc(ha);
  1516. qla2x00_wait_for_hba_online(ha);
  1517. scsi_unblock_requests(ha->host);
  1518. return rval;
  1519. }
  1520. /**
  1521. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  1522. * @ha: HA context
  1523. * @pcids: Pointer to the FCODE PCI data structure
  1524. *
  1525. * The process of retrieving the FCODE version information is at best
  1526. * described as interesting.
  1527. *
  1528. * Within the first 100h bytes of the image an ASCII string is present
  1529. * which contains several pieces of information including the FCODE
  1530. * version. Unfortunately it seems the only reliable way to retrieve
  1531. * the version is by scanning for another sentinel within the string,
  1532. * the FCODE build date:
  1533. *
  1534. * ... 2.00.02 10/17/02 ...
  1535. *
  1536. * Returns QLA_SUCCESS on successful retrieval of version.
  1537. */
  1538. static void
  1539. qla2x00_get_fcode_version(scsi_qla_host_t *ha, uint32_t pcids)
  1540. {
  1541. int ret = QLA_FUNCTION_FAILED;
  1542. uint32_t istart, iend, iter, vend;
  1543. uint8_t do_next, rbyte, *vbyte;
  1544. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1545. /* Skip the PCI data structure. */
  1546. istart = pcids +
  1547. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  1548. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  1549. iend = istart + 0x100;
  1550. do {
  1551. /* Scan for the sentinel date string...eeewww. */
  1552. do_next = 0;
  1553. iter = istart;
  1554. while ((iter < iend) && !do_next) {
  1555. iter++;
  1556. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  1557. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  1558. '/')
  1559. do_next++;
  1560. else if (qla2x00_read_flash_byte(ha,
  1561. iter + 3) == '/')
  1562. do_next++;
  1563. }
  1564. }
  1565. if (!do_next)
  1566. break;
  1567. /* Backtrack to previous ' ' (space). */
  1568. do_next = 0;
  1569. while ((iter > istart) && !do_next) {
  1570. iter--;
  1571. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  1572. do_next++;
  1573. }
  1574. if (!do_next)
  1575. break;
  1576. /*
  1577. * Mark end of version tag, and find previous ' ' (space) or
  1578. * string length (recent FCODE images -- major hack ahead!!!).
  1579. */
  1580. vend = iter - 1;
  1581. do_next = 0;
  1582. while ((iter > istart) && !do_next) {
  1583. iter--;
  1584. rbyte = qla2x00_read_flash_byte(ha, iter);
  1585. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  1586. do_next++;
  1587. }
  1588. if (!do_next)
  1589. break;
  1590. /* Mark beginning of version tag, and copy data. */
  1591. iter++;
  1592. if ((vend - iter) &&
  1593. ((vend - iter) < sizeof(ha->fcode_revision))) {
  1594. vbyte = ha->fcode_revision;
  1595. while (iter <= vend) {
  1596. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  1597. iter++;
  1598. }
  1599. ret = QLA_SUCCESS;
  1600. }
  1601. } while (0);
  1602. if (ret != QLA_SUCCESS)
  1603. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1604. }
  1605. int
  1606. qla2x00_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1607. {
  1608. int ret = QLA_SUCCESS;
  1609. uint8_t code_type, last_image;
  1610. uint32_t pcihdr, pcids;
  1611. uint8_t *dbyte;
  1612. uint16_t *dcode;
  1613. if (!ha->pio_address || !mbuf)
  1614. return QLA_FUNCTION_FAILED;
  1615. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1616. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1617. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1618. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1619. qla2x00_flash_enable(ha);
  1620. /* Begin with first PCI expansion ROM header. */
  1621. pcihdr = 0;
  1622. last_image = 1;
  1623. do {
  1624. /* Verify PCI expansion ROM header. */
  1625. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  1626. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  1627. /* No signature */
  1628. DEBUG2(printk("scsi(%ld): No matching ROM "
  1629. "signature.\n", ha->host_no));
  1630. ret = QLA_FUNCTION_FAILED;
  1631. break;
  1632. }
  1633. /* Locate PCI data structure. */
  1634. pcids = pcihdr +
  1635. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  1636. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  1637. /* Validate signature of PCI data structure. */
  1638. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  1639. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  1640. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  1641. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  1642. /* Incorrect header. */
  1643. DEBUG2(printk("%s(): PCI data struct not found "
  1644. "pcir_adr=%x.\n", __func__, pcids));
  1645. ret = QLA_FUNCTION_FAILED;
  1646. break;
  1647. }
  1648. /* Read version */
  1649. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  1650. switch (code_type) {
  1651. case ROM_CODE_TYPE_BIOS:
  1652. /* Intel x86, PC-AT compatible. */
  1653. ha->bios_revision[0] =
  1654. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1655. ha->bios_revision[1] =
  1656. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1657. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1658. ha->bios_revision[1], ha->bios_revision[0]));
  1659. break;
  1660. case ROM_CODE_TYPE_FCODE:
  1661. /* Open Firmware standard for PCI (FCode). */
  1662. /* Eeeewww... */
  1663. qla2x00_get_fcode_version(ha, pcids);
  1664. break;
  1665. case ROM_CODE_TYPE_EFI:
  1666. /* Extensible Firmware Interface (EFI). */
  1667. ha->efi_revision[0] =
  1668. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1669. ha->efi_revision[1] =
  1670. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1671. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1672. ha->efi_revision[1], ha->efi_revision[0]));
  1673. break;
  1674. default:
  1675. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1676. "pcids %x.\n", __func__, code_type, pcids));
  1677. break;
  1678. }
  1679. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  1680. /* Locate next PCI expansion ROM. */
  1681. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  1682. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  1683. } while (!last_image);
  1684. if (IS_QLA2322(ha)) {
  1685. /* Read firmware image information. */
  1686. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1687. dbyte = mbuf;
  1688. memset(dbyte, 0, 8);
  1689. dcode = (uint16_t *)dbyte;
  1690. qla2x00_read_flash_data(ha, dbyte, FA_RISC_CODE_ADDR * 4 + 10,
  1691. 8);
  1692. DEBUG3(printk("%s(%ld): dumping fw ver from flash:\n",
  1693. __func__, ha->host_no));
  1694. DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
  1695. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  1696. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  1697. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1698. dcode[3] == 0)) {
  1699. DEBUG2(printk("%s(): Unrecognized fw revision at "
  1700. "%x.\n", __func__, FA_RISC_CODE_ADDR * 4));
  1701. } else {
  1702. /* values are in big endian */
  1703. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  1704. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  1705. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  1706. }
  1707. }
  1708. qla2x00_flash_disable(ha);
  1709. return ret;
  1710. }
  1711. int
  1712. qla24xx_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1713. {
  1714. int ret = QLA_SUCCESS;
  1715. uint32_t pcihdr, pcids;
  1716. uint32_t *dcode;
  1717. uint8_t *bcode;
  1718. uint8_t code_type, last_image;
  1719. int i;
  1720. if (!mbuf)
  1721. return QLA_FUNCTION_FAILED;
  1722. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1723. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1724. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1725. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1726. dcode = mbuf;
  1727. /* Begin with first PCI expansion ROM header. */
  1728. pcihdr = 0;
  1729. last_image = 1;
  1730. do {
  1731. /* Verify PCI expansion ROM header. */
  1732. qla24xx_read_flash_data(ha, dcode, pcihdr >> 2, 0x20);
  1733. bcode = mbuf + (pcihdr % 4);
  1734. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
  1735. /* No signature */
  1736. DEBUG2(printk("scsi(%ld): No matching ROM "
  1737. "signature.\n", ha->host_no));
  1738. ret = QLA_FUNCTION_FAILED;
  1739. break;
  1740. }
  1741. /* Locate PCI data structure. */
  1742. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  1743. qla24xx_read_flash_data(ha, dcode, pcids >> 2, 0x20);
  1744. bcode = mbuf + (pcihdr % 4);
  1745. /* Validate signature of PCI data structure. */
  1746. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  1747. bcode[0x2] != 'I' || bcode[0x3] != 'R') {
  1748. /* Incorrect header. */
  1749. DEBUG2(printk("%s(): PCI data struct not found "
  1750. "pcir_adr=%x.\n", __func__, pcids));
  1751. ret = QLA_FUNCTION_FAILED;
  1752. break;
  1753. }
  1754. /* Read version */
  1755. code_type = bcode[0x14];
  1756. switch (code_type) {
  1757. case ROM_CODE_TYPE_BIOS:
  1758. /* Intel x86, PC-AT compatible. */
  1759. ha->bios_revision[0] = bcode[0x12];
  1760. ha->bios_revision[1] = bcode[0x13];
  1761. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1762. ha->bios_revision[1], ha->bios_revision[0]));
  1763. break;
  1764. case ROM_CODE_TYPE_FCODE:
  1765. /* Open Firmware standard for PCI (FCode). */
  1766. ha->fcode_revision[0] = bcode[0x12];
  1767. ha->fcode_revision[1] = bcode[0x13];
  1768. DEBUG3(printk("%s(): read FCODE %d.%d.\n", __func__,
  1769. ha->fcode_revision[1], ha->fcode_revision[0]));
  1770. break;
  1771. case ROM_CODE_TYPE_EFI:
  1772. /* Extensible Firmware Interface (EFI). */
  1773. ha->efi_revision[0] = bcode[0x12];
  1774. ha->efi_revision[1] = bcode[0x13];
  1775. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1776. ha->efi_revision[1], ha->efi_revision[0]));
  1777. break;
  1778. default:
  1779. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1780. "pcids %x.\n", __func__, code_type, pcids));
  1781. break;
  1782. }
  1783. last_image = bcode[0x15] & BIT_7;
  1784. /* Locate next PCI expansion ROM. */
  1785. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  1786. } while (!last_image);
  1787. /* Read firmware image information. */
  1788. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1789. dcode = mbuf;
  1790. qla24xx_read_flash_data(ha, dcode, FA_RISC_CODE_ADDR + 4, 4);
  1791. for (i = 0; i < 4; i++)
  1792. dcode[i] = be32_to_cpu(dcode[i]);
  1793. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  1794. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  1795. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1796. dcode[3] == 0)) {
  1797. DEBUG2(printk("%s(): Unrecognized fw version at %x.\n",
  1798. __func__, FA_RISC_CODE_ADDR));
  1799. } else {
  1800. ha->fw_revision[0] = dcode[0];
  1801. ha->fw_revision[1] = dcode[1];
  1802. ha->fw_revision[2] = dcode[2];
  1803. ha->fw_revision[3] = dcode[3];
  1804. }
  1805. return ret;
  1806. }