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