qla_sup.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. }