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