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