[SCSI] qla2xxx: Add MSI-X support.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / qla2xxx / qla_init.c
blobb6f0494e034c37ed9c555f4a5f224bf26180eba6
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
9 #include <linux/delay.h>
10 #include <linux/vmalloc.h>
12 #include "qla_devtbl.h"
14 /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
15 #ifndef EXT_IS_LUN_BIT_SET
16 #define EXT_IS_LUN_BIT_SET(P,L) \
17 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
18 #define EXT_SET_LUN_BIT(P,L) \
19 ((P)->mask[L/8] |= (0x80 >> (L%8)))
20 #endif
23 * QLogic ISP2x00 Hardware Support Function Prototypes.
25 static int qla2x00_isp_firmware(scsi_qla_host_t *);
26 static void qla2x00_resize_request_q(scsi_qla_host_t *);
27 static int qla2x00_setup_chip(scsi_qla_host_t *);
28 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
29 static int qla2x00_init_rings(scsi_qla_host_t *);
30 static int qla2x00_fw_ready(scsi_qla_host_t *);
31 static int qla2x00_configure_hba(scsi_qla_host_t *);
32 static int qla2x00_configure_loop(scsi_qla_host_t *);
33 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
34 static int qla2x00_configure_fabric(scsi_qla_host_t *);
35 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
36 static int qla2x00_device_resync(scsi_qla_host_t *);
37 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
38 uint16_t *);
40 static int qla2x00_restart_isp(scsi_qla_host_t *);
42 static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
44 /****************************************************************************/
45 /* QLogic ISP2x00 Hardware Support Functions. */
46 /****************************************************************************/
49 * qla2x00_initialize_adapter
50 * Initialize board.
52 * Input:
53 * ha = adapter block pointer.
55 * Returns:
56 * 0 = success
58 int
59 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
61 int rval;
63 /* Clear adapter flags. */
64 ha->flags.online = 0;
65 ha->flags.reset_active = 0;
66 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
67 atomic_set(&ha->loop_state, LOOP_DOWN);
68 ha->device_flags = 0;
69 ha->dpc_flags = 0;
70 ha->flags.management_server_logged_in = 0;
71 ha->marker_needed = 0;
72 ha->mbx_flags = 0;
73 ha->isp_abort_cnt = 0;
74 ha->beacon_blink_led = 0;
75 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
77 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
78 rval = ha->isp_ops.pci_config(ha);
79 if (rval) {
80 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
81 ha->host_no));
82 return (rval);
85 ha->isp_ops.reset_chip(ha);
87 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
89 ha->isp_ops.nvram_config(ha);
91 if (ha->flags.disable_serdes) {
92 /* Mask HBA via NVRAM settings? */
93 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
94 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
95 ha->port_name[0], ha->port_name[1],
96 ha->port_name[2], ha->port_name[3],
97 ha->port_name[4], ha->port_name[5],
98 ha->port_name[6], ha->port_name[7]);
99 return QLA_FUNCTION_FAILED;
102 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
104 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
105 rval = ha->isp_ops.chip_diag(ha);
106 if (rval)
107 return (rval);
108 rval = qla2x00_setup_chip(ha);
109 if (rval)
110 return (rval);
112 rval = qla2x00_init_rings(ha);
114 return (rval);
118 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
119 * @ha: HA context
121 * Returns 0 on success.
124 qla2100_pci_config(scsi_qla_host_t *ha)
126 uint16_t w, mwi;
127 uint32_t d;
128 unsigned long flags;
129 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
131 pci_set_master(ha->pdev);
132 mwi = 0;
133 if (pci_set_mwi(ha->pdev))
134 mwi = PCI_COMMAND_INVALIDATE;
136 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
137 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
138 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
140 /* Reset expansion ROM address decode enable */
141 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
142 d &= ~PCI_ROM_ADDRESS_ENABLE;
143 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
145 /* Get PCI bus information. */
146 spin_lock_irqsave(&ha->hardware_lock, flags);
147 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
148 spin_unlock_irqrestore(&ha->hardware_lock, flags);
150 return QLA_SUCCESS;
154 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
155 * @ha: HA context
157 * Returns 0 on success.
160 qla2300_pci_config(scsi_qla_host_t *ha)
162 uint16_t w, mwi;
163 uint32_t d;
164 unsigned long flags = 0;
165 uint32_t cnt;
166 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
168 pci_set_master(ha->pdev);
169 mwi = 0;
170 if (pci_set_mwi(ha->pdev))
171 mwi = PCI_COMMAND_INVALIDATE;
173 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
174 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
176 if (IS_QLA2322(ha) || IS_QLA6322(ha))
177 w &= ~PCI_COMMAND_INTX_DISABLE;
180 * If this is a 2300 card and not 2312, reset the
181 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
182 * the 2310 also reports itself as a 2300 so we need to get the
183 * fb revision level -- a 6 indicates it really is a 2300 and
184 * not a 2310.
186 if (IS_QLA2300(ha)) {
187 spin_lock_irqsave(&ha->hardware_lock, flags);
189 /* Pause RISC. */
190 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
191 for (cnt = 0; cnt < 30000; cnt++) {
192 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
193 break;
195 udelay(10);
198 /* Select FPM registers. */
199 WRT_REG_WORD(&reg->ctrl_status, 0x20);
200 RD_REG_WORD(&reg->ctrl_status);
202 /* Get the fb rev level */
203 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
205 if (ha->fb_rev == FPM_2300)
206 w &= ~PCI_COMMAND_INVALIDATE;
208 /* Deselect FPM registers. */
209 WRT_REG_WORD(&reg->ctrl_status, 0x0);
210 RD_REG_WORD(&reg->ctrl_status);
212 /* Release RISC module. */
213 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
214 for (cnt = 0; cnt < 30000; cnt++) {
215 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
216 break;
218 udelay(10);
221 spin_unlock_irqrestore(&ha->hardware_lock, flags);
223 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
225 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
227 /* Reset expansion ROM address decode enable */
228 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
229 d &= ~PCI_ROM_ADDRESS_ENABLE;
230 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
232 /* Get PCI bus information. */
233 spin_lock_irqsave(&ha->hardware_lock, flags);
234 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
235 spin_unlock_irqrestore(&ha->hardware_lock, flags);
237 return QLA_SUCCESS;
241 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
242 * @ha: HA context
244 * Returns 0 on success.
247 qla24xx_pci_config(scsi_qla_host_t *ha)
249 uint16_t w, mwi;
250 uint32_t d;
251 unsigned long flags = 0;
252 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
253 int pcix_cmd_reg, pcie_dctl_reg;
255 pci_set_master(ha->pdev);
256 mwi = 0;
257 if (pci_set_mwi(ha->pdev))
258 mwi = PCI_COMMAND_INVALIDATE;
260 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
261 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
262 w &= ~PCI_COMMAND_INTX_DISABLE;
263 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
265 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
267 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
268 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
269 if (pcix_cmd_reg) {
270 uint16_t pcix_cmd;
272 pcix_cmd_reg += PCI_X_CMD;
273 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
274 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
275 pcix_cmd |= 0x0008;
276 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
279 /* PCIe -- adjust Maximum Read Request Size (2048). */
280 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
281 if (pcie_dctl_reg) {
282 uint16_t pcie_dctl;
284 pcie_dctl_reg += PCI_EXP_DEVCTL;
285 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
286 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
287 pcie_dctl |= 0x4000;
288 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
291 /* Reset expansion ROM address decode enable */
292 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
293 d &= ~PCI_ROM_ADDRESS_ENABLE;
294 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
296 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->chip_revision);
298 /* Get PCI bus information. */
299 spin_lock_irqsave(&ha->hardware_lock, flags);
300 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
301 spin_unlock_irqrestore(&ha->hardware_lock, flags);
303 return QLA_SUCCESS;
307 * qla2x00_isp_firmware() - Choose firmware image.
308 * @ha: HA context
310 * Returns 0 on success.
312 static int
313 qla2x00_isp_firmware(scsi_qla_host_t *ha)
315 int rval;
317 /* Assume loading risc code */
318 rval = QLA_FUNCTION_FAILED;
320 if (ha->flags.disable_risc_code_load) {
321 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
322 ha->host_no));
323 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
325 /* Verify checksum of loaded RISC code. */
326 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
329 if (rval) {
330 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
331 ha->host_no));
334 return (rval);
338 * qla2x00_reset_chip() - Reset ISP chip.
339 * @ha: HA context
341 * Returns 0 on success.
343 void
344 qla2x00_reset_chip(scsi_qla_host_t *ha)
346 unsigned long flags = 0;
347 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
348 uint32_t cnt;
349 uint16_t cmd;
351 ha->isp_ops.disable_intrs(ha);
353 spin_lock_irqsave(&ha->hardware_lock, flags);
355 /* Turn off master enable */
356 cmd = 0;
357 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
358 cmd &= ~PCI_COMMAND_MASTER;
359 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
361 if (!IS_QLA2100(ha)) {
362 /* Pause RISC. */
363 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
364 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
365 for (cnt = 0; cnt < 30000; cnt++) {
366 if ((RD_REG_WORD(&reg->hccr) &
367 HCCR_RISC_PAUSE) != 0)
368 break;
369 udelay(100);
371 } else {
372 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
373 udelay(10);
376 /* Select FPM registers. */
377 WRT_REG_WORD(&reg->ctrl_status, 0x20);
378 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
380 /* FPM Soft Reset. */
381 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
382 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
384 /* Toggle Fpm Reset. */
385 if (!IS_QLA2200(ha)) {
386 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
387 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
390 /* Select frame buffer registers. */
391 WRT_REG_WORD(&reg->ctrl_status, 0x10);
392 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
394 /* Reset frame buffer FIFOs. */
395 if (IS_QLA2200(ha)) {
396 WRT_FB_CMD_REG(ha, reg, 0xa000);
397 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
398 } else {
399 WRT_FB_CMD_REG(ha, reg, 0x00fc);
401 /* Read back fb_cmd until zero or 3 seconds max */
402 for (cnt = 0; cnt < 3000; cnt++) {
403 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
404 break;
405 udelay(100);
409 /* Select RISC module registers. */
410 WRT_REG_WORD(&reg->ctrl_status, 0);
411 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
413 /* Reset RISC processor. */
414 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
415 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
417 /* Release RISC processor. */
418 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
419 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
422 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
423 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
425 /* Reset ISP chip. */
426 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
428 /* Wait for RISC to recover from reset. */
429 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
431 * It is necessary to for a delay here since the card doesn't
432 * respond to PCI reads during a reset. On some architectures
433 * this will result in an MCA.
435 udelay(20);
436 for (cnt = 30000; cnt; cnt--) {
437 if ((RD_REG_WORD(&reg->ctrl_status) &
438 CSR_ISP_SOFT_RESET) == 0)
439 break;
440 udelay(100);
442 } else
443 udelay(10);
445 /* Reset RISC processor. */
446 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
448 WRT_REG_WORD(&reg->semaphore, 0);
450 /* Release RISC processor. */
451 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
452 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
454 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
455 for (cnt = 0; cnt < 30000; cnt++) {
456 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
457 break;
459 udelay(100);
461 } else
462 udelay(100);
464 /* Turn on master enable */
465 cmd |= PCI_COMMAND_MASTER;
466 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
468 /* Disable RISC pause on FPM parity error. */
469 if (!IS_QLA2100(ha)) {
470 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
471 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
474 spin_unlock_irqrestore(&ha->hardware_lock, flags);
478 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
479 * @ha: HA context
481 * Returns 0 on success.
483 static inline void
484 qla24xx_reset_risc(scsi_qla_host_t *ha)
486 unsigned long flags = 0;
487 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
488 uint32_t cnt, d2;
489 uint16_t wd;
491 spin_lock_irqsave(&ha->hardware_lock, flags);
493 /* Reset RISC. */
494 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
495 for (cnt = 0; cnt < 30000; cnt++) {
496 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
497 break;
499 udelay(10);
502 WRT_REG_DWORD(&reg->ctrl_status,
503 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
504 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
506 udelay(100);
507 /* Wait for firmware to complete NVRAM accesses. */
508 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
509 for (cnt = 10000 ; cnt && d2; cnt--) {
510 udelay(5);
511 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
512 barrier();
515 /* Wait for soft-reset to complete. */
516 d2 = RD_REG_DWORD(&reg->ctrl_status);
517 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
518 udelay(5);
519 d2 = RD_REG_DWORD(&reg->ctrl_status);
520 barrier();
523 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
524 RD_REG_DWORD(&reg->hccr);
526 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
527 RD_REG_DWORD(&reg->hccr);
529 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
530 RD_REG_DWORD(&reg->hccr);
532 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
533 for (cnt = 6000000 ; cnt && d2; cnt--) {
534 udelay(5);
535 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
536 barrier();
539 spin_unlock_irqrestore(&ha->hardware_lock, flags);
543 * qla24xx_reset_chip() - Reset ISP24xx chip.
544 * @ha: HA context
546 * Returns 0 on success.
548 void
549 qla24xx_reset_chip(scsi_qla_host_t *ha)
551 ha->isp_ops.disable_intrs(ha);
553 /* Perform RISC reset. */
554 qla24xx_reset_risc(ha);
558 * qla2x00_chip_diag() - Test chip for proper operation.
559 * @ha: HA context
561 * Returns 0 on success.
564 qla2x00_chip_diag(scsi_qla_host_t *ha)
566 int rval;
567 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
568 unsigned long flags = 0;
569 uint16_t data;
570 uint32_t cnt;
571 uint16_t mb[5];
573 /* Assume a failed state */
574 rval = QLA_FUNCTION_FAILED;
576 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
577 ha->host_no, (u_long)&reg->flash_address));
579 spin_lock_irqsave(&ha->hardware_lock, flags);
581 /* Reset ISP chip. */
582 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
585 * We need to have a delay here since the card will not respond while
586 * in reset causing an MCA on some architectures.
588 udelay(20);
589 data = qla2x00_debounce_register(&reg->ctrl_status);
590 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
591 udelay(5);
592 data = RD_REG_WORD(&reg->ctrl_status);
593 barrier();
596 if (!cnt)
597 goto chip_diag_failed;
599 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
600 ha->host_no));
602 /* Reset RISC processor. */
603 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
604 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
606 /* Workaround for QLA2312 PCI parity error */
607 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
608 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
609 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
610 udelay(5);
611 data = RD_MAILBOX_REG(ha, reg, 0);
612 barrier();
614 } else
615 udelay(10);
617 if (!cnt)
618 goto chip_diag_failed;
620 /* Check product ID of chip */
621 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
623 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
624 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
625 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
626 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
627 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
628 mb[3] != PROD_ID_3) {
629 qla_printk(KERN_WARNING, ha,
630 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
632 goto chip_diag_failed;
634 ha->product_id[0] = mb[1];
635 ha->product_id[1] = mb[2];
636 ha->product_id[2] = mb[3];
637 ha->product_id[3] = mb[4];
639 /* Adjust fw RISC transfer size */
640 if (ha->request_q_length > 1024)
641 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
642 else
643 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
644 ha->request_q_length;
646 if (IS_QLA2200(ha) &&
647 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
648 /* Limit firmware transfer size with a 2200A */
649 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
650 ha->host_no));
652 ha->device_type |= DT_ISP2200A;
653 ha->fw_transfer_size = 128;
656 /* Wrap Incoming Mailboxes Test. */
657 spin_unlock_irqrestore(&ha->hardware_lock, flags);
659 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
660 rval = qla2x00_mbx_reg_test(ha);
661 if (rval) {
662 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
663 ha->host_no));
664 qla_printk(KERN_WARNING, ha,
665 "Failed mailbox send register test\n");
667 else {
668 /* Flag a successful rval */
669 rval = QLA_SUCCESS;
671 spin_lock_irqsave(&ha->hardware_lock, flags);
673 chip_diag_failed:
674 if (rval)
675 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
676 "****\n", ha->host_no));
678 spin_unlock_irqrestore(&ha->hardware_lock, flags);
680 return (rval);
684 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
685 * @ha: HA context
687 * Returns 0 on success.
690 qla24xx_chip_diag(scsi_qla_host_t *ha)
692 int rval;
694 /* Perform RISC reset. */
695 qla24xx_reset_risc(ha);
697 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
699 rval = qla2x00_mbx_reg_test(ha);
700 if (rval) {
701 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
702 ha->host_no));
703 qla_printk(KERN_WARNING, ha,
704 "Failed mailbox send register test\n");
705 } else {
706 /* Flag a successful rval */
707 rval = QLA_SUCCESS;
710 return rval;
713 void
714 qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
716 int rval;
717 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
718 eft_size;
719 dma_addr_t eft_dma;
720 void *eft;
722 if (ha->fw_dump) {
723 qla_printk(KERN_WARNING, ha,
724 "Firmware dump previously allocated.\n");
725 return;
728 ha->fw_dumped = 0;
729 fixed_size = mem_size = eft_size = 0;
730 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
731 fixed_size = sizeof(struct qla2100_fw_dump);
732 } else if (IS_QLA23XX(ha)) {
733 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
734 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
735 sizeof(uint16_t);
736 } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
737 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
738 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
739 sizeof(uint32_t);
741 /* Allocate memory for Extended Trace Buffer. */
742 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
743 GFP_KERNEL);
744 if (!eft) {
745 qla_printk(KERN_WARNING, ha, "Unable to allocate "
746 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
747 goto cont_alloc;
750 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
751 EFT_NUM_BUFFERS);
752 if (rval) {
753 qla_printk(KERN_WARNING, ha, "Unable to initialize "
754 "EFT (%d).\n", rval);
755 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
756 eft_dma);
757 goto cont_alloc;
760 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
761 EFT_SIZE / 1024);
763 eft_size = EFT_SIZE;
764 memset(eft, 0, eft_size);
765 ha->eft_dma = eft_dma;
766 ha->eft = eft;
768 cont_alloc:
769 req_q_size = ha->request_q_length * sizeof(request_t);
770 rsp_q_size = ha->response_q_length * sizeof(response_t);
772 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
773 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
774 eft_size;
776 ha->fw_dump = vmalloc(dump_size);
777 if (!ha->fw_dump) {
778 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
779 "firmware dump!!!\n", dump_size / 1024);
781 if (ha->eft) {
782 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
783 ha->eft_dma);
784 ha->eft = NULL;
785 ha->eft_dma = 0;
787 return;
790 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
791 dump_size / 1024);
793 ha->fw_dump_len = dump_size;
794 ha->fw_dump->signature[0] = 'Q';
795 ha->fw_dump->signature[1] = 'L';
796 ha->fw_dump->signature[2] = 'G';
797 ha->fw_dump->signature[3] = 'C';
798 ha->fw_dump->version = __constant_htonl(1);
800 ha->fw_dump->fixed_size = htonl(fixed_size);
801 ha->fw_dump->mem_size = htonl(mem_size);
802 ha->fw_dump->req_q_size = htonl(req_q_size);
803 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
805 ha->fw_dump->eft_size = htonl(eft_size);
806 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
807 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
809 ha->fw_dump->header_size =
810 htonl(offsetof(struct qla2xxx_fw_dump, isp));
814 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
815 * @ha: HA context
817 * Returns 0 on success.
819 static void
820 qla2x00_resize_request_q(scsi_qla_host_t *ha)
822 int rval;
823 uint16_t fw_iocb_cnt = 0;
824 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
825 dma_addr_t request_dma;
826 request_t *request_ring;
828 /* Valid only on recent ISPs. */
829 if (IS_QLA2100(ha) || IS_QLA2200(ha))
830 return;
832 /* Retrieve IOCB counts available to the firmware. */
833 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
834 if (rval)
835 return;
836 /* No point in continuing if current settings are sufficient. */
837 if (fw_iocb_cnt < 1024)
838 return;
839 if (ha->request_q_length >= request_q_length)
840 return;
842 /* Attempt to claim larger area for request queue. */
843 request_ring = dma_alloc_coherent(&ha->pdev->dev,
844 (request_q_length + 1) * sizeof(request_t), &request_dma,
845 GFP_KERNEL);
846 if (request_ring == NULL)
847 return;
849 /* Resize successful, report extensions. */
850 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
851 (ha->fw_memory_size + 1) / 1024);
852 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
853 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
855 /* Clear old allocations. */
856 dma_free_coherent(&ha->pdev->dev,
857 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
858 ha->request_dma);
860 /* Begin using larger queue. */
861 ha->request_q_length = request_q_length;
862 ha->request_ring = request_ring;
863 ha->request_dma = request_dma;
867 * qla2x00_setup_chip() - Load and start RISC firmware.
868 * @ha: HA context
870 * Returns 0 on success.
872 static int
873 qla2x00_setup_chip(scsi_qla_host_t *ha)
875 int rval;
876 uint32_t srisc_address = 0;
878 /* Load firmware sequences */
879 rval = ha->isp_ops.load_risc(ha, &srisc_address);
880 if (rval == QLA_SUCCESS) {
881 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
882 "code.\n", ha->host_no));
884 rval = qla2x00_verify_checksum(ha, srisc_address);
885 if (rval == QLA_SUCCESS) {
886 /* Start firmware execution. */
887 DEBUG(printk("scsi(%ld): Checksum OK, start "
888 "firmware.\n", ha->host_no));
890 rval = qla2x00_execute_fw(ha, srisc_address);
891 /* Retrieve firmware information. */
892 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
893 qla2x00_get_fw_version(ha,
894 &ha->fw_major_version,
895 &ha->fw_minor_version,
896 &ha->fw_subminor_version,
897 &ha->fw_attributes, &ha->fw_memory_size);
898 qla2x00_resize_request_q(ha);
900 if (ql2xallocfwdump)
901 qla2x00_alloc_fw_dump(ha);
903 } else {
904 DEBUG2(printk(KERN_INFO
905 "scsi(%ld): ISP Firmware failed checksum.\n",
906 ha->host_no));
910 if (rval) {
911 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
912 ha->host_no));
915 return (rval);
919 * qla2x00_init_response_q_entries() - Initializes response queue entries.
920 * @ha: HA context
922 * Beginning of request ring has initialization control block already built
923 * by nvram config routine.
925 * Returns 0 on success.
927 static void
928 qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
930 uint16_t cnt;
931 response_t *pkt;
933 pkt = ha->response_ring_ptr;
934 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
935 pkt->signature = RESPONSE_PROCESSED;
936 pkt++;
942 * qla2x00_update_fw_options() - Read and process firmware options.
943 * @ha: HA context
945 * Returns 0 on success.
947 void
948 qla2x00_update_fw_options(scsi_qla_host_t *ha)
950 uint16_t swing, emphasis, tx_sens, rx_sens;
952 memset(ha->fw_options, 0, sizeof(ha->fw_options));
953 qla2x00_get_fw_options(ha, ha->fw_options);
955 if (IS_QLA2100(ha) || IS_QLA2200(ha))
956 return;
958 /* Serial Link options. */
959 DEBUG3(printk("scsi(%ld): Serial link options:\n",
960 ha->host_no));
961 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
962 sizeof(ha->fw_seriallink_options)));
964 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
965 if (ha->fw_seriallink_options[3] & BIT_2) {
966 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
968 /* 1G settings */
969 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
970 emphasis = (ha->fw_seriallink_options[2] &
971 (BIT_4 | BIT_3)) >> 3;
972 tx_sens = ha->fw_seriallink_options[0] &
973 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
974 rx_sens = (ha->fw_seriallink_options[0] &
975 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
976 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
977 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
978 if (rx_sens == 0x0)
979 rx_sens = 0x3;
980 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
981 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
982 ha->fw_options[10] |= BIT_5 |
983 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
984 (tx_sens & (BIT_1 | BIT_0));
986 /* 2G settings */
987 swing = (ha->fw_seriallink_options[2] &
988 (BIT_7 | BIT_6 | BIT_5)) >> 5;
989 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
990 tx_sens = ha->fw_seriallink_options[1] &
991 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
992 rx_sens = (ha->fw_seriallink_options[1] &
993 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
994 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
995 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
996 if (rx_sens == 0x0)
997 rx_sens = 0x3;
998 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
999 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1000 ha->fw_options[11] |= BIT_5 |
1001 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1002 (tx_sens & (BIT_1 | BIT_0));
1005 /* FCP2 options. */
1006 /* Return command IOCBs without waiting for an ABTS to complete. */
1007 ha->fw_options[3] |= BIT_13;
1009 /* LED scheme. */
1010 if (ha->flags.enable_led_scheme)
1011 ha->fw_options[2] |= BIT_12;
1013 /* Detect ISP6312. */
1014 if (IS_QLA6312(ha))
1015 ha->fw_options[2] |= BIT_13;
1017 /* Update firmware options. */
1018 qla2x00_set_fw_options(ha, ha->fw_options);
1021 void
1022 qla24xx_update_fw_options(scsi_qla_host_t *ha)
1024 int rval;
1026 /* Update Serial Link options. */
1027 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
1028 return;
1030 rval = qla2x00_set_serdes_params(ha,
1031 le16_to_cpu(ha->fw_seriallink_options24[1]),
1032 le16_to_cpu(ha->fw_seriallink_options24[2]),
1033 le16_to_cpu(ha->fw_seriallink_options24[3]));
1034 if (rval != QLA_SUCCESS) {
1035 qla_printk(KERN_WARNING, ha,
1036 "Unable to update Serial Link options (%x).\n", rval);
1040 void
1041 qla2x00_config_rings(struct scsi_qla_host *ha)
1043 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1045 /* Setup ring parameters in initialization control block. */
1046 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1047 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1048 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1049 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1050 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1051 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1052 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1053 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1055 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1056 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1057 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1058 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1059 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1062 void
1063 qla24xx_config_rings(struct scsi_qla_host *ha)
1065 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1066 struct init_cb_24xx *icb;
1068 /* Setup ring parameters in initialization control block. */
1069 icb = (struct init_cb_24xx *)ha->init_cb;
1070 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1071 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1072 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1073 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1074 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1075 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1076 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1077 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1079 WRT_REG_DWORD(&reg->req_q_in, 0);
1080 WRT_REG_DWORD(&reg->req_q_out, 0);
1081 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1082 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1083 RD_REG_DWORD(&reg->rsp_q_out);
1087 * qla2x00_init_rings() - Initializes firmware.
1088 * @ha: HA context
1090 * Beginning of request ring has initialization control block already built
1091 * by nvram config routine.
1093 * Returns 0 on success.
1095 static int
1096 qla2x00_init_rings(scsi_qla_host_t *ha)
1098 int rval;
1099 unsigned long flags = 0;
1100 int cnt;
1102 spin_lock_irqsave(&ha->hardware_lock, flags);
1104 /* Clear outstanding commands array. */
1105 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1106 ha->outstanding_cmds[cnt] = NULL;
1108 ha->current_outstanding_cmd = 0;
1110 /* Clear RSCN queue. */
1111 ha->rscn_in_ptr = 0;
1112 ha->rscn_out_ptr = 0;
1114 /* Initialize firmware. */
1115 ha->request_ring_ptr = ha->request_ring;
1116 ha->req_ring_index = 0;
1117 ha->req_q_cnt = ha->request_q_length;
1118 ha->response_ring_ptr = ha->response_ring;
1119 ha->rsp_ring_index = 0;
1121 /* Initialize response queue entries */
1122 qla2x00_init_response_q_entries(ha);
1124 ha->isp_ops.config_rings(ha);
1126 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1128 /* Update any ISP specific firmware options before initialization. */
1129 ha->isp_ops.update_fw_options(ha);
1131 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
1132 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1133 if (rval) {
1134 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1135 ha->host_no));
1136 } else {
1137 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1138 ha->host_no));
1141 return (rval);
1145 * qla2x00_fw_ready() - Waits for firmware ready.
1146 * @ha: HA context
1148 * Returns 0 on success.
1150 static int
1151 qla2x00_fw_ready(scsi_qla_host_t *ha)
1153 int rval;
1154 unsigned long wtime, mtime;
1155 uint16_t min_wait; /* Minimum wait time if loop is down */
1156 uint16_t wait_time; /* Wait time if loop is coming ready */
1157 uint16_t fw_state;
1159 rval = QLA_SUCCESS;
1161 /* 20 seconds for loop down. */
1162 min_wait = 20;
1165 * Firmware should take at most one RATOV to login, plus 5 seconds for
1166 * our own processing.
1168 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1169 wait_time = min_wait;
1172 /* Min wait time if loop down */
1173 mtime = jiffies + (min_wait * HZ);
1175 /* wait time before firmware ready */
1176 wtime = jiffies + (wait_time * HZ);
1178 /* Wait for ISP to finish LIP */
1179 if (!ha->flags.init_done)
1180 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1182 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1183 ha->host_no));
1185 do {
1186 rval = qla2x00_get_firmware_state(ha, &fw_state);
1187 if (rval == QLA_SUCCESS) {
1188 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1189 ha->device_flags &= ~DFLG_NO_CABLE;
1191 if (fw_state == FSTATE_READY) {
1192 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1193 ha->host_no));
1195 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1196 &ha->login_timeout, &ha->r_a_tov);
1198 rval = QLA_SUCCESS;
1199 break;
1202 rval = QLA_FUNCTION_FAILED;
1204 if (atomic_read(&ha->loop_down_timer) &&
1205 fw_state != FSTATE_READY) {
1206 /* Loop down. Timeout on min_wait for states
1207 * other than Wait for Login.
1209 if (time_after_eq(jiffies, mtime)) {
1210 qla_printk(KERN_INFO, ha,
1211 "Cable is unplugged...\n");
1213 ha->device_flags |= DFLG_NO_CABLE;
1214 break;
1217 } else {
1218 /* Mailbox cmd failed. Timeout on min_wait. */
1219 if (time_after_eq(jiffies, mtime))
1220 break;
1223 if (time_after_eq(jiffies, wtime))
1224 break;
1226 /* Delay for a while */
1227 msleep(500);
1229 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1230 ha->host_no, fw_state, jiffies));
1231 } while (1);
1233 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1234 ha->host_no, fw_state, jiffies));
1236 if (rval) {
1237 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1238 ha->host_no));
1241 return (rval);
1245 * qla2x00_configure_hba
1246 * Setup adapter context.
1248 * Input:
1249 * ha = adapter state pointer.
1251 * Returns:
1252 * 0 = success
1254 * Context:
1255 * Kernel context.
1257 static int
1258 qla2x00_configure_hba(scsi_qla_host_t *ha)
1260 int rval;
1261 uint16_t loop_id;
1262 uint16_t topo;
1263 uint8_t al_pa;
1264 uint8_t area;
1265 uint8_t domain;
1266 char connect_type[22];
1268 /* Get host addresses. */
1269 rval = qla2x00_get_adapter_id(ha,
1270 &loop_id, &al_pa, &area, &domain, &topo);
1271 if (rval != QLA_SUCCESS) {
1272 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
1273 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1274 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1275 __func__, ha->host_no));
1276 } else {
1277 qla_printk(KERN_WARNING, ha,
1278 "ERROR -- Unable to get host loop ID.\n");
1279 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1281 return (rval);
1284 if (topo == 4) {
1285 qla_printk(KERN_INFO, ha,
1286 "Cannot get topology - retrying.\n");
1287 return (QLA_FUNCTION_FAILED);
1290 ha->loop_id = loop_id;
1292 /* initialize */
1293 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1294 ha->operating_mode = LOOP;
1296 switch (topo) {
1297 case 0:
1298 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1299 ha->host_no));
1300 ha->current_topology = ISP_CFG_NL;
1301 strcpy(connect_type, "(Loop)");
1302 break;
1304 case 1:
1305 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1306 ha->host_no));
1307 ha->current_topology = ISP_CFG_FL;
1308 strcpy(connect_type, "(FL_Port)");
1309 break;
1311 case 2:
1312 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1313 ha->host_no));
1314 ha->operating_mode = P2P;
1315 ha->current_topology = ISP_CFG_N;
1316 strcpy(connect_type, "(N_Port-to-N_Port)");
1317 break;
1319 case 3:
1320 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1321 ha->host_no));
1322 ha->operating_mode = P2P;
1323 ha->current_topology = ISP_CFG_F;
1324 strcpy(connect_type, "(F_Port)");
1325 break;
1327 default:
1328 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1329 "Using NL.\n",
1330 ha->host_no, topo));
1331 ha->current_topology = ISP_CFG_NL;
1332 strcpy(connect_type, "(Loop)");
1333 break;
1336 /* Save Host port and loop ID. */
1337 /* byte order - Big Endian */
1338 ha->d_id.b.domain = domain;
1339 ha->d_id.b.area = area;
1340 ha->d_id.b.al_pa = al_pa;
1342 if (!ha->flags.init_done)
1343 qla_printk(KERN_INFO, ha,
1344 "Topology - %s, Host Loop address 0x%x\n",
1345 connect_type, ha->loop_id);
1347 if (rval) {
1348 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1349 } else {
1350 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1353 return(rval);
1357 * NVRAM configuration for ISP 2xxx
1359 * Input:
1360 * ha = adapter block pointer.
1362 * Output:
1363 * initialization control block in response_ring
1364 * host adapters parameters in host adapter block
1366 * Returns:
1367 * 0 = success.
1370 qla2x00_nvram_config(scsi_qla_host_t *ha)
1372 int rval;
1373 uint8_t chksum = 0;
1374 uint16_t cnt;
1375 uint8_t *dptr1, *dptr2;
1376 init_cb_t *icb = ha->init_cb;
1377 nvram_t *nv = (nvram_t *)ha->request_ring;
1378 uint8_t *ptr = (uint8_t *)ha->request_ring;
1379 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1381 rval = QLA_SUCCESS;
1383 /* Determine NVRAM starting address. */
1384 ha->nvram_size = sizeof(nvram_t);
1385 ha->nvram_base = 0;
1386 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1387 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1388 ha->nvram_base = 0x80;
1390 /* Get NVRAM data and calculate checksum. */
1391 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1392 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1393 chksum += *ptr++;
1395 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1396 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1397 ha->nvram_size));
1399 /* Bad NVRAM data, set defaults parameters. */
1400 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1401 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1402 /* Reset NVRAM data. */
1403 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1404 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1405 nv->nvram_version);
1406 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1407 "invalid -- WWPN) defaults.\n");
1410 * Set default initialization control block.
1412 memset(nv, 0, ha->nvram_size);
1413 nv->parameter_block_version = ICB_VERSION;
1415 if (IS_QLA23XX(ha)) {
1416 nv->firmware_options[0] = BIT_2 | BIT_1;
1417 nv->firmware_options[1] = BIT_7 | BIT_5;
1418 nv->add_firmware_options[0] = BIT_5;
1419 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1420 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1421 nv->special_options[1] = BIT_7;
1422 } else if (IS_QLA2200(ha)) {
1423 nv->firmware_options[0] = BIT_2 | BIT_1;
1424 nv->firmware_options[1] = BIT_7 | BIT_5;
1425 nv->add_firmware_options[0] = BIT_5;
1426 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1427 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1428 } else if (IS_QLA2100(ha)) {
1429 nv->firmware_options[0] = BIT_3 | BIT_1;
1430 nv->firmware_options[1] = BIT_5;
1431 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1434 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1435 nv->execution_throttle = __constant_cpu_to_le16(16);
1436 nv->retry_count = 8;
1437 nv->retry_delay = 1;
1439 nv->port_name[0] = 33;
1440 nv->port_name[3] = 224;
1441 nv->port_name[4] = 139;
1443 nv->login_timeout = 4;
1446 * Set default host adapter parameters
1448 nv->host_p[1] = BIT_2;
1449 nv->reset_delay = 5;
1450 nv->port_down_retry_count = 8;
1451 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1452 nv->link_down_timeout = 60;
1454 rval = 1;
1457 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1459 * The SN2 does not provide BIOS emulation which means you can't change
1460 * potentially bogus BIOS settings. Force the use of default settings
1461 * for link rate and frame size. Hope that the rest of the settings
1462 * are valid.
1464 if (ia64_platform_is("sn2")) {
1465 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1466 if (IS_QLA23XX(ha))
1467 nv->special_options[1] = BIT_7;
1469 #endif
1471 /* Reset Initialization control block */
1472 memset(icb, 0, ha->init_cb_size);
1475 * Setup driver NVRAM options.
1477 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1478 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1479 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1480 nv->firmware_options[1] &= ~BIT_4;
1482 if (IS_QLA23XX(ha)) {
1483 nv->firmware_options[0] |= BIT_2;
1484 nv->firmware_options[0] &= ~BIT_3;
1485 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1487 if (IS_QLA2300(ha)) {
1488 if (ha->fb_rev == FPM_2310) {
1489 strcpy(ha->model_number, "QLA2310");
1490 } else {
1491 strcpy(ha->model_number, "QLA2300");
1493 } else {
1494 if (rval == 0 &&
1495 memcmp(nv->model_number, BINZERO,
1496 sizeof(nv->model_number)) != 0) {
1497 char *st, *en;
1499 strncpy(ha->model_number, nv->model_number,
1500 sizeof(nv->model_number));
1501 st = en = ha->model_number;
1502 en += sizeof(nv->model_number) - 1;
1503 while (en > st) {
1504 if (*en != 0x20 && *en != 0x00)
1505 break;
1506 *en-- = '\0';
1508 } else {
1509 uint16_t index;
1511 index = (ha->pdev->subsystem_device & 0xff);
1512 if (index < QLA_MODEL_NAMES) {
1513 strcpy(ha->model_number,
1514 qla2x00_model_name[index * 2]);
1515 ha->model_desc =
1516 qla2x00_model_name[index * 2 + 1];
1517 } else {
1518 strcpy(ha->model_number, "QLA23xx");
1522 } else if (IS_QLA2200(ha)) {
1523 nv->firmware_options[0] |= BIT_2;
1525 * 'Point-to-point preferred, else loop' is not a safe
1526 * connection mode setting.
1528 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1529 (BIT_5 | BIT_4)) {
1530 /* Force 'loop preferred, else point-to-point'. */
1531 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1532 nv->add_firmware_options[0] |= BIT_5;
1534 strcpy(ha->model_number, "QLA22xx");
1535 } else /*if (IS_QLA2100(ha))*/ {
1536 strcpy(ha->model_number, "QLA2100");
1540 * Copy over NVRAM RISC parameter block to initialization control block.
1542 dptr1 = (uint8_t *)icb;
1543 dptr2 = (uint8_t *)&nv->parameter_block_version;
1544 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1545 while (cnt--)
1546 *dptr1++ = *dptr2++;
1548 /* Copy 2nd half. */
1549 dptr1 = (uint8_t *)icb->add_firmware_options;
1550 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1551 while (cnt--)
1552 *dptr1++ = *dptr2++;
1554 /* Use alternate WWN? */
1555 if (nv->host_p[1] & BIT_7) {
1556 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1557 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1560 /* Prepare nodename */
1561 if ((icb->firmware_options[1] & BIT_6) == 0) {
1563 * Firmware will apply the following mask if the nodename was
1564 * not provided.
1566 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1567 icb->node_name[0] &= 0xF0;
1571 * Set host adapter parameters.
1573 if (nv->host_p[0] & BIT_7)
1574 ql2xextended_error_logging = 1;
1575 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1576 /* Always load RISC code on non ISP2[12]00 chips. */
1577 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1578 ha->flags.disable_risc_code_load = 0;
1579 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1580 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1581 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1582 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1583 ha->flags.disable_serdes = 0;
1585 ha->operating_mode =
1586 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1588 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1589 sizeof(ha->fw_seriallink_options));
1591 /* save HBA serial number */
1592 ha->serial0 = icb->port_name[5];
1593 ha->serial1 = icb->port_name[6];
1594 ha->serial2 = icb->port_name[7];
1595 ha->node_name = icb->node_name;
1596 ha->port_name = icb->port_name;
1598 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1600 ha->retry_count = nv->retry_count;
1602 /* Set minimum login_timeout to 4 seconds. */
1603 if (nv->login_timeout < ql2xlogintimeout)
1604 nv->login_timeout = ql2xlogintimeout;
1605 if (nv->login_timeout < 4)
1606 nv->login_timeout = 4;
1607 ha->login_timeout = nv->login_timeout;
1608 icb->login_timeout = nv->login_timeout;
1610 /* Set minimum RATOV to 200 tenths of a second. */
1611 ha->r_a_tov = 200;
1613 ha->loop_reset_delay = nv->reset_delay;
1615 /* Link Down Timeout = 0:
1617 * When Port Down timer expires we will start returning
1618 * I/O's to OS with "DID_NO_CONNECT".
1620 * Link Down Timeout != 0:
1622 * The driver waits for the link to come up after link down
1623 * before returning I/Os to OS with "DID_NO_CONNECT".
1625 if (nv->link_down_timeout == 0) {
1626 ha->loop_down_abort_time =
1627 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1628 } else {
1629 ha->link_down_timeout = nv->link_down_timeout;
1630 ha->loop_down_abort_time =
1631 (LOOP_DOWN_TIME - ha->link_down_timeout);
1635 * Need enough time to try and get the port back.
1637 ha->port_down_retry_count = nv->port_down_retry_count;
1638 if (qlport_down_retry)
1639 ha->port_down_retry_count = qlport_down_retry;
1640 /* Set login_retry_count */
1641 ha->login_retry_count = nv->retry_count;
1642 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1643 ha->port_down_retry_count > 3)
1644 ha->login_retry_count = ha->port_down_retry_count;
1645 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1646 ha->login_retry_count = ha->port_down_retry_count;
1647 if (ql2xloginretrycount)
1648 ha->login_retry_count = ql2xloginretrycount;
1650 icb->lun_enables = __constant_cpu_to_le16(0);
1651 icb->command_resource_count = 0;
1652 icb->immediate_notify_resource_count = 0;
1653 icb->timeout = __constant_cpu_to_le16(0);
1655 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1656 /* Enable RIO */
1657 icb->firmware_options[0] &= ~BIT_3;
1658 icb->add_firmware_options[0] &=
1659 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1660 icb->add_firmware_options[0] |= BIT_2;
1661 icb->response_accumulation_timer = 3;
1662 icb->interrupt_delay_timer = 5;
1664 ha->flags.process_response_queue = 1;
1665 } else {
1666 /* Enable ZIO. */
1667 if (!ha->flags.init_done) {
1668 ha->zio_mode = icb->add_firmware_options[0] &
1669 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1670 ha->zio_timer = icb->interrupt_delay_timer ?
1671 icb->interrupt_delay_timer: 2;
1673 icb->add_firmware_options[0] &=
1674 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1675 ha->flags.process_response_queue = 0;
1676 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1677 ha->zio_mode = QLA_ZIO_MODE_6;
1679 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1680 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1681 ha->zio_timer * 100));
1682 qla_printk(KERN_INFO, ha,
1683 "ZIO mode %d enabled; timer delay (%d us).\n",
1684 ha->zio_mode, ha->zio_timer * 100);
1686 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1687 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1688 ha->flags.process_response_queue = 1;
1692 if (rval) {
1693 DEBUG2_3(printk(KERN_WARNING
1694 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1696 return (rval);
1699 static void
1700 qla2x00_rport_del(void *data)
1702 fc_port_t *fcport = data;
1703 struct fc_rport *rport;
1704 unsigned long flags;
1706 spin_lock_irqsave(&fcport->rport_lock, flags);
1707 rport = fcport->drport;
1708 fcport->drport = NULL;
1709 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1710 if (rport)
1711 fc_remote_port_delete(rport);
1716 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1717 * @ha: HA context
1718 * @flags: allocation flags
1720 * Returns a pointer to the allocated fcport, or NULL, if none available.
1722 static fc_port_t *
1723 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1725 fc_port_t *fcport;
1727 fcport = kmalloc(sizeof(fc_port_t), flags);
1728 if (fcport == NULL)
1729 return (fcport);
1731 /* Setup fcport template structure. */
1732 memset(fcport, 0, sizeof (fc_port_t));
1733 fcport->ha = ha;
1734 fcport->port_type = FCT_UNKNOWN;
1735 fcport->loop_id = FC_NO_LOOP_ID;
1736 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1737 fcport->flags = FCF_RLC_SUPPORT;
1738 fcport->supported_classes = FC_COS_UNSPECIFIED;
1739 spin_lock_init(&fcport->rport_lock);
1741 return (fcport);
1745 * qla2x00_configure_loop
1746 * Updates Fibre Channel Device Database with what is actually on loop.
1748 * Input:
1749 * ha = adapter block pointer.
1751 * Returns:
1752 * 0 = success.
1753 * 1 = error.
1754 * 2 = database was full and device was not configured.
1756 static int
1757 qla2x00_configure_loop(scsi_qla_host_t *ha)
1759 int rval;
1760 unsigned long flags, save_flags;
1762 rval = QLA_SUCCESS;
1764 /* Get Initiator ID */
1765 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1766 rval = qla2x00_configure_hba(ha);
1767 if (rval != QLA_SUCCESS) {
1768 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1769 ha->host_no));
1770 return (rval);
1774 save_flags = flags = ha->dpc_flags;
1775 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1776 ha->host_no, flags));
1779 * If we have both an RSCN and PORT UPDATE pending then handle them
1780 * both at the same time.
1782 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1783 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1785 /* Determine what we need to do */
1786 if (ha->current_topology == ISP_CFG_FL &&
1787 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1789 ha->flags.rscn_queue_overflow = 1;
1790 set_bit(RSCN_UPDATE, &flags);
1792 } else if (ha->current_topology == ISP_CFG_F &&
1793 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1795 ha->flags.rscn_queue_overflow = 1;
1796 set_bit(RSCN_UPDATE, &flags);
1797 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1799 } else if (ha->current_topology == ISP_CFG_N) {
1800 clear_bit(RSCN_UPDATE, &flags);
1802 } else if (!ha->flags.online ||
1803 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1805 ha->flags.rscn_queue_overflow = 1;
1806 set_bit(RSCN_UPDATE, &flags);
1807 set_bit(LOCAL_LOOP_UPDATE, &flags);
1810 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1811 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1812 rval = QLA_FUNCTION_FAILED;
1813 } else {
1814 rval = qla2x00_configure_local_loop(ha);
1818 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1819 if (LOOP_TRANSITION(ha)) {
1820 rval = QLA_FUNCTION_FAILED;
1821 } else {
1822 rval = qla2x00_configure_fabric(ha);
1826 if (rval == QLA_SUCCESS) {
1827 if (atomic_read(&ha->loop_down_timer) ||
1828 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1829 rval = QLA_FUNCTION_FAILED;
1830 } else {
1831 atomic_set(&ha->loop_state, LOOP_READY);
1833 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1837 if (rval) {
1838 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1839 __func__, ha->host_no));
1840 } else {
1841 DEBUG3(printk("%s: exiting normally\n", __func__));
1844 /* Restore state if a resync event occured during processing */
1845 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1846 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1847 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1848 if (test_bit(RSCN_UPDATE, &save_flags))
1849 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1852 return (rval);
1858 * qla2x00_configure_local_loop
1859 * Updates Fibre Channel Device Database with local loop devices.
1861 * Input:
1862 * ha = adapter block pointer.
1864 * Returns:
1865 * 0 = success.
1867 static int
1868 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1870 int rval, rval2;
1871 int found_devs;
1872 int found;
1873 fc_port_t *fcport, *new_fcport;
1875 uint16_t index;
1876 uint16_t entries;
1877 char *id_iter;
1878 uint16_t loop_id;
1879 uint8_t domain, area, al_pa;
1881 found_devs = 0;
1882 new_fcport = NULL;
1883 entries = MAX_FIBRE_DEVICES;
1885 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1886 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1888 /* Get list of logged in devices. */
1889 memset(ha->gid_list, 0, GID_LIST_SIZE);
1890 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1891 &entries);
1892 if (rval != QLA_SUCCESS)
1893 goto cleanup_allocation;
1895 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1896 ha->host_no, entries));
1897 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1898 entries * sizeof(struct gid_list_info)));
1900 /* Allocate temporary fcport for any new fcports discovered. */
1901 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1902 if (new_fcport == NULL) {
1903 rval = QLA_MEMORY_ALLOC_FAILED;
1904 goto cleanup_allocation;
1906 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1909 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1911 list_for_each_entry(fcport, &ha->fcports, list) {
1912 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1913 fcport->port_type != FCT_BROADCAST &&
1914 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1916 DEBUG(printk("scsi(%ld): Marking port lost, "
1917 "loop_id=0x%04x\n",
1918 ha->host_no, fcport->loop_id));
1920 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1921 fcport->flags &= ~FCF_FARP_DONE;
1925 /* Add devices to port list. */
1926 id_iter = (char *)ha->gid_list;
1927 for (index = 0; index < entries; index++) {
1928 domain = ((struct gid_list_info *)id_iter)->domain;
1929 area = ((struct gid_list_info *)id_iter)->area;
1930 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1931 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1932 loop_id = (uint16_t)
1933 ((struct gid_list_info *)id_iter)->loop_id_2100;
1934 else
1935 loop_id = le16_to_cpu(
1936 ((struct gid_list_info *)id_iter)->loop_id);
1937 id_iter += ha->gid_list_info_size;
1939 /* Bypass reserved domain fields. */
1940 if ((domain & 0xf0) == 0xf0)
1941 continue;
1943 /* Bypass if not same domain and area of adapter. */
1944 if (area && domain &&
1945 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
1946 continue;
1948 /* Bypass invalid local loop ID. */
1949 if (loop_id > LAST_LOCAL_LOOP_ID)
1950 continue;
1952 /* Fill in member data. */
1953 new_fcport->d_id.b.domain = domain;
1954 new_fcport->d_id.b.area = area;
1955 new_fcport->d_id.b.al_pa = al_pa;
1956 new_fcport->loop_id = loop_id;
1957 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1958 if (rval2 != QLA_SUCCESS) {
1959 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1960 "information -- get_port_database=%x, "
1961 "loop_id=0x%04x\n",
1962 ha->host_no, rval2, new_fcport->loop_id));
1963 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
1964 ha->host_no));
1965 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
1966 continue;
1969 /* Check for matching device in port list. */
1970 found = 0;
1971 fcport = NULL;
1972 list_for_each_entry(fcport, &ha->fcports, list) {
1973 if (memcmp(new_fcport->port_name, fcport->port_name,
1974 WWN_SIZE))
1975 continue;
1977 fcport->flags &= ~(FCF_FABRIC_DEVICE |
1978 FCF_PERSISTENT_BOUND);
1979 fcport->loop_id = new_fcport->loop_id;
1980 fcport->port_type = new_fcport->port_type;
1981 fcport->d_id.b24 = new_fcport->d_id.b24;
1982 memcpy(fcport->node_name, new_fcport->node_name,
1983 WWN_SIZE);
1985 found++;
1986 break;
1989 if (!found) {
1990 /* New device, add to fcports list. */
1991 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
1992 list_add_tail(&new_fcport->list, &ha->fcports);
1994 /* Allocate a new replacement fcport. */
1995 fcport = new_fcport;
1996 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1997 if (new_fcport == NULL) {
1998 rval = QLA_MEMORY_ALLOC_FAILED;
1999 goto cleanup_allocation;
2001 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2004 /* Base iIDMA settings on HBA port speed. */
2005 switch (ha->link_data_rate) {
2006 case PORT_SPEED_1GB:
2007 fcport->fp_speed = cpu_to_be16(BIT_15);
2008 break;
2009 case PORT_SPEED_2GB:
2010 fcport->fp_speed = cpu_to_be16(BIT_14);
2011 break;
2012 case PORT_SPEED_4GB:
2013 fcport->fp_speed = cpu_to_be16(BIT_13);
2014 break;
2017 qla2x00_update_fcport(ha, fcport);
2019 found_devs++;
2022 cleanup_allocation:
2023 kfree(new_fcport);
2025 if (rval != QLA_SUCCESS) {
2026 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2027 "rval=%x\n", ha->host_no, rval));
2030 if (found_devs) {
2031 ha->device_flags |= DFLG_LOCAL_DEVICES;
2032 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2035 return (rval);
2038 static void
2039 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2041 fc_port_t *fcport;
2043 qla2x00_mark_all_devices_lost(ha, 0);
2044 list_for_each_entry(fcport, &ha->fcports, list) {
2045 if (fcport->port_type != FCT_TARGET)
2046 continue;
2048 qla2x00_update_fcport(ha, fcport);
2052 static void
2053 qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2055 #define LS_UNKNOWN 2
2056 static char *link_speeds[5] = { "1", "2", "?", "4" };
2057 int rval;
2058 uint16_t port_speed, mb[6];
2060 if (!IS_QLA24XX(ha))
2061 return;
2063 switch (be16_to_cpu(fcport->fp_speed)) {
2064 case BIT_15:
2065 port_speed = PORT_SPEED_1GB;
2066 break;
2067 case BIT_14:
2068 port_speed = PORT_SPEED_2GB;
2069 break;
2070 case BIT_13:
2071 port_speed = PORT_SPEED_4GB;
2072 break;
2073 default:
2074 DEBUG2(printk("scsi(%ld): %02x%02x%02x%02x%02x%02x%02x%02x -- "
2075 "unsupported FM port operating speed (%04x).\n",
2076 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2077 fcport->port_name[2], fcport->port_name[3],
2078 fcport->port_name[4], fcport->port_name[5],
2079 fcport->port_name[6], fcport->port_name[7],
2080 be16_to_cpu(fcport->fp_speed)));
2081 port_speed = PORT_SPEED_UNKNOWN;
2082 break;
2084 if (port_speed == PORT_SPEED_UNKNOWN)
2085 return;
2087 rval = qla2x00_set_idma_speed(ha, fcport->loop_id, port_speed, mb);
2088 if (rval != QLA_SUCCESS) {
2089 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2090 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2091 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2092 fcport->port_name[2], fcport->port_name[3],
2093 fcport->port_name[4], fcport->port_name[5],
2094 fcport->port_name[6], fcport->port_name[7], rval,
2095 port_speed, mb[0], mb[1]));
2096 } else {
2097 DEBUG2(qla_printk(KERN_INFO, ha,
2098 "iIDMA adjusted to %s GB/s on "
2099 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2100 link_speeds[port_speed], fcport->port_name[0],
2101 fcport->port_name[1], fcport->port_name[2],
2102 fcport->port_name[3], fcport->port_name[4],
2103 fcport->port_name[5], fcport->port_name[6],
2104 fcport->port_name[7]));
2109 * qla2x00_update_fcport
2110 * Updates device on list.
2112 * Input:
2113 * ha = adapter block pointer.
2114 * fcport = port structure pointer.
2116 * Return:
2117 * 0 - Success
2118 * BIT_0 - error
2120 * Context:
2121 * Kernel context.
2123 void
2124 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2126 fcport->ha = ha;
2127 fcport->login_retry = 0;
2128 fcport->port_login_retry_count = ha->port_down_retry_count *
2129 PORT_RETRY_TIME;
2130 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2131 PORT_RETRY_TIME);
2132 fcport->flags &= ~FCF_LOGIN_NEEDED;
2134 qla2x00_iidma_fcport(ha, fcport);
2136 atomic_set(&fcport->state, FCS_ONLINE);
2138 qla2x00_reg_remote_port(ha, fcport);
2141 void
2142 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2144 struct fc_rport_identifiers rport_ids;
2145 struct fc_rport *rport;
2146 unsigned long flags;
2148 if (fcport->drport)
2149 qla2x00_rport_del(fcport);
2150 if (fcport->rport)
2151 return;
2153 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2154 rport_ids.port_name = wwn_to_u64(fcport->port_name);
2155 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2156 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2157 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2158 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2159 if (!rport) {
2160 qla_printk(KERN_WARNING, ha,
2161 "Unable to allocate fc remote port!\n");
2162 return;
2164 spin_lock_irqsave(&fcport->rport_lock, flags);
2165 fcport->rport = rport;
2166 *((fc_port_t **)rport->dd_data) = fcport;
2167 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2169 rport->supported_classes = fcport->supported_classes;
2171 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2172 if (fcport->port_type == FCT_INITIATOR)
2173 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2174 if (fcport->port_type == FCT_TARGET)
2175 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2176 fc_remote_port_rolechg(rport, rport_ids.roles);
2178 if (rport->scsi_target_id != -1 &&
2179 rport->scsi_target_id < ha->host->max_id)
2180 fcport->os_target_id = rport->scsi_target_id;
2184 * qla2x00_configure_fabric
2185 * Setup SNS devices with loop ID's.
2187 * Input:
2188 * ha = adapter block pointer.
2190 * Returns:
2191 * 0 = success.
2192 * BIT_0 = error
2194 static int
2195 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2197 int rval, rval2;
2198 fc_port_t *fcport, *fcptemp;
2199 uint16_t next_loopid;
2200 uint16_t mb[MAILBOX_REGISTER_COUNT];
2201 uint16_t loop_id;
2202 LIST_HEAD(new_fcports);
2204 /* If FL port exists, then SNS is present */
2205 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2206 loop_id = NPH_F_PORT;
2207 else
2208 loop_id = SNS_FL_PORT;
2209 rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
2210 if (rval != QLA_SUCCESS) {
2211 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2212 "Port\n", ha->host_no));
2214 ha->device_flags &= ~SWITCH_FOUND;
2215 return (QLA_SUCCESS);
2217 ha->device_flags |= SWITCH_FOUND;
2219 /* Mark devices that need re-synchronization. */
2220 rval2 = qla2x00_device_resync(ha);
2221 if (rval2 == QLA_RSCNS_HANDLED) {
2222 /* No point doing the scan, just continue. */
2223 return (QLA_SUCCESS);
2225 do {
2226 /* FDMI support. */
2227 if (ql2xfdmienable &&
2228 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2229 qla2x00_fdmi_register(ha);
2231 /* Ensure we are logged into the SNS. */
2232 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2233 loop_id = NPH_SNS;
2234 else
2235 loop_id = SIMPLE_NAME_SERVER;
2236 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
2237 0xfc, mb, BIT_1 | BIT_0);
2238 if (mb[0] != MBS_COMMAND_COMPLETE) {
2239 DEBUG2(qla_printk(KERN_INFO, ha,
2240 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2241 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2242 mb[0], mb[1], mb[2], mb[6], mb[7]));
2243 return (QLA_SUCCESS);
2246 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2247 if (qla2x00_rft_id(ha)) {
2248 /* EMPTY */
2249 DEBUG2(printk("scsi(%ld): Register FC-4 "
2250 "TYPE failed.\n", ha->host_no));
2252 if (qla2x00_rff_id(ha)) {
2253 /* EMPTY */
2254 DEBUG2(printk("scsi(%ld): Register FC-4 "
2255 "Features failed.\n", ha->host_no));
2257 if (qla2x00_rnn_id(ha)) {
2258 /* EMPTY */
2259 DEBUG2(printk("scsi(%ld): Register Node Name "
2260 "failed.\n", ha->host_no));
2261 } else if (qla2x00_rsnn_nn(ha)) {
2262 /* EMPTY */
2263 DEBUG2(printk("scsi(%ld): Register Symbolic "
2264 "Node Name failed.\n", ha->host_no));
2268 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2269 if (rval != QLA_SUCCESS)
2270 break;
2273 * Logout all previous fabric devices marked lost, except
2274 * tape devices.
2276 list_for_each_entry(fcport, &ha->fcports, list) {
2277 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2278 break;
2280 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2281 continue;
2283 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2284 qla2x00_mark_device_lost(ha, fcport,
2285 ql2xplogiabsentdevice, 0);
2286 if (fcport->loop_id != FC_NO_LOOP_ID &&
2287 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2288 fcport->port_type != FCT_INITIATOR &&
2289 fcport->port_type != FCT_BROADCAST) {
2290 ha->isp_ops.fabric_logout(ha,
2291 fcport->loop_id,
2292 fcport->d_id.b.domain,
2293 fcport->d_id.b.area,
2294 fcport->d_id.b.al_pa);
2295 fcport->loop_id = FC_NO_LOOP_ID;
2300 /* Starting free loop ID. */
2301 next_loopid = ha->min_external_loopid;
2304 * Scan through our port list and login entries that need to be
2305 * logged in.
2307 list_for_each_entry(fcport, &ha->fcports, list) {
2308 if (atomic_read(&ha->loop_down_timer) ||
2309 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2310 break;
2312 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2313 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2314 continue;
2316 if (fcport->loop_id == FC_NO_LOOP_ID) {
2317 fcport->loop_id = next_loopid;
2318 rval = qla2x00_find_new_loop_id(ha, fcport);
2319 if (rval != QLA_SUCCESS) {
2320 /* Ran out of IDs to use */
2321 break;
2324 /* Login and update database */
2325 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2328 /* Exit if out of loop IDs. */
2329 if (rval != QLA_SUCCESS) {
2330 break;
2334 * Login and add the new devices to our port list.
2336 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2337 if (atomic_read(&ha->loop_down_timer) ||
2338 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2339 break;
2341 /* Find a new loop ID to use. */
2342 fcport->loop_id = next_loopid;
2343 rval = qla2x00_find_new_loop_id(ha, fcport);
2344 if (rval != QLA_SUCCESS) {
2345 /* Ran out of IDs to use */
2346 break;
2349 /* Remove device from the new list and add it to DB */
2350 list_move_tail(&fcport->list, &ha->fcports);
2352 /* Login and update database */
2353 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2355 } while (0);
2357 /* Free all new device structures not processed. */
2358 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2359 list_del(&fcport->list);
2360 kfree(fcport);
2363 if (rval) {
2364 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2365 "rval=%d\n", ha->host_no, rval));
2368 return (rval);
2373 * qla2x00_find_all_fabric_devs
2375 * Input:
2376 * ha = adapter block pointer.
2377 * dev = database device entry pointer.
2379 * Returns:
2380 * 0 = success.
2382 * Context:
2383 * Kernel context.
2385 static int
2386 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2388 int rval;
2389 uint16_t loop_id;
2390 fc_port_t *fcport, *new_fcport, *fcptemp;
2391 int found;
2393 sw_info_t *swl;
2394 int swl_idx;
2395 int first_dev, last_dev;
2396 port_id_t wrap, nxt_d_id;
2398 rval = QLA_SUCCESS;
2400 /* Try GID_PT to get device list, else GAN. */
2401 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2402 if (swl == NULL) {
2403 /*EMPTY*/
2404 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2405 "on GA_NXT\n", ha->host_no));
2406 } else {
2407 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2408 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2409 kfree(swl);
2410 swl = NULL;
2411 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2412 kfree(swl);
2413 swl = NULL;
2414 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2415 kfree(swl);
2416 swl = NULL;
2417 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2418 qla2x00_gpsc(ha, swl);
2421 swl_idx = 0;
2423 /* Allocate temporary fcport for any new fcports discovered. */
2424 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2425 if (new_fcport == NULL) {
2426 kfree(swl);
2427 return (QLA_MEMORY_ALLOC_FAILED);
2429 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2431 /* Set start port ID scan at adapter ID. */
2432 first_dev = 1;
2433 last_dev = 0;
2435 /* Starting free loop ID. */
2436 loop_id = ha->min_external_loopid;
2437 for (; loop_id <= ha->last_loop_id; loop_id++) {
2438 if (qla2x00_is_reserved_id(ha, loop_id))
2439 continue;
2441 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2442 break;
2444 if (swl != NULL) {
2445 if (last_dev) {
2446 wrap.b24 = new_fcport->d_id.b24;
2447 } else {
2448 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2449 memcpy(new_fcport->node_name,
2450 swl[swl_idx].node_name, WWN_SIZE);
2451 memcpy(new_fcport->port_name,
2452 swl[swl_idx].port_name, WWN_SIZE);
2453 memcpy(new_fcport->fabric_port_name,
2454 swl[swl_idx].fabric_port_name, WWN_SIZE);
2455 new_fcport->fp_speed = swl[swl_idx].fp_speed;
2457 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2458 last_dev = 1;
2460 swl_idx++;
2462 } else {
2463 /* Send GA_NXT to the switch */
2464 rval = qla2x00_ga_nxt(ha, new_fcport);
2465 if (rval != QLA_SUCCESS) {
2466 qla_printk(KERN_WARNING, ha,
2467 "SNS scan failed -- assuming zero-entry "
2468 "result...\n");
2469 list_for_each_entry_safe(fcport, fcptemp,
2470 new_fcports, list) {
2471 list_del(&fcport->list);
2472 kfree(fcport);
2474 rval = QLA_SUCCESS;
2475 break;
2479 /* If wrap on switch device list, exit. */
2480 if (first_dev) {
2481 wrap.b24 = new_fcport->d_id.b24;
2482 first_dev = 0;
2483 } else if (new_fcport->d_id.b24 == wrap.b24) {
2484 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2485 ha->host_no, new_fcport->d_id.b.domain,
2486 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2487 break;
2490 /* Bypass if host adapter. */
2491 if (new_fcport->d_id.b24 == ha->d_id.b24)
2492 continue;
2494 /* Bypass if same domain and area of adapter. */
2495 if (((new_fcport->d_id.b24 & 0xffff00) ==
2496 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2497 ISP_CFG_FL)
2498 continue;
2500 /* Bypass reserved domain fields. */
2501 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2502 continue;
2504 /* Locate matching device in database. */
2505 found = 0;
2506 list_for_each_entry(fcport, &ha->fcports, list) {
2507 if (memcmp(new_fcport->port_name, fcport->port_name,
2508 WWN_SIZE))
2509 continue;
2511 found++;
2513 /* Update port state. */
2514 memcpy(fcport->fabric_port_name,
2515 new_fcport->fabric_port_name, WWN_SIZE);
2516 fcport->fp_speed = new_fcport->fp_speed;
2519 * If address the same and state FCS_ONLINE, nothing
2520 * changed.
2522 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2523 atomic_read(&fcport->state) == FCS_ONLINE) {
2524 break;
2528 * If device was not a fabric device before.
2530 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2531 fcport->d_id.b24 = new_fcport->d_id.b24;
2532 fcport->loop_id = FC_NO_LOOP_ID;
2533 fcport->flags |= (FCF_FABRIC_DEVICE |
2534 FCF_LOGIN_NEEDED);
2535 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2536 break;
2540 * Port ID changed or device was marked to be updated;
2541 * Log it out if still logged in and mark it for
2542 * relogin later.
2544 fcport->d_id.b24 = new_fcport->d_id.b24;
2545 fcport->flags |= FCF_LOGIN_NEEDED;
2546 if (fcport->loop_id != FC_NO_LOOP_ID &&
2547 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2548 fcport->port_type != FCT_INITIATOR &&
2549 fcport->port_type != FCT_BROADCAST) {
2550 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2551 fcport->d_id.b.domain, fcport->d_id.b.area,
2552 fcport->d_id.b.al_pa);
2553 fcport->loop_id = FC_NO_LOOP_ID;
2556 break;
2559 if (found)
2560 continue;
2562 /* If device was not in our fcports list, then add it. */
2563 list_add_tail(&new_fcport->list, new_fcports);
2565 /* Allocate a new replacement fcport. */
2566 nxt_d_id.b24 = new_fcport->d_id.b24;
2567 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2568 if (new_fcport == NULL) {
2569 kfree(swl);
2570 return (QLA_MEMORY_ALLOC_FAILED);
2572 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2573 new_fcport->d_id.b24 = nxt_d_id.b24;
2576 kfree(swl);
2577 kfree(new_fcport);
2579 if (!list_empty(new_fcports))
2580 ha->device_flags |= DFLG_FABRIC_DEVICES;
2582 return (rval);
2586 * qla2x00_find_new_loop_id
2587 * Scan through our port list and find a new usable loop ID.
2589 * Input:
2590 * ha: adapter state pointer.
2591 * dev: port structure pointer.
2593 * Returns:
2594 * qla2x00 local function return status code.
2596 * Context:
2597 * Kernel context.
2599 static int
2600 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2602 int rval;
2603 int found;
2604 fc_port_t *fcport;
2605 uint16_t first_loop_id;
2607 rval = QLA_SUCCESS;
2609 /* Save starting loop ID. */
2610 first_loop_id = dev->loop_id;
2612 for (;;) {
2613 /* Skip loop ID if already used by adapter. */
2614 if (dev->loop_id == ha->loop_id) {
2615 dev->loop_id++;
2618 /* Skip reserved loop IDs. */
2619 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2620 dev->loop_id++;
2623 /* Reset loop ID if passed the end. */
2624 if (dev->loop_id > ha->last_loop_id) {
2625 /* first loop ID. */
2626 dev->loop_id = ha->min_external_loopid;
2629 /* Check for loop ID being already in use. */
2630 found = 0;
2631 fcport = NULL;
2632 list_for_each_entry(fcport, &ha->fcports, list) {
2633 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2634 /* ID possibly in use */
2635 found++;
2636 break;
2640 /* If not in use then it is free to use. */
2641 if (!found) {
2642 break;
2645 /* ID in use. Try next value. */
2646 dev->loop_id++;
2648 /* If wrap around. No free ID to use. */
2649 if (dev->loop_id == first_loop_id) {
2650 dev->loop_id = FC_NO_LOOP_ID;
2651 rval = QLA_FUNCTION_FAILED;
2652 break;
2656 return (rval);
2660 * qla2x00_device_resync
2661 * Marks devices in the database that needs resynchronization.
2663 * Input:
2664 * ha = adapter block pointer.
2666 * Context:
2667 * Kernel context.
2669 static int
2670 qla2x00_device_resync(scsi_qla_host_t *ha)
2672 int rval;
2673 uint32_t mask;
2674 fc_port_t *fcport;
2675 uint32_t rscn_entry;
2676 uint8_t rscn_out_iter;
2677 uint8_t format;
2678 port_id_t d_id;
2680 rval = QLA_RSCNS_HANDLED;
2682 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2683 ha->flags.rscn_queue_overflow) {
2685 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2686 format = MSB(MSW(rscn_entry));
2687 d_id.b.domain = LSB(MSW(rscn_entry));
2688 d_id.b.area = MSB(LSW(rscn_entry));
2689 d_id.b.al_pa = LSB(LSW(rscn_entry));
2691 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2692 "[%02x/%02x%02x%02x].\n",
2693 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2694 d_id.b.area, d_id.b.al_pa));
2696 ha->rscn_out_ptr++;
2697 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2698 ha->rscn_out_ptr = 0;
2700 /* Skip duplicate entries. */
2701 for (rscn_out_iter = ha->rscn_out_ptr;
2702 !ha->flags.rscn_queue_overflow &&
2703 rscn_out_iter != ha->rscn_in_ptr;
2704 rscn_out_iter = (rscn_out_iter ==
2705 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2707 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2708 break;
2710 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2711 "entry found at [%d].\n", ha->host_no,
2712 rscn_out_iter));
2714 ha->rscn_out_ptr = rscn_out_iter;
2717 /* Queue overflow, set switch default case. */
2718 if (ha->flags.rscn_queue_overflow) {
2719 DEBUG(printk("scsi(%ld): device_resync: rscn "
2720 "overflow.\n", ha->host_no));
2722 format = 3;
2723 ha->flags.rscn_queue_overflow = 0;
2726 switch (format) {
2727 case 0:
2728 mask = 0xffffff;
2729 break;
2730 case 1:
2731 mask = 0xffff00;
2732 break;
2733 case 2:
2734 mask = 0xff0000;
2735 break;
2736 default:
2737 mask = 0x0;
2738 d_id.b24 = 0;
2739 ha->rscn_out_ptr = ha->rscn_in_ptr;
2740 break;
2743 rval = QLA_SUCCESS;
2745 list_for_each_entry(fcport, &ha->fcports, list) {
2746 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2747 (fcport->d_id.b24 & mask) != d_id.b24 ||
2748 fcport->port_type == FCT_BROADCAST)
2749 continue;
2751 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2752 if (format != 3 ||
2753 fcport->port_type != FCT_INITIATOR) {
2754 qla2x00_mark_device_lost(ha, fcport,
2755 0, 0);
2758 fcport->flags &= ~FCF_FARP_DONE;
2761 return (rval);
2765 * qla2x00_fabric_dev_login
2766 * Login fabric target device and update FC port database.
2768 * Input:
2769 * ha: adapter state pointer.
2770 * fcport: port structure list pointer.
2771 * next_loopid: contains value of a new loop ID that can be used
2772 * by the next login attempt.
2774 * Returns:
2775 * qla2x00 local function return status code.
2777 * Context:
2778 * Kernel context.
2780 static int
2781 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2782 uint16_t *next_loopid)
2784 int rval;
2785 int retry;
2786 uint8_t opts;
2788 rval = QLA_SUCCESS;
2789 retry = 0;
2791 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2792 if (rval == QLA_SUCCESS) {
2793 /* Send an ADISC to tape devices.*/
2794 opts = 0;
2795 if (fcport->flags & FCF_TAPE_PRESENT)
2796 opts |= BIT_1;
2797 rval = qla2x00_get_port_database(ha, fcport, opts);
2798 if (rval != QLA_SUCCESS) {
2799 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2800 fcport->d_id.b.domain, fcport->d_id.b.area,
2801 fcport->d_id.b.al_pa);
2802 qla2x00_mark_device_lost(ha, fcport, 1, 0);
2803 } else {
2804 qla2x00_update_fcport(ha, fcport);
2808 return (rval);
2812 * qla2x00_fabric_login
2813 * Issue fabric login command.
2815 * Input:
2816 * ha = adapter block pointer.
2817 * device = pointer to FC device type structure.
2819 * Returns:
2820 * 0 - Login successfully
2821 * 1 - Login failed
2822 * 2 - Initiator device
2823 * 3 - Fatal error
2826 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2827 uint16_t *next_loopid)
2829 int rval;
2830 int retry;
2831 uint16_t tmp_loopid;
2832 uint16_t mb[MAILBOX_REGISTER_COUNT];
2834 retry = 0;
2835 tmp_loopid = 0;
2837 for (;;) {
2838 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2839 "for port %02x%02x%02x.\n",
2840 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2841 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2843 /* Login fcport on switch. */
2844 ha->isp_ops.fabric_login(ha, fcport->loop_id,
2845 fcport->d_id.b.domain, fcport->d_id.b.area,
2846 fcport->d_id.b.al_pa, mb, BIT_0);
2847 if (mb[0] == MBS_PORT_ID_USED) {
2849 * Device has another loop ID. The firmware team
2850 * recommends the driver perform an implicit login with
2851 * the specified ID again. The ID we just used is save
2852 * here so we return with an ID that can be tried by
2853 * the next login.
2855 retry++;
2856 tmp_loopid = fcport->loop_id;
2857 fcport->loop_id = mb[1];
2859 DEBUG(printk("Fabric Login: port in use - next "
2860 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2861 fcport->loop_id, fcport->d_id.b.domain,
2862 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2864 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2866 * Login succeeded.
2868 if (retry) {
2869 /* A retry occurred before. */
2870 *next_loopid = tmp_loopid;
2871 } else {
2873 * No retry occurred before. Just increment the
2874 * ID value for next login.
2876 *next_loopid = (fcport->loop_id + 1);
2879 if (mb[1] & BIT_0) {
2880 fcport->port_type = FCT_INITIATOR;
2881 } else {
2882 fcport->port_type = FCT_TARGET;
2883 if (mb[1] & BIT_1) {
2884 fcport->flags |= FCF_TAPE_PRESENT;
2888 if (mb[10] & BIT_0)
2889 fcport->supported_classes |= FC_COS_CLASS2;
2890 if (mb[10] & BIT_1)
2891 fcport->supported_classes |= FC_COS_CLASS3;
2893 rval = QLA_SUCCESS;
2894 break;
2895 } else if (mb[0] == MBS_LOOP_ID_USED) {
2897 * Loop ID already used, try next loop ID.
2899 fcport->loop_id++;
2900 rval = qla2x00_find_new_loop_id(ha, fcport);
2901 if (rval != QLA_SUCCESS) {
2902 /* Ran out of loop IDs to use */
2903 break;
2905 } else if (mb[0] == MBS_COMMAND_ERROR) {
2907 * Firmware possibly timed out during login. If NO
2908 * retries are left to do then the device is declared
2909 * dead.
2911 *next_loopid = fcport->loop_id;
2912 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2913 fcport->d_id.b.domain, fcport->d_id.b.area,
2914 fcport->d_id.b.al_pa);
2915 qla2x00_mark_device_lost(ha, fcport, 1, 0);
2917 rval = 1;
2918 break;
2919 } else {
2921 * unrecoverable / not handled error
2923 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
2924 "loop_id=%x jiffies=%lx.\n",
2925 __func__, ha->host_no, mb[0],
2926 fcport->d_id.b.domain, fcport->d_id.b.area,
2927 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2929 *next_loopid = fcport->loop_id;
2930 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2931 fcport->d_id.b.domain, fcport->d_id.b.area,
2932 fcport->d_id.b.al_pa);
2933 fcport->loop_id = FC_NO_LOOP_ID;
2934 fcport->login_retry = 0;
2936 rval = 3;
2937 break;
2941 return (rval);
2945 * qla2x00_local_device_login
2946 * Issue local device login command.
2948 * Input:
2949 * ha = adapter block pointer.
2950 * loop_id = loop id of device to login to.
2952 * Returns (Where's the #define!!!!):
2953 * 0 - Login successfully
2954 * 1 - Login failed
2955 * 3 - Fatal error
2958 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
2960 int rval;
2961 uint16_t mb[MAILBOX_REGISTER_COUNT];
2963 memset(mb, 0, sizeof(mb));
2964 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
2965 if (rval == QLA_SUCCESS) {
2966 /* Interrogate mailbox registers for any errors */
2967 if (mb[0] == MBS_COMMAND_ERROR)
2968 rval = 1;
2969 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2970 /* device not in PCB table */
2971 rval = 3;
2974 return (rval);
2978 * qla2x00_loop_resync
2979 * Resync with fibre channel devices.
2981 * Input:
2982 * ha = adapter block pointer.
2984 * Returns:
2985 * 0 = success
2988 qla2x00_loop_resync(scsi_qla_host_t *ha)
2990 int rval;
2991 uint32_t wait_time;
2993 rval = QLA_SUCCESS;
2995 atomic_set(&ha->loop_state, LOOP_UPDATE);
2996 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2997 if (ha->flags.online) {
2998 if (!(rval = qla2x00_fw_ready(ha))) {
2999 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3000 wait_time = 256;
3001 do {
3002 atomic_set(&ha->loop_state, LOOP_UPDATE);
3004 /* Issue a marker after FW becomes ready. */
3005 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3006 ha->marker_needed = 0;
3008 /* Remap devices on Loop. */
3009 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3011 qla2x00_configure_loop(ha);
3012 wait_time--;
3013 } while (!atomic_read(&ha->loop_down_timer) &&
3014 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3015 wait_time &&
3016 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3020 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3021 return (QLA_FUNCTION_FAILED);
3024 if (rval) {
3025 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3028 return (rval);
3031 void
3032 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3034 int rescan_done;
3035 fc_port_t *fcport;
3037 rescan_done = 0;
3038 list_for_each_entry(fcport, &ha->fcports, list) {
3039 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3040 continue;
3042 qla2x00_update_fcport(ha, fcport);
3043 fcport->flags &= ~FCF_RESCAN_NEEDED;
3045 rescan_done = 1;
3047 qla2x00_probe_for_all_luns(ha);
3050 void
3051 qla2x00_update_fcports(scsi_qla_host_t *ha)
3053 fc_port_t *fcport;
3055 /* Go with deferred removal of rport references. */
3056 list_for_each_entry(fcport, &ha->fcports, list)
3057 if (fcport->drport)
3058 qla2x00_rport_del(fcport);
3062 * qla2x00_abort_isp
3063 * Resets ISP and aborts all outstanding commands.
3065 * Input:
3066 * ha = adapter block pointer.
3068 * Returns:
3069 * 0 = success
3072 qla2x00_abort_isp(scsi_qla_host_t *ha)
3074 int rval;
3075 unsigned long flags = 0;
3076 uint16_t cnt;
3077 srb_t *sp;
3078 uint8_t status = 0;
3080 if (ha->flags.online) {
3081 ha->flags.online = 0;
3082 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3084 qla_printk(KERN_INFO, ha,
3085 "Performing ISP error recovery - ha= %p.\n", ha);
3086 ha->isp_ops.reset_chip(ha);
3088 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3089 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3090 atomic_set(&ha->loop_state, LOOP_DOWN);
3091 qla2x00_mark_all_devices_lost(ha, 0);
3092 } else {
3093 if (!atomic_read(&ha->loop_down_timer))
3094 atomic_set(&ha->loop_down_timer,
3095 LOOP_DOWN_TIME);
3098 spin_lock_irqsave(&ha->hardware_lock, flags);
3099 /* Requeue all commands in outstanding command list. */
3100 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3101 sp = ha->outstanding_cmds[cnt];
3102 if (sp) {
3103 ha->outstanding_cmds[cnt] = NULL;
3104 sp->flags = 0;
3105 sp->cmd->result = DID_RESET << 16;
3106 sp->cmd->host_scribble = (unsigned char *)NULL;
3107 qla2x00_sp_compl(ha, sp);
3110 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3112 ha->isp_ops.nvram_config(ha);
3114 if (!qla2x00_restart_isp(ha)) {
3115 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3117 if (!atomic_read(&ha->loop_down_timer)) {
3119 * Issue marker command only when we are going
3120 * to start the I/O .
3122 ha->marker_needed = 1;
3125 ha->flags.online = 1;
3127 ha->isp_ops.enable_intrs(ha);
3129 ha->isp_abort_cnt = 0;
3130 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3132 if (ha->eft) {
3133 rval = qla2x00_trace_control(ha, TC_ENABLE,
3134 ha->eft_dma, EFT_NUM_BUFFERS);
3135 if (rval) {
3136 qla_printk(KERN_WARNING, ha,
3137 "Unable to reinitialize EFT "
3138 "(%d).\n", rval);
3141 } else { /* failed the ISP abort */
3142 ha->flags.online = 1;
3143 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3144 if (ha->isp_abort_cnt == 0) {
3145 qla_printk(KERN_WARNING, ha,
3146 "ISP error recovery failed - "
3147 "board disabled\n");
3149 * The next call disables the board
3150 * completely.
3152 ha->isp_ops.reset_adapter(ha);
3153 ha->flags.online = 0;
3154 clear_bit(ISP_ABORT_RETRY,
3155 &ha->dpc_flags);
3156 status = 0;
3157 } else { /* schedule another ISP abort */
3158 ha->isp_abort_cnt--;
3159 DEBUG(printk("qla%ld: ISP abort - "
3160 "retry remaining %d\n",
3161 ha->host_no, ha->isp_abort_cnt));
3162 status = 1;
3164 } else {
3165 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3166 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3167 "- retrying (%d) more times\n",
3168 ha->host_no, ha->isp_abort_cnt));
3169 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3170 status = 1;
3176 if (status) {
3177 qla_printk(KERN_INFO, ha,
3178 "qla2x00_abort_isp: **** FAILED ****\n");
3179 } else {
3180 DEBUG(printk(KERN_INFO
3181 "qla2x00_abort_isp(%ld): exiting.\n",
3182 ha->host_no));
3185 return(status);
3189 * qla2x00_restart_isp
3190 * restarts the ISP after a reset
3192 * Input:
3193 * ha = adapter block pointer.
3195 * Returns:
3196 * 0 = success
3198 static int
3199 qla2x00_restart_isp(scsi_qla_host_t *ha)
3201 uint8_t status = 0;
3202 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3203 unsigned long flags = 0;
3204 uint32_t wait_time;
3206 /* If firmware needs to be loaded */
3207 if (qla2x00_isp_firmware(ha)) {
3208 ha->flags.online = 0;
3209 if (!(status = ha->isp_ops.chip_diag(ha))) {
3210 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3211 status = qla2x00_setup_chip(ha);
3212 goto done;
3215 spin_lock_irqsave(&ha->hardware_lock, flags);
3217 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3219 * Disable SRAM, Instruction RAM and GP RAM
3220 * parity.
3222 WRT_REG_WORD(&reg->hccr,
3223 (HCCR_ENABLE_PARITY + 0x0));
3224 RD_REG_WORD(&reg->hccr);
3227 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3229 status = qla2x00_setup_chip(ha);
3231 spin_lock_irqsave(&ha->hardware_lock, flags);
3233 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3234 /* Enable proper parity */
3235 if (IS_QLA2300(ha))
3236 /* SRAM parity */
3237 WRT_REG_WORD(&reg->hccr,
3238 (HCCR_ENABLE_PARITY + 0x1));
3239 else
3241 * SRAM, Instruction RAM and GP RAM
3242 * parity.
3244 WRT_REG_WORD(&reg->hccr,
3245 (HCCR_ENABLE_PARITY + 0x7));
3246 RD_REG_WORD(&reg->hccr);
3249 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3253 done:
3254 if (!status && !(status = qla2x00_init_rings(ha))) {
3255 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3256 if (!(status = qla2x00_fw_ready(ha))) {
3257 DEBUG(printk("%s(): Start configure loop, "
3258 "status = %d\n", __func__, status));
3260 /* Issue a marker after FW becomes ready. */
3261 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3263 ha->flags.online = 1;
3264 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3265 wait_time = 256;
3266 do {
3267 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3268 qla2x00_configure_loop(ha);
3269 wait_time--;
3270 } while (!atomic_read(&ha->loop_down_timer) &&
3271 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3272 wait_time &&
3273 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3276 /* if no cable then assume it's good */
3277 if ((ha->device_flags & DFLG_NO_CABLE))
3278 status = 0;
3280 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3281 __func__,
3282 status));
3284 return (status);
3288 * qla2x00_reset_adapter
3289 * Reset adapter.
3291 * Input:
3292 * ha = adapter block pointer.
3294 void
3295 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3297 unsigned long flags = 0;
3298 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3300 ha->flags.online = 0;
3301 ha->isp_ops.disable_intrs(ha);
3303 spin_lock_irqsave(&ha->hardware_lock, flags);
3304 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3305 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3306 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3307 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3308 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3311 void
3312 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3314 unsigned long flags = 0;
3315 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3317 ha->flags.online = 0;
3318 ha->isp_ops.disable_intrs(ha);
3320 spin_lock_irqsave(&ha->hardware_lock, flags);
3321 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3322 RD_REG_DWORD(&reg->hccr);
3323 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3324 RD_REG_DWORD(&reg->hccr);
3325 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3329 qla24xx_nvram_config(scsi_qla_host_t *ha)
3331 int rval;
3332 struct init_cb_24xx *icb;
3333 struct nvram_24xx *nv;
3334 uint32_t *dptr;
3335 uint8_t *dptr1, *dptr2;
3336 uint32_t chksum;
3337 uint16_t cnt;
3339 rval = QLA_SUCCESS;
3340 icb = (struct init_cb_24xx *)ha->init_cb;
3341 nv = (struct nvram_24xx *)ha->request_ring;
3343 /* Determine NVRAM starting address. */
3344 ha->nvram_size = sizeof(struct nvram_24xx);
3345 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3346 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3347 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3348 if (PCI_FUNC(ha->pdev->devfn)) {
3349 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3350 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3353 /* Get NVRAM data and calculate checksum. */
3354 dptr = (uint32_t *)nv;
3355 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3356 ha->nvram_size);
3357 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3358 chksum += le32_to_cpu(*dptr++);
3360 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3361 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3362 ha->nvram_size));
3364 /* Bad NVRAM data, set defaults parameters. */
3365 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3366 || nv->id[3] != ' ' ||
3367 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3368 /* Reset NVRAM data. */
3369 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3370 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3371 le16_to_cpu(nv->nvram_version));
3372 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3373 "invalid -- WWPN) defaults.\n");
3376 * Set default initialization control block.
3378 memset(nv, 0, ha->nvram_size);
3379 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3380 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3381 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3382 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3383 nv->exchange_count = __constant_cpu_to_le16(0);
3384 nv->hard_address = __constant_cpu_to_le16(124);
3385 nv->port_name[0] = 0x21;
3386 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3387 nv->port_name[2] = 0x00;
3388 nv->port_name[3] = 0xe0;
3389 nv->port_name[4] = 0x8b;
3390 nv->port_name[5] = 0x1c;
3391 nv->port_name[6] = 0x55;
3392 nv->port_name[7] = 0x86;
3393 nv->node_name[0] = 0x20;
3394 nv->node_name[1] = 0x00;
3395 nv->node_name[2] = 0x00;
3396 nv->node_name[3] = 0xe0;
3397 nv->node_name[4] = 0x8b;
3398 nv->node_name[5] = 0x1c;
3399 nv->node_name[6] = 0x55;
3400 nv->node_name[7] = 0x86;
3401 nv->login_retry_count = __constant_cpu_to_le16(8);
3402 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3403 nv->login_timeout = __constant_cpu_to_le16(0);
3404 nv->firmware_options_1 =
3405 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3406 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3407 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3408 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3409 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3410 nv->efi_parameters = __constant_cpu_to_le32(0);
3411 nv->reset_delay = 5;
3412 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3413 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3414 nv->link_down_timeout = __constant_cpu_to_le16(30);
3416 rval = 1;
3419 /* Reset Initialization control block */
3420 memset(icb, 0, sizeof(struct init_cb_24xx));
3422 /* Copy 1st segment. */
3423 dptr1 = (uint8_t *)icb;
3424 dptr2 = (uint8_t *)&nv->version;
3425 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3426 while (cnt--)
3427 *dptr1++ = *dptr2++;
3429 icb->login_retry_count = nv->login_retry_count;
3430 icb->link_down_on_nos = nv->link_down_on_nos;
3432 /* Copy 2nd segment. */
3433 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3434 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3435 cnt = (uint8_t *)&icb->reserved_3 -
3436 (uint8_t *)&icb->interrupt_delay_timer;
3437 while (cnt--)
3438 *dptr1++ = *dptr2++;
3441 * Setup driver NVRAM options.
3443 if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3444 char *st, *en;
3445 uint16_t index;
3447 strncpy(ha->model_number, nv->model_name,
3448 sizeof(nv->model_name));
3449 st = en = ha->model_number;
3450 en += sizeof(nv->model_name) - 1;
3451 while (en > st) {
3452 if (*en != 0x20 && *en != 0x00)
3453 break;
3454 *en-- = '\0';
3457 index = (ha->pdev->subsystem_device & 0xff);
3458 if (index < QLA_MODEL_NAMES)
3459 ha->model_desc = qla2x00_model_name[index * 2 + 1];
3460 } else
3461 strcpy(ha->model_number, "QLA2462");
3463 /* Use alternate WWN? */
3464 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3465 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3466 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3469 /* Prepare nodename */
3470 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
3472 * Firmware will apply the following mask if the nodename was
3473 * not provided.
3475 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3476 icb->node_name[0] &= 0xF0;
3479 /* Set host adapter parameters. */
3480 ha->flags.disable_risc_code_load = 0;
3481 ha->flags.enable_lip_reset = 1;
3482 ha->flags.enable_lip_full_login = 1;
3483 ha->flags.enable_target_reset = 1;
3484 ha->flags.enable_led_scheme = 0;
3485 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
3487 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3488 (BIT_6 | BIT_5 | BIT_4)) >> 4;
3490 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3491 sizeof(ha->fw_seriallink_options24));
3493 /* save HBA serial number */
3494 ha->serial0 = icb->port_name[5];
3495 ha->serial1 = icb->port_name[6];
3496 ha->serial2 = icb->port_name[7];
3497 ha->node_name = icb->node_name;
3498 ha->port_name = icb->port_name;
3500 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3502 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3504 /* Set minimum login_timeout to 4 seconds. */
3505 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3506 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3507 if (le16_to_cpu(nv->login_timeout) < 4)
3508 nv->login_timeout = __constant_cpu_to_le16(4);
3509 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3510 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3512 /* Set minimum RATOV to 200 tenths of a second. */
3513 ha->r_a_tov = 200;
3515 ha->loop_reset_delay = nv->reset_delay;
3517 /* Link Down Timeout = 0:
3519 * When Port Down timer expires we will start returning
3520 * I/O's to OS with "DID_NO_CONNECT".
3522 * Link Down Timeout != 0:
3524 * The driver waits for the link to come up after link down
3525 * before returning I/Os to OS with "DID_NO_CONNECT".
3527 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3528 ha->loop_down_abort_time =
3529 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3530 } else {
3531 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3532 ha->loop_down_abort_time =
3533 (LOOP_DOWN_TIME - ha->link_down_timeout);
3536 /* Need enough time to try and get the port back. */
3537 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3538 if (qlport_down_retry)
3539 ha->port_down_retry_count = qlport_down_retry;
3541 /* Set login_retry_count */
3542 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3543 if (ha->port_down_retry_count ==
3544 le16_to_cpu(nv->port_down_retry_count) &&
3545 ha->port_down_retry_count > 3)
3546 ha->login_retry_count = ha->port_down_retry_count;
3547 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3548 ha->login_retry_count = ha->port_down_retry_count;
3549 if (ql2xloginretrycount)
3550 ha->login_retry_count = ql2xloginretrycount;
3552 /* Enable ZIO. */
3553 if (!ha->flags.init_done) {
3554 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3555 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3556 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3557 le16_to_cpu(icb->interrupt_delay_timer): 2;
3559 icb->firmware_options_2 &= __constant_cpu_to_le32(
3560 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3561 ha->flags.process_response_queue = 0;
3562 if (ha->zio_mode != QLA_ZIO_DISABLED) {
3563 ha->zio_mode = QLA_ZIO_MODE_6;
3565 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3566 "(%d us).\n", ha->host_no, ha->zio_mode,
3567 ha->zio_timer * 100));
3568 qla_printk(KERN_INFO, ha,
3569 "ZIO mode %d enabled; timer delay (%d us).\n",
3570 ha->zio_mode, ha->zio_timer * 100);
3572 icb->firmware_options_2 |= cpu_to_le32(
3573 (uint32_t)ha->zio_mode);
3574 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3575 ha->flags.process_response_queue = 1;
3578 if (rval) {
3579 DEBUG2_3(printk(KERN_WARNING
3580 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3582 return (rval);
3585 static int
3586 qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3588 int rval;
3589 int segments, fragment;
3590 uint32_t faddr;
3591 uint32_t *dcode, dlen;
3592 uint32_t risc_addr;
3593 uint32_t risc_size;
3594 uint32_t i;
3596 rval = QLA_SUCCESS;
3598 segments = FA_RISC_CODE_SEGMENTS;
3599 faddr = FA_RISC_CODE_ADDR;
3600 dcode = (uint32_t *)ha->request_ring;
3601 *srisc_addr = 0;
3603 /* Validate firmware image by checking version. */
3604 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3605 for (i = 0; i < 4; i++)
3606 dcode[i] = be32_to_cpu(dcode[i]);
3607 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3608 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3609 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3610 dcode[3] == 0)) {
3611 qla_printk(KERN_WARNING, ha,
3612 "Unable to verify integrity of flash firmware image!\n");
3613 qla_printk(KERN_WARNING, ha,
3614 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3615 dcode[1], dcode[2], dcode[3]);
3617 return QLA_FUNCTION_FAILED;
3620 while (segments && rval == QLA_SUCCESS) {
3621 /* Read segment's load information. */
3622 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3624 risc_addr = be32_to_cpu(dcode[2]);
3625 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3626 risc_size = be32_to_cpu(dcode[3]);
3628 fragment = 0;
3629 while (risc_size > 0 && rval == QLA_SUCCESS) {
3630 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3631 if (dlen > risc_size)
3632 dlen = risc_size;
3634 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3635 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3636 ha->host_no, risc_addr, dlen, faddr));
3638 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3639 for (i = 0; i < dlen; i++)
3640 dcode[i] = swab32(dcode[i]);
3642 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3643 dlen);
3644 if (rval) {
3645 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3646 "segment %d of firmware\n", ha->host_no,
3647 fragment));
3648 qla_printk(KERN_WARNING, ha,
3649 "[ERROR] Failed to load segment %d of "
3650 "firmware\n", fragment);
3651 break;
3654 faddr += dlen;
3655 risc_addr += dlen;
3656 risc_size -= dlen;
3657 fragment++;
3660 /* Next segment. */
3661 segments--;
3664 return rval;
3667 #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3670 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3672 int rval;
3673 int i, fragment;
3674 uint16_t *wcode, *fwcode;
3675 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3676 struct fw_blob *blob;
3678 /* Load firmware blob. */
3679 blob = qla2x00_request_firmware(ha);
3680 if (!blob) {
3681 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3682 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3683 "from: " QLA_FW_URL ".\n");
3684 return QLA_FUNCTION_FAILED;
3687 rval = QLA_SUCCESS;
3689 wcode = (uint16_t *)ha->request_ring;
3690 *srisc_addr = 0;
3691 fwcode = (uint16_t *)blob->fw->data;
3692 fwclen = 0;
3694 /* Validate firmware image by checking version. */
3695 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3696 qla_printk(KERN_WARNING, ha,
3697 "Unable to verify integrity of firmware image (%Zd)!\n",
3698 blob->fw->size);
3699 goto fail_fw_integrity;
3701 for (i = 0; i < 4; i++)
3702 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3703 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3704 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3705 wcode[2] == 0 && wcode[3] == 0)) {
3706 qla_printk(KERN_WARNING, ha,
3707 "Unable to verify integrity of firmware image!\n");
3708 qla_printk(KERN_WARNING, ha,
3709 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3710 wcode[1], wcode[2], wcode[3]);
3711 goto fail_fw_integrity;
3714 seg = blob->segs;
3715 while (*seg && rval == QLA_SUCCESS) {
3716 risc_addr = *seg;
3717 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3718 risc_size = be16_to_cpu(fwcode[3]);
3720 /* Validate firmware image size. */
3721 fwclen += risc_size * sizeof(uint16_t);
3722 if (blob->fw->size < fwclen) {
3723 qla_printk(KERN_WARNING, ha,
3724 "Unable to verify integrity of firmware image "
3725 "(%Zd)!\n", blob->fw->size);
3726 goto fail_fw_integrity;
3729 fragment = 0;
3730 while (risc_size > 0 && rval == QLA_SUCCESS) {
3731 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3732 if (wlen > risc_size)
3733 wlen = risc_size;
3735 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3736 "addr %x, number of words 0x%x.\n", ha->host_no,
3737 risc_addr, wlen));
3739 for (i = 0; i < wlen; i++)
3740 wcode[i] = swab16(fwcode[i]);
3742 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3743 wlen);
3744 if (rval) {
3745 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3746 "segment %d of firmware\n", ha->host_no,
3747 fragment));
3748 qla_printk(KERN_WARNING, ha,
3749 "[ERROR] Failed to load segment %d of "
3750 "firmware\n", fragment);
3751 break;
3754 fwcode += wlen;
3755 risc_addr += wlen;
3756 risc_size -= wlen;
3757 fragment++;
3760 /* Next segment. */
3761 seg++;
3763 return rval;
3765 fail_fw_integrity:
3766 return QLA_FUNCTION_FAILED;
3770 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3772 int rval;
3773 int segments, fragment;
3774 uint32_t *dcode, dlen;
3775 uint32_t risc_addr;
3776 uint32_t risc_size;
3777 uint32_t i;
3778 struct fw_blob *blob;
3779 uint32_t *fwcode, fwclen;
3781 /* Load firmware blob. */
3782 blob = qla2x00_request_firmware(ha);
3783 if (!blob) {
3784 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3785 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3786 "from: " QLA_FW_URL ".\n");
3788 /* Try to load RISC code from flash. */
3789 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3790 "outdated) firmware from flash.\n");
3791 return qla24xx_load_risc_flash(ha, srisc_addr);
3794 rval = QLA_SUCCESS;
3796 segments = FA_RISC_CODE_SEGMENTS;
3797 dcode = (uint32_t *)ha->request_ring;
3798 *srisc_addr = 0;
3799 fwcode = (uint32_t *)blob->fw->data;
3800 fwclen = 0;
3802 /* Validate firmware image by checking version. */
3803 if (blob->fw->size < 8 * sizeof(uint32_t)) {
3804 qla_printk(KERN_WARNING, ha,
3805 "Unable to verify integrity of firmware image (%Zd)!\n",
3806 blob->fw->size);
3807 goto fail_fw_integrity;
3809 for (i = 0; i < 4; i++)
3810 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3811 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3812 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3813 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3814 dcode[3] == 0)) {
3815 qla_printk(KERN_WARNING, ha,
3816 "Unable to verify integrity of firmware image!\n");
3817 qla_printk(KERN_WARNING, ha,
3818 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3819 dcode[1], dcode[2], dcode[3]);
3820 goto fail_fw_integrity;
3823 while (segments && rval == QLA_SUCCESS) {
3824 risc_addr = be32_to_cpu(fwcode[2]);
3825 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3826 risc_size = be32_to_cpu(fwcode[3]);
3828 /* Validate firmware image size. */
3829 fwclen += risc_size * sizeof(uint32_t);
3830 if (blob->fw->size < fwclen) {
3831 qla_printk(KERN_WARNING, ha,
3832 "Unable to verify integrity of firmware image "
3833 "(%Zd)!\n", blob->fw->size);
3835 goto fail_fw_integrity;
3838 fragment = 0;
3839 while (risc_size > 0 && rval == QLA_SUCCESS) {
3840 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3841 if (dlen > risc_size)
3842 dlen = risc_size;
3844 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3845 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3846 risc_addr, dlen));
3848 for (i = 0; i < dlen; i++)
3849 dcode[i] = swab32(fwcode[i]);
3851 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3852 dlen);
3853 if (rval) {
3854 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3855 "segment %d of firmware\n", ha->host_no,
3856 fragment));
3857 qla_printk(KERN_WARNING, ha,
3858 "[ERROR] Failed to load segment %d of "
3859 "firmware\n", fragment);
3860 break;
3863 fwcode += dlen;
3864 risc_addr += dlen;
3865 risc_size -= dlen;
3866 fragment++;
3869 /* Next segment. */
3870 segments--;
3872 return rval;
3874 fail_fw_integrity:
3875 return QLA_FUNCTION_FAILED;
3878 void
3879 qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
3881 int ret, retries;
3883 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
3884 return;
3886 ret = qla2x00_stop_firmware(ha);
3887 for (retries = 5; ret != QLA_SUCCESS && retries ; retries--) {
3888 qla2x00_reset_chip(ha);
3889 if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
3890 continue;
3891 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
3892 continue;
3893 qla_printk(KERN_INFO, ha,
3894 "Attempting retry of stop-firmware command...\n");
3895 ret = qla2x00_stop_firmware(ha);