]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/qla2xxx/qla_init.c
[SCSI] qla2xxx: Resync with latest HBA SSID specification -- 2.2j.
[net-next-2.6.git] / drivers / scsi / qla2xxx / qla_init.c
CommitLineData
1da177e4 1/*
fa90c54f
AV
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
1da177e4 4 *
fa90c54f 5 * See LICENSE.qla2xxx for copyright and licensing details.
1da177e4
LT
6 */
7#include "qla_def.h"
8
9#include <linux/delay.h>
0107109e 10#include <linux/vmalloc.h>
1da177e4
LT
11
12#include "qla_devtbl.h"
13
14/* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
15#ifndef EXT_IS_LUN_BIT_SET
16#define EXT_IS_LUN_BIT_SET(P,L) \
17 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
18#define EXT_SET_LUN_BIT(P,L) \
19 ((P)->mask[L/8] |= (0x80 >> (L%8)))
20#endif
21
22/*
23* QLogic ISP2x00 Hardware Support Function Prototypes.
24*/
1da177e4 25static int qla2x00_isp_firmware(scsi_qla_host_t *);
1da177e4
LT
26static void qla2x00_resize_request_q(scsi_qla_host_t *);
27static int qla2x00_setup_chip(scsi_qla_host_t *);
28static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
29static int qla2x00_init_rings(scsi_qla_host_t *);
30static int qla2x00_fw_ready(scsi_qla_host_t *);
31static int qla2x00_configure_hba(scsi_qla_host_t *);
1da177e4
LT
32static int qla2x00_configure_loop(scsi_qla_host_t *);
33static int qla2x00_configure_local_loop(scsi_qla_host_t *);
1da177e4
LT
34static int qla2x00_configure_fabric(scsi_qla_host_t *);
35static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
36static int qla2x00_device_resync(scsi_qla_host_t *);
37static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
38 uint16_t *);
1da177e4
LT
39
40static int qla2x00_restart_isp(scsi_qla_host_t *);
1da177e4
LT
41
42/****************************************************************************/
43/* QLogic ISP2x00 Hardware Support Functions. */
44/****************************************************************************/
45
46/*
47* qla2x00_initialize_adapter
48* Initialize board.
49*
50* Input:
51* ha = adapter block pointer.
52*
53* Returns:
54* 0 = success
55*/
56int
57qla2x00_initialize_adapter(scsi_qla_host_t *ha)
58{
59 int rval;
60 uint8_t restart_risc = 0;
61 uint8_t retry;
62 uint32_t wait_time;
63
64 /* Clear adapter flags. */
65 ha->flags.online = 0;
66 ha->flags.reset_active = 0;
67 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
68 atomic_set(&ha->loop_state, LOOP_DOWN);
69 ha->device_flags = 0;
1da177e4 70 ha->dpc_flags = 0;
1da177e4
LT
71 ha->flags.management_server_logged_in = 0;
72 ha->marker_needed = 0;
73 ha->mbx_flags = 0;
74 ha->isp_abort_cnt = 0;
75 ha->beacon_blink_led = 0;
cca5335c 76 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
1da177e4 77
0107109e 78 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
abbd8870 79 rval = ha->isp_ops.pci_config(ha);
1da177e4
LT
80 if (rval) {
81 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
82 ha->host_no));
83 return (rval);
84 }
85
abbd8870 86 ha->isp_ops.reset_chip(ha);
1da177e4 87
1da177e4 88 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
0107109e 89
abbd8870 90 ha->isp_ops.nvram_config(ha);
1da177e4
LT
91
92 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
93
94 retry = 10;
95 /*
96 * Try to configure the loop.
97 */
98 do {
99 restart_risc = 0;
100
101 /* If firmware needs to be loaded */
102 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
abbd8870 103 if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
1da177e4
LT
104 rval = qla2x00_setup_chip(ha);
105 }
106 }
107
108 if (rval == QLA_SUCCESS &&
109 (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
110check_fw_ready_again:
111 /*
fa2a1ce5 112 * Wait for a successful LIP up to a maximum
1da177e4
LT
113 * of (in seconds): RISC login timeout value,
114 * RISC retry count value, and port down retry
fa2a1ce5 115 * value OR a minimum of 4 seconds OR If no
1da177e4
LT
116 * cable, only 5 seconds.
117 */
118 rval = qla2x00_fw_ready(ha);
119 if (rval == QLA_SUCCESS) {
120 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
121
0107109e
AV
122 /* Issue a marker after FW becomes ready. */
123 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
124
1da177e4
LT
125 /*
126 * Wait at most MAX_TARGET RSCNs for a stable
127 * link.
128 */
129 wait_time = 256;
130 do {
131 clear_bit(LOOP_RESYNC_NEEDED,
132 &ha->dpc_flags);
133 rval = qla2x00_configure_loop(ha);
134
135 if (test_and_clear_bit(ISP_ABORT_NEEDED,
136 &ha->dpc_flags)) {
137 restart_risc = 1;
138 break;
139 }
140
141 /*
142 * If loop state change while we were
143 * discoverying devices then wait for
144 * LIP to complete
145 */
146
33135aa2
RA
147 if (atomic_read(&ha->loop_state) !=
148 LOOP_READY && retry--) {
1da177e4
LT
149 goto check_fw_ready_again;
150 }
151 wait_time--;
152 } while (!atomic_read(&ha->loop_down_timer) &&
153 retry &&
154 wait_time &&
155 (test_bit(LOOP_RESYNC_NEEDED,
156 &ha->dpc_flags)));
157
158 if (wait_time == 0)
159 rval = QLA_FUNCTION_FAILED;
1da177e4
LT
160 } else if (ha->device_flags & DFLG_NO_CABLE)
161 /* If no cable, then all is good. */
162 rval = QLA_SUCCESS;
163 }
164 } while (restart_risc && retry--);
165
166 if (rval == QLA_SUCCESS) {
167 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
1da177e4
LT
168 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
169 ha->marker_needed = 0;
170
171 ha->flags.online = 1;
172 } else {
173 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
174 }
175
176 return (rval);
177}
178
179/**
abbd8870 180 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
1da177e4
LT
181 * @ha: HA context
182 *
183 * Returns 0 on success.
184 */
abbd8870
AV
185int
186qla2100_pci_config(scsi_qla_host_t *ha)
1da177e4 187{
abbd8870 188 uint16_t w, mwi;
27b2f679 189 uint32_t d;
abbd8870 190 unsigned long flags;
3d71644c 191 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 192
1da177e4
LT
193 pci_set_master(ha->pdev);
194 mwi = 0;
195 if (pci_set_mwi(ha->pdev))
196 mwi = PCI_COMMAND_INVALIDATE;
1da177e4 197
1da177e4
LT
198 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
199 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
abbd8870
AV
200 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
201
202 /* Reset expansion ROM address decode enable */
27b2f679
AK
203 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
204 d &= ~PCI_ROM_ADDRESS_ENABLE;
205 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
1da177e4
LT
206
207 /* Get PCI bus information. */
208 spin_lock_irqsave(&ha->hardware_lock, flags);
3d71644c 209 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
1da177e4
LT
210 spin_unlock_irqrestore(&ha->hardware_lock, flags);
211
abbd8870
AV
212 return QLA_SUCCESS;
213}
1da177e4 214
abbd8870
AV
215/**
216 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
217 * @ha: HA context
218 *
219 * Returns 0 on success.
220 */
221int
222qla2300_pci_config(scsi_qla_host_t *ha)
223{
224 uint16_t w, mwi;
27b2f679 225 uint32_t d;
abbd8870
AV
226 unsigned long flags = 0;
227 uint32_t cnt;
3d71644c 228 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 229
abbd8870
AV
230 pci_set_master(ha->pdev);
231 mwi = 0;
232 if (pci_set_mwi(ha->pdev))
233 mwi = PCI_COMMAND_INVALIDATE;
1da177e4 234
abbd8870
AV
235 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
236 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1da177e4 237
abbd8870
AV
238 if (IS_QLA2322(ha) || IS_QLA6322(ha))
239 w &= ~PCI_COMMAND_INTX_DISABLE;
1da177e4 240
abbd8870
AV
241 /*
242 * If this is a 2300 card and not 2312, reset the
243 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
244 * the 2310 also reports itself as a 2300 so we need to get the
245 * fb revision level -- a 6 indicates it really is a 2300 and
246 * not a 2310.
247 */
248 if (IS_QLA2300(ha)) {
249 spin_lock_irqsave(&ha->hardware_lock, flags);
1da177e4 250
abbd8870 251 /* Pause RISC. */
3d71644c 252 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
abbd8870 253 for (cnt = 0; cnt < 30000; cnt++) {
3d71644c 254 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
abbd8870 255 break;
1da177e4 256
abbd8870
AV
257 udelay(10);
258 }
1da177e4 259
abbd8870 260 /* Select FPM registers. */
3d71644c
AV
261 WRT_REG_WORD(&reg->ctrl_status, 0x20);
262 RD_REG_WORD(&reg->ctrl_status);
abbd8870
AV
263
264 /* Get the fb rev level */
3d71644c 265 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
abbd8870
AV
266
267 if (ha->fb_rev == FPM_2300)
268 w &= ~PCI_COMMAND_INVALIDATE;
269
270 /* Deselect FPM registers. */
3d71644c
AV
271 WRT_REG_WORD(&reg->ctrl_status, 0x0);
272 RD_REG_WORD(&reg->ctrl_status);
abbd8870
AV
273
274 /* Release RISC module. */
3d71644c 275 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
abbd8870 276 for (cnt = 0; cnt < 30000; cnt++) {
3d71644c 277 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
abbd8870
AV
278 break;
279
280 udelay(10);
1da177e4 281 }
1da177e4 282
abbd8870
AV
283 spin_unlock_irqrestore(&ha->hardware_lock, flags);
284 }
1da177e4
LT
285 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
286
abbd8870
AV
287 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
288
1da177e4 289 /* Reset expansion ROM address decode enable */
27b2f679
AK
290 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
291 d &= ~PCI_ROM_ADDRESS_ENABLE;
292 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
1da177e4 293
abbd8870
AV
294 /* Get PCI bus information. */
295 spin_lock_irqsave(&ha->hardware_lock, flags);
3d71644c 296 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
abbd8870
AV
297 spin_unlock_irqrestore(&ha->hardware_lock, flags);
298
299 return QLA_SUCCESS;
1da177e4
LT
300}
301
0107109e
AV
302/**
303 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
304 * @ha: HA context
305 *
306 * Returns 0 on success.
307 */
308int
309qla24xx_pci_config(scsi_qla_host_t *ha)
310{
311 uint16_t w, mwi;
27b2f679 312 uint32_t d;
0107109e
AV
313 unsigned long flags = 0;
314 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
315 int pcix_cmd_reg, pcie_dctl_reg;
316
317 pci_set_master(ha->pdev);
318 mwi = 0;
319 if (pci_set_mwi(ha->pdev))
320 mwi = PCI_COMMAND_INVALIDATE;
0107109e
AV
321
322 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
323 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
324 w &= ~PCI_COMMAND_INTX_DISABLE;
325 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
326
327 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
328
329 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
330 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
331 if (pcix_cmd_reg) {
332 uint16_t pcix_cmd;
333
334 pcix_cmd_reg += PCI_X_CMD;
335 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
336 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
337 pcix_cmd |= 0x0008;
338 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
339 }
340
341 /* PCIe -- adjust Maximum Read Request Size (2048). */
342 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
343 if (pcie_dctl_reg) {
344 uint16_t pcie_dctl;
345
346 pcie_dctl_reg += PCI_EXP_DEVCTL;
347 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
348 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
349 pcie_dctl |= 0x4000;
350 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
351 }
352
353 /* Reset expansion ROM address decode enable */
27b2f679
AK
354 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
355 d &= ~PCI_ROM_ADDRESS_ENABLE;
356 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
0107109e
AV
357
358 /* Get PCI bus information. */
359 spin_lock_irqsave(&ha->hardware_lock, flags);
360 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
361 spin_unlock_irqrestore(&ha->hardware_lock, flags);
362
363 return QLA_SUCCESS;
364}
365
1da177e4
LT
366/**
367 * qla2x00_isp_firmware() - Choose firmware image.
368 * @ha: HA context
369 *
370 * Returns 0 on success.
371 */
372static int
373qla2x00_isp_firmware(scsi_qla_host_t *ha)
374{
375 int rval;
376
377 /* Assume loading risc code */
fa2a1ce5 378 rval = QLA_FUNCTION_FAILED;
1da177e4
LT
379
380 if (ha->flags.disable_risc_code_load) {
381 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
382 ha->host_no));
383 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
384
385 /* Verify checksum of loaded RISC code. */
441d1072 386 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
1da177e4
LT
387 }
388
389 if (rval) {
390 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
391 ha->host_no));
392 }
393
394 return (rval);
395}
396
397/**
398 * qla2x00_reset_chip() - Reset ISP chip.
399 * @ha: HA context
400 *
401 * Returns 0 on success.
402 */
abbd8870 403void
fa2a1ce5 404qla2x00_reset_chip(scsi_qla_host_t *ha)
1da177e4
LT
405{
406 unsigned long flags = 0;
3d71644c 407 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 408 uint32_t cnt;
1da177e4
LT
409 uint16_t cmd;
410
abbd8870 411 ha->isp_ops.disable_intrs(ha);
1da177e4
LT
412
413 spin_lock_irqsave(&ha->hardware_lock, flags);
414
415 /* Turn off master enable */
416 cmd = 0;
417 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
418 cmd &= ~PCI_COMMAND_MASTER;
419 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
420
421 if (!IS_QLA2100(ha)) {
422 /* Pause RISC. */
423 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
424 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
425 for (cnt = 0; cnt < 30000; cnt++) {
426 if ((RD_REG_WORD(&reg->hccr) &
427 HCCR_RISC_PAUSE) != 0)
428 break;
429 udelay(100);
430 }
431 } else {
432 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
433 udelay(10);
434 }
435
436 /* Select FPM registers. */
437 WRT_REG_WORD(&reg->ctrl_status, 0x20);
438 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
439
440 /* FPM Soft Reset. */
441 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
442 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
443
444 /* Toggle Fpm Reset. */
445 if (!IS_QLA2200(ha)) {
446 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
447 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
448 }
449
450 /* Select frame buffer registers. */
451 WRT_REG_WORD(&reg->ctrl_status, 0x10);
452 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
453
454 /* Reset frame buffer FIFOs. */
455 if (IS_QLA2200(ha)) {
456 WRT_FB_CMD_REG(ha, reg, 0xa000);
457 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
458 } else {
459 WRT_FB_CMD_REG(ha, reg, 0x00fc);
460
461 /* Read back fb_cmd until zero or 3 seconds max */
462 for (cnt = 0; cnt < 3000; cnt++) {
463 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
464 break;
465 udelay(100);
466 }
467 }
468
469 /* Select RISC module registers. */
470 WRT_REG_WORD(&reg->ctrl_status, 0);
471 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
472
473 /* Reset RISC processor. */
474 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
475 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
476
477 /* Release RISC processor. */
478 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
479 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
480 }
481
482 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
483 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
484
485 /* Reset ISP chip. */
486 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
487
488 /* Wait for RISC to recover from reset. */
489 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
490 /*
491 * It is necessary to for a delay here since the card doesn't
492 * respond to PCI reads during a reset. On some architectures
493 * this will result in an MCA.
494 */
495 udelay(20);
496 for (cnt = 30000; cnt; cnt--) {
497 if ((RD_REG_WORD(&reg->ctrl_status) &
498 CSR_ISP_SOFT_RESET) == 0)
499 break;
500 udelay(100);
501 }
502 } else
503 udelay(10);
504
505 /* Reset RISC processor. */
506 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
507
508 WRT_REG_WORD(&reg->semaphore, 0);
509
510 /* Release RISC processor. */
511 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
512 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
513
514 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
515 for (cnt = 0; cnt < 30000; cnt++) {
ffb39f03 516 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
1da177e4 517 break;
1da177e4
LT
518
519 udelay(100);
520 }
521 } else
522 udelay(100);
523
524 /* Turn on master enable */
525 cmd |= PCI_COMMAND_MASTER;
526 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
527
528 /* Disable RISC pause on FPM parity error. */
529 if (!IS_QLA2100(ha)) {
530 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
531 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
532 }
533
534 spin_unlock_irqrestore(&ha->hardware_lock, flags);
535}
536
0107109e 537/**
88c26663 538 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
0107109e
AV
539 * @ha: HA context
540 *
541 * Returns 0 on success.
542 */
88c26663
AV
543static inline void
544qla24xx_reset_risc(scsi_qla_host_t *ha)
0107109e
AV
545{
546 unsigned long flags = 0;
547 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
548 uint32_t cnt, d2;
335a1cc9 549 uint16_t wd;
0107109e 550
0107109e
AV
551 spin_lock_irqsave(&ha->hardware_lock, flags);
552
553 /* Reset RISC. */
554 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
555 for (cnt = 0; cnt < 30000; cnt++) {
556 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
557 break;
558
559 udelay(10);
560 }
561
562 WRT_REG_DWORD(&reg->ctrl_status,
563 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
335a1cc9 564 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
88c26663 565
335a1cc9 566 udelay(100);
88c26663 567 /* Wait for firmware to complete NVRAM accesses. */
88c26663
AV
568 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
569 for (cnt = 10000 ; cnt && d2; cnt--) {
570 udelay(5);
571 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
572 barrier();
573 }
574
335a1cc9 575 /* Wait for soft-reset to complete. */
0107109e
AV
576 d2 = RD_REG_DWORD(&reg->ctrl_status);
577 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
578 udelay(5);
579 d2 = RD_REG_DWORD(&reg->ctrl_status);
580 barrier();
581 }
582
583 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
584 RD_REG_DWORD(&reg->hccr);
585
586 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
587 RD_REG_DWORD(&reg->hccr);
588
589 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
590 RD_REG_DWORD(&reg->hccr);
591
592 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
593 for (cnt = 6000000 ; cnt && d2; cnt--) {
594 udelay(5);
595 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
596 barrier();
597 }
598
599 spin_unlock_irqrestore(&ha->hardware_lock, flags);
600}
601
88c26663
AV
602/**
603 * qla24xx_reset_chip() - Reset ISP24xx chip.
604 * @ha: HA context
605 *
606 * Returns 0 on success.
607 */
608void
609qla24xx_reset_chip(scsi_qla_host_t *ha)
610{
611 ha->isp_ops.disable_intrs(ha);
612
613 /* Perform RISC reset. */
614 qla24xx_reset_risc(ha);
615}
616
1da177e4
LT
617/**
618 * qla2x00_chip_diag() - Test chip for proper operation.
619 * @ha: HA context
620 *
621 * Returns 0 on success.
622 */
abbd8870 623int
1da177e4
LT
624qla2x00_chip_diag(scsi_qla_host_t *ha)
625{
626 int rval;
3d71644c 627 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4
LT
628 unsigned long flags = 0;
629 uint16_t data;
630 uint32_t cnt;
631 uint16_t mb[5];
632
633 /* Assume a failed state */
634 rval = QLA_FUNCTION_FAILED;
635
636 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
637 ha->host_no, (u_long)&reg->flash_address));
638
639 spin_lock_irqsave(&ha->hardware_lock, flags);
640
641 /* Reset ISP chip. */
642 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
643
644 /*
645 * We need to have a delay here since the card will not respond while
646 * in reset causing an MCA on some architectures.
647 */
648 udelay(20);
649 data = qla2x00_debounce_register(&reg->ctrl_status);
650 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
651 udelay(5);
652 data = RD_REG_WORD(&reg->ctrl_status);
653 barrier();
654 }
655
656 if (!cnt)
657 goto chip_diag_failed;
658
659 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
660 ha->host_no));
661
662 /* Reset RISC processor. */
663 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
664 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
665
666 /* Workaround for QLA2312 PCI parity error */
667 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
668 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
669 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
670 udelay(5);
671 data = RD_MAILBOX_REG(ha, reg, 0);
fa2a1ce5 672 barrier();
1da177e4
LT
673 }
674 } else
675 udelay(10);
676
677 if (!cnt)
678 goto chip_diag_failed;
679
680 /* Check product ID of chip */
681 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
682
683 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
684 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
685 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
686 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
687 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
688 mb[3] != PROD_ID_3) {
689 qla_printk(KERN_WARNING, ha,
690 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
691
692 goto chip_diag_failed;
693 }
694 ha->product_id[0] = mb[1];
695 ha->product_id[1] = mb[2];
696 ha->product_id[2] = mb[3];
697 ha->product_id[3] = mb[4];
698
699 /* Adjust fw RISC transfer size */
700 if (ha->request_q_length > 1024)
701 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
702 else
703 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
704 ha->request_q_length;
705
706 if (IS_QLA2200(ha) &&
707 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
708 /* Limit firmware transfer size with a 2200A */
709 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
710 ha->host_no));
711
ea5b6382 712 ha->device_type |= DT_ISP2200A;
1da177e4
LT
713 ha->fw_transfer_size = 128;
714 }
715
716 /* Wrap Incoming Mailboxes Test. */
717 spin_unlock_irqrestore(&ha->hardware_lock, flags);
718
719 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
720 rval = qla2x00_mbx_reg_test(ha);
721 if (rval) {
722 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
723 ha->host_no));
724 qla_printk(KERN_WARNING, ha,
725 "Failed mailbox send register test\n");
726 }
727 else {
728 /* Flag a successful rval */
729 rval = QLA_SUCCESS;
730 }
731 spin_lock_irqsave(&ha->hardware_lock, flags);
732
733chip_diag_failed:
734 if (rval)
735 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
736 "****\n", ha->host_no));
737
738 spin_unlock_irqrestore(&ha->hardware_lock, flags);
739
740 return (rval);
741}
742
0107109e
AV
743/**
744 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
745 * @ha: HA context
746 *
747 * Returns 0 on success.
748 */
749int
750qla24xx_chip_diag(scsi_qla_host_t *ha)
751{
752 int rval;
0107109e 753
88c26663
AV
754 /* Perform RISC reset. */
755 qla24xx_reset_risc(ha);
0107109e
AV
756
757 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
758
759 rval = qla2x00_mbx_reg_test(ha);
760 if (rval) {
761 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
762 ha->host_no));
763 qla_printk(KERN_WARNING, ha,
764 "Failed mailbox send register test\n");
765 } else {
766 /* Flag a successful rval */
767 rval = QLA_SUCCESS;
768 }
769
770 return rval;
771}
772
a7a167bf 773void
0107109e
AV
774qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
775{
a7a167bf
AV
776 int rval;
777 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
778 eft_size;
779 dma_addr_t eft_dma;
780 void *eft;
781
782 if (ha->fw_dump) {
783 qla_printk(KERN_WARNING, ha,
784 "Firmware dump previously allocated.\n");
785 return;
786 }
d4e3e04d 787
0107109e 788 ha->fw_dumped = 0;
a7a167bf 789 fixed_size = mem_size = eft_size = 0;
d4e3e04d 790 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
a7a167bf 791 fixed_size = sizeof(struct qla2100_fw_dump);
d4e3e04d 792 } else if (IS_QLA23XX(ha)) {
a7a167bf
AV
793 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
794 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
795 sizeof(uint16_t);
796 } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
797 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
798 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
799 sizeof(uint32_t);
800
801 /* Allocate memory for Extended Trace Buffer. */
802 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
803 GFP_KERNEL);
804 if (!eft) {
805 qla_printk(KERN_WARNING, ha, "Unable to allocate "
806 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
807 goto cont_alloc;
808 }
809
810 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
811 EFT_NUM_BUFFERS);
812 if (rval) {
813 qla_printk(KERN_WARNING, ha, "Unable to initialize "
814 "EFT (%d).\n", rval);
815 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
816 eft_dma);
817 goto cont_alloc;
818 }
819
820 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
821 EFT_SIZE / 1024);
822
823 eft_size = EFT_SIZE;
824 memset(eft, 0, eft_size);
825 ha->eft_dma = eft_dma;
826 ha->eft = eft;
d4e3e04d 827 }
a7a167bf
AV
828cont_alloc:
829 req_q_size = ha->request_q_length * sizeof(request_t);
830 rsp_q_size = ha->response_q_length * sizeof(response_t);
831
832 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
833 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
834 eft_size;
d4e3e04d
AV
835
836 ha->fw_dump = vmalloc(dump_size);
a7a167bf 837 if (!ha->fw_dump) {
0107109e 838 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
d4e3e04d 839 "firmware dump!!!\n", dump_size / 1024);
a7a167bf
AV
840
841 if (ha->eft) {
842 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
843 ha->eft_dma);
844 ha->eft = NULL;
845 ha->eft_dma = 0;
846 }
847 return;
848 }
849
850 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
851 dump_size / 1024);
852
853 ha->fw_dump_len = dump_size;
854 ha->fw_dump->signature[0] = 'Q';
855 ha->fw_dump->signature[1] = 'L';
856 ha->fw_dump->signature[2] = 'G';
857 ha->fw_dump->signature[3] = 'C';
858 ha->fw_dump->version = __constant_htonl(1);
859
860 ha->fw_dump->fixed_size = htonl(fixed_size);
861 ha->fw_dump->mem_size = htonl(mem_size);
862 ha->fw_dump->req_q_size = htonl(req_q_size);
863 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
864
865 ha->fw_dump->eft_size = htonl(eft_size);
866 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
867 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
868
869 ha->fw_dump->header_size =
870 htonl(offsetof(struct qla2xxx_fw_dump, isp));
0107109e
AV
871}
872
1da177e4
LT
873/**
874 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
875 * @ha: HA context
876 *
877 * Returns 0 on success.
878 */
879static void
880qla2x00_resize_request_q(scsi_qla_host_t *ha)
881{
882 int rval;
883 uint16_t fw_iocb_cnt = 0;
884 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
885 dma_addr_t request_dma;
886 request_t *request_ring;
887
888 /* Valid only on recent ISPs. */
889 if (IS_QLA2100(ha) || IS_QLA2200(ha))
890 return;
891
892 /* Retrieve IOCB counts available to the firmware. */
893 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
894 if (rval)
895 return;
896 /* No point in continuing if current settings are sufficient. */
897 if (fw_iocb_cnt < 1024)
898 return;
899 if (ha->request_q_length >= request_q_length)
900 return;
901
902 /* Attempt to claim larger area for request queue. */
903 request_ring = dma_alloc_coherent(&ha->pdev->dev,
904 (request_q_length + 1) * sizeof(request_t), &request_dma,
905 GFP_KERNEL);
906 if (request_ring == NULL)
907 return;
908
909 /* Resize successful, report extensions. */
910 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
911 (ha->fw_memory_size + 1) / 1024);
912 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
913 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
914
915 /* Clear old allocations. */
916 dma_free_coherent(&ha->pdev->dev,
917 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
918 ha->request_dma);
919
920 /* Begin using larger queue. */
921 ha->request_q_length = request_q_length;
922 ha->request_ring = request_ring;
923 ha->request_dma = request_dma;
924}
925
926/**
927 * qla2x00_setup_chip() - Load and start RISC firmware.
928 * @ha: HA context
929 *
930 * Returns 0 on success.
931 */
932static int
933qla2x00_setup_chip(scsi_qla_host_t *ha)
934{
0107109e
AV
935 int rval;
936 uint32_t srisc_address = 0;
1da177e4
LT
937
938 /* Load firmware sequences */
0107109e
AV
939 rval = ha->isp_ops.load_risc(ha, &srisc_address);
940 if (rval == QLA_SUCCESS) {
1da177e4
LT
941 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
942 "code.\n", ha->host_no));
943
0107109e 944 rval = qla2x00_verify_checksum(ha, srisc_address);
1da177e4
LT
945 if (rval == QLA_SUCCESS) {
946 /* Start firmware execution. */
947 DEBUG(printk("scsi(%ld): Checksum OK, start "
948 "firmware.\n", ha->host_no));
949
0107109e 950 rval = qla2x00_execute_fw(ha, srisc_address);
1da177e4
LT
951 /* Retrieve firmware information. */
952 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
953 qla2x00_get_fw_version(ha,
954 &ha->fw_major_version,
955 &ha->fw_minor_version,
956 &ha->fw_subminor_version,
957 &ha->fw_attributes, &ha->fw_memory_size);
958 qla2x00_resize_request_q(ha);
a7a167bf
AV
959
960 if (ql2xallocfwdump)
961 qla2x00_alloc_fw_dump(ha);
1da177e4
LT
962 }
963 } else {
964 DEBUG2(printk(KERN_INFO
965 "scsi(%ld): ISP Firmware failed checksum.\n",
966 ha->host_no));
967 }
968 }
969
970 if (rval) {
971 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
972 ha->host_no));
973 }
974
975 return (rval);
976}
977
978/**
979 * qla2x00_init_response_q_entries() - Initializes response queue entries.
980 * @ha: HA context
981 *
982 * Beginning of request ring has initialization control block already built
983 * by nvram config routine.
984 *
985 * Returns 0 on success.
986 */
987static void
988qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
989{
990 uint16_t cnt;
991 response_t *pkt;
992
993 pkt = ha->response_ring_ptr;
994 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
995 pkt->signature = RESPONSE_PROCESSED;
996 pkt++;
997 }
998
999}
1000
1001/**
1002 * qla2x00_update_fw_options() - Read and process firmware options.
1003 * @ha: HA context
1004 *
1005 * Returns 0 on success.
1006 */
abbd8870 1007void
1da177e4
LT
1008qla2x00_update_fw_options(scsi_qla_host_t *ha)
1009{
1010 uint16_t swing, emphasis, tx_sens, rx_sens;
1011
1012 memset(ha->fw_options, 0, sizeof(ha->fw_options));
1013 qla2x00_get_fw_options(ha, ha->fw_options);
1014
1015 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1016 return;
1017
1018 /* Serial Link options. */
1019 DEBUG3(printk("scsi(%ld): Serial link options:\n",
1020 ha->host_no));
1021 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1022 sizeof(ha->fw_seriallink_options)));
1023
1024 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1025 if (ha->fw_seriallink_options[3] & BIT_2) {
1026 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1027
1028 /* 1G settings */
1029 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1030 emphasis = (ha->fw_seriallink_options[2] &
1031 (BIT_4 | BIT_3)) >> 3;
1032 tx_sens = ha->fw_seriallink_options[0] &
fa2a1ce5 1033 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1da177e4
LT
1034 rx_sens = (ha->fw_seriallink_options[0] &
1035 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1036 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1037 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1038 if (rx_sens == 0x0)
1039 rx_sens = 0x3;
1040 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1041 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1042 ha->fw_options[10] |= BIT_5 |
1043 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1044 (tx_sens & (BIT_1 | BIT_0));
1045
1046 /* 2G settings */
1047 swing = (ha->fw_seriallink_options[2] &
1048 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1049 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1050 tx_sens = ha->fw_seriallink_options[1] &
fa2a1ce5 1051 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1da177e4
LT
1052 rx_sens = (ha->fw_seriallink_options[1] &
1053 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1054 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1055 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1056 if (rx_sens == 0x0)
1057 rx_sens = 0x3;
1058 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1059 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1060 ha->fw_options[11] |= BIT_5 |
1061 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1062 (tx_sens & (BIT_1 | BIT_0));
1063 }
1064
1065 /* FCP2 options. */
1066 /* Return command IOCBs without waiting for an ABTS to complete. */
1067 ha->fw_options[3] |= BIT_13;
1068
1069 /* LED scheme. */
1070 if (ha->flags.enable_led_scheme)
1071 ha->fw_options[2] |= BIT_12;
1072
48c02fde
AV
1073 /* Detect ISP6312. */
1074 if (IS_QLA6312(ha))
1075 ha->fw_options[2] |= BIT_13;
1076
1da177e4
LT
1077 /* Update firmware options. */
1078 qla2x00_set_fw_options(ha, ha->fw_options);
1079}
1080
0107109e
AV
1081void
1082qla24xx_update_fw_options(scsi_qla_host_t *ha)
1083{
1084 int rval;
1085
1086 /* Update Serial Link options. */
f94097ed 1087 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
0107109e
AV
1088 return;
1089
f94097ed
AV
1090 rval = qla2x00_set_serdes_params(ha,
1091 le16_to_cpu(ha->fw_seriallink_options24[1]),
1092 le16_to_cpu(ha->fw_seriallink_options24[2]),
1093 le16_to_cpu(ha->fw_seriallink_options24[3]));
0107109e
AV
1094 if (rval != QLA_SUCCESS) {
1095 qla_printk(KERN_WARNING, ha,
1096 "Unable to update Serial Link options (%x).\n", rval);
1097 }
1098}
1099
abbd8870
AV
1100void
1101qla2x00_config_rings(struct scsi_qla_host *ha)
1102{
3d71644c 1103 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
abbd8870
AV
1104
1105 /* Setup ring parameters in initialization control block. */
1106 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1107 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1108 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1109 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1110 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1111 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1112 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1113 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1114
1115 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1116 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1117 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1118 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1119 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1120}
1121
0107109e
AV
1122void
1123qla24xx_config_rings(struct scsi_qla_host *ha)
1124{
1125 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1126 struct init_cb_24xx *icb;
1127
1128 /* Setup ring parameters in initialization control block. */
1129 icb = (struct init_cb_24xx *)ha->init_cb;
1130 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1131 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1132 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1133 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1134 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1135 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1136 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1137 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1138
1139 WRT_REG_DWORD(&reg->req_q_in, 0);
1140 WRT_REG_DWORD(&reg->req_q_out, 0);
1141 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1142 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1143 RD_REG_DWORD(&reg->rsp_q_out);
1144}
1145
1da177e4
LT
1146/**
1147 * qla2x00_init_rings() - Initializes firmware.
1148 * @ha: HA context
1149 *
1150 * Beginning of request ring has initialization control block already built
1151 * by nvram config routine.
1152 *
1153 * Returns 0 on success.
1154 */
1155static int
1156qla2x00_init_rings(scsi_qla_host_t *ha)
1157{
1158 int rval;
1159 unsigned long flags = 0;
1160 int cnt;
1da177e4
LT
1161
1162 spin_lock_irqsave(&ha->hardware_lock, flags);
1163
1164 /* Clear outstanding commands array. */
1165 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1166 ha->outstanding_cmds[cnt] = NULL;
1167
1168 ha->current_outstanding_cmd = 0;
1169
1170 /* Clear RSCN queue. */
1171 ha->rscn_in_ptr = 0;
1172 ha->rscn_out_ptr = 0;
1173
1174 /* Initialize firmware. */
1175 ha->request_ring_ptr = ha->request_ring;
1176 ha->req_ring_index = 0;
1177 ha->req_q_cnt = ha->request_q_length;
1178 ha->response_ring_ptr = ha->response_ring;
1179 ha->rsp_ring_index = 0;
1180
1da177e4
LT
1181 /* Initialize response queue entries */
1182 qla2x00_init_response_q_entries(ha);
1183
abbd8870 1184 ha->isp_ops.config_rings(ha);
1da177e4
LT
1185
1186 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1187
1188 /* Update any ISP specific firmware options before initialization. */
abbd8870 1189 ha->isp_ops.update_fw_options(ha);
1da177e4
LT
1190
1191 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
0107109e 1192 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
1da177e4
LT
1193 if (rval) {
1194 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1195 ha->host_no));
1196 } else {
1197 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1198 ha->host_no));
1199 }
1200
1201 return (rval);
1202}
1203
1204/**
1205 * qla2x00_fw_ready() - Waits for firmware ready.
1206 * @ha: HA context
1207 *
1208 * Returns 0 on success.
1209 */
1210static int
1211qla2x00_fw_ready(scsi_qla_host_t *ha)
1212{
1213 int rval;
1214 unsigned long wtime, mtime;
1215 uint16_t min_wait; /* Minimum wait time if loop is down */
1216 uint16_t wait_time; /* Wait time if loop is coming ready */
1217 uint16_t fw_state;
1218
1219 rval = QLA_SUCCESS;
1220
1221 /* 20 seconds for loop down. */
fa2a1ce5 1222 min_wait = 20;
1da177e4
LT
1223
1224 /*
1225 * Firmware should take at most one RATOV to login, plus 5 seconds for
1226 * our own processing.
1227 */
1228 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1229 wait_time = min_wait;
1230 }
1231
1232 /* Min wait time if loop down */
1233 mtime = jiffies + (min_wait * HZ);
1234
1235 /* wait time before firmware ready */
1236 wtime = jiffies + (wait_time * HZ);
1237
1238 /* Wait for ISP to finish LIP */
1239 if (!ha->flags.init_done)
1240 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1241
1242 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1243 ha->host_no));
1244
1245 do {
1246 rval = qla2x00_get_firmware_state(ha, &fw_state);
1247 if (rval == QLA_SUCCESS) {
1248 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1249 ha->device_flags &= ~DFLG_NO_CABLE;
1250 }
1251 if (fw_state == FSTATE_READY) {
1252 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1253 ha->host_no));
1254
1255 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1256 &ha->login_timeout, &ha->r_a_tov);
1257
1258 rval = QLA_SUCCESS;
1259 break;
1260 }
1261
1262 rval = QLA_FUNCTION_FAILED;
1263
1264 if (atomic_read(&ha->loop_down_timer) &&
1265 (fw_state >= FSTATE_LOSS_OF_SYNC ||
1266 fw_state == FSTATE_WAIT_AL_PA)) {
1267 /* Loop down. Timeout on min_wait for states
fa2a1ce5
AV
1268 * other than Wait for Login.
1269 */
1da177e4
LT
1270 if (time_after_eq(jiffies, mtime)) {
1271 qla_printk(KERN_INFO, ha,
1272 "Cable is unplugged...\n");
1273
1274 ha->device_flags |= DFLG_NO_CABLE;
1275 break;
1276 }
1277 }
1278 } else {
1279 /* Mailbox cmd failed. Timeout on min_wait. */
1280 if (time_after_eq(jiffies, mtime))
1281 break;
1282 }
1283
1284 if (time_after_eq(jiffies, wtime))
1285 break;
1286
1287 /* Delay for a while */
1288 msleep(500);
1289
1290 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1291 ha->host_no, fw_state, jiffies));
1292 } while (1);
1293
1294 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1295 ha->host_no, fw_state, jiffies));
1296
1297 if (rval) {
1298 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1299 ha->host_no));
1300 }
1301
1302 return (rval);
1303}
1304
1305/*
1306* qla2x00_configure_hba
1307* Setup adapter context.
1308*
1309* Input:
1310* ha = adapter state pointer.
1311*
1312* Returns:
1313* 0 = success
1314*
1315* Context:
1316* Kernel context.
1317*/
1318static int
1319qla2x00_configure_hba(scsi_qla_host_t *ha)
1320{
1321 int rval;
1322 uint16_t loop_id;
1323 uint16_t topo;
1324 uint8_t al_pa;
1325 uint8_t area;
1326 uint8_t domain;
1327 char connect_type[22];
1328
1329 /* Get host addresses. */
1330 rval = qla2x00_get_adapter_id(ha,
1331 &loop_id, &al_pa, &area, &domain, &topo);
1332 if (rval != QLA_SUCCESS) {
23443b1d 1333 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
33135aa2
RA
1334 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1335 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1336 __func__, ha->host_no));
1337 } else {
1338 qla_printk(KERN_WARNING, ha,
1339 "ERROR -- Unable to get host loop ID.\n");
1340 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1341 }
1da177e4
LT
1342 return (rval);
1343 }
1344
1345 if (topo == 4) {
1346 qla_printk(KERN_INFO, ha,
1347 "Cannot get topology - retrying.\n");
1348 return (QLA_FUNCTION_FAILED);
1349 }
1350
1351 ha->loop_id = loop_id;
1352
1353 /* initialize */
1354 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1355 ha->operating_mode = LOOP;
1356
1357 switch (topo) {
1358 case 0:
1359 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1360 ha->host_no));
1361 ha->current_topology = ISP_CFG_NL;
1362 strcpy(connect_type, "(Loop)");
1363 break;
1364
1365 case 1:
1366 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1367 ha->host_no));
1368 ha->current_topology = ISP_CFG_FL;
1369 strcpy(connect_type, "(FL_Port)");
1370 break;
1371
1372 case 2:
1373 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1374 ha->host_no));
1375 ha->operating_mode = P2P;
1376 ha->current_topology = ISP_CFG_N;
1377 strcpy(connect_type, "(N_Port-to-N_Port)");
1378 break;
1379
1380 case 3:
1381 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1382 ha->host_no));
1383 ha->operating_mode = P2P;
1384 ha->current_topology = ISP_CFG_F;
1385 strcpy(connect_type, "(F_Port)");
1386 break;
1387
1388 default:
1389 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1390 "Using NL.\n",
1391 ha->host_no, topo));
1392 ha->current_topology = ISP_CFG_NL;
1393 strcpy(connect_type, "(Loop)");
1394 break;
1395 }
1396
1397 /* Save Host port and loop ID. */
1398 /* byte order - Big Endian */
1399 ha->d_id.b.domain = domain;
1400 ha->d_id.b.area = area;
1401 ha->d_id.b.al_pa = al_pa;
1402
1403 if (!ha->flags.init_done)
1404 qla_printk(KERN_INFO, ha,
1405 "Topology - %s, Host Loop address 0x%x\n",
1406 connect_type, ha->loop_id);
1407
1408 if (rval) {
1409 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1410 } else {
1411 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1412 }
1413
1414 return(rval);
1415}
1416
1417/*
1418* NVRAM configuration for ISP 2xxx
1419*
1420* Input:
1421* ha = adapter block pointer.
1422*
1423* Output:
1424* initialization control block in response_ring
1425* host adapters parameters in host adapter block
1426*
1427* Returns:
1428* 0 = success.
1429*/
abbd8870 1430int
1da177e4
LT
1431qla2x00_nvram_config(scsi_qla_host_t *ha)
1432{
0107109e
AV
1433 int rval;
1434 uint8_t chksum = 0;
1435 uint16_t cnt;
1436 uint8_t *dptr1, *dptr2;
1437 init_cb_t *icb = ha->init_cb;
1438 nvram_t *nv = (nvram_t *)ha->request_ring;
1439 uint8_t *ptr = (uint8_t *)ha->request_ring;
3d71644c 1440 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4
LT
1441
1442 rval = QLA_SUCCESS;
1443
1444 /* Determine NVRAM starting address. */
0107109e 1445 ha->nvram_size = sizeof(nvram_t);
1da177e4
LT
1446 ha->nvram_base = 0;
1447 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1448 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1449 ha->nvram_base = 0x80;
1450
1451 /* Get NVRAM data and calculate checksum. */
0107109e
AV
1452 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1453 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1454 chksum += *ptr++;
1da177e4
LT
1455
1456 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1457 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
0107109e 1458 ha->nvram_size));
1da177e4
LT
1459
1460 /* Bad NVRAM data, set defaults parameters. */
1461 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1462 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1463 /* Reset NVRAM data. */
1464 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1465 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1466 nv->nvram_version);
1467 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1468 "invalid -- WWPN) defaults.\n");
1469
1470 /*
1471 * Set default initialization control block.
1472 */
0107109e 1473 memset(nv, 0, ha->nvram_size);
1da177e4
LT
1474 nv->parameter_block_version = ICB_VERSION;
1475
1476 if (IS_QLA23XX(ha)) {
1477 nv->firmware_options[0] = BIT_2 | BIT_1;
1478 nv->firmware_options[1] = BIT_7 | BIT_5;
1479 nv->add_firmware_options[0] = BIT_5;
1480 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1481 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1482 nv->special_options[1] = BIT_7;
1483 } else if (IS_QLA2200(ha)) {
1484 nv->firmware_options[0] = BIT_2 | BIT_1;
1485 nv->firmware_options[1] = BIT_7 | BIT_5;
1486 nv->add_firmware_options[0] = BIT_5;
1487 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1488 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1489 } else if (IS_QLA2100(ha)) {
1490 nv->firmware_options[0] = BIT_3 | BIT_1;
1491 nv->firmware_options[1] = BIT_5;
1492 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1493 }
1494
1495 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1496 nv->execution_throttle = __constant_cpu_to_le16(16);
1497 nv->retry_count = 8;
1498 nv->retry_delay = 1;
1499
1500 nv->port_name[0] = 33;
1501 nv->port_name[3] = 224;
1502 nv->port_name[4] = 139;
1503
1504 nv->login_timeout = 4;
1505
1506 /*
1507 * Set default host adapter parameters
1508 */
1509 nv->host_p[1] = BIT_2;
1510 nv->reset_delay = 5;
1511 nv->port_down_retry_count = 8;
1512 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1513 nv->link_down_timeout = 60;
1514
1515 rval = 1;
1516 }
1517
1518#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1519 /*
1520 * The SN2 does not provide BIOS emulation which means you can't change
1521 * potentially bogus BIOS settings. Force the use of default settings
1522 * for link rate and frame size. Hope that the rest of the settings
1523 * are valid.
1524 */
1525 if (ia64_platform_is("sn2")) {
1526 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1527 if (IS_QLA23XX(ha))
1528 nv->special_options[1] = BIT_7;
1529 }
1530#endif
1531
1532 /* Reset Initialization control block */
0107109e 1533 memset(icb, 0, ha->init_cb_size);
1da177e4
LT
1534
1535 /*
1536 * Setup driver NVRAM options.
1537 */
1538 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1539 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1540 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1541 nv->firmware_options[1] &= ~BIT_4;
1542
1543 if (IS_QLA23XX(ha)) {
1544 nv->firmware_options[0] |= BIT_2;
1545 nv->firmware_options[0] &= ~BIT_3;
0107109e 1546 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1da177e4
LT
1547
1548 if (IS_QLA2300(ha)) {
1549 if (ha->fb_rev == FPM_2310) {
1550 strcpy(ha->model_number, "QLA2310");
1551 } else {
1552 strcpy(ha->model_number, "QLA2300");
1553 }
1554 } else {
1555 if (rval == 0 &&
1556 memcmp(nv->model_number, BINZERO,
1557 sizeof(nv->model_number)) != 0) {
1558 char *st, *en;
1559
1560 strncpy(ha->model_number, nv->model_number,
1561 sizeof(nv->model_number));
1562 st = en = ha->model_number;
1563 en += sizeof(nv->model_number) - 1;
1564 while (en > st) {
1565 if (*en != 0x20 && *en != 0x00)
1566 break;
1567 *en-- = '\0';
1568 }
1569 } else {
1570 uint16_t index;
1571
1572 index = (ha->pdev->subsystem_device & 0xff);
1573 if (index < QLA_MODEL_NAMES) {
1574 strcpy(ha->model_number,
1328962e 1575 qla2x00_model_name[index * 2]);
1da177e4 1576 ha->model_desc =
1328962e 1577 qla2x00_model_name[index * 2 + 1];
1da177e4
LT
1578 } else {
1579 strcpy(ha->model_number, "QLA23xx");
1580 }
1581 }
1582 }
1583 } else if (IS_QLA2200(ha)) {
1584 nv->firmware_options[0] |= BIT_2;
1585 /*
1586 * 'Point-to-point preferred, else loop' is not a safe
1587 * connection mode setting.
1588 */
1589 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1590 (BIT_5 | BIT_4)) {
1591 /* Force 'loop preferred, else point-to-point'. */
1592 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1593 nv->add_firmware_options[0] |= BIT_5;
1594 }
1595 strcpy(ha->model_number, "QLA22xx");
1596 } else /*if (IS_QLA2100(ha))*/ {
1597 strcpy(ha->model_number, "QLA2100");
1598 }
1599
1600 /*
1601 * Copy over NVRAM RISC parameter block to initialization control block.
1602 */
1603 dptr1 = (uint8_t *)icb;
1604 dptr2 = (uint8_t *)&nv->parameter_block_version;
1605 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1606 while (cnt--)
1607 *dptr1++ = *dptr2++;
1608
1609 /* Copy 2nd half. */
1610 dptr1 = (uint8_t *)icb->add_firmware_options;
1611 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1612 while (cnt--)
1613 *dptr1++ = *dptr2++;
1614
5341e868
AV
1615 /* Use alternate WWN? */
1616 if (nv->host_p[1] & BIT_7) {
1617 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1618 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1619 }
1620
1da177e4
LT
1621 /* Prepare nodename */
1622 if ((icb->firmware_options[1] & BIT_6) == 0) {
1623 /*
1624 * Firmware will apply the following mask if the nodename was
1625 * not provided.
1626 */
1627 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1628 icb->node_name[0] &= 0xF0;
1629 }
1630
1631 /*
1632 * Set host adapter parameters.
1633 */
1da177e4
LT
1634 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1635 /* Always load RISC code on non ISP2[12]00 chips. */
1636 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1637 ha->flags.disable_risc_code_load = 0;
1638 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1639 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1640 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
06c22bd1 1641 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1da177e4
LT
1642
1643 ha->operating_mode =
1644 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1645
1646 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1647 sizeof(ha->fw_seriallink_options));
1648
1649 /* save HBA serial number */
1650 ha->serial0 = icb->port_name[5];
1651 ha->serial1 = icb->port_name[6];
1652 ha->serial2 = icb->port_name[7];
3d71644c
AV
1653 ha->node_name = icb->node_name;
1654 ha->port_name = icb->port_name;
1da177e4
LT
1655
1656 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1657
1658 ha->retry_count = nv->retry_count;
1659
1660 /* Set minimum login_timeout to 4 seconds. */
1661 if (nv->login_timeout < ql2xlogintimeout)
1662 nv->login_timeout = ql2xlogintimeout;
1663 if (nv->login_timeout < 4)
1664 nv->login_timeout = 4;
1665 ha->login_timeout = nv->login_timeout;
1666 icb->login_timeout = nv->login_timeout;
1667
1668 /* Set minimum RATOV to 200 tenths of a second. */
1669 ha->r_a_tov = 200;
1670
1da177e4
LT
1671 ha->loop_reset_delay = nv->reset_delay;
1672
1da177e4
LT
1673 /* Link Down Timeout = 0:
1674 *
1675 * When Port Down timer expires we will start returning
1676 * I/O's to OS with "DID_NO_CONNECT".
1677 *
1678 * Link Down Timeout != 0:
1679 *
1680 * The driver waits for the link to come up after link down
1681 * before returning I/Os to OS with "DID_NO_CONNECT".
fa2a1ce5 1682 */
1da177e4
LT
1683 if (nv->link_down_timeout == 0) {
1684 ha->loop_down_abort_time =
354d6b21 1685 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1da177e4
LT
1686 } else {
1687 ha->link_down_timeout = nv->link_down_timeout;
1688 ha->loop_down_abort_time =
1689 (LOOP_DOWN_TIME - ha->link_down_timeout);
fa2a1ce5 1690 }
1da177e4 1691
1da177e4
LT
1692 /*
1693 * Need enough time to try and get the port back.
1694 */
1695 ha->port_down_retry_count = nv->port_down_retry_count;
1696 if (qlport_down_retry)
1697 ha->port_down_retry_count = qlport_down_retry;
1698 /* Set login_retry_count */
1699 ha->login_retry_count = nv->retry_count;
1700 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1701 ha->port_down_retry_count > 3)
1702 ha->login_retry_count = ha->port_down_retry_count;
1703 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1704 ha->login_retry_count = ha->port_down_retry_count;
1705 if (ql2xloginretrycount)
1706 ha->login_retry_count = ql2xloginretrycount;
1707
1da177e4
LT
1708 icb->lun_enables = __constant_cpu_to_le16(0);
1709 icb->command_resource_count = 0;
1710 icb->immediate_notify_resource_count = 0;
1711 icb->timeout = __constant_cpu_to_le16(0);
1712
1713 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1714 /* Enable RIO */
1715 icb->firmware_options[0] &= ~BIT_3;
1716 icb->add_firmware_options[0] &=
1717 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1718 icb->add_firmware_options[0] |= BIT_2;
1719 icb->response_accumulation_timer = 3;
1720 icb->interrupt_delay_timer = 5;
1721
1722 ha->flags.process_response_queue = 1;
1723 } else {
4fdfefe5
AV
1724 /* Enable ZIO. */
1725 if (!ha->flags.init_done) {
1726 ha->zio_mode = icb->add_firmware_options[0] &
1727 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1728 ha->zio_timer = icb->interrupt_delay_timer ?
1729 icb->interrupt_delay_timer: 2;
1730 }
1da177e4
LT
1731 icb->add_firmware_options[0] &=
1732 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4fdfefe5
AV
1733 ha->flags.process_response_queue = 0;
1734 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4a59f71d
AV
1735 ha->zio_mode = QLA_ZIO_MODE_6;
1736
4fdfefe5
AV
1737 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1738 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1739 ha->zio_timer * 100));
1da177e4 1740 qla_printk(KERN_INFO, ha,
4fdfefe5
AV
1741 "ZIO mode %d enabled; timer delay (%d us).\n",
1742 ha->zio_mode, ha->zio_timer * 100);
1da177e4 1743
4fdfefe5
AV
1744 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1745 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1da177e4
LT
1746 ha->flags.process_response_queue = 1;
1747 }
1748 }
1749
1750 if (rval) {
1751 DEBUG2_3(printk(KERN_WARNING
1752 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1753 }
1754 return (rval);
1755}
1756
19a7b4ae
JSEC
1757static void
1758qla2x00_rport_del(void *data)
1759{
1760 fc_port_t *fcport = data;
d97994dc
AV
1761 struct fc_rport *rport;
1762 unsigned long flags;
1763
1764 spin_lock_irqsave(&fcport->rport_lock, flags);
1765 rport = fcport->drport;
1766 fcport->drport = NULL;
1767 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1768 if (rport)
1769 fc_remote_port_delete(rport);
19a7b4ae 1770
19a7b4ae
JSEC
1771}
1772
1da177e4
LT
1773/**
1774 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1775 * @ha: HA context
1776 * @flags: allocation flags
1777 *
1778 * Returns a pointer to the allocated fcport, or NULL, if none available.
1779 */
1780fc_port_t *
c53033f6 1781qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1da177e4
LT
1782{
1783 fc_port_t *fcport;
1784
1785 fcport = kmalloc(sizeof(fc_port_t), flags);
1786 if (fcport == NULL)
1787 return (fcport);
1788
1789 /* Setup fcport template structure. */
1790 memset(fcport, 0, sizeof (fc_port_t));
1791 fcport->ha = ha;
1792 fcport->port_type = FCT_UNKNOWN;
1793 fcport->loop_id = FC_NO_LOOP_ID;
1da177e4
LT
1794 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1795 fcport->flags = FCF_RLC_SUPPORT;
ad3e0eda 1796 fcport->supported_classes = FC_COS_UNSPECIFIED;
d97994dc 1797 spin_lock_init(&fcport->rport_lock);
1da177e4
LT
1798
1799 return (fcport);
1800}
1801
1802/*
1803 * qla2x00_configure_loop
1804 * Updates Fibre Channel Device Database with what is actually on loop.
1805 *
1806 * Input:
1807 * ha = adapter block pointer.
1808 *
1809 * Returns:
1810 * 0 = success.
1811 * 1 = error.
1812 * 2 = database was full and device was not configured.
1813 */
1814static int
fa2a1ce5 1815qla2x00_configure_loop(scsi_qla_host_t *ha)
1da177e4
LT
1816{
1817 int rval;
1818 unsigned long flags, save_flags;
1819
1820 rval = QLA_SUCCESS;
1821
1822 /* Get Initiator ID */
1823 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1824 rval = qla2x00_configure_hba(ha);
1825 if (rval != QLA_SUCCESS) {
1826 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1827 ha->host_no));
1828 return (rval);
1829 }
1830 }
1831
1832 save_flags = flags = ha->dpc_flags;
1833 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1834 ha->host_no, flags));
1835
1836 /*
1837 * If we have both an RSCN and PORT UPDATE pending then handle them
1838 * both at the same time.
1839 */
1840 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1841 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1da177e4
LT
1842
1843 /* Determine what we need to do */
1844 if (ha->current_topology == ISP_CFG_FL &&
1845 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1846
1847 ha->flags.rscn_queue_overflow = 1;
1848 set_bit(RSCN_UPDATE, &flags);
1849
1850 } else if (ha->current_topology == ISP_CFG_F &&
1851 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1852
1853 ha->flags.rscn_queue_overflow = 1;
1854 set_bit(RSCN_UPDATE, &flags);
1855 clear_bit(LOCAL_LOOP_UPDATE, &flags);
21333b48
AV
1856
1857 } else if (ha->current_topology == ISP_CFG_N) {
1858 clear_bit(RSCN_UPDATE, &flags);
1da177e4
LT
1859
1860 } else if (!ha->flags.online ||
1861 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1862
1863 ha->flags.rscn_queue_overflow = 1;
1864 set_bit(RSCN_UPDATE, &flags);
1865 set_bit(LOCAL_LOOP_UPDATE, &flags);
1866 }
1867
1868 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1869 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1870 rval = QLA_FUNCTION_FAILED;
1871 } else {
1872 rval = qla2x00_configure_local_loop(ha);
1873 }
1874 }
1875
1876 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
23443b1d 1877 if (LOOP_TRANSITION(ha)) {
1da177e4
LT
1878 rval = QLA_FUNCTION_FAILED;
1879 } else {
1880 rval = qla2x00_configure_fabric(ha);
1881 }
1882 }
1883
1884 if (rval == QLA_SUCCESS) {
1885 if (atomic_read(&ha->loop_down_timer) ||
1886 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1887 rval = QLA_FUNCTION_FAILED;
1888 } else {
1da177e4
LT
1889 atomic_set(&ha->loop_state, LOOP_READY);
1890
1891 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1892 }
1893 }
1894
1895 if (rval) {
1896 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1897 __func__, ha->host_no));
1898 } else {
1899 DEBUG3(printk("%s: exiting normally\n", __func__));
1900 }
1901
1902 /* Restore state if a resync event occured during processing */
1903 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1904 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1905 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1906 if (test_bit(RSCN_UPDATE, &save_flags))
1907 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1908 }
1909
1910 return (rval);
1911}
1912
1913
1914
1915/*
1916 * qla2x00_configure_local_loop
1917 * Updates Fibre Channel Device Database with local loop devices.
1918 *
1919 * Input:
1920 * ha = adapter block pointer.
1921 *
1922 * Returns:
1923 * 0 = success.
1924 */
1925static int
fa2a1ce5 1926qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1da177e4
LT
1927{
1928 int rval, rval2;
1929 int found_devs;
1930 int found;
1931 fc_port_t *fcport, *new_fcport;
1932
1933 uint16_t index;
1934 uint16_t entries;
1935 char *id_iter;
1936 uint16_t loop_id;
1937 uint8_t domain, area, al_pa;
1938
1939 found_devs = 0;
1940 new_fcport = NULL;
1941 entries = MAX_FIBRE_DEVICES;
1942
1943 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1944 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1945
1946 /* Get list of logged in devices. */
1947 memset(ha->gid_list, 0, GID_LIST_SIZE);
1948 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1949 &entries);
1950 if (rval != QLA_SUCCESS)
1951 goto cleanup_allocation;
1952
1953 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1954 ha->host_no, entries));
1955 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1956 entries * sizeof(struct gid_list_info)));
1957
1958 /* Allocate temporary fcport for any new fcports discovered. */
1959 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1960 if (new_fcport == NULL) {
1961 rval = QLA_MEMORY_ALLOC_FAILED;
1962 goto cleanup_allocation;
1963 }
1964 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1965
1966 /*
1967 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1968 */
1969 list_for_each_entry(fcport, &ha->fcports, list) {
1970 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1971 fcport->port_type != FCT_BROADCAST &&
1972 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1973
1974 DEBUG(printk("scsi(%ld): Marking port lost, "
1975 "loop_id=0x%04x\n",
1976 ha->host_no, fcport->loop_id));
1977
1978 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1979 fcport->flags &= ~FCF_FARP_DONE;
1980 }
1981 }
1982
1983 /* Add devices to port list. */
1984 id_iter = (char *)ha->gid_list;
1985 for (index = 0; index < entries; index++) {
1986 domain = ((struct gid_list_info *)id_iter)->domain;
1987 area = ((struct gid_list_info *)id_iter)->area;
1988 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
abbd8870 1989 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1da177e4
LT
1990 loop_id = (uint16_t)
1991 ((struct gid_list_info *)id_iter)->loop_id_2100;
abbd8870 1992 else
1da177e4
LT
1993 loop_id = le16_to_cpu(
1994 ((struct gid_list_info *)id_iter)->loop_id);
abbd8870 1995 id_iter += ha->gid_list_info_size;
1da177e4
LT
1996
1997 /* Bypass reserved domain fields. */
1998 if ((domain & 0xf0) == 0xf0)
1999 continue;
2000
2001 /* Bypass if not same domain and area of adapter. */
f7d289f6
AV
2002 if (area && domain &&
2003 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
1da177e4
LT
2004 continue;
2005
2006 /* Bypass invalid local loop ID. */
2007 if (loop_id > LAST_LOCAL_LOOP_ID)
2008 continue;
2009
2010 /* Fill in member data. */
2011 new_fcport->d_id.b.domain = domain;
2012 new_fcport->d_id.b.area = area;
2013 new_fcport->d_id.b.al_pa = al_pa;
2014 new_fcport->loop_id = loop_id;
2015 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2016 if (rval2 != QLA_SUCCESS) {
2017 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2018 "information -- get_port_database=%x, "
2019 "loop_id=0x%04x\n",
2020 ha->host_no, rval2, new_fcport->loop_id));
c9d02acf
AV
2021 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2022 ha->host_no));
2023 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
1da177e4
LT
2024 continue;
2025 }
2026
2027 /* Check for matching device in port list. */
2028 found = 0;
2029 fcport = NULL;
2030 list_for_each_entry(fcport, &ha->fcports, list) {
2031 if (memcmp(new_fcport->port_name, fcport->port_name,
2032 WWN_SIZE))
2033 continue;
2034
2035 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2036 FCF_PERSISTENT_BOUND);
2037 fcport->loop_id = new_fcport->loop_id;
2038 fcport->port_type = new_fcport->port_type;
2039 fcport->d_id.b24 = new_fcport->d_id.b24;
2040 memcpy(fcport->node_name, new_fcport->node_name,
2041 WWN_SIZE);
2042
2043 found++;
2044 break;
2045 }
2046
2047 if (!found) {
2048 /* New device, add to fcports list. */
2049 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2050 list_add_tail(&new_fcport->list, &ha->fcports);
2051
2052 /* Allocate a new replacement fcport. */
2053 fcport = new_fcport;
2054 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2055 if (new_fcport == NULL) {
2056 rval = QLA_MEMORY_ALLOC_FAILED;
2057 goto cleanup_allocation;
2058 }
2059 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2060 }
2061
2062 qla2x00_update_fcport(ha, fcport);
2063
2064 found_devs++;
2065 }
2066
2067cleanup_allocation:
c9475cb0 2068 kfree(new_fcport);
1da177e4
LT
2069
2070 if (rval != QLA_SUCCESS) {
2071 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2072 "rval=%x\n", ha->host_no, rval));
2073 }
2074
2075 if (found_devs) {
2076 ha->device_flags |= DFLG_LOCAL_DEVICES;
2077 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2078 }
2079
2080 return (rval);
2081}
2082
2083static void
fa2a1ce5 2084qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
1da177e4
LT
2085{
2086 fc_port_t *fcport;
2087
d97994dc 2088 qla2x00_mark_all_devices_lost(ha, 0);
1da177e4
LT
2089 list_for_each_entry(fcport, &ha->fcports, list) {
2090 if (fcport->port_type != FCT_TARGET)
2091 continue;
2092
fa2a1ce5 2093 qla2x00_update_fcport(ha, fcport);
1da177e4
LT
2094 }
2095}
2096
2097/*
2098 * qla2x00_update_fcport
2099 * Updates device on list.
2100 *
2101 * Input:
2102 * ha = adapter block pointer.
2103 * fcport = port structure pointer.
2104 *
2105 * Return:
2106 * 0 - Success
2107 * BIT_0 - error
2108 *
2109 * Context:
2110 * Kernel context.
2111 */
052c40c8 2112void
1da177e4
LT
2113qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2114{
1da177e4
LT
2115 fcport->ha = ha;
2116 fcport->login_retry = 0;
2117 fcport->port_login_retry_count = ha->port_down_retry_count *
2118 PORT_RETRY_TIME;
2119 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2120 PORT_RETRY_TIME);
2121 fcport->flags &= ~FCF_LOGIN_NEEDED;
2122
1da177e4 2123 atomic_set(&fcport->state, FCS_ONLINE);
bdf79621 2124
8482e118
AV
2125 if (ha->flags.init_done)
2126 qla2x00_reg_remote_port(ha, fcport);
1da177e4
LT
2127}
2128
8482e118
AV
2129void
2130qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2131{
2132 struct fc_rport_identifiers rport_ids;
bdf79621 2133 struct fc_rport *rport;
d97994dc 2134 unsigned long flags;
8482e118 2135
d97994dc
AV
2136 if (fcport->drport)
2137 qla2x00_rport_del(fcport);
2138 if (fcport->rport)
2139 return;
8482e118 2140
f8b02a85
AV
2141 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2142 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e118
AV
2143 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2144 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
77d74143 2145 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
d97994dc 2146 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
77d74143
AV
2147 if (!rport) {
2148 qla_printk(KERN_WARNING, ha,
2149 "Unable to allocate fc remote port!\n");
2150 return;
2151 }
d97994dc
AV
2152 spin_lock_irqsave(&fcport->rport_lock, flags);
2153 fcport->rport = rport;
19a7b4ae 2154 *((fc_port_t **)rport->dd_data) = fcport;
d97994dc
AV
2155 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2156
ad3e0eda 2157 rport->supported_classes = fcport->supported_classes;
77d74143 2158
8482e118
AV
2159 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2160 if (fcport->port_type == FCT_INITIATOR)
2161 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2162 if (fcport->port_type == FCT_TARGET)
2163 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
77d74143 2164 fc_remote_port_rolechg(rport, rport_ids.roles);
1da177e4 2165
cc4731f5
AV
2166 if (rport->scsi_target_id != -1 &&
2167 rport->scsi_target_id < ha->host->max_id)
bdf79621 2168 fcport->os_target_id = rport->scsi_target_id;
1da177e4
LT
2169}
2170
1da177e4
LT
2171/*
2172 * qla2x00_configure_fabric
2173 * Setup SNS devices with loop ID's.
2174 *
2175 * Input:
2176 * ha = adapter block pointer.
2177 *
2178 * Returns:
2179 * 0 = success.
2180 * BIT_0 = error
2181 */
2182static int
2183qla2x00_configure_fabric(scsi_qla_host_t *ha)
2184{
2185 int rval, rval2;
2186 fc_port_t *fcport, *fcptemp;
2187 uint16_t next_loopid;
2188 uint16_t mb[MAILBOX_REGISTER_COUNT];
0107109e 2189 uint16_t loop_id;
1da177e4
LT
2190 LIST_HEAD(new_fcports);
2191
2192 /* If FL port exists, then SNS is present */
044cc6c8 2193 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
0107109e
AV
2194 loop_id = NPH_F_PORT;
2195 else
2196 loop_id = SNS_FL_PORT;
2197 rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
1da177e4
LT
2198 if (rval != QLA_SUCCESS) {
2199 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2200 "Port\n", ha->host_no));
2201
2202 ha->device_flags &= ~SWITCH_FOUND;
2203 return (QLA_SUCCESS);
2204 }
2205
2206 /* Mark devices that need re-synchronization. */
2207 rval2 = qla2x00_device_resync(ha);
2208 if (rval2 == QLA_RSCNS_HANDLED) {
2209 /* No point doing the scan, just continue. */
2210 return (QLA_SUCCESS);
2211 }
2212 do {
cca5335c
AV
2213 /* FDMI support. */
2214 if (ql2xfdmienable &&
2215 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2216 qla2x00_fdmi_register(ha);
2217
1da177e4 2218 /* Ensure we are logged into the SNS. */
044cc6c8 2219 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
0107109e
AV
2220 loop_id = NPH_SNS;
2221 else
2222 loop_id = SIMPLE_NAME_SERVER;
2223 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
abbd8870 2224 0xfc, mb, BIT_1 | BIT_0);
1da177e4
LT
2225 if (mb[0] != MBS_COMMAND_COMPLETE) {
2226 DEBUG2(qla_printk(KERN_INFO, ha,
2227 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
0107109e 2228 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
1da177e4
LT
2229 mb[0], mb[1], mb[2], mb[6], mb[7]));
2230 return (QLA_SUCCESS);
2231 }
2232
2233 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2234 if (qla2x00_rft_id(ha)) {
2235 /* EMPTY */
2236 DEBUG2(printk("scsi(%ld): Register FC-4 "
2237 "TYPE failed.\n", ha->host_no));
2238 }
2239 if (qla2x00_rff_id(ha)) {
2240 /* EMPTY */
2241 DEBUG2(printk("scsi(%ld): Register FC-4 "
2242 "Features failed.\n", ha->host_no));
2243 }
2244 if (qla2x00_rnn_id(ha)) {
2245 /* EMPTY */
2246 DEBUG2(printk("scsi(%ld): Register Node Name "
2247 "failed.\n", ha->host_no));
2248 } else if (qla2x00_rsnn_nn(ha)) {
2249 /* EMPTY */
2250 DEBUG2(printk("scsi(%ld): Register Symbolic "
2251 "Node Name failed.\n", ha->host_no));
2252 }
2253 }
2254
2255 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2256 if (rval != QLA_SUCCESS)
2257 break;
2258
2259 /*
2260 * Logout all previous fabric devices marked lost, except
2261 * tape devices.
2262 */
2263 list_for_each_entry(fcport, &ha->fcports, list) {
2264 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2265 break;
2266
2267 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2268 continue;
2269
2270 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2271 qla2x00_mark_device_lost(ha, fcport,
d97994dc 2272 ql2xplogiabsentdevice, 0);
1da177e4
LT
2273 if (fcport->loop_id != FC_NO_LOOP_ID &&
2274 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2275 fcport->port_type != FCT_INITIATOR &&
2276 fcport->port_type != FCT_BROADCAST) {
abbd8870 2277 ha->isp_ops.fabric_logout(ha,
1c7c6357
AV
2278 fcport->loop_id,
2279 fcport->d_id.b.domain,
2280 fcport->d_id.b.area,
2281 fcport->d_id.b.al_pa);
1da177e4
LT
2282 fcport->loop_id = FC_NO_LOOP_ID;
2283 }
2284 }
2285 }
2286
2287 /* Starting free loop ID. */
2288 next_loopid = ha->min_external_loopid;
2289
2290 /*
2291 * Scan through our port list and login entries that need to be
2292 * logged in.
2293 */
2294 list_for_each_entry(fcport, &ha->fcports, list) {
2295 if (atomic_read(&ha->loop_down_timer) ||
2296 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2297 break;
2298
2299 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2300 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2301 continue;
2302
2303 if (fcport->loop_id == FC_NO_LOOP_ID) {
2304 fcport->loop_id = next_loopid;
2305 rval = qla2x00_find_new_loop_id(ha, fcport);
2306 if (rval != QLA_SUCCESS) {
2307 /* Ran out of IDs to use */
2308 break;
2309 }
2310 }
1da177e4
LT
2311 /* Login and update database */
2312 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2313 }
2314
2315 /* Exit if out of loop IDs. */
2316 if (rval != QLA_SUCCESS) {
2317 break;
2318 }
2319
2320 /*
2321 * Login and add the new devices to our port list.
2322 */
2323 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2324 if (atomic_read(&ha->loop_down_timer) ||
2325 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2326 break;
2327
2328 /* Find a new loop ID to use. */
2329 fcport->loop_id = next_loopid;
2330 rval = qla2x00_find_new_loop_id(ha, fcport);
2331 if (rval != QLA_SUCCESS) {
2332 /* Ran out of IDs to use */
2333 break;
2334 }
2335
1da177e4
LT
2336 /* Remove device from the new list and add it to DB */
2337 list_del(&fcport->list);
2338 list_add_tail(&fcport->list, &ha->fcports);
bdf79621
AV
2339
2340 /* Login and update database */
2341 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
1da177e4
LT
2342 }
2343 } while (0);
2344
2345 /* Free all new device structures not processed. */
2346 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2347 list_del(&fcport->list);
2348 kfree(fcport);
2349 }
2350
2351 if (rval) {
2352 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2353 "rval=%d\n", ha->host_no, rval));
2354 }
2355
2356 return (rval);
2357}
2358
2359
2360/*
2361 * qla2x00_find_all_fabric_devs
2362 *
2363 * Input:
2364 * ha = adapter block pointer.
2365 * dev = database device entry pointer.
2366 *
2367 * Returns:
2368 * 0 = success.
2369 *
2370 * Context:
2371 * Kernel context.
2372 */
2373static int
2374qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2375{
2376 int rval;
2377 uint16_t loop_id;
2378 fc_port_t *fcport, *new_fcport, *fcptemp;
2379 int found;
2380
2381 sw_info_t *swl;
2382 int swl_idx;
2383 int first_dev, last_dev;
2384 port_id_t wrap, nxt_d_id;
2385
2386 rval = QLA_SUCCESS;
2387
2388 /* Try GID_PT to get device list, else GAN. */
2389 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2390 if (swl == NULL) {
2391 /*EMPTY*/
2392 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2393 "on GA_NXT\n", ha->host_no));
2394 } else {
2395 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2396 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2397 kfree(swl);
2398 swl = NULL;
2399 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2400 kfree(swl);
2401 swl = NULL;
2402 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2403 kfree(swl);
2404 swl = NULL;
2405 }
2406 }
2407 swl_idx = 0;
2408
2409 /* Allocate temporary fcport for any new fcports discovered. */
2410 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2411 if (new_fcport == NULL) {
c9475cb0 2412 kfree(swl);
1da177e4
LT
2413 return (QLA_MEMORY_ALLOC_FAILED);
2414 }
2415 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2416
2417 /* Set start port ID scan at adapter ID. */
2418 first_dev = 1;
2419 last_dev = 0;
2420
2421 /* Starting free loop ID. */
2422 loop_id = ha->min_external_loopid;
1da177e4 2423 for (; loop_id <= ha->last_loop_id; loop_id++) {
3d71644c 2424 if (qla2x00_is_reserved_id(ha, loop_id))
1da177e4
LT
2425 continue;
2426
23443b1d 2427 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
1da177e4
LT
2428 break;
2429
2430 if (swl != NULL) {
2431 if (last_dev) {
2432 wrap.b24 = new_fcport->d_id.b24;
2433 } else {
2434 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2435 memcpy(new_fcport->node_name,
2436 swl[swl_idx].node_name, WWN_SIZE);
2437 memcpy(new_fcport->port_name,
2438 swl[swl_idx].port_name, WWN_SIZE);
2439
2440 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2441 last_dev = 1;
2442 }
2443 swl_idx++;
2444 }
2445 } else {
2446 /* Send GA_NXT to the switch */
2447 rval = qla2x00_ga_nxt(ha, new_fcport);
2448 if (rval != QLA_SUCCESS) {
2449 qla_printk(KERN_WARNING, ha,
2450 "SNS scan failed -- assuming zero-entry "
2451 "result...\n");
2452 list_for_each_entry_safe(fcport, fcptemp,
2453 new_fcports, list) {
2454 list_del(&fcport->list);
2455 kfree(fcport);
2456 }
2457 rval = QLA_SUCCESS;
2458 break;
2459 }
2460 }
2461
2462 /* If wrap on switch device list, exit. */
2463 if (first_dev) {
2464 wrap.b24 = new_fcport->d_id.b24;
2465 first_dev = 0;
2466 } else if (new_fcport->d_id.b24 == wrap.b24) {
2467 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2468 ha->host_no, new_fcport->d_id.b.domain,
2469 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2470 break;
2471 }
2472
2473 /* Bypass if host adapter. */
2474 if (new_fcport->d_id.b24 == ha->d_id.b24)
2475 continue;
2476
f7d289f6
AV
2477 /* Bypass if same domain and area of adapter. */
2478 if (((new_fcport->d_id.b24 & 0xffff00) ==
2479 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2480 ISP_CFG_FL)
2481 continue;
2482
1da177e4
LT
2483 /* Bypass reserved domain fields. */
2484 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2485 continue;
2486
2487 /* Locate matching device in database. */
2488 found = 0;
2489 list_for_each_entry(fcport, &ha->fcports, list) {
2490 if (memcmp(new_fcport->port_name, fcport->port_name,
2491 WWN_SIZE))
2492 continue;
2493
2494 found++;
2495
2496 /*
2497 * If address the same and state FCS_ONLINE, nothing
2498 * changed.
2499 */
2500 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2501 atomic_read(&fcport->state) == FCS_ONLINE) {
2502 break;
2503 }
2504
2505 /*
2506 * If device was not a fabric device before.
2507 */
2508 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2509 fcport->d_id.b24 = new_fcport->d_id.b24;
2510 fcport->loop_id = FC_NO_LOOP_ID;
2511 fcport->flags |= (FCF_FABRIC_DEVICE |
2512 FCF_LOGIN_NEEDED);
2513 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2514 break;
2515 }
2516
2517 /*
2518 * Port ID changed or device was marked to be updated;
2519 * Log it out if still logged in and mark it for
2520 * relogin later.
2521 */
2522 fcport->d_id.b24 = new_fcport->d_id.b24;
2523 fcport->flags |= FCF_LOGIN_NEEDED;
2524 if (fcport->loop_id != FC_NO_LOOP_ID &&
2525 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2526 fcport->port_type != FCT_INITIATOR &&
2527 fcport->port_type != FCT_BROADCAST) {
1c7c6357
AV
2528 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2529 fcport->d_id.b.domain, fcport->d_id.b.area,
2530 fcport->d_id.b.al_pa);
1da177e4
LT
2531 fcport->loop_id = FC_NO_LOOP_ID;
2532 }
2533
2534 break;
2535 }
2536
2537 if (found)
2538 continue;
2539
2540 /* If device was not in our fcports list, then add it. */
2541 list_add_tail(&new_fcport->list, new_fcports);
2542
2543 /* Allocate a new replacement fcport. */
2544 nxt_d_id.b24 = new_fcport->d_id.b24;
2545 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2546 if (new_fcport == NULL) {
c9475cb0 2547 kfree(swl);
1da177e4
LT
2548 return (QLA_MEMORY_ALLOC_FAILED);
2549 }
2550 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2551 new_fcport->d_id.b24 = nxt_d_id.b24;
2552 }
2553
c9475cb0
JJ
2554 kfree(swl);
2555 kfree(new_fcport);
1da177e4
LT
2556
2557 if (!list_empty(new_fcports))
2558 ha->device_flags |= DFLG_FABRIC_DEVICES;
2559
2560 return (rval);
2561}
2562
2563/*
2564 * qla2x00_find_new_loop_id
2565 * Scan through our port list and find a new usable loop ID.
2566 *
2567 * Input:
2568 * ha: adapter state pointer.
2569 * dev: port structure pointer.
2570 *
2571 * Returns:
2572 * qla2x00 local function return status code.
2573 *
2574 * Context:
2575 * Kernel context.
2576 */
2577int
2578qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2579{
2580 int rval;
2581 int found;
2582 fc_port_t *fcport;
2583 uint16_t first_loop_id;
2584
2585 rval = QLA_SUCCESS;
2586
2587 /* Save starting loop ID. */
2588 first_loop_id = dev->loop_id;
2589
2590 for (;;) {
2591 /* Skip loop ID if already used by adapter. */
2592 if (dev->loop_id == ha->loop_id) {
2593 dev->loop_id++;
2594 }
2595
2596 /* Skip reserved loop IDs. */
3d71644c 2597 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
1da177e4
LT
2598 dev->loop_id++;
2599 }
2600
2601 /* Reset loop ID if passed the end. */
2602 if (dev->loop_id > ha->last_loop_id) {
2603 /* first loop ID. */
2604 dev->loop_id = ha->min_external_loopid;
2605 }
2606
2607 /* Check for loop ID being already in use. */
2608 found = 0;
2609 fcport = NULL;
2610 list_for_each_entry(fcport, &ha->fcports, list) {
2611 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2612 /* ID possibly in use */
2613 found++;
2614 break;
2615 }
2616 }
2617
2618 /* If not in use then it is free to use. */
2619 if (!found) {
2620 break;
2621 }
2622
2623 /* ID in use. Try next value. */
2624 dev->loop_id++;
2625
2626 /* If wrap around. No free ID to use. */
2627 if (dev->loop_id == first_loop_id) {
2628 dev->loop_id = FC_NO_LOOP_ID;
2629 rval = QLA_FUNCTION_FAILED;
2630 break;
2631 }
2632 }
2633
2634 return (rval);
2635}
2636
2637/*
2638 * qla2x00_device_resync
2639 * Marks devices in the database that needs resynchronization.
2640 *
2641 * Input:
2642 * ha = adapter block pointer.
2643 *
2644 * Context:
2645 * Kernel context.
2646 */
2647static int
fa2a1ce5 2648qla2x00_device_resync(scsi_qla_host_t *ha)
1da177e4
LT
2649{
2650 int rval;
1da177e4
LT
2651 uint32_t mask;
2652 fc_port_t *fcport;
2653 uint32_t rscn_entry;
2654 uint8_t rscn_out_iter;
2655 uint8_t format;
2656 port_id_t d_id;
2657
2658 rval = QLA_RSCNS_HANDLED;
2659
2660 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2661 ha->flags.rscn_queue_overflow) {
2662
2663 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2664 format = MSB(MSW(rscn_entry));
2665 d_id.b.domain = LSB(MSW(rscn_entry));
2666 d_id.b.area = MSB(LSW(rscn_entry));
2667 d_id.b.al_pa = LSB(LSW(rscn_entry));
2668
2669 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2670 "[%02x/%02x%02x%02x].\n",
2671 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2672 d_id.b.area, d_id.b.al_pa));
2673
2674 ha->rscn_out_ptr++;
2675 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2676 ha->rscn_out_ptr = 0;
2677
2678 /* Skip duplicate entries. */
2679 for (rscn_out_iter = ha->rscn_out_ptr;
2680 !ha->flags.rscn_queue_overflow &&
2681 rscn_out_iter != ha->rscn_in_ptr;
2682 rscn_out_iter = (rscn_out_iter ==
2683 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2684
2685 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2686 break;
2687
2688 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2689 "entry found at [%d].\n", ha->host_no,
2690 rscn_out_iter));
2691
2692 ha->rscn_out_ptr = rscn_out_iter;
2693 }
2694
2695 /* Queue overflow, set switch default case. */
2696 if (ha->flags.rscn_queue_overflow) {
2697 DEBUG(printk("scsi(%ld): device_resync: rscn "
2698 "overflow.\n", ha->host_no));
2699
2700 format = 3;
2701 ha->flags.rscn_queue_overflow = 0;
2702 }
2703
2704 switch (format) {
2705 case 0:
1da177e4
LT
2706 mask = 0xffffff;
2707 break;
2708 case 1:
2709 mask = 0xffff00;
2710 break;
2711 case 2:
2712 mask = 0xff0000;
2713 break;
2714 default:
2715 mask = 0x0;
2716 d_id.b24 = 0;
2717 ha->rscn_out_ptr = ha->rscn_in_ptr;
2718 break;
2719 }
2720
2721 rval = QLA_SUCCESS;
2722
1da177e4
LT
2723 list_for_each_entry(fcport, &ha->fcports, list) {
2724 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2725 (fcport->d_id.b24 & mask) != d_id.b24 ||
2726 fcport->port_type == FCT_BROADCAST)
2727 continue;
2728
2729 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2730 if (format != 3 ||
2731 fcport->port_type != FCT_INITIATOR) {
d97994dc
AV
2732 qla2x00_mark_device_lost(ha, fcport,
2733 0, 0);
1da177e4
LT
2734 }
2735 }
2736 fcport->flags &= ~FCF_FARP_DONE;
2737 }
2738 }
2739 return (rval);
2740}
2741
2742/*
2743 * qla2x00_fabric_dev_login
2744 * Login fabric target device and update FC port database.
2745 *
2746 * Input:
2747 * ha: adapter state pointer.
2748 * fcport: port structure list pointer.
2749 * next_loopid: contains value of a new loop ID that can be used
2750 * by the next login attempt.
2751 *
2752 * Returns:
2753 * qla2x00 local function return status code.
2754 *
2755 * Context:
2756 * Kernel context.
2757 */
2758static int
2759qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2760 uint16_t *next_loopid)
2761{
2762 int rval;
2763 int retry;
0107109e 2764 uint8_t opts;
1da177e4
LT
2765
2766 rval = QLA_SUCCESS;
2767 retry = 0;
2768
2769 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2770 if (rval == QLA_SUCCESS) {
0107109e
AV
2771 /* Send an ADISC to tape devices.*/
2772 opts = 0;
2773 if (fcport->flags & FCF_TAPE_PRESENT)
2774 opts |= BIT_1;
2775 rval = qla2x00_get_port_database(ha, fcport, opts);
1da177e4 2776 if (rval != QLA_SUCCESS) {
1c7c6357
AV
2777 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2778 fcport->d_id.b.domain, fcport->d_id.b.area,
2779 fcport->d_id.b.al_pa);
d97994dc 2780 qla2x00_mark_device_lost(ha, fcport, 1, 0);
1da177e4
LT
2781 } else {
2782 qla2x00_update_fcport(ha, fcport);
2783 }
2784 }
2785
2786 return (rval);
2787}
2788
2789/*
2790 * qla2x00_fabric_login
2791 * Issue fabric login command.
2792 *
2793 * Input:
2794 * ha = adapter block pointer.
2795 * device = pointer to FC device type structure.
2796 *
2797 * Returns:
2798 * 0 - Login successfully
2799 * 1 - Login failed
2800 * 2 - Initiator device
2801 * 3 - Fatal error
2802 */
2803int
2804qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2805 uint16_t *next_loopid)
2806{
2807 int rval;
2808 int retry;
2809 uint16_t tmp_loopid;
2810 uint16_t mb[MAILBOX_REGISTER_COUNT];
2811
2812 retry = 0;
2813 tmp_loopid = 0;
2814
2815 for (;;) {
2816 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2817 "for port %02x%02x%02x.\n",
2818 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2819 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2820
2821 /* Login fcport on switch. */
abbd8870 2822 ha->isp_ops.fabric_login(ha, fcport->loop_id,
1da177e4
LT
2823 fcport->d_id.b.domain, fcport->d_id.b.area,
2824 fcport->d_id.b.al_pa, mb, BIT_0);
2825 if (mb[0] == MBS_PORT_ID_USED) {
2826 /*
2827 * Device has another loop ID. The firmware team
0107109e
AV
2828 * recommends the driver perform an implicit login with
2829 * the specified ID again. The ID we just used is save
2830 * here so we return with an ID that can be tried by
2831 * the next login.
1da177e4
LT
2832 */
2833 retry++;
2834 tmp_loopid = fcport->loop_id;
2835 fcport->loop_id = mb[1];
2836
2837 DEBUG(printk("Fabric Login: port in use - next "
2838 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2839 fcport->loop_id, fcport->d_id.b.domain,
2840 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2841
2842 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2843 /*
2844 * Login succeeded.
2845 */
2846 if (retry) {
2847 /* A retry occurred before. */
2848 *next_loopid = tmp_loopid;
2849 } else {
2850 /*
2851 * No retry occurred before. Just increment the
2852 * ID value for next login.
2853 */
2854 *next_loopid = (fcport->loop_id + 1);
2855 }
2856
2857 if (mb[1] & BIT_0) {
2858 fcport->port_type = FCT_INITIATOR;
2859 } else {
2860 fcport->port_type = FCT_TARGET;
2861 if (mb[1] & BIT_1) {
2862 fcport->flags |= FCF_TAPE_PRESENT;
2863 }
2864 }
2865
ad3e0eda
AV
2866 if (mb[10] & BIT_0)
2867 fcport->supported_classes |= FC_COS_CLASS2;
2868 if (mb[10] & BIT_1)
2869 fcport->supported_classes |= FC_COS_CLASS3;
2870
1da177e4
LT
2871 rval = QLA_SUCCESS;
2872 break;
2873 } else if (mb[0] == MBS_LOOP_ID_USED) {
2874 /*
2875 * Loop ID already used, try next loop ID.
2876 */
2877 fcport->loop_id++;
2878 rval = qla2x00_find_new_loop_id(ha, fcport);
2879 if (rval != QLA_SUCCESS) {
2880 /* Ran out of loop IDs to use */
2881 break;
2882 }
2883 } else if (mb[0] == MBS_COMMAND_ERROR) {
2884 /*
2885 * Firmware possibly timed out during login. If NO
2886 * retries are left to do then the device is declared
2887 * dead.
2888 */
2889 *next_loopid = fcport->loop_id;
1c7c6357
AV
2890 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2891 fcport->d_id.b.domain, fcport->d_id.b.area,
2892 fcport->d_id.b.al_pa);
d97994dc 2893 qla2x00_mark_device_lost(ha, fcport, 1, 0);
1da177e4
LT
2894
2895 rval = 1;
2896 break;
2897 } else {
2898 /*
2899 * unrecoverable / not handled error
2900 */
2901 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
fa2a1ce5
AV
2902 "loop_id=%x jiffies=%lx.\n",
2903 __func__, ha->host_no, mb[0],
1da177e4
LT
2904 fcport->d_id.b.domain, fcport->d_id.b.area,
2905 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2906
2907 *next_loopid = fcport->loop_id;
1c7c6357
AV
2908 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2909 fcport->d_id.b.domain, fcport->d_id.b.area,
2910 fcport->d_id.b.al_pa);
1da177e4 2911 fcport->loop_id = FC_NO_LOOP_ID;
0eedfcf0 2912 fcport->login_retry = 0;
1da177e4
LT
2913
2914 rval = 3;
2915 break;
2916 }
2917 }
2918
2919 return (rval);
2920}
2921
2922/*
2923 * qla2x00_local_device_login
2924 * Issue local device login command.
2925 *
2926 * Input:
2927 * ha = adapter block pointer.
2928 * loop_id = loop id of device to login to.
2929 *
2930 * Returns (Where's the #define!!!!):
2931 * 0 - Login successfully
2932 * 1 - Login failed
2933 * 3 - Fatal error
2934 */
2935int
9a52a57c 2936qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
1da177e4
LT
2937{
2938 int rval;
2939 uint16_t mb[MAILBOX_REGISTER_COUNT];
2940
2941 memset(mb, 0, sizeof(mb));
9a52a57c 2942 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
1da177e4
LT
2943 if (rval == QLA_SUCCESS) {
2944 /* Interrogate mailbox registers for any errors */
2945 if (mb[0] == MBS_COMMAND_ERROR)
2946 rval = 1;
2947 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2948 /* device not in PCB table */
2949 rval = 3;
2950 }
2951
2952 return (rval);
2953}
2954
2955/*
2956 * qla2x00_loop_resync
2957 * Resync with fibre channel devices.
2958 *
2959 * Input:
2960 * ha = adapter block pointer.
2961 *
2962 * Returns:
2963 * 0 = success
2964 */
2965int
fa2a1ce5 2966qla2x00_loop_resync(scsi_qla_host_t *ha)
1da177e4
LT
2967{
2968 int rval;
2969 uint32_t wait_time;
2970
2971 rval = QLA_SUCCESS;
2972
2973 atomic_set(&ha->loop_state, LOOP_UPDATE);
1da177e4
LT
2974 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2975 if (ha->flags.online) {
2976 if (!(rval = qla2x00_fw_ready(ha))) {
2977 /* Wait at most MAX_TARGET RSCNs for a stable link. */
2978 wait_time = 256;
2979 do {
1da177e4
LT
2980 atomic_set(&ha->loop_state, LOOP_UPDATE);
2981
0107109e
AV
2982 /* Issue a marker after FW becomes ready. */
2983 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2984 ha->marker_needed = 0;
1da177e4
LT
2985
2986 /* Remap devices on Loop. */
2987 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2988
2989 qla2x00_configure_loop(ha);
2990 wait_time--;
2991 } while (!atomic_read(&ha->loop_down_timer) &&
2992 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2993 wait_time &&
2994 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2995 }
1da177e4
LT
2996 }
2997
2998 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2999 return (QLA_FUNCTION_FAILED);
3000 }
3001
3002 if (rval) {
3003 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3004 }
3005
3006 return (rval);
3007}
3008
1da177e4
LT
3009void
3010qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3011{
3012 int rescan_done;
3013 fc_port_t *fcport;
3014
3015 rescan_done = 0;
3016 list_for_each_entry(fcport, &ha->fcports, list) {
3017 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3018 continue;
3019
3020 qla2x00_update_fcport(ha, fcport);
3021 fcport->flags &= ~FCF_RESCAN_NEEDED;
3022
3023 rescan_done = 1;
3024 }
fa2a1ce5 3025 qla2x00_probe_for_all_luns(ha);
1da177e4
LT
3026}
3027
d97994dc
AV
3028void
3029qla2x00_update_fcports(scsi_qla_host_t *ha)
3030{
3031 fc_port_t *fcport;
3032
3033 /* Go with deferred removal of rport references. */
3034 list_for_each_entry(fcport, &ha->fcports, list)
3035 if (fcport->drport)
3036 qla2x00_rport_del(fcport);
3037}
3038
1da177e4
LT
3039/*
3040* qla2x00_abort_isp
3041* Resets ISP and aborts all outstanding commands.
3042*
3043* Input:
3044* ha = adapter block pointer.
3045*
3046* Returns:
3047* 0 = success
3048*/
3049int
3050qla2x00_abort_isp(scsi_qla_host_t *ha)
3051{
3052 unsigned long flags = 0;
3053 uint16_t cnt;
3054 srb_t *sp;
3055 uint8_t status = 0;
3056
3057 if (ha->flags.online) {
3058 ha->flags.online = 0;
3059 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1da177e4
LT
3060
3061 qla_printk(KERN_INFO, ha,
3062 "Performing ISP error recovery - ha= %p.\n", ha);
abbd8870 3063 ha->isp_ops.reset_chip(ha);
1da177e4
LT
3064
3065 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3066 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3067 atomic_set(&ha->loop_state, LOOP_DOWN);
d97994dc 3068 qla2x00_mark_all_devices_lost(ha, 0);
1da177e4
LT
3069 } else {
3070 if (!atomic_read(&ha->loop_down_timer))
3071 atomic_set(&ha->loop_down_timer,
3072 LOOP_DOWN_TIME);
3073 }
3074
3075 spin_lock_irqsave(&ha->hardware_lock, flags);
3076 /* Requeue all commands in outstanding command list. */
3077 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3078 sp = ha->outstanding_cmds[cnt];
3079 if (sp) {
3080 ha->outstanding_cmds[cnt] = NULL;
1da177e4 3081 sp->flags = 0;
f4f051eb 3082 sp->cmd->result = DID_RESET << 16;
1da177e4 3083 sp->cmd->host_scribble = (unsigned char *)NULL;
f4f051eb 3084 qla2x00_sp_compl(ha, sp);
1da177e4
LT
3085 }
3086 }
3087 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3088
abbd8870 3089 ha->isp_ops.nvram_config(ha);
1da177e4
LT
3090
3091 if (!qla2x00_restart_isp(ha)) {
3092 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3093
3094 if (!atomic_read(&ha->loop_down_timer)) {
3095 /*
3096 * Issue marker command only when we are going
3097 * to start the I/O .
3098 */
3099 ha->marker_needed = 1;
3100 }
3101
3102 ha->flags.online = 1;
3103
abbd8870 3104 ha->isp_ops.enable_intrs(ha);
1da177e4 3105
fa2a1ce5 3106 ha->isp_abort_cnt = 0;
1da177e4
LT
3107 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3108 } else { /* failed the ISP abort */
3109 ha->flags.online = 1;
3110 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3111 if (ha->isp_abort_cnt == 0) {
3112 qla_printk(KERN_WARNING, ha,
3113 "ISP error recovery failed - "
3114 "board disabled\n");
fa2a1ce5 3115 /*
1da177e4
LT
3116 * The next call disables the board
3117 * completely.
3118 */
abbd8870 3119 ha->isp_ops.reset_adapter(ha);
1da177e4
LT
3120 ha->flags.online = 0;
3121 clear_bit(ISP_ABORT_RETRY,
3122 &ha->dpc_flags);
3123 status = 0;
3124 } else { /* schedule another ISP abort */
3125 ha->isp_abort_cnt--;
3126 DEBUG(printk("qla%ld: ISP abort - "
0107109e 3127 "retry remaining %d\n",
1da177e4
LT
3128 ha->host_no, ha->isp_abort_cnt);)
3129 status = 1;
3130 }
3131 } else {
3132 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3133 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3134 "- retrying (%d) more times\n",
3135 ha->host_no, ha->isp_abort_cnt);)
3136 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3137 status = 1;
3138 }
3139 }
fa2a1ce5 3140
1da177e4
LT
3141 }
3142
3143 if (status) {
3144 qla_printk(KERN_INFO, ha,
3145 "qla2x00_abort_isp: **** FAILED ****\n");
3146 } else {
3147 DEBUG(printk(KERN_INFO
3148 "qla2x00_abort_isp(%ld): exiting.\n",
3149 ha->host_no);)
3150 }
3151
3152 return(status);
3153}
3154
3155/*
3156* qla2x00_restart_isp
3157* restarts the ISP after a reset
3158*
3159* Input:
3160* ha = adapter block pointer.
3161*
3162* Returns:
3163* 0 = success
3164*/
3165static int
3166qla2x00_restart_isp(scsi_qla_host_t *ha)
3167{
3168 uint8_t status = 0;
3d71644c 3169 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4
LT
3170 unsigned long flags = 0;
3171 uint32_t wait_time;
3172
3173 /* If firmware needs to be loaded */
3174 if (qla2x00_isp_firmware(ha)) {
3175 ha->flags.online = 0;
abbd8870 3176 if (!(status = ha->isp_ops.chip_diag(ha))) {
1da177e4
LT
3177 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3178 status = qla2x00_setup_chip(ha);
3179 goto done;
3180 }
3181
1da177e4
LT
3182 spin_lock_irqsave(&ha->hardware_lock, flags);
3183
044cc6c8 3184 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
0107109e
AV
3185 /*
3186 * Disable SRAM, Instruction RAM and GP RAM
3187 * parity.
3188 */
3189 WRT_REG_WORD(&reg->hccr,
3190 (HCCR_ENABLE_PARITY + 0x0));
3191 RD_REG_WORD(&reg->hccr);
3192 }
1da177e4
LT
3193
3194 spin_unlock_irqrestore(&ha->hardware_lock, flags);
fa2a1ce5 3195
1da177e4
LT
3196 status = qla2x00_setup_chip(ha);
3197
3198 spin_lock_irqsave(&ha->hardware_lock, flags);
fa2a1ce5 3199
044cc6c8 3200 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
0107109e
AV
3201 /* Enable proper parity */
3202 if (IS_QLA2300(ha))
3203 /* SRAM parity */
3204 WRT_REG_WORD(&reg->hccr,
3205 (HCCR_ENABLE_PARITY + 0x1));
3206 else
3207 /*
3208 * SRAM, Instruction RAM and GP RAM
3209 * parity.
3210 */
3211 WRT_REG_WORD(&reg->hccr,
3212 (HCCR_ENABLE_PARITY + 0x7));
3213 RD_REG_WORD(&reg->hccr);
3214 }
1da177e4
LT
3215
3216 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3217 }
3218 }
3219
3220 done:
3221 if (!status && !(status = qla2x00_init_rings(ha))) {
3222 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3223 if (!(status = qla2x00_fw_ready(ha))) {
3224 DEBUG(printk("%s(): Start configure loop, "
0107109e
AV
3225 "status = %d\n", __func__, status);)
3226
3227 /* Issue a marker after FW becomes ready. */
3228 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3229
1da177e4
LT
3230 ha->flags.online = 1;
3231 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3232 wait_time = 256;
3233 do {
3234 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3235 qla2x00_configure_loop(ha);
3236 wait_time--;
3237 } while (!atomic_read(&ha->loop_down_timer) &&
3238 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3239 wait_time &&
3240 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3241 }
3242
3243 /* if no cable then assume it's good */
fa2a1ce5 3244 if ((ha->device_flags & DFLG_NO_CABLE))
1da177e4
LT
3245 status = 0;
3246
3247 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3248 __func__,
3249 status);)
3250 }
3251 return (status);
3252}
3253
3254/*
3255* qla2x00_reset_adapter
3256* Reset adapter.
3257*
3258* Input:
3259* ha = adapter block pointer.
3260*/
abbd8870 3261void
1da177e4
LT
3262qla2x00_reset_adapter(scsi_qla_host_t *ha)
3263{
3264 unsigned long flags = 0;
3d71644c 3265 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4
LT
3266
3267 ha->flags.online = 0;
abbd8870 3268 ha->isp_ops.disable_intrs(ha);
1da177e4 3269
1da177e4
LT
3270 spin_lock_irqsave(&ha->hardware_lock, flags);
3271 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3272 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3273 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3274 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3275 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3276}
0107109e
AV
3277
3278void
3279qla24xx_reset_adapter(scsi_qla_host_t *ha)
3280{
3281 unsigned long flags = 0;
3282 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3283
3284 ha->flags.online = 0;
3285 ha->isp_ops.disable_intrs(ha);
3286
3287 spin_lock_irqsave(&ha->hardware_lock, flags);
3288 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3289 RD_REG_DWORD(&reg->hccr);
3290 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3291 RD_REG_DWORD(&reg->hccr);
3292 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3293}
3294
3295int
3296qla24xx_nvram_config(scsi_qla_host_t *ha)
3297{
3298 int rval;
3299 struct init_cb_24xx *icb;
3300 struct nvram_24xx *nv;
3301 uint32_t *dptr;
3302 uint8_t *dptr1, *dptr2;
3303 uint32_t chksum;
3304 uint16_t cnt;
3305
3306 rval = QLA_SUCCESS;
3307 icb = (struct init_cb_24xx *)ha->init_cb;
3308 nv = (struct nvram_24xx *)ha->request_ring;
3309
3310 /* Determine NVRAM starting address. */
3311 ha->nvram_size = sizeof(struct nvram_24xx);
3312 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6f641790
AV
3313 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3314 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3315 if (PCI_FUNC(ha->pdev->devfn)) {
0107109e 3316 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
6f641790
AV
3317 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3318 }
0107109e
AV
3319
3320 /* Get NVRAM data and calculate checksum. */
3321 dptr = (uint32_t *)nv;
3322 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3323 ha->nvram_size);
3324 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3325 chksum += le32_to_cpu(*dptr++);
3326
3327 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3328 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3329 ha->nvram_size));
3330
3331 /* Bad NVRAM data, set defaults parameters. */
3332 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3333 || nv->id[3] != ' ' ||
3334 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3335 /* Reset NVRAM data. */
3336 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3337 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3338 le16_to_cpu(nv->nvram_version));
3339 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3340 "invalid -- WWPN) defaults.\n");
3341
3342 /*
3343 * Set default initialization control block.
3344 */
3345 memset(nv, 0, ha->nvram_size);
3346 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3347 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3348 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3349 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3350 nv->exchange_count = __constant_cpu_to_le16(0);
3351 nv->hard_address = __constant_cpu_to_le16(124);
3352 nv->port_name[0] = 0x21;
3353 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3354 nv->port_name[2] = 0x00;
3355 nv->port_name[3] = 0xe0;
3356 nv->port_name[4] = 0x8b;
3357 nv->port_name[5] = 0x1c;
3358 nv->port_name[6] = 0x55;
3359 nv->port_name[7] = 0x86;
3360 nv->node_name[0] = 0x20;
3361 nv->node_name[1] = 0x00;
3362 nv->node_name[2] = 0x00;
3363 nv->node_name[3] = 0xe0;
3364 nv->node_name[4] = 0x8b;
3365 nv->node_name[5] = 0x1c;
3366 nv->node_name[6] = 0x55;
3367 nv->node_name[7] = 0x86;
3368 nv->login_retry_count = __constant_cpu_to_le16(8);
3369 nv->link_down_timeout = __constant_cpu_to_le16(200);
3370 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3371 nv->login_timeout = __constant_cpu_to_le16(0);
3372 nv->firmware_options_1 =
3373 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3374 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3375 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3376 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3377 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3378 nv->efi_parameters = __constant_cpu_to_le32(0);
3379 nv->reset_delay = 5;
3380 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3381 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3382 nv->link_down_timeout = __constant_cpu_to_le16(30);
3383
3384 rval = 1;
3385 }
3386
3387 /* Reset Initialization control block */
3388 memset(icb, 0, sizeof(struct init_cb_24xx));
3389
3390 /* Copy 1st segment. */
3391 dptr1 = (uint8_t *)icb;
3392 dptr2 = (uint8_t *)&nv->version;
3393 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3394 while (cnt--)
3395 *dptr1++ = *dptr2++;
3396
3397 icb->login_retry_count = nv->login_retry_count;
3398 icb->link_down_timeout = nv->link_down_timeout;
3399
3400 /* Copy 2nd segment. */
3401 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3402 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3403 cnt = (uint8_t *)&icb->reserved_3 -
3404 (uint8_t *)&icb->interrupt_delay_timer;
3405 while (cnt--)
3406 *dptr1++ = *dptr2++;
3407
3408 /*
3409 * Setup driver NVRAM options.
3410 */
3411 if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3412 char *st, *en;
3413 uint16_t index;
3414
3415 strncpy(ha->model_number, nv->model_name,
3416 sizeof(nv->model_name));
3417 st = en = ha->model_number;
3418 en += sizeof(nv->model_name) - 1;
3419 while (en > st) {
3420 if (*en != 0x20 && *en != 0x00)
3421 break;
3422 *en-- = '\0';
3423 }
3424
3425 index = (ha->pdev->subsystem_device & 0xff);
3426 if (index < QLA_MODEL_NAMES)
1328962e 3427 ha->model_desc = qla2x00_model_name[index * 2 + 1];
0107109e
AV
3428 } else
3429 strcpy(ha->model_number, "QLA2462");
3430
5341e868
AV
3431 /* Use alternate WWN? */
3432 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3433 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3434 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3435 }
3436
0107109e 3437 /* Prepare nodename */
fd0e7e4d 3438 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
0107109e
AV
3439 /*
3440 * Firmware will apply the following mask if the nodename was
3441 * not provided.
3442 */
3443 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3444 icb->node_name[0] &= 0xF0;
3445 }
3446
3447 /* Set host adapter parameters. */
3448 ha->flags.disable_risc_code_load = 0;
3449 ha->flags.enable_lip_reset = 1;
3450 ha->flags.enable_lip_full_login = 1;
3451 ha->flags.enable_target_reset = 1;
3452 ha->flags.enable_led_scheme = 0;
3453
fd0e7e4d
AV
3454 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3455 (BIT_6 | BIT_5 | BIT_4)) >> 4;
0107109e
AV
3456
3457 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3458 sizeof(ha->fw_seriallink_options24));
3459
3460 /* save HBA serial number */
3461 ha->serial0 = icb->port_name[5];
3462 ha->serial1 = icb->port_name[6];
3463 ha->serial2 = icb->port_name[7];
3464 ha->node_name = icb->node_name;
3465 ha->port_name = icb->port_name;
3466
bc8fb3cb
AV
3467 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3468
0107109e
AV
3469 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3470
3471 /* Set minimum login_timeout to 4 seconds. */
3472 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3473 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3474 if (le16_to_cpu(nv->login_timeout) < 4)
3475 nv->login_timeout = __constant_cpu_to_le16(4);
3476 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3477 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3478
3479 /* Set minimum RATOV to 200 tenths of a second. */
3480 ha->r_a_tov = 200;
3481
3482 ha->loop_reset_delay = nv->reset_delay;
3483
3484 /* Link Down Timeout = 0:
3485 *
3486 * When Port Down timer expires we will start returning
3487 * I/O's to OS with "DID_NO_CONNECT".
3488 *
3489 * Link Down Timeout != 0:
3490 *
3491 * The driver waits for the link to come up after link down
3492 * before returning I/Os to OS with "DID_NO_CONNECT".
3493 */
3494 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3495 ha->loop_down_abort_time =
3496 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3497 } else {
3498 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3499 ha->loop_down_abort_time =
3500 (LOOP_DOWN_TIME - ha->link_down_timeout);
3501 }
3502
3503 /* Need enough time to try and get the port back. */
3504 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3505 if (qlport_down_retry)
3506 ha->port_down_retry_count = qlport_down_retry;
3507
3508 /* Set login_retry_count */
3509 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3510 if (ha->port_down_retry_count ==
3511 le16_to_cpu(nv->port_down_retry_count) &&
3512 ha->port_down_retry_count > 3)
3513 ha->login_retry_count = ha->port_down_retry_count;
3514 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3515 ha->login_retry_count = ha->port_down_retry_count;
3516 if (ql2xloginretrycount)
3517 ha->login_retry_count = ql2xloginretrycount;
3518
4fdfefe5
AV
3519 /* Enable ZIO. */
3520 if (!ha->flags.init_done) {
3521 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3522 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3523 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3524 le16_to_cpu(icb->interrupt_delay_timer): 2;
3525 }
3526 icb->firmware_options_2 &= __constant_cpu_to_le32(
3527 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3528 ha->flags.process_response_queue = 0;
3529 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4a59f71d
AV
3530 ha->zio_mode = QLA_ZIO_MODE_6;
3531
4fdfefe5
AV
3532 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3533 "(%d us).\n", ha->host_no, ha->zio_mode,
3534 ha->zio_timer * 100));
3535 qla_printk(KERN_INFO, ha,
3536 "ZIO mode %d enabled; timer delay (%d us).\n",
3537 ha->zio_mode, ha->zio_timer * 100);
3538
3539 icb->firmware_options_2 |= cpu_to_le32(
3540 (uint32_t)ha->zio_mode);
3541 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3542 ha->flags.process_response_queue = 1;
3543 }
3544
0107109e
AV
3545 if (rval) {
3546 DEBUG2_3(printk(KERN_WARNING
3547 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3548 }
3549 return (rval);
3550}
3551
d1c61909
AV
3552int
3553qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3554{
3555 int rval;
3556 int segments, fragment;
3557 uint32_t faddr;
3558 uint32_t *dcode, dlen;
3559 uint32_t risc_addr;
3560 uint32_t risc_size;
3561 uint32_t i;
3562
3563 rval = QLA_SUCCESS;
3564
3565 segments = FA_RISC_CODE_SEGMENTS;
3566 faddr = FA_RISC_CODE_ADDR;
3567 dcode = (uint32_t *)ha->request_ring;
3568 *srisc_addr = 0;
3569
3570 /* Validate firmware image by checking version. */
3571 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3572 for (i = 0; i < 4; i++)
3573 dcode[i] = be32_to_cpu(dcode[i]);
3574 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3575 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3576 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3577 dcode[3] == 0)) {
3578 qla_printk(KERN_WARNING, ha,
3579 "Unable to verify integrity of flash firmware image!\n");
3580 qla_printk(KERN_WARNING, ha,
3581 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3582 dcode[1], dcode[2], dcode[3]);
3583
3584 return QLA_FUNCTION_FAILED;
3585 }
3586
3587 while (segments && rval == QLA_SUCCESS) {
3588 /* Read segment's load information. */
3589 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3590
3591 risc_addr = be32_to_cpu(dcode[2]);
3592 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3593 risc_size = be32_to_cpu(dcode[3]);
3594
3595 fragment = 0;
3596 while (risc_size > 0 && rval == QLA_SUCCESS) {
3597 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3598 if (dlen > risc_size)
3599 dlen = risc_size;
3600
3601 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3602 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3603 ha->host_no, risc_addr, dlen, faddr));
3604
3605 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3606 for (i = 0; i < dlen; i++)
3607 dcode[i] = swab32(dcode[i]);
3608
3609 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3610 dlen);
3611 if (rval) {
3612 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3613 "segment %d of firmware\n", ha->host_no,
3614 fragment));
3615 qla_printk(KERN_WARNING, ha,
3616 "[ERROR] Failed to load segment %d of "
3617 "firmware\n", fragment);
3618 break;
3619 }
3620
3621 faddr += dlen;
3622 risc_addr += dlen;
3623 risc_size -= dlen;
3624 fragment++;
3625 }
3626
3627 /* Next segment. */
3628 segments--;
3629 }
3630
3631 return rval;
3632}
3633
d1c61909
AV
3634#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3635
0107109e 3636int
5433383e
AV
3637qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3638{
3639 int rval;
3640 int i, fragment;
3641 uint16_t *wcode, *fwcode;
3642 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3643 struct fw_blob *blob;
3644
3645 /* Load firmware blob. */
3646 blob = qla2x00_request_firmware(ha);
3647 if (!blob) {
3648 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
d1c61909
AV
3649 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3650 "from: " QLA_FW_URL ".\n");
5433383e
AV
3651 return QLA_FUNCTION_FAILED;
3652 }
3653
3654 rval = QLA_SUCCESS;
3655
3656 wcode = (uint16_t *)ha->request_ring;
3657 *srisc_addr = 0;
3658 fwcode = (uint16_t *)blob->fw->data;
3659 fwclen = 0;
3660
3661 /* Validate firmware image by checking version. */
3662 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3663 qla_printk(KERN_WARNING, ha,
3664 "Unable to verify integrity of firmware image (%Zd)!\n",
3665 blob->fw->size);
3666 goto fail_fw_integrity;
3667 }
3668 for (i = 0; i < 4; i++)
3669 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3670 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3671 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3672 wcode[2] == 0 && wcode[3] == 0)) {
3673 qla_printk(KERN_WARNING, ha,
3674 "Unable to verify integrity of firmware image!\n");
3675 qla_printk(KERN_WARNING, ha,
3676 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3677 wcode[1], wcode[2], wcode[3]);
3678 goto fail_fw_integrity;
3679 }
3680
3681 seg = blob->segs;
3682 while (*seg && rval == QLA_SUCCESS) {
3683 risc_addr = *seg;
3684 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3685 risc_size = be16_to_cpu(fwcode[3]);
3686
3687 /* Validate firmware image size. */
3688 fwclen += risc_size * sizeof(uint16_t);
3689 if (blob->fw->size < fwclen) {
3690 qla_printk(KERN_WARNING, ha,
3691 "Unable to verify integrity of firmware image "
3692 "(%Zd)!\n", blob->fw->size);
3693 goto fail_fw_integrity;
3694 }
3695
3696 fragment = 0;
3697 while (risc_size > 0 && rval == QLA_SUCCESS) {
3698 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3699 if (wlen > risc_size)
3700 wlen = risc_size;
3701
3702 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3703 "addr %x, number of words 0x%x.\n", ha->host_no,
3704 risc_addr, wlen));
3705
3706 for (i = 0; i < wlen; i++)
3707 wcode[i] = swab16(fwcode[i]);
3708
3709 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3710 wlen);
3711 if (rval) {
3712 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3713 "segment %d of firmware\n", ha->host_no,
3714 fragment));
3715 qla_printk(KERN_WARNING, ha,
3716 "[ERROR] Failed to load segment %d of "
3717 "firmware\n", fragment);
3718 break;
3719 }
3720
3721 fwcode += wlen;
3722 risc_addr += wlen;
3723 risc_size -= wlen;
3724 fragment++;
3725 }
3726
3727 /* Next segment. */
3728 seg++;
3729 }
3730 return rval;
3731
3732fail_fw_integrity:
3733 return QLA_FUNCTION_FAILED;
3734}
3735
3736int
3737qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
0107109e
AV
3738{
3739 int rval;
3740 int segments, fragment;
3741 uint32_t *dcode, dlen;
3742 uint32_t risc_addr;
3743 uint32_t risc_size;
3744 uint32_t i;
5433383e 3745 struct fw_blob *blob;
0107109e
AV
3746 uint32_t *fwcode, fwclen;
3747
5433383e
AV
3748 /* Load firmware blob. */
3749 blob = qla2x00_request_firmware(ha);
3750 if (!blob) {
3751 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
d1c61909
AV
3752 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3753 "from: " QLA_FW_URL ".\n");
3754
3755 /* Try to load RISC code from flash. */
3756 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3757 "outdated) firmware from flash.\n");
3758 return qla24xx_load_risc_flash(ha, srisc_addr);
0107109e
AV
3759 }
3760
3761 rval = QLA_SUCCESS;
3762
3763 segments = FA_RISC_CODE_SEGMENTS;
3764 dcode = (uint32_t *)ha->request_ring;
3765 *srisc_addr = 0;
5433383e 3766 fwcode = (uint32_t *)blob->fw->data;
0107109e
AV
3767 fwclen = 0;
3768
3769 /* Validate firmware image by checking version. */
5433383e 3770 if (blob->fw->size < 8 * sizeof(uint32_t)) {
0107109e 3771 qla_printk(KERN_WARNING, ha,
5433383e
AV
3772 "Unable to verify integrity of firmware image (%Zd)!\n",
3773 blob->fw->size);
0107109e
AV
3774 goto fail_fw_integrity;
3775 }
3776 for (i = 0; i < 4; i++)
3777 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3778 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3779 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3780 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3781 dcode[3] == 0)) {
3782 qla_printk(KERN_WARNING, ha,
5433383e 3783 "Unable to verify integrity of firmware image!\n");
0107109e
AV
3784 qla_printk(KERN_WARNING, ha,
3785 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3786 dcode[1], dcode[2], dcode[3]);
3787 goto fail_fw_integrity;
3788 }
3789
3790 while (segments && rval == QLA_SUCCESS) {
3791 risc_addr = be32_to_cpu(fwcode[2]);
3792 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3793 risc_size = be32_to_cpu(fwcode[3]);
3794
3795 /* Validate firmware image size. */
3796 fwclen += risc_size * sizeof(uint32_t);
5433383e 3797 if (blob->fw->size < fwclen) {
0107109e 3798 qla_printk(KERN_WARNING, ha,
5433383e
AV
3799 "Unable to verify integrity of firmware image "
3800 "(%Zd)!\n", blob->fw->size);
3801
0107109e
AV
3802 goto fail_fw_integrity;
3803 }
3804
3805 fragment = 0;
3806 while (risc_size > 0 && rval == QLA_SUCCESS) {
3807 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3808 if (dlen > risc_size)
3809 dlen = risc_size;
3810
3811 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3812 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3813 risc_addr, dlen));
3814
3815 for (i = 0; i < dlen; i++)
3816 dcode[i] = swab32(fwcode[i]);
3817
590f98e5
AV
3818 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3819 dlen);
0107109e
AV
3820 if (rval) {
3821 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3822 "segment %d of firmware\n", ha->host_no,
3823 fragment));
3824 qla_printk(KERN_WARNING, ha,
3825 "[ERROR] Failed to load segment %d of "
3826 "firmware\n", fragment);
3827 break;
3828 }
3829
3830 fwcode += dlen;
3831 risc_addr += dlen;
3832 risc_size -= dlen;
3833 fragment++;
3834 }
3835
3836 /* Next segment. */
3837 segments--;
3838 }
0107109e
AV
3839 return rval;
3840
3841fail_fw_integrity:
0107109e 3842 return QLA_FUNCTION_FAILED;
0107109e 3843}