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