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