[SCSI] fix oops in scsi_release_buffers()
[linux-2.6/suspend2-2.6.18.git] / drivers / scsi / qla2xxx / qla_init.c
blob3e9b64137873574acd5144a35097ade5eaa84808
1 /*
2 * QLOGIC LINUX SOFTWARE
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2005 QLogic Corporation
6 * (www.qlogic.com)
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include "qla_def.h"
21 #include <linux/delay.h>
22 #include <linux/vmalloc.h>
23 #include <linux/firmware.h>
24 #include <scsi/scsi_transport_fc.h>
26 #include "qla_devtbl.h"
28 /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
29 #ifndef EXT_IS_LUN_BIT_SET
30 #define EXT_IS_LUN_BIT_SET(P,L) \
31 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
32 #define EXT_SET_LUN_BIT(P,L) \
33 ((P)->mask[L/8] |= (0x80 >> (L%8)))
34 #endif
37 * QLogic ISP2x00 Hardware Support Function Prototypes.
39 static int qla2x00_isp_firmware(scsi_qla_host_t *);
40 static void qla2x00_resize_request_q(scsi_qla_host_t *);
41 static int qla2x00_setup_chip(scsi_qla_host_t *);
42 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
43 static int qla2x00_init_rings(scsi_qla_host_t *);
44 static int qla2x00_fw_ready(scsi_qla_host_t *);
45 static int qla2x00_configure_hba(scsi_qla_host_t *);
46 static int qla2x00_configure_loop(scsi_qla_host_t *);
47 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
48 static void qla2x00_update_fcport(scsi_qla_host_t *, fc_port_t *);
49 static int qla2x00_configure_fabric(scsi_qla_host_t *);
50 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
51 static int qla2x00_device_resync(scsi_qla_host_t *);
52 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
53 uint16_t *);
55 static int qla2x00_restart_isp(scsi_qla_host_t *);
57 /****************************************************************************/
58 /* QLogic ISP2x00 Hardware Support Functions. */
59 /****************************************************************************/
62 * qla2x00_initialize_adapter
63 * Initialize board.
65 * Input:
66 * ha = adapter block pointer.
68 * Returns:
69 * 0 = success
71 int
72 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
74 int rval;
75 uint8_t restart_risc = 0;
76 uint8_t retry;
77 uint32_t wait_time;
79 /* Clear adapter flags. */
80 ha->flags.online = 0;
81 ha->flags.reset_active = 0;
82 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
83 atomic_set(&ha->loop_state, LOOP_DOWN);
84 ha->device_flags = 0;
85 ha->dpc_flags = 0;
86 ha->flags.management_server_logged_in = 0;
87 ha->marker_needed = 0;
88 ha->mbx_flags = 0;
89 ha->isp_abort_cnt = 0;
90 ha->beacon_blink_led = 0;
91 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
93 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
94 rval = ha->isp_ops.pci_config(ha);
95 if (rval) {
96 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
97 ha->host_no));
98 return (rval);
101 ha->isp_ops.reset_chip(ha);
103 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
105 ha->isp_ops.nvram_config(ha);
107 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
109 retry = 10;
111 * Try to configure the loop.
113 do {
114 restart_risc = 0;
116 /* If firmware needs to be loaded */
117 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
118 if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
119 rval = qla2x00_setup_chip(ha);
123 if (rval == QLA_SUCCESS &&
124 (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
125 check_fw_ready_again:
127 * Wait for a successful LIP up to a maximum
128 * of (in seconds): RISC login timeout value,
129 * RISC retry count value, and port down retry
130 * value OR a minimum of 4 seconds OR If no
131 * cable, only 5 seconds.
133 rval = qla2x00_fw_ready(ha);
134 if (rval == QLA_SUCCESS) {
135 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
137 /* Issue a marker after FW becomes ready. */
138 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
141 * Wait at most MAX_TARGET RSCNs for a stable
142 * link.
144 wait_time = 256;
145 do {
146 clear_bit(LOOP_RESYNC_NEEDED,
147 &ha->dpc_flags);
148 rval = qla2x00_configure_loop(ha);
150 if (test_and_clear_bit(ISP_ABORT_NEEDED,
151 &ha->dpc_flags)) {
152 restart_risc = 1;
153 break;
157 * If loop state change while we were
158 * discoverying devices then wait for
159 * LIP to complete
162 if (atomic_read(&ha->loop_state) ==
163 LOOP_DOWN && retry--) {
164 goto check_fw_ready_again;
166 wait_time--;
167 } while (!atomic_read(&ha->loop_down_timer) &&
168 retry &&
169 wait_time &&
170 (test_bit(LOOP_RESYNC_NEEDED,
171 &ha->dpc_flags)));
173 if (wait_time == 0)
174 rval = QLA_FUNCTION_FAILED;
175 } else if (ha->device_flags & DFLG_NO_CABLE)
176 /* If no cable, then all is good. */
177 rval = QLA_SUCCESS;
179 } while (restart_risc && retry--);
181 if (rval == QLA_SUCCESS) {
182 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
183 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
184 ha->marker_needed = 0;
186 ha->flags.online = 1;
187 } else {
188 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
191 return (rval);
195 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
196 * @ha: HA context
198 * Returns 0 on success.
201 qla2100_pci_config(scsi_qla_host_t *ha)
203 uint16_t w, mwi;
204 unsigned long flags;
205 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
207 pci_set_master(ha->pdev);
208 mwi = 0;
209 if (pci_set_mwi(ha->pdev))
210 mwi = PCI_COMMAND_INVALIDATE;
211 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
213 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
214 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
215 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
217 /* Reset expansion ROM address decode enable */
218 pci_read_config_word(ha->pdev, PCI_ROM_ADDRESS, &w);
219 w &= ~PCI_ROM_ADDRESS_ENABLE;
220 pci_write_config_word(ha->pdev, PCI_ROM_ADDRESS, w);
222 /* Get PCI bus information. */
223 spin_lock_irqsave(&ha->hardware_lock, flags);
224 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
225 spin_unlock_irqrestore(&ha->hardware_lock, flags);
227 return QLA_SUCCESS;
231 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
232 * @ha: HA context
234 * Returns 0 on success.
237 qla2300_pci_config(scsi_qla_host_t *ha)
239 uint16_t w, mwi;
240 unsigned long flags = 0;
241 uint32_t cnt;
242 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
244 pci_set_master(ha->pdev);
245 mwi = 0;
246 if (pci_set_mwi(ha->pdev))
247 mwi = PCI_COMMAND_INVALIDATE;
248 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
250 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
251 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
253 if (IS_QLA2322(ha) || IS_QLA6322(ha))
254 w &= ~PCI_COMMAND_INTX_DISABLE;
257 * If this is a 2300 card and not 2312, reset the
258 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
259 * the 2310 also reports itself as a 2300 so we need to get the
260 * fb revision level -- a 6 indicates it really is a 2300 and
261 * not a 2310.
263 if (IS_QLA2300(ha)) {
264 spin_lock_irqsave(&ha->hardware_lock, flags);
266 /* Pause RISC. */
267 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
268 for (cnt = 0; cnt < 30000; cnt++) {
269 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
270 break;
272 udelay(10);
275 /* Select FPM registers. */
276 WRT_REG_WORD(&reg->ctrl_status, 0x20);
277 RD_REG_WORD(&reg->ctrl_status);
279 /* Get the fb rev level */
280 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
282 if (ha->fb_rev == FPM_2300)
283 w &= ~PCI_COMMAND_INVALIDATE;
285 /* Deselect FPM registers. */
286 WRT_REG_WORD(&reg->ctrl_status, 0x0);
287 RD_REG_WORD(&reg->ctrl_status);
289 /* Release RISC module. */
290 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
291 for (cnt = 0; cnt < 30000; cnt++) {
292 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
293 break;
295 udelay(10);
298 spin_unlock_irqrestore(&ha->hardware_lock, flags);
300 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
302 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
304 /* Reset expansion ROM address decode enable */
305 pci_read_config_word(ha->pdev, PCI_ROM_ADDRESS, &w);
306 w &= ~PCI_ROM_ADDRESS_ENABLE;
307 pci_write_config_word(ha->pdev, PCI_ROM_ADDRESS, w);
309 /* Get PCI bus information. */
310 spin_lock_irqsave(&ha->hardware_lock, flags);
311 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
312 spin_unlock_irqrestore(&ha->hardware_lock, flags);
314 return QLA_SUCCESS;
318 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
319 * @ha: HA context
321 * Returns 0 on success.
324 qla24xx_pci_config(scsi_qla_host_t *ha)
326 uint16_t w, mwi;
327 unsigned long flags = 0;
328 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
329 int pcix_cmd_reg, pcie_dctl_reg;
331 pci_set_master(ha->pdev);
332 mwi = 0;
333 if (pci_set_mwi(ha->pdev))
334 mwi = PCI_COMMAND_INVALIDATE;
335 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
337 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
338 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
339 w &= ~PCI_COMMAND_INTX_DISABLE;
340 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
342 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
344 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
345 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
346 if (pcix_cmd_reg) {
347 uint16_t pcix_cmd;
349 pcix_cmd_reg += PCI_X_CMD;
350 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
351 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
352 pcix_cmd |= 0x0008;
353 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
356 /* PCIe -- adjust Maximum Read Request Size (2048). */
357 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
358 if (pcie_dctl_reg) {
359 uint16_t pcie_dctl;
361 pcie_dctl_reg += PCI_EXP_DEVCTL;
362 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
363 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
364 pcie_dctl |= 0x4000;
365 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
368 /* Reset expansion ROM address decode enable */
369 pci_read_config_word(ha->pdev, PCI_ROM_ADDRESS, &w);
370 w &= ~PCI_ROM_ADDRESS_ENABLE;
371 pci_write_config_word(ha->pdev, PCI_ROM_ADDRESS, w);
373 /* Get PCI bus information. */
374 spin_lock_irqsave(&ha->hardware_lock, flags);
375 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
376 spin_unlock_irqrestore(&ha->hardware_lock, flags);
378 return QLA_SUCCESS;
382 * qla2x00_isp_firmware() - Choose firmware image.
383 * @ha: HA context
385 * Returns 0 on success.
387 static int
388 qla2x00_isp_firmware(scsi_qla_host_t *ha)
390 int rval;
392 /* Assume loading risc code */
393 rval = QLA_FUNCTION_FAILED;
395 if (ha->flags.disable_risc_code_load) {
396 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
397 ha->host_no));
398 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
400 /* Verify checksum of loaded RISC code. */
401 rval = qla2x00_verify_checksum(ha,
402 IS_QLA24XX(ha) || IS_QLA25XX(ha) ? RISC_SADDRESS :
403 *ha->brd_info->fw_info[0].fwstart);
406 if (rval) {
407 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
408 ha->host_no));
411 return (rval);
415 * qla2x00_reset_chip() - Reset ISP chip.
416 * @ha: HA context
418 * Returns 0 on success.
420 void
421 qla2x00_reset_chip(scsi_qla_host_t *ha)
423 unsigned long flags = 0;
424 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
425 uint32_t cnt;
426 unsigned long mbx_flags = 0;
427 uint16_t cmd;
429 ha->isp_ops.disable_intrs(ha);
431 spin_lock_irqsave(&ha->hardware_lock, flags);
433 /* Turn off master enable */
434 cmd = 0;
435 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
436 cmd &= ~PCI_COMMAND_MASTER;
437 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
439 if (!IS_QLA2100(ha)) {
440 /* Pause RISC. */
441 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
442 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
443 for (cnt = 0; cnt < 30000; cnt++) {
444 if ((RD_REG_WORD(&reg->hccr) &
445 HCCR_RISC_PAUSE) != 0)
446 break;
447 udelay(100);
449 } else {
450 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
451 udelay(10);
454 /* Select FPM registers. */
455 WRT_REG_WORD(&reg->ctrl_status, 0x20);
456 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
458 /* FPM Soft Reset. */
459 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
460 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
462 /* Toggle Fpm Reset. */
463 if (!IS_QLA2200(ha)) {
464 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
465 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
468 /* Select frame buffer registers. */
469 WRT_REG_WORD(&reg->ctrl_status, 0x10);
470 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
472 /* Reset frame buffer FIFOs. */
473 if (IS_QLA2200(ha)) {
474 WRT_FB_CMD_REG(ha, reg, 0xa000);
475 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
476 } else {
477 WRT_FB_CMD_REG(ha, reg, 0x00fc);
479 /* Read back fb_cmd until zero or 3 seconds max */
480 for (cnt = 0; cnt < 3000; cnt++) {
481 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
482 break;
483 udelay(100);
487 /* Select RISC module registers. */
488 WRT_REG_WORD(&reg->ctrl_status, 0);
489 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
491 /* Reset RISC processor. */
492 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
493 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
495 /* Release RISC processor. */
496 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
497 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
500 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
501 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
503 /* Reset ISP chip. */
504 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
506 /* Wait for RISC to recover from reset. */
507 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
509 * It is necessary to for a delay here since the card doesn't
510 * respond to PCI reads during a reset. On some architectures
511 * this will result in an MCA.
513 udelay(20);
514 for (cnt = 30000; cnt; cnt--) {
515 if ((RD_REG_WORD(&reg->ctrl_status) &
516 CSR_ISP_SOFT_RESET) == 0)
517 break;
518 udelay(100);
520 } else
521 udelay(10);
523 /* Reset RISC processor. */
524 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
526 WRT_REG_WORD(&reg->semaphore, 0);
528 /* Release RISC processor. */
529 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
530 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
532 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
533 for (cnt = 0; cnt < 30000; cnt++) {
534 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
535 spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
537 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY) {
538 if (!(test_bit(ABORT_ISP_ACTIVE,
539 &ha->dpc_flags)))
540 spin_unlock_irqrestore(
541 &ha->mbx_reg_lock, mbx_flags);
542 break;
545 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
546 spin_unlock_irqrestore(&ha->mbx_reg_lock,
547 mbx_flags);
549 udelay(100);
551 } else
552 udelay(100);
554 /* Turn on master enable */
555 cmd |= PCI_COMMAND_MASTER;
556 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
558 /* Disable RISC pause on FPM parity error. */
559 if (!IS_QLA2100(ha)) {
560 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
561 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
564 spin_unlock_irqrestore(&ha->hardware_lock, flags);
568 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
569 * @ha: HA context
571 * Returns 0 on success.
573 static inline void
574 qla24xx_reset_risc(scsi_qla_host_t *ha)
576 unsigned long flags = 0;
577 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
578 uint32_t cnt, d2;
580 spin_lock_irqsave(&ha->hardware_lock, flags);
582 /* Reset RISC. */
583 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
584 for (cnt = 0; cnt < 30000; cnt++) {
585 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
586 break;
588 udelay(10);
591 WRT_REG_DWORD(&reg->ctrl_status,
592 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
593 RD_REG_DWORD(&reg->ctrl_status);
595 /* Wait for firmware to complete NVRAM accesses. */
596 udelay(5);
597 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
598 for (cnt = 10000 ; cnt && d2; cnt--) {
599 udelay(5);
600 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
601 barrier();
604 udelay(20);
605 d2 = RD_REG_DWORD(&reg->ctrl_status);
606 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
607 udelay(5);
608 d2 = RD_REG_DWORD(&reg->ctrl_status);
609 barrier();
612 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
613 RD_REG_DWORD(&reg->hccr);
615 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
616 RD_REG_DWORD(&reg->hccr);
618 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
619 RD_REG_DWORD(&reg->hccr);
621 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
622 for (cnt = 6000000 ; cnt && d2; cnt--) {
623 udelay(5);
624 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
625 barrier();
628 spin_unlock_irqrestore(&ha->hardware_lock, flags);
632 * qla24xx_reset_chip() - Reset ISP24xx chip.
633 * @ha: HA context
635 * Returns 0 on success.
637 void
638 qla24xx_reset_chip(scsi_qla_host_t *ha)
640 ha->isp_ops.disable_intrs(ha);
642 /* Perform RISC reset. */
643 qla24xx_reset_risc(ha);
647 * qla2x00_chip_diag() - Test chip for proper operation.
648 * @ha: HA context
650 * Returns 0 on success.
653 qla2x00_chip_diag(scsi_qla_host_t *ha)
655 int rval;
656 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
657 unsigned long flags = 0;
658 uint16_t data;
659 uint32_t cnt;
660 uint16_t mb[5];
662 /* Assume a failed state */
663 rval = QLA_FUNCTION_FAILED;
665 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
666 ha->host_no, (u_long)&reg->flash_address));
668 spin_lock_irqsave(&ha->hardware_lock, flags);
670 /* Reset ISP chip. */
671 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
674 * We need to have a delay here since the card will not respond while
675 * in reset causing an MCA on some architectures.
677 udelay(20);
678 data = qla2x00_debounce_register(&reg->ctrl_status);
679 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
680 udelay(5);
681 data = RD_REG_WORD(&reg->ctrl_status);
682 barrier();
685 if (!cnt)
686 goto chip_diag_failed;
688 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
689 ha->host_no));
691 /* Reset RISC processor. */
692 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
693 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
695 /* Workaround for QLA2312 PCI parity error */
696 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
697 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
698 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
699 udelay(5);
700 data = RD_MAILBOX_REG(ha, reg, 0);
701 barrier();
703 } else
704 udelay(10);
706 if (!cnt)
707 goto chip_diag_failed;
709 /* Check product ID of chip */
710 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
712 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
713 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
714 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
715 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
716 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
717 mb[3] != PROD_ID_3) {
718 qla_printk(KERN_WARNING, ha,
719 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
721 goto chip_diag_failed;
723 ha->product_id[0] = mb[1];
724 ha->product_id[1] = mb[2];
725 ha->product_id[2] = mb[3];
726 ha->product_id[3] = mb[4];
728 /* Adjust fw RISC transfer size */
729 if (ha->request_q_length > 1024)
730 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
731 else
732 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
733 ha->request_q_length;
735 if (IS_QLA2200(ha) &&
736 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
737 /* Limit firmware transfer size with a 2200A */
738 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
739 ha->host_no));
741 ha->fw_transfer_size = 128;
744 /* Wrap Incoming Mailboxes Test. */
745 spin_unlock_irqrestore(&ha->hardware_lock, flags);
747 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
748 rval = qla2x00_mbx_reg_test(ha);
749 if (rval) {
750 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
751 ha->host_no));
752 qla_printk(KERN_WARNING, ha,
753 "Failed mailbox send register test\n");
755 else {
756 /* Flag a successful rval */
757 rval = QLA_SUCCESS;
759 spin_lock_irqsave(&ha->hardware_lock, flags);
761 chip_diag_failed:
762 if (rval)
763 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
764 "****\n", ha->host_no));
766 spin_unlock_irqrestore(&ha->hardware_lock, flags);
768 return (rval);
772 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
773 * @ha: HA context
775 * Returns 0 on success.
778 qla24xx_chip_diag(scsi_qla_host_t *ha)
780 int rval;
782 /* Perform RISC reset. */
783 qla24xx_reset_risc(ha);
785 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
787 rval = qla2x00_mbx_reg_test(ha);
788 if (rval) {
789 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
790 ha->host_no));
791 qla_printk(KERN_WARNING, ha,
792 "Failed mailbox send register test\n");
793 } else {
794 /* Flag a successful rval */
795 rval = QLA_SUCCESS;
798 return rval;
801 static void
802 qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
804 ha->fw_dumped = 0;
805 ha->fw_dump24_len = sizeof(struct qla24xx_fw_dump);
806 ha->fw_dump24_len += (ha->fw_memory_size - 0x100000) * sizeof(uint32_t);
807 ha->fw_dump24 = vmalloc(ha->fw_dump24_len);
808 if (ha->fw_dump24)
809 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware "
810 "dump...\n", ha->fw_dump24_len / 1024);
811 else
812 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
813 "firmware dump!!!\n", ha->fw_dump24_len / 1024);
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 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
836 qla2x00_alloc_fw_dump(ha);
838 /* Retrieve IOCB counts available to the firmware. */
839 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
840 if (rval)
841 return;
842 /* No point in continuing if current settings are sufficient. */
843 if (fw_iocb_cnt < 1024)
844 return;
845 if (ha->request_q_length >= request_q_length)
846 return;
848 /* Attempt to claim larger area for request queue. */
849 request_ring = dma_alloc_coherent(&ha->pdev->dev,
850 (request_q_length + 1) * sizeof(request_t), &request_dma,
851 GFP_KERNEL);
852 if (request_ring == NULL)
853 return;
855 /* Resize successful, report extensions. */
856 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
857 (ha->fw_memory_size + 1) / 1024);
858 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
859 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
861 /* Clear old allocations. */
862 dma_free_coherent(&ha->pdev->dev,
863 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
864 ha->request_dma);
866 /* Begin using larger queue. */
867 ha->request_q_length = request_q_length;
868 ha->request_ring = request_ring;
869 ha->request_dma = request_dma;
873 * qla2x00_setup_chip() - Load and start RISC firmware.
874 * @ha: HA context
876 * Returns 0 on success.
878 static int
879 qla2x00_setup_chip(scsi_qla_host_t *ha)
881 int rval;
882 uint32_t srisc_address = 0;
884 /* Load firmware sequences */
885 rval = ha->isp_ops.load_risc(ha, &srisc_address);
886 if (rval == QLA_SUCCESS) {
887 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
888 "code.\n", ha->host_no));
890 rval = qla2x00_verify_checksum(ha, srisc_address);
891 if (rval == QLA_SUCCESS) {
892 /* Start firmware execution. */
893 DEBUG(printk("scsi(%ld): Checksum OK, start "
894 "firmware.\n", ha->host_no));
896 rval = qla2x00_execute_fw(ha, srisc_address);
897 /* Retrieve firmware information. */
898 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
899 qla2x00_get_fw_version(ha,
900 &ha->fw_major_version,
901 &ha->fw_minor_version,
902 &ha->fw_subminor_version,
903 &ha->fw_attributes, &ha->fw_memory_size);
904 qla2x00_resize_request_q(ha);
906 } else {
907 DEBUG2(printk(KERN_INFO
908 "scsi(%ld): ISP Firmware failed checksum.\n",
909 ha->host_no));
913 if (rval) {
914 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
915 ha->host_no));
918 return (rval);
922 * qla2x00_init_response_q_entries() - Initializes response queue entries.
923 * @ha: HA context
925 * Beginning of request ring has initialization control block already built
926 * by nvram config routine.
928 * Returns 0 on success.
930 static void
931 qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
933 uint16_t cnt;
934 response_t *pkt;
936 pkt = ha->response_ring_ptr;
937 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
938 pkt->signature = RESPONSE_PROCESSED;
939 pkt++;
945 * qla2x00_update_fw_options() - Read and process firmware options.
946 * @ha: HA context
948 * Returns 0 on success.
950 void
951 qla2x00_update_fw_options(scsi_qla_host_t *ha)
953 uint16_t swing, emphasis, tx_sens, rx_sens;
955 memset(ha->fw_options, 0, sizeof(ha->fw_options));
956 qla2x00_get_fw_options(ha, ha->fw_options);
958 if (IS_QLA2100(ha) || IS_QLA2200(ha))
959 return;
961 /* Serial Link options. */
962 DEBUG3(printk("scsi(%ld): Serial link options:\n",
963 ha->host_no));
964 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
965 sizeof(ha->fw_seriallink_options)));
967 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
968 if (ha->fw_seriallink_options[3] & BIT_2) {
969 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
971 /* 1G settings */
972 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
973 emphasis = (ha->fw_seriallink_options[2] &
974 (BIT_4 | BIT_3)) >> 3;
975 tx_sens = ha->fw_seriallink_options[0] &
976 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
977 rx_sens = (ha->fw_seriallink_options[0] &
978 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
979 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
980 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
981 if (rx_sens == 0x0)
982 rx_sens = 0x3;
983 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
984 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
985 ha->fw_options[10] |= BIT_5 |
986 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
987 (tx_sens & (BIT_1 | BIT_0));
989 /* 2G settings */
990 swing = (ha->fw_seriallink_options[2] &
991 (BIT_7 | BIT_6 | BIT_5)) >> 5;
992 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
993 tx_sens = ha->fw_seriallink_options[1] &
994 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
995 rx_sens = (ha->fw_seriallink_options[1] &
996 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
997 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
998 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
999 if (rx_sens == 0x0)
1000 rx_sens = 0x3;
1001 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1002 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1003 ha->fw_options[11] |= BIT_5 |
1004 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1005 (tx_sens & (BIT_1 | BIT_0));
1008 /* FCP2 options. */
1009 /* Return command IOCBs without waiting for an ABTS to complete. */
1010 ha->fw_options[3] |= BIT_13;
1012 /* LED scheme. */
1013 if (ha->flags.enable_led_scheme)
1014 ha->fw_options[2] |= BIT_12;
1016 /* Update firmware options. */
1017 qla2x00_set_fw_options(ha, ha->fw_options);
1020 void
1021 qla24xx_update_fw_options(scsi_qla_host_t *ha)
1023 int rval;
1025 /* Update Serial Link options. */
1026 if ((ha->fw_seriallink_options24[0] & BIT_0) == 0)
1027 return;
1029 rval = qla2x00_set_serdes_params(ha, ha->fw_seriallink_options24[1],
1030 ha->fw_seriallink_options24[2], ha->fw_seriallink_options24[3]);
1031 if (rval != QLA_SUCCESS) {
1032 qla_printk(KERN_WARNING, ha,
1033 "Unable to update Serial Link options (%x).\n", rval);
1037 void
1038 qla2x00_config_rings(struct scsi_qla_host *ha)
1040 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1042 /* Setup ring parameters in initialization control block. */
1043 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1044 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1045 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1046 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1047 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1048 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1049 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1050 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1052 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1053 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1054 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1055 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1056 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1059 void
1060 qla24xx_config_rings(struct scsi_qla_host *ha)
1062 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1063 struct init_cb_24xx *icb;
1065 /* Setup ring parameters in initialization control block. */
1066 icb = (struct init_cb_24xx *)ha->init_cb;
1067 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1068 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1069 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1070 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1071 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1072 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1073 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1074 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1076 WRT_REG_DWORD(&reg->req_q_in, 0);
1077 WRT_REG_DWORD(&reg->req_q_out, 0);
1078 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1079 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1080 RD_REG_DWORD(&reg->rsp_q_out);
1084 * qla2x00_init_rings() - Initializes firmware.
1085 * @ha: HA context
1087 * Beginning of request ring has initialization control block already built
1088 * by nvram config routine.
1090 * Returns 0 on success.
1092 static int
1093 qla2x00_init_rings(scsi_qla_host_t *ha)
1095 int rval;
1096 unsigned long flags = 0;
1097 int cnt;
1099 spin_lock_irqsave(&ha->hardware_lock, flags);
1101 /* Clear outstanding commands array. */
1102 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1103 ha->outstanding_cmds[cnt] = NULL;
1105 ha->current_outstanding_cmd = 0;
1107 /* Clear RSCN queue. */
1108 ha->rscn_in_ptr = 0;
1109 ha->rscn_out_ptr = 0;
1111 /* Initialize firmware. */
1112 ha->request_ring_ptr = ha->request_ring;
1113 ha->req_ring_index = 0;
1114 ha->req_q_cnt = ha->request_q_length;
1115 ha->response_ring_ptr = ha->response_ring;
1116 ha->rsp_ring_index = 0;
1118 /* Initialize response queue entries */
1119 qla2x00_init_response_q_entries(ha);
1121 ha->isp_ops.config_rings(ha);
1123 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1125 /* Update any ISP specific firmware options before initialization. */
1126 ha->isp_ops.update_fw_options(ha);
1128 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
1129 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1130 if (rval) {
1131 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1132 ha->host_no));
1133 } else {
1134 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1135 ha->host_no));
1138 return (rval);
1142 * qla2x00_fw_ready() - Waits for firmware ready.
1143 * @ha: HA context
1145 * Returns 0 on success.
1147 static int
1148 qla2x00_fw_ready(scsi_qla_host_t *ha)
1150 int rval;
1151 unsigned long wtime, mtime;
1152 uint16_t min_wait; /* Minimum wait time if loop is down */
1153 uint16_t wait_time; /* Wait time if loop is coming ready */
1154 uint16_t fw_state;
1156 rval = QLA_SUCCESS;
1158 /* 20 seconds for loop down. */
1159 min_wait = 20;
1162 * Firmware should take at most one RATOV to login, plus 5 seconds for
1163 * our own processing.
1165 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1166 wait_time = min_wait;
1169 /* Min wait time if loop down */
1170 mtime = jiffies + (min_wait * HZ);
1172 /* wait time before firmware ready */
1173 wtime = jiffies + (wait_time * HZ);
1175 /* Wait for ISP to finish LIP */
1176 if (!ha->flags.init_done)
1177 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1179 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1180 ha->host_no));
1182 do {
1183 rval = qla2x00_get_firmware_state(ha, &fw_state);
1184 if (rval == QLA_SUCCESS) {
1185 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1186 ha->device_flags &= ~DFLG_NO_CABLE;
1188 if (fw_state == FSTATE_READY) {
1189 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1190 ha->host_no));
1192 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1193 &ha->login_timeout, &ha->r_a_tov);
1195 rval = QLA_SUCCESS;
1196 break;
1199 rval = QLA_FUNCTION_FAILED;
1201 if (atomic_read(&ha->loop_down_timer) &&
1202 (fw_state >= FSTATE_LOSS_OF_SYNC ||
1203 fw_state == FSTATE_WAIT_AL_PA)) {
1204 /* Loop down. Timeout on min_wait for states
1205 * other than Wait for Login.
1207 if (time_after_eq(jiffies, mtime)) {
1208 qla_printk(KERN_INFO, ha,
1209 "Cable is unplugged...\n");
1211 ha->device_flags |= DFLG_NO_CABLE;
1212 break;
1215 } else {
1216 /* Mailbox cmd failed. Timeout on min_wait. */
1217 if (time_after_eq(jiffies, mtime))
1218 break;
1221 if (time_after_eq(jiffies, wtime))
1222 break;
1224 /* Delay for a while */
1225 msleep(500);
1227 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1228 ha->host_no, fw_state, jiffies));
1229 } while (1);
1231 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1232 ha->host_no, fw_state, jiffies));
1234 if (rval) {
1235 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1236 ha->host_no));
1239 return (rval);
1243 * qla2x00_configure_hba
1244 * Setup adapter context.
1246 * Input:
1247 * ha = adapter state pointer.
1249 * Returns:
1250 * 0 = success
1252 * Context:
1253 * Kernel context.
1255 static int
1256 qla2x00_configure_hba(scsi_qla_host_t *ha)
1258 int rval;
1259 uint16_t loop_id;
1260 uint16_t topo;
1261 uint8_t al_pa;
1262 uint8_t area;
1263 uint8_t domain;
1264 char connect_type[22];
1266 /* Get host addresses. */
1267 rval = qla2x00_get_adapter_id(ha,
1268 &loop_id, &al_pa, &area, &domain, &topo);
1269 if (rval != QLA_SUCCESS) {
1270 qla_printk(KERN_WARNING, ha,
1271 "ERROR -- Unable to get host loop ID.\n");
1272 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1273 return (rval);
1276 if (topo == 4) {
1277 qla_printk(KERN_INFO, ha,
1278 "Cannot get topology - retrying.\n");
1279 return (QLA_FUNCTION_FAILED);
1282 ha->loop_id = loop_id;
1284 /* initialize */
1285 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1286 ha->operating_mode = LOOP;
1288 switch (topo) {
1289 case 0:
1290 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1291 ha->host_no));
1292 ha->current_topology = ISP_CFG_NL;
1293 strcpy(connect_type, "(Loop)");
1294 break;
1296 case 1:
1297 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1298 ha->host_no));
1299 ha->current_topology = ISP_CFG_FL;
1300 strcpy(connect_type, "(FL_Port)");
1301 break;
1303 case 2:
1304 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1305 ha->host_no));
1306 ha->operating_mode = P2P;
1307 ha->current_topology = ISP_CFG_N;
1308 strcpy(connect_type, "(N_Port-to-N_Port)");
1309 break;
1311 case 3:
1312 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1313 ha->host_no));
1314 ha->operating_mode = P2P;
1315 ha->current_topology = ISP_CFG_F;
1316 strcpy(connect_type, "(F_Port)");
1317 break;
1319 default:
1320 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1321 "Using NL.\n",
1322 ha->host_no, topo));
1323 ha->current_topology = ISP_CFG_NL;
1324 strcpy(connect_type, "(Loop)");
1325 break;
1328 /* Save Host port and loop ID. */
1329 /* byte order - Big Endian */
1330 ha->d_id.b.domain = domain;
1331 ha->d_id.b.area = area;
1332 ha->d_id.b.al_pa = al_pa;
1334 if (!ha->flags.init_done)
1335 qla_printk(KERN_INFO, ha,
1336 "Topology - %s, Host Loop address 0x%x\n",
1337 connect_type, ha->loop_id);
1339 if (rval) {
1340 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1341 } else {
1342 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1345 return(rval);
1349 * NVRAM configuration for ISP 2xxx
1351 * Input:
1352 * ha = adapter block pointer.
1354 * Output:
1355 * initialization control block in response_ring
1356 * host adapters parameters in host adapter block
1358 * Returns:
1359 * 0 = success.
1362 qla2x00_nvram_config(scsi_qla_host_t *ha)
1364 int rval;
1365 uint8_t chksum = 0;
1366 uint16_t cnt;
1367 uint8_t *dptr1, *dptr2;
1368 init_cb_t *icb = ha->init_cb;
1369 nvram_t *nv = (nvram_t *)ha->request_ring;
1370 uint8_t *ptr = (uint8_t *)ha->request_ring;
1371 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1372 uint8_t timer_mode;
1374 rval = QLA_SUCCESS;
1376 /* Determine NVRAM starting address. */
1377 ha->nvram_size = sizeof(nvram_t);
1378 ha->nvram_base = 0;
1379 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1380 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1381 ha->nvram_base = 0x80;
1383 /* Get NVRAM data and calculate checksum. */
1384 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1385 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1386 chksum += *ptr++;
1388 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1389 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1390 ha->nvram_size));
1392 /* Bad NVRAM data, set defaults parameters. */
1393 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1394 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1395 /* Reset NVRAM data. */
1396 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1397 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1398 nv->nvram_version);
1399 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1400 "invalid -- WWPN) defaults.\n");
1403 * Set default initialization control block.
1405 memset(nv, 0, ha->nvram_size);
1406 nv->parameter_block_version = ICB_VERSION;
1408 if (IS_QLA23XX(ha)) {
1409 nv->firmware_options[0] = BIT_2 | BIT_1;
1410 nv->firmware_options[1] = BIT_7 | BIT_5;
1411 nv->add_firmware_options[0] = BIT_5;
1412 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1413 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1414 nv->special_options[1] = BIT_7;
1415 } else if (IS_QLA2200(ha)) {
1416 nv->firmware_options[0] = BIT_2 | BIT_1;
1417 nv->firmware_options[1] = BIT_7 | BIT_5;
1418 nv->add_firmware_options[0] = BIT_5;
1419 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1420 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1421 } else if (IS_QLA2100(ha)) {
1422 nv->firmware_options[0] = BIT_3 | BIT_1;
1423 nv->firmware_options[1] = BIT_5;
1424 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1427 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1428 nv->execution_throttle = __constant_cpu_to_le16(16);
1429 nv->retry_count = 8;
1430 nv->retry_delay = 1;
1432 nv->port_name[0] = 33;
1433 nv->port_name[3] = 224;
1434 nv->port_name[4] = 139;
1436 nv->login_timeout = 4;
1439 * Set default host adapter parameters
1441 nv->host_p[1] = BIT_2;
1442 nv->reset_delay = 5;
1443 nv->port_down_retry_count = 8;
1444 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1445 nv->link_down_timeout = 60;
1447 rval = 1;
1450 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1452 * The SN2 does not provide BIOS emulation which means you can't change
1453 * potentially bogus BIOS settings. Force the use of default settings
1454 * for link rate and frame size. Hope that the rest of the settings
1455 * are valid.
1457 if (ia64_platform_is("sn2")) {
1458 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1459 if (IS_QLA23XX(ha))
1460 nv->special_options[1] = BIT_7;
1462 #endif
1464 /* Reset Initialization control block */
1465 memset(icb, 0, ha->init_cb_size);
1468 * Setup driver NVRAM options.
1470 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1471 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1472 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1473 nv->firmware_options[1] &= ~BIT_4;
1475 if (IS_QLA23XX(ha)) {
1476 nv->firmware_options[0] |= BIT_2;
1477 nv->firmware_options[0] &= ~BIT_3;
1478 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1480 if (IS_QLA2300(ha)) {
1481 if (ha->fb_rev == FPM_2310) {
1482 strcpy(ha->model_number, "QLA2310");
1483 } else {
1484 strcpy(ha->model_number, "QLA2300");
1486 } else {
1487 if (rval == 0 &&
1488 memcmp(nv->model_number, BINZERO,
1489 sizeof(nv->model_number)) != 0) {
1490 char *st, *en;
1492 strncpy(ha->model_number, nv->model_number,
1493 sizeof(nv->model_number));
1494 st = en = ha->model_number;
1495 en += sizeof(nv->model_number) - 1;
1496 while (en > st) {
1497 if (*en != 0x20 && *en != 0x00)
1498 break;
1499 *en-- = '\0';
1501 } else {
1502 uint16_t index;
1504 index = (ha->pdev->subsystem_device & 0xff);
1505 if (index < QLA_MODEL_NAMES) {
1506 strcpy(ha->model_number,
1507 qla2x00_model_name[index]);
1508 ha->model_desc =
1509 qla2x00_model_desc[index];
1510 } else {
1511 strcpy(ha->model_number, "QLA23xx");
1515 } else if (IS_QLA2200(ha)) {
1516 nv->firmware_options[0] |= BIT_2;
1518 * 'Point-to-point preferred, else loop' is not a safe
1519 * connection mode setting.
1521 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1522 (BIT_5 | BIT_4)) {
1523 /* Force 'loop preferred, else point-to-point'. */
1524 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1525 nv->add_firmware_options[0] |= BIT_5;
1527 strcpy(ha->model_number, "QLA22xx");
1528 } else /*if (IS_QLA2100(ha))*/ {
1529 strcpy(ha->model_number, "QLA2100");
1533 * Copy over NVRAM RISC parameter block to initialization control block.
1535 dptr1 = (uint8_t *)icb;
1536 dptr2 = (uint8_t *)&nv->parameter_block_version;
1537 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1538 while (cnt--)
1539 *dptr1++ = *dptr2++;
1541 /* Copy 2nd half. */
1542 dptr1 = (uint8_t *)icb->add_firmware_options;
1543 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1544 while (cnt--)
1545 *dptr1++ = *dptr2++;
1547 /* Prepare nodename */
1548 if ((icb->firmware_options[1] & BIT_6) == 0) {
1550 * Firmware will apply the following mask if the nodename was
1551 * not provided.
1553 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1554 icb->node_name[0] &= 0xF0;
1558 * Set host adapter parameters.
1560 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1561 /* Always load RISC code on non ISP2[12]00 chips. */
1562 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1563 ha->flags.disable_risc_code_load = 0;
1564 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1565 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1566 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1567 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1569 ha->operating_mode =
1570 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1572 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1573 sizeof(ha->fw_seriallink_options));
1575 /* save HBA serial number */
1576 ha->serial0 = icb->port_name[5];
1577 ha->serial1 = icb->port_name[6];
1578 ha->serial2 = icb->port_name[7];
1579 ha->node_name = icb->node_name;
1580 ha->port_name = icb->port_name;
1582 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1584 ha->retry_count = nv->retry_count;
1586 /* Set minimum login_timeout to 4 seconds. */
1587 if (nv->login_timeout < ql2xlogintimeout)
1588 nv->login_timeout = ql2xlogintimeout;
1589 if (nv->login_timeout < 4)
1590 nv->login_timeout = 4;
1591 ha->login_timeout = nv->login_timeout;
1592 icb->login_timeout = nv->login_timeout;
1594 /* Set minimum RATOV to 200 tenths of a second. */
1595 ha->r_a_tov = 200;
1597 ha->loop_reset_delay = nv->reset_delay;
1599 /* Link Down Timeout = 0:
1601 * When Port Down timer expires we will start returning
1602 * I/O's to OS with "DID_NO_CONNECT".
1604 * Link Down Timeout != 0:
1606 * The driver waits for the link to come up after link down
1607 * before returning I/Os to OS with "DID_NO_CONNECT".
1609 if (nv->link_down_timeout == 0) {
1610 ha->loop_down_abort_time =
1611 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1612 } else {
1613 ha->link_down_timeout = nv->link_down_timeout;
1614 ha->loop_down_abort_time =
1615 (LOOP_DOWN_TIME - ha->link_down_timeout);
1619 * Need enough time to try and get the port back.
1621 ha->port_down_retry_count = nv->port_down_retry_count;
1622 if (qlport_down_retry)
1623 ha->port_down_retry_count = qlport_down_retry;
1624 /* Set login_retry_count */
1625 ha->login_retry_count = nv->retry_count;
1626 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1627 ha->port_down_retry_count > 3)
1628 ha->login_retry_count = ha->port_down_retry_count;
1629 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1630 ha->login_retry_count = ha->port_down_retry_count;
1631 if (ql2xloginretrycount)
1632 ha->login_retry_count = ql2xloginretrycount;
1634 icb->lun_enables = __constant_cpu_to_le16(0);
1635 icb->command_resource_count = 0;
1636 icb->immediate_notify_resource_count = 0;
1637 icb->timeout = __constant_cpu_to_le16(0);
1639 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1640 /* Enable RIO */
1641 icb->firmware_options[0] &= ~BIT_3;
1642 icb->add_firmware_options[0] &=
1643 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1644 icb->add_firmware_options[0] |= BIT_2;
1645 icb->response_accumulation_timer = 3;
1646 icb->interrupt_delay_timer = 5;
1648 ha->flags.process_response_queue = 1;
1649 } else {
1650 /* Enable ZIO -- Support mode 5 only. */
1651 timer_mode = icb->add_firmware_options[0] &
1652 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1653 icb->add_firmware_options[0] &=
1654 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1655 if (ql2xenablezio)
1656 timer_mode = BIT_2 | BIT_0;
1657 if (timer_mode == (BIT_2 | BIT_0)) {
1658 DEBUG2(printk("scsi(%ld): ZIO enabled; timer delay "
1659 "(%d).\n", ha->host_no, ql2xintrdelaytimer));
1660 qla_printk(KERN_INFO, ha,
1661 "ZIO enabled; timer delay (%d).\n",
1662 ql2xintrdelaytimer);
1664 icb->add_firmware_options[0] |= timer_mode;
1665 icb->interrupt_delay_timer = ql2xintrdelaytimer;
1666 ha->flags.process_response_queue = 1;
1670 if (rval) {
1671 DEBUG2_3(printk(KERN_WARNING
1672 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1674 return (rval);
1678 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1679 * @ha: HA context
1680 * @flags: allocation flags
1682 * Returns a pointer to the allocated fcport, or NULL, if none available.
1684 fc_port_t *
1685 qla2x00_alloc_fcport(scsi_qla_host_t *ha, int flags)
1687 fc_port_t *fcport;
1689 fcport = kmalloc(sizeof(fc_port_t), flags);
1690 if (fcport == NULL)
1691 return (fcport);
1693 /* Setup fcport template structure. */
1694 memset(fcport, 0, sizeof (fc_port_t));
1695 fcport->ha = ha;
1696 fcport->port_type = FCT_UNKNOWN;
1697 fcport->loop_id = FC_NO_LOOP_ID;
1698 fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
1699 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1700 fcport->flags = FCF_RLC_SUPPORT;
1701 fcport->supported_classes = FC_COS_UNSPECIFIED;
1703 return (fcport);
1707 * qla2x00_configure_loop
1708 * Updates Fibre Channel Device Database with what is actually on loop.
1710 * Input:
1711 * ha = adapter block pointer.
1713 * Returns:
1714 * 0 = success.
1715 * 1 = error.
1716 * 2 = database was full and device was not configured.
1718 static int
1719 qla2x00_configure_loop(scsi_qla_host_t *ha)
1721 int rval;
1722 unsigned long flags, save_flags;
1724 rval = QLA_SUCCESS;
1726 /* Get Initiator ID */
1727 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1728 rval = qla2x00_configure_hba(ha);
1729 if (rval != QLA_SUCCESS) {
1730 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1731 ha->host_no));
1732 return (rval);
1736 save_flags = flags = ha->dpc_flags;
1737 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1738 ha->host_no, flags));
1741 * If we have both an RSCN and PORT UPDATE pending then handle them
1742 * both at the same time.
1744 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1745 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1747 /* Determine what we need to do */
1748 if (ha->current_topology == ISP_CFG_FL &&
1749 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1751 ha->flags.rscn_queue_overflow = 1;
1752 set_bit(RSCN_UPDATE, &flags);
1754 } else if (ha->current_topology == ISP_CFG_F &&
1755 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1757 ha->flags.rscn_queue_overflow = 1;
1758 set_bit(RSCN_UPDATE, &flags);
1759 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1761 } else if (!ha->flags.online ||
1762 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1764 ha->flags.rscn_queue_overflow = 1;
1765 set_bit(RSCN_UPDATE, &flags);
1766 set_bit(LOCAL_LOOP_UPDATE, &flags);
1769 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1770 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1771 rval = QLA_FUNCTION_FAILED;
1772 } else {
1773 rval = qla2x00_configure_local_loop(ha);
1777 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1778 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1779 rval = QLA_FUNCTION_FAILED;
1780 } else {
1781 rval = qla2x00_configure_fabric(ha);
1785 if (rval == QLA_SUCCESS) {
1786 if (atomic_read(&ha->loop_down_timer) ||
1787 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1788 rval = QLA_FUNCTION_FAILED;
1789 } else {
1790 atomic_set(&ha->loop_state, LOOP_READY);
1792 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1796 if (rval) {
1797 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1798 __func__, ha->host_no));
1799 } else {
1800 DEBUG3(printk("%s: exiting normally\n", __func__));
1803 /* Restore state if a resync event occured during processing */
1804 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1805 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1806 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1807 if (test_bit(RSCN_UPDATE, &save_flags))
1808 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1811 return (rval);
1817 * qla2x00_configure_local_loop
1818 * Updates Fibre Channel Device Database with local loop devices.
1820 * Input:
1821 * ha = adapter block pointer.
1823 * Returns:
1824 * 0 = success.
1826 static int
1827 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1829 int rval, rval2;
1830 int found_devs;
1831 int found;
1832 fc_port_t *fcport, *new_fcport;
1834 uint16_t index;
1835 uint16_t entries;
1836 char *id_iter;
1837 uint16_t loop_id;
1838 uint8_t domain, area, al_pa;
1840 found_devs = 0;
1841 new_fcport = NULL;
1842 entries = MAX_FIBRE_DEVICES;
1844 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1845 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1847 /* Get list of logged in devices. */
1848 memset(ha->gid_list, 0, GID_LIST_SIZE);
1849 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1850 &entries);
1851 if (rval != QLA_SUCCESS)
1852 goto cleanup_allocation;
1854 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1855 ha->host_no, entries));
1856 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1857 entries * sizeof(struct gid_list_info)));
1859 /* Allocate temporary fcport for any new fcports discovered. */
1860 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1861 if (new_fcport == NULL) {
1862 rval = QLA_MEMORY_ALLOC_FAILED;
1863 goto cleanup_allocation;
1865 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1868 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1870 list_for_each_entry(fcport, &ha->fcports, list) {
1871 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1872 fcport->port_type != FCT_BROADCAST &&
1873 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1875 DEBUG(printk("scsi(%ld): Marking port lost, "
1876 "loop_id=0x%04x\n",
1877 ha->host_no, fcport->loop_id));
1879 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1880 fcport->flags &= ~FCF_FARP_DONE;
1884 /* Add devices to port list. */
1885 id_iter = (char *)ha->gid_list;
1886 for (index = 0; index < entries; index++) {
1887 domain = ((struct gid_list_info *)id_iter)->domain;
1888 area = ((struct gid_list_info *)id_iter)->area;
1889 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1890 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1891 loop_id = (uint16_t)
1892 ((struct gid_list_info *)id_iter)->loop_id_2100;
1893 else
1894 loop_id = le16_to_cpu(
1895 ((struct gid_list_info *)id_iter)->loop_id);
1896 id_iter += ha->gid_list_info_size;
1898 /* Bypass reserved domain fields. */
1899 if ((domain & 0xf0) == 0xf0)
1900 continue;
1902 /* Bypass if not same domain and area of adapter. */
1903 if (area && domain &&
1904 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
1905 continue;
1907 /* Bypass invalid local loop ID. */
1908 if (loop_id > LAST_LOCAL_LOOP_ID)
1909 continue;
1911 /* Fill in member data. */
1912 new_fcport->d_id.b.domain = domain;
1913 new_fcport->d_id.b.area = area;
1914 new_fcport->d_id.b.al_pa = al_pa;
1915 new_fcport->loop_id = loop_id;
1916 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1917 if (rval2 != QLA_SUCCESS) {
1918 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1919 "information -- get_port_database=%x, "
1920 "loop_id=0x%04x\n",
1921 ha->host_no, rval2, new_fcport->loop_id));
1922 continue;
1925 /* Check for matching device in port list. */
1926 found = 0;
1927 fcport = NULL;
1928 list_for_each_entry(fcport, &ha->fcports, list) {
1929 if (memcmp(new_fcport->port_name, fcport->port_name,
1930 WWN_SIZE))
1931 continue;
1933 fcport->flags &= ~(FCF_FABRIC_DEVICE |
1934 FCF_PERSISTENT_BOUND);
1935 fcport->loop_id = new_fcport->loop_id;
1936 fcport->port_type = new_fcport->port_type;
1937 fcport->d_id.b24 = new_fcport->d_id.b24;
1938 memcpy(fcport->node_name, new_fcport->node_name,
1939 WWN_SIZE);
1941 found++;
1942 break;
1945 if (!found) {
1946 /* New device, add to fcports list. */
1947 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
1948 list_add_tail(&new_fcport->list, &ha->fcports);
1950 /* Allocate a new replacement fcport. */
1951 fcport = new_fcport;
1952 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1953 if (new_fcport == NULL) {
1954 rval = QLA_MEMORY_ALLOC_FAILED;
1955 goto cleanup_allocation;
1957 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1960 qla2x00_update_fcport(ha, fcport);
1962 found_devs++;
1965 cleanup_allocation:
1966 if (new_fcport)
1967 kfree(new_fcport);
1969 if (rval != QLA_SUCCESS) {
1970 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
1971 "rval=%x\n", ha->host_no, rval));
1974 if (found_devs) {
1975 ha->device_flags |= DFLG_LOCAL_DEVICES;
1976 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
1979 return (rval);
1982 static void
1983 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
1985 fc_port_t *fcport;
1987 qla2x00_mark_all_devices_lost(ha);
1988 list_for_each_entry(fcport, &ha->fcports, list) {
1989 if (fcport->port_type != FCT_TARGET)
1990 continue;
1992 qla2x00_update_fcport(ha, fcport);
1997 * qla2x00_update_fcport
1998 * Updates device on list.
2000 * Input:
2001 * ha = adapter block pointer.
2002 * fcport = port structure pointer.
2004 * Return:
2005 * 0 - Success
2006 * BIT_0 - error
2008 * Context:
2009 * Kernel context.
2011 static void
2012 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2014 uint16_t index;
2015 unsigned long flags;
2016 srb_t *sp;
2018 fcport->ha = ha;
2019 fcport->login_retry = 0;
2020 fcport->port_login_retry_count = ha->port_down_retry_count *
2021 PORT_RETRY_TIME;
2022 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2023 PORT_RETRY_TIME);
2024 fcport->flags &= ~FCF_LOGIN_NEEDED;
2027 * Check for outstanding cmd on tape Bypass LUN discovery if active
2028 * command on tape.
2030 if (fcport->flags & FCF_TAPE_PRESENT) {
2031 spin_lock_irqsave(&ha->hardware_lock, flags);
2032 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
2033 fc_port_t *sfcp;
2035 if ((sp = ha->outstanding_cmds[index]) != 0) {
2036 sfcp = sp->fcport;
2037 if (sfcp == fcport) {
2038 atomic_set(&fcport->state, FCS_ONLINE);
2039 spin_unlock_irqrestore(
2040 &ha->hardware_lock, flags);
2041 return;
2045 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2048 if (fcport->port_type == FCT_INITIATOR ||
2049 fcport->port_type == FCT_BROADCAST)
2050 fcport->device_type = TYPE_PROCESSOR;
2052 atomic_set(&fcport->state, FCS_ONLINE);
2054 if (ha->flags.init_done)
2055 qla2x00_reg_remote_port(ha, fcport);
2058 void
2059 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2061 struct fc_rport_identifiers rport_ids;
2062 struct fc_rport *rport;
2064 if (fcport->rport) {
2065 fc_remote_port_unblock(fcport->rport);
2066 return;
2069 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2070 rport_ids.port_name = wwn_to_u64(fcport->port_name);
2071 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2072 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2073 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2074 fcport->rport = rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2075 if (!rport) {
2076 qla_printk(KERN_WARNING, ha,
2077 "Unable to allocate fc remote port!\n");
2078 return;
2080 rport->dd_data = fcport;
2081 rport->supported_classes = fcport->supported_classes;
2083 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2084 if (fcport->port_type == FCT_INITIATOR)
2085 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2086 if (fcport->port_type == FCT_TARGET)
2087 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2088 fc_remote_port_rolechg(rport, rport_ids.roles);
2090 if (rport->scsi_target_id != -1 &&
2091 rport->scsi_target_id < ha->host->max_id)
2092 fcport->os_target_id = rport->scsi_target_id;
2096 * qla2x00_configure_fabric
2097 * Setup SNS devices with loop ID's.
2099 * Input:
2100 * ha = adapter block pointer.
2102 * Returns:
2103 * 0 = success.
2104 * BIT_0 = error
2106 static int
2107 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2109 int rval, rval2;
2110 fc_port_t *fcport, *fcptemp;
2111 uint16_t next_loopid;
2112 uint16_t mb[MAILBOX_REGISTER_COUNT];
2113 uint16_t loop_id;
2114 LIST_HEAD(new_fcports);
2116 /* If FL port exists, then SNS is present */
2117 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
2118 loop_id = NPH_F_PORT;
2119 else
2120 loop_id = SNS_FL_PORT;
2121 rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
2122 if (rval != QLA_SUCCESS) {
2123 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2124 "Port\n", ha->host_no));
2126 ha->device_flags &= ~SWITCH_FOUND;
2127 return (QLA_SUCCESS);
2130 /* Mark devices that need re-synchronization. */
2131 rval2 = qla2x00_device_resync(ha);
2132 if (rval2 == QLA_RSCNS_HANDLED) {
2133 /* No point doing the scan, just continue. */
2134 return (QLA_SUCCESS);
2136 do {
2137 /* FDMI support. */
2138 if (ql2xfdmienable &&
2139 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2140 qla2x00_fdmi_register(ha);
2142 /* Ensure we are logged into the SNS. */
2143 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
2144 loop_id = NPH_SNS;
2145 else
2146 loop_id = SIMPLE_NAME_SERVER;
2147 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
2148 0xfc, mb, BIT_1 | BIT_0);
2149 if (mb[0] != MBS_COMMAND_COMPLETE) {
2150 DEBUG2(qla_printk(KERN_INFO, ha,
2151 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2152 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2153 mb[0], mb[1], mb[2], mb[6], mb[7]));
2154 return (QLA_SUCCESS);
2157 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2158 if (qla2x00_rft_id(ha)) {
2159 /* EMPTY */
2160 DEBUG2(printk("scsi(%ld): Register FC-4 "
2161 "TYPE failed.\n", ha->host_no));
2163 if (qla2x00_rff_id(ha)) {
2164 /* EMPTY */
2165 DEBUG2(printk("scsi(%ld): Register FC-4 "
2166 "Features failed.\n", ha->host_no));
2168 if (qla2x00_rnn_id(ha)) {
2169 /* EMPTY */
2170 DEBUG2(printk("scsi(%ld): Register Node Name "
2171 "failed.\n", ha->host_no));
2172 } else if (qla2x00_rsnn_nn(ha)) {
2173 /* EMPTY */
2174 DEBUG2(printk("scsi(%ld): Register Symbolic "
2175 "Node Name failed.\n", ha->host_no));
2179 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2180 if (rval != QLA_SUCCESS)
2181 break;
2184 * Logout all previous fabric devices marked lost, except
2185 * tape devices.
2187 list_for_each_entry(fcport, &ha->fcports, list) {
2188 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2189 break;
2191 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2192 continue;
2194 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2195 qla2x00_mark_device_lost(ha, fcport,
2196 ql2xplogiabsentdevice);
2197 if (fcport->loop_id != FC_NO_LOOP_ID &&
2198 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2199 fcport->port_type != FCT_INITIATOR &&
2200 fcport->port_type != FCT_BROADCAST) {
2202 ha->isp_ops.fabric_logout(ha,
2203 fcport->loop_id,
2204 fcport->d_id.b.domain,
2205 fcport->d_id.b.area,
2206 fcport->d_id.b.al_pa);
2207 fcport->loop_id = FC_NO_LOOP_ID;
2212 /* Starting free loop ID. */
2213 next_loopid = ha->min_external_loopid;
2216 * Scan through our port list and login entries that need to be
2217 * logged in.
2219 list_for_each_entry(fcport, &ha->fcports, list) {
2220 if (atomic_read(&ha->loop_down_timer) ||
2221 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2222 break;
2224 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2225 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2226 continue;
2228 if (fcport->loop_id == FC_NO_LOOP_ID) {
2229 fcport->loop_id = next_loopid;
2230 rval = qla2x00_find_new_loop_id(ha, fcport);
2231 if (rval != QLA_SUCCESS) {
2232 /* Ran out of IDs to use */
2233 break;
2236 /* Login and update database */
2237 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2240 /* Exit if out of loop IDs. */
2241 if (rval != QLA_SUCCESS) {
2242 break;
2246 * Login and add the new devices to our port list.
2248 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2249 if (atomic_read(&ha->loop_down_timer) ||
2250 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2251 break;
2253 /* Find a new loop ID to use. */
2254 fcport->loop_id = next_loopid;
2255 rval = qla2x00_find_new_loop_id(ha, fcport);
2256 if (rval != QLA_SUCCESS) {
2257 /* Ran out of IDs to use */
2258 break;
2261 /* Remove device from the new list and add it to DB */
2262 list_del(&fcport->list);
2263 list_add_tail(&fcport->list, &ha->fcports);
2265 /* Login and update database */
2266 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2268 } while (0);
2270 /* Free all new device structures not processed. */
2271 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2272 list_del(&fcport->list);
2273 kfree(fcport);
2276 if (rval) {
2277 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2278 "rval=%d\n", ha->host_no, rval));
2281 return (rval);
2286 * qla2x00_find_all_fabric_devs
2288 * Input:
2289 * ha = adapter block pointer.
2290 * dev = database device entry pointer.
2292 * Returns:
2293 * 0 = success.
2295 * Context:
2296 * Kernel context.
2298 static int
2299 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2301 int rval;
2302 uint16_t loop_id;
2303 fc_port_t *fcport, *new_fcport, *fcptemp;
2304 int found;
2306 sw_info_t *swl;
2307 int swl_idx;
2308 int first_dev, last_dev;
2309 port_id_t wrap, nxt_d_id;
2311 rval = QLA_SUCCESS;
2313 /* Try GID_PT to get device list, else GAN. */
2314 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2315 if (swl == NULL) {
2316 /*EMPTY*/
2317 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2318 "on GA_NXT\n", ha->host_no));
2319 } else {
2320 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2321 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2322 kfree(swl);
2323 swl = NULL;
2324 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2325 kfree(swl);
2326 swl = NULL;
2327 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2328 kfree(swl);
2329 swl = NULL;
2332 swl_idx = 0;
2334 /* Allocate temporary fcport for any new fcports discovered. */
2335 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2336 if (new_fcport == NULL) {
2337 if (swl)
2338 kfree(swl);
2339 return (QLA_MEMORY_ALLOC_FAILED);
2341 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2343 /* Set start port ID scan at adapter ID. */
2344 first_dev = 1;
2345 last_dev = 0;
2347 /* Starting free loop ID. */
2348 loop_id = ha->min_external_loopid;
2349 for (; loop_id <= ha->last_loop_id; loop_id++) {
2350 if (qla2x00_is_reserved_id(ha, loop_id))
2351 continue;
2353 if (atomic_read(&ha->loop_down_timer) ||
2354 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2355 break;
2357 if (swl != NULL) {
2358 if (last_dev) {
2359 wrap.b24 = new_fcport->d_id.b24;
2360 } else {
2361 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2362 memcpy(new_fcport->node_name,
2363 swl[swl_idx].node_name, WWN_SIZE);
2364 memcpy(new_fcport->port_name,
2365 swl[swl_idx].port_name, WWN_SIZE);
2367 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2368 last_dev = 1;
2370 swl_idx++;
2372 } else {
2373 /* Send GA_NXT to the switch */
2374 rval = qla2x00_ga_nxt(ha, new_fcport);
2375 if (rval != QLA_SUCCESS) {
2376 qla_printk(KERN_WARNING, ha,
2377 "SNS scan failed -- assuming zero-entry "
2378 "result...\n");
2379 list_for_each_entry_safe(fcport, fcptemp,
2380 new_fcports, list) {
2381 list_del(&fcport->list);
2382 kfree(fcport);
2384 rval = QLA_SUCCESS;
2385 break;
2389 /* If wrap on switch device list, exit. */
2390 if (first_dev) {
2391 wrap.b24 = new_fcport->d_id.b24;
2392 first_dev = 0;
2393 } else if (new_fcport->d_id.b24 == wrap.b24) {
2394 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2395 ha->host_no, new_fcport->d_id.b.domain,
2396 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2397 break;
2400 /* Bypass if host adapter. */
2401 if (new_fcport->d_id.b24 == ha->d_id.b24)
2402 continue;
2404 /* Bypass if same domain and area of adapter. */
2405 if (((new_fcport->d_id.b24 & 0xffff00) ==
2406 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2407 ISP_CFG_FL)
2408 continue;
2410 /* Bypass reserved domain fields. */
2411 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2412 continue;
2414 /* Locate matching device in database. */
2415 found = 0;
2416 list_for_each_entry(fcport, &ha->fcports, list) {
2417 if (memcmp(new_fcport->port_name, fcport->port_name,
2418 WWN_SIZE))
2419 continue;
2421 found++;
2424 * If address the same and state FCS_ONLINE, nothing
2425 * changed.
2427 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2428 atomic_read(&fcport->state) == FCS_ONLINE) {
2429 break;
2433 * If device was not a fabric device before.
2435 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2436 fcport->d_id.b24 = new_fcport->d_id.b24;
2437 fcport->loop_id = FC_NO_LOOP_ID;
2438 fcport->flags |= (FCF_FABRIC_DEVICE |
2439 FCF_LOGIN_NEEDED);
2440 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2441 break;
2445 * Port ID changed or device was marked to be updated;
2446 * Log it out if still logged in and mark it for
2447 * relogin later.
2449 fcport->d_id.b24 = new_fcport->d_id.b24;
2450 fcport->flags |= FCF_LOGIN_NEEDED;
2451 if (fcport->loop_id != FC_NO_LOOP_ID &&
2452 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2453 fcport->port_type != FCT_INITIATOR &&
2454 fcport->port_type != FCT_BROADCAST) {
2455 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2456 fcport->d_id.b.domain, fcport->d_id.b.area,
2457 fcport->d_id.b.al_pa);
2458 fcport->loop_id = FC_NO_LOOP_ID;
2461 break;
2464 if (found)
2465 continue;
2467 /* If device was not in our fcports list, then add it. */
2468 list_add_tail(&new_fcport->list, new_fcports);
2470 /* Allocate a new replacement fcport. */
2471 nxt_d_id.b24 = new_fcport->d_id.b24;
2472 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2473 if (new_fcport == NULL) {
2474 if (swl)
2475 kfree(swl);
2476 return (QLA_MEMORY_ALLOC_FAILED);
2478 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2479 new_fcport->d_id.b24 = nxt_d_id.b24;
2482 if (swl)
2483 kfree(swl);
2485 if (new_fcport)
2486 kfree(new_fcport);
2488 if (!list_empty(new_fcports))
2489 ha->device_flags |= DFLG_FABRIC_DEVICES;
2491 return (rval);
2495 * qla2x00_find_new_loop_id
2496 * Scan through our port list and find a new usable loop ID.
2498 * Input:
2499 * ha: adapter state pointer.
2500 * dev: port structure pointer.
2502 * Returns:
2503 * qla2x00 local function return status code.
2505 * Context:
2506 * Kernel context.
2509 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2511 int rval;
2512 int found;
2513 fc_port_t *fcport;
2514 uint16_t first_loop_id;
2516 rval = QLA_SUCCESS;
2518 /* Save starting loop ID. */
2519 first_loop_id = dev->loop_id;
2521 for (;;) {
2522 /* Skip loop ID if already used by adapter. */
2523 if (dev->loop_id == ha->loop_id) {
2524 dev->loop_id++;
2527 /* Skip reserved loop IDs. */
2528 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2529 dev->loop_id++;
2532 /* Reset loop ID if passed the end. */
2533 if (dev->loop_id > ha->last_loop_id) {
2534 /* first loop ID. */
2535 dev->loop_id = ha->min_external_loopid;
2538 /* Check for loop ID being already in use. */
2539 found = 0;
2540 fcport = NULL;
2541 list_for_each_entry(fcport, &ha->fcports, list) {
2542 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2543 /* ID possibly in use */
2544 found++;
2545 break;
2549 /* If not in use then it is free to use. */
2550 if (!found) {
2551 break;
2554 /* ID in use. Try next value. */
2555 dev->loop_id++;
2557 /* If wrap around. No free ID to use. */
2558 if (dev->loop_id == first_loop_id) {
2559 dev->loop_id = FC_NO_LOOP_ID;
2560 rval = QLA_FUNCTION_FAILED;
2561 break;
2565 return (rval);
2569 * qla2x00_device_resync
2570 * Marks devices in the database that needs resynchronization.
2572 * Input:
2573 * ha = adapter block pointer.
2575 * Context:
2576 * Kernel context.
2578 static int
2579 qla2x00_device_resync(scsi_qla_host_t *ha)
2581 int rval;
2582 int rval2;
2583 uint32_t mask;
2584 fc_port_t *fcport;
2585 uint32_t rscn_entry;
2586 uint8_t rscn_out_iter;
2587 uint8_t format;
2588 port_id_t d_id;
2590 rval = QLA_RSCNS_HANDLED;
2592 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2593 ha->flags.rscn_queue_overflow) {
2595 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2596 format = MSB(MSW(rscn_entry));
2597 d_id.b.domain = LSB(MSW(rscn_entry));
2598 d_id.b.area = MSB(LSW(rscn_entry));
2599 d_id.b.al_pa = LSB(LSW(rscn_entry));
2601 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2602 "[%02x/%02x%02x%02x].\n",
2603 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2604 d_id.b.area, d_id.b.al_pa));
2606 ha->rscn_out_ptr++;
2607 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2608 ha->rscn_out_ptr = 0;
2610 /* Skip duplicate entries. */
2611 for (rscn_out_iter = ha->rscn_out_ptr;
2612 !ha->flags.rscn_queue_overflow &&
2613 rscn_out_iter != ha->rscn_in_ptr;
2614 rscn_out_iter = (rscn_out_iter ==
2615 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2617 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2618 break;
2620 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2621 "entry found at [%d].\n", ha->host_no,
2622 rscn_out_iter));
2624 ha->rscn_out_ptr = rscn_out_iter;
2627 /* Queue overflow, set switch default case. */
2628 if (ha->flags.rscn_queue_overflow) {
2629 DEBUG(printk("scsi(%ld): device_resync: rscn "
2630 "overflow.\n", ha->host_no));
2632 format = 3;
2633 ha->flags.rscn_queue_overflow = 0;
2636 switch (format) {
2637 case 0:
2638 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) &&
2639 !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
2640 !IS_QLA24XX(ha) && !IS_QLA25XX(ha) &&
2641 ha->flags.init_done) {
2642 /* Handle port RSCN via asyncronous IOCBs */
2643 rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
2644 NULL, 0);
2645 if (rval2 == QLA_SUCCESS)
2646 continue;
2648 mask = 0xffffff;
2649 break;
2650 case 1:
2651 mask = 0xffff00;
2652 break;
2653 case 2:
2654 mask = 0xff0000;
2655 break;
2656 default:
2657 mask = 0x0;
2658 d_id.b24 = 0;
2659 ha->rscn_out_ptr = ha->rscn_in_ptr;
2660 break;
2663 rval = QLA_SUCCESS;
2665 /* Abort any outstanding IO descriptors. */
2666 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2667 qla2x00_cancel_io_descriptors(ha);
2669 list_for_each_entry(fcport, &ha->fcports, list) {
2670 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2671 (fcport->d_id.b24 & mask) != d_id.b24 ||
2672 fcport->port_type == FCT_BROADCAST)
2673 continue;
2675 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2676 if (format != 3 ||
2677 fcport->port_type != FCT_INITIATOR) {
2678 qla2x00_mark_device_lost(ha, fcport, 0);
2681 fcport->flags &= ~FCF_FARP_DONE;
2684 return (rval);
2688 * qla2x00_fabric_dev_login
2689 * Login fabric target device and update FC port database.
2691 * Input:
2692 * ha: adapter state pointer.
2693 * fcport: port structure list pointer.
2694 * next_loopid: contains value of a new loop ID that can be used
2695 * by the next login attempt.
2697 * Returns:
2698 * qla2x00 local function return status code.
2700 * Context:
2701 * Kernel context.
2703 static int
2704 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2705 uint16_t *next_loopid)
2707 int rval;
2708 int retry;
2709 uint8_t opts;
2711 rval = QLA_SUCCESS;
2712 retry = 0;
2714 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2715 if (rval == QLA_SUCCESS) {
2716 /* Send an ADISC to tape devices.*/
2717 opts = 0;
2718 if (fcport->flags & FCF_TAPE_PRESENT)
2719 opts |= BIT_1;
2720 rval = qla2x00_get_port_database(ha, fcport, opts);
2721 if (rval != QLA_SUCCESS) {
2722 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2723 fcport->d_id.b.domain, fcport->d_id.b.area,
2724 fcport->d_id.b.al_pa);
2725 qla2x00_mark_device_lost(ha, fcport, 1);
2727 } else {
2728 qla2x00_update_fcport(ha, fcport);
2732 return (rval);
2736 * qla2x00_fabric_login
2737 * Issue fabric login command.
2739 * Input:
2740 * ha = adapter block pointer.
2741 * device = pointer to FC device type structure.
2743 * Returns:
2744 * 0 - Login successfully
2745 * 1 - Login failed
2746 * 2 - Initiator device
2747 * 3 - Fatal error
2750 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2751 uint16_t *next_loopid)
2753 int rval;
2754 int retry;
2755 uint16_t tmp_loopid;
2756 uint16_t mb[MAILBOX_REGISTER_COUNT];
2758 retry = 0;
2759 tmp_loopid = 0;
2761 for (;;) {
2762 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2763 "for port %02x%02x%02x.\n",
2764 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2765 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2767 /* Login fcport on switch. */
2768 ha->isp_ops.fabric_login(ha, fcport->loop_id,
2769 fcport->d_id.b.domain, fcport->d_id.b.area,
2770 fcport->d_id.b.al_pa, mb, BIT_0);
2771 if (mb[0] == MBS_PORT_ID_USED) {
2773 * Device has another loop ID. The firmware team
2774 * recommends the driver perform an implicit login with
2775 * the specified ID again. The ID we just used is save
2776 * here so we return with an ID that can be tried by
2777 * the next login.
2779 retry++;
2780 tmp_loopid = fcport->loop_id;
2781 fcport->loop_id = mb[1];
2783 DEBUG(printk("Fabric Login: port in use - next "
2784 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2785 fcport->loop_id, fcport->d_id.b.domain,
2786 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2788 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2790 * Login succeeded.
2792 if (retry) {
2793 /* A retry occurred before. */
2794 *next_loopid = tmp_loopid;
2795 } else {
2797 * No retry occurred before. Just increment the
2798 * ID value for next login.
2800 *next_loopid = (fcport->loop_id + 1);
2803 if (mb[1] & BIT_0) {
2804 fcport->port_type = FCT_INITIATOR;
2805 } else {
2806 fcport->port_type = FCT_TARGET;
2807 if (mb[1] & BIT_1) {
2808 fcport->flags |= FCF_TAPE_PRESENT;
2812 if (mb[10] & BIT_0)
2813 fcport->supported_classes |= FC_COS_CLASS2;
2814 if (mb[10] & BIT_1)
2815 fcport->supported_classes |= FC_COS_CLASS3;
2817 rval = QLA_SUCCESS;
2818 break;
2819 } else if (mb[0] == MBS_LOOP_ID_USED) {
2821 * Loop ID already used, try next loop ID.
2823 fcport->loop_id++;
2824 rval = qla2x00_find_new_loop_id(ha, fcport);
2825 if (rval != QLA_SUCCESS) {
2826 /* Ran out of loop IDs to use */
2827 break;
2829 } else if (mb[0] == MBS_COMMAND_ERROR) {
2831 * Firmware possibly timed out during login. If NO
2832 * retries are left to do then the device is declared
2833 * dead.
2835 *next_loopid = fcport->loop_id;
2836 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2837 fcport->d_id.b.domain, fcport->d_id.b.area,
2838 fcport->d_id.b.al_pa);
2839 qla2x00_mark_device_lost(ha, fcport, 1);
2841 rval = 1;
2842 break;
2843 } else {
2845 * unrecoverable / not handled error
2847 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
2848 "loop_id=%x jiffies=%lx.\n",
2849 __func__, ha->host_no, mb[0],
2850 fcport->d_id.b.domain, fcport->d_id.b.area,
2851 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2853 *next_loopid = fcport->loop_id;
2854 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2855 fcport->d_id.b.domain, fcport->d_id.b.area,
2856 fcport->d_id.b.al_pa);
2857 fcport->loop_id = FC_NO_LOOP_ID;
2858 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2860 rval = 3;
2861 break;
2865 return (rval);
2869 * qla2x00_local_device_login
2870 * Issue local device login command.
2872 * Input:
2873 * ha = adapter block pointer.
2874 * loop_id = loop id of device to login to.
2876 * Returns (Where's the #define!!!!):
2877 * 0 - Login successfully
2878 * 1 - Login failed
2879 * 3 - Fatal error
2882 qla2x00_local_device_login(scsi_qla_host_t *ha, uint16_t loop_id)
2884 int rval;
2885 uint16_t mb[MAILBOX_REGISTER_COUNT];
2887 memset(mb, 0, sizeof(mb));
2888 rval = qla2x00_login_local_device(ha, loop_id, mb, BIT_0);
2889 if (rval == QLA_SUCCESS) {
2890 /* Interrogate mailbox registers for any errors */
2891 if (mb[0] == MBS_COMMAND_ERROR)
2892 rval = 1;
2893 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2894 /* device not in PCB table */
2895 rval = 3;
2898 return (rval);
2902 * qla2x00_loop_resync
2903 * Resync with fibre channel devices.
2905 * Input:
2906 * ha = adapter block pointer.
2908 * Returns:
2909 * 0 = success
2912 qla2x00_loop_resync(scsi_qla_host_t *ha)
2914 int rval;
2915 uint32_t wait_time;
2917 rval = QLA_SUCCESS;
2919 atomic_set(&ha->loop_state, LOOP_UPDATE);
2920 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2921 if (ha->flags.online) {
2922 if (!(rval = qla2x00_fw_ready(ha))) {
2923 /* Wait at most MAX_TARGET RSCNs for a stable link. */
2924 wait_time = 256;
2925 do {
2926 atomic_set(&ha->loop_state, LOOP_UPDATE);
2928 /* Issue a marker after FW becomes ready. */
2929 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2930 ha->marker_needed = 0;
2932 /* Remap devices on Loop. */
2933 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2935 qla2x00_configure_loop(ha);
2936 wait_time--;
2937 } while (!atomic_read(&ha->loop_down_timer) &&
2938 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2939 wait_time &&
2940 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2944 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2945 return (QLA_FUNCTION_FAILED);
2948 if (rval) {
2949 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
2952 return (rval);
2955 void
2956 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
2958 int rescan_done;
2959 fc_port_t *fcport;
2961 rescan_done = 0;
2962 list_for_each_entry(fcport, &ha->fcports, list) {
2963 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
2964 continue;
2966 qla2x00_update_fcport(ha, fcport);
2967 fcport->flags &= ~FCF_RESCAN_NEEDED;
2969 rescan_done = 1;
2971 qla2x00_probe_for_all_luns(ha);
2975 * qla2x00_abort_isp
2976 * Resets ISP and aborts all outstanding commands.
2978 * Input:
2979 * ha = adapter block pointer.
2981 * Returns:
2982 * 0 = success
2985 qla2x00_abort_isp(scsi_qla_host_t *ha)
2987 unsigned long flags = 0;
2988 uint16_t cnt;
2989 srb_t *sp;
2990 uint8_t status = 0;
2992 if (ha->flags.online) {
2993 ha->flags.online = 0;
2994 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2996 qla_printk(KERN_INFO, ha,
2997 "Performing ISP error recovery - ha= %p.\n", ha);
2998 ha->isp_ops.reset_chip(ha);
3000 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3001 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3002 atomic_set(&ha->loop_state, LOOP_DOWN);
3003 qla2x00_mark_all_devices_lost(ha);
3004 } else {
3005 if (!atomic_read(&ha->loop_down_timer))
3006 atomic_set(&ha->loop_down_timer,
3007 LOOP_DOWN_TIME);
3010 spin_lock_irqsave(&ha->hardware_lock, flags);
3011 /* Requeue all commands in outstanding command list. */
3012 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3013 sp = ha->outstanding_cmds[cnt];
3014 if (sp) {
3015 ha->outstanding_cmds[cnt] = NULL;
3016 sp->flags = 0;
3017 sp->cmd->result = DID_RESET << 16;
3018 sp->cmd->host_scribble = (unsigned char *)NULL;
3019 qla2x00_sp_compl(ha, sp);
3022 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3024 ha->isp_ops.nvram_config(ha);
3026 if (!qla2x00_restart_isp(ha)) {
3027 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3029 if (!atomic_read(&ha->loop_down_timer)) {
3031 * Issue marker command only when we are going
3032 * to start the I/O .
3034 ha->marker_needed = 1;
3037 ha->flags.online = 1;
3039 ha->isp_ops.enable_intrs(ha);
3041 ha->isp_abort_cnt = 0;
3042 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3043 } else { /* failed the ISP abort */
3044 ha->flags.online = 1;
3045 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3046 if (ha->isp_abort_cnt == 0) {
3047 qla_printk(KERN_WARNING, ha,
3048 "ISP error recovery failed - "
3049 "board disabled\n");
3051 * The next call disables the board
3052 * completely.
3054 ha->isp_ops.reset_adapter(ha);
3055 ha->flags.online = 0;
3056 clear_bit(ISP_ABORT_RETRY,
3057 &ha->dpc_flags);
3058 status = 0;
3059 } else { /* schedule another ISP abort */
3060 ha->isp_abort_cnt--;
3061 DEBUG(printk("qla%ld: ISP abort - "
3062 "retry remaining %d\n",
3063 ha->host_no, ha->isp_abort_cnt);)
3064 status = 1;
3066 } else {
3067 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3068 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3069 "- retrying (%d) more times\n",
3070 ha->host_no, ha->isp_abort_cnt);)
3071 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3072 status = 1;
3078 if (status) {
3079 qla_printk(KERN_INFO, ha,
3080 "qla2x00_abort_isp: **** FAILED ****\n");
3081 } else {
3082 DEBUG(printk(KERN_INFO
3083 "qla2x00_abort_isp(%ld): exiting.\n",
3084 ha->host_no);)
3087 return(status);
3091 * qla2x00_restart_isp
3092 * restarts the ISP after a reset
3094 * Input:
3095 * ha = adapter block pointer.
3097 * Returns:
3098 * 0 = success
3100 static int
3101 qla2x00_restart_isp(scsi_qla_host_t *ha)
3103 uint8_t status = 0;
3104 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3105 unsigned long flags = 0;
3106 uint32_t wait_time;
3108 /* If firmware needs to be loaded */
3109 if (qla2x00_isp_firmware(ha)) {
3110 ha->flags.online = 0;
3111 if (!(status = ha->isp_ops.chip_diag(ha))) {
3112 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3113 status = qla2x00_setup_chip(ha);
3114 goto done;
3117 spin_lock_irqsave(&ha->hardware_lock, flags);
3119 if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha)) {
3121 * Disable SRAM, Instruction RAM and GP RAM
3122 * parity.
3124 WRT_REG_WORD(&reg->hccr,
3125 (HCCR_ENABLE_PARITY + 0x0));
3126 RD_REG_WORD(&reg->hccr);
3129 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3131 status = qla2x00_setup_chip(ha);
3133 spin_lock_irqsave(&ha->hardware_lock, flags);
3135 if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha)) {
3136 /* Enable proper parity */
3137 if (IS_QLA2300(ha))
3138 /* SRAM parity */
3139 WRT_REG_WORD(&reg->hccr,
3140 (HCCR_ENABLE_PARITY + 0x1));
3141 else
3143 * SRAM, Instruction RAM and GP RAM
3144 * parity.
3146 WRT_REG_WORD(&reg->hccr,
3147 (HCCR_ENABLE_PARITY + 0x7));
3148 RD_REG_WORD(&reg->hccr);
3151 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3155 done:
3156 if (!status && !(status = qla2x00_init_rings(ha))) {
3157 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3158 if (!(status = qla2x00_fw_ready(ha))) {
3159 DEBUG(printk("%s(): Start configure loop, "
3160 "status = %d\n", __func__, status);)
3162 /* Issue a marker after FW becomes ready. */
3163 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3165 ha->flags.online = 1;
3166 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3167 wait_time = 256;
3168 do {
3169 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3170 qla2x00_configure_loop(ha);
3171 wait_time--;
3172 } while (!atomic_read(&ha->loop_down_timer) &&
3173 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3174 wait_time &&
3175 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3178 /* if no cable then assume it's good */
3179 if ((ha->device_flags & DFLG_NO_CABLE))
3180 status = 0;
3182 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3183 __func__,
3184 status);)
3186 return (status);
3190 * qla2x00_reset_adapter
3191 * Reset adapter.
3193 * Input:
3194 * ha = adapter block pointer.
3196 void
3197 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3199 unsigned long flags = 0;
3200 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3202 ha->flags.online = 0;
3203 ha->isp_ops.disable_intrs(ha);
3205 spin_lock_irqsave(&ha->hardware_lock, flags);
3206 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3207 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3208 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3209 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3210 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3213 void
3214 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3216 unsigned long flags = 0;
3217 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3219 ha->flags.online = 0;
3220 ha->isp_ops.disable_intrs(ha);
3222 spin_lock_irqsave(&ha->hardware_lock, flags);
3223 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3224 RD_REG_DWORD(&reg->hccr);
3225 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3226 RD_REG_DWORD(&reg->hccr);
3227 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3231 qla24xx_nvram_config(scsi_qla_host_t *ha)
3233 int rval;
3234 struct init_cb_24xx *icb;
3235 struct nvram_24xx *nv;
3236 uint32_t *dptr;
3237 uint8_t *dptr1, *dptr2;
3238 uint32_t chksum;
3239 uint16_t cnt;
3241 rval = QLA_SUCCESS;
3242 icb = (struct init_cb_24xx *)ha->init_cb;
3243 nv = (struct nvram_24xx *)ha->request_ring;
3245 /* Determine NVRAM starting address. */
3246 ha->nvram_size = sizeof(struct nvram_24xx);
3247 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3248 if (PCI_FUNC(ha->pdev->devfn))
3249 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3251 /* Get NVRAM data and calculate checksum. */
3252 dptr = (uint32_t *)nv;
3253 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3254 ha->nvram_size);
3255 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3256 chksum += le32_to_cpu(*dptr++);
3258 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3259 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3260 ha->nvram_size));
3262 /* Bad NVRAM data, set defaults parameters. */
3263 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3264 || nv->id[3] != ' ' ||
3265 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3266 /* Reset NVRAM data. */
3267 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3268 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3269 le16_to_cpu(nv->nvram_version));
3270 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3271 "invalid -- WWPN) defaults.\n");
3274 * Set default initialization control block.
3276 memset(nv, 0, ha->nvram_size);
3277 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3278 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3279 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3280 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3281 nv->exchange_count = __constant_cpu_to_le16(0);
3282 nv->hard_address = __constant_cpu_to_le16(124);
3283 nv->port_name[0] = 0x21;
3284 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3285 nv->port_name[2] = 0x00;
3286 nv->port_name[3] = 0xe0;
3287 nv->port_name[4] = 0x8b;
3288 nv->port_name[5] = 0x1c;
3289 nv->port_name[6] = 0x55;
3290 nv->port_name[7] = 0x86;
3291 nv->node_name[0] = 0x20;
3292 nv->node_name[1] = 0x00;
3293 nv->node_name[2] = 0x00;
3294 nv->node_name[3] = 0xe0;
3295 nv->node_name[4] = 0x8b;
3296 nv->node_name[5] = 0x1c;
3297 nv->node_name[6] = 0x55;
3298 nv->node_name[7] = 0x86;
3299 nv->login_retry_count = __constant_cpu_to_le16(8);
3300 nv->link_down_timeout = __constant_cpu_to_le16(200);
3301 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3302 nv->login_timeout = __constant_cpu_to_le16(0);
3303 nv->firmware_options_1 =
3304 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3305 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3306 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3307 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3308 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3309 nv->efi_parameters = __constant_cpu_to_le32(0);
3310 nv->reset_delay = 5;
3311 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3312 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3313 nv->link_down_timeout = __constant_cpu_to_le16(30);
3315 rval = 1;
3318 /* Reset Initialization control block */
3319 memset(icb, 0, sizeof(struct init_cb_24xx));
3321 /* Copy 1st segment. */
3322 dptr1 = (uint8_t *)icb;
3323 dptr2 = (uint8_t *)&nv->version;
3324 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3325 while (cnt--)
3326 *dptr1++ = *dptr2++;
3328 icb->login_retry_count = nv->login_retry_count;
3329 icb->link_down_timeout = nv->link_down_timeout;
3331 /* Copy 2nd segment. */
3332 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3333 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3334 cnt = (uint8_t *)&icb->reserved_3 -
3335 (uint8_t *)&icb->interrupt_delay_timer;
3336 while (cnt--)
3337 *dptr1++ = *dptr2++;
3340 * Setup driver NVRAM options.
3342 if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3343 char *st, *en;
3344 uint16_t index;
3346 strncpy(ha->model_number, nv->model_name,
3347 sizeof(nv->model_name));
3348 st = en = ha->model_number;
3349 en += sizeof(nv->model_name) - 1;
3350 while (en > st) {
3351 if (*en != 0x20 && *en != 0x00)
3352 break;
3353 *en-- = '\0';
3356 index = (ha->pdev->subsystem_device & 0xff);
3357 if (index < QLA_MODEL_NAMES)
3358 ha->model_desc = qla2x00_model_desc[index];
3359 } else
3360 strcpy(ha->model_number, "QLA2462");
3362 /* Prepare nodename */
3363 if ((icb->firmware_options_1 & BIT_14) == 0) {
3365 * Firmware will apply the following mask if the nodename was
3366 * not provided.
3368 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3369 icb->node_name[0] &= 0xF0;
3372 /* Set host adapter parameters. */
3373 ha->flags.disable_risc_code_load = 0;
3374 ha->flags.enable_lip_reset = 1;
3375 ha->flags.enable_lip_full_login = 1;
3376 ha->flags.enable_target_reset = 1;
3377 ha->flags.enable_led_scheme = 0;
3379 ha->operating_mode =
3380 (icb->firmware_options_2 & (BIT_6 | BIT_5 | BIT_4)) >> 4;
3382 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3383 sizeof(ha->fw_seriallink_options24));
3385 /* save HBA serial number */
3386 ha->serial0 = icb->port_name[5];
3387 ha->serial1 = icb->port_name[6];
3388 ha->serial2 = icb->port_name[7];
3389 ha->node_name = icb->node_name;
3390 ha->port_name = icb->port_name;
3392 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3394 /* Set minimum login_timeout to 4 seconds. */
3395 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3396 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3397 if (le16_to_cpu(nv->login_timeout) < 4)
3398 nv->login_timeout = __constant_cpu_to_le16(4);
3399 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3400 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3402 /* Set minimum RATOV to 200 tenths of a second. */
3403 ha->r_a_tov = 200;
3405 ha->loop_reset_delay = nv->reset_delay;
3407 /* Link Down Timeout = 0:
3409 * When Port Down timer expires we will start returning
3410 * I/O's to OS with "DID_NO_CONNECT".
3412 * Link Down Timeout != 0:
3414 * The driver waits for the link to come up after link down
3415 * before returning I/Os to OS with "DID_NO_CONNECT".
3417 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3418 ha->loop_down_abort_time =
3419 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3420 } else {
3421 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3422 ha->loop_down_abort_time =
3423 (LOOP_DOWN_TIME - ha->link_down_timeout);
3426 /* Need enough time to try and get the port back. */
3427 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3428 if (qlport_down_retry)
3429 ha->port_down_retry_count = qlport_down_retry;
3431 /* Set login_retry_count */
3432 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3433 if (ha->port_down_retry_count ==
3434 le16_to_cpu(nv->port_down_retry_count) &&
3435 ha->port_down_retry_count > 3)
3436 ha->login_retry_count = ha->port_down_retry_count;
3437 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3438 ha->login_retry_count = ha->port_down_retry_count;
3439 if (ql2xloginretrycount)
3440 ha->login_retry_count = ql2xloginretrycount;
3442 if (rval) {
3443 DEBUG2_3(printk(KERN_WARNING
3444 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3446 return (rval);
3450 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3452 int rval;
3453 uint16_t cnt;
3454 uint16_t *risc_code;
3455 unsigned long risc_address;
3456 unsigned long risc_code_size;
3457 int num;
3458 int i;
3459 uint16_t *req_ring;
3460 struct qla_fw_info *fw_iter;
3462 rval = QLA_SUCCESS;
3464 /* Load firmware sequences */
3465 fw_iter = ha->brd_info->fw_info;
3466 *srisc_addr = *ha->brd_info->fw_info->fwstart;
3467 while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
3468 risc_code = fw_iter->fwcode;
3469 risc_code_size = *fw_iter->fwlen;
3471 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL) {
3472 risc_address = *fw_iter->fwstart;
3473 } else {
3474 /* Extended address */
3475 risc_address = *fw_iter->lfwstart;
3478 num = 0;
3479 rval = 0;
3480 while (risc_code_size > 0 && !rval) {
3481 cnt = (uint16_t)(ha->fw_transfer_size >> 1);
3482 if (cnt > risc_code_size)
3483 cnt = risc_code_size;
3485 DEBUG7(printk("scsi(%ld): Loading risc segment@ "
3486 "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
3487 ha->host_no, risc_code, cnt, risc_address));
3489 req_ring = (uint16_t *)ha->request_ring;
3490 for (i = 0; i < cnt; i++)
3491 req_ring[i] = cpu_to_le16(risc_code[i]);
3493 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL) {
3494 rval = qla2x00_load_ram(ha, ha->request_dma,
3495 risc_address, cnt);
3496 } else {
3497 rval = qla2x00_load_ram_ext(ha,
3498 ha->request_dma, risc_address, cnt);
3500 if (rval) {
3501 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
3502 "load segment %d of firmware\n",
3503 ha->host_no, num));
3504 qla_printk(KERN_WARNING, ha,
3505 "[ERROR] Failed to load segment %d of "
3506 "firmware\n", num);
3508 qla2x00_dump_regs(ha);
3509 break;
3512 risc_code += cnt;
3513 risc_address += cnt;
3514 risc_code_size -= cnt;
3515 num++;
3518 /* Next firmware sequence */
3519 fw_iter++;
3522 return (rval);
3526 qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3528 int rval;
3529 int segments, fragment;
3530 uint32_t faddr;
3531 uint32_t *dcode, dlen;
3532 uint32_t risc_addr;
3533 uint32_t risc_size;
3534 uint32_t i;
3536 rval = QLA_SUCCESS;
3538 segments = FA_RISC_CODE_SEGMENTS;
3539 faddr = FA_RISC_CODE_ADDR;
3540 dcode = (uint32_t *)ha->request_ring;
3541 *srisc_addr = 0;
3543 /* Validate firmware image by checking version. */
3544 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3545 for (i = 0; i < 4; i++)
3546 dcode[i] = be32_to_cpu(dcode[i]);
3547 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3548 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3549 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3550 dcode[3] == 0)) {
3551 qla_printk(KERN_WARNING, ha,
3552 "Unable to verify integrity of flash firmware image!\n");
3553 qla_printk(KERN_WARNING, ha,
3554 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3555 dcode[1], dcode[2], dcode[3]);
3557 return QLA_FUNCTION_FAILED;
3560 while (segments && rval == QLA_SUCCESS) {
3561 /* Read segment's load information. */
3562 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3564 risc_addr = be32_to_cpu(dcode[2]);
3565 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3566 risc_size = be32_to_cpu(dcode[3]);
3568 fragment = 0;
3569 while (risc_size > 0 && rval == QLA_SUCCESS) {
3570 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3571 if (dlen > risc_size)
3572 dlen = risc_size;
3574 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3575 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3576 ha->host_no, risc_addr, dlen, faddr));
3578 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3579 for (i = 0; i < dlen; i++)
3580 dcode[i] = swab32(dcode[i]);
3582 rval = qla2x00_load_ram_ext(ha, ha->request_dma,
3583 risc_addr, dlen);
3584 if (rval) {
3585 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3586 "segment %d of firmware\n", ha->host_no,
3587 fragment));
3588 qla_printk(KERN_WARNING, ha,
3589 "[ERROR] Failed to load segment %d of "
3590 "firmware\n", fragment);
3591 break;
3594 faddr += dlen;
3595 risc_addr += dlen;
3596 risc_size -= dlen;
3597 fragment++;
3600 /* Next segment. */
3601 segments--;
3604 return rval;
3608 qla24xx_load_risc_hotplug(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3610 int rval;
3611 int segments, fragment;
3612 uint32_t *dcode, dlen;
3613 uint32_t risc_addr;
3614 uint32_t risc_size;
3615 uint32_t i;
3616 const struct firmware *fw_entry;
3617 uint32_t *fwcode, fwclen;
3619 if (request_firmware(&fw_entry, ha->brd_info->fw_fname,
3620 &ha->pdev->dev)) {
3621 qla_printk(KERN_ERR, ha,
3622 "Firmware image file not available: '%s'\n",
3623 ha->brd_info->fw_fname);
3624 return QLA_FUNCTION_FAILED;
3627 rval = QLA_SUCCESS;
3629 segments = FA_RISC_CODE_SEGMENTS;
3630 dcode = (uint32_t *)ha->request_ring;
3631 *srisc_addr = 0;
3632 fwcode = (uint32_t *)fw_entry->data;
3633 fwclen = 0;
3635 /* Validate firmware image by checking version. */
3636 if (fw_entry->size < 8 * sizeof(uint32_t)) {
3637 qla_printk(KERN_WARNING, ha,
3638 "Unable to verify integrity of flash firmware image "
3639 "(%Zd)!\n", fw_entry->size);
3640 goto fail_fw_integrity;
3642 for (i = 0; i < 4; i++)
3643 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3644 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3645 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3646 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3647 dcode[3] == 0)) {
3648 qla_printk(KERN_WARNING, ha,
3649 "Unable to verify integrity of flash firmware image!\n");
3650 qla_printk(KERN_WARNING, ha,
3651 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3652 dcode[1], dcode[2], dcode[3]);
3653 goto fail_fw_integrity;
3656 while (segments && rval == QLA_SUCCESS) {
3657 risc_addr = be32_to_cpu(fwcode[2]);
3658 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3659 risc_size = be32_to_cpu(fwcode[3]);
3661 /* Validate firmware image size. */
3662 fwclen += risc_size * sizeof(uint32_t);
3663 if (fw_entry->size < fwclen) {
3664 qla_printk(KERN_WARNING, ha,
3665 "Unable to verify integrity of flash firmware "
3666 "image (%Zd)!\n", fw_entry->size);
3667 goto fail_fw_integrity;
3670 fragment = 0;
3671 while (risc_size > 0 && rval == QLA_SUCCESS) {
3672 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3673 if (dlen > risc_size)
3674 dlen = risc_size;
3676 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3677 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3678 risc_addr, dlen));
3680 for (i = 0; i < dlen; i++)
3681 dcode[i] = swab32(fwcode[i]);
3683 rval = qla2x00_load_ram_ext(ha, ha->request_dma,
3684 risc_addr, dlen);
3685 if (rval) {
3686 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3687 "segment %d of firmware\n", ha->host_no,
3688 fragment));
3689 qla_printk(KERN_WARNING, ha,
3690 "[ERROR] Failed to load segment %d of "
3691 "firmware\n", fragment);
3692 break;
3695 fwcode += dlen;
3696 risc_addr += dlen;
3697 risc_size -= dlen;
3698 fragment++;
3701 /* Next segment. */
3702 segments--;
3705 release_firmware(fw_entry);
3706 return rval;
3708 fail_fw_integrity:
3710 release_firmware(fw_entry);
3711 return QLA_FUNCTION_FAILED;