Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / scsi / qla2xxx / qla_init.c
blob89a3fc0595bbe4c7236d3d6118d8544e4541e7b3
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;
197 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
199 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
200 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
201 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
203 /* Reset expansion ROM address decode enable */
204 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
205 d &= ~PCI_ROM_ADDRESS_ENABLE;
206 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
208 /* Get PCI bus information. */
209 spin_lock_irqsave(&ha->hardware_lock, flags);
210 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
211 spin_unlock_irqrestore(&ha->hardware_lock, flags);
213 return QLA_SUCCESS;
217 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
218 * @ha: HA context
220 * Returns 0 on success.
223 qla2300_pci_config(scsi_qla_host_t *ha)
225 uint16_t w, mwi;
226 uint32_t d;
227 unsigned long flags = 0;
228 uint32_t cnt;
229 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
231 pci_set_master(ha->pdev);
232 mwi = 0;
233 if (pci_set_mwi(ha->pdev))
234 mwi = PCI_COMMAND_INVALIDATE;
235 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
237 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
238 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
240 if (IS_QLA2322(ha) || IS_QLA6322(ha))
241 w &= ~PCI_COMMAND_INTX_DISABLE;
244 * If this is a 2300 card and not 2312, reset the
245 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
246 * the 2310 also reports itself as a 2300 so we need to get the
247 * fb revision level -- a 6 indicates it really is a 2300 and
248 * not a 2310.
250 if (IS_QLA2300(ha)) {
251 spin_lock_irqsave(&ha->hardware_lock, flags);
253 /* Pause RISC. */
254 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
255 for (cnt = 0; cnt < 30000; cnt++) {
256 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
257 break;
259 udelay(10);
262 /* Select FPM registers. */
263 WRT_REG_WORD(&reg->ctrl_status, 0x20);
264 RD_REG_WORD(&reg->ctrl_status);
266 /* Get the fb rev level */
267 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
269 if (ha->fb_rev == FPM_2300)
270 w &= ~PCI_COMMAND_INVALIDATE;
272 /* Deselect FPM registers. */
273 WRT_REG_WORD(&reg->ctrl_status, 0x0);
274 RD_REG_WORD(&reg->ctrl_status);
276 /* Release RISC module. */
277 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
278 for (cnt = 0; cnt < 30000; cnt++) {
279 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
280 break;
282 udelay(10);
285 spin_unlock_irqrestore(&ha->hardware_lock, flags);
287 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
289 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
291 /* Reset expansion ROM address decode enable */
292 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
293 d &= ~PCI_ROM_ADDRESS_ENABLE;
294 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
296 /* Get PCI bus information. */
297 spin_lock_irqsave(&ha->hardware_lock, flags);
298 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
299 spin_unlock_irqrestore(&ha->hardware_lock, flags);
301 return QLA_SUCCESS;
305 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
306 * @ha: HA context
308 * Returns 0 on success.
311 qla24xx_pci_config(scsi_qla_host_t *ha)
313 uint16_t w, mwi;
314 uint32_t d;
315 unsigned long flags = 0;
316 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
317 int pcix_cmd_reg, pcie_dctl_reg;
319 pci_set_master(ha->pdev);
320 mwi = 0;
321 if (pci_set_mwi(ha->pdev))
322 mwi = PCI_COMMAND_INVALIDATE;
323 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
325 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
326 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
327 w &= ~PCI_COMMAND_INTX_DISABLE;
328 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
330 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
332 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
333 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
334 if (pcix_cmd_reg) {
335 uint16_t pcix_cmd;
337 pcix_cmd_reg += PCI_X_CMD;
338 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
339 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
340 pcix_cmd |= 0x0008;
341 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
344 /* PCIe -- adjust Maximum Read Request Size (2048). */
345 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
346 if (pcie_dctl_reg) {
347 uint16_t pcie_dctl;
349 pcie_dctl_reg += PCI_EXP_DEVCTL;
350 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
351 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
352 pcie_dctl |= 0x4000;
353 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
356 /* Reset expansion ROM address decode enable */
357 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
358 d &= ~PCI_ROM_ADDRESS_ENABLE;
359 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
361 /* Get PCI bus information. */
362 spin_lock_irqsave(&ha->hardware_lock, flags);
363 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
364 spin_unlock_irqrestore(&ha->hardware_lock, flags);
366 return QLA_SUCCESS;
370 * qla2x00_isp_firmware() - Choose firmware image.
371 * @ha: HA context
373 * Returns 0 on success.
375 static int
376 qla2x00_isp_firmware(scsi_qla_host_t *ha)
378 int rval;
380 /* Assume loading risc code */
381 rval = QLA_FUNCTION_FAILED;
383 if (ha->flags.disable_risc_code_load) {
384 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
385 ha->host_no));
386 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
388 /* Verify checksum of loaded RISC code. */
389 rval = qla2x00_verify_checksum(ha,
390 IS_QLA24XX(ha) || IS_QLA54XX(ha) ? RISC_SADDRESS :
391 *ha->brd_info->fw_info[0].fwstart);
394 if (rval) {
395 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
396 ha->host_no));
399 return (rval);
403 * qla2x00_reset_chip() - Reset ISP chip.
404 * @ha: HA context
406 * Returns 0 on success.
408 void
409 qla2x00_reset_chip(scsi_qla_host_t *ha)
411 unsigned long flags = 0;
412 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
413 uint32_t cnt;
414 unsigned long mbx_flags = 0;
415 uint16_t cmd;
417 ha->isp_ops.disable_intrs(ha);
419 spin_lock_irqsave(&ha->hardware_lock, flags);
421 /* Turn off master enable */
422 cmd = 0;
423 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
424 cmd &= ~PCI_COMMAND_MASTER;
425 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
427 if (!IS_QLA2100(ha)) {
428 /* Pause RISC. */
429 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
430 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
431 for (cnt = 0; cnt < 30000; cnt++) {
432 if ((RD_REG_WORD(&reg->hccr) &
433 HCCR_RISC_PAUSE) != 0)
434 break;
435 udelay(100);
437 } else {
438 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
439 udelay(10);
442 /* Select FPM registers. */
443 WRT_REG_WORD(&reg->ctrl_status, 0x20);
444 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
446 /* FPM Soft Reset. */
447 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
448 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
450 /* Toggle Fpm Reset. */
451 if (!IS_QLA2200(ha)) {
452 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
453 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
456 /* Select frame buffer registers. */
457 WRT_REG_WORD(&reg->ctrl_status, 0x10);
458 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
460 /* Reset frame buffer FIFOs. */
461 if (IS_QLA2200(ha)) {
462 WRT_FB_CMD_REG(ha, reg, 0xa000);
463 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
464 } else {
465 WRT_FB_CMD_REG(ha, reg, 0x00fc);
467 /* Read back fb_cmd until zero or 3 seconds max */
468 for (cnt = 0; cnt < 3000; cnt++) {
469 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
470 break;
471 udelay(100);
475 /* Select RISC module registers. */
476 WRT_REG_WORD(&reg->ctrl_status, 0);
477 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
479 /* Reset RISC processor. */
480 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
481 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
483 /* Release RISC processor. */
484 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
485 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
488 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
489 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
491 /* Reset ISP chip. */
492 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
494 /* Wait for RISC to recover from reset. */
495 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
497 * It is necessary to for a delay here since the card doesn't
498 * respond to PCI reads during a reset. On some architectures
499 * this will result in an MCA.
501 udelay(20);
502 for (cnt = 30000; cnt; cnt--) {
503 if ((RD_REG_WORD(&reg->ctrl_status) &
504 CSR_ISP_SOFT_RESET) == 0)
505 break;
506 udelay(100);
508 } else
509 udelay(10);
511 /* Reset RISC processor. */
512 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
514 WRT_REG_WORD(&reg->semaphore, 0);
516 /* Release RISC processor. */
517 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
518 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
520 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
521 for (cnt = 0; cnt < 30000; cnt++) {
522 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
523 spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
525 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY) {
526 if (!(test_bit(ABORT_ISP_ACTIVE,
527 &ha->dpc_flags)))
528 spin_unlock_irqrestore(
529 &ha->mbx_reg_lock, mbx_flags);
530 break;
533 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
534 spin_unlock_irqrestore(&ha->mbx_reg_lock,
535 mbx_flags);
537 udelay(100);
539 } else
540 udelay(100);
542 /* Turn on master enable */
543 cmd |= PCI_COMMAND_MASTER;
544 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
546 /* Disable RISC pause on FPM parity error. */
547 if (!IS_QLA2100(ha)) {
548 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
549 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
552 spin_unlock_irqrestore(&ha->hardware_lock, flags);
556 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
557 * @ha: HA context
559 * Returns 0 on success.
561 static inline void
562 qla24xx_reset_risc(scsi_qla_host_t *ha)
564 unsigned long flags = 0;
565 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
566 uint32_t cnt, d2;
567 uint16_t wd;
569 spin_lock_irqsave(&ha->hardware_lock, flags);
571 /* Reset RISC. */
572 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
573 for (cnt = 0; cnt < 30000; cnt++) {
574 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
575 break;
577 udelay(10);
580 WRT_REG_DWORD(&reg->ctrl_status,
581 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
582 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
584 udelay(100);
585 /* Wait for firmware to complete NVRAM accesses. */
586 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
587 for (cnt = 10000 ; cnt && d2; cnt--) {
588 udelay(5);
589 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
590 barrier();
593 /* Wait for soft-reset to complete. */
594 d2 = RD_REG_DWORD(&reg->ctrl_status);
595 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
596 udelay(5);
597 d2 = RD_REG_DWORD(&reg->ctrl_status);
598 barrier();
601 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
602 RD_REG_DWORD(&reg->hccr);
604 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
605 RD_REG_DWORD(&reg->hccr);
607 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
608 RD_REG_DWORD(&reg->hccr);
610 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
611 for (cnt = 6000000 ; cnt && d2; cnt--) {
612 udelay(5);
613 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
614 barrier();
617 spin_unlock_irqrestore(&ha->hardware_lock, flags);
621 * qla24xx_reset_chip() - Reset ISP24xx chip.
622 * @ha: HA context
624 * Returns 0 on success.
626 void
627 qla24xx_reset_chip(scsi_qla_host_t *ha)
629 ha->isp_ops.disable_intrs(ha);
631 /* Perform RISC reset. */
632 qla24xx_reset_risc(ha);
636 * qla2x00_chip_diag() - Test chip for proper operation.
637 * @ha: HA context
639 * Returns 0 on success.
642 qla2x00_chip_diag(scsi_qla_host_t *ha)
644 int rval;
645 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
646 unsigned long flags = 0;
647 uint16_t data;
648 uint32_t cnt;
649 uint16_t mb[5];
651 /* Assume a failed state */
652 rval = QLA_FUNCTION_FAILED;
654 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
655 ha->host_no, (u_long)&reg->flash_address));
657 spin_lock_irqsave(&ha->hardware_lock, flags);
659 /* Reset ISP chip. */
660 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
663 * We need to have a delay here since the card will not respond while
664 * in reset causing an MCA on some architectures.
666 udelay(20);
667 data = qla2x00_debounce_register(&reg->ctrl_status);
668 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
669 udelay(5);
670 data = RD_REG_WORD(&reg->ctrl_status);
671 barrier();
674 if (!cnt)
675 goto chip_diag_failed;
677 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
678 ha->host_no));
680 /* Reset RISC processor. */
681 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
682 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
684 /* Workaround for QLA2312 PCI parity error */
685 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
686 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
687 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
688 udelay(5);
689 data = RD_MAILBOX_REG(ha, reg, 0);
690 barrier();
692 } else
693 udelay(10);
695 if (!cnt)
696 goto chip_diag_failed;
698 /* Check product ID of chip */
699 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
701 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
702 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
703 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
704 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
705 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
706 mb[3] != PROD_ID_3) {
707 qla_printk(KERN_WARNING, ha,
708 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
710 goto chip_diag_failed;
712 ha->product_id[0] = mb[1];
713 ha->product_id[1] = mb[2];
714 ha->product_id[2] = mb[3];
715 ha->product_id[3] = mb[4];
717 /* Adjust fw RISC transfer size */
718 if (ha->request_q_length > 1024)
719 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
720 else
721 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
722 ha->request_q_length;
724 if (IS_QLA2200(ha) &&
725 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
726 /* Limit firmware transfer size with a 2200A */
727 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
728 ha->host_no));
730 ha->device_type |= DT_ISP2200A;
731 ha->fw_transfer_size = 128;
734 /* Wrap Incoming Mailboxes Test. */
735 spin_unlock_irqrestore(&ha->hardware_lock, flags);
737 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
738 rval = qla2x00_mbx_reg_test(ha);
739 if (rval) {
740 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
741 ha->host_no));
742 qla_printk(KERN_WARNING, ha,
743 "Failed mailbox send register test\n");
745 else {
746 /* Flag a successful rval */
747 rval = QLA_SUCCESS;
749 spin_lock_irqsave(&ha->hardware_lock, flags);
751 chip_diag_failed:
752 if (rval)
753 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
754 "****\n", ha->host_no));
756 spin_unlock_irqrestore(&ha->hardware_lock, flags);
758 return (rval);
762 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
763 * @ha: HA context
765 * Returns 0 on success.
768 qla24xx_chip_diag(scsi_qla_host_t *ha)
770 int rval;
772 /* Perform RISC reset. */
773 qla24xx_reset_risc(ha);
775 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
777 rval = qla2x00_mbx_reg_test(ha);
778 if (rval) {
779 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
780 ha->host_no));
781 qla_printk(KERN_WARNING, ha,
782 "Failed mailbox send register test\n");
783 } else {
784 /* Flag a successful rval */
785 rval = QLA_SUCCESS;
788 return rval;
791 static void
792 qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
794 ha->fw_dumped = 0;
795 ha->fw_dump24_len = sizeof(struct qla24xx_fw_dump);
796 ha->fw_dump24_len += (ha->fw_memory_size - 0x100000) * sizeof(uint32_t);
797 ha->fw_dump24 = vmalloc(ha->fw_dump24_len);
798 if (ha->fw_dump24)
799 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware "
800 "dump...\n", ha->fw_dump24_len / 1024);
801 else
802 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
803 "firmware dump!!!\n", ha->fw_dump24_len / 1024);
807 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
808 * @ha: HA context
810 * Returns 0 on success.
812 static void
813 qla2x00_resize_request_q(scsi_qla_host_t *ha)
815 int rval;
816 uint16_t fw_iocb_cnt = 0;
817 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
818 dma_addr_t request_dma;
819 request_t *request_ring;
821 /* Valid only on recent ISPs. */
822 if (IS_QLA2100(ha) || IS_QLA2200(ha))
823 return;
825 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
826 qla2x00_alloc_fw_dump(ha);
828 /* Retrieve IOCB counts available to the firmware. */
829 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
830 if (rval)
831 return;
832 /* No point in continuing if current settings are sufficient. */
833 if (fw_iocb_cnt < 1024)
834 return;
835 if (ha->request_q_length >= request_q_length)
836 return;
838 /* Attempt to claim larger area for request queue. */
839 request_ring = dma_alloc_coherent(&ha->pdev->dev,
840 (request_q_length + 1) * sizeof(request_t), &request_dma,
841 GFP_KERNEL);
842 if (request_ring == NULL)
843 return;
845 /* Resize successful, report extensions. */
846 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
847 (ha->fw_memory_size + 1) / 1024);
848 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
849 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
851 /* Clear old allocations. */
852 dma_free_coherent(&ha->pdev->dev,
853 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
854 ha->request_dma);
856 /* Begin using larger queue. */
857 ha->request_q_length = request_q_length;
858 ha->request_ring = request_ring;
859 ha->request_dma = request_dma;
863 * qla2x00_setup_chip() - Load and start RISC firmware.
864 * @ha: HA context
866 * Returns 0 on success.
868 static int
869 qla2x00_setup_chip(scsi_qla_host_t *ha)
871 int rval;
872 uint32_t srisc_address = 0;
874 /* Load firmware sequences */
875 rval = ha->isp_ops.load_risc(ha, &srisc_address);
876 if (rval == QLA_SUCCESS) {
877 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
878 "code.\n", ha->host_no));
880 rval = qla2x00_verify_checksum(ha, srisc_address);
881 if (rval == QLA_SUCCESS) {
882 /* Start firmware execution. */
883 DEBUG(printk("scsi(%ld): Checksum OK, start "
884 "firmware.\n", ha->host_no));
886 rval = qla2x00_execute_fw(ha, srisc_address);
887 /* Retrieve firmware information. */
888 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
889 qla2x00_get_fw_version(ha,
890 &ha->fw_major_version,
891 &ha->fw_minor_version,
892 &ha->fw_subminor_version,
893 &ha->fw_attributes, &ha->fw_memory_size);
894 qla2x00_resize_request_q(ha);
896 } else {
897 DEBUG2(printk(KERN_INFO
898 "scsi(%ld): ISP Firmware failed checksum.\n",
899 ha->host_no));
903 if (rval) {
904 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
905 ha->host_no));
908 return (rval);
912 * qla2x00_init_response_q_entries() - Initializes response queue entries.
913 * @ha: HA context
915 * Beginning of request ring has initialization control block already built
916 * by nvram config routine.
918 * Returns 0 on success.
920 static void
921 qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
923 uint16_t cnt;
924 response_t *pkt;
926 pkt = ha->response_ring_ptr;
927 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
928 pkt->signature = RESPONSE_PROCESSED;
929 pkt++;
935 * qla2x00_update_fw_options() - Read and process firmware options.
936 * @ha: HA context
938 * Returns 0 on success.
940 void
941 qla2x00_update_fw_options(scsi_qla_host_t *ha)
943 uint16_t swing, emphasis, tx_sens, rx_sens;
945 memset(ha->fw_options, 0, sizeof(ha->fw_options));
946 qla2x00_get_fw_options(ha, ha->fw_options);
948 if (IS_QLA2100(ha) || IS_QLA2200(ha))
949 return;
951 /* Serial Link options. */
952 DEBUG3(printk("scsi(%ld): Serial link options:\n",
953 ha->host_no));
954 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
955 sizeof(ha->fw_seriallink_options)));
957 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
958 if (ha->fw_seriallink_options[3] & BIT_2) {
959 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
961 /* 1G settings */
962 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
963 emphasis = (ha->fw_seriallink_options[2] &
964 (BIT_4 | BIT_3)) >> 3;
965 tx_sens = ha->fw_seriallink_options[0] &
966 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
967 rx_sens = (ha->fw_seriallink_options[0] &
968 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
969 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
970 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
971 if (rx_sens == 0x0)
972 rx_sens = 0x3;
973 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
974 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
975 ha->fw_options[10] |= BIT_5 |
976 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
977 (tx_sens & (BIT_1 | BIT_0));
979 /* 2G settings */
980 swing = (ha->fw_seriallink_options[2] &
981 (BIT_7 | BIT_6 | BIT_5)) >> 5;
982 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
983 tx_sens = ha->fw_seriallink_options[1] &
984 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
985 rx_sens = (ha->fw_seriallink_options[1] &
986 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
987 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
988 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
989 if (rx_sens == 0x0)
990 rx_sens = 0x3;
991 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
992 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
993 ha->fw_options[11] |= BIT_5 |
994 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
995 (tx_sens & (BIT_1 | BIT_0));
998 /* FCP2 options. */
999 /* Return command IOCBs without waiting for an ABTS to complete. */
1000 ha->fw_options[3] |= BIT_13;
1002 /* LED scheme. */
1003 if (ha->flags.enable_led_scheme)
1004 ha->fw_options[2] |= BIT_12;
1006 /* Detect ISP6312. */
1007 if (IS_QLA6312(ha))
1008 ha->fw_options[2] |= BIT_13;
1010 /* Update firmware options. */
1011 qla2x00_set_fw_options(ha, ha->fw_options);
1014 void
1015 qla24xx_update_fw_options(scsi_qla_host_t *ha)
1017 int rval;
1019 /* Update Serial Link options. */
1020 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
1021 return;
1023 rval = qla2x00_set_serdes_params(ha,
1024 le16_to_cpu(ha->fw_seriallink_options24[1]),
1025 le16_to_cpu(ha->fw_seriallink_options24[2]),
1026 le16_to_cpu(ha->fw_seriallink_options24[3]));
1027 if (rval != QLA_SUCCESS) {
1028 qla_printk(KERN_WARNING, ha,
1029 "Unable to update Serial Link options (%x).\n", rval);
1033 void
1034 qla2x00_config_rings(struct scsi_qla_host *ha)
1036 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1038 /* Setup ring parameters in initialization control block. */
1039 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1040 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1041 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1042 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1043 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1044 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1045 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1046 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1048 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1049 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1050 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1051 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1052 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1055 void
1056 qla24xx_config_rings(struct scsi_qla_host *ha)
1058 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1059 struct init_cb_24xx *icb;
1061 /* Setup ring parameters in initialization control block. */
1062 icb = (struct init_cb_24xx *)ha->init_cb;
1063 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1064 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1065 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1066 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1067 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1068 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1069 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1070 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1072 WRT_REG_DWORD(&reg->req_q_in, 0);
1073 WRT_REG_DWORD(&reg->req_q_out, 0);
1074 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1075 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1076 RD_REG_DWORD(&reg->rsp_q_out);
1080 * qla2x00_init_rings() - Initializes firmware.
1081 * @ha: HA context
1083 * Beginning of request ring has initialization control block already built
1084 * by nvram config routine.
1086 * Returns 0 on success.
1088 static int
1089 qla2x00_init_rings(scsi_qla_host_t *ha)
1091 int rval;
1092 unsigned long flags = 0;
1093 int cnt;
1095 spin_lock_irqsave(&ha->hardware_lock, flags);
1097 /* Clear outstanding commands array. */
1098 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1099 ha->outstanding_cmds[cnt] = NULL;
1101 ha->current_outstanding_cmd = 0;
1103 /* Clear RSCN queue. */
1104 ha->rscn_in_ptr = 0;
1105 ha->rscn_out_ptr = 0;
1107 /* Initialize firmware. */
1108 ha->request_ring_ptr = ha->request_ring;
1109 ha->req_ring_index = 0;
1110 ha->req_q_cnt = ha->request_q_length;
1111 ha->response_ring_ptr = ha->response_ring;
1112 ha->rsp_ring_index = 0;
1114 /* Initialize response queue entries */
1115 qla2x00_init_response_q_entries(ha);
1117 ha->isp_ops.config_rings(ha);
1119 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1121 /* Update any ISP specific firmware options before initialization. */
1122 ha->isp_ops.update_fw_options(ha);
1124 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
1125 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1126 if (rval) {
1127 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1128 ha->host_no));
1129 } else {
1130 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1131 ha->host_no));
1134 return (rval);
1138 * qla2x00_fw_ready() - Waits for firmware ready.
1139 * @ha: HA context
1141 * Returns 0 on success.
1143 static int
1144 qla2x00_fw_ready(scsi_qla_host_t *ha)
1146 int rval;
1147 unsigned long wtime, mtime;
1148 uint16_t min_wait; /* Minimum wait time if loop is down */
1149 uint16_t wait_time; /* Wait time if loop is coming ready */
1150 uint16_t fw_state;
1152 rval = QLA_SUCCESS;
1154 /* 20 seconds for loop down. */
1155 min_wait = 20;
1158 * Firmware should take at most one RATOV to login, plus 5 seconds for
1159 * our own processing.
1161 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1162 wait_time = min_wait;
1165 /* Min wait time if loop down */
1166 mtime = jiffies + (min_wait * HZ);
1168 /* wait time before firmware ready */
1169 wtime = jiffies + (wait_time * HZ);
1171 /* Wait for ISP to finish LIP */
1172 if (!ha->flags.init_done)
1173 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1175 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1176 ha->host_no));
1178 do {
1179 rval = qla2x00_get_firmware_state(ha, &fw_state);
1180 if (rval == QLA_SUCCESS) {
1181 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1182 ha->device_flags &= ~DFLG_NO_CABLE;
1184 if (fw_state == FSTATE_READY) {
1185 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1186 ha->host_no));
1188 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1189 &ha->login_timeout, &ha->r_a_tov);
1191 rval = QLA_SUCCESS;
1192 break;
1195 rval = QLA_FUNCTION_FAILED;
1197 if (atomic_read(&ha->loop_down_timer) &&
1198 (fw_state >= FSTATE_LOSS_OF_SYNC ||
1199 fw_state == FSTATE_WAIT_AL_PA)) {
1200 /* Loop down. Timeout on min_wait for states
1201 * other than Wait for Login.
1203 if (time_after_eq(jiffies, mtime)) {
1204 qla_printk(KERN_INFO, ha,
1205 "Cable is unplugged...\n");
1207 ha->device_flags |= DFLG_NO_CABLE;
1208 break;
1211 } else {
1212 /* Mailbox cmd failed. Timeout on min_wait. */
1213 if (time_after_eq(jiffies, mtime))
1214 break;
1217 if (time_after_eq(jiffies, wtime))
1218 break;
1220 /* Delay for a while */
1221 msleep(500);
1223 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1224 ha->host_no, fw_state, jiffies));
1225 } while (1);
1227 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1228 ha->host_no, fw_state, jiffies));
1230 if (rval) {
1231 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1232 ha->host_no));
1235 return (rval);
1239 * qla2x00_configure_hba
1240 * Setup adapter context.
1242 * Input:
1243 * ha = adapter state pointer.
1245 * Returns:
1246 * 0 = success
1248 * Context:
1249 * Kernel context.
1251 static int
1252 qla2x00_configure_hba(scsi_qla_host_t *ha)
1254 int rval;
1255 uint16_t loop_id;
1256 uint16_t topo;
1257 uint8_t al_pa;
1258 uint8_t area;
1259 uint8_t domain;
1260 char connect_type[22];
1262 /* Get host addresses. */
1263 rval = qla2x00_get_adapter_id(ha,
1264 &loop_id, &al_pa, &area, &domain, &topo);
1265 if (rval != QLA_SUCCESS) {
1266 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
1267 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1268 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1269 __func__, ha->host_no));
1270 } else {
1271 qla_printk(KERN_WARNING, ha,
1272 "ERROR -- Unable to get host loop ID.\n");
1273 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1275 return (rval);
1278 if (topo == 4) {
1279 qla_printk(KERN_INFO, ha,
1280 "Cannot get topology - retrying.\n");
1281 return (QLA_FUNCTION_FAILED);
1284 ha->loop_id = loop_id;
1286 /* initialize */
1287 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1288 ha->operating_mode = LOOP;
1290 switch (topo) {
1291 case 0:
1292 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1293 ha->host_no));
1294 ha->current_topology = ISP_CFG_NL;
1295 strcpy(connect_type, "(Loop)");
1296 break;
1298 case 1:
1299 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1300 ha->host_no));
1301 ha->current_topology = ISP_CFG_FL;
1302 strcpy(connect_type, "(FL_Port)");
1303 break;
1305 case 2:
1306 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1307 ha->host_no));
1308 ha->operating_mode = P2P;
1309 ha->current_topology = ISP_CFG_N;
1310 strcpy(connect_type, "(N_Port-to-N_Port)");
1311 break;
1313 case 3:
1314 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1315 ha->host_no));
1316 ha->operating_mode = P2P;
1317 ha->current_topology = ISP_CFG_F;
1318 strcpy(connect_type, "(F_Port)");
1319 break;
1321 default:
1322 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1323 "Using NL.\n",
1324 ha->host_no, topo));
1325 ha->current_topology = ISP_CFG_NL;
1326 strcpy(connect_type, "(Loop)");
1327 break;
1330 /* Save Host port and loop ID. */
1331 /* byte order - Big Endian */
1332 ha->d_id.b.domain = domain;
1333 ha->d_id.b.area = area;
1334 ha->d_id.b.al_pa = al_pa;
1336 if (!ha->flags.init_done)
1337 qla_printk(KERN_INFO, ha,
1338 "Topology - %s, Host Loop address 0x%x\n",
1339 connect_type, ha->loop_id);
1341 if (rval) {
1342 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1343 } else {
1344 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1347 return(rval);
1351 * NVRAM configuration for ISP 2xxx
1353 * Input:
1354 * ha = adapter block pointer.
1356 * Output:
1357 * initialization control block in response_ring
1358 * host adapters parameters in host adapter block
1360 * Returns:
1361 * 0 = success.
1364 qla2x00_nvram_config(scsi_qla_host_t *ha)
1366 int rval;
1367 uint8_t chksum = 0;
1368 uint16_t cnt;
1369 uint8_t *dptr1, *dptr2;
1370 init_cb_t *icb = ha->init_cb;
1371 nvram_t *nv = (nvram_t *)ha->request_ring;
1372 uint8_t *ptr = (uint8_t *)ha->request_ring;
1373 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1375 rval = QLA_SUCCESS;
1377 /* Determine NVRAM starting address. */
1378 ha->nvram_size = sizeof(nvram_t);
1379 ha->nvram_base = 0;
1380 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1381 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1382 ha->nvram_base = 0x80;
1384 /* Get NVRAM data and calculate checksum. */
1385 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1386 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1387 chksum += *ptr++;
1389 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1390 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1391 ha->nvram_size));
1393 /* Bad NVRAM data, set defaults parameters. */
1394 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1395 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1396 /* Reset NVRAM data. */
1397 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1398 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1399 nv->nvram_version);
1400 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1401 "invalid -- WWPN) defaults.\n");
1404 * Set default initialization control block.
1406 memset(nv, 0, ha->nvram_size);
1407 nv->parameter_block_version = ICB_VERSION;
1409 if (IS_QLA23XX(ha)) {
1410 nv->firmware_options[0] = BIT_2 | BIT_1;
1411 nv->firmware_options[1] = BIT_7 | BIT_5;
1412 nv->add_firmware_options[0] = BIT_5;
1413 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1414 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1415 nv->special_options[1] = BIT_7;
1416 } else if (IS_QLA2200(ha)) {
1417 nv->firmware_options[0] = BIT_2 | BIT_1;
1418 nv->firmware_options[1] = BIT_7 | BIT_5;
1419 nv->add_firmware_options[0] = BIT_5;
1420 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1421 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1422 } else if (IS_QLA2100(ha)) {
1423 nv->firmware_options[0] = BIT_3 | BIT_1;
1424 nv->firmware_options[1] = BIT_5;
1425 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1428 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1429 nv->execution_throttle = __constant_cpu_to_le16(16);
1430 nv->retry_count = 8;
1431 nv->retry_delay = 1;
1433 nv->port_name[0] = 33;
1434 nv->port_name[3] = 224;
1435 nv->port_name[4] = 139;
1437 nv->login_timeout = 4;
1440 * Set default host adapter parameters
1442 nv->host_p[1] = BIT_2;
1443 nv->reset_delay = 5;
1444 nv->port_down_retry_count = 8;
1445 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1446 nv->link_down_timeout = 60;
1448 rval = 1;
1451 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1453 * The SN2 does not provide BIOS emulation which means you can't change
1454 * potentially bogus BIOS settings. Force the use of default settings
1455 * for link rate and frame size. Hope that the rest of the settings
1456 * are valid.
1458 if (ia64_platform_is("sn2")) {
1459 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1460 if (IS_QLA23XX(ha))
1461 nv->special_options[1] = BIT_7;
1463 #endif
1465 /* Reset Initialization control block */
1466 memset(icb, 0, ha->init_cb_size);
1469 * Setup driver NVRAM options.
1471 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1472 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1473 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1474 nv->firmware_options[1] &= ~BIT_4;
1476 if (IS_QLA23XX(ha)) {
1477 nv->firmware_options[0] |= BIT_2;
1478 nv->firmware_options[0] &= ~BIT_3;
1479 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1481 if (IS_QLA2300(ha)) {
1482 if (ha->fb_rev == FPM_2310) {
1483 strcpy(ha->model_number, "QLA2310");
1484 } else {
1485 strcpy(ha->model_number, "QLA2300");
1487 } else {
1488 if (rval == 0 &&
1489 memcmp(nv->model_number, BINZERO,
1490 sizeof(nv->model_number)) != 0) {
1491 char *st, *en;
1493 strncpy(ha->model_number, nv->model_number,
1494 sizeof(nv->model_number));
1495 st = en = ha->model_number;
1496 en += sizeof(nv->model_number) - 1;
1497 while (en > st) {
1498 if (*en != 0x20 && *en != 0x00)
1499 break;
1500 *en-- = '\0';
1502 } else {
1503 uint16_t index;
1505 index = (ha->pdev->subsystem_device & 0xff);
1506 if (index < QLA_MODEL_NAMES) {
1507 strcpy(ha->model_number,
1508 qla2x00_model_name[index * 2]);
1509 ha->model_desc =
1510 qla2x00_model_name[index * 2 + 1];
1511 } else {
1512 strcpy(ha->model_number, "QLA23xx");
1516 } else if (IS_QLA2200(ha)) {
1517 nv->firmware_options[0] |= BIT_2;
1519 * 'Point-to-point preferred, else loop' is not a safe
1520 * connection mode setting.
1522 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1523 (BIT_5 | BIT_4)) {
1524 /* Force 'loop preferred, else point-to-point'. */
1525 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1526 nv->add_firmware_options[0] |= BIT_5;
1528 strcpy(ha->model_number, "QLA22xx");
1529 } else /*if (IS_QLA2100(ha))*/ {
1530 strcpy(ha->model_number, "QLA2100");
1534 * Copy over NVRAM RISC parameter block to initialization control block.
1536 dptr1 = (uint8_t *)icb;
1537 dptr2 = (uint8_t *)&nv->parameter_block_version;
1538 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1539 while (cnt--)
1540 *dptr1++ = *dptr2++;
1542 /* Copy 2nd half. */
1543 dptr1 = (uint8_t *)icb->add_firmware_options;
1544 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1545 while (cnt--)
1546 *dptr1++ = *dptr2++;
1548 /* Prepare nodename */
1549 if ((icb->firmware_options[1] & BIT_6) == 0) {
1551 * Firmware will apply the following mask if the nodename was
1552 * not provided.
1554 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1555 icb->node_name[0] &= 0xF0;
1559 * Set host adapter parameters.
1561 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1562 /* Always load RISC code on non ISP2[12]00 chips. */
1563 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1564 ha->flags.disable_risc_code_load = 0;
1565 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1566 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1567 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1568 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1570 ha->operating_mode =
1571 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1573 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1574 sizeof(ha->fw_seriallink_options));
1576 /* save HBA serial number */
1577 ha->serial0 = icb->port_name[5];
1578 ha->serial1 = icb->port_name[6];
1579 ha->serial2 = icb->port_name[7];
1580 ha->node_name = icb->node_name;
1581 ha->port_name = icb->port_name;
1583 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1585 ha->retry_count = nv->retry_count;
1587 /* Set minimum login_timeout to 4 seconds. */
1588 if (nv->login_timeout < ql2xlogintimeout)
1589 nv->login_timeout = ql2xlogintimeout;
1590 if (nv->login_timeout < 4)
1591 nv->login_timeout = 4;
1592 ha->login_timeout = nv->login_timeout;
1593 icb->login_timeout = nv->login_timeout;
1595 /* Set minimum RATOV to 200 tenths of a second. */
1596 ha->r_a_tov = 200;
1598 ha->loop_reset_delay = nv->reset_delay;
1600 /* Link Down Timeout = 0:
1602 * When Port Down timer expires we will start returning
1603 * I/O's to OS with "DID_NO_CONNECT".
1605 * Link Down Timeout != 0:
1607 * The driver waits for the link to come up after link down
1608 * before returning I/Os to OS with "DID_NO_CONNECT".
1610 if (nv->link_down_timeout == 0) {
1611 ha->loop_down_abort_time =
1612 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1613 } else {
1614 ha->link_down_timeout = nv->link_down_timeout;
1615 ha->loop_down_abort_time =
1616 (LOOP_DOWN_TIME - ha->link_down_timeout);
1620 * Need enough time to try and get the port back.
1622 ha->port_down_retry_count = nv->port_down_retry_count;
1623 if (qlport_down_retry)
1624 ha->port_down_retry_count = qlport_down_retry;
1625 /* Set login_retry_count */
1626 ha->login_retry_count = nv->retry_count;
1627 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1628 ha->port_down_retry_count > 3)
1629 ha->login_retry_count = ha->port_down_retry_count;
1630 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1631 ha->login_retry_count = ha->port_down_retry_count;
1632 if (ql2xloginretrycount)
1633 ha->login_retry_count = ql2xloginretrycount;
1635 icb->lun_enables = __constant_cpu_to_le16(0);
1636 icb->command_resource_count = 0;
1637 icb->immediate_notify_resource_count = 0;
1638 icb->timeout = __constant_cpu_to_le16(0);
1640 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1641 /* Enable RIO */
1642 icb->firmware_options[0] &= ~BIT_3;
1643 icb->add_firmware_options[0] &=
1644 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1645 icb->add_firmware_options[0] |= BIT_2;
1646 icb->response_accumulation_timer = 3;
1647 icb->interrupt_delay_timer = 5;
1649 ha->flags.process_response_queue = 1;
1650 } else {
1651 /* Enable ZIO. */
1652 if (!ha->flags.init_done) {
1653 ha->zio_mode = icb->add_firmware_options[0] &
1654 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1655 ha->zio_timer = icb->interrupt_delay_timer ?
1656 icb->interrupt_delay_timer: 2;
1658 icb->add_firmware_options[0] &=
1659 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1660 ha->flags.process_response_queue = 0;
1661 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1662 ha->zio_mode = QLA_ZIO_MODE_6;
1664 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1665 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1666 ha->zio_timer * 100));
1667 qla_printk(KERN_INFO, ha,
1668 "ZIO mode %d enabled; timer delay (%d us).\n",
1669 ha->zio_mode, ha->zio_timer * 100);
1671 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1672 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1673 ha->flags.process_response_queue = 1;
1677 if (rval) {
1678 DEBUG2_3(printk(KERN_WARNING
1679 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1681 return (rval);
1684 static void
1685 qla2x00_rport_add(void *data)
1687 fc_port_t *fcport = data;
1689 qla2x00_reg_remote_port(fcport->ha, fcport);
1692 static void
1693 qla2x00_rport_del(void *data)
1695 fc_port_t *fcport = data;
1696 struct fc_rport *rport;
1697 unsigned long flags;
1699 spin_lock_irqsave(&fcport->rport_lock, flags);
1700 rport = fcport->drport;
1701 fcport->drport = NULL;
1702 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1703 if (rport)
1704 fc_remote_port_delete(rport);
1709 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1710 * @ha: HA context
1711 * @flags: allocation flags
1713 * Returns a pointer to the allocated fcport, or NULL, if none available.
1715 fc_port_t *
1716 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1718 fc_port_t *fcport;
1720 fcport = kmalloc(sizeof(fc_port_t), flags);
1721 if (fcport == NULL)
1722 return (fcport);
1724 /* Setup fcport template structure. */
1725 memset(fcport, 0, sizeof (fc_port_t));
1726 fcport->ha = ha;
1727 fcport->port_type = FCT_UNKNOWN;
1728 fcport->loop_id = FC_NO_LOOP_ID;
1729 fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
1730 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1731 fcport->flags = FCF_RLC_SUPPORT;
1732 fcport->supported_classes = FC_COS_UNSPECIFIED;
1733 spin_lock_init(&fcport->rport_lock);
1734 INIT_WORK(&fcport->rport_add_work, qla2x00_rport_add, fcport);
1735 INIT_WORK(&fcport->rport_del_work, qla2x00_rport_del, fcport);
1737 return (fcport);
1741 * qla2x00_configure_loop
1742 * Updates Fibre Channel Device Database with what is actually on loop.
1744 * Input:
1745 * ha = adapter block pointer.
1747 * Returns:
1748 * 0 = success.
1749 * 1 = error.
1750 * 2 = database was full and device was not configured.
1752 static int
1753 qla2x00_configure_loop(scsi_qla_host_t *ha)
1755 int rval;
1756 unsigned long flags, save_flags;
1758 rval = QLA_SUCCESS;
1760 /* Get Initiator ID */
1761 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1762 rval = qla2x00_configure_hba(ha);
1763 if (rval != QLA_SUCCESS) {
1764 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1765 ha->host_no));
1766 return (rval);
1770 save_flags = flags = ha->dpc_flags;
1771 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1772 ha->host_no, flags));
1775 * If we have both an RSCN and PORT UPDATE pending then handle them
1776 * both at the same time.
1778 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1779 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1781 /* Determine what we need to do */
1782 if (ha->current_topology == ISP_CFG_FL &&
1783 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1785 ha->flags.rscn_queue_overflow = 1;
1786 set_bit(RSCN_UPDATE, &flags);
1788 } else if (ha->current_topology == ISP_CFG_F &&
1789 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1791 ha->flags.rscn_queue_overflow = 1;
1792 set_bit(RSCN_UPDATE, &flags);
1793 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1795 } else if (!ha->flags.online ||
1796 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1798 ha->flags.rscn_queue_overflow = 1;
1799 set_bit(RSCN_UPDATE, &flags);
1800 set_bit(LOCAL_LOOP_UPDATE, &flags);
1803 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1804 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1805 rval = QLA_FUNCTION_FAILED;
1806 } else {
1807 rval = qla2x00_configure_local_loop(ha);
1811 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1812 if (LOOP_TRANSITION(ha)) {
1813 rval = QLA_FUNCTION_FAILED;
1814 } else {
1815 rval = qla2x00_configure_fabric(ha);
1819 if (rval == QLA_SUCCESS) {
1820 if (atomic_read(&ha->loop_down_timer) ||
1821 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1822 rval = QLA_FUNCTION_FAILED;
1823 } else {
1824 atomic_set(&ha->loop_state, LOOP_READY);
1826 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1830 if (rval) {
1831 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1832 __func__, ha->host_no));
1833 } else {
1834 DEBUG3(printk("%s: exiting normally\n", __func__));
1837 /* Restore state if a resync event occured during processing */
1838 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1839 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1840 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1841 if (test_bit(RSCN_UPDATE, &save_flags))
1842 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1845 return (rval);
1851 * qla2x00_configure_local_loop
1852 * Updates Fibre Channel Device Database with local loop devices.
1854 * Input:
1855 * ha = adapter block pointer.
1857 * Returns:
1858 * 0 = success.
1860 static int
1861 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1863 int rval, rval2;
1864 int found_devs;
1865 int found;
1866 fc_port_t *fcport, *new_fcport;
1868 uint16_t index;
1869 uint16_t entries;
1870 char *id_iter;
1871 uint16_t loop_id;
1872 uint8_t domain, area, al_pa;
1874 found_devs = 0;
1875 new_fcport = NULL;
1876 entries = MAX_FIBRE_DEVICES;
1878 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1879 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1881 /* Get list of logged in devices. */
1882 memset(ha->gid_list, 0, GID_LIST_SIZE);
1883 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1884 &entries);
1885 if (rval != QLA_SUCCESS)
1886 goto cleanup_allocation;
1888 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1889 ha->host_no, entries));
1890 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1891 entries * sizeof(struct gid_list_info)));
1893 /* Allocate temporary fcport for any new fcports discovered. */
1894 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1895 if (new_fcport == NULL) {
1896 rval = QLA_MEMORY_ALLOC_FAILED;
1897 goto cleanup_allocation;
1899 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1902 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1904 list_for_each_entry(fcport, &ha->fcports, list) {
1905 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1906 fcport->port_type != FCT_BROADCAST &&
1907 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1909 DEBUG(printk("scsi(%ld): Marking port lost, "
1910 "loop_id=0x%04x\n",
1911 ha->host_no, fcport->loop_id));
1913 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1914 fcport->flags &= ~FCF_FARP_DONE;
1918 /* Add devices to port list. */
1919 id_iter = (char *)ha->gid_list;
1920 for (index = 0; index < entries; index++) {
1921 domain = ((struct gid_list_info *)id_iter)->domain;
1922 area = ((struct gid_list_info *)id_iter)->area;
1923 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1924 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1925 loop_id = (uint16_t)
1926 ((struct gid_list_info *)id_iter)->loop_id_2100;
1927 else
1928 loop_id = le16_to_cpu(
1929 ((struct gid_list_info *)id_iter)->loop_id);
1930 id_iter += ha->gid_list_info_size;
1932 /* Bypass reserved domain fields. */
1933 if ((domain & 0xf0) == 0xf0)
1934 continue;
1936 /* Bypass if not same domain and area of adapter. */
1937 if (area && domain &&
1938 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
1939 continue;
1941 /* Bypass invalid local loop ID. */
1942 if (loop_id > LAST_LOCAL_LOOP_ID)
1943 continue;
1945 /* Fill in member data. */
1946 new_fcport->d_id.b.domain = domain;
1947 new_fcport->d_id.b.area = area;
1948 new_fcport->d_id.b.al_pa = al_pa;
1949 new_fcport->loop_id = loop_id;
1950 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1951 if (rval2 != QLA_SUCCESS) {
1952 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1953 "information -- get_port_database=%x, "
1954 "loop_id=0x%04x\n",
1955 ha->host_no, rval2, new_fcport->loop_id));
1956 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
1957 ha->host_no));
1958 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
1959 continue;
1962 /* Check for matching device in port list. */
1963 found = 0;
1964 fcport = NULL;
1965 list_for_each_entry(fcport, &ha->fcports, list) {
1966 if (memcmp(new_fcport->port_name, fcport->port_name,
1967 WWN_SIZE))
1968 continue;
1970 fcport->flags &= ~(FCF_FABRIC_DEVICE |
1971 FCF_PERSISTENT_BOUND);
1972 fcport->loop_id = new_fcport->loop_id;
1973 fcport->port_type = new_fcport->port_type;
1974 fcport->d_id.b24 = new_fcport->d_id.b24;
1975 memcpy(fcport->node_name, new_fcport->node_name,
1976 WWN_SIZE);
1978 found++;
1979 break;
1982 if (!found) {
1983 /* New device, add to fcports list. */
1984 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
1985 list_add_tail(&new_fcport->list, &ha->fcports);
1987 /* Allocate a new replacement fcport. */
1988 fcport = new_fcport;
1989 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1990 if (new_fcport == NULL) {
1991 rval = QLA_MEMORY_ALLOC_FAILED;
1992 goto cleanup_allocation;
1994 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1997 qla2x00_update_fcport(ha, fcport);
1999 found_devs++;
2002 cleanup_allocation:
2003 kfree(new_fcport);
2005 if (rval != QLA_SUCCESS) {
2006 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2007 "rval=%x\n", ha->host_no, rval));
2010 if (found_devs) {
2011 ha->device_flags |= DFLG_LOCAL_DEVICES;
2012 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2015 return (rval);
2018 static void
2019 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2021 fc_port_t *fcport;
2023 qla2x00_mark_all_devices_lost(ha, 0);
2024 list_for_each_entry(fcport, &ha->fcports, list) {
2025 if (fcport->port_type != FCT_TARGET)
2026 continue;
2028 qla2x00_update_fcport(ha, fcport);
2033 * qla2x00_update_fcport
2034 * Updates device on list.
2036 * Input:
2037 * ha = adapter block pointer.
2038 * fcport = port structure pointer.
2040 * Return:
2041 * 0 - Success
2042 * BIT_0 - error
2044 * Context:
2045 * Kernel context.
2047 void
2048 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2050 fcport->ha = ha;
2051 fcport->login_retry = 0;
2052 fcport->port_login_retry_count = ha->port_down_retry_count *
2053 PORT_RETRY_TIME;
2054 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2055 PORT_RETRY_TIME);
2056 fcport->flags &= ~FCF_LOGIN_NEEDED;
2058 if (fcport->port_type == FCT_INITIATOR ||
2059 fcport->port_type == FCT_BROADCAST)
2060 fcport->device_type = TYPE_PROCESSOR;
2062 atomic_set(&fcport->state, FCS_ONLINE);
2064 if (ha->flags.init_done)
2065 qla2x00_reg_remote_port(ha, fcport);
2068 void
2069 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2071 struct fc_rport_identifiers rport_ids;
2072 struct fc_rport *rport;
2073 unsigned long flags;
2075 if (fcport->drport)
2076 qla2x00_rport_del(fcport);
2077 if (fcport->rport)
2078 return;
2080 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2081 rport_ids.port_name = wwn_to_u64(fcport->port_name);
2082 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2083 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2084 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2085 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2086 if (!rport) {
2087 qla_printk(KERN_WARNING, ha,
2088 "Unable to allocate fc remote port!\n");
2089 return;
2091 spin_lock_irqsave(&fcport->rport_lock, flags);
2092 fcport->rport = rport;
2093 *((fc_port_t **)rport->dd_data) = fcport;
2094 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2096 rport->supported_classes = fcport->supported_classes;
2098 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2099 if (fcport->port_type == FCT_INITIATOR)
2100 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2101 if (fcport->port_type == FCT_TARGET)
2102 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2103 fc_remote_port_rolechg(rport, rport_ids.roles);
2105 if (rport->scsi_target_id != -1 &&
2106 rport->scsi_target_id < ha->host->max_id)
2107 fcport->os_target_id = rport->scsi_target_id;
2111 * qla2x00_configure_fabric
2112 * Setup SNS devices with loop ID's.
2114 * Input:
2115 * ha = adapter block pointer.
2117 * Returns:
2118 * 0 = success.
2119 * BIT_0 = error
2121 static int
2122 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2124 int rval, rval2;
2125 fc_port_t *fcport, *fcptemp;
2126 uint16_t next_loopid;
2127 uint16_t mb[MAILBOX_REGISTER_COUNT];
2128 uint16_t loop_id;
2129 LIST_HEAD(new_fcports);
2131 /* If FL port exists, then SNS is present */
2132 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2133 loop_id = NPH_F_PORT;
2134 else
2135 loop_id = SNS_FL_PORT;
2136 rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
2137 if (rval != QLA_SUCCESS) {
2138 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2139 "Port\n", ha->host_no));
2141 ha->device_flags &= ~SWITCH_FOUND;
2142 return (QLA_SUCCESS);
2145 /* Mark devices that need re-synchronization. */
2146 rval2 = qla2x00_device_resync(ha);
2147 if (rval2 == QLA_RSCNS_HANDLED) {
2148 /* No point doing the scan, just continue. */
2149 return (QLA_SUCCESS);
2151 do {
2152 /* FDMI support. */
2153 if (ql2xfdmienable &&
2154 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2155 qla2x00_fdmi_register(ha);
2157 /* Ensure we are logged into the SNS. */
2158 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2159 loop_id = NPH_SNS;
2160 else
2161 loop_id = SIMPLE_NAME_SERVER;
2162 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
2163 0xfc, mb, BIT_1 | BIT_0);
2164 if (mb[0] != MBS_COMMAND_COMPLETE) {
2165 DEBUG2(qla_printk(KERN_INFO, ha,
2166 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2167 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2168 mb[0], mb[1], mb[2], mb[6], mb[7]));
2169 return (QLA_SUCCESS);
2172 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2173 if (qla2x00_rft_id(ha)) {
2174 /* EMPTY */
2175 DEBUG2(printk("scsi(%ld): Register FC-4 "
2176 "TYPE failed.\n", ha->host_no));
2178 if (qla2x00_rff_id(ha)) {
2179 /* EMPTY */
2180 DEBUG2(printk("scsi(%ld): Register FC-4 "
2181 "Features failed.\n", ha->host_no));
2183 if (qla2x00_rnn_id(ha)) {
2184 /* EMPTY */
2185 DEBUG2(printk("scsi(%ld): Register Node Name "
2186 "failed.\n", ha->host_no));
2187 } else if (qla2x00_rsnn_nn(ha)) {
2188 /* EMPTY */
2189 DEBUG2(printk("scsi(%ld): Register Symbolic "
2190 "Node Name failed.\n", ha->host_no));
2194 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2195 if (rval != QLA_SUCCESS)
2196 break;
2199 * Logout all previous fabric devices marked lost, except
2200 * tape devices.
2202 list_for_each_entry(fcport, &ha->fcports, list) {
2203 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2204 break;
2206 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2207 continue;
2209 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2210 qla2x00_mark_device_lost(ha, fcport,
2211 ql2xplogiabsentdevice, 0);
2212 if (fcport->loop_id != FC_NO_LOOP_ID &&
2213 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2214 fcport->port_type != FCT_INITIATOR &&
2215 fcport->port_type != FCT_BROADCAST) {
2216 ha->isp_ops.fabric_logout(ha,
2217 fcport->loop_id,
2218 fcport->d_id.b.domain,
2219 fcport->d_id.b.area,
2220 fcport->d_id.b.al_pa);
2221 fcport->loop_id = FC_NO_LOOP_ID;
2226 /* Starting free loop ID. */
2227 next_loopid = ha->min_external_loopid;
2230 * Scan through our port list and login entries that need to be
2231 * logged in.
2233 list_for_each_entry(fcport, &ha->fcports, list) {
2234 if (atomic_read(&ha->loop_down_timer) ||
2235 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2236 break;
2238 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2239 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2240 continue;
2242 if (fcport->loop_id == FC_NO_LOOP_ID) {
2243 fcport->loop_id = next_loopid;
2244 rval = qla2x00_find_new_loop_id(ha, fcport);
2245 if (rval != QLA_SUCCESS) {
2246 /* Ran out of IDs to use */
2247 break;
2250 /* Login and update database */
2251 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2254 /* Exit if out of loop IDs. */
2255 if (rval != QLA_SUCCESS) {
2256 break;
2260 * Login and add the new devices to our port list.
2262 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2263 if (atomic_read(&ha->loop_down_timer) ||
2264 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2265 break;
2267 /* Find a new loop ID to use. */
2268 fcport->loop_id = next_loopid;
2269 rval = qla2x00_find_new_loop_id(ha, fcport);
2270 if (rval != QLA_SUCCESS) {
2271 /* Ran out of IDs to use */
2272 break;
2275 /* Remove device from the new list and add it to DB */
2276 list_del(&fcport->list);
2277 list_add_tail(&fcport->list, &ha->fcports);
2279 /* Login and update database */
2280 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2282 } while (0);
2284 /* Free all new device structures not processed. */
2285 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2286 list_del(&fcport->list);
2287 kfree(fcport);
2290 if (rval) {
2291 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2292 "rval=%d\n", ha->host_no, rval));
2295 return (rval);
2300 * qla2x00_find_all_fabric_devs
2302 * Input:
2303 * ha = adapter block pointer.
2304 * dev = database device entry pointer.
2306 * Returns:
2307 * 0 = success.
2309 * Context:
2310 * Kernel context.
2312 static int
2313 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2315 int rval;
2316 uint16_t loop_id;
2317 fc_port_t *fcport, *new_fcport, *fcptemp;
2318 int found;
2320 sw_info_t *swl;
2321 int swl_idx;
2322 int first_dev, last_dev;
2323 port_id_t wrap, nxt_d_id;
2325 rval = QLA_SUCCESS;
2327 /* Try GID_PT to get device list, else GAN. */
2328 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2329 if (swl == NULL) {
2330 /*EMPTY*/
2331 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2332 "on GA_NXT\n", ha->host_no));
2333 } else {
2334 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2335 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2336 kfree(swl);
2337 swl = NULL;
2338 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2339 kfree(swl);
2340 swl = NULL;
2341 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2342 kfree(swl);
2343 swl = NULL;
2346 swl_idx = 0;
2348 /* Allocate temporary fcport for any new fcports discovered. */
2349 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2350 if (new_fcport == NULL) {
2351 kfree(swl);
2352 return (QLA_MEMORY_ALLOC_FAILED);
2354 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2356 /* Set start port ID scan at adapter ID. */
2357 first_dev = 1;
2358 last_dev = 0;
2360 /* Starting free loop ID. */
2361 loop_id = ha->min_external_loopid;
2362 for (; loop_id <= ha->last_loop_id; loop_id++) {
2363 if (qla2x00_is_reserved_id(ha, loop_id))
2364 continue;
2366 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2367 break;
2369 if (swl != NULL) {
2370 if (last_dev) {
2371 wrap.b24 = new_fcport->d_id.b24;
2372 } else {
2373 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2374 memcpy(new_fcport->node_name,
2375 swl[swl_idx].node_name, WWN_SIZE);
2376 memcpy(new_fcport->port_name,
2377 swl[swl_idx].port_name, WWN_SIZE);
2379 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2380 last_dev = 1;
2382 swl_idx++;
2384 } else {
2385 /* Send GA_NXT to the switch */
2386 rval = qla2x00_ga_nxt(ha, new_fcport);
2387 if (rval != QLA_SUCCESS) {
2388 qla_printk(KERN_WARNING, ha,
2389 "SNS scan failed -- assuming zero-entry "
2390 "result...\n");
2391 list_for_each_entry_safe(fcport, fcptemp,
2392 new_fcports, list) {
2393 list_del(&fcport->list);
2394 kfree(fcport);
2396 rval = QLA_SUCCESS;
2397 break;
2401 /* If wrap on switch device list, exit. */
2402 if (first_dev) {
2403 wrap.b24 = new_fcport->d_id.b24;
2404 first_dev = 0;
2405 } else if (new_fcport->d_id.b24 == wrap.b24) {
2406 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2407 ha->host_no, new_fcport->d_id.b.domain,
2408 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2409 break;
2412 /* Bypass if host adapter. */
2413 if (new_fcport->d_id.b24 == ha->d_id.b24)
2414 continue;
2416 /* Bypass if same domain and area of adapter. */
2417 if (((new_fcport->d_id.b24 & 0xffff00) ==
2418 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2419 ISP_CFG_FL)
2420 continue;
2422 /* Bypass reserved domain fields. */
2423 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2424 continue;
2426 /* Locate matching device in database. */
2427 found = 0;
2428 list_for_each_entry(fcport, &ha->fcports, list) {
2429 if (memcmp(new_fcport->port_name, fcport->port_name,
2430 WWN_SIZE))
2431 continue;
2433 found++;
2436 * If address the same and state FCS_ONLINE, nothing
2437 * changed.
2439 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2440 atomic_read(&fcport->state) == FCS_ONLINE) {
2441 break;
2445 * If device was not a fabric device before.
2447 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2448 fcport->d_id.b24 = new_fcport->d_id.b24;
2449 fcport->loop_id = FC_NO_LOOP_ID;
2450 fcport->flags |= (FCF_FABRIC_DEVICE |
2451 FCF_LOGIN_NEEDED);
2452 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2453 break;
2457 * Port ID changed or device was marked to be updated;
2458 * Log it out if still logged in and mark it for
2459 * relogin later.
2461 fcport->d_id.b24 = new_fcport->d_id.b24;
2462 fcport->flags |= FCF_LOGIN_NEEDED;
2463 if (fcport->loop_id != FC_NO_LOOP_ID &&
2464 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2465 fcport->port_type != FCT_INITIATOR &&
2466 fcport->port_type != FCT_BROADCAST) {
2467 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2468 fcport->d_id.b.domain, fcport->d_id.b.area,
2469 fcport->d_id.b.al_pa);
2470 fcport->loop_id = FC_NO_LOOP_ID;
2473 break;
2476 if (found)
2477 continue;
2479 /* If device was not in our fcports list, then add it. */
2480 list_add_tail(&new_fcport->list, new_fcports);
2482 /* Allocate a new replacement fcport. */
2483 nxt_d_id.b24 = new_fcport->d_id.b24;
2484 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2485 if (new_fcport == NULL) {
2486 kfree(swl);
2487 return (QLA_MEMORY_ALLOC_FAILED);
2489 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2490 new_fcport->d_id.b24 = nxt_d_id.b24;
2493 kfree(swl);
2494 kfree(new_fcport);
2496 if (!list_empty(new_fcports))
2497 ha->device_flags |= DFLG_FABRIC_DEVICES;
2499 return (rval);
2503 * qla2x00_find_new_loop_id
2504 * Scan through our port list and find a new usable loop ID.
2506 * Input:
2507 * ha: adapter state pointer.
2508 * dev: port structure pointer.
2510 * Returns:
2511 * qla2x00 local function return status code.
2513 * Context:
2514 * Kernel context.
2517 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2519 int rval;
2520 int found;
2521 fc_port_t *fcport;
2522 uint16_t first_loop_id;
2524 rval = QLA_SUCCESS;
2526 /* Save starting loop ID. */
2527 first_loop_id = dev->loop_id;
2529 for (;;) {
2530 /* Skip loop ID if already used by adapter. */
2531 if (dev->loop_id == ha->loop_id) {
2532 dev->loop_id++;
2535 /* Skip reserved loop IDs. */
2536 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2537 dev->loop_id++;
2540 /* Reset loop ID if passed the end. */
2541 if (dev->loop_id > ha->last_loop_id) {
2542 /* first loop ID. */
2543 dev->loop_id = ha->min_external_loopid;
2546 /* Check for loop ID being already in use. */
2547 found = 0;
2548 fcport = NULL;
2549 list_for_each_entry(fcport, &ha->fcports, list) {
2550 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2551 /* ID possibly in use */
2552 found++;
2553 break;
2557 /* If not in use then it is free to use. */
2558 if (!found) {
2559 break;
2562 /* ID in use. Try next value. */
2563 dev->loop_id++;
2565 /* If wrap around. No free ID to use. */
2566 if (dev->loop_id == first_loop_id) {
2567 dev->loop_id = FC_NO_LOOP_ID;
2568 rval = QLA_FUNCTION_FAILED;
2569 break;
2573 return (rval);
2577 * qla2x00_device_resync
2578 * Marks devices in the database that needs resynchronization.
2580 * Input:
2581 * ha = adapter block pointer.
2583 * Context:
2584 * Kernel context.
2586 static int
2587 qla2x00_device_resync(scsi_qla_host_t *ha)
2589 int rval;
2590 int rval2;
2591 uint32_t mask;
2592 fc_port_t *fcport;
2593 uint32_t rscn_entry;
2594 uint8_t rscn_out_iter;
2595 uint8_t format;
2596 port_id_t d_id;
2598 rval = QLA_RSCNS_HANDLED;
2600 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2601 ha->flags.rscn_queue_overflow) {
2603 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2604 format = MSB(MSW(rscn_entry));
2605 d_id.b.domain = LSB(MSW(rscn_entry));
2606 d_id.b.area = MSB(LSW(rscn_entry));
2607 d_id.b.al_pa = LSB(LSW(rscn_entry));
2609 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2610 "[%02x/%02x%02x%02x].\n",
2611 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2612 d_id.b.area, d_id.b.al_pa));
2614 ha->rscn_out_ptr++;
2615 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2616 ha->rscn_out_ptr = 0;
2618 /* Skip duplicate entries. */
2619 for (rscn_out_iter = ha->rscn_out_ptr;
2620 !ha->flags.rscn_queue_overflow &&
2621 rscn_out_iter != ha->rscn_in_ptr;
2622 rscn_out_iter = (rscn_out_iter ==
2623 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2625 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2626 break;
2628 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2629 "entry found at [%d].\n", ha->host_no,
2630 rscn_out_iter));
2632 ha->rscn_out_ptr = rscn_out_iter;
2635 /* Queue overflow, set switch default case. */
2636 if (ha->flags.rscn_queue_overflow) {
2637 DEBUG(printk("scsi(%ld): device_resync: rscn "
2638 "overflow.\n", ha->host_no));
2640 format = 3;
2641 ha->flags.rscn_queue_overflow = 0;
2644 switch (format) {
2645 case 0:
2646 if (ql2xprocessrscn &&
2647 !IS_QLA2100(ha) && !IS_QLA2200(ha) &&
2648 !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
2649 !IS_QLA24XX(ha) && !IS_QLA54XX(ha) &&
2650 ha->flags.init_done) {
2651 /* Handle port RSCN via asyncronous IOCBs */
2652 rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
2653 NULL, 0);
2654 if (rval2 == QLA_SUCCESS)
2655 continue;
2657 mask = 0xffffff;
2658 break;
2659 case 1:
2660 mask = 0xffff00;
2661 break;
2662 case 2:
2663 mask = 0xff0000;
2664 break;
2665 default:
2666 mask = 0x0;
2667 d_id.b24 = 0;
2668 ha->rscn_out_ptr = ha->rscn_in_ptr;
2669 break;
2672 rval = QLA_SUCCESS;
2674 /* Abort any outstanding IO descriptors. */
2675 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2676 qla2x00_cancel_io_descriptors(ha);
2678 list_for_each_entry(fcport, &ha->fcports, list) {
2679 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2680 (fcport->d_id.b24 & mask) != d_id.b24 ||
2681 fcport->port_type == FCT_BROADCAST)
2682 continue;
2684 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2685 if (format != 3 ||
2686 fcport->port_type != FCT_INITIATOR) {
2687 qla2x00_mark_device_lost(ha, fcport,
2688 0, 0);
2691 fcport->flags &= ~FCF_FARP_DONE;
2694 return (rval);
2698 * qla2x00_fabric_dev_login
2699 * Login fabric target device and update FC port database.
2701 * Input:
2702 * ha: adapter state pointer.
2703 * fcport: port structure list pointer.
2704 * next_loopid: contains value of a new loop ID that can be used
2705 * by the next login attempt.
2707 * Returns:
2708 * qla2x00 local function return status code.
2710 * Context:
2711 * Kernel context.
2713 static int
2714 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2715 uint16_t *next_loopid)
2717 int rval;
2718 int retry;
2719 uint8_t opts;
2721 rval = QLA_SUCCESS;
2722 retry = 0;
2724 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2725 if (rval == QLA_SUCCESS) {
2726 /* Send an ADISC to tape devices.*/
2727 opts = 0;
2728 if (fcport->flags & FCF_TAPE_PRESENT)
2729 opts |= BIT_1;
2730 rval = qla2x00_get_port_database(ha, fcport, opts);
2731 if (rval != QLA_SUCCESS) {
2732 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2733 fcport->d_id.b.domain, fcport->d_id.b.area,
2734 fcport->d_id.b.al_pa);
2735 qla2x00_mark_device_lost(ha, fcport, 1, 0);
2736 } else {
2737 qla2x00_update_fcport(ha, fcport);
2741 return (rval);
2745 * qla2x00_fabric_login
2746 * Issue fabric login command.
2748 * Input:
2749 * ha = adapter block pointer.
2750 * device = pointer to FC device type structure.
2752 * Returns:
2753 * 0 - Login successfully
2754 * 1 - Login failed
2755 * 2 - Initiator device
2756 * 3 - Fatal error
2759 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2760 uint16_t *next_loopid)
2762 int rval;
2763 int retry;
2764 uint16_t tmp_loopid;
2765 uint16_t mb[MAILBOX_REGISTER_COUNT];
2767 retry = 0;
2768 tmp_loopid = 0;
2770 for (;;) {
2771 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2772 "for port %02x%02x%02x.\n",
2773 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2774 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2776 /* Login fcport on switch. */
2777 ha->isp_ops.fabric_login(ha, fcport->loop_id,
2778 fcport->d_id.b.domain, fcport->d_id.b.area,
2779 fcport->d_id.b.al_pa, mb, BIT_0);
2780 if (mb[0] == MBS_PORT_ID_USED) {
2782 * Device has another loop ID. The firmware team
2783 * recommends the driver perform an implicit login with
2784 * the specified ID again. The ID we just used is save
2785 * here so we return with an ID that can be tried by
2786 * the next login.
2788 retry++;
2789 tmp_loopid = fcport->loop_id;
2790 fcport->loop_id = mb[1];
2792 DEBUG(printk("Fabric Login: port in use - next "
2793 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2794 fcport->loop_id, fcport->d_id.b.domain,
2795 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2797 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2799 * Login succeeded.
2801 if (retry) {
2802 /* A retry occurred before. */
2803 *next_loopid = tmp_loopid;
2804 } else {
2806 * No retry occurred before. Just increment the
2807 * ID value for next login.
2809 *next_loopid = (fcport->loop_id + 1);
2812 if (mb[1] & BIT_0) {
2813 fcport->port_type = FCT_INITIATOR;
2814 } else {
2815 fcport->port_type = FCT_TARGET;
2816 if (mb[1] & BIT_1) {
2817 fcport->flags |= FCF_TAPE_PRESENT;
2821 if (mb[10] & BIT_0)
2822 fcport->supported_classes |= FC_COS_CLASS2;
2823 if (mb[10] & BIT_1)
2824 fcport->supported_classes |= FC_COS_CLASS3;
2826 rval = QLA_SUCCESS;
2827 break;
2828 } else if (mb[0] == MBS_LOOP_ID_USED) {
2830 * Loop ID already used, try next loop ID.
2832 fcport->loop_id++;
2833 rval = qla2x00_find_new_loop_id(ha, fcport);
2834 if (rval != QLA_SUCCESS) {
2835 /* Ran out of loop IDs to use */
2836 break;
2838 } else if (mb[0] == MBS_COMMAND_ERROR) {
2840 * Firmware possibly timed out during login. If NO
2841 * retries are left to do then the device is declared
2842 * dead.
2844 *next_loopid = fcport->loop_id;
2845 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2846 fcport->d_id.b.domain, fcport->d_id.b.area,
2847 fcport->d_id.b.al_pa);
2848 qla2x00_mark_device_lost(ha, fcport, 1, 0);
2850 rval = 1;
2851 break;
2852 } else {
2854 * unrecoverable / not handled error
2856 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
2857 "loop_id=%x jiffies=%lx.\n",
2858 __func__, ha->host_no, mb[0],
2859 fcport->d_id.b.domain, fcport->d_id.b.area,
2860 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2862 *next_loopid = fcport->loop_id;
2863 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2864 fcport->d_id.b.domain, fcport->d_id.b.area,
2865 fcport->d_id.b.al_pa);
2866 fcport->loop_id = FC_NO_LOOP_ID;
2867 fcport->login_retry = 0;
2869 rval = 3;
2870 break;
2874 return (rval);
2878 * qla2x00_local_device_login
2879 * Issue local device login command.
2881 * Input:
2882 * ha = adapter block pointer.
2883 * loop_id = loop id of device to login to.
2885 * Returns (Where's the #define!!!!):
2886 * 0 - Login successfully
2887 * 1 - Login failed
2888 * 3 - Fatal error
2891 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
2893 int rval;
2894 uint16_t mb[MAILBOX_REGISTER_COUNT];
2896 memset(mb, 0, sizeof(mb));
2897 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
2898 if (rval == QLA_SUCCESS) {
2899 /* Interrogate mailbox registers for any errors */
2900 if (mb[0] == MBS_COMMAND_ERROR)
2901 rval = 1;
2902 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2903 /* device not in PCB table */
2904 rval = 3;
2907 return (rval);
2911 * qla2x00_loop_resync
2912 * Resync with fibre channel devices.
2914 * Input:
2915 * ha = adapter block pointer.
2917 * Returns:
2918 * 0 = success
2921 qla2x00_loop_resync(scsi_qla_host_t *ha)
2923 int rval;
2924 uint32_t wait_time;
2926 rval = QLA_SUCCESS;
2928 atomic_set(&ha->loop_state, LOOP_UPDATE);
2929 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2930 if (ha->flags.online) {
2931 if (!(rval = qla2x00_fw_ready(ha))) {
2932 /* Wait at most MAX_TARGET RSCNs for a stable link. */
2933 wait_time = 256;
2934 do {
2935 atomic_set(&ha->loop_state, LOOP_UPDATE);
2937 /* Issue a marker after FW becomes ready. */
2938 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2939 ha->marker_needed = 0;
2941 /* Remap devices on Loop. */
2942 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2944 qla2x00_configure_loop(ha);
2945 wait_time--;
2946 } while (!atomic_read(&ha->loop_down_timer) &&
2947 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2948 wait_time &&
2949 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2953 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2954 return (QLA_FUNCTION_FAILED);
2957 if (rval) {
2958 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
2961 return (rval);
2964 void
2965 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
2967 int rescan_done;
2968 fc_port_t *fcport;
2970 rescan_done = 0;
2971 list_for_each_entry(fcport, &ha->fcports, list) {
2972 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
2973 continue;
2975 qla2x00_update_fcport(ha, fcport);
2976 fcport->flags &= ~FCF_RESCAN_NEEDED;
2978 rescan_done = 1;
2980 qla2x00_probe_for_all_luns(ha);
2983 void
2984 qla2x00_update_fcports(scsi_qla_host_t *ha)
2986 fc_port_t *fcport;
2988 /* Go with deferred removal of rport references. */
2989 list_for_each_entry(fcport, &ha->fcports, list)
2990 if (fcport->drport)
2991 qla2x00_rport_del(fcport);
2995 * qla2x00_abort_isp
2996 * Resets ISP and aborts all outstanding commands.
2998 * Input:
2999 * ha = adapter block pointer.
3001 * Returns:
3002 * 0 = success
3005 qla2x00_abort_isp(scsi_qla_host_t *ha)
3007 unsigned long flags = 0;
3008 uint16_t cnt;
3009 srb_t *sp;
3010 uint8_t status = 0;
3012 if (ha->flags.online) {
3013 ha->flags.online = 0;
3014 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3016 qla_printk(KERN_INFO, ha,
3017 "Performing ISP error recovery - ha= %p.\n", ha);
3018 ha->isp_ops.reset_chip(ha);
3020 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3021 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3022 atomic_set(&ha->loop_state, LOOP_DOWN);
3023 qla2x00_mark_all_devices_lost(ha, 0);
3024 } else {
3025 if (!atomic_read(&ha->loop_down_timer))
3026 atomic_set(&ha->loop_down_timer,
3027 LOOP_DOWN_TIME);
3030 spin_lock_irqsave(&ha->hardware_lock, flags);
3031 /* Requeue all commands in outstanding command list. */
3032 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3033 sp = ha->outstanding_cmds[cnt];
3034 if (sp) {
3035 ha->outstanding_cmds[cnt] = NULL;
3036 sp->flags = 0;
3037 sp->cmd->result = DID_RESET << 16;
3038 sp->cmd->host_scribble = (unsigned char *)NULL;
3039 qla2x00_sp_compl(ha, sp);
3042 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3044 ha->isp_ops.nvram_config(ha);
3046 if (!qla2x00_restart_isp(ha)) {
3047 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3049 if (!atomic_read(&ha->loop_down_timer)) {
3051 * Issue marker command only when we are going
3052 * to start the I/O .
3054 ha->marker_needed = 1;
3057 ha->flags.online = 1;
3059 ha->isp_ops.enable_intrs(ha);
3061 ha->isp_abort_cnt = 0;
3062 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3063 } else { /* failed the ISP abort */
3064 ha->flags.online = 1;
3065 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3066 if (ha->isp_abort_cnt == 0) {
3067 qla_printk(KERN_WARNING, ha,
3068 "ISP error recovery failed - "
3069 "board disabled\n");
3071 * The next call disables the board
3072 * completely.
3074 ha->isp_ops.reset_adapter(ha);
3075 ha->flags.online = 0;
3076 clear_bit(ISP_ABORT_RETRY,
3077 &ha->dpc_flags);
3078 status = 0;
3079 } else { /* schedule another ISP abort */
3080 ha->isp_abort_cnt--;
3081 DEBUG(printk("qla%ld: ISP abort - "
3082 "retry remaining %d\n",
3083 ha->host_no, ha->isp_abort_cnt);)
3084 status = 1;
3086 } else {
3087 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3088 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3089 "- retrying (%d) more times\n",
3090 ha->host_no, ha->isp_abort_cnt);)
3091 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3092 status = 1;
3098 if (status) {
3099 qla_printk(KERN_INFO, ha,
3100 "qla2x00_abort_isp: **** FAILED ****\n");
3101 } else {
3102 DEBUG(printk(KERN_INFO
3103 "qla2x00_abort_isp(%ld): exiting.\n",
3104 ha->host_no);)
3107 return(status);
3111 * qla2x00_restart_isp
3112 * restarts the ISP after a reset
3114 * Input:
3115 * ha = adapter block pointer.
3117 * Returns:
3118 * 0 = success
3120 static int
3121 qla2x00_restart_isp(scsi_qla_host_t *ha)
3123 uint8_t status = 0;
3124 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3125 unsigned long flags = 0;
3126 uint32_t wait_time;
3128 /* If firmware needs to be loaded */
3129 if (qla2x00_isp_firmware(ha)) {
3130 ha->flags.online = 0;
3131 if (!(status = ha->isp_ops.chip_diag(ha))) {
3132 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3133 status = qla2x00_setup_chip(ha);
3134 goto done;
3137 spin_lock_irqsave(&ha->hardware_lock, flags);
3139 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3141 * Disable SRAM, Instruction RAM and GP RAM
3142 * parity.
3144 WRT_REG_WORD(&reg->hccr,
3145 (HCCR_ENABLE_PARITY + 0x0));
3146 RD_REG_WORD(&reg->hccr);
3149 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3151 status = qla2x00_setup_chip(ha);
3153 spin_lock_irqsave(&ha->hardware_lock, flags);
3155 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3156 /* Enable proper parity */
3157 if (IS_QLA2300(ha))
3158 /* SRAM parity */
3159 WRT_REG_WORD(&reg->hccr,
3160 (HCCR_ENABLE_PARITY + 0x1));
3161 else
3163 * SRAM, Instruction RAM and GP RAM
3164 * parity.
3166 WRT_REG_WORD(&reg->hccr,
3167 (HCCR_ENABLE_PARITY + 0x7));
3168 RD_REG_WORD(&reg->hccr);
3171 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3175 done:
3176 if (!status && !(status = qla2x00_init_rings(ha))) {
3177 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3178 if (!(status = qla2x00_fw_ready(ha))) {
3179 DEBUG(printk("%s(): Start configure loop, "
3180 "status = %d\n", __func__, status);)
3182 /* Issue a marker after FW becomes ready. */
3183 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3185 ha->flags.online = 1;
3186 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3187 wait_time = 256;
3188 do {
3189 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3190 qla2x00_configure_loop(ha);
3191 wait_time--;
3192 } while (!atomic_read(&ha->loop_down_timer) &&
3193 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3194 wait_time &&
3195 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3198 /* if no cable then assume it's good */
3199 if ((ha->device_flags & DFLG_NO_CABLE))
3200 status = 0;
3202 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3203 __func__,
3204 status);)
3206 return (status);
3210 * qla2x00_reset_adapter
3211 * Reset adapter.
3213 * Input:
3214 * ha = adapter block pointer.
3216 void
3217 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3219 unsigned long flags = 0;
3220 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3222 ha->flags.online = 0;
3223 ha->isp_ops.disable_intrs(ha);
3225 spin_lock_irqsave(&ha->hardware_lock, flags);
3226 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3227 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3228 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3229 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3230 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3233 void
3234 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3236 unsigned long flags = 0;
3237 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3239 ha->flags.online = 0;
3240 ha->isp_ops.disable_intrs(ha);
3242 spin_lock_irqsave(&ha->hardware_lock, flags);
3243 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3244 RD_REG_DWORD(&reg->hccr);
3245 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3246 RD_REG_DWORD(&reg->hccr);
3247 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3251 qla24xx_nvram_config(scsi_qla_host_t *ha)
3253 int rval;
3254 struct init_cb_24xx *icb;
3255 struct nvram_24xx *nv;
3256 uint32_t *dptr;
3257 uint8_t *dptr1, *dptr2;
3258 uint32_t chksum;
3259 uint16_t cnt;
3261 rval = QLA_SUCCESS;
3262 icb = (struct init_cb_24xx *)ha->init_cb;
3263 nv = (struct nvram_24xx *)ha->request_ring;
3265 /* Determine NVRAM starting address. */
3266 ha->nvram_size = sizeof(struct nvram_24xx);
3267 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3268 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3269 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3270 if (PCI_FUNC(ha->pdev->devfn)) {
3271 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3272 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3275 /* Get NVRAM data and calculate checksum. */
3276 dptr = (uint32_t *)nv;
3277 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3278 ha->nvram_size);
3279 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3280 chksum += le32_to_cpu(*dptr++);
3282 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3283 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3284 ha->nvram_size));
3286 /* Bad NVRAM data, set defaults parameters. */
3287 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3288 || nv->id[3] != ' ' ||
3289 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3290 /* Reset NVRAM data. */
3291 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3292 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3293 le16_to_cpu(nv->nvram_version));
3294 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3295 "invalid -- WWPN) defaults.\n");
3298 * Set default initialization control block.
3300 memset(nv, 0, ha->nvram_size);
3301 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3302 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3303 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3304 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3305 nv->exchange_count = __constant_cpu_to_le16(0);
3306 nv->hard_address = __constant_cpu_to_le16(124);
3307 nv->port_name[0] = 0x21;
3308 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3309 nv->port_name[2] = 0x00;
3310 nv->port_name[3] = 0xe0;
3311 nv->port_name[4] = 0x8b;
3312 nv->port_name[5] = 0x1c;
3313 nv->port_name[6] = 0x55;
3314 nv->port_name[7] = 0x86;
3315 nv->node_name[0] = 0x20;
3316 nv->node_name[1] = 0x00;
3317 nv->node_name[2] = 0x00;
3318 nv->node_name[3] = 0xe0;
3319 nv->node_name[4] = 0x8b;
3320 nv->node_name[5] = 0x1c;
3321 nv->node_name[6] = 0x55;
3322 nv->node_name[7] = 0x86;
3323 nv->login_retry_count = __constant_cpu_to_le16(8);
3324 nv->link_down_timeout = __constant_cpu_to_le16(200);
3325 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3326 nv->login_timeout = __constant_cpu_to_le16(0);
3327 nv->firmware_options_1 =
3328 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3329 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3330 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3331 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3332 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3333 nv->efi_parameters = __constant_cpu_to_le32(0);
3334 nv->reset_delay = 5;
3335 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3336 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3337 nv->link_down_timeout = __constant_cpu_to_le16(30);
3339 rval = 1;
3342 /* Reset Initialization control block */
3343 memset(icb, 0, sizeof(struct init_cb_24xx));
3345 /* Copy 1st segment. */
3346 dptr1 = (uint8_t *)icb;
3347 dptr2 = (uint8_t *)&nv->version;
3348 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3349 while (cnt--)
3350 *dptr1++ = *dptr2++;
3352 icb->login_retry_count = nv->login_retry_count;
3353 icb->link_down_timeout = nv->link_down_timeout;
3355 /* Copy 2nd segment. */
3356 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3357 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3358 cnt = (uint8_t *)&icb->reserved_3 -
3359 (uint8_t *)&icb->interrupt_delay_timer;
3360 while (cnt--)
3361 *dptr1++ = *dptr2++;
3364 * Setup driver NVRAM options.
3366 if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3367 char *st, *en;
3368 uint16_t index;
3370 strncpy(ha->model_number, nv->model_name,
3371 sizeof(nv->model_name));
3372 st = en = ha->model_number;
3373 en += sizeof(nv->model_name) - 1;
3374 while (en > st) {
3375 if (*en != 0x20 && *en != 0x00)
3376 break;
3377 *en-- = '\0';
3380 index = (ha->pdev->subsystem_device & 0xff);
3381 if (index < QLA_MODEL_NAMES)
3382 ha->model_desc = qla2x00_model_name[index * 2 + 1];
3383 } else
3384 strcpy(ha->model_number, "QLA2462");
3386 /* Prepare nodename */
3387 if ((icb->firmware_options_1 & BIT_14) == 0) {
3389 * Firmware will apply the following mask if the nodename was
3390 * not provided.
3392 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3393 icb->node_name[0] &= 0xF0;
3396 /* Set host adapter parameters. */
3397 ha->flags.disable_risc_code_load = 0;
3398 ha->flags.enable_lip_reset = 1;
3399 ha->flags.enable_lip_full_login = 1;
3400 ha->flags.enable_target_reset = 1;
3401 ha->flags.enable_led_scheme = 0;
3403 ha->operating_mode =
3404 (icb->firmware_options_2 & (BIT_6 | BIT_5 | BIT_4)) >> 4;
3406 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3407 sizeof(ha->fw_seriallink_options24));
3409 /* save HBA serial number */
3410 ha->serial0 = icb->port_name[5];
3411 ha->serial1 = icb->port_name[6];
3412 ha->serial2 = icb->port_name[7];
3413 ha->node_name = icb->node_name;
3414 ha->port_name = icb->port_name;
3416 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3418 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3420 /* Set minimum login_timeout to 4 seconds. */
3421 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3422 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3423 if (le16_to_cpu(nv->login_timeout) < 4)
3424 nv->login_timeout = __constant_cpu_to_le16(4);
3425 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3426 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3428 /* Set minimum RATOV to 200 tenths of a second. */
3429 ha->r_a_tov = 200;
3431 ha->loop_reset_delay = nv->reset_delay;
3433 /* Link Down Timeout = 0:
3435 * When Port Down timer expires we will start returning
3436 * I/O's to OS with "DID_NO_CONNECT".
3438 * Link Down Timeout != 0:
3440 * The driver waits for the link to come up after link down
3441 * before returning I/Os to OS with "DID_NO_CONNECT".
3443 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3444 ha->loop_down_abort_time =
3445 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3446 } else {
3447 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3448 ha->loop_down_abort_time =
3449 (LOOP_DOWN_TIME - ha->link_down_timeout);
3452 /* Need enough time to try and get the port back. */
3453 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3454 if (qlport_down_retry)
3455 ha->port_down_retry_count = qlport_down_retry;
3457 /* Set login_retry_count */
3458 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3459 if (ha->port_down_retry_count ==
3460 le16_to_cpu(nv->port_down_retry_count) &&
3461 ha->port_down_retry_count > 3)
3462 ha->login_retry_count = ha->port_down_retry_count;
3463 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3464 ha->login_retry_count = ha->port_down_retry_count;
3465 if (ql2xloginretrycount)
3466 ha->login_retry_count = ql2xloginretrycount;
3468 /* Enable ZIO. */
3469 if (!ha->flags.init_done) {
3470 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3471 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3472 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3473 le16_to_cpu(icb->interrupt_delay_timer): 2;
3475 icb->firmware_options_2 &= __constant_cpu_to_le32(
3476 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3477 ha->flags.process_response_queue = 0;
3478 if (ha->zio_mode != QLA_ZIO_DISABLED) {
3479 ha->zio_mode = QLA_ZIO_MODE_6;
3481 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3482 "(%d us).\n", ha->host_no, ha->zio_mode,
3483 ha->zio_timer * 100));
3484 qla_printk(KERN_INFO, ha,
3485 "ZIO mode %d enabled; timer delay (%d us).\n",
3486 ha->zio_mode, ha->zio_timer * 100);
3488 icb->firmware_options_2 |= cpu_to_le32(
3489 (uint32_t)ha->zio_mode);
3490 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3491 ha->flags.process_response_queue = 1;
3494 if (rval) {
3495 DEBUG2_3(printk(KERN_WARNING
3496 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3498 return (rval);
3501 #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
3504 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3506 int rval, num, i;
3507 uint32_t cnt;
3508 uint16_t *risc_code;
3509 uint32_t risc_addr, risc_size;
3510 uint16_t *req_ring;
3511 struct qla_fw_info *fw_iter;
3513 rval = QLA_SUCCESS;
3515 /* Load firmware sequences */
3516 fw_iter = ha->brd_info->fw_info;
3517 *srisc_addr = *ha->brd_info->fw_info->fwstart;
3518 while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
3519 risc_code = fw_iter->fwcode;
3520 risc_size = *fw_iter->fwlen;
3521 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL)
3522 risc_addr = *fw_iter->fwstart;
3523 else
3524 risc_addr = *fw_iter->lfwstart;
3526 num = 0;
3527 rval = 0;
3528 while (risc_size > 0 && !rval) {
3529 cnt = (uint16_t)(ha->fw_transfer_size >> 1);
3530 if (cnt > risc_size)
3531 cnt = risc_size;
3533 DEBUG7(printk("scsi(%ld): Loading risc segment@ "
3534 "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
3535 ha->host_no, risc_code, cnt, risc_addr));
3537 req_ring = (uint16_t *)ha->request_ring;
3538 for (i = 0; i < cnt; i++)
3539 req_ring[i] = cpu_to_le16(risc_code[i]);
3541 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3542 cnt);
3543 if (rval) {
3544 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
3545 "load segment %d of firmware\n",
3546 ha->host_no, num));
3547 qla_printk(KERN_WARNING, ha,
3548 "[ERROR] Failed to load segment %d of "
3549 "firmware\n", num);
3551 qla2x00_dump_regs(ha);
3552 break;
3555 risc_code += cnt;
3556 risc_addr += cnt;
3557 risc_size -= cnt;
3558 num++;
3561 /* Next firmware sequence */
3562 fw_iter++;
3564 return rval;
3568 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3570 int rval, num, i;
3571 uint32_t cnt;
3572 uint32_t *risc_code;
3573 uint32_t risc_addr, risc_size;
3574 uint32_t *req_ring;
3575 struct qla_fw_info *fw_iter;
3577 rval = QLA_SUCCESS;
3579 /* Load firmware sequences */
3580 fw_iter = ha->brd_info->fw_info;
3581 *srisc_addr = *((uint32_t *)fw_iter->lfwstart);
3582 while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
3583 risc_code = (uint32_t *)fw_iter->fwcode;
3584 risc_size = *((uint32_t *)fw_iter->fwlen);
3585 risc_addr = *((uint32_t *)fw_iter->lfwstart);
3587 num = 0;
3588 rval = 0;
3589 while (risc_size > 0 && !rval) {
3590 cnt = (uint32_t)(ha->fw_transfer_size >> 2);
3591 if (cnt > risc_size)
3592 cnt = risc_size;
3594 DEBUG7(printk("scsi(%ld): Loading risc segment@ "
3595 "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
3596 ha->host_no, risc_code, cnt, risc_addr));
3598 req_ring = (uint32_t *)ha->request_ring;
3599 for (i = 0; i < cnt; i++)
3600 req_ring[i] = cpu_to_le32(risc_code[i]);
3602 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3603 cnt);
3604 if (rval) {
3605 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
3606 "load segment %d of firmware\n",
3607 ha->host_no, num));
3608 qla_printk(KERN_WARNING, ha,
3609 "[ERROR] Failed to load segment %d of "
3610 "firmware\n", num);
3612 qla2x00_dump_regs(ha);
3613 break;
3616 risc_code += cnt;
3617 risc_addr += cnt;
3618 risc_size -= cnt;
3619 num++;
3622 /* Next firmware sequence */
3623 fw_iter++;
3625 return rval;
3629 qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3631 int rval;
3632 int segments, fragment;
3633 uint32_t faddr;
3634 uint32_t *dcode, dlen;
3635 uint32_t risc_addr;
3636 uint32_t risc_size;
3637 uint32_t i;
3639 rval = QLA_SUCCESS;
3641 segments = FA_RISC_CODE_SEGMENTS;
3642 faddr = FA_RISC_CODE_ADDR;
3643 dcode = (uint32_t *)ha->request_ring;
3644 *srisc_addr = 0;
3646 /* Validate firmware image by checking version. */
3647 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3648 for (i = 0; i < 4; i++)
3649 dcode[i] = be32_to_cpu(dcode[i]);
3650 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3651 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3652 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3653 dcode[3] == 0)) {
3654 qla_printk(KERN_WARNING, ha,
3655 "Unable to verify integrity of flash firmware image!\n");
3656 qla_printk(KERN_WARNING, ha,
3657 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3658 dcode[1], dcode[2], dcode[3]);
3660 return QLA_FUNCTION_FAILED;
3663 while (segments && rval == QLA_SUCCESS) {
3664 /* Read segment's load information. */
3665 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3667 risc_addr = be32_to_cpu(dcode[2]);
3668 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3669 risc_size = be32_to_cpu(dcode[3]);
3671 fragment = 0;
3672 while (risc_size > 0 && rval == QLA_SUCCESS) {
3673 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3674 if (dlen > risc_size)
3675 dlen = risc_size;
3677 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3678 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3679 ha->host_no, risc_addr, dlen, faddr));
3681 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3682 for (i = 0; i < dlen; i++)
3683 dcode[i] = swab32(dcode[i]);
3685 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3686 dlen);
3687 if (rval) {
3688 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3689 "segment %d of firmware\n", ha->host_no,
3690 fragment));
3691 qla_printk(KERN_WARNING, ha,
3692 "[ERROR] Failed to load segment %d of "
3693 "firmware\n", fragment);
3694 break;
3697 faddr += dlen;
3698 risc_addr += dlen;
3699 risc_size -= dlen;
3700 fragment++;
3703 /* Next segment. */
3704 segments--;
3707 return rval;
3710 #else /* !defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE) */
3713 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3715 int rval;
3716 int i, fragment;
3717 uint16_t *wcode, *fwcode;
3718 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3719 struct fw_blob *blob;
3721 /* Load firmware blob. */
3722 blob = qla2x00_request_firmware(ha);
3723 if (!blob) {
3724 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3725 return QLA_FUNCTION_FAILED;
3728 rval = QLA_SUCCESS;
3730 wcode = (uint16_t *)ha->request_ring;
3731 *srisc_addr = 0;
3732 fwcode = (uint16_t *)blob->fw->data;
3733 fwclen = 0;
3735 /* Validate firmware image by checking version. */
3736 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3737 qla_printk(KERN_WARNING, ha,
3738 "Unable to verify integrity of firmware image (%Zd)!\n",
3739 blob->fw->size);
3740 goto fail_fw_integrity;
3742 for (i = 0; i < 4; i++)
3743 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3744 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3745 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3746 wcode[2] == 0 && wcode[3] == 0)) {
3747 qla_printk(KERN_WARNING, ha,
3748 "Unable to verify integrity of firmware image!\n");
3749 qla_printk(KERN_WARNING, ha,
3750 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3751 wcode[1], wcode[2], wcode[3]);
3752 goto fail_fw_integrity;
3755 seg = blob->segs;
3756 while (*seg && rval == QLA_SUCCESS) {
3757 risc_addr = *seg;
3758 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3759 risc_size = be16_to_cpu(fwcode[3]);
3761 /* Validate firmware image size. */
3762 fwclen += risc_size * sizeof(uint16_t);
3763 if (blob->fw->size < fwclen) {
3764 qla_printk(KERN_WARNING, ha,
3765 "Unable to verify integrity of firmware image "
3766 "(%Zd)!\n", blob->fw->size);
3767 goto fail_fw_integrity;
3770 fragment = 0;
3771 while (risc_size > 0 && rval == QLA_SUCCESS) {
3772 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3773 if (wlen > risc_size)
3774 wlen = risc_size;
3776 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3777 "addr %x, number of words 0x%x.\n", ha->host_no,
3778 risc_addr, wlen));
3780 for (i = 0; i < wlen; i++)
3781 wcode[i] = swab16(fwcode[i]);
3783 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3784 wlen);
3785 if (rval) {
3786 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3787 "segment %d of firmware\n", ha->host_no,
3788 fragment));
3789 qla_printk(KERN_WARNING, ha,
3790 "[ERROR] Failed to load segment %d of "
3791 "firmware\n", fragment);
3792 break;
3795 fwcode += wlen;
3796 risc_addr += wlen;
3797 risc_size -= wlen;
3798 fragment++;
3801 /* Next segment. */
3802 seg++;
3804 return rval;
3806 fail_fw_integrity:
3807 return QLA_FUNCTION_FAILED;
3811 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3813 int rval;
3814 int segments, fragment;
3815 uint32_t *dcode, dlen;
3816 uint32_t risc_addr;
3817 uint32_t risc_size;
3818 uint32_t i;
3819 struct fw_blob *blob;
3820 uint32_t *fwcode, fwclen;
3822 /* Load firmware blob. */
3823 blob = qla2x00_request_firmware(ha);
3824 if (!blob) {
3825 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3826 return QLA_FUNCTION_FAILED;
3829 rval = QLA_SUCCESS;
3831 segments = FA_RISC_CODE_SEGMENTS;
3832 dcode = (uint32_t *)ha->request_ring;
3833 *srisc_addr = 0;
3834 fwcode = (uint32_t *)blob->fw->data;
3835 fwclen = 0;
3837 /* Validate firmware image by checking version. */
3838 if (blob->fw->size < 8 * sizeof(uint32_t)) {
3839 qla_printk(KERN_WARNING, ha,
3840 "Unable to verify integrity of firmware image (%Zd)!\n",
3841 blob->fw->size);
3842 goto fail_fw_integrity;
3844 for (i = 0; i < 4; i++)
3845 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3846 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3847 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3848 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3849 dcode[3] == 0)) {
3850 qla_printk(KERN_WARNING, ha,
3851 "Unable to verify integrity of firmware image!\n");
3852 qla_printk(KERN_WARNING, ha,
3853 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3854 dcode[1], dcode[2], dcode[3]);
3855 goto fail_fw_integrity;
3858 while (segments && rval == QLA_SUCCESS) {
3859 risc_addr = be32_to_cpu(fwcode[2]);
3860 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3861 risc_size = be32_to_cpu(fwcode[3]);
3863 /* Validate firmware image size. */
3864 fwclen += risc_size * sizeof(uint32_t);
3865 if (blob->fw->size < fwclen) {
3866 qla_printk(KERN_WARNING, ha,
3867 "Unable to verify integrity of firmware image "
3868 "(%Zd)!\n", blob->fw->size);
3870 goto fail_fw_integrity;
3873 fragment = 0;
3874 while (risc_size > 0 && rval == QLA_SUCCESS) {
3875 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3876 if (dlen > risc_size)
3877 dlen = risc_size;
3879 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3880 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3881 risc_addr, dlen));
3883 for (i = 0; i < dlen; i++)
3884 dcode[i] = swab32(fwcode[i]);
3886 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3887 dlen);
3888 if (rval) {
3889 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3890 "segment %d of firmware\n", ha->host_no,
3891 fragment));
3892 qla_printk(KERN_WARNING, ha,
3893 "[ERROR] Failed to load segment %d of "
3894 "firmware\n", fragment);
3895 break;
3898 fwcode += dlen;
3899 risc_addr += dlen;
3900 risc_size -= dlen;
3901 fragment++;
3904 /* Next segment. */
3905 segments--;
3907 return rval;
3909 fail_fw_integrity:
3910 return QLA_FUNCTION_FAILED;
3912 #endif