Merge branch 'gfar' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc...
[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 /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
15 #ifndef EXT_IS_LUN_BIT_SET
16 #define EXT_IS_LUN_BIT_SET(P,L) \
17     (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
18 #define EXT_SET_LUN_BIT(P,L) \
19     ((P)->mask[L/8] |= (0x80 >> (L%8)))
20 #endif
21
22 /*
23 *  QLogic ISP2x00 Hardware Support Function Prototypes.
24 */
25 static int qla2x00_isp_firmware(scsi_qla_host_t *);
26 static void qla2x00_resize_request_q(scsi_qla_host_t *);
27 static int qla2x00_setup_chip(scsi_qla_host_t *);
28 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
29 static int qla2x00_init_rings(scsi_qla_host_t *);
30 static int qla2x00_fw_ready(scsi_qla_host_t *);
31 static int qla2x00_configure_hba(scsi_qla_host_t *);
32 static int qla2x00_configure_loop(scsi_qla_host_t *);
33 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
34 static int qla2x00_configure_fabric(scsi_qla_host_t *);
35 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
36 static int qla2x00_device_resync(scsi_qla_host_t *);
37 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
38     uint16_t *);
39
40 static int qla2x00_restart_isp(scsi_qla_host_t *);
41
42 static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
43
44 /****************************************************************************/
45 /*                QLogic ISP2x00 Hardware Support Functions.                */
46 /****************************************************************************/
47
48 /*
49 * qla2x00_initialize_adapter
50 *      Initialize board.
51 *
52 * Input:
53 *      ha = adapter block pointer.
54 *
55 * Returns:
56 *      0 = success
57 */
58 int
59 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
60 {
61         int     rval;
62
63         /* Clear adapter flags. */
64         ha->flags.online = 0;
65         ha->flags.reset_active = 0;
66         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
67         atomic_set(&ha->loop_state, LOOP_DOWN);
68         ha->device_flags = DFLG_NO_CABLE;
69         ha->dpc_flags = 0;
70         ha->flags.management_server_logged_in = 0;
71         ha->marker_needed = 0;
72         ha->mbx_flags = 0;
73         ha->isp_abort_cnt = 0;
74         ha->beacon_blink_led = 0;
75         set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
76
77         qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
78         rval = ha->isp_ops.pci_config(ha);
79         if (rval) {
80                 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
81                     ha->host_no));
82                 return (rval);
83         }
84
85         ha->isp_ops.reset_chip(ha);
86
87         ha->isp_ops.get_flash_version(ha, ha->request_ring);
88
89         qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
90
91         rval = ha->isp_ops.nvram_config(ha);
92         if (rval) {
93                 DEBUG2(printk("scsi(%ld): Unable to verify NVRAM data.\n",
94                     ha->host_no));
95                 return rval;
96         }
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 /*
1397 * NVRAM configuration for ISP 2xxx
1398 *
1399 * Input:
1400 *      ha                = adapter block pointer.
1401 *
1402 * Output:
1403 *      initialization control block in response_ring
1404 *      host adapters parameters in host adapter block
1405 *
1406 * Returns:
1407 *      0 = success.
1408 */
1409 int
1410 qla2x00_nvram_config(scsi_qla_host_t *ha)
1411 {
1412         uint8_t         chksum = 0;
1413         uint16_t        cnt;
1414         uint8_t         *dptr1, *dptr2;
1415         init_cb_t       *icb = ha->init_cb;
1416         nvram_t         *nv = (nvram_t *)ha->request_ring;
1417         uint8_t         *ptr = (uint8_t *)ha->request_ring;
1418         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1419
1420         /* Determine NVRAM starting address. */
1421         ha->nvram_size = sizeof(nvram_t);
1422         ha->nvram_base = 0;
1423         if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1424                 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1425                         ha->nvram_base = 0x80;
1426
1427         /* Get NVRAM data and calculate checksum. */
1428         ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1429         for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1430                 chksum += *ptr++;
1431
1432         DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1433         DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
1434             ha->nvram_size));
1435
1436         /* Bad NVRAM data, set defaults parameters. */
1437         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1438             nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1439                 /* Reset NVRAM data. */
1440                 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1441                     "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1442                     nv->nvram_version);
1443                 return QLA_FUNCTION_FAILED;
1444         }
1445
1446 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1447         /*
1448          * The SN2 does not provide BIOS emulation which means you can't change
1449          * potentially bogus BIOS settings. Force the use of default settings
1450          * for link rate and frame size.  Hope that the rest of the settings
1451          * are valid.
1452          */
1453         if (ia64_platform_is("sn2")) {
1454                 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1455                 if (IS_QLA23XX(ha))
1456                         nv->special_options[1] = BIT_7;
1457         }
1458 #endif
1459
1460         /* Reset Initialization control block */
1461         memset(icb, 0, ha->init_cb_size);
1462
1463         /*
1464          * Setup driver NVRAM options.
1465          */
1466         nv->firmware_options[0] |= (BIT_6 | BIT_1);
1467         nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1468         nv->firmware_options[1] |= (BIT_5 | BIT_0);
1469         nv->firmware_options[1] &= ~BIT_4;
1470
1471         if (IS_QLA23XX(ha)) {
1472                 nv->firmware_options[0] |= BIT_2;
1473                 nv->firmware_options[0] &= ~BIT_3;
1474                 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1475
1476                 if (IS_QLA2300(ha)) {
1477                         if (ha->fb_rev == FPM_2310) {
1478                                 strcpy(ha->model_number, "QLA2310");
1479                         } else {
1480                                 strcpy(ha->model_number, "QLA2300");
1481                         }
1482                 } else {
1483                         qla2x00_set_model_info(ha, nv->model_number,
1484                             sizeof(nv->model_number), "QLA23xx");
1485                 }
1486         } else if (IS_QLA2200(ha)) {
1487                 nv->firmware_options[0] |= BIT_2;
1488                 /*
1489                  * 'Point-to-point preferred, else loop' is not a safe
1490                  * connection mode setting.
1491                  */
1492                 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1493                     (BIT_5 | BIT_4)) {
1494                         /* Force 'loop preferred, else point-to-point'. */
1495                         nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1496                         nv->add_firmware_options[0] |= BIT_5;
1497                 }
1498                 strcpy(ha->model_number, "QLA22xx");
1499         } else /*if (IS_QLA2100(ha))*/ {
1500                 strcpy(ha->model_number, "QLA2100");
1501         }
1502
1503         /*
1504          * Copy over NVRAM RISC parameter block to initialization control block.
1505          */
1506         dptr1 = (uint8_t *)icb;
1507         dptr2 = (uint8_t *)&nv->parameter_block_version;
1508         cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1509         while (cnt--)
1510                 *dptr1++ = *dptr2++;
1511
1512         /* Copy 2nd half. */
1513         dptr1 = (uint8_t *)icb->add_firmware_options;
1514         cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1515         while (cnt--)
1516                 *dptr1++ = *dptr2++;
1517
1518         /* Use alternate WWN? */
1519         if (nv->host_p[1] & BIT_7) {
1520                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1521                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1522         }
1523
1524         /* Prepare nodename */
1525         if ((icb->firmware_options[1] & BIT_6) == 0) {
1526                 /*
1527                  * Firmware will apply the following mask if the nodename was
1528                  * not provided.
1529                  */
1530                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1531                 icb->node_name[0] &= 0xF0;
1532         }
1533
1534         /*
1535          * Set host adapter parameters.
1536          */
1537         if (nv->host_p[0] & BIT_7)
1538                 ql2xextended_error_logging = 1;
1539         ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1540         /* Always load RISC code on non ISP2[12]00 chips. */
1541         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1542                 ha->flags.disable_risc_code_load = 0;
1543         ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1544         ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1545         ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1546         ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1547         ha->flags.disable_serdes = 0;
1548
1549         ha->operating_mode =
1550             (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1551
1552         memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1553             sizeof(ha->fw_seriallink_options));
1554
1555         /* save HBA serial number */
1556         ha->serial0 = icb->port_name[5];
1557         ha->serial1 = icb->port_name[6];
1558         ha->serial2 = icb->port_name[7];
1559         ha->node_name = icb->node_name;
1560         ha->port_name = icb->port_name;
1561
1562         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1563
1564         ha->retry_count = nv->retry_count;
1565
1566         /* Set minimum login_timeout to 4 seconds. */
1567         if (nv->login_timeout < ql2xlogintimeout)
1568                 nv->login_timeout = ql2xlogintimeout;
1569         if (nv->login_timeout < 4)
1570                 nv->login_timeout = 4;
1571         ha->login_timeout = nv->login_timeout;
1572         icb->login_timeout = nv->login_timeout;
1573
1574         /* Set minimum RATOV to 200 tenths of a second. */
1575         ha->r_a_tov = 200;
1576
1577         ha->loop_reset_delay = nv->reset_delay;
1578
1579         /* Link Down Timeout = 0:
1580          *
1581          *      When Port Down timer expires we will start returning
1582          *      I/O's to OS with "DID_NO_CONNECT".
1583          *
1584          * Link Down Timeout != 0:
1585          *
1586          *       The driver waits for the link to come up after link down
1587          *       before returning I/Os to OS with "DID_NO_CONNECT".
1588          */
1589         if (nv->link_down_timeout == 0) {
1590                 ha->loop_down_abort_time =
1591                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1592         } else {
1593                 ha->link_down_timeout =  nv->link_down_timeout;
1594                 ha->loop_down_abort_time =
1595                     (LOOP_DOWN_TIME - ha->link_down_timeout);
1596         }
1597
1598         /*
1599          * Need enough time to try and get the port back.
1600          */
1601         ha->port_down_retry_count = nv->port_down_retry_count;
1602         if (qlport_down_retry)
1603                 ha->port_down_retry_count = qlport_down_retry;
1604         /* Set login_retry_count */
1605         ha->login_retry_count  = nv->retry_count;
1606         if (ha->port_down_retry_count == nv->port_down_retry_count &&
1607             ha->port_down_retry_count > 3)
1608                 ha->login_retry_count = ha->port_down_retry_count;
1609         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1610                 ha->login_retry_count = ha->port_down_retry_count;
1611         if (ql2xloginretrycount)
1612                 ha->login_retry_count = ql2xloginretrycount;
1613
1614         icb->lun_enables = __constant_cpu_to_le16(0);
1615         icb->command_resource_count = 0;
1616         icb->immediate_notify_resource_count = 0;
1617         icb->timeout = __constant_cpu_to_le16(0);
1618
1619         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1620                 /* Enable RIO */
1621                 icb->firmware_options[0] &= ~BIT_3;
1622                 icb->add_firmware_options[0] &=
1623                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1624                 icb->add_firmware_options[0] |= BIT_2;
1625                 icb->response_accumulation_timer = 3;
1626                 icb->interrupt_delay_timer = 5;
1627
1628                 ha->flags.process_response_queue = 1;
1629         } else {
1630                 /* Enable ZIO. */
1631                 if (!ha->flags.init_done) {
1632                         ha->zio_mode = icb->add_firmware_options[0] &
1633                             (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1634                         ha->zio_timer = icb->interrupt_delay_timer ?
1635                             icb->interrupt_delay_timer: 2;
1636                 }
1637                 icb->add_firmware_options[0] &=
1638                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1639                 ha->flags.process_response_queue = 0;
1640                 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1641                         ha->zio_mode = QLA_ZIO_MODE_6;
1642
1643                         DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1644                             "delay (%d us).\n", ha->host_no, ha->zio_mode,
1645                             ha->zio_timer * 100));
1646                         qla_printk(KERN_INFO, ha,
1647                             "ZIO mode %d enabled; timer delay (%d us).\n",
1648                             ha->zio_mode, ha->zio_timer * 100);
1649
1650                         icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1651                         icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1652                         ha->flags.process_response_queue = 1;
1653                 }
1654         }
1655
1656         return QLA_SUCCESS;
1657 }
1658
1659 static void
1660 qla2x00_rport_del(void *data)
1661 {
1662         fc_port_t *fcport = data;
1663         struct fc_rport *rport;
1664         unsigned long flags;
1665
1666         spin_lock_irqsave(&fcport->rport_lock, flags);
1667         rport = fcport->drport;
1668         fcport->drport = NULL;
1669         spin_unlock_irqrestore(&fcport->rport_lock, flags);
1670         if (rport)
1671                 fc_remote_port_delete(rport);
1672
1673 }
1674
1675 /**
1676  * qla2x00_alloc_fcport() - Allocate a generic fcport.
1677  * @ha: HA context
1678  * @flags: allocation flags
1679  *
1680  * Returns a pointer to the allocated fcport, or NULL, if none available.
1681  */
1682 static fc_port_t *
1683 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1684 {
1685         fc_port_t *fcport;
1686
1687         fcport = kmalloc(sizeof(fc_port_t), flags);
1688         if (fcport == NULL)
1689                 return (fcport);
1690
1691         /* Setup fcport template structure. */
1692         memset(fcport, 0, sizeof (fc_port_t));
1693         fcport->ha = ha;
1694         fcport->port_type = FCT_UNKNOWN;
1695         fcport->loop_id = FC_NO_LOOP_ID;
1696         atomic_set(&fcport->state, FCS_UNCONFIGURED);
1697         fcport->flags = FCF_RLC_SUPPORT;
1698         fcport->supported_classes = FC_COS_UNSPECIFIED;
1699         spin_lock_init(&fcport->rport_lock);
1700
1701         return (fcport);
1702 }
1703
1704 /*
1705  * qla2x00_configure_loop
1706  *      Updates Fibre Channel Device Database with what is actually on loop.
1707  *
1708  * Input:
1709  *      ha                = adapter block pointer.
1710  *
1711  * Returns:
1712  *      0 = success.
1713  *      1 = error.
1714  *      2 = database was full and device was not configured.
1715  */
1716 static int
1717 qla2x00_configure_loop(scsi_qla_host_t *ha)
1718 {
1719         int  rval;
1720         unsigned long flags, save_flags;
1721
1722         rval = QLA_SUCCESS;
1723
1724         /* Get Initiator ID */
1725         if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1726                 rval = qla2x00_configure_hba(ha);
1727                 if (rval != QLA_SUCCESS) {
1728                         DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1729                             ha->host_no));
1730                         return (rval);
1731                 }
1732         }
1733
1734         save_flags = flags = ha->dpc_flags;
1735         DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1736             ha->host_no, flags));
1737
1738         /*
1739          * If we have both an RSCN and PORT UPDATE pending then handle them
1740          * both at the same time.
1741          */
1742         clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1743         clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1744
1745         /* Determine what we need to do */
1746         if (ha->current_topology == ISP_CFG_FL &&
1747             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1748
1749                 ha->flags.rscn_queue_overflow = 1;
1750                 set_bit(RSCN_UPDATE, &flags);
1751
1752         } else if (ha->current_topology == ISP_CFG_F &&
1753             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1754
1755                 ha->flags.rscn_queue_overflow = 1;
1756                 set_bit(RSCN_UPDATE, &flags);
1757                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1758
1759         } else if (ha->current_topology == ISP_CFG_N) {
1760                 clear_bit(RSCN_UPDATE, &flags);
1761
1762         } else if (!ha->flags.online ||
1763             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1764
1765                 ha->flags.rscn_queue_overflow = 1;
1766                 set_bit(RSCN_UPDATE, &flags);
1767                 set_bit(LOCAL_LOOP_UPDATE, &flags);
1768         }
1769
1770         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1771                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1772                         rval = QLA_FUNCTION_FAILED;
1773                 } else {
1774                         rval = qla2x00_configure_local_loop(ha);
1775                 }
1776         }
1777
1778         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1779                 if (LOOP_TRANSITION(ha)) {
1780                         rval = QLA_FUNCTION_FAILED;
1781                 } else {
1782                         rval = qla2x00_configure_fabric(ha);
1783                 }
1784         }
1785
1786         if (rval == QLA_SUCCESS) {
1787                 if (atomic_read(&ha->loop_down_timer) ||
1788                     test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1789                         rval = QLA_FUNCTION_FAILED;
1790                 } else {
1791                         atomic_set(&ha->loop_state, LOOP_READY);
1792
1793                         DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1794                 }
1795         }
1796
1797         if (rval) {
1798                 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1799                     __func__, ha->host_no));
1800         } else {
1801                 DEBUG3(printk("%s: exiting normally\n", __func__));
1802         }
1803
1804         /* Restore state if a resync event occured during processing */
1805         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1806                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1807                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1808                 if (test_bit(RSCN_UPDATE, &save_flags))
1809                         set_bit(RSCN_UPDATE, &ha->dpc_flags);
1810         }
1811
1812         return (rval);
1813 }
1814
1815
1816
1817 /*
1818  * qla2x00_configure_local_loop
1819  *      Updates Fibre Channel Device Database with local loop devices.
1820  *
1821  * Input:
1822  *      ha = adapter block pointer.
1823  *
1824  * Returns:
1825  *      0 = success.
1826  */
1827 static int
1828 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1829 {
1830         int             rval, rval2;
1831         int             found_devs;
1832         int             found;
1833         fc_port_t       *fcport, *new_fcport;
1834
1835         uint16_t        index;
1836         uint16_t        entries;
1837         char            *id_iter;
1838         uint16_t        loop_id;
1839         uint8_t         domain, area, al_pa;
1840
1841         found_devs = 0;
1842         new_fcport = NULL;
1843         entries = MAX_FIBRE_DEVICES;
1844
1845         DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1846         DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1847
1848         /* Get list of logged in devices. */
1849         memset(ha->gid_list, 0, GID_LIST_SIZE);
1850         rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1851             &entries);
1852         if (rval != QLA_SUCCESS)
1853                 goto cleanup_allocation;
1854
1855         DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1856             ha->host_no, entries));
1857         DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1858             entries * sizeof(struct gid_list_info)));
1859
1860         /* Allocate temporary fcport for any new fcports discovered. */
1861         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1862         if (new_fcport == NULL) {
1863                 rval = QLA_MEMORY_ALLOC_FAILED;
1864                 goto cleanup_allocation;
1865         }
1866         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1867
1868         /*
1869          * Mark local devices that were present with FCF_DEVICE_LOST for now.
1870          */
1871         list_for_each_entry(fcport, &ha->fcports, list) {
1872                 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1873                     fcport->port_type != FCT_BROADCAST &&
1874                     (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1875
1876                         DEBUG(printk("scsi(%ld): Marking port lost, "
1877                             "loop_id=0x%04x\n",
1878                             ha->host_no, fcport->loop_id));
1879
1880                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
1881                         fcport->flags &= ~FCF_FARP_DONE;
1882                 }
1883         }
1884
1885         /* Add devices to port list. */
1886         id_iter = (char *)ha->gid_list;
1887         for (index = 0; index < entries; index++) {
1888                 domain = ((struct gid_list_info *)id_iter)->domain;
1889                 area = ((struct gid_list_info *)id_iter)->area;
1890                 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1891                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1892                         loop_id = (uint16_t)
1893                             ((struct gid_list_info *)id_iter)->loop_id_2100;
1894                 else
1895                         loop_id = le16_to_cpu(
1896                             ((struct gid_list_info *)id_iter)->loop_id);
1897                 id_iter += ha->gid_list_info_size;
1898
1899                 /* Bypass reserved domain fields. */
1900                 if ((domain & 0xf0) == 0xf0)
1901                         continue;
1902
1903                 /* Bypass if not same domain and area of adapter. */
1904                 if (area && domain &&
1905                     (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
1906                         continue;
1907
1908                 /* Bypass invalid local loop ID. */
1909                 if (loop_id > LAST_LOCAL_LOOP_ID)
1910                         continue;
1911
1912                 /* Fill in member data. */
1913                 new_fcport->d_id.b.domain = domain;
1914                 new_fcport->d_id.b.area = area;
1915                 new_fcport->d_id.b.al_pa = al_pa;
1916                 new_fcport->loop_id = loop_id;
1917                 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1918                 if (rval2 != QLA_SUCCESS) {
1919                         DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1920                             "information -- get_port_database=%x, "
1921                             "loop_id=0x%04x\n",
1922                             ha->host_no, rval2, new_fcport->loop_id));
1923                         DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
1924                             ha->host_no));
1925                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
1926                         continue;
1927                 }
1928
1929                 /* Check for matching device in port list. */
1930                 found = 0;
1931                 fcport = NULL;
1932                 list_for_each_entry(fcport, &ha->fcports, list) {
1933                         if (memcmp(new_fcport->port_name, fcport->port_name,
1934                             WWN_SIZE))
1935                                 continue;
1936
1937                         fcport->flags &= ~(FCF_FABRIC_DEVICE |
1938                             FCF_PERSISTENT_BOUND);
1939                         fcport->loop_id = new_fcport->loop_id;
1940                         fcport->port_type = new_fcport->port_type;
1941                         fcport->d_id.b24 = new_fcport->d_id.b24;
1942                         memcpy(fcport->node_name, new_fcport->node_name,
1943                             WWN_SIZE);
1944
1945                         found++;
1946                         break;
1947                 }
1948
1949                 if (!found) {
1950                         /* New device, add to fcports list. */
1951                         new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
1952                         list_add_tail(&new_fcport->list, &ha->fcports);
1953
1954                         /* Allocate a new replacement fcport. */
1955                         fcport = new_fcport;
1956                         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1957                         if (new_fcport == NULL) {
1958                                 rval = QLA_MEMORY_ALLOC_FAILED;
1959                                 goto cleanup_allocation;
1960                         }
1961                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1962                 }
1963
1964                 /* Base iIDMA settings on HBA port speed. */
1965                 switch (ha->link_data_rate) {
1966                 case PORT_SPEED_1GB:
1967                         fcport->fp_speed = cpu_to_be16(BIT_15);
1968                         break;
1969                 case PORT_SPEED_2GB:
1970                         fcport->fp_speed = cpu_to_be16(BIT_14);
1971                         break;
1972                 case PORT_SPEED_4GB:
1973                         fcport->fp_speed = cpu_to_be16(BIT_13);
1974                         break;
1975                 }
1976
1977                 qla2x00_update_fcport(ha, fcport);
1978
1979                 found_devs++;
1980         }
1981
1982 cleanup_allocation:
1983         kfree(new_fcport);
1984
1985         if (rval != QLA_SUCCESS) {
1986                 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
1987                     "rval=%x\n", ha->host_no, rval));
1988         }
1989
1990         if (found_devs) {
1991                 ha->device_flags |= DFLG_LOCAL_DEVICES;
1992                 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
1993         }
1994
1995         return (rval);
1996 }
1997
1998 static void
1999 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2000 {
2001         fc_port_t       *fcport;
2002
2003         qla2x00_mark_all_devices_lost(ha, 0);
2004         list_for_each_entry(fcport, &ha->fcports, list) {
2005                 if (fcport->port_type != FCT_TARGET)
2006                         continue;
2007
2008                 qla2x00_update_fcport(ha, fcport);
2009         }
2010 }
2011
2012 static void
2013 qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2014 {
2015 #define LS_UNKNOWN      2
2016         static char *link_speeds[5] = { "1", "2", "?", "4" };
2017         int rval;
2018         uint16_t port_speed, mb[6];
2019
2020         if (!IS_QLA24XX(ha))
2021                 return;
2022
2023         switch (be16_to_cpu(fcport->fp_speed)) {
2024         case BIT_15:
2025                 port_speed = PORT_SPEED_1GB;
2026                 break;
2027         case BIT_14:
2028                 port_speed = PORT_SPEED_2GB;
2029                 break;
2030         case BIT_13:
2031                 port_speed = PORT_SPEED_4GB;
2032                 break;
2033         default:
2034                 DEBUG2(printk("scsi(%ld): %02x%02x%02x%02x%02x%02x%02x%02x -- "
2035                     "unsupported FM port operating speed (%04x).\n",
2036                     ha->host_no, fcport->port_name[0], fcport->port_name[1],
2037                     fcport->port_name[2], fcport->port_name[3],
2038                     fcport->port_name[4], fcport->port_name[5],
2039                     fcport->port_name[6], fcport->port_name[7],
2040                     be16_to_cpu(fcport->fp_speed)));
2041                 port_speed = PORT_SPEED_UNKNOWN;
2042                 break;
2043         }
2044         if (port_speed == PORT_SPEED_UNKNOWN)
2045                 return;
2046
2047         rval = qla2x00_set_idma_speed(ha, fcport->loop_id, port_speed, mb);
2048         if (rval != QLA_SUCCESS) {
2049                 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2050                     "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2051                     ha->host_no, fcport->port_name[0], fcport->port_name[1],
2052                     fcport->port_name[2], fcport->port_name[3],
2053                     fcport->port_name[4], fcport->port_name[5],
2054                     fcport->port_name[6], fcport->port_name[7], rval,
2055                     port_speed, mb[0], mb[1]));
2056         } else {
2057                 DEBUG2(qla_printk(KERN_INFO, ha,
2058                     "iIDMA adjusted to %s GB/s on "
2059                     "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2060                     link_speeds[port_speed], fcport->port_name[0],
2061                     fcport->port_name[1], fcport->port_name[2],
2062                     fcport->port_name[3], fcport->port_name[4],
2063                     fcport->port_name[5], fcport->port_name[6],
2064                     fcport->port_name[7]));
2065         }
2066 }
2067
2068 static void
2069 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2070 {
2071         struct fc_rport_identifiers rport_ids;
2072         struct fc_rport *rport;
2073         unsigned long flags;
2074
2075         if (fcport->drport)
2076                 qla2x00_rport_del(fcport);
2077         if (fcport->rport)
2078                 return;
2079
2080         rport_ids.node_name = wwn_to_u64(fcport->node_name);
2081         rport_ids.port_name = wwn_to_u64(fcport->port_name);
2082         rport_ids.port_id = fcport->d_id.b.domain << 16 |
2083             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2084         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2085         rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2086         if (!rport) {
2087                 qla_printk(KERN_WARNING, ha,
2088                     "Unable to allocate fc remote port!\n");
2089                 return;
2090         }
2091         spin_lock_irqsave(&fcport->rport_lock, flags);
2092         fcport->rport = rport;
2093         *((fc_port_t **)rport->dd_data) = fcport;
2094         spin_unlock_irqrestore(&fcport->rport_lock, flags);
2095
2096         rport->supported_classes = fcport->supported_classes;
2097
2098         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2099         if (fcport->port_type == FCT_INITIATOR)
2100                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2101         if (fcport->port_type == FCT_TARGET)
2102                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2103         fc_remote_port_rolechg(rport, rport_ids.roles);
2104
2105         if (rport->scsi_target_id != -1 &&
2106             rport->scsi_target_id < ha->host->max_id)
2107                 fcport->os_target_id = rport->scsi_target_id;
2108 }
2109
2110 /*
2111  * qla2x00_update_fcport
2112  *      Updates device on list.
2113  *
2114  * Input:
2115  *      ha = adapter block pointer.
2116  *      fcport = port structure pointer.
2117  *
2118  * Return:
2119  *      0  - Success
2120  *  BIT_0 - error
2121  *
2122  * Context:
2123  *      Kernel context.
2124  */
2125 void
2126 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2127 {
2128         fcport->ha = ha;
2129         fcport->login_retry = 0;
2130         fcport->port_login_retry_count = ha->port_down_retry_count *
2131             PORT_RETRY_TIME;
2132         atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2133             PORT_RETRY_TIME);
2134         fcport->flags &= ~FCF_LOGIN_NEEDED;
2135
2136         qla2x00_iidma_fcport(ha, fcport);
2137
2138         atomic_set(&fcport->state, FCS_ONLINE);
2139
2140         qla2x00_reg_remote_port(ha, fcport);
2141 }
2142
2143 /*
2144  * qla2x00_configure_fabric
2145  *      Setup SNS devices with loop ID's.
2146  *
2147  * Input:
2148  *      ha = adapter block pointer.
2149  *
2150  * Returns:
2151  *      0 = success.
2152  *      BIT_0 = error
2153  */
2154 static int
2155 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2156 {
2157         int     rval, rval2;
2158         fc_port_t       *fcport, *fcptemp;
2159         uint16_t        next_loopid;
2160         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2161         uint16_t        loop_id;
2162         LIST_HEAD(new_fcports);
2163
2164         /* If FL port exists, then SNS is present */
2165         if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2166                 loop_id = NPH_F_PORT;
2167         else
2168                 loop_id = SNS_FL_PORT;
2169         rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
2170         if (rval != QLA_SUCCESS) {
2171                 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2172                     "Port\n", ha->host_no));
2173
2174                 ha->device_flags &= ~SWITCH_FOUND;
2175                 return (QLA_SUCCESS);
2176         }
2177         ha->device_flags |= SWITCH_FOUND;
2178
2179         /* Mark devices that need re-synchronization. */
2180         rval2 = qla2x00_device_resync(ha);
2181         if (rval2 == QLA_RSCNS_HANDLED) {
2182                 /* No point doing the scan, just continue. */
2183                 return (QLA_SUCCESS);
2184         }
2185         do {
2186                 /* FDMI support. */
2187                 if (ql2xfdmienable &&
2188                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2189                         qla2x00_fdmi_register(ha);
2190
2191                 /* Ensure we are logged into the SNS. */
2192                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2193                         loop_id = NPH_SNS;
2194                 else
2195                         loop_id = SIMPLE_NAME_SERVER;
2196                 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
2197                     0xfc, mb, BIT_1 | BIT_0);
2198                 if (mb[0] != MBS_COMMAND_COMPLETE) {
2199                         DEBUG2(qla_printk(KERN_INFO, ha,
2200                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2201                             "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2202                             mb[0], mb[1], mb[2], mb[6], mb[7]));
2203                         return (QLA_SUCCESS);
2204                 }
2205
2206                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2207                         if (qla2x00_rft_id(ha)) {
2208                                 /* EMPTY */
2209                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2210                                     "TYPE failed.\n", ha->host_no));
2211                         }
2212                         if (qla2x00_rff_id(ha)) {
2213                                 /* EMPTY */
2214                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2215                                     "Features failed.\n", ha->host_no));
2216                         }
2217                         if (qla2x00_rnn_id(ha)) {
2218                                 /* EMPTY */
2219                                 DEBUG2(printk("scsi(%ld): Register Node Name "
2220                                     "failed.\n", ha->host_no));
2221                         } else if (qla2x00_rsnn_nn(ha)) {
2222                                 /* EMPTY */
2223                                 DEBUG2(printk("scsi(%ld): Register Symbolic "
2224                                     "Node Name failed.\n", ha->host_no));
2225                         }
2226                 }
2227
2228                 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2229                 if (rval != QLA_SUCCESS)
2230                         break;
2231
2232                 /*
2233                  * Logout all previous fabric devices marked lost, except
2234                  * tape devices.
2235                  */
2236                 list_for_each_entry(fcport, &ha->fcports, list) {
2237                         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2238                                 break;
2239
2240                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2241                                 continue;
2242
2243                         if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2244                                 qla2x00_mark_device_lost(ha, fcport,
2245                                     ql2xplogiabsentdevice, 0);
2246                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
2247                                     (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2248                                     fcport->port_type != FCT_INITIATOR &&
2249                                     fcport->port_type != FCT_BROADCAST) {
2250                                         ha->isp_ops.fabric_logout(ha,
2251                                             fcport->loop_id,
2252                                             fcport->d_id.b.domain,
2253                                             fcport->d_id.b.area,
2254                                             fcport->d_id.b.al_pa);
2255                                         fcport->loop_id = FC_NO_LOOP_ID;
2256                                 }
2257                         }
2258                 }
2259
2260                 /* Starting free loop ID. */
2261                 next_loopid = ha->min_external_loopid;
2262
2263                 /*
2264                  * Scan through our port list and login entries that need to be
2265                  * logged in.
2266                  */
2267                 list_for_each_entry(fcport, &ha->fcports, list) {
2268                         if (atomic_read(&ha->loop_down_timer) ||
2269                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2270                                 break;
2271
2272                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2273                             (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2274                                 continue;
2275
2276                         if (fcport->loop_id == FC_NO_LOOP_ID) {
2277                                 fcport->loop_id = next_loopid;
2278                                 rval = qla2x00_find_new_loop_id(ha, fcport);
2279                                 if (rval != QLA_SUCCESS) {
2280                                         /* Ran out of IDs to use */
2281                                         break;
2282                                 }
2283                         }
2284                         /* Login and update database */
2285                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2286                 }
2287
2288                 /* Exit if out of loop IDs. */
2289                 if (rval != QLA_SUCCESS) {
2290                         break;
2291                 }
2292
2293                 /*
2294                  * Login and add the new devices to our port list.
2295                  */
2296                 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2297                         if (atomic_read(&ha->loop_down_timer) ||
2298                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2299                                 break;
2300
2301                         /* Find a new loop ID to use. */
2302                         fcport->loop_id = next_loopid;
2303                         rval = qla2x00_find_new_loop_id(ha, fcport);
2304                         if (rval != QLA_SUCCESS) {
2305                                 /* Ran out of IDs to use */
2306                                 break;
2307                         }
2308
2309                         /* Remove device from the new list and add it to DB */
2310                         list_move_tail(&fcport->list, &ha->fcports);
2311
2312                         /* Login and update database */
2313                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2314                 }
2315         } while (0);
2316
2317         /* Free all new device structures not processed. */
2318         list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2319                 list_del(&fcport->list);
2320                 kfree(fcport);
2321         }
2322
2323         if (rval) {
2324                 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2325                     "rval=%d\n", ha->host_no, rval));
2326         }
2327
2328         return (rval);
2329 }
2330
2331
2332 /*
2333  * qla2x00_find_all_fabric_devs
2334  *
2335  * Input:
2336  *      ha = adapter block pointer.
2337  *      dev = database device entry pointer.
2338  *
2339  * Returns:
2340  *      0 = success.
2341  *
2342  * Context:
2343  *      Kernel context.
2344  */
2345 static int
2346 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2347 {
2348         int             rval;
2349         uint16_t        loop_id;
2350         fc_port_t       *fcport, *new_fcport, *fcptemp;
2351         int             found;
2352
2353         sw_info_t       *swl;
2354         int             swl_idx;
2355         int             first_dev, last_dev;
2356         port_id_t       wrap, nxt_d_id;
2357
2358         rval = QLA_SUCCESS;
2359
2360         /* Try GID_PT to get device list, else GAN. */
2361         swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2362         if (swl == NULL) {
2363                 /*EMPTY*/
2364                 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2365                     "on GA_NXT\n", ha->host_no));
2366         } else {
2367                 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2368                 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2369                         kfree(swl);
2370                         swl = NULL;
2371                 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2372                         kfree(swl);
2373                         swl = NULL;
2374                 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2375                         kfree(swl);
2376                         swl = NULL;
2377                 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2378                         qla2x00_gpsc(ha, swl);
2379                 }
2380         }
2381         swl_idx = 0;
2382
2383         /* Allocate temporary fcport for any new fcports discovered. */
2384         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2385         if (new_fcport == NULL) {
2386                 kfree(swl);
2387                 return (QLA_MEMORY_ALLOC_FAILED);
2388         }
2389         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2390
2391         /* Set start port ID scan at adapter ID. */
2392         first_dev = 1;
2393         last_dev = 0;
2394
2395         /* Starting free loop ID. */
2396         loop_id = ha->min_external_loopid;
2397         for (; loop_id <= ha->last_loop_id; loop_id++) {
2398                 if (qla2x00_is_reserved_id(ha, loop_id))
2399                         continue;
2400
2401                 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2402                         break;
2403
2404                 if (swl != NULL) {
2405                         if (last_dev) {
2406                                 wrap.b24 = new_fcport->d_id.b24;
2407                         } else {
2408                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2409                                 memcpy(new_fcport->node_name,
2410                                     swl[swl_idx].node_name, WWN_SIZE);
2411                                 memcpy(new_fcport->port_name,
2412                                     swl[swl_idx].port_name, WWN_SIZE);
2413                                 memcpy(new_fcport->fabric_port_name,
2414                                     swl[swl_idx].fabric_port_name, WWN_SIZE);
2415                                 new_fcport->fp_speed = swl[swl_idx].fp_speed;
2416
2417                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2418                                         last_dev = 1;
2419                                 }
2420                                 swl_idx++;
2421                         }
2422                 } else {
2423                         /* Send GA_NXT to the switch */
2424                         rval = qla2x00_ga_nxt(ha, new_fcport);
2425                         if (rval != QLA_SUCCESS) {
2426                                 qla_printk(KERN_WARNING, ha,
2427                                     "SNS scan failed -- assuming zero-entry "
2428                                     "result...\n");
2429                                 list_for_each_entry_safe(fcport, fcptemp,
2430                                     new_fcports, list) {
2431                                         list_del(&fcport->list);
2432                                         kfree(fcport);
2433                                 }
2434                                 rval = QLA_SUCCESS;
2435                                 break;
2436                         }
2437                 }
2438
2439                 /* If wrap on switch device list, exit. */
2440                 if (first_dev) {
2441                         wrap.b24 = new_fcport->d_id.b24;
2442                         first_dev = 0;
2443                 } else if (new_fcport->d_id.b24 == wrap.b24) {
2444                         DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2445                             ha->host_no, new_fcport->d_id.b.domain,
2446                             new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2447                         break;
2448                 }
2449
2450                 /* Bypass if host adapter. */
2451                 if (new_fcport->d_id.b24 == ha->d_id.b24)
2452                         continue;
2453
2454                 /* Bypass if same domain and area of adapter. */
2455                 if (((new_fcport->d_id.b24 & 0xffff00) ==
2456                     (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2457                         ISP_CFG_FL)
2458                             continue;
2459
2460                 /* Bypass reserved domain fields. */
2461                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2462                         continue;
2463
2464                 /* Locate matching device in database. */
2465                 found = 0;
2466                 list_for_each_entry(fcport, &ha->fcports, list) {
2467                         if (memcmp(new_fcport->port_name, fcport->port_name,
2468                             WWN_SIZE))
2469                                 continue;
2470
2471                         found++;
2472
2473                         /* Update port state. */
2474                         memcpy(fcport->fabric_port_name,
2475                             new_fcport->fabric_port_name, WWN_SIZE);
2476                         fcport->fp_speed = new_fcport->fp_speed;
2477
2478                         /*
2479                          * If address the same and state FCS_ONLINE, nothing
2480                          * changed.
2481                          */
2482                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2483                             atomic_read(&fcport->state) == FCS_ONLINE) {
2484                                 break;
2485                         }
2486
2487                         /*
2488                          * If device was not a fabric device before.
2489                          */
2490                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2491                                 fcport->d_id.b24 = new_fcport->d_id.b24;
2492                                 fcport->loop_id = FC_NO_LOOP_ID;
2493                                 fcport->flags |= (FCF_FABRIC_DEVICE |
2494                                     FCF_LOGIN_NEEDED);
2495                                 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2496                                 break;
2497                         }
2498
2499                         /*
2500                          * Port ID changed or device was marked to be updated;
2501                          * Log it out if still logged in and mark it for
2502                          * relogin later.
2503                          */
2504                         fcport->d_id.b24 = new_fcport->d_id.b24;
2505                         fcport->flags |= FCF_LOGIN_NEEDED;
2506                         if (fcport->loop_id != FC_NO_LOOP_ID &&
2507                             (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2508                             fcport->port_type != FCT_INITIATOR &&
2509                             fcport->port_type != FCT_BROADCAST) {
2510                                 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2511                                     fcport->d_id.b.domain, fcport->d_id.b.area,
2512                                     fcport->d_id.b.al_pa);
2513                                 fcport->loop_id = FC_NO_LOOP_ID;
2514                         }
2515
2516                         break;
2517                 }
2518
2519                 if (found)
2520                         continue;
2521
2522                 /* If device was not in our fcports list, then add it. */
2523                 list_add_tail(&new_fcport->list, new_fcports);
2524
2525                 /* Allocate a new replacement fcport. */
2526                 nxt_d_id.b24 = new_fcport->d_id.b24;
2527                 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2528                 if (new_fcport == NULL) {
2529                         kfree(swl);
2530                         return (QLA_MEMORY_ALLOC_FAILED);
2531                 }
2532                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2533                 new_fcport->d_id.b24 = nxt_d_id.b24;
2534         }
2535
2536         kfree(swl);
2537         kfree(new_fcport);
2538
2539         if (!list_empty(new_fcports))
2540                 ha->device_flags |= DFLG_FABRIC_DEVICES;
2541
2542         return (rval);
2543 }
2544
2545 /*
2546  * qla2x00_find_new_loop_id
2547  *      Scan through our port list and find a new usable loop ID.
2548  *
2549  * Input:
2550  *      ha:     adapter state pointer.
2551  *      dev:    port structure pointer.
2552  *
2553  * Returns:
2554  *      qla2x00 local function return status code.
2555  *
2556  * Context:
2557  *      Kernel context.
2558  */
2559 static int
2560 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2561 {
2562         int     rval;
2563         int     found;
2564         fc_port_t *fcport;
2565         uint16_t first_loop_id;
2566
2567         rval = QLA_SUCCESS;
2568
2569         /* Save starting loop ID. */
2570         first_loop_id = dev->loop_id;
2571
2572         for (;;) {
2573                 /* Skip loop ID if already used by adapter. */
2574                 if (dev->loop_id == ha->loop_id) {
2575                         dev->loop_id++;
2576                 }
2577
2578                 /* Skip reserved loop IDs. */
2579                 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2580                         dev->loop_id++;
2581                 }
2582
2583                 /* Reset loop ID if passed the end. */
2584                 if (dev->loop_id > ha->last_loop_id) {
2585                         /* first loop ID. */
2586                         dev->loop_id = ha->min_external_loopid;
2587                 }
2588
2589                 /* Check for loop ID being already in use. */
2590                 found = 0;
2591                 fcport = NULL;
2592                 list_for_each_entry(fcport, &ha->fcports, list) {
2593                         if (fcport->loop_id == dev->loop_id && fcport != dev) {
2594                                 /* ID possibly in use */
2595                                 found++;
2596                                 break;
2597                         }
2598                 }
2599
2600                 /* If not in use then it is free to use. */
2601                 if (!found) {
2602                         break;
2603                 }
2604
2605                 /* ID in use. Try next value. */
2606                 dev->loop_id++;
2607
2608                 /* If wrap around. No free ID to use. */
2609                 if (dev->loop_id == first_loop_id) {
2610                         dev->loop_id = FC_NO_LOOP_ID;
2611                         rval = QLA_FUNCTION_FAILED;
2612                         break;
2613                 }
2614         }
2615
2616         return (rval);
2617 }
2618
2619 /*
2620  * qla2x00_device_resync
2621  *      Marks devices in the database that needs resynchronization.
2622  *
2623  * Input:
2624  *      ha = adapter block pointer.
2625  *
2626  * Context:
2627  *      Kernel context.
2628  */
2629 static int
2630 qla2x00_device_resync(scsi_qla_host_t *ha)
2631 {
2632         int     rval;
2633         uint32_t mask;
2634         fc_port_t *fcport;
2635         uint32_t rscn_entry;
2636         uint8_t rscn_out_iter;
2637         uint8_t format;
2638         port_id_t d_id;
2639
2640         rval = QLA_RSCNS_HANDLED;
2641
2642         while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2643             ha->flags.rscn_queue_overflow) {
2644
2645                 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2646                 format = MSB(MSW(rscn_entry));
2647                 d_id.b.domain = LSB(MSW(rscn_entry));
2648                 d_id.b.area = MSB(LSW(rscn_entry));
2649                 d_id.b.al_pa = LSB(LSW(rscn_entry));
2650
2651                 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2652                     "[%02x/%02x%02x%02x].\n",
2653                     ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2654                     d_id.b.area, d_id.b.al_pa));
2655
2656                 ha->rscn_out_ptr++;
2657                 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2658                         ha->rscn_out_ptr = 0;
2659
2660                 /* Skip duplicate entries. */
2661                 for (rscn_out_iter = ha->rscn_out_ptr;
2662                     !ha->flags.rscn_queue_overflow &&
2663                     rscn_out_iter != ha->rscn_in_ptr;
2664                     rscn_out_iter = (rscn_out_iter ==
2665                         (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2666
2667                         if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2668                                 break;
2669
2670                         DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2671                             "entry found at [%d].\n", ha->host_no,
2672                             rscn_out_iter));
2673
2674                         ha->rscn_out_ptr = rscn_out_iter;
2675                 }
2676
2677                 /* Queue overflow, set switch default case. */
2678                 if (ha->flags.rscn_queue_overflow) {
2679                         DEBUG(printk("scsi(%ld): device_resync: rscn "
2680                             "overflow.\n", ha->host_no));
2681
2682                         format = 3;
2683                         ha->flags.rscn_queue_overflow = 0;
2684                 }
2685
2686                 switch (format) {
2687                 case 0:
2688                         mask = 0xffffff;
2689                         break;
2690                 case 1:
2691                         mask = 0xffff00;
2692                         break;
2693                 case 2:
2694                         mask = 0xff0000;
2695                         break;
2696                 default:
2697                         mask = 0x0;
2698                         d_id.b24 = 0;
2699                         ha->rscn_out_ptr = ha->rscn_in_ptr;
2700                         break;
2701                 }
2702
2703                 rval = QLA_SUCCESS;
2704
2705                 list_for_each_entry(fcport, &ha->fcports, list) {
2706                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2707                             (fcport->d_id.b24 & mask) != d_id.b24 ||
2708                             fcport->port_type == FCT_BROADCAST)
2709                                 continue;
2710
2711                         if (atomic_read(&fcport->state) == FCS_ONLINE) {
2712                                 if (format != 3 ||
2713                                     fcport->port_type != FCT_INITIATOR) {
2714                                         qla2x00_mark_device_lost(ha, fcport,
2715                                             0, 0);
2716                                 }
2717                         }
2718                         fcport->flags &= ~FCF_FARP_DONE;
2719                 }
2720         }
2721         return (rval);
2722 }
2723
2724 /*
2725  * qla2x00_fabric_dev_login
2726  *      Login fabric target device and update FC port database.
2727  *
2728  * Input:
2729  *      ha:             adapter state pointer.
2730  *      fcport:         port structure list pointer.
2731  *      next_loopid:    contains value of a new loop ID that can be used
2732  *                      by the next login attempt.
2733  *
2734  * Returns:
2735  *      qla2x00 local function return status code.
2736  *
2737  * Context:
2738  *      Kernel context.
2739  */
2740 static int
2741 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2742     uint16_t *next_loopid)
2743 {
2744         int     rval;
2745         int     retry;
2746         uint8_t opts;
2747
2748         rval = QLA_SUCCESS;
2749         retry = 0;
2750
2751         rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2752         if (rval == QLA_SUCCESS) {
2753                 /* Send an ADISC to tape devices.*/
2754                 opts = 0;
2755                 if (fcport->flags & FCF_TAPE_PRESENT)
2756                         opts |= BIT_1;
2757                 rval = qla2x00_get_port_database(ha, fcport, opts);
2758                 if (rval != QLA_SUCCESS) {
2759                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2760                             fcport->d_id.b.domain, fcport->d_id.b.area,
2761                             fcport->d_id.b.al_pa);
2762                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2763                 } else {
2764                         qla2x00_update_fcport(ha, fcport);
2765                 }
2766         }
2767
2768         return (rval);
2769 }
2770
2771 /*
2772  * qla2x00_fabric_login
2773  *      Issue fabric login command.
2774  *
2775  * Input:
2776  *      ha = adapter block pointer.
2777  *      device = pointer to FC device type structure.
2778  *
2779  * Returns:
2780  *      0 - Login successfully
2781  *      1 - Login failed
2782  *      2 - Initiator device
2783  *      3 - Fatal error
2784  */
2785 int
2786 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2787     uint16_t *next_loopid)
2788 {
2789         int     rval;
2790         int     retry;
2791         uint16_t tmp_loopid;
2792         uint16_t mb[MAILBOX_REGISTER_COUNT];
2793
2794         retry = 0;
2795         tmp_loopid = 0;
2796
2797         for (;;) {
2798                 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2799                     "for port %02x%02x%02x.\n",
2800                     ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2801                     fcport->d_id.b.area, fcport->d_id.b.al_pa));
2802
2803                 /* Login fcport on switch. */
2804                 ha->isp_ops.fabric_login(ha, fcport->loop_id,
2805                     fcport->d_id.b.domain, fcport->d_id.b.area,
2806                     fcport->d_id.b.al_pa, mb, BIT_0);
2807                 if (mb[0] == MBS_PORT_ID_USED) {
2808                         /*
2809                          * Device has another loop ID.  The firmware team
2810                          * recommends the driver perform an implicit login with
2811                          * the specified ID again. The ID we just used is save
2812                          * here so we return with an ID that can be tried by
2813                          * the next login.
2814                          */
2815                         retry++;
2816                         tmp_loopid = fcport->loop_id;
2817                         fcport->loop_id = mb[1];
2818
2819                         DEBUG(printk("Fabric Login: port in use - next "
2820                             "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2821                             fcport->loop_id, fcport->d_id.b.domain,
2822                             fcport->d_id.b.area, fcport->d_id.b.al_pa));
2823
2824                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2825                         /*
2826                          * Login succeeded.
2827                          */
2828                         if (retry) {
2829                                 /* A retry occurred before. */
2830                                 *next_loopid = tmp_loopid;
2831                         } else {
2832                                 /*
2833                                  * No retry occurred before. Just increment the
2834                                  * ID value for next login.
2835                                  */
2836                                 *next_loopid = (fcport->loop_id + 1);
2837                         }
2838
2839                         if (mb[1] & BIT_0) {
2840                                 fcport->port_type = FCT_INITIATOR;
2841                         } else {
2842                                 fcport->port_type = FCT_TARGET;
2843                                 if (mb[1] & BIT_1) {
2844                                         fcport->flags |= FCF_TAPE_PRESENT;
2845                                 }
2846                         }
2847
2848                         if (mb[10] & BIT_0)
2849                                 fcport->supported_classes |= FC_COS_CLASS2;
2850                         if (mb[10] & BIT_1)
2851                                 fcport->supported_classes |= FC_COS_CLASS3;
2852
2853                         rval = QLA_SUCCESS;
2854                         break;
2855                 } else if (mb[0] == MBS_LOOP_ID_USED) {
2856                         /*
2857                          * Loop ID already used, try next loop ID.
2858                          */
2859                         fcport->loop_id++;
2860                         rval = qla2x00_find_new_loop_id(ha, fcport);
2861                         if (rval != QLA_SUCCESS) {
2862                                 /* Ran out of loop IDs to use */
2863                                 break;
2864                         }
2865                 } else if (mb[0] == MBS_COMMAND_ERROR) {
2866                         /*
2867                          * Firmware possibly timed out during login. If NO
2868                          * retries are left to do then the device is declared
2869                          * dead.
2870                          */
2871                         *next_loopid = fcport->loop_id;
2872                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2873                             fcport->d_id.b.domain, fcport->d_id.b.area,
2874                             fcport->d_id.b.al_pa);
2875                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2876
2877                         rval = 1;
2878                         break;
2879                 } else {
2880                         /*
2881                          * unrecoverable / not handled error
2882                          */
2883                         DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
2884                             "loop_id=%x jiffies=%lx.\n",
2885                             __func__, ha->host_no, mb[0],
2886                             fcport->d_id.b.domain, fcport->d_id.b.area,
2887                             fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2888
2889                         *next_loopid = fcport->loop_id;
2890                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2891                             fcport->d_id.b.domain, fcport->d_id.b.area,
2892                             fcport->d_id.b.al_pa);
2893                         fcport->loop_id = FC_NO_LOOP_ID;
2894                         fcport->login_retry = 0;
2895
2896                         rval = 3;
2897                         break;
2898                 }
2899         }
2900
2901         return (rval);
2902 }
2903
2904 /*
2905  * qla2x00_local_device_login
2906  *      Issue local device login command.
2907  *
2908  * Input:
2909  *      ha = adapter block pointer.
2910  *      loop_id = loop id of device to login to.
2911  *
2912  * Returns (Where's the #define!!!!):
2913  *      0 - Login successfully
2914  *      1 - Login failed
2915  *      3 - Fatal error
2916  */
2917 int
2918 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
2919 {
2920         int             rval;
2921         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2922
2923         memset(mb, 0, sizeof(mb));
2924         rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
2925         if (rval == QLA_SUCCESS) {
2926                 /* Interrogate mailbox registers for any errors */
2927                 if (mb[0] == MBS_COMMAND_ERROR)
2928                         rval = 1;
2929                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2930                         /* device not in PCB table */
2931                         rval = 3;
2932         }
2933
2934         return (rval);
2935 }
2936
2937 /*
2938  *  qla2x00_loop_resync
2939  *      Resync with fibre channel devices.
2940  *
2941  * Input:
2942  *      ha = adapter block pointer.
2943  *
2944  * Returns:
2945  *      0 = success
2946  */
2947 int
2948 qla2x00_loop_resync(scsi_qla_host_t *ha)
2949 {
2950         int   rval;
2951         uint32_t wait_time;
2952
2953         rval = QLA_SUCCESS;
2954
2955         atomic_set(&ha->loop_state, LOOP_UPDATE);
2956         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2957         if (ha->flags.online) {
2958                 if (!(rval = qla2x00_fw_ready(ha))) {
2959                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
2960                         wait_time = 256;
2961                         do {
2962                                 atomic_set(&ha->loop_state, LOOP_UPDATE);
2963
2964                                 /* Issue a marker after FW becomes ready. */
2965                                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2966                                 ha->marker_needed = 0;
2967
2968                                 /* Remap devices on Loop. */
2969                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2970
2971                                 qla2x00_configure_loop(ha);
2972                                 wait_time--;
2973                         } while (!atomic_read(&ha->loop_down_timer) &&
2974                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2975                                 wait_time &&
2976                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2977                 }
2978         }
2979
2980         if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2981                 return (QLA_FUNCTION_FAILED);
2982         }
2983
2984         if (rval) {
2985                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
2986         }
2987
2988         return (rval);
2989 }
2990
2991 void
2992 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
2993 {
2994         int rescan_done;
2995         fc_port_t *fcport;
2996
2997         rescan_done = 0;
2998         list_for_each_entry(fcport, &ha->fcports, list) {
2999                 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3000                         continue;
3001
3002                 qla2x00_update_fcport(ha, fcport);
3003                 fcport->flags &= ~FCF_RESCAN_NEEDED;
3004
3005                 rescan_done = 1;
3006         }
3007         qla2x00_probe_for_all_luns(ha);
3008 }
3009
3010 void
3011 qla2x00_update_fcports(scsi_qla_host_t *ha)
3012 {
3013         fc_port_t *fcport;
3014
3015         /* Go with deferred removal of rport references. */
3016         list_for_each_entry(fcport, &ha->fcports, list)
3017                 if (fcport->drport)
3018                         qla2x00_rport_del(fcport);
3019 }
3020
3021 /*
3022 *  qla2x00_abort_isp
3023 *      Resets ISP and aborts all outstanding commands.
3024 *
3025 * Input:
3026 *      ha           = adapter block pointer.
3027 *
3028 * Returns:
3029 *      0 = success
3030 */
3031 int
3032 qla2x00_abort_isp(scsi_qla_host_t *ha)
3033 {
3034         int rval;
3035         unsigned long flags = 0;
3036         uint16_t       cnt;
3037         srb_t          *sp;
3038         uint8_t        status = 0;
3039
3040         if (ha->flags.online) {
3041                 ha->flags.online = 0;
3042                 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3043
3044                 qla_printk(KERN_INFO, ha,
3045                     "Performing ISP error recovery - ha= %p.\n", ha);
3046                 ha->isp_ops.reset_chip(ha);
3047
3048                 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3049                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3050                         atomic_set(&ha->loop_state, LOOP_DOWN);
3051                         qla2x00_mark_all_devices_lost(ha, 0);
3052                 } else {
3053                         if (!atomic_read(&ha->loop_down_timer))
3054                                 atomic_set(&ha->loop_down_timer,
3055                                     LOOP_DOWN_TIME);
3056                 }
3057
3058                 spin_lock_irqsave(&ha->hardware_lock, flags);
3059                 /* Requeue all commands in outstanding command list. */
3060                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3061                         sp = ha->outstanding_cmds[cnt];
3062                         if (sp) {
3063                                 ha->outstanding_cmds[cnt] = NULL;
3064                                 sp->flags = 0;
3065                                 sp->cmd->result = DID_RESET << 16;
3066                                 sp->cmd->host_scribble = (unsigned char *)NULL;
3067                                 qla2x00_sp_compl(ha, sp);
3068                         }
3069                 }
3070                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3071
3072                 ha->isp_ops.get_flash_version(ha, ha->request_ring);
3073
3074                 rval = ha->isp_ops.nvram_config(ha);
3075                 if (rval)
3076                         goto isp_abort_retry;
3077
3078                 if (!qla2x00_restart_isp(ha)) {
3079                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3080
3081                         if (!atomic_read(&ha->loop_down_timer)) {
3082                                 /*
3083                                  * Issue marker command only when we are going
3084                                  * to start the I/O .
3085                                  */
3086                                 ha->marker_needed = 1;
3087                         }
3088
3089                         ha->flags.online = 1;
3090
3091                         ha->isp_ops.enable_intrs(ha);
3092
3093                         ha->isp_abort_cnt = 0;
3094                         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3095
3096                         if (ha->eft) {
3097                                 rval = qla2x00_trace_control(ha, TC_ENABLE,
3098                                     ha->eft_dma, EFT_NUM_BUFFERS);
3099                                 if (rval) {
3100                                         qla_printk(KERN_WARNING, ha,
3101                                             "Unable to reinitialize EFT "
3102                                             "(%d).\n", rval);
3103                                 }
3104                         }
3105                 } else {        /* failed the ISP abort */
3106 isp_abort_retry:
3107                         ha->flags.online = 1;
3108                         if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3109                                 if (ha->isp_abort_cnt == 0) {
3110                                         qla_printk(KERN_WARNING, ha,
3111                                             "ISP error recovery failed - "
3112                                             "board disabled\n");
3113                                         /*
3114                                          * The next call disables the board
3115                                          * completely.
3116                                          */
3117                                         ha->isp_ops.reset_adapter(ha);
3118                                         ha->flags.online = 0;
3119                                         clear_bit(ISP_ABORT_RETRY,
3120                                             &ha->dpc_flags);
3121                                         status = 0;
3122                                 } else { /* schedule another ISP abort */
3123                                         ha->isp_abort_cnt--;
3124                                         DEBUG(printk("qla%ld: ISP abort - "
3125                                             "retry remaining %d\n",
3126                                             ha->host_no, ha->isp_abort_cnt));
3127                                         status = 1;
3128                                 }
3129                         } else {
3130                                 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3131                                 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3132                                     "- retrying (%d) more times\n",
3133                                     ha->host_no, ha->isp_abort_cnt));
3134                                 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3135                                 status = 1;
3136                         }
3137                 }
3138
3139         }
3140
3141         if (status) {
3142                 qla_printk(KERN_INFO, ha,
3143                         "qla2x00_abort_isp: **** FAILED ****\n");
3144         } else {
3145                 DEBUG(printk(KERN_INFO
3146                                 "qla2x00_abort_isp(%ld): exiting.\n",
3147                                 ha->host_no));
3148         }
3149
3150         return(status);
3151 }
3152
3153 /*
3154 *  qla2x00_restart_isp
3155 *      restarts the ISP after a reset
3156 *
3157 * Input:
3158 *      ha = adapter block pointer.
3159 *
3160 * Returns:
3161 *      0 = success
3162 */
3163 static int
3164 qla2x00_restart_isp(scsi_qla_host_t *ha)
3165 {
3166         uint8_t         status = 0;
3167         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3168         unsigned long   flags = 0;
3169         uint32_t wait_time;
3170
3171         /* If firmware needs to be loaded */
3172         if (qla2x00_isp_firmware(ha)) {
3173                 ha->flags.online = 0;
3174                 if (!(status = ha->isp_ops.chip_diag(ha))) {
3175                         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3176                                 status = qla2x00_setup_chip(ha);
3177                                 goto done;
3178                         }
3179
3180                         spin_lock_irqsave(&ha->hardware_lock, flags);
3181
3182                         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3183                                 /*
3184                                  * Disable SRAM, Instruction RAM and GP RAM
3185                                  * parity.
3186                                  */
3187                                 WRT_REG_WORD(&reg->hccr,
3188                                     (HCCR_ENABLE_PARITY + 0x0));
3189                                 RD_REG_WORD(&reg->hccr);
3190                         }
3191
3192                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3193
3194                         status = qla2x00_setup_chip(ha);
3195
3196                         spin_lock_irqsave(&ha->hardware_lock, flags);
3197
3198                         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3199                                 /* Enable proper parity */
3200                                 if (IS_QLA2300(ha))
3201                                         /* SRAM parity */
3202                                         WRT_REG_WORD(&reg->hccr,
3203                                             (HCCR_ENABLE_PARITY + 0x1));
3204                                 else
3205                                         /*
3206                                          * SRAM, Instruction RAM and GP RAM
3207                                          * parity.
3208                                          */
3209                                         WRT_REG_WORD(&reg->hccr,
3210                                             (HCCR_ENABLE_PARITY + 0x7));
3211                                 RD_REG_WORD(&reg->hccr);
3212                         }
3213
3214                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3215                 }
3216         }
3217
3218  done:
3219         if (!status && !(status = qla2x00_init_rings(ha))) {
3220                 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3221                 if (!(status = qla2x00_fw_ready(ha))) {
3222                         DEBUG(printk("%s(): Start configure loop, "
3223                             "status = %d\n", __func__, status));
3224
3225                         /* Issue a marker after FW becomes ready. */
3226                         qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3227
3228                         ha->flags.online = 1;
3229                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3230                         wait_time = 256;
3231                         do {
3232                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3233                                 qla2x00_configure_loop(ha);
3234                                 wait_time--;
3235                         } while (!atomic_read(&ha->loop_down_timer) &&
3236                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3237                                 wait_time &&
3238                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3239                 }
3240
3241                 /* if no cable then assume it's good */
3242                 if ((ha->device_flags & DFLG_NO_CABLE))
3243                         status = 0;
3244
3245                 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3246                                 __func__,
3247                                 status));
3248         }
3249         return (status);
3250 }
3251
3252 /*
3253 * qla2x00_reset_adapter
3254 *      Reset adapter.
3255 *
3256 * Input:
3257 *      ha = adapter block pointer.
3258 */
3259 void
3260 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3261 {
3262         unsigned long flags = 0;
3263         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3264
3265         ha->flags.online = 0;
3266         ha->isp_ops.disable_intrs(ha);
3267
3268         spin_lock_irqsave(&ha->hardware_lock, flags);
3269         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3270         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3271         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3272         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3273         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3274 }
3275
3276 void
3277 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3278 {
3279         unsigned long flags = 0;
3280         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3281
3282         ha->flags.online = 0;
3283         ha->isp_ops.disable_intrs(ha);
3284
3285         spin_lock_irqsave(&ha->hardware_lock, flags);
3286         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3287         RD_REG_DWORD(&reg->hccr);
3288         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3289         RD_REG_DWORD(&reg->hccr);
3290         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3291 }
3292
3293 int
3294 qla24xx_nvram_config(scsi_qla_host_t *ha)
3295 {
3296         struct init_cb_24xx *icb;
3297         struct nvram_24xx *nv;
3298         uint32_t *dptr;
3299         uint8_t  *dptr1, *dptr2;
3300         uint32_t chksum;
3301         uint16_t cnt;
3302
3303         icb = (struct init_cb_24xx *)ha->init_cb;
3304         nv = (struct nvram_24xx *)ha->request_ring;
3305
3306         /* Determine NVRAM starting address. */
3307         ha->nvram_size = sizeof(struct nvram_24xx);
3308         ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3309         ha->vpd_size = FA_NVRAM_VPD_SIZE;
3310         ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3311         if (PCI_FUNC(ha->pdev->devfn)) {
3312                 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3313                 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3314         }
3315
3316         /* Get NVRAM data and calculate checksum. */
3317         dptr = (uint32_t *)nv;
3318         ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3319             ha->nvram_size);
3320         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3321                 chksum += le32_to_cpu(*dptr++);
3322
3323         DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3324         DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3325             ha->nvram_size));
3326
3327         /* Bad NVRAM data, set defaults parameters. */
3328         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3329             || nv->id[3] != ' ' ||
3330             nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3331                 /* Reset NVRAM data. */
3332                 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3333                     "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3334                     le16_to_cpu(nv->nvram_version));
3335                 return QLA_FUNCTION_FAILED;
3336         }
3337
3338         /* Reset Initialization control block */
3339         memset(icb, 0, sizeof(struct init_cb_24xx));
3340
3341         /* Copy 1st segment. */
3342         dptr1 = (uint8_t *)icb;
3343         dptr2 = (uint8_t *)&nv->version;
3344         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3345         while (cnt--)
3346                 *dptr1++ = *dptr2++;
3347
3348         icb->login_retry_count = nv->login_retry_count;
3349         icb->link_down_on_nos = nv->link_down_on_nos;
3350
3351         /* Copy 2nd segment. */
3352         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3353         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3354         cnt = (uint8_t *)&icb->reserved_3 -
3355             (uint8_t *)&icb->interrupt_delay_timer;
3356         while (cnt--)
3357                 *dptr1++ = *dptr2++;
3358
3359         /*
3360          * Setup driver NVRAM options.
3361          */
3362         qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3363             "QLA2462");
3364
3365         /* Use alternate WWN? */
3366         if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3367                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3368                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3369         }
3370
3371         /* Prepare nodename */
3372         if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
3373                 /*
3374                  * Firmware will apply the following mask if the nodename was
3375                  * not provided.
3376                  */
3377                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3378                 icb->node_name[0] &= 0xF0;
3379         }
3380
3381         /* Set host adapter parameters. */
3382         ha->flags.disable_risc_code_load = 0;
3383         ha->flags.enable_lip_reset = 0;
3384         ha->flags.enable_lip_full_login =
3385             le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3386         ha->flags.enable_target_reset =
3387             le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
3388         ha->flags.enable_led_scheme = 0;
3389         ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
3390
3391         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3392             (BIT_6 | BIT_5 | BIT_4)) >> 4;
3393
3394         memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3395             sizeof(ha->fw_seriallink_options24));
3396
3397         /* save HBA serial number */
3398         ha->serial0 = icb->port_name[5];
3399         ha->serial1 = icb->port_name[6];
3400         ha->serial2 = icb->port_name[7];
3401         ha->node_name = icb->node_name;
3402         ha->port_name = icb->port_name;
3403
3404         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3405
3406         ha->retry_count = le16_to_cpu(nv->login_retry_count);
3407
3408         /* Set minimum login_timeout to 4 seconds. */
3409         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3410                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3411         if (le16_to_cpu(nv->login_timeout) < 4)
3412                 nv->login_timeout = __constant_cpu_to_le16(4);
3413         ha->login_timeout = le16_to_cpu(nv->login_timeout);
3414         icb->login_timeout = cpu_to_le16(nv->login_timeout);
3415
3416         /* Set minimum RATOV to 200 tenths of a second. */
3417         ha->r_a_tov = 200;
3418
3419         ha->loop_reset_delay = nv->reset_delay;
3420
3421         /* Link Down Timeout = 0:
3422          *
3423          *      When Port Down timer expires we will start returning
3424          *      I/O's to OS with "DID_NO_CONNECT".
3425          *
3426          * Link Down Timeout != 0:
3427          *
3428          *       The driver waits for the link to come up after link down
3429          *       before returning I/Os to OS with "DID_NO_CONNECT".
3430          */
3431         if (le16_to_cpu(nv->link_down_timeout) == 0) {
3432                 ha->loop_down_abort_time =
3433                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3434         } else {
3435                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3436                 ha->loop_down_abort_time =
3437                     (LOOP_DOWN_TIME - ha->link_down_timeout);
3438         }
3439
3440         /* Need enough time to try and get the port back. */
3441         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3442         if (qlport_down_retry)
3443                 ha->port_down_retry_count = qlport_down_retry;
3444
3445         /* Set login_retry_count */
3446         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
3447         if (ha->port_down_retry_count ==
3448             le16_to_cpu(nv->port_down_retry_count) &&
3449             ha->port_down_retry_count > 3)
3450                 ha->login_retry_count = ha->port_down_retry_count;
3451         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3452                 ha->login_retry_count = ha->port_down_retry_count;
3453         if (ql2xloginretrycount)
3454                 ha->login_retry_count = ql2xloginretrycount;
3455
3456         /* Enable ZIO. */
3457         if (!ha->flags.init_done) {
3458                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3459                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3460                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3461                     le16_to_cpu(icb->interrupt_delay_timer): 2;
3462         }
3463         icb->firmware_options_2 &= __constant_cpu_to_le32(
3464             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3465         ha->flags.process_response_queue = 0;
3466         if (ha->zio_mode != QLA_ZIO_DISABLED) {
3467                 ha->zio_mode = QLA_ZIO_MODE_6;
3468
3469                 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3470                     "(%d us).\n", ha->host_no, ha->zio_mode,
3471                     ha->zio_timer * 100));
3472                 qla_printk(KERN_INFO, ha,
3473                     "ZIO mode %d enabled; timer delay (%d us).\n",
3474                     ha->zio_mode, ha->zio_timer * 100);
3475
3476                 icb->firmware_options_2 |= cpu_to_le32(
3477                     (uint32_t)ha->zio_mode);
3478                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3479                 ha->flags.process_response_queue = 1;
3480         }
3481
3482         return QLA_SUCCESS;
3483 }
3484
3485 static int
3486 qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3487 {
3488         int     rval;
3489         int     segments, fragment;
3490         uint32_t faddr;
3491         uint32_t *dcode, dlen;
3492         uint32_t risc_addr;
3493         uint32_t risc_size;
3494         uint32_t i;
3495
3496         rval = QLA_SUCCESS;
3497
3498         segments = FA_RISC_CODE_SEGMENTS;
3499         faddr = FA_RISC_CODE_ADDR;
3500         dcode = (uint32_t *)ha->request_ring;
3501         *srisc_addr = 0;
3502
3503         /* Validate firmware image by checking version. */
3504         qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3505         for (i = 0; i < 4; i++)
3506                 dcode[i] = be32_to_cpu(dcode[i]);
3507         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3508             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3509             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3510                 dcode[3] == 0)) {
3511                 qla_printk(KERN_WARNING, ha,
3512                     "Unable to verify integrity of flash firmware image!\n");
3513                 qla_printk(KERN_WARNING, ha,
3514                     "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3515                     dcode[1], dcode[2], dcode[3]);
3516
3517                 return QLA_FUNCTION_FAILED;
3518         }
3519
3520         while (segments && rval == QLA_SUCCESS) {
3521                 /* Read segment's load information. */
3522                 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3523
3524                 risc_addr = be32_to_cpu(dcode[2]);
3525                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3526                 risc_size = be32_to_cpu(dcode[3]);
3527
3528                 fragment = 0;
3529                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3530                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3531                         if (dlen > risc_size)
3532                                 dlen = risc_size;
3533
3534                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3535                             "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3536                             ha->host_no, risc_addr, dlen, faddr));
3537
3538                         qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3539                         for (i = 0; i < dlen; i++)
3540                                 dcode[i] = swab32(dcode[i]);
3541
3542                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3543                             dlen);
3544                         if (rval) {
3545                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3546                                     "segment %d of firmware\n", ha->host_no,
3547                                     fragment));
3548                                 qla_printk(KERN_WARNING, ha,
3549                                     "[ERROR] Failed to load segment %d of "
3550                                     "firmware\n", fragment);
3551                                 break;
3552                         }
3553
3554                         faddr += dlen;
3555                         risc_addr += dlen;
3556                         risc_size -= dlen;
3557                         fragment++;
3558                 }
3559
3560                 /* Next segment. */
3561                 segments--;
3562         }
3563
3564         return rval;
3565 }
3566
3567 #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3568
3569 int
3570 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3571 {
3572         int     rval;
3573         int     i, fragment;
3574         uint16_t *wcode, *fwcode;
3575         uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3576         struct fw_blob *blob;
3577
3578         /* Load firmware blob. */
3579         blob = qla2x00_request_firmware(ha);
3580         if (!blob) {
3581                 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3582                 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3583                     "from: " QLA_FW_URL ".\n");
3584                 return QLA_FUNCTION_FAILED;
3585         }
3586
3587         rval = QLA_SUCCESS;
3588
3589         wcode = (uint16_t *)ha->request_ring;
3590         *srisc_addr = 0;
3591         fwcode = (uint16_t *)blob->fw->data;
3592         fwclen = 0;
3593
3594         /* Validate firmware image by checking version. */
3595         if (blob->fw->size < 8 * sizeof(uint16_t)) {
3596                 qla_printk(KERN_WARNING, ha,
3597                     "Unable to verify integrity of firmware image (%Zd)!\n",
3598                     blob->fw->size);
3599                 goto fail_fw_integrity;
3600         }
3601         for (i = 0; i < 4; i++)
3602                 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3603         if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3604             wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3605                 wcode[2] == 0 && wcode[3] == 0)) {
3606                 qla_printk(KERN_WARNING, ha,
3607                     "Unable to verify integrity of firmware image!\n");
3608                 qla_printk(KERN_WARNING, ha,
3609                     "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3610                     wcode[1], wcode[2], wcode[3]);
3611                 goto fail_fw_integrity;
3612         }
3613
3614         seg = blob->segs;
3615         while (*seg && rval == QLA_SUCCESS) {
3616                 risc_addr = *seg;
3617                 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3618                 risc_size = be16_to_cpu(fwcode[3]);
3619
3620                 /* Validate firmware image size. */
3621                 fwclen += risc_size * sizeof(uint16_t);
3622                 if (blob->fw->size < fwclen) {
3623                         qla_printk(KERN_WARNING, ha,
3624                             "Unable to verify integrity of firmware image "
3625                             "(%Zd)!\n", blob->fw->size);
3626                         goto fail_fw_integrity;
3627                 }
3628
3629                 fragment = 0;
3630                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3631                         wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3632                         if (wlen > risc_size)
3633                                 wlen = risc_size;
3634
3635                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3636                             "addr %x, number of words 0x%x.\n", ha->host_no,
3637                             risc_addr, wlen));
3638
3639                         for (i = 0; i < wlen; i++)
3640                                 wcode[i] = swab16(fwcode[i]);
3641
3642                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3643                             wlen);
3644                         if (rval) {
3645                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3646                                     "segment %d of firmware\n", ha->host_no,
3647                                     fragment));
3648                                 qla_printk(KERN_WARNING, ha,
3649                                     "[ERROR] Failed to load segment %d of "
3650                                     "firmware\n", fragment);
3651                                 break;
3652                         }
3653
3654                         fwcode += wlen;
3655                         risc_addr += wlen;
3656                         risc_size -= wlen;
3657                         fragment++;
3658                 }
3659
3660                 /* Next segment. */
3661                 seg++;
3662         }
3663         return rval;
3664
3665 fail_fw_integrity:
3666         return QLA_FUNCTION_FAILED;
3667 }
3668
3669 int
3670 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3671 {
3672         int     rval;
3673         int     segments, fragment;
3674         uint32_t *dcode, dlen;
3675         uint32_t risc_addr;
3676         uint32_t risc_size;
3677         uint32_t i;
3678         struct fw_blob *blob;
3679         uint32_t *fwcode, fwclen;
3680
3681         /* Load firmware blob. */
3682         blob = qla2x00_request_firmware(ha);
3683         if (!blob) {
3684                 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3685                 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3686                     "from: " QLA_FW_URL ".\n");
3687
3688                 /* Try to load RISC code from flash. */
3689                 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3690                     "outdated) firmware from flash.\n");
3691                 return qla24xx_load_risc_flash(ha, srisc_addr);
3692         }
3693
3694         rval = QLA_SUCCESS;
3695
3696         segments = FA_RISC_CODE_SEGMENTS;
3697         dcode = (uint32_t *)ha->request_ring;
3698         *srisc_addr = 0;
3699         fwcode = (uint32_t *)blob->fw->data;
3700         fwclen = 0;
3701
3702         /* Validate firmware image by checking version. */
3703         if (blob->fw->size < 8 * sizeof(uint32_t)) {
3704                 qla_printk(KERN_WARNING, ha,
3705                     "Unable to verify integrity of firmware image (%Zd)!\n",
3706                     blob->fw->size);
3707                 goto fail_fw_integrity;
3708         }
3709         for (i = 0; i < 4; i++)
3710                 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3711         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3712             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3713             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3714                 dcode[3] == 0)) {
3715                 qla_printk(KERN_WARNING, ha,
3716                     "Unable to verify integrity of firmware image!\n");
3717                 qla_printk(KERN_WARNING, ha,
3718                     "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3719                     dcode[1], dcode[2], dcode[3]);
3720                 goto fail_fw_integrity;
3721         }
3722
3723         while (segments && rval == QLA_SUCCESS) {
3724                 risc_addr = be32_to_cpu(fwcode[2]);
3725                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3726                 risc_size = be32_to_cpu(fwcode[3]);
3727
3728                 /* Validate firmware image size. */
3729                 fwclen += risc_size * sizeof(uint32_t);
3730                 if (blob->fw->size < fwclen) {
3731                         qla_printk(KERN_WARNING, ha,
3732                             "Unable to verify integrity of firmware image "
3733                             "(%Zd)!\n", blob->fw->size);
3734
3735                         goto fail_fw_integrity;
3736                 }
3737
3738                 fragment = 0;
3739                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3740                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3741                         if (dlen > risc_size)
3742                                 dlen = risc_size;
3743
3744                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3745                             "addr %x, number of dwords 0x%x.\n", ha->host_no,
3746                             risc_addr, dlen));
3747
3748                         for (i = 0; i < dlen; i++)
3749                                 dcode[i] = swab32(fwcode[i]);
3750
3751                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3752                             dlen);
3753                         if (rval) {
3754                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3755                                     "segment %d of firmware\n", ha->host_no,
3756                                     fragment));
3757                                 qla_printk(KERN_WARNING, ha,
3758                                     "[ERROR] Failed to load segment %d of "
3759                                     "firmware\n", fragment);
3760                                 break;
3761                         }
3762
3763                         fwcode += dlen;
3764                         risc_addr += dlen;
3765                         risc_size -= dlen;
3766                         fragment++;
3767                 }
3768
3769                 /* Next segment. */
3770                 segments--;
3771         }
3772         return rval;
3773
3774 fail_fw_integrity:
3775         return QLA_FUNCTION_FAILED;
3776 }
3777
3778 void
3779 qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
3780 {
3781         int ret, retries;
3782
3783         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
3784                 return;
3785
3786         ret = qla2x00_stop_firmware(ha);
3787         for (retries = 5; ret != QLA_SUCCESS && retries ; retries--) {
3788                 qla2x00_reset_chip(ha);
3789                 if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
3790                         continue;
3791                 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
3792                         continue;
3793                 qla_printk(KERN_INFO, ha,
3794                     "Attempting retry of stop-firmware command...\n");
3795                 ret = qla2x00_stop_firmware(ha);
3796         }
3797 }