IPVS: fix netns if reading ip_vs_* procfs entries
[linux-2.6/linux-mips.git] / drivers / scsi / qla2xxx / qla_sup.c
blob22070621206c78d6eec28666dad84e3a6a756329
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2010 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12 #include <asm/uaccess.h>
15 * NVRAM support routines
18 /**
19 * qla2x00_lock_nvram_access() -
20 * @ha: HA context
22 static void
23 qla2x00_lock_nvram_access(struct qla_hw_data *ha)
25 uint16_t data;
26 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
28 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
29 data = RD_REG_WORD(&reg->nvram);
30 while (data & NVR_BUSY) {
31 udelay(100);
32 data = RD_REG_WORD(&reg->nvram);
35 /* Lock resource */
36 WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
37 RD_REG_WORD(&reg->u.isp2300.host_semaphore);
38 udelay(5);
39 data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
40 while ((data & BIT_0) == 0) {
41 /* Lock failed */
42 udelay(100);
43 WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
44 RD_REG_WORD(&reg->u.isp2300.host_semaphore);
45 udelay(5);
46 data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
51 /**
52 * qla2x00_unlock_nvram_access() -
53 * @ha: HA context
55 static void
56 qla2x00_unlock_nvram_access(struct qla_hw_data *ha)
58 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
60 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
61 WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0);
62 RD_REG_WORD(&reg->u.isp2300.host_semaphore);
66 /**
67 * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
68 * @ha: HA context
69 * @data: Serial interface selector
71 static void
72 qla2x00_nv_write(struct qla_hw_data *ha, uint16_t data)
74 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
76 WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
77 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
78 NVRAM_DELAY();
79 WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_CLOCK |
80 NVR_WRT_ENABLE);
81 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
82 NVRAM_DELAY();
83 WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
84 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
85 NVRAM_DELAY();
88 /**
89 * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
90 * NVRAM.
91 * @ha: HA context
92 * @nv_cmd: NVRAM command
94 * Bit definitions for NVRAM command:
96 * Bit 26 = start bit
97 * Bit 25, 24 = opcode
98 * Bit 23-16 = address
99 * Bit 15-0 = write data
101 * Returns the word read from nvram @addr.
103 static uint16_t
104 qla2x00_nvram_request(struct qla_hw_data *ha, uint32_t nv_cmd)
106 uint8_t cnt;
107 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
108 uint16_t data = 0;
109 uint16_t reg_data;
111 /* Send command to NVRAM. */
112 nv_cmd <<= 5;
113 for (cnt = 0; cnt < 11; cnt++) {
114 if (nv_cmd & BIT_31)
115 qla2x00_nv_write(ha, NVR_DATA_OUT);
116 else
117 qla2x00_nv_write(ha, 0);
118 nv_cmd <<= 1;
121 /* Read data from NVRAM. */
122 for (cnt = 0; cnt < 16; cnt++) {
123 WRT_REG_WORD(&reg->nvram, NVR_SELECT | NVR_CLOCK);
124 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
125 NVRAM_DELAY();
126 data <<= 1;
127 reg_data = RD_REG_WORD(&reg->nvram);
128 if (reg_data & NVR_DATA_IN)
129 data |= BIT_0;
130 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
131 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
132 NVRAM_DELAY();
135 /* Deselect chip. */
136 WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
137 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
138 NVRAM_DELAY();
140 return data;
145 * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
146 * request routine to get the word from NVRAM.
147 * @ha: HA context
148 * @addr: Address in NVRAM to read
150 * Returns the word read from nvram @addr.
152 static uint16_t
153 qla2x00_get_nvram_word(struct qla_hw_data *ha, uint32_t addr)
155 uint16_t data;
156 uint32_t nv_cmd;
158 nv_cmd = addr << 16;
159 nv_cmd |= NV_READ_OP;
160 data = qla2x00_nvram_request(ha, nv_cmd);
162 return (data);
166 * qla2x00_nv_deselect() - Deselect NVRAM operations.
167 * @ha: HA context
169 static void
170 qla2x00_nv_deselect(struct qla_hw_data *ha)
172 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
174 WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
175 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
176 NVRAM_DELAY();
180 * qla2x00_write_nvram_word() - Write NVRAM data.
181 * @ha: HA context
182 * @addr: Address in NVRAM to write
183 * @data: word to program
185 static void
186 qla2x00_write_nvram_word(struct qla_hw_data *ha, uint32_t addr, uint16_t data)
188 int count;
189 uint16_t word;
190 uint32_t nv_cmd, wait_cnt;
191 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
193 qla2x00_nv_write(ha, NVR_DATA_OUT);
194 qla2x00_nv_write(ha, 0);
195 qla2x00_nv_write(ha, 0);
197 for (word = 0; word < 8; word++)
198 qla2x00_nv_write(ha, NVR_DATA_OUT);
200 qla2x00_nv_deselect(ha);
202 /* Write data */
203 nv_cmd = (addr << 16) | NV_WRITE_OP;
204 nv_cmd |= data;
205 nv_cmd <<= 5;
206 for (count = 0; count < 27; count++) {
207 if (nv_cmd & BIT_31)
208 qla2x00_nv_write(ha, NVR_DATA_OUT);
209 else
210 qla2x00_nv_write(ha, 0);
212 nv_cmd <<= 1;
215 qla2x00_nv_deselect(ha);
217 /* Wait for NVRAM to become ready */
218 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
219 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
220 wait_cnt = NVR_WAIT_CNT;
221 do {
222 if (!--wait_cnt) {
223 DEBUG9_10(qla_printk(KERN_WARNING, ha,
224 "NVRAM didn't go ready...\n"));
225 break;
227 NVRAM_DELAY();
228 word = RD_REG_WORD(&reg->nvram);
229 } while ((word & NVR_DATA_IN) == 0);
231 qla2x00_nv_deselect(ha);
233 /* Disable writes */
234 qla2x00_nv_write(ha, NVR_DATA_OUT);
235 for (count = 0; count < 10; count++)
236 qla2x00_nv_write(ha, 0);
238 qla2x00_nv_deselect(ha);
241 static int
242 qla2x00_write_nvram_word_tmo(struct qla_hw_data *ha, uint32_t addr,
243 uint16_t data, uint32_t tmo)
245 int ret, count;
246 uint16_t word;
247 uint32_t nv_cmd;
248 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
250 ret = QLA_SUCCESS;
252 qla2x00_nv_write(ha, NVR_DATA_OUT);
253 qla2x00_nv_write(ha, 0);
254 qla2x00_nv_write(ha, 0);
256 for (word = 0; word < 8; word++)
257 qla2x00_nv_write(ha, NVR_DATA_OUT);
259 qla2x00_nv_deselect(ha);
261 /* Write data */
262 nv_cmd = (addr << 16) | NV_WRITE_OP;
263 nv_cmd |= data;
264 nv_cmd <<= 5;
265 for (count = 0; count < 27; count++) {
266 if (nv_cmd & BIT_31)
267 qla2x00_nv_write(ha, NVR_DATA_OUT);
268 else
269 qla2x00_nv_write(ha, 0);
271 nv_cmd <<= 1;
274 qla2x00_nv_deselect(ha);
276 /* Wait for NVRAM to become ready */
277 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
278 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
279 do {
280 NVRAM_DELAY();
281 word = RD_REG_WORD(&reg->nvram);
282 if (!--tmo) {
283 ret = QLA_FUNCTION_FAILED;
284 break;
286 } while ((word & NVR_DATA_IN) == 0);
288 qla2x00_nv_deselect(ha);
290 /* Disable writes */
291 qla2x00_nv_write(ha, NVR_DATA_OUT);
292 for (count = 0; count < 10; count++)
293 qla2x00_nv_write(ha, 0);
295 qla2x00_nv_deselect(ha);
297 return ret;
301 * qla2x00_clear_nvram_protection() -
302 * @ha: HA context
304 static int
305 qla2x00_clear_nvram_protection(struct qla_hw_data *ha)
307 int ret, stat;
308 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
309 uint32_t word, wait_cnt;
310 uint16_t wprot, wprot_old;
312 /* Clear NVRAM write protection. */
313 ret = QLA_FUNCTION_FAILED;
315 wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
316 stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
317 __constant_cpu_to_le16(0x1234), 100000);
318 wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
319 if (stat != QLA_SUCCESS || wprot != 0x1234) {
320 /* Write enable. */
321 qla2x00_nv_write(ha, NVR_DATA_OUT);
322 qla2x00_nv_write(ha, 0);
323 qla2x00_nv_write(ha, 0);
324 for (word = 0; word < 8; word++)
325 qla2x00_nv_write(ha, NVR_DATA_OUT);
327 qla2x00_nv_deselect(ha);
329 /* Enable protection register. */
330 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
331 qla2x00_nv_write(ha, NVR_PR_ENABLE);
332 qla2x00_nv_write(ha, NVR_PR_ENABLE);
333 for (word = 0; word < 8; word++)
334 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
336 qla2x00_nv_deselect(ha);
338 /* Clear protection register (ffff is cleared). */
339 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
340 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
341 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
342 for (word = 0; word < 8; word++)
343 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
345 qla2x00_nv_deselect(ha);
347 /* Wait for NVRAM to become ready. */
348 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
349 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
350 wait_cnt = NVR_WAIT_CNT;
351 do {
352 if (!--wait_cnt) {
353 DEBUG9_10(qla_printk(KERN_WARNING, ha,
354 "NVRAM didn't go ready...\n"));
355 break;
357 NVRAM_DELAY();
358 word = RD_REG_WORD(&reg->nvram);
359 } while ((word & NVR_DATA_IN) == 0);
361 if (wait_cnt)
362 ret = QLA_SUCCESS;
363 } else
364 qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
366 return ret;
369 static void
370 qla2x00_set_nvram_protection(struct qla_hw_data *ha, int stat)
372 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
373 uint32_t word, wait_cnt;
375 if (stat != QLA_SUCCESS)
376 return;
378 /* Set NVRAM write protection. */
379 /* Write enable. */
380 qla2x00_nv_write(ha, NVR_DATA_OUT);
381 qla2x00_nv_write(ha, 0);
382 qla2x00_nv_write(ha, 0);
383 for (word = 0; word < 8; word++)
384 qla2x00_nv_write(ha, NVR_DATA_OUT);
386 qla2x00_nv_deselect(ha);
388 /* Enable protection register. */
389 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
390 qla2x00_nv_write(ha, NVR_PR_ENABLE);
391 qla2x00_nv_write(ha, NVR_PR_ENABLE);
392 for (word = 0; word < 8; word++)
393 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
395 qla2x00_nv_deselect(ha);
397 /* Enable protection register. */
398 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
399 qla2x00_nv_write(ha, NVR_PR_ENABLE);
400 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
401 for (word = 0; word < 8; word++)
402 qla2x00_nv_write(ha, NVR_PR_ENABLE);
404 qla2x00_nv_deselect(ha);
406 /* Wait for NVRAM to become ready. */
407 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
408 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
409 wait_cnt = NVR_WAIT_CNT;
410 do {
411 if (!--wait_cnt) {
412 DEBUG9_10(qla_printk(KERN_WARNING, ha,
413 "NVRAM didn't go ready...\n"));
414 break;
416 NVRAM_DELAY();
417 word = RD_REG_WORD(&reg->nvram);
418 } while ((word & NVR_DATA_IN) == 0);
422 /*****************************************************************************/
423 /* Flash Manipulation Routines */
424 /*****************************************************************************/
426 static inline uint32_t
427 flash_conf_addr(struct qla_hw_data *ha, uint32_t faddr)
429 return ha->flash_conf_off | faddr;
432 static inline uint32_t
433 flash_data_addr(struct qla_hw_data *ha, uint32_t faddr)
435 return ha->flash_data_off | faddr;
438 static inline uint32_t
439 nvram_conf_addr(struct qla_hw_data *ha, uint32_t naddr)
441 return ha->nvram_conf_off | naddr;
444 static inline uint32_t
445 nvram_data_addr(struct qla_hw_data *ha, uint32_t naddr)
447 return ha->nvram_data_off | naddr;
450 static uint32_t
451 qla24xx_read_flash_dword(struct qla_hw_data *ha, uint32_t addr)
453 int rval;
454 uint32_t cnt, data;
455 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
457 WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
458 /* Wait for READ cycle to complete. */
459 rval = QLA_SUCCESS;
460 for (cnt = 3000;
461 (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
462 rval == QLA_SUCCESS; cnt--) {
463 if (cnt)
464 udelay(10);
465 else
466 rval = QLA_FUNCTION_TIMEOUT;
467 cond_resched();
470 /* TODO: What happens if we time out? */
471 data = 0xDEADDEAD;
472 if (rval == QLA_SUCCESS)
473 data = RD_REG_DWORD(&reg->flash_data);
475 return data;
478 uint32_t *
479 qla24xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
480 uint32_t dwords)
482 uint32_t i;
483 struct qla_hw_data *ha = vha->hw;
485 /* Dword reads to flash. */
486 for (i = 0; i < dwords; i++, faddr++)
487 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
488 flash_data_addr(ha, faddr)));
490 return dwptr;
493 static int
494 qla24xx_write_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t data)
496 int rval;
497 uint32_t cnt;
498 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
500 WRT_REG_DWORD(&reg->flash_data, data);
501 RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
502 WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
503 /* Wait for Write cycle to complete. */
504 rval = QLA_SUCCESS;
505 for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
506 rval == QLA_SUCCESS; cnt--) {
507 if (cnt)
508 udelay(10);
509 else
510 rval = QLA_FUNCTION_TIMEOUT;
511 cond_resched();
513 return rval;
516 static void
517 qla24xx_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
518 uint8_t *flash_id)
520 uint32_t ids;
522 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x03ab));
523 *man_id = LSB(ids);
524 *flash_id = MSB(ids);
526 /* Check if man_id and flash_id are valid. */
527 if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
528 /* Read information using 0x9f opcode
529 * Device ID, Mfg ID would be read in the format:
530 * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
531 * Example: ATMEL 0x00 01 45 1F
532 * Extract MFG and Dev ID from last two bytes.
534 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x009f));
535 *man_id = LSB(ids);
536 *flash_id = MSB(ids);
540 static int
541 qla2xxx_find_flt_start(scsi_qla_host_t *vha, uint32_t *start)
543 const char *loc, *locations[] = { "DEF", "PCI" };
544 uint32_t pcihdr, pcids;
545 uint32_t *dcode;
546 uint8_t *buf, *bcode, last_image;
547 uint16_t cnt, chksum, *wptr;
548 struct qla_flt_location *fltl;
549 struct qla_hw_data *ha = vha->hw;
550 struct req_que *req = ha->req_q_map[0];
553 * FLT-location structure resides after the last PCI region.
556 /* Begin with sane defaults. */
557 loc = locations[0];
558 *start = 0;
559 if (IS_QLA24XX_TYPE(ha))
560 *start = FA_FLASH_LAYOUT_ADDR_24;
561 else if (IS_QLA25XX(ha))
562 *start = FA_FLASH_LAYOUT_ADDR;
563 else if (IS_QLA81XX(ha))
564 *start = FA_FLASH_LAYOUT_ADDR_81;
565 else if (IS_QLA82XX(ha)) {
566 *start = FA_FLASH_LAYOUT_ADDR_82;
567 goto end;
569 /* Begin with first PCI expansion ROM header. */
570 buf = (uint8_t *)req->ring;
571 dcode = (uint32_t *)req->ring;
572 pcihdr = 0;
573 last_image = 1;
574 do {
575 /* Verify PCI expansion ROM header. */
576 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
577 bcode = buf + (pcihdr % 4);
578 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa)
579 goto end;
581 /* Locate PCI data structure. */
582 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
583 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
584 bcode = buf + (pcihdr % 4);
586 /* Validate signature of PCI data structure. */
587 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
588 bcode[0x2] != 'I' || bcode[0x3] != 'R')
589 goto end;
591 last_image = bcode[0x15] & BIT_7;
593 /* Locate next PCI expansion ROM. */
594 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
595 } while (!last_image);
597 /* Now verify FLT-location structure. */
598 fltl = (struct qla_flt_location *)req->ring;
599 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2,
600 sizeof(struct qla_flt_location) >> 2);
601 if (fltl->sig[0] != 'Q' || fltl->sig[1] != 'F' ||
602 fltl->sig[2] != 'L' || fltl->sig[3] != 'T')
603 goto end;
605 wptr = (uint16_t *)req->ring;
606 cnt = sizeof(struct qla_flt_location) >> 1;
607 for (chksum = 0; cnt; cnt--)
608 chksum += le16_to_cpu(*wptr++);
609 if (chksum) {
610 qla_printk(KERN_ERR, ha,
611 "Inconsistent FLTL detected: checksum=0x%x.\n", chksum);
612 qla2x00_dump_buffer(buf, sizeof(struct qla_flt_location));
613 return QLA_FUNCTION_FAILED;
616 /* Good data. Use specified location. */
617 loc = locations[1];
618 *start = (le16_to_cpu(fltl->start_hi) << 16 |
619 le16_to_cpu(fltl->start_lo)) >> 2;
620 end:
621 DEBUG2(qla_printk(KERN_DEBUG, ha, "FLTL[%s] = 0x%x.\n", loc, *start));
622 return QLA_SUCCESS;
625 static void
626 qla2xxx_get_flt_info(scsi_qla_host_t *vha, uint32_t flt_addr)
628 const char *loc, *locations[] = { "DEF", "FLT" };
629 const uint32_t def_fw[] =
630 { FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR_81 };
631 const uint32_t def_boot[] =
632 { FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR_81 };
633 const uint32_t def_vpd_nvram[] =
634 { FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR_81 };
635 const uint32_t def_vpd0[] =
636 { 0, 0, FA_VPD0_ADDR_81 };
637 const uint32_t def_vpd1[] =
638 { 0, 0, FA_VPD1_ADDR_81 };
639 const uint32_t def_nvram0[] =
640 { 0, 0, FA_NVRAM0_ADDR_81 };
641 const uint32_t def_nvram1[] =
642 { 0, 0, FA_NVRAM1_ADDR_81 };
643 const uint32_t def_fdt[] =
644 { FA_FLASH_DESCR_ADDR_24, FA_FLASH_DESCR_ADDR,
645 FA_FLASH_DESCR_ADDR_81 };
646 const uint32_t def_npiv_conf0[] =
647 { FA_NPIV_CONF0_ADDR_24, FA_NPIV_CONF0_ADDR,
648 FA_NPIV_CONF0_ADDR_81 };
649 const uint32_t def_npiv_conf1[] =
650 { FA_NPIV_CONF1_ADDR_24, FA_NPIV_CONF1_ADDR,
651 FA_NPIV_CONF1_ADDR_81 };
652 const uint32_t fcp_prio_cfg0[] =
653 { FA_FCP_PRIO0_ADDR, FA_FCP_PRIO0_ADDR_25,
654 0 };
655 const uint32_t fcp_prio_cfg1[] =
656 { FA_FCP_PRIO1_ADDR, FA_FCP_PRIO1_ADDR_25,
657 0 };
658 uint32_t def;
659 uint16_t *wptr;
660 uint16_t cnt, chksum;
661 uint32_t start;
662 struct qla_flt_header *flt;
663 struct qla_flt_region *region;
664 struct qla_hw_data *ha = vha->hw;
665 struct req_que *req = ha->req_q_map[0];
667 def = 0;
668 if (IS_QLA25XX(ha))
669 def = 1;
670 else if (IS_QLA81XX(ha))
671 def = 2;
673 /* Assign FCP prio region since older adapters may not have FLT, or
674 FCP prio region in it's FLT.
676 ha->flt_region_fcp_prio = ha->flags.port0 ?
677 fcp_prio_cfg0[def] : fcp_prio_cfg1[def];
679 ha->flt_region_flt = flt_addr;
680 wptr = (uint16_t *)req->ring;
681 flt = (struct qla_flt_header *)req->ring;
682 region = (struct qla_flt_region *)&flt[1];
683 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
684 flt_addr << 2, OPTROM_BURST_SIZE);
685 if (*wptr == __constant_cpu_to_le16(0xffff))
686 goto no_flash_data;
687 if (flt->version != __constant_cpu_to_le16(1)) {
688 DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported FLT detected: "
689 "version=0x%x length=0x%x checksum=0x%x.\n",
690 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
691 le16_to_cpu(flt->checksum)));
692 goto no_flash_data;
695 cnt = (sizeof(struct qla_flt_header) + le16_to_cpu(flt->length)) >> 1;
696 for (chksum = 0; cnt; cnt--)
697 chksum += le16_to_cpu(*wptr++);
698 if (chksum) {
699 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FLT detected: "
700 "version=0x%x length=0x%x checksum=0x%x.\n",
701 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
702 chksum));
703 goto no_flash_data;
706 loc = locations[1];
707 cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
708 for ( ; cnt; cnt--, region++) {
709 /* Store addresses as DWORD offsets. */
710 start = le32_to_cpu(region->start) >> 2;
712 DEBUG3(qla_printk(KERN_DEBUG, ha, "FLT[%02x]: start=0x%x "
713 "end=0x%x size=0x%x.\n", le32_to_cpu(region->code), start,
714 le32_to_cpu(region->end) >> 2, le32_to_cpu(region->size)));
716 switch (le32_to_cpu(region->code) & 0xff) {
717 case FLT_REG_FW:
718 ha->flt_region_fw = start;
719 break;
720 case FLT_REG_BOOT_CODE:
721 ha->flt_region_boot = start;
722 break;
723 case FLT_REG_VPD_0:
724 ha->flt_region_vpd_nvram = start;
725 if (IS_QLA82XX(ha))
726 break;
727 if (ha->flags.port0)
728 ha->flt_region_vpd = start;
729 break;
730 case FLT_REG_VPD_1:
731 if (IS_QLA82XX(ha))
732 break;
733 if (!ha->flags.port0)
734 ha->flt_region_vpd = start;
735 break;
736 case FLT_REG_NVRAM_0:
737 if (ha->flags.port0)
738 ha->flt_region_nvram = start;
739 break;
740 case FLT_REG_NVRAM_1:
741 if (!ha->flags.port0)
742 ha->flt_region_nvram = start;
743 break;
744 case FLT_REG_FDT:
745 ha->flt_region_fdt = start;
746 break;
747 case FLT_REG_NPIV_CONF_0:
748 if (ha->flags.port0)
749 ha->flt_region_npiv_conf = start;
750 break;
751 case FLT_REG_NPIV_CONF_1:
752 if (!ha->flags.port0)
753 ha->flt_region_npiv_conf = start;
754 break;
755 case FLT_REG_GOLD_FW:
756 ha->flt_region_gold_fw = start;
757 break;
758 case FLT_REG_FCP_PRIO_0:
759 if (ha->flags.port0)
760 ha->flt_region_fcp_prio = start;
761 break;
762 case FLT_REG_FCP_PRIO_1:
763 if (!ha->flags.port0)
764 ha->flt_region_fcp_prio = start;
765 break;
766 case FLT_REG_BOOT_CODE_82XX:
767 ha->flt_region_boot = start;
768 break;
769 case FLT_REG_FW_82XX:
770 ha->flt_region_fw = start;
771 break;
772 case FLT_REG_GOLD_FW_82XX:
773 ha->flt_region_gold_fw = start;
774 break;
775 case FLT_REG_BOOTLOAD_82XX:
776 ha->flt_region_bootload = start;
777 break;
778 case FLT_REG_VPD_82XX:
779 ha->flt_region_vpd = start;
780 break;
783 goto done;
785 no_flash_data:
786 /* Use hardcoded defaults. */
787 loc = locations[0];
788 ha->flt_region_fw = def_fw[def];
789 ha->flt_region_boot = def_boot[def];
790 ha->flt_region_vpd_nvram = def_vpd_nvram[def];
791 ha->flt_region_vpd = ha->flags.port0 ?
792 def_vpd0[def] : def_vpd1[def];
793 ha->flt_region_nvram = ha->flags.port0 ?
794 def_nvram0[def] : def_nvram1[def];
795 ha->flt_region_fdt = def_fdt[def];
796 ha->flt_region_npiv_conf = ha->flags.port0 ?
797 def_npiv_conf0[def] : def_npiv_conf1[def];
798 done:
799 DEBUG2(qla_printk(KERN_DEBUG, ha, "FLT[%s]: boot=0x%x fw=0x%x "
800 "vpd_nvram=0x%x vpd=0x%x nvram=0x%x fdt=0x%x flt=0x%x "
801 "npiv=0x%x. fcp_prio_cfg=0x%x\n", loc, ha->flt_region_boot,
802 ha->flt_region_fw, ha->flt_region_vpd_nvram, ha->flt_region_vpd,
803 ha->flt_region_nvram, ha->flt_region_fdt, ha->flt_region_flt,
804 ha->flt_region_npiv_conf, ha->flt_region_fcp_prio));
807 static void
808 qla2xxx_get_fdt_info(scsi_qla_host_t *vha)
810 #define FLASH_BLK_SIZE_4K 0x1000
811 #define FLASH_BLK_SIZE_32K 0x8000
812 #define FLASH_BLK_SIZE_64K 0x10000
813 const char *loc, *locations[] = { "MID", "FDT" };
814 uint16_t cnt, chksum;
815 uint16_t *wptr;
816 struct qla_fdt_layout *fdt;
817 uint8_t man_id, flash_id;
818 uint16_t mid = 0, fid = 0;
819 struct qla_hw_data *ha = vha->hw;
820 struct req_que *req = ha->req_q_map[0];
822 wptr = (uint16_t *)req->ring;
823 fdt = (struct qla_fdt_layout *)req->ring;
824 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
825 ha->flt_region_fdt << 2, OPTROM_BURST_SIZE);
826 if (*wptr == __constant_cpu_to_le16(0xffff))
827 goto no_flash_data;
828 if (fdt->sig[0] != 'Q' || fdt->sig[1] != 'L' || fdt->sig[2] != 'I' ||
829 fdt->sig[3] != 'D')
830 goto no_flash_data;
832 for (cnt = 0, chksum = 0; cnt < sizeof(struct qla_fdt_layout) >> 1;
833 cnt++)
834 chksum += le16_to_cpu(*wptr++);
835 if (chksum) {
836 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FDT detected: "
837 "checksum=0x%x id=%c version=0x%x.\n", chksum, fdt->sig[0],
838 le16_to_cpu(fdt->version)));
839 DEBUG9(qla2x00_dump_buffer((uint8_t *)fdt, sizeof(*fdt)));
840 goto no_flash_data;
843 loc = locations[1];
844 mid = le16_to_cpu(fdt->man_id);
845 fid = le16_to_cpu(fdt->id);
846 ha->fdt_wrt_disable = fdt->wrt_disable_bits;
847 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0300 | fdt->erase_cmd);
848 ha->fdt_block_size = le32_to_cpu(fdt->block_size);
849 if (fdt->unprotect_sec_cmd) {
850 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0300 |
851 fdt->unprotect_sec_cmd);
852 ha->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
853 flash_conf_addr(ha, 0x0300 | fdt->protect_sec_cmd):
854 flash_conf_addr(ha, 0x0336);
856 goto done;
857 no_flash_data:
858 loc = locations[0];
859 if (IS_QLA82XX(ha)) {
860 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
861 goto done;
863 qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
864 mid = man_id;
865 fid = flash_id;
866 ha->fdt_wrt_disable = 0x9c;
867 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x03d8);
868 switch (man_id) {
869 case 0xbf: /* STT flash. */
870 if (flash_id == 0x8e)
871 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
872 else
873 ha->fdt_block_size = FLASH_BLK_SIZE_32K;
875 if (flash_id == 0x80)
876 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0352);
877 break;
878 case 0x13: /* ST M25P80. */
879 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
880 break;
881 case 0x1f: /* Atmel 26DF081A. */
882 ha->fdt_block_size = FLASH_BLK_SIZE_4K;
883 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0320);
884 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0339);
885 ha->fdt_protect_sec_cmd = flash_conf_addr(ha, 0x0336);
886 break;
887 default:
888 /* Default to 64 kb sector size. */
889 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
890 break;
892 done:
893 DEBUG2(qla_printk(KERN_DEBUG, ha, "FDT[%s]: (0x%x/0x%x) erase=0x%x "
894 "pro=%x upro=%x wrtd=0x%x blk=0x%x.\n", loc, mid, fid,
895 ha->fdt_erase_cmd, ha->fdt_protect_sec_cmd,
896 ha->fdt_unprotect_sec_cmd, ha->fdt_wrt_disable,
897 ha->fdt_block_size));
900 static void
901 qla2xxx_get_idc_param(scsi_qla_host_t *vha)
903 #define QLA82XX_IDC_PARAM_ADDR 0x003e885c
904 uint32_t *wptr;
905 struct qla_hw_data *ha = vha->hw;
906 struct req_que *req = ha->req_q_map[0];
908 if (!IS_QLA82XX(ha))
909 return;
911 wptr = (uint32_t *)req->ring;
912 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
913 QLA82XX_IDC_PARAM_ADDR , 8);
915 if (*wptr == __constant_cpu_to_le32(0xffffffff)) {
916 ha->nx_dev_init_timeout = QLA82XX_ROM_DEV_INIT_TIMEOUT;
917 ha->nx_reset_timeout = QLA82XX_ROM_DRV_RESET_ACK_TIMEOUT;
918 } else {
919 ha->nx_dev_init_timeout = le32_to_cpu(*wptr++);
920 ha->nx_reset_timeout = le32_to_cpu(*wptr);
922 return;
926 qla2xxx_get_flash_info(scsi_qla_host_t *vha)
928 int ret;
929 uint32_t flt_addr;
930 struct qla_hw_data *ha = vha->hw;
932 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA8XXX_TYPE(ha))
933 return QLA_SUCCESS;
935 ret = qla2xxx_find_flt_start(vha, &flt_addr);
936 if (ret != QLA_SUCCESS)
937 return ret;
939 qla2xxx_get_flt_info(vha, flt_addr);
940 qla2xxx_get_fdt_info(vha);
941 qla2xxx_get_idc_param(vha);
943 return QLA_SUCCESS;
946 void
947 qla2xxx_flash_npiv_conf(scsi_qla_host_t *vha)
949 #define NPIV_CONFIG_SIZE (16*1024)
950 void *data;
951 uint16_t *wptr;
952 uint16_t cnt, chksum;
953 int i;
954 struct qla_npiv_header hdr;
955 struct qla_npiv_entry *entry;
956 struct qla_hw_data *ha = vha->hw;
958 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA8XXX_TYPE(ha))
959 return;
961 ha->isp_ops->read_optrom(vha, (uint8_t *)&hdr,
962 ha->flt_region_npiv_conf << 2, sizeof(struct qla_npiv_header));
963 if (hdr.version == __constant_cpu_to_le16(0xffff))
964 return;
965 if (hdr.version != __constant_cpu_to_le16(1)) {
966 DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported NPIV-Config "
967 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
968 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
969 le16_to_cpu(hdr.checksum)));
970 return;
973 data = kmalloc(NPIV_CONFIG_SIZE, GFP_KERNEL);
974 if (!data) {
975 DEBUG2(qla_printk(KERN_INFO, ha, "NPIV-Config: Unable to "
976 "allocate memory.\n"));
977 return;
980 ha->isp_ops->read_optrom(vha, (uint8_t *)data,
981 ha->flt_region_npiv_conf << 2, NPIV_CONFIG_SIZE);
983 cnt = (sizeof(struct qla_npiv_header) + le16_to_cpu(hdr.entries) *
984 sizeof(struct qla_npiv_entry)) >> 1;
985 for (wptr = data, chksum = 0; cnt; cnt--)
986 chksum += le16_to_cpu(*wptr++);
987 if (chksum) {
988 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent NPIV-Config "
989 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
990 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
991 chksum));
992 goto done;
995 entry = data + sizeof(struct qla_npiv_header);
996 cnt = le16_to_cpu(hdr.entries);
997 for (i = 0; cnt; cnt--, entry++, i++) {
998 uint16_t flags;
999 struct fc_vport_identifiers vid;
1000 struct fc_vport *vport;
1002 memcpy(&ha->npiv_info[i], entry, sizeof(struct qla_npiv_entry));
1004 flags = le16_to_cpu(entry->flags);
1005 if (flags == 0xffff)
1006 continue;
1007 if ((flags & BIT_0) == 0)
1008 continue;
1010 memset(&vid, 0, sizeof(vid));
1011 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1012 vid.vport_type = FC_PORTTYPE_NPIV;
1013 vid.disable = false;
1014 vid.port_name = wwn_to_u64(entry->port_name);
1015 vid.node_name = wwn_to_u64(entry->node_name);
1017 DEBUG2(qla_printk(KERN_INFO, ha, "NPIV[%02x]: wwpn=%llx "
1018 "wwnn=%llx vf_id=0x%x Q_qos=0x%x F_qos=0x%x.\n", cnt,
1019 (unsigned long long)vid.port_name,
1020 (unsigned long long)vid.node_name,
1021 le16_to_cpu(entry->vf_id),
1022 entry->q_qos, entry->f_qos));
1024 if (i < QLA_PRECONFIG_VPORTS) {
1025 vport = fc_vport_create(vha->host, 0, &vid);
1026 if (!vport)
1027 qla_printk(KERN_INFO, ha,
1028 "NPIV-Config: Failed to create vport [%02x]: "
1029 "wwpn=%llx wwnn=%llx.\n", cnt,
1030 (unsigned long long)vid.port_name,
1031 (unsigned long long)vid.node_name);
1034 done:
1035 kfree(data);
1038 static int
1039 qla24xx_unprotect_flash(scsi_qla_host_t *vha)
1041 struct qla_hw_data *ha = vha->hw;
1042 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1044 if (ha->flags.fac_supported)
1045 return qla81xx_fac_do_write_enable(vha, 1);
1047 /* Enable flash write. */
1048 WRT_REG_DWORD(&reg->ctrl_status,
1049 RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
1050 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1052 if (!ha->fdt_wrt_disable)
1053 goto done;
1055 /* Disable flash write-protection, first clear SR protection bit */
1056 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
1057 /* Then write zero again to clear remaining SR bits.*/
1058 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
1059 done:
1060 return QLA_SUCCESS;
1063 static int
1064 qla24xx_protect_flash(scsi_qla_host_t *vha)
1066 uint32_t cnt;
1067 struct qla_hw_data *ha = vha->hw;
1068 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1070 if (ha->flags.fac_supported)
1071 return qla81xx_fac_do_write_enable(vha, 0);
1073 if (!ha->fdt_wrt_disable)
1074 goto skip_wrt_protect;
1076 /* Enable flash write-protection and wait for completion. */
1077 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101),
1078 ha->fdt_wrt_disable);
1079 for (cnt = 300; cnt &&
1080 qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x005)) & BIT_0;
1081 cnt--) {
1082 udelay(10);
1085 skip_wrt_protect:
1086 /* Disable flash write. */
1087 WRT_REG_DWORD(&reg->ctrl_status,
1088 RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
1089 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1091 return QLA_SUCCESS;
1094 static int
1095 qla24xx_erase_sector(scsi_qla_host_t *vha, uint32_t fdata)
1097 struct qla_hw_data *ha = vha->hw;
1098 uint32_t start, finish;
1100 if (ha->flags.fac_supported) {
1101 start = fdata >> 2;
1102 finish = start + (ha->fdt_block_size >> 2) - 1;
1103 return qla81xx_fac_erase_sector(vha, flash_data_addr(ha,
1104 start), flash_data_addr(ha, finish));
1107 return qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
1108 (fdata & 0xff00) | ((fdata << 16) & 0xff0000) |
1109 ((fdata >> 16) & 0xff));
1112 static int
1113 qla24xx_write_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
1114 uint32_t dwords)
1116 int ret;
1117 uint32_t liter;
1118 uint32_t sec_mask, rest_addr;
1119 uint32_t fdata;
1120 dma_addr_t optrom_dma;
1121 void *optrom = NULL;
1122 struct qla_hw_data *ha = vha->hw;
1124 /* Prepare burst-capable write on supported ISPs. */
1125 if ((IS_QLA25XX(ha) || IS_QLA81XX(ha)) && !(faddr & 0xfff) &&
1126 dwords > OPTROM_BURST_DWORDS) {
1127 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
1128 &optrom_dma, GFP_KERNEL);
1129 if (!optrom) {
1130 qla_printk(KERN_DEBUG, ha,
1131 "Unable to allocate memory for optrom burst write "
1132 "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
1136 rest_addr = (ha->fdt_block_size >> 2) - 1;
1137 sec_mask = ~rest_addr;
1139 ret = qla24xx_unprotect_flash(vha);
1140 if (ret != QLA_SUCCESS) {
1141 qla_printk(KERN_WARNING, ha,
1142 "Unable to unprotect flash for update.\n");
1143 goto done;
1146 for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
1147 fdata = (faddr & sec_mask) << 2;
1149 /* Are we at the beginning of a sector? */
1150 if ((faddr & rest_addr) == 0) {
1151 /* Do sector unprotect. */
1152 if (ha->fdt_unprotect_sec_cmd)
1153 qla24xx_write_flash_dword(ha,
1154 ha->fdt_unprotect_sec_cmd,
1155 (fdata & 0xff00) | ((fdata << 16) &
1156 0xff0000) | ((fdata >> 16) & 0xff));
1157 ret = qla24xx_erase_sector(vha, fdata);
1158 if (ret != QLA_SUCCESS) {
1159 DEBUG9(qla_printk(KERN_WARNING, ha,
1160 "Unable to erase sector: address=%x.\n",
1161 faddr));
1162 break;
1166 /* Go with burst-write. */
1167 if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
1168 /* Copy data to DMA'ble buffer. */
1169 memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
1171 ret = qla2x00_load_ram(vha, optrom_dma,
1172 flash_data_addr(ha, faddr),
1173 OPTROM_BURST_DWORDS);
1174 if (ret != QLA_SUCCESS) {
1175 qla_printk(KERN_WARNING, ha,
1176 "Unable to burst-write optrom segment "
1177 "(%x/%x/%llx).\n", ret,
1178 flash_data_addr(ha, faddr),
1179 (unsigned long long)optrom_dma);
1180 qla_printk(KERN_WARNING, ha,
1181 "Reverting to slow-write.\n");
1183 dma_free_coherent(&ha->pdev->dev,
1184 OPTROM_BURST_SIZE, optrom, optrom_dma);
1185 optrom = NULL;
1186 } else {
1187 liter += OPTROM_BURST_DWORDS - 1;
1188 faddr += OPTROM_BURST_DWORDS - 1;
1189 dwptr += OPTROM_BURST_DWORDS - 1;
1190 continue;
1194 ret = qla24xx_write_flash_dword(ha,
1195 flash_data_addr(ha, faddr), cpu_to_le32(*dwptr));
1196 if (ret != QLA_SUCCESS) {
1197 DEBUG9(printk("%s(%ld) Unable to program flash "
1198 "address=%x data=%x.\n", __func__,
1199 vha->host_no, faddr, *dwptr));
1200 break;
1203 /* Do sector protect. */
1204 if (ha->fdt_unprotect_sec_cmd &&
1205 ((faddr & rest_addr) == rest_addr))
1206 qla24xx_write_flash_dword(ha,
1207 ha->fdt_protect_sec_cmd,
1208 (fdata & 0xff00) | ((fdata << 16) &
1209 0xff0000) | ((fdata >> 16) & 0xff));
1212 ret = qla24xx_protect_flash(vha);
1213 if (ret != QLA_SUCCESS)
1214 qla_printk(KERN_WARNING, ha,
1215 "Unable to protect flash after update.\n");
1216 done:
1217 if (optrom)
1218 dma_free_coherent(&ha->pdev->dev,
1219 OPTROM_BURST_SIZE, optrom, optrom_dma);
1221 return ret;
1224 uint8_t *
1225 qla2x00_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1226 uint32_t bytes)
1228 uint32_t i;
1229 uint16_t *wptr;
1230 struct qla_hw_data *ha = vha->hw;
1232 /* Word reads to NVRAM via registers. */
1233 wptr = (uint16_t *)buf;
1234 qla2x00_lock_nvram_access(ha);
1235 for (i = 0; i < bytes >> 1; i++, naddr++)
1236 wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
1237 naddr));
1238 qla2x00_unlock_nvram_access(ha);
1240 return buf;
1243 uint8_t *
1244 qla24xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1245 uint32_t bytes)
1247 uint32_t i;
1248 uint32_t *dwptr;
1249 struct qla_hw_data *ha = vha->hw;
1251 if (IS_QLA82XX(ha))
1252 return buf;
1254 /* Dword reads to flash. */
1255 dwptr = (uint32_t *)buf;
1256 for (i = 0; i < bytes >> 2; i++, naddr++)
1257 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
1258 nvram_data_addr(ha, naddr)));
1260 return buf;
1264 qla2x00_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1265 uint32_t bytes)
1267 int ret, stat;
1268 uint32_t i;
1269 uint16_t *wptr;
1270 unsigned long flags;
1271 struct qla_hw_data *ha = vha->hw;
1273 ret = QLA_SUCCESS;
1275 spin_lock_irqsave(&ha->hardware_lock, flags);
1276 qla2x00_lock_nvram_access(ha);
1278 /* Disable NVRAM write-protection. */
1279 stat = qla2x00_clear_nvram_protection(ha);
1281 wptr = (uint16_t *)buf;
1282 for (i = 0; i < bytes >> 1; i++, naddr++) {
1283 qla2x00_write_nvram_word(ha, naddr,
1284 cpu_to_le16(*wptr));
1285 wptr++;
1288 /* Enable NVRAM write-protection. */
1289 qla2x00_set_nvram_protection(ha, stat);
1291 qla2x00_unlock_nvram_access(ha);
1292 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1294 return ret;
1298 qla24xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1299 uint32_t bytes)
1301 int ret;
1302 uint32_t i;
1303 uint32_t *dwptr;
1304 struct qla_hw_data *ha = vha->hw;
1305 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1307 ret = QLA_SUCCESS;
1309 if (IS_QLA82XX(ha))
1310 return ret;
1312 /* Enable flash write. */
1313 WRT_REG_DWORD(&reg->ctrl_status,
1314 RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
1315 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1317 /* Disable NVRAM write-protection. */
1318 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
1319 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
1321 /* Dword writes to flash. */
1322 dwptr = (uint32_t *)buf;
1323 for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
1324 ret = qla24xx_write_flash_dword(ha,
1325 nvram_data_addr(ha, naddr), cpu_to_le32(*dwptr));
1326 if (ret != QLA_SUCCESS) {
1327 DEBUG9(qla_printk(KERN_WARNING, ha,
1328 "Unable to program nvram address=%x data=%x.\n",
1329 naddr, *dwptr));
1330 break;
1334 /* Enable NVRAM write-protection. */
1335 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0x8c);
1337 /* Disable flash write. */
1338 WRT_REG_DWORD(&reg->ctrl_status,
1339 RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
1340 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1342 return ret;
1345 uint8_t *
1346 qla25xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1347 uint32_t bytes)
1349 uint32_t i;
1350 uint32_t *dwptr;
1351 struct qla_hw_data *ha = vha->hw;
1353 /* Dword reads to flash. */
1354 dwptr = (uint32_t *)buf;
1355 for (i = 0; i < bytes >> 2; i++, naddr++)
1356 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
1357 flash_data_addr(ha, ha->flt_region_vpd_nvram | naddr)));
1359 return buf;
1363 qla25xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1364 uint32_t bytes)
1366 struct qla_hw_data *ha = vha->hw;
1367 #define RMW_BUFFER_SIZE (64 * 1024)
1368 uint8_t *dbuf;
1370 dbuf = vmalloc(RMW_BUFFER_SIZE);
1371 if (!dbuf)
1372 return QLA_MEMORY_ALLOC_FAILED;
1373 ha->isp_ops->read_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
1374 RMW_BUFFER_SIZE);
1375 memcpy(dbuf + (naddr << 2), buf, bytes);
1376 ha->isp_ops->write_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
1377 RMW_BUFFER_SIZE);
1378 vfree(dbuf);
1380 return QLA_SUCCESS;
1383 static inline void
1384 qla2x00_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
1386 if (IS_QLA2322(ha)) {
1387 /* Flip all colors. */
1388 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1389 /* Turn off. */
1390 ha->beacon_color_state = 0;
1391 *pflags = GPIO_LED_ALL_OFF;
1392 } else {
1393 /* Turn on. */
1394 ha->beacon_color_state = QLA_LED_ALL_ON;
1395 *pflags = GPIO_LED_RGA_ON;
1397 } else {
1398 /* Flip green led only. */
1399 if (ha->beacon_color_state == QLA_LED_GRN_ON) {
1400 /* Turn off. */
1401 ha->beacon_color_state = 0;
1402 *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
1403 } else {
1404 /* Turn on. */
1405 ha->beacon_color_state = QLA_LED_GRN_ON;
1406 *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
1411 #define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
1413 void
1414 qla2x00_beacon_blink(struct scsi_qla_host *vha)
1416 uint16_t gpio_enable;
1417 uint16_t gpio_data;
1418 uint16_t led_color = 0;
1419 unsigned long flags;
1420 struct qla_hw_data *ha = vha->hw;
1421 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1423 if (IS_QLA82XX(ha))
1424 return;
1426 spin_lock_irqsave(&ha->hardware_lock, flags);
1428 /* Save the Original GPIOE. */
1429 if (ha->pio_address) {
1430 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1431 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
1432 } else {
1433 gpio_enable = RD_REG_WORD(&reg->gpioe);
1434 gpio_data = RD_REG_WORD(&reg->gpiod);
1437 /* Set the modified gpio_enable values */
1438 gpio_enable |= GPIO_LED_MASK;
1440 if (ha->pio_address) {
1441 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
1442 } else {
1443 WRT_REG_WORD(&reg->gpioe, gpio_enable);
1444 RD_REG_WORD(&reg->gpioe);
1447 qla2x00_flip_colors(ha, &led_color);
1449 /* Clear out any previously set LED color. */
1450 gpio_data &= ~GPIO_LED_MASK;
1452 /* Set the new input LED color to GPIOD. */
1453 gpio_data |= led_color;
1455 /* Set the modified gpio_data values */
1456 if (ha->pio_address) {
1457 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
1458 } else {
1459 WRT_REG_WORD(&reg->gpiod, gpio_data);
1460 RD_REG_WORD(&reg->gpiod);
1463 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1467 qla2x00_beacon_on(struct scsi_qla_host *vha)
1469 uint16_t gpio_enable;
1470 uint16_t gpio_data;
1471 unsigned long flags;
1472 struct qla_hw_data *ha = vha->hw;
1473 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1475 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1476 ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
1478 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
1479 qla_printk(KERN_WARNING, ha,
1480 "Unable to update fw options (beacon on).\n");
1481 return QLA_FUNCTION_FAILED;
1484 /* Turn off LEDs. */
1485 spin_lock_irqsave(&ha->hardware_lock, flags);
1486 if (ha->pio_address) {
1487 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1488 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
1489 } else {
1490 gpio_enable = RD_REG_WORD(&reg->gpioe);
1491 gpio_data = RD_REG_WORD(&reg->gpiod);
1493 gpio_enable |= GPIO_LED_MASK;
1495 /* Set the modified gpio_enable values. */
1496 if (ha->pio_address) {
1497 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
1498 } else {
1499 WRT_REG_WORD(&reg->gpioe, gpio_enable);
1500 RD_REG_WORD(&reg->gpioe);
1503 /* Clear out previously set LED colour. */
1504 gpio_data &= ~GPIO_LED_MASK;
1505 if (ha->pio_address) {
1506 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
1507 } else {
1508 WRT_REG_WORD(&reg->gpiod, gpio_data);
1509 RD_REG_WORD(&reg->gpiod);
1511 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1514 * Let the per HBA timer kick off the blinking process based on
1515 * the following flags. No need to do anything else now.
1517 ha->beacon_blink_led = 1;
1518 ha->beacon_color_state = 0;
1520 return QLA_SUCCESS;
1524 qla2x00_beacon_off(struct scsi_qla_host *vha)
1526 int rval = QLA_SUCCESS;
1527 struct qla_hw_data *ha = vha->hw;
1529 ha->beacon_blink_led = 0;
1531 /* Set the on flag so when it gets flipped it will be off. */
1532 if (IS_QLA2322(ha))
1533 ha->beacon_color_state = QLA_LED_ALL_ON;
1534 else
1535 ha->beacon_color_state = QLA_LED_GRN_ON;
1537 ha->isp_ops->beacon_blink(vha); /* This turns green LED off */
1539 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1540 ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
1542 rval = qla2x00_set_fw_options(vha, ha->fw_options);
1543 if (rval != QLA_SUCCESS)
1544 qla_printk(KERN_WARNING, ha,
1545 "Unable to update fw options (beacon off).\n");
1546 return rval;
1550 static inline void
1551 qla24xx_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
1553 /* Flip all colors. */
1554 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1555 /* Turn off. */
1556 ha->beacon_color_state = 0;
1557 *pflags = 0;
1558 } else {
1559 /* Turn on. */
1560 ha->beacon_color_state = QLA_LED_ALL_ON;
1561 *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
1565 void
1566 qla24xx_beacon_blink(struct scsi_qla_host *vha)
1568 uint16_t led_color = 0;
1569 uint32_t gpio_data;
1570 unsigned long flags;
1571 struct qla_hw_data *ha = vha->hw;
1572 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1574 /* Save the Original GPIOD. */
1575 spin_lock_irqsave(&ha->hardware_lock, flags);
1576 gpio_data = RD_REG_DWORD(&reg->gpiod);
1578 /* Enable the gpio_data reg for update. */
1579 gpio_data |= GPDX_LED_UPDATE_MASK;
1581 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1582 gpio_data = RD_REG_DWORD(&reg->gpiod);
1584 /* Set the color bits. */
1585 qla24xx_flip_colors(ha, &led_color);
1587 /* Clear out any previously set LED color. */
1588 gpio_data &= ~GPDX_LED_COLOR_MASK;
1590 /* Set the new input LED color to GPIOD. */
1591 gpio_data |= led_color;
1593 /* Set the modified gpio_data values. */
1594 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1595 gpio_data = RD_REG_DWORD(&reg->gpiod);
1596 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1600 qla24xx_beacon_on(struct scsi_qla_host *vha)
1602 uint32_t gpio_data;
1603 unsigned long flags;
1604 struct qla_hw_data *ha = vha->hw;
1605 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1607 if (IS_QLA82XX(ha))
1608 return QLA_SUCCESS;
1610 if (ha->beacon_blink_led == 0) {
1611 /* Enable firmware for update */
1612 ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
1614 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS)
1615 return QLA_FUNCTION_FAILED;
1617 if (qla2x00_get_fw_options(vha, ha->fw_options) !=
1618 QLA_SUCCESS) {
1619 qla_printk(KERN_WARNING, ha,
1620 "Unable to update fw options (beacon on).\n");
1621 return QLA_FUNCTION_FAILED;
1624 spin_lock_irqsave(&ha->hardware_lock, flags);
1625 gpio_data = RD_REG_DWORD(&reg->gpiod);
1627 /* Enable the gpio_data reg for update. */
1628 gpio_data |= GPDX_LED_UPDATE_MASK;
1629 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1630 RD_REG_DWORD(&reg->gpiod);
1632 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1635 /* So all colors blink together. */
1636 ha->beacon_color_state = 0;
1638 /* Let the per HBA timer kick off the blinking process. */
1639 ha->beacon_blink_led = 1;
1641 return QLA_SUCCESS;
1645 qla24xx_beacon_off(struct scsi_qla_host *vha)
1647 uint32_t gpio_data;
1648 unsigned long flags;
1649 struct qla_hw_data *ha = vha->hw;
1650 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1652 if (IS_QLA82XX(ha))
1653 return QLA_SUCCESS;
1655 ha->beacon_blink_led = 0;
1656 ha->beacon_color_state = QLA_LED_ALL_ON;
1658 ha->isp_ops->beacon_blink(vha); /* Will flip to all off. */
1660 /* Give control back to firmware. */
1661 spin_lock_irqsave(&ha->hardware_lock, flags);
1662 gpio_data = RD_REG_DWORD(&reg->gpiod);
1664 /* Disable the gpio_data reg for update. */
1665 gpio_data &= ~GPDX_LED_UPDATE_MASK;
1666 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1667 RD_REG_DWORD(&reg->gpiod);
1668 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1670 ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
1672 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
1673 qla_printk(KERN_WARNING, ha,
1674 "Unable to update fw options (beacon off).\n");
1675 return QLA_FUNCTION_FAILED;
1678 if (qla2x00_get_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
1679 qla_printk(KERN_WARNING, ha,
1680 "Unable to get fw options (beacon off).\n");
1681 return QLA_FUNCTION_FAILED;
1684 return QLA_SUCCESS;
1689 * Flash support routines
1693 * qla2x00_flash_enable() - Setup flash for reading and writing.
1694 * @ha: HA context
1696 static void
1697 qla2x00_flash_enable(struct qla_hw_data *ha)
1699 uint16_t data;
1700 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1702 data = RD_REG_WORD(&reg->ctrl_status);
1703 data |= CSR_FLASH_ENABLE;
1704 WRT_REG_WORD(&reg->ctrl_status, data);
1705 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1709 * qla2x00_flash_disable() - Disable flash and allow RISC to run.
1710 * @ha: HA context
1712 static void
1713 qla2x00_flash_disable(struct qla_hw_data *ha)
1715 uint16_t data;
1716 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1718 data = RD_REG_WORD(&reg->ctrl_status);
1719 data &= ~(CSR_FLASH_ENABLE);
1720 WRT_REG_WORD(&reg->ctrl_status, data);
1721 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1725 * qla2x00_read_flash_byte() - Reads a byte from flash
1726 * @ha: HA context
1727 * @addr: Address in flash to read
1729 * A word is read from the chip, but, only the lower byte is valid.
1731 * Returns the byte read from flash @addr.
1733 static uint8_t
1734 qla2x00_read_flash_byte(struct qla_hw_data *ha, uint32_t addr)
1736 uint16_t data;
1737 uint16_t bank_select;
1738 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1740 bank_select = RD_REG_WORD(&reg->ctrl_status);
1742 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1743 /* Specify 64K address range: */
1744 /* clear out Module Select and Flash Address bits [19:16]. */
1745 bank_select &= ~0xf8;
1746 bank_select |= addr >> 12 & 0xf0;
1747 bank_select |= CSR_FLASH_64K_BANK;
1748 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1749 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1751 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1752 data = RD_REG_WORD(&reg->flash_data);
1754 return (uint8_t)data;
1757 /* Setup bit 16 of flash address. */
1758 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1759 bank_select |= CSR_FLASH_64K_BANK;
1760 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1761 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1762 } else if (((addr & BIT_16) == 0) &&
1763 (bank_select & CSR_FLASH_64K_BANK)) {
1764 bank_select &= ~(CSR_FLASH_64K_BANK);
1765 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1766 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1769 /* Always perform IO mapped accesses to the FLASH registers. */
1770 if (ha->pio_address) {
1771 uint16_t data2;
1773 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
1774 do {
1775 data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
1776 barrier();
1777 cpu_relax();
1778 data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
1779 } while (data != data2);
1780 } else {
1781 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1782 data = qla2x00_debounce_register(&reg->flash_data);
1785 return (uint8_t)data;
1789 * qla2x00_write_flash_byte() - Write a byte to flash
1790 * @ha: HA context
1791 * @addr: Address in flash to write
1792 * @data: Data to write
1794 static void
1795 qla2x00_write_flash_byte(struct qla_hw_data *ha, uint32_t addr, uint8_t data)
1797 uint16_t bank_select;
1798 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1800 bank_select = RD_REG_WORD(&reg->ctrl_status);
1801 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1802 /* Specify 64K address range: */
1803 /* clear out Module Select and Flash Address bits [19:16]. */
1804 bank_select &= ~0xf8;
1805 bank_select |= addr >> 12 & 0xf0;
1806 bank_select |= CSR_FLASH_64K_BANK;
1807 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1808 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1810 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1811 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1812 WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
1813 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1815 return;
1818 /* Setup bit 16 of flash address. */
1819 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1820 bank_select |= CSR_FLASH_64K_BANK;
1821 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1822 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1823 } else if (((addr & BIT_16) == 0) &&
1824 (bank_select & CSR_FLASH_64K_BANK)) {
1825 bank_select &= ~(CSR_FLASH_64K_BANK);
1826 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1827 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1830 /* Always perform IO mapped accesses to the FLASH registers. */
1831 if (ha->pio_address) {
1832 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
1833 WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
1834 } else {
1835 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1836 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1837 WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
1838 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1843 * qla2x00_poll_flash() - Polls flash for completion.
1844 * @ha: HA context
1845 * @addr: Address in flash to poll
1846 * @poll_data: Data to be polled
1847 * @man_id: Flash manufacturer ID
1848 * @flash_id: Flash ID
1850 * This function polls the device until bit 7 of what is read matches data
1851 * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
1852 * out (a fatal error). The flash book recommeds reading bit 7 again after
1853 * reading bit 5 as a 1.
1855 * Returns 0 on success, else non-zero.
1857 static int
1858 qla2x00_poll_flash(struct qla_hw_data *ha, uint32_t addr, uint8_t poll_data,
1859 uint8_t man_id, uint8_t flash_id)
1861 int status;
1862 uint8_t flash_data;
1863 uint32_t cnt;
1865 status = 1;
1867 /* Wait for 30 seconds for command to finish. */
1868 poll_data &= BIT_7;
1869 for (cnt = 3000000; cnt; cnt--) {
1870 flash_data = qla2x00_read_flash_byte(ha, addr);
1871 if ((flash_data & BIT_7) == poll_data) {
1872 status = 0;
1873 break;
1876 if (man_id != 0x40 && man_id != 0xda) {
1877 if ((flash_data & BIT_5) && cnt > 2)
1878 cnt = 2;
1880 udelay(10);
1881 barrier();
1882 cond_resched();
1884 return status;
1888 * qla2x00_program_flash_address() - Programs a flash address
1889 * @ha: HA context
1890 * @addr: Address in flash to program
1891 * @data: Data to be written in flash
1892 * @man_id: Flash manufacturer ID
1893 * @flash_id: Flash ID
1895 * Returns 0 on success, else non-zero.
1897 static int
1898 qla2x00_program_flash_address(struct qla_hw_data *ha, uint32_t addr,
1899 uint8_t data, uint8_t man_id, uint8_t flash_id)
1901 /* Write Program Command Sequence. */
1902 if (IS_OEM_001(ha)) {
1903 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1904 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1905 qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
1906 qla2x00_write_flash_byte(ha, addr, data);
1907 } else {
1908 if (man_id == 0xda && flash_id == 0xc1) {
1909 qla2x00_write_flash_byte(ha, addr, data);
1910 if (addr & 0x7e)
1911 return 0;
1912 } else {
1913 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1914 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1915 qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
1916 qla2x00_write_flash_byte(ha, addr, data);
1920 udelay(150);
1922 /* Wait for write to complete. */
1923 return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
1927 * qla2x00_erase_flash() - Erase the flash.
1928 * @ha: HA context
1929 * @man_id: Flash manufacturer ID
1930 * @flash_id: Flash ID
1932 * Returns 0 on success, else non-zero.
1934 static int
1935 qla2x00_erase_flash(struct qla_hw_data *ha, uint8_t man_id, uint8_t flash_id)
1937 /* Individual Sector Erase Command Sequence */
1938 if (IS_OEM_001(ha)) {
1939 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1940 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1941 qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
1942 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1943 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1944 qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
1945 } else {
1946 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1947 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1948 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
1949 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1950 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1951 qla2x00_write_flash_byte(ha, 0x5555, 0x10);
1954 udelay(150);
1956 /* Wait for erase to complete. */
1957 return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
1961 * qla2x00_erase_flash_sector() - Erase a flash sector.
1962 * @ha: HA context
1963 * @addr: Flash sector to erase
1964 * @sec_mask: Sector address mask
1965 * @man_id: Flash manufacturer ID
1966 * @flash_id: Flash ID
1968 * Returns 0 on success, else non-zero.
1970 static int
1971 qla2x00_erase_flash_sector(struct qla_hw_data *ha, uint32_t addr,
1972 uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
1974 /* Individual Sector Erase Command Sequence */
1975 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1976 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1977 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
1978 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1979 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1980 if (man_id == 0x1f && flash_id == 0x13)
1981 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
1982 else
1983 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
1985 udelay(150);
1987 /* Wait for erase to complete. */
1988 return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
1992 * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
1993 * @man_id: Flash manufacturer ID
1994 * @flash_id: Flash ID
1996 static void
1997 qla2x00_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
1998 uint8_t *flash_id)
2000 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2001 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2002 qla2x00_write_flash_byte(ha, 0x5555, 0x90);
2003 *man_id = qla2x00_read_flash_byte(ha, 0x0000);
2004 *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
2005 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2006 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2007 qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
2010 static void
2011 qla2x00_read_flash_data(struct qla_hw_data *ha, uint8_t *tmp_buf,
2012 uint32_t saddr, uint32_t length)
2014 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2015 uint32_t midpoint, ilength;
2016 uint8_t data;
2018 midpoint = length / 2;
2020 WRT_REG_WORD(&reg->nvram, 0);
2021 RD_REG_WORD(&reg->nvram);
2022 for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
2023 if (ilength == midpoint) {
2024 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2025 RD_REG_WORD(&reg->nvram);
2027 data = qla2x00_read_flash_byte(ha, saddr);
2028 if (saddr % 100)
2029 udelay(10);
2030 *tmp_buf = data;
2031 cond_resched();
2035 static inline void
2036 qla2x00_suspend_hba(struct scsi_qla_host *vha)
2038 int cnt;
2039 unsigned long flags;
2040 struct qla_hw_data *ha = vha->hw;
2041 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2043 /* Suspend HBA. */
2044 scsi_block_requests(vha->host);
2045 ha->isp_ops->disable_intrs(ha);
2046 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2048 /* Pause RISC. */
2049 spin_lock_irqsave(&ha->hardware_lock, flags);
2050 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2051 RD_REG_WORD(&reg->hccr);
2052 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2053 for (cnt = 0; cnt < 30000; cnt++) {
2054 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
2055 break;
2056 udelay(100);
2058 } else {
2059 udelay(10);
2061 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2064 static inline void
2065 qla2x00_resume_hba(struct scsi_qla_host *vha)
2067 struct qla_hw_data *ha = vha->hw;
2069 /* Resume HBA. */
2070 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2071 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2072 qla2xxx_wake_dpc(vha);
2073 qla2x00_wait_for_chip_reset(vha);
2074 scsi_unblock_requests(vha->host);
2077 uint8_t *
2078 qla2x00_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2079 uint32_t offset, uint32_t length)
2081 uint32_t addr, midpoint;
2082 uint8_t *data;
2083 struct qla_hw_data *ha = vha->hw;
2084 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2086 /* Suspend HBA. */
2087 qla2x00_suspend_hba(vha);
2089 /* Go with read. */
2090 midpoint = ha->optrom_size / 2;
2092 qla2x00_flash_enable(ha);
2093 WRT_REG_WORD(&reg->nvram, 0);
2094 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
2095 for (addr = offset, data = buf; addr < length; addr++, data++) {
2096 if (addr == midpoint) {
2097 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2098 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
2101 *data = qla2x00_read_flash_byte(ha, addr);
2103 qla2x00_flash_disable(ha);
2105 /* Resume HBA. */
2106 qla2x00_resume_hba(vha);
2108 return buf;
2112 qla2x00_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2113 uint32_t offset, uint32_t length)
2116 int rval;
2117 uint8_t man_id, flash_id, sec_number, data;
2118 uint16_t wd;
2119 uint32_t addr, liter, sec_mask, rest_addr;
2120 struct qla_hw_data *ha = vha->hw;
2121 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2123 /* Suspend HBA. */
2124 qla2x00_suspend_hba(vha);
2126 rval = QLA_SUCCESS;
2127 sec_number = 0;
2129 /* Reset ISP chip. */
2130 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2131 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
2133 /* Go with write. */
2134 qla2x00_flash_enable(ha);
2135 do { /* Loop once to provide quick error exit */
2136 /* Structure of flash memory based on manufacturer */
2137 if (IS_OEM_001(ha)) {
2138 /* OEM variant with special flash part. */
2139 man_id = flash_id = 0;
2140 rest_addr = 0xffff;
2141 sec_mask = 0x10000;
2142 goto update_flash;
2144 qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
2145 switch (man_id) {
2146 case 0x20: /* ST flash. */
2147 if (flash_id == 0xd2 || flash_id == 0xe3) {
2149 * ST m29w008at part - 64kb sector size with
2150 * 32kb,8kb,8kb,16kb sectors at memory address
2151 * 0xf0000.
2153 rest_addr = 0xffff;
2154 sec_mask = 0x10000;
2155 break;
2158 * ST m29w010b part - 16kb sector size
2159 * Default to 16kb sectors
2161 rest_addr = 0x3fff;
2162 sec_mask = 0x1c000;
2163 break;
2164 case 0x40: /* Mostel flash. */
2165 /* Mostel v29c51001 part - 512 byte sector size. */
2166 rest_addr = 0x1ff;
2167 sec_mask = 0x1fe00;
2168 break;
2169 case 0xbf: /* SST flash. */
2170 /* SST39sf10 part - 4kb sector size. */
2171 rest_addr = 0xfff;
2172 sec_mask = 0x1f000;
2173 break;
2174 case 0xda: /* Winbond flash. */
2175 /* Winbond W29EE011 part - 256 byte sector size. */
2176 rest_addr = 0x7f;
2177 sec_mask = 0x1ff80;
2178 break;
2179 case 0xc2: /* Macronix flash. */
2180 /* 64k sector size. */
2181 if (flash_id == 0x38 || flash_id == 0x4f) {
2182 rest_addr = 0xffff;
2183 sec_mask = 0x10000;
2184 break;
2186 /* Fall through... */
2188 case 0x1f: /* Atmel flash. */
2189 /* 512k sector size. */
2190 if (flash_id == 0x13) {
2191 rest_addr = 0x7fffffff;
2192 sec_mask = 0x80000000;
2193 break;
2195 /* Fall through... */
2197 case 0x01: /* AMD flash. */
2198 if (flash_id == 0x38 || flash_id == 0x40 ||
2199 flash_id == 0x4f) {
2200 /* Am29LV081 part - 64kb sector size. */
2201 /* Am29LV002BT part - 64kb sector size. */
2202 rest_addr = 0xffff;
2203 sec_mask = 0x10000;
2204 break;
2205 } else if (flash_id == 0x3e) {
2207 * Am29LV008b part - 64kb sector size with
2208 * 32kb,8kb,8kb,16kb sector at memory address
2209 * h0xf0000.
2211 rest_addr = 0xffff;
2212 sec_mask = 0x10000;
2213 break;
2214 } else if (flash_id == 0x20 || flash_id == 0x6e) {
2216 * Am29LV010 part or AM29f010 - 16kb sector
2217 * size.
2219 rest_addr = 0x3fff;
2220 sec_mask = 0x1c000;
2221 break;
2222 } else if (flash_id == 0x6d) {
2223 /* Am29LV001 part - 8kb sector size. */
2224 rest_addr = 0x1fff;
2225 sec_mask = 0x1e000;
2226 break;
2228 default:
2229 /* Default to 16 kb sector size. */
2230 rest_addr = 0x3fff;
2231 sec_mask = 0x1c000;
2232 break;
2235 update_flash:
2236 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2237 if (qla2x00_erase_flash(ha, man_id, flash_id)) {
2238 rval = QLA_FUNCTION_FAILED;
2239 break;
2243 for (addr = offset, liter = 0; liter < length; liter++,
2244 addr++) {
2245 data = buf[liter];
2246 /* Are we at the beginning of a sector? */
2247 if ((addr & rest_addr) == 0) {
2248 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2249 if (addr >= 0x10000UL) {
2250 if (((addr >> 12) & 0xf0) &&
2251 ((man_id == 0x01 &&
2252 flash_id == 0x3e) ||
2253 (man_id == 0x20 &&
2254 flash_id == 0xd2))) {
2255 sec_number++;
2256 if (sec_number == 1) {
2257 rest_addr =
2258 0x7fff;
2259 sec_mask =
2260 0x18000;
2261 } else if (
2262 sec_number == 2 ||
2263 sec_number == 3) {
2264 rest_addr =
2265 0x1fff;
2266 sec_mask =
2267 0x1e000;
2268 } else if (
2269 sec_number == 4) {
2270 rest_addr =
2271 0x3fff;
2272 sec_mask =
2273 0x1c000;
2277 } else if (addr == ha->optrom_size / 2) {
2278 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2279 RD_REG_WORD(&reg->nvram);
2282 if (flash_id == 0xda && man_id == 0xc1) {
2283 qla2x00_write_flash_byte(ha, 0x5555,
2284 0xaa);
2285 qla2x00_write_flash_byte(ha, 0x2aaa,
2286 0x55);
2287 qla2x00_write_flash_byte(ha, 0x5555,
2288 0xa0);
2289 } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
2290 /* Then erase it */
2291 if (qla2x00_erase_flash_sector(ha,
2292 addr, sec_mask, man_id,
2293 flash_id)) {
2294 rval = QLA_FUNCTION_FAILED;
2295 break;
2297 if (man_id == 0x01 && flash_id == 0x6d)
2298 sec_number++;
2302 if (man_id == 0x01 && flash_id == 0x6d) {
2303 if (sec_number == 1 &&
2304 addr == (rest_addr - 1)) {
2305 rest_addr = 0x0fff;
2306 sec_mask = 0x1f000;
2307 } else if (sec_number == 3 && (addr & 0x7ffe)) {
2308 rest_addr = 0x3fff;
2309 sec_mask = 0x1c000;
2313 if (qla2x00_program_flash_address(ha, addr, data,
2314 man_id, flash_id)) {
2315 rval = QLA_FUNCTION_FAILED;
2316 break;
2318 cond_resched();
2320 } while (0);
2321 qla2x00_flash_disable(ha);
2323 /* Resume HBA. */
2324 qla2x00_resume_hba(vha);
2326 return rval;
2329 uint8_t *
2330 qla24xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2331 uint32_t offset, uint32_t length)
2333 struct qla_hw_data *ha = vha->hw;
2335 /* Suspend HBA. */
2336 scsi_block_requests(vha->host);
2337 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2339 /* Go with read. */
2340 qla24xx_read_flash_data(vha, (uint32_t *)buf, offset >> 2, length >> 2);
2342 /* Resume HBA. */
2343 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2344 scsi_unblock_requests(vha->host);
2346 return buf;
2350 qla24xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2351 uint32_t offset, uint32_t length)
2353 int rval;
2354 struct qla_hw_data *ha = vha->hw;
2356 /* Suspend HBA. */
2357 scsi_block_requests(vha->host);
2358 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2360 /* Go with write. */
2361 rval = qla24xx_write_flash_data(vha, (uint32_t *)buf, offset >> 2,
2362 length >> 2);
2364 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2365 scsi_unblock_requests(vha->host);
2367 return rval;
2370 uint8_t *
2371 qla25xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2372 uint32_t offset, uint32_t length)
2374 int rval;
2375 dma_addr_t optrom_dma;
2376 void *optrom;
2377 uint8_t *pbuf;
2378 uint32_t faddr, left, burst;
2379 struct qla_hw_data *ha = vha->hw;
2381 if (IS_QLA25XX(ha) || IS_QLA81XX(ha))
2382 goto try_fast;
2383 if (offset & 0xfff)
2384 goto slow_read;
2385 if (length < OPTROM_BURST_SIZE)
2386 goto slow_read;
2388 try_fast:
2389 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2390 &optrom_dma, GFP_KERNEL);
2391 if (!optrom) {
2392 qla_printk(KERN_DEBUG, ha,
2393 "Unable to allocate memory for optrom burst read "
2394 "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
2396 goto slow_read;
2399 pbuf = buf;
2400 faddr = offset >> 2;
2401 left = length >> 2;
2402 burst = OPTROM_BURST_DWORDS;
2403 while (left != 0) {
2404 if (burst > left)
2405 burst = left;
2407 rval = qla2x00_dump_ram(vha, optrom_dma,
2408 flash_data_addr(ha, faddr), burst);
2409 if (rval) {
2410 qla_printk(KERN_WARNING, ha,
2411 "Unable to burst-read optrom segment "
2412 "(%x/%x/%llx).\n", rval,
2413 flash_data_addr(ha, faddr),
2414 (unsigned long long)optrom_dma);
2415 qla_printk(KERN_WARNING, ha,
2416 "Reverting to slow-read.\n");
2418 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2419 optrom, optrom_dma);
2420 goto slow_read;
2423 memcpy(pbuf, optrom, burst * 4);
2425 left -= burst;
2426 faddr += burst;
2427 pbuf += burst * 4;
2430 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
2431 optrom_dma);
2433 return buf;
2435 slow_read:
2436 return qla24xx_read_optrom_data(vha, buf, offset, length);
2440 * qla2x00_get_fcode_version() - Determine an FCODE image's version.
2441 * @ha: HA context
2442 * @pcids: Pointer to the FCODE PCI data structure
2444 * The process of retrieving the FCODE version information is at best
2445 * described as interesting.
2447 * Within the first 100h bytes of the image an ASCII string is present
2448 * which contains several pieces of information including the FCODE
2449 * version. Unfortunately it seems the only reliable way to retrieve
2450 * the version is by scanning for another sentinel within the string,
2451 * the FCODE build date:
2453 * ... 2.00.02 10/17/02 ...
2455 * Returns QLA_SUCCESS on successful retrieval of version.
2457 static void
2458 qla2x00_get_fcode_version(struct qla_hw_data *ha, uint32_t pcids)
2460 int ret = QLA_FUNCTION_FAILED;
2461 uint32_t istart, iend, iter, vend;
2462 uint8_t do_next, rbyte, *vbyte;
2464 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2466 /* Skip the PCI data structure. */
2467 istart = pcids +
2468 ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
2469 qla2x00_read_flash_byte(ha, pcids + 0x0A));
2470 iend = istart + 0x100;
2471 do {
2472 /* Scan for the sentinel date string...eeewww. */
2473 do_next = 0;
2474 iter = istart;
2475 while ((iter < iend) && !do_next) {
2476 iter++;
2477 if (qla2x00_read_flash_byte(ha, iter) == '/') {
2478 if (qla2x00_read_flash_byte(ha, iter + 2) ==
2479 '/')
2480 do_next++;
2481 else if (qla2x00_read_flash_byte(ha,
2482 iter + 3) == '/')
2483 do_next++;
2486 if (!do_next)
2487 break;
2489 /* Backtrack to previous ' ' (space). */
2490 do_next = 0;
2491 while ((iter > istart) && !do_next) {
2492 iter--;
2493 if (qla2x00_read_flash_byte(ha, iter) == ' ')
2494 do_next++;
2496 if (!do_next)
2497 break;
2500 * Mark end of version tag, and find previous ' ' (space) or
2501 * string length (recent FCODE images -- major hack ahead!!!).
2503 vend = iter - 1;
2504 do_next = 0;
2505 while ((iter > istart) && !do_next) {
2506 iter--;
2507 rbyte = qla2x00_read_flash_byte(ha, iter);
2508 if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
2509 do_next++;
2511 if (!do_next)
2512 break;
2514 /* Mark beginning of version tag, and copy data. */
2515 iter++;
2516 if ((vend - iter) &&
2517 ((vend - iter) < sizeof(ha->fcode_revision))) {
2518 vbyte = ha->fcode_revision;
2519 while (iter <= vend) {
2520 *vbyte++ = qla2x00_read_flash_byte(ha, iter);
2521 iter++;
2523 ret = QLA_SUCCESS;
2525 } while (0);
2527 if (ret != QLA_SUCCESS)
2528 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2532 qla2x00_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
2534 int ret = QLA_SUCCESS;
2535 uint8_t code_type, last_image;
2536 uint32_t pcihdr, pcids;
2537 uint8_t *dbyte;
2538 uint16_t *dcode;
2539 struct qla_hw_data *ha = vha->hw;
2541 if (!ha->pio_address || !mbuf)
2542 return QLA_FUNCTION_FAILED;
2544 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2545 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2546 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2547 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2549 qla2x00_flash_enable(ha);
2551 /* Begin with first PCI expansion ROM header. */
2552 pcihdr = 0;
2553 last_image = 1;
2554 do {
2555 /* Verify PCI expansion ROM header. */
2556 if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
2557 qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
2558 /* No signature */
2559 DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
2560 "signature.\n"));
2561 ret = QLA_FUNCTION_FAILED;
2562 break;
2565 /* Locate PCI data structure. */
2566 pcids = pcihdr +
2567 ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
2568 qla2x00_read_flash_byte(ha, pcihdr + 0x18));
2570 /* Validate signature of PCI data structure. */
2571 if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
2572 qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
2573 qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
2574 qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
2575 /* Incorrect header. */
2576 DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
2577 "found pcir_adr=%x.\n", pcids));
2578 ret = QLA_FUNCTION_FAILED;
2579 break;
2582 /* Read version */
2583 code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
2584 switch (code_type) {
2585 case ROM_CODE_TYPE_BIOS:
2586 /* Intel x86, PC-AT compatible. */
2587 ha->bios_revision[0] =
2588 qla2x00_read_flash_byte(ha, pcids + 0x12);
2589 ha->bios_revision[1] =
2590 qla2x00_read_flash_byte(ha, pcids + 0x13);
2591 DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
2592 ha->bios_revision[1], ha->bios_revision[0]));
2593 break;
2594 case ROM_CODE_TYPE_FCODE:
2595 /* Open Firmware standard for PCI (FCode). */
2596 /* Eeeewww... */
2597 qla2x00_get_fcode_version(ha, pcids);
2598 break;
2599 case ROM_CODE_TYPE_EFI:
2600 /* Extensible Firmware Interface (EFI). */
2601 ha->efi_revision[0] =
2602 qla2x00_read_flash_byte(ha, pcids + 0x12);
2603 ha->efi_revision[1] =
2604 qla2x00_read_flash_byte(ha, pcids + 0x13);
2605 DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
2606 ha->efi_revision[1], ha->efi_revision[0]));
2607 break;
2608 default:
2609 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
2610 "type %x at pcids %x.\n", code_type, pcids));
2611 break;
2614 last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
2616 /* Locate next PCI expansion ROM. */
2617 pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
2618 qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
2619 } while (!last_image);
2621 if (IS_QLA2322(ha)) {
2622 /* Read firmware image information. */
2623 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2624 dbyte = mbuf;
2625 memset(dbyte, 0, 8);
2626 dcode = (uint16_t *)dbyte;
2628 qla2x00_read_flash_data(ha, dbyte, ha->flt_region_fw * 4 + 10,
2630 DEBUG3(qla_printk(KERN_DEBUG, ha, "dumping fw ver from "
2631 "flash:\n"));
2632 DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
2634 if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
2635 dcode[2] == 0xffff && dcode[3] == 0xffff) ||
2636 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2637 dcode[3] == 0)) {
2638 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
2639 "revision at %x.\n", ha->flt_region_fw * 4));
2640 } else {
2641 /* values are in big endian */
2642 ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
2643 ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
2644 ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
2648 qla2x00_flash_disable(ha);
2650 return ret;
2654 qla24xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
2656 int ret = QLA_SUCCESS;
2657 uint32_t pcihdr, pcids;
2658 uint32_t *dcode;
2659 uint8_t *bcode;
2660 uint8_t code_type, last_image;
2661 int i;
2662 struct qla_hw_data *ha = vha->hw;
2664 if (IS_QLA82XX(ha))
2665 return ret;
2667 if (!mbuf)
2668 return QLA_FUNCTION_FAILED;
2670 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2671 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2672 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2673 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2675 dcode = mbuf;
2677 /* Begin with first PCI expansion ROM header. */
2678 pcihdr = ha->flt_region_boot << 2;
2679 last_image = 1;
2680 do {
2681 /* Verify PCI expansion ROM header. */
2682 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
2683 bcode = mbuf + (pcihdr % 4);
2684 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
2685 /* No signature */
2686 DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
2687 "signature.\n"));
2688 ret = QLA_FUNCTION_FAILED;
2689 break;
2692 /* Locate PCI data structure. */
2693 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
2695 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
2696 bcode = mbuf + (pcihdr % 4);
2698 /* Validate signature of PCI data structure. */
2699 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
2700 bcode[0x2] != 'I' || bcode[0x3] != 'R') {
2701 /* Incorrect header. */
2702 DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
2703 "found pcir_adr=%x.\n", pcids));
2704 ret = QLA_FUNCTION_FAILED;
2705 break;
2708 /* Read version */
2709 code_type = bcode[0x14];
2710 switch (code_type) {
2711 case ROM_CODE_TYPE_BIOS:
2712 /* Intel x86, PC-AT compatible. */
2713 ha->bios_revision[0] = bcode[0x12];
2714 ha->bios_revision[1] = bcode[0x13];
2715 DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
2716 ha->bios_revision[1], ha->bios_revision[0]));
2717 break;
2718 case ROM_CODE_TYPE_FCODE:
2719 /* Open Firmware standard for PCI (FCode). */
2720 ha->fcode_revision[0] = bcode[0x12];
2721 ha->fcode_revision[1] = bcode[0x13];
2722 DEBUG3(qla_printk(KERN_DEBUG, ha, "read FCODE %d.%d.\n",
2723 ha->fcode_revision[1], ha->fcode_revision[0]));
2724 break;
2725 case ROM_CODE_TYPE_EFI:
2726 /* Extensible Firmware Interface (EFI). */
2727 ha->efi_revision[0] = bcode[0x12];
2728 ha->efi_revision[1] = bcode[0x13];
2729 DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
2730 ha->efi_revision[1], ha->efi_revision[0]));
2731 break;
2732 default:
2733 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
2734 "type %x at pcids %x.\n", code_type, pcids));
2735 break;
2738 last_image = bcode[0x15] & BIT_7;
2740 /* Locate next PCI expansion ROM. */
2741 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
2742 } while (!last_image);
2744 /* Read firmware image information. */
2745 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2746 dcode = mbuf;
2748 qla24xx_read_flash_data(vha, dcode, ha->flt_region_fw + 4, 4);
2749 for (i = 0; i < 4; i++)
2750 dcode[i] = be32_to_cpu(dcode[i]);
2752 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
2753 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
2754 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2755 dcode[3] == 0)) {
2756 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
2757 "revision at %x.\n", ha->flt_region_fw * 4));
2758 } else {
2759 ha->fw_revision[0] = dcode[0];
2760 ha->fw_revision[1] = dcode[1];
2761 ha->fw_revision[2] = dcode[2];
2762 ha->fw_revision[3] = dcode[3];
2765 /* Check for golden firmware and get version if available */
2766 if (!IS_QLA81XX(ha)) {
2767 /* Golden firmware is not present in non 81XX adapters */
2768 return ret;
2771 memset(ha->gold_fw_version, 0, sizeof(ha->gold_fw_version));
2772 dcode = mbuf;
2773 ha->isp_ops->read_optrom(vha, (uint8_t *)dcode,
2774 ha->flt_region_gold_fw << 2, 32);
2776 if (dcode[4] == 0xFFFFFFFF && dcode[5] == 0xFFFFFFFF &&
2777 dcode[6] == 0xFFFFFFFF && dcode[7] == 0xFFFFFFFF) {
2778 DEBUG2(qla_printk(KERN_INFO, ha,
2779 "%s(%ld): Unrecognized golden fw at 0x%x.\n",
2780 __func__, vha->host_no, ha->flt_region_gold_fw * 4));
2781 return ret;
2784 for (i = 4; i < 8; i++)
2785 ha->gold_fw_version[i-4] = be32_to_cpu(dcode[i]);
2787 return ret;
2790 static int
2791 qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
2793 if (pos >= end || *pos != 0x82)
2794 return 0;
2796 pos += 3 + pos[1];
2797 if (pos >= end || *pos != 0x90)
2798 return 0;
2800 pos += 3 + pos[1];
2801 if (pos >= end || *pos != 0x78)
2802 return 0;
2804 return 1;
2808 qla2xxx_get_vpd_field(scsi_qla_host_t *vha, char *key, char *str, size_t size)
2810 struct qla_hw_data *ha = vha->hw;
2811 uint8_t *pos = ha->vpd;
2812 uint8_t *end = pos + ha->vpd_size;
2813 int len = 0;
2815 if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
2816 return 0;
2818 while (pos < end && *pos != 0x78) {
2819 len = (*pos == 0x82) ? pos[1] : pos[2];
2821 if (!strncmp(pos, key, strlen(key)))
2822 break;
2824 if (*pos != 0x90 && *pos != 0x91)
2825 pos += len;
2827 pos += 3;
2830 if (pos < end - len && *pos != 0x78)
2831 return snprintf(str, size, "%.*s", len, pos + 3);
2833 return 0;
2837 qla24xx_read_fcp_prio_cfg(scsi_qla_host_t *vha)
2839 int len, max_len;
2840 uint32_t fcp_prio_addr;
2841 struct qla_hw_data *ha = vha->hw;
2843 if (!ha->fcp_prio_cfg) {
2844 ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
2845 if (!ha->fcp_prio_cfg) {
2846 qla_printk(KERN_WARNING, ha,
2847 "Unable to allocate memory for fcp priority data "
2848 "(%x).\n", FCP_PRIO_CFG_SIZE);
2849 return QLA_FUNCTION_FAILED;
2852 memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
2854 fcp_prio_addr = ha->flt_region_fcp_prio;
2856 /* first read the fcp priority data header from flash */
2857 ha->isp_ops->read_optrom(vha, (uint8_t *)ha->fcp_prio_cfg,
2858 fcp_prio_addr << 2, FCP_PRIO_CFG_HDR_SIZE);
2860 if (!qla24xx_fcp_prio_cfg_valid(ha->fcp_prio_cfg, 0))
2861 goto fail;
2863 /* read remaining FCP CMD config data from flash */
2864 fcp_prio_addr += (FCP_PRIO_CFG_HDR_SIZE >> 2);
2865 len = ha->fcp_prio_cfg->num_entries * FCP_PRIO_CFG_ENTRY_SIZE;
2866 max_len = FCP_PRIO_CFG_SIZE - FCP_PRIO_CFG_HDR_SIZE;
2868 ha->isp_ops->read_optrom(vha, (uint8_t *)&ha->fcp_prio_cfg->entry[0],
2869 fcp_prio_addr << 2, (len < max_len ? len : max_len));
2871 /* revalidate the entire FCP priority config data, including entries */
2872 if (!qla24xx_fcp_prio_cfg_valid(ha->fcp_prio_cfg, 1))
2873 goto fail;
2875 ha->flags.fcp_prio_enabled = 1;
2876 return QLA_SUCCESS;
2877 fail:
2878 vfree(ha->fcp_prio_cfg);
2879 ha->fcp_prio_cfg = NULL;
2880 return QLA_FUNCTION_FAILED;