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