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