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