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