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