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