d4dd26bf434ed37b7fd732174dcd80ee441ce517
[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 qla2x00_rport_add(void *data)
1729 {
1730         fc_port_t *fcport = data;
1731
1732         qla2x00_reg_remote_port(fcport->ha, fcport);
1733 }
1734
1735 static void
1736 qla2x00_rport_del(void *data)
1737 {
1738         fc_port_t *fcport = data;
1739         struct fc_rport *rport;
1740         unsigned long flags;
1741
1742         spin_lock_irqsave(&fcport->rport_lock, flags);
1743         rport = fcport->drport;
1744         fcport->drport = NULL;
1745         spin_unlock_irqrestore(&fcport->rport_lock, flags);
1746         if (rport)
1747                 fc_remote_port_delete(rport);
1748
1749 }
1750
1751 /**
1752  * qla2x00_alloc_fcport() - Allocate a generic fcport.
1753  * @ha: HA context
1754  * @flags: allocation flags
1755  *
1756  * Returns a pointer to the allocated fcport, or NULL, if none available.
1757  */
1758 fc_port_t *
1759 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1760 {
1761         fc_port_t *fcport;
1762
1763         fcport = kmalloc(sizeof(fc_port_t), flags);
1764         if (fcport == NULL)
1765                 return (fcport);
1766
1767         /* Setup fcport template structure. */
1768         memset(fcport, 0, sizeof (fc_port_t));
1769         fcport->ha = ha;
1770         fcport->port_type = FCT_UNKNOWN;
1771         fcport->loop_id = FC_NO_LOOP_ID;
1772         fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
1773         atomic_set(&fcport->state, FCS_UNCONFIGURED);
1774         fcport->flags = FCF_RLC_SUPPORT;
1775         fcport->supported_classes = FC_COS_UNSPECIFIED;
1776         spin_lock_init(&fcport->rport_lock);
1777         INIT_WORK(&fcport->rport_add_work, qla2x00_rport_add, fcport);
1778         INIT_WORK(&fcport->rport_del_work, qla2x00_rport_del, fcport);
1779
1780         return (fcport);
1781 }
1782
1783 /*
1784  * qla2x00_configure_loop
1785  *      Updates Fibre Channel Device Database with what is actually on loop.
1786  *
1787  * Input:
1788  *      ha                = adapter block pointer.
1789  *
1790  * Returns:
1791  *      0 = success.
1792  *      1 = error.
1793  *      2 = database was full and device was not configured.
1794  */
1795 int
1796 qla2x00_configure_loop(scsi_qla_host_t *ha)
1797 {
1798         int  rval;
1799         unsigned long flags, save_flags;
1800
1801         rval = QLA_SUCCESS;
1802
1803         /* Get Initiator ID */
1804         if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1805                 rval = qla2x00_configure_hba(ha);
1806                 if (rval != QLA_SUCCESS) {
1807                         DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1808                             ha->host_no));
1809                         return (rval);
1810                 }
1811         }
1812
1813         save_flags = flags = ha->dpc_flags;
1814         DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1815             ha->host_no, flags));
1816
1817         /*
1818          * If we have both an RSCN and PORT UPDATE pending then handle them
1819          * both at the same time.
1820          */
1821         clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1822         clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1823
1824         /* Determine what we need to do */
1825         if (ha->current_topology == ISP_CFG_FL &&
1826             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1827
1828                 ha->flags.rscn_queue_overflow = 1;
1829                 set_bit(RSCN_UPDATE, &flags);
1830
1831         } else if (ha->current_topology == ISP_CFG_F &&
1832             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1833
1834                 ha->flags.rscn_queue_overflow = 1;
1835                 set_bit(RSCN_UPDATE, &flags);
1836                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1837
1838         } else if (!ha->flags.online ||
1839             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1840
1841                 ha->flags.rscn_queue_overflow = 1;
1842                 set_bit(RSCN_UPDATE, &flags);
1843                 set_bit(LOCAL_LOOP_UPDATE, &flags);
1844         }
1845
1846         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1847                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1848                         rval = QLA_FUNCTION_FAILED;
1849                 } else {
1850                         rval = qla2x00_configure_local_loop(ha);
1851                 }
1852         }
1853
1854         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1855                 if (LOOP_TRANSITION(ha)) {
1856                         rval = QLA_FUNCTION_FAILED;
1857                 } else {
1858                         rval = qla2x00_configure_fabric(ha);
1859                 }
1860         }
1861
1862         if (rval == QLA_SUCCESS) {
1863                 if (atomic_read(&ha->loop_down_timer) ||
1864                     test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1865                         rval = QLA_FUNCTION_FAILED;
1866                 } else {
1867                         atomic_set(&ha->loop_state, LOOP_READY);
1868
1869                         DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1870                 }
1871         }
1872
1873         if (rval) {
1874                 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1875                     __func__, ha->host_no));
1876         } else {
1877                 DEBUG3(printk("%s: exiting normally\n", __func__));
1878         }
1879
1880         /* Restore state if a resync event occured during processing */
1881         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1882                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1883                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1884                 if (test_bit(RSCN_UPDATE, &save_flags))
1885                         set_bit(RSCN_UPDATE, &ha->dpc_flags);
1886         }
1887
1888         return (rval);
1889 }
1890
1891
1892
1893 /*
1894  * qla2x00_configure_local_loop
1895  *      Updates Fibre Channel Device Database with local loop devices.
1896  *
1897  * Input:
1898  *      ha = adapter block pointer.
1899  *
1900  * Returns:
1901  *      0 = success.
1902  */
1903 int
1904 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1905 {
1906         int             rval, rval2;
1907         int             found_devs;
1908         int             found;
1909         fc_port_t       *fcport, *new_fcport;
1910
1911         uint16_t        index;
1912         uint16_t        entries;
1913         char            *id_iter;
1914         uint16_t        loop_id;
1915         uint8_t         domain, area, al_pa;
1916
1917         found_devs = 0;
1918         new_fcport = NULL;
1919         entries = MAX_FIBRE_DEVICES;
1920
1921         DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1922         DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1923
1924         /* Get list of logged in devices. */
1925         memset(ha->gid_list, 0, GID_LIST_SIZE);
1926         rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1927             &entries);
1928         if (rval != QLA_SUCCESS)
1929                 goto cleanup_allocation;
1930
1931         DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1932             ha->host_no, entries));
1933         DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1934             entries * sizeof(struct gid_list_info)));
1935
1936         /* Allocate temporary fcport for any new fcports discovered. */
1937         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1938         if (new_fcport == NULL) {
1939                 rval = QLA_MEMORY_ALLOC_FAILED;
1940                 goto cleanup_allocation;
1941         }
1942         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1943
1944         /*
1945          * Mark local devices that were present with FCF_DEVICE_LOST for now.
1946          */
1947         list_for_each_entry(fcport, &ha->fcports, list) {
1948                 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1949                     fcport->port_type != FCT_BROADCAST &&
1950                     (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1951
1952                         DEBUG(printk("scsi(%ld): Marking port lost, "
1953                             "loop_id=0x%04x\n",
1954                             ha->host_no, fcport->loop_id));
1955
1956                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
1957                         fcport->flags &= ~FCF_FARP_DONE;
1958                 }
1959         }
1960
1961         /* Add devices to port list. */
1962         id_iter = (char *)ha->gid_list;
1963         for (index = 0; index < entries; index++) {
1964                 domain = ((struct gid_list_info *)id_iter)->domain;
1965                 area = ((struct gid_list_info *)id_iter)->area;
1966                 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1967                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1968                         loop_id = (uint16_t)
1969                             ((struct gid_list_info *)id_iter)->loop_id_2100;
1970                 else
1971                         loop_id = le16_to_cpu(
1972                             ((struct gid_list_info *)id_iter)->loop_id);
1973                 id_iter += ha->gid_list_info_size;
1974
1975                 /* Bypass reserved domain fields. */
1976                 if ((domain & 0xf0) == 0xf0)
1977                         continue;
1978
1979                 /* Bypass if not same domain and area of adapter. */
1980                 if (area && domain &&
1981                     (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
1982                         continue;
1983
1984                 /* Bypass invalid local loop ID. */
1985                 if (loop_id > LAST_LOCAL_LOOP_ID)
1986                         continue;
1987
1988                 /* Fill in member data. */
1989                 new_fcport->d_id.b.domain = domain;
1990                 new_fcport->d_id.b.area = area;
1991                 new_fcport->d_id.b.al_pa = al_pa;
1992                 new_fcport->loop_id = loop_id;
1993                 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1994                 if (rval2 != QLA_SUCCESS) {
1995                         DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1996                             "information -- get_port_database=%x, "
1997                             "loop_id=0x%04x\n",
1998                             ha->host_no, rval2, new_fcport->loop_id));
1999                         DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2000                             ha->host_no));
2001                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2002                         continue;
2003                 }
2004
2005                 /* Check for matching device in port list. */
2006                 found = 0;
2007                 fcport = NULL;
2008                 list_for_each_entry(fcport, &ha->fcports, list) {
2009                         if (memcmp(new_fcport->port_name, fcport->port_name,
2010                             WWN_SIZE))
2011                                 continue;
2012
2013                         fcport->flags &= ~(FCF_FABRIC_DEVICE |
2014                             FCF_PERSISTENT_BOUND);
2015                         fcport->loop_id = new_fcport->loop_id;
2016                         fcport->port_type = new_fcport->port_type;
2017                         fcport->d_id.b24 = new_fcport->d_id.b24;
2018                         memcpy(fcport->node_name, new_fcport->node_name,
2019                             WWN_SIZE);
2020
2021                         found++;
2022                         break;
2023                 }
2024
2025                 if (!found) {
2026                         /* New device, add to fcports list. */
2027                         new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2028                         list_add_tail(&new_fcport->list, &ha->fcports);
2029
2030                         /* Allocate a new replacement fcport. */
2031                         fcport = new_fcport;
2032                         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2033                         if (new_fcport == NULL) {
2034                                 rval = QLA_MEMORY_ALLOC_FAILED;
2035                                 goto cleanup_allocation;
2036                         }
2037                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2038                 }
2039
2040                 qla2x00_update_fcport(ha, fcport);
2041
2042                 found_devs++;
2043         }
2044
2045 cleanup_allocation:
2046         kfree(new_fcport);
2047
2048         if (rval != QLA_SUCCESS) {
2049                 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2050                     "rval=%x\n", ha->host_no, rval));
2051         }
2052
2053         if (found_devs) {
2054                 ha->device_flags |= DFLG_LOCAL_DEVICES;
2055                 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2056         }
2057
2058         return (rval);
2059 }
2060
2061 static void
2062 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2063 {
2064         fc_port_t       *fcport;
2065
2066         qla2x00_mark_all_devices_lost(ha, 0);
2067         list_for_each_entry(fcport, &ha->fcports, list) {
2068                 if (fcport->port_type != FCT_TARGET)
2069                         continue;
2070
2071                 qla2x00_update_fcport(ha, fcport);
2072         }
2073 }
2074
2075 /*
2076  * qla2x00_update_fcport
2077  *      Updates device on list.
2078  *
2079  * Input:
2080  *      ha = adapter block pointer.
2081  *      fcport = port structure pointer.
2082  *
2083  * Return:
2084  *      0  - Success
2085  *  BIT_0 - error
2086  *
2087  * Context:
2088  *      Kernel context.
2089  */
2090 void
2091 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2092 {
2093         fcport->ha = ha;
2094         fcport->login_retry = 0;
2095         fcport->port_login_retry_count = ha->port_down_retry_count *
2096             PORT_RETRY_TIME;
2097         atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2098             PORT_RETRY_TIME);
2099         fcport->flags &= ~FCF_LOGIN_NEEDED;
2100
2101         if (fcport->port_type == FCT_INITIATOR ||
2102             fcport->port_type == FCT_BROADCAST)
2103                 fcport->device_type = TYPE_PROCESSOR;
2104
2105         atomic_set(&fcport->state, FCS_ONLINE);
2106
2107         if (ha->flags.init_done)
2108                 qla2x00_reg_remote_port(ha, fcport);
2109 }
2110
2111 void
2112 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2113 {
2114         struct fc_rport_identifiers rport_ids;
2115         struct fc_rport *rport;
2116         unsigned long flags;
2117
2118         if (fcport->drport)
2119                 qla2x00_rport_del(fcport);
2120         if (fcport->rport)
2121                 return;
2122
2123         rport_ids.node_name = wwn_to_u64(fcport->node_name);
2124         rport_ids.port_name = wwn_to_u64(fcport->port_name);
2125         rport_ids.port_id = fcport->d_id.b.domain << 16 |
2126             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2127         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2128         rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2129         if (!rport) {
2130                 qla_printk(KERN_WARNING, ha,
2131                     "Unable to allocate fc remote port!\n");
2132                 return;
2133         }
2134         spin_lock_irqsave(&fcport->rport_lock, flags);
2135         fcport->rport = rport;
2136         *((fc_port_t **)rport->dd_data) = fcport;
2137         spin_unlock_irqrestore(&fcport->rport_lock, flags);
2138
2139         rport->supported_classes = fcport->supported_classes;
2140
2141         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2142         if (fcport->port_type == FCT_INITIATOR)
2143                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2144         if (fcport->port_type == FCT_TARGET)
2145                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2146         fc_remote_port_rolechg(rport, rport_ids.roles);
2147
2148         if (rport->scsi_target_id != -1 &&
2149             rport->scsi_target_id < ha->host->max_id)
2150                 fcport->os_target_id = rport->scsi_target_id;
2151 }
2152
2153 /*
2154  * qla2x00_configure_fabric
2155  *      Setup SNS devices with loop ID's.
2156  *
2157  * Input:
2158  *      ha = adapter block pointer.
2159  *
2160  * Returns:
2161  *      0 = success.
2162  *      BIT_0 = error
2163  */
2164 int
2165 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2166 {
2167         int     rval, rval2;
2168         fc_port_t       *fcport, *fcptemp;
2169         uint16_t        next_loopid;
2170         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2171         uint16_t        loop_id;
2172         LIST_HEAD(new_fcports);
2173
2174         /* If FL port exists, then SNS is present */
2175         if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2176                 loop_id = NPH_F_PORT;
2177         else
2178                 loop_id = SNS_FL_PORT;
2179         rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
2180         if (rval != QLA_SUCCESS) {
2181                 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2182                     "Port\n", ha->host_no));
2183
2184                 ha->device_flags &= ~SWITCH_FOUND;
2185                 return (QLA_SUCCESS);
2186         }
2187
2188         /* Mark devices that need re-synchronization. */
2189         rval2 = qla2x00_device_resync(ha);
2190         if (rval2 == QLA_RSCNS_HANDLED) {
2191                 /* No point doing the scan, just continue. */
2192                 return (QLA_SUCCESS);
2193         }
2194         do {
2195                 /* FDMI support. */
2196                 if (ql2xfdmienable &&
2197                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2198                         qla2x00_fdmi_register(ha);
2199
2200                 /* Ensure we are logged into the SNS. */
2201                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2202                         loop_id = NPH_SNS;
2203                 else
2204                         loop_id = SIMPLE_NAME_SERVER;
2205                 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
2206                     0xfc, mb, BIT_1 | BIT_0);
2207                 if (mb[0] != MBS_COMMAND_COMPLETE) {
2208                         DEBUG2(qla_printk(KERN_INFO, ha,
2209                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2210                             "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2211                             mb[0], mb[1], mb[2], mb[6], mb[7]));
2212                         return (QLA_SUCCESS);
2213                 }
2214
2215                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2216                         if (qla2x00_rft_id(ha)) {
2217                                 /* EMPTY */
2218                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2219                                     "TYPE failed.\n", ha->host_no));
2220                         }
2221                         if (qla2x00_rff_id(ha)) {
2222                                 /* EMPTY */
2223                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2224                                     "Features failed.\n", ha->host_no));
2225                         }
2226                         if (qla2x00_rnn_id(ha)) {
2227                                 /* EMPTY */
2228                                 DEBUG2(printk("scsi(%ld): Register Node Name "
2229                                     "failed.\n", ha->host_no));
2230                         } else if (qla2x00_rsnn_nn(ha)) {
2231                                 /* EMPTY */
2232                                 DEBUG2(printk("scsi(%ld): Register Symbolic "
2233                                     "Node Name failed.\n", ha->host_no));
2234                         }
2235                 }
2236
2237                 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2238                 if (rval != QLA_SUCCESS)
2239                         break;
2240
2241                 /*
2242                  * Logout all previous fabric devices marked lost, except
2243                  * tape devices.
2244                  */
2245                 list_for_each_entry(fcport, &ha->fcports, list) {
2246                         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2247                                 break;
2248
2249                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2250                                 continue;
2251
2252                         if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2253                                 qla2x00_mark_device_lost(ha, fcport,
2254                                     ql2xplogiabsentdevice, 0);
2255                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
2256                                     (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2257                                     fcport->port_type != FCT_INITIATOR &&
2258                                     fcport->port_type != FCT_BROADCAST) {
2259                                         ha->isp_ops.fabric_logout(ha,
2260                                             fcport->loop_id,
2261                                             fcport->d_id.b.domain,
2262                                             fcport->d_id.b.area,
2263                                             fcport->d_id.b.al_pa);
2264                                         fcport->loop_id = FC_NO_LOOP_ID;
2265                                 }
2266                         }
2267                 }
2268
2269                 /* Starting free loop ID. */
2270                 next_loopid = ha->min_external_loopid;
2271
2272                 /*
2273                  * Scan through our port list and login entries that need to be
2274                  * logged in.
2275                  */
2276                 list_for_each_entry(fcport, &ha->fcports, list) {
2277                         if (atomic_read(&ha->loop_down_timer) ||
2278                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2279                                 break;
2280
2281                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2282                             (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2283                                 continue;
2284
2285                         if (fcport->loop_id == FC_NO_LOOP_ID) {
2286                                 fcport->loop_id = next_loopid;
2287                                 rval = qla2x00_find_new_loop_id(ha, fcport);
2288                                 if (rval != QLA_SUCCESS) {
2289                                         /* Ran out of IDs to use */
2290                                         break;
2291                                 }
2292                         }
2293                         /* Login and update database */
2294                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2295                 }
2296
2297                 /* Exit if out of loop IDs. */
2298                 if (rval != QLA_SUCCESS) {
2299                         break;
2300                 }
2301
2302                 /*
2303                  * Login and add the new devices to our port list.
2304                  */
2305                 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2306                         if (atomic_read(&ha->loop_down_timer) ||
2307                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2308                                 break;
2309
2310                         /* Find a new loop ID to use. */
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                         /* Remove device from the new list and add it to DB */
2319                         list_del(&fcport->list);
2320                         list_add_tail(&fcport->list, &ha->fcports);
2321
2322                         /* Login and update database */
2323                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2324                 }
2325         } while (0);
2326
2327         /* Free all new device structures not processed. */
2328         list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2329                 list_del(&fcport->list);
2330                 kfree(fcport);
2331         }
2332
2333         if (rval) {
2334                 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2335                     "rval=%d\n", ha->host_no, rval));
2336         }
2337
2338         return (rval);
2339 }
2340
2341
2342 /*
2343  * qla2x00_find_all_fabric_devs
2344  *
2345  * Input:
2346  *      ha = adapter block pointer.
2347  *      dev = database device entry pointer.
2348  *
2349  * Returns:
2350  *      0 = success.
2351  *
2352  * Context:
2353  *      Kernel context.
2354  */
2355 static int
2356 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2357 {
2358         int             rval;
2359         uint16_t        loop_id;
2360         fc_port_t       *fcport, *new_fcport, *fcptemp;
2361         int             found;
2362
2363         sw_info_t       *swl;
2364         int             swl_idx;
2365         int             first_dev, last_dev;
2366         port_id_t       wrap, nxt_d_id;
2367
2368         rval = QLA_SUCCESS;
2369
2370         /* Try GID_PT to get device list, else GAN. */
2371         swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2372         if (swl == NULL) {
2373                 /*EMPTY*/
2374                 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2375                     "on GA_NXT\n", ha->host_no));
2376         } else {
2377                 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2378                 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2379                         kfree(swl);
2380                         swl = NULL;
2381                 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2382                         kfree(swl);
2383                         swl = NULL;
2384                 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2385                         kfree(swl);
2386                         swl = NULL;
2387                 }
2388         }
2389         swl_idx = 0;
2390
2391         /* Allocate temporary fcport for any new fcports discovered. */
2392         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2393         if (new_fcport == NULL) {
2394                 kfree(swl);
2395                 return (QLA_MEMORY_ALLOC_FAILED);
2396         }
2397         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2398
2399         /* Set start port ID scan at adapter ID. */
2400         first_dev = 1;
2401         last_dev = 0;
2402
2403         /* Starting free loop ID. */
2404         loop_id = ha->min_external_loopid;
2405         for (; loop_id <= ha->last_loop_id; loop_id++) {
2406                 if (qla2x00_is_reserved_id(ha, loop_id))
2407                         continue;
2408
2409                 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2410                         break;
2411
2412                 if (swl != NULL) {
2413                         if (last_dev) {
2414                                 wrap.b24 = new_fcport->d_id.b24;
2415                         } else {
2416                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2417                                 memcpy(new_fcport->node_name,
2418                                     swl[swl_idx].node_name, WWN_SIZE);
2419                                 memcpy(new_fcport->port_name,
2420                                     swl[swl_idx].port_name, WWN_SIZE);
2421
2422                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2423                                         last_dev = 1;
2424                                 }
2425                                 swl_idx++;
2426                         }
2427                 } else {
2428                         /* Send GA_NXT to the switch */
2429                         rval = qla2x00_ga_nxt(ha, new_fcport);
2430                         if (rval != QLA_SUCCESS) {
2431                                 qla_printk(KERN_WARNING, ha,
2432                                     "SNS scan failed -- assuming zero-entry "
2433                                     "result...\n");
2434                                 list_for_each_entry_safe(fcport, fcptemp,
2435                                     new_fcports, list) {
2436                                         list_del(&fcport->list);
2437                                         kfree(fcport);
2438                                 }
2439                                 rval = QLA_SUCCESS;
2440                                 break;
2441                         }
2442                 }
2443
2444                 /* If wrap on switch device list, exit. */
2445                 if (first_dev) {
2446                         wrap.b24 = new_fcport->d_id.b24;
2447                         first_dev = 0;
2448                 } else if (new_fcport->d_id.b24 == wrap.b24) {
2449                         DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2450                             ha->host_no, new_fcport->d_id.b.domain,
2451                             new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2452                         break;
2453                 }
2454
2455                 /* Bypass if host adapter. */
2456                 if (new_fcport->d_id.b24 == ha->d_id.b24)
2457                         continue;
2458
2459                 /* Bypass if same domain and area of adapter. */
2460                 if (((new_fcport->d_id.b24 & 0xffff00) ==
2461                     (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2462                         ISP_CFG_FL)
2463                             continue;
2464
2465                 /* Bypass reserved domain fields. */
2466                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2467                         continue;
2468
2469                 /* Locate matching device in database. */
2470                 found = 0;
2471                 list_for_each_entry(fcport, &ha->fcports, list) {
2472                         if (memcmp(new_fcport->port_name, fcport->port_name,
2473                             WWN_SIZE))
2474                                 continue;
2475
2476                         found++;
2477
2478                         /*
2479                          * If address the same and state FCS_ONLINE, nothing
2480                          * changed.
2481                          */
2482                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2483                             atomic_read(&fcport->state) == FCS_ONLINE) {
2484                                 break;
2485                         }
2486
2487                         /*
2488                          * If device was not a fabric device before.
2489                          */
2490                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2491                                 fcport->d_id.b24 = new_fcport->d_id.b24;
2492                                 fcport->loop_id = FC_NO_LOOP_ID;
2493                                 fcport->flags |= (FCF_FABRIC_DEVICE |
2494                                     FCF_LOGIN_NEEDED);
2495                                 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2496                                 break;
2497                         }
2498
2499                         /*
2500                          * Port ID changed or device was marked to be updated;
2501                          * Log it out if still logged in and mark it for
2502                          * relogin later.
2503                          */
2504                         fcport->d_id.b24 = new_fcport->d_id.b24;
2505                         fcport->flags |= FCF_LOGIN_NEEDED;
2506                         if (fcport->loop_id != FC_NO_LOOP_ID &&
2507                             (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2508                             fcport->port_type != FCT_INITIATOR &&
2509                             fcport->port_type != FCT_BROADCAST) {
2510                                 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2511                                     fcport->d_id.b.domain, fcport->d_id.b.area,
2512                                     fcport->d_id.b.al_pa);
2513                                 fcport->loop_id = FC_NO_LOOP_ID;
2514                         }
2515
2516                         break;
2517                 }
2518
2519                 if (found)
2520                         continue;
2521
2522                 /* If device was not in our fcports list, then add it. */
2523                 list_add_tail(&new_fcport->list, new_fcports);
2524
2525                 /* Allocate a new replacement fcport. */
2526                 nxt_d_id.b24 = new_fcport->d_id.b24;
2527                 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2528                 if (new_fcport == NULL) {
2529                         kfree(swl);
2530                         return (QLA_MEMORY_ALLOC_FAILED);
2531                 }
2532                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2533                 new_fcport->d_id.b24 = nxt_d_id.b24;
2534         }
2535
2536         kfree(swl);
2537         kfree(new_fcport);
2538
2539         if (!list_empty(new_fcports))
2540                 ha->device_flags |= DFLG_FABRIC_DEVICES;
2541
2542         return (rval);
2543 }
2544
2545 /*
2546  * qla2x00_find_new_loop_id
2547  *      Scan through our port list and find a new usable loop ID.
2548  *
2549  * Input:
2550  *      ha:     adapter state pointer.
2551  *      dev:    port structure pointer.
2552  *
2553  * Returns:
2554  *      qla2x00 local function return status code.
2555  *
2556  * Context:
2557  *      Kernel context.
2558  */
2559 int
2560 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2561 {
2562         int     rval;
2563         int     found;
2564         fc_port_t *fcport;
2565         uint16_t first_loop_id;
2566
2567         rval = QLA_SUCCESS;
2568
2569         /* Save starting loop ID. */
2570         first_loop_id = dev->loop_id;
2571
2572         for (;;) {
2573                 /* Skip loop ID if already used by adapter. */
2574                 if (dev->loop_id == ha->loop_id) {
2575                         dev->loop_id++;
2576                 }
2577
2578                 /* Skip reserved loop IDs. */
2579                 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2580                         dev->loop_id++;
2581                 }
2582
2583                 /* Reset loop ID if passed the end. */
2584                 if (dev->loop_id > ha->last_loop_id) {
2585                         /* first loop ID. */
2586                         dev->loop_id = ha->min_external_loopid;
2587                 }
2588
2589                 /* Check for loop ID being already in use. */
2590                 found = 0;
2591                 fcport = NULL;
2592                 list_for_each_entry(fcport, &ha->fcports, list) {
2593                         if (fcport->loop_id == dev->loop_id && fcport != dev) {
2594                                 /* ID possibly in use */
2595                                 found++;
2596                                 break;
2597                         }
2598                 }
2599
2600                 /* If not in use then it is free to use. */
2601                 if (!found) {
2602                         break;
2603                 }
2604
2605                 /* ID in use. Try next value. */
2606                 dev->loop_id++;
2607
2608                 /* If wrap around. No free ID to use. */
2609                 if (dev->loop_id == first_loop_id) {
2610                         dev->loop_id = FC_NO_LOOP_ID;
2611                         rval = QLA_FUNCTION_FAILED;
2612                         break;
2613                 }
2614         }
2615
2616         return (rval);
2617 }
2618
2619 /*
2620  * qla2x00_device_resync
2621  *      Marks devices in the database that needs resynchronization.
2622  *
2623  * Input:
2624  *      ha = adapter block pointer.
2625  *
2626  * Context:
2627  *      Kernel context.
2628  */
2629 static int
2630 qla2x00_device_resync(scsi_qla_host_t *ha)
2631 {
2632         int     rval;
2633         int     rval2;
2634         uint32_t mask;
2635         fc_port_t *fcport;
2636         uint32_t rscn_entry;
2637         uint8_t rscn_out_iter;
2638         uint8_t format;
2639         port_id_t d_id;
2640
2641         rval = QLA_RSCNS_HANDLED;
2642
2643         while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2644             ha->flags.rscn_queue_overflow) {
2645
2646                 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2647                 format = MSB(MSW(rscn_entry));
2648                 d_id.b.domain = LSB(MSW(rscn_entry));
2649                 d_id.b.area = MSB(LSW(rscn_entry));
2650                 d_id.b.al_pa = LSB(LSW(rscn_entry));
2651
2652                 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2653                     "[%02x/%02x%02x%02x].\n",
2654                     ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2655                     d_id.b.area, d_id.b.al_pa));
2656
2657                 ha->rscn_out_ptr++;
2658                 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2659                         ha->rscn_out_ptr = 0;
2660
2661                 /* Skip duplicate entries. */
2662                 for (rscn_out_iter = ha->rscn_out_ptr;
2663                     !ha->flags.rscn_queue_overflow &&
2664                     rscn_out_iter != ha->rscn_in_ptr;
2665                     rscn_out_iter = (rscn_out_iter ==
2666                         (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2667
2668                         if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2669                                 break;
2670
2671                         DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2672                             "entry found at [%d].\n", ha->host_no,
2673                             rscn_out_iter));
2674
2675                         ha->rscn_out_ptr = rscn_out_iter;
2676                 }
2677
2678                 /* Queue overflow, set switch default case. */
2679                 if (ha->flags.rscn_queue_overflow) {
2680                         DEBUG(printk("scsi(%ld): device_resync: rscn "
2681                             "overflow.\n", ha->host_no));
2682
2683                         format = 3;
2684                         ha->flags.rscn_queue_overflow = 0;
2685                 }
2686
2687                 switch (format) {
2688                 case 0:
2689                         if (ql2xprocessrscn &&
2690                             !IS_QLA2100(ha) && !IS_QLA2200(ha) &&
2691                             !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
2692                             !IS_QLA24XX(ha) && !IS_QLA54XX(ha) &&
2693                             ha->flags.init_done) {
2694                                 /* Handle port RSCN via asyncronous IOCBs */
2695                                 rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
2696                                     NULL, 0);
2697                                 if (rval2 == QLA_SUCCESS)
2698                                         continue;
2699                         }
2700                         mask = 0xffffff;
2701                         break;
2702                 case 1:
2703                         mask = 0xffff00;
2704                         break;
2705                 case 2:
2706                         mask = 0xff0000;
2707                         break;
2708                 default:
2709                         mask = 0x0;
2710                         d_id.b24 = 0;
2711                         ha->rscn_out_ptr = ha->rscn_in_ptr;
2712                         break;
2713                 }
2714
2715                 rval = QLA_SUCCESS;
2716
2717                 /* Abort any outstanding IO descriptors. */
2718                 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2719                         qla2x00_cancel_io_descriptors(ha);
2720
2721                 list_for_each_entry(fcport, &ha->fcports, list) {
2722                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2723                             (fcport->d_id.b24 & mask) != d_id.b24 ||
2724                             fcport->port_type == FCT_BROADCAST)
2725                                 continue;
2726
2727                         if (atomic_read(&fcport->state) == FCS_ONLINE) {
2728                                 if (format != 3 ||
2729                                     fcport->port_type != FCT_INITIATOR) {
2730                                         qla2x00_mark_device_lost(ha, fcport,
2731                                             0, 0);
2732                                 }
2733                         }
2734                         fcport->flags &= ~FCF_FARP_DONE;
2735                 }
2736         }
2737         return (rval);
2738 }
2739
2740 /*
2741  * qla2x00_fabric_dev_login
2742  *      Login fabric target device and update FC port database.
2743  *
2744  * Input:
2745  *      ha:             adapter state pointer.
2746  *      fcport:         port structure list pointer.
2747  *      next_loopid:    contains value of a new loop ID that can be used
2748  *                      by the next login attempt.
2749  *
2750  * Returns:
2751  *      qla2x00 local function return status code.
2752  *
2753  * Context:
2754  *      Kernel context.
2755  */
2756 static int
2757 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2758     uint16_t *next_loopid)
2759 {
2760         int     rval;
2761         int     retry;
2762         uint8_t opts;
2763
2764         rval = QLA_SUCCESS;
2765         retry = 0;
2766
2767         rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2768         if (rval == QLA_SUCCESS) {
2769                 /* Send an ADISC to tape devices.*/
2770                 opts = 0;
2771                 if (fcport->flags & FCF_TAPE_PRESENT)
2772                         opts |= BIT_1;
2773                 rval = qla2x00_get_port_database(ha, fcport, opts);
2774                 if (rval != QLA_SUCCESS) {
2775                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2776                             fcport->d_id.b.domain, fcport->d_id.b.area,
2777                             fcport->d_id.b.al_pa);
2778                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2779                 } else {
2780                         qla2x00_update_fcport(ha, fcport);
2781                 }
2782         }
2783
2784         return (rval);
2785 }
2786
2787 /*
2788  * qla2x00_fabric_login
2789  *      Issue fabric login command.
2790  *
2791  * Input:
2792  *      ha = adapter block pointer.
2793  *      device = pointer to FC device type structure.
2794  *
2795  * Returns:
2796  *      0 - Login successfully
2797  *      1 - Login failed
2798  *      2 - Initiator device
2799  *      3 - Fatal error
2800  */
2801 int
2802 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2803     uint16_t *next_loopid)
2804 {
2805         int     rval;
2806         int     retry;
2807         uint16_t tmp_loopid;
2808         uint16_t mb[MAILBOX_REGISTER_COUNT];
2809
2810         retry = 0;
2811         tmp_loopid = 0;
2812
2813         for (;;) {
2814                 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2815                     "for port %02x%02x%02x.\n",
2816                     ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2817                     fcport->d_id.b.area, fcport->d_id.b.al_pa));
2818
2819                 /* Login fcport on switch. */
2820                 ha->isp_ops.fabric_login(ha, fcport->loop_id,
2821                     fcport->d_id.b.domain, fcport->d_id.b.area,
2822                     fcport->d_id.b.al_pa, mb, BIT_0);
2823                 if (mb[0] == MBS_PORT_ID_USED) {
2824                         /*
2825                          * Device has another loop ID.  The firmware team
2826                          * recommends the driver perform an implicit login with
2827                          * the specified ID again. The ID we just used is save
2828                          * here so we return with an ID that can be tried by
2829                          * the next login.
2830                          */
2831                         retry++;
2832                         tmp_loopid = fcport->loop_id;
2833                         fcport->loop_id = mb[1];
2834
2835                         DEBUG(printk("Fabric Login: port in use - next "
2836                             "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2837                             fcport->loop_id, fcport->d_id.b.domain,
2838                             fcport->d_id.b.area, fcport->d_id.b.al_pa));
2839
2840                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2841                         /*
2842                          * Login succeeded.
2843                          */
2844                         if (retry) {
2845                                 /* A retry occurred before. */
2846                                 *next_loopid = tmp_loopid;
2847                         } else {
2848                                 /*
2849                                  * No retry occurred before. Just increment the
2850                                  * ID value for next login.
2851                                  */
2852                                 *next_loopid = (fcport->loop_id + 1);
2853                         }
2854
2855                         if (mb[1] & BIT_0) {
2856                                 fcport->port_type = FCT_INITIATOR;
2857                         } else {
2858                                 fcport->port_type = FCT_TARGET;
2859                                 if (mb[1] & BIT_1) {
2860                                         fcport->flags |= FCF_TAPE_PRESENT;
2861                                 }
2862                         }
2863
2864                         if (mb[10] & BIT_0)
2865                                 fcport->supported_classes |= FC_COS_CLASS2;
2866                         if (mb[10] & BIT_1)
2867                                 fcport->supported_classes |= FC_COS_CLASS3;
2868
2869                         rval = QLA_SUCCESS;
2870                         break;
2871                 } else if (mb[0] == MBS_LOOP_ID_USED) {
2872                         /*
2873                          * Loop ID already used, try next loop ID.
2874                          */
2875                         fcport->loop_id++;
2876                         rval = qla2x00_find_new_loop_id(ha, fcport);
2877                         if (rval != QLA_SUCCESS) {
2878                                 /* Ran out of loop IDs to use */
2879                                 break;
2880                         }
2881                 } else if (mb[0] == MBS_COMMAND_ERROR) {
2882                         /*
2883                          * Firmware possibly timed out during login. If NO
2884                          * retries are left to do then the device is declared
2885                          * dead.
2886                          */
2887                         *next_loopid = fcport->loop_id;
2888                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2889                             fcport->d_id.b.domain, fcport->d_id.b.area,
2890                             fcport->d_id.b.al_pa);
2891                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2892
2893                         rval = 1;
2894                         break;
2895                 } else {
2896                         /*
2897                          * unrecoverable / not handled error
2898                          */
2899                         DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
2900                             "loop_id=%x jiffies=%lx.\n",
2901                             __func__, ha->host_no, mb[0],
2902                             fcport->d_id.b.domain, fcport->d_id.b.area,
2903                             fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2904
2905                         *next_loopid = fcport->loop_id;
2906                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2907                             fcport->d_id.b.domain, fcport->d_id.b.area,
2908                             fcport->d_id.b.al_pa);
2909                         fcport->loop_id = FC_NO_LOOP_ID;
2910                         fcport->login_retry = 0;
2911
2912                         rval = 3;
2913                         break;
2914                 }
2915         }
2916
2917         return (rval);
2918 }
2919
2920 /*
2921  * qla2x00_local_device_login
2922  *      Issue local device login command.
2923  *
2924  * Input:
2925  *      ha = adapter block pointer.
2926  *      loop_id = loop id of device to login to.
2927  *
2928  * Returns (Where's the #define!!!!):
2929  *      0 - Login successfully
2930  *      1 - Login failed
2931  *      3 - Fatal error
2932  */
2933 int
2934 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
2935 {
2936         int             rval;
2937         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2938
2939         memset(mb, 0, sizeof(mb));
2940         rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
2941         if (rval == QLA_SUCCESS) {
2942                 /* Interrogate mailbox registers for any errors */
2943                 if (mb[0] == MBS_COMMAND_ERROR)
2944                         rval = 1;
2945                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2946                         /* device not in PCB table */
2947                         rval = 3;
2948         }
2949
2950         return (rval);
2951 }
2952
2953 /*
2954  *  qla2x00_loop_resync
2955  *      Resync with fibre channel devices.
2956  *
2957  * Input:
2958  *      ha = adapter block pointer.
2959  *
2960  * Returns:
2961  *      0 = success
2962  */
2963 int
2964 qla2x00_loop_resync(scsi_qla_host_t *ha)
2965 {
2966         int   rval;
2967         uint32_t wait_time;
2968
2969         rval = QLA_SUCCESS;
2970
2971         atomic_set(&ha->loop_state, LOOP_UPDATE);
2972         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2973         if (ha->flags.online) {
2974                 if (!(rval = qla2x00_fw_ready(ha))) {
2975                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
2976                         wait_time = 256;
2977                         do {
2978                                 atomic_set(&ha->loop_state, LOOP_UPDATE);
2979
2980                                 /* Issue a marker after FW becomes ready. */
2981                                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2982                                 ha->marker_needed = 0;
2983
2984                                 /* Remap devices on Loop. */
2985                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2986
2987                                 qla2x00_configure_loop(ha);
2988                                 wait_time--;
2989                         } while (!atomic_read(&ha->loop_down_timer) &&
2990                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2991                                 wait_time &&
2992                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2993                 }
2994         }
2995
2996         if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2997                 return (QLA_FUNCTION_FAILED);
2998         }
2999
3000         if (rval) {
3001                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3002         }
3003
3004         return (rval);
3005 }
3006
3007 void
3008 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3009 {
3010         int rescan_done;
3011         fc_port_t *fcport;
3012
3013         rescan_done = 0;
3014         list_for_each_entry(fcport, &ha->fcports, list) {
3015                 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3016                         continue;
3017
3018                 qla2x00_update_fcport(ha, fcport);
3019                 fcport->flags &= ~FCF_RESCAN_NEEDED;
3020
3021                 rescan_done = 1;
3022         }
3023         qla2x00_probe_for_all_luns(ha);
3024 }
3025
3026 void
3027 qla2x00_update_fcports(scsi_qla_host_t *ha)
3028 {
3029         fc_port_t *fcport;
3030
3031         /* Go with deferred removal of rport references. */
3032         list_for_each_entry(fcport, &ha->fcports, list)
3033                 if (fcport->drport)
3034                         qla2x00_rport_del(fcport);
3035 }
3036
3037 /*
3038 *  qla2x00_abort_isp
3039 *      Resets ISP and aborts all outstanding commands.
3040 *
3041 * Input:
3042 *      ha           = adapter block pointer.
3043 *
3044 * Returns:
3045 *      0 = success
3046 */
3047 int
3048 qla2x00_abort_isp(scsi_qla_host_t *ha)
3049 {
3050         unsigned long flags = 0;
3051         uint16_t       cnt;
3052         srb_t          *sp;
3053         uint8_t        status = 0;
3054
3055         if (ha->flags.online) {
3056                 ha->flags.online = 0;
3057                 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3058
3059                 qla_printk(KERN_INFO, ha,
3060                     "Performing ISP error recovery - ha= %p.\n", ha);
3061                 ha->isp_ops.reset_chip(ha);
3062
3063                 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3064                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3065                         atomic_set(&ha->loop_state, LOOP_DOWN);
3066                         qla2x00_mark_all_devices_lost(ha, 0);
3067                 } else {
3068                         if (!atomic_read(&ha->loop_down_timer))
3069                                 atomic_set(&ha->loop_down_timer,
3070                                     LOOP_DOWN_TIME);
3071                 }
3072
3073                 spin_lock_irqsave(&ha->hardware_lock, flags);
3074                 /* Requeue all commands in outstanding command list. */
3075                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3076                         sp = ha->outstanding_cmds[cnt];
3077                         if (sp) {
3078                                 ha->outstanding_cmds[cnt] = NULL;
3079                                 sp->flags = 0;
3080                                 sp->cmd->result = DID_RESET << 16;
3081                                 sp->cmd->host_scribble = (unsigned char *)NULL;
3082                                 qla2x00_sp_compl(ha, sp);
3083                         }
3084                 }
3085                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3086
3087                 ha->isp_ops.nvram_config(ha);
3088
3089                 if (!qla2x00_restart_isp(ha)) {
3090                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3091
3092                         if (!atomic_read(&ha->loop_down_timer)) {
3093                                 /*
3094                                  * Issue marker command only when we are going
3095                                  * to start the I/O .
3096                                  */
3097                                 ha->marker_needed = 1;
3098                         }
3099
3100                         ha->flags.online = 1;
3101
3102 #ifdef FC_TARGET_SUPPORT
3103                         /* Enable target response to SCSI bus. */
3104                         if (ha->flags.enable_target_mode)
3105                                 qla2x00_enable_lun(ha);
3106 #endif
3107
3108                         ha->isp_ops.enable_intrs(ha);
3109
3110                         ha->isp_abort_cnt = 0;
3111                         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3112                 } else {        /* failed the ISP abort */
3113                         ha->flags.online = 1;
3114                         if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3115                                 if (ha->isp_abort_cnt == 0) {
3116                                         qla_printk(KERN_WARNING, ha,
3117                                             "ISP error recovery failed - "
3118                                             "board disabled\n");
3119                                         /*
3120                                          * The next call disables the board
3121                                          * completely.
3122                                          */
3123                                         ha->isp_ops.reset_adapter(ha);
3124                                         ha->flags.online = 0;
3125                                         clear_bit(ISP_ABORT_RETRY,
3126                                             &ha->dpc_flags);
3127                                         status = 0;
3128                                 } else { /* schedule another ISP abort */
3129                                         ha->isp_abort_cnt--;
3130                                         DEBUG(printk("qla%ld: ISP abort - "
3131                                             "retry remaining %d\n",
3132                                             ha->host_no, ha->isp_abort_cnt);)
3133                                         status = 1;
3134                                 }
3135                         } else {
3136                                 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3137                                 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3138                                     "- retrying (%d) more times\n",
3139                                     ha->host_no, ha->isp_abort_cnt);)
3140                                 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3141                                 status = 1;
3142                         }
3143                 }
3144
3145         }
3146
3147         if (status) {
3148                 qla_printk(KERN_INFO, ha,
3149                         "qla2x00_abort_isp: **** FAILED ****\n");
3150         } else {
3151                 DEBUG(printk(KERN_INFO
3152                                 "qla2x00_abort_isp(%ld): exiting.\n",
3153                                 ha->host_no);)
3154         }
3155
3156         return(status);
3157 }
3158
3159 /*
3160 *  qla2x00_restart_isp
3161 *      restarts the ISP after a reset
3162 *
3163 * Input:
3164 *      ha = adapter block pointer.
3165 *
3166 * Returns:
3167 *      0 = success
3168 */
3169 static int
3170 qla2x00_restart_isp(scsi_qla_host_t *ha)
3171 {
3172         uint8_t         status = 0;
3173         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3174         unsigned long   flags = 0;
3175         uint32_t wait_time;
3176
3177         /* If firmware needs to be loaded */
3178         if (qla2x00_isp_firmware(ha)) {
3179                 ha->flags.online = 0;
3180                 if (!(status = ha->isp_ops.chip_diag(ha))) {
3181                         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3182                                 status = qla2x00_setup_chip(ha);
3183                                 goto done;
3184                         }
3185
3186                         spin_lock_irqsave(&ha->hardware_lock, flags);
3187
3188                         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3189                                 /*
3190                                  * Disable SRAM, Instruction RAM and GP RAM
3191                                  * parity.
3192                                  */
3193                                 WRT_REG_WORD(&reg->hccr,
3194                                     (HCCR_ENABLE_PARITY + 0x0));
3195                                 RD_REG_WORD(&reg->hccr);
3196                         }
3197
3198                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3199
3200                         status = qla2x00_setup_chip(ha);
3201
3202                         spin_lock_irqsave(&ha->hardware_lock, flags);
3203
3204                         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3205                                 /* Enable proper parity */
3206                                 if (IS_QLA2300(ha))
3207                                         /* SRAM parity */
3208                                         WRT_REG_WORD(&reg->hccr,
3209                                             (HCCR_ENABLE_PARITY + 0x1));
3210                                 else
3211                                         /*
3212                                          * SRAM, Instruction RAM and GP RAM
3213                                          * parity.
3214                                          */
3215                                         WRT_REG_WORD(&reg->hccr,
3216                                             (HCCR_ENABLE_PARITY + 0x7));
3217                                 RD_REG_WORD(&reg->hccr);
3218                         }
3219
3220                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3221                 }
3222         }
3223
3224  done:
3225         if (!status && !(status = qla2x00_init_rings(ha))) {
3226                 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3227                 if (!(status = qla2x00_fw_ready(ha))) {
3228                         DEBUG(printk("%s(): Start configure loop, "
3229                             "status = %d\n", __func__, status);)
3230
3231                         /* Issue a marker after FW becomes ready. */
3232                         qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3233
3234                         ha->flags.online = 1;
3235                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3236                         wait_time = 256;
3237                         do {
3238                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3239                                 qla2x00_configure_loop(ha);
3240                                 wait_time--;
3241                         } while (!atomic_read(&ha->loop_down_timer) &&
3242                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3243                                 wait_time &&
3244                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3245                 }
3246
3247                 /* if no cable then assume it's good */
3248                 if ((ha->device_flags & DFLG_NO_CABLE))
3249                         status = 0;
3250
3251                 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3252                                 __func__,
3253                                 status);)
3254         }
3255         return (status);
3256 }
3257
3258 /*
3259 * qla2x00_reset_adapter
3260 *      Reset adapter.
3261 *
3262 * Input:
3263 *      ha = adapter block pointer.
3264 */
3265 void
3266 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3267 {
3268         unsigned long flags = 0;
3269         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3270
3271         ha->flags.online = 0;
3272         ha->isp_ops.disable_intrs(ha);
3273
3274         spin_lock_irqsave(&ha->hardware_lock, flags);
3275         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3276         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3277         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3278         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3279         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3280 }
3281
3282 void
3283 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3284 {
3285         unsigned long flags = 0;
3286         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3287
3288         ha->flags.online = 0;
3289         ha->isp_ops.disable_intrs(ha);
3290
3291         spin_lock_irqsave(&ha->hardware_lock, flags);
3292         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3293         RD_REG_DWORD(&reg->hccr);
3294         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3295         RD_REG_DWORD(&reg->hccr);
3296         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3297 }
3298
3299 int
3300 qla24xx_nvram_config(scsi_qla_host_t *ha)
3301 {
3302         int   rval;
3303         struct init_cb_24xx *icb;
3304         struct nvram_24xx *nv;
3305         uint32_t *dptr;
3306         uint8_t  *dptr1, *dptr2;
3307         uint32_t chksum;
3308         uint16_t cnt;
3309
3310         rval = QLA_SUCCESS;
3311         icb = (struct init_cb_24xx *)ha->init_cb;
3312         nv = (struct nvram_24xx *)ha->request_ring;
3313
3314         /* Determine NVRAM starting address. */
3315         ha->nvram_size = sizeof(struct nvram_24xx);
3316         ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3317         ha->vpd_size = FA_NVRAM_VPD_SIZE;
3318         ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3319         if (PCI_FUNC(ha->pdev->devfn)) {
3320                 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3321                 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3322         }
3323
3324         /* Get NVRAM data and calculate checksum. */
3325         dptr = (uint32_t *)nv;
3326         ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3327             ha->nvram_size);
3328         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3329                 chksum += le32_to_cpu(*dptr++);
3330
3331         DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3332         DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3333             ha->nvram_size));
3334
3335         /* Bad NVRAM data, set defaults parameters. */
3336         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3337             || nv->id[3] != ' ' ||
3338             nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3339                 /* Reset NVRAM data. */
3340                 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3341                     "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3342                     le16_to_cpu(nv->nvram_version));
3343                 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3344                     "invalid -- WWPN) defaults.\n");
3345
3346                 /*
3347                  * Set default initialization control block.
3348                  */
3349                 memset(nv, 0, ha->nvram_size);
3350                 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3351                 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3352                 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3353                 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3354                 nv->exchange_count = __constant_cpu_to_le16(0);
3355                 nv->hard_address = __constant_cpu_to_le16(124);
3356                 nv->port_name[0] = 0x21;
3357                 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3358                 nv->port_name[2] = 0x00;
3359                 nv->port_name[3] = 0xe0;
3360                 nv->port_name[4] = 0x8b;
3361                 nv->port_name[5] = 0x1c;
3362                 nv->port_name[6] = 0x55;
3363                 nv->port_name[7] = 0x86;
3364                 nv->node_name[0] = 0x20;
3365                 nv->node_name[1] = 0x00;
3366                 nv->node_name[2] = 0x00;
3367                 nv->node_name[3] = 0xe0;
3368                 nv->node_name[4] = 0x8b;
3369                 nv->node_name[5] = 0x1c;
3370                 nv->node_name[6] = 0x55;
3371                 nv->node_name[7] = 0x86;
3372                 nv->login_retry_count = __constant_cpu_to_le16(8);
3373                 nv->link_down_timeout = __constant_cpu_to_le16(200);
3374                 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3375                 nv->login_timeout = __constant_cpu_to_le16(0);
3376                 nv->firmware_options_1 =
3377                     __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3378                 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3379                 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3380                 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3381                 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3382                 nv->efi_parameters = __constant_cpu_to_le32(0);
3383                 nv->reset_delay = 5;
3384                 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3385                 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3386                 nv->link_down_timeout = __constant_cpu_to_le16(30);
3387
3388                 rval = 1;
3389         }
3390
3391         /* Reset Initialization control block */
3392         memset(icb, 0, sizeof(struct init_cb_24xx));
3393
3394         /* Copy 1st segment. */
3395         dptr1 = (uint8_t *)icb;
3396         dptr2 = (uint8_t *)&nv->version;
3397         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3398         while (cnt--)
3399                 *dptr1++ = *dptr2++;
3400
3401         icb->login_retry_count = nv->login_retry_count;
3402         icb->link_down_timeout = nv->link_down_timeout;
3403
3404         /* Copy 2nd segment. */
3405         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3406         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3407         cnt = (uint8_t *)&icb->reserved_3 -
3408             (uint8_t *)&icb->interrupt_delay_timer;
3409         while (cnt--)
3410                 *dptr1++ = *dptr2++;
3411
3412         /*
3413          * Setup driver NVRAM options.
3414          */
3415         if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3416                 char *st, *en;
3417                 uint16_t index;
3418
3419                 strncpy(ha->model_number, nv->model_name,
3420                     sizeof(nv->model_name));
3421                 st = en = ha->model_number;
3422                 en += sizeof(nv->model_name) - 1;
3423                 while (en > st) {
3424                         if (*en != 0x20 && *en != 0x00)
3425                                 break;
3426                         *en-- = '\0';
3427                 }
3428
3429                 index = (ha->pdev->subsystem_device & 0xff);
3430                 if (index < QLA_MODEL_NAMES)
3431                         ha->model_desc = qla2x00_model_name[index * 2 + 1];
3432         } else
3433                 strcpy(ha->model_number, "QLA2462");
3434
3435         /* Prepare nodename */
3436         if ((icb->firmware_options_1 & BIT_14) == 0) {
3437                 /*
3438                  * Firmware will apply the following mask if the nodename was
3439                  * not provided.
3440                  */
3441                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3442                 icb->node_name[0] &= 0xF0;
3443         }
3444
3445         /* Set host adapter parameters. */
3446         ha->flags.disable_risc_code_load = 0;
3447         ha->flags.enable_lip_reset = 1;
3448         ha->flags.enable_lip_full_login = 1;
3449         ha->flags.enable_target_reset = 1;
3450         ha->flags.enable_led_scheme = 0;
3451
3452         ha->operating_mode =
3453             (icb->firmware_options_2 & (BIT_6 | BIT_5 | BIT_4)) >> 4;
3454
3455         memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3456             sizeof(ha->fw_seriallink_options24));
3457
3458         /* save HBA serial number */
3459         ha->serial0 = icb->port_name[5];
3460         ha->serial1 = icb->port_name[6];
3461         ha->serial2 = icb->port_name[7];
3462         ha->node_name = icb->node_name;
3463         ha->port_name = icb->port_name;
3464
3465         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3466
3467         ha->retry_count = le16_to_cpu(nv->login_retry_count);
3468
3469         /* Set minimum login_timeout to 4 seconds. */
3470         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3471                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3472         if (le16_to_cpu(nv->login_timeout) < 4)
3473                 nv->login_timeout = __constant_cpu_to_le16(4);
3474         ha->login_timeout = le16_to_cpu(nv->login_timeout);
3475         icb->login_timeout = cpu_to_le16(nv->login_timeout);
3476
3477         /* Set minimum RATOV to 200 tenths of a second. */
3478         ha->r_a_tov = 200;
3479
3480         ha->loop_reset_delay = nv->reset_delay;
3481
3482         /* Link Down Timeout = 0:
3483          *
3484          *      When Port Down timer expires we will start returning
3485          *      I/O's to OS with "DID_NO_CONNECT".
3486          *
3487          * Link Down Timeout != 0:
3488          *
3489          *       The driver waits for the link to come up after link down
3490          *       before returning I/Os to OS with "DID_NO_CONNECT".
3491          */
3492         if (le16_to_cpu(nv->link_down_timeout) == 0) {
3493                 ha->loop_down_abort_time =
3494                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3495         } else {
3496                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3497                 ha->loop_down_abort_time =
3498                     (LOOP_DOWN_TIME - ha->link_down_timeout);
3499         }
3500
3501         /* Need enough time to try and get the port back. */
3502         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3503         if (qlport_down_retry)
3504                 ha->port_down_retry_count = qlport_down_retry;
3505
3506         /* Set login_retry_count */
3507         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
3508         if (ha->port_down_retry_count ==
3509             le16_to_cpu(nv->port_down_retry_count) &&
3510             ha->port_down_retry_count > 3)
3511                 ha->login_retry_count = ha->port_down_retry_count;
3512         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3513                 ha->login_retry_count = ha->port_down_retry_count;
3514         if (ql2xloginretrycount)
3515                 ha->login_retry_count = ql2xloginretrycount;
3516
3517         /* Enable ZIO. */
3518         if (!ha->flags.init_done) {
3519                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3520                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3521                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3522                     le16_to_cpu(icb->interrupt_delay_timer): 2;
3523         }
3524         icb->firmware_options_2 &= __constant_cpu_to_le32(
3525             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3526         ha->flags.process_response_queue = 0;
3527         if (ha->zio_mode != QLA_ZIO_DISABLED) {
3528                 ha->zio_mode = QLA_ZIO_MODE_6;
3529
3530                 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3531                     "(%d us).\n", ha->host_no, ha->zio_mode,
3532                     ha->zio_timer * 100));
3533                 qla_printk(KERN_INFO, ha,
3534                     "ZIO mode %d enabled; timer delay (%d us).\n",
3535                     ha->zio_mode, ha->zio_timer * 100);
3536
3537                 icb->firmware_options_2 |= cpu_to_le32(
3538                     (uint32_t)ha->zio_mode);
3539                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3540                 ha->flags.process_response_queue = 1;
3541         }
3542
3543         if (rval) {
3544                 DEBUG2_3(printk(KERN_WARNING
3545                     "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3546         }
3547         return (rval);
3548 }
3549
3550 #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
3551
3552 int
3553 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3554 {
3555         int     rval, num, i;
3556         uint32_t cnt;
3557         uint16_t *risc_code;
3558         uint32_t risc_addr, risc_size;
3559         uint16_t *req_ring;
3560         struct qla_fw_info *fw_iter;
3561
3562         rval = QLA_SUCCESS;
3563
3564         /* Load firmware sequences */
3565         fw_iter = ha->brd_info->fw_info;
3566         *srisc_addr = *ha->brd_info->fw_info->fwstart;
3567         while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
3568                 risc_code = fw_iter->fwcode;
3569                 risc_size = *fw_iter->fwlen;
3570                 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL)