[SCSI] qla2xxx: Rework firmware-trace facilities.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / qla2xxx / qla_init.c
blob880de6f380e96d1d3bd2c9bba24520abf6e1c7a7
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 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/vmalloc.h>
12 #include "qla_devtbl.h"
14 /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
15 #ifndef EXT_IS_LUN_BIT_SET
16 #define EXT_IS_LUN_BIT_SET(P,L) \
17 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
18 #define EXT_SET_LUN_BIT(P,L) \
19 ((P)->mask[L/8] |= (0x80 >> (L%8)))
20 #endif
23 * QLogic ISP2x00 Hardware Support Function Prototypes.
25 static int qla2x00_isp_firmware(scsi_qla_host_t *);
26 static void qla2x00_resize_request_q(scsi_qla_host_t *);
27 static int qla2x00_setup_chip(scsi_qla_host_t *);
28 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
29 static int qla2x00_init_rings(scsi_qla_host_t *);
30 static int qla2x00_fw_ready(scsi_qla_host_t *);
31 static int qla2x00_configure_hba(scsi_qla_host_t *);
32 static int qla2x00_configure_loop(scsi_qla_host_t *);
33 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
34 static int qla2x00_configure_fabric(scsi_qla_host_t *);
35 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
36 static int qla2x00_device_resync(scsi_qla_host_t *);
37 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
38 uint16_t *);
40 static int qla2x00_restart_isp(scsi_qla_host_t *);
42 /****************************************************************************/
43 /* QLogic ISP2x00 Hardware Support Functions. */
44 /****************************************************************************/
47 * qla2x00_initialize_adapter
48 * Initialize board.
50 * Input:
51 * ha = adapter block pointer.
53 * Returns:
54 * 0 = success
56 int
57 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
59 int rval;
60 uint8_t restart_risc = 0;
61 uint8_t retry;
62 uint32_t wait_time;
64 /* Clear adapter flags. */
65 ha->flags.online = 0;
66 ha->flags.reset_active = 0;
67 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
68 atomic_set(&ha->loop_state, LOOP_DOWN);
69 ha->device_flags = 0;
70 ha->dpc_flags = 0;
71 ha->flags.management_server_logged_in = 0;
72 ha->marker_needed = 0;
73 ha->mbx_flags = 0;
74 ha->isp_abort_cnt = 0;
75 ha->beacon_blink_led = 0;
76 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
78 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
79 rval = ha->isp_ops.pci_config(ha);
80 if (rval) {
81 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
82 ha->host_no));
83 return (rval);
86 ha->isp_ops.reset_chip(ha);
88 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
90 ha->isp_ops.nvram_config(ha);
92 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
94 retry = 10;
96 * Try to configure the loop.
98 do {
99 restart_risc = 0;
101 /* If firmware needs to be loaded */
102 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
103 if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
104 rval = qla2x00_setup_chip(ha);
108 if (rval == QLA_SUCCESS &&
109 (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
110 check_fw_ready_again:
112 * Wait for a successful LIP up to a maximum
113 * of (in seconds): RISC login timeout value,
114 * RISC retry count value, and port down retry
115 * value OR a minimum of 4 seconds OR If no
116 * cable, only 5 seconds.
118 rval = qla2x00_fw_ready(ha);
119 if (rval == QLA_SUCCESS) {
120 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
122 /* Issue a marker after FW becomes ready. */
123 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
126 * Wait at most MAX_TARGET RSCNs for a stable
127 * link.
129 wait_time = 256;
130 do {
131 clear_bit(LOOP_RESYNC_NEEDED,
132 &ha->dpc_flags);
133 rval = qla2x00_configure_loop(ha);
135 if (test_and_clear_bit(ISP_ABORT_NEEDED,
136 &ha->dpc_flags)) {
137 restart_risc = 1;
138 break;
142 * If loop state change while we were
143 * discoverying devices then wait for
144 * LIP to complete
147 if (atomic_read(&ha->loop_state) !=
148 LOOP_READY && retry--) {
149 goto check_fw_ready_again;
151 wait_time--;
152 } while (!atomic_read(&ha->loop_down_timer) &&
153 retry &&
154 wait_time &&
155 (test_bit(LOOP_RESYNC_NEEDED,
156 &ha->dpc_flags)));
158 if (wait_time == 0)
159 rval = QLA_FUNCTION_FAILED;
160 } else if (ha->device_flags & DFLG_NO_CABLE)
161 /* If no cable, then all is good. */
162 rval = QLA_SUCCESS;
164 } while (restart_risc && retry--);
166 if (rval == QLA_SUCCESS) {
167 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
168 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
169 ha->marker_needed = 0;
171 ha->flags.online = 1;
172 } else {
173 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
176 return (rval);
180 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
181 * @ha: HA context
183 * Returns 0 on success.
186 qla2100_pci_config(scsi_qla_host_t *ha)
188 uint16_t w, mwi;
189 uint32_t d;
190 unsigned long flags;
191 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
193 pci_set_master(ha->pdev);
194 mwi = 0;
195 if (pci_set_mwi(ha->pdev))
196 mwi = PCI_COMMAND_INVALIDATE;
198 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
199 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
200 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
202 /* Reset expansion ROM address decode enable */
203 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
204 d &= ~PCI_ROM_ADDRESS_ENABLE;
205 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
207 /* Get PCI bus information. */
208 spin_lock_irqsave(&ha->hardware_lock, flags);
209 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
210 spin_unlock_irqrestore(&ha->hardware_lock, flags);
212 return QLA_SUCCESS;
216 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
217 * @ha: HA context
219 * Returns 0 on success.
222 qla2300_pci_config(scsi_qla_host_t *ha)
224 uint16_t w, mwi;
225 uint32_t d;
226 unsigned long flags = 0;
227 uint32_t cnt;
228 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
230 pci_set_master(ha->pdev);
231 mwi = 0;
232 if (pci_set_mwi(ha->pdev))
233 mwi = PCI_COMMAND_INVALIDATE;
235 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
236 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
238 if (IS_QLA2322(ha) || IS_QLA6322(ha))
239 w &= ~PCI_COMMAND_INTX_DISABLE;
242 * If this is a 2300 card and not 2312, reset the
243 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
244 * the 2310 also reports itself as a 2300 so we need to get the
245 * fb revision level -- a 6 indicates it really is a 2300 and
246 * not a 2310.
248 if (IS_QLA2300(ha)) {
249 spin_lock_irqsave(&ha->hardware_lock, flags);
251 /* Pause RISC. */
252 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
253 for (cnt = 0; cnt < 30000; cnt++) {
254 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
255 break;
257 udelay(10);
260 /* Select FPM registers. */
261 WRT_REG_WORD(&reg->ctrl_status, 0x20);
262 RD_REG_WORD(&reg->ctrl_status);
264 /* Get the fb rev level */
265 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
267 if (ha->fb_rev == FPM_2300)
268 w &= ~PCI_COMMAND_INVALIDATE;
270 /* Deselect FPM registers. */
271 WRT_REG_WORD(&reg->ctrl_status, 0x0);
272 RD_REG_WORD(&reg->ctrl_status);
274 /* Release RISC module. */
275 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
276 for (cnt = 0; cnt < 30000; cnt++) {
277 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
278 break;
280 udelay(10);
283 spin_unlock_irqrestore(&ha->hardware_lock, flags);
285 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
287 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
289 /* Reset expansion ROM address decode enable */
290 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
291 d &= ~PCI_ROM_ADDRESS_ENABLE;
292 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
294 /* Get PCI bus information. */
295 spin_lock_irqsave(&ha->hardware_lock, flags);
296 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
297 spin_unlock_irqrestore(&ha->hardware_lock, flags);
299 return QLA_SUCCESS;
303 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
304 * @ha: HA context
306 * Returns 0 on success.
309 qla24xx_pci_config(scsi_qla_host_t *ha)
311 uint16_t w, mwi;
312 uint32_t d;
313 unsigned long flags = 0;
314 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
315 int pcix_cmd_reg, pcie_dctl_reg;
317 pci_set_master(ha->pdev);
318 mwi = 0;
319 if (pci_set_mwi(ha->pdev))
320 mwi = PCI_COMMAND_INVALIDATE;
322 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
323 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
324 w &= ~PCI_COMMAND_INTX_DISABLE;
325 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
327 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
329 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
330 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
331 if (pcix_cmd_reg) {
332 uint16_t pcix_cmd;
334 pcix_cmd_reg += PCI_X_CMD;
335 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
336 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
337 pcix_cmd |= 0x0008;
338 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
341 /* PCIe -- adjust Maximum Read Request Size (2048). */
342 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
343 if (pcie_dctl_reg) {
344 uint16_t pcie_dctl;
346 pcie_dctl_reg += PCI_EXP_DEVCTL;
347 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
348 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
349 pcie_dctl |= 0x4000;
350 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
353 /* Reset expansion ROM address decode enable */
354 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
355 d &= ~PCI_ROM_ADDRESS_ENABLE;
356 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
358 /* Get PCI bus information. */
359 spin_lock_irqsave(&ha->hardware_lock, flags);
360 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
361 spin_unlock_irqrestore(&ha->hardware_lock, flags);
363 return QLA_SUCCESS;
367 * qla2x00_isp_firmware() - Choose firmware image.
368 * @ha: HA context
370 * Returns 0 on success.
372 static int
373 qla2x00_isp_firmware(scsi_qla_host_t *ha)
375 int rval;
377 /* Assume loading risc code */
378 rval = QLA_FUNCTION_FAILED;
380 if (ha->flags.disable_risc_code_load) {
381 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
382 ha->host_no));
383 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
385 /* Verify checksum of loaded RISC code. */
386 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
389 if (rval) {
390 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
391 ha->host_no));
394 return (rval);
398 * qla2x00_reset_chip() - Reset ISP chip.
399 * @ha: HA context
401 * Returns 0 on success.
403 void
404 qla2x00_reset_chip(scsi_qla_host_t *ha)
406 unsigned long flags = 0;
407 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
408 uint32_t cnt;
409 uint16_t cmd;
411 ha->isp_ops.disable_intrs(ha);
413 spin_lock_irqsave(&ha->hardware_lock, flags);
415 /* Turn off master enable */
416 cmd = 0;
417 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
418 cmd &= ~PCI_COMMAND_MASTER;
419 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
421 if (!IS_QLA2100(ha)) {
422 /* Pause RISC. */
423 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
424 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
425 for (cnt = 0; cnt < 30000; cnt++) {
426 if ((RD_REG_WORD(&reg->hccr) &
427 HCCR_RISC_PAUSE) != 0)
428 break;
429 udelay(100);
431 } else {
432 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
433 udelay(10);
436 /* Select FPM registers. */
437 WRT_REG_WORD(&reg->ctrl_status, 0x20);
438 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
440 /* FPM Soft Reset. */
441 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
442 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
444 /* Toggle Fpm Reset. */
445 if (!IS_QLA2200(ha)) {
446 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
447 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
450 /* Select frame buffer registers. */
451 WRT_REG_WORD(&reg->ctrl_status, 0x10);
452 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
454 /* Reset frame buffer FIFOs. */
455 if (IS_QLA2200(ha)) {
456 WRT_FB_CMD_REG(ha, reg, 0xa000);
457 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
458 } else {
459 WRT_FB_CMD_REG(ha, reg, 0x00fc);
461 /* Read back fb_cmd until zero or 3 seconds max */
462 for (cnt = 0; cnt < 3000; cnt++) {
463 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
464 break;
465 udelay(100);
469 /* Select RISC module registers. */
470 WRT_REG_WORD(&reg->ctrl_status, 0);
471 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
473 /* Reset RISC processor. */
474 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
475 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
477 /* Release RISC processor. */
478 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
479 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
482 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
483 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
485 /* Reset ISP chip. */
486 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
488 /* Wait for RISC to recover from reset. */
489 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
491 * It is necessary to for a delay here since the card doesn't
492 * respond to PCI reads during a reset. On some architectures
493 * this will result in an MCA.
495 udelay(20);
496 for (cnt = 30000; cnt; cnt--) {
497 if ((RD_REG_WORD(&reg->ctrl_status) &
498 CSR_ISP_SOFT_RESET) == 0)
499 break;
500 udelay(100);
502 } else
503 udelay(10);
505 /* Reset RISC processor. */
506 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
508 WRT_REG_WORD(&reg->semaphore, 0);
510 /* Release RISC processor. */
511 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
512 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
514 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
515 for (cnt = 0; cnt < 30000; cnt++) {
516 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
517 break;
519 udelay(100);
521 } else
522 udelay(100);
524 /* Turn on master enable */
525 cmd |= PCI_COMMAND_MASTER;
526 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
528 /* Disable RISC pause on FPM parity error. */
529 if (!IS_QLA2100(ha)) {
530 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
531 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
534 spin_unlock_irqrestore(&ha->hardware_lock, flags);
538 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
539 * @ha: HA context
541 * Returns 0 on success.
543 static inline void
544 qla24xx_reset_risc(scsi_qla_host_t *ha)
546 unsigned long flags = 0;
547 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
548 uint32_t cnt, d2;
549 uint16_t wd;
551 spin_lock_irqsave(&ha->hardware_lock, flags);
553 /* Reset RISC. */
554 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
555 for (cnt = 0; cnt < 30000; cnt++) {
556 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
557 break;
559 udelay(10);
562 WRT_REG_DWORD(&reg->ctrl_status,
563 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
564 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
566 udelay(100);
567 /* Wait for firmware to complete NVRAM accesses. */
568 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
569 for (cnt = 10000 ; cnt && d2; cnt--) {
570 udelay(5);
571 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
572 barrier();
575 /* Wait for soft-reset to complete. */
576 d2 = RD_REG_DWORD(&reg->ctrl_status);
577 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
578 udelay(5);
579 d2 = RD_REG_DWORD(&reg->ctrl_status);
580 barrier();
583 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
584 RD_REG_DWORD(&reg->hccr);
586 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
587 RD_REG_DWORD(&reg->hccr);
589 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
590 RD_REG_DWORD(&reg->hccr);
592 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
593 for (cnt = 6000000 ; cnt && d2; cnt--) {
594 udelay(5);
595 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
596 barrier();
599 spin_unlock_irqrestore(&ha->hardware_lock, flags);
603 * qla24xx_reset_chip() - Reset ISP24xx chip.
604 * @ha: HA context
606 * Returns 0 on success.
608 void
609 qla24xx_reset_chip(scsi_qla_host_t *ha)
611 ha->isp_ops.disable_intrs(ha);
613 /* Perform RISC reset. */
614 qla24xx_reset_risc(ha);
618 * qla2x00_chip_diag() - Test chip for proper operation.
619 * @ha: HA context
621 * Returns 0 on success.
624 qla2x00_chip_diag(scsi_qla_host_t *ha)
626 int rval;
627 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
628 unsigned long flags = 0;
629 uint16_t data;
630 uint32_t cnt;
631 uint16_t mb[5];
633 /* Assume a failed state */
634 rval = QLA_FUNCTION_FAILED;
636 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
637 ha->host_no, (u_long)&reg->flash_address));
639 spin_lock_irqsave(&ha->hardware_lock, flags);
641 /* Reset ISP chip. */
642 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
645 * We need to have a delay here since the card will not respond while
646 * in reset causing an MCA on some architectures.
648 udelay(20);
649 data = qla2x00_debounce_register(&reg->ctrl_status);
650 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
651 udelay(5);
652 data = RD_REG_WORD(&reg->ctrl_status);
653 barrier();
656 if (!cnt)
657 goto chip_diag_failed;
659 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
660 ha->host_no));
662 /* Reset RISC processor. */
663 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
664 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
666 /* Workaround for QLA2312 PCI parity error */
667 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
668 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
669 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
670 udelay(5);
671 data = RD_MAILBOX_REG(ha, reg, 0);
672 barrier();
674 } else
675 udelay(10);
677 if (!cnt)
678 goto chip_diag_failed;
680 /* Check product ID of chip */
681 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
683 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
684 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
685 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
686 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
687 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
688 mb[3] != PROD_ID_3) {
689 qla_printk(KERN_WARNING, ha,
690 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
692 goto chip_diag_failed;
694 ha->product_id[0] = mb[1];
695 ha->product_id[1] = mb[2];
696 ha->product_id[2] = mb[3];
697 ha->product_id[3] = mb[4];
699 /* Adjust fw RISC transfer size */
700 if (ha->request_q_length > 1024)
701 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
702 else
703 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
704 ha->request_q_length;
706 if (IS_QLA2200(ha) &&
707 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
708 /* Limit firmware transfer size with a 2200A */
709 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
710 ha->host_no));
712 ha->device_type |= DT_ISP2200A;
713 ha->fw_transfer_size = 128;
716 /* Wrap Incoming Mailboxes Test. */
717 spin_unlock_irqrestore(&ha->hardware_lock, flags);
719 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
720 rval = qla2x00_mbx_reg_test(ha);
721 if (rval) {
722 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
723 ha->host_no));
724 qla_printk(KERN_WARNING, ha,
725 "Failed mailbox send register test\n");
727 else {
728 /* Flag a successful rval */
729 rval = QLA_SUCCESS;
731 spin_lock_irqsave(&ha->hardware_lock, flags);
733 chip_diag_failed:
734 if (rval)
735 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
736 "****\n", ha->host_no));
738 spin_unlock_irqrestore(&ha->hardware_lock, flags);
740 return (rval);
744 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
745 * @ha: HA context
747 * Returns 0 on success.
750 qla24xx_chip_diag(scsi_qla_host_t *ha)
752 int rval;
754 /* Perform RISC reset. */
755 qla24xx_reset_risc(ha);
757 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
759 rval = qla2x00_mbx_reg_test(ha);
760 if (rval) {
761 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
762 ha->host_no));
763 qla_printk(KERN_WARNING, ha,
764 "Failed mailbox send register test\n");
765 } else {
766 /* Flag a successful rval */
767 rval = QLA_SUCCESS;
770 return rval;
773 void
774 qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
776 int rval;
777 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
778 eft_size;
779 dma_addr_t eft_dma;
780 void *eft;
782 if (ha->fw_dump) {
783 qla_printk(KERN_WARNING, ha,
784 "Firmware dump previously allocated.\n");
785 return;
788 ha->fw_dumped = 0;
789 fixed_size = mem_size = eft_size = 0;
790 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
791 fixed_size = sizeof(struct qla2100_fw_dump);
792 } else if (IS_QLA23XX(ha)) {
793 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
794 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
795 sizeof(uint16_t);
796 } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
797 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
798 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
799 sizeof(uint32_t);
801 /* Allocate memory for Extended Trace Buffer. */
802 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
803 GFP_KERNEL);
804 if (!eft) {
805 qla_printk(KERN_WARNING, ha, "Unable to allocate "
806 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
807 goto cont_alloc;
810 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
811 EFT_NUM_BUFFERS);
812 if (rval) {
813 qla_printk(KERN_WARNING, ha, "Unable to initialize "
814 "EFT (%d).\n", rval);
815 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
816 eft_dma);
817 goto cont_alloc;
820 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
821 EFT_SIZE / 1024);
823 eft_size = EFT_SIZE;
824 memset(eft, 0, eft_size);
825 ha->eft_dma = eft_dma;
826 ha->eft = eft;
828 cont_alloc:
829 req_q_size = ha->request_q_length * sizeof(request_t);
830 rsp_q_size = ha->response_q_length * sizeof(response_t);
832 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
833 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
834 eft_size;
836 ha->fw_dump = vmalloc(dump_size);
837 if (!ha->fw_dump) {
838 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
839 "firmware dump!!!\n", dump_size / 1024);
841 if (ha->eft) {
842 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
843 ha->eft_dma);
844 ha->eft = NULL;
845 ha->eft_dma = 0;
847 return;
850 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
851 dump_size / 1024);
853 ha->fw_dump_len = dump_size;
854 ha->fw_dump->signature[0] = 'Q';
855 ha->fw_dump->signature[1] = 'L';
856 ha->fw_dump->signature[2] = 'G';
857 ha->fw_dump->signature[3] = 'C';
858 ha->fw_dump->version = __constant_htonl(1);
860 ha->fw_dump->fixed_size = htonl(fixed_size);
861 ha->fw_dump->mem_size = htonl(mem_size);
862 ha->fw_dump->req_q_size = htonl(req_q_size);
863 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
865 ha->fw_dump->eft_size = htonl(eft_size);
866 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
867 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
869 ha->fw_dump->header_size =
870 htonl(offsetof(struct qla2xxx_fw_dump, isp));
874 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
875 * @ha: HA context
877 * Returns 0 on success.
879 static void
880 qla2x00_resize_request_q(scsi_qla_host_t *ha)
882 int rval;
883 uint16_t fw_iocb_cnt = 0;
884 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
885 dma_addr_t request_dma;
886 request_t *request_ring;
888 /* Valid only on recent ISPs. */
889 if (IS_QLA2100(ha) || IS_QLA2200(ha))
890 return;
892 /* Retrieve IOCB counts available to the firmware. */
893 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
894 if (rval)
895 return;
896 /* No point in continuing if current settings are sufficient. */
897 if (fw_iocb_cnt < 1024)
898 return;
899 if (ha->request_q_length >= request_q_length)
900 return;
902 /* Attempt to claim larger area for request queue. */
903 request_ring = dma_alloc_coherent(&ha->pdev->dev,
904 (request_q_length + 1) * sizeof(request_t), &request_dma,
905 GFP_KERNEL);
906 if (request_ring == NULL)
907 return;
909 /* Resize successful, report extensions. */
910 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
911 (ha->fw_memory_size + 1) / 1024);
912 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
913 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
915 /* Clear old allocations. */
916 dma_free_coherent(&ha->pdev->dev,
917 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
918 ha->request_dma);
920 /* Begin using larger queue. */
921 ha->request_q_length = request_q_length;
922 ha->request_ring = request_ring;
923 ha->request_dma = request_dma;
927 * qla2x00_setup_chip() - Load and start RISC firmware.
928 * @ha: HA context
930 * Returns 0 on success.
932 static int
933 qla2x00_setup_chip(scsi_qla_host_t *ha)
935 int rval;
936 uint32_t srisc_address = 0;
938 /* Load firmware sequences */
939 rval = ha->isp_ops.load_risc(ha, &srisc_address);
940 if (rval == QLA_SUCCESS) {
941 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
942 "code.\n", ha->host_no));
944 rval = qla2x00_verify_checksum(ha, srisc_address);
945 if (rval == QLA_SUCCESS) {
946 /* Start firmware execution. */
947 DEBUG(printk("scsi(%ld): Checksum OK, start "
948 "firmware.\n", ha->host_no));
950 rval = qla2x00_execute_fw(ha, srisc_address);
951 /* Retrieve firmware information. */
952 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
953 qla2x00_get_fw_version(ha,
954 &ha->fw_major_version,
955 &ha->fw_minor_version,
956 &ha->fw_subminor_version,
957 &ha->fw_attributes, &ha->fw_memory_size);
958 qla2x00_resize_request_q(ha);
960 if (ql2xallocfwdump)
961 qla2x00_alloc_fw_dump(ha);
963 } else {
964 DEBUG2(printk(KERN_INFO
965 "scsi(%ld): ISP Firmware failed checksum.\n",
966 ha->host_no));
970 if (rval) {
971 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
972 ha->host_no));
975 return (rval);
979 * qla2x00_init_response_q_entries() - Initializes response queue entries.
980 * @ha: HA context
982 * Beginning of request ring has initialization control block already built
983 * by nvram config routine.
985 * Returns 0 on success.
987 static void
988 qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
990 uint16_t cnt;
991 response_t *pkt;
993 pkt = ha->response_ring_ptr;
994 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
995 pkt->signature = RESPONSE_PROCESSED;
996 pkt++;
1002 * qla2x00_update_fw_options() - Read and process firmware options.
1003 * @ha: HA context
1005 * Returns 0 on success.
1007 void
1008 qla2x00_update_fw_options(scsi_qla_host_t *ha)
1010 uint16_t swing, emphasis, tx_sens, rx_sens;
1012 memset(ha->fw_options, 0, sizeof(ha->fw_options));
1013 qla2x00_get_fw_options(ha, ha->fw_options);
1015 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1016 return;
1018 /* Serial Link options. */
1019 DEBUG3(printk("scsi(%ld): Serial link options:\n",
1020 ha->host_no));
1021 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1022 sizeof(ha->fw_seriallink_options)));
1024 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1025 if (ha->fw_seriallink_options[3] & BIT_2) {
1026 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1028 /* 1G settings */
1029 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1030 emphasis = (ha->fw_seriallink_options[2] &
1031 (BIT_4 | BIT_3)) >> 3;
1032 tx_sens = ha->fw_seriallink_options[0] &
1033 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1034 rx_sens = (ha->fw_seriallink_options[0] &
1035 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1036 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1037 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1038 if (rx_sens == 0x0)
1039 rx_sens = 0x3;
1040 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1041 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1042 ha->fw_options[10] |= BIT_5 |
1043 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1044 (tx_sens & (BIT_1 | BIT_0));
1046 /* 2G settings */
1047 swing = (ha->fw_seriallink_options[2] &
1048 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1049 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1050 tx_sens = ha->fw_seriallink_options[1] &
1051 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1052 rx_sens = (ha->fw_seriallink_options[1] &
1053 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1054 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1055 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1056 if (rx_sens == 0x0)
1057 rx_sens = 0x3;
1058 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1059 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1060 ha->fw_options[11] |= BIT_5 |
1061 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1062 (tx_sens & (BIT_1 | BIT_0));
1065 /* FCP2 options. */
1066 /* Return command IOCBs without waiting for an ABTS to complete. */
1067 ha->fw_options[3] |= BIT_13;
1069 /* LED scheme. */
1070 if (ha->flags.enable_led_scheme)
1071 ha->fw_options[2] |= BIT_12;
1073 /* Detect ISP6312. */
1074 if (IS_QLA6312(ha))
1075 ha->fw_options[2] |= BIT_13;
1077 /* Update firmware options. */
1078 qla2x00_set_fw_options(ha, ha->fw_options);
1081 void
1082 qla24xx_update_fw_options(scsi_qla_host_t *ha)
1084 int rval;
1086 /* Update Serial Link options. */
1087 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
1088 return;
1090 rval = qla2x00_set_serdes_params(ha,
1091 le16_to_cpu(ha->fw_seriallink_options24[1]),
1092 le16_to_cpu(ha->fw_seriallink_options24[2]),
1093 le16_to_cpu(ha->fw_seriallink_options24[3]));
1094 if (rval != QLA_SUCCESS) {
1095 qla_printk(KERN_WARNING, ha,
1096 "Unable to update Serial Link options (%x).\n", rval);
1100 void
1101 qla2x00_config_rings(struct scsi_qla_host *ha)
1103 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1105 /* Setup ring parameters in initialization control block. */
1106 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1107 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1108 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1109 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1110 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1111 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1112 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1113 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1115 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1116 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1117 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1118 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1119 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1122 void
1123 qla24xx_config_rings(struct scsi_qla_host *ha)
1125 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1126 struct init_cb_24xx *icb;
1128 /* Setup ring parameters in initialization control block. */
1129 icb = (struct init_cb_24xx *)ha->init_cb;
1130 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1131 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1132 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1133 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1134 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1135 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1136 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1137 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1139 WRT_REG_DWORD(&reg->req_q_in, 0);
1140 WRT_REG_DWORD(&reg->req_q_out, 0);
1141 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1142 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1143 RD_REG_DWORD(&reg->rsp_q_out);
1147 * qla2x00_init_rings() - Initializes firmware.
1148 * @ha: HA context
1150 * Beginning of request ring has initialization control block already built
1151 * by nvram config routine.
1153 * Returns 0 on success.
1155 static int
1156 qla2x00_init_rings(scsi_qla_host_t *ha)
1158 int rval;
1159 unsigned long flags = 0;
1160 int cnt;
1162 spin_lock_irqsave(&ha->hardware_lock, flags);
1164 /* Clear outstanding commands array. */
1165 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1166 ha->outstanding_cmds[cnt] = NULL;
1168 ha->current_outstanding_cmd = 0;
1170 /* Clear RSCN queue. */
1171 ha->rscn_in_ptr = 0;
1172 ha->rscn_out_ptr = 0;
1174 /* Initialize firmware. */
1175 ha->request_ring_ptr = ha->request_ring;
1176 ha->req_ring_index = 0;
1177 ha->req_q_cnt = ha->request_q_length;
1178 ha->response_ring_ptr = ha->response_ring;
1179 ha->rsp_ring_index = 0;
1181 /* Initialize response queue entries */
1182 qla2x00_init_response_q_entries(ha);
1184 ha->isp_ops.config_rings(ha);
1186 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1188 /* Update any ISP specific firmware options before initialization. */
1189 ha->isp_ops.update_fw_options(ha);
1191 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
1192 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1193 if (rval) {
1194 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1195 ha->host_no));
1196 } else {
1197 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1198 ha->host_no));
1201 return (rval);
1205 * qla2x00_fw_ready() - Waits for firmware ready.
1206 * @ha: HA context
1208 * Returns 0 on success.
1210 static int
1211 qla2x00_fw_ready(scsi_qla_host_t *ha)
1213 int rval;
1214 unsigned long wtime, mtime;
1215 uint16_t min_wait; /* Minimum wait time if loop is down */
1216 uint16_t wait_time; /* Wait time if loop is coming ready */
1217 uint16_t fw_state;
1219 rval = QLA_SUCCESS;
1221 /* 20 seconds for loop down. */
1222 min_wait = 20;
1225 * Firmware should take at most one RATOV to login, plus 5 seconds for
1226 * our own processing.
1228 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1229 wait_time = min_wait;
1232 /* Min wait time if loop down */
1233 mtime = jiffies + (min_wait * HZ);
1235 /* wait time before firmware ready */
1236 wtime = jiffies + (wait_time * HZ);
1238 /* Wait for ISP to finish LIP */
1239 if (!ha->flags.init_done)
1240 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1242 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1243 ha->host_no));
1245 do {
1246 rval = qla2x00_get_firmware_state(ha, &fw_state);
1247 if (rval == QLA_SUCCESS) {
1248 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1249 ha->device_flags &= ~DFLG_NO_CABLE;
1251 if (fw_state == FSTATE_READY) {
1252 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1253 ha->host_no));
1255 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1256 &ha->login_timeout, &ha->r_a_tov);
1258 rval = QLA_SUCCESS;
1259 break;
1262 rval = QLA_FUNCTION_FAILED;
1264 if (atomic_read(&ha->loop_down_timer) &&
1265 (fw_state >= FSTATE_LOSS_OF_SYNC ||
1266 fw_state == FSTATE_WAIT_AL_PA)) {
1267 /* Loop down. Timeout on min_wait for states
1268 * other than Wait for Login.
1270 if (time_after_eq(jiffies, mtime)) {
1271 qla_printk(KERN_INFO, ha,
1272 "Cable is unplugged...\n");
1274 ha->device_flags |= DFLG_NO_CABLE;
1275 break;
1278 } else {
1279 /* Mailbox cmd failed. Timeout on min_wait. */
1280 if (time_after_eq(jiffies, mtime))
1281 break;
1284 if (time_after_eq(jiffies, wtime))
1285 break;
1287 /* Delay for a while */
1288 msleep(500);
1290 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1291 ha->host_no, fw_state, jiffies));
1292 } while (1);
1294 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1295 ha->host_no, fw_state, jiffies));
1297 if (rval) {
1298 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1299 ha->host_no));
1302 return (rval);
1306 * qla2x00_configure_hba
1307 * Setup adapter context.
1309 * Input:
1310 * ha = adapter state pointer.
1312 * Returns:
1313 * 0 = success
1315 * Context:
1316 * Kernel context.
1318 static int
1319 qla2x00_configure_hba(scsi_qla_host_t *ha)
1321 int rval;
1322 uint16_t loop_id;
1323 uint16_t topo;
1324 uint8_t al_pa;
1325 uint8_t area;
1326 uint8_t domain;
1327 char connect_type[22];
1329 /* Get host addresses. */
1330 rval = qla2x00_get_adapter_id(ha,
1331 &loop_id, &al_pa, &area, &domain, &topo);
1332 if (rval != QLA_SUCCESS) {
1333 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
1334 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1335 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1336 __func__, ha->host_no));
1337 } else {
1338 qla_printk(KERN_WARNING, ha,
1339 "ERROR -- Unable to get host loop ID.\n");
1340 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1342 return (rval);
1345 if (topo == 4) {
1346 qla_printk(KERN_INFO, ha,
1347 "Cannot get topology - retrying.\n");
1348 return (QLA_FUNCTION_FAILED);
1351 ha->loop_id = loop_id;
1353 /* initialize */
1354 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1355 ha->operating_mode = LOOP;
1357 switch (topo) {
1358 case 0:
1359 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1360 ha->host_no));
1361 ha->current_topology = ISP_CFG_NL;
1362 strcpy(connect_type, "(Loop)");
1363 break;
1365 case 1:
1366 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1367 ha->host_no));
1368 ha->current_topology = ISP_CFG_FL;
1369 strcpy(connect_type, "(FL_Port)");
1370 break;
1372 case 2:
1373 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1374 ha->host_no));
1375 ha->operating_mode = P2P;
1376 ha->current_topology = ISP_CFG_N;
1377 strcpy(connect_type, "(N_Port-to-N_Port)");
1378 break;
1380 case 3:
1381 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1382 ha->host_no));
1383 ha->operating_mode = P2P;
1384 ha->current_topology = ISP_CFG_F;
1385 strcpy(connect_type, "(F_Port)");
1386 break;
1388 default:
1389 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1390 "Using NL.\n",
1391 ha->host_no, topo));
1392 ha->current_topology = ISP_CFG_NL;
1393 strcpy(connect_type, "(Loop)");
1394 break;
1397 /* Save Host port and loop ID. */
1398 /* byte order - Big Endian */
1399 ha->d_id.b.domain = domain;
1400 ha->d_id.b.area = area;
1401 ha->d_id.b.al_pa = al_pa;
1403 if (!ha->flags.init_done)
1404 qla_printk(KERN_INFO, ha,
1405 "Topology - %s, Host Loop address 0x%x\n",
1406 connect_type, ha->loop_id);
1408 if (rval) {
1409 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1410 } else {
1411 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1414 return(rval);
1418 * NVRAM configuration for ISP 2xxx
1420 * Input:
1421 * ha = adapter block pointer.
1423 * Output:
1424 * initialization control block in response_ring
1425 * host adapters parameters in host adapter block
1427 * Returns:
1428 * 0 = success.
1431 qla2x00_nvram_config(scsi_qla_host_t *ha)
1433 int rval;
1434 uint8_t chksum = 0;
1435 uint16_t cnt;
1436 uint8_t *dptr1, *dptr2;
1437 init_cb_t *icb = ha->init_cb;
1438 nvram_t *nv = (nvram_t *)ha->request_ring;
1439 uint8_t *ptr = (uint8_t *)ha->request_ring;
1440 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1442 rval = QLA_SUCCESS;
1444 /* Determine NVRAM starting address. */
1445 ha->nvram_size = sizeof(nvram_t);
1446 ha->nvram_base = 0;
1447 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1448 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1449 ha->nvram_base = 0x80;
1451 /* Get NVRAM data and calculate checksum. */
1452 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1453 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1454 chksum += *ptr++;
1456 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1457 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1458 ha->nvram_size));
1460 /* Bad NVRAM data, set defaults parameters. */
1461 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1462 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1463 /* Reset NVRAM data. */
1464 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1465 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1466 nv->nvram_version);
1467 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1468 "invalid -- WWPN) defaults.\n");
1471 * Set default initialization control block.
1473 memset(nv, 0, ha->nvram_size);
1474 nv->parameter_block_version = ICB_VERSION;
1476 if (IS_QLA23XX(ha)) {
1477 nv->firmware_options[0] = BIT_2 | BIT_1;
1478 nv->firmware_options[1] = BIT_7 | BIT_5;
1479 nv->add_firmware_options[0] = BIT_5;
1480 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1481 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1482 nv->special_options[1] = BIT_7;
1483 } else if (IS_QLA2200(ha)) {
1484 nv->firmware_options[0] = BIT_2 | BIT_1;
1485 nv->firmware_options[1] = BIT_7 | BIT_5;
1486 nv->add_firmware_options[0] = BIT_5;
1487 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1488 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1489 } else if (IS_QLA2100(ha)) {
1490 nv->firmware_options[0] = BIT_3 | BIT_1;
1491 nv->firmware_options[1] = BIT_5;
1492 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1495 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1496 nv->execution_throttle = __constant_cpu_to_le16(16);
1497 nv->retry_count = 8;
1498 nv->retry_delay = 1;
1500 nv->port_name[0] = 33;
1501 nv->port_name[3] = 224;
1502 nv->port_name[4] = 139;
1504 nv->login_timeout = 4;
1507 * Set default host adapter parameters
1509 nv->host_p[1] = BIT_2;
1510 nv->reset_delay = 5;
1511 nv->port_down_retry_count = 8;
1512 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1513 nv->link_down_timeout = 60;
1515 rval = 1;
1518 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1520 * The SN2 does not provide BIOS emulation which means you can't change
1521 * potentially bogus BIOS settings. Force the use of default settings
1522 * for link rate and frame size. Hope that the rest of the settings
1523 * are valid.
1525 if (ia64_platform_is("sn2")) {
1526 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1527 if (IS_QLA23XX(ha))
1528 nv->special_options[1] = BIT_7;
1530 #endif
1532 /* Reset Initialization control block */
1533 memset(icb, 0, ha->init_cb_size);
1536 * Setup driver NVRAM options.
1538 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1539 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1540 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1541 nv->firmware_options[1] &= ~BIT_4;
1543 if (IS_QLA23XX(ha)) {
1544 nv->firmware_options[0] |= BIT_2;
1545 nv->firmware_options[0] &= ~BIT_3;
1546 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1548 if (IS_QLA2300(ha)) {
1549 if (ha->fb_rev == FPM_2310) {
1550 strcpy(ha->model_number, "QLA2310");
1551 } else {
1552 strcpy(ha->model_number, "QLA2300");
1554 } else {
1555 if (rval == 0 &&
1556 memcmp(nv->model_number, BINZERO,
1557 sizeof(nv->model_number)) != 0) {
1558 char *st, *en;
1560 strncpy(ha->model_number, nv->model_number,
1561 sizeof(nv->model_number));
1562 st = en = ha->model_number;
1563 en += sizeof(nv->model_number) - 1;
1564 while (en > st) {
1565 if (*en != 0x20 && *en != 0x00)
1566 break;
1567 *en-- = '\0';
1569 } else {
1570 uint16_t index;
1572 index = (ha->pdev->subsystem_device & 0xff);
1573 if (index < QLA_MODEL_NAMES) {
1574 strcpy(ha->model_number,
1575 qla2x00_model_name[index * 2]);
1576 ha->model_desc =
1577 qla2x00_model_name[index * 2 + 1];
1578 } else {
1579 strcpy(ha->model_number, "QLA23xx");
1583 } else if (IS_QLA2200(ha)) {
1584 nv->firmware_options[0] |= BIT_2;
1586 * 'Point-to-point preferred, else loop' is not a safe
1587 * connection mode setting.
1589 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1590 (BIT_5 | BIT_4)) {
1591 /* Force 'loop preferred, else point-to-point'. */
1592 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1593 nv->add_firmware_options[0] |= BIT_5;
1595 strcpy(ha->model_number, "QLA22xx");
1596 } else /*if (IS_QLA2100(ha))*/ {
1597 strcpy(ha->model_number, "QLA2100");
1601 * Copy over NVRAM RISC parameter block to initialization control block.
1603 dptr1 = (uint8_t *)icb;
1604 dptr2 = (uint8_t *)&nv->parameter_block_version;
1605 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1606 while (cnt--)
1607 *dptr1++ = *dptr2++;
1609 /* Copy 2nd half. */
1610 dptr1 = (uint8_t *)icb->add_firmware_options;
1611 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1612 while (cnt--)
1613 *dptr1++ = *dptr2++;
1615 /* Use alternate WWN? */
1616 if (nv->host_p[1] & BIT_7) {
1617 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1618 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1621 /* Prepare nodename */
1622 if ((icb->firmware_options[1] & BIT_6) == 0) {
1624 * Firmware will apply the following mask if the nodename was
1625 * not provided.
1627 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1628 icb->node_name[0] &= 0xF0;
1632 * Set host adapter parameters.
1634 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1635 /* Always load RISC code on non ISP2[12]00 chips. */
1636 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1637 ha->flags.disable_risc_code_load = 0;
1638 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1639 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1640 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1641 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1643 ha->operating_mode =
1644 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1646 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1647 sizeof(ha->fw_seriallink_options));
1649 /* save HBA serial number */
1650 ha->serial0 = icb->port_name[5];
1651 ha->serial1 = icb->port_name[6];
1652 ha->serial2 = icb->port_name[7];
1653 ha->node_name = icb->node_name;
1654 ha->port_name = icb->port_name;
1656 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1658 ha->retry_count = nv->retry_count;
1660 /* Set minimum login_timeout to 4 seconds. */
1661 if (nv->login_timeout < ql2xlogintimeout)
1662 nv->login_timeout = ql2xlogintimeout;
1663 if (nv->login_timeout < 4)
1664 nv->login_timeout = 4;
1665 ha->login_timeout = nv->login_timeout;
1666 icb->login_timeout = nv->login_timeout;
1668 /* Set minimum RATOV to 200 tenths of a second. */
1669 ha->r_a_tov = 200;
1671 ha->loop_reset_delay = nv->reset_delay;
1673 /* Link Down Timeout = 0:
1675 * When Port Down timer expires we will start returning
1676 * I/O's to OS with "DID_NO_CONNECT".
1678 * Link Down Timeout != 0:
1680 * The driver waits for the link to come up after link down
1681 * before returning I/Os to OS with "DID_NO_CONNECT".
1683 if (nv->link_down_timeout == 0) {
1684 ha->loop_down_abort_time =
1685 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1686 } else {
1687 ha->link_down_timeout = nv->link_down_timeout;
1688 ha->loop_down_abort_time =
1689 (LOOP_DOWN_TIME - ha->link_down_timeout);
1693 * Need enough time to try and get the port back.
1695 ha->port_down_retry_count = nv->port_down_retry_count;
1696 if (qlport_down_retry)
1697 ha->port_down_retry_count = qlport_down_retry;
1698 /* Set login_retry_count */
1699 ha->login_retry_count = nv->retry_count;
1700 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1701 ha->port_down_retry_count > 3)
1702 ha->login_retry_count = ha->port_down_retry_count;
1703 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1704 ha->login_retry_count = ha->port_down_retry_count;
1705 if (ql2xloginretrycount)
1706 ha->login_retry_count = ql2xloginretrycount;
1708 icb->lun_enables = __constant_cpu_to_le16(0);
1709 icb->command_resource_count = 0;
1710 icb->immediate_notify_resource_count = 0;
1711 icb->timeout = __constant_cpu_to_le16(0);
1713 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1714 /* Enable RIO */
1715 icb->firmware_options[0] &= ~BIT_3;
1716 icb->add_firmware_options[0] &=
1717 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1718 icb->add_firmware_options[0] |= BIT_2;
1719 icb->response_accumulation_timer = 3;
1720 icb->interrupt_delay_timer = 5;
1722 ha->flags.process_response_queue = 1;
1723 } else {
1724 /* Enable ZIO. */
1725 if (!ha->flags.init_done) {
1726 ha->zio_mode = icb->add_firmware_options[0] &
1727 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1728 ha->zio_timer = icb->interrupt_delay_timer ?
1729 icb->interrupt_delay_timer: 2;
1731 icb->add_firmware_options[0] &=
1732 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1733 ha->flags.process_response_queue = 0;
1734 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1735 ha->zio_mode = QLA_ZIO_MODE_6;
1737 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1738 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1739 ha->zio_timer * 100));
1740 qla_printk(KERN_INFO, ha,
1741 "ZIO mode %d enabled; timer delay (%d us).\n",
1742 ha->zio_mode, ha->zio_timer * 100);
1744 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1745 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1746 ha->flags.process_response_queue = 1;
1750 if (rval) {
1751 DEBUG2_3(printk(KERN_WARNING
1752 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1754 return (rval);
1757 static void
1758 qla2x00_rport_del(void *data)
1760 fc_port_t *fcport = data;
1761 struct fc_rport *rport;
1762 unsigned long flags;
1764 spin_lock_irqsave(&fcport->rport_lock, flags);
1765 rport = fcport->drport;
1766 fcport->drport = NULL;
1767 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1768 if (rport)
1769 fc_remote_port_delete(rport);
1774 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1775 * @ha: HA context
1776 * @flags: allocation flags
1778 * Returns a pointer to the allocated fcport, or NULL, if none available.
1780 fc_port_t *
1781 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1783 fc_port_t *fcport;
1785 fcport = kmalloc(sizeof(fc_port_t), flags);
1786 if (fcport == NULL)
1787 return (fcport);
1789 /* Setup fcport template structure. */
1790 memset(fcport, 0, sizeof (fc_port_t));
1791 fcport->ha = ha;
1792 fcport->port_type = FCT_UNKNOWN;
1793 fcport->loop_id = FC_NO_LOOP_ID;
1794 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1795 fcport->flags = FCF_RLC_SUPPORT;
1796 fcport->supported_classes = FC_COS_UNSPECIFIED;
1797 spin_lock_init(&fcport->rport_lock);
1799 return (fcport);
1803 * qla2x00_configure_loop
1804 * Updates Fibre Channel Device Database with what is actually on loop.
1806 * Input:
1807 * ha = adapter block pointer.
1809 * Returns:
1810 * 0 = success.
1811 * 1 = error.
1812 * 2 = database was full and device was not configured.
1814 static int
1815 qla2x00_configure_loop(scsi_qla_host_t *ha)
1817 int rval;
1818 unsigned long flags, save_flags;
1820 rval = QLA_SUCCESS;
1822 /* Get Initiator ID */
1823 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1824 rval = qla2x00_configure_hba(ha);
1825 if (rval != QLA_SUCCESS) {
1826 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1827 ha->host_no));
1828 return (rval);
1832 save_flags = flags = ha->dpc_flags;
1833 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1834 ha->host_no, flags));
1837 * If we have both an RSCN and PORT UPDATE pending then handle them
1838 * both at the same time.
1840 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1841 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1843 /* Determine what we need to do */
1844 if (ha->current_topology == ISP_CFG_FL &&
1845 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1847 ha->flags.rscn_queue_overflow = 1;
1848 set_bit(RSCN_UPDATE, &flags);
1850 } else if (ha->current_topology == ISP_CFG_F &&
1851 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1853 ha->flags.rscn_queue_overflow = 1;
1854 set_bit(RSCN_UPDATE, &flags);
1855 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1857 } else if (ha->current_topology == ISP_CFG_N) {
1858 clear_bit(RSCN_UPDATE, &flags);
1860 } else if (!ha->flags.online ||
1861 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1863 ha->flags.rscn_queue_overflow = 1;
1864 set_bit(RSCN_UPDATE, &flags);
1865 set_bit(LOCAL_LOOP_UPDATE, &flags);
1868 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1869 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1870 rval = QLA_FUNCTION_FAILED;
1871 } else {
1872 rval = qla2x00_configure_local_loop(ha);
1876 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1877 if (LOOP_TRANSITION(ha)) {
1878 rval = QLA_FUNCTION_FAILED;
1879 } else {
1880 rval = qla2x00_configure_fabric(ha);
1884 if (rval == QLA_SUCCESS) {
1885 if (atomic_read(&ha->loop_down_timer) ||
1886 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1887 rval = QLA_FUNCTION_FAILED;
1888 } else {
1889 atomic_set(&ha->loop_state, LOOP_READY);
1891 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1895 if (rval) {
1896 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1897 __func__, ha->host_no));
1898 } else {
1899 DEBUG3(printk("%s: exiting normally\n", __func__));
1902 /* Restore state if a resync event occured during processing */
1903 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1904 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1905 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1906 if (test_bit(RSCN_UPDATE, &save_flags))
1907 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1910 return (rval);
1916 * qla2x00_configure_local_loop
1917 * Updates Fibre Channel Device Database with local loop devices.
1919 * Input:
1920 * ha = adapter block pointer.
1922 * Returns:
1923 * 0 = success.
1925 static int
1926 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1928 int rval, rval2;
1929 int found_devs;
1930 int found;
1931 fc_port_t *fcport, *new_fcport;
1933 uint16_t index;
1934 uint16_t entries;
1935 char *id_iter;
1936 uint16_t loop_id;
1937 uint8_t domain, area, al_pa;
1939 found_devs = 0;
1940 new_fcport = NULL;
1941 entries = MAX_FIBRE_DEVICES;
1943 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1944 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1946 /* Get list of logged in devices. */
1947 memset(ha->gid_list, 0, GID_LIST_SIZE);
1948 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1949 &entries);
1950 if (rval != QLA_SUCCESS)
1951 goto cleanup_allocation;
1953 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1954 ha->host_no, entries));
1955 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1956 entries * sizeof(struct gid_list_info)));
1958 /* Allocate temporary fcport for any new fcports discovered. */
1959 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1960 if (new_fcport == NULL) {
1961 rval = QLA_MEMORY_ALLOC_FAILED;
1962 goto cleanup_allocation;
1964 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1967 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1969 list_for_each_entry(fcport, &ha->fcports, list) {
1970 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1971 fcport->port_type != FCT_BROADCAST &&
1972 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1974 DEBUG(printk("scsi(%ld): Marking port lost, "
1975 "loop_id=0x%04x\n",
1976 ha->host_no, fcport->loop_id));
1978 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1979 fcport->flags &= ~FCF_FARP_DONE;
1983 /* Add devices to port list. */
1984 id_iter = (char *)ha->gid_list;
1985 for (index = 0; index < entries; index++) {
1986 domain = ((struct gid_list_info *)id_iter)->domain;
1987 area = ((struct gid_list_info *)id_iter)->area;
1988 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1989 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1990 loop_id = (uint16_t)
1991 ((struct gid_list_info *)id_iter)->loop_id_2100;
1992 else
1993 loop_id = le16_to_cpu(
1994 ((struct gid_list_info *)id_iter)->loop_id);
1995 id_iter += ha->gid_list_info_size;
1997 /* Bypass reserved domain fields. */
1998 if ((domain & 0xf0) == 0xf0)
1999 continue;
2001 /* Bypass if not same domain and area of adapter. */
2002 if (area && domain &&
2003 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
2004 continue;
2006 /* Bypass invalid local loop ID. */
2007 if (loop_id > LAST_LOCAL_LOOP_ID)
2008 continue;
2010 /* Fill in member data. */
2011 new_fcport->d_id.b.domain = domain;
2012 new_fcport->d_id.b.area = area;
2013 new_fcport->d_id.b.al_pa = al_pa;
2014 new_fcport->loop_id = loop_id;
2015 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2016 if (rval2 != QLA_SUCCESS) {
2017 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2018 "information -- get_port_database=%x, "
2019 "loop_id=0x%04x\n",
2020 ha->host_no, rval2, new_fcport->loop_id));
2021 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2022 ha->host_no));
2023 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2024 continue;
2027 /* Check for matching device in port list. */
2028 found = 0;
2029 fcport = NULL;
2030 list_for_each_entry(fcport, &ha->fcports, list) {
2031 if (memcmp(new_fcport->port_name, fcport->port_name,
2032 WWN_SIZE))
2033 continue;
2035 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2036 FCF_PERSISTENT_BOUND);
2037 fcport->loop_id = new_fcport->loop_id;
2038 fcport->port_type = new_fcport->port_type;
2039 fcport->d_id.b24 = new_fcport->d_id.b24;
2040 memcpy(fcport->node_name, new_fcport->node_name,
2041 WWN_SIZE);
2043 found++;
2044 break;
2047 if (!found) {
2048 /* New device, add to fcports list. */
2049 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2050 list_add_tail(&new_fcport->list, &ha->fcports);
2052 /* Allocate a new replacement fcport. */
2053 fcport = new_fcport;
2054 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2055 if (new_fcport == NULL) {
2056 rval = QLA_MEMORY_ALLOC_FAILED;
2057 goto cleanup_allocation;
2059 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2062 qla2x00_update_fcport(ha, fcport);
2064 found_devs++;
2067 cleanup_allocation:
2068 kfree(new_fcport);
2070 if (rval != QLA_SUCCESS) {
2071 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2072 "rval=%x\n", ha->host_no, rval));
2075 if (found_devs) {
2076 ha->device_flags |= DFLG_LOCAL_DEVICES;
2077 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2080 return (rval);
2083 static void
2084 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2086 fc_port_t *fcport;
2088 qla2x00_mark_all_devices_lost(ha, 0);
2089 list_for_each_entry(fcport, &ha->fcports, list) {
2090 if (fcport->port_type != FCT_TARGET)
2091 continue;
2093 qla2x00_update_fcport(ha, fcport);
2098 * qla2x00_update_fcport
2099 * Updates device on list.
2101 * Input:
2102 * ha = adapter block pointer.
2103 * fcport = port structure pointer.
2105 * Return:
2106 * 0 - Success
2107 * BIT_0 - error
2109 * Context:
2110 * Kernel context.
2112 void
2113 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2115 fcport->ha = ha;
2116 fcport->login_retry = 0;
2117 fcport->port_login_retry_count = ha->port_down_retry_count *
2118 PORT_RETRY_TIME;
2119 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2120 PORT_RETRY_TIME);
2121 fcport->flags &= ~FCF_LOGIN_NEEDED;
2123 atomic_set(&fcport->state, FCS_ONLINE);
2125 if (ha->flags.init_done)
2126 qla2x00_reg_remote_port(ha, fcport);
2129 void
2130 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2132 struct fc_rport_identifiers rport_ids;
2133 struct fc_rport *rport;
2134 unsigned long flags;
2136 if (fcport->drport)
2137 qla2x00_rport_del(fcport);
2138 if (fcport->rport)
2139 return;
2141 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2142 rport_ids.port_name = wwn_to_u64(fcport->port_name);
2143 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2144 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2145 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2146 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2147 if (!rport) {
2148 qla_printk(KERN_WARNING, ha,
2149 "Unable to allocate fc remote port!\n");
2150 return;
2152 spin_lock_irqsave(&fcport->rport_lock, flags);
2153 fcport->rport = rport;
2154 *((fc_port_t **)rport->dd_data) = fcport;
2155 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2157 rport->supported_classes = fcport->supported_classes;
2159 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2160 if (fcport->port_type == FCT_INITIATOR)
2161 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2162 if (fcport->port_type == FCT_TARGET)
2163 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2164 fc_remote_port_rolechg(rport, rport_ids.roles);
2166 if (rport->scsi_target_id != -1 &&
2167 rport->scsi_target_id < ha->host->max_id)
2168 fcport->os_target_id = rport->scsi_target_id;
2172 * qla2x00_configure_fabric
2173 * Setup SNS devices with loop ID's.
2175 * Input:
2176 * ha = adapter block pointer.
2178 * Returns:
2179 * 0 = success.
2180 * BIT_0 = error
2182 static int
2183 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2185 int rval, rval2;
2186 fc_port_t *fcport, *fcptemp;
2187 uint16_t next_loopid;
2188 uint16_t mb[MAILBOX_REGISTER_COUNT];
2189 uint16_t loop_id;
2190 LIST_HEAD(new_fcports);
2192 /* If FL port exists, then SNS is present */
2193 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2194 loop_id = NPH_F_PORT;
2195 else
2196 loop_id = SNS_FL_PORT;
2197 rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
2198 if (rval != QLA_SUCCESS) {
2199 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2200 "Port\n", ha->host_no));
2202 ha->device_flags &= ~SWITCH_FOUND;
2203 return (QLA_SUCCESS);
2206 /* Mark devices that need re-synchronization. */
2207 rval2 = qla2x00_device_resync(ha);
2208 if (rval2 == QLA_RSCNS_HANDLED) {
2209 /* No point doing the scan, just continue. */
2210 return (QLA_SUCCESS);
2212 do {
2213 /* FDMI support. */
2214 if (ql2xfdmienable &&
2215 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2216 qla2x00_fdmi_register(ha);
2218 /* Ensure we are logged into the SNS. */
2219 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2220 loop_id = NPH_SNS;
2221 else
2222 loop_id = SIMPLE_NAME_SERVER;
2223 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
2224 0xfc, mb, BIT_1 | BIT_0);
2225 if (mb[0] != MBS_COMMAND_COMPLETE) {
2226 DEBUG2(qla_printk(KERN_INFO, ha,
2227 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2228 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2229 mb[0], mb[1], mb[2], mb[6], mb[7]));
2230 return (QLA_SUCCESS);
2233 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2234 if (qla2x00_rft_id(ha)) {
2235 /* EMPTY */
2236 DEBUG2(printk("scsi(%ld): Register FC-4 "
2237 "TYPE failed.\n", ha->host_no));
2239 if (qla2x00_rff_id(ha)) {
2240 /* EMPTY */
2241 DEBUG2(printk("scsi(%ld): Register FC-4 "
2242 "Features failed.\n", ha->host_no));
2244 if (qla2x00_rnn_id(ha)) {
2245 /* EMPTY */
2246 DEBUG2(printk("scsi(%ld): Register Node Name "
2247 "failed.\n", ha->host_no));
2248 } else if (qla2x00_rsnn_nn(ha)) {
2249 /* EMPTY */
2250 DEBUG2(printk("scsi(%ld): Register Symbolic "
2251 "Node Name failed.\n", ha->host_no));
2255 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2256 if (rval != QLA_SUCCESS)
2257 break;
2260 * Logout all previous fabric devices marked lost, except
2261 * tape devices.
2263 list_for_each_entry(fcport, &ha->fcports, list) {
2264 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2265 break;
2267 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2268 continue;
2270 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2271 qla2x00_mark_device_lost(ha, fcport,
2272 ql2xplogiabsentdevice, 0);
2273 if (fcport->loop_id != FC_NO_LOOP_ID &&
2274 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2275 fcport->port_type != FCT_INITIATOR &&
2276 fcport->port_type != FCT_BROADCAST) {
2277 ha->isp_ops.fabric_logout(ha,
2278 fcport->loop_id,
2279 fcport->d_id.b.domain,
2280 fcport->d_id.b.area,
2281 fcport->d_id.b.al_pa);
2282 fcport->loop_id = FC_NO_LOOP_ID;
2287 /* Starting free loop ID. */
2288 next_loopid = ha->min_external_loopid;
2291 * Scan through our port list and login entries that need to be
2292 * logged in.
2294 list_for_each_entry(fcport, &ha->fcports, list) {
2295 if (atomic_read(&ha->loop_down_timer) ||
2296 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2297 break;
2299 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2300 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2301 continue;
2303 if (fcport->loop_id == FC_NO_LOOP_ID) {
2304 fcport->loop_id = next_loopid;
2305 rval = qla2x00_find_new_loop_id(ha, fcport);
2306 if (rval != QLA_SUCCESS) {
2307 /* Ran out of IDs to use */
2308 break;
2311 /* Login and update database */
2312 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2315 /* Exit if out of loop IDs. */
2316 if (rval != QLA_SUCCESS) {
2317 break;
2321 * Login and add the new devices to our port list.
2323 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2324 if (atomic_read(&ha->loop_down_timer) ||
2325 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2326 break;
2328 /* Find a new loop ID to use. */
2329 fcport->loop_id = next_loopid;
2330 rval = qla2x00_find_new_loop_id(ha, fcport);
2331 if (rval != QLA_SUCCESS) {
2332 /* Ran out of IDs to use */
2333 break;
2336 /* Remove device from the new list and add it to DB */
2337 list_del(&fcport->list);
2338 list_add_tail(&fcport->list, &ha->fcports);
2340 /* Login and update database */
2341 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2343 } while (0);
2345 /* Free all new device structures not processed. */
2346 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2347 list_del(&fcport->list);
2348 kfree(fcport);
2351 if (rval) {
2352 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2353 "rval=%d\n", ha->host_no, rval));
2356 return (rval);
2361 * qla2x00_find_all_fabric_devs
2363 * Input:
2364 * ha = adapter block pointer.
2365 * dev = database device entry pointer.
2367 * Returns:
2368 * 0 = success.
2370 * Context:
2371 * Kernel context.
2373 static int
2374 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2376 int rval;
2377 uint16_t loop_id;
2378 fc_port_t *fcport, *new_fcport, *fcptemp;
2379 int found;
2381 sw_info_t *swl;
2382 int swl_idx;
2383 int first_dev, last_dev;
2384 port_id_t wrap, nxt_d_id;
2386 rval = QLA_SUCCESS;
2388 /* Try GID_PT to get device list, else GAN. */
2389 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2390 if (swl == NULL) {
2391 /*EMPTY*/
2392 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2393 "on GA_NXT\n", ha->host_no));
2394 } else {
2395 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2396 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2397 kfree(swl);
2398 swl = NULL;
2399 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2400 kfree(swl);
2401 swl = NULL;
2402 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2403 kfree(swl);
2404 swl = NULL;
2407 swl_idx = 0;
2409 /* Allocate temporary fcport for any new fcports discovered. */
2410 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2411 if (new_fcport == NULL) {
2412 kfree(swl);
2413 return (QLA_MEMORY_ALLOC_FAILED);
2415 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2417 /* Set start port ID scan at adapter ID. */
2418 first_dev = 1;
2419 last_dev = 0;
2421 /* Starting free loop ID. */
2422 loop_id = ha->min_external_loopid;
2423 for (; loop_id <= ha->last_loop_id; loop_id++) {
2424 if (qla2x00_is_reserved_id(ha, loop_id))
2425 continue;
2427 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2428 break;
2430 if (swl != NULL) {
2431 if (last_dev) {
2432 wrap.b24 = new_fcport->d_id.b24;
2433 } else {
2434 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2435 memcpy(new_fcport->node_name,
2436 swl[swl_idx].node_name, WWN_SIZE);
2437 memcpy(new_fcport->port_name,
2438 swl[swl_idx].port_name, WWN_SIZE);
2440 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2441 last_dev = 1;
2443 swl_idx++;
2445 } else {
2446 /* Send GA_NXT to the switch */
2447 rval = qla2x00_ga_nxt(ha, new_fcport);
2448 if (rval != QLA_SUCCESS) {
2449 qla_printk(KERN_WARNING, ha,
2450 "SNS scan failed -- assuming zero-entry "
2451 "result...\n");
2452 list_for_each_entry_safe(fcport, fcptemp,
2453 new_fcports, list) {
2454 list_del(&fcport->list);
2455 kfree(fcport);
2457 rval = QLA_SUCCESS;
2458 break;
2462 /* If wrap on switch device list, exit. */
2463 if (first_dev) {
2464 wrap.b24 = new_fcport->d_id.b24;
2465 first_dev = 0;
2466 } else if (new_fcport->d_id.b24 == wrap.b24) {
2467 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2468 ha->host_no, new_fcport->d_id.b.domain,
2469 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2470 break;
2473 /* Bypass if host adapter. */
2474 if (new_fcport->d_id.b24 == ha->d_id.b24)
2475 continue;
2477 /* Bypass if same domain and area of adapter. */
2478 if (((new_fcport->d_id.b24 & 0xffff00) ==
2479 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2480 ISP_CFG_FL)
2481 continue;
2483 /* Bypass reserved domain fields. */
2484 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2485 continue;
2487 /* Locate matching device in database. */
2488 found = 0;
2489 list_for_each_entry(fcport, &ha->fcports, list) {
2490 if (memcmp(new_fcport->port_name, fcport->port_name,
2491 WWN_SIZE))
2492 continue;
2494 found++;
2497 * If address the same and state FCS_ONLINE, nothing
2498 * changed.
2500 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2501 atomic_read(&fcport->state) == FCS_ONLINE) {
2502 break;
2506 * If device was not a fabric device before.
2508 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2509 fcport->d_id.b24 = new_fcport->d_id.b24;
2510 fcport->loop_id = FC_NO_LOOP_ID;
2511 fcport->flags |= (FCF_FABRIC_DEVICE |
2512 FCF_LOGIN_NEEDED);
2513 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2514 break;
2518 * Port ID changed or device was marked to be updated;
2519 * Log it out if still logged in and mark it for
2520 * relogin later.
2522 fcport->d_id.b24 = new_fcport->d_id.b24;
2523 fcport->flags |= FCF_LOGIN_NEEDED;
2524 if (fcport->loop_id != FC_NO_LOOP_ID &&
2525 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2526 fcport->port_type != FCT_INITIATOR &&
2527 fcport->port_type != FCT_BROADCAST) {
2528 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2529 fcport->d_id.b.domain, fcport->d_id.b.area,
2530 fcport->d_id.b.al_pa);
2531 fcport->loop_id = FC_NO_LOOP_ID;
2534 break;
2537 if (found)
2538 continue;
2540 /* If device was not in our fcports list, then add it. */
2541 list_add_tail(&new_fcport->list, new_fcports);
2543 /* Allocate a new replacement fcport. */
2544 nxt_d_id.b24 = new_fcport->d_id.b24;
2545 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2546 if (new_fcport == NULL) {
2547 kfree(swl);
2548 return (QLA_MEMORY_ALLOC_FAILED);
2550 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2551 new_fcport->d_id.b24 = nxt_d_id.b24;
2554 kfree(swl);
2555 kfree(new_fcport);
2557 if (!list_empty(new_fcports))
2558 ha->device_flags |= DFLG_FABRIC_DEVICES;
2560 return (rval);
2564 * qla2x00_find_new_loop_id
2565 * Scan through our port list and find a new usable loop ID.
2567 * Input:
2568 * ha: adapter state pointer.
2569 * dev: port structure pointer.
2571 * Returns:
2572 * qla2x00 local function return status code.
2574 * Context:
2575 * Kernel context.
2578 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2580 int rval;
2581 int found;
2582 fc_port_t *fcport;
2583 uint16_t first_loop_id;
2585 rval = QLA_SUCCESS;
2587 /* Save starting loop ID. */
2588 first_loop_id = dev->loop_id;
2590 for (;;) {
2591 /* Skip loop ID if already used by adapter. */
2592 if (dev->loop_id == ha->loop_id) {
2593 dev->loop_id++;
2596 /* Skip reserved loop IDs. */
2597 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2598 dev->loop_id++;
2601 /* Reset loop ID if passed the end. */
2602 if (dev->loop_id > ha->last_loop_id) {
2603 /* first loop ID. */
2604 dev->loop_id = ha->min_external_loopid;
2607 /* Check for loop ID being already in use. */
2608 found = 0;
2609 fcport = NULL;
2610 list_for_each_entry(fcport, &ha->fcports, list) {
2611 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2612 /* ID possibly in use */
2613 found++;
2614 break;
2618 /* If not in use then it is free to use. */
2619 if (!found) {
2620 break;
2623 /* ID in use. Try next value. */
2624 dev->loop_id++;
2626 /* If wrap around. No free ID to use. */
2627 if (dev->loop_id == first_loop_id) {
2628 dev->loop_id = FC_NO_LOOP_ID;
2629 rval = QLA_FUNCTION_FAILED;
2630 break;
2634 return (rval);
2638 * qla2x00_device_resync
2639 * Marks devices in the database that needs resynchronization.
2641 * Input:
2642 * ha = adapter block pointer.
2644 * Context:
2645 * Kernel context.
2647 static int
2648 qla2x00_device_resync(scsi_qla_host_t *ha)
2650 int rval;
2651 uint32_t mask;
2652 fc_port_t *fcport;
2653 uint32_t rscn_entry;
2654 uint8_t rscn_out_iter;
2655 uint8_t format;
2656 port_id_t d_id;
2658 rval = QLA_RSCNS_HANDLED;
2660 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2661 ha->flags.rscn_queue_overflow) {
2663 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2664 format = MSB(MSW(rscn_entry));
2665 d_id.b.domain = LSB(MSW(rscn_entry));
2666 d_id.b.area = MSB(LSW(rscn_entry));
2667 d_id.b.al_pa = LSB(LSW(rscn_entry));
2669 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2670 "[%02x/%02x%02x%02x].\n",
2671 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2672 d_id.b.area, d_id.b.al_pa));
2674 ha->rscn_out_ptr++;
2675 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2676 ha->rscn_out_ptr = 0;
2678 /* Skip duplicate entries. */
2679 for (rscn_out_iter = ha->rscn_out_ptr;
2680 !ha->flags.rscn_queue_overflow &&
2681 rscn_out_iter != ha->rscn_in_ptr;
2682 rscn_out_iter = (rscn_out_iter ==
2683 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2685 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2686 break;
2688 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2689 "entry found at [%d].\n", ha->host_no,
2690 rscn_out_iter));
2692 ha->rscn_out_ptr = rscn_out_iter;
2695 /* Queue overflow, set switch default case. */
2696 if (ha->flags.rscn_queue_overflow) {
2697 DEBUG(printk("scsi(%ld): device_resync: rscn "
2698 "overflow.\n", ha->host_no));
2700 format = 3;
2701 ha->flags.rscn_queue_overflow = 0;
2704 switch (format) {
2705 case 0:
2706 mask = 0xffffff;
2707 break;
2708 case 1:
2709 mask = 0xffff00;
2710 break;
2711 case 2:
2712 mask = 0xff0000;
2713 break;
2714 default:
2715 mask = 0x0;
2716 d_id.b24 = 0;
2717 ha->rscn_out_ptr = ha->rscn_in_ptr;
2718 break;
2721 rval = QLA_SUCCESS;
2723 list_for_each_entry(fcport, &ha->fcports, list) {
2724 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2725 (fcport->d_id.b24 & mask) != d_id.b24 ||
2726 fcport->port_type == FCT_BROADCAST)
2727 continue;
2729 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2730 if (format != 3 ||
2731 fcport->port_type != FCT_INITIATOR) {
2732 qla2x00_mark_device_lost(ha, fcport,
2733 0, 0);
2736 fcport->flags &= ~FCF_FARP_DONE;
2739 return (rval);
2743 * qla2x00_fabric_dev_login
2744 * Login fabric target device and update FC port database.
2746 * Input:
2747 * ha: adapter state pointer.
2748 * fcport: port structure list pointer.
2749 * next_loopid: contains value of a new loop ID that can be used
2750 * by the next login attempt.
2752 * Returns:
2753 * qla2x00 local function return status code.
2755 * Context:
2756 * Kernel context.
2758 static int
2759 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2760 uint16_t *next_loopid)
2762 int rval;
2763 int retry;
2764 uint8_t opts;
2766 rval = QLA_SUCCESS;
2767 retry = 0;
2769 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2770 if (rval == QLA_SUCCESS) {
2771 /* Send an ADISC to tape devices.*/
2772 opts = 0;
2773 if (fcport->flags & FCF_TAPE_PRESENT)
2774 opts |= BIT_1;
2775 rval = qla2x00_get_port_database(ha, fcport, opts);
2776 if (rval != QLA_SUCCESS) {
2777 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2778 fcport->d_id.b.domain, fcport->d_id.b.area,
2779 fcport->d_id.b.al_pa);
2780 qla2x00_mark_device_lost(ha, fcport, 1, 0);
2781 } else {
2782 qla2x00_update_fcport(ha, fcport);
2786 return (rval);
2790 * qla2x00_fabric_login
2791 * Issue fabric login command.
2793 * Input:
2794 * ha = adapter block pointer.
2795 * device = pointer to FC device type structure.
2797 * Returns:
2798 * 0 - Login successfully
2799 * 1 - Login failed
2800 * 2 - Initiator device
2801 * 3 - Fatal error
2804 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2805 uint16_t *next_loopid)
2807 int rval;
2808 int retry;
2809 uint16_t tmp_loopid;
2810 uint16_t mb[MAILBOX_REGISTER_COUNT];
2812 retry = 0;
2813 tmp_loopid = 0;
2815 for (;;) {
2816 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2817 "for port %02x%02x%02x.\n",
2818 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2819 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2821 /* Login fcport on switch. */
2822 ha->isp_ops.fabric_login(ha, fcport->loop_id,
2823 fcport->d_id.b.domain, fcport->d_id.b.area,
2824 fcport->d_id.b.al_pa, mb, BIT_0);
2825 if (mb[0] == MBS_PORT_ID_USED) {
2827 * Device has another loop ID. The firmware team
2828 * recommends the driver perform an implicit login with
2829 * the specified ID again. The ID we just used is save
2830 * here so we return with an ID that can be tried by
2831 * the next login.
2833 retry++;
2834 tmp_loopid = fcport->loop_id;
2835 fcport->loop_id = mb[1];
2837 DEBUG(printk("Fabric Login: port in use - next "
2838 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2839 fcport->loop_id, fcport->d_id.b.domain,
2840 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2842 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2844 * Login succeeded.
2846 if (retry) {
2847 /* A retry occurred before. */
2848 *next_loopid = tmp_loopid;
2849 } else {
2851 * No retry occurred before. Just increment the
2852 * ID value for next login.
2854 *next_loopid = (fcport->loop_id + 1);
2857 if (mb[1] & BIT_0) {
2858 fcport->port_type = FCT_INITIATOR;
2859 } else {
2860 fcport->port_type = FCT_TARGET;
2861 if (mb[1] & BIT_1) {
2862 fcport->flags |= FCF_TAPE_PRESENT;
2866 if (mb[10] & BIT_0)
2867 fcport->supported_classes |= FC_COS_CLASS2;
2868 if (mb[10] & BIT_1)
2869 fcport->supported_classes |= FC_COS_CLASS3;
2871 rval = QLA_SUCCESS;
2872 break;
2873 } else if (mb[0] == MBS_LOOP_ID_USED) {
2875 * Loop ID already used, try next loop ID.
2877 fcport->loop_id++;
2878 rval = qla2x00_find_new_loop_id(ha, fcport);
2879 if (rval != QLA_SUCCESS) {
2880 /* Ran out of loop IDs to use */
2881 break;
2883 } else if (mb[0] == MBS_COMMAND_ERROR) {
2885 * Firmware possibly timed out during login. If NO
2886 * retries are left to do then the device is declared
2887 * dead.
2889 *next_loopid = fcport->loop_id;
2890 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2891 fcport->d_id.b.domain, fcport->d_id.b.area,
2892 fcport->d_id.b.al_pa);
2893 qla2x00_mark_device_lost(ha, fcport, 1, 0);
2895 rval = 1;
2896 break;
2897 } else {
2899 * unrecoverable / not handled error
2901 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
2902 "loop_id=%x jiffies=%lx.\n",
2903 __func__, ha->host_no, mb[0],
2904 fcport->d_id.b.domain, fcport->d_id.b.area,
2905 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2907 *next_loopid = fcport->loop_id;
2908 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2909 fcport->d_id.b.domain, fcport->d_id.b.area,
2910 fcport->d_id.b.al_pa);
2911 fcport->loop_id = FC_NO_LOOP_ID;
2912 fcport->login_retry = 0;
2914 rval = 3;
2915 break;
2919 return (rval);
2923 * qla2x00_local_device_login
2924 * Issue local device login command.
2926 * Input:
2927 * ha = adapter block pointer.
2928 * loop_id = loop id of device to login to.
2930 * Returns (Where's the #define!!!!):
2931 * 0 - Login successfully
2932 * 1 - Login failed
2933 * 3 - Fatal error
2936 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
2938 int rval;
2939 uint16_t mb[MAILBOX_REGISTER_COUNT];
2941 memset(mb, 0, sizeof(mb));
2942 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
2943 if (rval == QLA_SUCCESS) {
2944 /* Interrogate mailbox registers for any errors */
2945 if (mb[0] == MBS_COMMAND_ERROR)
2946 rval = 1;
2947 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2948 /* device not in PCB table */
2949 rval = 3;
2952 return (rval);
2956 * qla2x00_loop_resync
2957 * Resync with fibre channel devices.
2959 * Input:
2960 * ha = adapter block pointer.
2962 * Returns:
2963 * 0 = success
2966 qla2x00_loop_resync(scsi_qla_host_t *ha)
2968 int rval;
2969 uint32_t wait_time;
2971 rval = QLA_SUCCESS;
2973 atomic_set(&ha->loop_state, LOOP_UPDATE);
2974 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2975 if (ha->flags.online) {
2976 if (!(rval = qla2x00_fw_ready(ha))) {
2977 /* Wait at most MAX_TARGET RSCNs for a stable link. */
2978 wait_time = 256;
2979 do {
2980 atomic_set(&ha->loop_state, LOOP_UPDATE);
2982 /* Issue a marker after FW becomes ready. */
2983 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2984 ha->marker_needed = 0;
2986 /* Remap devices on Loop. */
2987 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2989 qla2x00_configure_loop(ha);
2990 wait_time--;
2991 } while (!atomic_read(&ha->loop_down_timer) &&
2992 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2993 wait_time &&
2994 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2998 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2999 return (QLA_FUNCTION_FAILED);
3002 if (rval) {
3003 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3006 return (rval);
3009 void
3010 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3012 int rescan_done;
3013 fc_port_t *fcport;
3015 rescan_done = 0;
3016 list_for_each_entry(fcport, &ha->fcports, list) {
3017 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3018 continue;
3020 qla2x00_update_fcport(ha, fcport);
3021 fcport->flags &= ~FCF_RESCAN_NEEDED;
3023 rescan_done = 1;
3025 qla2x00_probe_for_all_luns(ha);
3028 void
3029 qla2x00_update_fcports(scsi_qla_host_t *ha)
3031 fc_port_t *fcport;
3033 /* Go with deferred removal of rport references. */
3034 list_for_each_entry(fcport, &ha->fcports, list)
3035 if (fcport->drport)
3036 qla2x00_rport_del(fcport);
3040 * qla2x00_abort_isp
3041 * Resets ISP and aborts all outstanding commands.
3043 * Input:
3044 * ha = adapter block pointer.
3046 * Returns:
3047 * 0 = success
3050 qla2x00_abort_isp(scsi_qla_host_t *ha)
3052 unsigned long flags = 0;
3053 uint16_t cnt;
3054 srb_t *sp;
3055 uint8_t status = 0;
3057 if (ha->flags.online) {
3058 ha->flags.online = 0;
3059 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3061 qla_printk(KERN_INFO, ha,
3062 "Performing ISP error recovery - ha= %p.\n", ha);
3063 ha->isp_ops.reset_chip(ha);
3065 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3066 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3067 atomic_set(&ha->loop_state, LOOP_DOWN);
3068 qla2x00_mark_all_devices_lost(ha, 0);
3069 } else {
3070 if (!atomic_read(&ha->loop_down_timer))
3071 atomic_set(&ha->loop_down_timer,
3072 LOOP_DOWN_TIME);
3075 spin_lock_irqsave(&ha->hardware_lock, flags);
3076 /* Requeue all commands in outstanding command list. */
3077 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3078 sp = ha->outstanding_cmds[cnt];
3079 if (sp) {
3080 ha->outstanding_cmds[cnt] = NULL;
3081 sp->flags = 0;
3082 sp->cmd->result = DID_RESET << 16;
3083 sp->cmd->host_scribble = (unsigned char *)NULL;
3084 qla2x00_sp_compl(ha, sp);
3087 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3089 ha->isp_ops.nvram_config(ha);
3091 if (!qla2x00_restart_isp(ha)) {
3092 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3094 if (!atomic_read(&ha->loop_down_timer)) {
3096 * Issue marker command only when we are going
3097 * to start the I/O .
3099 ha->marker_needed = 1;
3102 ha->flags.online = 1;
3104 ha->isp_ops.enable_intrs(ha);
3106 ha->isp_abort_cnt = 0;
3107 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3108 } else { /* failed the ISP abort */
3109 ha->flags.online = 1;
3110 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3111 if (ha->isp_abort_cnt == 0) {
3112 qla_printk(KERN_WARNING, ha,
3113 "ISP error recovery failed - "
3114 "board disabled\n");
3116 * The next call disables the board
3117 * completely.
3119 ha->isp_ops.reset_adapter(ha);
3120 ha->flags.online = 0;
3121 clear_bit(ISP_ABORT_RETRY,
3122 &ha->dpc_flags);
3123 status = 0;
3124 } else { /* schedule another ISP abort */
3125 ha->isp_abort_cnt--;
3126 DEBUG(printk("qla%ld: ISP abort - "
3127 "retry remaining %d\n",
3128 ha->host_no, ha->isp_abort_cnt);)
3129 status = 1;
3131 } else {
3132 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3133 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3134 "- retrying (%d) more times\n",
3135 ha->host_no, ha->isp_abort_cnt);)
3136 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3137 status = 1;
3143 if (status) {
3144 qla_printk(KERN_INFO, ha,
3145 "qla2x00_abort_isp: **** FAILED ****\n");
3146 } else {
3147 DEBUG(printk(KERN_INFO
3148 "qla2x00_abort_isp(%ld): exiting.\n",
3149 ha->host_no);)
3152 return(status);
3156 * qla2x00_restart_isp
3157 * restarts the ISP after a reset
3159 * Input:
3160 * ha = adapter block pointer.
3162 * Returns:
3163 * 0 = success
3165 static int
3166 qla2x00_restart_isp(scsi_qla_host_t *ha)
3168 uint8_t status = 0;
3169 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3170 unsigned long flags = 0;
3171 uint32_t wait_time;
3173 /* If firmware needs to be loaded */
3174 if (qla2x00_isp_firmware(ha)) {
3175 ha->flags.online = 0;
3176 if (!(status = ha->isp_ops.chip_diag(ha))) {
3177 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3178 status = qla2x00_setup_chip(ha);
3179 goto done;
3182 spin_lock_irqsave(&ha->hardware_lock, flags);
3184 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3186 * Disable SRAM, Instruction RAM and GP RAM
3187 * parity.
3189 WRT_REG_WORD(&reg->hccr,
3190 (HCCR_ENABLE_PARITY + 0x0));
3191 RD_REG_WORD(&reg->hccr);
3194 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3196 status = qla2x00_setup_chip(ha);
3198 spin_lock_irqsave(&ha->hardware_lock, flags);
3200 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3201 /* Enable proper parity */
3202 if (IS_QLA2300(ha))
3203 /* SRAM parity */
3204 WRT_REG_WORD(&reg->hccr,
3205 (HCCR_ENABLE_PARITY + 0x1));
3206 else
3208 * SRAM, Instruction RAM and GP RAM
3209 * parity.
3211 WRT_REG_WORD(&reg->hccr,
3212 (HCCR_ENABLE_PARITY + 0x7));
3213 RD_REG_WORD(&reg->hccr);
3216 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3220 done:
3221 if (!status && !(status = qla2x00_init_rings(ha))) {
3222 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3223 if (!(status = qla2x00_fw_ready(ha))) {
3224 DEBUG(printk("%s(): Start configure loop, "
3225 "status = %d\n", __func__, status);)
3227 /* Issue a marker after FW becomes ready. */
3228 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3230 ha->flags.online = 1;
3231 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3232 wait_time = 256;
3233 do {
3234 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3235 qla2x00_configure_loop(ha);
3236 wait_time--;
3237 } while (!atomic_read(&ha->loop_down_timer) &&
3238 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3239 wait_time &&
3240 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3243 /* if no cable then assume it's good */
3244 if ((ha->device_flags & DFLG_NO_CABLE))
3245 status = 0;
3247 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3248 __func__,
3249 status);)
3251 return (status);
3255 * qla2x00_reset_adapter
3256 * Reset adapter.
3258 * Input:
3259 * ha = adapter block pointer.
3261 void
3262 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3264 unsigned long flags = 0;
3265 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3267 ha->flags.online = 0;
3268 ha->isp_ops.disable_intrs(ha);
3270 spin_lock_irqsave(&ha->hardware_lock, flags);
3271 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3272 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3273 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3274 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3275 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3278 void
3279 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3281 unsigned long flags = 0;
3282 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3284 ha->flags.online = 0;
3285 ha->isp_ops.disable_intrs(ha);
3287 spin_lock_irqsave(&ha->hardware_lock, flags);
3288 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3289 RD_REG_DWORD(&reg->hccr);
3290 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3291 RD_REG_DWORD(&reg->hccr);
3292 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3296 qla24xx_nvram_config(scsi_qla_host_t *ha)
3298 int rval;
3299 struct init_cb_24xx *icb;
3300 struct nvram_24xx *nv;
3301 uint32_t *dptr;
3302 uint8_t *dptr1, *dptr2;
3303 uint32_t chksum;
3304 uint16_t cnt;
3306 rval = QLA_SUCCESS;
3307 icb = (struct init_cb_24xx *)ha->init_cb;
3308 nv = (struct nvram_24xx *)ha->request_ring;
3310 /* Determine NVRAM starting address. */
3311 ha->nvram_size = sizeof(struct nvram_24xx);
3312 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3313 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3314 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3315 if (PCI_FUNC(ha->pdev->devfn)) {
3316 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3317 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3320 /* Get NVRAM data and calculate checksum. */
3321 dptr = (uint32_t *)nv;
3322 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3323 ha->nvram_size);
3324 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3325 chksum += le32_to_cpu(*dptr++);
3327 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3328 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3329 ha->nvram_size));
3331 /* Bad NVRAM data, set defaults parameters. */
3332 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3333 || nv->id[3] != ' ' ||
3334 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3335 /* Reset NVRAM data. */
3336 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3337 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3338 le16_to_cpu(nv->nvram_version));
3339 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3340 "invalid -- WWPN) defaults.\n");
3343 * Set default initialization control block.
3345 memset(nv, 0, ha->nvram_size);
3346 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3347 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3348 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3349 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3350 nv->exchange_count = __constant_cpu_to_le16(0);
3351 nv->hard_address = __constant_cpu_to_le16(124);
3352 nv->port_name[0] = 0x21;
3353 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3354 nv->port_name[2] = 0x00;
3355 nv->port_name[3] = 0xe0;
3356 nv->port_name[4] = 0x8b;
3357 nv->port_name[5] = 0x1c;
3358 nv->port_name[6] = 0x55;
3359 nv->port_name[7] = 0x86;
3360 nv->node_name[0] = 0x20;
3361 nv->node_name[1] = 0x00;
3362 nv->node_name[2] = 0x00;
3363 nv->node_name[3] = 0xe0;
3364 nv->node_name[4] = 0x8b;
3365 nv->node_name[5] = 0x1c;
3366 nv->node_name[6] = 0x55;
3367 nv->node_name[7] = 0x86;
3368 nv->login_retry_count = __constant_cpu_to_le16(8);
3369 nv->link_down_timeout = __constant_cpu_to_le16(200);
3370 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3371 nv->login_timeout = __constant_cpu_to_le16(0);
3372 nv->firmware_options_1 =
3373 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3374 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3375 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3376 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3377 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3378 nv->efi_parameters = __constant_cpu_to_le32(0);
3379 nv->reset_delay = 5;
3380 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3381 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3382 nv->link_down_timeout = __constant_cpu_to_le16(30);
3384 rval = 1;
3387 /* Reset Initialization control block */
3388 memset(icb, 0, sizeof(struct init_cb_24xx));
3390 /* Copy 1st segment. */
3391 dptr1 = (uint8_t *)icb;
3392 dptr2 = (uint8_t *)&nv->version;
3393 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3394 while (cnt--)
3395 *dptr1++ = *dptr2++;
3397 icb->login_retry_count = nv->login_retry_count;
3398 icb->link_down_timeout = nv->link_down_timeout;
3400 /* Copy 2nd segment. */
3401 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3402 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3403 cnt = (uint8_t *)&icb->reserved_3 -
3404 (uint8_t *)&icb->interrupt_delay_timer;
3405 while (cnt--)
3406 *dptr1++ = *dptr2++;
3409 * Setup driver NVRAM options.
3411 if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3412 char *st, *en;
3413 uint16_t index;
3415 strncpy(ha->model_number, nv->model_name,
3416 sizeof(nv->model_name));
3417 st = en = ha->model_number;
3418 en += sizeof(nv->model_name) - 1;
3419 while (en > st) {
3420 if (*en != 0x20 && *en != 0x00)
3421 break;
3422 *en-- = '\0';
3425 index = (ha->pdev->subsystem_device & 0xff);
3426 if (index < QLA_MODEL_NAMES)
3427 ha->model_desc = qla2x00_model_name[index * 2 + 1];
3428 } else
3429 strcpy(ha->model_number, "QLA2462");
3431 /* Use alternate WWN? */
3432 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3433 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3434 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3437 /* Prepare nodename */
3438 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
3440 * Firmware will apply the following mask if the nodename was
3441 * not provided.
3443 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3444 icb->node_name[0] &= 0xF0;
3447 /* Set host adapter parameters. */
3448 ha->flags.disable_risc_code_load = 0;
3449 ha->flags.enable_lip_reset = 1;
3450 ha->flags.enable_lip_full_login = 1;
3451 ha->flags.enable_target_reset = 1;
3452 ha->flags.enable_led_scheme = 0;
3454 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3455 (BIT_6 | BIT_5 | BIT_4)) >> 4;
3457 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3458 sizeof(ha->fw_seriallink_options24));
3460 /* save HBA serial number */
3461 ha->serial0 = icb->port_name[5];
3462 ha->serial1 = icb->port_name[6];
3463 ha->serial2 = icb->port_name[7];
3464 ha->node_name = icb->node_name;
3465 ha->port_name = icb->port_name;
3467 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3469 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3471 /* Set minimum login_timeout to 4 seconds. */
3472 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3473 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3474 if (le16_to_cpu(nv->login_timeout) < 4)
3475 nv->login_timeout = __constant_cpu_to_le16(4);
3476 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3477 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3479 /* Set minimum RATOV to 200 tenths of a second. */
3480 ha->r_a_tov = 200;
3482 ha->loop_reset_delay = nv->reset_delay;
3484 /* Link Down Timeout = 0:
3486 * When Port Down timer expires we will start returning
3487 * I/O's to OS with "DID_NO_CONNECT".
3489 * Link Down Timeout != 0:
3491 * The driver waits for the link to come up after link down
3492 * before returning I/Os to OS with "DID_NO_CONNECT".
3494 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3495 ha->loop_down_abort_time =
3496 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3497 } else {
3498 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3499 ha->loop_down_abort_time =
3500 (LOOP_DOWN_TIME - ha->link_down_timeout);
3503 /* Need enough time to try and get the port back. */
3504 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3505 if (qlport_down_retry)
3506 ha->port_down_retry_count = qlport_down_retry;
3508 /* Set login_retry_count */
3509 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3510 if (ha->port_down_retry_count ==
3511 le16_to_cpu(nv->port_down_retry_count) &&
3512 ha->port_down_retry_count > 3)
3513 ha->login_retry_count = ha->port_down_retry_count;
3514 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3515 ha->login_retry_count = ha->port_down_retry_count;
3516 if (ql2xloginretrycount)
3517 ha->login_retry_count = ql2xloginretrycount;
3519 /* Enable ZIO. */
3520 if (!ha->flags.init_done) {
3521 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3522 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3523 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3524 le16_to_cpu(icb->interrupt_delay_timer): 2;
3526 icb->firmware_options_2 &= __constant_cpu_to_le32(
3527 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3528 ha->flags.process_response_queue = 0;
3529 if (ha->zio_mode != QLA_ZIO_DISABLED) {
3530 ha->zio_mode = QLA_ZIO_MODE_6;
3532 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3533 "(%d us).\n", ha->host_no, ha->zio_mode,
3534 ha->zio_timer * 100));
3535 qla_printk(KERN_INFO, ha,
3536 "ZIO mode %d enabled; timer delay (%d us).\n",
3537 ha->zio_mode, ha->zio_timer * 100);
3539 icb->firmware_options_2 |= cpu_to_le32(
3540 (uint32_t)ha->zio_mode);
3541 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3542 ha->flags.process_response_queue = 1;
3545 if (rval) {
3546 DEBUG2_3(printk(KERN_WARNING
3547 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3549 return (rval);
3553 qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3555 int rval;
3556 int segments, fragment;
3557 uint32_t faddr;
3558 uint32_t *dcode, dlen;
3559 uint32_t risc_addr;
3560 uint32_t risc_size;
3561 uint32_t i;
3563 rval = QLA_SUCCESS;
3565 segments = FA_RISC_CODE_SEGMENTS;
3566 faddr = FA_RISC_CODE_ADDR;
3567 dcode = (uint32_t *)ha->request_ring;
3568 *srisc_addr = 0;
3570 /* Validate firmware image by checking version. */
3571 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3572 for (i = 0; i < 4; i++)
3573 dcode[i] = be32_to_cpu(dcode[i]);
3574 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3575 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3576 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3577 dcode[3] == 0)) {
3578 qla_printk(KERN_WARNING, ha,
3579 "Unable to verify integrity of flash firmware image!\n");
3580 qla_printk(KERN_WARNING, ha,
3581 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3582 dcode[1], dcode[2], dcode[3]);
3584 return QLA_FUNCTION_FAILED;
3587 while (segments && rval == QLA_SUCCESS) {
3588 /* Read segment's load information. */
3589 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3591 risc_addr = be32_to_cpu(dcode[2]);
3592 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3593 risc_size = be32_to_cpu(dcode[3]);
3595 fragment = 0;
3596 while (risc_size > 0 && rval == QLA_SUCCESS) {
3597 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3598 if (dlen > risc_size)
3599 dlen = risc_size;
3601 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3602 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3603 ha->host_no, risc_addr, dlen, faddr));
3605 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3606 for (i = 0; i < dlen; i++)
3607 dcode[i] = swab32(dcode[i]);
3609 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3610 dlen);
3611 if (rval) {
3612 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3613 "segment %d of firmware\n", ha->host_no,
3614 fragment));
3615 qla_printk(KERN_WARNING, ha,
3616 "[ERROR] Failed to load segment %d of "
3617 "firmware\n", fragment);
3618 break;
3621 faddr += dlen;
3622 risc_addr += dlen;
3623 risc_size -= dlen;
3624 fragment++;
3627 /* Next segment. */
3628 segments--;
3631 return rval;
3634 #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3637 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3639 int rval;
3640 int i, fragment;
3641 uint16_t *wcode, *fwcode;
3642 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3643 struct fw_blob *blob;
3645 /* Load firmware blob. */
3646 blob = qla2x00_request_firmware(ha);
3647 if (!blob) {
3648 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3649 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3650 "from: " QLA_FW_URL ".\n");
3651 return QLA_FUNCTION_FAILED;
3654 rval = QLA_SUCCESS;
3656 wcode = (uint16_t *)ha->request_ring;
3657 *srisc_addr = 0;
3658 fwcode = (uint16_t *)blob->fw->data;
3659 fwclen = 0;
3661 /* Validate firmware image by checking version. */
3662 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3663 qla_printk(KERN_WARNING, ha,
3664 "Unable to verify integrity of firmware image (%Zd)!\n",
3665 blob->fw->size);
3666 goto fail_fw_integrity;
3668 for (i = 0; i < 4; i++)
3669 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3670 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3671 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3672 wcode[2] == 0 && wcode[3] == 0)) {
3673 qla_printk(KERN_WARNING, ha,
3674 "Unable to verify integrity of firmware image!\n");
3675 qla_printk(KERN_WARNING, ha,
3676 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3677 wcode[1], wcode[2], wcode[3]);
3678 goto fail_fw_integrity;
3681 seg = blob->segs;
3682 while (*seg && rval == QLA_SUCCESS) {
3683 risc_addr = *seg;
3684 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3685 risc_size = be16_to_cpu(fwcode[3]);
3687 /* Validate firmware image size. */
3688 fwclen += risc_size * sizeof(uint16_t);
3689 if (blob->fw->size < fwclen) {
3690 qla_printk(KERN_WARNING, ha,
3691 "Unable to verify integrity of firmware image "
3692 "(%Zd)!\n", blob->fw->size);
3693 goto fail_fw_integrity;
3696 fragment = 0;
3697 while (risc_size > 0 && rval == QLA_SUCCESS) {
3698 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3699 if (wlen > risc_size)
3700 wlen = risc_size;
3702 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3703 "addr %x, number of words 0x%x.\n", ha->host_no,
3704 risc_addr, wlen));
3706 for (i = 0; i < wlen; i++)
3707 wcode[i] = swab16(fwcode[i]);
3709 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3710 wlen);
3711 if (rval) {
3712 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3713 "segment %d of firmware\n", ha->host_no,
3714 fragment));
3715 qla_printk(KERN_WARNING, ha,
3716 "[ERROR] Failed to load segment %d of "
3717 "firmware\n", fragment);
3718 break;
3721 fwcode += wlen;
3722 risc_addr += wlen;
3723 risc_size -= wlen;
3724 fragment++;
3727 /* Next segment. */
3728 seg++;
3730 return rval;
3732 fail_fw_integrity:
3733 return QLA_FUNCTION_FAILED;
3737 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3739 int rval;
3740 int segments, fragment;
3741 uint32_t *dcode, dlen;
3742 uint32_t risc_addr;
3743 uint32_t risc_size;
3744 uint32_t i;
3745 struct fw_blob *blob;
3746 uint32_t *fwcode, fwclen;
3748 /* Load firmware blob. */
3749 blob = qla2x00_request_firmware(ha);
3750 if (!blob) {
3751 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3752 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3753 "from: " QLA_FW_URL ".\n");
3755 /* Try to load RISC code from flash. */
3756 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3757 "outdated) firmware from flash.\n");
3758 return qla24xx_load_risc_flash(ha, srisc_addr);
3761 rval = QLA_SUCCESS;
3763 segments = FA_RISC_CODE_SEGMENTS;
3764 dcode = (uint32_t *)ha->request_ring;
3765 *srisc_addr = 0;
3766 fwcode = (uint32_t *)blob->fw->data;
3767 fwclen = 0;
3769 /* Validate firmware image by checking version. */
3770 if (blob->fw->size < 8 * sizeof(uint32_t)) {
3771 qla_printk(KERN_WARNING, ha,
3772 "Unable to verify integrity of firmware image (%Zd)!\n",
3773 blob->fw->size);
3774 goto fail_fw_integrity;
3776 for (i = 0; i < 4; i++)
3777 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3778 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3779 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3780 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3781 dcode[3] == 0)) {
3782 qla_printk(KERN_WARNING, ha,
3783 "Unable to verify integrity of firmware image!\n");
3784 qla_printk(KERN_WARNING, ha,
3785 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3786 dcode[1], dcode[2], dcode[3]);
3787 goto fail_fw_integrity;
3790 while (segments && rval == QLA_SUCCESS) {
3791 risc_addr = be32_to_cpu(fwcode[2]);
3792 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3793 risc_size = be32_to_cpu(fwcode[3]);
3795 /* Validate firmware image size. */
3796 fwclen += risc_size * sizeof(uint32_t);
3797 if (blob->fw->size < fwclen) {
3798 qla_printk(KERN_WARNING, ha,
3799 "Unable to verify integrity of firmware image "
3800 "(%Zd)!\n", blob->fw->size);
3802 goto fail_fw_integrity;
3805 fragment = 0;
3806 while (risc_size > 0 && rval == QLA_SUCCESS) {
3807 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3808 if (dlen > risc_size)
3809 dlen = risc_size;
3811 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3812 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3813 risc_addr, dlen));
3815 for (i = 0; i < dlen; i++)
3816 dcode[i] = swab32(fwcode[i]);
3818 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3819 dlen);
3820 if (rval) {
3821 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3822 "segment %d of firmware\n", ha->host_no,
3823 fragment));
3824 qla_printk(KERN_WARNING, ha,
3825 "[ERROR] Failed to load segment %d of "
3826 "firmware\n", fragment);
3827 break;
3830 fwcode += dlen;
3831 risc_addr += dlen;
3832 risc_size -= dlen;
3833 fragment++;
3836 /* Next segment. */
3837 segments--;
3839 return rval;
3841 fail_fw_integrity:
3842 return QLA_FUNCTION_FAILED;