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