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