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