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