Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / qla2xxx / qla_init.c
blob1ab5d92c3868eb349285faf6aa7e3ceca71ae124
1 /*
2 * QLOGIC LINUX SOFTWARE
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2004 QLogic Corporation
6 * (www.qlogic.com)
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
11 * later version.
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.
19 #include "qla_def.h"
21 #include <linux/delay.h>
23 #include "qla_devtbl.h"
25 /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
26 #ifndef EXT_IS_LUN_BIT_SET
27 #define EXT_IS_LUN_BIT_SET(P,L) \
28 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
29 #define EXT_SET_LUN_BIT(P,L) \
30 ((P)->mask[L/8] |= (0x80 >> (L%8)))
31 #endif
34 * QLogic ISP2x00 Hardware Support Function Prototypes.
36 static int qla2x00_pci_config(scsi_qla_host_t *);
37 static int qla2x00_isp_firmware(scsi_qla_host_t *);
38 static void qla2x00_reset_chip(scsi_qla_host_t *);
39 static int qla2x00_chip_diag(scsi_qla_host_t *);
40 static void qla2x00_resize_request_q(scsi_qla_host_t *);
41 static int qla2x00_setup_chip(scsi_qla_host_t *);
42 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
43 static int qla2x00_init_rings(scsi_qla_host_t *);
44 static int qla2x00_fw_ready(scsi_qla_host_t *);
45 static int qla2x00_configure_hba(scsi_qla_host_t *);
46 static int qla2x00_nvram_config(scsi_qla_host_t *);
47 static void qla2x00_init_tgt_map(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 void qla2x00_lun_discovery(scsi_qla_host_t *, fc_port_t *);
52 static int qla2x00_rpt_lun_discovery(scsi_qla_host_t *, fc_port_t *,
53 inq_cmd_rsp_t *, dma_addr_t);
54 static int qla2x00_report_lun(scsi_qla_host_t *, fc_port_t *);
55 static fc_lun_t *qla2x00_cfg_lun(scsi_qla_host_t *, fc_port_t *, uint16_t,
56 inq_cmd_rsp_t *, dma_addr_t);
57 static fc_lun_t * qla2x00_add_lun(fc_port_t *, uint16_t);
58 static int qla2x00_inquiry(scsi_qla_host_t *, fc_port_t *, uint16_t,
59 inq_cmd_rsp_t *, dma_addr_t);
60 static int qla2x00_configure_fabric(scsi_qla_host_t *);
61 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
62 static int qla2x00_device_resync(scsi_qla_host_t *);
63 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
64 uint16_t *);
65 static void qla2x00_config_os(scsi_qla_host_t *ha);
66 static uint16_t qla2x00_fcport_bind(scsi_qla_host_t *ha, fc_port_t *fcport);
67 static os_lun_t * qla2x00_fclun_bind(scsi_qla_host_t *, fc_port_t *,
68 fc_lun_t *);
69 static void qla2x00_lun_free(scsi_qla_host_t *, uint16_t, uint16_t);
71 static int qla2x00_restart_isp(scsi_qla_host_t *);
72 static void qla2x00_reset_adapter(scsi_qla_host_t *);
73 static os_tgt_t *qla2x00_tgt_alloc(scsi_qla_host_t *, uint16_t);
74 static os_lun_t *qla2x00_lun_alloc(scsi_qla_host_t *, uint16_t, uint16_t);
76 /****************************************************************************/
77 /* QLogic ISP2x00 Hardware Support Functions. */
78 /****************************************************************************/
81 * qla2x00_initialize_adapter
82 * Initialize board.
84 * Input:
85 * ha = adapter block pointer.
87 * Returns:
88 * 0 = success
90 int
91 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
93 int rval;
94 uint8_t restart_risc = 0;
95 uint8_t retry;
96 uint32_t wait_time;
98 /* Clear adapter flags. */
99 ha->flags.online = 0;
100 ha->flags.reset_active = 0;
101 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
102 atomic_set(&ha->loop_state, LOOP_DOWN);
103 ha->device_flags = 0;
104 ha->sns_retry_cnt = 0;
105 ha->dpc_flags = 0;
106 ha->failback_delay = 0;
107 ha->flags.management_server_logged_in = 0;
108 ha->marker_needed = 0;
109 ha->mbx_flags = 0;
110 ha->isp_abort_cnt = 0;
111 ha->beacon_blink_led = 0;
113 rval = qla2x00_pci_config(ha);
114 if (rval) {
115 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
116 ha->host_no));
117 return (rval);
120 qla2x00_reset_chip(ha);
122 /* Initialize target map database. */
123 qla2x00_init_tgt_map(ha);
125 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
126 qla2x00_nvram_config(ha);
128 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
130 retry = 10;
132 * Try to configure the loop.
134 do {
135 restart_risc = 0;
137 /* If firmware needs to be loaded */
138 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
139 if ((rval = qla2x00_chip_diag(ha)) == QLA_SUCCESS) {
140 rval = qla2x00_setup_chip(ha);
144 if (rval == QLA_SUCCESS &&
145 (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
146 check_fw_ready_again:
148 * Wait for a successful LIP up to a maximum
149 * of (in seconds): RISC login timeout value,
150 * RISC retry count value, and port down retry
151 * value OR a minimum of 4 seconds OR If no
152 * cable, only 5 seconds.
154 rval = qla2x00_fw_ready(ha);
155 if (rval == QLA_SUCCESS) {
156 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
159 * Wait at most MAX_TARGET RSCNs for a stable
160 * link.
162 wait_time = 256;
163 do {
164 clear_bit(LOOP_RESYNC_NEEDED,
165 &ha->dpc_flags);
166 rval = qla2x00_configure_loop(ha);
168 if (test_and_clear_bit(ISP_ABORT_NEEDED,
169 &ha->dpc_flags)) {
170 restart_risc = 1;
171 break;
175 * If loop state change while we were
176 * discoverying devices then wait for
177 * LIP to complete
180 if (atomic_read(&ha->loop_state) ==
181 LOOP_DOWN && retry--) {
182 goto check_fw_ready_again;
184 wait_time--;
185 } while (!atomic_read(&ha->loop_down_timer) &&
186 retry &&
187 wait_time &&
188 (test_bit(LOOP_RESYNC_NEEDED,
189 &ha->dpc_flags)));
191 if (wait_time == 0)
192 rval = QLA_FUNCTION_FAILED;
193 if (ha->mem_err)
194 restart_risc = 1;
195 } else if (ha->device_flags & DFLG_NO_CABLE)
196 /* If no cable, then all is good. */
197 rval = QLA_SUCCESS;
199 } while (restart_risc && retry--);
201 if (rval == QLA_SUCCESS) {
202 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
203 ha->marker_needed = 1;
204 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
205 ha->marker_needed = 0;
207 ha->flags.online = 1;
208 } else {
209 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
212 return (rval);
216 * qla2x00_pci_config() - Setup device PCI configuration registers.
217 * @ha: HA context
219 * Returns 0 on success.
221 static int
222 qla2x00_pci_config(scsi_qla_host_t *ha)
224 uint16_t w, mwi;
225 unsigned long flags = 0;
226 uint32_t cnt;
228 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
231 * Turn on PCI master; for system BIOSes that don't turn it on by
232 * default.
234 pci_set_master(ha->pdev);
235 mwi = 0;
236 if (pci_set_mwi(ha->pdev))
237 mwi = PCI_COMMAND_INVALIDATE;
238 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
240 if (!ha->iobase)
241 return (QLA_FUNCTION_FAILED);
244 * We want to respect framework's setting of PCI configuration space
245 * command register and also want to make sure that all bits of
246 * interest to us are properly set in command register.
248 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
249 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
251 /* Get PCI bus information. */
252 spin_lock_irqsave(&ha->hardware_lock, flags);
253 ha->pci_attr = RD_REG_WORD(&ha->iobase->ctrl_status);
254 spin_unlock_irqrestore(&ha->hardware_lock, flags);
256 if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
257 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
259 /* PCI Specification Revision 2.3 changes */
260 if (IS_QLA2322(ha) || IS_QLA6322(ha))
261 /* Command Register - Reset Interrupt Disable. */
262 w &= ~PCI_COMMAND_INTX_DISABLE;
265 * If this is a 2300 card and not 2312, reset the
266 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
267 * the 2310 also reports itself as a 2300 so we need to get the
268 * fb revision level -- a 6 indicates it really is a 2300 and
269 * not a 2310.
271 if (IS_QLA2300(ha)) {
272 spin_lock_irqsave(&ha->hardware_lock, flags);
274 /* Pause RISC. */
275 WRT_REG_WORD(&ha->iobase->hccr, HCCR_PAUSE_RISC);
276 for (cnt = 0; cnt < 30000; cnt++) {
277 if ((RD_REG_WORD(&ha->iobase->hccr) &
278 HCCR_RISC_PAUSE) != 0)
279 break;
281 udelay(10);
284 /* Select FPM registers. */
285 WRT_REG_WORD(&ha->iobase->ctrl_status, 0x20);
286 RD_REG_WORD(&ha->iobase->ctrl_status);
288 /* Get the fb rev level */
289 ha->fb_rev = RD_FB_CMD_REG(ha, ha->iobase);
291 if (ha->fb_rev == FPM_2300)
292 w &= ~PCI_COMMAND_INVALIDATE;
294 /* Deselect FPM registers. */
295 WRT_REG_WORD(&ha->iobase->ctrl_status, 0x0);
296 RD_REG_WORD(&ha->iobase->ctrl_status);
298 /* Release RISC module. */
299 WRT_REG_WORD(&ha->iobase->hccr, HCCR_RELEASE_RISC);
300 for (cnt = 0; cnt < 30000; cnt++) {
301 if ((RD_REG_WORD(&ha->iobase->hccr) &
302 HCCR_RISC_PAUSE) == 0)
303 break;
305 udelay(10);
308 spin_unlock_irqrestore(&ha->hardware_lock, flags);
312 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
314 /* Reset expansion ROM address decode enable */
315 pci_read_config_word(ha->pdev, PCI_ROM_ADDRESS, &w);
316 w &= ~PCI_ROM_ADDRESS_ENABLE;
317 pci_write_config_word(ha->pdev, PCI_ROM_ADDRESS, w);
319 return (QLA_SUCCESS);
323 * qla2x00_isp_firmware() - Choose firmware image.
324 * @ha: HA context
326 * Returns 0 on success.
328 static int
329 qla2x00_isp_firmware(scsi_qla_host_t *ha)
331 int rval;
333 /* Assume loading risc code */
334 rval = QLA_FUNCTION_FAILED;
336 if (ha->flags.disable_risc_code_load) {
337 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
338 ha->host_no));
339 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
341 /* Verify checksum of loaded RISC code. */
342 rval = qla2x00_verify_checksum(ha);
345 if (rval) {
346 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
347 ha->host_no));
350 return (rval);
354 * qla2x00_reset_chip() - Reset ISP chip.
355 * @ha: HA context
357 * Returns 0 on success.
359 static void
360 qla2x00_reset_chip(scsi_qla_host_t *ha)
362 unsigned long flags = 0;
363 device_reg_t __iomem *reg = ha->iobase;
364 uint32_t cnt;
365 unsigned long mbx_flags = 0;
366 uint16_t cmd;
368 /* Disable ISP interrupts. */
369 qla2x00_disable_intrs(ha);
371 spin_lock_irqsave(&ha->hardware_lock, flags);
373 /* Turn off master enable */
374 cmd = 0;
375 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
376 cmd &= ~PCI_COMMAND_MASTER;
377 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
379 if (!IS_QLA2100(ha)) {
380 /* Pause RISC. */
381 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
382 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
383 for (cnt = 0; cnt < 30000; cnt++) {
384 if ((RD_REG_WORD(&reg->hccr) &
385 HCCR_RISC_PAUSE) != 0)
386 break;
387 udelay(100);
389 } else {
390 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
391 udelay(10);
394 /* Select FPM registers. */
395 WRT_REG_WORD(&reg->ctrl_status, 0x20);
396 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
398 /* FPM Soft Reset. */
399 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
400 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
402 /* Toggle Fpm Reset. */
403 if (!IS_QLA2200(ha)) {
404 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
405 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
408 /* Select frame buffer registers. */
409 WRT_REG_WORD(&reg->ctrl_status, 0x10);
410 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
412 /* Reset frame buffer FIFOs. */
413 if (IS_QLA2200(ha)) {
414 WRT_FB_CMD_REG(ha, reg, 0xa000);
415 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
416 } else {
417 WRT_FB_CMD_REG(ha, reg, 0x00fc);
419 /* Read back fb_cmd until zero or 3 seconds max */
420 for (cnt = 0; cnt < 3000; cnt++) {
421 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
422 break;
423 udelay(100);
427 /* Select RISC module registers. */
428 WRT_REG_WORD(&reg->ctrl_status, 0);
429 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
431 /* Reset RISC processor. */
432 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
433 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
435 /* Release RISC processor. */
436 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
437 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
440 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
441 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
443 /* Reset ISP chip. */
444 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
446 /* Wait for RISC to recover from reset. */
447 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
449 * It is necessary to for a delay here since the card doesn't
450 * respond to PCI reads during a reset. On some architectures
451 * this will result in an MCA.
453 udelay(20);
454 for (cnt = 30000; cnt; cnt--) {
455 if ((RD_REG_WORD(&reg->ctrl_status) &
456 CSR_ISP_SOFT_RESET) == 0)
457 break;
458 udelay(100);
460 } else
461 udelay(10);
463 /* Reset RISC processor. */
464 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
466 WRT_REG_WORD(&reg->semaphore, 0);
468 /* Release RISC processor. */
469 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
470 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
472 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
473 for (cnt = 0; cnt < 30000; cnt++) {
474 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
475 spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
477 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY) {
478 if (!(test_bit(ABORT_ISP_ACTIVE,
479 &ha->dpc_flags)))
480 spin_unlock_irqrestore(
481 &ha->mbx_reg_lock, mbx_flags);
482 break;
485 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
486 spin_unlock_irqrestore(&ha->mbx_reg_lock,
487 mbx_flags);
489 udelay(100);
491 } else
492 udelay(100);
494 /* Turn on master enable */
495 cmd |= PCI_COMMAND_MASTER;
496 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
498 /* Disable RISC pause on FPM parity error. */
499 if (!IS_QLA2100(ha)) {
500 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
501 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
504 spin_unlock_irqrestore(&ha->hardware_lock, flags);
508 * qla2x00_chip_diag() - Test chip for proper operation.
509 * @ha: HA context
511 * Returns 0 on success.
513 static int
514 qla2x00_chip_diag(scsi_qla_host_t *ha)
516 int rval;
517 device_reg_t __iomem *reg = ha->iobase;
518 unsigned long flags = 0;
519 uint16_t data;
520 uint32_t cnt;
521 uint16_t mb[5];
523 /* Assume a failed state */
524 rval = QLA_FUNCTION_FAILED;
526 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
527 ha->host_no, (u_long)&reg->flash_address));
529 spin_lock_irqsave(&ha->hardware_lock, flags);
531 /* Reset ISP chip. */
532 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
535 * We need to have a delay here since the card will not respond while
536 * in reset causing an MCA on some architectures.
538 udelay(20);
539 data = qla2x00_debounce_register(&reg->ctrl_status);
540 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
541 udelay(5);
542 data = RD_REG_WORD(&reg->ctrl_status);
543 barrier();
546 if (!cnt)
547 goto chip_diag_failed;
549 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
550 ha->host_no));
552 /* Reset RISC processor. */
553 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
554 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
556 /* Workaround for QLA2312 PCI parity error */
557 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
558 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
559 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
560 udelay(5);
561 data = RD_MAILBOX_REG(ha, reg, 0);
562 barrier();
564 } else
565 udelay(10);
567 if (!cnt)
568 goto chip_diag_failed;
570 /* Check product ID of chip */
571 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
573 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
574 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
575 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
576 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
577 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
578 mb[3] != PROD_ID_3) {
579 qla_printk(KERN_WARNING, ha,
580 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
582 goto chip_diag_failed;
584 ha->product_id[0] = mb[1];
585 ha->product_id[1] = mb[2];
586 ha->product_id[2] = mb[3];
587 ha->product_id[3] = mb[4];
589 /* Adjust fw RISC transfer size */
590 if (ha->request_q_length > 1024)
591 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
592 else
593 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
594 ha->request_q_length;
596 if (IS_QLA2200(ha) &&
597 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
598 /* Limit firmware transfer size with a 2200A */
599 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
600 ha->host_no));
602 ha->fw_transfer_size = 128;
605 /* Wrap Incoming Mailboxes Test. */
606 spin_unlock_irqrestore(&ha->hardware_lock, flags);
608 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
609 rval = qla2x00_mbx_reg_test(ha);
610 if (rval) {
611 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
612 ha->host_no));
613 qla_printk(KERN_WARNING, ha,
614 "Failed mailbox send register test\n");
616 else {
617 /* Flag a successful rval */
618 rval = QLA_SUCCESS;
620 spin_lock_irqsave(&ha->hardware_lock, flags);
622 chip_diag_failed:
623 if (rval)
624 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
625 "****\n", ha->host_no));
627 spin_unlock_irqrestore(&ha->hardware_lock, flags);
629 return (rval);
633 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
634 * @ha: HA context
636 * Returns 0 on success.
638 static void
639 qla2x00_resize_request_q(scsi_qla_host_t *ha)
641 int rval;
642 uint16_t fw_iocb_cnt = 0;
643 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
644 dma_addr_t request_dma;
645 request_t *request_ring;
647 /* Valid only on recent ISPs. */
648 if (IS_QLA2100(ha) || IS_QLA2200(ha))
649 return;
651 /* Retrieve IOCB counts available to the firmware. */
652 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
653 if (rval)
654 return;
655 /* No point in continuing if current settings are sufficient. */
656 if (fw_iocb_cnt < 1024)
657 return;
658 if (ha->request_q_length >= request_q_length)
659 return;
661 /* Attempt to claim larger area for request queue. */
662 request_ring = dma_alloc_coherent(&ha->pdev->dev,
663 (request_q_length + 1) * sizeof(request_t), &request_dma,
664 GFP_KERNEL);
665 if (request_ring == NULL)
666 return;
668 /* Resize successful, report extensions. */
669 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
670 (ha->fw_memory_size + 1) / 1024);
671 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
672 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
674 /* Clear old allocations. */
675 dma_free_coherent(&ha->pdev->dev,
676 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
677 ha->request_dma);
679 /* Begin using larger queue. */
680 ha->request_q_length = request_q_length;
681 ha->request_ring = request_ring;
682 ha->request_dma = request_dma;
686 * qla2x00_setup_chip() - Load and start RISC firmware.
687 * @ha: HA context
689 * Returns 0 on success.
691 static int
692 qla2x00_setup_chip(scsi_qla_host_t *ha)
694 int rval;
695 uint16_t cnt;
696 uint16_t *risc_code;
697 unsigned long risc_address;
698 unsigned long risc_code_size;
699 int num;
700 int i;
701 uint16_t *req_ring;
702 struct qla_fw_info *fw_iter;
704 rval = QLA_SUCCESS;
706 /* Load firmware sequences */
707 fw_iter = ha->brd_info->fw_info;
708 while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
709 risc_code = fw_iter->fwcode;
710 risc_code_size = *fw_iter->fwlen;
712 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL) {
713 risc_address = *fw_iter->fwstart;
714 } else {
715 /* Extended address */
716 risc_address = *fw_iter->lfwstart;
719 num = 0;
720 rval = 0;
721 while (risc_code_size > 0 && !rval) {
722 cnt = (uint16_t)(ha->fw_transfer_size >> 1);
723 if (cnt > risc_code_size)
724 cnt = risc_code_size;
726 DEBUG7(printk("scsi(%ld): Loading risc segment@ "
727 "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
728 ha->host_no, risc_code, cnt, risc_address));
730 req_ring = (uint16_t *)ha->request_ring;
731 for (i = 0; i < cnt; i++)
732 req_ring[i] = cpu_to_le16(risc_code[i]);
734 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL) {
735 rval = qla2x00_load_ram(ha,
736 ha->request_dma, risc_address, cnt);
737 } else {
738 rval = qla2x00_load_ram_ext(ha,
739 ha->request_dma, risc_address, cnt);
741 if (rval) {
742 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
743 "load segment %d of firmware\n",
744 ha->host_no, num));
745 qla_printk(KERN_WARNING, ha,
746 "[ERROR] Failed to load "
747 "segment %d of firmware\n", num);
749 qla2x00_dump_regs(ha);
750 break;
753 risc_code += cnt;
754 risc_address += cnt;
755 risc_code_size -= cnt;
756 num++;
759 /* Next firmware sequence */
760 fw_iter++;
763 /* Verify checksum of loaded RISC code. */
764 if (!rval) {
765 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
766 "code.\n", ha->host_no));
768 rval = qla2x00_verify_checksum(ha);
769 if (rval == QLA_SUCCESS) {
770 /* Start firmware execution. */
771 DEBUG(printk("scsi(%ld): Checksum OK, start "
772 "firmware.\n", ha->host_no));
774 rval = qla2x00_execute_fw(ha);
775 /* Retrieve firmware information. */
776 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
777 qla2x00_get_fw_version(ha,
778 &ha->fw_major_version,
779 &ha->fw_minor_version,
780 &ha->fw_subminor_version,
781 &ha->fw_attributes, &ha->fw_memory_size);
782 qla2x00_resize_request_q(ha);
784 } else {
785 DEBUG2(printk(KERN_INFO
786 "scsi(%ld): ISP Firmware failed checksum.\n",
787 ha->host_no));
791 if (rval) {
792 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
793 ha->host_no));
796 return (rval);
800 * qla2x00_init_response_q_entries() - Initializes response queue entries.
801 * @ha: HA context
803 * Beginning of request ring has initialization control block already built
804 * by nvram config routine.
806 * Returns 0 on success.
808 static void
809 qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
811 uint16_t cnt;
812 response_t *pkt;
814 pkt = ha->response_ring_ptr;
815 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
816 pkt->signature = RESPONSE_PROCESSED;
817 pkt++;
823 * qla2x00_update_fw_options() - Read and process firmware options.
824 * @ha: HA context
826 * Returns 0 on success.
828 static void
829 qla2x00_update_fw_options(scsi_qla_host_t *ha)
831 uint16_t swing, emphasis, tx_sens, rx_sens;
833 memset(ha->fw_options, 0, sizeof(ha->fw_options));
834 qla2x00_get_fw_options(ha, ha->fw_options);
836 if (IS_QLA2100(ha) || IS_QLA2200(ha))
837 return;
839 /* Serial Link options. */
840 DEBUG3(printk("scsi(%ld): Serial link options:\n",
841 ha->host_no));
842 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
843 sizeof(ha->fw_seriallink_options)));
845 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
846 if (ha->fw_seriallink_options[3] & BIT_2) {
847 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
849 /* 1G settings */
850 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
851 emphasis = (ha->fw_seriallink_options[2] &
852 (BIT_4 | BIT_3)) >> 3;
853 tx_sens = ha->fw_seriallink_options[0] &
854 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
855 rx_sens = (ha->fw_seriallink_options[0] &
856 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
857 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
858 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
859 if (rx_sens == 0x0)
860 rx_sens = 0x3;
861 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
862 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
863 ha->fw_options[10] |= BIT_5 |
864 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
865 (tx_sens & (BIT_1 | BIT_0));
867 /* 2G settings */
868 swing = (ha->fw_seriallink_options[2] &
869 (BIT_7 | BIT_6 | BIT_5)) >> 5;
870 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
871 tx_sens = ha->fw_seriallink_options[1] &
872 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
873 rx_sens = (ha->fw_seriallink_options[1] &
874 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
875 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
876 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
877 if (rx_sens == 0x0)
878 rx_sens = 0x3;
879 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
880 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
881 ha->fw_options[11] |= BIT_5 |
882 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
883 (tx_sens & (BIT_1 | BIT_0));
886 /* FCP2 options. */
887 /* Return command IOCBs without waiting for an ABTS to complete. */
888 ha->fw_options[3] |= BIT_13;
890 /* LED scheme. */
891 if (ha->flags.enable_led_scheme)
892 ha->fw_options[2] |= BIT_12;
894 /* Update firmware options. */
895 qla2x00_set_fw_options(ha, ha->fw_options);
899 * qla2x00_init_rings() - Initializes firmware.
900 * @ha: HA context
902 * Beginning of request ring has initialization control block already built
903 * by nvram config routine.
905 * Returns 0 on success.
907 static int
908 qla2x00_init_rings(scsi_qla_host_t *ha)
910 int rval;
911 unsigned long flags = 0;
912 int cnt;
913 device_reg_t __iomem *reg = ha->iobase;
915 spin_lock_irqsave(&ha->hardware_lock, flags);
917 /* Clear outstanding commands array. */
918 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
919 ha->outstanding_cmds[cnt] = NULL;
921 ha->current_outstanding_cmd = 0;
923 /* Clear RSCN queue. */
924 ha->rscn_in_ptr = 0;
925 ha->rscn_out_ptr = 0;
927 /* Initialize firmware. */
928 ha->request_ring_ptr = ha->request_ring;
929 ha->req_ring_index = 0;
930 ha->req_q_cnt = ha->request_q_length;
931 ha->response_ring_ptr = ha->response_ring;
932 ha->rsp_ring_index = 0;
934 /* Setup ring parameters in initialization control block. */
935 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
936 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
937 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
938 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
939 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
940 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
941 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
942 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
944 /* Initialize response queue entries */
945 qla2x00_init_response_q_entries(ha);
947 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
948 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
949 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
950 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
951 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
953 spin_unlock_irqrestore(&ha->hardware_lock, flags);
955 /* Update any ISP specific firmware options before initialization. */
956 qla2x00_update_fw_options(ha);
958 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
959 rval = qla2x00_init_firmware(ha, sizeof(init_cb_t));
960 if (rval) {
961 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
962 ha->host_no));
963 } else {
964 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
965 ha->host_no));
968 return (rval);
972 * qla2x00_fw_ready() - Waits for firmware ready.
973 * @ha: HA context
975 * Returns 0 on success.
977 static int
978 qla2x00_fw_ready(scsi_qla_host_t *ha)
980 int rval;
981 unsigned long wtime, mtime;
982 uint16_t min_wait; /* Minimum wait time if loop is down */
983 uint16_t wait_time; /* Wait time if loop is coming ready */
984 uint16_t fw_state;
986 rval = QLA_SUCCESS;
988 /* 20 seconds for loop down. */
989 min_wait = 20;
992 * Firmware should take at most one RATOV to login, plus 5 seconds for
993 * our own processing.
995 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
996 wait_time = min_wait;
999 /* Min wait time if loop down */
1000 mtime = jiffies + (min_wait * HZ);
1002 /* wait time before firmware ready */
1003 wtime = jiffies + (wait_time * HZ);
1005 /* Wait for ISP to finish LIP */
1006 if (!ha->flags.init_done)
1007 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1009 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1010 ha->host_no));
1012 do {
1013 rval = qla2x00_get_firmware_state(ha, &fw_state);
1014 if (rval == QLA_SUCCESS) {
1015 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1016 ha->device_flags &= ~DFLG_NO_CABLE;
1018 if (fw_state == FSTATE_READY) {
1019 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1020 ha->host_no));
1022 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1023 &ha->login_timeout, &ha->r_a_tov);
1025 rval = QLA_SUCCESS;
1026 break;
1029 rval = QLA_FUNCTION_FAILED;
1031 if (atomic_read(&ha->loop_down_timer) &&
1032 (fw_state >= FSTATE_LOSS_OF_SYNC ||
1033 fw_state == FSTATE_WAIT_AL_PA)) {
1034 /* Loop down. Timeout on min_wait for states
1035 * other than Wait for Login.
1037 if (time_after_eq(jiffies, mtime)) {
1038 qla_printk(KERN_INFO, ha,
1039 "Cable is unplugged...\n");
1041 ha->device_flags |= DFLG_NO_CABLE;
1042 break;
1045 } else {
1046 /* Mailbox cmd failed. Timeout on min_wait. */
1047 if (time_after_eq(jiffies, mtime))
1048 break;
1051 if (time_after_eq(jiffies, wtime))
1052 break;
1054 /* Delay for a while */
1055 msleep(500);
1057 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1058 ha->host_no, fw_state, jiffies));
1059 } while (1);
1061 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1062 ha->host_no, fw_state, jiffies));
1064 if (rval) {
1065 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1066 ha->host_no));
1069 return (rval);
1073 * qla2x00_configure_hba
1074 * Setup adapter context.
1076 * Input:
1077 * ha = adapter state pointer.
1079 * Returns:
1080 * 0 = success
1082 * Context:
1083 * Kernel context.
1085 static int
1086 qla2x00_configure_hba(scsi_qla_host_t *ha)
1088 int rval;
1089 uint16_t loop_id;
1090 uint16_t topo;
1091 uint8_t al_pa;
1092 uint8_t area;
1093 uint8_t domain;
1094 char connect_type[22];
1096 /* Get host addresses. */
1097 rval = qla2x00_get_adapter_id(ha,
1098 &loop_id, &al_pa, &area, &domain, &topo);
1099 if (rval != QLA_SUCCESS) {
1100 qla_printk(KERN_WARNING, ha,
1101 "ERROR -- Unable to get host loop ID.\n");
1102 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1103 return (rval);
1106 if (topo == 4) {
1107 qla_printk(KERN_INFO, ha,
1108 "Cannot get topology - retrying.\n");
1109 return (QLA_FUNCTION_FAILED);
1112 ha->loop_id = loop_id;
1114 /* initialize */
1115 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1116 ha->operating_mode = LOOP;
1118 switch (topo) {
1119 case 0:
1120 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1121 ha->host_no));
1122 ha->current_topology = ISP_CFG_NL;
1123 strcpy(connect_type, "(Loop)");
1124 break;
1126 case 1:
1127 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1128 ha->host_no));
1129 ha->current_topology = ISP_CFG_FL;
1130 strcpy(connect_type, "(FL_Port)");
1131 break;
1133 case 2:
1134 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1135 ha->host_no));
1136 ha->operating_mode = P2P;
1137 ha->current_topology = ISP_CFG_N;
1138 strcpy(connect_type, "(N_Port-to-N_Port)");
1139 break;
1141 case 3:
1142 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1143 ha->host_no));
1144 ha->operating_mode = P2P;
1145 ha->current_topology = ISP_CFG_F;
1146 strcpy(connect_type, "(F_Port)");
1147 break;
1149 default:
1150 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1151 "Using NL.\n",
1152 ha->host_no, topo));
1153 ha->current_topology = ISP_CFG_NL;
1154 strcpy(connect_type, "(Loop)");
1155 break;
1158 /* Save Host port and loop ID. */
1159 /* byte order - Big Endian */
1160 ha->d_id.b.domain = domain;
1161 ha->d_id.b.area = area;
1162 ha->d_id.b.al_pa = al_pa;
1164 if (!ha->flags.init_done)
1165 qla_printk(KERN_INFO, ha,
1166 "Topology - %s, Host Loop address 0x%x\n",
1167 connect_type, ha->loop_id);
1169 if (rval) {
1170 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1171 } else {
1172 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1175 return(rval);
1179 * NVRAM configuration for ISP 2xxx
1181 * Input:
1182 * ha = adapter block pointer.
1184 * Output:
1185 * initialization control block in response_ring
1186 * host adapters parameters in host adapter block
1188 * Returns:
1189 * 0 = success.
1191 static int
1192 qla2x00_nvram_config(scsi_qla_host_t *ha)
1194 int rval;
1195 uint8_t chksum = 0;
1196 uint16_t cnt;
1197 uint8_t *dptr1, *dptr2;
1198 init_cb_t *icb = ha->init_cb;
1199 nvram_t *nv = (nvram_t *)ha->request_ring;
1200 uint16_t *wptr = (uint16_t *)ha->request_ring;
1201 device_reg_t __iomem *reg = ha->iobase;
1202 uint8_t timer_mode;
1204 rval = QLA_SUCCESS;
1206 /* Determine NVRAM starting address. */
1207 ha->nvram_base = 0;
1208 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1209 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1210 ha->nvram_base = 0x80;
1212 /* Get NVRAM data and calculate checksum. */
1213 qla2x00_lock_nvram_access(ha);
1214 for (cnt = 0; cnt < sizeof(nvram_t)/2; cnt++) {
1215 *wptr = cpu_to_le16(qla2x00_get_nvram_word(ha,
1216 (cnt+ha->nvram_base)));
1217 chksum += (uint8_t)*wptr;
1218 chksum += (uint8_t)(*wptr >> 8);
1219 wptr++;
1221 qla2x00_unlock_nvram_access(ha);
1223 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1224 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1225 sizeof(nvram_t)));
1227 /* Bad NVRAM data, set defaults parameters. */
1228 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1229 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1230 /* Reset NVRAM data. */
1231 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1232 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1233 nv->nvram_version);
1234 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1235 "invalid -- WWPN) defaults.\n");
1238 * Set default initialization control block.
1240 memset(nv, 0, sizeof(nvram_t));
1241 nv->parameter_block_version = ICB_VERSION;
1243 if (IS_QLA23XX(ha)) {
1244 nv->firmware_options[0] = BIT_2 | BIT_1;
1245 nv->firmware_options[1] = BIT_7 | BIT_5;
1246 nv->add_firmware_options[0] = BIT_5;
1247 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1248 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1249 nv->special_options[1] = BIT_7;
1250 } else if (IS_QLA2200(ha)) {
1251 nv->firmware_options[0] = BIT_2 | BIT_1;
1252 nv->firmware_options[1] = BIT_7 | BIT_5;
1253 nv->add_firmware_options[0] = BIT_5;
1254 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1255 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1256 } else if (IS_QLA2100(ha)) {
1257 nv->firmware_options[0] = BIT_3 | BIT_1;
1258 nv->firmware_options[1] = BIT_5;
1259 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1262 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1263 nv->execution_throttle = __constant_cpu_to_le16(16);
1264 nv->retry_count = 8;
1265 nv->retry_delay = 1;
1267 nv->port_name[0] = 33;
1268 nv->port_name[3] = 224;
1269 nv->port_name[4] = 139;
1271 nv->login_timeout = 4;
1274 * Set default host adapter parameters
1276 nv->host_p[1] = BIT_2;
1277 nv->reset_delay = 5;
1278 nv->port_down_retry_count = 8;
1279 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1280 nv->link_down_timeout = 60;
1282 rval = 1;
1285 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1287 * The SN2 does not provide BIOS emulation which means you can't change
1288 * potentially bogus BIOS settings. Force the use of default settings
1289 * for link rate and frame size. Hope that the rest of the settings
1290 * are valid.
1292 if (ia64_platform_is("sn2")) {
1293 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1294 if (IS_QLA23XX(ha))
1295 nv->special_options[1] = BIT_7;
1297 #endif
1299 /* Reset Initialization control block */
1300 memset(icb, 0, sizeof(init_cb_t));
1303 * Setup driver NVRAM options.
1305 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1306 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1307 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1308 nv->firmware_options[1] &= ~BIT_4;
1310 if (IS_QLA23XX(ha)) {
1311 nv->firmware_options[0] |= BIT_2;
1312 nv->firmware_options[0] &= ~BIT_3;
1314 if (IS_QLA2300(ha)) {
1315 if (ha->fb_rev == FPM_2310) {
1316 strcpy(ha->model_number, "QLA2310");
1317 } else {
1318 strcpy(ha->model_number, "QLA2300");
1320 } else {
1321 if (rval == 0 &&
1322 memcmp(nv->model_number, BINZERO,
1323 sizeof(nv->model_number)) != 0) {
1324 char *st, *en;
1326 strncpy(ha->model_number, nv->model_number,
1327 sizeof(nv->model_number));
1328 st = en = ha->model_number;
1329 en += sizeof(nv->model_number) - 1;
1330 while (en > st) {
1331 if (*en != 0x20 && *en != 0x00)
1332 break;
1333 *en-- = '\0';
1335 } else {
1336 uint16_t index;
1338 index = (ha->pdev->subsystem_device & 0xff);
1339 if (index < QLA_MODEL_NAMES) {
1340 strcpy(ha->model_number,
1341 qla2x00_model_name[index]);
1342 ha->model_desc =
1343 qla2x00_model_desc[index];
1344 } else {
1345 strcpy(ha->model_number, "QLA23xx");
1349 } else if (IS_QLA2200(ha)) {
1350 nv->firmware_options[0] |= BIT_2;
1352 * 'Point-to-point preferred, else loop' is not a safe
1353 * connection mode setting.
1355 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1356 (BIT_5 | BIT_4)) {
1357 /* Force 'loop preferred, else point-to-point'. */
1358 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1359 nv->add_firmware_options[0] |= BIT_5;
1361 strcpy(ha->model_number, "QLA22xx");
1362 } else /*if (IS_QLA2100(ha))*/ {
1363 strcpy(ha->model_number, "QLA2100");
1367 * Copy over NVRAM RISC parameter block to initialization control block.
1369 dptr1 = (uint8_t *)icb;
1370 dptr2 = (uint8_t *)&nv->parameter_block_version;
1371 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1372 while (cnt--)
1373 *dptr1++ = *dptr2++;
1375 /* Copy 2nd half. */
1376 dptr1 = (uint8_t *)icb->add_firmware_options;
1377 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1378 while (cnt--)
1379 *dptr1++ = *dptr2++;
1381 /* Prepare nodename */
1382 if ((icb->firmware_options[1] & BIT_6) == 0) {
1384 * Firmware will apply the following mask if the nodename was
1385 * not provided.
1387 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1388 icb->node_name[0] &= 0xF0;
1392 * Set host adapter parameters.
1394 ha->nvram_version = nv->nvram_version;
1396 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1397 /* Always load RISC code on non ISP2[12]00 chips. */
1398 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1399 ha->flags.disable_risc_code_load = 0;
1400 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1401 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1402 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1403 ha->flags.enable_led_scheme = ((nv->efi_parameters & BIT_3) ? 1 : 0);
1405 ha->operating_mode =
1406 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1408 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1409 sizeof(ha->fw_seriallink_options));
1411 /* save HBA serial number */
1412 ha->serial0 = icb->port_name[5];
1413 ha->serial1 = icb->port_name[6];
1414 ha->serial2 = icb->port_name[7];
1415 memcpy(ha->node_name, icb->node_name, WWN_SIZE);
1417 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1419 ha->retry_count = nv->retry_count;
1421 /* Set minimum login_timeout to 4 seconds. */
1422 if (nv->login_timeout < ql2xlogintimeout)
1423 nv->login_timeout = ql2xlogintimeout;
1424 if (nv->login_timeout < 4)
1425 nv->login_timeout = 4;
1426 ha->login_timeout = nv->login_timeout;
1427 icb->login_timeout = nv->login_timeout;
1429 /* Set minimum RATOV to 200 tenths of a second. */
1430 ha->r_a_tov = 200;
1432 ha->minimum_timeout =
1433 (ha->login_timeout * ha->retry_count) + nv->port_down_retry_count;
1434 ha->loop_reset_delay = nv->reset_delay;
1436 /* Will get the value from NVRAM. */
1437 ha->loop_down_timeout = LOOP_DOWN_TIMEOUT;
1439 /* Link Down Timeout = 0:
1441 * When Port Down timer expires we will start returning
1442 * I/O's to OS with "DID_NO_CONNECT".
1444 * Link Down Timeout != 0:
1446 * The driver waits for the link to come up after link down
1447 * before returning I/Os to OS with "DID_NO_CONNECT".
1449 if (nv->link_down_timeout == 0) {
1450 ha->loop_down_abort_time =
1451 (LOOP_DOWN_TIME - ha->loop_down_timeout);
1452 } else {
1453 ha->link_down_timeout = nv->link_down_timeout;
1454 ha->loop_down_abort_time =
1455 (LOOP_DOWN_TIME - ha->link_down_timeout);
1458 ha->max_luns = MAX_LUNS;
1459 ha->max_probe_luns = le16_to_cpu(nv->max_luns_per_target);
1460 if (ha->max_probe_luns == 0)
1461 ha->max_probe_luns = MIN_LUNS;
1464 * Need enough time to try and get the port back.
1466 ha->port_down_retry_count = nv->port_down_retry_count;
1467 if (qlport_down_retry)
1468 ha->port_down_retry_count = qlport_down_retry;
1469 /* Set login_retry_count */
1470 ha->login_retry_count = nv->retry_count;
1471 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1472 ha->port_down_retry_count > 3)
1473 ha->login_retry_count = ha->port_down_retry_count;
1474 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1475 ha->login_retry_count = ha->port_down_retry_count;
1476 if (ql2xloginretrycount)
1477 ha->login_retry_count = ql2xloginretrycount;
1479 ha->binding_type = Bind;
1480 if (ha->binding_type != BIND_BY_PORT_NAME &&
1481 ha->binding_type != BIND_BY_PORT_ID) {
1482 qla_printk(KERN_WARNING, ha,
1483 "Invalid binding type specified (%d), "
1484 "defaulting to BIND_BY_PORT_NAME!!!\n", ha->binding_type);
1486 ha->binding_type = BIND_BY_PORT_NAME;
1489 icb->lun_enables = __constant_cpu_to_le16(0);
1490 icb->command_resource_count = 0;
1491 icb->immediate_notify_resource_count = 0;
1492 icb->timeout = __constant_cpu_to_le16(0);
1494 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1495 /* Enable RIO */
1496 icb->firmware_options[0] &= ~BIT_3;
1497 icb->add_firmware_options[0] &=
1498 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1499 icb->add_firmware_options[0] |= BIT_2;
1500 icb->response_accumulation_timer = 3;
1501 icb->interrupt_delay_timer = 5;
1503 ha->flags.process_response_queue = 1;
1504 } else {
1505 /* Enable ZIO -- Support mode 5 only. */
1506 timer_mode = icb->add_firmware_options[0] &
1507 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1508 icb->add_firmware_options[0] &=
1509 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1510 if (ql2xenablezio)
1511 timer_mode = BIT_2 | BIT_0;
1512 if (timer_mode == (BIT_2 | BIT_0)) {
1513 DEBUG2(printk("scsi(%ld): ZIO enabled; timer delay "
1514 "(%d).\n", ha->host_no, ql2xintrdelaytimer));
1515 qla_printk(KERN_INFO, ha,
1516 "ZIO enabled; timer delay (%d).\n",
1517 ql2xintrdelaytimer);
1519 icb->add_firmware_options[0] |= timer_mode;
1520 icb->interrupt_delay_timer = ql2xintrdelaytimer;
1521 ha->flags.process_response_queue = 1;
1525 if (rval) {
1526 DEBUG2_3(printk(KERN_WARNING
1527 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1529 return (rval);
1533 * qla2x00_init_tgt_map
1534 * Initializes target map.
1536 * Input:
1537 * ha = adapter block pointer.
1539 * Output:
1540 * TGT_Q initialized
1542 static void
1543 qla2x00_init_tgt_map(scsi_qla_host_t *ha)
1545 uint32_t t;
1547 for (t = 0; t < MAX_TARGETS; t++)
1548 TGT_Q(ha, t) = (os_tgt_t *)NULL;
1552 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1553 * @ha: HA context
1554 * @flags: allocation flags
1556 * Returns a pointer to the allocated fcport, or NULL, if none available.
1558 fc_port_t *
1559 qla2x00_alloc_fcport(scsi_qla_host_t *ha, int flags)
1561 fc_port_t *fcport;
1563 fcport = kmalloc(sizeof(fc_port_t), flags);
1564 if (fcport == NULL)
1565 return (fcport);
1567 /* Setup fcport template structure. */
1568 memset(fcport, 0, sizeof (fc_port_t));
1569 fcport->ha = ha;
1570 fcport->port_type = FCT_UNKNOWN;
1571 fcport->loop_id = FC_NO_LOOP_ID;
1572 fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
1573 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1574 fcport->flags = FCF_RLC_SUPPORT;
1575 INIT_LIST_HEAD(&fcport->fcluns);
1577 return (fcport);
1581 * qla2x00_configure_loop
1582 * Updates Fibre Channel Device Database with what is actually on loop.
1584 * Input:
1585 * ha = adapter block pointer.
1587 * Returns:
1588 * 0 = success.
1589 * 1 = error.
1590 * 2 = database was full and device was not configured.
1592 static int
1593 qla2x00_configure_loop(scsi_qla_host_t *ha)
1595 int rval;
1596 unsigned long flags, save_flags;
1598 rval = QLA_SUCCESS;
1600 /* Get Initiator ID */
1601 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1602 rval = qla2x00_configure_hba(ha);
1603 if (rval != QLA_SUCCESS) {
1604 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1605 ha->host_no));
1606 return (rval);
1610 save_flags = flags = ha->dpc_flags;
1611 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1612 ha->host_no, flags));
1615 * If we have both an RSCN and PORT UPDATE pending then handle them
1616 * both at the same time.
1618 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1619 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1620 ha->mem_err = 0 ;
1622 /* Determine what we need to do */
1623 if (ha->current_topology == ISP_CFG_FL &&
1624 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1626 ha->flags.rscn_queue_overflow = 1;
1627 set_bit(RSCN_UPDATE, &flags);
1629 } else if (ha->current_topology == ISP_CFG_F &&
1630 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1632 ha->flags.rscn_queue_overflow = 1;
1633 set_bit(RSCN_UPDATE, &flags);
1634 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1636 } else if (!ha->flags.online ||
1637 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1639 ha->flags.rscn_queue_overflow = 1;
1640 set_bit(RSCN_UPDATE, &flags);
1641 set_bit(LOCAL_LOOP_UPDATE, &flags);
1644 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1645 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1646 rval = QLA_FUNCTION_FAILED;
1647 } else {
1648 rval = qla2x00_configure_local_loop(ha);
1652 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1653 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1654 rval = QLA_FUNCTION_FAILED;
1655 } else {
1656 rval = qla2x00_configure_fabric(ha);
1660 if (rval == QLA_SUCCESS) {
1661 if (atomic_read(&ha->loop_down_timer) ||
1662 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1663 rval = QLA_FUNCTION_FAILED;
1664 } else {
1665 qla2x00_config_os(ha);
1666 atomic_set(&ha->loop_state, LOOP_READY);
1668 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1672 if (rval) {
1673 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1674 __func__, ha->host_no));
1675 } else {
1676 DEBUG3(printk("%s: exiting normally\n", __func__));
1679 /* Restore state if a resync event occured during processing */
1680 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1681 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1682 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1683 if (test_bit(RSCN_UPDATE, &save_flags))
1684 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1687 return (rval);
1693 * qla2x00_configure_local_loop
1694 * Updates Fibre Channel Device Database with local loop devices.
1696 * Input:
1697 * ha = adapter block pointer.
1699 * Returns:
1700 * 0 = success.
1702 static int
1703 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1705 int rval, rval2;
1706 int found_devs;
1707 int found;
1708 fc_port_t *fcport, *new_fcport;
1710 uint16_t index;
1711 uint16_t entries;
1712 char *id_iter;
1713 uint16_t loop_id;
1714 uint8_t domain, area, al_pa;
1716 found_devs = 0;
1717 new_fcport = NULL;
1718 entries = MAX_FIBRE_DEVICES;
1720 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1721 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1723 /* Get list of logged in devices. */
1724 memset(ha->gid_list, 0, GID_LIST_SIZE);
1725 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1726 &entries);
1727 if (rval != QLA_SUCCESS)
1728 goto cleanup_allocation;
1730 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1731 ha->host_no, entries));
1732 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1733 entries * sizeof(struct gid_list_info)));
1735 /* Allocate temporary fcport for any new fcports discovered. */
1736 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1737 if (new_fcport == NULL) {
1738 rval = QLA_MEMORY_ALLOC_FAILED;
1739 goto cleanup_allocation;
1741 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1744 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1746 list_for_each_entry(fcport, &ha->fcports, list) {
1747 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1748 fcport->port_type != FCT_BROADCAST &&
1749 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1751 DEBUG(printk("scsi(%ld): Marking port lost, "
1752 "loop_id=0x%04x\n",
1753 ha->host_no, fcport->loop_id));
1755 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1756 fcport->flags &= ~FCF_FARP_DONE;
1760 /* Add devices to port list. */
1761 id_iter = (char *)ha->gid_list;
1762 for (index = 0; index < entries; index++) {
1763 domain = ((struct gid_list_info *)id_iter)->domain;
1764 area = ((struct gid_list_info *)id_iter)->area;
1765 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1766 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1767 loop_id = (uint16_t)
1768 ((struct gid_list_info *)id_iter)->loop_id_2100;
1769 id_iter += 4;
1770 } else {
1771 loop_id = le16_to_cpu(
1772 ((struct gid_list_info *)id_iter)->loop_id);
1773 id_iter += 6;
1776 /* Bypass reserved domain fields. */
1777 if ((domain & 0xf0) == 0xf0)
1778 continue;
1780 /* Bypass if not same domain and area of adapter. */
1781 if (area != ha->d_id.b.area || domain != ha->d_id.b.domain)
1782 continue;
1784 /* Bypass invalid local loop ID. */
1785 if (loop_id > LAST_LOCAL_LOOP_ID)
1786 continue;
1788 /* Fill in member data. */
1789 new_fcport->d_id.b.domain = domain;
1790 new_fcport->d_id.b.area = area;
1791 new_fcport->d_id.b.al_pa = al_pa;
1792 new_fcport->loop_id = loop_id;
1793 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1794 if (rval2 != QLA_SUCCESS) {
1795 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1796 "information -- get_port_database=%x, "
1797 "loop_id=0x%04x\n",
1798 ha->host_no, rval2, new_fcport->loop_id));
1799 continue;
1802 /* Check for matching device in port list. */
1803 found = 0;
1804 fcport = NULL;
1805 list_for_each_entry(fcport, &ha->fcports, list) {
1806 if (memcmp(new_fcport->port_name, fcport->port_name,
1807 WWN_SIZE))
1808 continue;
1810 fcport->flags &= ~(FCF_FABRIC_DEVICE |
1811 FCF_PERSISTENT_BOUND);
1812 fcport->loop_id = new_fcport->loop_id;
1813 fcport->port_type = new_fcport->port_type;
1814 fcport->d_id.b24 = new_fcport->d_id.b24;
1815 memcpy(fcport->node_name, new_fcport->node_name,
1816 WWN_SIZE);
1818 found++;
1819 break;
1822 if (!found) {
1823 /* New device, add to fcports list. */
1824 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
1825 list_add_tail(&new_fcport->list, &ha->fcports);
1827 /* Allocate a new replacement fcport. */
1828 fcport = new_fcport;
1829 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1830 if (new_fcport == NULL) {
1831 rval = QLA_MEMORY_ALLOC_FAILED;
1832 goto cleanup_allocation;
1834 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1837 qla2x00_update_fcport(ha, fcport);
1839 found_devs++;
1842 cleanup_allocation:
1843 if (new_fcport)
1844 kfree(new_fcport);
1846 if (rval != QLA_SUCCESS) {
1847 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
1848 "rval=%x\n", ha->host_no, rval));
1851 if (found_devs) {
1852 ha->device_flags |= DFLG_LOCAL_DEVICES;
1853 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
1856 return (rval);
1859 static void
1860 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
1862 fc_port_t *fcport;
1864 qla2x00_mark_all_devices_lost(ha);
1865 list_for_each_entry(fcport, &ha->fcports, list) {
1866 if (fcport->port_type != FCT_TARGET)
1867 continue;
1869 qla2x00_update_fcport(ha, fcport);
1874 * qla2x00_update_fcport
1875 * Updates device on list.
1877 * Input:
1878 * ha = adapter block pointer.
1879 * fcport = port structure pointer.
1881 * Return:
1882 * 0 - Success
1883 * BIT_0 - error
1885 * Context:
1886 * Kernel context.
1888 static void
1889 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
1891 uint16_t index;
1892 unsigned long flags;
1893 srb_t *sp;
1895 fcport->ha = ha;
1896 fcport->login_retry = 0;
1897 fcport->port_login_retry_count = ha->port_down_retry_count *
1898 PORT_RETRY_TIME;
1899 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
1900 PORT_RETRY_TIME);
1901 fcport->flags &= ~FCF_LOGIN_NEEDED;
1904 * Check for outstanding cmd on tape Bypass LUN discovery if active
1905 * command on tape.
1907 if (fcport->flags & FCF_TAPE_PRESENT) {
1908 spin_lock_irqsave(&ha->hardware_lock, flags);
1909 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1910 if ((sp = ha->outstanding_cmds[index]) != 0) {
1911 if (sp->fclun->fcport == fcport) {
1912 atomic_set(&fcport->state, FCS_ONLINE);
1913 spin_unlock_irqrestore(
1914 &ha->hardware_lock, flags);
1915 return;
1919 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1922 /* Do LUN discovery. */
1923 if (fcport->port_type == FCT_INITIATOR ||
1924 fcport->port_type == FCT_BROADCAST) {
1925 fcport->device_type = TYPE_PROCESSOR;
1926 } else {
1927 qla2x00_lun_discovery(ha, fcport);
1929 atomic_set(&fcport->state, FCS_ONLINE);
1933 * qla2x00_lun_discovery
1934 * Issue SCSI inquiry command for LUN discovery.
1936 * Input:
1937 * ha: adapter state pointer.
1938 * fcport: FC port structure pointer.
1940 * Context:
1941 * Kernel context.
1943 static void
1944 qla2x00_lun_discovery(scsi_qla_host_t *ha, fc_port_t *fcport)
1946 inq_cmd_rsp_t *inq;
1947 dma_addr_t inq_dma;
1948 uint16_t lun;
1950 inq = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &inq_dma);
1951 if (inq == NULL) {
1952 qla_printk(KERN_WARNING, ha,
1953 "Memory Allocation failed - INQ\n");
1954 return;
1957 /* Always add a fc_lun_t structure for lun 0 -- mid-layer requirement */
1958 qla2x00_add_lun(fcport, 0);
1960 /* If report LUN works, exit. */
1961 if (qla2x00_rpt_lun_discovery(ha, fcport, inq, inq_dma) !=
1962 QLA_SUCCESS) {
1963 for (lun = 0; lun < ha->max_probe_luns; lun++) {
1964 /* Configure LUN. */
1965 qla2x00_cfg_lun(ha, fcport, lun, inq, inq_dma);
1969 dma_pool_free(ha->s_dma_pool, inq, inq_dma);
1973 * qla2x00_rpt_lun_discovery
1974 * Issue SCSI report LUN command for LUN discovery.
1976 * Input:
1977 * ha: adapter state pointer.
1978 * fcport: FC port structure pointer.
1980 * Returns:
1981 * qla2x00 local function return status code.
1983 * Context:
1984 * Kernel context.
1986 static int
1987 qla2x00_rpt_lun_discovery(scsi_qla_host_t *ha, fc_port_t *fcport,
1988 inq_cmd_rsp_t *inq, dma_addr_t inq_dma)
1990 int rval;
1991 uint32_t len, cnt;
1992 uint16_t lun;
1994 /* Assume a failed status */
1995 rval = QLA_FUNCTION_FAILED;
1997 /* No point in continuing if the device doesn't support RLC */
1998 if ((fcport->flags & FCF_RLC_SUPPORT) == 0)
1999 return (rval);
2001 rval = qla2x00_report_lun(ha, fcport);
2002 if (rval != QLA_SUCCESS)
2003 return (rval);
2005 /* Configure LUN list. */
2006 len = be32_to_cpu(ha->rlc_rsp->list.hdr.len);
2007 len /= 8;
2008 for (cnt = 0; cnt < len; cnt++) {
2009 lun = CHAR_TO_SHORT(ha->rlc_rsp->list.lst[cnt].lsb,
2010 ha->rlc_rsp->list.lst[cnt].msb.b);
2012 DEBUG3(printk("scsi(%ld): RLC lun = (%d)\n", ha->host_no, lun));
2014 /* We only support 0 through MAX_LUNS-1 range */
2015 if (lun < MAX_LUNS) {
2016 qla2x00_cfg_lun(ha, fcport, lun, inq, inq_dma);
2019 atomic_set(&fcport->state, FCS_ONLINE);
2021 return (rval);
2025 * qla2x00_report_lun
2026 * Issue SCSI report LUN command.
2028 * Input:
2029 * ha: adapter state pointer.
2030 * fcport: FC port structure pointer.
2032 * Returns:
2033 * qla2x00 local function return status code.
2035 * Context:
2036 * Kernel context.
2038 static int
2039 qla2x00_report_lun(scsi_qla_host_t *ha, fc_port_t *fcport)
2041 int rval;
2042 uint16_t retries;
2043 uint16_t comp_status;
2044 uint16_t scsi_status;
2045 rpt_lun_cmd_rsp_t *rlc;
2046 dma_addr_t rlc_dma;
2048 rval = QLA_FUNCTION_FAILED;
2049 rlc = ha->rlc_rsp;
2050 rlc_dma = ha->rlc_rsp_dma;
2052 for (retries = 3; retries; retries--) {
2053 memset(rlc, 0, sizeof(rpt_lun_cmd_rsp_t));
2054 rlc->p.cmd.entry_type = COMMAND_A64_TYPE;
2055 rlc->p.cmd.entry_count = 1;
2056 SET_TARGET_ID(ha, rlc->p.cmd.target, fcport->loop_id);
2057 rlc->p.cmd.control_flags =
2058 __constant_cpu_to_le16(CF_READ | CF_SIMPLE_TAG);
2059 rlc->p.cmd.scsi_cdb[0] = REPORT_LUNS;
2060 rlc->p.cmd.scsi_cdb[8] = MSB(sizeof(rpt_lun_lst_t));
2061 rlc->p.cmd.scsi_cdb[9] = LSB(sizeof(rpt_lun_lst_t));
2062 rlc->p.cmd.dseg_count = __constant_cpu_to_le16(1);
2063 rlc->p.cmd.timeout = __constant_cpu_to_le16(10);
2064 rlc->p.cmd.byte_count =
2065 __constant_cpu_to_le32(sizeof(rpt_lun_lst_t));
2066 rlc->p.cmd.dseg_0_address[0] = cpu_to_le32(
2067 LSD(rlc_dma + sizeof(sts_entry_t)));
2068 rlc->p.cmd.dseg_0_address[1] = cpu_to_le32(
2069 MSD(rlc_dma + sizeof(sts_entry_t)));
2070 rlc->p.cmd.dseg_0_length =
2071 __constant_cpu_to_le32(sizeof(rpt_lun_lst_t));
2073 rval = qla2x00_issue_iocb(ha, rlc, rlc_dma,
2074 sizeof(rpt_lun_cmd_rsp_t));
2076 comp_status = le16_to_cpu(rlc->p.rsp.comp_status);
2077 scsi_status = le16_to_cpu(rlc->p.rsp.scsi_status);
2079 if (rval != QLA_SUCCESS || comp_status != CS_COMPLETE ||
2080 scsi_status & SS_CHECK_CONDITION) {
2082 /* Device underrun, treat as OK. */
2083 if (rval == QLA_SUCCESS &&
2084 comp_status == CS_DATA_UNDERRUN &&
2085 scsi_status & SS_RESIDUAL_UNDER) {
2087 rval = QLA_SUCCESS;
2088 break;
2091 DEBUG(printk("scsi(%ld): RLC failed to issue iocb! "
2092 "fcport=[%04x/%p] rval=%x cs=%x ss=%x\n",
2093 ha->host_no, fcport->loop_id, fcport, rval,
2094 comp_status, scsi_status));
2096 rval = QLA_FUNCTION_FAILED;
2097 if (scsi_status & SS_CHECK_CONDITION) {
2098 DEBUG2(printk("scsi(%ld): RLC "
2099 "SS_CHECK_CONDITION Sense Data "
2100 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
2101 ha->host_no,
2102 rlc->p.rsp.req_sense_data[0],
2103 rlc->p.rsp.req_sense_data[1],
2104 rlc->p.rsp.req_sense_data[2],
2105 rlc->p.rsp.req_sense_data[3],
2106 rlc->p.rsp.req_sense_data[4],
2107 rlc->p.rsp.req_sense_data[5],
2108 rlc->p.rsp.req_sense_data[6],
2109 rlc->p.rsp.req_sense_data[7]));
2110 if (rlc->p.rsp.req_sense_data[2] ==
2111 ILLEGAL_REQUEST) {
2112 fcport->flags &= ~(FCF_RLC_SUPPORT);
2113 break;
2116 } else {
2117 break;
2121 return (rval);
2125 * qla2x00_cfg_lun
2126 * Configures LUN into fcport LUN list.
2128 * Input:
2129 * fcport: FC port structure pointer.
2130 * lun: LUN number.
2132 * Context:
2133 * Kernel context.
2135 static fc_lun_t *
2136 qla2x00_cfg_lun(scsi_qla_host_t *ha, fc_port_t *fcport, uint16_t lun,
2137 inq_cmd_rsp_t *inq, dma_addr_t inq_dma)
2139 fc_lun_t *fclun;
2140 uint8_t device_type;
2142 /* Bypass LUNs that failed. */
2143 if (qla2x00_inquiry(ha, fcport, lun, inq, inq_dma) != QLA_SUCCESS) {
2144 DEBUG2(printk("scsi(%ld): Failed inquiry - loop id=0x%04x "
2145 "lun=%d\n", ha->host_no, fcport->loop_id, lun));
2147 return (NULL);
2149 device_type = (inq->inq[0] & 0x1f);
2150 switch (device_type) {
2151 case TYPE_DISK:
2152 case TYPE_PROCESSOR:
2153 case TYPE_WORM:
2154 case TYPE_ROM:
2155 case TYPE_SCANNER:
2156 case TYPE_MOD:
2157 case TYPE_MEDIUM_CHANGER:
2158 case TYPE_ENCLOSURE:
2159 case 0x20:
2160 case 0x0C:
2161 break;
2162 case TYPE_TAPE:
2163 fcport->flags |= FCF_TAPE_PRESENT;
2164 break;
2165 default:
2166 DEBUG2(printk("scsi(%ld): Unsupported lun type -- "
2167 "loop id=0x%04x lun=%d type=%x\n",
2168 ha->host_no, fcport->loop_id, lun, device_type));
2169 return (NULL);
2172 fcport->device_type = device_type;
2173 fclun = qla2x00_add_lun(fcport, lun);
2175 if (fclun != NULL) {
2176 atomic_set(&fcport->state, FCS_ONLINE);
2179 return (fclun);
2183 * qla2x00_add_lun
2184 * Adds LUN to database
2186 * Input:
2187 * fcport: FC port structure pointer.
2188 * lun: LUN number.
2190 * Context:
2191 * Kernel context.
2193 static fc_lun_t *
2194 qla2x00_add_lun(fc_port_t *fcport, uint16_t lun)
2196 int found;
2197 fc_lun_t *fclun;
2199 if (fcport == NULL) {
2200 DEBUG(printk("scsi: Unable to add lun to NULL port\n"));
2201 return (NULL);
2204 /* Allocate LUN if not already allocated. */
2205 found = 0;
2206 list_for_each_entry(fclun, &fcport->fcluns, list) {
2207 if (fclun->lun == lun) {
2208 found++;
2209 break;
2212 if (found)
2213 return (NULL);
2215 fclun = kmalloc(sizeof(fc_lun_t), GFP_ATOMIC);
2216 if (fclun == NULL) {
2217 printk(KERN_WARNING
2218 "%s(): Memory Allocation failed - FCLUN\n",
2219 __func__);
2220 return (NULL);
2223 /* Setup LUN structure. */
2224 memset(fclun, 0, sizeof(fc_lun_t));
2225 fclun->lun = lun;
2226 fclun->fcport = fcport;
2227 fclun->o_fcport = fcport;
2228 fclun->device_type = fcport->device_type;
2229 atomic_set(&fcport->state, FCS_UNCONFIGURED);
2231 list_add_tail(&fclun->list, &fcport->fcluns);
2233 return (fclun);
2237 * qla2x00_inquiry
2238 * Issue SCSI inquiry command.
2240 * Input:
2241 * ha = adapter block pointer.
2242 * fcport = FC port structure pointer.
2244 * Return:
2245 * 0 - Success
2246 * BIT_0 - error
2248 * Context:
2249 * Kernel context.
2251 static int
2252 qla2x00_inquiry(scsi_qla_host_t *ha,
2253 fc_port_t *fcport, uint16_t lun, inq_cmd_rsp_t *inq, dma_addr_t inq_dma)
2255 int rval;
2256 uint16_t retries;
2257 uint16_t comp_status;
2258 uint16_t scsi_status;
2260 rval = QLA_FUNCTION_FAILED;
2262 for (retries = 3; retries; retries--) {
2263 memset(inq, 0, sizeof(inq_cmd_rsp_t));
2264 inq->p.cmd.entry_type = COMMAND_A64_TYPE;
2265 inq->p.cmd.entry_count = 1;
2266 inq->p.cmd.lun = cpu_to_le16(lun);
2267 SET_TARGET_ID(ha, inq->p.cmd.target, fcport->loop_id);
2268 inq->p.cmd.control_flags =
2269 __constant_cpu_to_le16(CF_READ | CF_SIMPLE_TAG);
2270 inq->p.cmd.scsi_cdb[0] = INQUIRY;
2271 inq->p.cmd.scsi_cdb[4] = INQ_DATA_SIZE;
2272 inq->p.cmd.dseg_count = __constant_cpu_to_le16(1);
2273 inq->p.cmd.timeout = __constant_cpu_to_le16(10);
2274 inq->p.cmd.byte_count =
2275 __constant_cpu_to_le32(INQ_DATA_SIZE);
2276 inq->p.cmd.dseg_0_address[0] = cpu_to_le32(
2277 LSD(inq_dma + sizeof(sts_entry_t)));
2278 inq->p.cmd.dseg_0_address[1] = cpu_to_le32(
2279 MSD(inq_dma + sizeof(sts_entry_t)));
2280 inq->p.cmd.dseg_0_length =
2281 __constant_cpu_to_le32(INQ_DATA_SIZE);
2283 DEBUG5(printk("scsi(%ld): Lun Inquiry - fcport=[%04x/%p],"
2284 " lun (%d)\n",
2285 ha->host_no, fcport->loop_id, fcport, lun));
2287 rval = qla2x00_issue_iocb(ha, inq, inq_dma,
2288 sizeof(inq_cmd_rsp_t));
2290 comp_status = le16_to_cpu(inq->p.rsp.comp_status);
2291 scsi_status = le16_to_cpu(inq->p.rsp.scsi_status);
2293 DEBUG5(printk("scsi(%ld): lun (%d) inquiry - "
2294 "inq[0]= 0x%x, comp status 0x%x, scsi status 0x%x, "
2295 "rval=%d\n",
2296 ha->host_no, lun, inq->inq[0], comp_status, scsi_status,
2297 rval));
2299 if (rval != QLA_SUCCESS || comp_status != CS_COMPLETE ||
2300 scsi_status & SS_CHECK_CONDITION) {
2302 DEBUG(printk("scsi(%ld): INQ failed to issue iocb! "
2303 "fcport=[%04x/%p] rval=%x cs=%x ss=%x\n",
2304 ha->host_no, fcport->loop_id, fcport, rval,
2305 comp_status, scsi_status));
2307 if (rval == QLA_SUCCESS)
2308 rval = QLA_FUNCTION_FAILED;
2310 if (scsi_status & SS_CHECK_CONDITION) {
2311 DEBUG2(printk("scsi(%ld): INQ "
2312 "SS_CHECK_CONDITION Sense Data "
2313 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
2314 ha->host_no,
2315 inq->p.rsp.req_sense_data[0],
2316 inq->p.rsp.req_sense_data[1],
2317 inq->p.rsp.req_sense_data[2],
2318 inq->p.rsp.req_sense_data[3],
2319 inq->p.rsp.req_sense_data[4],
2320 inq->p.rsp.req_sense_data[5],
2321 inq->p.rsp.req_sense_data[6],
2322 inq->p.rsp.req_sense_data[7]));
2325 /* Device underrun drop LUN. */
2326 if (comp_status == CS_DATA_UNDERRUN &&
2327 scsi_status & SS_RESIDUAL_UNDER) {
2328 break;
2330 } else {
2331 break;
2335 return (rval);
2340 * qla2x00_configure_fabric
2341 * Setup SNS devices with loop ID's.
2343 * Input:
2344 * ha = adapter block pointer.
2346 * Returns:
2347 * 0 = success.
2348 * BIT_0 = error
2350 static int
2351 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2353 int rval, rval2;
2354 fc_port_t *fcport, *fcptemp;
2355 uint16_t next_loopid;
2356 uint16_t mb[MAILBOX_REGISTER_COUNT];
2357 LIST_HEAD(new_fcports);
2359 /* If FL port exists, then SNS is present */
2360 rval = qla2x00_get_port_name(ha, SNS_FL_PORT, NULL, 0);
2361 if (rval != QLA_SUCCESS) {
2362 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2363 "Port\n", ha->host_no));
2365 ha->device_flags &= ~SWITCH_FOUND;
2366 return (QLA_SUCCESS);
2369 /* Mark devices that need re-synchronization. */
2370 rval2 = qla2x00_device_resync(ha);
2371 if (rval2 == QLA_RSCNS_HANDLED) {
2372 /* No point doing the scan, just continue. */
2373 return (QLA_SUCCESS);
2375 do {
2376 /* Ensure we are logged into the SNS. */
2377 qla2x00_login_fabric(ha, SIMPLE_NAME_SERVER, 0xff, 0xff, 0xfc,
2378 mb, BIT_1 | BIT_0);
2379 if (mb[0] != MBS_COMMAND_COMPLETE) {
2380 DEBUG2(qla_printk(KERN_INFO, ha,
2381 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2382 "mb[2]=%x mb[6]=%x mb[7]=%x\n", SIMPLE_NAME_SERVER,
2383 mb[0], mb[1], mb[2], mb[6], mb[7]));
2384 return (QLA_SUCCESS);
2387 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2388 if (qla2x00_rft_id(ha)) {
2389 /* EMPTY */
2390 DEBUG2(printk("scsi(%ld): Register FC-4 "
2391 "TYPE failed.\n", ha->host_no));
2393 if (qla2x00_rff_id(ha)) {
2394 /* EMPTY */
2395 DEBUG2(printk("scsi(%ld): Register FC-4 "
2396 "Features failed.\n", ha->host_no));
2398 if (qla2x00_rnn_id(ha)) {
2399 /* EMPTY */
2400 DEBUG2(printk("scsi(%ld): Register Node Name "
2401 "failed.\n", ha->host_no));
2402 } else if (qla2x00_rsnn_nn(ha)) {
2403 /* EMPTY */
2404 DEBUG2(printk("scsi(%ld): Register Symbolic "
2405 "Node Name failed.\n", ha->host_no));
2409 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2410 if (rval != QLA_SUCCESS)
2411 break;
2414 * Logout all previous fabric devices marked lost, except
2415 * tape devices.
2417 list_for_each_entry(fcport, &ha->fcports, list) {
2418 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2419 break;
2421 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2422 continue;
2424 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2425 qla2x00_mark_device_lost(ha, fcport,
2426 ql2xplogiabsentdevice);
2427 if (fcport->loop_id != FC_NO_LOOP_ID &&
2428 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2429 fcport->port_type != FCT_INITIATOR &&
2430 fcport->port_type != FCT_BROADCAST) {
2432 qla2x00_fabric_logout(ha,
2433 fcport->loop_id);
2434 fcport->loop_id = FC_NO_LOOP_ID;
2439 /* Starting free loop ID. */
2440 next_loopid = ha->min_external_loopid;
2443 * Scan through our port list and login entries that need to be
2444 * logged in.
2446 list_for_each_entry(fcport, &ha->fcports, list) {
2447 if (atomic_read(&ha->loop_down_timer) ||
2448 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2449 break;
2451 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2452 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2453 continue;
2455 if (fcport->loop_id == FC_NO_LOOP_ID) {
2456 fcport->loop_id = next_loopid;
2457 rval = qla2x00_find_new_loop_id(ha, fcport);
2458 if (rval != QLA_SUCCESS) {
2459 /* Ran out of IDs to use */
2460 break;
2464 /* Login and update database */
2465 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2468 /* Exit if out of loop IDs. */
2469 if (rval != QLA_SUCCESS) {
2470 break;
2474 * Login and add the new devices to our port list.
2476 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2477 if (atomic_read(&ha->loop_down_timer) ||
2478 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2479 break;
2481 /* Find a new loop ID to use. */
2482 fcport->loop_id = next_loopid;
2483 rval = qla2x00_find_new_loop_id(ha, fcport);
2484 if (rval != QLA_SUCCESS) {
2485 /* Ran out of IDs to use */
2486 break;
2489 /* Login and update database */
2490 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2492 /* Remove device from the new list and add it to DB */
2493 list_del(&fcport->list);
2494 list_add_tail(&fcport->list, &ha->fcports);
2496 } while (0);
2498 /* Free all new device structures not processed. */
2499 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2500 list_del(&fcport->list);
2501 kfree(fcport);
2504 if (rval) {
2505 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2506 "rval=%d\n", ha->host_no, rval));
2509 return (rval);
2514 * qla2x00_find_all_fabric_devs
2516 * Input:
2517 * ha = adapter block pointer.
2518 * dev = database device entry pointer.
2520 * Returns:
2521 * 0 = success.
2523 * Context:
2524 * Kernel context.
2526 static int
2527 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2529 int rval;
2530 uint16_t loop_id;
2531 fc_port_t *fcport, *new_fcport, *fcptemp;
2532 int found;
2534 sw_info_t *swl;
2535 int swl_idx;
2536 int first_dev, last_dev;
2537 port_id_t wrap, nxt_d_id;
2539 rval = QLA_SUCCESS;
2541 /* Try GID_PT to get device list, else GAN. */
2542 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2543 if (swl == NULL) {
2544 /*EMPTY*/
2545 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2546 "on GA_NXT\n", ha->host_no));
2547 } else {
2548 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2549 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2550 kfree(swl);
2551 swl = NULL;
2552 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2553 kfree(swl);
2554 swl = NULL;
2555 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2556 kfree(swl);
2557 swl = NULL;
2560 swl_idx = 0;
2562 /* Allocate temporary fcport for any new fcports discovered. */
2563 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2564 if (new_fcport == NULL) {
2565 if (swl)
2566 kfree(swl);
2567 return (QLA_MEMORY_ALLOC_FAILED);
2569 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2571 /* Set start port ID scan at adapter ID. */
2572 first_dev = 1;
2573 last_dev = 0;
2575 /* Starting free loop ID. */
2576 loop_id = ha->min_external_loopid;
2578 for (; loop_id <= ha->last_loop_id; loop_id++) {
2579 if (RESERVED_LOOP_ID(loop_id))
2580 continue;
2582 if (atomic_read(&ha->loop_down_timer) ||
2583 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2584 break;
2586 if (swl != NULL) {
2587 if (last_dev) {
2588 wrap.b24 = new_fcport->d_id.b24;
2589 } else {
2590 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2591 memcpy(new_fcport->node_name,
2592 swl[swl_idx].node_name, WWN_SIZE);
2593 memcpy(new_fcport->port_name,
2594 swl[swl_idx].port_name, WWN_SIZE);
2596 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2597 last_dev = 1;
2599 swl_idx++;
2601 } else {
2602 /* Send GA_NXT to the switch */
2603 rval = qla2x00_ga_nxt(ha, new_fcport);
2604 if (rval != QLA_SUCCESS) {
2605 qla_printk(KERN_WARNING, ha,
2606 "SNS scan failed -- assuming zero-entry "
2607 "result...\n");
2608 list_for_each_entry_safe(fcport, fcptemp,
2609 new_fcports, list) {
2610 list_del(&fcport->list);
2611 kfree(fcport);
2613 rval = QLA_SUCCESS;
2614 break;
2618 /* If wrap on switch device list, exit. */
2619 if (first_dev) {
2620 wrap.b24 = new_fcport->d_id.b24;
2621 first_dev = 0;
2622 } else if (new_fcport->d_id.b24 == wrap.b24) {
2623 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2624 ha->host_no, new_fcport->d_id.b.domain,
2625 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2626 break;
2629 /* Bypass if host adapter. */
2630 if (new_fcport->d_id.b24 == ha->d_id.b24)
2631 continue;
2633 /* Bypass reserved domain fields. */
2634 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2635 continue;
2637 /* Locate matching device in database. */
2638 found = 0;
2639 list_for_each_entry(fcport, &ha->fcports, list) {
2640 if (memcmp(new_fcport->port_name, fcport->port_name,
2641 WWN_SIZE))
2642 continue;
2644 found++;
2647 * If address the same and state FCS_ONLINE, nothing
2648 * changed.
2650 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2651 atomic_read(&fcport->state) == FCS_ONLINE) {
2652 break;
2656 * If device was not a fabric device before.
2658 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2659 fcport->d_id.b24 = new_fcport->d_id.b24;
2660 fcport->loop_id = FC_NO_LOOP_ID;
2661 fcport->flags |= (FCF_FABRIC_DEVICE |
2662 FCF_LOGIN_NEEDED);
2663 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2664 break;
2668 * Port ID changed or device was marked to be updated;
2669 * Log it out if still logged in and mark it for
2670 * relogin later.
2672 fcport->d_id.b24 = new_fcport->d_id.b24;
2673 fcport->flags |= FCF_LOGIN_NEEDED;
2674 if (fcport->loop_id != FC_NO_LOOP_ID &&
2675 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2676 fcport->port_type != FCT_INITIATOR &&
2677 fcport->port_type != FCT_BROADCAST) {
2678 qla2x00_fabric_logout(ha, fcport->loop_id);
2679 fcport->loop_id = FC_NO_LOOP_ID;
2682 break;
2685 if (found)
2686 continue;
2688 /* If device was not in our fcports list, then add it. */
2689 list_add_tail(&new_fcport->list, new_fcports);
2691 /* Allocate a new replacement fcport. */
2692 nxt_d_id.b24 = new_fcport->d_id.b24;
2693 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2694 if (new_fcport == NULL) {
2695 if (swl)
2696 kfree(swl);
2697 return (QLA_MEMORY_ALLOC_FAILED);
2699 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2700 new_fcport->d_id.b24 = nxt_d_id.b24;
2703 if (swl)
2704 kfree(swl);
2706 if (new_fcport)
2707 kfree(new_fcport);
2709 if (!list_empty(new_fcports))
2710 ha->device_flags |= DFLG_FABRIC_DEVICES;
2712 return (rval);
2716 * qla2x00_find_new_loop_id
2717 * Scan through our port list and find a new usable loop ID.
2719 * Input:
2720 * ha: adapter state pointer.
2721 * dev: port structure pointer.
2723 * Returns:
2724 * qla2x00 local function return status code.
2726 * Context:
2727 * Kernel context.
2730 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2732 int rval;
2733 int found;
2734 fc_port_t *fcport;
2735 uint16_t first_loop_id;
2737 rval = QLA_SUCCESS;
2739 /* Save starting loop ID. */
2740 first_loop_id = dev->loop_id;
2742 for (;;) {
2743 /* Skip loop ID if already used by adapter. */
2744 if (dev->loop_id == ha->loop_id) {
2745 dev->loop_id++;
2748 /* Skip reserved loop IDs. */
2749 while (RESERVED_LOOP_ID(dev->loop_id)) {
2750 dev->loop_id++;
2753 /* Reset loop ID if passed the end. */
2754 if (dev->loop_id > ha->last_loop_id) {
2755 /* first loop ID. */
2756 dev->loop_id = ha->min_external_loopid;
2759 /* Check for loop ID being already in use. */
2760 found = 0;
2761 fcport = NULL;
2762 list_for_each_entry(fcport, &ha->fcports, list) {
2763 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2764 /* ID possibly in use */
2765 found++;
2766 break;
2770 /* If not in use then it is free to use. */
2771 if (!found) {
2772 break;
2775 /* ID in use. Try next value. */
2776 dev->loop_id++;
2778 /* If wrap around. No free ID to use. */
2779 if (dev->loop_id == first_loop_id) {
2780 dev->loop_id = FC_NO_LOOP_ID;
2781 rval = QLA_FUNCTION_FAILED;
2782 break;
2786 return (rval);
2790 * qla2x00_device_resync
2791 * Marks devices in the database that needs resynchronization.
2793 * Input:
2794 * ha = adapter block pointer.
2796 * Context:
2797 * Kernel context.
2799 static int
2800 qla2x00_device_resync(scsi_qla_host_t *ha)
2802 int rval;
2803 int rval2;
2804 uint32_t mask;
2805 fc_port_t *fcport;
2806 uint32_t rscn_entry;
2807 uint8_t rscn_out_iter;
2808 uint8_t format;
2809 port_id_t d_id;
2811 rval = QLA_RSCNS_HANDLED;
2813 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2814 ha->flags.rscn_queue_overflow) {
2816 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2817 format = MSB(MSW(rscn_entry));
2818 d_id.b.domain = LSB(MSW(rscn_entry));
2819 d_id.b.area = MSB(LSW(rscn_entry));
2820 d_id.b.al_pa = LSB(LSW(rscn_entry));
2822 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2823 "[%02x/%02x%02x%02x].\n",
2824 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2825 d_id.b.area, d_id.b.al_pa));
2827 ha->rscn_out_ptr++;
2828 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2829 ha->rscn_out_ptr = 0;
2831 /* Skip duplicate entries. */
2832 for (rscn_out_iter = ha->rscn_out_ptr;
2833 !ha->flags.rscn_queue_overflow &&
2834 rscn_out_iter != ha->rscn_in_ptr;
2835 rscn_out_iter = (rscn_out_iter ==
2836 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2838 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2839 break;
2841 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2842 "entry found at [%d].\n", ha->host_no,
2843 rscn_out_iter));
2845 ha->rscn_out_ptr = rscn_out_iter;
2848 /* Queue overflow, set switch default case. */
2849 if (ha->flags.rscn_queue_overflow) {
2850 DEBUG(printk("scsi(%ld): device_resync: rscn "
2851 "overflow.\n", ha->host_no));
2853 format = 3;
2854 ha->flags.rscn_queue_overflow = 0;
2857 switch (format) {
2858 case 0:
2859 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) &&
2860 !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
2861 ha->flags.init_done) {
2862 /* Handle port RSCN via asyncronous IOCBs */
2863 rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
2864 NULL, 0);
2865 if (rval2 == QLA_SUCCESS)
2866 continue;
2868 mask = 0xffffff;
2869 break;
2870 case 1:
2871 mask = 0xffff00;
2872 break;
2873 case 2:
2874 mask = 0xff0000;
2875 break;
2876 default:
2877 mask = 0x0;
2878 d_id.b24 = 0;
2879 ha->rscn_out_ptr = ha->rscn_in_ptr;
2880 break;
2883 rval = QLA_SUCCESS;
2885 /* Abort any outstanding IO descriptors. */
2886 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2887 qla2x00_cancel_io_descriptors(ha);
2889 list_for_each_entry(fcport, &ha->fcports, list) {
2890 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2891 (fcport->d_id.b24 & mask) != d_id.b24 ||
2892 fcport->port_type == FCT_BROADCAST)
2893 continue;
2895 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2896 if (format != 3 ||
2897 fcport->port_type != FCT_INITIATOR) {
2898 atomic_set(&fcport->state,
2899 FCS_DEVICE_LOST);
2902 fcport->flags &= ~FCF_FARP_DONE;
2905 return (rval);
2909 * qla2x00_fabric_dev_login
2910 * Login fabric target device and update FC port database.
2912 * Input:
2913 * ha: adapter state pointer.
2914 * fcport: port structure list pointer.
2915 * next_loopid: contains value of a new loop ID that can be used
2916 * by the next login attempt.
2918 * Returns:
2919 * qla2x00 local function return status code.
2921 * Context:
2922 * Kernel context.
2924 static int
2925 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2926 uint16_t *next_loopid)
2928 int rval;
2929 int retry;
2931 rval = QLA_SUCCESS;
2932 retry = 0;
2934 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2935 if (rval == QLA_SUCCESS) {
2936 rval = qla2x00_get_port_database(ha, fcport, 0);
2937 if (rval != QLA_SUCCESS) {
2938 qla2x00_fabric_logout(ha, fcport->loop_id);
2939 } else {
2940 qla2x00_update_fcport(ha, fcport);
2944 return (rval);
2948 * qla2x00_fabric_login
2949 * Issue fabric login command.
2951 * Input:
2952 * ha = adapter block pointer.
2953 * device = pointer to FC device type structure.
2955 * Returns:
2956 * 0 - Login successfully
2957 * 1 - Login failed
2958 * 2 - Initiator device
2959 * 3 - Fatal error
2962 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2963 uint16_t *next_loopid)
2965 int rval;
2966 int retry;
2967 uint16_t tmp_loopid;
2968 uint16_t mb[MAILBOX_REGISTER_COUNT];
2970 retry = 0;
2971 tmp_loopid = 0;
2973 for (;;) {
2974 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2975 "for port %02x%02x%02x.\n",
2976 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2977 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2979 /* Login fcport on switch. */
2980 qla2x00_login_fabric(ha, fcport->loop_id,
2981 fcport->d_id.b.domain, fcport->d_id.b.area,
2982 fcport->d_id.b.al_pa, mb, BIT_0);
2983 if (mb[0] == MBS_PORT_ID_USED) {
2985 * Device has another loop ID. The firmware team
2986 * recommends us to perform an implicit login with the
2987 * specified ID again. The ID we just used is save here
2988 * so we return with an ID that can be tried by the
2989 * next login.
2991 retry++;
2992 tmp_loopid = fcport->loop_id;
2993 fcport->loop_id = mb[1];
2995 DEBUG(printk("Fabric Login: port in use - next "
2996 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2997 fcport->loop_id, fcport->d_id.b.domain,
2998 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3000 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3002 * Login succeeded.
3004 if (retry) {
3005 /* A retry occurred before. */
3006 *next_loopid = tmp_loopid;
3007 } else {
3009 * No retry occurred before. Just increment the
3010 * ID value for next login.
3012 *next_loopid = (fcport->loop_id + 1);
3015 if (mb[1] & BIT_0) {
3016 fcport->port_type = FCT_INITIATOR;
3017 } else {
3018 fcport->port_type = FCT_TARGET;
3019 if (mb[1] & BIT_1) {
3020 fcport->flags |= FCF_TAPE_PRESENT;
3024 rval = QLA_SUCCESS;
3025 break;
3026 } else if (mb[0] == MBS_LOOP_ID_USED) {
3028 * Loop ID already used, try next loop ID.
3030 fcport->loop_id++;
3031 rval = qla2x00_find_new_loop_id(ha, fcport);
3032 if (rval != QLA_SUCCESS) {
3033 /* Ran out of loop IDs to use */
3034 break;
3036 } else if (mb[0] == MBS_COMMAND_ERROR) {
3038 * Firmware possibly timed out during login. If NO
3039 * retries are left to do then the device is declared
3040 * dead.
3042 *next_loopid = fcport->loop_id;
3043 qla2x00_fabric_logout(ha, fcport->loop_id);
3044 qla2x00_mark_device_lost(ha, fcport, 1);
3046 rval = 1;
3047 break;
3048 } else {
3050 * unrecoverable / not handled error
3052 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
3053 "loop_id=%x jiffies=%lx.\n",
3054 __func__, ha->host_no, mb[0],
3055 fcport->d_id.b.domain, fcport->d_id.b.area,
3056 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3058 *next_loopid = fcport->loop_id;
3059 qla2x00_fabric_logout(ha, fcport->loop_id);
3060 fcport->loop_id = FC_NO_LOOP_ID;
3061 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
3063 rval = 3;
3064 break;
3068 return (rval);
3072 * qla2x00_local_device_login
3073 * Issue local device login command.
3075 * Input:
3076 * ha = adapter block pointer.
3077 * loop_id = loop id of device to login to.
3079 * Returns (Where's the #define!!!!):
3080 * 0 - Login successfully
3081 * 1 - Login failed
3082 * 3 - Fatal error
3085 qla2x00_local_device_login(scsi_qla_host_t *ha, uint16_t loop_id)
3087 int rval;
3088 uint16_t mb[MAILBOX_REGISTER_COUNT];
3090 memset(mb, 0, sizeof(mb));
3091 rval = qla2x00_login_local_device(ha, loop_id, mb, BIT_0);
3092 if (rval == QLA_SUCCESS) {
3093 /* Interrogate mailbox registers for any errors */
3094 if (mb[0] == MBS_COMMAND_ERROR)
3095 rval = 1;
3096 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3097 /* device not in PCB table */
3098 rval = 3;
3101 return (rval);
3105 * qla2x00_loop_resync
3106 * Resync with fibre channel devices.
3108 * Input:
3109 * ha = adapter block pointer.
3111 * Returns:
3112 * 0 = success
3115 qla2x00_loop_resync(scsi_qla_host_t *ha)
3117 int rval;
3118 uint32_t wait_time;
3120 rval = QLA_SUCCESS;
3122 atomic_set(&ha->loop_state, LOOP_UPDATE);
3123 qla2x00_stats.loop_resync++;
3124 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3125 if (ha->flags.online) {
3126 if (!(rval = qla2x00_fw_ready(ha))) {
3127 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3128 wait_time = 256;
3129 do {
3130 /* v2.19.05b6 */
3131 atomic_set(&ha->loop_state, LOOP_UPDATE);
3134 * Issue marker command only when we are going
3135 * to start the I/O .
3137 ha->marker_needed = 1;
3139 /* Remap devices on Loop. */
3140 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3142 qla2x00_configure_loop(ha);
3143 wait_time--;
3144 } while (!atomic_read(&ha->loop_down_timer) &&
3145 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3146 wait_time &&
3147 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3149 qla2x00_restart_queues(ha, 1);
3152 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3153 return (QLA_FUNCTION_FAILED);
3156 if (rval) {
3157 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3160 return (rval);
3164 * qla2x00_restart_queues
3165 * Restart device queues.
3167 * Input:
3168 * ha = adapter block pointer.
3170 * Context:
3171 * Kernel/Interrupt context.
3173 void
3174 qla2x00_restart_queues(scsi_qla_host_t *ha, uint8_t flush)
3176 srb_t *sp;
3177 int retry_q_cnt = 0;
3178 int pending_q_cnt = 0;
3179 struct list_head *list, *temp;
3180 unsigned long flags = 0;
3182 clear_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
3184 /* start pending queue */
3185 pending_q_cnt = ha->qthreads;
3186 if (flush) {
3187 spin_lock_irqsave(&ha->list_lock,flags);
3188 list_for_each_safe(list, temp, &ha->pending_queue) {
3189 sp = list_entry(list, srb_t, list);
3191 if ((sp->flags & SRB_TAPE))
3192 continue;
3195 * When time expire return request back to OS as BUSY
3197 __del_from_pending_queue(ha, sp);
3198 sp->cmd->result = DID_BUS_BUSY << 16;
3199 sp->cmd->host_scribble = (unsigned char *)NULL;
3200 __add_to_done_queue(ha, sp);
3202 spin_unlock_irqrestore(&ha->list_lock, flags);
3203 } else {
3204 if (!list_empty(&ha->pending_queue))
3205 qla2x00_next(ha);
3209 * Clear out our retry queue
3211 if (flush) {
3212 spin_lock_irqsave(&ha->list_lock, flags);
3213 retry_q_cnt = ha->retry_q_cnt;
3214 list_for_each_safe(list, temp, &ha->retry_queue) {
3215 sp = list_entry(list, srb_t, list);
3216 /* when time expire return request back to OS as BUSY */
3217 __del_from_retry_queue(ha, sp);
3218 sp->cmd->result = DID_BUS_BUSY << 16;
3219 sp->cmd->host_scribble = (unsigned char *)NULL;
3220 __add_to_done_queue(ha, sp);
3222 spin_unlock_irqrestore(&ha->list_lock, flags);
3224 DEBUG2(printk("%s(%ld): callback %d commands.\n",
3225 __func__,
3226 ha->host_no,
3227 retry_q_cnt);)
3230 DEBUG2(printk("%s(%ld): active=%ld, retry=%d, pending=%d, "
3231 "done=%ld, scsi retry=%d commands.\n",
3232 __func__,
3233 ha->host_no,
3234 ha->actthreads,
3235 ha->retry_q_cnt,
3236 pending_q_cnt,
3237 ha->done_q_cnt,
3238 ha->scsi_retry_q_cnt);)
3240 if (!list_empty(&ha->done_queue))
3241 qla2x00_done(ha);
3244 void
3245 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3247 int rescan_done;
3248 fc_port_t *fcport;
3250 rescan_done = 0;
3251 list_for_each_entry(fcport, &ha->fcports, list) {
3252 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3253 continue;
3255 qla2x00_update_fcport(ha, fcport);
3256 fcport->flags &= ~FCF_RESCAN_NEEDED;
3258 rescan_done = 1;
3260 qla2x00_probe_for_all_luns(ha);
3262 /* Update OS target and lun structures if necessary. */
3263 if (rescan_done) {
3264 qla2x00_config_os(ha);
3270 * qla2x00_config_os
3271 * Setup OS target and LUN structures.
3273 * Input:
3274 * ha = adapter state pointer.
3276 * Context:
3277 * Kernel context.
3279 static void
3280 qla2x00_config_os(scsi_qla_host_t *ha)
3282 fc_port_t *fcport;
3283 fc_lun_t *fclun;
3284 os_tgt_t *tq;
3285 uint16_t tgt;
3288 for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
3289 if ((tq = TGT_Q(ha, tgt)) == NULL)
3290 continue;
3292 clear_bit(TQF_ONLINE, &tq->flags);
3295 list_for_each_entry(fcport, &ha->fcports, list) {
3296 if (atomic_read(&fcport->state) != FCS_ONLINE ||
3297 fcport->port_type == FCT_INITIATOR ||
3298 fcport->port_type == FCT_BROADCAST) {
3299 fcport->os_target_id = MAX_TARGETS;
3300 continue;
3303 if (fcport->flags & FCF_FO_MASKED) {
3304 continue;
3307 /* Bind FC port to OS target number. */
3308 if (qla2x00_fcport_bind(ha, fcport) == MAX_TARGETS) {
3309 continue;
3312 /* Bind FC LUN to OS LUN number. */
3313 list_for_each_entry(fclun, &fcport->fcluns, list) {
3314 qla2x00_fclun_bind(ha, fcport, fclun);
3320 * qla2x00_fcport_bind
3321 * Locates a target number for FC port.
3323 * Input:
3324 * ha = adapter state pointer.
3325 * fcport = FC port structure pointer.
3327 * Returns:
3328 * target number
3330 * Context:
3331 * Kernel context.
3333 static uint16_t
3334 qla2x00_fcport_bind(scsi_qla_host_t *ha, fc_port_t *fcport)
3336 int found;
3337 uint16_t tgt;
3338 os_tgt_t *tq;
3340 /* Check for persistent binding. */
3341 for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
3342 if ((tq = TGT_Q(ha, tgt)) == NULL)
3343 continue;
3345 found = 0;
3346 switch (ha->binding_type) {
3347 case BIND_BY_PORT_ID:
3348 if (fcport->d_id.b24 == tq->d_id.b24) {
3349 memcpy(tq->node_name, fcport->node_name,
3350 WWN_SIZE);
3351 memcpy(tq->port_name, fcport->port_name,
3352 WWN_SIZE);
3353 found++;
3355 break;
3356 case BIND_BY_PORT_NAME:
3357 if (memcmp(fcport->port_name, tq->port_name,
3358 WWN_SIZE) == 0) {
3360 * In case of persistent binding, update the
3361 * WWNN.
3363 memcpy(tq->node_name, fcport->node_name,
3364 WWN_SIZE);
3365 found++;
3367 break;
3369 if (found)
3370 break;
3373 /* TODO: honor the ConfigRequired flag */
3374 if (tgt == MAX_TARGETS) {
3375 /* Check if targetID 0 available. */
3376 tgt = 0;
3378 if (TGT_Q(ha, tgt) != NULL) {
3379 /* Locate first free target for device. */
3380 for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
3381 if (TGT_Q(ha, tgt) == NULL) {
3382 break;
3386 if (tgt != MAX_TARGETS) {
3387 if ((tq = qla2x00_tgt_alloc(ha, tgt)) != NULL) {
3388 memcpy(tq->node_name, fcport->node_name,
3389 WWN_SIZE);
3390 memcpy(tq->port_name, fcport->port_name,
3391 WWN_SIZE);
3392 tq->d_id.b24 = fcport->d_id.b24;
3397 /* Reset target numbers incase it changed. */
3398 fcport->os_target_id = tgt;
3399 if (tgt != MAX_TARGETS && tq != NULL) {
3400 DEBUG2(printk("scsi(%ld): Assigning target ID=%02d @ %p to "
3401 "loop id=0x%04x, port state=0x%x, port down retry=%d\n",
3402 ha->host_no, tgt, tq, fcport->loop_id,
3403 atomic_read(&fcport->state),
3404 atomic_read(&fcport->port_down_timer)));
3406 fcport->tgt_queue = tq;
3407 fcport->flags |= FCF_PERSISTENT_BOUND;
3408 tq->fcport = fcport;
3409 set_bit(TQF_ONLINE, &tq->flags);
3410 tq->port_down_retry_count = ha->port_down_retry_count;
3413 if (tgt == MAX_TARGETS) {
3414 qla_printk(KERN_WARNING, ha,
3415 "Unable to bind fcport, loop_id=%x\n", fcport->loop_id);
3418 return (tgt);
3422 * qla2x00_fclun_bind
3423 * Binds all FC device LUNS to OS LUNS.
3425 * Input:
3426 * ha: adapter state pointer.
3427 * fcport: FC port structure pointer.
3429 * Returns:
3430 * target number
3432 * Context:
3433 * Kernel context.
3435 static os_lun_t *
3436 qla2x00_fclun_bind(scsi_qla_host_t *ha, fc_port_t *fcport, fc_lun_t *fclun)
3438 os_lun_t *lq;
3439 uint16_t tgt;
3440 uint16_t lun;
3442 tgt = fcport->os_target_id;
3443 lun = fclun->lun;
3445 /* Allocate LUNs */
3446 if (lun >= MAX_LUNS) {
3447 DEBUG2(printk("scsi(%ld): Unable to bind lun, invalid "
3448 "lun=(%x).\n", ha->host_no, lun));
3449 return (NULL);
3452 /* Always alloc LUN 0 so kernel will scan past LUN 0. */
3453 if (lun != 0 && (EXT_IS_LUN_BIT_SET(&(fcport->lun_mask), lun))) {
3454 return (NULL);
3457 if ((lq = qla2x00_lun_alloc(ha, tgt, lun)) == NULL) {
3458 qla_printk(KERN_WARNING, ha,
3459 "Unable to bind fclun, loop_id=%x lun=%x\n",
3460 fcport->loop_id, lun);
3461 return (NULL);
3464 lq->fclun = fclun;
3466 return (lq);
3470 * qla2x00_tgt_alloc
3471 * Allocate and pre-initialize target queue.
3473 * Input:
3474 * ha = adapter block pointer.
3475 * t = SCSI target number.
3477 * Returns:
3478 * NULL = failure
3480 * Context:
3481 * Kernel context.
3483 static os_tgt_t *
3484 qla2x00_tgt_alloc(scsi_qla_host_t *ha, uint16_t tgt)
3486 os_tgt_t *tq;
3489 * If SCSI addressing OK, allocate TGT queue and lock.
3491 if (tgt >= MAX_TARGETS) {
3492 DEBUG2(printk("scsi(%ld): Unable to allocate target, invalid "
3493 "target number %d.\n", ha->host_no, tgt));
3494 return (NULL);
3497 tq = TGT_Q(ha, tgt);
3498 if (tq == NULL) {
3499 tq = kmalloc(sizeof(os_tgt_t), GFP_ATOMIC);
3500 if (tq != NULL) {
3501 DEBUG2(printk("scsi(%ld): Alloc Target %d @ %p\n",
3502 ha->host_no, tgt, tq));
3504 memset(tq, 0, sizeof(os_tgt_t));
3505 tq->ha = ha;
3507 TGT_Q(ha, tgt) = tq;
3510 if (tq != NULL) {
3511 tq->port_down_retry_count = ha->port_down_retry_count;
3512 } else {
3513 qla_printk(KERN_WARNING, ha,
3514 "Unable to allocate target.\n");
3515 ha->mem_err++;
3518 return (tq);
3522 * qla2x00_tgt_free
3523 * Frees target and LUN queues.
3525 * Input:
3526 * ha = adapter block pointer.
3527 * t = SCSI target number.
3529 * Context:
3530 * Kernel context.
3532 void
3533 qla2x00_tgt_free(scsi_qla_host_t *ha, uint16_t tgt)
3535 os_tgt_t *tq;
3536 uint16_t lun;
3539 * If SCSI addressing OK, allocate TGT queue and lock.
3541 if (tgt >= MAX_TARGETS) {
3542 DEBUG2(printk("scsi(%ld): Unable to de-allocate target, "
3543 "invalid target number %d.\n", ha->host_no, tgt));
3545 return;
3548 tq = TGT_Q(ha, tgt);
3549 if (tq != NULL) {
3550 TGT_Q(ha, tgt) = NULL;
3552 /* Free LUN structures. */
3553 for (lun = 0; lun < MAX_LUNS; lun++)
3554 qla2x00_lun_free(ha, tgt, lun);
3556 kfree(tq);
3559 return;
3563 * qla2x00_lun_alloc
3564 * Allocate and initialize LUN queue.
3566 * Input:
3567 * ha = adapter block pointer.
3568 * t = SCSI target number.
3569 * l = LUN number.
3571 * Returns:
3572 * NULL = failure
3574 * Context:
3575 * Kernel context.
3577 static os_lun_t *
3578 qla2x00_lun_alloc(scsi_qla_host_t *ha, uint16_t tgt, uint16_t lun)
3580 os_lun_t *lq;
3583 * If SCSI addressing OK, allocate LUN queue.
3585 if (tgt >= MAX_TARGETS || lun >= MAX_LUNS || TGT_Q(ha, tgt) == NULL) {
3586 DEBUG2(printk("scsi(%ld): Unable to allocate lun, invalid "
3587 "parameter.\n", ha->host_no));
3589 return (NULL);
3592 lq = LUN_Q(ha, tgt, lun);
3593 if (lq == NULL) {
3594 lq = kmalloc(sizeof(os_lun_t), GFP_ATOMIC);
3595 if (lq != NULL) {
3596 DEBUG2(printk("scsi(%ld): Alloc Lun %d @ tgt %d.\n",
3597 ha->host_no, lun, tgt));
3599 memset(lq, 0, sizeof(os_lun_t));
3600 LUN_Q(ha, tgt, lun) = lq;
3603 * The following lun queue initialization code
3604 * must be duplicated in alloc_ioctl_mem function
3605 * for ioctl_lq.
3607 lq->q_state = LUN_STATE_READY;
3608 spin_lock_init(&lq->q_lock);
3612 if (lq == NULL) {
3613 qla_printk(KERN_WARNING, ha, "Unable to allocate lun.\n");
3616 return (lq);
3620 * qla2x00_lun_free
3621 * Frees LUN queue.
3623 * Input:
3624 * ha = adapter block pointer.
3625 * t = SCSI target number.
3627 * Context:
3628 * Kernel context.
3630 static void
3631 qla2x00_lun_free(scsi_qla_host_t *ha, uint16_t tgt, uint16_t lun)
3633 os_lun_t *lq;
3636 * If SCSI addressing OK, allocate TGT queue and lock.
3638 if (tgt >= MAX_TARGETS || lun >= MAX_LUNS) {
3639 DEBUG2(printk("scsi(%ld): Unable to deallocate lun, invalid "
3640 "parameter.\n", ha->host_no));
3642 return;
3645 if (TGT_Q(ha, tgt) != NULL && (lq = LUN_Q(ha, tgt, lun)) != NULL) {
3646 LUN_Q(ha, tgt, lun) = NULL;
3647 kfree(lq);
3650 return;
3654 * qla2x00_abort_isp
3655 * Resets ISP and aborts all outstanding commands.
3657 * Input:
3658 * ha = adapter block pointer.
3660 * Returns:
3661 * 0 = success
3664 qla2x00_abort_isp(scsi_qla_host_t *ha)
3666 unsigned long flags = 0;
3667 uint16_t cnt;
3668 srb_t *sp;
3669 uint8_t status = 0;
3671 if (ha->flags.online) {
3672 ha->flags.online = 0;
3673 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3674 qla2x00_stats.ispAbort++;
3675 ha->total_isp_aborts++; /* used by ioctl */
3676 ha->sns_retry_cnt = 0;
3678 qla_printk(KERN_INFO, ha,
3679 "Performing ISP error recovery - ha= %p.\n", ha);
3680 qla2x00_reset_chip(ha);
3682 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3683 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3684 atomic_set(&ha->loop_state, LOOP_DOWN);
3685 qla2x00_mark_all_devices_lost(ha);
3686 } else {
3687 if (!atomic_read(&ha->loop_down_timer))
3688 atomic_set(&ha->loop_down_timer,
3689 LOOP_DOWN_TIME);
3692 spin_lock_irqsave(&ha->hardware_lock, flags);
3693 /* Requeue all commands in outstanding command list. */
3694 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3695 sp = ha->outstanding_cmds[cnt];
3696 if (sp) {
3697 ha->outstanding_cmds[cnt] = NULL;
3698 if (ha->actthreads)
3699 ha->actthreads--;
3700 sp->lun_queue->out_cnt--;
3703 * Set the cmd host_byte status depending on
3704 * whether the scsi_error_handler is
3705 * active or not.
3707 if (sp->flags & SRB_TAPE) {
3708 sp->cmd->result = DID_NO_CONNECT << 16;
3709 } else {
3710 if (ha->host->eh_active != EH_ACTIVE)
3711 sp->cmd->result =
3712 DID_BUS_BUSY << 16;
3713 else
3714 sp->cmd->result =
3715 DID_RESET << 16;
3717 sp->flags = 0;
3718 sp->cmd->host_scribble = (unsigned char *)NULL;
3719 add_to_done_queue(ha, sp);
3722 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3724 qla2x00_nvram_config(ha);
3726 if (!qla2x00_restart_isp(ha)) {
3727 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3729 if (!atomic_read(&ha->loop_down_timer)) {
3731 * Issue marker command only when we are going
3732 * to start the I/O .
3734 ha->marker_needed = 1;
3737 ha->flags.online = 1;
3739 /* Enable ISP interrupts. */
3740 qla2x00_enable_intrs(ha);
3742 /* v2.19.5b6 Return all commands */
3743 qla2x00_abort_queues(ha, 1);
3745 /* Restart queues that may have been stopped. */
3746 qla2x00_restart_queues(ha, 1);
3747 ha->isp_abort_cnt = 0;
3748 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3749 } else { /* failed the ISP abort */
3750 ha->flags.online = 1;
3751 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3752 if (ha->isp_abort_cnt == 0) {
3753 qla_printk(KERN_WARNING, ha,
3754 "ISP error recovery failed - "
3755 "board disabled\n");
3757 * The next call disables the board
3758 * completely.
3760 qla2x00_reset_adapter(ha);
3761 qla2x00_abort_queues(ha, 0);
3762 ha->flags.online = 0;
3763 clear_bit(ISP_ABORT_RETRY,
3764 &ha->dpc_flags);
3765 status = 0;
3766 } else { /* schedule another ISP abort */
3767 ha->isp_abort_cnt--;
3768 DEBUG(printk("qla%ld: ISP abort - "
3769 "retry remainning %d\n",
3770 ha->host_no, ha->isp_abort_cnt);)
3771 status = 1;
3773 } else {
3774 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3775 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3776 "- retrying (%d) more times\n",
3777 ha->host_no, ha->isp_abort_cnt);)
3778 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3779 status = 1;
3785 if (status) {
3786 qla_printk(KERN_INFO, ha,
3787 "qla2x00_abort_isp: **** FAILED ****\n");
3788 } else {
3789 DEBUG(printk(KERN_INFO
3790 "qla2x00_abort_isp(%ld): exiting.\n",
3791 ha->host_no);)
3794 return(status);
3798 * qla2x00_restart_isp
3799 * restarts the ISP after a reset
3801 * Input:
3802 * ha = adapter block pointer.
3804 * Returns:
3805 * 0 = success
3807 static int
3808 qla2x00_restart_isp(scsi_qla_host_t *ha)
3810 uint8_t status = 0;
3811 device_reg_t __iomem *reg = ha->iobase;
3812 unsigned long flags = 0;
3813 uint32_t wait_time;
3815 /* If firmware needs to be loaded */
3816 if (qla2x00_isp_firmware(ha)) {
3817 ha->flags.online = 0;
3818 if (!(status = qla2x00_chip_diag(ha))) {
3819 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3820 status = qla2x00_setup_chip(ha);
3821 goto done;
3824 reg = ha->iobase;
3826 spin_lock_irqsave(&ha->hardware_lock, flags);
3828 /* Disable SRAM, Instruction RAM and GP RAM parity. */
3829 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3830 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3832 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3834 status = qla2x00_setup_chip(ha);
3836 spin_lock_irqsave(&ha->hardware_lock, flags);
3838 /* Enable proper parity */
3839 if (IS_QLA2300(ha))
3840 /* SRAM parity */
3841 WRT_REG_WORD(&reg->hccr,
3842 (HCCR_ENABLE_PARITY + 0x1));
3843 else
3844 /* SRAM, Instruction RAM and GP RAM parity */
3845 WRT_REG_WORD(&reg->hccr,
3846 (HCCR_ENABLE_PARITY + 0x7));
3847 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3849 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3853 done:
3854 if (!status && !(status = qla2x00_init_rings(ha))) {
3855 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3856 if (!(status = qla2x00_fw_ready(ha))) {
3857 DEBUG(printk("%s(): Start configure loop, "
3858 "status = %d\n",
3859 __func__,
3860 status);)
3861 ha->flags.online = 1;
3862 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3863 wait_time = 256;
3864 do {
3865 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3866 qla2x00_configure_loop(ha);
3867 wait_time--;
3868 } while (!atomic_read(&ha->loop_down_timer) &&
3869 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3870 wait_time &&
3871 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3874 /* if no cable then assume it's good */
3875 if ((ha->device_flags & DFLG_NO_CABLE))
3876 status = 0;
3878 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3879 __func__,
3880 status);)
3882 return (status);
3886 * qla2x00_reset_adapter
3887 * Reset adapter.
3889 * Input:
3890 * ha = adapter block pointer.
3892 static void
3893 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3895 unsigned long flags = 0;
3896 device_reg_t __iomem *reg = ha->iobase;
3898 ha->flags.online = 0;
3899 qla2x00_disable_intrs(ha);
3901 /* Reset RISC processor. */
3902 spin_lock_irqsave(&ha->hardware_lock, flags);
3903 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3904 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3905 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3906 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3907 spin_unlock_irqrestore(&ha->hardware_lock, flags);