[SCSI] qla2xxx: Re-factor isp_operations to static structures.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / qla2xxx / qla_init.c
blob401a8798ce5247f7145a5b7787e472e18057e5de
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 #ifdef CONFIG_SPARC
15 #include <asm/prom.h>
16 #endif
18 /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
19 #ifndef EXT_IS_LUN_BIT_SET
20 #define EXT_IS_LUN_BIT_SET(P,L) \
21 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
22 #define EXT_SET_LUN_BIT(P,L) \
23 ((P)->mask[L/8] |= (0x80 >> (L%8)))
24 #endif
27 * QLogic ISP2x00 Hardware Support Function Prototypes.
29 static int qla2x00_isp_firmware(scsi_qla_host_t *);
30 static void qla2x00_resize_request_q(scsi_qla_host_t *);
31 static int qla2x00_setup_chip(scsi_qla_host_t *);
32 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
33 static int qla2x00_init_rings(scsi_qla_host_t *);
34 static int qla2x00_fw_ready(scsi_qla_host_t *);
35 static int qla2x00_configure_hba(scsi_qla_host_t *);
36 static int qla2x00_configure_loop(scsi_qla_host_t *);
37 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
38 static int qla2x00_configure_fabric(scsi_qla_host_t *);
39 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
40 static int qla2x00_device_resync(scsi_qla_host_t *);
41 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
42 uint16_t *);
44 static int qla2x00_restart_isp(scsi_qla_host_t *);
46 static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
48 /****************************************************************************/
49 /* QLogic ISP2x00 Hardware Support Functions. */
50 /****************************************************************************/
53 * qla2x00_initialize_adapter
54 * Initialize board.
56 * Input:
57 * ha = adapter block pointer.
59 * Returns:
60 * 0 = success
62 int
63 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
65 int rval;
67 /* Clear adapter flags. */
68 ha->flags.online = 0;
69 ha->flags.reset_active = 0;
70 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
71 atomic_set(&ha->loop_state, LOOP_DOWN);
72 ha->device_flags = DFLG_NO_CABLE;
73 ha->dpc_flags = 0;
74 ha->flags.management_server_logged_in = 0;
75 ha->marker_needed = 0;
76 ha->mbx_flags = 0;
77 ha->isp_abort_cnt = 0;
78 ha->beacon_blink_led = 0;
79 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
81 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
82 rval = ha->isp_ops->pci_config(ha);
83 if (rval) {
84 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
85 ha->host_no));
86 return (rval);
89 ha->isp_ops->reset_chip(ha);
91 ha->isp_ops->get_flash_version(ha, ha->request_ring);
93 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
95 ha->isp_ops->nvram_config(ha);
97 if (ha->flags.disable_serdes) {
98 /* Mask HBA via NVRAM settings? */
99 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
100 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
101 ha->port_name[0], ha->port_name[1],
102 ha->port_name[2], ha->port_name[3],
103 ha->port_name[4], ha->port_name[5],
104 ha->port_name[6], ha->port_name[7]);
105 return QLA_FUNCTION_FAILED;
108 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
110 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
111 rval = ha->isp_ops->chip_diag(ha);
112 if (rval)
113 return (rval);
114 rval = qla2x00_setup_chip(ha);
115 if (rval)
116 return (rval);
118 rval = qla2x00_init_rings(ha);
120 return (rval);
124 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
125 * @ha: HA context
127 * Returns 0 on success.
130 qla2100_pci_config(scsi_qla_host_t *ha)
132 int ret;
133 uint16_t w;
134 uint32_t d;
135 unsigned long flags;
136 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
138 pci_set_master(ha->pdev);
139 ret = pci_set_mwi(ha->pdev);
141 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
142 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
143 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
145 /* Reset expansion ROM address decode enable */
146 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
147 d &= ~PCI_ROM_ADDRESS_ENABLE;
148 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
150 /* Get PCI bus information. */
151 spin_lock_irqsave(&ha->hardware_lock, flags);
152 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
153 spin_unlock_irqrestore(&ha->hardware_lock, flags);
155 return QLA_SUCCESS;
159 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
160 * @ha: HA context
162 * Returns 0 on success.
165 qla2300_pci_config(scsi_qla_host_t *ha)
167 int ret;
168 uint16_t w;
169 uint32_t d;
170 unsigned long flags = 0;
171 uint32_t cnt;
172 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
174 pci_set_master(ha->pdev);
175 ret = pci_set_mwi(ha->pdev);
177 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
178 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
180 if (IS_QLA2322(ha) || IS_QLA6322(ha))
181 w &= ~PCI_COMMAND_INTX_DISABLE;
182 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
185 * If this is a 2300 card and not 2312, reset the
186 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
187 * the 2310 also reports itself as a 2300 so we need to get the
188 * fb revision level -- a 6 indicates it really is a 2300 and
189 * not a 2310.
191 if (IS_QLA2300(ha)) {
192 spin_lock_irqsave(&ha->hardware_lock, flags);
194 /* Pause RISC. */
195 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
196 for (cnt = 0; cnt < 30000; cnt++) {
197 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
198 break;
200 udelay(10);
203 /* Select FPM registers. */
204 WRT_REG_WORD(&reg->ctrl_status, 0x20);
205 RD_REG_WORD(&reg->ctrl_status);
207 /* Get the fb rev level */
208 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
210 if (ha->fb_rev == FPM_2300)
211 pci_clear_mwi(ha->pdev);
213 /* Deselect FPM registers. */
214 WRT_REG_WORD(&reg->ctrl_status, 0x0);
215 RD_REG_WORD(&reg->ctrl_status);
217 /* Release RISC module. */
218 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
219 for (cnt = 0; cnt < 30000; cnt++) {
220 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
221 break;
223 udelay(10);
226 spin_unlock_irqrestore(&ha->hardware_lock, flags);
229 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
231 /* Reset expansion ROM address decode enable */
232 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
233 d &= ~PCI_ROM_ADDRESS_ENABLE;
234 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
236 /* Get PCI bus information. */
237 spin_lock_irqsave(&ha->hardware_lock, flags);
238 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
239 spin_unlock_irqrestore(&ha->hardware_lock, flags);
241 return QLA_SUCCESS;
245 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
246 * @ha: HA context
248 * Returns 0 on success.
251 qla24xx_pci_config(scsi_qla_host_t *ha)
253 int ret;
254 uint16_t w;
255 uint32_t d;
256 unsigned long flags = 0;
257 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
258 int pcix_cmd_reg, pcie_dctl_reg;
260 pci_set_master(ha->pdev);
261 ret = pci_set_mwi(ha->pdev);
263 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
264 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
265 w &= ~PCI_COMMAND_INTX_DISABLE;
266 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
268 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
270 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
271 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
272 if (pcix_cmd_reg) {
273 uint16_t pcix_cmd;
275 pcix_cmd_reg += PCI_X_CMD;
276 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
277 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
278 pcix_cmd |= 0x0008;
279 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
282 /* PCIe -- adjust Maximum Read Request Size (2048). */
283 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
284 if (pcie_dctl_reg) {
285 uint16_t pcie_dctl;
287 pcie_dctl_reg += PCI_EXP_DEVCTL;
288 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
289 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
290 pcie_dctl |= 0x4000;
291 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
294 /* Reset expansion ROM address decode enable */
295 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
296 d &= ~PCI_ROM_ADDRESS_ENABLE;
297 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
299 ha->chip_revision = ha->pdev->revision;
301 /* Get PCI bus information. */
302 spin_lock_irqsave(&ha->hardware_lock, flags);
303 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
304 spin_unlock_irqrestore(&ha->hardware_lock, flags);
306 return QLA_SUCCESS;
310 * qla2x00_isp_firmware() - Choose firmware image.
311 * @ha: HA context
313 * Returns 0 on success.
315 static int
316 qla2x00_isp_firmware(scsi_qla_host_t *ha)
318 int rval;
320 /* Assume loading risc code */
321 rval = QLA_FUNCTION_FAILED;
323 if (ha->flags.disable_risc_code_load) {
324 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
325 ha->host_no));
326 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
328 /* Verify checksum of loaded RISC code. */
329 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
332 if (rval) {
333 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
334 ha->host_no));
337 return (rval);
341 * qla2x00_reset_chip() - Reset ISP chip.
342 * @ha: HA context
344 * Returns 0 on success.
346 void
347 qla2x00_reset_chip(scsi_qla_host_t *ha)
349 unsigned long flags = 0;
350 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
351 uint32_t cnt;
352 uint16_t cmd;
354 ha->isp_ops->disable_intrs(ha);
356 spin_lock_irqsave(&ha->hardware_lock, flags);
358 /* Turn off master enable */
359 cmd = 0;
360 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
361 cmd &= ~PCI_COMMAND_MASTER;
362 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
364 if (!IS_QLA2100(ha)) {
365 /* Pause RISC. */
366 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
367 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
368 for (cnt = 0; cnt < 30000; cnt++) {
369 if ((RD_REG_WORD(&reg->hccr) &
370 HCCR_RISC_PAUSE) != 0)
371 break;
372 udelay(100);
374 } else {
375 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
376 udelay(10);
379 /* Select FPM registers. */
380 WRT_REG_WORD(&reg->ctrl_status, 0x20);
381 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
383 /* FPM Soft Reset. */
384 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
385 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
387 /* Toggle Fpm Reset. */
388 if (!IS_QLA2200(ha)) {
389 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
390 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
393 /* Select frame buffer registers. */
394 WRT_REG_WORD(&reg->ctrl_status, 0x10);
395 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
397 /* Reset frame buffer FIFOs. */
398 if (IS_QLA2200(ha)) {
399 WRT_FB_CMD_REG(ha, reg, 0xa000);
400 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
401 } else {
402 WRT_FB_CMD_REG(ha, reg, 0x00fc);
404 /* Read back fb_cmd until zero or 3 seconds max */
405 for (cnt = 0; cnt < 3000; cnt++) {
406 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
407 break;
408 udelay(100);
412 /* Select RISC module registers. */
413 WRT_REG_WORD(&reg->ctrl_status, 0);
414 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
416 /* Reset RISC processor. */
417 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
418 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
420 /* Release RISC processor. */
421 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
422 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
425 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
426 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
428 /* Reset ISP chip. */
429 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
431 /* Wait for RISC to recover from reset. */
432 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
434 * It is necessary to for a delay here since the card doesn't
435 * respond to PCI reads during a reset. On some architectures
436 * this will result in an MCA.
438 udelay(20);
439 for (cnt = 30000; cnt; cnt--) {
440 if ((RD_REG_WORD(&reg->ctrl_status) &
441 CSR_ISP_SOFT_RESET) == 0)
442 break;
443 udelay(100);
445 } else
446 udelay(10);
448 /* Reset RISC processor. */
449 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
451 WRT_REG_WORD(&reg->semaphore, 0);
453 /* Release RISC processor. */
454 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
455 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
457 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
458 for (cnt = 0; cnt < 30000; cnt++) {
459 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
460 break;
462 udelay(100);
464 } else
465 udelay(100);
467 /* Turn on master enable */
468 cmd |= PCI_COMMAND_MASTER;
469 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
471 /* Disable RISC pause on FPM parity error. */
472 if (!IS_QLA2100(ha)) {
473 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
474 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
477 spin_unlock_irqrestore(&ha->hardware_lock, flags);
481 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
482 * @ha: HA context
484 * Returns 0 on success.
486 static inline void
487 qla24xx_reset_risc(scsi_qla_host_t *ha)
489 unsigned long flags = 0;
490 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
491 uint32_t cnt, d2;
492 uint16_t wd;
494 spin_lock_irqsave(&ha->hardware_lock, flags);
496 /* Reset RISC. */
497 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
498 for (cnt = 0; cnt < 30000; cnt++) {
499 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
500 break;
502 udelay(10);
505 WRT_REG_DWORD(&reg->ctrl_status,
506 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
507 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
509 udelay(100);
510 /* Wait for firmware to complete NVRAM accesses. */
511 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
512 for (cnt = 10000 ; cnt && d2; cnt--) {
513 udelay(5);
514 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
515 barrier();
518 /* Wait for soft-reset to complete. */
519 d2 = RD_REG_DWORD(&reg->ctrl_status);
520 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
521 udelay(5);
522 d2 = RD_REG_DWORD(&reg->ctrl_status);
523 barrier();
526 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
527 RD_REG_DWORD(&reg->hccr);
529 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
530 RD_REG_DWORD(&reg->hccr);
532 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
533 RD_REG_DWORD(&reg->hccr);
535 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
536 for (cnt = 6000000 ; cnt && d2; cnt--) {
537 udelay(5);
538 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
539 barrier();
542 spin_unlock_irqrestore(&ha->hardware_lock, flags);
546 * qla24xx_reset_chip() - Reset ISP24xx chip.
547 * @ha: HA context
549 * Returns 0 on success.
551 void
552 qla24xx_reset_chip(scsi_qla_host_t *ha)
554 ha->isp_ops->disable_intrs(ha);
556 /* Perform RISC reset. */
557 qla24xx_reset_risc(ha);
561 * qla2x00_chip_diag() - Test chip for proper operation.
562 * @ha: HA context
564 * Returns 0 on success.
567 qla2x00_chip_diag(scsi_qla_host_t *ha)
569 int rval;
570 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
571 unsigned long flags = 0;
572 uint16_t data;
573 uint32_t cnt;
574 uint16_t mb[5];
576 /* Assume a failed state */
577 rval = QLA_FUNCTION_FAILED;
579 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
580 ha->host_no, (u_long)&reg->flash_address));
582 spin_lock_irqsave(&ha->hardware_lock, flags);
584 /* Reset ISP chip. */
585 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
588 * We need to have a delay here since the card will not respond while
589 * in reset causing an MCA on some architectures.
591 udelay(20);
592 data = qla2x00_debounce_register(&reg->ctrl_status);
593 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
594 udelay(5);
595 data = RD_REG_WORD(&reg->ctrl_status);
596 barrier();
599 if (!cnt)
600 goto chip_diag_failed;
602 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
603 ha->host_no));
605 /* Reset RISC processor. */
606 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
607 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
609 /* Workaround for QLA2312 PCI parity error */
610 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
611 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
612 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
613 udelay(5);
614 data = RD_MAILBOX_REG(ha, reg, 0);
615 barrier();
617 } else
618 udelay(10);
620 if (!cnt)
621 goto chip_diag_failed;
623 /* Check product ID of chip */
624 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
626 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
627 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
628 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
629 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
630 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
631 mb[3] != PROD_ID_3) {
632 qla_printk(KERN_WARNING, ha,
633 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
635 goto chip_diag_failed;
637 ha->product_id[0] = mb[1];
638 ha->product_id[1] = mb[2];
639 ha->product_id[2] = mb[3];
640 ha->product_id[3] = mb[4];
642 /* Adjust fw RISC transfer size */
643 if (ha->request_q_length > 1024)
644 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
645 else
646 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
647 ha->request_q_length;
649 if (IS_QLA2200(ha) &&
650 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
651 /* Limit firmware transfer size with a 2200A */
652 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
653 ha->host_no));
655 ha->device_type |= DT_ISP2200A;
656 ha->fw_transfer_size = 128;
659 /* Wrap Incoming Mailboxes Test. */
660 spin_unlock_irqrestore(&ha->hardware_lock, flags);
662 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
663 rval = qla2x00_mbx_reg_test(ha);
664 if (rval) {
665 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
666 ha->host_no));
667 qla_printk(KERN_WARNING, ha,
668 "Failed mailbox send register test\n");
670 else {
671 /* Flag a successful rval */
672 rval = QLA_SUCCESS;
674 spin_lock_irqsave(&ha->hardware_lock, flags);
676 chip_diag_failed:
677 if (rval)
678 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
679 "****\n", ha->host_no));
681 spin_unlock_irqrestore(&ha->hardware_lock, flags);
683 return (rval);
687 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
688 * @ha: HA context
690 * Returns 0 on success.
693 qla24xx_chip_diag(scsi_qla_host_t *ha)
695 int rval;
697 /* Perform RISC reset. */
698 qla24xx_reset_risc(ha);
700 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
702 rval = qla2x00_mbx_reg_test(ha);
703 if (rval) {
704 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
705 ha->host_no));
706 qla_printk(KERN_WARNING, ha,
707 "Failed mailbox send register test\n");
708 } else {
709 /* Flag a successful rval */
710 rval = QLA_SUCCESS;
713 return rval;
716 void
717 qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
719 int rval;
720 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
721 eft_size;
722 dma_addr_t eft_dma;
723 void *eft;
725 if (ha->fw_dump) {
726 qla_printk(KERN_WARNING, ha,
727 "Firmware dump previously allocated.\n");
728 return;
731 ha->fw_dumped = 0;
732 fixed_size = mem_size = eft_size = 0;
733 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
734 fixed_size = sizeof(struct qla2100_fw_dump);
735 } else if (IS_QLA23XX(ha)) {
736 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
737 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
738 sizeof(uint16_t);
739 } else if (IS_FWI2_CAPABLE(ha)) {
740 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
741 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
742 sizeof(uint32_t);
744 /* Allocate memory for Extended Trace Buffer. */
745 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
746 GFP_KERNEL);
747 if (!eft) {
748 qla_printk(KERN_WARNING, ha, "Unable to allocate "
749 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
750 goto cont_alloc;
753 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
754 EFT_NUM_BUFFERS);
755 if (rval) {
756 qla_printk(KERN_WARNING, ha, "Unable to initialize "
757 "EFT (%d).\n", rval);
758 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
759 eft_dma);
760 goto cont_alloc;
763 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
764 EFT_SIZE / 1024);
766 eft_size = EFT_SIZE;
767 memset(eft, 0, eft_size);
768 ha->eft_dma = eft_dma;
769 ha->eft = eft;
771 cont_alloc:
772 req_q_size = ha->request_q_length * sizeof(request_t);
773 rsp_q_size = ha->response_q_length * sizeof(response_t);
775 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
776 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
777 eft_size;
779 ha->fw_dump = vmalloc(dump_size);
780 if (!ha->fw_dump) {
781 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
782 "firmware dump!!!\n", dump_size / 1024);
784 if (ha->eft) {
785 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
786 ha->eft_dma);
787 ha->eft = NULL;
788 ha->eft_dma = 0;
790 return;
793 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
794 dump_size / 1024);
796 ha->fw_dump_len = dump_size;
797 ha->fw_dump->signature[0] = 'Q';
798 ha->fw_dump->signature[1] = 'L';
799 ha->fw_dump->signature[2] = 'G';
800 ha->fw_dump->signature[3] = 'C';
801 ha->fw_dump->version = __constant_htonl(1);
803 ha->fw_dump->fixed_size = htonl(fixed_size);
804 ha->fw_dump->mem_size = htonl(mem_size);
805 ha->fw_dump->req_q_size = htonl(req_q_size);
806 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
808 ha->fw_dump->eft_size = htonl(eft_size);
809 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
810 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
812 ha->fw_dump->header_size =
813 htonl(offsetof(struct qla2xxx_fw_dump, isp));
817 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
818 * @ha: HA context
820 * Returns 0 on success.
822 static void
823 qla2x00_resize_request_q(scsi_qla_host_t *ha)
825 int rval;
826 uint16_t fw_iocb_cnt = 0;
827 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
828 dma_addr_t request_dma;
829 request_t *request_ring;
831 /* Valid only on recent ISPs. */
832 if (IS_QLA2100(ha) || IS_QLA2200(ha))
833 return;
835 /* Retrieve IOCB counts available to the firmware. */
836 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
837 if (rval)
838 return;
839 /* No point in continuing if current settings are sufficient. */
840 if (fw_iocb_cnt < 1024)
841 return;
842 if (ha->request_q_length >= request_q_length)
843 return;
845 /* Attempt to claim larger area for request queue. */
846 request_ring = dma_alloc_coherent(&ha->pdev->dev,
847 (request_q_length + 1) * sizeof(request_t), &request_dma,
848 GFP_KERNEL);
849 if (request_ring == NULL)
850 return;
852 /* Resize successful, report extensions. */
853 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
854 (ha->fw_memory_size + 1) / 1024);
855 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
856 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
858 /* Clear old allocations. */
859 dma_free_coherent(&ha->pdev->dev,
860 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
861 ha->request_dma);
863 /* Begin using larger queue. */
864 ha->request_q_length = request_q_length;
865 ha->request_ring = request_ring;
866 ha->request_dma = request_dma;
870 * qla2x00_setup_chip() - Load and start RISC firmware.
871 * @ha: HA context
873 * Returns 0 on success.
875 static int
876 qla2x00_setup_chip(scsi_qla_host_t *ha)
878 int rval;
879 uint32_t srisc_address = 0;
881 /* Load firmware sequences */
882 rval = ha->isp_ops->load_risc(ha, &srisc_address);
883 if (rval == QLA_SUCCESS) {
884 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
885 "code.\n", ha->host_no));
887 rval = qla2x00_verify_checksum(ha, srisc_address);
888 if (rval == QLA_SUCCESS) {
889 /* Start firmware execution. */
890 DEBUG(printk("scsi(%ld): Checksum OK, start "
891 "firmware.\n", ha->host_no));
893 rval = qla2x00_execute_fw(ha, srisc_address);
894 /* Retrieve firmware information. */
895 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
896 qla2x00_get_fw_version(ha,
897 &ha->fw_major_version,
898 &ha->fw_minor_version,
899 &ha->fw_subminor_version,
900 &ha->fw_attributes, &ha->fw_memory_size);
901 qla2x00_resize_request_q(ha);
902 ha->flags.npiv_supported = 0;
903 if (IS_QLA24XX(ha) &&
904 (ha->fw_attributes & BIT_2))
905 ha->flags.npiv_supported = 1;
907 if (ql2xallocfwdump)
908 qla2x00_alloc_fw_dump(ha);
910 } else {
911 DEBUG2(printk(KERN_INFO
912 "scsi(%ld): ISP Firmware failed checksum.\n",
913 ha->host_no));
917 if (rval) {
918 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
919 ha->host_no));
922 return (rval);
926 * qla2x00_init_response_q_entries() - Initializes response queue entries.
927 * @ha: HA context
929 * Beginning of request ring has initialization control block already built
930 * by nvram config routine.
932 * Returns 0 on success.
934 static void
935 qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
937 uint16_t cnt;
938 response_t *pkt;
940 pkt = ha->response_ring_ptr;
941 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
942 pkt->signature = RESPONSE_PROCESSED;
943 pkt++;
949 * qla2x00_update_fw_options() - Read and process firmware options.
950 * @ha: HA context
952 * Returns 0 on success.
954 void
955 qla2x00_update_fw_options(scsi_qla_host_t *ha)
957 uint16_t swing, emphasis, tx_sens, rx_sens;
959 memset(ha->fw_options, 0, sizeof(ha->fw_options));
960 qla2x00_get_fw_options(ha, ha->fw_options);
962 if (IS_QLA2100(ha) || IS_QLA2200(ha))
963 return;
965 /* Serial Link options. */
966 DEBUG3(printk("scsi(%ld): Serial link options:\n",
967 ha->host_no));
968 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
969 sizeof(ha->fw_seriallink_options)));
971 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
972 if (ha->fw_seriallink_options[3] & BIT_2) {
973 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
975 /* 1G settings */
976 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
977 emphasis = (ha->fw_seriallink_options[2] &
978 (BIT_4 | BIT_3)) >> 3;
979 tx_sens = ha->fw_seriallink_options[0] &
980 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
981 rx_sens = (ha->fw_seriallink_options[0] &
982 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
983 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
984 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
985 if (rx_sens == 0x0)
986 rx_sens = 0x3;
987 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
988 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
989 ha->fw_options[10] |= BIT_5 |
990 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
991 (tx_sens & (BIT_1 | BIT_0));
993 /* 2G settings */
994 swing = (ha->fw_seriallink_options[2] &
995 (BIT_7 | BIT_6 | BIT_5)) >> 5;
996 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
997 tx_sens = ha->fw_seriallink_options[1] &
998 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
999 rx_sens = (ha->fw_seriallink_options[1] &
1000 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1001 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1002 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1003 if (rx_sens == 0x0)
1004 rx_sens = 0x3;
1005 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1006 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1007 ha->fw_options[11] |= BIT_5 |
1008 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1009 (tx_sens & (BIT_1 | BIT_0));
1012 /* FCP2 options. */
1013 /* Return command IOCBs without waiting for an ABTS to complete. */
1014 ha->fw_options[3] |= BIT_13;
1016 /* LED scheme. */
1017 if (ha->flags.enable_led_scheme)
1018 ha->fw_options[2] |= BIT_12;
1020 /* Detect ISP6312. */
1021 if (IS_QLA6312(ha))
1022 ha->fw_options[2] |= BIT_13;
1024 /* Update firmware options. */
1025 qla2x00_set_fw_options(ha, ha->fw_options);
1028 void
1029 qla24xx_update_fw_options(scsi_qla_host_t *ha)
1031 int rval;
1033 /* Update Serial Link options. */
1034 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
1035 return;
1037 rval = qla2x00_set_serdes_params(ha,
1038 le16_to_cpu(ha->fw_seriallink_options24[1]),
1039 le16_to_cpu(ha->fw_seriallink_options24[2]),
1040 le16_to_cpu(ha->fw_seriallink_options24[3]));
1041 if (rval != QLA_SUCCESS) {
1042 qla_printk(KERN_WARNING, ha,
1043 "Unable to update Serial Link options (%x).\n", rval);
1047 void
1048 qla2x00_config_rings(struct scsi_qla_host *ha)
1050 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1052 /* Setup ring parameters in initialization control block. */
1053 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1054 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1055 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1056 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1057 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1058 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1059 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1060 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1062 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1063 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1064 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1065 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1066 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1069 void
1070 qla24xx_config_rings(struct scsi_qla_host *ha)
1072 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1073 struct init_cb_24xx *icb;
1075 /* Setup ring parameters in initialization control block. */
1076 icb = (struct init_cb_24xx *)ha->init_cb;
1077 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1078 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1079 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1080 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1081 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1082 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1083 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1084 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1086 WRT_REG_DWORD(&reg->req_q_in, 0);
1087 WRT_REG_DWORD(&reg->req_q_out, 0);
1088 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1089 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1090 RD_REG_DWORD(&reg->rsp_q_out);
1094 * qla2x00_init_rings() - Initializes firmware.
1095 * @ha: HA context
1097 * Beginning of request ring has initialization control block already built
1098 * by nvram config routine.
1100 * Returns 0 on success.
1102 static int
1103 qla2x00_init_rings(scsi_qla_host_t *ha)
1105 int rval;
1106 unsigned long flags = 0;
1107 int cnt;
1108 struct mid_init_cb_24xx *mid_init_cb =
1109 (struct mid_init_cb_24xx *) ha->init_cb;
1111 spin_lock_irqsave(&ha->hardware_lock, flags);
1113 /* Clear outstanding commands array. */
1114 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1115 ha->outstanding_cmds[cnt] = NULL;
1117 ha->current_outstanding_cmd = 0;
1119 /* Clear RSCN queue. */
1120 ha->rscn_in_ptr = 0;
1121 ha->rscn_out_ptr = 0;
1123 /* Initialize firmware. */
1124 ha->request_ring_ptr = ha->request_ring;
1125 ha->req_ring_index = 0;
1126 ha->req_q_cnt = ha->request_q_length;
1127 ha->response_ring_ptr = ha->response_ring;
1128 ha->rsp_ring_index = 0;
1130 /* Initialize response queue entries */
1131 qla2x00_init_response_q_entries(ha);
1133 ha->isp_ops->config_rings(ha);
1135 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1137 /* Update any ISP specific firmware options before initialization. */
1138 ha->isp_ops->update_fw_options(ha);
1140 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
1142 mid_init_cb->count = MAX_NUM_VPORT_FABRIC;
1143 ha->max_npiv_vports = MAX_NUM_VPORT_FABRIC;
1145 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1146 if (rval) {
1147 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1148 ha->host_no));
1149 } else {
1150 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1151 ha->host_no));
1154 return (rval);
1158 * qla2x00_fw_ready() - Waits for firmware ready.
1159 * @ha: HA context
1161 * Returns 0 on success.
1163 static int
1164 qla2x00_fw_ready(scsi_qla_host_t *ha)
1166 int rval;
1167 unsigned long wtime, mtime;
1168 uint16_t min_wait; /* Minimum wait time if loop is down */
1169 uint16_t wait_time; /* Wait time if loop is coming ready */
1170 uint16_t fw_state;
1172 rval = QLA_SUCCESS;
1174 /* 20 seconds for loop down. */
1175 min_wait = 20;
1178 * Firmware should take at most one RATOV to login, plus 5 seconds for
1179 * our own processing.
1181 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1182 wait_time = min_wait;
1185 /* Min wait time if loop down */
1186 mtime = jiffies + (min_wait * HZ);
1188 /* wait time before firmware ready */
1189 wtime = jiffies + (wait_time * HZ);
1191 /* Wait for ISP to finish LIP */
1192 if (!ha->flags.init_done)
1193 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1195 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1196 ha->host_no));
1198 do {
1199 rval = qla2x00_get_firmware_state(ha, &fw_state);
1200 if (rval == QLA_SUCCESS) {
1201 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1202 ha->device_flags &= ~DFLG_NO_CABLE;
1204 if (fw_state == FSTATE_READY) {
1205 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1206 ha->host_no));
1208 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1209 &ha->login_timeout, &ha->r_a_tov);
1211 rval = QLA_SUCCESS;
1212 break;
1215 rval = QLA_FUNCTION_FAILED;
1217 if (atomic_read(&ha->loop_down_timer) &&
1218 fw_state != FSTATE_READY) {
1219 /* Loop down. Timeout on min_wait for states
1220 * other than Wait for Login.
1222 if (time_after_eq(jiffies, mtime)) {
1223 qla_printk(KERN_INFO, ha,
1224 "Cable is unplugged...\n");
1226 ha->device_flags |= DFLG_NO_CABLE;
1227 break;
1230 } else {
1231 /* Mailbox cmd failed. Timeout on min_wait. */
1232 if (time_after_eq(jiffies, mtime))
1233 break;
1236 if (time_after_eq(jiffies, wtime))
1237 break;
1239 /* Delay for a while */
1240 msleep(500);
1242 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1243 ha->host_no, fw_state, jiffies));
1244 } while (1);
1246 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1247 ha->host_no, fw_state, jiffies));
1249 if (rval) {
1250 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1251 ha->host_no));
1254 return (rval);
1258 * qla2x00_configure_hba
1259 * Setup adapter context.
1261 * Input:
1262 * ha = adapter state pointer.
1264 * Returns:
1265 * 0 = success
1267 * Context:
1268 * Kernel context.
1270 static int
1271 qla2x00_configure_hba(scsi_qla_host_t *ha)
1273 int rval;
1274 uint16_t loop_id;
1275 uint16_t topo;
1276 uint16_t sw_cap;
1277 uint8_t al_pa;
1278 uint8_t area;
1279 uint8_t domain;
1280 char connect_type[22];
1282 /* Get host addresses. */
1283 rval = qla2x00_get_adapter_id(ha,
1284 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
1285 if (rval != QLA_SUCCESS) {
1286 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
1287 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1288 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1289 __func__, ha->host_no));
1290 } else {
1291 qla_printk(KERN_WARNING, ha,
1292 "ERROR -- Unable to get host loop ID.\n");
1293 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1295 return (rval);
1298 if (topo == 4) {
1299 qla_printk(KERN_INFO, ha,
1300 "Cannot get topology - retrying.\n");
1301 return (QLA_FUNCTION_FAILED);
1304 ha->loop_id = loop_id;
1306 /* initialize */
1307 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1308 ha->operating_mode = LOOP;
1309 ha->switch_cap = 0;
1311 switch (topo) {
1312 case 0:
1313 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1314 ha->host_no));
1315 ha->current_topology = ISP_CFG_NL;
1316 strcpy(connect_type, "(Loop)");
1317 break;
1319 case 1:
1320 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1321 ha->host_no));
1322 ha->switch_cap = sw_cap;
1323 ha->current_topology = ISP_CFG_FL;
1324 strcpy(connect_type, "(FL_Port)");
1325 break;
1327 case 2:
1328 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1329 ha->host_no));
1330 ha->operating_mode = P2P;
1331 ha->current_topology = ISP_CFG_N;
1332 strcpy(connect_type, "(N_Port-to-N_Port)");
1333 break;
1335 case 3:
1336 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1337 ha->host_no));
1338 ha->switch_cap = sw_cap;
1339 ha->operating_mode = P2P;
1340 ha->current_topology = ISP_CFG_F;
1341 strcpy(connect_type, "(F_Port)");
1342 break;
1344 default:
1345 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1346 "Using NL.\n",
1347 ha->host_no, topo));
1348 ha->current_topology = ISP_CFG_NL;
1349 strcpy(connect_type, "(Loop)");
1350 break;
1353 /* Save Host port and loop ID. */
1354 /* byte order - Big Endian */
1355 ha->d_id.b.domain = domain;
1356 ha->d_id.b.area = area;
1357 ha->d_id.b.al_pa = al_pa;
1359 if (!ha->flags.init_done)
1360 qla_printk(KERN_INFO, ha,
1361 "Topology - %s, Host Loop address 0x%x\n",
1362 connect_type, ha->loop_id);
1364 if (rval) {
1365 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1366 } else {
1367 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1370 return(rval);
1373 static inline void
1374 qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
1376 char *st, *en;
1377 uint16_t index;
1379 if (memcmp(model, BINZERO, len) != 0) {
1380 strncpy(ha->model_number, model, len);
1381 st = en = ha->model_number;
1382 en += len - 1;
1383 while (en > st) {
1384 if (*en != 0x20 && *en != 0x00)
1385 break;
1386 *en-- = '\0';
1389 index = (ha->pdev->subsystem_device & 0xff);
1390 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1391 index < QLA_MODEL_NAMES)
1392 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1393 } else {
1394 index = (ha->pdev->subsystem_device & 0xff);
1395 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1396 index < QLA_MODEL_NAMES) {
1397 strcpy(ha->model_number,
1398 qla2x00_model_name[index * 2]);
1399 ha->model_desc = qla2x00_model_name[index * 2 + 1];
1400 } else {
1401 strcpy(ha->model_number, def);
1406 /* On sparc systems, obtain port and node WWN from firmware
1407 * properties.
1409 static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
1411 #ifdef CONFIG_SPARC
1412 struct pci_dev *pdev = ha->pdev;
1413 struct device_node *dp = pci_device_to_OF_node(pdev);
1414 const u8 *val;
1415 int len;
1417 val = of_get_property(dp, "port-wwn", &len);
1418 if (val && len >= WWN_SIZE)
1419 memcpy(nv->port_name, val, WWN_SIZE);
1421 val = of_get_property(dp, "node-wwn", &len);
1422 if (val && len >= WWN_SIZE)
1423 memcpy(nv->node_name, val, WWN_SIZE);
1424 #endif
1428 * NVRAM configuration for ISP 2xxx
1430 * Input:
1431 * ha = adapter block pointer.
1433 * Output:
1434 * initialization control block in response_ring
1435 * host adapters parameters in host adapter block
1437 * Returns:
1438 * 0 = success.
1441 qla2x00_nvram_config(scsi_qla_host_t *ha)
1443 int rval;
1444 uint8_t chksum = 0;
1445 uint16_t cnt;
1446 uint8_t *dptr1, *dptr2;
1447 init_cb_t *icb = ha->init_cb;
1448 nvram_t *nv = (nvram_t *)ha->request_ring;
1449 uint8_t *ptr = (uint8_t *)ha->request_ring;
1450 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1452 rval = QLA_SUCCESS;
1454 /* Determine NVRAM starting address. */
1455 ha->nvram_size = sizeof(nvram_t);
1456 ha->nvram_base = 0;
1457 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1458 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1459 ha->nvram_base = 0x80;
1461 /* Get NVRAM data and calculate checksum. */
1462 ha->isp_ops->read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1463 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1464 chksum += *ptr++;
1466 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1467 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1468 ha->nvram_size));
1470 /* Bad NVRAM data, set defaults parameters. */
1471 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1472 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1473 /* Reset NVRAM data. */
1474 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1475 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1476 nv->nvram_version);
1477 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1478 "invalid -- WWPN) defaults.\n");
1481 * Set default initialization control block.
1483 memset(nv, 0, ha->nvram_size);
1484 nv->parameter_block_version = ICB_VERSION;
1486 if (IS_QLA23XX(ha)) {
1487 nv->firmware_options[0] = BIT_2 | BIT_1;
1488 nv->firmware_options[1] = BIT_7 | BIT_5;
1489 nv->add_firmware_options[0] = BIT_5;
1490 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1491 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1492 nv->special_options[1] = BIT_7;
1493 } else if (IS_QLA2200(ha)) {
1494 nv->firmware_options[0] = BIT_2 | BIT_1;
1495 nv->firmware_options[1] = BIT_7 | BIT_5;
1496 nv->add_firmware_options[0] = BIT_5;
1497 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1498 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1499 } else if (IS_QLA2100(ha)) {
1500 nv->firmware_options[0] = BIT_3 | BIT_1;
1501 nv->firmware_options[1] = BIT_5;
1502 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1505 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1506 nv->execution_throttle = __constant_cpu_to_le16(16);
1507 nv->retry_count = 8;
1508 nv->retry_delay = 1;
1510 nv->port_name[0] = 33;
1511 nv->port_name[3] = 224;
1512 nv->port_name[4] = 139;
1514 qla2xxx_nvram_wwn_from_ofw(ha, nv);
1516 nv->login_timeout = 4;
1519 * Set default host adapter parameters
1521 nv->host_p[1] = BIT_2;
1522 nv->reset_delay = 5;
1523 nv->port_down_retry_count = 8;
1524 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1525 nv->link_down_timeout = 60;
1527 rval = 1;
1530 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1532 * The SN2 does not provide BIOS emulation which means you can't change
1533 * potentially bogus BIOS settings. Force the use of default settings
1534 * for link rate and frame size. Hope that the rest of the settings
1535 * are valid.
1537 if (ia64_platform_is("sn2")) {
1538 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1539 if (IS_QLA23XX(ha))
1540 nv->special_options[1] = BIT_7;
1542 #endif
1544 /* Reset Initialization control block */
1545 memset(icb, 0, ha->init_cb_size);
1548 * Setup driver NVRAM options.
1550 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1551 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1552 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1553 nv->firmware_options[1] &= ~BIT_4;
1555 if (IS_QLA23XX(ha)) {
1556 nv->firmware_options[0] |= BIT_2;
1557 nv->firmware_options[0] &= ~BIT_3;
1558 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1560 if (IS_QLA2300(ha)) {
1561 if (ha->fb_rev == FPM_2310) {
1562 strcpy(ha->model_number, "QLA2310");
1563 } else {
1564 strcpy(ha->model_number, "QLA2300");
1566 } else {
1567 qla2x00_set_model_info(ha, nv->model_number,
1568 sizeof(nv->model_number), "QLA23xx");
1570 } else if (IS_QLA2200(ha)) {
1571 nv->firmware_options[0] |= BIT_2;
1573 * 'Point-to-point preferred, else loop' is not a safe
1574 * connection mode setting.
1576 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1577 (BIT_5 | BIT_4)) {
1578 /* Force 'loop preferred, else point-to-point'. */
1579 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1580 nv->add_firmware_options[0] |= BIT_5;
1582 strcpy(ha->model_number, "QLA22xx");
1583 } else /*if (IS_QLA2100(ha))*/ {
1584 strcpy(ha->model_number, "QLA2100");
1588 * Copy over NVRAM RISC parameter block to initialization control block.
1590 dptr1 = (uint8_t *)icb;
1591 dptr2 = (uint8_t *)&nv->parameter_block_version;
1592 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1593 while (cnt--)
1594 *dptr1++ = *dptr2++;
1596 /* Copy 2nd half. */
1597 dptr1 = (uint8_t *)icb->add_firmware_options;
1598 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1599 while (cnt--)
1600 *dptr1++ = *dptr2++;
1602 /* Use alternate WWN? */
1603 if (nv->host_p[1] & BIT_7) {
1604 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1605 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1608 /* Prepare nodename */
1609 if ((icb->firmware_options[1] & BIT_6) == 0) {
1611 * Firmware will apply the following mask if the nodename was
1612 * not provided.
1614 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1615 icb->node_name[0] &= 0xF0;
1619 * Set host adapter parameters.
1621 if (nv->host_p[0] & BIT_7)
1622 ql2xextended_error_logging = 1;
1623 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1624 /* Always load RISC code on non ISP2[12]00 chips. */
1625 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1626 ha->flags.disable_risc_code_load = 0;
1627 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1628 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1629 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1630 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1631 ha->flags.disable_serdes = 0;
1633 ha->operating_mode =
1634 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1636 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1637 sizeof(ha->fw_seriallink_options));
1639 /* save HBA serial number */
1640 ha->serial0 = icb->port_name[5];
1641 ha->serial1 = icb->port_name[6];
1642 ha->serial2 = icb->port_name[7];
1643 ha->node_name = icb->node_name;
1644 ha->port_name = icb->port_name;
1646 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1648 ha->retry_count = nv->retry_count;
1650 /* Set minimum login_timeout to 4 seconds. */
1651 if (nv->login_timeout < ql2xlogintimeout)
1652 nv->login_timeout = ql2xlogintimeout;
1653 if (nv->login_timeout < 4)
1654 nv->login_timeout = 4;
1655 ha->login_timeout = nv->login_timeout;
1656 icb->login_timeout = nv->login_timeout;
1658 /* Set minimum RATOV to 200 tenths of a second. */
1659 ha->r_a_tov = 200;
1661 ha->loop_reset_delay = nv->reset_delay;
1663 /* Link Down Timeout = 0:
1665 * When Port Down timer expires we will start returning
1666 * I/O's to OS with "DID_NO_CONNECT".
1668 * Link Down Timeout != 0:
1670 * The driver waits for the link to come up after link down
1671 * before returning I/Os to OS with "DID_NO_CONNECT".
1673 if (nv->link_down_timeout == 0) {
1674 ha->loop_down_abort_time =
1675 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1676 } else {
1677 ha->link_down_timeout = nv->link_down_timeout;
1678 ha->loop_down_abort_time =
1679 (LOOP_DOWN_TIME - ha->link_down_timeout);
1683 * Need enough time to try and get the port back.
1685 ha->port_down_retry_count = nv->port_down_retry_count;
1686 if (qlport_down_retry)
1687 ha->port_down_retry_count = qlport_down_retry;
1688 /* Set login_retry_count */
1689 ha->login_retry_count = nv->retry_count;
1690 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1691 ha->port_down_retry_count > 3)
1692 ha->login_retry_count = ha->port_down_retry_count;
1693 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1694 ha->login_retry_count = ha->port_down_retry_count;
1695 if (ql2xloginretrycount)
1696 ha->login_retry_count = ql2xloginretrycount;
1698 icb->lun_enables = __constant_cpu_to_le16(0);
1699 icb->command_resource_count = 0;
1700 icb->immediate_notify_resource_count = 0;
1701 icb->timeout = __constant_cpu_to_le16(0);
1703 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1704 /* Enable RIO */
1705 icb->firmware_options[0] &= ~BIT_3;
1706 icb->add_firmware_options[0] &=
1707 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1708 icb->add_firmware_options[0] |= BIT_2;
1709 icb->response_accumulation_timer = 3;
1710 icb->interrupt_delay_timer = 5;
1712 ha->flags.process_response_queue = 1;
1713 } else {
1714 /* Enable ZIO. */
1715 if (!ha->flags.init_done) {
1716 ha->zio_mode = icb->add_firmware_options[0] &
1717 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1718 ha->zio_timer = icb->interrupt_delay_timer ?
1719 icb->interrupt_delay_timer: 2;
1721 icb->add_firmware_options[0] &=
1722 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1723 ha->flags.process_response_queue = 0;
1724 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1725 ha->zio_mode = QLA_ZIO_MODE_6;
1727 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1728 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1729 ha->zio_timer * 100));
1730 qla_printk(KERN_INFO, ha,
1731 "ZIO mode %d enabled; timer delay (%d us).\n",
1732 ha->zio_mode, ha->zio_timer * 100);
1734 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1735 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1736 ha->flags.process_response_queue = 1;
1740 if (rval) {
1741 DEBUG2_3(printk(KERN_WARNING
1742 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1744 return (rval);
1747 static void
1748 qla2x00_rport_del(void *data)
1750 fc_port_t *fcport = data;
1751 struct fc_rport *rport;
1752 unsigned long flags;
1754 spin_lock_irqsave(&fcport->rport_lock, flags);
1755 rport = fcport->drport;
1756 fcport->drport = NULL;
1757 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1758 if (rport)
1759 fc_remote_port_delete(rport);
1763 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1764 * @ha: HA context
1765 * @flags: allocation flags
1767 * Returns a pointer to the allocated fcport, or NULL, if none available.
1769 static fc_port_t *
1770 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1772 fc_port_t *fcport;
1774 fcport = kmalloc(sizeof(fc_port_t), flags);
1775 if (fcport == NULL)
1776 return (fcport);
1778 /* Setup fcport template structure. */
1779 memset(fcport, 0, sizeof (fc_port_t));
1780 fcport->ha = ha;
1781 fcport->vp_idx = ha->vp_idx;
1782 fcport->port_type = FCT_UNKNOWN;
1783 fcport->loop_id = FC_NO_LOOP_ID;
1784 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1785 fcport->flags = FCF_RLC_SUPPORT;
1786 fcport->supported_classes = FC_COS_UNSPECIFIED;
1787 spin_lock_init(&fcport->rport_lock);
1789 return (fcport);
1793 * qla2x00_configure_loop
1794 * Updates Fibre Channel Device Database with what is actually on loop.
1796 * Input:
1797 * ha = adapter block pointer.
1799 * Returns:
1800 * 0 = success.
1801 * 1 = error.
1802 * 2 = database was full and device was not configured.
1804 static int
1805 qla2x00_configure_loop(scsi_qla_host_t *ha)
1807 int rval;
1808 unsigned long flags, save_flags;
1810 rval = QLA_SUCCESS;
1812 /* Get Initiator ID */
1813 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1814 rval = qla2x00_configure_hba(ha);
1815 if (rval != QLA_SUCCESS) {
1816 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1817 ha->host_no));
1818 return (rval);
1822 save_flags = flags = ha->dpc_flags;
1823 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1824 ha->host_no, flags));
1827 * If we have both an RSCN and PORT UPDATE pending then handle them
1828 * both at the same time.
1830 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1831 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1833 /* Determine what we need to do */
1834 if (ha->current_topology == ISP_CFG_FL &&
1835 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1837 ha->flags.rscn_queue_overflow = 1;
1838 set_bit(RSCN_UPDATE, &flags);
1840 } else if (ha->current_topology == ISP_CFG_F &&
1841 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1843 ha->flags.rscn_queue_overflow = 1;
1844 set_bit(RSCN_UPDATE, &flags);
1845 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1847 } else if (ha->current_topology == ISP_CFG_N) {
1848 clear_bit(RSCN_UPDATE, &flags);
1850 } else if (!ha->flags.online ||
1851 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1853 ha->flags.rscn_queue_overflow = 1;
1854 set_bit(RSCN_UPDATE, &flags);
1855 set_bit(LOCAL_LOOP_UPDATE, &flags);
1858 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1859 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1860 rval = QLA_FUNCTION_FAILED;
1861 } else {
1862 rval = qla2x00_configure_local_loop(ha);
1866 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1867 if (LOOP_TRANSITION(ha)) {
1868 rval = QLA_FUNCTION_FAILED;
1869 } else {
1870 rval = qla2x00_configure_fabric(ha);
1874 if (rval == QLA_SUCCESS) {
1875 if (atomic_read(&ha->loop_down_timer) ||
1876 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1877 rval = QLA_FUNCTION_FAILED;
1878 } else {
1879 atomic_set(&ha->loop_state, LOOP_READY);
1881 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1885 if (rval) {
1886 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1887 __func__, ha->host_no));
1888 } else {
1889 DEBUG3(printk("%s: exiting normally\n", __func__));
1892 /* Restore state if a resync event occured during processing */
1893 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1894 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1895 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1896 if (test_bit(RSCN_UPDATE, &save_flags))
1897 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1900 return (rval);
1906 * qla2x00_configure_local_loop
1907 * Updates Fibre Channel Device Database with local loop devices.
1909 * Input:
1910 * ha = adapter block pointer.
1912 * Returns:
1913 * 0 = success.
1915 static int
1916 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1918 int rval, rval2;
1919 int found_devs;
1920 int found;
1921 fc_port_t *fcport, *new_fcport;
1923 uint16_t index;
1924 uint16_t entries;
1925 char *id_iter;
1926 uint16_t loop_id;
1927 uint8_t domain, area, al_pa;
1928 scsi_qla_host_t *pha = to_qla_parent(ha);
1930 found_devs = 0;
1931 new_fcport = NULL;
1932 entries = MAX_FIBRE_DEVICES;
1934 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1935 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1937 /* Get list of logged in devices. */
1938 memset(ha->gid_list, 0, GID_LIST_SIZE);
1939 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1940 &entries);
1941 if (rval != QLA_SUCCESS)
1942 goto cleanup_allocation;
1944 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1945 ha->host_no, entries));
1946 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1947 entries * sizeof(struct gid_list_info)));
1949 /* Allocate temporary fcport for any new fcports discovered. */
1950 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1951 if (new_fcport == NULL) {
1952 rval = QLA_MEMORY_ALLOC_FAILED;
1953 goto cleanup_allocation;
1955 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1958 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1960 list_for_each_entry(fcport, &pha->fcports, list) {
1961 if (fcport->vp_idx != ha->vp_idx)
1962 continue;
1964 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1965 fcport->port_type != FCT_BROADCAST &&
1966 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1968 DEBUG(printk("scsi(%ld): Marking port lost, "
1969 "loop_id=0x%04x\n",
1970 ha->host_no, fcport->loop_id));
1972 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1973 fcport->flags &= ~FCF_FARP_DONE;
1977 /* Add devices to port list. */
1978 id_iter = (char *)ha->gid_list;
1979 for (index = 0; index < entries; index++) {
1980 domain = ((struct gid_list_info *)id_iter)->domain;
1981 area = ((struct gid_list_info *)id_iter)->area;
1982 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1983 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1984 loop_id = (uint16_t)
1985 ((struct gid_list_info *)id_iter)->loop_id_2100;
1986 else
1987 loop_id = le16_to_cpu(
1988 ((struct gid_list_info *)id_iter)->loop_id);
1989 id_iter += ha->gid_list_info_size;
1991 /* Bypass reserved domain fields. */
1992 if ((domain & 0xf0) == 0xf0)
1993 continue;
1995 /* Bypass if not same domain and area of adapter. */
1996 if (area && domain &&
1997 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
1998 continue;
2000 /* Bypass invalid local loop ID. */
2001 if (loop_id > LAST_LOCAL_LOOP_ID)
2002 continue;
2004 /* Fill in member data. */
2005 new_fcport->d_id.b.domain = domain;
2006 new_fcport->d_id.b.area = area;
2007 new_fcport->d_id.b.al_pa = al_pa;
2008 new_fcport->loop_id = loop_id;
2009 new_fcport->vp_idx = ha->vp_idx;
2010 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2011 if (rval2 != QLA_SUCCESS) {
2012 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2013 "information -- get_port_database=%x, "
2014 "loop_id=0x%04x\n",
2015 ha->host_no, rval2, new_fcport->loop_id));
2016 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2017 ha->host_no));
2018 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2019 continue;
2022 /* Check for matching device in port list. */
2023 found = 0;
2024 fcport = NULL;
2025 list_for_each_entry(fcport, &pha->fcports, list) {
2026 if (fcport->vp_idx != ha->vp_idx)
2027 continue;
2029 if (memcmp(new_fcport->port_name, fcport->port_name,
2030 WWN_SIZE))
2031 continue;
2033 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2034 FCF_PERSISTENT_BOUND);
2035 fcport->loop_id = new_fcport->loop_id;
2036 fcport->port_type = new_fcport->port_type;
2037 fcport->d_id.b24 = new_fcport->d_id.b24;
2038 memcpy(fcport->node_name, new_fcport->node_name,
2039 WWN_SIZE);
2041 found++;
2042 break;
2045 if (!found) {
2046 /* New device, add to fcports list. */
2047 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2048 if (ha->parent) {
2049 new_fcport->ha = ha;
2050 new_fcport->vp_idx = ha->vp_idx;
2051 list_add_tail(&new_fcport->vp_fcport,
2052 &ha->vp_fcports);
2054 list_add_tail(&new_fcport->list, &pha->fcports);
2056 /* Allocate a new replacement fcport. */
2057 fcport = new_fcport;
2058 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2059 if (new_fcport == NULL) {
2060 rval = QLA_MEMORY_ALLOC_FAILED;
2061 goto cleanup_allocation;
2063 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2066 /* Base iIDMA settings on HBA port speed. */
2067 switch (ha->link_data_rate) {
2068 case PORT_SPEED_1GB:
2069 fcport->fp_speed = cpu_to_be16(BIT_15);
2070 break;
2071 case PORT_SPEED_2GB:
2072 fcport->fp_speed = cpu_to_be16(BIT_14);
2073 break;
2074 case PORT_SPEED_4GB:
2075 fcport->fp_speed = cpu_to_be16(BIT_13);
2076 break;
2079 qla2x00_update_fcport(ha, fcport);
2081 found_devs++;
2084 cleanup_allocation:
2085 kfree(new_fcport);
2087 if (rval != QLA_SUCCESS) {
2088 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2089 "rval=%x\n", ha->host_no, rval));
2092 if (found_devs) {
2093 ha->device_flags |= DFLG_LOCAL_DEVICES;
2094 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2097 return (rval);
2100 static void
2101 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2103 fc_port_t *fcport;
2105 qla2x00_mark_all_devices_lost(ha, 0);
2106 list_for_each_entry(fcport, &ha->fcports, list) {
2107 if (fcport->port_type != FCT_TARGET)
2108 continue;
2110 qla2x00_update_fcport(ha, fcport);
2114 static void
2115 qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2117 #define LS_UNKNOWN 2
2118 static char *link_speeds[5] = { "1", "2", "?", "4" };
2119 int rval;
2120 uint16_t port_speed, mb[6];
2122 if (!IS_IIDMA_CAPABLE(ha))
2123 return;
2125 switch (be16_to_cpu(fcport->fp_speed)) {
2126 case BIT_15:
2127 port_speed = PORT_SPEED_1GB;
2128 break;
2129 case BIT_14:
2130 port_speed = PORT_SPEED_2GB;
2131 break;
2132 case BIT_13:
2133 port_speed = PORT_SPEED_4GB;
2134 break;
2135 default:
2136 DEBUG2(printk("scsi(%ld): %02x%02x%02x%02x%02x%02x%02x%02x -- "
2137 "unsupported FM port operating speed (%04x).\n",
2138 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2139 fcport->port_name[2], fcport->port_name[3],
2140 fcport->port_name[4], fcport->port_name[5],
2141 fcport->port_name[6], fcport->port_name[7],
2142 be16_to_cpu(fcport->fp_speed)));
2143 port_speed = PORT_SPEED_UNKNOWN;
2144 break;
2146 if (port_speed == PORT_SPEED_UNKNOWN)
2147 return;
2149 rval = qla2x00_set_idma_speed(ha, fcport->loop_id, port_speed, mb);
2150 if (rval != QLA_SUCCESS) {
2151 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2152 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2153 ha->host_no, fcport->port_name[0], fcport->port_name[1],
2154 fcport->port_name[2], fcport->port_name[3],
2155 fcport->port_name[4], fcport->port_name[5],
2156 fcport->port_name[6], fcport->port_name[7], rval,
2157 port_speed, mb[0], mb[1]));
2158 } else {
2159 DEBUG2(qla_printk(KERN_INFO, ha,
2160 "iIDMA adjusted to %s GB/s on "
2161 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2162 link_speeds[port_speed], fcport->port_name[0],
2163 fcport->port_name[1], fcport->port_name[2],
2164 fcport->port_name[3], fcport->port_name[4],
2165 fcport->port_name[5], fcport->port_name[6],
2166 fcport->port_name[7]));
2170 static void
2171 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2173 struct fc_rport_identifiers rport_ids;
2174 struct fc_rport *rport;
2175 unsigned long flags;
2177 if (fcport->drport)
2178 qla2x00_rport_del(fcport);
2179 if (fcport->rport)
2180 return;
2182 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2183 rport_ids.port_name = wwn_to_u64(fcport->port_name);
2184 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2185 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2186 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2187 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2188 if (!rport) {
2189 qla_printk(KERN_WARNING, ha,
2190 "Unable to allocate fc remote port!\n");
2191 return;
2193 spin_lock_irqsave(&fcport->rport_lock, flags);
2194 fcport->rport = rport;
2195 *((fc_port_t **)rport->dd_data) = fcport;
2196 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2198 rport->supported_classes = fcport->supported_classes;
2200 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2201 if (fcport->port_type == FCT_INITIATOR)
2202 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2203 if (fcport->port_type == FCT_TARGET)
2204 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2205 fc_remote_port_rolechg(rport, rport_ids.roles);
2207 if (rport->scsi_target_id != -1 &&
2208 rport->scsi_target_id < ha->host->max_id)
2209 fcport->os_target_id = rport->scsi_target_id;
2213 * qla2x00_update_fcport
2214 * Updates device on list.
2216 * Input:
2217 * ha = adapter block pointer.
2218 * fcport = port structure pointer.
2220 * Return:
2221 * 0 - Success
2222 * BIT_0 - error
2224 * Context:
2225 * Kernel context.
2227 void
2228 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2230 scsi_qla_host_t *pha = to_qla_parent(ha);
2232 fcport->ha = ha;
2233 fcport->login_retry = 0;
2234 fcport->port_login_retry_count = pha->port_down_retry_count *
2235 PORT_RETRY_TIME;
2236 atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
2237 PORT_RETRY_TIME);
2238 fcport->flags &= ~FCF_LOGIN_NEEDED;
2240 qla2x00_iidma_fcport(ha, fcport);
2242 atomic_set(&fcport->state, FCS_ONLINE);
2244 qla2x00_reg_remote_port(ha, fcport);
2248 * qla2x00_configure_fabric
2249 * Setup SNS devices with loop ID's.
2251 * Input:
2252 * ha = adapter block pointer.
2254 * Returns:
2255 * 0 = success.
2256 * BIT_0 = error
2258 static int
2259 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2261 int rval, rval2;
2262 fc_port_t *fcport, *fcptemp;
2263 uint16_t next_loopid;
2264 uint16_t mb[MAILBOX_REGISTER_COUNT];
2265 uint16_t loop_id;
2266 LIST_HEAD(new_fcports);
2267 scsi_qla_host_t *pha = to_qla_parent(ha);
2269 /* If FL port exists, then SNS is present */
2270 if (IS_FWI2_CAPABLE(ha))
2271 loop_id = NPH_F_PORT;
2272 else
2273 loop_id = SNS_FL_PORT;
2274 rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
2275 if (rval != QLA_SUCCESS) {
2276 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2277 "Port\n", ha->host_no));
2279 ha->device_flags &= ~SWITCH_FOUND;
2280 return (QLA_SUCCESS);
2282 ha->device_flags |= SWITCH_FOUND;
2284 /* Mark devices that need re-synchronization. */
2285 rval2 = qla2x00_device_resync(ha);
2286 if (rval2 == QLA_RSCNS_HANDLED) {
2287 /* No point doing the scan, just continue. */
2288 return (QLA_SUCCESS);
2290 do {
2291 /* FDMI support. */
2292 if (ql2xfdmienable &&
2293 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2294 qla2x00_fdmi_register(ha);
2296 /* Ensure we are logged into the SNS. */
2297 if (IS_FWI2_CAPABLE(ha))
2298 loop_id = NPH_SNS;
2299 else
2300 loop_id = SIMPLE_NAME_SERVER;
2301 ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
2302 0xfc, mb, BIT_1 | BIT_0);
2303 if (mb[0] != MBS_COMMAND_COMPLETE) {
2304 DEBUG2(qla_printk(KERN_INFO, ha,
2305 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2306 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2307 mb[0], mb[1], mb[2], mb[6], mb[7]));
2308 return (QLA_SUCCESS);
2311 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2312 if (qla2x00_rft_id(ha)) {
2313 /* EMPTY */
2314 DEBUG2(printk("scsi(%ld): Register FC-4 "
2315 "TYPE failed.\n", ha->host_no));
2317 if (qla2x00_rff_id(ha)) {
2318 /* EMPTY */
2319 DEBUG2(printk("scsi(%ld): Register FC-4 "
2320 "Features failed.\n", ha->host_no));
2322 if (qla2x00_rnn_id(ha)) {
2323 /* EMPTY */
2324 DEBUG2(printk("scsi(%ld): Register Node Name "
2325 "failed.\n", ha->host_no));
2326 } else if (qla2x00_rsnn_nn(ha)) {
2327 /* EMPTY */
2328 DEBUG2(printk("scsi(%ld): Register Symbolic "
2329 "Node Name failed.\n", ha->host_no));
2333 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2334 if (rval != QLA_SUCCESS)
2335 break;
2338 * Logout all previous fabric devices marked lost, except
2339 * tape devices.
2341 list_for_each_entry(fcport, &pha->fcports, list) {
2342 if (fcport->vp_idx !=ha->vp_idx)
2343 continue;
2345 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2346 break;
2348 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2349 continue;
2351 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2352 qla2x00_mark_device_lost(ha, fcport,
2353 ql2xplogiabsentdevice, 0);
2354 if (fcport->loop_id != FC_NO_LOOP_ID &&
2355 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2356 fcport->port_type != FCT_INITIATOR &&
2357 fcport->port_type != FCT_BROADCAST) {
2358 ha->isp_ops->fabric_logout(ha,
2359 fcport->loop_id,
2360 fcport->d_id.b.domain,
2361 fcport->d_id.b.area,
2362 fcport->d_id.b.al_pa);
2363 fcport->loop_id = FC_NO_LOOP_ID;
2368 /* Starting free loop ID. */
2369 next_loopid = pha->min_external_loopid;
2372 * Scan through our port list and login entries that need to be
2373 * logged in.
2375 list_for_each_entry(fcport, &pha->fcports, list) {
2376 if (fcport->vp_idx != ha->vp_idx)
2377 continue;
2379 if (atomic_read(&ha->loop_down_timer) ||
2380 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2381 break;
2383 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2384 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2385 continue;
2387 if (fcport->loop_id == FC_NO_LOOP_ID) {
2388 fcport->loop_id = next_loopid;
2389 rval = qla2x00_find_new_loop_id(ha, fcport);
2390 if (rval != QLA_SUCCESS) {
2391 /* Ran out of IDs to use */
2392 break;
2395 /* Login and update database */
2396 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2399 /* Exit if out of loop IDs. */
2400 if (rval != QLA_SUCCESS) {
2401 break;
2405 * Login and add the new devices to our port list.
2407 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2408 if (atomic_read(&ha->loop_down_timer) ||
2409 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2410 break;
2412 /* Find a new loop ID to use. */
2413 fcport->loop_id = next_loopid;
2414 rval = qla2x00_find_new_loop_id(ha, fcport);
2415 if (rval != QLA_SUCCESS) {
2416 /* Ran out of IDs to use */
2417 break;
2420 /* Login and update database */
2421 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2423 if (ha->parent) {
2424 fcport->ha = ha;
2425 fcport->vp_idx = ha->vp_idx;
2426 list_add_tail(&fcport->vp_fcport,
2427 &ha->vp_fcports);
2428 list_move_tail(&fcport->list,
2429 &ha->parent->fcports);
2430 } else
2431 list_move_tail(&fcport->list, &ha->fcports);
2433 } while (0);
2435 /* Free all new device structures not processed. */
2436 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2437 list_del(&fcport->list);
2438 kfree(fcport);
2441 if (rval) {
2442 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2443 "rval=%d\n", ha->host_no, rval));
2446 return (rval);
2451 * qla2x00_find_all_fabric_devs
2453 * Input:
2454 * ha = adapter block pointer.
2455 * dev = database device entry pointer.
2457 * Returns:
2458 * 0 = success.
2460 * Context:
2461 * Kernel context.
2463 static int
2464 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2466 int rval;
2467 uint16_t loop_id;
2468 fc_port_t *fcport, *new_fcport, *fcptemp;
2469 int found;
2471 sw_info_t *swl;
2472 int swl_idx;
2473 int first_dev, last_dev;
2474 port_id_t wrap, nxt_d_id;
2475 int vp_index;
2476 int empty_vp_index;
2477 int found_vp;
2478 scsi_qla_host_t *vha;
2479 scsi_qla_host_t *pha = to_qla_parent(ha);
2481 rval = QLA_SUCCESS;
2483 /* Try GID_PT to get device list, else GAN. */
2484 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2485 if (swl == NULL) {
2486 /*EMPTY*/
2487 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2488 "on GA_NXT\n", ha->host_no));
2489 } else {
2490 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2491 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2492 kfree(swl);
2493 swl = NULL;
2494 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2495 kfree(swl);
2496 swl = NULL;
2497 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2498 kfree(swl);
2499 swl = NULL;
2500 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2501 qla2x00_gpsc(ha, swl);
2504 swl_idx = 0;
2506 /* Allocate temporary fcport for any new fcports discovered. */
2507 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2508 if (new_fcport == NULL) {
2509 kfree(swl);
2510 return (QLA_MEMORY_ALLOC_FAILED);
2512 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2513 new_fcport->vp_idx = ha->vp_idx;
2514 /* Set start port ID scan at adapter ID. */
2515 first_dev = 1;
2516 last_dev = 0;
2518 /* Starting free loop ID. */
2519 loop_id = pha->min_external_loopid;
2520 for (; loop_id <= ha->last_loop_id; loop_id++) {
2521 if (qla2x00_is_reserved_id(ha, loop_id))
2522 continue;
2524 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2525 break;
2527 if (swl != NULL) {
2528 if (last_dev) {
2529 wrap.b24 = new_fcport->d_id.b24;
2530 } else {
2531 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2532 memcpy(new_fcport->node_name,
2533 swl[swl_idx].node_name, WWN_SIZE);
2534 memcpy(new_fcport->port_name,
2535 swl[swl_idx].port_name, WWN_SIZE);
2536 memcpy(new_fcport->fabric_port_name,
2537 swl[swl_idx].fabric_port_name, WWN_SIZE);
2538 new_fcport->fp_speed = swl[swl_idx].fp_speed;
2540 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2541 last_dev = 1;
2543 swl_idx++;
2545 } else {
2546 /* Send GA_NXT to the switch */
2547 rval = qla2x00_ga_nxt(ha, new_fcport);
2548 if (rval != QLA_SUCCESS) {
2549 qla_printk(KERN_WARNING, ha,
2550 "SNS scan failed -- assuming zero-entry "
2551 "result...\n");
2552 list_for_each_entry_safe(fcport, fcptemp,
2553 new_fcports, list) {
2554 list_del(&fcport->list);
2555 kfree(fcport);
2557 rval = QLA_SUCCESS;
2558 break;
2562 /* If wrap on switch device list, exit. */
2563 if (first_dev) {
2564 wrap.b24 = new_fcport->d_id.b24;
2565 first_dev = 0;
2566 } else if (new_fcport->d_id.b24 == wrap.b24) {
2567 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2568 ha->host_no, new_fcport->d_id.b.domain,
2569 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2570 break;
2573 /* Bypass if same physical adapter. */
2574 if (new_fcport->d_id.b24 == pha->d_id.b24)
2575 continue;
2577 /* Bypass virtual ports of the same host. */
2578 if (pha->num_vhosts) {
2579 vp_index = find_next_bit(
2580 (unsigned long *)pha->vp_idx_map,
2581 MAX_MULTI_ID_FABRIC + 1, 1);
2583 for (;vp_index <= MAX_MULTI_ID_FABRIC;
2584 vp_index = find_next_bit(
2585 (unsigned long *)pha->vp_idx_map,
2586 MAX_MULTI_ID_FABRIC + 1, vp_index + 1)) {
2587 empty_vp_index = 1;
2588 found_vp = 0;
2589 list_for_each_entry(vha, &pha->vp_list,
2590 vp_list) {
2591 if (vp_index == vha->vp_idx) {
2592 empty_vp_index = 0;
2593 found_vp = 1;
2594 break;
2598 if (empty_vp_index)
2599 continue;
2601 if (found_vp &&
2602 new_fcport->d_id.b24 == vha->d_id.b24)
2603 break;
2605 if (vp_index <= MAX_MULTI_ID_FABRIC)
2606 continue;
2609 /* Bypass if same domain and area of adapter. */
2610 if (((new_fcport->d_id.b24 & 0xffff00) ==
2611 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2612 ISP_CFG_FL)
2613 continue;
2615 /* Bypass reserved domain fields. */
2616 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2617 continue;
2619 /* Locate matching device in database. */
2620 found = 0;
2621 list_for_each_entry(fcport, &pha->fcports, list) {
2622 if (new_fcport->vp_idx != fcport->vp_idx)
2623 continue;
2624 if (memcmp(new_fcport->port_name, fcport->port_name,
2625 WWN_SIZE))
2626 continue;
2628 found++;
2630 /* Update port state. */
2631 memcpy(fcport->fabric_port_name,
2632 new_fcport->fabric_port_name, WWN_SIZE);
2633 fcport->fp_speed = new_fcport->fp_speed;
2636 * If address the same and state FCS_ONLINE, nothing
2637 * changed.
2639 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2640 atomic_read(&fcport->state) == FCS_ONLINE) {
2641 break;
2645 * If device was not a fabric device before.
2647 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2648 fcport->d_id.b24 = new_fcport->d_id.b24;
2649 fcport->loop_id = FC_NO_LOOP_ID;
2650 fcport->flags |= (FCF_FABRIC_DEVICE |
2651 FCF_LOGIN_NEEDED);
2652 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2653 break;
2657 * Port ID changed or device was marked to be updated;
2658 * Log it out if still logged in and mark it for
2659 * relogin later.
2661 fcport->d_id.b24 = new_fcport->d_id.b24;
2662 fcport->flags |= FCF_LOGIN_NEEDED;
2663 if (fcport->loop_id != FC_NO_LOOP_ID &&
2664 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2665 fcport->port_type != FCT_INITIATOR &&
2666 fcport->port_type != FCT_BROADCAST) {
2667 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
2668 fcport->d_id.b.domain, fcport->d_id.b.area,
2669 fcport->d_id.b.al_pa);
2670 fcport->loop_id = FC_NO_LOOP_ID;
2673 break;
2676 if (found)
2677 continue;
2679 /* If device was not in our fcports list, then add it. */
2680 list_add_tail(&new_fcport->list, new_fcports);
2682 /* Allocate a new replacement fcport. */
2683 nxt_d_id.b24 = new_fcport->d_id.b24;
2684 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2685 if (new_fcport == NULL) {
2686 kfree(swl);
2687 return (QLA_MEMORY_ALLOC_FAILED);
2689 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2690 new_fcport->d_id.b24 = nxt_d_id.b24;
2691 new_fcport->vp_idx = ha->vp_idx;
2694 kfree(swl);
2695 kfree(new_fcport);
2697 if (!list_empty(new_fcports))
2698 ha->device_flags |= DFLG_FABRIC_DEVICES;
2700 return (rval);
2704 * qla2x00_find_new_loop_id
2705 * Scan through our port list and find a new usable loop ID.
2707 * Input:
2708 * ha: adapter state pointer.
2709 * dev: port structure pointer.
2711 * Returns:
2712 * qla2x00 local function return status code.
2714 * Context:
2715 * Kernel context.
2717 static int
2718 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2720 int rval;
2721 int found;
2722 fc_port_t *fcport;
2723 uint16_t first_loop_id;
2724 scsi_qla_host_t *pha = to_qla_parent(ha);
2726 rval = QLA_SUCCESS;
2728 /* Save starting loop ID. */
2729 first_loop_id = dev->loop_id;
2731 for (;;) {
2732 /* Skip loop ID if already used by adapter. */
2733 if (dev->loop_id == ha->loop_id) {
2734 dev->loop_id++;
2737 /* Skip reserved loop IDs. */
2738 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2739 dev->loop_id++;
2742 /* Reset loop ID if passed the end. */
2743 if (dev->loop_id > ha->last_loop_id) {
2744 /* first loop ID. */
2745 dev->loop_id = ha->min_external_loopid;
2748 /* Check for loop ID being already in use. */
2749 found = 0;
2750 fcport = NULL;
2751 list_for_each_entry(fcport, &pha->fcports, list) {
2752 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2753 /* ID possibly in use */
2754 found++;
2755 break;
2759 /* If not in use then it is free to use. */
2760 if (!found) {
2761 break;
2764 /* ID in use. Try next value. */
2765 dev->loop_id++;
2767 /* If wrap around. No free ID to use. */
2768 if (dev->loop_id == first_loop_id) {
2769 dev->loop_id = FC_NO_LOOP_ID;
2770 rval = QLA_FUNCTION_FAILED;
2771 break;
2775 return (rval);
2779 * qla2x00_device_resync
2780 * Marks devices in the database that needs resynchronization.
2782 * Input:
2783 * ha = adapter block pointer.
2785 * Context:
2786 * Kernel context.
2788 static int
2789 qla2x00_device_resync(scsi_qla_host_t *ha)
2791 int rval;
2792 uint32_t mask;
2793 fc_port_t *fcport;
2794 uint32_t rscn_entry;
2795 uint8_t rscn_out_iter;
2796 uint8_t format;
2797 port_id_t d_id;
2798 scsi_qla_host_t *pha = to_qla_parent(ha);
2800 rval = QLA_RSCNS_HANDLED;
2802 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2803 ha->flags.rscn_queue_overflow) {
2805 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2806 format = MSB(MSW(rscn_entry));
2807 d_id.b.domain = LSB(MSW(rscn_entry));
2808 d_id.b.area = MSB(LSW(rscn_entry));
2809 d_id.b.al_pa = LSB(LSW(rscn_entry));
2811 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2812 "[%02x/%02x%02x%02x].\n",
2813 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2814 d_id.b.area, d_id.b.al_pa));
2816 ha->rscn_out_ptr++;
2817 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2818 ha->rscn_out_ptr = 0;
2820 /* Skip duplicate entries. */
2821 for (rscn_out_iter = ha->rscn_out_ptr;
2822 !ha->flags.rscn_queue_overflow &&
2823 rscn_out_iter != ha->rscn_in_ptr;
2824 rscn_out_iter = (rscn_out_iter ==
2825 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2827 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2828 break;
2830 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2831 "entry found at [%d].\n", ha->host_no,
2832 rscn_out_iter));
2834 ha->rscn_out_ptr = rscn_out_iter;
2837 /* Queue overflow, set switch default case. */
2838 if (ha->flags.rscn_queue_overflow) {
2839 DEBUG(printk("scsi(%ld): device_resync: rscn "
2840 "overflow.\n", ha->host_no));
2842 format = 3;
2843 ha->flags.rscn_queue_overflow = 0;
2846 switch (format) {
2847 case 0:
2848 mask = 0xffffff;
2849 break;
2850 case 1:
2851 mask = 0xffff00;
2852 break;
2853 case 2:
2854 mask = 0xff0000;
2855 break;
2856 default:
2857 mask = 0x0;
2858 d_id.b24 = 0;
2859 ha->rscn_out_ptr = ha->rscn_in_ptr;
2860 break;
2863 rval = QLA_SUCCESS;
2865 list_for_each_entry(fcport, &pha->fcports, list) {
2866 if (fcport->vp_idx != ha->vp_idx)
2867 continue;
2869 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2870 (fcport->d_id.b24 & mask) != d_id.b24 ||
2871 fcport->port_type == FCT_BROADCAST)
2872 continue;
2874 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2875 if (format != 3 ||
2876 fcport->port_type != FCT_INITIATOR) {
2877 qla2x00_mark_device_lost(ha, fcport,
2878 0, 0);
2881 fcport->flags &= ~FCF_FARP_DONE;
2884 return (rval);
2888 * qla2x00_fabric_dev_login
2889 * Login fabric target device and update FC port database.
2891 * Input:
2892 * ha: adapter state pointer.
2893 * fcport: port structure list pointer.
2894 * next_loopid: contains value of a new loop ID that can be used
2895 * by the next login attempt.
2897 * Returns:
2898 * qla2x00 local function return status code.
2900 * Context:
2901 * Kernel context.
2903 static int
2904 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2905 uint16_t *next_loopid)
2907 int rval;
2908 int retry;
2909 uint8_t opts;
2911 rval = QLA_SUCCESS;
2912 retry = 0;
2914 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2915 if (rval == QLA_SUCCESS) {
2916 /* Send an ADISC to tape devices.*/
2917 opts = 0;
2918 if (fcport->flags & FCF_TAPE_PRESENT)
2919 opts |= BIT_1;
2920 rval = qla2x00_get_port_database(ha, fcport, opts);
2921 if (rval != QLA_SUCCESS) {
2922 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
2923 fcport->d_id.b.domain, fcport->d_id.b.area,
2924 fcport->d_id.b.al_pa);
2925 qla2x00_mark_device_lost(ha, fcport, 1, 0);
2926 } else {
2927 qla2x00_update_fcport(ha, fcport);
2931 return (rval);
2935 * qla2x00_fabric_login
2936 * Issue fabric login command.
2938 * Input:
2939 * ha = adapter block pointer.
2940 * device = pointer to FC device type structure.
2942 * Returns:
2943 * 0 - Login successfully
2944 * 1 - Login failed
2945 * 2 - Initiator device
2946 * 3 - Fatal error
2949 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2950 uint16_t *next_loopid)
2952 int rval;
2953 int retry;
2954 uint16_t tmp_loopid;
2955 uint16_t mb[MAILBOX_REGISTER_COUNT];
2957 retry = 0;
2958 tmp_loopid = 0;
2960 for (;;) {
2961 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2962 "for port %02x%02x%02x.\n",
2963 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2964 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2966 /* Login fcport on switch. */
2967 ha->isp_ops->fabric_login(ha, fcport->loop_id,
2968 fcport->d_id.b.domain, fcport->d_id.b.area,
2969 fcport->d_id.b.al_pa, mb, BIT_0);
2970 if (mb[0] == MBS_PORT_ID_USED) {
2972 * Device has another loop ID. The firmware team
2973 * recommends the driver perform an implicit login with
2974 * the specified ID again. The ID we just used is save
2975 * here so we return with an ID that can be tried by
2976 * the next login.
2978 retry++;
2979 tmp_loopid = fcport->loop_id;
2980 fcport->loop_id = mb[1];
2982 DEBUG(printk("Fabric Login: port in use - next "
2983 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2984 fcport->loop_id, fcport->d_id.b.domain,
2985 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2987 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2989 * Login succeeded.
2991 if (retry) {
2992 /* A retry occurred before. */
2993 *next_loopid = tmp_loopid;
2994 } else {
2996 * No retry occurred before. Just increment the
2997 * ID value for next login.
2999 *next_loopid = (fcport->loop_id + 1);
3002 if (mb[1] & BIT_0) {
3003 fcport->port_type = FCT_INITIATOR;
3004 } else {
3005 fcport->port_type = FCT_TARGET;
3006 if (mb[1] & BIT_1) {
3007 fcport->flags |= FCF_TAPE_PRESENT;
3011 if (mb[10] & BIT_0)
3012 fcport->supported_classes |= FC_COS_CLASS2;
3013 if (mb[10] & BIT_1)
3014 fcport->supported_classes |= FC_COS_CLASS3;
3016 rval = QLA_SUCCESS;
3017 break;
3018 } else if (mb[0] == MBS_LOOP_ID_USED) {
3020 * Loop ID already used, try next loop ID.
3022 fcport->loop_id++;
3023 rval = qla2x00_find_new_loop_id(ha, fcport);
3024 if (rval != QLA_SUCCESS) {
3025 /* Ran out of loop IDs to use */
3026 break;
3028 } else if (mb[0] == MBS_COMMAND_ERROR) {
3030 * Firmware possibly timed out during login. If NO
3031 * retries are left to do then the device is declared
3032 * dead.
3034 *next_loopid = fcport->loop_id;
3035 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
3036 fcport->d_id.b.domain, fcport->d_id.b.area,
3037 fcport->d_id.b.al_pa);
3038 qla2x00_mark_device_lost(ha, fcport, 1, 0);
3040 rval = 1;
3041 break;
3042 } else {
3044 * unrecoverable / not handled error
3046 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
3047 "loop_id=%x jiffies=%lx.\n",
3048 __func__, ha->host_no, mb[0],
3049 fcport->d_id.b.domain, fcport->d_id.b.area,
3050 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3052 *next_loopid = fcport->loop_id;
3053 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
3054 fcport->d_id.b.domain, fcport->d_id.b.area,
3055 fcport->d_id.b.al_pa);
3056 fcport->loop_id = FC_NO_LOOP_ID;
3057 fcport->login_retry = 0;
3059 rval = 3;
3060 break;
3064 return (rval);
3068 * qla2x00_local_device_login
3069 * Issue local device login command.
3071 * Input:
3072 * ha = adapter block pointer.
3073 * loop_id = loop id of device to login to.
3075 * Returns (Where's the #define!!!!):
3076 * 0 - Login successfully
3077 * 1 - Login failed
3078 * 3 - Fatal error
3081 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
3083 int rval;
3084 uint16_t mb[MAILBOX_REGISTER_COUNT];
3086 memset(mb, 0, sizeof(mb));
3087 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
3088 if (rval == QLA_SUCCESS) {
3089 /* Interrogate mailbox registers for any errors */
3090 if (mb[0] == MBS_COMMAND_ERROR)
3091 rval = 1;
3092 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3093 /* device not in PCB table */
3094 rval = 3;
3097 return (rval);
3101 * qla2x00_loop_resync
3102 * Resync with fibre channel devices.
3104 * Input:
3105 * ha = adapter block pointer.
3107 * Returns:
3108 * 0 = success
3111 qla2x00_loop_resync(scsi_qla_host_t *ha)
3113 int rval;
3114 uint32_t wait_time;
3116 rval = QLA_SUCCESS;
3118 atomic_set(&ha->loop_state, LOOP_UPDATE);
3119 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3120 if (ha->flags.online) {
3121 if (!(rval = qla2x00_fw_ready(ha))) {
3122 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3123 wait_time = 256;
3124 do {
3125 atomic_set(&ha->loop_state, LOOP_UPDATE);
3127 /* Issue a marker after FW becomes ready. */
3128 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3129 ha->marker_needed = 0;
3131 /* Remap devices on Loop. */
3132 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3134 qla2x00_configure_loop(ha);
3135 wait_time--;
3136 } while (!atomic_read(&ha->loop_down_timer) &&
3137 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3138 wait_time &&
3139 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3143 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3144 return (QLA_FUNCTION_FAILED);
3147 if (rval) {
3148 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3151 return (rval);
3154 void
3155 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3157 int rescan_done;
3158 fc_port_t *fcport;
3160 rescan_done = 0;
3161 list_for_each_entry(fcport, &ha->fcports, list) {
3162 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3163 continue;
3165 qla2x00_update_fcport(ha, fcport);
3166 fcport->flags &= ~FCF_RESCAN_NEEDED;
3168 rescan_done = 1;
3170 qla2x00_probe_for_all_luns(ha);
3173 void
3174 qla2x00_update_fcports(scsi_qla_host_t *ha)
3176 fc_port_t *fcport;
3178 /* Go with deferred removal of rport references. */
3179 list_for_each_entry(fcport, &ha->fcports, list)
3180 if (fcport->drport)
3181 qla2x00_rport_del(fcport);
3185 * qla2x00_abort_isp
3186 * Resets ISP and aborts all outstanding commands.
3188 * Input:
3189 * ha = adapter block pointer.
3191 * Returns:
3192 * 0 = success
3195 qla2x00_abort_isp(scsi_qla_host_t *ha)
3197 int rval;
3198 unsigned long flags = 0;
3199 uint16_t cnt;
3200 srb_t *sp;
3201 uint8_t status = 0;
3203 if (ha->flags.online) {
3204 ha->flags.online = 0;
3205 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3207 qla_printk(KERN_INFO, ha,
3208 "Performing ISP error recovery - ha= %p.\n", ha);
3209 ha->isp_ops->reset_chip(ha);
3211 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3212 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3213 atomic_set(&ha->loop_state, LOOP_DOWN);
3214 qla2x00_mark_all_devices_lost(ha, 0);
3215 } else {
3216 if (!atomic_read(&ha->loop_down_timer))
3217 atomic_set(&ha->loop_down_timer,
3218 LOOP_DOWN_TIME);
3221 spin_lock_irqsave(&ha->hardware_lock, flags);
3222 /* Requeue all commands in outstanding command list. */
3223 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3224 sp = ha->outstanding_cmds[cnt];
3225 if (sp) {
3226 ha->outstanding_cmds[cnt] = NULL;
3227 sp->flags = 0;
3228 sp->cmd->result = DID_RESET << 16;
3229 sp->cmd->host_scribble = (unsigned char *)NULL;
3230 qla2x00_sp_compl(ha, sp);
3233 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3235 ha->isp_ops->get_flash_version(ha, ha->request_ring);
3237 ha->isp_ops->nvram_config(ha);
3239 if (!qla2x00_restart_isp(ha)) {
3240 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3242 if (!atomic_read(&ha->loop_down_timer)) {
3244 * Issue marker command only when we are going
3245 * to start the I/O .
3247 ha->marker_needed = 1;
3250 ha->flags.online = 1;
3252 ha->isp_ops->enable_intrs(ha);
3254 ha->isp_abort_cnt = 0;
3255 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3257 if (ha->eft) {
3258 rval = qla2x00_trace_control(ha, TC_ENABLE,
3259 ha->eft_dma, EFT_NUM_BUFFERS);
3260 if (rval) {
3261 qla_printk(KERN_WARNING, ha,
3262 "Unable to reinitialize EFT "
3263 "(%d).\n", rval);
3266 } else { /* failed the ISP abort */
3267 ha->flags.online = 1;
3268 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3269 if (ha->isp_abort_cnt == 0) {
3270 qla_printk(KERN_WARNING, ha,
3271 "ISP error recovery failed - "
3272 "board disabled\n");
3274 * The next call disables the board
3275 * completely.
3277 ha->isp_ops->reset_adapter(ha);
3278 ha->flags.online = 0;
3279 clear_bit(ISP_ABORT_RETRY,
3280 &ha->dpc_flags);
3281 status = 0;
3282 } else { /* schedule another ISP abort */
3283 ha->isp_abort_cnt--;
3284 DEBUG(printk("qla%ld: ISP abort - "
3285 "retry remaining %d\n",
3286 ha->host_no, ha->isp_abort_cnt));
3287 status = 1;
3289 } else {
3290 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3291 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3292 "- retrying (%d) more times\n",
3293 ha->host_no, ha->isp_abort_cnt));
3294 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3295 status = 1;
3301 if (status) {
3302 qla_printk(KERN_INFO, ha,
3303 "qla2x00_abort_isp: **** FAILED ****\n");
3304 } else {
3305 DEBUG(printk(KERN_INFO
3306 "qla2x00_abort_isp(%ld): exiting.\n",
3307 ha->host_no));
3310 return(status);
3314 * qla2x00_restart_isp
3315 * restarts the ISP after a reset
3317 * Input:
3318 * ha = adapter block pointer.
3320 * Returns:
3321 * 0 = success
3323 static int
3324 qla2x00_restart_isp(scsi_qla_host_t *ha)
3326 uint8_t status = 0;
3327 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3328 unsigned long flags = 0;
3329 uint32_t wait_time;
3331 /* If firmware needs to be loaded */
3332 if (qla2x00_isp_firmware(ha)) {
3333 ha->flags.online = 0;
3334 if (!(status = ha->isp_ops->chip_diag(ha))) {
3335 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3336 status = qla2x00_setup_chip(ha);
3337 goto done;
3340 spin_lock_irqsave(&ha->hardware_lock, flags);
3342 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3344 * Disable SRAM, Instruction RAM and GP RAM
3345 * parity.
3347 WRT_REG_WORD(&reg->hccr,
3348 (HCCR_ENABLE_PARITY + 0x0));
3349 RD_REG_WORD(&reg->hccr);
3352 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3354 status = qla2x00_setup_chip(ha);
3356 spin_lock_irqsave(&ha->hardware_lock, flags);
3358 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3359 /* Enable proper parity */
3360 if (IS_QLA2300(ha))
3361 /* SRAM parity */
3362 WRT_REG_WORD(&reg->hccr,
3363 (HCCR_ENABLE_PARITY + 0x1));
3364 else
3366 * SRAM, Instruction RAM and GP RAM
3367 * parity.
3369 WRT_REG_WORD(&reg->hccr,
3370 (HCCR_ENABLE_PARITY + 0x7));
3371 RD_REG_WORD(&reg->hccr);
3374 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3378 done:
3379 if (!status && !(status = qla2x00_init_rings(ha))) {
3380 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3381 if (!(status = qla2x00_fw_ready(ha))) {
3382 DEBUG(printk("%s(): Start configure loop, "
3383 "status = %d\n", __func__, status));
3385 /* Issue a marker after FW becomes ready. */
3386 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3388 ha->flags.online = 1;
3389 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3390 wait_time = 256;
3391 do {
3392 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3393 qla2x00_configure_loop(ha);
3394 wait_time--;
3395 } while (!atomic_read(&ha->loop_down_timer) &&
3396 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3397 wait_time &&
3398 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3401 /* if no cable then assume it's good */
3402 if ((ha->device_flags & DFLG_NO_CABLE))
3403 status = 0;
3405 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3406 __func__,
3407 status));
3409 return (status);
3413 * qla2x00_reset_adapter
3414 * Reset adapter.
3416 * Input:
3417 * ha = adapter block pointer.
3419 void
3420 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3422 unsigned long flags = 0;
3423 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3425 ha->flags.online = 0;
3426 ha->isp_ops->disable_intrs(ha);
3428 spin_lock_irqsave(&ha->hardware_lock, flags);
3429 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3430 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3431 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3432 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3433 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3436 void
3437 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3439 unsigned long flags = 0;
3440 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3442 ha->flags.online = 0;
3443 ha->isp_ops->disable_intrs(ha);
3445 spin_lock_irqsave(&ha->hardware_lock, flags);
3446 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3447 RD_REG_DWORD(&reg->hccr);
3448 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3449 RD_REG_DWORD(&reg->hccr);
3450 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3453 /* On sparc systems, obtain port and node WWN from firmware
3454 * properties.
3456 static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
3458 #ifdef CONFIG_SPARC
3459 struct pci_dev *pdev = ha->pdev;
3460 struct device_node *dp = pci_device_to_OF_node(pdev);
3461 const u8 *val;
3462 int len;
3464 val = of_get_property(dp, "port-wwn", &len);
3465 if (val && len >= WWN_SIZE)
3466 memcpy(nv->port_name, val, WWN_SIZE);
3468 val = of_get_property(dp, "node-wwn", &len);
3469 if (val && len >= WWN_SIZE)
3470 memcpy(nv->node_name, val, WWN_SIZE);
3471 #endif
3475 qla24xx_nvram_config(scsi_qla_host_t *ha)
3477 int rval;
3478 struct init_cb_24xx *icb;
3479 struct nvram_24xx *nv;
3480 uint32_t *dptr;
3481 uint8_t *dptr1, *dptr2;
3482 uint32_t chksum;
3483 uint16_t cnt;
3485 rval = QLA_SUCCESS;
3486 icb = (struct init_cb_24xx *)ha->init_cb;
3487 nv = (struct nvram_24xx *)ha->request_ring;
3489 /* Determine NVRAM starting address. */
3490 ha->nvram_size = sizeof(struct nvram_24xx);
3491 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3492 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3493 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3494 if (PCI_FUNC(ha->pdev->devfn)) {
3495 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3496 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3499 /* Get NVRAM data and calculate checksum. */
3500 dptr = (uint32_t *)nv;
3501 ha->isp_ops->read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3502 ha->nvram_size);
3503 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3504 chksum += le32_to_cpu(*dptr++);
3506 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3507 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3508 ha->nvram_size));
3510 /* Bad NVRAM data, set defaults parameters. */
3511 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3512 || nv->id[3] != ' ' ||
3513 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3514 /* Reset NVRAM data. */
3515 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3516 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3517 le16_to_cpu(nv->nvram_version));
3518 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3519 "invalid -- WWPN) defaults.\n");
3522 * Set default initialization control block.
3524 memset(nv, 0, ha->nvram_size);
3525 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3526 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3527 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3528 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3529 nv->exchange_count = __constant_cpu_to_le16(0);
3530 nv->hard_address = __constant_cpu_to_le16(124);
3531 nv->port_name[0] = 0x21;
3532 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3533 nv->port_name[2] = 0x00;
3534 nv->port_name[3] = 0xe0;
3535 nv->port_name[4] = 0x8b;
3536 nv->port_name[5] = 0x1c;
3537 nv->port_name[6] = 0x55;
3538 nv->port_name[7] = 0x86;
3539 nv->node_name[0] = 0x20;
3540 nv->node_name[1] = 0x00;
3541 nv->node_name[2] = 0x00;
3542 nv->node_name[3] = 0xe0;
3543 nv->node_name[4] = 0x8b;
3544 nv->node_name[5] = 0x1c;
3545 nv->node_name[6] = 0x55;
3546 nv->node_name[7] = 0x86;
3547 qla24xx_nvram_wwn_from_ofw(ha, nv);
3548 nv->login_retry_count = __constant_cpu_to_le16(8);
3549 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3550 nv->login_timeout = __constant_cpu_to_le16(0);
3551 nv->firmware_options_1 =
3552 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3553 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3554 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3555 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3556 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3557 nv->efi_parameters = __constant_cpu_to_le32(0);
3558 nv->reset_delay = 5;
3559 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3560 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3561 nv->link_down_timeout = __constant_cpu_to_le16(30);
3563 rval = 1;
3566 /* Reset Initialization control block */
3567 memset(icb, 0, sizeof(struct init_cb_24xx));
3569 /* Copy 1st segment. */
3570 dptr1 = (uint8_t *)icb;
3571 dptr2 = (uint8_t *)&nv->version;
3572 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3573 while (cnt--)
3574 *dptr1++ = *dptr2++;
3576 icb->login_retry_count = nv->login_retry_count;
3577 icb->link_down_on_nos = nv->link_down_on_nos;
3579 /* Copy 2nd segment. */
3580 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3581 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3582 cnt = (uint8_t *)&icb->reserved_3 -
3583 (uint8_t *)&icb->interrupt_delay_timer;
3584 while (cnt--)
3585 *dptr1++ = *dptr2++;
3588 * Setup driver NVRAM options.
3590 qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3591 "QLA2462");
3593 /* Use alternate WWN? */
3594 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3595 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3596 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3599 /* Prepare nodename */
3600 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
3602 * Firmware will apply the following mask if the nodename was
3603 * not provided.
3605 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3606 icb->node_name[0] &= 0xF0;
3609 /* Set host adapter parameters. */
3610 ha->flags.disable_risc_code_load = 0;
3611 ha->flags.enable_lip_reset = 0;
3612 ha->flags.enable_lip_full_login =
3613 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3614 ha->flags.enable_target_reset =
3615 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
3616 ha->flags.enable_led_scheme = 0;
3617 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
3619 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3620 (BIT_6 | BIT_5 | BIT_4)) >> 4;
3622 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3623 sizeof(ha->fw_seriallink_options24));
3625 /* save HBA serial number */
3626 ha->serial0 = icb->port_name[5];
3627 ha->serial1 = icb->port_name[6];
3628 ha->serial2 = icb->port_name[7];
3629 ha->node_name = icb->node_name;
3630 ha->port_name = icb->port_name;
3632 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3634 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3636 /* Set minimum login_timeout to 4 seconds. */
3637 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3638 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3639 if (le16_to_cpu(nv->login_timeout) < 4)
3640 nv->login_timeout = __constant_cpu_to_le16(4);
3641 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3642 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3644 /* Set minimum RATOV to 200 tenths of a second. */
3645 ha->r_a_tov = 200;
3647 ha->loop_reset_delay = nv->reset_delay;
3649 /* Link Down Timeout = 0:
3651 * When Port Down timer expires we will start returning
3652 * I/O's to OS with "DID_NO_CONNECT".
3654 * Link Down Timeout != 0:
3656 * The driver waits for the link to come up after link down
3657 * before returning I/Os to OS with "DID_NO_CONNECT".
3659 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3660 ha->loop_down_abort_time =
3661 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3662 } else {
3663 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3664 ha->loop_down_abort_time =
3665 (LOOP_DOWN_TIME - ha->link_down_timeout);
3668 /* Need enough time to try and get the port back. */
3669 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3670 if (qlport_down_retry)
3671 ha->port_down_retry_count = qlport_down_retry;
3673 /* Set login_retry_count */
3674 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3675 if (ha->port_down_retry_count ==
3676 le16_to_cpu(nv->port_down_retry_count) &&
3677 ha->port_down_retry_count > 3)
3678 ha->login_retry_count = ha->port_down_retry_count;
3679 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3680 ha->login_retry_count = ha->port_down_retry_count;
3681 if (ql2xloginretrycount)
3682 ha->login_retry_count = ql2xloginretrycount;
3684 /* Enable ZIO. */
3685 if (!ha->flags.init_done) {
3686 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3687 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3688 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3689 le16_to_cpu(icb->interrupt_delay_timer): 2;
3691 icb->firmware_options_2 &= __constant_cpu_to_le32(
3692 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3693 ha->flags.process_response_queue = 0;
3694 if (ha->zio_mode != QLA_ZIO_DISABLED) {
3695 ha->zio_mode = QLA_ZIO_MODE_6;
3697 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3698 "(%d us).\n", ha->host_no, ha->zio_mode,
3699 ha->zio_timer * 100));
3700 qla_printk(KERN_INFO, ha,
3701 "ZIO mode %d enabled; timer delay (%d us).\n",
3702 ha->zio_mode, ha->zio_timer * 100);
3704 icb->firmware_options_2 |= cpu_to_le32(
3705 (uint32_t)ha->zio_mode);
3706 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3707 ha->flags.process_response_queue = 1;
3710 if (rval) {
3711 DEBUG2_3(printk(KERN_WARNING
3712 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3714 return (rval);
3717 static int
3718 qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3720 int rval;
3721 int segments, fragment;
3722 uint32_t faddr;
3723 uint32_t *dcode, dlen;
3724 uint32_t risc_addr;
3725 uint32_t risc_size;
3726 uint32_t i;
3728 rval = QLA_SUCCESS;
3730 segments = FA_RISC_CODE_SEGMENTS;
3731 faddr = FA_RISC_CODE_ADDR;
3732 dcode = (uint32_t *)ha->request_ring;
3733 *srisc_addr = 0;
3735 /* Validate firmware image by checking version. */
3736 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3737 for (i = 0; i < 4; i++)
3738 dcode[i] = be32_to_cpu(dcode[i]);
3739 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3740 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3741 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3742 dcode[3] == 0)) {
3743 qla_printk(KERN_WARNING, ha,
3744 "Unable to verify integrity of flash firmware image!\n");
3745 qla_printk(KERN_WARNING, ha,
3746 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3747 dcode[1], dcode[2], dcode[3]);
3749 return QLA_FUNCTION_FAILED;
3752 while (segments && rval == QLA_SUCCESS) {
3753 /* Read segment's load information. */
3754 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3756 risc_addr = be32_to_cpu(dcode[2]);
3757 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3758 risc_size = be32_to_cpu(dcode[3]);
3760 fragment = 0;
3761 while (risc_size > 0 && rval == QLA_SUCCESS) {
3762 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3763 if (dlen > risc_size)
3764 dlen = risc_size;
3766 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3767 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3768 ha->host_no, risc_addr, dlen, faddr));
3770 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3771 for (i = 0; i < dlen; i++)
3772 dcode[i] = swab32(dcode[i]);
3774 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3775 dlen);
3776 if (rval) {
3777 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3778 "segment %d of firmware\n", ha->host_no,
3779 fragment));
3780 qla_printk(KERN_WARNING, ha,
3781 "[ERROR] Failed to load segment %d of "
3782 "firmware\n", fragment);
3783 break;
3786 faddr += dlen;
3787 risc_addr += dlen;
3788 risc_size -= dlen;
3789 fragment++;
3792 /* Next segment. */
3793 segments--;
3796 return rval;
3799 #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3802 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3804 int rval;
3805 int i, fragment;
3806 uint16_t *wcode, *fwcode;
3807 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3808 struct fw_blob *blob;
3810 /* Load firmware blob. */
3811 blob = qla2x00_request_firmware(ha);
3812 if (!blob) {
3813 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3814 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3815 "from: " QLA_FW_URL ".\n");
3816 return QLA_FUNCTION_FAILED;
3819 rval = QLA_SUCCESS;
3821 wcode = (uint16_t *)ha->request_ring;
3822 *srisc_addr = 0;
3823 fwcode = (uint16_t *)blob->fw->data;
3824 fwclen = 0;
3826 /* Validate firmware image by checking version. */
3827 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3828 qla_printk(KERN_WARNING, ha,
3829 "Unable to verify integrity of firmware image (%Zd)!\n",
3830 blob->fw->size);
3831 goto fail_fw_integrity;
3833 for (i = 0; i < 4; i++)
3834 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3835 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3836 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3837 wcode[2] == 0 && wcode[3] == 0)) {
3838 qla_printk(KERN_WARNING, ha,
3839 "Unable to verify integrity of firmware image!\n");
3840 qla_printk(KERN_WARNING, ha,
3841 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3842 wcode[1], wcode[2], wcode[3]);
3843 goto fail_fw_integrity;
3846 seg = blob->segs;
3847 while (*seg && rval == QLA_SUCCESS) {
3848 risc_addr = *seg;
3849 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3850 risc_size = be16_to_cpu(fwcode[3]);
3852 /* Validate firmware image size. */
3853 fwclen += risc_size * sizeof(uint16_t);
3854 if (blob->fw->size < fwclen) {
3855 qla_printk(KERN_WARNING, ha,
3856 "Unable to verify integrity of firmware image "
3857 "(%Zd)!\n", blob->fw->size);
3858 goto fail_fw_integrity;
3861 fragment = 0;
3862 while (risc_size > 0 && rval == QLA_SUCCESS) {
3863 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3864 if (wlen > risc_size)
3865 wlen = risc_size;
3867 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3868 "addr %x, number of words 0x%x.\n", ha->host_no,
3869 risc_addr, wlen));
3871 for (i = 0; i < wlen; i++)
3872 wcode[i] = swab16(fwcode[i]);
3874 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3875 wlen);
3876 if (rval) {
3877 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3878 "segment %d of firmware\n", ha->host_no,
3879 fragment));
3880 qla_printk(KERN_WARNING, ha,
3881 "[ERROR] Failed to load segment %d of "
3882 "firmware\n", fragment);
3883 break;
3886 fwcode += wlen;
3887 risc_addr += wlen;
3888 risc_size -= wlen;
3889 fragment++;
3892 /* Next segment. */
3893 seg++;
3895 return rval;
3897 fail_fw_integrity:
3898 return QLA_FUNCTION_FAILED;
3902 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3904 int rval;
3905 int segments, fragment;
3906 uint32_t *dcode, dlen;
3907 uint32_t risc_addr;
3908 uint32_t risc_size;
3909 uint32_t i;
3910 struct fw_blob *blob;
3911 uint32_t *fwcode, fwclen;
3913 /* Load firmware blob. */
3914 blob = qla2x00_request_firmware(ha);
3915 if (!blob) {
3916 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3917 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3918 "from: " QLA_FW_URL ".\n");
3920 /* Try to load RISC code from flash. */
3921 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3922 "outdated) firmware from flash.\n");
3923 return qla24xx_load_risc_flash(ha, srisc_addr);
3926 rval = QLA_SUCCESS;
3928 segments = FA_RISC_CODE_SEGMENTS;
3929 dcode = (uint32_t *)ha->request_ring;
3930 *srisc_addr = 0;
3931 fwcode = (uint32_t *)blob->fw->data;
3932 fwclen = 0;
3934 /* Validate firmware image by checking version. */
3935 if (blob->fw->size < 8 * sizeof(uint32_t)) {
3936 qla_printk(KERN_WARNING, ha,
3937 "Unable to verify integrity of firmware image (%Zd)!\n",
3938 blob->fw->size);
3939 goto fail_fw_integrity;
3941 for (i = 0; i < 4; i++)
3942 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3943 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3944 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3945 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3946 dcode[3] == 0)) {
3947 qla_printk(KERN_WARNING, ha,
3948 "Unable to verify integrity of firmware image!\n");
3949 qla_printk(KERN_WARNING, ha,
3950 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3951 dcode[1], dcode[2], dcode[3]);
3952 goto fail_fw_integrity;
3955 while (segments && rval == QLA_SUCCESS) {
3956 risc_addr = be32_to_cpu(fwcode[2]);
3957 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3958 risc_size = be32_to_cpu(fwcode[3]);
3960 /* Validate firmware image size. */
3961 fwclen += risc_size * sizeof(uint32_t);
3962 if (blob->fw->size < fwclen) {
3963 qla_printk(KERN_WARNING, ha,
3964 "Unable to verify integrity of firmware image "
3965 "(%Zd)!\n", blob->fw->size);
3967 goto fail_fw_integrity;
3970 fragment = 0;
3971 while (risc_size > 0 && rval == QLA_SUCCESS) {
3972 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3973 if (dlen > risc_size)
3974 dlen = risc_size;
3976 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3977 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3978 risc_addr, dlen));
3980 for (i = 0; i < dlen; i++)
3981 dcode[i] = swab32(fwcode[i]);
3983 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3984 dlen);
3985 if (rval) {
3986 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3987 "segment %d of firmware\n", ha->host_no,
3988 fragment));
3989 qla_printk(KERN_WARNING, ha,
3990 "[ERROR] Failed to load segment %d of "
3991 "firmware\n", fragment);
3992 break;
3995 fwcode += dlen;
3996 risc_addr += dlen;
3997 risc_size -= dlen;
3998 fragment++;
4001 /* Next segment. */
4002 segments--;
4004 return rval;
4006 fail_fw_integrity:
4007 return QLA_FUNCTION_FAILED;
4010 void
4011 qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
4013 int ret, retries;
4015 if (!IS_FWI2_CAPABLE(ha))
4016 return;
4017 if (!ha->fw_major_version)
4018 return;
4020 ret = qla2x00_stop_firmware(ha);
4021 for (retries = 5; ret != QLA_SUCCESS && retries ; retries--) {
4022 qla2x00_reset_chip(ha);
4023 if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
4024 continue;
4025 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
4026 continue;
4027 qla_printk(KERN_INFO, ha,
4028 "Attempting retry of stop-firmware command...\n");
4029 ret = qla2x00_stop_firmware(ha);
4034 qla24xx_configure_vhba(scsi_qla_host_t *ha)
4036 int rval = QLA_SUCCESS;
4037 uint16_t mb[MAILBOX_REGISTER_COUNT];
4039 if (!ha->parent)
4040 return -EINVAL;
4042 rval = qla2x00_fw_ready(ha);
4043 if (rval == QLA_SUCCESS) {
4044 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
4045 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
4048 ha->flags.management_server_logged_in = 0;
4050 /* Login to SNS first */
4051 qla24xx_login_fabric(ha, NPH_SNS, 0xff, 0xff, 0xfc,
4052 mb, BIT_1);
4053 if (mb[0] != MBS_COMMAND_COMPLETE) {
4054 DEBUG15(qla_printk(KERN_INFO, ha,
4055 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4056 "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4057 mb[0], mb[1], mb[2], mb[6], mb[7]));
4058 return (QLA_FUNCTION_FAILED);
4061 atomic_set(&ha->loop_down_timer, 0);
4062 atomic_set(&ha->loop_state, LOOP_UP);
4063 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
4064 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
4065 rval = qla2x00_loop_resync(ha);
4067 return rval;