qla_sup.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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;
  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. do {
  112. NVRAM_DELAY();
  113. word = RD_REG_WORD(&reg->nvram);
  114. } while ((word & NVR_DATA_IN) == 0);
  115. qla2x00_nv_deselect(ha);
  116. /* Disable writes */
  117. qla2x00_nv_write(ha, NVR_DATA_OUT);
  118. for (count = 0; count < 10; count++)
  119. qla2x00_nv_write(ha, 0);
  120. qla2x00_nv_deselect(ha);
  121. }
  122. static int
  123. qla2x00_write_nvram_word_tmo(scsi_qla_host_t *ha, uint32_t addr, uint16_t data,
  124. uint32_t tmo)
  125. {
  126. int ret, count;
  127. uint16_t word;
  128. uint32_t nv_cmd;
  129. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  130. ret = QLA_SUCCESS;
  131. qla2x00_nv_write(ha, NVR_DATA_OUT);
  132. qla2x00_nv_write(ha, 0);
  133. qla2x00_nv_write(ha, 0);
  134. for (word = 0; word < 8; word++)
  135. qla2x00_nv_write(ha, NVR_DATA_OUT);
  136. qla2x00_nv_deselect(ha);
  137. /* Write data */
  138. nv_cmd = (addr << 16) | NV_WRITE_OP;
  139. nv_cmd |= data;
  140. nv_cmd <<= 5;
  141. for (count = 0; count < 27; count++) {
  142. if (nv_cmd & BIT_31)
  143. qla2x00_nv_write(ha, NVR_DATA_OUT);
  144. else
  145. qla2x00_nv_write(ha, 0);
  146. nv_cmd <<= 1;
  147. }
  148. qla2x00_nv_deselect(ha);
  149. /* Wait for NVRAM to become ready */
  150. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  151. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  152. do {
  153. NVRAM_DELAY();
  154. word = RD_REG_WORD(&reg->nvram);
  155. if (!--tmo) {
  156. ret = QLA_FUNCTION_FAILED;
  157. break;
  158. }
  159. } while ((word & NVR_DATA_IN) == 0);
  160. qla2x00_nv_deselect(ha);
  161. /* Disable writes */
  162. qla2x00_nv_write(ha, NVR_DATA_OUT);
  163. for (count = 0; count < 10; count++)
  164. qla2x00_nv_write(ha, 0);
  165. qla2x00_nv_deselect(ha);
  166. return ret;
  167. }
  168. /**
  169. * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
  170. * NVRAM.
  171. * @ha: HA context
  172. * @nv_cmd: NVRAM command
  173. *
  174. * Bit definitions for NVRAM command:
  175. *
  176. * Bit 26 = start bit
  177. * Bit 25, 24 = opcode
  178. * Bit 23-16 = address
  179. * Bit 15-0 = write data
  180. *
  181. * Returns the word read from nvram @addr.
  182. */
  183. static uint16_t
  184. qla2x00_nvram_request(scsi_qla_host_t *ha, uint32_t nv_cmd)
  185. {
  186. uint8_t cnt;
  187. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  188. uint16_t data = 0;
  189. uint16_t reg_data;
  190. /* Send command to NVRAM. */
  191. nv_cmd <<= 5;
  192. for (cnt = 0; cnt < 11; cnt++) {
  193. if (nv_cmd & BIT_31)
  194. qla2x00_nv_write(ha, NVR_DATA_OUT);
  195. else
  196. qla2x00_nv_write(ha, 0);
  197. nv_cmd <<= 1;
  198. }
  199. /* Read data from NVRAM. */
  200. for (cnt = 0; cnt < 16; cnt++) {
  201. WRT_REG_WORD(&reg->nvram, NVR_SELECT | NVR_CLOCK);
  202. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  203. NVRAM_DELAY();
  204. data <<= 1;
  205. reg_data = RD_REG_WORD(&reg->nvram);
  206. if (reg_data & NVR_DATA_IN)
  207. data |= BIT_0;
  208. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  209. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  210. NVRAM_DELAY();
  211. }
  212. /* Deselect chip. */
  213. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  214. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  215. NVRAM_DELAY();
  216. return (data);
  217. }
  218. /**
  219. * qla2x00_nv_write() - Clean NVRAM operations.
  220. * @ha: HA context
  221. */
  222. static void
  223. qla2x00_nv_deselect(scsi_qla_host_t *ha)
  224. {
  225. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  226. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  227. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  228. NVRAM_DELAY();
  229. }
  230. /**
  231. * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
  232. * @ha: HA context
  233. * @data: Serial interface selector
  234. */
  235. static void
  236. qla2x00_nv_write(scsi_qla_host_t *ha, uint16_t data)
  237. {
  238. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  239. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  240. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  241. NVRAM_DELAY();
  242. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT| NVR_CLOCK |
  243. NVR_WRT_ENABLE);
  244. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  245. NVRAM_DELAY();
  246. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  247. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  248. NVRAM_DELAY();
  249. }
  250. /**
  251. * qla2x00_clear_nvram_protection() -
  252. * @ha: HA context
  253. */
  254. static int
  255. qla2x00_clear_nvram_protection(scsi_qla_host_t *ha)
  256. {
  257. int ret, stat;
  258. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  259. uint32_t word;
  260. uint16_t wprot, wprot_old;
  261. /* Clear NVRAM write protection. */
  262. ret = QLA_FUNCTION_FAILED;
  263. wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, 0));
  264. stat = qla2x00_write_nvram_word_tmo(ha, 0,
  265. __constant_cpu_to_le16(0x1234), 100000);
  266. wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, 0));
  267. if (stat != QLA_SUCCESS || wprot != __constant_cpu_to_le16(0x1234)) {
  268. /* Write enable. */
  269. qla2x00_nv_write(ha, NVR_DATA_OUT);
  270. qla2x00_nv_write(ha, 0);
  271. qla2x00_nv_write(ha, 0);
  272. for (word = 0; word < 8; word++)
  273. qla2x00_nv_write(ha, NVR_DATA_OUT);
  274. qla2x00_nv_deselect(ha);
  275. /* Enable protection register. */
  276. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  277. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  278. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  279. for (word = 0; word < 8; word++)
  280. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  281. qla2x00_nv_deselect(ha);
  282. /* Clear protection register (ffff is cleared). */
  283. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  284. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  285. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  286. for (word = 0; word < 8; word++)
  287. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  288. qla2x00_nv_deselect(ha);
  289. /* Wait for NVRAM to become ready. */
  290. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  291. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  292. do {
  293. NVRAM_DELAY();
  294. word = RD_REG_WORD(&reg->nvram);
  295. } while ((word & NVR_DATA_IN) == 0);
  296. ret = QLA_SUCCESS;
  297. } else
  298. qla2x00_write_nvram_word(ha, 0, wprot_old);
  299. return ret;
  300. }
  301. static void
  302. qla2x00_set_nvram_protection(scsi_qla_host_t *ha, int stat)
  303. {
  304. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  305. uint32_t word;
  306. if (stat != QLA_SUCCESS)
  307. return;
  308. /* Set NVRAM write protection. */
  309. /* Write enable. */
  310. qla2x00_nv_write(ha, NVR_DATA_OUT);
  311. qla2x00_nv_write(ha, 0);
  312. qla2x00_nv_write(ha, 0);
  313. for (word = 0; word < 8; word++)
  314. qla2x00_nv_write(ha, NVR_DATA_OUT);
  315. qla2x00_nv_deselect(ha);
  316. /* Enable protection register. */
  317. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  318. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  319. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  320. for (word = 0; word < 8; word++)
  321. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  322. qla2x00_nv_deselect(ha);
  323. /* Enable protection register. */
  324. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  325. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  326. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  327. for (word = 0; word < 8; word++)
  328. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  329. qla2x00_nv_deselect(ha);
  330. /* Wait for NVRAM to become ready. */
  331. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  332. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  333. do {
  334. NVRAM_DELAY();
  335. word = RD_REG_WORD(&reg->nvram);
  336. } while ((word & NVR_DATA_IN) == 0);
  337. }
  338. /*****************************************************************************/
  339. /* Flash Manipulation Routines */
  340. /*****************************************************************************/
  341. static inline uint32_t
  342. flash_conf_to_access_addr(uint32_t faddr)
  343. {
  344. return FARX_ACCESS_FLASH_CONF | faddr;
  345. }
  346. static inline uint32_t
  347. flash_data_to_access_addr(uint32_t faddr)
  348. {
  349. return FARX_ACCESS_FLASH_DATA | faddr;
  350. }
  351. static inline uint32_t
  352. nvram_conf_to_access_addr(uint32_t naddr)
  353. {
  354. return FARX_ACCESS_NVRAM_CONF | naddr;
  355. }
  356. static inline uint32_t
  357. nvram_data_to_access_addr(uint32_t naddr)
  358. {
  359. return FARX_ACCESS_NVRAM_DATA | naddr;
  360. }
  361. uint32_t
  362. qla24xx_read_flash_dword(scsi_qla_host_t *ha, uint32_t addr)
  363. {
  364. int rval;
  365. uint32_t cnt, data;
  366. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  367. WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  368. /* Wait for READ cycle to complete. */
  369. rval = QLA_SUCCESS;
  370. for (cnt = 3000;
  371. (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
  372. rval == QLA_SUCCESS; cnt--) {
  373. if (cnt)
  374. udelay(10);
  375. else
  376. rval = QLA_FUNCTION_TIMEOUT;
  377. }
  378. /* TODO: What happens if we time out? */
  379. data = 0xDEADDEAD;
  380. if (rval == QLA_SUCCESS)
  381. data = RD_REG_DWORD(&reg->flash_data);
  382. return data;
  383. }
  384. uint32_t *
  385. qla24xx_read_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  386. uint32_t dwords)
  387. {
  388. uint32_t i;
  389. /* Dword reads to flash. */
  390. for (i = 0; i < dwords; i++, faddr++)
  391. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  392. flash_data_to_access_addr(faddr)));
  393. return dwptr;
  394. }
  395. int
  396. qla24xx_write_flash_dword(scsi_qla_host_t *ha, uint32_t addr, uint32_t data)
  397. {
  398. int rval;
  399. uint32_t cnt;
  400. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  401. WRT_REG_DWORD(&reg->flash_data, data);
  402. RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
  403. WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
  404. /* Wait for Write cycle to complete. */
  405. rval = QLA_SUCCESS;
  406. for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
  407. rval == QLA_SUCCESS; cnt--) {
  408. if (cnt)
  409. udelay(10);
  410. else
  411. rval = QLA_FUNCTION_TIMEOUT;
  412. }
  413. return rval;
  414. }
  415. void
  416. qla24xx_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  417. uint8_t *flash_id)
  418. {
  419. uint32_t ids;
  420. ids = qla24xx_read_flash_dword(ha, flash_data_to_access_addr(0xd03ab));
  421. *man_id = LSB(ids);
  422. *flash_id = MSB(ids);
  423. }
  424. int
  425. qla24xx_write_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  426. uint32_t dwords)
  427. {
  428. int ret;
  429. uint32_t liter;
  430. uint32_t sec_mask, rest_addr, conf_addr;
  431. uint32_t fdata;
  432. uint8_t man_id, flash_id;
  433. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  434. ret = QLA_SUCCESS;
  435. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  436. DEBUG9(printk("%s(%ld): Flash man_id=%d flash_id=%d\n", __func__,
  437. ha->host_no, man_id, flash_id));
  438. conf_addr = flash_conf_to_access_addr(0x03d8);
  439. switch (man_id) {
  440. case 0xbf: /* STT flash. */
  441. rest_addr = 0x1fff;
  442. sec_mask = 0x3e000;
  443. if (flash_id == 0x80)
  444. conf_addr = flash_conf_to_access_addr(0x0352);
  445. break;
  446. case 0x13: /* ST M25P80. */
  447. rest_addr = 0x3fff;
  448. sec_mask = 0x3c000;
  449. break;
  450. default:
  451. /* Default to 64 kb sector size. */
  452. rest_addr = 0x3fff;
  453. sec_mask = 0x3c000;
  454. break;
  455. }
  456. /* Enable flash write. */
  457. WRT_REG_DWORD(&reg->ctrl_status,
  458. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  459. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  460. /* Disable flash write-protection. */
  461. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  462. do { /* Loop once to provide quick error exit. */
  463. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  464. /* Are we at the beginning of a sector? */
  465. if ((faddr & rest_addr) == 0) {
  466. fdata = (faddr & sec_mask) << 2;
  467. ret = qla24xx_write_flash_dword(ha, conf_addr,
  468. (fdata & 0xff00) |((fdata << 16) &
  469. 0xff0000) | ((fdata >> 16) & 0xff));
  470. if (ret != QLA_SUCCESS) {
  471. DEBUG9(printk("%s(%ld) Unable to flash "
  472. "sector: address=%x.\n", __func__,
  473. ha->host_no, faddr));
  474. break;
  475. }
  476. }
  477. ret = qla24xx_write_flash_dword(ha,
  478. flash_data_to_access_addr(faddr),
  479. cpu_to_le32(*dwptr));
  480. if (ret != QLA_SUCCESS) {
  481. DEBUG9(printk("%s(%ld) Unable to program flash "
  482. "address=%x data=%x.\n", __func__,
  483. ha->host_no, faddr, *dwptr));
  484. break;
  485. }
  486. }
  487. } while (0);
  488. /* Enable flash write-protection. */
  489. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0x9c);
  490. /* Disable flash write. */
  491. WRT_REG_DWORD(&reg->ctrl_status,
  492. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  493. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  494. return ret;
  495. }
  496. uint8_t *
  497. qla2x00_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  498. uint32_t bytes)
  499. {
  500. uint32_t i;
  501. uint16_t *wptr;
  502. /* Word reads to NVRAM via registers. */
  503. wptr = (uint16_t *)buf;
  504. qla2x00_lock_nvram_access(ha);
  505. for (i = 0; i < bytes >> 1; i++, naddr++)
  506. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  507. naddr));
  508. qla2x00_unlock_nvram_access(ha);
  509. return buf;
  510. }
  511. uint8_t *
  512. qla24xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  513. uint32_t bytes)
  514. {
  515. uint32_t i;
  516. uint32_t *dwptr;
  517. /* Dword reads to flash. */
  518. dwptr = (uint32_t *)buf;
  519. for (i = 0; i < bytes >> 2; i++, naddr++)
  520. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  521. nvram_data_to_access_addr(naddr)));
  522. return buf;
  523. }
  524. int
  525. qla2x00_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  526. uint32_t bytes)
  527. {
  528. int ret, stat;
  529. uint32_t i;
  530. uint16_t *wptr;
  531. ret = QLA_SUCCESS;
  532. qla2x00_lock_nvram_access(ha);
  533. /* Disable NVRAM write-protection. */
  534. stat = qla2x00_clear_nvram_protection(ha);
  535. wptr = (uint16_t *)buf;
  536. for (i = 0; i < bytes >> 1; i++, naddr++) {
  537. qla2x00_write_nvram_word(ha, naddr,
  538. cpu_to_le16(*wptr));
  539. wptr++;
  540. }
  541. /* Enable NVRAM write-protection. */
  542. qla2x00_set_nvram_protection(ha, stat);
  543. qla2x00_unlock_nvram_access(ha);
  544. return ret;
  545. }
  546. int
  547. qla24xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  548. uint32_t bytes)
  549. {
  550. int ret;
  551. uint32_t i;
  552. uint32_t *dwptr;
  553. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  554. ret = QLA_SUCCESS;
  555. /* Enable flash write. */
  556. WRT_REG_DWORD(&reg->ctrl_status,
  557. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  558. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  559. /* Disable NVRAM write-protection. */
  560. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  561. 0);
  562. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  563. 0);
  564. /* Dword writes to flash. */
  565. dwptr = (uint32_t *)buf;
  566. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  567. ret = qla24xx_write_flash_dword(ha,
  568. nvram_data_to_access_addr(naddr),
  569. cpu_to_le32(*dwptr));
  570. if (ret != QLA_SUCCESS) {
  571. DEBUG9(printk("%s(%ld) Unable to program "
  572. "nvram address=%x data=%x.\n", __func__,
  573. ha->host_no, naddr, *dwptr));
  574. break;
  575. }
  576. }
  577. /* Enable NVRAM write-protection. */
  578. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  579. 0x8c);
  580. /* Disable flash write. */
  581. WRT_REG_DWORD(&reg->ctrl_status,
  582. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  583. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  584. return ret;
  585. }
  586. static inline void
  587. qla2x00_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  588. {
  589. if (IS_QLA2322(ha)) {
  590. /* Flip all colors. */
  591. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  592. /* Turn off. */
  593. ha->beacon_color_state = 0;
  594. *pflags = GPIO_LED_ALL_OFF;
  595. } else {
  596. /* Turn on. */
  597. ha->beacon_color_state = QLA_LED_ALL_ON;
  598. *pflags = GPIO_LED_RGA_ON;
  599. }
  600. } else {
  601. /* Flip green led only. */
  602. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  603. /* Turn off. */
  604. ha->beacon_color_state = 0;
  605. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  606. } else {
  607. /* Turn on. */
  608. ha->beacon_color_state = QLA_LED_GRN_ON;
  609. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  610. }
  611. }
  612. }
  613. void
  614. qla2x00_beacon_blink(struct scsi_qla_host *ha)
  615. {
  616. uint16_t gpio_enable;
  617. uint16_t gpio_data;
  618. uint16_t led_color = 0;
  619. unsigned long flags;
  620. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  621. if (ha->pio_address)
  622. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  623. spin_lock_irqsave(&ha->hardware_lock, flags);
  624. /* Save the Original GPIOE. */
  625. if (ha->pio_address) {
  626. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  627. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  628. } else {
  629. gpio_enable = RD_REG_WORD(&reg->gpioe);
  630. gpio_data = RD_REG_WORD(&reg->gpiod);
  631. }
  632. /* Set the modified gpio_enable values */
  633. gpio_enable |= GPIO_LED_MASK;
  634. if (ha->pio_address) {
  635. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  636. } else {
  637. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  638. RD_REG_WORD(&reg->gpioe);
  639. }
  640. qla2x00_flip_colors(ha, &led_color);
  641. /* Clear out any previously set LED color. */
  642. gpio_data &= ~GPIO_LED_MASK;
  643. /* Set the new input LED color to GPIOD. */
  644. gpio_data |= led_color;
  645. /* Set the modified gpio_data values */
  646. if (ha->pio_address) {
  647. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  648. } else {
  649. WRT_REG_WORD(&reg->gpiod, gpio_data);
  650. RD_REG_WORD(&reg->gpiod);
  651. }
  652. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  653. }
  654. int
  655. qla2x00_beacon_on(struct scsi_qla_host *ha)
  656. {
  657. uint16_t gpio_enable;
  658. uint16_t gpio_data;
  659. unsigned long flags;
  660. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  661. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  662. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  663. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  664. qla_printk(KERN_WARNING, ha,
  665. "Unable to update fw options (beacon on).\n");
  666. return QLA_FUNCTION_FAILED;
  667. }
  668. if (ha->pio_address)
  669. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  670. /* Turn off LEDs. */
  671. spin_lock_irqsave(&ha->hardware_lock, flags);
  672. if (ha->pio_address) {
  673. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  674. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  675. } else {
  676. gpio_enable = RD_REG_WORD(&reg->gpioe);
  677. gpio_data = RD_REG_WORD(&reg->gpiod);
  678. }
  679. gpio_enable |= GPIO_LED_MASK;
  680. /* Set the modified gpio_enable values. */
  681. if (ha->pio_address) {
  682. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  683. } else {
  684. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  685. RD_REG_WORD(&reg->gpioe);
  686. }
  687. /* Clear out previously set LED colour. */
  688. gpio_data &= ~GPIO_LED_MASK;
  689. if (ha->pio_address) {
  690. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  691. } else {
  692. WRT_REG_WORD(&reg->gpiod, gpio_data);
  693. RD_REG_WORD(&reg->gpiod);
  694. }
  695. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  696. /*
  697. * Let the per HBA timer kick off the blinking process based on
  698. * the following flags. No need to do anything else now.
  699. */
  700. ha->beacon_blink_led = 1;
  701. ha->beacon_color_state = 0;
  702. return QLA_SUCCESS;
  703. }
  704. int
  705. qla2x00_beacon_off(struct scsi_qla_host *ha)
  706. {
  707. int rval = QLA_SUCCESS;
  708. ha->beacon_blink_led = 0;
  709. /* Set the on flag so when it gets flipped it will be off. */
  710. if (IS_QLA2322(ha))
  711. ha->beacon_color_state = QLA_LED_ALL_ON;
  712. else
  713. ha->beacon_color_state = QLA_LED_GRN_ON;
  714. ha->isp_ops.beacon_blink(ha); /* This turns green LED off */
  715. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  716. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  717. rval = qla2x00_set_fw_options(ha, ha->fw_options);
  718. if (rval != QLA_SUCCESS)
  719. qla_printk(KERN_WARNING, ha,
  720. "Unable to update fw options (beacon off).\n");
  721. return rval;
  722. }
  723. static inline void
  724. qla24xx_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  725. {
  726. /* Flip all colors. */
  727. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  728. /* Turn off. */
  729. ha->beacon_color_state = 0;
  730. *pflags = 0;
  731. } else {
  732. /* Turn on. */
  733. ha->beacon_color_state = QLA_LED_ALL_ON;
  734. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  735. }
  736. }
  737. void
  738. qla24xx_beacon_blink(struct scsi_qla_host *ha)
  739. {
  740. uint16_t led_color = 0;
  741. uint32_t gpio_data;
  742. unsigned long flags;
  743. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  744. /* Save the Original GPIOD. */
  745. spin_lock_irqsave(&ha->hardware_lock, flags);
  746. gpio_data = RD_REG_DWORD(&reg->gpiod);
  747. /* Enable the gpio_data reg for update. */
  748. gpio_data |= GPDX_LED_UPDATE_MASK;
  749. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  750. gpio_data = RD_REG_DWORD(&reg->gpiod);
  751. /* Set the color bits. */
  752. qla24xx_flip_colors(ha, &led_color);
  753. /* Clear out any previously set LED color. */
  754. gpio_data &= ~GPDX_LED_COLOR_MASK;
  755. /* Set the new input LED color to GPIOD. */
  756. gpio_data |= led_color;
  757. /* Set the modified gpio_data values. */
  758. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  759. gpio_data = RD_REG_DWORD(&reg->gpiod);
  760. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  761. }
  762. int
  763. qla24xx_beacon_on(struct scsi_qla_host *ha)
  764. {
  765. uint32_t gpio_data;
  766. unsigned long flags;
  767. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  768. if (ha->beacon_blink_led == 0) {
  769. /* Enable firmware for update */
  770. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  771. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS)
  772. return QLA_FUNCTION_FAILED;
  773. if (qla2x00_get_fw_options(ha, ha->fw_options) !=
  774. QLA_SUCCESS) {
  775. qla_printk(KERN_WARNING, ha,
  776. "Unable to update fw options (beacon on).\n");
  777. return QLA_FUNCTION_FAILED;
  778. }
  779. spin_lock_irqsave(&ha->hardware_lock, flags);
  780. gpio_data = RD_REG_DWORD(&reg->gpiod);
  781. /* Enable the gpio_data reg for update. */
  782. gpio_data |= GPDX_LED_UPDATE_MASK;
  783. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  784. RD_REG_DWORD(&reg->gpiod);
  785. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  786. }
  787. /* So all colors blink together. */
  788. ha->beacon_color_state = 0;
  789. /* Let the per HBA timer kick off the blinking process. */
  790. ha->beacon_blink_led = 1;
  791. return QLA_SUCCESS;
  792. }
  793. int
  794. qla24xx_beacon_off(struct scsi_qla_host *ha)
  795. {
  796. uint32_t gpio_data;
  797. unsigned long flags;
  798. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  799. ha->beacon_blink_led = 0;
  800. ha->beacon_color_state = QLA_LED_ALL_ON;
  801. ha->isp_ops.beacon_blink(ha); /* Will flip to all off. */
  802. /* Give control back to firmware. */
  803. spin_lock_irqsave(&ha->hardware_lock, flags);
  804. gpio_data = RD_REG_DWORD(&reg->gpiod);
  805. /* Disable the gpio_data reg for update. */
  806. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  807. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  808. RD_REG_DWORD(&reg->gpiod);
  809. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  810. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  811. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  812. qla_printk(KERN_WARNING, ha,
  813. "Unable to update fw options (beacon off).\n");
  814. return QLA_FUNCTION_FAILED;
  815. }
  816. if (qla2x00_get_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  817. qla_printk(KERN_WARNING, ha,
  818. "Unable to get fw options (beacon off).\n");
  819. return QLA_FUNCTION_FAILED;
  820. }
  821. return QLA_SUCCESS;
  822. }
  823. /*
  824. * Flash support routines
  825. */
  826. /**
  827. * qla2x00_flash_enable() - Setup flash for reading and writing.
  828. * @ha: HA context
  829. */
  830. static void
  831. qla2x00_flash_enable(scsi_qla_host_t *ha)
  832. {
  833. uint16_t data;
  834. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  835. data = RD_REG_WORD(&reg->ctrl_status);
  836. data |= CSR_FLASH_ENABLE;
  837. WRT_REG_WORD(&reg->ctrl_status, data);
  838. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  839. }
  840. /**
  841. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  842. * @ha: HA context
  843. */
  844. static void
  845. qla2x00_flash_disable(scsi_qla_host_t *ha)
  846. {
  847. uint16_t data;
  848. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  849. data = RD_REG_WORD(&reg->ctrl_status);
  850. data &= ~(CSR_FLASH_ENABLE);
  851. WRT_REG_WORD(&reg->ctrl_status, data);
  852. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  853. }
  854. /**
  855. * qla2x00_read_flash_byte() - Reads a byte from flash
  856. * @ha: HA context
  857. * @addr: Address in flash to read
  858. *
  859. * A word is read from the chip, but, only the lower byte is valid.
  860. *
  861. * Returns the byte read from flash @addr.
  862. */
  863. static uint8_t
  864. qla2x00_read_flash_byte(scsi_qla_host_t *ha, uint32_t addr)
  865. {
  866. uint16_t data;
  867. uint16_t bank_select;
  868. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  869. bank_select = RD_REG_WORD(&reg->ctrl_status);
  870. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  871. /* Specify 64K address range: */
  872. /* clear out Module Select and Flash Address bits [19:16]. */
  873. bank_select &= ~0xf8;
  874. bank_select |= addr >> 12 & 0xf0;
  875. bank_select |= CSR_FLASH_64K_BANK;
  876. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  877. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  878. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  879. data = RD_REG_WORD(&reg->flash_data);
  880. return (uint8_t)data;
  881. }
  882. /* Setup bit 16 of flash address. */
  883. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  884. bank_select |= CSR_FLASH_64K_BANK;
  885. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  886. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  887. } else if (((addr & BIT_16) == 0) &&
  888. (bank_select & CSR_FLASH_64K_BANK)) {
  889. bank_select &= ~(CSR_FLASH_64K_BANK);
  890. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  891. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  892. }
  893. /* Always perform IO mapped accesses to the FLASH registers. */
  894. if (ha->pio_address) {
  895. uint16_t data2;
  896. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  897. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  898. do {
  899. data = RD_REG_WORD_PIO(&reg->flash_data);
  900. barrier();
  901. cpu_relax();
  902. data2 = RD_REG_WORD_PIO(&reg->flash_data);
  903. } while (data != data2);
  904. } else {
  905. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  906. data = qla2x00_debounce_register(&reg->flash_data);
  907. }
  908. return (uint8_t)data;
  909. }
  910. /**
  911. * qla2x00_write_flash_byte() - Write a byte to flash
  912. * @ha: HA context
  913. * @addr: Address in flash to write
  914. * @data: Data to write
  915. */
  916. static void
  917. qla2x00_write_flash_byte(scsi_qla_host_t *ha, uint32_t addr, uint8_t data)
  918. {
  919. uint16_t bank_select;
  920. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  921. bank_select = RD_REG_WORD(&reg->ctrl_status);
  922. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  923. /* Specify 64K address range: */
  924. /* clear out Module Select and Flash Address bits [19:16]. */
  925. bank_select &= ~0xf8;
  926. bank_select |= addr >> 12 & 0xf0;
  927. bank_select |= CSR_FLASH_64K_BANK;
  928. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  929. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  930. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  931. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  932. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  933. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  934. return;
  935. }
  936. /* Setup bit 16 of flash address. */
  937. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  938. bank_select |= CSR_FLASH_64K_BANK;
  939. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  940. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  941. } else if (((addr & BIT_16) == 0) &&
  942. (bank_select & CSR_FLASH_64K_BANK)) {
  943. bank_select &= ~(CSR_FLASH_64K_BANK);
  944. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  945. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  946. }
  947. /* Always perform IO mapped accesses to the FLASH registers. */
  948. if (ha->pio_address) {
  949. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  950. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  951. WRT_REG_WORD_PIO(&reg->flash_data, (uint16_t)data);
  952. } else {
  953. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  954. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  955. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  956. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  957. }
  958. }
  959. /**
  960. * qla2x00_poll_flash() - Polls flash for completion.
  961. * @ha: HA context
  962. * @addr: Address in flash to poll
  963. * @poll_data: Data to be polled
  964. * @man_id: Flash manufacturer ID
  965. * @flash_id: Flash ID
  966. *
  967. * This function polls the device until bit 7 of what is read matches data
  968. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  969. * out (a fatal error). The flash book recommeds reading bit 7 again after
  970. * reading bit 5 as a 1.
  971. *
  972. * Returns 0 on success, else non-zero.
  973. */
  974. static int
  975. qla2x00_poll_flash(scsi_qla_host_t *ha, uint32_t addr, uint8_t poll_data,
  976. uint8_t man_id, uint8_t flash_id)
  977. {
  978. int status;
  979. uint8_t flash_data;
  980. uint32_t cnt;
  981. status = 1;
  982. /* Wait for 30 seconds for command to finish. */
  983. poll_data &= BIT_7;
  984. for (cnt = 3000000; cnt; cnt--) {
  985. flash_data = qla2x00_read_flash_byte(ha, addr);
  986. if ((flash_data & BIT_7) == poll_data) {
  987. status = 0;
  988. break;
  989. }
  990. if (man_id != 0x40 && man_id != 0xda) {
  991. if ((flash_data & BIT_5) && cnt > 2)
  992. cnt = 2;
  993. }
  994. udelay(10);
  995. barrier();
  996. }
  997. return status;
  998. }
  999. #define IS_OEM_001(ha) \
  1000. ((ha)->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2322 && \
  1001. (ha)->pdev->subsystem_vendor == 0x1028 && \
  1002. (ha)->pdev->subsystem_device == 0x0170)
  1003. /**
  1004. * qla2x00_program_flash_address() - Programs a flash address
  1005. * @ha: HA context
  1006. * @addr: Address in flash to program
  1007. * @data: Data to be written in flash
  1008. * @man_id: Flash manufacturer ID
  1009. * @flash_id: Flash ID
  1010. *
  1011. * Returns 0 on success, else non-zero.
  1012. */
  1013. static int
  1014. qla2x00_program_flash_address(scsi_qla_host_t *ha, uint32_t addr, uint8_t data,
  1015. uint8_t man_id, uint8_t flash_id)
  1016. {
  1017. /* Write Program Command Sequence. */
  1018. if (IS_OEM_001(ha)) {
  1019. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1020. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1021. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1022. qla2x00_write_flash_byte(ha, addr, data);
  1023. } else {
  1024. if (man_id == 0xda && flash_id == 0xc1) {
  1025. qla2x00_write_flash_byte(ha, addr, data);
  1026. if (addr & 0x7e)
  1027. return 0;
  1028. } else {
  1029. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1030. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1031. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1032. qla2x00_write_flash_byte(ha, addr, data);
  1033. }
  1034. }
  1035. udelay(150);
  1036. /* Wait for write to complete. */
  1037. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1038. }
  1039. /**
  1040. * qla2x00_erase_flash() - Erase the flash.
  1041. * @ha: HA context
  1042. * @man_id: Flash manufacturer ID
  1043. * @flash_id: Flash ID
  1044. *
  1045. * Returns 0 on success, else non-zero.
  1046. */
  1047. static int
  1048. qla2x00_erase_flash(scsi_qla_host_t *ha, uint8_t man_id, uint8_t flash_id)
  1049. {
  1050. /* Individual Sector Erase Command Sequence */
  1051. if (IS_OEM_001(ha)) {
  1052. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1053. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1054. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1055. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1056. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1057. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1058. } else {
  1059. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1060. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1061. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1062. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1063. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1064. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1065. }
  1066. udelay(150);
  1067. /* Wait for erase to complete. */
  1068. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1069. }
  1070. /**
  1071. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1072. * @ha: HA context
  1073. * @addr: Flash sector to erase
  1074. * @sec_mask: Sector address mask
  1075. * @man_id: Flash manufacturer ID
  1076. * @flash_id: Flash ID
  1077. *
  1078. * Returns 0 on success, else non-zero.
  1079. */
  1080. static int
  1081. qla2x00_erase_flash_sector(scsi_qla_host_t *ha, uint32_t addr,
  1082. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1083. {
  1084. /* Individual Sector Erase Command Sequence */
  1085. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1086. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1087. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1088. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1089. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1090. if (man_id == 0x1f && flash_id == 0x13)
  1091. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1092. else
  1093. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1094. udelay(150);
  1095. /* Wait for erase to complete. */
  1096. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1097. }
  1098. /**
  1099. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1100. * @man_id: Flash manufacturer ID
  1101. * @flash_id: Flash ID
  1102. */
  1103. static void
  1104. qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  1105. uint8_t *flash_id)
  1106. {
  1107. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1108. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1109. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1110. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1111. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1112. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1113. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1114. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1115. }
  1116. static inline void
  1117. qla2x00_suspend_hba(struct scsi_qla_host *ha)
  1118. {
  1119. int cnt;
  1120. unsigned long flags;
  1121. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1122. /* Suspend HBA. */
  1123. scsi_block_requests(ha->host);
  1124. ha->isp_ops.disable_intrs(ha);
  1125. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1126. /* Pause RISC. */
  1127. spin_lock_irqsave(&ha->hardware_lock, flags);
  1128. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1129. RD_REG_WORD(&reg->hccr);
  1130. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1131. for (cnt = 0; cnt < 30000; cnt++) {
  1132. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1133. break;
  1134. udelay(100);
  1135. }
  1136. } else {
  1137. udelay(10);
  1138. }
  1139. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1140. }
  1141. static inline void
  1142. qla2x00_resume_hba(struct scsi_qla_host *ha)
  1143. {
  1144. /* Resume HBA. */
  1145. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1146. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1147. qla2xxx_wake_dpc(ha);
  1148. qla2x00_wait_for_hba_online(ha);
  1149. scsi_unblock_requests(ha->host);
  1150. }
  1151. uint8_t *
  1152. qla2x00_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1153. uint32_t offset, uint32_t length)
  1154. {
  1155. unsigned long flags;
  1156. uint32_t addr, midpoint;
  1157. uint8_t *data;
  1158. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1159. /* Suspend HBA. */
  1160. qla2x00_suspend_hba(ha);
  1161. /* Go with read. */
  1162. spin_lock_irqsave(&ha->hardware_lock, flags);
  1163. midpoint = ha->optrom_size / 2;
  1164. qla2x00_flash_enable(ha);
  1165. WRT_REG_WORD(&reg->nvram, 0);
  1166. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1167. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1168. if (addr == midpoint) {
  1169. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1170. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1171. }
  1172. *data = qla2x00_read_flash_byte(ha, addr);
  1173. }
  1174. qla2x00_flash_disable(ha);
  1175. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1176. /* Resume HBA. */
  1177. qla2x00_resume_hba(ha);
  1178. return buf;
  1179. }
  1180. int
  1181. qla2x00_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1182. uint32_t offset, uint32_t length)
  1183. {
  1184. int rval;
  1185. unsigned long flags;
  1186. uint8_t man_id, flash_id, sec_number, data;
  1187. uint16_t wd;
  1188. uint32_t addr, liter, sec_mask, rest_addr;
  1189. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1190. /* Suspend HBA. */
  1191. qla2x00_suspend_hba(ha);
  1192. rval = QLA_SUCCESS;
  1193. sec_number = 0;
  1194. /* Reset ISP chip. */
  1195. spin_lock_irqsave(&ha->hardware_lock, flags);
  1196. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1197. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1198. /* Go with write. */
  1199. qla2x00_flash_enable(ha);
  1200. do { /* Loop once to provide quick error exit */
  1201. /* Structure of flash memory based on manufacturer */
  1202. if (IS_OEM_001(ha)) {
  1203. /* OEM variant with special flash part. */
  1204. man_id = flash_id = 0;
  1205. rest_addr = 0xffff;
  1206. sec_mask = 0x10000;
  1207. goto update_flash;
  1208. }
  1209. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1210. switch (man_id) {
  1211. case 0x20: /* ST flash. */
  1212. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1213. /*
  1214. * ST m29w008at part - 64kb sector size with
  1215. * 32kb,8kb,8kb,16kb sectors at memory address
  1216. * 0xf0000.
  1217. */
  1218. rest_addr = 0xffff;
  1219. sec_mask = 0x10000;
  1220. break;
  1221. }
  1222. /*
  1223. * ST m29w010b part - 16kb sector size
  1224. * Default to 16kb sectors
  1225. */
  1226. rest_addr = 0x3fff;
  1227. sec_mask = 0x1c000;
  1228. break;
  1229. case 0x40: /* Mostel flash. */
  1230. /* Mostel v29c51001 part - 512 byte sector size. */
  1231. rest_addr = 0x1ff;
  1232. sec_mask = 0x1fe00;
  1233. break;
  1234. case 0xbf: /* SST flash. */
  1235. /* SST39sf10 part - 4kb sector size. */
  1236. rest_addr = 0xfff;
  1237. sec_mask = 0x1f000;
  1238. break;
  1239. case 0xda: /* Winbond flash. */
  1240. /* Winbond W29EE011 part - 256 byte sector size. */
  1241. rest_addr = 0x7f;
  1242. sec_mask = 0x1ff80;
  1243. break;
  1244. case 0xc2: /* Macronix flash. */
  1245. /* 64k sector size. */
  1246. if (flash_id == 0x38 || flash_id == 0x4f) {
  1247. rest_addr = 0xffff;
  1248. sec_mask = 0x10000;
  1249. break;
  1250. }
  1251. /* Fall through... */
  1252. case 0x1f: /* Atmel flash. */
  1253. /* 512k sector size. */
  1254. if (flash_id == 0x13) {
  1255. rest_addr = 0x7fffffff;
  1256. sec_mask = 0x80000000;
  1257. break;
  1258. }
  1259. /* Fall through... */
  1260. case 0x01: /* AMD flash. */
  1261. if (flash_id == 0x38 || flash_id == 0x40 ||
  1262. flash_id == 0x4f) {
  1263. /* Am29LV081 part - 64kb sector size. */
  1264. /* Am29LV002BT part - 64kb sector size. */
  1265. rest_addr = 0xffff;
  1266. sec_mask = 0x10000;
  1267. break;
  1268. } else if (flash_id == 0x3e) {
  1269. /*
  1270. * Am29LV008b part - 64kb sector size with
  1271. * 32kb,8kb,8kb,16kb sector at memory address
  1272. * h0xf0000.
  1273. */
  1274. rest_addr = 0xffff;
  1275. sec_mask = 0x10000;
  1276. break;
  1277. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1278. /*
  1279. * Am29LV010 part or AM29f010 - 16kb sector
  1280. * size.
  1281. */
  1282. rest_addr = 0x3fff;
  1283. sec_mask = 0x1c000;
  1284. break;
  1285. } else if (flash_id == 0x6d) {
  1286. /* Am29LV001 part - 8kb sector size. */
  1287. rest_addr = 0x1fff;
  1288. sec_mask = 0x1e000;
  1289. break;
  1290. }
  1291. default:
  1292. /* Default to 16 kb sector size. */
  1293. rest_addr = 0x3fff;
  1294. sec_mask = 0x1c000;
  1295. break;
  1296. }
  1297. update_flash:
  1298. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1299. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1300. rval = QLA_FUNCTION_FAILED;
  1301. break;
  1302. }
  1303. }
  1304. for (addr = offset, liter = 0; liter < length; liter++,
  1305. addr++) {
  1306. data = buf[liter];
  1307. /* Are we at the beginning of a sector? */
  1308. if ((addr & rest_addr) == 0) {
  1309. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1310. if (addr >= 0x10000UL) {
  1311. if (((addr >> 12) & 0xf0) &&
  1312. ((man_id == 0x01 &&
  1313. flash_id == 0x3e) ||
  1314. (man_id == 0x20 &&
  1315. flash_id == 0xd2))) {
  1316. sec_number++;
  1317. if (sec_number == 1) {
  1318. rest_addr =
  1319. 0x7fff;
  1320. sec_mask =
  1321. 0x18000;
  1322. } else if (
  1323. sec_number == 2 ||
  1324. sec_number == 3) {
  1325. rest_addr =
  1326. 0x1fff;
  1327. sec_mask =
  1328. 0x1e000;
  1329. } else if (
  1330. sec_number == 4) {
  1331. rest_addr =
  1332. 0x3fff;
  1333. sec_mask =
  1334. 0x1c000;
  1335. }
  1336. }
  1337. }
  1338. } else if (addr == ha->optrom_size / 2) {
  1339. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1340. RD_REG_WORD(&reg->nvram);
  1341. }
  1342. if (flash_id == 0xda && man_id == 0xc1) {
  1343. qla2x00_write_flash_byte(ha, 0x5555,
  1344. 0xaa);
  1345. qla2x00_write_flash_byte(ha, 0x2aaa,
  1346. 0x55);
  1347. qla2x00_write_flash_byte(ha, 0x5555,
  1348. 0xa0);
  1349. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1350. /* Then erase it */
  1351. if (qla2x00_erase_flash_sector(ha,
  1352. addr, sec_mask, man_id,
  1353. flash_id)) {
  1354. rval = QLA_FUNCTION_FAILED;
  1355. break;
  1356. }
  1357. if (man_id == 0x01 && flash_id == 0x6d)
  1358. sec_number++;
  1359. }
  1360. }
  1361. if (man_id == 0x01 && flash_id == 0x6d) {
  1362. if (sec_number == 1 &&
  1363. addr == (rest_addr - 1)) {
  1364. rest_addr = 0x0fff;
  1365. sec_mask = 0x1f000;
  1366. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1367. rest_addr = 0x3fff;
  1368. sec_mask = 0x1c000;
  1369. }
  1370. }
  1371. if (qla2x00_program_flash_address(ha, addr, data,
  1372. man_id, flash_id)) {
  1373. rval = QLA_FUNCTION_FAILED;
  1374. break;
  1375. }
  1376. }
  1377. } while (0);
  1378. qla2x00_flash_disable(ha);
  1379. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1380. /* Resume HBA. */
  1381. qla2x00_resume_hba(ha);
  1382. return rval;
  1383. }
  1384. uint8_t *
  1385. qla24xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1386. uint32_t offset, uint32_t length)
  1387. {
  1388. /* Suspend HBA. */
  1389. scsi_block_requests(ha->host);
  1390. ha->isp_ops.disable_intrs(ha);
  1391. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1392. /* Go with read. */
  1393. qla24xx_read_flash_data(ha, (uint32_t *)buf, offset >> 2, length >> 2);
  1394. /* Resume HBA. */
  1395. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1396. ha->isp_ops.enable_intrs(ha);
  1397. scsi_unblock_requests(ha->host);
  1398. return buf;
  1399. }
  1400. int
  1401. qla24xx_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1402. uint32_t offset, uint32_t length)
  1403. {
  1404. int rval;
  1405. /* Suspend HBA. */
  1406. scsi_block_requests(ha->host);
  1407. ha->isp_ops.disable_intrs(ha);
  1408. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1409. /* Go with write. */
  1410. rval = qla24xx_write_flash_data(ha, (uint32_t *)buf, offset >> 2,
  1411. length >> 2);
  1412. /* Resume HBA -- RISC reset needed. */
  1413. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1414. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1415. qla2xxx_wake_dpc(ha);
  1416. qla2x00_wait_for_hba_online(ha);
  1417. scsi_unblock_requests(ha->host);
  1418. return rval;
  1419. }