qla_sup.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /******************************************************************************
  2. * QLOGIC LINUX SOFTWARE
  3. *
  4. * QLogic ISP2x00 device driver for Linux 2.6.x
  5. * Copyright (C) 2003-2005 QLogic Corporation
  6. * (www.qlogic.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. ******************************************************************************/
  19. #include "qla_def.h"
  20. #include <linux/delay.h>
  21. #include <asm/uaccess.h>
  22. static uint16_t qla2x00_nvram_request(scsi_qla_host_t *, uint32_t);
  23. static void qla2x00_nv_deselect(scsi_qla_host_t *);
  24. static void qla2x00_nv_write(scsi_qla_host_t *, uint16_t);
  25. /*
  26. * NVRAM support routines
  27. */
  28. /**
  29. * qla2x00_lock_nvram_access() -
  30. * @ha: HA context
  31. */
  32. void
  33. qla2x00_lock_nvram_access(scsi_qla_host_t *ha)
  34. {
  35. uint16_t data;
  36. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  37. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  38. data = RD_REG_WORD(&reg->nvram);
  39. while (data & NVR_BUSY) {
  40. udelay(100);
  41. data = RD_REG_WORD(&reg->nvram);
  42. }
  43. /* Lock resource */
  44. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
  45. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  46. udelay(5);
  47. data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  48. while ((data & BIT_0) == 0) {
  49. /* Lock failed */
  50. udelay(100);
  51. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
  52. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  53. udelay(5);
  54. data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  55. }
  56. }
  57. }
  58. /**
  59. * qla2x00_unlock_nvram_access() -
  60. * @ha: HA context
  61. */
  62. void
  63. qla2x00_unlock_nvram_access(scsi_qla_host_t *ha)
  64. {
  65. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  66. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  67. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0);
  68. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  69. }
  70. }
  71. /**
  72. * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
  73. * request routine to get the word from NVRAM.
  74. * @ha: HA context
  75. * @addr: Address in NVRAM to read
  76. *
  77. * Returns the word read from nvram @addr.
  78. */
  79. uint16_t
  80. qla2x00_get_nvram_word(scsi_qla_host_t *ha, uint32_t addr)
  81. {
  82. uint16_t data;
  83. uint32_t nv_cmd;
  84. nv_cmd = addr << 16;
  85. nv_cmd |= NV_READ_OP;
  86. data = qla2x00_nvram_request(ha, nv_cmd);
  87. return (data);
  88. }
  89. /**
  90. * qla2x00_write_nvram_word() - Write NVRAM data.
  91. * @ha: HA context
  92. * @addr: Address in NVRAM to write
  93. * @data: word to program
  94. */
  95. void
  96. qla2x00_write_nvram_word(scsi_qla_host_t *ha, uint32_t addr, uint16_t data)
  97. {
  98. int count;
  99. uint16_t word;
  100. uint32_t nv_cmd;
  101. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  102. qla2x00_nv_write(ha, NVR_DATA_OUT);
  103. qla2x00_nv_write(ha, 0);
  104. qla2x00_nv_write(ha, 0);
  105. for (word = 0; word < 8; word++)
  106. qla2x00_nv_write(ha, NVR_DATA_OUT);
  107. qla2x00_nv_deselect(ha);
  108. /* Write data */
  109. nv_cmd = (addr << 16) | NV_WRITE_OP;
  110. nv_cmd |= data;
  111. nv_cmd <<= 5;
  112. for (count = 0; count < 27; count++) {
  113. if (nv_cmd & BIT_31)
  114. qla2x00_nv_write(ha, NVR_DATA_OUT);
  115. else
  116. qla2x00_nv_write(ha, 0);
  117. nv_cmd <<= 1;
  118. }
  119. qla2x00_nv_deselect(ha);
  120. /* Wait for NVRAM to become ready */
  121. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  122. do {
  123. NVRAM_DELAY();
  124. word = RD_REG_WORD(&reg->nvram);
  125. } while ((word & NVR_DATA_IN) == 0);
  126. qla2x00_nv_deselect(ha);
  127. /* Disable writes */
  128. qla2x00_nv_write(ha, NVR_DATA_OUT);
  129. for (count = 0; count < 10; count++)
  130. qla2x00_nv_write(ha, 0);
  131. qla2x00_nv_deselect(ha);
  132. }
  133. static int
  134. qla2x00_write_nvram_word_tmo(scsi_qla_host_t *ha, uint32_t addr, uint16_t data,
  135. uint32_t tmo)
  136. {
  137. int ret, count;
  138. uint16_t word;
  139. uint32_t nv_cmd;
  140. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  141. ret = QLA_SUCCESS;
  142. qla2x00_nv_write(ha, NVR_DATA_OUT);
  143. qla2x00_nv_write(ha, 0);
  144. qla2x00_nv_write(ha, 0);
  145. for (word = 0; word < 8; word++)
  146. qla2x00_nv_write(ha, NVR_DATA_OUT);
  147. qla2x00_nv_deselect(ha);
  148. /* Write data */
  149. nv_cmd = (addr << 16) | NV_WRITE_OP;
  150. nv_cmd |= data;
  151. nv_cmd <<= 5;
  152. for (count = 0; count < 27; count++) {
  153. if (nv_cmd & BIT_31)
  154. qla2x00_nv_write(ha, NVR_DATA_OUT);
  155. else
  156. qla2x00_nv_write(ha, 0);
  157. nv_cmd <<= 1;
  158. }
  159. qla2x00_nv_deselect(ha);
  160. /* Wait for NVRAM to become ready */
  161. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  162. do {
  163. NVRAM_DELAY();
  164. word = RD_REG_WORD(&reg->nvram);
  165. if (!--tmo) {
  166. ret = QLA_FUNCTION_FAILED;
  167. break;
  168. }
  169. } while ((word & NVR_DATA_IN) == 0);
  170. qla2x00_nv_deselect(ha);
  171. /* Disable writes */
  172. qla2x00_nv_write(ha, NVR_DATA_OUT);
  173. for (count = 0; count < 10; count++)
  174. qla2x00_nv_write(ha, 0);
  175. qla2x00_nv_deselect(ha);
  176. return ret;
  177. }
  178. /**
  179. * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
  180. * NVRAM.
  181. * @ha: HA context
  182. * @nv_cmd: NVRAM command
  183. *
  184. * Bit definitions for NVRAM command:
  185. *
  186. * Bit 26 = start bit
  187. * Bit 25, 24 = opcode
  188. * Bit 23-16 = address
  189. * Bit 15-0 = write data
  190. *
  191. * Returns the word read from nvram @addr.
  192. */
  193. static uint16_t
  194. qla2x00_nvram_request(scsi_qla_host_t *ha, uint32_t nv_cmd)
  195. {
  196. uint8_t cnt;
  197. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  198. uint16_t data = 0;
  199. uint16_t reg_data;
  200. /* Send command to NVRAM. */
  201. nv_cmd <<= 5;
  202. for (cnt = 0; cnt < 11; cnt++) {
  203. if (nv_cmd & BIT_31)
  204. qla2x00_nv_write(ha, NVR_DATA_OUT);
  205. else
  206. qla2x00_nv_write(ha, 0);
  207. nv_cmd <<= 1;
  208. }
  209. /* Read data from NVRAM. */
  210. for (cnt = 0; cnt < 16; cnt++) {
  211. WRT_REG_WORD(&reg->nvram, NVR_SELECT | NVR_CLOCK);
  212. NVRAM_DELAY();
  213. data <<= 1;
  214. reg_data = RD_REG_WORD(&reg->nvram);
  215. if (reg_data & NVR_DATA_IN)
  216. data |= BIT_0;
  217. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  218. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  219. NVRAM_DELAY();
  220. }
  221. /* Deselect chip. */
  222. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  223. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  224. NVRAM_DELAY();
  225. return (data);
  226. }
  227. /**
  228. * qla2x00_nv_write() - Clean NVRAM operations.
  229. * @ha: HA context
  230. */
  231. static void
  232. qla2x00_nv_deselect(scsi_qla_host_t *ha)
  233. {
  234. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  235. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  236. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  237. NVRAM_DELAY();
  238. }
  239. /**
  240. * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
  241. * @ha: HA context
  242. * @data: Serial interface selector
  243. */
  244. static void
  245. qla2x00_nv_write(scsi_qla_host_t *ha, uint16_t data)
  246. {
  247. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  248. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  249. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  250. NVRAM_DELAY();
  251. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT| NVR_CLOCK |
  252. NVR_WRT_ENABLE);
  253. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  254. NVRAM_DELAY();
  255. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  256. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  257. NVRAM_DELAY();
  258. }
  259. /**
  260. * qla2x00_clear_nvram_protection() -
  261. * @ha: HA context
  262. */
  263. static int
  264. qla2x00_clear_nvram_protection(scsi_qla_host_t *ha)
  265. {
  266. int ret, stat;
  267. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  268. uint32_t word;
  269. uint16_t wprot, wprot_old;
  270. /* Clear NVRAM write protection. */
  271. ret = QLA_FUNCTION_FAILED;
  272. wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, 0));
  273. stat = qla2x00_write_nvram_word_tmo(ha, 0,
  274. __constant_cpu_to_le16(0x1234), 100000);
  275. wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, 0));
  276. if (stat != QLA_SUCCESS || wprot != __constant_cpu_to_le16(0x1234)) {
  277. /* Write enable. */
  278. qla2x00_nv_write(ha, NVR_DATA_OUT);
  279. qla2x00_nv_write(ha, 0);
  280. qla2x00_nv_write(ha, 0);
  281. for (word = 0; word < 8; word++)
  282. qla2x00_nv_write(ha, NVR_DATA_OUT);
  283. qla2x00_nv_deselect(ha);
  284. /* Enable protection register. */
  285. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  286. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  287. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  288. for (word = 0; word < 8; word++)
  289. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  290. qla2x00_nv_deselect(ha);
  291. /* Clear protection register (ffff is cleared). */
  292. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  293. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  294. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  295. for (word = 0; word < 8; word++)
  296. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  297. qla2x00_nv_deselect(ha);
  298. /* Wait for NVRAM to become ready. */
  299. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  300. do {
  301. NVRAM_DELAY();
  302. word = RD_REG_WORD(&reg->nvram);
  303. } while ((word & NVR_DATA_IN) == 0);
  304. ret = QLA_SUCCESS;
  305. } else
  306. qla2x00_write_nvram_word(ha, 0, wprot_old);
  307. return ret;
  308. }
  309. static void
  310. qla2x00_set_nvram_protection(scsi_qla_host_t *ha, int stat)
  311. {
  312. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  313. uint32_t word;
  314. if (stat != QLA_SUCCESS)
  315. return;
  316. /* Set NVRAM write protection. */
  317. /* Write enable. */
  318. qla2x00_nv_write(ha, NVR_DATA_OUT);
  319. qla2x00_nv_write(ha, 0);
  320. qla2x00_nv_write(ha, 0);
  321. for (word = 0; word < 8; word++)
  322. qla2x00_nv_write(ha, NVR_DATA_OUT);
  323. qla2x00_nv_deselect(ha);
  324. /* Enable protection register. */
  325. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  326. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  327. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  328. for (word = 0; word < 8; word++)
  329. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  330. qla2x00_nv_deselect(ha);
  331. /* Enable protection register. */
  332. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  333. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  334. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  335. for (word = 0; word < 8; word++)
  336. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  337. qla2x00_nv_deselect(ha);
  338. /* Wait for NVRAM to become ready. */
  339. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  340. do {
  341. NVRAM_DELAY();
  342. word = RD_REG_WORD(&reg->nvram);
  343. } while ((word & NVR_DATA_IN) == 0);
  344. }
  345. /*****************************************************************************/
  346. /* Flash Manipulation Routines */
  347. /*****************************************************************************/
  348. static inline uint32_t
  349. flash_conf_to_access_addr(uint32_t faddr)
  350. {
  351. return FARX_ACCESS_FLASH_CONF | faddr;
  352. }
  353. static inline uint32_t
  354. flash_data_to_access_addr(uint32_t faddr)
  355. {
  356. return FARX_ACCESS_FLASH_DATA | faddr;
  357. }
  358. static inline uint32_t
  359. nvram_conf_to_access_addr(uint32_t naddr)
  360. {
  361. return FARX_ACCESS_NVRAM_CONF | naddr;
  362. }
  363. static inline uint32_t
  364. nvram_data_to_access_addr(uint32_t naddr)
  365. {
  366. return FARX_ACCESS_NVRAM_DATA | naddr;
  367. }
  368. uint32_t
  369. qla24xx_read_flash_dword(scsi_qla_host_t *ha, uint32_t addr)
  370. {
  371. int rval;
  372. uint32_t cnt, data;
  373. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  374. WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  375. /* Wait for READ cycle to complete. */
  376. rval = QLA_SUCCESS;
  377. for (cnt = 3000;
  378. (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
  379. rval == QLA_SUCCESS; cnt--) {
  380. if (cnt)
  381. udelay(10);
  382. else
  383. rval = QLA_FUNCTION_TIMEOUT;
  384. }
  385. /* TODO: What happens if we time out? */
  386. data = 0xDEADDEAD;
  387. if (rval == QLA_SUCCESS)
  388. data = RD_REG_DWORD(&reg->flash_data);
  389. return data;
  390. }
  391. uint32_t *
  392. qla24xx_read_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  393. uint32_t dwords)
  394. {
  395. uint32_t i;
  396. /* Dword reads to flash. */
  397. for (i = 0; i < dwords; i++, faddr++)
  398. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  399. flash_data_to_access_addr(faddr)));
  400. return dwptr;
  401. }
  402. int
  403. qla24xx_write_flash_dword(scsi_qla_host_t *ha, uint32_t addr, uint32_t data)
  404. {
  405. int rval;
  406. uint32_t cnt;
  407. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  408. WRT_REG_DWORD(&reg->flash_data, data);
  409. RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
  410. WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
  411. /* Wait for Write cycle to complete. */
  412. rval = QLA_SUCCESS;
  413. for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
  414. rval == QLA_SUCCESS; cnt--) {
  415. if (cnt)
  416. udelay(10);
  417. else
  418. rval = QLA_FUNCTION_TIMEOUT;
  419. }
  420. return rval;
  421. }
  422. void
  423. qla24xx_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  424. uint8_t *flash_id)
  425. {
  426. uint32_t ids;
  427. ids = qla24xx_read_flash_dword(ha, flash_data_to_access_addr(0xd03ab));
  428. *man_id = LSB(ids);
  429. *flash_id = MSB(ids);
  430. }
  431. int
  432. qla24xx_write_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  433. uint32_t dwords)
  434. {
  435. int ret;
  436. uint32_t liter;
  437. uint32_t sec_mask, rest_addr, conf_addr;
  438. uint32_t fdata;
  439. uint8_t man_id, flash_id;
  440. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  441. ret = QLA_SUCCESS;
  442. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  443. DEBUG9(printk("%s(%ld): Flash man_id=%d flash_id=%d\n", __func__,
  444. ha->host_no, man_id, flash_id));
  445. conf_addr = flash_conf_to_access_addr(0x03d8);
  446. switch (man_id) {
  447. case 0xbf: /* STT flash. */
  448. rest_addr = 0x1fff;
  449. sec_mask = 0x3e000;
  450. if (flash_id == 0x80)
  451. conf_addr = flash_conf_to_access_addr(0x0352);
  452. break;
  453. case 0x13: /* ST M25P80. */
  454. rest_addr = 0x3fff;
  455. sec_mask = 0x3c000;
  456. break;
  457. default:
  458. /* Default to 64 kb sector size. */
  459. rest_addr = 0x3fff;
  460. sec_mask = 0x3c000;
  461. break;
  462. }
  463. /* Enable flash write. */
  464. WRT_REG_DWORD(&reg->ctrl_status,
  465. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  466. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  467. /* Disable flash write-protection. */
  468. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  469. do { /* Loop once to provide quick error exit. */
  470. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  471. /* Are we at the beginning of a sector? */
  472. if ((faddr & rest_addr) == 0) {
  473. fdata = (faddr & sec_mask) << 2;
  474. ret = qla24xx_write_flash_dword(ha, conf_addr,
  475. (fdata & 0xff00) |((fdata << 16) &
  476. 0xff0000) | ((fdata >> 16) & 0xff));
  477. if (ret != QLA_SUCCESS) {
  478. DEBUG9(printk("%s(%ld) Unable to flash "
  479. "sector: address=%x.\n", __func__,
  480. ha->host_no, faddr));
  481. break;
  482. }
  483. }
  484. ret = qla24xx_write_flash_dword(ha,
  485. flash_data_to_access_addr(faddr),
  486. cpu_to_le32(*dwptr));
  487. if (ret != QLA_SUCCESS) {
  488. DEBUG9(printk("%s(%ld) Unable to program flash "
  489. "address=%x data=%x.\n", __func__,
  490. ha->host_no, faddr, *dwptr));
  491. break;
  492. }
  493. }
  494. } while (0);
  495. /* Disable flash write. */
  496. WRT_REG_DWORD(&reg->ctrl_status,
  497. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  498. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  499. return ret;
  500. }
  501. uint8_t *
  502. qla2x00_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  503. uint32_t bytes)
  504. {
  505. uint32_t i;
  506. uint16_t *wptr;
  507. /* Word reads to NVRAM via registers. */
  508. wptr = (uint16_t *)buf;
  509. qla2x00_lock_nvram_access(ha);
  510. for (i = 0; i < bytes >> 1; i++, naddr++)
  511. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  512. naddr));
  513. qla2x00_unlock_nvram_access(ha);
  514. return buf;
  515. }
  516. uint8_t *
  517. qla24xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  518. uint32_t bytes)
  519. {
  520. uint32_t i;
  521. uint32_t *dwptr;
  522. /* Dword reads to flash. */
  523. dwptr = (uint32_t *)buf;
  524. for (i = 0; i < bytes >> 2; i++, naddr++)
  525. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  526. nvram_data_to_access_addr(naddr)));
  527. return buf;
  528. }
  529. int
  530. qla2x00_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  531. uint32_t bytes)
  532. {
  533. int ret, stat;
  534. uint32_t i;
  535. uint16_t *wptr;
  536. ret = QLA_SUCCESS;
  537. qla2x00_lock_nvram_access(ha);
  538. /* Disable NVRAM write-protection. */
  539. stat = qla2x00_clear_nvram_protection(ha);
  540. wptr = (uint16_t *)buf;
  541. for (i = 0; i < bytes >> 1; i++, naddr++) {
  542. qla2x00_write_nvram_word(ha, naddr,
  543. cpu_to_le16(*wptr));
  544. wptr++;
  545. }
  546. /* Enable NVRAM write-protection. */
  547. qla2x00_set_nvram_protection(ha, stat);
  548. qla2x00_unlock_nvram_access(ha);
  549. return ret;
  550. }
  551. int
  552. qla24xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  553. uint32_t bytes)
  554. {
  555. int ret;
  556. uint32_t i;
  557. uint32_t *dwptr;
  558. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  559. ret = QLA_SUCCESS;
  560. /* Enable flash write. */
  561. WRT_REG_DWORD(&reg->ctrl_status,
  562. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  563. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  564. /* Disable NVRAM write-protection. */
  565. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  566. 0);
  567. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  568. 0);
  569. /* Dword writes to flash. */
  570. dwptr = (uint32_t *)buf;
  571. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  572. ret = qla24xx_write_flash_dword(ha,
  573. nvram_data_to_access_addr(naddr),
  574. cpu_to_le32(*dwptr));
  575. if (ret != QLA_SUCCESS) {
  576. DEBUG9(printk("%s(%ld) Unable to program "
  577. "nvram address=%x data=%x.\n", __func__,
  578. ha->host_no, naddr, *dwptr));
  579. break;
  580. }
  581. }
  582. /* Enable NVRAM write-protection. */
  583. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  584. 0x8c);
  585. /* Disable flash write. */
  586. WRT_REG_DWORD(&reg->ctrl_status,
  587. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  588. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  589. return ret;
  590. }