qla_sup.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  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. #define OPTROM_BURST_SIZE 0x1000
  362. #define OPTROM_BURST_DWORDS (OPTROM_BURST_SIZE / 4)
  363. static inline uint32_t
  364. flash_conf_to_access_addr(uint32_t faddr)
  365. {
  366. return FARX_ACCESS_FLASH_CONF | faddr;
  367. }
  368. static inline uint32_t
  369. flash_data_to_access_addr(uint32_t faddr)
  370. {
  371. return FARX_ACCESS_FLASH_DATA | faddr;
  372. }
  373. static inline uint32_t
  374. nvram_conf_to_access_addr(uint32_t naddr)
  375. {
  376. return FARX_ACCESS_NVRAM_CONF | naddr;
  377. }
  378. static inline uint32_t
  379. nvram_data_to_access_addr(uint32_t naddr)
  380. {
  381. return FARX_ACCESS_NVRAM_DATA | naddr;
  382. }
  383. static uint32_t
  384. qla24xx_read_flash_dword(scsi_qla_host_t *ha, uint32_t addr)
  385. {
  386. int rval;
  387. uint32_t cnt, data;
  388. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  389. WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  390. /* Wait for READ cycle to complete. */
  391. rval = QLA_SUCCESS;
  392. for (cnt = 3000;
  393. (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
  394. rval == QLA_SUCCESS; cnt--) {
  395. if (cnt)
  396. udelay(10);
  397. else
  398. rval = QLA_FUNCTION_TIMEOUT;
  399. cond_resched();
  400. }
  401. /* TODO: What happens if we time out? */
  402. data = 0xDEADDEAD;
  403. if (rval == QLA_SUCCESS)
  404. data = RD_REG_DWORD(&reg->flash_data);
  405. return data;
  406. }
  407. uint32_t *
  408. qla24xx_read_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  409. uint32_t dwords)
  410. {
  411. uint32_t i;
  412. /* Dword reads to flash. */
  413. for (i = 0; i < dwords; i++, faddr++)
  414. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  415. flash_data_to_access_addr(faddr)));
  416. return dwptr;
  417. }
  418. static int
  419. qla24xx_write_flash_dword(scsi_qla_host_t *ha, uint32_t addr, uint32_t data)
  420. {
  421. int rval;
  422. uint32_t cnt;
  423. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  424. WRT_REG_DWORD(&reg->flash_data, data);
  425. RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
  426. WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
  427. /* Wait for Write cycle to complete. */
  428. rval = QLA_SUCCESS;
  429. for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
  430. rval == QLA_SUCCESS; cnt--) {
  431. if (cnt)
  432. udelay(10);
  433. else
  434. rval = QLA_FUNCTION_TIMEOUT;
  435. cond_resched();
  436. }
  437. return rval;
  438. }
  439. static void
  440. qla24xx_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  441. uint8_t *flash_id)
  442. {
  443. uint32_t ids;
  444. ids = qla24xx_read_flash_dword(ha, flash_data_to_access_addr(0xd03ab));
  445. *man_id = LSB(ids);
  446. *flash_id = MSB(ids);
  447. /* Check if man_id and flash_id are valid. */
  448. if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
  449. /* Read information using 0x9f opcode
  450. * Device ID, Mfg ID would be read in the format:
  451. * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
  452. * Example: ATMEL 0x00 01 45 1F
  453. * Extract MFG and Dev ID from last two bytes.
  454. */
  455. ids = qla24xx_read_flash_dword(ha,
  456. flash_data_to_access_addr(0xd009f));
  457. *man_id = LSB(ids);
  458. *flash_id = MSB(ids);
  459. }
  460. }
  461. static int
  462. qla24xx_write_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  463. uint32_t dwords)
  464. {
  465. int ret;
  466. uint32_t liter, miter;
  467. uint32_t sec_mask, rest_addr, conf_addr;
  468. uint32_t fdata, findex ;
  469. uint8_t man_id, flash_id;
  470. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  471. dma_addr_t optrom_dma;
  472. void *optrom = NULL;
  473. uint32_t *s, *d;
  474. ret = QLA_SUCCESS;
  475. /* Prepare burst-capable write on supported ISPs. */
  476. if (IS_QLA25XX(ha) && !(faddr & 0xfff) &&
  477. dwords > OPTROM_BURST_DWORDS) {
  478. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  479. &optrom_dma, GFP_KERNEL);
  480. if (!optrom) {
  481. qla_printk(KERN_DEBUG, ha,
  482. "Unable to allocate memory for optrom burst write "
  483. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  484. }
  485. }
  486. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  487. DEBUG9(printk("%s(%ld): Flash man_id=%d flash_id=%d\n", __func__,
  488. ha->host_no, man_id, flash_id));
  489. conf_addr = flash_conf_to_access_addr(0x03d8);
  490. switch (man_id) {
  491. case 0xbf: /* STT flash. */
  492. if (flash_id == 0x8e) {
  493. rest_addr = 0x3fff;
  494. sec_mask = 0x7c000;
  495. } else {
  496. rest_addr = 0x1fff;
  497. sec_mask = 0x7e000;
  498. }
  499. if (flash_id == 0x80)
  500. conf_addr = flash_conf_to_access_addr(0x0352);
  501. break;
  502. case 0x13: /* ST M25P80. */
  503. rest_addr = 0x3fff;
  504. sec_mask = 0x7c000;
  505. break;
  506. case 0x1f: // Atmel 26DF081A
  507. rest_addr = 0x3fff;
  508. sec_mask = 0x7c000;
  509. conf_addr = flash_conf_to_access_addr(0x0320);
  510. break;
  511. default:
  512. /* Default to 64 kb sector size. */
  513. rest_addr = 0x3fff;
  514. sec_mask = 0x7c000;
  515. break;
  516. }
  517. /* Enable flash write. */
  518. WRT_REG_DWORD(&reg->ctrl_status,
  519. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  520. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  521. /* Disable flash write-protection. */
  522. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  523. /* Some flash parts need an additional zero-write to clear bits.*/
  524. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  525. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  526. if (man_id == 0x1f) {
  527. findex = faddr << 2;
  528. fdata = findex & sec_mask;
  529. } else {
  530. findex = faddr;
  531. fdata = (findex & sec_mask) << 2;
  532. }
  533. /* Are we at the beginning of a sector? */
  534. if ((findex & rest_addr) == 0) {
  535. /* Do sector unprotect at 4K boundry for Atmel part. */
  536. if (man_id == 0x1f)
  537. qla24xx_write_flash_dword(ha,
  538. flash_conf_to_access_addr(0x0339),
  539. (fdata & 0xff00) | ((fdata << 16) &
  540. 0xff0000) | ((fdata >> 16) & 0xff));
  541. ret = qla24xx_write_flash_dword(ha, conf_addr,
  542. (fdata & 0xff00) |((fdata << 16) &
  543. 0xff0000) | ((fdata >> 16) & 0xff));
  544. if (ret != QLA_SUCCESS) {
  545. DEBUG9(printk("%s(%ld) Unable to flash "
  546. "sector: address=%x.\n", __func__,
  547. ha->host_no, faddr));
  548. break;
  549. }
  550. }
  551. /* Go with burst-write. */
  552. if (optrom && (liter + OPTROM_BURST_DWORDS) < dwords) {
  553. /* Copy data to DMA'ble buffer. */
  554. for (miter = 0, s = optrom, d = dwptr;
  555. miter < OPTROM_BURST_DWORDS; miter++, s++, d++)
  556. *s = cpu_to_le32(*d);
  557. ret = qla2x00_load_ram(ha, optrom_dma,
  558. flash_data_to_access_addr(faddr),
  559. OPTROM_BURST_DWORDS);
  560. if (ret != QLA_SUCCESS) {
  561. qla_printk(KERN_WARNING, ha,
  562. "Unable to burst-write optrom segment "
  563. "(%x/%x/%llx).\n", ret,
  564. flash_data_to_access_addr(faddr),
  565. optrom_dma);
  566. qla_printk(KERN_WARNING, ha,
  567. "Reverting to slow-write.\n");
  568. dma_free_coherent(&ha->pdev->dev,
  569. OPTROM_BURST_SIZE, optrom, optrom_dma);
  570. optrom = NULL;
  571. } else {
  572. liter += OPTROM_BURST_DWORDS - 1;
  573. faddr += OPTROM_BURST_DWORDS - 1;
  574. dwptr += OPTROM_BURST_DWORDS - 1;
  575. continue;
  576. }
  577. }
  578. ret = qla24xx_write_flash_dword(ha,
  579. flash_data_to_access_addr(faddr), cpu_to_le32(*dwptr));
  580. if (ret != QLA_SUCCESS) {
  581. DEBUG9(printk("%s(%ld) Unable to program flash "
  582. "address=%x data=%x.\n", __func__,
  583. ha->host_no, faddr, *dwptr));
  584. break;
  585. }
  586. /* Do sector protect at 4K boundry for Atmel part. */
  587. if (man_id == 0x1f &&
  588. ((faddr & rest_addr) == rest_addr))
  589. qla24xx_write_flash_dword(ha,
  590. flash_conf_to_access_addr(0x0336),
  591. (fdata & 0xff00) | ((fdata << 16) &
  592. 0xff0000) | ((fdata >> 16) & 0xff));
  593. }
  594. /* Enable flash write-protection. */
  595. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0x9c);
  596. /* Disable flash write. */
  597. WRT_REG_DWORD(&reg->ctrl_status,
  598. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  599. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  600. if (optrom)
  601. dma_free_coherent(&ha->pdev->dev,
  602. OPTROM_BURST_SIZE, optrom, optrom_dma);
  603. return ret;
  604. }
  605. uint8_t *
  606. qla2x00_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  607. uint32_t bytes)
  608. {
  609. uint32_t i;
  610. uint16_t *wptr;
  611. /* Word reads to NVRAM via registers. */
  612. wptr = (uint16_t *)buf;
  613. qla2x00_lock_nvram_access(ha);
  614. for (i = 0; i < bytes >> 1; i++, naddr++)
  615. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  616. naddr));
  617. qla2x00_unlock_nvram_access(ha);
  618. return buf;
  619. }
  620. uint8_t *
  621. qla24xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  622. uint32_t bytes)
  623. {
  624. uint32_t i;
  625. uint32_t *dwptr;
  626. /* Dword reads to flash. */
  627. dwptr = (uint32_t *)buf;
  628. for (i = 0; i < bytes >> 2; i++, naddr++)
  629. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  630. nvram_data_to_access_addr(naddr)));
  631. return buf;
  632. }
  633. int
  634. qla2x00_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  635. uint32_t bytes)
  636. {
  637. int ret, stat;
  638. uint32_t i;
  639. uint16_t *wptr;
  640. ret = QLA_SUCCESS;
  641. qla2x00_lock_nvram_access(ha);
  642. /* Disable NVRAM write-protection. */
  643. stat = qla2x00_clear_nvram_protection(ha);
  644. wptr = (uint16_t *)buf;
  645. for (i = 0; i < bytes >> 1; i++, naddr++) {
  646. qla2x00_write_nvram_word(ha, naddr,
  647. cpu_to_le16(*wptr));
  648. wptr++;
  649. }
  650. /* Enable NVRAM write-protection. */
  651. qla2x00_set_nvram_protection(ha, stat);
  652. qla2x00_unlock_nvram_access(ha);
  653. return ret;
  654. }
  655. int
  656. qla24xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  657. uint32_t bytes)
  658. {
  659. int ret;
  660. uint32_t i;
  661. uint32_t *dwptr;
  662. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  663. ret = QLA_SUCCESS;
  664. /* Enable flash write. */
  665. WRT_REG_DWORD(&reg->ctrl_status,
  666. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  667. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  668. /* Disable NVRAM write-protection. */
  669. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  670. 0);
  671. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  672. 0);
  673. /* Dword writes to flash. */
  674. dwptr = (uint32_t *)buf;
  675. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  676. ret = qla24xx_write_flash_dword(ha,
  677. nvram_data_to_access_addr(naddr),
  678. cpu_to_le32(*dwptr));
  679. if (ret != QLA_SUCCESS) {
  680. DEBUG9(printk("%s(%ld) Unable to program "
  681. "nvram address=%x data=%x.\n", __func__,
  682. ha->host_no, naddr, *dwptr));
  683. break;
  684. }
  685. }
  686. /* Enable NVRAM write-protection. */
  687. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  688. 0x8c);
  689. /* Disable flash write. */
  690. WRT_REG_DWORD(&reg->ctrl_status,
  691. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  692. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  693. return ret;
  694. }
  695. uint8_t *
  696. qla25xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  697. uint32_t bytes)
  698. {
  699. uint32_t i;
  700. uint32_t *dwptr;
  701. /* Dword reads to flash. */
  702. dwptr = (uint32_t *)buf;
  703. for (i = 0; i < bytes >> 2; i++, naddr++)
  704. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  705. flash_data_to_access_addr(FA_VPD_NVRAM_ADDR | naddr)));
  706. return buf;
  707. }
  708. int
  709. qla25xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  710. uint32_t bytes)
  711. {
  712. return qla24xx_write_flash_data(ha, (uint32_t *)buf,
  713. FA_VPD_NVRAM_ADDR | naddr, bytes >> 2);
  714. }
  715. static inline void
  716. qla2x00_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  717. {
  718. if (IS_QLA2322(ha)) {
  719. /* Flip all colors. */
  720. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  721. /* Turn off. */
  722. ha->beacon_color_state = 0;
  723. *pflags = GPIO_LED_ALL_OFF;
  724. } else {
  725. /* Turn on. */
  726. ha->beacon_color_state = QLA_LED_ALL_ON;
  727. *pflags = GPIO_LED_RGA_ON;
  728. }
  729. } else {
  730. /* Flip green led only. */
  731. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  732. /* Turn off. */
  733. ha->beacon_color_state = 0;
  734. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  735. } else {
  736. /* Turn on. */
  737. ha->beacon_color_state = QLA_LED_GRN_ON;
  738. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  739. }
  740. }
  741. }
  742. void
  743. qla2x00_beacon_blink(struct scsi_qla_host *ha)
  744. {
  745. uint16_t gpio_enable;
  746. uint16_t gpio_data;
  747. uint16_t led_color = 0;
  748. unsigned long flags;
  749. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  750. if (ha->pio_address)
  751. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  752. spin_lock_irqsave(&ha->hardware_lock, flags);
  753. /* Save the Original GPIOE. */
  754. if (ha->pio_address) {
  755. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  756. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  757. } else {
  758. gpio_enable = RD_REG_WORD(&reg->gpioe);
  759. gpio_data = RD_REG_WORD(&reg->gpiod);
  760. }
  761. /* Set the modified gpio_enable values */
  762. gpio_enable |= GPIO_LED_MASK;
  763. if (ha->pio_address) {
  764. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  765. } else {
  766. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  767. RD_REG_WORD(&reg->gpioe);
  768. }
  769. qla2x00_flip_colors(ha, &led_color);
  770. /* Clear out any previously set LED color. */
  771. gpio_data &= ~GPIO_LED_MASK;
  772. /* Set the new input LED color to GPIOD. */
  773. gpio_data |= led_color;
  774. /* Set the modified gpio_data values */
  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. int
  784. qla2x00_beacon_on(struct scsi_qla_host *ha)
  785. {
  786. uint16_t gpio_enable;
  787. uint16_t gpio_data;
  788. unsigned long flags;
  789. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  790. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  791. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  792. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  793. qla_printk(KERN_WARNING, ha,
  794. "Unable to update fw options (beacon on).\n");
  795. return QLA_FUNCTION_FAILED;
  796. }
  797. if (ha->pio_address)
  798. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  799. /* Turn off LEDs. */
  800. spin_lock_irqsave(&ha->hardware_lock, flags);
  801. if (ha->pio_address) {
  802. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  803. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  804. } else {
  805. gpio_enable = RD_REG_WORD(&reg->gpioe);
  806. gpio_data = RD_REG_WORD(&reg->gpiod);
  807. }
  808. gpio_enable |= GPIO_LED_MASK;
  809. /* Set the modified gpio_enable values. */
  810. if (ha->pio_address) {
  811. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  812. } else {
  813. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  814. RD_REG_WORD(&reg->gpioe);
  815. }
  816. /* Clear out previously set LED colour. */
  817. gpio_data &= ~GPIO_LED_MASK;
  818. if (ha->pio_address) {
  819. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  820. } else {
  821. WRT_REG_WORD(&reg->gpiod, gpio_data);
  822. RD_REG_WORD(&reg->gpiod);
  823. }
  824. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  825. /*
  826. * Let the per HBA timer kick off the blinking process based on
  827. * the following flags. No need to do anything else now.
  828. */
  829. ha->beacon_blink_led = 1;
  830. ha->beacon_color_state = 0;
  831. return QLA_SUCCESS;
  832. }
  833. int
  834. qla2x00_beacon_off(struct scsi_qla_host *ha)
  835. {
  836. int rval = QLA_SUCCESS;
  837. ha->beacon_blink_led = 0;
  838. /* Set the on flag so when it gets flipped it will be off. */
  839. if (IS_QLA2322(ha))
  840. ha->beacon_color_state = QLA_LED_ALL_ON;
  841. else
  842. ha->beacon_color_state = QLA_LED_GRN_ON;
  843. ha->isp_ops->beacon_blink(ha); /* This turns green LED off */
  844. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  845. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  846. rval = qla2x00_set_fw_options(ha, ha->fw_options);
  847. if (rval != QLA_SUCCESS)
  848. qla_printk(KERN_WARNING, ha,
  849. "Unable to update fw options (beacon off).\n");
  850. return rval;
  851. }
  852. static inline void
  853. qla24xx_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  854. {
  855. /* Flip all colors. */
  856. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  857. /* Turn off. */
  858. ha->beacon_color_state = 0;
  859. *pflags = 0;
  860. } else {
  861. /* Turn on. */
  862. ha->beacon_color_state = QLA_LED_ALL_ON;
  863. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  864. }
  865. }
  866. void
  867. qla24xx_beacon_blink(struct scsi_qla_host *ha)
  868. {
  869. uint16_t led_color = 0;
  870. uint32_t gpio_data;
  871. unsigned long flags;
  872. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  873. /* Save the Original GPIOD. */
  874. spin_lock_irqsave(&ha->hardware_lock, flags);
  875. gpio_data = RD_REG_DWORD(&reg->gpiod);
  876. /* Enable the gpio_data reg for update. */
  877. gpio_data |= GPDX_LED_UPDATE_MASK;
  878. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  879. gpio_data = RD_REG_DWORD(&reg->gpiod);
  880. /* Set the color bits. */
  881. qla24xx_flip_colors(ha, &led_color);
  882. /* Clear out any previously set LED color. */
  883. gpio_data &= ~GPDX_LED_COLOR_MASK;
  884. /* Set the new input LED color to GPIOD. */
  885. gpio_data |= led_color;
  886. /* Set the modified gpio_data values. */
  887. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  888. gpio_data = RD_REG_DWORD(&reg->gpiod);
  889. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  890. }
  891. int
  892. qla24xx_beacon_on(struct scsi_qla_host *ha)
  893. {
  894. uint32_t gpio_data;
  895. unsigned long flags;
  896. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  897. if (ha->beacon_blink_led == 0) {
  898. /* Enable firmware for update */
  899. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  900. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS)
  901. return QLA_FUNCTION_FAILED;
  902. if (qla2x00_get_fw_options(ha, ha->fw_options) !=
  903. QLA_SUCCESS) {
  904. qla_printk(KERN_WARNING, ha,
  905. "Unable to update fw options (beacon on).\n");
  906. return QLA_FUNCTION_FAILED;
  907. }
  908. spin_lock_irqsave(&ha->hardware_lock, flags);
  909. gpio_data = RD_REG_DWORD(&reg->gpiod);
  910. /* Enable the gpio_data reg for update. */
  911. gpio_data |= GPDX_LED_UPDATE_MASK;
  912. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  913. RD_REG_DWORD(&reg->gpiod);
  914. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  915. }
  916. /* So all colors blink together. */
  917. ha->beacon_color_state = 0;
  918. /* Let the per HBA timer kick off the blinking process. */
  919. ha->beacon_blink_led = 1;
  920. return QLA_SUCCESS;
  921. }
  922. int
  923. qla24xx_beacon_off(struct scsi_qla_host *ha)
  924. {
  925. uint32_t gpio_data;
  926. unsigned long flags;
  927. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  928. ha->beacon_blink_led = 0;
  929. ha->beacon_color_state = QLA_LED_ALL_ON;
  930. ha->isp_ops->beacon_blink(ha); /* Will flip to all off. */
  931. /* Give control back to firmware. */
  932. spin_lock_irqsave(&ha->hardware_lock, flags);
  933. gpio_data = RD_REG_DWORD(&reg->gpiod);
  934. /* Disable the gpio_data reg for update. */
  935. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  936. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  937. RD_REG_DWORD(&reg->gpiod);
  938. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  939. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  940. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  941. qla_printk(KERN_WARNING, ha,
  942. "Unable to update fw options (beacon off).\n");
  943. return QLA_FUNCTION_FAILED;
  944. }
  945. if (qla2x00_get_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  946. qla_printk(KERN_WARNING, ha,
  947. "Unable to get fw options (beacon off).\n");
  948. return QLA_FUNCTION_FAILED;
  949. }
  950. return QLA_SUCCESS;
  951. }
  952. /*
  953. * Flash support routines
  954. */
  955. /**
  956. * qla2x00_flash_enable() - Setup flash for reading and writing.
  957. * @ha: HA context
  958. */
  959. static void
  960. qla2x00_flash_enable(scsi_qla_host_t *ha)
  961. {
  962. uint16_t data;
  963. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  964. data = RD_REG_WORD(&reg->ctrl_status);
  965. data |= CSR_FLASH_ENABLE;
  966. WRT_REG_WORD(&reg->ctrl_status, data);
  967. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  968. }
  969. /**
  970. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  971. * @ha: HA context
  972. */
  973. static void
  974. qla2x00_flash_disable(scsi_qla_host_t *ha)
  975. {
  976. uint16_t data;
  977. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  978. data = RD_REG_WORD(&reg->ctrl_status);
  979. data &= ~(CSR_FLASH_ENABLE);
  980. WRT_REG_WORD(&reg->ctrl_status, data);
  981. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  982. }
  983. /**
  984. * qla2x00_read_flash_byte() - Reads a byte from flash
  985. * @ha: HA context
  986. * @addr: Address in flash to read
  987. *
  988. * A word is read from the chip, but, only the lower byte is valid.
  989. *
  990. * Returns the byte read from flash @addr.
  991. */
  992. static uint8_t
  993. qla2x00_read_flash_byte(scsi_qla_host_t *ha, uint32_t addr)
  994. {
  995. uint16_t data;
  996. uint16_t bank_select;
  997. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  998. bank_select = RD_REG_WORD(&reg->ctrl_status);
  999. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1000. /* Specify 64K address range: */
  1001. /* clear out Module Select and Flash Address bits [19:16]. */
  1002. bank_select &= ~0xf8;
  1003. bank_select |= addr >> 12 & 0xf0;
  1004. bank_select |= CSR_FLASH_64K_BANK;
  1005. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1006. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1007. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1008. data = RD_REG_WORD(&reg->flash_data);
  1009. return (uint8_t)data;
  1010. }
  1011. /* Setup bit 16 of flash address. */
  1012. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  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. } else if (((addr & BIT_16) == 0) &&
  1017. (bank_select & CSR_FLASH_64K_BANK)) {
  1018. bank_select &= ~(CSR_FLASH_64K_BANK);
  1019. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1020. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1021. }
  1022. /* Always perform IO mapped accesses to the FLASH registers. */
  1023. if (ha->pio_address) {
  1024. uint16_t data2;
  1025. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  1026. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  1027. do {
  1028. data = RD_REG_WORD_PIO(&reg->flash_data);
  1029. barrier();
  1030. cpu_relax();
  1031. data2 = RD_REG_WORD_PIO(&reg->flash_data);
  1032. } while (data != data2);
  1033. } else {
  1034. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1035. data = qla2x00_debounce_register(&reg->flash_data);
  1036. }
  1037. return (uint8_t)data;
  1038. }
  1039. /**
  1040. * qla2x00_write_flash_byte() - Write a byte to flash
  1041. * @ha: HA context
  1042. * @addr: Address in flash to write
  1043. * @data: Data to write
  1044. */
  1045. static void
  1046. qla2x00_write_flash_byte(scsi_qla_host_t *ha, uint32_t addr, uint8_t data)
  1047. {
  1048. uint16_t bank_select;
  1049. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1050. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1051. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1052. /* Specify 64K address range: */
  1053. /* clear out Module Select and Flash Address bits [19:16]. */
  1054. bank_select &= ~0xf8;
  1055. bank_select |= addr >> 12 & 0xf0;
  1056. bank_select |= CSR_FLASH_64K_BANK;
  1057. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1058. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1059. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1060. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1061. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1062. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1063. return;
  1064. }
  1065. /* Setup bit 16 of flash address. */
  1066. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1067. bank_select |= CSR_FLASH_64K_BANK;
  1068. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1069. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1070. } else if (((addr & BIT_16) == 0) &&
  1071. (bank_select & CSR_FLASH_64K_BANK)) {
  1072. bank_select &= ~(CSR_FLASH_64K_BANK);
  1073. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1074. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1075. }
  1076. /* Always perform IO mapped accesses to the FLASH registers. */
  1077. if (ha->pio_address) {
  1078. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  1079. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  1080. WRT_REG_WORD_PIO(&reg->flash_data, (uint16_t)data);
  1081. } else {
  1082. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1083. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1084. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1085. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1086. }
  1087. }
  1088. /**
  1089. * qla2x00_poll_flash() - Polls flash for completion.
  1090. * @ha: HA context
  1091. * @addr: Address in flash to poll
  1092. * @poll_data: Data to be polled
  1093. * @man_id: Flash manufacturer ID
  1094. * @flash_id: Flash ID
  1095. *
  1096. * This function polls the device until bit 7 of what is read matches data
  1097. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1098. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1099. * reading bit 5 as a 1.
  1100. *
  1101. * Returns 0 on success, else non-zero.
  1102. */
  1103. static int
  1104. qla2x00_poll_flash(scsi_qla_host_t *ha, uint32_t addr, uint8_t poll_data,
  1105. uint8_t man_id, uint8_t flash_id)
  1106. {
  1107. int status;
  1108. uint8_t flash_data;
  1109. uint32_t cnt;
  1110. status = 1;
  1111. /* Wait for 30 seconds for command to finish. */
  1112. poll_data &= BIT_7;
  1113. for (cnt = 3000000; cnt; cnt--) {
  1114. flash_data = qla2x00_read_flash_byte(ha, addr);
  1115. if ((flash_data & BIT_7) == poll_data) {
  1116. status = 0;
  1117. break;
  1118. }
  1119. if (man_id != 0x40 && man_id != 0xda) {
  1120. if ((flash_data & BIT_5) && cnt > 2)
  1121. cnt = 2;
  1122. }
  1123. udelay(10);
  1124. barrier();
  1125. cond_resched();
  1126. }
  1127. return status;
  1128. }
  1129. /**
  1130. * qla2x00_program_flash_address() - Programs a flash address
  1131. * @ha: HA context
  1132. * @addr: Address in flash to program
  1133. * @data: Data to be written in flash
  1134. * @man_id: Flash manufacturer ID
  1135. * @flash_id: Flash ID
  1136. *
  1137. * Returns 0 on success, else non-zero.
  1138. */
  1139. static int
  1140. qla2x00_program_flash_address(scsi_qla_host_t *ha, uint32_t addr, uint8_t data,
  1141. uint8_t man_id, uint8_t flash_id)
  1142. {
  1143. /* Write Program Command Sequence. */
  1144. if (IS_OEM_001(ha)) {
  1145. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1146. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1147. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1148. qla2x00_write_flash_byte(ha, addr, data);
  1149. } else {
  1150. if (man_id == 0xda && flash_id == 0xc1) {
  1151. qla2x00_write_flash_byte(ha, addr, data);
  1152. if (addr & 0x7e)
  1153. return 0;
  1154. } else {
  1155. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1156. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1157. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1158. qla2x00_write_flash_byte(ha, addr, data);
  1159. }
  1160. }
  1161. udelay(150);
  1162. /* Wait for write to complete. */
  1163. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1164. }
  1165. /**
  1166. * qla2x00_erase_flash() - Erase the flash.
  1167. * @ha: HA context
  1168. * @man_id: Flash manufacturer ID
  1169. * @flash_id: Flash ID
  1170. *
  1171. * Returns 0 on success, else non-zero.
  1172. */
  1173. static int
  1174. qla2x00_erase_flash(scsi_qla_host_t *ha, uint8_t man_id, uint8_t flash_id)
  1175. {
  1176. /* Individual Sector Erase Command Sequence */
  1177. if (IS_OEM_001(ha)) {
  1178. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1179. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1180. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1181. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1182. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1183. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1184. } else {
  1185. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1186. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1187. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1188. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1189. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1190. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1191. }
  1192. udelay(150);
  1193. /* Wait for erase to complete. */
  1194. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1195. }
  1196. /**
  1197. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1198. * @ha: HA context
  1199. * @addr: Flash sector to erase
  1200. * @sec_mask: Sector address mask
  1201. * @man_id: Flash manufacturer ID
  1202. * @flash_id: Flash ID
  1203. *
  1204. * Returns 0 on success, else non-zero.
  1205. */
  1206. static int
  1207. qla2x00_erase_flash_sector(scsi_qla_host_t *ha, uint32_t addr,
  1208. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1209. {
  1210. /* Individual Sector Erase Command Sequence */
  1211. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1212. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1213. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1214. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1215. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1216. if (man_id == 0x1f && flash_id == 0x13)
  1217. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1218. else
  1219. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1220. udelay(150);
  1221. /* Wait for erase to complete. */
  1222. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1223. }
  1224. /**
  1225. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1226. * @man_id: Flash manufacturer ID
  1227. * @flash_id: Flash ID
  1228. */
  1229. static void
  1230. qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  1231. uint8_t *flash_id)
  1232. {
  1233. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1234. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1235. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1236. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1237. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1238. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1239. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1240. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1241. }
  1242. static void
  1243. qla2x00_read_flash_data(scsi_qla_host_t *ha, uint8_t *tmp_buf, uint32_t saddr,
  1244. uint32_t length)
  1245. {
  1246. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1247. uint32_t midpoint, ilength;
  1248. uint8_t data;
  1249. midpoint = length / 2;
  1250. WRT_REG_WORD(&reg->nvram, 0);
  1251. RD_REG_WORD(&reg->nvram);
  1252. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1253. if (ilength == midpoint) {
  1254. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1255. RD_REG_WORD(&reg->nvram);
  1256. }
  1257. data = qla2x00_read_flash_byte(ha, saddr);
  1258. if (saddr % 100)
  1259. udelay(10);
  1260. *tmp_buf = data;
  1261. cond_resched();
  1262. }
  1263. }
  1264. static inline void
  1265. qla2x00_suspend_hba(struct scsi_qla_host *ha)
  1266. {
  1267. int cnt;
  1268. unsigned long flags;
  1269. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1270. /* Suspend HBA. */
  1271. scsi_block_requests(ha->host);
  1272. ha->isp_ops->disable_intrs(ha);
  1273. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1274. /* Pause RISC. */
  1275. spin_lock_irqsave(&ha->hardware_lock, flags);
  1276. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1277. RD_REG_WORD(&reg->hccr);
  1278. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1279. for (cnt = 0; cnt < 30000; cnt++) {
  1280. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1281. break;
  1282. udelay(100);
  1283. }
  1284. } else {
  1285. udelay(10);
  1286. }
  1287. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1288. }
  1289. static inline void
  1290. qla2x00_resume_hba(struct scsi_qla_host *ha)
  1291. {
  1292. /* Resume HBA. */
  1293. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1294. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1295. qla2xxx_wake_dpc(ha);
  1296. qla2x00_wait_for_hba_online(ha);
  1297. scsi_unblock_requests(ha->host);
  1298. }
  1299. uint8_t *
  1300. qla2x00_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1301. uint32_t offset, uint32_t length)
  1302. {
  1303. uint32_t addr, midpoint;
  1304. uint8_t *data;
  1305. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1306. /* Suspend HBA. */
  1307. qla2x00_suspend_hba(ha);
  1308. /* Go with read. */
  1309. midpoint = ha->optrom_size / 2;
  1310. qla2x00_flash_enable(ha);
  1311. WRT_REG_WORD(&reg->nvram, 0);
  1312. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1313. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1314. if (addr == midpoint) {
  1315. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1316. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1317. }
  1318. *data = qla2x00_read_flash_byte(ha, addr);
  1319. }
  1320. qla2x00_flash_disable(ha);
  1321. /* Resume HBA. */
  1322. qla2x00_resume_hba(ha);
  1323. return buf;
  1324. }
  1325. int
  1326. qla2x00_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1327. uint32_t offset, uint32_t length)
  1328. {
  1329. int rval;
  1330. uint8_t man_id, flash_id, sec_number, data;
  1331. uint16_t wd;
  1332. uint32_t addr, liter, sec_mask, rest_addr;
  1333. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1334. /* Suspend HBA. */
  1335. qla2x00_suspend_hba(ha);
  1336. rval = QLA_SUCCESS;
  1337. sec_number = 0;
  1338. /* Reset ISP chip. */
  1339. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1340. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1341. /* Go with write. */
  1342. qla2x00_flash_enable(ha);
  1343. do { /* Loop once to provide quick error exit */
  1344. /* Structure of flash memory based on manufacturer */
  1345. if (IS_OEM_001(ha)) {
  1346. /* OEM variant with special flash part. */
  1347. man_id = flash_id = 0;
  1348. rest_addr = 0xffff;
  1349. sec_mask = 0x10000;
  1350. goto update_flash;
  1351. }
  1352. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1353. switch (man_id) {
  1354. case 0x20: /* ST flash. */
  1355. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1356. /*
  1357. * ST m29w008at part - 64kb sector size with
  1358. * 32kb,8kb,8kb,16kb sectors at memory address
  1359. * 0xf0000.
  1360. */
  1361. rest_addr = 0xffff;
  1362. sec_mask = 0x10000;
  1363. break;
  1364. }
  1365. /*
  1366. * ST m29w010b part - 16kb sector size
  1367. * Default to 16kb sectors
  1368. */
  1369. rest_addr = 0x3fff;
  1370. sec_mask = 0x1c000;
  1371. break;
  1372. case 0x40: /* Mostel flash. */
  1373. /* Mostel v29c51001 part - 512 byte sector size. */
  1374. rest_addr = 0x1ff;
  1375. sec_mask = 0x1fe00;
  1376. break;
  1377. case 0xbf: /* SST flash. */
  1378. /* SST39sf10 part - 4kb sector size. */
  1379. rest_addr = 0xfff;
  1380. sec_mask = 0x1f000;
  1381. break;
  1382. case 0xda: /* Winbond flash. */
  1383. /* Winbond W29EE011 part - 256 byte sector size. */
  1384. rest_addr = 0x7f;
  1385. sec_mask = 0x1ff80;
  1386. break;
  1387. case 0xc2: /* Macronix flash. */
  1388. /* 64k sector size. */
  1389. if (flash_id == 0x38 || flash_id == 0x4f) {
  1390. rest_addr = 0xffff;
  1391. sec_mask = 0x10000;
  1392. break;
  1393. }
  1394. /* Fall through... */
  1395. case 0x1f: /* Atmel flash. */
  1396. /* 512k sector size. */
  1397. if (flash_id == 0x13) {
  1398. rest_addr = 0x7fffffff;
  1399. sec_mask = 0x80000000;
  1400. break;
  1401. }
  1402. /* Fall through... */
  1403. case 0x01: /* AMD flash. */
  1404. if (flash_id == 0x38 || flash_id == 0x40 ||
  1405. flash_id == 0x4f) {
  1406. /* Am29LV081 part - 64kb sector size. */
  1407. /* Am29LV002BT part - 64kb sector size. */
  1408. rest_addr = 0xffff;
  1409. sec_mask = 0x10000;
  1410. break;
  1411. } else if (flash_id == 0x3e) {
  1412. /*
  1413. * Am29LV008b part - 64kb sector size with
  1414. * 32kb,8kb,8kb,16kb sector at memory address
  1415. * h0xf0000.
  1416. */
  1417. rest_addr = 0xffff;
  1418. sec_mask = 0x10000;
  1419. break;
  1420. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1421. /*
  1422. * Am29LV010 part or AM29f010 - 16kb sector
  1423. * size.
  1424. */
  1425. rest_addr = 0x3fff;
  1426. sec_mask = 0x1c000;
  1427. break;
  1428. } else if (flash_id == 0x6d) {
  1429. /* Am29LV001 part - 8kb sector size. */
  1430. rest_addr = 0x1fff;
  1431. sec_mask = 0x1e000;
  1432. break;
  1433. }
  1434. default:
  1435. /* Default to 16 kb sector size. */
  1436. rest_addr = 0x3fff;
  1437. sec_mask = 0x1c000;
  1438. break;
  1439. }
  1440. update_flash:
  1441. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1442. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1443. rval = QLA_FUNCTION_FAILED;
  1444. break;
  1445. }
  1446. }
  1447. for (addr = offset, liter = 0; liter < length; liter++,
  1448. addr++) {
  1449. data = buf[liter];
  1450. /* Are we at the beginning of a sector? */
  1451. if ((addr & rest_addr) == 0) {
  1452. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1453. if (addr >= 0x10000UL) {
  1454. if (((addr >> 12) & 0xf0) &&
  1455. ((man_id == 0x01 &&
  1456. flash_id == 0x3e) ||
  1457. (man_id == 0x20 &&
  1458. flash_id == 0xd2))) {
  1459. sec_number++;
  1460. if (sec_number == 1) {
  1461. rest_addr =
  1462. 0x7fff;
  1463. sec_mask =
  1464. 0x18000;
  1465. } else if (
  1466. sec_number == 2 ||
  1467. sec_number == 3) {
  1468. rest_addr =
  1469. 0x1fff;
  1470. sec_mask =
  1471. 0x1e000;
  1472. } else if (
  1473. sec_number == 4) {
  1474. rest_addr =
  1475. 0x3fff;
  1476. sec_mask =
  1477. 0x1c000;
  1478. }
  1479. }
  1480. }
  1481. } else if (addr == ha->optrom_size / 2) {
  1482. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1483. RD_REG_WORD(&reg->nvram);
  1484. }
  1485. if (flash_id == 0xda && man_id == 0xc1) {
  1486. qla2x00_write_flash_byte(ha, 0x5555,
  1487. 0xaa);
  1488. qla2x00_write_flash_byte(ha, 0x2aaa,
  1489. 0x55);
  1490. qla2x00_write_flash_byte(ha, 0x5555,
  1491. 0xa0);
  1492. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1493. /* Then erase it */
  1494. if (qla2x00_erase_flash_sector(ha,
  1495. addr, sec_mask, man_id,
  1496. flash_id)) {
  1497. rval = QLA_FUNCTION_FAILED;
  1498. break;
  1499. }
  1500. if (man_id == 0x01 && flash_id == 0x6d)
  1501. sec_number++;
  1502. }
  1503. }
  1504. if (man_id == 0x01 && flash_id == 0x6d) {
  1505. if (sec_number == 1 &&
  1506. addr == (rest_addr - 1)) {
  1507. rest_addr = 0x0fff;
  1508. sec_mask = 0x1f000;
  1509. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1510. rest_addr = 0x3fff;
  1511. sec_mask = 0x1c000;
  1512. }
  1513. }
  1514. if (qla2x00_program_flash_address(ha, addr, data,
  1515. man_id, flash_id)) {
  1516. rval = QLA_FUNCTION_FAILED;
  1517. break;
  1518. }
  1519. cond_resched();
  1520. }
  1521. } while (0);
  1522. qla2x00_flash_disable(ha);
  1523. /* Resume HBA. */
  1524. qla2x00_resume_hba(ha);
  1525. return rval;
  1526. }
  1527. uint8_t *
  1528. qla24xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1529. uint32_t offset, uint32_t length)
  1530. {
  1531. /* Suspend HBA. */
  1532. scsi_block_requests(ha->host);
  1533. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1534. /* Go with read. */
  1535. qla24xx_read_flash_data(ha, (uint32_t *)buf, offset >> 2, length >> 2);
  1536. /* Resume HBA. */
  1537. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1538. scsi_unblock_requests(ha->host);
  1539. return buf;
  1540. }
  1541. int
  1542. qla24xx_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1543. uint32_t offset, uint32_t length)
  1544. {
  1545. int rval;
  1546. /* Suspend HBA. */
  1547. scsi_block_requests(ha->host);
  1548. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1549. /* Go with write. */
  1550. rval = qla24xx_write_flash_data(ha, (uint32_t *)buf, offset >> 2,
  1551. length >> 2);
  1552. /* Resume HBA -- RISC reset needed. */
  1553. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1554. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1555. qla2xxx_wake_dpc(ha);
  1556. qla2x00_wait_for_hba_online(ha);
  1557. scsi_unblock_requests(ha->host);
  1558. return rval;
  1559. }
  1560. uint8_t *
  1561. qla25xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1562. uint32_t offset, uint32_t length)
  1563. {
  1564. int rval;
  1565. dma_addr_t optrom_dma;
  1566. void *optrom;
  1567. uint8_t *pbuf;
  1568. uint32_t faddr, left, burst;
  1569. if (offset & 0xfff)
  1570. goto slow_read;
  1571. if (length < OPTROM_BURST_SIZE)
  1572. goto slow_read;
  1573. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1574. &optrom_dma, GFP_KERNEL);
  1575. if (!optrom) {
  1576. qla_printk(KERN_DEBUG, ha,
  1577. "Unable to allocate memory for optrom burst read "
  1578. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  1579. goto slow_read;
  1580. }
  1581. pbuf = buf;
  1582. faddr = offset >> 2;
  1583. left = length >> 2;
  1584. burst = OPTROM_BURST_DWORDS;
  1585. while (left != 0) {
  1586. if (burst > left)
  1587. burst = left;
  1588. rval = qla2x00_dump_ram(ha, optrom_dma,
  1589. flash_data_to_access_addr(faddr), burst);
  1590. if (rval) {
  1591. qla_printk(KERN_WARNING, ha,
  1592. "Unable to burst-read optrom segment "
  1593. "(%x/%x/%llx).\n", rval,
  1594. flash_data_to_access_addr(faddr), optrom_dma);
  1595. qla_printk(KERN_WARNING, ha,
  1596. "Reverting to slow-read.\n");
  1597. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1598. optrom, optrom_dma);
  1599. goto slow_read;
  1600. }
  1601. memcpy(pbuf, optrom, burst * 4);
  1602. left -= burst;
  1603. faddr += burst;
  1604. pbuf += burst * 4;
  1605. }
  1606. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
  1607. optrom_dma);
  1608. return buf;
  1609. slow_read:
  1610. return qla24xx_read_optrom_data(ha, buf, offset, length);
  1611. }
  1612. /**
  1613. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  1614. * @ha: HA context
  1615. * @pcids: Pointer to the FCODE PCI data structure
  1616. *
  1617. * The process of retrieving the FCODE version information is at best
  1618. * described as interesting.
  1619. *
  1620. * Within the first 100h bytes of the image an ASCII string is present
  1621. * which contains several pieces of information including the FCODE
  1622. * version. Unfortunately it seems the only reliable way to retrieve
  1623. * the version is by scanning for another sentinel within the string,
  1624. * the FCODE build date:
  1625. *
  1626. * ... 2.00.02 10/17/02 ...
  1627. *
  1628. * Returns QLA_SUCCESS on successful retrieval of version.
  1629. */
  1630. static void
  1631. qla2x00_get_fcode_version(scsi_qla_host_t *ha, uint32_t pcids)
  1632. {
  1633. int ret = QLA_FUNCTION_FAILED;
  1634. uint32_t istart, iend, iter, vend;
  1635. uint8_t do_next, rbyte, *vbyte;
  1636. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1637. /* Skip the PCI data structure. */
  1638. istart = pcids +
  1639. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  1640. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  1641. iend = istart + 0x100;
  1642. do {
  1643. /* Scan for the sentinel date string...eeewww. */
  1644. do_next = 0;
  1645. iter = istart;
  1646. while ((iter < iend) && !do_next) {
  1647. iter++;
  1648. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  1649. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  1650. '/')
  1651. do_next++;
  1652. else if (qla2x00_read_flash_byte(ha,
  1653. iter + 3) == '/')
  1654. do_next++;
  1655. }
  1656. }
  1657. if (!do_next)
  1658. break;
  1659. /* Backtrack to previous ' ' (space). */
  1660. do_next = 0;
  1661. while ((iter > istart) && !do_next) {
  1662. iter--;
  1663. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  1664. do_next++;
  1665. }
  1666. if (!do_next)
  1667. break;
  1668. /*
  1669. * Mark end of version tag, and find previous ' ' (space) or
  1670. * string length (recent FCODE images -- major hack ahead!!!).
  1671. */
  1672. vend = iter - 1;
  1673. do_next = 0;
  1674. while ((iter > istart) && !do_next) {
  1675. iter--;
  1676. rbyte = qla2x00_read_flash_byte(ha, iter);
  1677. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  1678. do_next++;
  1679. }
  1680. if (!do_next)
  1681. break;
  1682. /* Mark beginning of version tag, and copy data. */
  1683. iter++;
  1684. if ((vend - iter) &&
  1685. ((vend - iter) < sizeof(ha->fcode_revision))) {
  1686. vbyte = ha->fcode_revision;
  1687. while (iter <= vend) {
  1688. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  1689. iter++;
  1690. }
  1691. ret = QLA_SUCCESS;
  1692. }
  1693. } while (0);
  1694. if (ret != QLA_SUCCESS)
  1695. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1696. }
  1697. int
  1698. qla2x00_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1699. {
  1700. int ret = QLA_SUCCESS;
  1701. uint8_t code_type, last_image;
  1702. uint32_t pcihdr, pcids;
  1703. uint8_t *dbyte;
  1704. uint16_t *dcode;
  1705. if (!ha->pio_address || !mbuf)
  1706. return QLA_FUNCTION_FAILED;
  1707. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1708. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1709. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1710. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1711. qla2x00_flash_enable(ha);
  1712. /* Begin with first PCI expansion ROM header. */
  1713. pcihdr = 0;
  1714. last_image = 1;
  1715. do {
  1716. /* Verify PCI expansion ROM header. */
  1717. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  1718. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  1719. /* No signature */
  1720. DEBUG2(printk("scsi(%ld): No matching ROM "
  1721. "signature.\n", ha->host_no));
  1722. ret = QLA_FUNCTION_FAILED;
  1723. break;
  1724. }
  1725. /* Locate PCI data structure. */
  1726. pcids = pcihdr +
  1727. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  1728. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  1729. /* Validate signature of PCI data structure. */
  1730. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  1731. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  1732. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  1733. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  1734. /* Incorrect header. */
  1735. DEBUG2(printk("%s(): PCI data struct not found "
  1736. "pcir_adr=%x.\n", __func__, pcids));
  1737. ret = QLA_FUNCTION_FAILED;
  1738. break;
  1739. }
  1740. /* Read version */
  1741. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  1742. switch (code_type) {
  1743. case ROM_CODE_TYPE_BIOS:
  1744. /* Intel x86, PC-AT compatible. */
  1745. ha->bios_revision[0] =
  1746. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1747. ha->bios_revision[1] =
  1748. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1749. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1750. ha->bios_revision[1], ha->bios_revision[0]));
  1751. break;
  1752. case ROM_CODE_TYPE_FCODE:
  1753. /* Open Firmware standard for PCI (FCode). */
  1754. /* Eeeewww... */
  1755. qla2x00_get_fcode_version(ha, pcids);
  1756. break;
  1757. case ROM_CODE_TYPE_EFI:
  1758. /* Extensible Firmware Interface (EFI). */
  1759. ha->efi_revision[0] =
  1760. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1761. ha->efi_revision[1] =
  1762. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1763. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1764. ha->efi_revision[1], ha->efi_revision[0]));
  1765. break;
  1766. default:
  1767. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1768. "pcids %x.\n", __func__, code_type, pcids));
  1769. break;
  1770. }
  1771. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  1772. /* Locate next PCI expansion ROM. */
  1773. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  1774. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  1775. } while (!last_image);
  1776. if (IS_QLA2322(ha)) {
  1777. /* Read firmware image information. */
  1778. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1779. dbyte = mbuf;
  1780. memset(dbyte, 0, 8);
  1781. dcode = (uint16_t *)dbyte;
  1782. qla2x00_read_flash_data(ha, dbyte, FA_RISC_CODE_ADDR * 4 + 10,
  1783. 8);
  1784. DEBUG3(printk("%s(%ld): dumping fw ver from flash:\n",
  1785. __func__, ha->host_no));
  1786. DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
  1787. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  1788. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  1789. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1790. dcode[3] == 0)) {
  1791. DEBUG2(printk("%s(): Unrecognized fw revision at "
  1792. "%x.\n", __func__, FA_RISC_CODE_ADDR * 4));
  1793. } else {
  1794. /* values are in big endian */
  1795. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  1796. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  1797. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  1798. }
  1799. }
  1800. qla2x00_flash_disable(ha);
  1801. return ret;
  1802. }
  1803. int
  1804. qla24xx_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1805. {
  1806. int ret = QLA_SUCCESS;
  1807. uint32_t pcihdr, pcids;
  1808. uint32_t *dcode;
  1809. uint8_t *bcode;
  1810. uint8_t code_type, last_image;
  1811. int i;
  1812. if (!mbuf)
  1813. return QLA_FUNCTION_FAILED;
  1814. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1815. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1816. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1817. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1818. dcode = mbuf;
  1819. /* Begin with first PCI expansion ROM header. */
  1820. pcihdr = 0;
  1821. last_image = 1;
  1822. do {
  1823. /* Verify PCI expansion ROM header. */
  1824. qla24xx_read_flash_data(ha, dcode, pcihdr >> 2, 0x20);
  1825. bcode = mbuf + (pcihdr % 4);
  1826. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
  1827. /* No signature */
  1828. DEBUG2(printk("scsi(%ld): No matching ROM "
  1829. "signature.\n", ha->host_no));
  1830. ret = QLA_FUNCTION_FAILED;
  1831. break;
  1832. }
  1833. /* Locate PCI data structure. */
  1834. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  1835. qla24xx_read_flash_data(ha, dcode, pcids >> 2, 0x20);
  1836. bcode = mbuf + (pcihdr % 4);
  1837. /* Validate signature of PCI data structure. */
  1838. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  1839. bcode[0x2] != 'I' || bcode[0x3] != 'R') {
  1840. /* Incorrect header. */
  1841. DEBUG2(printk("%s(): PCI data struct not found "
  1842. "pcir_adr=%x.\n", __func__, pcids));
  1843. ret = QLA_FUNCTION_FAILED;
  1844. break;
  1845. }
  1846. /* Read version */
  1847. code_type = bcode[0x14];
  1848. switch (code_type) {
  1849. case ROM_CODE_TYPE_BIOS:
  1850. /* Intel x86, PC-AT compatible. */
  1851. ha->bios_revision[0] = bcode[0x12];
  1852. ha->bios_revision[1] = bcode[0x13];
  1853. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1854. ha->bios_revision[1], ha->bios_revision[0]));
  1855. break;
  1856. case ROM_CODE_TYPE_FCODE:
  1857. /* Open Firmware standard for PCI (FCode). */
  1858. ha->fcode_revision[0] = bcode[0x12];
  1859. ha->fcode_revision[1] = bcode[0x13];
  1860. DEBUG3(printk("%s(): read FCODE %d.%d.\n", __func__,
  1861. ha->fcode_revision[1], ha->fcode_revision[0]));
  1862. break;
  1863. case ROM_CODE_TYPE_EFI:
  1864. /* Extensible Firmware Interface (EFI). */
  1865. ha->efi_revision[0] = bcode[0x12];
  1866. ha->efi_revision[1] = bcode[0x13];
  1867. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1868. ha->efi_revision[1], ha->efi_revision[0]));
  1869. break;
  1870. default:
  1871. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1872. "pcids %x.\n", __func__, code_type, pcids));
  1873. break;
  1874. }
  1875. last_image = bcode[0x15] & BIT_7;
  1876. /* Locate next PCI expansion ROM. */
  1877. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  1878. } while (!last_image);
  1879. /* Read firmware image information. */
  1880. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1881. dcode = mbuf;
  1882. qla24xx_read_flash_data(ha, dcode, FA_RISC_CODE_ADDR + 4, 4);
  1883. for (i = 0; i < 4; i++)
  1884. dcode[i] = be32_to_cpu(dcode[i]);
  1885. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  1886. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  1887. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1888. dcode[3] == 0)) {
  1889. DEBUG2(printk("%s(): Unrecognized fw version at %x.\n",
  1890. __func__, FA_RISC_CODE_ADDR));
  1891. } else {
  1892. ha->fw_revision[0] = dcode[0];
  1893. ha->fw_revision[1] = dcode[1];
  1894. ha->fw_revision[2] = dcode[2];
  1895. ha->fw_revision[3] = dcode[3];
  1896. }
  1897. return ret;
  1898. }