]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/qla2xxx/qla_init.c
[SCSI] qla2xxx: Remove reference to request queue from scsi request block.
[net-next-2.6.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
88c26663 733 /* Perform RISC reset. */
e315cd28 734 qla24xx_reset_risc(vha);
0107109e 735
73208dfd 736 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
0107109e 737
e315cd28 738 rval = qla2x00_mbx_reg_test(vha);
0107109e
AV
739 if (rval) {
740 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
e315cd28 741 vha->host_no));
0107109e
AV
742 qla_printk(KERN_WARNING, ha,
743 "Failed mailbox send register test\n");
744 } else {
745 /* Flag a successful rval */
746 rval = QLA_SUCCESS;
747 }
748
749 return rval;
750}
751
a7a167bf 752void
e315cd28 753qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
0107109e 754{
a7a167bf
AV
755 int rval;
756 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
73208dfd 757 eft_size, fce_size, mq_size;
df613b96
AV
758 dma_addr_t tc_dma;
759 void *tc;
e315cd28 760 struct qla_hw_data *ha = vha->hw;
73208dfd
AC
761 struct req_que *req = ha->req_q_map[0];
762 struct rsp_que *rsp = ha->rsp_q_map[0];
a7a167bf
AV
763
764 if (ha->fw_dump) {
765 qla_printk(KERN_WARNING, ha,
766 "Firmware dump previously allocated.\n");
767 return;
768 }
d4e3e04d 769
0107109e 770 ha->fw_dumped = 0;
73208dfd 771 fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
d4e3e04d 772 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
a7a167bf 773 fixed_size = sizeof(struct qla2100_fw_dump);
d4e3e04d 774 } else if (IS_QLA23XX(ha)) {
a7a167bf
AV
775 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
776 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
777 sizeof(uint16_t);
e428924c 778 } else if (IS_FWI2_CAPABLE(ha)) {
3a03eb79
AV
779 if (IS_QLA81XX(ha))
780 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
781 else if (IS_QLA25XX(ha))
782 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
783 else
784 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
a7a167bf
AV
785 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
786 sizeof(uint32_t);
73208dfd
AC
787 if (ha->mqenable)
788 mq_size = sizeof(struct qla2xxx_mq_chain);
df613b96 789 /* Allocate memory for Fibre Channel Event Buffer. */
3a03eb79 790 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha))
436a7b11 791 goto try_eft;
df613b96
AV
792
793 tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
794 GFP_KERNEL);
795 if (!tc) {
796 qla_printk(KERN_WARNING, ha, "Unable to allocate "
797 "(%d KB) for FCE.\n", FCE_SIZE / 1024);
17d98630 798 goto try_eft;
df613b96
AV
799 }
800
801 memset(tc, 0, FCE_SIZE);
e315cd28 802 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
df613b96
AV
803 ha->fce_mb, &ha->fce_bufs);
804 if (rval) {
805 qla_printk(KERN_WARNING, ha, "Unable to initialize "
806 "FCE (%d).\n", rval);
807 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
808 tc_dma);
809 ha->flags.fce_enabled = 0;
17d98630 810 goto try_eft;
df613b96
AV
811 }
812
813 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for FCE...\n",
814 FCE_SIZE / 1024);
815
7d9dade3 816 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
df613b96
AV
817 ha->flags.fce_enabled = 1;
818 ha->fce_dma = tc_dma;
819 ha->fce = tc;
436a7b11
AV
820try_eft:
821 /* Allocate memory for Extended Trace Buffer. */
822 tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
823 GFP_KERNEL);
824 if (!tc) {
825 qla_printk(KERN_WARNING, ha, "Unable to allocate "
826 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
827 goto cont_alloc;
828 }
829
830 memset(tc, 0, EFT_SIZE);
e315cd28 831 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
436a7b11
AV
832 if (rval) {
833 qla_printk(KERN_WARNING, ha, "Unable to initialize "
834 "EFT (%d).\n", rval);
835 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
836 tc_dma);
837 goto cont_alloc;
838 }
839
840 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
841 EFT_SIZE / 1024);
842
843 eft_size = EFT_SIZE;
844 ha->eft_dma = tc_dma;
845 ha->eft = tc;
d4e3e04d 846 }
a7a167bf 847cont_alloc:
73208dfd
AC
848 req_q_size = req->length * sizeof(request_t);
849 rsp_q_size = rsp->length * sizeof(response_t);
a7a167bf
AV
850
851 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
2afa19a9 852 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
bb99de67
AV
853 ha->chain_offset = dump_size;
854 dump_size += mq_size + fce_size;
d4e3e04d
AV
855
856 ha->fw_dump = vmalloc(dump_size);
a7a167bf 857 if (!ha->fw_dump) {
0107109e 858 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
d4e3e04d 859 "firmware dump!!!\n", dump_size / 1024);
a7a167bf
AV
860
861 if (ha->eft) {
862 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
863 ha->eft_dma);
864 ha->eft = NULL;
865 ha->eft_dma = 0;
866 }
867 return;
868 }
a7a167bf
AV
869 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
870 dump_size / 1024);
871
872 ha->fw_dump_len = dump_size;
873 ha->fw_dump->signature[0] = 'Q';
874 ha->fw_dump->signature[1] = 'L';
875 ha->fw_dump->signature[2] = 'G';
876 ha->fw_dump->signature[3] = 'C';
877 ha->fw_dump->version = __constant_htonl(1);
878
879 ha->fw_dump->fixed_size = htonl(fixed_size);
880 ha->fw_dump->mem_size = htonl(mem_size);
881 ha->fw_dump->req_q_size = htonl(req_q_size);
882 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
883
884 ha->fw_dump->eft_size = htonl(eft_size);
885 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
886 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
887
888 ha->fw_dump->header_size =
889 htonl(offsetof(struct qla2xxx_fw_dump, isp));
0107109e
AV
890}
891
1da177e4
LT
892/**
893 * qla2x00_setup_chip() - Load and start RISC firmware.
894 * @ha: HA context
895 *
896 * Returns 0 on success.
897 */
898static int
e315cd28 899qla2x00_setup_chip(scsi_qla_host_t *vha)
1da177e4 900{
0107109e
AV
901 int rval;
902 uint32_t srisc_address = 0;
e315cd28 903 struct qla_hw_data *ha = vha->hw;
3db0652e
AV
904 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
905 unsigned long flags;
dda772e8 906 uint16_t fw_major_version;
3db0652e
AV
907
908 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
909 /* Disable SRAM, Instruction RAM and GP RAM parity. */
910 spin_lock_irqsave(&ha->hardware_lock, flags);
911 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
912 RD_REG_WORD(&reg->hccr);
913 spin_unlock_irqrestore(&ha->hardware_lock, flags);
914 }
1da177e4
LT
915
916 /* Load firmware sequences */
e315cd28 917 rval = ha->isp_ops->load_risc(vha, &srisc_address);
0107109e 918 if (rval == QLA_SUCCESS) {
1da177e4 919 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
e315cd28 920 "code.\n", vha->host_no));
1da177e4 921
e315cd28 922 rval = qla2x00_verify_checksum(vha, srisc_address);
1da177e4
LT
923 if (rval == QLA_SUCCESS) {
924 /* Start firmware execution. */
925 DEBUG(printk("scsi(%ld): Checksum OK, start "
e315cd28 926 "firmware.\n", vha->host_no));
1da177e4 927
e315cd28 928 rval = qla2x00_execute_fw(vha, srisc_address);
1da177e4 929 /* Retrieve firmware information. */
dda772e8
AV
930 if (rval == QLA_SUCCESS) {
931 fw_major_version = ha->fw_major_version;
e315cd28 932 qla2x00_get_fw_version(vha,
1da177e4
LT
933 &ha->fw_major_version,
934 &ha->fw_minor_version,
935 &ha->fw_subminor_version,
3a03eb79 936 &ha->fw_attributes, &ha->fw_memory_size,
55a96158
AV
937 ha->mpi_version, &ha->mpi_capabilities,
938 ha->phy_version);
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 }
990
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 */
4d4df193 1307 uint16_t state[3];
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
1406 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
e315cd28 1407 vha->host_no, state[0], jiffies));
1da177e4
LT
1408
1409 if (rval) {
1410 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
e315cd28 1411 vha->host_no));
1da177e4
LT
1412 }
1413
1414 return (rval);
1415}
1416
1417/*
1418* qla2x00_configure_hba
1419* Setup adapter context.
1420*
1421* Input:
1422* ha = adapter state pointer.
1423*
1424* Returns:
1425* 0 = success
1426*
1427* Context:
1428* Kernel context.
1429*/
1430static int
e315cd28 1431qla2x00_configure_hba(scsi_qla_host_t *vha)
1da177e4
LT
1432{
1433 int rval;
1434 uint16_t loop_id;
1435 uint16_t topo;
2c3dfe3f 1436 uint16_t sw_cap;
1da177e4
LT
1437 uint8_t al_pa;
1438 uint8_t area;
1439 uint8_t domain;
1440 char connect_type[22];
e315cd28 1441 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
1442
1443 /* Get host addresses. */
e315cd28 1444 rval = qla2x00_get_adapter_id(vha,
2c3dfe3f 1445 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
1da177e4 1446 if (rval != QLA_SUCCESS) {
e315cd28 1447 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
33135aa2
RA
1448 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1449 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
e315cd28 1450 __func__, vha->host_no));
33135aa2
RA
1451 } else {
1452 qla_printk(KERN_WARNING, ha,
1453 "ERROR -- Unable to get host loop ID.\n");
e315cd28 1454 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
33135aa2 1455 }
1da177e4
LT
1456 return (rval);
1457 }
1458
1459 if (topo == 4) {
1460 qla_printk(KERN_INFO, ha,
1461 "Cannot get topology - retrying.\n");
1462 return (QLA_FUNCTION_FAILED);
1463 }
1464
e315cd28 1465 vha->loop_id = loop_id;
1da177e4
LT
1466
1467 /* initialize */
1468 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1469 ha->operating_mode = LOOP;
2c3dfe3f 1470 ha->switch_cap = 0;
1da177e4
LT
1471
1472 switch (topo) {
1473 case 0:
1474 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
e315cd28 1475 vha->host_no));
1da177e4
LT
1476 ha->current_topology = ISP_CFG_NL;
1477 strcpy(connect_type, "(Loop)");
1478 break;
1479
1480 case 1:
1481 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
e315cd28 1482 vha->host_no));
2c3dfe3f 1483 ha->switch_cap = sw_cap;
1da177e4
LT
1484 ha->current_topology = ISP_CFG_FL;
1485 strcpy(connect_type, "(FL_Port)");
1486 break;
1487
1488 case 2:
1489 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
e315cd28 1490 vha->host_no));
1da177e4
LT
1491 ha->operating_mode = P2P;
1492 ha->current_topology = ISP_CFG_N;
1493 strcpy(connect_type, "(N_Port-to-N_Port)");
1494 break;
1495
1496 case 3:
1497 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
e315cd28 1498 vha->host_no));
2c3dfe3f 1499 ha->switch_cap = sw_cap;
1da177e4
LT
1500 ha->operating_mode = P2P;
1501 ha->current_topology = ISP_CFG_F;
1502 strcpy(connect_type, "(F_Port)");
1503 break;
1504
1505 default:
1506 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1507 "Using NL.\n",
e315cd28 1508 vha->host_no, topo));
1da177e4
LT
1509 ha->current_topology = ISP_CFG_NL;
1510 strcpy(connect_type, "(Loop)");
1511 break;
1512 }
1513
1514 /* Save Host port and loop ID. */
1515 /* byte order - Big Endian */
e315cd28
AC
1516 vha->d_id.b.domain = domain;
1517 vha->d_id.b.area = area;
1518 vha->d_id.b.al_pa = al_pa;
1da177e4 1519
e315cd28 1520 if (!vha->flags.init_done)
1da177e4
LT
1521 qla_printk(KERN_INFO, ha,
1522 "Topology - %s, Host Loop address 0x%x\n",
e315cd28 1523 connect_type, vha->loop_id);
1da177e4
LT
1524
1525 if (rval) {
e315cd28 1526 DEBUG2_3(printk("scsi(%ld): FAILED.\n", vha->host_no));
1da177e4 1527 } else {
e315cd28 1528 DEBUG3(printk("scsi(%ld): exiting normally.\n", vha->host_no));
1da177e4
LT
1529 }
1530
1531 return(rval);
1532}
1533
9bb9fcf2 1534static inline void
e315cd28
AC
1535qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
1536 char *def)
9bb9fcf2
AV
1537{
1538 char *st, *en;
1539 uint16_t index;
e315cd28 1540 struct qla_hw_data *ha = vha->hw;
9bb9fcf2
AV
1541
1542 if (memcmp(model, BINZERO, len) != 0) {
1543 strncpy(ha->model_number, model, len);
1544 st = en = ha->model_number;
1545 en += len - 1;
1546 while (en > st) {
1547 if (*en != 0x20 && *en != 0x00)
1548 break;
1549 *en-- = '\0';
1550 }
1551
1552 index = (ha->pdev->subsystem_device & 0xff);
1553 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1554 index < QLA_MODEL_NAMES)
1ee27146
JC
1555 strncpy(ha->model_desc,
1556 qla2x00_model_name[index * 2 + 1],
1557 sizeof(ha->model_desc) - 1);
9bb9fcf2
AV
1558 } else {
1559 index = (ha->pdev->subsystem_device & 0xff);
1560 if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1561 index < QLA_MODEL_NAMES) {
1562 strcpy(ha->model_number,
1563 qla2x00_model_name[index * 2]);
1ee27146
JC
1564 strncpy(ha->model_desc,
1565 qla2x00_model_name[index * 2 + 1],
1566 sizeof(ha->model_desc) - 1);
9bb9fcf2
AV
1567 } else {
1568 strcpy(ha->model_number, def);
1569 }
1570 }
1ee27146 1571 if (IS_FWI2_CAPABLE(ha))
e315cd28 1572 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
1ee27146 1573 sizeof(ha->model_desc));
9bb9fcf2
AV
1574}
1575
4e08df3f
DM
1576/* On sparc systems, obtain port and node WWN from firmware
1577 * properties.
1578 */
e315cd28 1579static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
4e08df3f
DM
1580{
1581#ifdef CONFIG_SPARC
e315cd28 1582 struct qla_hw_data *ha = vha->hw;
4e08df3f 1583 struct pci_dev *pdev = ha->pdev;
15576bc8
DM
1584 struct device_node *dp = pci_device_to_OF_node(pdev);
1585 const u8 *val;
4e08df3f
DM
1586 int len;
1587
1588 val = of_get_property(dp, "port-wwn", &len);
1589 if (val && len >= WWN_SIZE)
1590 memcpy(nv->port_name, val, WWN_SIZE);
1591
1592 val = of_get_property(dp, "node-wwn", &len);
1593 if (val && len >= WWN_SIZE)
1594 memcpy(nv->node_name, val, WWN_SIZE);
1595#endif
1596}
1597
1da177e4
LT
1598/*
1599* NVRAM configuration for ISP 2xxx
1600*
1601* Input:
1602* ha = adapter block pointer.
1603*
1604* Output:
1605* initialization control block in response_ring
1606* host adapters parameters in host adapter block
1607*
1608* Returns:
1609* 0 = success.
1610*/
abbd8870 1611int
e315cd28 1612qla2x00_nvram_config(scsi_qla_host_t *vha)
1da177e4 1613{
4e08df3f 1614 int rval;
0107109e
AV
1615 uint8_t chksum = 0;
1616 uint16_t cnt;
1617 uint8_t *dptr1, *dptr2;
e315cd28 1618 struct qla_hw_data *ha = vha->hw;
0107109e 1619 init_cb_t *icb = ha->init_cb;
281afe19
SJ
1620 nvram_t *nv = ha->nvram;
1621 uint8_t *ptr = ha->nvram;
3d71644c 1622 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 1623
4e08df3f
DM
1624 rval = QLA_SUCCESS;
1625
1da177e4 1626 /* Determine NVRAM starting address. */
0107109e 1627 ha->nvram_size = sizeof(nvram_t);
1da177e4
LT
1628 ha->nvram_base = 0;
1629 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1630 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1631 ha->nvram_base = 0x80;
1632
1633 /* Get NVRAM data and calculate checksum. */
e315cd28 1634 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
0107109e
AV
1635 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1636 chksum += *ptr++;
1da177e4 1637
e315cd28 1638 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
281afe19 1639 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
1da177e4
LT
1640
1641 /* Bad NVRAM data, set defaults parameters. */
1642 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1643 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1644 /* Reset NVRAM data. */
1645 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1646 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1647 nv->nvram_version);
4e08df3f
DM
1648 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1649 "invalid -- WWPN) defaults.\n");
1650
1651 /*
1652 * Set default initialization control block.
1653 */
1654 memset(nv, 0, ha->nvram_size);
1655 nv->parameter_block_version = ICB_VERSION;
1656
1657 if (IS_QLA23XX(ha)) {
1658 nv->firmware_options[0] = BIT_2 | BIT_1;
1659 nv->firmware_options[1] = BIT_7 | BIT_5;
1660 nv->add_firmware_options[0] = BIT_5;
1661 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1662 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1663 nv->special_options[1] = BIT_7;
1664 } else if (IS_QLA2200(ha)) {
1665 nv->firmware_options[0] = BIT_2 | BIT_1;
1666 nv->firmware_options[1] = BIT_7 | BIT_5;
1667 nv->add_firmware_options[0] = BIT_5;
1668 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1669 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1670 } else if (IS_QLA2100(ha)) {
1671 nv->firmware_options[0] = BIT_3 | BIT_1;
1672 nv->firmware_options[1] = BIT_5;
1673 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1674 }
1675
1676 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1677 nv->execution_throttle = __constant_cpu_to_le16(16);
1678 nv->retry_count = 8;
1679 nv->retry_delay = 1;
1680
1681 nv->port_name[0] = 33;
1682 nv->port_name[3] = 224;
1683 nv->port_name[4] = 139;
1684
e315cd28 1685 qla2xxx_nvram_wwn_from_ofw(vha, nv);
4e08df3f
DM
1686
1687 nv->login_timeout = 4;
1688
1689 /*
1690 * Set default host adapter parameters
1691 */
1692 nv->host_p[1] = BIT_2;
1693 nv->reset_delay = 5;
1694 nv->port_down_retry_count = 8;
1695 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1696 nv->link_down_timeout = 60;
1697
1698 rval = 1;
1da177e4
LT
1699 }
1700
1701#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1702 /*
1703 * The SN2 does not provide BIOS emulation which means you can't change
1704 * potentially bogus BIOS settings. Force the use of default settings
1705 * for link rate and frame size. Hope that the rest of the settings
1706 * are valid.
1707 */
1708 if (ia64_platform_is("sn2")) {
1709 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1710 if (IS_QLA23XX(ha))
1711 nv->special_options[1] = BIT_7;
1712 }
1713#endif
1714
1715 /* Reset Initialization control block */
0107109e 1716 memset(icb, 0, ha->init_cb_size);
1da177e4
LT
1717
1718 /*
1719 * Setup driver NVRAM options.
1720 */
1721 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1722 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1723 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1724 nv->firmware_options[1] &= ~BIT_4;
1725
1726 if (IS_QLA23XX(ha)) {
1727 nv->firmware_options[0] |= BIT_2;
1728 nv->firmware_options[0] &= ~BIT_3;
0107109e 1729 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1da177e4
LT
1730
1731 if (IS_QLA2300(ha)) {
1732 if (ha->fb_rev == FPM_2310) {
1733 strcpy(ha->model_number, "QLA2310");
1734 } else {
1735 strcpy(ha->model_number, "QLA2300");
1736 }
1737 } else {
e315cd28 1738 qla2x00_set_model_info(vha, nv->model_number,
9bb9fcf2 1739 sizeof(nv->model_number), "QLA23xx");
1da177e4
LT
1740 }
1741 } else if (IS_QLA2200(ha)) {
1742 nv->firmware_options[0] |= BIT_2;
1743 /*
1744 * 'Point-to-point preferred, else loop' is not a safe
1745 * connection mode setting.
1746 */
1747 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1748 (BIT_5 | BIT_4)) {
1749 /* Force 'loop preferred, else point-to-point'. */
1750 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1751 nv->add_firmware_options[0] |= BIT_5;
1752 }
1753 strcpy(ha->model_number, "QLA22xx");
1754 } else /*if (IS_QLA2100(ha))*/ {
1755 strcpy(ha->model_number, "QLA2100");
1756 }
1757
1758 /*
1759 * Copy over NVRAM RISC parameter block to initialization control block.
1760 */
1761 dptr1 = (uint8_t *)icb;
1762 dptr2 = (uint8_t *)&nv->parameter_block_version;
1763 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1764 while (cnt--)
1765 *dptr1++ = *dptr2++;
1766
1767 /* Copy 2nd half. */
1768 dptr1 = (uint8_t *)icb->add_firmware_options;
1769 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1770 while (cnt--)
1771 *dptr1++ = *dptr2++;
1772
5341e868
AV
1773 /* Use alternate WWN? */
1774 if (nv->host_p[1] & BIT_7) {
1775 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1776 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1777 }
1778
1da177e4
LT
1779 /* Prepare nodename */
1780 if ((icb->firmware_options[1] & BIT_6) == 0) {
1781 /*
1782 * Firmware will apply the following mask if the nodename was
1783 * not provided.
1784 */
1785 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1786 icb->node_name[0] &= 0xF0;
1787 }
1788
1789 /*
1790 * Set host adapter parameters.
1791 */
0181944f 1792 if (nv->host_p[0] & BIT_7)
11010fec 1793 ql2xextended_error_logging = 1;
1da177e4
LT
1794 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1795 /* Always load RISC code on non ISP2[12]00 chips. */
1796 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1797 ha->flags.disable_risc_code_load = 0;
1798 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1799 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1800 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
06c22bd1 1801 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
d4c760c2 1802 ha->flags.disable_serdes = 0;
1da177e4
LT
1803
1804 ha->operating_mode =
1805 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1806
1807 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1808 sizeof(ha->fw_seriallink_options));
1809
1810 /* save HBA serial number */
1811 ha->serial0 = icb->port_name[5];
1812 ha->serial1 = icb->port_name[6];
1813 ha->serial2 = icb->port_name[7];
e315cd28
AC
1814 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
1815 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
1da177e4
LT
1816
1817 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1818
1819 ha->retry_count = nv->retry_count;
1820
1821 /* Set minimum login_timeout to 4 seconds. */
1822 if (nv->login_timeout < ql2xlogintimeout)
1823 nv->login_timeout = ql2xlogintimeout;
1824 if (nv->login_timeout < 4)
1825 nv->login_timeout = 4;
1826 ha->login_timeout = nv->login_timeout;
1827 icb->login_timeout = nv->login_timeout;
1828
00a537b8
AV
1829 /* Set minimum RATOV to 100 tenths of a second. */
1830 ha->r_a_tov = 100;
1da177e4 1831
1da177e4
LT
1832 ha->loop_reset_delay = nv->reset_delay;
1833
1da177e4
LT
1834 /* Link Down Timeout = 0:
1835 *
1836 * When Port Down timer expires we will start returning
1837 * I/O's to OS with "DID_NO_CONNECT".
1838 *
1839 * Link Down Timeout != 0:
1840 *
1841 * The driver waits for the link to come up after link down
1842 * before returning I/Os to OS with "DID_NO_CONNECT".
fa2a1ce5 1843 */
1da177e4
LT
1844 if (nv->link_down_timeout == 0) {
1845 ha->loop_down_abort_time =
354d6b21 1846 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1da177e4
LT
1847 } else {
1848 ha->link_down_timeout = nv->link_down_timeout;
1849 ha->loop_down_abort_time =
1850 (LOOP_DOWN_TIME - ha->link_down_timeout);
fa2a1ce5 1851 }
1da177e4 1852
1da177e4
LT
1853 /*
1854 * Need enough time to try and get the port back.
1855 */
1856 ha->port_down_retry_count = nv->port_down_retry_count;
1857 if (qlport_down_retry)
1858 ha->port_down_retry_count = qlport_down_retry;
1859 /* Set login_retry_count */
1860 ha->login_retry_count = nv->retry_count;
1861 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1862 ha->port_down_retry_count > 3)
1863 ha->login_retry_count = ha->port_down_retry_count;
1864 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1865 ha->login_retry_count = ha->port_down_retry_count;
1866 if (ql2xloginretrycount)
1867 ha->login_retry_count = ql2xloginretrycount;
1868
1da177e4
LT
1869 icb->lun_enables = __constant_cpu_to_le16(0);
1870 icb->command_resource_count = 0;
1871 icb->immediate_notify_resource_count = 0;
1872 icb->timeout = __constant_cpu_to_le16(0);
1873
1874 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1875 /* Enable RIO */
1876 icb->firmware_options[0] &= ~BIT_3;
1877 icb->add_firmware_options[0] &=
1878 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1879 icb->add_firmware_options[0] |= BIT_2;
1880 icb->response_accumulation_timer = 3;
1881 icb->interrupt_delay_timer = 5;
1882
e315cd28 1883 vha->flags.process_response_queue = 1;
1da177e4 1884 } else {
4fdfefe5 1885 /* Enable ZIO. */
e315cd28 1886 if (!vha->flags.init_done) {
4fdfefe5
AV
1887 ha->zio_mode = icb->add_firmware_options[0] &
1888 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1889 ha->zio_timer = icb->interrupt_delay_timer ?
1890 icb->interrupt_delay_timer: 2;
1891 }
1da177e4
LT
1892 icb->add_firmware_options[0] &=
1893 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
e315cd28 1894 vha->flags.process_response_queue = 0;
4fdfefe5 1895 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4a59f71d
AV
1896 ha->zio_mode = QLA_ZIO_MODE_6;
1897
4fdfefe5 1898 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
e315cd28 1899 "delay (%d us).\n", vha->host_no, ha->zio_mode,
4fdfefe5 1900 ha->zio_timer * 100));
1da177e4 1901 qla_printk(KERN_INFO, ha,
4fdfefe5
AV
1902 "ZIO mode %d enabled; timer delay (%d us).\n",
1903 ha->zio_mode, ha->zio_timer * 100);
1da177e4 1904
4fdfefe5
AV
1905 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1906 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
e315cd28 1907 vha->flags.process_response_queue = 1;
1da177e4
LT
1908 }
1909 }
1910
4e08df3f
DM
1911 if (rval) {
1912 DEBUG2_3(printk(KERN_WARNING
e315cd28 1913 "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
4e08df3f
DM
1914 }
1915 return (rval);
1da177e4
LT
1916}
1917
19a7b4ae
JSEC
1918static void
1919qla2x00_rport_del(void *data)
1920{
1921 fc_port_t *fcport = data;
d97994dc 1922 struct fc_rport *rport;
d97994dc 1923
e315cd28 1924 spin_lock_irq(fcport->vha->host->host_lock);
d97994dc
AV
1925 rport = fcport->drport;
1926 fcport->drport = NULL;
e315cd28 1927 spin_unlock_irq(fcport->vha->host->host_lock);
d97994dc
AV
1928 if (rport)
1929 fc_remote_port_delete(rport);
19a7b4ae
JSEC
1930}
1931
1da177e4
LT
1932/**
1933 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1934 * @ha: HA context
1935 * @flags: allocation flags
1936 *
1937 * Returns a pointer to the allocated fcport, or NULL, if none available.
1938 */
413975a0 1939static fc_port_t *
e315cd28 1940qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
1da177e4
LT
1941{
1942 fc_port_t *fcport;
1943
bbfbbbc1
MK
1944 fcport = kzalloc(sizeof(fc_port_t), flags);
1945 if (!fcport)
1946 return NULL;
1da177e4
LT
1947
1948 /* Setup fcport template structure. */
e315cd28
AC
1949 fcport->vha = vha;
1950 fcport->vp_idx = vha->vp_idx;
1da177e4
LT
1951 fcport->port_type = FCT_UNKNOWN;
1952 fcport->loop_id = FC_NO_LOOP_ID;
1da177e4 1953 atomic_set(&fcport->state, FCS_UNCONFIGURED);
ad3e0eda 1954 fcport->supported_classes = FC_COS_UNSPECIFIED;
1da177e4 1955
bbfbbbc1 1956 return fcport;
1da177e4
LT
1957}
1958
1959/*
1960 * qla2x00_configure_loop
1961 * Updates Fibre Channel Device Database with what is actually on loop.
1962 *
1963 * Input:
1964 * ha = adapter block pointer.
1965 *
1966 * Returns:
1967 * 0 = success.
1968 * 1 = error.
1969 * 2 = database was full and device was not configured.
1970 */
1971static int
e315cd28 1972qla2x00_configure_loop(scsi_qla_host_t *vha)
1da177e4
LT
1973{
1974 int rval;
1975 unsigned long flags, save_flags;
e315cd28 1976 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
1977 rval = QLA_SUCCESS;
1978
1979 /* Get Initiator ID */
e315cd28
AC
1980 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
1981 rval = qla2x00_configure_hba(vha);
1da177e4
LT
1982 if (rval != QLA_SUCCESS) {
1983 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
e315cd28 1984 vha->host_no));
1da177e4
LT
1985 return (rval);
1986 }
1987 }
1988
e315cd28 1989 save_flags = flags = vha->dpc_flags;
1da177e4 1990 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
e315cd28 1991 vha->host_no, flags));
1da177e4
LT
1992
1993 /*
1994 * If we have both an RSCN and PORT UPDATE pending then handle them
1995 * both at the same time.
1996 */
e315cd28
AC
1997 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1998 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
1da177e4
LT
1999
2000 /* Determine what we need to do */
2001 if (ha->current_topology == ISP_CFG_FL &&
2002 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2003
e315cd28 2004 vha->flags.rscn_queue_overflow = 1;
1da177e4
LT
2005 set_bit(RSCN_UPDATE, &flags);
2006
2007 } else if (ha->current_topology == ISP_CFG_F &&
2008 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2009
e315cd28 2010 vha->flags.rscn_queue_overflow = 1;
1da177e4
LT
2011 set_bit(RSCN_UPDATE, &flags);
2012 clear_bit(LOCAL_LOOP_UPDATE, &flags);
21333b48
AV
2013
2014 } else if (ha->current_topology == ISP_CFG_N) {
2015 clear_bit(RSCN_UPDATE, &flags);
1da177e4 2016
e315cd28 2017 } else if (!vha->flags.online ||
1da177e4
LT
2018 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
2019
e315cd28 2020 vha->flags.rscn_queue_overflow = 1;
1da177e4
LT
2021 set_bit(RSCN_UPDATE, &flags);
2022 set_bit(LOCAL_LOOP_UPDATE, &flags);
2023 }
2024
2025 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
e315cd28 2026 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
1da177e4 2027 rval = QLA_FUNCTION_FAILED;
e315cd28
AC
2028 else
2029 rval = qla2x00_configure_local_loop(vha);
1da177e4
LT
2030 }
2031
2032 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
e315cd28 2033 if (LOOP_TRANSITION(vha))
1da177e4 2034 rval = QLA_FUNCTION_FAILED;
e315cd28
AC
2035 else
2036 rval = qla2x00_configure_fabric(vha);
1da177e4
LT
2037 }
2038
2039 if (rval == QLA_SUCCESS) {
e315cd28
AC
2040 if (atomic_read(&vha->loop_down_timer) ||
2041 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
1da177e4
LT
2042 rval = QLA_FUNCTION_FAILED;
2043 } else {
e315cd28 2044 atomic_set(&vha->loop_state, LOOP_READY);
1da177e4 2045
e315cd28 2046 DEBUG(printk("scsi(%ld): LOOP READY\n", vha->host_no));
1da177e4
LT
2047 }
2048 }
2049
2050 if (rval) {
2051 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
e315cd28 2052 __func__, vha->host_no));
1da177e4
LT
2053 } else {
2054 DEBUG3(printk("%s: exiting normally\n", __func__));
2055 }
2056
cc3ef7bc 2057 /* Restore state if a resync event occurred during processing */
e315cd28 2058 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
1da177e4 2059 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
e315cd28 2060 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
73208dfd 2061 if (test_bit(RSCN_UPDATE, &save_flags))
e315cd28 2062 set_bit(RSCN_UPDATE, &vha->dpc_flags);
1da177e4
LT
2063 }
2064
2065 return (rval);
2066}
2067
2068
2069
2070/*
2071 * qla2x00_configure_local_loop
2072 * Updates Fibre Channel Device Database with local loop devices.
2073 *
2074 * Input:
2075 * ha = adapter block pointer.
2076 *
2077 * Returns:
2078 * 0 = success.
2079 */
2080static int
e315cd28 2081qla2x00_configure_local_loop(scsi_qla_host_t *vha)
1da177e4
LT
2082{
2083 int rval, rval2;
2084 int found_devs;
2085 int found;
2086 fc_port_t *fcport, *new_fcport;
2087
2088 uint16_t index;
2089 uint16_t entries;
2090 char *id_iter;
2091 uint16_t loop_id;
2092 uint8_t domain, area, al_pa;
e315cd28 2093 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
2094
2095 found_devs = 0;
2096 new_fcport = NULL;
2097 entries = MAX_FIBRE_DEVICES;
2098
e315cd28
AC
2099 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", vha->host_no));
2100 DEBUG3(qla2x00_get_fcal_position_map(vha, NULL));
1da177e4
LT
2101
2102 /* Get list of logged in devices. */
2103 memset(ha->gid_list, 0, GID_LIST_SIZE);
e315cd28 2104 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
1da177e4
LT
2105 &entries);
2106 if (rval != QLA_SUCCESS)
2107 goto cleanup_allocation;
2108
2109 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
7640335e 2110 vha->host_no, entries));
1da177e4
LT
2111 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2112 entries * sizeof(struct gid_list_info)));
2113
2114 /* Allocate temporary fcport for any new fcports discovered. */
e315cd28 2115 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1da177e4
LT
2116 if (new_fcport == NULL) {
2117 rval = QLA_MEMORY_ALLOC_FAILED;
2118 goto cleanup_allocation;
2119 }
2120 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2121
2122 /*
2123 * Mark local devices that were present with FCF_DEVICE_LOST for now.
2124 */
e315cd28 2125 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
2126 if (atomic_read(&fcport->state) == FCS_ONLINE &&
2127 fcport->port_type != FCT_BROADCAST &&
2128 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2129
2130 DEBUG(printk("scsi(%ld): Marking port lost, "
2131 "loop_id=0x%04x\n",
e315cd28 2132 vha->host_no, fcport->loop_id));
1da177e4
LT
2133
2134 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1da177e4
LT
2135 }
2136 }
2137
2138 /* Add devices to port list. */
2139 id_iter = (char *)ha->gid_list;
2140 for (index = 0; index < entries; index++) {
2141 domain = ((struct gid_list_info *)id_iter)->domain;
2142 area = ((struct gid_list_info *)id_iter)->area;
2143 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
abbd8870 2144 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1da177e4
LT
2145 loop_id = (uint16_t)
2146 ((struct gid_list_info *)id_iter)->loop_id_2100;
abbd8870 2147 else
1da177e4
LT
2148 loop_id = le16_to_cpu(
2149 ((struct gid_list_info *)id_iter)->loop_id);
abbd8870 2150 id_iter += ha->gid_list_info_size;
1da177e4
LT
2151
2152 /* Bypass reserved domain fields. */
2153 if ((domain & 0xf0) == 0xf0)
2154 continue;
2155
2156 /* Bypass if not same domain and area of adapter. */
f7d289f6 2157 if (area && domain &&
e315cd28 2158 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
1da177e4
LT
2159 continue;
2160
2161 /* Bypass invalid local loop ID. */
2162 if (loop_id > LAST_LOCAL_LOOP_ID)
2163 continue;
2164
2165 /* Fill in member data. */
2166 new_fcport->d_id.b.domain = domain;
2167 new_fcport->d_id.b.area = area;
2168 new_fcport->d_id.b.al_pa = al_pa;
2169 new_fcport->loop_id = loop_id;
e315cd28
AC
2170 new_fcport->vp_idx = vha->vp_idx;
2171 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
1da177e4
LT
2172 if (rval2 != QLA_SUCCESS) {
2173 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2174 "information -- get_port_database=%x, "
2175 "loop_id=0x%04x\n",
e315cd28 2176 vha->host_no, rval2, new_fcport->loop_id));
c9d02acf 2177 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
e315cd28
AC
2178 vha->host_no));
2179 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1da177e4
LT
2180 continue;
2181 }
2182
2183 /* Check for matching device in port list. */
2184 found = 0;
2185 fcport = NULL;
e315cd28 2186 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
2187 if (memcmp(new_fcport->port_name, fcport->port_name,
2188 WWN_SIZE))
2189 continue;
2190
ddb9b126 2191 fcport->flags &= ~FCF_FABRIC_DEVICE;
1da177e4
LT
2192 fcport->loop_id = new_fcport->loop_id;
2193 fcport->port_type = new_fcport->port_type;
2194 fcport->d_id.b24 = new_fcport->d_id.b24;
2195 memcpy(fcport->node_name, new_fcport->node_name,
2196 WWN_SIZE);
2197
2198 found++;
2199 break;
2200 }
2201
2202 if (!found) {
2203 /* New device, add to fcports list. */
e315cd28
AC
2204 if (vha->vp_idx) {
2205 new_fcport->vha = vha;
2206 new_fcport->vp_idx = vha->vp_idx;
2c3dfe3f 2207 }
e315cd28 2208 list_add_tail(&new_fcport->list, &vha->vp_fcports);
1da177e4
LT
2209
2210 /* Allocate a new replacement fcport. */
2211 fcport = new_fcport;
e315cd28 2212 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1da177e4
LT
2213 if (new_fcport == NULL) {
2214 rval = QLA_MEMORY_ALLOC_FAILED;
2215 goto cleanup_allocation;
2216 }
2217 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2218 }
2219
d8b45213 2220 /* Base iIDMA settings on HBA port speed. */
a3cbdfad 2221 fcport->fp_speed = ha->link_data_rate;
d8b45213 2222
e315cd28 2223 qla2x00_update_fcport(vha, fcport);
1da177e4
LT
2224
2225 found_devs++;
2226 }
2227
2228cleanup_allocation:
c9475cb0 2229 kfree(new_fcport);
1da177e4
LT
2230
2231 if (rval != QLA_SUCCESS) {
2232 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
e315cd28 2233 "rval=%x\n", vha->host_no, rval));
1da177e4
LT
2234 }
2235
1da177e4
LT
2236 return (rval);
2237}
2238
d8b45213 2239static void
e315cd28 2240qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
d8b45213
AV
2241{
2242#define LS_UNKNOWN 2
a3cbdfad 2243 static char *link_speeds[5] = { "1", "2", "?", "4", "8" };
d8b45213 2244 int rval;
a3cbdfad 2245 uint16_t mb[6];
e315cd28 2246 struct qla_hw_data *ha = vha->hw;
d8b45213 2247
c76f2c01 2248 if (!IS_IIDMA_CAPABLE(ha))
d8b45213
AV
2249 return;
2250
39bd9622
AV
2251 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2252 fcport->fp_speed > ha->link_data_rate)
d8b45213
AV
2253 return;
2254
e315cd28 2255 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
a3cbdfad 2256 mb);
d8b45213
AV
2257 if (rval != QLA_SUCCESS) {
2258 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2259 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
e315cd28 2260 vha->host_no, fcport->port_name[0], fcport->port_name[1],
d8b45213
AV
2261 fcport->port_name[2], fcport->port_name[3],
2262 fcport->port_name[4], fcport->port_name[5],
2263 fcport->port_name[6], fcport->port_name[7], rval,
a3cbdfad 2264 fcport->fp_speed, mb[0], mb[1]));
d8b45213
AV
2265 } else {
2266 DEBUG2(qla_printk(KERN_INFO, ha,
2267 "iIDMA adjusted to %s GB/s on "
2268 "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
a3cbdfad 2269 link_speeds[fcport->fp_speed], fcport->port_name[0],
d8b45213
AV
2270 fcport->port_name[1], fcport->port_name[2],
2271 fcport->port_name[3], fcport->port_name[4],
2272 fcport->port_name[5], fcport->port_name[6],
2273 fcport->port_name[7]));
2274 }
2275}
2276
23be331d 2277static void
e315cd28 2278qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e118
AV
2279{
2280 struct fc_rport_identifiers rport_ids;
bdf79621 2281 struct fc_rport *rport;
e315cd28 2282 struct qla_hw_data *ha = vha->hw;
8482e118 2283
d97994dc
AV
2284 if (fcport->drport)
2285 qla2x00_rport_del(fcport);
8482e118 2286
f8b02a85
AV
2287 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2288 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e118
AV
2289 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2290 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
77d74143 2291 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
e315cd28 2292 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
77d74143
AV
2293 if (!rport) {
2294 qla_printk(KERN_WARNING, ha,
2295 "Unable to allocate fc remote port!\n");
2296 return;
2297 }
e315cd28 2298 spin_lock_irq(fcport->vha->host->host_lock);
19a7b4ae 2299 *((fc_port_t **)rport->dd_data) = fcport;
e315cd28 2300 spin_unlock_irq(fcport->vha->host->host_lock);
d97994dc 2301
ad3e0eda 2302 rport->supported_classes = fcport->supported_classes;
77d74143 2303
8482e118
AV
2304 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2305 if (fcport->port_type == FCT_INITIATOR)
2306 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2307 if (fcport->port_type == FCT_TARGET)
2308 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
77d74143 2309 fc_remote_port_rolechg(rport, rport_ids.roles);
1da177e4
LT
2310}
2311
23be331d
AB
2312/*
2313 * qla2x00_update_fcport
2314 * Updates device on list.
2315 *
2316 * Input:
2317 * ha = adapter block pointer.
2318 * fcport = port structure pointer.
2319 *
2320 * Return:
2321 * 0 - Success
2322 * BIT_0 - error
2323 *
2324 * Context:
2325 * Kernel context.
2326 */
2327void
e315cd28 2328qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
23be331d 2329{
e315cd28 2330 struct qla_hw_data *ha = vha->hw;
2c3dfe3f 2331
e315cd28 2332 fcport->vha = vha;
23be331d 2333 fcport->login_retry = 0;
e315cd28 2334 fcport->port_login_retry_count = ha->port_down_retry_count *
23be331d 2335 PORT_RETRY_TIME;
e315cd28 2336 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
23be331d
AB
2337 PORT_RETRY_TIME);
2338 fcport->flags &= ~FCF_LOGIN_NEEDED;
2339
e315cd28 2340 qla2x00_iidma_fcport(vha, fcport);
23be331d
AB
2341
2342 atomic_set(&fcport->state, FCS_ONLINE);
2343
e315cd28 2344 qla2x00_reg_remote_port(vha, fcport);
23be331d
AB
2345}
2346
1da177e4
LT
2347/*
2348 * qla2x00_configure_fabric
2349 * Setup SNS devices with loop ID's.
2350 *
2351 * Input:
2352 * ha = adapter block pointer.
2353 *
2354 * Returns:
2355 * 0 = success.
2356 * BIT_0 = error
2357 */
2358static int
e315cd28 2359qla2x00_configure_fabric(scsi_qla_host_t *vha)
1da177e4
LT
2360{
2361 int rval, rval2;
2362 fc_port_t *fcport, *fcptemp;
2363 uint16_t next_loopid;
2364 uint16_t mb[MAILBOX_REGISTER_COUNT];
0107109e 2365 uint16_t loop_id;
1da177e4 2366 LIST_HEAD(new_fcports);
e315cd28
AC
2367 struct qla_hw_data *ha = vha->hw;
2368 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
1da177e4
LT
2369
2370 /* If FL port exists, then SNS is present */
e428924c 2371 if (IS_FWI2_CAPABLE(ha))
0107109e
AV
2372 loop_id = NPH_F_PORT;
2373 else
2374 loop_id = SNS_FL_PORT;
e315cd28 2375 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
1da177e4
LT
2376 if (rval != QLA_SUCCESS) {
2377 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
e315cd28 2378 "Port\n", vha->host_no));
1da177e4 2379
e315cd28 2380 vha->device_flags &= ~SWITCH_FOUND;
1da177e4
LT
2381 return (QLA_SUCCESS);
2382 }
e315cd28 2383 vha->device_flags |= SWITCH_FOUND;
1da177e4
LT
2384
2385 /* Mark devices that need re-synchronization. */
e315cd28 2386 rval2 = qla2x00_device_resync(vha);
1da177e4
LT
2387 if (rval2 == QLA_RSCNS_HANDLED) {
2388 /* No point doing the scan, just continue. */
2389 return (QLA_SUCCESS);
2390 }
2391 do {
cca5335c
AV
2392 /* FDMI support. */
2393 if (ql2xfdmienable &&
e315cd28
AC
2394 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
2395 qla2x00_fdmi_register(vha);
cca5335c 2396
1da177e4 2397 /* Ensure we are logged into the SNS. */
e428924c 2398 if (IS_FWI2_CAPABLE(ha))
0107109e
AV
2399 loop_id = NPH_SNS;
2400 else
2401 loop_id = SIMPLE_NAME_SERVER;
e315cd28 2402 ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
abbd8870 2403 0xfc, mb, BIT_1 | BIT_0);
1da177e4
LT
2404 if (mb[0] != MBS_COMMAND_COMPLETE) {
2405 DEBUG2(qla_printk(KERN_INFO, ha,
2406 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
0107109e 2407 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
1da177e4
LT
2408 mb[0], mb[1], mb[2], mb[6], mb[7]));
2409 return (QLA_SUCCESS);
2410 }
2411
e315cd28
AC
2412 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
2413 if (qla2x00_rft_id(vha)) {
1da177e4
LT
2414 /* EMPTY */
2415 DEBUG2(printk("scsi(%ld): Register FC-4 "
e315cd28 2416 "TYPE failed.\n", vha->host_no));
1da177e4 2417 }
e315cd28 2418 if (qla2x00_rff_id(vha)) {
1da177e4
LT
2419 /* EMPTY */
2420 DEBUG2(printk("scsi(%ld): Register FC-4 "
e315cd28 2421 "Features failed.\n", vha->host_no));
1da177e4 2422 }
e315cd28 2423 if (qla2x00_rnn_id(vha)) {
1da177e4
LT
2424 /* EMPTY */
2425 DEBUG2(printk("scsi(%ld): Register Node Name "
e315cd28
AC
2426 "failed.\n", vha->host_no));
2427 } else if (qla2x00_rsnn_nn(vha)) {
1da177e4
LT
2428 /* EMPTY */
2429 DEBUG2(printk("scsi(%ld): Register Symbolic "
e315cd28 2430 "Node Name failed.\n", vha->host_no));
1da177e4
LT
2431 }
2432 }
2433
e315cd28 2434 rval = qla2x00_find_all_fabric_devs(vha, &new_fcports);
1da177e4
LT
2435 if (rval != QLA_SUCCESS)
2436 break;
2437
2438 /*
2439 * Logout all previous fabric devices marked lost, except
2440 * tape devices.
2441 */
e315cd28
AC
2442 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2443 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
1da177e4
LT
2444 break;
2445
2446 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2447 continue;
2448
2449 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
e315cd28 2450 qla2x00_mark_device_lost(vha, fcport,
d97994dc 2451 ql2xplogiabsentdevice, 0);
1da177e4
LT
2452 if (fcport->loop_id != FC_NO_LOOP_ID &&
2453 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2454 fcport->port_type != FCT_INITIATOR &&
2455 fcport->port_type != FCT_BROADCAST) {
e315cd28 2456 ha->isp_ops->fabric_logout(vha,
1c7c6357
AV
2457 fcport->loop_id,
2458 fcport->d_id.b.domain,
2459 fcport->d_id.b.area,
2460 fcport->d_id.b.al_pa);
1da177e4
LT
2461 fcport->loop_id = FC_NO_LOOP_ID;
2462 }
2463 }
2464 }
2465
2466 /* Starting free loop ID. */
e315cd28 2467 next_loopid = ha->min_external_loopid;
1da177e4
LT
2468
2469 /*
2470 * Scan through our port list and login entries that need to be
2471 * logged in.
2472 */
e315cd28
AC
2473 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2474 if (atomic_read(&vha->loop_down_timer) ||
2475 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
1da177e4
LT
2476 break;
2477
2478 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2479 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2480 continue;
2481
2482 if (fcport->loop_id == FC_NO_LOOP_ID) {
2483 fcport->loop_id = next_loopid;
d4486fd6 2484 rval = qla2x00_find_new_loop_id(
e315cd28 2485 base_vha, fcport);
1da177e4
LT
2486 if (rval != QLA_SUCCESS) {
2487 /* Ran out of IDs to use */
2488 break;
2489 }
2490 }
1da177e4 2491 /* Login and update database */
e315cd28 2492 qla2x00_fabric_dev_login(vha, fcport, &next_loopid);
1da177e4
LT
2493 }
2494
2495 /* Exit if out of loop IDs. */
2496 if (rval != QLA_SUCCESS) {
2497 break;
2498 }
2499
2500 /*
2501 * Login and add the new devices to our port list.
2502 */
2503 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
e315cd28
AC
2504 if (atomic_read(&vha->loop_down_timer) ||
2505 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
1da177e4
LT
2506 break;
2507
2508 /* Find a new loop ID to use. */
2509 fcport->loop_id = next_loopid;
e315cd28 2510 rval = qla2x00_find_new_loop_id(base_vha, fcport);
1da177e4
LT
2511 if (rval != QLA_SUCCESS) {
2512 /* Ran out of IDs to use */
2513 break;
2514 }
2515
bdf79621 2516 /* Login and update database */
e315cd28
AC
2517 qla2x00_fabric_dev_login(vha, fcport, &next_loopid);
2518
2519 if (vha->vp_idx) {
2520 fcport->vha = vha;
2521 fcport->vp_idx = vha->vp_idx;
2522 }
2523 list_move_tail(&fcport->list, &vha->vp_fcports);
1da177e4
LT
2524 }
2525 } while (0);
2526
2527 /* Free all new device structures not processed. */
2528 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2529 list_del(&fcport->list);
2530 kfree(fcport);
2531 }
2532
2533 if (rval) {
2534 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
e315cd28 2535 "rval=%d\n", vha->host_no, rval));
1da177e4
LT
2536 }
2537
2538 return (rval);
2539}
2540
2541
2542/*
2543 * qla2x00_find_all_fabric_devs
2544 *
2545 * Input:
2546 * ha = adapter block pointer.
2547 * dev = database device entry pointer.
2548 *
2549 * Returns:
2550 * 0 = success.
2551 *
2552 * Context:
2553 * Kernel context.
2554 */
2555static int
e315cd28
AC
2556qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha,
2557 struct list_head *new_fcports)
1da177e4
LT
2558{
2559 int rval;
2560 uint16_t loop_id;
2561 fc_port_t *fcport, *new_fcport, *fcptemp;
2562 int found;
2563
2564 sw_info_t *swl;
2565 int swl_idx;
2566 int first_dev, last_dev;
2567 port_id_t wrap, nxt_d_id;
e315cd28
AC
2568 struct qla_hw_data *ha = vha->hw;
2569 struct scsi_qla_host *vp, *base_vha = pci_get_drvdata(ha->pdev);
ee546b6e 2570 struct scsi_qla_host *tvp;
1da177e4
LT
2571
2572 rval = QLA_SUCCESS;
2573
2574 /* Try GID_PT to get device list, else GAN. */
4b89258c 2575 swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_KERNEL);
bbfbbbc1 2576 if (!swl) {
1da177e4
LT
2577 /*EMPTY*/
2578 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
e315cd28 2579 "on GA_NXT\n", vha->host_no));
1da177e4 2580 } else {
e315cd28 2581 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
1da177e4
LT
2582 kfree(swl);
2583 swl = NULL;
e315cd28 2584 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
1da177e4
LT
2585 kfree(swl);
2586 swl = NULL;
e315cd28 2587 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
1da177e4
LT
2588 kfree(swl);
2589 swl = NULL;
e5896bd5 2590 } else if (ql2xiidmaenable &&
e315cd28
AC
2591 qla2x00_gfpn_id(vha, swl) == QLA_SUCCESS) {
2592 qla2x00_gpsc(vha, swl);
1da177e4
LT
2593 }
2594 }
2595 swl_idx = 0;
2596
2597 /* Allocate temporary fcport for any new fcports discovered. */
e315cd28 2598 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1da177e4 2599 if (new_fcport == NULL) {
c9475cb0 2600 kfree(swl);
1da177e4
LT
2601 return (QLA_MEMORY_ALLOC_FAILED);
2602 }
2603 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
1da177e4
LT
2604 /* Set start port ID scan at adapter ID. */
2605 first_dev = 1;
2606 last_dev = 0;
2607
2608 /* Starting free loop ID. */
e315cd28
AC
2609 loop_id = ha->min_external_loopid;
2610 for (; loop_id <= ha->max_loop_id; loop_id++) {
2611 if (qla2x00_is_reserved_id(vha, loop_id))
1da177e4
LT
2612 continue;
2613
e315cd28 2614 if (atomic_read(&vha->loop_down_timer) || LOOP_TRANSITION(vha))
1da177e4
LT
2615 break;
2616
2617 if (swl != NULL) {
2618 if (last_dev) {
2619 wrap.b24 = new_fcport->d_id.b24;
2620 } else {
2621 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2622 memcpy(new_fcport->node_name,
2623 swl[swl_idx].node_name, WWN_SIZE);
2624 memcpy(new_fcport->port_name,
2625 swl[swl_idx].port_name, WWN_SIZE);
d8b45213
AV
2626 memcpy(new_fcport->fabric_port_name,
2627 swl[swl_idx].fabric_port_name, WWN_SIZE);
2628 new_fcport->fp_speed = swl[swl_idx].fp_speed;
1da177e4
LT
2629
2630 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2631 last_dev = 1;
2632 }
2633 swl_idx++;
2634 }
2635 } else {
2636 /* Send GA_NXT to the switch */
e315cd28 2637 rval = qla2x00_ga_nxt(vha, new_fcport);
1da177e4
LT
2638 if (rval != QLA_SUCCESS) {
2639 qla_printk(KERN_WARNING, ha,
2640 "SNS scan failed -- assuming zero-entry "
2641 "result...\n");
2642 list_for_each_entry_safe(fcport, fcptemp,
2643 new_fcports, list) {
2644 list_del(&fcport->list);
2645 kfree(fcport);
2646 }
2647 rval = QLA_SUCCESS;
2648 break;
2649 }
2650 }
2651
2652 /* If wrap on switch device list, exit. */
2653 if (first_dev) {
2654 wrap.b24 = new_fcport->d_id.b24;
2655 first_dev = 0;
2656 } else if (new_fcport->d_id.b24 == wrap.b24) {
2657 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
e315cd28 2658 vha->host_no, new_fcport->d_id.b.domain,
1da177e4
LT
2659 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2660 break;
2661 }
2662
2c3dfe3f 2663 /* Bypass if same physical adapter. */
e315cd28 2664 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
1da177e4
LT
2665 continue;
2666
2c3dfe3f 2667 /* Bypass virtual ports of the same host. */
e315cd28
AC
2668 found = 0;
2669 if (ha->num_vhosts) {
ee546b6e 2670 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
e315cd28
AC
2671 if (new_fcport->d_id.b24 == vp->d_id.b24) {
2672 found = 1;
2c3dfe3f 2673 break;
e315cd28 2674 }
2c3dfe3f 2675 }
e315cd28 2676 if (found)
2c3dfe3f
SJ
2677 continue;
2678 }
2679
f7d289f6
AV
2680 /* Bypass if same domain and area of adapter. */
2681 if (((new_fcport->d_id.b24 & 0xffff00) ==
e315cd28 2682 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
f7d289f6
AV
2683 ISP_CFG_FL)
2684 continue;
2685
1da177e4
LT
2686 /* Bypass reserved domain fields. */
2687 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2688 continue;
2689
2690 /* Locate matching device in database. */
2691 found = 0;
e315cd28 2692 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
2693 if (memcmp(new_fcport->port_name, fcport->port_name,
2694 WWN_SIZE))
2695 continue;
2696
2697 found++;
2698
d8b45213
AV
2699 /* Update port state. */
2700 memcpy(fcport->fabric_port_name,
2701 new_fcport->fabric_port_name, WWN_SIZE);
2702 fcport->fp_speed = new_fcport->fp_speed;
2703
1da177e4
LT
2704 /*
2705 * If address the same and state FCS_ONLINE, nothing
2706 * changed.
2707 */
2708 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2709 atomic_read(&fcport->state) == FCS_ONLINE) {
2710 break;
2711 }
2712
2713 /*
2714 * If device was not a fabric device before.
2715 */
2716 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2717 fcport->d_id.b24 = new_fcport->d_id.b24;
2718 fcport->loop_id = FC_NO_LOOP_ID;
2719 fcport->flags |= (FCF_FABRIC_DEVICE |
2720 FCF_LOGIN_NEEDED);
1da177e4
LT
2721 break;
2722 }
2723
2724 /*
2725 * Port ID changed or device was marked to be updated;
2726 * Log it out if still logged in and mark it for
2727 * relogin later.
2728 */
2729 fcport->d_id.b24 = new_fcport->d_id.b24;
2730 fcport->flags |= FCF_LOGIN_NEEDED;
2731 if (fcport->loop_id != FC_NO_LOOP_ID &&
2732 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2733 fcport->port_type != FCT_INITIATOR &&
2734 fcport->port_type != FCT_BROADCAST) {
e315cd28 2735 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
1c7c6357
AV
2736 fcport->d_id.b.domain, fcport->d_id.b.area,
2737 fcport->d_id.b.al_pa);
1da177e4
LT
2738 fcport->loop_id = FC_NO_LOOP_ID;
2739 }
2740
2741 break;
2742 }
2743
2744 if (found)
2745 continue;
1da177e4
LT
2746 /* If device was not in our fcports list, then add it. */
2747 list_add_tail(&new_fcport->list, new_fcports);
2748
2749 /* Allocate a new replacement fcport. */
2750 nxt_d_id.b24 = new_fcport->d_id.b24;
e315cd28 2751 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1da177e4 2752 if (new_fcport == NULL) {
c9475cb0 2753 kfree(swl);
1da177e4
LT
2754 return (QLA_MEMORY_ALLOC_FAILED);
2755 }
2756 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2757 new_fcport->d_id.b24 = nxt_d_id.b24;
2758 }
2759
c9475cb0
JJ
2760 kfree(swl);
2761 kfree(new_fcport);
1da177e4 2762
1da177e4
LT
2763 return (rval);
2764}
2765
2766/*
2767 * qla2x00_find_new_loop_id
2768 * Scan through our port list and find a new usable loop ID.
2769 *
2770 * Input:
2771 * ha: adapter state pointer.
2772 * dev: port structure pointer.
2773 *
2774 * Returns:
2775 * qla2x00 local function return status code.
2776 *
2777 * Context:
2778 * Kernel context.
2779 */
413975a0 2780static int
e315cd28 2781qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
1da177e4
LT
2782{
2783 int rval;
2784 int found;
2785 fc_port_t *fcport;
2786 uint16_t first_loop_id;
e315cd28
AC
2787 struct qla_hw_data *ha = vha->hw;
2788 struct scsi_qla_host *vp;
ee546b6e 2789 struct scsi_qla_host *tvp;
1da177e4
LT
2790
2791 rval = QLA_SUCCESS;
2792
2793 /* Save starting loop ID. */
2794 first_loop_id = dev->loop_id;
2795
2796 for (;;) {
2797 /* Skip loop ID if already used by adapter. */
e315cd28 2798 if (dev->loop_id == vha->loop_id)
1da177e4 2799 dev->loop_id++;
1da177e4
LT
2800
2801 /* Skip reserved loop IDs. */
e315cd28 2802 while (qla2x00_is_reserved_id(vha, dev->loop_id))
1da177e4 2803 dev->loop_id++;
1da177e4
LT
2804
2805 /* Reset loop ID if passed the end. */
e315cd28 2806 if (dev->loop_id > ha->max_loop_id) {
1da177e4
LT
2807 /* first loop ID. */
2808 dev->loop_id = ha->min_external_loopid;
2809 }
2810
2811 /* Check for loop ID being already in use. */
2812 found = 0;
2813 fcport = NULL;
ee546b6e 2814 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
e315cd28
AC
2815 list_for_each_entry(fcport, &vp->vp_fcports, list) {
2816 if (fcport->loop_id == dev->loop_id &&
2817 fcport != dev) {
2818 /* ID possibly in use */
2819 found++;
2820 break;
2821 }
1da177e4 2822 }
e315cd28
AC
2823 if (found)
2824 break;
1da177e4
LT
2825 }
2826
2827 /* If not in use then it is free to use. */
2828 if (!found) {
2829 break;
2830 }
2831
2832 /* ID in use. Try next value. */
2833 dev->loop_id++;
2834
2835 /* If wrap around. No free ID to use. */
2836 if (dev->loop_id == first_loop_id) {
2837 dev->loop_id = FC_NO_LOOP_ID;
2838 rval = QLA_FUNCTION_FAILED;
2839 break;
2840 }
2841 }
2842
2843 return (rval);
2844}
2845
2846/*
2847 * qla2x00_device_resync
2848 * Marks devices in the database that needs resynchronization.
2849 *
2850 * Input:
2851 * ha = adapter block pointer.
2852 *
2853 * Context:
2854 * Kernel context.
2855 */
2856static int
e315cd28 2857qla2x00_device_resync(scsi_qla_host_t *vha)
1da177e4
LT
2858{
2859 int rval;
1da177e4
LT
2860 uint32_t mask;
2861 fc_port_t *fcport;
2862 uint32_t rscn_entry;
2863 uint8_t rscn_out_iter;
2864 uint8_t format;
2865 port_id_t d_id;
2866
2867 rval = QLA_RSCNS_HANDLED;
2868
e315cd28
AC
2869 while (vha->rscn_out_ptr != vha->rscn_in_ptr ||
2870 vha->flags.rscn_queue_overflow) {
1da177e4 2871
e315cd28 2872 rscn_entry = vha->rscn_queue[vha->rscn_out_ptr];
1da177e4
LT
2873 format = MSB(MSW(rscn_entry));
2874 d_id.b.domain = LSB(MSW(rscn_entry));
2875 d_id.b.area = MSB(LSW(rscn_entry));
2876 d_id.b.al_pa = LSB(LSW(rscn_entry));
2877
2878 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2879 "[%02x/%02x%02x%02x].\n",
e315cd28 2880 vha->host_no, vha->rscn_out_ptr, format, d_id.b.domain,
1da177e4
LT
2881 d_id.b.area, d_id.b.al_pa));
2882
e315cd28
AC
2883 vha->rscn_out_ptr++;
2884 if (vha->rscn_out_ptr == MAX_RSCN_COUNT)
2885 vha->rscn_out_ptr = 0;
1da177e4
LT
2886
2887 /* Skip duplicate entries. */
e315cd28
AC
2888 for (rscn_out_iter = vha->rscn_out_ptr;
2889 !vha->flags.rscn_queue_overflow &&
2890 rscn_out_iter != vha->rscn_in_ptr;
1da177e4
LT
2891 rscn_out_iter = (rscn_out_iter ==
2892 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2893
e315cd28 2894 if (rscn_entry != vha->rscn_queue[rscn_out_iter])
1da177e4
LT
2895 break;
2896
2897 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
e315cd28 2898 "entry found at [%d].\n", vha->host_no,
1da177e4
LT
2899 rscn_out_iter));
2900
e315cd28 2901 vha->rscn_out_ptr = rscn_out_iter;
1da177e4
LT
2902 }
2903
2904 /* Queue overflow, set switch default case. */
e315cd28 2905 if (vha->flags.rscn_queue_overflow) {
1da177e4 2906 DEBUG(printk("scsi(%ld): device_resync: rscn "
e315cd28 2907 "overflow.\n", vha->host_no));
1da177e4
LT
2908
2909 format = 3;
e315cd28 2910 vha->flags.rscn_queue_overflow = 0;
1da177e4
LT
2911 }
2912
2913 switch (format) {
2914 case 0:
1da177e4
LT
2915 mask = 0xffffff;
2916 break;
2917 case 1:
2918 mask = 0xffff00;
2919 break;
2920 case 2:
2921 mask = 0xff0000;
2922 break;
2923 default:
2924 mask = 0x0;
2925 d_id.b24 = 0;
e315cd28 2926 vha->rscn_out_ptr = vha->rscn_in_ptr;
1da177e4
LT
2927 break;
2928 }
2929
2930 rval = QLA_SUCCESS;
2931
e315cd28 2932 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
2933 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2934 (fcport->d_id.b24 & mask) != d_id.b24 ||
2935 fcport->port_type == FCT_BROADCAST)
2936 continue;
2937
2938 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2939 if (format != 3 ||
2940 fcport->port_type != FCT_INITIATOR) {
e315cd28 2941 qla2x00_mark_device_lost(vha, fcport,
d97994dc 2942 0, 0);
1da177e4
LT
2943 }
2944 }
1da177e4
LT
2945 }
2946 }
2947 return (rval);
2948}
2949
2950/*
2951 * qla2x00_fabric_dev_login
2952 * Login fabric target device and update FC port database.
2953 *
2954 * Input:
2955 * ha: adapter state pointer.
2956 * fcport: port structure list pointer.
2957 * next_loopid: contains value of a new loop ID that can be used
2958 * by the next login attempt.
2959 *
2960 * Returns:
2961 * qla2x00 local function return status code.
2962 *
2963 * Context:
2964 * Kernel context.
2965 */
2966static int
e315cd28 2967qla2x00_fabric_dev_login(scsi_qla_host_t *vha, fc_port_t *fcport,
1da177e4
LT
2968 uint16_t *next_loopid)
2969{
2970 int rval;
2971 int retry;
0107109e 2972 uint8_t opts;
e315cd28 2973 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
2974
2975 rval = QLA_SUCCESS;
2976 retry = 0;
2977
e315cd28 2978 rval = qla2x00_fabric_login(vha, fcport, next_loopid);
1da177e4 2979 if (rval == QLA_SUCCESS) {
0107109e
AV
2980 /* Send an ADISC to tape devices.*/
2981 opts = 0;
2982 if (fcport->flags & FCF_TAPE_PRESENT)
2983 opts |= BIT_1;
e315cd28 2984 rval = qla2x00_get_port_database(vha, fcport, opts);
1da177e4 2985 if (rval != QLA_SUCCESS) {
e315cd28 2986 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
1c7c6357
AV
2987 fcport->d_id.b.domain, fcport->d_id.b.area,
2988 fcport->d_id.b.al_pa);
e315cd28 2989 qla2x00_mark_device_lost(vha, fcport, 1, 0);
1da177e4 2990 } else {
e315cd28 2991 qla2x00_update_fcport(vha, fcport);
1da177e4
LT
2992 }
2993 }
2994
2995 return (rval);
2996}
2997
2998/*
2999 * qla2x00_fabric_login
3000 * Issue fabric login command.
3001 *
3002 * Input:
3003 * ha = adapter block pointer.
3004 * device = pointer to FC device type structure.
3005 *
3006 * Returns:
3007 * 0 - Login successfully
3008 * 1 - Login failed
3009 * 2 - Initiator device
3010 * 3 - Fatal error
3011 */
3012int
e315cd28 3013qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
1da177e4
LT
3014 uint16_t *next_loopid)
3015{
3016 int rval;
3017 int retry;
3018 uint16_t tmp_loopid;
3019 uint16_t mb[MAILBOX_REGISTER_COUNT];
e315cd28 3020 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
3021
3022 retry = 0;
3023 tmp_loopid = 0;
3024
3025 for (;;) {
3026 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
3027 "for port %02x%02x%02x.\n",
e315cd28 3028 vha->host_no, fcport->loop_id, fcport->d_id.b.domain,
1da177e4
LT
3029 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3030
3031 /* Login fcport on switch. */
e315cd28 3032 ha->isp_ops->fabric_login(vha, fcport->loop_id,
1da177e4
LT
3033 fcport->d_id.b.domain, fcport->d_id.b.area,
3034 fcport->d_id.b.al_pa, mb, BIT_0);
3035 if (mb[0] == MBS_PORT_ID_USED) {
3036 /*
3037 * Device has another loop ID. The firmware team
0107109e
AV
3038 * recommends the driver perform an implicit login with
3039 * the specified ID again. The ID we just used is save
3040 * here so we return with an ID that can be tried by
3041 * the next login.
1da177e4
LT
3042 */
3043 retry++;
3044 tmp_loopid = fcport->loop_id;
3045 fcport->loop_id = mb[1];
3046
3047 DEBUG(printk("Fabric Login: port in use - next "
3048 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3049 fcport->loop_id, fcport->d_id.b.domain,
3050 fcport->d_id.b.area, fcport->d_id.b.al_pa));
3051
3052 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3053 /*
3054 * Login succeeded.
3055 */
3056 if (retry) {
3057 /* A retry occurred before. */
3058 *next_loopid = tmp_loopid;
3059 } else {
3060 /*
3061 * No retry occurred before. Just increment the
3062 * ID value for next login.
3063 */
3064 *next_loopid = (fcport->loop_id + 1);
3065 }
3066
3067 if (mb[1] & BIT_0) {
3068 fcport->port_type = FCT_INITIATOR;
3069 } else {
3070 fcport->port_type = FCT_TARGET;
3071 if (mb[1] & BIT_1) {
3072 fcport->flags |= FCF_TAPE_PRESENT;
3073 }
3074 }
3075
ad3e0eda
AV
3076 if (mb[10] & BIT_0)
3077 fcport->supported_classes |= FC_COS_CLASS2;
3078 if (mb[10] & BIT_1)
3079 fcport->supported_classes |= FC_COS_CLASS3;
3080
1da177e4
LT
3081 rval = QLA_SUCCESS;
3082 break;
3083 } else if (mb[0] == MBS_LOOP_ID_USED) {
3084 /*
3085 * Loop ID already used, try next loop ID.
3086 */
3087 fcport->loop_id++;
e315cd28 3088 rval = qla2x00_find_new_loop_id(vha, fcport);
1da177e4
LT
3089 if (rval != QLA_SUCCESS) {
3090 /* Ran out of loop IDs to use */
3091 break;
3092 }
3093 } else if (mb[0] == MBS_COMMAND_ERROR) {
3094 /*
3095 * Firmware possibly timed out during login. If NO
3096 * retries are left to do then the device is declared
3097 * dead.
3098 */
3099 *next_loopid = fcport->loop_id;
e315cd28 3100 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
1c7c6357
AV
3101 fcport->d_id.b.domain, fcport->d_id.b.area,
3102 fcport->d_id.b.al_pa);
e315cd28 3103 qla2x00_mark_device_lost(vha, fcport, 1, 0);
1da177e4
LT
3104
3105 rval = 1;
3106 break;
3107 } else {
3108 /*
3109 * unrecoverable / not handled error
3110 */
3111 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
fa2a1ce5 3112 "loop_id=%x jiffies=%lx.\n",
e315cd28 3113 __func__, vha->host_no, mb[0],
1da177e4
LT
3114 fcport->d_id.b.domain, fcport->d_id.b.area,
3115 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3116
3117 *next_loopid = fcport->loop_id;
e315cd28 3118 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
1c7c6357
AV
3119 fcport->d_id.b.domain, fcport->d_id.b.area,
3120 fcport->d_id.b.al_pa);
1da177e4 3121 fcport->loop_id = FC_NO_LOOP_ID;
0eedfcf0 3122 fcport->login_retry = 0;
1da177e4
LT
3123
3124 rval = 3;
3125 break;
3126 }
3127 }
3128
3129 return (rval);
3130}
3131
3132/*
3133 * qla2x00_local_device_login
3134 * Issue local device login command.
3135 *
3136 * Input:
3137 * ha = adapter block pointer.
3138 * loop_id = loop id of device to login to.
3139 *
3140 * Returns (Where's the #define!!!!):
3141 * 0 - Login successfully
3142 * 1 - Login failed
3143 * 3 - Fatal error
3144 */
3145int
e315cd28 3146qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
1da177e4
LT
3147{
3148 int rval;
3149 uint16_t mb[MAILBOX_REGISTER_COUNT];
3150
3151 memset(mb, 0, sizeof(mb));
e315cd28 3152 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
1da177e4
LT
3153 if (rval == QLA_SUCCESS) {
3154 /* Interrogate mailbox registers for any errors */
3155 if (mb[0] == MBS_COMMAND_ERROR)
3156 rval = 1;
3157 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3158 /* device not in PCB table */
3159 rval = 3;
3160 }
3161
3162 return (rval);
3163}
3164
3165/*
3166 * qla2x00_loop_resync
3167 * Resync with fibre channel devices.
3168 *
3169 * Input:
3170 * ha = adapter block pointer.
3171 *
3172 * Returns:
3173 * 0 = success
3174 */
3175int
e315cd28 3176qla2x00_loop_resync(scsi_qla_host_t *vha)
1da177e4 3177{
73208dfd 3178 int rval = QLA_SUCCESS;
1da177e4 3179 uint32_t wait_time;
67c2e93a
AC
3180 struct req_que *req;
3181 struct rsp_que *rsp;
3182
3183 if (ql2xmultique_tag)
3184 req = vha->hw->req_q_map[0];
3185 else
3186 req = vha->req;
3187 rsp = req->rsp;
1da177e4 3188
e315cd28
AC
3189 atomic_set(&vha->loop_state, LOOP_UPDATE);
3190 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
3191 if (vha->flags.online) {
3192 if (!(rval = qla2x00_fw_ready(vha))) {
1da177e4
LT
3193 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3194 wait_time = 256;
3195 do {
e315cd28 3196 atomic_set(&vha->loop_state, LOOP_UPDATE);
1da177e4 3197
0107109e 3198 /* Issue a marker after FW becomes ready. */
73208dfd
AC
3199 qla2x00_marker(vha, req, rsp, 0, 0,
3200 MK_SYNC_ALL);
e315cd28 3201 vha->marker_needed = 0;
1da177e4
LT
3202
3203 /* Remap devices on Loop. */
e315cd28 3204 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1da177e4 3205
e315cd28 3206 qla2x00_configure_loop(vha);
1da177e4 3207 wait_time--;
e315cd28
AC
3208 } while (!atomic_read(&vha->loop_down_timer) &&
3209 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
3210 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
3211 &vha->dpc_flags)));
1da177e4 3212 }
1da177e4
LT
3213 }
3214
e315cd28 3215 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
1da177e4 3216 return (QLA_FUNCTION_FAILED);
1da177e4 3217
e315cd28 3218 if (rval)
1da177e4 3219 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
1da177e4
LT
3220
3221 return (rval);
3222}
3223
d97994dc 3224void
e315cd28 3225qla2x00_update_fcports(scsi_qla_host_t *vha)
d97994dc
AV
3226{
3227 fc_port_t *fcport;
3228
3229 /* Go with deferred removal of rport references. */
e315cd28
AC
3230 list_for_each_entry(fcport, &vha->vp_fcports, list)
3231 if (fcport && fcport->drport &&
c9c5ced9 3232 atomic_read(&fcport->state) != FCS_UNCONFIGURED)
d97994dc
AV
3233 qla2x00_rport_del(fcport);
3234}
3235
1da177e4
LT
3236/*
3237* qla2x00_abort_isp
3238* Resets ISP and aborts all outstanding commands.
3239*
3240* Input:
3241* ha = adapter block pointer.
3242*
3243* Returns:
3244* 0 = success
3245*/
3246int
e315cd28 3247qla2x00_abort_isp(scsi_qla_host_t *vha)
1da177e4 3248{
476e8978 3249 int rval;
1da177e4 3250 uint8_t status = 0;
e315cd28
AC
3251 struct qla_hw_data *ha = vha->hw;
3252 struct scsi_qla_host *vp;
ee546b6e 3253 struct scsi_qla_host *tvp;
73208dfd 3254 struct req_que *req = ha->req_q_map[0];
1da177e4 3255
e315cd28
AC
3256 if (vha->flags.online) {
3257 vha->flags.online = 0;
2533cf67 3258 ha->flags.chip_reset_done = 0;
e315cd28 3259 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
e5f5f6f7 3260 ha->qla_stats.total_isp_aborts++;
1da177e4
LT
3261
3262 qla_printk(KERN_INFO, ha,
3263 "Performing ISP error recovery - ha= %p.\n", ha);
e315cd28
AC
3264 ha->isp_ops->reset_chip(vha);
3265
3266 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
3267 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
3268 atomic_set(&vha->loop_state, LOOP_DOWN);
3269 qla2x00_mark_all_devices_lost(vha, 0);
ee546b6e 3270 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list)
e315cd28 3271 qla2x00_mark_all_devices_lost(vp, 0);
1da177e4 3272 } else {
e315cd28
AC
3273 if (!atomic_read(&vha->loop_down_timer))
3274 atomic_set(&vha->loop_down_timer,
1da177e4
LT
3275 LOOP_DOWN_TIME);
3276 }
3277
1da177e4 3278 /* Requeue all commands in outstanding command list. */
e315cd28 3279 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
1da177e4 3280
73208dfd 3281 ha->isp_ops->get_flash_version(vha, req->ring);
30c47662 3282
e315cd28 3283 ha->isp_ops->nvram_config(vha);
1da177e4 3284
e315cd28
AC
3285 if (!qla2x00_restart_isp(vha)) {
3286 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
1da177e4 3287
e315cd28 3288 if (!atomic_read(&vha->loop_down_timer)) {
1da177e4
LT
3289 /*
3290 * Issue marker command only when we are going
3291 * to start the I/O .
3292 */
e315cd28 3293 vha->marker_needed = 1;
1da177e4
LT
3294 }
3295
e315cd28 3296 vha->flags.online = 1;
1da177e4 3297
fd34f556 3298 ha->isp_ops->enable_intrs(ha);
1da177e4 3299
fa2a1ce5 3300 ha->isp_abort_cnt = 0;
e315cd28 3301 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
476e8978 3302
df613b96
AV
3303 if (ha->fce) {
3304 ha->flags.fce_enabled = 1;
3305 memset(ha->fce, 0,
3306 fce_calc_size(ha->fce_bufs));
e315cd28 3307 rval = qla2x00_enable_fce_trace(vha,
df613b96
AV
3308 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
3309 &ha->fce_bufs);
3310 if (rval) {
3311 qla_printk(KERN_WARNING, ha,
3312 "Unable to reinitialize FCE "
3313 "(%d).\n", rval);
3314 ha->flags.fce_enabled = 0;
3315 }
3316 }
436a7b11
AV
3317
3318 if (ha->eft) {
3319 memset(ha->eft, 0, EFT_SIZE);
e315cd28 3320 rval = qla2x00_enable_eft_trace(vha,
436a7b11
AV
3321 ha->eft_dma, EFT_NUM_BUFFERS);
3322 if (rval) {
3323 qla_printk(KERN_WARNING, ha,
3324 "Unable to reinitialize EFT "
3325 "(%d).\n", rval);
3326 }
3327 }
1da177e4 3328 } else { /* failed the ISP abort */
e315cd28
AC
3329 vha->flags.online = 1;
3330 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1da177e4
LT
3331 if (ha->isp_abort_cnt == 0) {
3332 qla_printk(KERN_WARNING, ha,
3333 "ISP error recovery failed - "
3334 "board disabled\n");
fa2a1ce5 3335 /*
1da177e4
LT
3336 * The next call disables the board
3337 * completely.
3338 */
e315cd28
AC
3339 ha->isp_ops->reset_adapter(vha);
3340 vha->flags.online = 0;
1da177e4 3341 clear_bit(ISP_ABORT_RETRY,
e315cd28 3342 &vha->dpc_flags);
1da177e4
LT
3343 status = 0;
3344 } else { /* schedule another ISP abort */
3345 ha->isp_abort_cnt--;
3346 DEBUG(printk("qla%ld: ISP abort - "
0107109e 3347 "retry remaining %d\n",
e315cd28 3348 vha->host_no, ha->isp_abort_cnt));
1da177e4
LT
3349 status = 1;
3350 }
3351 } else {
3352 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3353 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3354 "- retrying (%d) more times\n",
e315cd28
AC
3355 vha->host_no, ha->isp_abort_cnt));
3356 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
1da177e4
LT
3357 status = 1;
3358 }
3359 }
fa2a1ce5 3360
1da177e4
LT
3361 }
3362
e315cd28
AC
3363 if (!status) {
3364 DEBUG(printk(KERN_INFO
3365 "qla2x00_abort_isp(%ld): succeeded.\n",
3366 vha->host_no));
ee546b6e 3367 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
e315cd28
AC
3368 if (vp->vp_idx)
3369 qla2x00_vp_abort_isp(vp);
3370 }
3371 } else {
1da177e4
LT
3372 qla_printk(KERN_INFO, ha,
3373 "qla2x00_abort_isp: **** FAILED ****\n");
1da177e4
LT
3374 }
3375
3376 return(status);
3377}
3378
3379/*
3380* qla2x00_restart_isp
3381* restarts the ISP after a reset
3382*
3383* Input:
3384* ha = adapter block pointer.
3385*
3386* Returns:
3387* 0 = success
3388*/
3389static int
e315cd28 3390qla2x00_restart_isp(scsi_qla_host_t *vha)
1da177e4 3391{
c6b2fca8 3392 int status = 0;
1da177e4 3393 uint32_t wait_time;
e315cd28 3394 struct qla_hw_data *ha = vha->hw;
73208dfd
AC
3395 struct req_que *req = ha->req_q_map[0];
3396 struct rsp_que *rsp = ha->rsp_q_map[0];
1da177e4
LT
3397
3398 /* If firmware needs to be loaded */
e315cd28
AC
3399 if (qla2x00_isp_firmware(vha)) {
3400 vha->flags.online = 0;
3401 status = ha->isp_ops->chip_diag(vha);
3402 if (!status)
3403 status = qla2x00_setup_chip(vha);
1da177e4
LT
3404 }
3405
e315cd28
AC
3406 if (!status && !(status = qla2x00_init_rings(vha))) {
3407 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
2533cf67 3408 ha->flags.chip_reset_done = 1;
73208dfd
AC
3409 /* Initialize the queues in use */
3410 qla25xx_init_queues(ha);
3411
e315cd28
AC
3412 status = qla2x00_fw_ready(vha);
3413 if (!status) {
1da177e4 3414 DEBUG(printk("%s(): Start configure loop, "
744f11fd 3415 "status = %d\n", __func__, status));
0107109e
AV
3416
3417 /* Issue a marker after FW becomes ready. */
73208dfd 3418 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
0107109e 3419
e315cd28 3420 vha->flags.online = 1;
1da177e4
LT
3421 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3422 wait_time = 256;
3423 do {
e315cd28
AC
3424 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
3425 qla2x00_configure_loop(vha);
1da177e4 3426 wait_time--;
e315cd28
AC
3427 } while (!atomic_read(&vha->loop_down_timer) &&
3428 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
3429 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
3430 &vha->dpc_flags)));
1da177e4
LT
3431 }
3432
3433 /* if no cable then assume it's good */
e315cd28 3434 if ((vha->device_flags & DFLG_NO_CABLE))
1da177e4
LT
3435 status = 0;
3436
3437 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3438 __func__,
744f11fd 3439 status));
1da177e4
LT
3440 }
3441 return (status);
3442}
3443
73208dfd
AC
3444static int
3445qla25xx_init_queues(struct qla_hw_data *ha)
3446{
3447 struct rsp_que *rsp = NULL;
3448 struct req_que *req = NULL;
3449 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
3450 int ret = -1;
3451 int i;
3452
2afa19a9 3453 for (i = 1; i < ha->max_rsp_queues; i++) {
73208dfd
AC
3454 rsp = ha->rsp_q_map[i];
3455 if (rsp) {
3456 rsp->options &= ~BIT_0;
618a7523 3457 ret = qla25xx_init_rsp_que(base_vha, rsp);
73208dfd
AC
3458 if (ret != QLA_SUCCESS)
3459 DEBUG2_17(printk(KERN_WARNING
3460 "%s Rsp que:%d init failed\n", __func__,
3461 rsp->id));
3462 else
3463 DEBUG2_17(printk(KERN_INFO
3464 "%s Rsp que:%d inited\n", __func__,
3465 rsp->id));
3466 }
2afa19a9
AC
3467 }
3468 for (i = 1; i < ha->max_req_queues; i++) {
73208dfd
AC
3469 req = ha->req_q_map[i];
3470 if (req) {
29bdccbe 3471 /* Clear outstanding commands array. */
73208dfd 3472 req->options &= ~BIT_0;
618a7523 3473 ret = qla25xx_init_req_que(base_vha, req);
73208dfd
AC
3474 if (ret != QLA_SUCCESS)
3475 DEBUG2_17(printk(KERN_WARNING
3476 "%s Req que:%d init failed\n", __func__,
3477 req->id));
3478 else
3479 DEBUG2_17(printk(KERN_WARNING
29bdccbe 3480 "%s Req que:%d inited\n", __func__,
73208dfd
AC
3481 req->id));
3482 }
3483 }
3484 return ret;
3485}
3486
1da177e4
LT
3487/*
3488* qla2x00_reset_adapter
3489* Reset adapter.
3490*
3491* Input:
3492* ha = adapter block pointer.
3493*/
abbd8870 3494void
e315cd28 3495qla2x00_reset_adapter(scsi_qla_host_t *vha)
1da177e4
LT
3496{
3497 unsigned long flags = 0;
e315cd28 3498 struct qla_hw_data *ha = vha->hw;
3d71644c 3499 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1da177e4 3500
e315cd28 3501 vha->flags.online = 0;
fd34f556 3502 ha->isp_ops->disable_intrs(ha);
1da177e4 3503
1da177e4
LT
3504 spin_lock_irqsave(&ha->hardware_lock, flags);
3505 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3506 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3507 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3508 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3509 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3510}
0107109e
AV
3511
3512void
e315cd28 3513qla24xx_reset_adapter(scsi_qla_host_t *vha)
0107109e
AV
3514{
3515 unsigned long flags = 0;
e315cd28 3516 struct qla_hw_data *ha = vha->hw;
0107109e
AV
3517 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3518
e315cd28 3519 vha->flags.online = 0;
fd34f556 3520 ha->isp_ops->disable_intrs(ha);
0107109e
AV
3521
3522 spin_lock_irqsave(&ha->hardware_lock, flags);
3523 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3524 RD_REG_DWORD(&reg->hccr);
3525 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3526 RD_REG_DWORD(&reg->hccr);
3527 spin_unlock_irqrestore(&ha->hardware_lock, flags);
09ff36d3
AV
3528
3529 if (IS_NOPOLLING_TYPE(ha))
3530 ha->isp_ops->enable_intrs(ha);
0107109e
AV
3531}
3532
4e08df3f
DM
3533/* On sparc systems, obtain port and node WWN from firmware
3534 * properties.
3535 */
e315cd28
AC
3536static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
3537 struct nvram_24xx *nv)
4e08df3f
DM
3538{
3539#ifdef CONFIG_SPARC
e315cd28 3540 struct qla_hw_data *ha = vha->hw;
4e08df3f 3541 struct pci_dev *pdev = ha->pdev;
15576bc8
DM
3542 struct device_node *dp = pci_device_to_OF_node(pdev);
3543 const u8 *val;
4e08df3f
DM
3544 int len;
3545
3546 val = of_get_property(dp, "port-wwn", &len);
3547 if (val && len >= WWN_SIZE)
3548 memcpy(nv->port_name, val, WWN_SIZE);
3549
3550 val = of_get_property(dp, "node-wwn", &len);
3551 if (val && len >= WWN_SIZE)
3552 memcpy(nv->node_name, val, WWN_SIZE);
3553#endif
3554}
3555
0107109e 3556int
e315cd28 3557qla24xx_nvram_config(scsi_qla_host_t *vha)
0107109e 3558{
4e08df3f 3559 int rval;
0107109e
AV
3560 struct init_cb_24xx *icb;
3561 struct nvram_24xx *nv;
3562 uint32_t *dptr;
3563 uint8_t *dptr1, *dptr2;
3564 uint32_t chksum;
3565 uint16_t cnt;
e315cd28 3566 struct qla_hw_data *ha = vha->hw;
0107109e 3567
4e08df3f 3568 rval = QLA_SUCCESS;
0107109e 3569 icb = (struct init_cb_24xx *)ha->init_cb;
281afe19 3570 nv = ha->nvram;
0107109e
AV
3571
3572 /* Determine NVRAM starting address. */
3573 ha->nvram_size = sizeof(struct nvram_24xx);
3574 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6f641790
AV
3575 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3576 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3577 if (PCI_FUNC(ha->pdev->devfn)) {
0107109e 3578 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
6f641790
AV
3579 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3580 }
0107109e 3581
281afe19
SJ
3582 /* Get VPD data into cache */
3583 ha->vpd = ha->nvram + VPD_OFFSET;
e315cd28 3584 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
281afe19
SJ
3585 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
3586
3587 /* Get NVRAM data into cache and calculate checksum. */
0107109e 3588 dptr = (uint32_t *)nv;
e315cd28 3589 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
0107109e
AV
3590 ha->nvram_size);
3591 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3592 chksum += le32_to_cpu(*dptr++);
3593
7640335e 3594 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
281afe19 3595 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
0107109e
AV
3596
3597 /* Bad NVRAM data, set defaults parameters. */
3598 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3599 || nv->id[3] != ' ' ||
3600 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3601 /* Reset NVRAM data. */
3602 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3603 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3604 le16_to_cpu(nv->nvram_version));
4e08df3f
DM
3605 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3606 "invalid -- WWPN) defaults.\n");
3607
3608 /*
3609 * Set default initialization control block.
3610 */
3611 memset(nv, 0, ha->nvram_size);
3612 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3613 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3614 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3615 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3616 nv->exchange_count = __constant_cpu_to_le16(0);
3617 nv->hard_address = __constant_cpu_to_le16(124);
3618 nv->port_name[0] = 0x21;
3619 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3620 nv->port_name[2] = 0x00;
3621 nv->port_name[3] = 0xe0;
3622 nv->port_name[4] = 0x8b;
3623 nv->port_name[5] = 0x1c;
3624 nv->port_name[6] = 0x55;
3625 nv->port_name[7] = 0x86;
3626 nv->node_name[0] = 0x20;
3627 nv->node_name[1] = 0x00;
3628 nv->node_name[2] = 0x00;
3629 nv->node_name[3] = 0xe0;
3630 nv->node_name[4] = 0x8b;
3631 nv->node_name[5] = 0x1c;
3632 nv->node_name[6] = 0x55;
3633 nv->node_name[7] = 0x86;
e315cd28 3634 qla24xx_nvram_wwn_from_ofw(vha, nv);
4e08df3f
DM
3635 nv->login_retry_count = __constant_cpu_to_le16(8);
3636 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3637 nv->login_timeout = __constant_cpu_to_le16(0);
3638 nv->firmware_options_1 =
3639 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3640 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3641 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3642 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3643 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3644 nv->efi_parameters = __constant_cpu_to_le32(0);
3645 nv->reset_delay = 5;
3646 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3647 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3648 nv->link_down_timeout = __constant_cpu_to_le16(30);
3649
3650 rval = 1;
0107109e
AV
3651 }
3652
3653 /* Reset Initialization control block */
e315cd28 3654 memset(icb, 0, ha->init_cb_size);
0107109e
AV
3655
3656 /* Copy 1st segment. */
3657 dptr1 = (uint8_t *)icb;
3658 dptr2 = (uint8_t *)&nv->version;
3659 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3660 while (cnt--)
3661 *dptr1++ = *dptr2++;
3662
3663 icb->login_retry_count = nv->login_retry_count;
3ea66e28 3664 icb->link_down_on_nos = nv->link_down_on_nos;
0107109e
AV
3665
3666 /* Copy 2nd segment. */
3667 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3668 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3669 cnt = (uint8_t *)&icb->reserved_3 -
3670 (uint8_t *)&icb->interrupt_delay_timer;
3671 while (cnt--)
3672 *dptr1++ = *dptr2++;
3673
3674 /*
3675 * Setup driver NVRAM options.
3676 */
e315cd28 3677 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
9bb9fcf2 3678 "QLA2462");
0107109e 3679
5341e868
AV
3680 /* Use alternate WWN? */
3681 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3682 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3683 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3684 }
3685
0107109e 3686 /* Prepare nodename */
fd0e7e4d 3687 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
0107109e
AV
3688 /*
3689 * Firmware will apply the following mask if the nodename was
3690 * not provided.
3691 */
3692 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3693 icb->node_name[0] &= 0xF0;
3694 }
3695
3696 /* Set host adapter parameters. */
3697 ha->flags.disable_risc_code_load = 0;
0c8c39af
AV
3698 ha->flags.enable_lip_reset = 0;
3699 ha->flags.enable_lip_full_login =
3700 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3701 ha->flags.enable_target_reset =
3702 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
0107109e 3703 ha->flags.enable_led_scheme = 0;
d4c760c2 3704 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
0107109e 3705
fd0e7e4d
AV
3706 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3707 (BIT_6 | BIT_5 | BIT_4)) >> 4;
0107109e
AV
3708
3709 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3710 sizeof(ha->fw_seriallink_options24));
3711
3712 /* save HBA serial number */
3713 ha->serial0 = icb->port_name[5];
3714 ha->serial1 = icb->port_name[6];
3715 ha->serial2 = icb->port_name[7];
e315cd28
AC
3716 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
3717 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
0107109e 3718
bc8fb3cb
AV
3719 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3720
0107109e
AV
3721 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3722
3723 /* Set minimum login_timeout to 4 seconds. */
3724 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3725 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3726 if (le16_to_cpu(nv->login_timeout) < 4)
3727 nv->login_timeout = __constant_cpu_to_le16(4);
3728 ha->login_timeout = le16_to_cpu(nv->login_timeout);
c6852c4c 3729 icb->login_timeout = nv->login_timeout;
0107109e 3730
00a537b8
AV
3731 /* Set minimum RATOV to 100 tenths of a second. */
3732 ha->r_a_tov = 100;
0107109e
AV
3733
3734 ha->loop_reset_delay = nv->reset_delay;
3735
3736 /* Link Down Timeout = 0:
3737 *
3738 * When Port Down timer expires we will start returning
3739 * I/O's to OS with "DID_NO_CONNECT".
3740 *
3741 * Link Down Timeout != 0:
3742 *
3743 * The driver waits for the link to come up after link down
3744 * before returning I/Os to OS with "DID_NO_CONNECT".
3745 */
3746 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3747 ha->loop_down_abort_time =
3748 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3749 } else {
3750 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3751 ha->loop_down_abort_time =
3752 (LOOP_DOWN_TIME - ha->link_down_timeout);
3753 }
3754
3755 /* Need enough time to try and get the port back. */
3756 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3757 if (qlport_down_retry)
3758 ha->port_down_retry_count = qlport_down_retry;
3759
3760 /* Set login_retry_count */
3761 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3762 if (ha->port_down_retry_count ==
3763 le16_to_cpu(nv->port_down_retry_count) &&
3764 ha->port_down_retry_count > 3)
3765 ha->login_retry_count = ha->port_down_retry_count;
3766 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3767 ha->login_retry_count = ha->port_down_retry_count;
3768 if (ql2xloginretrycount)
3769 ha->login_retry_count = ql2xloginretrycount;
3770
4fdfefe5 3771 /* Enable ZIO. */
e315cd28 3772 if (!vha->flags.init_done) {
4fdfefe5
AV
3773 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3774 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3775 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3776 le16_to_cpu(icb->interrupt_delay_timer): 2;
3777 }
3778 icb->firmware_options_2 &= __constant_cpu_to_le32(
3779 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
e315cd28 3780 vha->flags.process_response_queue = 0;
4fdfefe5 3781 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4a59f71d
AV
3782 ha->zio_mode = QLA_ZIO_MODE_6;
3783
4fdfefe5 3784 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
e315cd28 3785 "(%d us).\n", vha->host_no, ha->zio_mode,
4fdfefe5
AV
3786 ha->zio_timer * 100));
3787 qla_printk(KERN_INFO, ha,
3788 "ZIO mode %d enabled; timer delay (%d us).\n",
3789 ha->zio_mode, ha->zio_timer * 100);
3790
3791 icb->firmware_options_2 |= cpu_to_le32(
3792 (uint32_t)ha->zio_mode);
3793 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
e315cd28 3794 vha->flags.process_response_queue = 1;
4fdfefe5
AV
3795 }
3796
4e08df3f
DM
3797 if (rval) {
3798 DEBUG2_3(printk(KERN_WARNING
e315cd28 3799 "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
4e08df3f
DM
3800 }
3801 return (rval);
0107109e
AV
3802}
3803
413975a0 3804static int
e315cd28 3805qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr)
d1c61909 3806{
73208dfd 3807 int rval = QLA_SUCCESS;
d1c61909
AV
3808 int segments, fragment;
3809 uint32_t faddr;
3810 uint32_t *dcode, dlen;
3811 uint32_t risc_addr;
3812 uint32_t risc_size;
3813 uint32_t i;
e315cd28 3814 struct qla_hw_data *ha = vha->hw;
73208dfd 3815 struct req_que *req = ha->req_q_map[0];
eaac30be
AV
3816
3817 qla_printk(KERN_INFO, ha,
3818 "FW: Loading from flash (%x)...\n", ha->flt_region_fw);
3819
d1c61909
AV
3820 rval = QLA_SUCCESS;
3821
3822 segments = FA_RISC_CODE_SEGMENTS;
c00d8994 3823 faddr = ha->flt_region_fw;
73208dfd 3824 dcode = (uint32_t *)req->ring;
d1c61909
AV
3825 *srisc_addr = 0;
3826
3827 /* Validate firmware image by checking version. */
e315cd28 3828 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
d1c61909
AV
3829 for (i = 0; i < 4; i++)
3830 dcode[i] = be32_to_cpu(dcode[i]);
3831 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3832 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3833 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3834 dcode[3] == 0)) {
3835 qla_printk(KERN_WARNING, ha,
3836 "Unable to verify integrity of flash firmware image!\n");
3837 qla_printk(KERN_WARNING, ha,
3838 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3839 dcode[1], dcode[2], dcode[3]);
3840
3841 return QLA_FUNCTION_FAILED;
3842 }
3843
3844 while (segments && rval == QLA_SUCCESS) {
3845 /* Read segment's load information. */
e315cd28 3846 qla24xx_read_flash_data(vha, dcode, faddr, 4);
d1c61909
AV
3847
3848 risc_addr = be32_to_cpu(dcode[2]);
3849 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3850 risc_size = be32_to_cpu(dcode[3]);
3851
3852 fragment = 0;
3853 while (risc_size > 0 && rval == QLA_SUCCESS) {
3854 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3855 if (dlen > risc_size)
3856 dlen = risc_size;
3857
3858 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3859 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
e315cd28 3860 vha->host_no, risc_addr, dlen, faddr));
d1c61909 3861
e315cd28 3862 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
d1c61909
AV
3863 for (i = 0; i < dlen; i++)
3864 dcode[i] = swab32(dcode[i]);
3865
73208dfd 3866 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
d1c61909
AV
3867 dlen);
3868 if (rval) {
3869 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
e315cd28 3870 "segment %d of firmware\n", vha->host_no,
d1c61909
AV
3871 fragment));
3872 qla_printk(KERN_WARNING, ha,
3873 "[ERROR] Failed to load segment %d of "
3874 "firmware\n", fragment);
3875 break;
3876 }
3877
3878 faddr += dlen;
3879 risc_addr += dlen;
3880 risc_size -= dlen;
3881 fragment++;
3882 }
3883
3884 /* Next segment. */
3885 segments--;
3886 }
3887
3888 return rval;
3889}
3890
d1c61909
AV
3891#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3892
0107109e 3893int
e315cd28 3894qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
5433383e
AV
3895{
3896 int rval;
3897 int i, fragment;
3898 uint16_t *wcode, *fwcode;
3899 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3900 struct fw_blob *blob;
e315cd28 3901 struct qla_hw_data *ha = vha->hw;
73208dfd 3902 struct req_que *req = ha->req_q_map[0];
5433383e
AV
3903
3904 /* Load firmware blob. */
e315cd28 3905 blob = qla2x00_request_firmware(vha);
5433383e
AV
3906 if (!blob) {
3907 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
d1c61909
AV
3908 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3909 "from: " QLA_FW_URL ".\n");
5433383e
AV
3910 return QLA_FUNCTION_FAILED;
3911 }
3912
3913 rval = QLA_SUCCESS;
3914
73208dfd 3915 wcode = (uint16_t *)req->ring;
5433383e
AV
3916 *srisc_addr = 0;
3917 fwcode = (uint16_t *)blob->fw->data;
3918 fwclen = 0;
3919
3920 /* Validate firmware image by checking version. */
3921 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3922 qla_printk(KERN_WARNING, ha,
3923 "Unable to verify integrity of firmware image (%Zd)!\n",
3924 blob->fw->size);
3925 goto fail_fw_integrity;
3926 }
3927 for (i = 0; i < 4; i++)
3928 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3929 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3930 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3931 wcode[2] == 0 && wcode[3] == 0)) {
3932 qla_printk(KERN_WARNING, ha,
3933 "Unable to verify integrity of firmware image!\n");
3934 qla_printk(KERN_WARNING, ha,
3935 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3936 wcode[1], wcode[2], wcode[3]);
3937 goto fail_fw_integrity;
3938 }
3939
3940 seg = blob->segs;
3941 while (*seg && rval == QLA_SUCCESS) {
3942 risc_addr = *seg;
3943 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3944 risc_size = be16_to_cpu(fwcode[3]);
3945
3946 /* Validate firmware image size. */
3947 fwclen += risc_size * sizeof(uint16_t);
3948 if (blob->fw->size < fwclen) {
3949 qla_printk(KERN_WARNING, ha,
3950 "Unable to verify integrity of firmware image "
3951 "(%Zd)!\n", blob->fw->size);
3952 goto fail_fw_integrity;
3953 }
3954
3955 fragment = 0;
3956 while (risc_size > 0 && rval == QLA_SUCCESS) {
3957 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3958 if (wlen > risc_size)
3959 wlen = risc_size;
3960
3961 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
e315cd28 3962 "addr %x, number of words 0x%x.\n", vha->host_no,
5433383e
AV
3963 risc_addr, wlen));
3964
3965 for (i = 0; i < wlen; i++)
3966 wcode[i] = swab16(fwcode[i]);
3967
73208dfd 3968 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
5433383e
AV
3969 wlen);
3970 if (rval) {
3971 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
e315cd28 3972 "segment %d of firmware\n", vha->host_no,
5433383e
AV
3973 fragment));
3974 qla_printk(KERN_WARNING, ha,
3975 "[ERROR] Failed to load segment %d of "
3976 "firmware\n", fragment);
3977 break;
3978 }
3979
3980 fwcode += wlen;
3981 risc_addr += wlen;
3982 risc_size -= wlen;
3983 fragment++;
3984 }
3985
3986 /* Next segment. */
3987 seg++;
3988 }
3989 return rval;
3990
3991fail_fw_integrity:
3992 return QLA_FUNCTION_FAILED;
3993}
3994
eaac30be
AV
3995static int
3996qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
0107109e
AV
3997{
3998 int rval;
3999 int segments, fragment;
4000 uint32_t *dcode, dlen;
4001 uint32_t risc_addr;
4002 uint32_t risc_size;
4003 uint32_t i;
5433383e 4004 struct fw_blob *blob;
0107109e 4005 uint32_t *fwcode, fwclen;
e315cd28 4006 struct qla_hw_data *ha = vha->hw;
73208dfd 4007 struct req_que *req = ha->req_q_map[0];
0107109e 4008
5433383e 4009 /* Load firmware blob. */
e315cd28 4010 blob = qla2x00_request_firmware(vha);
5433383e
AV
4011 if (!blob) {
4012 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
d1c61909
AV
4013 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
4014 "from: " QLA_FW_URL ".\n");
4015
eaac30be 4016 return QLA_FUNCTION_FAILED;
0107109e
AV
4017 }
4018
eaac30be
AV
4019 qla_printk(KERN_INFO, ha,
4020 "FW: Loading via request-firmware...\n");
4021
0107109e
AV
4022 rval = QLA_SUCCESS;
4023
4024 segments = FA_RISC_CODE_SEGMENTS;
73208dfd 4025 dcode = (uint32_t *)req->ring;
0107109e 4026 *srisc_addr = 0;
5433383e 4027 fwcode = (uint32_t *)blob->fw->data;
0107109e
AV
4028 fwclen = 0;
4029
4030 /* Validate firmware image by checking version. */
5433383e 4031 if (blob->fw->size < 8 * sizeof(uint32_t)) {
0107109e 4032 qla_printk(KERN_WARNING, ha,
5433383e
AV
4033 "Unable to verify integrity of firmware image (%Zd)!\n",
4034 blob->fw->size);
0107109e
AV
4035 goto fail_fw_integrity;
4036 }
4037 for (i = 0; i < 4; i++)
4038 dcode[i] = be32_to_cpu(fwcode[i + 4]);
4039 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
4040 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
4041 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
4042 dcode[3] == 0)) {
4043 qla_printk(KERN_WARNING, ha,
5433383e 4044 "Unable to verify integrity of firmware image!\n");
0107109e
AV
4045 qla_printk(KERN_WARNING, ha,
4046 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
4047 dcode[1], dcode[2], dcode[3]);
4048 goto fail_fw_integrity;
4049 }
4050
4051 while (segments && rval == QLA_SUCCESS) {
4052 risc_addr = be32_to_cpu(fwcode[2]);
4053 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
4054 risc_size = be32_to_cpu(fwcode[3]);
4055
4056 /* Validate firmware image size. */
4057 fwclen += risc_size * sizeof(uint32_t);
5433383e 4058 if (blob->fw->size < fwclen) {
0107109e 4059 qla_printk(KERN_WARNING, ha,
5433383e
AV
4060 "Unable to verify integrity of firmware image "
4061 "(%Zd)!\n", blob->fw->size);
4062
0107109e
AV
4063 goto fail_fw_integrity;
4064 }
4065
4066 fragment = 0;
4067 while (risc_size > 0 && rval == QLA_SUCCESS) {
4068 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
4069 if (dlen > risc_size)
4070 dlen = risc_size;
4071
4072 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
e315cd28 4073 "addr %x, number of dwords 0x%x.\n", vha->host_no,
0107109e
AV
4074 risc_addr, dlen));
4075
4076 for (i = 0; i < dlen; i++)
4077 dcode[i] = swab32(fwcode[i]);
4078
73208dfd 4079 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
590f98e5 4080 dlen);
0107109e
AV
4081 if (rval) {
4082 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
e315cd28 4083 "segment %d of firmware\n", vha->host_no,
0107109e
AV
4084 fragment));
4085 qla_printk(KERN_WARNING, ha,
4086 "[ERROR] Failed to load segment %d of "
4087 "firmware\n", fragment);
4088 break;
4089 }
4090
4091 fwcode += dlen;
4092 risc_addr += dlen;
4093 risc_size -= dlen;
4094 fragment++;
4095 }
4096
4097 /* Next segment. */
4098 segments--;
4099 }
0107109e
AV
4100 return rval;
4101
4102fail_fw_integrity:
0107109e 4103 return QLA_FUNCTION_FAILED;
0107109e 4104}
18c6c127 4105
eaac30be
AV
4106int
4107qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
4108{
4109 int rval;
4110
4111 /*
4112 * FW Load priority:
4113 * 1) Firmware via request-firmware interface (.bin file).
4114 * 2) Firmware residing in flash.
4115 */
4116 rval = qla24xx_load_risc_blob(vha, srisc_addr);
4117 if (rval == QLA_SUCCESS)
4118 return rval;
4119
4120 return qla24xx_load_risc_flash(vha, srisc_addr);
4121}
4122
4123int
4124qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
4125{
4126 int rval;
4127
4128 /*
4129 * FW Load priority:
4130 * 1) Firmware residing in flash.
4131 * 2) Firmware via request-firmware interface (.bin file).
4132 */
4133 rval = qla24xx_load_risc_flash(vha, srisc_addr);
4134 if (rval == QLA_SUCCESS)
4135 return rval;
4136
4137 return qla24xx_load_risc_blob(vha, srisc_addr);
4138}
4139
18c6c127 4140void
e315cd28 4141qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
18c6c127
AV
4142{
4143 int ret, retries;
e315cd28 4144 struct qla_hw_data *ha = vha->hw;
18c6c127 4145
e428924c 4146 if (!IS_FWI2_CAPABLE(ha))
18c6c127 4147 return;
75edf81d
AV
4148 if (!ha->fw_major_version)
4149 return;
18c6c127 4150
e315cd28 4151 ret = qla2x00_stop_firmware(vha);
7c7f1f29
AV
4152 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
4153 retries ; retries--) {
e315cd28
AC
4154 ha->isp_ops->reset_chip(vha);
4155 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
18c6c127 4156 continue;
e315cd28 4157 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
18c6c127
AV
4158 continue;
4159 qla_printk(KERN_INFO, ha,
4160 "Attempting retry of stop-firmware command...\n");
e315cd28 4161 ret = qla2x00_stop_firmware(vha);
18c6c127
AV
4162 }
4163}
2c3dfe3f
SJ
4164
4165int
e315cd28 4166qla24xx_configure_vhba(scsi_qla_host_t *vha)
2c3dfe3f
SJ
4167{
4168 int rval = QLA_SUCCESS;
4169 uint16_t mb[MAILBOX_REGISTER_COUNT];
e315cd28
AC
4170 struct qla_hw_data *ha = vha->hw;
4171 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
67c2e93a
AC
4172 struct req_que *req;
4173 struct rsp_que *rsp;
2c3dfe3f 4174
e315cd28 4175 if (!vha->vp_idx)
2c3dfe3f
SJ
4176 return -EINVAL;
4177
e315cd28 4178 rval = qla2x00_fw_ready(base_vha);
67c2e93a
AC
4179 if (ql2xmultique_tag)
4180 req = ha->req_q_map[0];
4181 else
4182 req = vha->req;
4183 rsp = req->rsp;
4184
2c3dfe3f 4185 if (rval == QLA_SUCCESS) {
e315cd28 4186 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
73208dfd 4187 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
2c3dfe3f
SJ
4188 }
4189
e315cd28 4190 vha->flags.management_server_logged_in = 0;
2c3dfe3f
SJ
4191
4192 /* Login to SNS first */
e315cd28 4193 ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb, BIT_1);
2c3dfe3f
SJ
4194 if (mb[0] != MBS_COMMAND_COMPLETE) {
4195 DEBUG15(qla_printk(KERN_INFO, ha,
4196 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4197 "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4198 mb[0], mb[1], mb[2], mb[6], mb[7]));
4199 return (QLA_FUNCTION_FAILED);
4200 }
4201
e315cd28
AC
4202 atomic_set(&vha->loop_down_timer, 0);
4203 atomic_set(&vha->loop_state, LOOP_UP);
4204 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4205 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4206 rval = qla2x00_loop_resync(base_vha);
2c3dfe3f
SJ
4207
4208 return rval;
4209}
4d4df193
HK
4210
4211/* 84XX Support **************************************************************/
4212
4213static LIST_HEAD(qla_cs84xx_list);
4214static DEFINE_MUTEX(qla_cs84xx_mutex);
4215
4216static struct qla_chip_state_84xx *
e315cd28 4217qla84xx_get_chip(struct scsi_qla_host *vha)
4d4df193
HK
4218{
4219 struct qla_chip_state_84xx *cs84xx;
e315cd28 4220 struct qla_hw_data *ha = vha->hw;
4d4df193
HK
4221
4222 mutex_lock(&qla_cs84xx_mutex);
4223
4224 /* Find any shared 84xx chip. */
4225 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
4226 if (cs84xx->bus == ha->pdev->bus) {
4227 kref_get(&cs84xx->kref);
4228 goto done;
4229 }
4230 }
4231
4232 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
4233 if (!cs84xx)
4234 goto done;
4235
4236 kref_init(&cs84xx->kref);
4237 spin_lock_init(&cs84xx->access_lock);
4238 mutex_init(&cs84xx->fw_update_mutex);
4239 cs84xx->bus = ha->pdev->bus;
4240
4241 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
4242done:
4243 mutex_unlock(&qla_cs84xx_mutex);
4244 return cs84xx;
4245}
4246
4247static void
4248__qla84xx_chip_release(struct kref *kref)
4249{
4250 struct qla_chip_state_84xx *cs84xx =
4251 container_of(kref, struct qla_chip_state_84xx, kref);
4252
4253 mutex_lock(&qla_cs84xx_mutex);
4254 list_del(&cs84xx->list);
4255 mutex_unlock(&qla_cs84xx_mutex);
4256 kfree(cs84xx);
4257}
4258
4259void
e315cd28 4260qla84xx_put_chip(struct scsi_qla_host *vha)
4d4df193 4261{
e315cd28 4262 struct qla_hw_data *ha = vha->hw;
4d4df193
HK
4263 if (ha->cs84xx)
4264 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
4265}
4266
4267static int
e315cd28 4268qla84xx_init_chip(scsi_qla_host_t *vha)
4d4df193
HK
4269{
4270 int rval;
4271 uint16_t status[2];
e315cd28 4272 struct qla_hw_data *ha = vha->hw;
4d4df193
HK
4273
4274 mutex_lock(&ha->cs84xx->fw_update_mutex);
4275
e315cd28 4276 rval = qla84xx_verify_chip(vha, status);
4d4df193
HK
4277
4278 mutex_unlock(&ha->cs84xx->fw_update_mutex);
4279
4280 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
4281 QLA_SUCCESS;
4282}
3a03eb79
AV
4283
4284/* 81XX Support **************************************************************/
4285
4286int
4287qla81xx_nvram_config(scsi_qla_host_t *vha)
4288{
4289 int rval;
4290 struct init_cb_81xx *icb;
4291 struct nvram_81xx *nv;
4292 uint32_t *dptr;
4293 uint8_t *dptr1, *dptr2;
4294 uint32_t chksum;
4295 uint16_t cnt;
4296 struct qla_hw_data *ha = vha->hw;
4297
4298 rval = QLA_SUCCESS;
4299 icb = (struct init_cb_81xx *)ha->init_cb;
4300 nv = ha->nvram;
4301
4302 /* Determine NVRAM starting address. */
4303 ha->nvram_size = sizeof(struct nvram_81xx);
3a03eb79 4304 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3a03eb79
AV
4305
4306 /* Get VPD data into cache */
4307 ha->vpd = ha->nvram + VPD_OFFSET;
3d79038f
AV
4308 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
4309 ha->vpd_size);
3a03eb79
AV
4310
4311 /* Get NVRAM data into cache and calculate checksum. */
3d79038f 4312 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
3a03eb79 4313 ha->nvram_size);
3d79038f 4314 dptr = (uint32_t *)nv;
3a03eb79
AV
4315 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
4316 chksum += le32_to_cpu(*dptr++);
4317
7640335e 4318 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
3a03eb79
AV
4319 DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
4320
4321 /* Bad NVRAM data, set defaults parameters. */
4322 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
4323 || nv->id[3] != ' ' ||
4324 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
4325 /* Reset NVRAM data. */
4326 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
4327 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
4328 le16_to_cpu(nv->nvram_version));
4329 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
4330 "invalid -- WWPN) defaults.\n");
4331
4332 /*
4333 * Set default initialization control block.
4334 */
4335 memset(nv, 0, ha->nvram_size);
4336 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
4337 nv->version = __constant_cpu_to_le16(ICB_VERSION);
4338 nv->frame_payload_size = __constant_cpu_to_le16(2048);
4339 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
4340 nv->exchange_count = __constant_cpu_to_le16(0);
4341 nv->port_name[0] = 0x21;
4342 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
4343 nv->port_name[2] = 0x00;
4344 nv->port_name[3] = 0xe0;
4345 nv->port_name[4] = 0x8b;
4346 nv->port_name[5] = 0x1c;
4347 nv->port_name[6] = 0x55;
4348 nv->port_name[7] = 0x86;
4349 nv->node_name[0] = 0x20;
4350 nv->node_name[1] = 0x00;
4351 nv->node_name[2] = 0x00;
4352 nv->node_name[3] = 0xe0;
4353 nv->node_name[4] = 0x8b;
4354 nv->node_name[5] = 0x1c;
4355 nv->node_name[6] = 0x55;
4356 nv->node_name[7] = 0x86;
4357 nv->login_retry_count = __constant_cpu_to_le16(8);
4358 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
4359 nv->login_timeout = __constant_cpu_to_le16(0);
4360 nv->firmware_options_1 =
4361 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
4362 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
4363 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4364 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
4365 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
4366 nv->efi_parameters = __constant_cpu_to_le32(0);
4367 nv->reset_delay = 5;
4368 nv->max_luns_per_target = __constant_cpu_to_le16(128);
4369 nv->port_down_retry_count = __constant_cpu_to_le16(30);
4370 nv->link_down_timeout = __constant_cpu_to_le16(30);
4371 nv->enode_mac[0] = 0x01;
4372 nv->enode_mac[1] = 0x02;
4373 nv->enode_mac[2] = 0x03;
4374 nv->enode_mac[3] = 0x04;
4375 nv->enode_mac[4] = 0x05;
4376 nv->enode_mac[5] = 0x06 + PCI_FUNC(ha->pdev->devfn);
4377
4378 rval = 1;
4379 }
4380
4381 /* Reset Initialization control block */
4382 memset(icb, 0, sizeof(struct init_cb_81xx));
4383
4384 /* Copy 1st segment. */
4385 dptr1 = (uint8_t *)icb;
4386 dptr2 = (uint8_t *)&nv->version;
4387 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
4388 while (cnt--)
4389 *dptr1++ = *dptr2++;
4390
4391 icb->login_retry_count = nv->login_retry_count;
4392
4393 /* Copy 2nd segment. */
4394 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
4395 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
4396 cnt = (uint8_t *)&icb->reserved_5 -
4397 (uint8_t *)&icb->interrupt_delay_timer;
4398 while (cnt--)
4399 *dptr1++ = *dptr2++;
4400
4401 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
4402 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
4403 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
4404 icb->enode_mac[0] = 0x01;
4405 icb->enode_mac[1] = 0x02;
4406 icb->enode_mac[2] = 0x03;
4407 icb->enode_mac[3] = 0x04;
4408 icb->enode_mac[4] = 0x05;
4409 icb->enode_mac[5] = 0x06 + PCI_FUNC(ha->pdev->devfn);
4410 }
4411
b64b0e8f
AV
4412 /* Use extended-initialization control block. */
4413 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
4414
3a03eb79
AV
4415 /*
4416 * Setup driver NVRAM options.
4417 */
4418 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
4419 "QLE81XX");
4420
4421 /* Use alternate WWN? */
4422 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
4423 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4424 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4425 }
4426
4427 /* Prepare nodename */
4428 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
4429 /*
4430 * Firmware will apply the following mask if the nodename was
4431 * not provided.
4432 */
4433 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4434 icb->node_name[0] &= 0xF0;
4435 }
4436
4437 /* Set host adapter parameters. */
4438 ha->flags.disable_risc_code_load = 0;
4439 ha->flags.enable_lip_reset = 0;
4440 ha->flags.enable_lip_full_login =
4441 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
4442 ha->flags.enable_target_reset =
4443 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
4444 ha->flags.enable_led_scheme = 0;
4445 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
4446
4447 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
4448 (BIT_6 | BIT_5 | BIT_4)) >> 4;
4449
4450 /* save HBA serial number */
4451 ha->serial0 = icb->port_name[5];
4452 ha->serial1 = icb->port_name[6];
4453 ha->serial2 = icb->port_name[7];
4454 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4455 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
4456
4457 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
4458
4459 ha->retry_count = le16_to_cpu(nv->login_retry_count);
4460
4461 /* Set minimum login_timeout to 4 seconds. */
4462 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
4463 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
4464 if (le16_to_cpu(nv->login_timeout) < 4)
4465 nv->login_timeout = __constant_cpu_to_le16(4);
4466 ha->login_timeout = le16_to_cpu(nv->login_timeout);
4467 icb->login_timeout = nv->login_timeout;
4468
4469 /* Set minimum RATOV to 100 tenths of a second. */
4470 ha->r_a_tov = 100;
4471
4472 ha->loop_reset_delay = nv->reset_delay;
4473
4474 /* Link Down Timeout = 0:
4475 *
4476 * When Port Down timer expires we will start returning
4477 * I/O's to OS with "DID_NO_CONNECT".
4478 *
4479 * Link Down Timeout != 0:
4480 *
4481 * The driver waits for the link to come up after link down
4482 * before returning I/Os to OS with "DID_NO_CONNECT".
4483 */
4484 if (le16_to_cpu(nv->link_down_timeout) == 0) {
4485 ha->loop_down_abort_time =
4486 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
4487 } else {
4488 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
4489 ha->loop_down_abort_time =
4490 (LOOP_DOWN_TIME - ha->link_down_timeout);
4491 }
4492
4493 /* Need enough time to try and get the port back. */
4494 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
4495 if (qlport_down_retry)
4496 ha->port_down_retry_count = qlport_down_retry;
4497
4498 /* Set login_retry_count */
4499 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
4500 if (ha->port_down_retry_count ==
4501 le16_to_cpu(nv->port_down_retry_count) &&
4502 ha->port_down_retry_count > 3)
4503 ha->login_retry_count = ha->port_down_retry_count;
4504 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4505 ha->login_retry_count = ha->port_down_retry_count;
4506 if (ql2xloginretrycount)
4507 ha->login_retry_count = ql2xloginretrycount;
4508
4509 /* Enable ZIO. */
4510 if (!vha->flags.init_done) {
4511 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
4512 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4513 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
4514 le16_to_cpu(icb->interrupt_delay_timer): 2;
4515 }
4516 icb->firmware_options_2 &= __constant_cpu_to_le32(
4517 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
4518 vha->flags.process_response_queue = 0;
4519 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4520 ha->zio_mode = QLA_ZIO_MODE_6;
4521
4522 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
4523 "(%d us).\n", vha->host_no, ha->zio_mode,
4524 ha->zio_timer * 100));
4525 qla_printk(KERN_INFO, ha,
4526 "ZIO mode %d enabled; timer delay (%d us).\n",
4527 ha->zio_mode, ha->zio_timer * 100);
4528
4529 icb->firmware_options_2 |= cpu_to_le32(
4530 (uint32_t)ha->zio_mode);
4531 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
4532 vha->flags.process_response_queue = 1;
4533 }
4534
4535 if (rval) {
4536 DEBUG2_3(printk(KERN_WARNING
4537 "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
4538 }
4539 return (rval);
4540}
4541
4542void
4543qla81xx_update_fw_options(scsi_qla_host_t *ha)
4544{
4545}