2 * QLOGIC LINUX SOFTWARE
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2004 QLogic Corporation
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
21 #include <linux/delay.h>
22 #include <scsi/scsi_transport_fc.h>
24 #include "qla_devtbl.h"
26 /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
27 #ifndef EXT_IS_LUN_BIT_SET
28 #define EXT_IS_LUN_BIT_SET(P,L) \
29 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
30 #define EXT_SET_LUN_BIT(P,L) \
31 ((P)->mask[L/8] |= (0x80 >> (L%8)))
35 * QLogic ISP2x00 Hardware Support Function Prototypes.
37 static int qla2x00_pci_config(scsi_qla_host_t *);
38 static int qla2x00_isp_firmware(scsi_qla_host_t *);
39 static void qla2x00_reset_chip(scsi_qla_host_t *);
40 static int qla2x00_chip_diag(scsi_qla_host_t *);
41 static void qla2x00_resize_request_q(scsi_qla_host_t *);
42 static int qla2x00_setup_chip(scsi_qla_host_t *);
43 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
44 static int qla2x00_init_rings(scsi_qla_host_t *);
45 static int qla2x00_fw_ready(scsi_qla_host_t *);
46 static int qla2x00_configure_hba(scsi_qla_host_t *);
47 static int qla2x00_nvram_config(scsi_qla_host_t *);
48 static int qla2x00_configure_loop(scsi_qla_host_t *);
49 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
50 static void qla2x00_update_fcport(scsi_qla_host_t *, fc_port_t *);
51 static int qla2x00_configure_fabric(scsi_qla_host_t *);
52 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
53 static int qla2x00_device_resync(scsi_qla_host_t *);
54 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
57 static int qla2x00_restart_isp(scsi_qla_host_t *);
58 static void qla2x00_reset_adapter(scsi_qla_host_t *);
60 /****************************************************************************/
61 /* QLogic ISP2x00 Hardware Support Functions. */
62 /****************************************************************************/
65 * qla2x00_initialize_adapter
69 * ha = adapter block pointer.
75 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
78 uint8_t restart_risc = 0;
82 /* Clear adapter flags. */
84 ha->flags.reset_active = 0;
85 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
86 atomic_set(&ha->loop_state, LOOP_DOWN);
89 ha->flags.management_server_logged_in = 0;
90 ha->marker_needed = 0;
92 ha->isp_abort_cnt = 0;
93 ha->beacon_blink_led = 0;
95 rval = qla2x00_pci_config(ha);
97 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
102 qla2x00_reset_chip(ha);
104 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
105 qla2x00_nvram_config(ha);
107 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
111 * Try to configure the loop.
116 /* If firmware needs to be loaded */
117 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
118 if ((rval = qla2x00_chip_diag(ha)) == QLA_SUCCESS) {
119 rval = qla2x00_setup_chip(ha);
123 if (rval == QLA_SUCCESS &&
124 (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
125 check_fw_ready_again:
127 * Wait for a successful LIP up to a maximum
128 * of (in seconds): RISC login timeout value,
129 * RISC retry count value, and port down retry
130 * value OR a minimum of 4 seconds OR If no
131 * cable, only 5 seconds.
133 rval = qla2x00_fw_ready(ha);
134 if (rval == QLA_SUCCESS) {
135 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
138 * Wait at most MAX_TARGET RSCNs for a stable
143 clear_bit(LOOP_RESYNC_NEEDED,
145 rval = qla2x00_configure_loop(ha);
147 if (test_and_clear_bit(ISP_ABORT_NEEDED,
154 * If loop state change while we were
155 * discoverying devices then wait for
159 if (atomic_read(&ha->loop_state) ==
160 LOOP_DOWN && retry--) {
161 goto check_fw_ready_again;
164 } while (!atomic_read(&ha->loop_down_timer) &&
167 (test_bit(LOOP_RESYNC_NEEDED,
171 rval = QLA_FUNCTION_FAILED;
172 } else if (ha->device_flags & DFLG_NO_CABLE)
173 /* If no cable, then all is good. */
176 } while (restart_risc && retry--);
178 if (rval == QLA_SUCCESS) {
179 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
180 ha->marker_needed = 1;
181 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
182 ha->marker_needed = 0;
184 ha->flags.online = 1;
186 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
193 * qla2x00_pci_config() - Setup device PCI configuration registers.
196 * Returns 0 on success.
199 qla2x00_pci_config(scsi_qla_host_t *ha)
202 unsigned long flags = 0;
205 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
208 * Turn on PCI master; for system BIOSes that don't turn it on by
211 pci_set_master(ha->pdev);
213 if (pci_set_mwi(ha->pdev))
214 mwi = PCI_COMMAND_INVALIDATE;
215 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
218 return (QLA_FUNCTION_FAILED);
221 * We want to respect framework's setting of PCI configuration space
222 * command register and also want to make sure that all bits of
223 * interest to us are properly set in command register.
225 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
226 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
228 /* Get PCI bus information. */
229 spin_lock_irqsave(&ha->hardware_lock, flags);
230 ha->pci_attr = RD_REG_WORD(&ha->iobase->ctrl_status);
231 spin_unlock_irqrestore(&ha->hardware_lock, flags);
233 if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
234 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
236 /* PCI Specification Revision 2.3 changes */
237 if (IS_QLA2322(ha) || IS_QLA6322(ha))
238 /* Command Register - Reset Interrupt Disable. */
239 w &= ~PCI_COMMAND_INTX_DISABLE;
242 * If this is a 2300 card and not 2312, reset the
243 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
244 * the 2310 also reports itself as a 2300 so we need to get the
245 * fb revision level -- a 6 indicates it really is a 2300 and
248 if (IS_QLA2300(ha)) {
249 spin_lock_irqsave(&ha->hardware_lock, flags);
252 WRT_REG_WORD(&ha->iobase->hccr, HCCR_PAUSE_RISC);
253 for (cnt = 0; cnt < 30000; cnt++) {
254 if ((RD_REG_WORD(&ha->iobase->hccr) &
255 HCCR_RISC_PAUSE) != 0)
261 /* Select FPM registers. */
262 WRT_REG_WORD(&ha->iobase->ctrl_status, 0x20);
263 RD_REG_WORD(&ha->iobase->ctrl_status);
265 /* Get the fb rev level */
266 ha->fb_rev = RD_FB_CMD_REG(ha, ha->iobase);
268 if (ha->fb_rev == FPM_2300)
269 w &= ~PCI_COMMAND_INVALIDATE;
271 /* Deselect FPM registers. */
272 WRT_REG_WORD(&ha->iobase->ctrl_status, 0x0);
273 RD_REG_WORD(&ha->iobase->ctrl_status);
275 /* Release RISC module. */
276 WRT_REG_WORD(&ha->iobase->hccr, HCCR_RELEASE_RISC);
277 for (cnt = 0; cnt < 30000; cnt++) {
278 if ((RD_REG_WORD(&ha->iobase->hccr) &
279 HCCR_RISC_PAUSE) == 0)
285 spin_unlock_irqrestore(&ha->hardware_lock, flags);
289 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
291 /* Reset expansion ROM address decode enable */
292 pci_read_config_word(ha->pdev, PCI_ROM_ADDRESS, &w);
293 w &= ~PCI_ROM_ADDRESS_ENABLE;
294 pci_write_config_word(ha->pdev, PCI_ROM_ADDRESS, w);
296 return (QLA_SUCCESS);
300 * qla2x00_isp_firmware() - Choose firmware image.
303 * Returns 0 on success.
306 qla2x00_isp_firmware(scsi_qla_host_t *ha)
310 /* Assume loading risc code */
311 rval = QLA_FUNCTION_FAILED;
313 if (ha->flags.disable_risc_code_load) {
314 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
316 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
318 /* Verify checksum of loaded RISC code. */
319 rval = qla2x00_verify_checksum(ha);
323 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
331 * qla2x00_reset_chip() - Reset ISP chip.
334 * Returns 0 on success.
337 qla2x00_reset_chip(scsi_qla_host_t *ha)
339 unsigned long flags = 0;
340 device_reg_t __iomem *reg = ha->iobase;
342 unsigned long mbx_flags = 0;
345 /* Disable ISP interrupts. */
346 qla2x00_disable_intrs(ha);
348 spin_lock_irqsave(&ha->hardware_lock, flags);
350 /* Turn off master enable */
352 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
353 cmd &= ~PCI_COMMAND_MASTER;
354 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
356 if (!IS_QLA2100(ha)) {
358 WRT_REG_WORD(®->hccr, HCCR_PAUSE_RISC);
359 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
360 for (cnt = 0; cnt < 30000; cnt++) {
361 if ((RD_REG_WORD(®->hccr) &
362 HCCR_RISC_PAUSE) != 0)
367 RD_REG_WORD(®->hccr); /* PCI Posting. */
371 /* Select FPM registers. */
372 WRT_REG_WORD(®->ctrl_status, 0x20);
373 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
375 /* FPM Soft Reset. */
376 WRT_REG_WORD(®->fpm_diag_config, 0x100);
377 RD_REG_WORD(®->fpm_diag_config); /* PCI Posting. */
379 /* Toggle Fpm Reset. */
380 if (!IS_QLA2200(ha)) {
381 WRT_REG_WORD(®->fpm_diag_config, 0x0);
382 RD_REG_WORD(®->fpm_diag_config); /* PCI Posting. */
385 /* Select frame buffer registers. */
386 WRT_REG_WORD(®->ctrl_status, 0x10);
387 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
389 /* Reset frame buffer FIFOs. */
390 if (IS_QLA2200(ha)) {
391 WRT_FB_CMD_REG(ha, reg, 0xa000);
392 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
394 WRT_FB_CMD_REG(ha, reg, 0x00fc);
396 /* Read back fb_cmd until zero or 3 seconds max */
397 for (cnt = 0; cnt < 3000; cnt++) {
398 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
404 /* Select RISC module registers. */
405 WRT_REG_WORD(®->ctrl_status, 0);
406 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
408 /* Reset RISC processor. */
409 WRT_REG_WORD(®->hccr, HCCR_RESET_RISC);
410 RD_REG_WORD(®->hccr); /* PCI Posting. */
412 /* Release RISC processor. */
413 WRT_REG_WORD(®->hccr, HCCR_RELEASE_RISC);
414 RD_REG_WORD(®->hccr); /* PCI Posting. */
417 WRT_REG_WORD(®->hccr, HCCR_CLR_RISC_INT);
418 WRT_REG_WORD(®->hccr, HCCR_CLR_HOST_INT);
420 /* Reset ISP chip. */
421 WRT_REG_WORD(®->ctrl_status, CSR_ISP_SOFT_RESET);
423 /* Wait for RISC to recover from reset. */
424 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
426 * It is necessary to for a delay here since the card doesn't
427 * respond to PCI reads during a reset. On some architectures
428 * this will result in an MCA.
431 for (cnt = 30000; cnt; cnt--) {
432 if ((RD_REG_WORD(®->ctrl_status) &
433 CSR_ISP_SOFT_RESET) == 0)
440 /* Reset RISC processor. */
441 WRT_REG_WORD(®->hccr, HCCR_RESET_RISC);
443 WRT_REG_WORD(®->semaphore, 0);
445 /* Release RISC processor. */
446 WRT_REG_WORD(®->hccr, HCCR_RELEASE_RISC);
447 RD_REG_WORD(®->hccr); /* PCI Posting. */
449 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
450 for (cnt = 0; cnt < 30000; cnt++) {
451 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
452 spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
454 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY) {
455 if (!(test_bit(ABORT_ISP_ACTIVE,
457 spin_unlock_irqrestore(
458 &ha->mbx_reg_lock, mbx_flags);
462 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
463 spin_unlock_irqrestore(&ha->mbx_reg_lock,
471 /* Turn on master enable */
472 cmd |= PCI_COMMAND_MASTER;
473 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
475 /* Disable RISC pause on FPM parity error. */
476 if (!IS_QLA2100(ha)) {
477 WRT_REG_WORD(®->hccr, HCCR_DISABLE_PARITY_PAUSE);
478 RD_REG_WORD(®->hccr); /* PCI Posting. */
481 spin_unlock_irqrestore(&ha->hardware_lock, flags);
485 * qla2x00_chip_diag() - Test chip for proper operation.
488 * Returns 0 on success.
491 qla2x00_chip_diag(scsi_qla_host_t *ha)
494 device_reg_t __iomem *reg = ha->iobase;
495 unsigned long flags = 0;
500 /* Assume a failed state */
501 rval = QLA_FUNCTION_FAILED;
503 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
504 ha->host_no, (u_long)®->flash_address));
506 spin_lock_irqsave(&ha->hardware_lock, flags);
508 /* Reset ISP chip. */
509 WRT_REG_WORD(®->ctrl_status, CSR_ISP_SOFT_RESET);
512 * We need to have a delay here since the card will not respond while
513 * in reset causing an MCA on some architectures.
516 data = qla2x00_debounce_register(®->ctrl_status);
517 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
519 data = RD_REG_WORD(®->ctrl_status);
524 goto chip_diag_failed;
526 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
529 /* Reset RISC processor. */
530 WRT_REG_WORD(®->hccr, HCCR_RESET_RISC);
531 WRT_REG_WORD(®->hccr, HCCR_RELEASE_RISC);
533 /* Workaround for QLA2312 PCI parity error */
534 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
535 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
536 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
538 data = RD_MAILBOX_REG(ha, reg, 0);
545 goto chip_diag_failed;
547 /* Check product ID of chip */
548 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
550 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
551 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
552 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
553 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
554 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
555 mb[3] != PROD_ID_3) {
556 qla_printk(KERN_WARNING, ha,
557 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
559 goto chip_diag_failed;
561 ha->product_id[0] = mb[1];
562 ha->product_id[1] = mb[2];
563 ha->product_id[2] = mb[3];
564 ha->product_id[3] = mb[4];
566 /* Adjust fw RISC transfer size */
567 if (ha->request_q_length > 1024)
568 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
570 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
571 ha->request_q_length;
573 if (IS_QLA2200(ha) &&
574 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
575 /* Limit firmware transfer size with a 2200A */
576 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
579 ha->fw_transfer_size = 128;
582 /* Wrap Incoming Mailboxes Test. */
583 spin_unlock_irqrestore(&ha->hardware_lock, flags);
585 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
586 rval = qla2x00_mbx_reg_test(ha);
588 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
590 qla_printk(KERN_WARNING, ha,
591 "Failed mailbox send register test\n");
594 /* Flag a successful rval */
597 spin_lock_irqsave(&ha->hardware_lock, flags);
601 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
602 "****\n", ha->host_no));
604 spin_unlock_irqrestore(&ha->hardware_lock, flags);
610 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
613 * Returns 0 on success.
616 qla2x00_resize_request_q(scsi_qla_host_t *ha)
619 uint16_t fw_iocb_cnt = 0;
620 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
621 dma_addr_t request_dma;
622 request_t *request_ring;
624 /* Valid only on recent ISPs. */
625 if (IS_QLA2100(ha) || IS_QLA2200(ha))
628 /* Retrieve IOCB counts available to the firmware. */
629 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
632 /* No point in continuing if current settings are sufficient. */
633 if (fw_iocb_cnt < 1024)
635 if (ha->request_q_length >= request_q_length)
638 /* Attempt to claim larger area for request queue. */
639 request_ring = dma_alloc_coherent(&ha->pdev->dev,
640 (request_q_length + 1) * sizeof(request_t), &request_dma,
642 if (request_ring == NULL)
645 /* Resize successful, report extensions. */
646 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
647 (ha->fw_memory_size + 1) / 1024);
648 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
649 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
651 /* Clear old allocations. */
652 dma_free_coherent(&ha->pdev->dev,
653 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
656 /* Begin using larger queue. */
657 ha->request_q_length = request_q_length;
658 ha->request_ring = request_ring;
659 ha->request_dma = request_dma;
663 * qla2x00_setup_chip() - Load and start RISC firmware.
666 * Returns 0 on success.
669 qla2x00_setup_chip(scsi_qla_host_t *ha)
674 unsigned long risc_address;
675 unsigned long risc_code_size;
679 struct qla_fw_info *fw_iter;
683 /* Load firmware sequences */
684 fw_iter = ha->brd_info->fw_info;
685 while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
686 risc_code = fw_iter->fwcode;
687 risc_code_size = *fw_iter->fwlen;
689 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL) {
690 risc_address = *fw_iter->fwstart;
692 /* Extended address */
693 risc_address = *fw_iter->lfwstart;
698 while (risc_code_size > 0 && !rval) {
699 cnt = (uint16_t)(ha->fw_transfer_size >> 1);
700 if (cnt > risc_code_size)
701 cnt = risc_code_size;
703 DEBUG7(printk("scsi(%ld): Loading risc segment@ "
704 "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
705 ha->host_no, risc_code, cnt, risc_address));
707 req_ring = (uint16_t *)ha->request_ring;
708 for (i = 0; i < cnt; i++)
709 req_ring[i] = cpu_to_le16(risc_code[i]);
711 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL) {
712 rval = qla2x00_load_ram(ha,
713 ha->request_dma, risc_address, cnt);
715 rval = qla2x00_load_ram_ext(ha,
716 ha->request_dma, risc_address, cnt);
719 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
720 "load segment %d of firmware\n",
722 qla_printk(KERN_WARNING, ha,
723 "[ERROR] Failed to load "
724 "segment %d of firmware\n", num);
726 qla2x00_dump_regs(ha);
732 risc_code_size -= cnt;
736 /* Next firmware sequence */
740 /* Verify checksum of loaded RISC code. */
742 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
743 "code.\n", ha->host_no));
745 rval = qla2x00_verify_checksum(ha);
746 if (rval == QLA_SUCCESS) {
747 /* Start firmware execution. */
748 DEBUG(printk("scsi(%ld): Checksum OK, start "
749 "firmware.\n", ha->host_no));
751 rval = qla2x00_execute_fw(ha);
752 /* Retrieve firmware information. */
753 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
754 qla2x00_get_fw_version(ha,
755 &ha->fw_major_version,
756 &ha->fw_minor_version,
757 &ha->fw_subminor_version,
758 &ha->fw_attributes, &ha->fw_memory_size);
759 qla2x00_resize_request_q(ha);
762 DEBUG2(printk(KERN_INFO
763 "scsi(%ld): ISP Firmware failed checksum.\n",
769 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
777 * qla2x00_init_response_q_entries() - Initializes response queue entries.
780 * Beginning of request ring has initialization control block already built
781 * by nvram config routine.
783 * Returns 0 on success.
786 qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
791 pkt = ha->response_ring_ptr;
792 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
793 pkt->signature = RESPONSE_PROCESSED;
800 * qla2x00_update_fw_options() - Read and process firmware options.
803 * Returns 0 on success.
806 qla2x00_update_fw_options(scsi_qla_host_t *ha)
808 uint16_t swing, emphasis, tx_sens, rx_sens;
810 memset(ha->fw_options, 0, sizeof(ha->fw_options));
811 qla2x00_get_fw_options(ha, ha->fw_options);
813 if (IS_QLA2100(ha) || IS_QLA2200(ha))
816 /* Serial Link options. */
817 DEBUG3(printk("scsi(%ld): Serial link options:\n",
819 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
820 sizeof(ha->fw_seriallink_options)));
822 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
823 if (ha->fw_seriallink_options[3] & BIT_2) {
824 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
827 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
828 emphasis = (ha->fw_seriallink_options[2] &
829 (BIT_4 | BIT_3)) >> 3;
830 tx_sens = ha->fw_seriallink_options[0] &
831 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
832 rx_sens = (ha->fw_seriallink_options[0] &
833 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
834 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
835 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
838 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
839 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
840 ha->fw_options[10] |= BIT_5 |
841 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
842 (tx_sens & (BIT_1 | BIT_0));
845 swing = (ha->fw_seriallink_options[2] &
846 (BIT_7 | BIT_6 | BIT_5)) >> 5;
847 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
848 tx_sens = ha->fw_seriallink_options[1] &
849 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
850 rx_sens = (ha->fw_seriallink_options[1] &
851 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
852 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
853 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
856 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
857 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
858 ha->fw_options[11] |= BIT_5 |
859 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
860 (tx_sens & (BIT_1 | BIT_0));
864 /* Return command IOCBs without waiting for an ABTS to complete. */
865 ha->fw_options[3] |= BIT_13;
868 if (ha->flags.enable_led_scheme)
869 ha->fw_options[2] |= BIT_12;
871 /* Update firmware options. */
872 qla2x00_set_fw_options(ha, ha->fw_options);
876 * qla2x00_init_rings() - Initializes firmware.
879 * Beginning of request ring has initialization control block already built
880 * by nvram config routine.
882 * Returns 0 on success.
885 qla2x00_init_rings(scsi_qla_host_t *ha)
888 unsigned long flags = 0;
890 device_reg_t __iomem *reg = ha->iobase;
892 spin_lock_irqsave(&ha->hardware_lock, flags);
894 /* Clear outstanding commands array. */
895 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
896 ha->outstanding_cmds[cnt] = NULL;
898 ha->current_outstanding_cmd = 0;
900 /* Clear RSCN queue. */
902 ha->rscn_out_ptr = 0;
904 /* Initialize firmware. */
905 ha->request_ring_ptr = ha->request_ring;
906 ha->req_ring_index = 0;
907 ha->req_q_cnt = ha->request_q_length;
908 ha->response_ring_ptr = ha->response_ring;
909 ha->rsp_ring_index = 0;
911 /* Setup ring parameters in initialization control block. */
912 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
913 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
914 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
915 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
916 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
917 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
918 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
919 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
921 /* Initialize response queue entries */
922 qla2x00_init_response_q_entries(ha);
924 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
925 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
926 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
927 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
928 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
930 spin_unlock_irqrestore(&ha->hardware_lock, flags);
932 /* Update any ISP specific firmware options before initialization. */
933 qla2x00_update_fw_options(ha);
935 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
936 rval = qla2x00_init_firmware(ha, sizeof(init_cb_t));
938 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
941 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
949 * qla2x00_fw_ready() - Waits for firmware ready.
952 * Returns 0 on success.
955 qla2x00_fw_ready(scsi_qla_host_t *ha)
958 unsigned long wtime, mtime;
959 uint16_t min_wait; /* Minimum wait time if loop is down */
960 uint16_t wait_time; /* Wait time if loop is coming ready */
965 /* 20 seconds for loop down. */
969 * Firmware should take at most one RATOV to login, plus 5 seconds for
970 * our own processing.
972 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
973 wait_time = min_wait;
976 /* Min wait time if loop down */
977 mtime = jiffies + (min_wait * HZ);
979 /* wait time before firmware ready */
980 wtime = jiffies + (wait_time * HZ);
982 /* Wait for ISP to finish LIP */
983 if (!ha->flags.init_done)
984 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
986 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
990 rval = qla2x00_get_firmware_state(ha, &fw_state);
991 if (rval == QLA_SUCCESS) {
992 if (fw_state < FSTATE_LOSS_OF_SYNC) {
993 ha->device_flags &= ~DFLG_NO_CABLE;
995 if (fw_state == FSTATE_READY) {
996 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
999 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1000 &ha->login_timeout, &ha->r_a_tov);
1006 rval = QLA_FUNCTION_FAILED;
1008 if (atomic_read(&ha->loop_down_timer) &&
1009 (fw_state >= FSTATE_LOSS_OF_SYNC ||
1010 fw_state == FSTATE_WAIT_AL_PA)) {
1011 /* Loop down. Timeout on min_wait for states
1012 * other than Wait for Login.
1014 if (time_after_eq(jiffies, mtime)) {
1015 qla_printk(KERN_INFO, ha,
1016 "Cable is unplugged...\n");
1018 ha->device_flags |= DFLG_NO_CABLE;
1023 /* Mailbox cmd failed. Timeout on min_wait. */
1024 if (time_after_eq(jiffies, mtime))
1028 if (time_after_eq(jiffies, wtime))
1031 /* Delay for a while */
1034 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1035 ha->host_no, fw_state, jiffies));
1038 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1039 ha->host_no, fw_state, jiffies));
1042 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1050 * qla2x00_configure_hba
1051 * Setup adapter context.
1054 * ha = adapter state pointer.
1063 qla2x00_configure_hba(scsi_qla_host_t *ha)
1071 char connect_type[22];
1073 /* Get host addresses. */
1074 rval = qla2x00_get_adapter_id(ha,
1075 &loop_id, &al_pa, &area, &domain, &topo);
1076 if (rval != QLA_SUCCESS) {
1077 qla_printk(KERN_WARNING, ha,
1078 "ERROR -- Unable to get host loop ID.\n");
1079 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1084 qla_printk(KERN_INFO, ha,
1085 "Cannot get topology - retrying.\n");
1086 return (QLA_FUNCTION_FAILED);
1089 ha->loop_id = loop_id;
1092 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1093 ha->operating_mode = LOOP;
1097 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1099 ha->current_topology = ISP_CFG_NL;
1100 strcpy(connect_type, "(Loop)");
1104 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1106 ha->current_topology = ISP_CFG_FL;
1107 strcpy(connect_type, "(FL_Port)");
1111 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1113 ha->operating_mode = P2P;
1114 ha->current_topology = ISP_CFG_N;
1115 strcpy(connect_type, "(N_Port-to-N_Port)");
1119 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1121 ha->operating_mode = P2P;
1122 ha->current_topology = ISP_CFG_F;
1123 strcpy(connect_type, "(F_Port)");
1127 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1129 ha->host_no, topo));
1130 ha->current_topology = ISP_CFG_NL;
1131 strcpy(connect_type, "(Loop)");
1135 /* Save Host port and loop ID. */
1136 /* byte order - Big Endian */
1137 ha->d_id.b.domain = domain;
1138 ha->d_id.b.area = area;
1139 ha->d_id.b.al_pa = al_pa;
1141 if (!ha->flags.init_done)
1142 qla_printk(KERN_INFO, ha,
1143 "Topology - %s, Host Loop address 0x%x\n",
1144 connect_type, ha->loop_id);
1147 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1149 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1156 * NVRAM configuration for ISP 2xxx
1159 * ha = adapter block pointer.
1162 * initialization control block in response_ring
1163 * host adapters parameters in host adapter block
1169 qla2x00_nvram_config(scsi_qla_host_t *ha)
1174 uint8_t *dptr1, *dptr2;
1175 init_cb_t *icb = ha->init_cb;
1176 nvram_t *nv = (nvram_t *)ha->request_ring;
1177 uint16_t *wptr = (uint16_t *)ha->request_ring;
1178 device_reg_t __iomem *reg = ha->iobase;
1183 /* Determine NVRAM starting address. */
1185 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1186 if ((RD_REG_WORD(®->ctrl_status) >> 14) == 1)
1187 ha->nvram_base = 0x80;
1189 /* Get NVRAM data and calculate checksum. */
1190 qla2x00_lock_nvram_access(ha);
1191 for (cnt = 0; cnt < sizeof(nvram_t)/2; cnt++) {
1192 *wptr = cpu_to_le16(qla2x00_get_nvram_word(ha,
1193 (cnt+ha->nvram_base)));
1194 chksum += (uint8_t)*wptr;
1195 chksum += (uint8_t)(*wptr >> 8);
1198 qla2x00_unlock_nvram_access(ha);
1200 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1201 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1204 /* Bad NVRAM data, set defaults parameters. */
1205 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1206 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1207 /* Reset NVRAM data. */
1208 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1209 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1211 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1212 "invalid -- WWPN) defaults.\n");
1215 * Set default initialization control block.
1217 memset(nv, 0, sizeof(nvram_t));
1218 nv->parameter_block_version = ICB_VERSION;
1220 if (IS_QLA23XX(ha)) {
1221 nv->firmware_options[0] = BIT_2 | BIT_1;
1222 nv->firmware_options[1] = BIT_7 | BIT_5;
1223 nv->add_firmware_options[0] = BIT_5;
1224 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1225 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1226 nv->special_options[1] = BIT_7;
1227 } else if (IS_QLA2200(ha)) {
1228 nv->firmware_options[0] = BIT_2 | BIT_1;
1229 nv->firmware_options[1] = BIT_7 | BIT_5;
1230 nv->add_firmware_options[0] = BIT_5;
1231 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1232 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1233 } else if (IS_QLA2100(ha)) {
1234 nv->firmware_options[0] = BIT_3 | BIT_1;
1235 nv->firmware_options[1] = BIT_5;
1236 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1239 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1240 nv->execution_throttle = __constant_cpu_to_le16(16);
1241 nv->retry_count = 8;
1242 nv->retry_delay = 1;
1244 nv->port_name[0] = 33;
1245 nv->port_name[3] = 224;
1246 nv->port_name[4] = 139;
1248 nv->login_timeout = 4;
1251 * Set default host adapter parameters
1253 nv->host_p[1] = BIT_2;
1254 nv->reset_delay = 5;
1255 nv->port_down_retry_count = 8;
1256 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1257 nv->link_down_timeout = 60;
1262 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1264 * The SN2 does not provide BIOS emulation which means you can't change
1265 * potentially bogus BIOS settings. Force the use of default settings
1266 * for link rate and frame size. Hope that the rest of the settings
1269 if (ia64_platform_is("sn2")) {
1270 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1272 nv->special_options[1] = BIT_7;
1276 /* Reset Initialization control block */
1277 memset(icb, 0, sizeof(init_cb_t));
1280 * Setup driver NVRAM options.
1282 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1283 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1284 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1285 nv->firmware_options[1] &= ~BIT_4;
1287 if (IS_QLA23XX(ha)) {
1288 nv->firmware_options[0] |= BIT_2;
1289 nv->firmware_options[0] &= ~BIT_3;
1291 if (IS_QLA2300(ha)) {
1292 if (ha->fb_rev == FPM_2310) {
1293 strcpy(ha->model_number, "QLA2310");
1295 strcpy(ha->model_number, "QLA2300");
1299 memcmp(nv->model_number, BINZERO,
1300 sizeof(nv->model_number)) != 0) {
1303 strncpy(ha->model_number, nv->model_number,
1304 sizeof(nv->model_number));
1305 st = en = ha->model_number;
1306 en += sizeof(nv->model_number) - 1;
1308 if (*en != 0x20 && *en != 0x00)
1315 index = (ha->pdev->subsystem_device & 0xff);
1316 if (index < QLA_MODEL_NAMES) {
1317 strcpy(ha->model_number,
1318 qla2x00_model_name[index]);
1320 qla2x00_model_desc[index];
1322 strcpy(ha->model_number, "QLA23xx");
1326 } else if (IS_QLA2200(ha)) {
1327 nv->firmware_options[0] |= BIT_2;
1329 * 'Point-to-point preferred, else loop' is not a safe
1330 * connection mode setting.
1332 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1334 /* Force 'loop preferred, else point-to-point'. */
1335 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1336 nv->add_firmware_options[0] |= BIT_5;
1338 strcpy(ha->model_number, "QLA22xx");
1339 } else /*if (IS_QLA2100(ha))*/ {
1340 strcpy(ha->model_number, "QLA2100");
1344 * Copy over NVRAM RISC parameter block to initialization control block.
1346 dptr1 = (uint8_t *)icb;
1347 dptr2 = (uint8_t *)&nv->parameter_block_version;
1348 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1350 *dptr1++ = *dptr2++;
1352 /* Copy 2nd half. */
1353 dptr1 = (uint8_t *)icb->add_firmware_options;
1354 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1356 *dptr1++ = *dptr2++;
1358 /* Prepare nodename */
1359 if ((icb->firmware_options[1] & BIT_6) == 0) {
1361 * Firmware will apply the following mask if the nodename was
1364 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1365 icb->node_name[0] &= 0xF0;
1369 * Set host adapter parameters.
1371 ha->nvram_version = nv->nvram_version;
1373 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1374 /* Always load RISC code on non ISP2[12]00 chips. */
1375 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1376 ha->flags.disable_risc_code_load = 0;
1377 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1378 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1379 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1380 ha->flags.enable_led_scheme = ((nv->efi_parameters & BIT_3) ? 1 : 0);
1382 ha->operating_mode =
1383 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1385 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1386 sizeof(ha->fw_seriallink_options));
1388 /* save HBA serial number */
1389 ha->serial0 = icb->port_name[5];
1390 ha->serial1 = icb->port_name[6];
1391 ha->serial2 = icb->port_name[7];
1392 memcpy(ha->node_name, icb->node_name, WWN_SIZE);
1394 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1396 ha->retry_count = nv->retry_count;
1398 /* Set minimum login_timeout to 4 seconds. */
1399 if (nv->login_timeout < ql2xlogintimeout)
1400 nv->login_timeout = ql2xlogintimeout;
1401 if (nv->login_timeout < 4)
1402 nv->login_timeout = 4;
1403 ha->login_timeout = nv->login_timeout;
1404 icb->login_timeout = nv->login_timeout;
1406 /* Set minimum RATOV to 200 tenths of a second. */
1409 ha->loop_reset_delay = nv->reset_delay;
1411 /* Link Down Timeout = 0:
1413 * When Port Down timer expires we will start returning
1414 * I/O's to OS with "DID_NO_CONNECT".
1416 * Link Down Timeout != 0:
1418 * The driver waits for the link to come up after link down
1419 * before returning I/Os to OS with "DID_NO_CONNECT".
1421 if (nv->link_down_timeout == 0) {
1422 ha->loop_down_abort_time =
1423 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1425 ha->link_down_timeout = nv->link_down_timeout;
1426 ha->loop_down_abort_time =
1427 (LOOP_DOWN_TIME - ha->link_down_timeout);
1431 * Need enough time to try and get the port back.
1433 ha->port_down_retry_count = nv->port_down_retry_count;
1434 if (qlport_down_retry)
1435 ha->port_down_retry_count = qlport_down_retry;
1436 /* Set login_retry_count */
1437 ha->login_retry_count = nv->retry_count;
1438 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1439 ha->port_down_retry_count > 3)
1440 ha->login_retry_count = ha->port_down_retry_count;
1441 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1442 ha->login_retry_count = ha->port_down_retry_count;
1443 if (ql2xloginretrycount)
1444 ha->login_retry_count = ql2xloginretrycount;
1446 icb->lun_enables = __constant_cpu_to_le16(0);
1447 icb->command_resource_count = 0;
1448 icb->immediate_notify_resource_count = 0;
1449 icb->timeout = __constant_cpu_to_le16(0);
1451 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1453 icb->firmware_options[0] &= ~BIT_3;
1454 icb->add_firmware_options[0] &=
1455 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1456 icb->add_firmware_options[0] |= BIT_2;
1457 icb->response_accumulation_timer = 3;
1458 icb->interrupt_delay_timer = 5;
1460 ha->flags.process_response_queue = 1;
1462 /* Enable ZIO -- Support mode 5 only. */
1463 timer_mode = icb->add_firmware_options[0] &
1464 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1465 icb->add_firmware_options[0] &=
1466 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1468 timer_mode = BIT_2 | BIT_0;
1469 if (timer_mode == (BIT_2 | BIT_0)) {
1470 DEBUG2(printk("scsi(%ld): ZIO enabled; timer delay "
1471 "(%d).\n", ha->host_no, ql2xintrdelaytimer));
1472 qla_printk(KERN_INFO, ha,
1473 "ZIO enabled; timer delay (%d).\n",
1474 ql2xintrdelaytimer);
1476 icb->add_firmware_options[0] |= timer_mode;
1477 icb->interrupt_delay_timer = ql2xintrdelaytimer;
1478 ha->flags.process_response_queue = 1;
1483 DEBUG2_3(printk(KERN_WARNING
1484 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1490 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1492 * @flags: allocation flags
1494 * Returns a pointer to the allocated fcport, or NULL, if none available.
1497 qla2x00_alloc_fcport(scsi_qla_host_t *ha, int flags)
1501 fcport = kmalloc(sizeof(fc_port_t), flags);
1505 /* Setup fcport template structure. */
1506 memset(fcport, 0, sizeof (fc_port_t));
1508 fcport->port_type = FCT_UNKNOWN;
1509 fcport->loop_id = FC_NO_LOOP_ID;
1510 fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
1511 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1512 fcport->flags = FCF_RLC_SUPPORT;
1518 * qla2x00_configure_loop
1519 * Updates Fibre Channel Device Database with what is actually on loop.
1522 * ha = adapter block pointer.
1527 * 2 = database was full and device was not configured.
1530 qla2x00_configure_loop(scsi_qla_host_t *ha)
1533 unsigned long flags, save_flags;
1537 /* Get Initiator ID */
1538 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1539 rval = qla2x00_configure_hba(ha);
1540 if (rval != QLA_SUCCESS) {
1541 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1547 save_flags = flags = ha->dpc_flags;
1548 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1549 ha->host_no, flags));
1552 * If we have both an RSCN and PORT UPDATE pending then handle them
1553 * both at the same time.
1555 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1556 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1558 /* Determine what we need to do */
1559 if (ha->current_topology == ISP_CFG_FL &&
1560 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1562 ha->flags.rscn_queue_overflow = 1;
1563 set_bit(RSCN_UPDATE, &flags);
1565 } else if (ha->current_topology == ISP_CFG_F &&
1566 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1568 ha->flags.rscn_queue_overflow = 1;
1569 set_bit(RSCN_UPDATE, &flags);
1570 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1572 } else if (!ha->flags.online ||
1573 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1575 ha->flags.rscn_queue_overflow = 1;
1576 set_bit(RSCN_UPDATE, &flags);
1577 set_bit(LOCAL_LOOP_UPDATE, &flags);
1580 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1581 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1582 rval = QLA_FUNCTION_FAILED;
1584 rval = qla2x00_configure_local_loop(ha);
1588 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1589 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1590 rval = QLA_FUNCTION_FAILED;
1592 rval = qla2x00_configure_fabric(ha);
1596 if (rval == QLA_SUCCESS) {
1597 if (atomic_read(&ha->loop_down_timer) ||
1598 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1599 rval = QLA_FUNCTION_FAILED;
1601 atomic_set(&ha->loop_state, LOOP_READY);
1603 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1608 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1609 __func__, ha->host_no));
1611 DEBUG3(printk("%s: exiting normally\n", __func__));
1614 /* Restore state if a resync event occured during processing */
1615 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1616 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1617 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1618 if (test_bit(RSCN_UPDATE, &save_flags))
1619 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1628 * qla2x00_configure_local_loop
1629 * Updates Fibre Channel Device Database with local loop devices.
1632 * ha = adapter block pointer.
1638 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1643 fc_port_t *fcport, *new_fcport;
1649 uint8_t domain, area, al_pa;
1653 entries = MAX_FIBRE_DEVICES;
1655 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1656 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1658 /* Get list of logged in devices. */
1659 memset(ha->gid_list, 0, GID_LIST_SIZE);
1660 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1662 if (rval != QLA_SUCCESS)
1663 goto cleanup_allocation;
1665 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1666 ha->host_no, entries));
1667 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1668 entries * sizeof(struct gid_list_info)));
1670 /* Allocate temporary fcport for any new fcports discovered. */
1671 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1672 if (new_fcport == NULL) {
1673 rval = QLA_MEMORY_ALLOC_FAILED;
1674 goto cleanup_allocation;
1676 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1679 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1681 list_for_each_entry(fcport, &ha->fcports, list) {
1682 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1683 fcport->port_type != FCT_BROADCAST &&
1684 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1686 DEBUG(printk("scsi(%ld): Marking port lost, "
1688 ha->host_no, fcport->loop_id));
1690 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1691 fcport->flags &= ~FCF_FARP_DONE;
1695 /* Add devices to port list. */
1696 id_iter = (char *)ha->gid_list;
1697 for (index = 0; index < entries; index++) {
1698 domain = ((struct gid_list_info *)id_iter)->domain;
1699 area = ((struct gid_list_info *)id_iter)->area;
1700 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1701 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1702 loop_id = (uint16_t)
1703 ((struct gid_list_info *)id_iter)->loop_id_2100;
1706 loop_id = le16_to_cpu(
1707 ((struct gid_list_info *)id_iter)->loop_id);
1711 /* Bypass reserved domain fields. */
1712 if ((domain & 0xf0) == 0xf0)
1715 /* Bypass if not same domain and area of adapter. */
1716 if (area != ha->d_id.b.area || domain != ha->d_id.b.domain)
1719 /* Bypass invalid local loop ID. */
1720 if (loop_id > LAST_LOCAL_LOOP_ID)
1723 /* Fill in member data. */
1724 new_fcport->d_id.b.domain = domain;
1725 new_fcport->d_id.b.area = area;
1726 new_fcport->d_id.b.al_pa = al_pa;
1727 new_fcport->loop_id = loop_id;
1728 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1729 if (rval2 != QLA_SUCCESS) {
1730 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1731 "information -- get_port_database=%x, "
1733 ha->host_no, rval2, new_fcport->loop_id));
1737 /* Check for matching device in port list. */
1740 list_for_each_entry(fcport, &ha->fcports, list) {
1741 if (memcmp(new_fcport->port_name, fcport->port_name,
1745 fcport->flags &= ~(FCF_FABRIC_DEVICE |
1746 FCF_PERSISTENT_BOUND);
1747 fcport->loop_id = new_fcport->loop_id;
1748 fcport->port_type = new_fcport->port_type;
1749 fcport->d_id.b24 = new_fcport->d_id.b24;
1750 memcpy(fcport->node_name, new_fcport->node_name,
1758 /* New device, add to fcports list. */
1759 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
1760 list_add_tail(&new_fcport->list, &ha->fcports);
1762 /* Allocate a new replacement fcport. */
1763 fcport = new_fcport;
1764 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1765 if (new_fcport == NULL) {
1766 rval = QLA_MEMORY_ALLOC_FAILED;
1767 goto cleanup_allocation;
1769 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1772 qla2x00_update_fcport(ha, fcport);
1781 if (rval != QLA_SUCCESS) {
1782 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
1783 "rval=%x\n", ha->host_no, rval));
1787 ha->device_flags |= DFLG_LOCAL_DEVICES;
1788 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
1795 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
1799 qla2x00_mark_all_devices_lost(ha);
1800 list_for_each_entry(fcport, &ha->fcports, list) {
1801 if (fcport->port_type != FCT_TARGET)
1804 qla2x00_update_fcport(ha, fcport);
1809 * qla2x00_update_fcport
1810 * Updates device on list.
1813 * ha = adapter block pointer.
1814 * fcport = port structure pointer.
1824 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
1827 unsigned long flags;
1831 fcport->login_retry = 0;
1832 fcport->port_login_retry_count = ha->port_down_retry_count *
1834 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
1836 fcport->flags &= ~FCF_LOGIN_NEEDED;
1839 * Check for outstanding cmd on tape Bypass LUN discovery if active
1842 if (fcport->flags & FCF_TAPE_PRESENT) {
1843 spin_lock_irqsave(&ha->hardware_lock, flags);
1844 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1847 if ((sp = ha->outstanding_cmds[index]) != 0) {
1849 if (sfcp == fcport) {
1850 atomic_set(&fcport->state, FCS_ONLINE);
1851 spin_unlock_irqrestore(
1852 &ha->hardware_lock, flags);
1857 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1860 if (fcport->port_type == FCT_INITIATOR ||
1861 fcport->port_type == FCT_BROADCAST)
1862 fcport->device_type = TYPE_PROCESSOR;
1864 atomic_set(&fcport->state, FCS_ONLINE);
1866 if (ha->flags.init_done)
1867 qla2x00_reg_remote_port(ha, fcport);
1871 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
1873 struct fc_rport_identifiers rport_ids;
1874 struct fc_rport *rport;
1876 if (fcport->rport) {
1877 fc_remote_port_unblock(fcport->rport);
1881 rport_ids.node_name = be64_to_cpu(*(uint64_t *)fcport->node_name);
1882 rport_ids.port_name = be64_to_cpu(*(uint64_t *)fcport->port_name);
1883 rport_ids.port_id = fcport->d_id.b.domain << 16 |
1884 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
1885 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
1886 if (fcport->port_type == FCT_INITIATOR)
1887 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1888 if (fcport->port_type == FCT_TARGET)
1889 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1891 fcport->rport = rport = fc_remote_port_add(ha->host, 0, &rport_ids);
1893 qla_printk(KERN_WARNING, ha,
1894 "Unable to allocate fc remote port!\n");
1896 if (rport->scsi_target_id != -1 && rport->scsi_target_id < MAX_TARGETS)
1897 fcport->os_target_id = rport->scsi_target_id;
1899 rport->dd_data = fcport;
1903 * qla2x00_configure_fabric
1904 * Setup SNS devices with loop ID's.
1907 * ha = adapter block pointer.
1914 qla2x00_configure_fabric(scsi_qla_host_t *ha)
1917 fc_port_t *fcport, *fcptemp;
1918 uint16_t next_loopid;
1919 uint16_t mb[MAILBOX_REGISTER_COUNT];
1920 LIST_HEAD(new_fcports);
1922 /* If FL port exists, then SNS is present */
1923 rval = qla2x00_get_port_name(ha, SNS_FL_PORT, NULL, 0);
1924 if (rval != QLA_SUCCESS) {
1925 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
1926 "Port\n", ha->host_no));
1928 ha->device_flags &= ~SWITCH_FOUND;
1929 return (QLA_SUCCESS);
1932 /* Mark devices that need re-synchronization. */
1933 rval2 = qla2x00_device_resync(ha);
1934 if (rval2 == QLA_RSCNS_HANDLED) {
1935 /* No point doing the scan, just continue. */
1936 return (QLA_SUCCESS);
1939 /* Ensure we are logged into the SNS. */
1940 qla2x00_login_fabric(ha, SIMPLE_NAME_SERVER, 0xff, 0xff, 0xfc,
1942 if (mb[0] != MBS_COMMAND_COMPLETE) {
1943 DEBUG2(qla_printk(KERN_INFO, ha,
1944 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
1945 "mb[2]=%x mb[6]=%x mb[7]=%x\n", SIMPLE_NAME_SERVER,
1946 mb[0], mb[1], mb[2], mb[6], mb[7]));
1947 return (QLA_SUCCESS);
1950 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
1951 if (qla2x00_rft_id(ha)) {
1953 DEBUG2(printk("scsi(%ld): Register FC-4 "
1954 "TYPE failed.\n", ha->host_no));
1956 if (qla2x00_rff_id(ha)) {
1958 DEBUG2(printk("scsi(%ld): Register FC-4 "
1959 "Features failed.\n", ha->host_no));
1961 if (qla2x00_rnn_id(ha)) {
1963 DEBUG2(printk("scsi(%ld): Register Node Name "
1964 "failed.\n", ha->host_no));
1965 } else if (qla2x00_rsnn_nn(ha)) {
1967 DEBUG2(printk("scsi(%ld): Register Symbolic "
1968 "Node Name failed.\n", ha->host_no));
1972 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
1973 if (rval != QLA_SUCCESS)
1977 * Logout all previous fabric devices marked lost, except
1980 list_for_each_entry(fcport, &ha->fcports, list) {
1981 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
1984 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
1987 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
1988 qla2x00_mark_device_lost(ha, fcport,
1989 ql2xplogiabsentdevice);
1990 if (fcport->loop_id != FC_NO_LOOP_ID &&
1991 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
1992 fcport->port_type != FCT_INITIATOR &&
1993 fcport->port_type != FCT_BROADCAST) {
1995 qla2x00_fabric_logout(ha,
1997 fcport->loop_id = FC_NO_LOOP_ID;
2002 /* Starting free loop ID. */
2003 next_loopid = ha->min_external_loopid;
2006 * Scan through our port list and login entries that need to be
2009 list_for_each_entry(fcport, &ha->fcports, list) {
2010 if (atomic_read(&ha->loop_down_timer) ||
2011 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2014 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2015 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2018 if (fcport->loop_id == FC_NO_LOOP_ID) {
2019 fcport->loop_id = next_loopid;
2020 rval = qla2x00_find_new_loop_id(ha, fcport);
2021 if (rval != QLA_SUCCESS) {
2022 /* Ran out of IDs to use */
2027 /* Login and update database */
2028 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2031 /* Exit if out of loop IDs. */
2032 if (rval != QLA_SUCCESS) {
2037 * Login and add the new devices to our port list.
2039 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2040 if (atomic_read(&ha->loop_down_timer) ||
2041 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2044 /* Find a new loop ID to use. */
2045 fcport->loop_id = next_loopid;
2046 rval = qla2x00_find_new_loop_id(ha, fcport);
2047 if (rval != QLA_SUCCESS) {
2048 /* Ran out of IDs to use */
2052 /* Remove device from the new list and add it to DB */
2053 list_del(&fcport->list);
2054 list_add_tail(&fcport->list, &ha->fcports);
2056 /* Login and update database */
2057 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2061 /* Free all new device structures not processed. */
2062 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2063 list_del(&fcport->list);
2068 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2069 "rval=%d\n", ha->host_no, rval));
2077 * qla2x00_find_all_fabric_devs
2080 * ha = adapter block pointer.
2081 * dev = database device entry pointer.
2090 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2094 fc_port_t *fcport, *new_fcport, *fcptemp;
2099 int first_dev, last_dev;
2100 port_id_t wrap, nxt_d_id;
2104 /* Try GID_PT to get device list, else GAN. */
2105 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2108 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2109 "on GA_NXT\n", ha->host_no));
2111 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2112 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2115 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2118 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2125 /* Allocate temporary fcport for any new fcports discovered. */
2126 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2127 if (new_fcport == NULL) {
2130 return (QLA_MEMORY_ALLOC_FAILED);
2132 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2134 /* Set start port ID scan at adapter ID. */
2138 /* Starting free loop ID. */
2139 loop_id = ha->min_external_loopid;
2141 for (; loop_id <= ha->last_loop_id; loop_id++) {
2142 if (RESERVED_LOOP_ID(loop_id))
2145 if (atomic_read(&ha->loop_down_timer) ||
2146 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2151 wrap.b24 = new_fcport->d_id.b24;
2153 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2154 memcpy(new_fcport->node_name,
2155 swl[swl_idx].node_name, WWN_SIZE);
2156 memcpy(new_fcport->port_name,
2157 swl[swl_idx].port_name, WWN_SIZE);
2159 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2165 /* Send GA_NXT to the switch */
2166 rval = qla2x00_ga_nxt(ha, new_fcport);
2167 if (rval != QLA_SUCCESS) {
2168 qla_printk(KERN_WARNING, ha,
2169 "SNS scan failed -- assuming zero-entry "
2171 list_for_each_entry_safe(fcport, fcptemp,
2172 new_fcports, list) {
2173 list_del(&fcport->list);
2181 /* If wrap on switch device list, exit. */
2183 wrap.b24 = new_fcport->d_id.b24;
2185 } else if (new_fcport->d_id.b24 == wrap.b24) {
2186 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2187 ha->host_no, new_fcport->d_id.b.domain,
2188 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2192 /* Bypass if host adapter. */
2193 if (new_fcport->d_id.b24 == ha->d_id.b24)
2196 /* Bypass reserved domain fields. */
2197 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2200 /* Locate matching device in database. */
2202 list_for_each_entry(fcport, &ha->fcports, list) {
2203 if (memcmp(new_fcport->port_name, fcport->port_name,
2210 * If address the same and state FCS_ONLINE, nothing
2213 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2214 atomic_read(&fcport->state) == FCS_ONLINE) {
2219 * If device was not a fabric device before.
2221 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2222 fcport->d_id.b24 = new_fcport->d_id.b24;
2223 fcport->loop_id = FC_NO_LOOP_ID;
2224 fcport->flags |= (FCF_FABRIC_DEVICE |
2226 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2231 * Port ID changed or device was marked to be updated;
2232 * Log it out if still logged in and mark it for
2235 fcport->d_id.b24 = new_fcport->d_id.b24;
2236 fcport->flags |= FCF_LOGIN_NEEDED;
2237 if (fcport->loop_id != FC_NO_LOOP_ID &&
2238 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2239 fcport->port_type != FCT_INITIATOR &&
2240 fcport->port_type != FCT_BROADCAST) {
2241 qla2x00_fabric_logout(ha, fcport->loop_id);
2242 fcport->loop_id = FC_NO_LOOP_ID;
2251 /* If device was not in our fcports list, then add it. */
2252 list_add_tail(&new_fcport->list, new_fcports);
2254 /* Allocate a new replacement fcport. */
2255 nxt_d_id.b24 = new_fcport->d_id.b24;
2256 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2257 if (new_fcport == NULL) {
2260 return (QLA_MEMORY_ALLOC_FAILED);
2262 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2263 new_fcport->d_id.b24 = nxt_d_id.b24;
2272 if (!list_empty(new_fcports))
2273 ha->device_flags |= DFLG_FABRIC_DEVICES;
2279 * qla2x00_find_new_loop_id
2280 * Scan through our port list and find a new usable loop ID.
2283 * ha: adapter state pointer.
2284 * dev: port structure pointer.
2287 * qla2x00 local function return status code.
2293 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2298 uint16_t first_loop_id;
2302 /* Save starting loop ID. */
2303 first_loop_id = dev->loop_id;
2306 /* Skip loop ID if already used by adapter. */
2307 if (dev->loop_id == ha->loop_id) {
2311 /* Skip reserved loop IDs. */
2312 while (RESERVED_LOOP_ID(dev->loop_id)) {
2316 /* Reset loop ID if passed the end. */
2317 if (dev->loop_id > ha->last_loop_id) {
2318 /* first loop ID. */
2319 dev->loop_id = ha->min_external_loopid;
2322 /* Check for loop ID being already in use. */
2325 list_for_each_entry(fcport, &ha->fcports, list) {
2326 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2327 /* ID possibly in use */
2333 /* If not in use then it is free to use. */
2338 /* ID in use. Try next value. */
2341 /* If wrap around. No free ID to use. */
2342 if (dev->loop_id == first_loop_id) {
2343 dev->loop_id = FC_NO_LOOP_ID;
2344 rval = QLA_FUNCTION_FAILED;
2353 * qla2x00_device_resync
2354 * Marks devices in the database that needs resynchronization.
2357 * ha = adapter block pointer.
2363 qla2x00_device_resync(scsi_qla_host_t *ha)
2369 uint32_t rscn_entry;
2370 uint8_t rscn_out_iter;
2374 rval = QLA_RSCNS_HANDLED;
2376 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2377 ha->flags.rscn_queue_overflow) {
2379 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2380 format = MSB(MSW(rscn_entry));
2381 d_id.b.domain = LSB(MSW(rscn_entry));
2382 d_id.b.area = MSB(LSW(rscn_entry));
2383 d_id.b.al_pa = LSB(LSW(rscn_entry));
2385 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2386 "[%02x/%02x%02x%02x].\n",
2387 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2388 d_id.b.area, d_id.b.al_pa));
2391 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2392 ha->rscn_out_ptr = 0;
2394 /* Skip duplicate entries. */
2395 for (rscn_out_iter = ha->rscn_out_ptr;
2396 !ha->flags.rscn_queue_overflow &&
2397 rscn_out_iter != ha->rscn_in_ptr;
2398 rscn_out_iter = (rscn_out_iter ==
2399 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2401 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2404 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2405 "entry found at [%d].\n", ha->host_no,
2408 ha->rscn_out_ptr = rscn_out_iter;
2411 /* Queue overflow, set switch default case. */
2412 if (ha->flags.rscn_queue_overflow) {
2413 DEBUG(printk("scsi(%ld): device_resync: rscn "
2414 "overflow.\n", ha->host_no));
2417 ha->flags.rscn_queue_overflow = 0;
2422 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) &&
2423 !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
2424 ha->flags.init_done) {
2425 /* Handle port RSCN via asyncronous IOCBs */
2426 rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
2428 if (rval2 == QLA_SUCCESS)
2442 ha->rscn_out_ptr = ha->rscn_in_ptr;
2448 /* Abort any outstanding IO descriptors. */
2449 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2450 qla2x00_cancel_io_descriptors(ha);
2452 list_for_each_entry(fcport, &ha->fcports, list) {
2453 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2454 (fcport->d_id.b24 & mask) != d_id.b24 ||
2455 fcport->port_type == FCT_BROADCAST)
2458 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2460 fcport->port_type != FCT_INITIATOR) {
2461 qla2x00_mark_device_lost(ha, fcport, 0);
2464 fcport->flags &= ~FCF_FARP_DONE;
2471 * qla2x00_fabric_dev_login
2472 * Login fabric target device and update FC port database.
2475 * ha: adapter state pointer.
2476 * fcport: port structure list pointer.
2477 * next_loopid: contains value of a new loop ID that can be used
2478 * by the next login attempt.
2481 * qla2x00 local function return status code.
2487 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2488 uint16_t *next_loopid)
2496 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2497 if (rval == QLA_SUCCESS) {
2498 rval = qla2x00_get_port_database(ha, fcport, 0);
2499 if (rval != QLA_SUCCESS) {
2500 qla2x00_fabric_logout(ha, fcport->loop_id);
2502 qla2x00_update_fcport(ha, fcport);
2510 * qla2x00_fabric_login
2511 * Issue fabric login command.
2514 * ha = adapter block pointer.
2515 * device = pointer to FC device type structure.
2518 * 0 - Login successfully
2520 * 2 - Initiator device
2524 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2525 uint16_t *next_loopid)
2529 uint16_t tmp_loopid;
2530 uint16_t mb[MAILBOX_REGISTER_COUNT];
2536 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2537 "for port %02x%02x%02x.\n",
2538 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2539 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2541 /* Login fcport on switch. */
2542 qla2x00_login_fabric(ha, fcport->loop_id,
2543 fcport->d_id.b.domain, fcport->d_id.b.area,
2544 fcport->d_id.b.al_pa, mb, BIT_0);
2545 if (mb[0] == MBS_PORT_ID_USED) {
2547 * Device has another loop ID. The firmware team
2548 * recommends us to perform an implicit login with the
2549 * specified ID again. The ID we just used is save here
2550 * so we return with an ID that can be tried by the
2554 tmp_loopid = fcport->loop_id;
2555 fcport->loop_id = mb[1];
2557 DEBUG(printk("Fabric Login: port in use - next "
2558 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2559 fcport->loop_id, fcport->d_id.b.domain,
2560 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2562 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2567 /* A retry occurred before. */
2568 *next_loopid = tmp_loopid;
2571 * No retry occurred before. Just increment the
2572 * ID value for next login.
2574 *next_loopid = (fcport->loop_id + 1);
2577 if (mb[1] & BIT_0) {
2578 fcport->port_type = FCT_INITIATOR;
2580 fcport->port_type = FCT_TARGET;
2581 if (mb[1] & BIT_1) {
2582 fcport->flags |= FCF_TAPE_PRESENT;
2588 } else if (mb[0] == MBS_LOOP_ID_USED) {
2590 * Loop ID already used, try next loop ID.
2593 rval = qla2x00_find_new_loop_id(ha, fcport);
2594 if (rval != QLA_SUCCESS) {
2595 /* Ran out of loop IDs to use */
2598 } else if (mb[0] == MBS_COMMAND_ERROR) {
2600 * Firmware possibly timed out during login. If NO
2601 * retries are left to do then the device is declared
2604 *next_loopid = fcport->loop_id;
2605 qla2x00_fabric_logout(ha, fcport->loop_id);
2606 qla2x00_mark_device_lost(ha, fcport, 1);
2612 * unrecoverable / not handled error
2614 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
2615 "loop_id=%x jiffies=%lx.\n",
2616 __func__, ha->host_no, mb[0],
2617 fcport->d_id.b.domain, fcport->d_id.b.area,
2618 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2620 *next_loopid = fcport->loop_id;
2621 qla2x00_fabric_logout(ha, fcport->loop_id);
2622 fcport->loop_id = FC_NO_LOOP_ID;
2623 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2634 * qla2x00_local_device_login
2635 * Issue local device login command.
2638 * ha = adapter block pointer.
2639 * loop_id = loop id of device to login to.
2641 * Returns (Where's the #define!!!!):
2642 * 0 - Login successfully
2647 qla2x00_local_device_login(scsi_qla_host_t *ha, uint16_t loop_id)
2650 uint16_t mb[MAILBOX_REGISTER_COUNT];
2652 memset(mb, 0, sizeof(mb));
2653 rval = qla2x00_login_local_device(ha, loop_id, mb, BIT_0);
2654 if (rval == QLA_SUCCESS) {
2655 /* Interrogate mailbox registers for any errors */
2656 if (mb[0] == MBS_COMMAND_ERROR)
2658 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2659 /* device not in PCB table */
2667 * qla2x00_loop_resync
2668 * Resync with fibre channel devices.
2671 * ha = adapter block pointer.
2677 qla2x00_loop_resync(scsi_qla_host_t *ha)
2684 atomic_set(&ha->loop_state, LOOP_UPDATE);
2685 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2686 if (ha->flags.online) {
2687 if (!(rval = qla2x00_fw_ready(ha))) {
2688 /* Wait at most MAX_TARGET RSCNs for a stable link. */
2692 atomic_set(&ha->loop_state, LOOP_UPDATE);
2695 * Issue marker command only when we are going
2696 * to start the I/O .
2698 ha->marker_needed = 1;
2700 /* Remap devices on Loop. */
2701 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2703 qla2x00_configure_loop(ha);
2705 } while (!atomic_read(&ha->loop_down_timer) &&
2706 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2708 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2712 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2713 return (QLA_FUNCTION_FAILED);
2717 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
2724 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
2730 list_for_each_entry(fcport, &ha->fcports, list) {
2731 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
2734 qla2x00_update_fcport(ha, fcport);
2735 fcport->flags &= ~FCF_RESCAN_NEEDED;
2739 qla2x00_probe_for_all_luns(ha);
2744 * Resets ISP and aborts all outstanding commands.
2747 * ha = adapter block pointer.
2753 qla2x00_abort_isp(scsi_qla_host_t *ha)
2755 unsigned long flags = 0;
2760 if (ha->flags.online) {
2761 ha->flags.online = 0;
2762 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2764 qla_printk(KERN_INFO, ha,
2765 "Performing ISP error recovery - ha= %p.\n", ha);
2766 qla2x00_reset_chip(ha);
2768 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
2769 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
2770 atomic_set(&ha->loop_state, LOOP_DOWN);
2771 qla2x00_mark_all_devices_lost(ha);
2773 if (!atomic_read(&ha->loop_down_timer))
2774 atomic_set(&ha->loop_down_timer,
2778 spin_lock_irqsave(&ha->hardware_lock, flags);
2779 /* Requeue all commands in outstanding command list. */
2780 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
2781 sp = ha->outstanding_cmds[cnt];
2783 ha->outstanding_cmds[cnt] = NULL;
2785 sp->cmd->result = DID_RESET << 16;
2786 sp->cmd->host_scribble = (unsigned char *)NULL;
2787 qla2x00_sp_compl(ha, sp);
2790 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2792 qla2x00_nvram_config(ha);
2794 if (!qla2x00_restart_isp(ha)) {
2795 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2797 if (!atomic_read(&ha->loop_down_timer)) {
2799 * Issue marker command only when we are going
2800 * to start the I/O .
2802 ha->marker_needed = 1;
2805 ha->flags.online = 1;
2807 /* Enable ISP interrupts. */
2808 qla2x00_enable_intrs(ha);
2810 ha->isp_abort_cnt = 0;
2811 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2812 } else { /* failed the ISP abort */
2813 ha->flags.online = 1;
2814 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
2815 if (ha->isp_abort_cnt == 0) {
2816 qla_printk(KERN_WARNING, ha,
2817 "ISP error recovery failed - "
2818 "board disabled\n");
2820 * The next call disables the board
2823 qla2x00_reset_adapter(ha);
2824 ha->flags.online = 0;
2825 clear_bit(ISP_ABORT_RETRY,
2828 } else { /* schedule another ISP abort */
2829 ha->isp_abort_cnt--;
2830 DEBUG(printk("qla%ld: ISP abort - "
2831 "retry remainning %d\n",
2832 ha->host_no, ha->isp_abort_cnt);)
2836 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
2837 DEBUG(printk("qla2x00(%ld): ISP error recovery "
2838 "- retrying (%d) more times\n",
2839 ha->host_no, ha->isp_abort_cnt);)
2840 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2848 qla_printk(KERN_INFO, ha,
2849 "qla2x00_abort_isp: **** FAILED ****\n");
2851 DEBUG(printk(KERN_INFO
2852 "qla2x00_abort_isp(%ld): exiting.\n",
2860 * qla2x00_restart_isp
2861 * restarts the ISP after a reset
2864 * ha = adapter block pointer.
2870 qla2x00_restart_isp(scsi_qla_host_t *ha)
2873 device_reg_t __iomem *reg = ha->iobase;
2874 unsigned long flags = 0;
2877 /* If firmware needs to be loaded */
2878 if (qla2x00_isp_firmware(ha)) {
2879 ha->flags.online = 0;
2880 if (!(status = qla2x00_chip_diag(ha))) {
2881 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2882 status = qla2x00_setup_chip(ha);
2888 spin_lock_irqsave(&ha->hardware_lock, flags);
2890 /* Disable SRAM, Instruction RAM and GP RAM parity. */
2891 WRT_REG_WORD(®->hccr, (HCCR_ENABLE_PARITY + 0x0));
2892 RD_REG_WORD(®->hccr); /* PCI Posting. */
2894 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2896 status = qla2x00_setup_chip(ha);
2898 spin_lock_irqsave(&ha->hardware_lock, flags);
2900 /* Enable proper parity */
2903 WRT_REG_WORD(®->hccr,
2904 (HCCR_ENABLE_PARITY + 0x1));
2906 /* SRAM, Instruction RAM and GP RAM parity */
2907 WRT_REG_WORD(®->hccr,
2908 (HCCR_ENABLE_PARITY + 0x7));
2909 RD_REG_WORD(®->hccr); /* PCI Posting. */
2911 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2916 if (!status && !(status = qla2x00_init_rings(ha))) {
2917 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2918 if (!(status = qla2x00_fw_ready(ha))) {
2919 DEBUG(printk("%s(): Start configure loop, "
2923 ha->flags.online = 1;
2924 /* Wait at most MAX_TARGET RSCNs for a stable link. */
2927 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2928 qla2x00_configure_loop(ha);
2930 } while (!atomic_read(&ha->loop_down_timer) &&
2931 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2933 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2936 /* if no cable then assume it's good */
2937 if ((ha->device_flags & DFLG_NO_CABLE))
2940 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
2948 * qla2x00_reset_adapter
2952 * ha = adapter block pointer.
2955 qla2x00_reset_adapter(scsi_qla_host_t *ha)
2957 unsigned long flags = 0;
2958 device_reg_t __iomem *reg = ha->iobase;
2960 ha->flags.online = 0;
2961 qla2x00_disable_intrs(ha);
2963 /* Reset RISC processor. */
2964 spin_lock_irqsave(&ha->hardware_lock, flags);
2965 WRT_REG_WORD(®->hccr, HCCR_RESET_RISC);
2966 RD_REG_WORD(®->hccr); /* PCI Posting. */
2967 WRT_REG_WORD(®->hccr, HCCR_RELEASE_RISC);
2968 RD_REG_WORD(®->hccr); /* PCI Posting. */
2969 spin_unlock_irqrestore(&ha->hardware_lock, flags);