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