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