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