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