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