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