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