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