2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
9 #include <linux/delay.h>
10 #include <linux/vmalloc.h>
11 #include <asm/uaccess.h>
14 * NVRAM support routines
18 * qla2x00_lock_nvram_access() -
22 qla2x00_lock_nvram_access(struct qla_hw_data *ha)
25 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
27 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
28 data = RD_REG_WORD(®->nvram);
29 while (data & NVR_BUSY) {
31 data = RD_REG_WORD(®->nvram);
35 WRT_REG_WORD(®->u.isp2300.host_semaphore, 0x1);
36 RD_REG_WORD(®->u.isp2300.host_semaphore);
38 data = RD_REG_WORD(®->u.isp2300.host_semaphore);
39 while ((data & BIT_0) == 0) {
42 WRT_REG_WORD(®->u.isp2300.host_semaphore, 0x1);
43 RD_REG_WORD(®->u.isp2300.host_semaphore);
45 data = RD_REG_WORD(®->u.isp2300.host_semaphore);
51 * qla2x00_unlock_nvram_access() -
55 qla2x00_unlock_nvram_access(struct qla_hw_data *ha)
57 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
59 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
60 WRT_REG_WORD(®->u.isp2300.host_semaphore, 0);
61 RD_REG_WORD(®->u.isp2300.host_semaphore);
66 * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
68 * @data: Serial interface selector
71 qla2x00_nv_write(struct qla_hw_data *ha, uint16_t data)
73 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
75 WRT_REG_WORD(®->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
76 RD_REG_WORD(®->nvram); /* PCI Posting. */
78 WRT_REG_WORD(®->nvram, data | NVR_SELECT | NVR_CLOCK |
80 RD_REG_WORD(®->nvram); /* PCI Posting. */
82 WRT_REG_WORD(®->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
83 RD_REG_WORD(®->nvram); /* PCI Posting. */
88 * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
91 * @nv_cmd: NVRAM command
93 * Bit definitions for NVRAM command:
98 * Bit 15-0 = write data
100 * Returns the word read from nvram @addr.
103 qla2x00_nvram_request(struct qla_hw_data *ha, uint32_t nv_cmd)
106 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
110 /* Send command to NVRAM. */
112 for (cnt = 0; cnt < 11; cnt++) {
114 qla2x00_nv_write(ha, NVR_DATA_OUT);
116 qla2x00_nv_write(ha, 0);
120 /* Read data from NVRAM. */
121 for (cnt = 0; cnt < 16; cnt++) {
122 WRT_REG_WORD(®->nvram, NVR_SELECT | NVR_CLOCK);
123 RD_REG_WORD(®->nvram); /* PCI Posting. */
126 reg_data = RD_REG_WORD(®->nvram);
127 if (reg_data & NVR_DATA_IN)
129 WRT_REG_WORD(®->nvram, NVR_SELECT);
130 RD_REG_WORD(®->nvram); /* PCI Posting. */
135 WRT_REG_WORD(®->nvram, NVR_DESELECT);
136 RD_REG_WORD(®->nvram); /* PCI Posting. */
144 * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
145 * request routine to get the word from NVRAM.
147 * @addr: Address in NVRAM to read
149 * Returns the word read from nvram @addr.
152 qla2x00_get_nvram_word(struct qla_hw_data *ha, uint32_t addr)
158 nv_cmd |= NV_READ_OP;
159 data = qla2x00_nvram_request(ha, nv_cmd);
165 * qla2x00_nv_deselect() - Deselect NVRAM operations.
169 qla2x00_nv_deselect(struct qla_hw_data *ha)
171 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
173 WRT_REG_WORD(®->nvram, NVR_DESELECT);
174 RD_REG_WORD(®->nvram); /* PCI Posting. */
179 * qla2x00_write_nvram_word() - Write NVRAM data.
181 * @addr: Address in NVRAM to write
182 * @data: word to program
185 qla2x00_write_nvram_word(struct qla_hw_data *ha, uint32_t addr, uint16_t data)
189 uint32_t nv_cmd, wait_cnt;
190 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
192 qla2x00_nv_write(ha, NVR_DATA_OUT);
193 qla2x00_nv_write(ha, 0);
194 qla2x00_nv_write(ha, 0);
196 for (word = 0; word < 8; word++)
197 qla2x00_nv_write(ha, NVR_DATA_OUT);
199 qla2x00_nv_deselect(ha);
202 nv_cmd = (addr << 16) | NV_WRITE_OP;
205 for (count = 0; count < 27; count++) {
207 qla2x00_nv_write(ha, NVR_DATA_OUT);
209 qla2x00_nv_write(ha, 0);
214 qla2x00_nv_deselect(ha);
216 /* Wait for NVRAM to become ready */
217 WRT_REG_WORD(®->nvram, NVR_SELECT);
218 RD_REG_WORD(®->nvram); /* PCI Posting. */
219 wait_cnt = NVR_WAIT_CNT;
222 DEBUG9_10(printk("%s(%ld): NVRAM didn't go ready...\n",
223 __func__, vha->host_no));
227 word = RD_REG_WORD(®->nvram);
228 } while ((word & NVR_DATA_IN) == 0);
230 qla2x00_nv_deselect(ha);
233 qla2x00_nv_write(ha, NVR_DATA_OUT);
234 for (count = 0; count < 10; count++)
235 qla2x00_nv_write(ha, 0);
237 qla2x00_nv_deselect(ha);
241 qla2x00_write_nvram_word_tmo(struct qla_hw_data *ha, uint32_t addr,
242 uint16_t data, uint32_t tmo)
247 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
251 qla2x00_nv_write(ha, NVR_DATA_OUT);
252 qla2x00_nv_write(ha, 0);
253 qla2x00_nv_write(ha, 0);
255 for (word = 0; word < 8; word++)
256 qla2x00_nv_write(ha, NVR_DATA_OUT);
258 qla2x00_nv_deselect(ha);
261 nv_cmd = (addr << 16) | NV_WRITE_OP;
264 for (count = 0; count < 27; count++) {
266 qla2x00_nv_write(ha, NVR_DATA_OUT);
268 qla2x00_nv_write(ha, 0);
273 qla2x00_nv_deselect(ha);
275 /* Wait for NVRAM to become ready */
276 WRT_REG_WORD(®->nvram, NVR_SELECT);
277 RD_REG_WORD(®->nvram); /* PCI Posting. */
280 word = RD_REG_WORD(®->nvram);
282 ret = QLA_FUNCTION_FAILED;
285 } while ((word & NVR_DATA_IN) == 0);
287 qla2x00_nv_deselect(ha);
290 qla2x00_nv_write(ha, NVR_DATA_OUT);
291 for (count = 0; count < 10; count++)
292 qla2x00_nv_write(ha, 0);
294 qla2x00_nv_deselect(ha);
300 * qla2x00_clear_nvram_protection() -
304 qla2x00_clear_nvram_protection(struct qla_hw_data *ha)
307 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
308 uint32_t word, wait_cnt;
309 uint16_t wprot, wprot_old;
311 /* Clear NVRAM write protection. */
312 ret = QLA_FUNCTION_FAILED;
314 wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
315 stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
316 __constant_cpu_to_le16(0x1234), 100000);
317 wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
318 if (stat != QLA_SUCCESS || wprot != 0x1234) {
320 qla2x00_nv_write(ha, NVR_DATA_OUT);
321 qla2x00_nv_write(ha, 0);
322 qla2x00_nv_write(ha, 0);
323 for (word = 0; word < 8; word++)
324 qla2x00_nv_write(ha, NVR_DATA_OUT);
326 qla2x00_nv_deselect(ha);
328 /* Enable protection register. */
329 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
330 qla2x00_nv_write(ha, NVR_PR_ENABLE);
331 qla2x00_nv_write(ha, NVR_PR_ENABLE);
332 for (word = 0; word < 8; word++)
333 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
335 qla2x00_nv_deselect(ha);
337 /* Clear protection register (ffff is cleared). */
338 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
339 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
340 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
341 for (word = 0; word < 8; word++)
342 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
344 qla2x00_nv_deselect(ha);
346 /* Wait for NVRAM to become ready. */
347 WRT_REG_WORD(®->nvram, NVR_SELECT);
348 RD_REG_WORD(®->nvram); /* PCI Posting. */
349 wait_cnt = NVR_WAIT_CNT;
352 DEBUG9_10(qla_printk(
353 "NVRAM didn't go ready...\n"));
357 word = RD_REG_WORD(®->nvram);
358 } while ((word & NVR_DATA_IN) == 0);
363 qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
369 qla2x00_set_nvram_protection(struct qla_hw_data *ha, int stat)
371 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
372 uint32_t word, wait_cnt;
374 if (stat != QLA_SUCCESS)
377 /* Set NVRAM write protection. */
379 qla2x00_nv_write(ha, NVR_DATA_OUT);
380 qla2x00_nv_write(ha, 0);
381 qla2x00_nv_write(ha, 0);
382 for (word = 0; word < 8; word++)
383 qla2x00_nv_write(ha, NVR_DATA_OUT);
385 qla2x00_nv_deselect(ha);
387 /* Enable protection register. */
388 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
389 qla2x00_nv_write(ha, NVR_PR_ENABLE);
390 qla2x00_nv_write(ha, NVR_PR_ENABLE);
391 for (word = 0; word < 8; word++)
392 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
394 qla2x00_nv_deselect(ha);
396 /* Enable protection register. */
397 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
398 qla2x00_nv_write(ha, NVR_PR_ENABLE);
399 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
400 for (word = 0; word < 8; word++)
401 qla2x00_nv_write(ha, NVR_PR_ENABLE);
403 qla2x00_nv_deselect(ha);
405 /* Wait for NVRAM to become ready. */
406 WRT_REG_WORD(®->nvram, NVR_SELECT);
407 RD_REG_WORD(®->nvram); /* PCI Posting. */
408 wait_cnt = NVR_WAIT_CNT;
411 DEBUG9_10(qla_printk("NVRAM didn't go ready...\n"));
415 word = RD_REG_WORD(®->nvram);
416 } while ((word & NVR_DATA_IN) == 0);
420 /*****************************************************************************/
421 /* Flash Manipulation Routines */
422 /*****************************************************************************/
424 #define OPTROM_BURST_SIZE 0x1000
425 #define OPTROM_BURST_DWORDS (OPTROM_BURST_SIZE / 4)
427 static inline uint32_t
428 flash_conf_addr(struct qla_hw_data *ha, uint32_t faddr)
430 return ha->flash_conf_off | faddr;
433 static inline uint32_t
434 flash_data_addr(struct qla_hw_data *ha, uint32_t faddr)
436 return ha->flash_data_off | faddr;
439 static inline uint32_t
440 nvram_conf_addr(struct qla_hw_data *ha, uint32_t naddr)
442 return ha->nvram_conf_off | naddr;
445 static inline uint32_t
446 nvram_data_addr(struct qla_hw_data *ha, uint32_t naddr)
448 return ha->nvram_data_off | naddr;
452 qla24xx_read_flash_dword(struct qla_hw_data *ha, uint32_t addr)
456 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
458 WRT_REG_DWORD(®->flash_addr, addr & ~FARX_DATA_FLAG);
459 /* Wait for READ cycle to complete. */
462 (RD_REG_DWORD(®->flash_addr) & FARX_DATA_FLAG) == 0 &&
463 rval == QLA_SUCCESS; cnt--) {
467 rval = QLA_FUNCTION_TIMEOUT;
471 /* TODO: What happens if we time out? */
473 if (rval == QLA_SUCCESS)
474 data = RD_REG_DWORD(®->flash_data);
480 qla24xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
484 struct qla_hw_data *ha = vha->hw;
486 /* Dword reads to flash. */
487 for (i = 0; i < dwords; i++, faddr++)
488 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
489 flash_data_addr(ha, faddr)));
495 qla24xx_write_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t data)
499 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
501 WRT_REG_DWORD(®->flash_data, data);
502 RD_REG_DWORD(®->flash_data); /* PCI Posting. */
503 WRT_REG_DWORD(®->flash_addr, addr | FARX_DATA_FLAG);
504 /* Wait for Write cycle to complete. */
506 for (cnt = 500000; (RD_REG_DWORD(®->flash_addr) & FARX_DATA_FLAG) &&
507 rval == QLA_SUCCESS; cnt--) {
511 rval = QLA_FUNCTION_TIMEOUT;
518 qla24xx_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
523 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x03ab));
525 *flash_id = MSB(ids);
527 /* Check if man_id and flash_id are valid. */
528 if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
529 /* Read information using 0x9f opcode
530 * Device ID, Mfg ID would be read in the format:
531 * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
532 * Example: ATMEL 0x00 01 45 1F
533 * Extract MFG and Dev ID from last two bytes.
535 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x009f));
537 *flash_id = MSB(ids);
542 qla2xxx_find_flt_start(scsi_qla_host_t *vha, uint32_t *start)
544 const char *loc, *locations[] = { "DEF", "PCI" };
545 uint32_t pcihdr, pcids;
547 uint8_t *buf, *bcode, last_image;
548 uint16_t cnt, chksum, *wptr;
549 struct qla_flt_location *fltl;
550 struct qla_hw_data *ha = vha->hw;
551 struct req_que *req = ha->req_q_map[0];
554 * FLT-location structure resides after the last PCI region.
557 /* Begin with sane defaults. */
560 if (IS_QLA24XX_TYPE(ha))
561 *start = FA_FLASH_LAYOUT_ADDR_24;
562 else if (IS_QLA25XX(ha))
563 *start = FA_FLASH_LAYOUT_ADDR;
564 else if (IS_QLA81XX(ha))
565 *start = FA_FLASH_LAYOUT_ADDR_81;
566 /* Begin with first PCI expansion ROM header. */
567 buf = (uint8_t *)req->ring;
568 dcode = (uint32_t *)req->ring;
572 /* Verify PCI expansion ROM header. */
573 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
574 bcode = buf + (pcihdr % 4);
575 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa)
578 /* Locate PCI data structure. */
579 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
580 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
581 bcode = buf + (pcihdr % 4);
583 /* Validate signature of PCI data structure. */
584 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
585 bcode[0x2] != 'I' || bcode[0x3] != 'R')
588 last_image = bcode[0x15] & BIT_7;
590 /* Locate next PCI expansion ROM. */
591 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
592 } while (!last_image);
594 /* Now verify FLT-location structure. */
595 fltl = (struct qla_flt_location *)req->ring;
596 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2,
597 sizeof(struct qla_flt_location) >> 2);
598 if (fltl->sig[0] != 'Q' || fltl->sig[1] != 'F' ||
599 fltl->sig[2] != 'L' || fltl->sig[3] != 'T')
602 wptr = (uint16_t *)req->ring;
603 cnt = sizeof(struct qla_flt_location) >> 1;
604 for (chksum = 0; cnt; cnt--)
605 chksum += le16_to_cpu(*wptr++);
607 qla_printk(KERN_ERR, ha,
608 "Inconsistent FLTL detected: checksum=0x%x.\n", chksum);
609 qla2x00_dump_buffer(buf, sizeof(struct qla_flt_location));
610 return QLA_FUNCTION_FAILED;
613 /* Good data. Use specified location. */
615 *start = (le16_to_cpu(fltl->start_hi) << 16 |
616 le16_to_cpu(fltl->start_lo)) >> 2;
618 DEBUG2(qla_printk(KERN_DEBUG, ha, "FLTL[%s] = 0x%x.\n", loc, *start));
623 qla2xxx_get_flt_info(scsi_qla_host_t *vha, uint32_t flt_addr)
625 const char *loc, *locations[] = { "DEF", "FLT" };
626 const uint32_t def_fw[] =
627 { FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR_81 };
628 const uint32_t def_boot[] =
629 { FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR_81 };
630 const uint32_t def_vpd_nvram[] =
631 { FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR_81 };
632 const uint32_t def_vpd0[] =
633 { 0, 0, FA_VPD0_ADDR_81 };
634 const uint32_t def_vpd1[] =
635 { 0, 0, FA_VPD1_ADDR_81 };
636 const uint32_t def_nvram0[] =
637 { 0, 0, FA_NVRAM0_ADDR_81 };
638 const uint32_t def_nvram1[] =
639 { 0, 0, FA_NVRAM1_ADDR_81 };
640 const uint32_t def_fdt[] =
641 { FA_FLASH_DESCR_ADDR_24, FA_FLASH_DESCR_ADDR,
642 FA_FLASH_DESCR_ADDR_81 };
643 const uint32_t def_npiv_conf0[] =
644 { FA_NPIV_CONF0_ADDR_24, FA_NPIV_CONF0_ADDR,
645 FA_NPIV_CONF0_ADDR_81 };
646 const uint32_t def_npiv_conf1[] =
647 { FA_NPIV_CONF1_ADDR_24, FA_NPIV_CONF1_ADDR,
648 FA_NPIV_CONF1_ADDR_81 };
651 uint16_t cnt, chksum;
653 struct qla_flt_header *flt;
654 struct qla_flt_region *region;
655 struct qla_hw_data *ha = vha->hw;
656 struct req_que *req = ha->req_q_map[0];
658 ha->flt_region_flt = flt_addr;
659 wptr = (uint16_t *)req->ring;
660 flt = (struct qla_flt_header *)req->ring;
661 region = (struct qla_flt_region *)&flt[1];
662 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
663 flt_addr << 2, OPTROM_BURST_SIZE);
664 if (*wptr == __constant_cpu_to_le16(0xffff))
666 if (flt->version != __constant_cpu_to_le16(1)) {
667 DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported FLT detected: "
668 "version=0x%x length=0x%x checksum=0x%x.\n",
669 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
670 le16_to_cpu(flt->checksum)));
674 cnt = (sizeof(struct qla_flt_header) + le16_to_cpu(flt->length)) >> 1;
675 for (chksum = 0; cnt; cnt--)
676 chksum += le16_to_cpu(*wptr++);
678 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FLT detected: "
679 "version=0x%x length=0x%x checksum=0x%x.\n",
680 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
686 cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
687 for ( ; cnt; cnt--, region++) {
688 /* Store addresses as DWORD offsets. */
689 start = le32_to_cpu(region->start) >> 2;
691 DEBUG3(qla_printk(KERN_DEBUG, ha, "FLT[%02x]: start=0x%x "
692 "end=0x%x size=0x%x.\n", le32_to_cpu(region->code), start,
693 le32_to_cpu(region->end) >> 2, le32_to_cpu(region->size)));
695 switch (le32_to_cpu(region->code) & 0xff) {
697 ha->flt_region_fw = start;
699 case FLT_REG_BOOT_CODE:
700 ha->flt_region_boot = start;
703 ha->flt_region_vpd_nvram = start;
704 if (!(PCI_FUNC(ha->pdev->devfn) & 1))
705 ha->flt_region_vpd = start;
708 if (PCI_FUNC(ha->pdev->devfn) & 1)
709 ha->flt_region_vpd = start;
711 case FLT_REG_NVRAM_0:
712 if (!(PCI_FUNC(ha->pdev->devfn) & 1))
713 ha->flt_region_nvram = start;
715 case FLT_REG_NVRAM_1:
716 if (PCI_FUNC(ha->pdev->devfn) & 1)
717 ha->flt_region_nvram = start;
720 ha->flt_region_fdt = start;
722 case FLT_REG_NPIV_CONF_0:
723 if (!(PCI_FUNC(ha->pdev->devfn) & 1))
724 ha->flt_region_npiv_conf = start;
726 case FLT_REG_NPIV_CONF_1:
727 if (PCI_FUNC(ha->pdev->devfn) & 1)
728 ha->flt_region_npiv_conf = start;
735 /* Use hardcoded defaults. */
738 if (IS_QLA24XX_TYPE(ha))
740 else if (IS_QLA25XX(ha))
742 else if (IS_QLA81XX(ha))
744 ha->flt_region_fw = def_fw[def];
745 ha->flt_region_boot = def_boot[def];
746 ha->flt_region_vpd_nvram = def_vpd_nvram[def];
747 ha->flt_region_vpd = !(PCI_FUNC(ha->pdev->devfn) & 1) ?
748 def_vpd0[def]: def_vpd1[def];
749 ha->flt_region_nvram = !(PCI_FUNC(ha->pdev->devfn) & 1) ?
750 def_nvram0[def]: def_nvram1[def];
751 ha->flt_region_fdt = def_fdt[def];
752 ha->flt_region_npiv_conf = !(PCI_FUNC(ha->pdev->devfn) & 1) ?
753 def_npiv_conf0[def]: def_npiv_conf1[def];
755 DEBUG2(qla_printk(KERN_DEBUG, ha, "FLT[%s]: boot=0x%x fw=0x%x "
756 "vpd_nvram=0x%x vpd=0x%x nvram=0x%x fdt=0x%x flt=0x%x "
757 "npiv=0x%x.\n", loc, ha->flt_region_boot, ha->flt_region_fw,
758 ha->flt_region_vpd_nvram, ha->flt_region_vpd, ha->flt_region_nvram,
759 ha->flt_region_fdt, ha->flt_region_flt, ha->flt_region_npiv_conf));
763 qla2xxx_get_fdt_info(scsi_qla_host_t *vha)
765 #define FLASH_BLK_SIZE_4K 0x1000
766 #define FLASH_BLK_SIZE_32K 0x8000
767 #define FLASH_BLK_SIZE_64K 0x10000
768 const char *loc, *locations[] = { "MID", "FDT" };
769 uint16_t cnt, chksum;
771 struct qla_fdt_layout *fdt;
772 uint8_t man_id, flash_id;
774 struct qla_hw_data *ha = vha->hw;
775 struct req_que *req = ha->req_q_map[0];
777 wptr = (uint16_t *)req->ring;
778 fdt = (struct qla_fdt_layout *)req->ring;
779 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
780 ha->flt_region_fdt << 2, OPTROM_BURST_SIZE);
781 if (*wptr == __constant_cpu_to_le16(0xffff))
783 if (fdt->sig[0] != 'Q' || fdt->sig[1] != 'L' || fdt->sig[2] != 'I' ||
787 for (cnt = 0, chksum = 0; cnt < sizeof(struct qla_fdt_layout) >> 1;
789 chksum += le16_to_cpu(*wptr++);
791 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FDT detected: "
792 "checksum=0x%x id=%c version=0x%x.\n", chksum, fdt->sig[0],
793 le16_to_cpu(fdt->version)));
794 DEBUG9(qla2x00_dump_buffer((uint8_t *)fdt, sizeof(*fdt)));
799 mid = le16_to_cpu(fdt->man_id);
800 fid = le16_to_cpu(fdt->id);
801 ha->fdt_wrt_disable = fdt->wrt_disable_bits;
802 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0300 | fdt->erase_cmd);
803 ha->fdt_block_size = le32_to_cpu(fdt->block_size);
804 if (fdt->unprotect_sec_cmd) {
805 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0300 |
806 fdt->unprotect_sec_cmd);
807 ha->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
808 flash_conf_addr(ha, 0x0300 | fdt->protect_sec_cmd):
809 flash_conf_addr(ha, 0x0336);
814 qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
817 ha->fdt_wrt_disable = 0x9c;
818 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x03d8);
820 case 0xbf: /* STT flash. */
821 if (flash_id == 0x8e)
822 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
824 ha->fdt_block_size = FLASH_BLK_SIZE_32K;
826 if (flash_id == 0x80)
827 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0352);
829 case 0x13: /* ST M25P80. */
830 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
832 case 0x1f: /* Atmel 26DF081A. */
833 ha->fdt_block_size = FLASH_BLK_SIZE_4K;
834 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0320);
835 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0339);
836 ha->fdt_protect_sec_cmd = flash_conf_addr(ha, 0x0336);
839 /* Default to 64 kb sector size. */
840 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
844 DEBUG2(qla_printk(KERN_DEBUG, ha, "FDT[%s]: (0x%x/0x%x) erase=0x%x "
845 "pro=%x upro=%x wrtd=0x%x blk=0x%x.\n", loc, mid, fid,
846 ha->fdt_erase_cmd, ha->fdt_protect_sec_cmd,
847 ha->fdt_unprotect_sec_cmd, ha->fdt_wrt_disable,
848 ha->fdt_block_size));
852 qla2xxx_get_flash_info(scsi_qla_host_t *vha)
856 struct qla_hw_data *ha = vha->hw;
858 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA81XX(ha))
861 ret = qla2xxx_find_flt_start(vha, &flt_addr);
862 if (ret != QLA_SUCCESS)
865 qla2xxx_get_flt_info(vha, flt_addr);
866 qla2xxx_get_fdt_info(vha);
872 qla2xxx_flash_npiv_conf(scsi_qla_host_t *vha)
874 #define NPIV_CONFIG_SIZE (16*1024)
877 uint16_t cnt, chksum;
879 struct qla_npiv_header hdr;
880 struct qla_npiv_entry *entry;
881 struct qla_hw_data *ha = vha->hw;
883 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA81XX(ha))
886 ha->isp_ops->read_optrom(vha, (uint8_t *)&hdr,
887 ha->flt_region_npiv_conf << 2, sizeof(struct qla_npiv_header));
888 if (hdr.version == __constant_cpu_to_le16(0xffff))
890 if (hdr.version != __constant_cpu_to_le16(1)) {
891 DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported NPIV-Config "
892 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
893 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
894 le16_to_cpu(hdr.checksum)));
898 data = kmalloc(NPIV_CONFIG_SIZE, GFP_KERNEL);
900 DEBUG2(qla_printk(KERN_INFO, ha, "NPIV-Config: Unable to "
901 "allocate memory.\n"));
905 ha->isp_ops->read_optrom(vha, (uint8_t *)data,
906 ha->flt_region_npiv_conf << 2, NPIV_CONFIG_SIZE);
908 cnt = (sizeof(struct qla_npiv_header) + le16_to_cpu(hdr.entries) *
909 sizeof(struct qla_npiv_entry)) >> 1;
910 for (wptr = data, chksum = 0; cnt; cnt--)
911 chksum += le16_to_cpu(*wptr++);
913 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent NPIV-Config "
914 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
915 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
920 entry = data + sizeof(struct qla_npiv_header);
921 cnt = le16_to_cpu(hdr.entries);
922 for (i = 0; cnt; cnt--, entry++, i++) {
924 struct fc_vport_identifiers vid;
925 struct fc_vport *vport;
927 flags = le16_to_cpu(entry->flags);
930 if ((flags & BIT_0) == 0)
933 memset(&vid, 0, sizeof(vid));
934 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
935 vid.vport_type = FC_PORTTYPE_NPIV;
937 vid.port_name = wwn_to_u64(entry->port_name);
938 vid.node_name = wwn_to_u64(entry->node_name);
940 memcpy(&ha->npiv_info[i], entry, sizeof(struct qla_npiv_entry));
942 DEBUG2(qla_printk(KERN_DEBUG, ha, "NPIV[%02x]: wwpn=%llx "
943 "wwnn=%llx vf_id=0x%x Q_qos=0x%x F_qos=0x%x.\n", cnt,
944 vid.port_name, vid.node_name, le16_to_cpu(entry->vf_id),
945 entry->q_qos, entry->f_qos));
947 if (i < QLA_PRECONFIG_VPORTS) {
948 vport = fc_vport_create(vha->host, 0, &vid);
950 qla_printk(KERN_INFO, ha,
951 "NPIV-Config: Failed to create vport [%02x]: "
952 "wwpn=%llx wwnn=%llx.\n", cnt,
953 vid.port_name, vid.node_name);
958 ha->npiv_info = NULL;
962 qla24xx_unprotect_flash(scsi_qla_host_t *vha)
964 struct qla_hw_data *ha = vha->hw;
965 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
967 if (ha->flags.fac_supported)
968 return qla81xx_fac_do_write_enable(vha, 1);
970 /* Enable flash write. */
971 WRT_REG_DWORD(®->ctrl_status,
972 RD_REG_DWORD(®->ctrl_status) | CSRX_FLASH_ENABLE);
973 RD_REG_DWORD(®->ctrl_status); /* PCI Posting. */
975 if (!ha->fdt_wrt_disable)
978 /* Disable flash write-protection, first clear SR protection bit */
979 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
980 /* Then write zero again to clear remaining SR bits.*/
981 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
987 qla24xx_protect_flash(scsi_qla_host_t *vha)
990 struct qla_hw_data *ha = vha->hw;
991 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
993 if (ha->flags.fac_supported)
994 return qla81xx_fac_do_write_enable(vha, 0);
996 if (!ha->fdt_wrt_disable)
997 goto skip_wrt_protect;
999 /* Enable flash write-protection and wait for completion. */
1000 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101),
1001 ha->fdt_wrt_disable);
1002 for (cnt = 300; cnt &&
1003 qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x005)) & BIT_0;
1009 /* Disable flash write. */
1010 WRT_REG_DWORD(®->ctrl_status,
1011 RD_REG_DWORD(®->ctrl_status) & ~CSRX_FLASH_ENABLE);
1012 RD_REG_DWORD(®->ctrl_status); /* PCI Posting. */
1018 qla24xx_erase_sector(scsi_qla_host_t *vha, uint32_t fdata)
1020 struct qla_hw_data *ha = vha->hw;
1021 uint32_t start, finish;
1023 if (ha->flags.fac_supported) {
1025 finish = start + (ha->fdt_block_size >> 2) - 1;
1026 return qla81xx_fac_erase_sector(vha, flash_data_addr(ha,
1027 start), flash_data_addr(ha, finish));
1030 return qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
1031 (fdata & 0xff00) | ((fdata << 16) & 0xff0000) |
1032 ((fdata >> 16) & 0xff));
1036 qla24xx_write_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
1041 uint32_t sec_mask, rest_addr;
1043 dma_addr_t optrom_dma;
1044 void *optrom = NULL;
1045 struct qla_hw_data *ha = vha->hw;
1047 /* Prepare burst-capable write on supported ISPs. */
1048 if ((IS_QLA25XX(ha) || IS_QLA81XX(ha)) && !(faddr & 0xfff) &&
1049 dwords > OPTROM_BURST_DWORDS) {
1050 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
1051 &optrom_dma, GFP_KERNEL);
1053 qla_printk(KERN_DEBUG, ha,
1054 "Unable to allocate memory for optrom burst write "
1055 "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
1059 rest_addr = (ha->fdt_block_size >> 2) - 1;
1060 sec_mask = ~rest_addr;
1062 ret = qla24xx_unprotect_flash(vha);
1063 if (ret != QLA_SUCCESS) {
1064 qla_printk(KERN_WARNING, ha,
1065 "Unable to unprotect flash for update.\n");
1069 for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
1070 fdata = (faddr & sec_mask) << 2;
1072 /* Are we at the beginning of a sector? */
1073 if ((faddr & rest_addr) == 0) {
1074 /* Do sector unprotect. */
1075 if (ha->fdt_unprotect_sec_cmd)
1076 qla24xx_write_flash_dword(ha,
1077 ha->fdt_unprotect_sec_cmd,
1078 (fdata & 0xff00) | ((fdata << 16) &
1079 0xff0000) | ((fdata >> 16) & 0xff));
1080 ret = qla24xx_erase_sector(vha, fdata);
1081 if (ret != QLA_SUCCESS) {
1082 DEBUG9(qla_printk("Unable to erase sector: "
1083 "address=%x.\n", faddr));
1088 /* Go with burst-write. */
1089 if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
1090 /* Copy data to DMA'ble buffer. */
1091 memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
1093 ret = qla2x00_load_ram(vha, optrom_dma,
1094 flash_data_addr(ha, faddr),
1095 OPTROM_BURST_DWORDS);
1096 if (ret != QLA_SUCCESS) {
1097 qla_printk(KERN_WARNING, ha,
1098 "Unable to burst-write optrom segment "
1099 "(%x/%x/%llx).\n", ret,
1100 flash_data_addr(ha, faddr),
1101 (unsigned long long)optrom_dma);
1102 qla_printk(KERN_WARNING, ha,
1103 "Reverting to slow-write.\n");
1105 dma_free_coherent(&ha->pdev->dev,
1106 OPTROM_BURST_SIZE, optrom, optrom_dma);
1109 liter += OPTROM_BURST_DWORDS - 1;
1110 faddr += OPTROM_BURST_DWORDS - 1;
1111 dwptr += OPTROM_BURST_DWORDS - 1;
1116 ret = qla24xx_write_flash_dword(ha,
1117 flash_data_addr(ha, faddr), cpu_to_le32(*dwptr));
1118 if (ret != QLA_SUCCESS) {
1119 DEBUG9(printk("%s(%ld) Unable to program flash "
1120 "address=%x data=%x.\n", __func__,
1121 vha->host_no, faddr, *dwptr));
1125 /* Do sector protect. */
1126 if (ha->fdt_unprotect_sec_cmd &&
1127 ((faddr & rest_addr) == rest_addr))
1128 qla24xx_write_flash_dword(ha,
1129 ha->fdt_protect_sec_cmd,
1130 (fdata & 0xff00) | ((fdata << 16) &
1131 0xff0000) | ((fdata >> 16) & 0xff));
1134 ret = qla24xx_protect_flash(vha);
1135 if (ret != QLA_SUCCESS)
1136 qla_printk(KERN_WARNING, ha,
1137 "Unable to protect flash after update.\n");
1140 dma_free_coherent(&ha->pdev->dev,
1141 OPTROM_BURST_SIZE, optrom, optrom_dma);
1147 qla2x00_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1152 struct qla_hw_data *ha = vha->hw;
1154 /* Word reads to NVRAM via registers. */
1155 wptr = (uint16_t *)buf;
1156 qla2x00_lock_nvram_access(ha);
1157 for (i = 0; i < bytes >> 1; i++, naddr++)
1158 wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
1160 qla2x00_unlock_nvram_access(ha);
1166 qla24xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1171 struct qla_hw_data *ha = vha->hw;
1173 /* Dword reads to flash. */
1174 dwptr = (uint32_t *)buf;
1175 for (i = 0; i < bytes >> 2; i++, naddr++)
1176 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
1177 nvram_data_addr(ha, naddr)));
1183 qla2x00_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1189 unsigned long flags;
1190 struct qla_hw_data *ha = vha->hw;
1194 spin_lock_irqsave(&ha->hardware_lock, flags);
1195 qla2x00_lock_nvram_access(ha);
1197 /* Disable NVRAM write-protection. */
1198 stat = qla2x00_clear_nvram_protection(ha);
1200 wptr = (uint16_t *)buf;
1201 for (i = 0; i < bytes >> 1; i++, naddr++) {
1202 qla2x00_write_nvram_word(ha, naddr,
1203 cpu_to_le16(*wptr));
1207 /* Enable NVRAM write-protection. */
1208 qla2x00_set_nvram_protection(ha, stat);
1210 qla2x00_unlock_nvram_access(ha);
1211 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1217 qla24xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1223 struct qla_hw_data *ha = vha->hw;
1224 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1228 /* Enable flash write. */
1229 WRT_REG_DWORD(®->ctrl_status,
1230 RD_REG_DWORD(®->ctrl_status) | CSRX_FLASH_ENABLE);
1231 RD_REG_DWORD(®->ctrl_status); /* PCI Posting. */
1233 /* Disable NVRAM write-protection. */
1234 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
1235 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
1237 /* Dword writes to flash. */
1238 dwptr = (uint32_t *)buf;
1239 for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
1240 ret = qla24xx_write_flash_dword(ha,
1241 nvram_data_addr(ha, naddr), cpu_to_le32(*dwptr));
1242 if (ret != QLA_SUCCESS) {
1243 DEBUG9(qla_printk("Unable to program nvram address=%x "
1244 "data=%x.\n", naddr, *dwptr));
1249 /* Enable NVRAM write-protection. */
1250 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0x8c);
1252 /* Disable flash write. */
1253 WRT_REG_DWORD(®->ctrl_status,
1254 RD_REG_DWORD(®->ctrl_status) & ~CSRX_FLASH_ENABLE);
1255 RD_REG_DWORD(®->ctrl_status); /* PCI Posting. */
1261 qla25xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1266 struct qla_hw_data *ha = vha->hw;
1268 /* Dword reads to flash. */
1269 dwptr = (uint32_t *)buf;
1270 for (i = 0; i < bytes >> 2; i++, naddr++)
1271 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
1272 flash_data_addr(ha, ha->flt_region_vpd_nvram | naddr)));
1278 qla25xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
1281 struct qla_hw_data *ha = vha->hw;
1282 #define RMW_BUFFER_SIZE (64 * 1024)
1285 dbuf = vmalloc(RMW_BUFFER_SIZE);
1287 return QLA_MEMORY_ALLOC_FAILED;
1288 ha->isp_ops->read_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
1290 memcpy(dbuf + (naddr << 2), buf, bytes);
1291 ha->isp_ops->write_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
1299 qla2x00_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
1301 if (IS_QLA2322(ha)) {
1302 /* Flip all colors. */
1303 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1305 ha->beacon_color_state = 0;
1306 *pflags = GPIO_LED_ALL_OFF;
1309 ha->beacon_color_state = QLA_LED_ALL_ON;
1310 *pflags = GPIO_LED_RGA_ON;
1313 /* Flip green led only. */
1314 if (ha->beacon_color_state == QLA_LED_GRN_ON) {
1316 ha->beacon_color_state = 0;
1317 *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
1320 ha->beacon_color_state = QLA_LED_GRN_ON;
1321 *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
1326 #define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
1329 qla2x00_beacon_blink(struct scsi_qla_host *vha)
1331 uint16_t gpio_enable;
1333 uint16_t led_color = 0;
1334 unsigned long flags;
1335 struct qla_hw_data *ha = vha->hw;
1336 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1338 spin_lock_irqsave(&ha->hardware_lock, flags);
1340 /* Save the Original GPIOE. */
1341 if (ha->pio_address) {
1342 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1343 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
1345 gpio_enable = RD_REG_WORD(®->gpioe);
1346 gpio_data = RD_REG_WORD(®->gpiod);
1349 /* Set the modified gpio_enable values */
1350 gpio_enable |= GPIO_LED_MASK;
1352 if (ha->pio_address) {
1353 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
1355 WRT_REG_WORD(®->gpioe, gpio_enable);
1356 RD_REG_WORD(®->gpioe);
1359 qla2x00_flip_colors(ha, &led_color);
1361 /* Clear out any previously set LED color. */
1362 gpio_data &= ~GPIO_LED_MASK;
1364 /* Set the new input LED color to GPIOD. */
1365 gpio_data |= led_color;
1367 /* Set the modified gpio_data values */
1368 if (ha->pio_address) {
1369 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
1371 WRT_REG_WORD(®->gpiod, gpio_data);
1372 RD_REG_WORD(®->gpiod);
1375 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1379 qla2x00_beacon_on(struct scsi_qla_host *vha)
1381 uint16_t gpio_enable;
1383 unsigned long flags;
1384 struct qla_hw_data *ha = vha->hw;
1385 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1387 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1388 ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
1390 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
1391 qla_printk(KERN_WARNING, ha,
1392 "Unable to update fw options (beacon on).\n");
1393 return QLA_FUNCTION_FAILED;
1396 /* Turn off LEDs. */
1397 spin_lock_irqsave(&ha->hardware_lock, flags);
1398 if (ha->pio_address) {
1399 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1400 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
1402 gpio_enable = RD_REG_WORD(®->gpioe);
1403 gpio_data = RD_REG_WORD(®->gpiod);
1405 gpio_enable |= GPIO_LED_MASK;
1407 /* Set the modified gpio_enable values. */
1408 if (ha->pio_address) {
1409 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
1411 WRT_REG_WORD(®->gpioe, gpio_enable);
1412 RD_REG_WORD(®->gpioe);
1415 /* Clear out previously set LED colour. */
1416 gpio_data &= ~GPIO_LED_MASK;
1417 if (ha->pio_address) {
1418 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
1420 WRT_REG_WORD(®->gpiod, gpio_data);
1421 RD_REG_WORD(®->gpiod);
1423 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1426 * Let the per HBA timer kick off the blinking process based on
1427 * the following flags. No need to do anything else now.
1429 ha->beacon_blink_led = 1;
1430 ha->beacon_color_state = 0;
1436 qla2x00_beacon_off(struct scsi_qla_host *vha)
1438 int rval = QLA_SUCCESS;
1439 struct qla_hw_data *ha = vha->hw;
1441 ha->beacon_blink_led = 0;
1443 /* Set the on flag so when it gets flipped it will be off. */
1445 ha->beacon_color_state = QLA_LED_ALL_ON;
1447 ha->beacon_color_state = QLA_LED_GRN_ON;
1449 ha->isp_ops->beacon_blink(vha); /* This turns green LED off */
1451 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1452 ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
1454 rval = qla2x00_set_fw_options(vha, ha->fw_options);
1455 if (rval != QLA_SUCCESS)
1456 qla_printk(KERN_WARNING, ha,
1457 "Unable to update fw options (beacon off).\n");
1463 qla24xx_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
1465 /* Flip all colors. */
1466 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1468 ha->beacon_color_state = 0;
1472 ha->beacon_color_state = QLA_LED_ALL_ON;
1473 *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
1478 qla24xx_beacon_blink(struct scsi_qla_host *vha)
1480 uint16_t led_color = 0;
1482 unsigned long flags;
1483 struct qla_hw_data *ha = vha->hw;
1484 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1486 /* Save the Original GPIOD. */
1487 spin_lock_irqsave(&ha->hardware_lock, flags);
1488 gpio_data = RD_REG_DWORD(®->gpiod);
1490 /* Enable the gpio_data reg for update. */
1491 gpio_data |= GPDX_LED_UPDATE_MASK;
1493 WRT_REG_DWORD(®->gpiod, gpio_data);
1494 gpio_data = RD_REG_DWORD(®->gpiod);
1496 /* Set the color bits. */
1497 qla24xx_flip_colors(ha, &led_color);
1499 /* Clear out any previously set LED color. */
1500 gpio_data &= ~GPDX_LED_COLOR_MASK;
1502 /* Set the new input LED color to GPIOD. */
1503 gpio_data |= led_color;
1505 /* Set the modified gpio_data values. */
1506 WRT_REG_DWORD(®->gpiod, gpio_data);
1507 gpio_data = RD_REG_DWORD(®->gpiod);
1508 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1512 qla24xx_beacon_on(struct scsi_qla_host *vha)
1515 unsigned long flags;
1516 struct qla_hw_data *ha = vha->hw;
1517 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1519 if (ha->beacon_blink_led == 0) {
1520 /* Enable firmware for update */
1521 ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
1523 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS)
1524 return QLA_FUNCTION_FAILED;
1526 if (qla2x00_get_fw_options(vha, ha->fw_options) !=
1528 qla_printk(KERN_WARNING, ha,
1529 "Unable to update fw options (beacon on).\n");
1530 return QLA_FUNCTION_FAILED;
1533 spin_lock_irqsave(&ha->hardware_lock, flags);
1534 gpio_data = RD_REG_DWORD(®->gpiod);
1536 /* Enable the gpio_data reg for update. */
1537 gpio_data |= GPDX_LED_UPDATE_MASK;
1538 WRT_REG_DWORD(®->gpiod, gpio_data);
1539 RD_REG_DWORD(®->gpiod);
1541 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1544 /* So all colors blink together. */
1545 ha->beacon_color_state = 0;
1547 /* Let the per HBA timer kick off the blinking process. */
1548 ha->beacon_blink_led = 1;
1554 qla24xx_beacon_off(struct scsi_qla_host *vha)
1557 unsigned long flags;
1558 struct qla_hw_data *ha = vha->hw;
1559 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1561 ha->beacon_blink_led = 0;
1562 ha->beacon_color_state = QLA_LED_ALL_ON;
1564 ha->isp_ops->beacon_blink(vha); /* Will flip to all off. */
1566 /* Give control back to firmware. */
1567 spin_lock_irqsave(&ha->hardware_lock, flags);
1568 gpio_data = RD_REG_DWORD(®->gpiod);
1570 /* Disable the gpio_data reg for update. */
1571 gpio_data &= ~GPDX_LED_UPDATE_MASK;
1572 WRT_REG_DWORD(®->gpiod, gpio_data);
1573 RD_REG_DWORD(®->gpiod);
1574 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1576 ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
1578 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
1579 qla_printk(KERN_WARNING, ha,
1580 "Unable to update fw options (beacon off).\n");
1581 return QLA_FUNCTION_FAILED;
1584 if (qla2x00_get_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
1585 qla_printk(KERN_WARNING, ha,
1586 "Unable to get fw options (beacon off).\n");
1587 return QLA_FUNCTION_FAILED;
1595 * Flash support routines
1599 * qla2x00_flash_enable() - Setup flash for reading and writing.
1603 qla2x00_flash_enable(struct qla_hw_data *ha)
1606 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1608 data = RD_REG_WORD(®->ctrl_status);
1609 data |= CSR_FLASH_ENABLE;
1610 WRT_REG_WORD(®->ctrl_status, data);
1611 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1615 * qla2x00_flash_disable() - Disable flash and allow RISC to run.
1619 qla2x00_flash_disable(struct qla_hw_data *ha)
1622 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1624 data = RD_REG_WORD(®->ctrl_status);
1625 data &= ~(CSR_FLASH_ENABLE);
1626 WRT_REG_WORD(®->ctrl_status, data);
1627 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1631 * qla2x00_read_flash_byte() - Reads a byte from flash
1633 * @addr: Address in flash to read
1635 * A word is read from the chip, but, only the lower byte is valid.
1637 * Returns the byte read from flash @addr.
1640 qla2x00_read_flash_byte(struct qla_hw_data *ha, uint32_t addr)
1643 uint16_t bank_select;
1644 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1646 bank_select = RD_REG_WORD(®->ctrl_status);
1648 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1649 /* Specify 64K address range: */
1650 /* clear out Module Select and Flash Address bits [19:16]. */
1651 bank_select &= ~0xf8;
1652 bank_select |= addr >> 12 & 0xf0;
1653 bank_select |= CSR_FLASH_64K_BANK;
1654 WRT_REG_WORD(®->ctrl_status, bank_select);
1655 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1657 WRT_REG_WORD(®->flash_address, (uint16_t)addr);
1658 data = RD_REG_WORD(®->flash_data);
1660 return (uint8_t)data;
1663 /* Setup bit 16 of flash address. */
1664 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1665 bank_select |= CSR_FLASH_64K_BANK;
1666 WRT_REG_WORD(®->ctrl_status, bank_select);
1667 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1668 } else if (((addr & BIT_16) == 0) &&
1669 (bank_select & CSR_FLASH_64K_BANK)) {
1670 bank_select &= ~(CSR_FLASH_64K_BANK);
1671 WRT_REG_WORD(®->ctrl_status, bank_select);
1672 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1675 /* Always perform IO mapped accesses to the FLASH registers. */
1676 if (ha->pio_address) {
1679 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
1681 data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
1684 data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
1685 } while (data != data2);
1687 WRT_REG_WORD(®->flash_address, (uint16_t)addr);
1688 data = qla2x00_debounce_register(®->flash_data);
1691 return (uint8_t)data;
1695 * qla2x00_write_flash_byte() - Write a byte to flash
1697 * @addr: Address in flash to write
1698 * @data: Data to write
1701 qla2x00_write_flash_byte(struct qla_hw_data *ha, uint32_t addr, uint8_t data)
1703 uint16_t bank_select;
1704 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1706 bank_select = RD_REG_WORD(®->ctrl_status);
1707 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1708 /* Specify 64K address range: */
1709 /* clear out Module Select and Flash Address bits [19:16]. */
1710 bank_select &= ~0xf8;
1711 bank_select |= addr >> 12 & 0xf0;
1712 bank_select |= CSR_FLASH_64K_BANK;
1713 WRT_REG_WORD(®->ctrl_status, bank_select);
1714 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1716 WRT_REG_WORD(®->flash_address, (uint16_t)addr);
1717 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1718 WRT_REG_WORD(®->flash_data, (uint16_t)data);
1719 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1724 /* Setup bit 16 of flash address. */
1725 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1726 bank_select |= CSR_FLASH_64K_BANK;
1727 WRT_REG_WORD(®->ctrl_status, bank_select);
1728 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1729 } else if (((addr & BIT_16) == 0) &&
1730 (bank_select & CSR_FLASH_64K_BANK)) {
1731 bank_select &= ~(CSR_FLASH_64K_BANK);
1732 WRT_REG_WORD(®->ctrl_status, bank_select);
1733 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1736 /* Always perform IO mapped accesses to the FLASH registers. */
1737 if (ha->pio_address) {
1738 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
1739 WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
1741 WRT_REG_WORD(®->flash_address, (uint16_t)addr);
1742 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1743 WRT_REG_WORD(®->flash_data, (uint16_t)data);
1744 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
1749 * qla2x00_poll_flash() - Polls flash for completion.
1751 * @addr: Address in flash to poll
1752 * @poll_data: Data to be polled
1753 * @man_id: Flash manufacturer ID
1754 * @flash_id: Flash ID
1756 * This function polls the device until bit 7 of what is read matches data
1757 * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
1758 * out (a fatal error). The flash book recommeds reading bit 7 again after
1759 * reading bit 5 as a 1.
1761 * Returns 0 on success, else non-zero.
1764 qla2x00_poll_flash(struct qla_hw_data *ha, uint32_t addr, uint8_t poll_data,
1765 uint8_t man_id, uint8_t flash_id)
1773 /* Wait for 30 seconds for command to finish. */
1775 for (cnt = 3000000; cnt; cnt--) {
1776 flash_data = qla2x00_read_flash_byte(ha, addr);
1777 if ((flash_data & BIT_7) == poll_data) {
1782 if (man_id != 0x40 && man_id != 0xda) {
1783 if ((flash_data & BIT_5) && cnt > 2)
1794 * qla2x00_program_flash_address() - Programs a flash address
1796 * @addr: Address in flash to program
1797 * @data: Data to be written in flash
1798 * @man_id: Flash manufacturer ID
1799 * @flash_id: Flash ID
1801 * Returns 0 on success, else non-zero.
1804 qla2x00_program_flash_address(struct qla_hw_data *ha, uint32_t addr,
1805 uint8_t data, uint8_t man_id, uint8_t flash_id)
1807 /* Write Program Command Sequence. */
1808 if (IS_OEM_001(ha)) {
1809 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1810 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1811 qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
1812 qla2x00_write_flash_byte(ha, addr, data);
1814 if (man_id == 0xda && flash_id == 0xc1) {
1815 qla2x00_write_flash_byte(ha, addr, data);
1819 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1820 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1821 qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
1822 qla2x00_write_flash_byte(ha, addr, data);
1828 /* Wait for write to complete. */
1829 return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
1833 * qla2x00_erase_flash() - Erase the flash.
1835 * @man_id: Flash manufacturer ID
1836 * @flash_id: Flash ID
1838 * Returns 0 on success, else non-zero.
1841 qla2x00_erase_flash(struct qla_hw_data *ha, uint8_t man_id, uint8_t flash_id)
1843 /* Individual Sector Erase Command Sequence */
1844 if (IS_OEM_001(ha)) {
1845 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1846 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1847 qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
1848 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1849 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1850 qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
1852 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1853 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1854 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
1855 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1856 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1857 qla2x00_write_flash_byte(ha, 0x5555, 0x10);
1862 /* Wait for erase to complete. */
1863 return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
1867 * qla2x00_erase_flash_sector() - Erase a flash sector.
1869 * @addr: Flash sector to erase
1870 * @sec_mask: Sector address mask
1871 * @man_id: Flash manufacturer ID
1872 * @flash_id: Flash ID
1874 * Returns 0 on success, else non-zero.
1877 qla2x00_erase_flash_sector(struct qla_hw_data *ha, uint32_t addr,
1878 uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
1880 /* Individual Sector Erase Command Sequence */
1881 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1882 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1883 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
1884 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1885 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1886 if (man_id == 0x1f && flash_id == 0x13)
1887 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
1889 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
1893 /* Wait for erase to complete. */
1894 return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
1898 * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
1899 * @man_id: Flash manufacturer ID
1900 * @flash_id: Flash ID
1903 qla2x00_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
1906 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1907 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1908 qla2x00_write_flash_byte(ha, 0x5555, 0x90);
1909 *man_id = qla2x00_read_flash_byte(ha, 0x0000);
1910 *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
1911 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1912 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1913 qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
1917 qla2x00_read_flash_data(struct qla_hw_data *ha, uint8_t *tmp_buf,
1918 uint32_t saddr, uint32_t length)
1920 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1921 uint32_t midpoint, ilength;
1924 midpoint = length / 2;
1926 WRT_REG_WORD(®->nvram, 0);
1927 RD_REG_WORD(®->nvram);
1928 for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
1929 if (ilength == midpoint) {
1930 WRT_REG_WORD(®->nvram, NVR_SELECT);
1931 RD_REG_WORD(®->nvram);
1933 data = qla2x00_read_flash_byte(ha, saddr);
1942 qla2x00_suspend_hba(struct scsi_qla_host *vha)
1945 unsigned long flags;
1946 struct qla_hw_data *ha = vha->hw;
1947 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1950 scsi_block_requests(vha->host);
1951 ha->isp_ops->disable_intrs(ha);
1952 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
1955 spin_lock_irqsave(&ha->hardware_lock, flags);
1956 WRT_REG_WORD(®->hccr, HCCR_PAUSE_RISC);
1957 RD_REG_WORD(®->hccr);
1958 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
1959 for (cnt = 0; cnt < 30000; cnt++) {
1960 if ((RD_REG_WORD(®->hccr) & HCCR_RISC_PAUSE) != 0)
1967 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1971 qla2x00_resume_hba(struct scsi_qla_host *vha)
1973 struct qla_hw_data *ha = vha->hw;
1976 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
1977 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1978 qla2xxx_wake_dpc(vha);
1979 qla2x00_wait_for_chip_reset(vha);
1980 scsi_unblock_requests(vha->host);
1984 qla2x00_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
1985 uint32_t offset, uint32_t length)
1987 uint32_t addr, midpoint;
1989 struct qla_hw_data *ha = vha->hw;
1990 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1993 qla2x00_suspend_hba(vha);
1996 midpoint = ha->optrom_size / 2;
1998 qla2x00_flash_enable(ha);
1999 WRT_REG_WORD(®->nvram, 0);
2000 RD_REG_WORD(®->nvram); /* PCI Posting. */
2001 for (addr = offset, data = buf; addr < length; addr++, data++) {
2002 if (addr == midpoint) {
2003 WRT_REG_WORD(®->nvram, NVR_SELECT);
2004 RD_REG_WORD(®->nvram); /* PCI Posting. */
2007 *data = qla2x00_read_flash_byte(ha, addr);
2009 qla2x00_flash_disable(ha);
2012 qla2x00_resume_hba(vha);
2018 qla2x00_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2019 uint32_t offset, uint32_t length)
2023 uint8_t man_id, flash_id, sec_number, data;
2025 uint32_t addr, liter, sec_mask, rest_addr;
2026 struct qla_hw_data *ha = vha->hw;
2027 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2030 qla2x00_suspend_hba(vha);
2035 /* Reset ISP chip. */
2036 WRT_REG_WORD(®->ctrl_status, CSR_ISP_SOFT_RESET);
2037 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
2039 /* Go with write. */
2040 qla2x00_flash_enable(ha);
2041 do { /* Loop once to provide quick error exit */
2042 /* Structure of flash memory based on manufacturer */
2043 if (IS_OEM_001(ha)) {
2044 /* OEM variant with special flash part. */
2045 man_id = flash_id = 0;
2050 qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
2052 case 0x20: /* ST flash. */
2053 if (flash_id == 0xd2 || flash_id == 0xe3) {
2055 * ST m29w008at part - 64kb sector size with
2056 * 32kb,8kb,8kb,16kb sectors at memory address
2064 * ST m29w010b part - 16kb sector size
2065 * Default to 16kb sectors
2070 case 0x40: /* Mostel flash. */
2071 /* Mostel v29c51001 part - 512 byte sector size. */
2075 case 0xbf: /* SST flash. */
2076 /* SST39sf10 part - 4kb sector size. */
2080 case 0xda: /* Winbond flash. */
2081 /* Winbond W29EE011 part - 256 byte sector size. */
2085 case 0xc2: /* Macronix flash. */
2086 /* 64k sector size. */
2087 if (flash_id == 0x38 || flash_id == 0x4f) {
2092 /* Fall through... */
2094 case 0x1f: /* Atmel flash. */
2095 /* 512k sector size. */
2096 if (flash_id == 0x13) {
2097 rest_addr = 0x7fffffff;
2098 sec_mask = 0x80000000;
2101 /* Fall through... */
2103 case 0x01: /* AMD flash. */
2104 if (flash_id == 0x38 || flash_id == 0x40 ||
2106 /* Am29LV081 part - 64kb sector size. */
2107 /* Am29LV002BT part - 64kb sector size. */
2111 } else if (flash_id == 0x3e) {
2113 * Am29LV008b part - 64kb sector size with
2114 * 32kb,8kb,8kb,16kb sector at memory address
2120 } else if (flash_id == 0x20 || flash_id == 0x6e) {
2122 * Am29LV010 part or AM29f010 - 16kb sector
2128 } else if (flash_id == 0x6d) {
2129 /* Am29LV001 part - 8kb sector size. */
2135 /* Default to 16 kb sector size. */
2142 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2143 if (qla2x00_erase_flash(ha, man_id, flash_id)) {
2144 rval = QLA_FUNCTION_FAILED;
2149 for (addr = offset, liter = 0; liter < length; liter++,
2152 /* Are we at the beginning of a sector? */
2153 if ((addr & rest_addr) == 0) {
2154 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2155 if (addr >= 0x10000UL) {
2156 if (((addr >> 12) & 0xf0) &&
2158 flash_id == 0x3e) ||
2160 flash_id == 0xd2))) {
2162 if (sec_number == 1) {
2183 } else if (addr == ha->optrom_size / 2) {
2184 WRT_REG_WORD(®->nvram, NVR_SELECT);
2185 RD_REG_WORD(®->nvram);
2188 if (flash_id == 0xda && man_id == 0xc1) {
2189 qla2x00_write_flash_byte(ha, 0x5555,
2191 qla2x00_write_flash_byte(ha, 0x2aaa,
2193 qla2x00_write_flash_byte(ha, 0x5555,
2195 } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
2197 if (qla2x00_erase_flash_sector(ha,
2198 addr, sec_mask, man_id,
2200 rval = QLA_FUNCTION_FAILED;
2203 if (man_id == 0x01 && flash_id == 0x6d)
2208 if (man_id == 0x01 && flash_id == 0x6d) {
2209 if (sec_number == 1 &&
2210 addr == (rest_addr - 1)) {
2213 } else if (sec_number == 3 && (addr & 0x7ffe)) {
2219 if (qla2x00_program_flash_address(ha, addr, data,
2220 man_id, flash_id)) {
2221 rval = QLA_FUNCTION_FAILED;
2227 qla2x00_flash_disable(ha);
2230 qla2x00_resume_hba(vha);
2236 qla24xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2237 uint32_t offset, uint32_t length)
2239 struct qla_hw_data *ha = vha->hw;
2242 scsi_block_requests(vha->host);
2243 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2246 qla24xx_read_flash_data(vha, (uint32_t *)buf, offset >> 2, length >> 2);
2249 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2250 scsi_unblock_requests(vha->host);
2256 qla24xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2257 uint32_t offset, uint32_t length)
2260 struct qla_hw_data *ha = vha->hw;
2263 scsi_block_requests(vha->host);
2264 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2266 /* Go with write. */
2267 rval = qla24xx_write_flash_data(vha, (uint32_t *)buf, offset >> 2,
2270 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2271 scsi_unblock_requests(vha->host);
2277 qla25xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2278 uint32_t offset, uint32_t length)
2281 dma_addr_t optrom_dma;
2284 uint32_t faddr, left, burst;
2285 struct qla_hw_data *ha = vha->hw;
2289 if (length < OPTROM_BURST_SIZE)
2292 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2293 &optrom_dma, GFP_KERNEL);
2295 qla_printk(KERN_DEBUG, ha,
2296 "Unable to allocate memory for optrom burst read "
2297 "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
2303 faddr = offset >> 2;
2305 burst = OPTROM_BURST_DWORDS;
2310 rval = qla2x00_dump_ram(vha, optrom_dma,
2311 flash_data_addr(ha, faddr), burst);
2313 qla_printk(KERN_WARNING, ha,
2314 "Unable to burst-read optrom segment "
2315 "(%x/%x/%llx).\n", rval,
2316 flash_data_addr(ha, faddr),
2317 (unsigned long long)optrom_dma);
2318 qla_printk(KERN_WARNING, ha,
2319 "Reverting to slow-read.\n");
2321 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2322 optrom, optrom_dma);
2326 memcpy(pbuf, optrom, burst * 4);
2333 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
2339 return qla24xx_read_optrom_data(vha, buf, offset, length);
2343 * qla2x00_get_fcode_version() - Determine an FCODE image's version.
2345 * @pcids: Pointer to the FCODE PCI data structure
2347 * The process of retrieving the FCODE version information is at best
2348 * described as interesting.
2350 * Within the first 100h bytes of the image an ASCII string is present
2351 * which contains several pieces of information including the FCODE
2352 * version. Unfortunately it seems the only reliable way to retrieve
2353 * the version is by scanning for another sentinel within the string,
2354 * the FCODE build date:
2356 * ... 2.00.02 10/17/02 ...
2358 * Returns QLA_SUCCESS on successful retrieval of version.
2361 qla2x00_get_fcode_version(struct qla_hw_data *ha, uint32_t pcids)
2363 int ret = QLA_FUNCTION_FAILED;
2364 uint32_t istart, iend, iter, vend;
2365 uint8_t do_next, rbyte, *vbyte;
2367 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2369 /* Skip the PCI data structure. */
2371 ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
2372 qla2x00_read_flash_byte(ha, pcids + 0x0A));
2373 iend = istart + 0x100;
2375 /* Scan for the sentinel date string...eeewww. */
2378 while ((iter < iend) && !do_next) {
2380 if (qla2x00_read_flash_byte(ha, iter) == '/') {
2381 if (qla2x00_read_flash_byte(ha, iter + 2) ==
2384 else if (qla2x00_read_flash_byte(ha,
2392 /* Backtrack to previous ' ' (space). */
2394 while ((iter > istart) && !do_next) {
2396 if (qla2x00_read_flash_byte(ha, iter) == ' ')
2403 * Mark end of version tag, and find previous ' ' (space) or
2404 * string length (recent FCODE images -- major hack ahead!!!).
2408 while ((iter > istart) && !do_next) {
2410 rbyte = qla2x00_read_flash_byte(ha, iter);
2411 if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
2417 /* Mark beginning of version tag, and copy data. */
2419 if ((vend - iter) &&
2420 ((vend - iter) < sizeof(ha->fcode_revision))) {
2421 vbyte = ha->fcode_revision;
2422 while (iter <= vend) {
2423 *vbyte++ = qla2x00_read_flash_byte(ha, iter);
2430 if (ret != QLA_SUCCESS)
2431 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2435 qla2x00_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
2437 int ret = QLA_SUCCESS;
2438 uint8_t code_type, last_image;
2439 uint32_t pcihdr, pcids;
2442 struct qla_hw_data *ha = vha->hw;
2444 if (!ha->pio_address || !mbuf)
2445 return QLA_FUNCTION_FAILED;
2447 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2448 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2449 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2450 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2452 qla2x00_flash_enable(ha);
2454 /* Begin with first PCI expansion ROM header. */
2458 /* Verify PCI expansion ROM header. */
2459 if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
2460 qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
2462 DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
2464 ret = QLA_FUNCTION_FAILED;
2468 /* Locate PCI data structure. */
2470 ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
2471 qla2x00_read_flash_byte(ha, pcihdr + 0x18));
2473 /* Validate signature of PCI data structure. */
2474 if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
2475 qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
2476 qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
2477 qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
2478 /* Incorrect header. */
2479 DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
2480 "found pcir_adr=%x.\n", pcids));
2481 ret = QLA_FUNCTION_FAILED;
2486 code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
2487 switch (code_type) {
2488 case ROM_CODE_TYPE_BIOS:
2489 /* Intel x86, PC-AT compatible. */
2490 ha->bios_revision[0] =
2491 qla2x00_read_flash_byte(ha, pcids + 0x12);
2492 ha->bios_revision[1] =
2493 qla2x00_read_flash_byte(ha, pcids + 0x13);
2494 DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
2495 ha->bios_revision[1], ha->bios_revision[0]));
2497 case ROM_CODE_TYPE_FCODE:
2498 /* Open Firmware standard for PCI (FCode). */
2500 qla2x00_get_fcode_version(ha, pcids);
2502 case ROM_CODE_TYPE_EFI:
2503 /* Extensible Firmware Interface (EFI). */
2504 ha->efi_revision[0] =
2505 qla2x00_read_flash_byte(ha, pcids + 0x12);
2506 ha->efi_revision[1] =
2507 qla2x00_read_flash_byte(ha, pcids + 0x13);
2508 DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
2509 ha->efi_revision[1], ha->efi_revision[0]));
2512 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
2513 "type %x at pcids %x.\n", code_type, pcids));
2517 last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
2519 /* Locate next PCI expansion ROM. */
2520 pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
2521 qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
2522 } while (!last_image);
2524 if (IS_QLA2322(ha)) {
2525 /* Read firmware image information. */
2526 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2528 memset(dbyte, 0, 8);
2529 dcode = (uint16_t *)dbyte;
2531 qla2x00_read_flash_data(ha, dbyte, ha->flt_region_fw * 4 + 10,
2533 DEBUG3(qla_printk(KERN_DEBUG, ha, "dumping fw ver from "
2535 DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
2537 if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
2538 dcode[2] == 0xffff && dcode[3] == 0xffff) ||
2539 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2541 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
2542 "revision at %x.\n", ha->flt_region_fw * 4));
2544 /* values are in big endian */
2545 ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
2546 ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
2547 ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
2551 qla2x00_flash_disable(ha);
2557 qla24xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
2559 int ret = QLA_SUCCESS;
2560 uint32_t pcihdr, pcids;
2563 uint8_t code_type, last_image;
2565 struct qla_hw_data *ha = vha->hw;
2568 return QLA_FUNCTION_FAILED;
2570 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2571 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2572 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2573 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2577 /* Begin with first PCI expansion ROM header. */
2578 pcihdr = ha->flt_region_boot << 2;
2581 /* Verify PCI expansion ROM header. */
2582 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
2583 bcode = mbuf + (pcihdr % 4);
2584 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
2586 DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
2588 ret = QLA_FUNCTION_FAILED;
2592 /* Locate PCI data structure. */
2593 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
2595 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
2596 bcode = mbuf + (pcihdr % 4);
2598 /* Validate signature of PCI data structure. */
2599 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
2600 bcode[0x2] != 'I' || bcode[0x3] != 'R') {
2601 /* Incorrect header. */
2602 DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
2603 "found pcir_adr=%x.\n", pcids));
2604 ret = QLA_FUNCTION_FAILED;
2609 code_type = bcode[0x14];
2610 switch (code_type) {
2611 case ROM_CODE_TYPE_BIOS:
2612 /* Intel x86, PC-AT compatible. */
2613 ha->bios_revision[0] = bcode[0x12];
2614 ha->bios_revision[1] = bcode[0x13];
2615 DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
2616 ha->bios_revision[1], ha->bios_revision[0]));
2618 case ROM_CODE_TYPE_FCODE:
2619 /* Open Firmware standard for PCI (FCode). */
2620 ha->fcode_revision[0] = bcode[0x12];
2621 ha->fcode_revision[1] = bcode[0x13];
2622 DEBUG3(qla_printk(KERN_DEBUG, ha, "read FCODE %d.%d.\n",
2623 ha->fcode_revision[1], ha->fcode_revision[0]));
2625 case ROM_CODE_TYPE_EFI:
2626 /* Extensible Firmware Interface (EFI). */
2627 ha->efi_revision[0] = bcode[0x12];
2628 ha->efi_revision[1] = bcode[0x13];
2629 DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
2630 ha->efi_revision[1], ha->efi_revision[0]));
2633 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
2634 "type %x at pcids %x.\n", code_type, pcids));
2638 last_image = bcode[0x15] & BIT_7;
2640 /* Locate next PCI expansion ROM. */
2641 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
2642 } while (!last_image);
2644 /* Read firmware image information. */
2645 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2648 qla24xx_read_flash_data(vha, dcode, ha->flt_region_fw + 4, 4);
2649 for (i = 0; i < 4; i++)
2650 dcode[i] = be32_to_cpu(dcode[i]);
2652 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
2653 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
2654 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2656 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
2657 "revision at %x.\n", ha->flt_region_fw * 4));
2659 ha->fw_revision[0] = dcode[0];
2660 ha->fw_revision[1] = dcode[1];
2661 ha->fw_revision[2] = dcode[2];
2662 ha->fw_revision[3] = dcode[3];
2669 qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
2671 if (pos >= end || *pos != 0x82)
2675 if (pos >= end || *pos != 0x90)
2679 if (pos >= end || *pos != 0x78)
2686 qla2xxx_get_vpd_field(scsi_qla_host_t *vha, char *key, char *str, size_t size)
2688 struct qla_hw_data *ha = vha->hw;
2689 uint8_t *pos = ha->vpd;
2690 uint8_t *end = pos + ha->vpd_size;
2693 if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
2696 while (pos < end && *pos != 0x78) {
2697 len = (*pos == 0x82) ? pos[1] : pos[2];
2699 if (!strncmp(pos, key, strlen(key)))
2702 if (*pos != 0x90 && *pos != 0x91)
2708 if (pos < end - len && *pos != 0x78)
2709 return snprintf(str, size, "%.*s", len, pos + 3);