Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/voyager-2.6
[linux-2.6.git] / drivers / scsi / qla2xxx / qla_init.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2005 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/delay.h>
10 #include <linux/vmalloc.h>
11
12 #include "qla_devtbl.h"
13
14 /* 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 "