]> err.no Git - linux-2.6/blob - drivers/scsi/qla2xxx/qla_init.c
b1b20f43d72270ac84d1041b21079ced7b2a3afb
[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         /* Use alternate WWN? */
1536         if (nv->host_p[1] & BIT_7) {
1537                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1538                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1539         }
1540
1541         /* Prepare nodename */
1542         if ((icb->firmware_options[1] & BIT_6) == 0) {
1543                 /*
1544                  * Firmware will apply the following mask if the nodename was
1545                  * not provided.
1546                  */
1547                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1548                 icb->node_name[0] &= 0xF0;
1549         }
1550
1551         /*
1552          * Set host adapter parameters.
1553          */
1554         ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1555         /* Always load RISC code on non ISP2[12]00 chips. */
1556         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1557                 ha->flags.disable_risc_code_load = 0;
1558         ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1559         ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1560         ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1561         ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1562
1563         ha->operating_mode =
1564             (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1565
1566         memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1567             sizeof(ha->fw_seriallink_options));
1568
1569         /* save HBA serial number */
1570         ha->serial0 = icb->port_name[5];
1571         ha->serial1 = icb->port_name[6];
1572         ha->serial2 = icb->port_name[7];
1573         ha->node_name = icb->node_name;
1574         ha->port_name = icb->port_name;
1575
1576         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1577
1578         ha->retry_count = nv->retry_count;
1579
1580         /* Set minimum login_timeout to 4 seconds. */
1581         if (nv->login_timeout < ql2xlogintimeout)
1582                 nv->login_timeout = ql2xlogintimeout;
1583         if (nv->login_timeout < 4)
1584                 nv->login_timeout = 4;
1585         ha->login_timeout = nv->login_timeout;
1586         icb->login_timeout = nv->login_timeout;
1587
1588         /* Set minimum RATOV to 200 tenths of a second. */
1589         ha->r_a_tov = 200;
1590
1591         ha->loop_reset_delay = nv->reset_delay;
1592
1593         /* Link Down Timeout = 0:
1594          *
1595          *      When Port Down timer expires we will start returning
1596          *      I/O's to OS with "DID_NO_CONNECT".
1597          *
1598          * Link Down Timeout != 0:
1599          *
1600          *       The driver waits for the link to come up after link down
1601          *       before returning I/Os to OS with "DID_NO_CONNECT".
1602          */
1603         if (nv->link_down_timeout == 0) {
1604                 ha->loop_down_abort_time =
1605                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1606         } else {
1607                 ha->link_down_timeout =  nv->link_down_timeout;
1608                 ha->loop_down_abort_time =
1609                     (LOOP_DOWN_TIME - ha->link_down_timeout);
1610         }
1611
1612         /*
1613          * Need enough time to try and get the port back.
1614          */
1615         ha->port_down_retry_count = nv->port_down_retry_count;
1616         if (qlport_down_retry)
1617                 ha->port_down_retry_count = qlport_down_retry;
1618         /* Set login_retry_count */
1619         ha->login_retry_count  = nv->retry_count;
1620         if (ha->port_down_retry_count == nv->port_down_retry_count &&
1621             ha->port_down_retry_count > 3)
1622                 ha->login_retry_count = ha->port_down_retry_count;
1623         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1624                 ha->login_retry_count = ha->port_down_retry_count;
1625         if (ql2xloginretrycount)
1626                 ha->login_retry_count = ql2xloginretrycount;
1627
1628         icb->lun_enables = __constant_cpu_to_le16(0);
1629         icb->command_resource_count = 0;
1630         icb->immediate_notify_resource_count = 0;
1631         icb->timeout = __constant_cpu_to_le16(0);
1632
1633         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1634                 /* Enable RIO */
1635                 icb->firmware_options[0] &= ~BIT_3;
1636                 icb->add_firmware_options[0] &=
1637                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1638                 icb->add_firmware_options[0] |= BIT_2;
1639                 icb->response_accumulation_timer = 3;
1640                 icb->interrupt_delay_timer = 5;
1641
1642                 ha->flags.process_response_queue = 1;
1643         } else {
1644                 /* Enable ZIO. */
1645                 if (!ha->flags.init_done) {
1646                         ha->zio_mode = icb->add_firmware_options[0] &
1647                             (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1648                         ha->zio_timer = icb->interrupt_delay_timer ?
1649                             icb->interrupt_delay_timer: 2;
1650                 }
1651                 icb->add_firmware_options[0] &=
1652                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1653                 ha->flags.process_response_queue = 0;
1654                 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1655                         ha->zio_mode = QLA_ZIO_MODE_6;
1656
1657                         DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1658                             "delay (%d us).\n", ha->host_no, ha->zio_mode,
1659                             ha->zio_timer * 100));
1660                         qla_printk(KERN_INFO, ha,
1661                             "ZIO mode %d enabled; timer delay (%d us).\n",
1662                             ha->zio_mode, ha->zio_timer * 100);
1663
1664                         icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1665                         icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1666                         ha->flags.process_response_queue = 1;
1667                 }
1668         }
1669
1670         if (rval) {
1671                 DEBUG2_3(printk(KERN_WARNING
1672                     "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1673         }
1674         return (rval);
1675 }
1676
1677 static void
1678 qla2x00_rport_add(void *data)
1679 {
1680         fc_port_t *fcport = data;
1681
1682         qla2x00_reg_remote_port(fcport->ha, fcport);
1683 }
1684
1685 static void
1686 qla2x00_rport_del(void *data)
1687 {
1688         fc_port_t *fcport = data;
1689         struct fc_rport *rport;
1690         unsigned long flags;
1691
1692         spin_lock_irqsave(&fcport->rport_lock, flags);
1693         rport = fcport->drport;
1694         fcport->drport = NULL;
1695         spin_unlock_irqrestore(&fcport->rport_lock, flags);
1696         if (rport)
1697                 fc_remote_port_delete(rport);
1698
1699 }
1700
1701 /**
1702  * qla2x00_alloc_fcport() - Allocate a generic fcport.
1703  * @ha: HA context
1704  * @flags: allocation flags
1705  *
1706  * Returns a pointer to the allocated fcport, or NULL, if none available.
1707  */
1708 fc_port_t *
1709 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1710 {
1711         fc_port_t *fcport;
1712
1713         fcport = kmalloc(sizeof(fc_port_t), flags);
1714         if (fcport == NULL)
1715                 return (fcport);
1716
1717         /* Setup fcport template structure. */
1718         memset(fcport, 0, sizeof (fc_port_t));
1719         fcport->ha = ha;
1720         fcport->port_type = FCT_UNKNOWN;
1721         fcport->loop_id = FC_NO_LOOP_ID;
1722         fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
1723         atomic_set(&fcport->state, FCS_UNCONFIGURED);
1724         fcport->flags = FCF_RLC_SUPPORT;
1725         fcport->supported_classes = FC_COS_UNSPECIFIED;
1726         spin_lock_init(&fcport->rport_lock);
1727         INIT_WORK(&fcport->rport_add_work, qla2x00_rport_add, fcport);
1728         INIT_WORK(&fcport->rport_del_work, qla2x00_rport_del, fcport);
1729
1730         return (fcport);
1731 }
1732
1733 /*
1734  * qla2x00_configure_loop
1735  *      Updates Fibre Channel Device Database with what is actually on loop.
1736  *
1737  * Input:
1738  *      ha                = adapter block pointer.
1739  *
1740  * Returns:
1741  *      0 = success.
1742  *      1 = error.
1743  *      2 = database was full and device was not configured.
1744  */
1745 static int
1746 qla2x00_configure_loop(scsi_qla_host_t *ha)
1747 {
1748         int  rval;
1749         unsigned long flags, save_flags;
1750
1751         rval = QLA_SUCCESS;
1752
1753         /* Get Initiator ID */
1754         if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1755                 rval = qla2x00_configure_hba(ha);
1756                 if (rval != QLA_SUCCESS) {
1757                         DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1758                             ha->host_no));
1759                         return (rval);
1760                 }
1761         }
1762
1763         save_flags = flags = ha->dpc_flags;
1764         DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1765             ha->host_no, flags));
1766
1767         /*
1768          * If we have both an RSCN and PORT UPDATE pending then handle them
1769          * both at the same time.
1770          */
1771         clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1772         clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1773
1774         /* Determine what we need to do */
1775         if (ha->current_topology == ISP_CFG_FL &&
1776             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1777
1778                 ha->flags.rscn_queue_overflow = 1;
1779                 set_bit(RSCN_UPDATE, &flags);
1780
1781         } else if (ha->current_topology == ISP_CFG_F &&
1782             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1783
1784                 ha->flags.rscn_queue_overflow = 1;
1785                 set_bit(RSCN_UPDATE, &flags);
1786                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1787
1788         } else if (!ha->flags.online ||
1789             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1790
1791                 ha->flags.rscn_queue_overflow = 1;
1792                 set_bit(RSCN_UPDATE, &flags);
1793                 set_bit(LOCAL_LOOP_UPDATE, &flags);
1794         }
1795
1796         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1797                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1798                         rval = QLA_FUNCTION_FAILED;
1799                 } else {
1800                         rval = qla2x00_configure_local_loop(ha);
1801                 }
1802         }
1803
1804         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1805                 if (LOOP_TRANSITION(ha)) {
1806                         rval = QLA_FUNCTION_FAILED;
1807                 } else {
1808                         rval = qla2x00_configure_fabric(ha);
1809                 }
1810         }
1811
1812         if (rval == QLA_SUCCESS) {
1813                 if (atomic_read(&ha->loop_down_timer) ||
1814                     test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1815                         rval = QLA_FUNCTION_FAILED;
1816                 } else {
1817                         atomic_set(&ha->loop_state, LOOP_READY);
1818
1819                         DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1820                 }
1821         }
1822
1823         if (rval) {
1824                 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1825                     __func__, ha->host_no));
1826         } else {
1827                 DEBUG3(printk("%s: exiting normally\n", __func__));
1828         }
1829
1830         /* Restore state if a resync event occured during processing */
1831         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1832                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1833                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1834                 if (test_bit(RSCN_UPDATE, &save_flags))
1835                         set_bit(RSCN_UPDATE, &ha->dpc_flags);
1836         }
1837
1838         return (rval);
1839 }
1840
1841
1842
1843 /*
1844  * qla2x00_configure_local_loop
1845  *      Updates Fibre Channel Device Database with local loop devices.
1846  *
1847  * Input:
1848  *      ha = adapter block pointer.
1849  *
1850  * Returns:
1851  *      0 = success.
1852  */
1853 static int
1854 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1855 {
1856         int             rval, rval2;
1857         int             found_devs;
1858         int             found;
1859         fc_port_t       *fcport, *new_fcport;
1860
1861         uint16_t        index;
1862         uint16_t        entries;
1863         char            *id_iter;
1864         uint16_t        loop_id;
1865         uint8_t         domain, area, al_pa;
1866
1867         found_devs = 0;
1868         new_fcport = NULL;
1869         entries = MAX_FIBRE_DEVICES;
1870
1871         DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1872         DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1873
1874         /* Get list of logged in devices. */
1875         memset(ha->gid_list, 0, GID_LIST_SIZE);
1876         rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1877             &entries);
1878         if (rval != QLA_SUCCESS)
1879                 goto cleanup_allocation;
1880
1881         DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1882             ha->host_no, entries));
1883         DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1884             entries * sizeof(struct gid_list_info)));
1885
1886         /* Allocate temporary fcport for any new fcports discovered. */
1887         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1888         if (new_fcport == NULL) {
1889                 rval = QLA_MEMORY_ALLOC_FAILED;
1890                 goto cleanup_allocation;
1891         }
1892         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1893
1894         /*
1895          * Mark local devices that were present with FCF_DEVICE_LOST for now.
1896          */
1897         list_for_each_entry(fcport, &ha->fcports, list) {
1898                 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1899                     fcport->port_type != FCT_BROADCAST &&
1900                     (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1901
1902                         DEBUG(printk("scsi(%ld): Marking port lost, "
1903                             "loop_id=0x%04x\n",
1904                             ha->host_no, fcport->loop_id));
1905
1906                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
1907                         fcport->flags &= ~FCF_FARP_DONE;
1908                 }
1909         }
1910
1911         /* Add devices to port list. */
1912         id_iter = (char *)ha->gid_list;
1913         for (index = 0; index < entries; index++) {
1914                 domain = ((struct gid_list_info *)id_iter)->domain;
1915                 area = ((struct gid_list_info *)id_iter)->area;
1916                 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
1917                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1918                         loop_id = (uint16_t)
1919                             ((struct gid_list_info *)id_iter)->loop_id_2100;
1920                 else
1921                         loop_id = le16_to_cpu(
1922                             ((struct gid_list_info *)id_iter)->loop_id);
1923                 id_iter += ha->gid_list_info_size;
1924
1925                 /* Bypass reserved domain fields. */
1926                 if ((domain & 0xf0) == 0xf0)
1927                         continue;
1928
1929                 /* Bypass if not same domain and area of adapter. */
1930                 if (area && domain &&
1931                     (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
1932                         continue;
1933
1934                 /* Bypass invalid local loop ID. */
1935                 if (loop_id > LAST_LOCAL_LOOP_ID)
1936                         continue;
1937
1938                 /* Fill in member data. */
1939                 new_fcport->d_id.b.domain = domain;
1940                 new_fcport->d_id.b.area = area;
1941                 new_fcport->d_id.b.al_pa = al_pa;
1942                 new_fcport->loop_id = loop_id;
1943                 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1944                 if (rval2 != QLA_SUCCESS) {
1945                         DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1946                             "information -- get_port_database=%x, "
1947                             "loop_id=0x%04x\n",
1948                             ha->host_no, rval2, new_fcport->loop_id));
1949                         DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
1950                             ha->host_no));
1951                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
1952                         continue;
1953                 }
1954
1955                 /* Check for matching device in port list. */
1956                 found = 0;
1957                 fcport = NULL;
1958                 list_for_each_entry(fcport, &ha->fcports, list) {
1959                         if (memcmp(new_fcport->port_name, fcport->port_name,
1960                             WWN_SIZE))
1961                                 continue;
1962
1963                         fcport->flags &= ~(FCF_FABRIC_DEVICE |
1964                             FCF_PERSISTENT_BOUND);
1965                         fcport->loop_id = new_fcport->loop_id;
1966                         fcport->port_type = new_fcport->port_type;
1967                         fcport->d_id.b24 = new_fcport->d_id.b24;
1968                         memcpy(fcport->node_name, new_fcport->node_name,
1969                             WWN_SIZE);
1970
1971                         found++;
1972                         break;
1973                 }
1974
1975                 if (!found) {
1976                         /* New device, add to fcports list. */
1977                         new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
1978                         list_add_tail(&new_fcport->list, &ha->fcports);
1979
1980                         /* Allocate a new replacement fcport. */
1981                         fcport = new_fcport;
1982                         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1983                         if (new_fcport == NULL) {
1984                                 rval = QLA_MEMORY_ALLOC_FAILED;
1985                                 goto cleanup_allocation;
1986                         }
1987                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1988                 }
1989
1990                 qla2x00_update_fcport(ha, fcport);
1991
1992                 found_devs++;
1993         }
1994
1995 cleanup_allocation:
1996         kfree(new_fcport);
1997
1998         if (rval != QLA_SUCCESS) {
1999                 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2000                     "rval=%x\n", ha->host_no, rval));
2001         }
2002
2003         if (found_devs) {
2004                 ha->device_flags |= DFLG_LOCAL_DEVICES;
2005                 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2006         }
2007
2008         return (rval);
2009 }
2010
2011 static void
2012 qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
2013 {
2014         fc_port_t       *fcport;
2015
2016         qla2x00_mark_all_devices_lost(ha, 0);
2017         list_for_each_entry(fcport, &ha->fcports, list) {
2018                 if (fcport->port_type != FCT_TARGET)
2019                         continue;
2020
2021                 qla2x00_update_fcport(ha, fcport);
2022         }
2023 }
2024
2025 /*
2026  * qla2x00_update_fcport
2027  *      Updates device on list.
2028  *
2029  * Input:
2030  *      ha = adapter block pointer.
2031  *      fcport = port structure pointer.
2032  *
2033  * Return:
2034  *      0  - Success
2035  *  BIT_0 - error
2036  *
2037  * Context:
2038  *      Kernel context.
2039  */
2040 void
2041 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2042 {
2043         fcport->ha = ha;
2044         fcport->login_retry = 0;
2045         fcport->port_login_retry_count = ha->port_down_retry_count *
2046             PORT_RETRY_TIME;
2047         atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2048             PORT_RETRY_TIME);
2049         fcport->flags &= ~FCF_LOGIN_NEEDED;
2050
2051         if (fcport->port_type == FCT_INITIATOR ||
2052             fcport->port_type == FCT_BROADCAST)
2053                 fcport->device_type = TYPE_PROCESSOR;
2054
2055         atomic_set(&fcport->state, FCS_ONLINE);
2056
2057         if (ha->flags.init_done)
2058                 qla2x00_reg_remote_port(ha, fcport);
2059 }
2060
2061 void
2062 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2063 {
2064         struct fc_rport_identifiers rport_ids;
2065         struct fc_rport *rport;
2066         unsigned long flags;
2067
2068         if (fcport->drport)
2069                 qla2x00_rport_del(fcport);
2070         if (fcport->rport)
2071                 return;
2072
2073         rport_ids.node_name = wwn_to_u64(fcport->node_name);
2074         rport_ids.port_name = wwn_to_u64(fcport->port_name);
2075         rport_ids.port_id = fcport->d_id.b.domain << 16 |
2076             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2077         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2078         rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2079         if (!rport) {
2080                 qla_printk(KERN_WARNING, ha,
2081                     "Unable to allocate fc remote port!\n");
2082                 return;
2083         }
2084         spin_lock_irqsave(&fcport->rport_lock, flags);
2085         fcport->rport = rport;
2086         *((fc_port_t **)rport->dd_data) = fcport;
2087         spin_unlock_irqrestore(&fcport->rport_lock, flags);
2088
2089         rport->supported_classes = fcport->supported_classes;
2090
2091         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2092         if (fcport->port_type == FCT_INITIATOR)
2093                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2094         if (fcport->port_type == FCT_TARGET)
2095                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2096         fc_remote_port_rolechg(rport, rport_ids.roles);
2097
2098         if (rport->scsi_target_id != -1 &&
2099             rport->scsi_target_id < ha->host->max_id)
2100                 fcport->os_target_id = rport->scsi_target_id;
2101 }
2102
2103 /*
2104  * qla2x00_configure_fabric
2105  *      Setup SNS devices with loop ID's.
2106  *
2107  * Input:
2108  *      ha = adapter block pointer.
2109  *
2110  * Returns:
2111  *      0 = success.
2112  *      BIT_0 = error
2113  */
2114 static int
2115 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2116 {
2117         int     rval, rval2;
2118         fc_port_t       *fcport, *fcptemp;
2119         uint16_t        next_loopid;
2120         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2121         uint16_t        loop_id;
2122         LIST_HEAD(new_fcports);
2123
2124         /* If FL port exists, then SNS is present */
2125         if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2126                 loop_id = NPH_F_PORT;
2127         else
2128                 loop_id = SNS_FL_PORT;
2129         rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
2130         if (rval != QLA_SUCCESS) {
2131                 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2132                     "Port\n", ha->host_no));
2133
2134                 ha->device_flags &= ~SWITCH_FOUND;
2135                 return (QLA_SUCCESS);
2136         }
2137
2138         /* Mark devices that need re-synchronization. */
2139         rval2 = qla2x00_device_resync(ha);
2140         if (rval2 == QLA_RSCNS_HANDLED) {
2141                 /* No point doing the scan, just continue. */
2142                 return (QLA_SUCCESS);
2143         }
2144         do {
2145                 /* FDMI support. */
2146                 if (ql2xfdmienable &&
2147                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2148                         qla2x00_fdmi_register(ha);
2149
2150                 /* Ensure we are logged into the SNS. */
2151                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
2152                         loop_id = NPH_SNS;
2153                 else
2154                         loop_id = SIMPLE_NAME_SERVER;
2155                 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
2156                     0xfc, mb, BIT_1 | BIT_0);
2157                 if (mb[0] != MBS_COMMAND_COMPLETE) {
2158                         DEBUG2(qla_printk(KERN_INFO, ha,
2159                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2160                             "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2161                             mb[0], mb[1], mb[2], mb[6], mb[7]));
2162                         return (QLA_SUCCESS);
2163                 }
2164
2165                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2166                         if (qla2x00_rft_id(ha)) {
2167                                 /* EMPTY */
2168                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2169                                     "TYPE failed.\n", ha->host_no));
2170                         }
2171                         if (qla2x00_rff_id(ha)) {
2172                                 /* EMPTY */
2173                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2174                                     "Features failed.\n", ha->host_no));
2175                         }
2176                         if (qla2x00_rnn_id(ha)) {
2177                                 /* EMPTY */
2178                                 DEBUG2(printk("scsi(%ld): Register Node Name "
2179                                     "failed.\n", ha->host_no));
2180                         } else if (qla2x00_rsnn_nn(ha)) {
2181                                 /* EMPTY */
2182                                 DEBUG2(printk("scsi(%ld): Register Symbolic "
2183                                     "Node Name failed.\n", ha->host_no));
2184                         }
2185                 }
2186
2187                 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2188                 if (rval != QLA_SUCCESS)
2189                         break;
2190
2191                 /*
2192                  * Logout all previous fabric devices marked lost, except
2193                  * tape devices.
2194                  */
2195                 list_for_each_entry(fcport, &ha->fcports, list) {
2196                         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2197                                 break;
2198
2199                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2200                                 continue;
2201
2202                         if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2203                                 qla2x00_mark_device_lost(ha, fcport,
2204                                     ql2xplogiabsentdevice, 0);
2205                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
2206                                     (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2207                                     fcport->port_type != FCT_INITIATOR &&
2208                                     fcport->port_type != FCT_BROADCAST) {
2209                                         ha->isp_ops.fabric_logout(ha,
2210                                             fcport->loop_id,
2211                                             fcport->d_id.b.domain,
2212                                             fcport->d_id.b.area,
2213                                             fcport->d_id.b.al_pa);
2214                                         fcport->loop_id = FC_NO_LOOP_ID;
2215                                 }
2216                         }
2217                 }
2218
2219                 /* Starting free loop ID. */
2220                 next_loopid = ha->min_external_loopid;
2221
2222                 /*
2223                  * Scan through our port list and login entries that need to be
2224                  * logged in.
2225                  */
2226                 list_for_each_entry(fcport, &ha->fcports, list) {
2227                         if (atomic_read(&ha->loop_down_timer) ||
2228                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2229                                 break;
2230
2231                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2232                             (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2233                                 continue;
2234
2235                         if (fcport->loop_id == FC_NO_LOOP_ID) {
2236                                 fcport->loop_id = next_loopid;
2237                                 rval = qla2x00_find_new_loop_id(ha, fcport);
2238                                 if (rval != QLA_SUCCESS) {
2239                                         /* Ran out of IDs to use */
2240                                         break;
2241                                 }
2242                         }
2243                         /* Login and update database */
2244                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2245                 }
2246
2247                 /* Exit if out of loop IDs. */
2248                 if (rval != QLA_SUCCESS) {
2249                         break;
2250                 }
2251
2252                 /*
2253                  * Login and add the new devices to our port list.
2254                  */
2255                 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2256                         if (atomic_read(&ha->loop_down_timer) ||
2257                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2258                                 break;
2259
2260                         /* Find a new loop ID to use. */
2261                         fcport->loop_id = next_loopid;
2262                         rval = qla2x00_find_new_loop_id(ha, fcport);
2263                         if (rval != QLA_SUCCESS) {
2264                                 /* Ran out of IDs to use */
2265                                 break;
2266                         }
2267
2268                         /* Remove device from the new list and add it to DB */
2269                         list_del(&fcport->list);
2270                         list_add_tail(&fcport->list, &ha->fcports);
2271
2272                         /* Login and update database */
2273                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2274                 }
2275         } while (0);
2276
2277         /* Free all new device structures not processed. */
2278         list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2279                 list_del(&fcport->list);
2280                 kfree(fcport);
2281         }
2282
2283         if (rval) {
2284                 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2285                     "rval=%d\n", ha->host_no, rval));
2286         }
2287
2288         return (rval);
2289 }
2290
2291
2292 /*
2293  * qla2x00_find_all_fabric_devs
2294  *
2295  * Input:
2296  *      ha = adapter block pointer.
2297  *      dev = database device entry pointer.
2298  *
2299  * Returns:
2300  *      0 = success.
2301  *
2302  * Context:
2303  *      Kernel context.
2304  */
2305 static int
2306 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2307 {
2308         int             rval;
2309         uint16_t        loop_id;
2310         fc_port_t       *fcport, *new_fcport, *fcptemp;
2311         int             found;
2312
2313         sw_info_t       *swl;
2314         int             swl_idx;
2315         int             first_dev, last_dev;
2316         port_id_t       wrap, nxt_d_id;
2317
2318         rval = QLA_SUCCESS;
2319
2320         /* Try GID_PT to get device list, else GAN. */
2321         swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2322         if (swl == NULL) {
2323                 /*EMPTY*/
2324                 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2325                     "on GA_NXT\n", ha->host_no));
2326         } else {
2327                 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2328                 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2329                         kfree(swl);
2330                         swl = NULL;
2331                 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2332                         kfree(swl);
2333                         swl = NULL;
2334                 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2335                         kfree(swl);
2336                         swl = NULL;
2337                 }
2338         }
2339         swl_idx = 0;
2340
2341         /* Allocate temporary fcport for any new fcports discovered. */
2342         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2343         if (new_fcport == NULL) {
2344                 kfree(swl);
2345                 return (QLA_MEMORY_ALLOC_FAILED);
2346         }
2347         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2348
2349         /* Set start port ID scan at adapter ID. */
2350         first_dev = 1;
2351         last_dev = 0;
2352
2353         /* Starting free loop ID. */
2354         loop_id = ha->min_external_loopid;
2355         for (; loop_id <= ha->last_loop_id; loop_id++) {
2356                 if (qla2x00_is_reserved_id(ha, loop_id))
2357                         continue;
2358
2359                 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2360                         break;
2361
2362                 if (swl != NULL) {
2363                         if (last_dev) {
2364                                 wrap.b24 = new_fcport->d_id.b24;
2365                         } else {
2366                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2367                                 memcpy(new_fcport->node_name,
2368                                     swl[swl_idx].node_name, WWN_SIZE);
2369                                 memcpy(new_fcport->port_name,
2370                                     swl[swl_idx].port_name, WWN_SIZE);
2371
2372                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2373                                         last_dev = 1;
2374                                 }
2375                                 swl_idx++;
2376                         }
2377                 } else {
2378                         /* Send GA_NXT to the switch */
2379                         rval = qla2x00_ga_nxt(ha, new_fcport);
2380                         if (rval != QLA_SUCCESS) {
2381                                 qla_printk(KERN_WARNING, ha,
2382                                     "SNS scan failed -- assuming zero-entry "
2383                                     "result...\n");
2384                                 list_for_each_entry_safe(fcport, fcptemp,
2385                                     new_fcports, list) {
2386                                         list_del(&fcport->list);
2387                                         kfree(fcport);
2388                                 }
2389                                 rval = QLA_SUCCESS;
2390                                 break;
2391                         }
2392                 }
2393
2394                 /* If wrap on switch device list, exit. */
2395                 if (first_dev) {
2396                         wrap.b24 = new_fcport->d_id.b24;
2397                         first_dev = 0;
2398                 } else if (new_fcport->d_id.b24 == wrap.b24) {
2399                         DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2400                             ha->host_no, new_fcport->d_id.b.domain,
2401                             new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2402                         break;
2403                 }
2404
2405                 /* Bypass if host adapter. */
2406                 if (new_fcport->d_id.b24 == ha->d_id.b24)
2407                         continue;
2408
2409                 /* Bypass if same domain and area of adapter. */
2410                 if (((new_fcport->d_id.b24 & 0xffff00) ==
2411                     (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2412                         ISP_CFG_FL)
2413                             continue;
2414
2415                 /* Bypass reserved domain fields. */
2416                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2417                         continue;
2418
2419                 /* Locate matching device in database. */
2420                 found = 0;
2421                 list_for_each_entry(fcport, &ha->fcports, list) {
2422                         if (memcmp(new_fcport->port_name, fcport->port_name,
2423                             WWN_SIZE))
2424                                 continue;
2425
2426                         found++;
2427
2428                         /*
2429                          * If address the same and state FCS_ONLINE, nothing
2430                          * changed.
2431                          */
2432                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2433                             atomic_read(&fcport->state) == FCS_ONLINE) {
2434                                 break;
2435                         }
2436
2437                         /*
2438                          * If device was not a fabric device before.
2439                          */
2440                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2441                                 fcport->d_id.b24 = new_fcport->d_id.b24;
2442                                 fcport->loop_id = FC_NO_LOOP_ID;
2443                                 fcport->flags |= (FCF_FABRIC_DEVICE |
2444                                     FCF_LOGIN_NEEDED);
2445                                 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2446                                 break;
2447                         }
2448
2449                         /*
2450                          * Port ID changed or device was marked to be updated;
2451                          * Log it out if still logged in and mark it for
2452                          * relogin later.
2453                          */
2454                         fcport->d_id.b24 = new_fcport->d_id.b24;
2455                         fcport->flags |= FCF_LOGIN_NEEDED;
2456                         if (fcport->loop_id != FC_NO_LOOP_ID &&
2457                             (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2458                             fcport->port_type != FCT_INITIATOR &&
2459                             fcport->port_type != FCT_BROADCAST) {
2460                                 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2461                                     fcport->d_id.b.domain, fcport->d_id.b.area,
2462                                     fcport->d_id.b.al_pa);
2463                                 fcport->loop_id = FC_NO_LOOP_ID;
2464                         }
2465
2466                         break;
2467                 }
2468
2469                 if (found)
2470                         continue;
2471
2472                 /* If device was not in our fcports list, then add it. */
2473                 list_add_tail(&new_fcport->list, new_fcports);
2474
2475                 /* Allocate a new replacement fcport. */
2476                 nxt_d_id.b24 = new_fcport->d_id.b24;
2477                 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2478                 if (new_fcport == NULL) {
2479                         kfree(swl);
2480                         return (QLA_MEMORY_ALLOC_FAILED);
2481                 }
2482                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2483                 new_fcport->d_id.b24 = nxt_d_id.b24;
2484         }
2485
2486         kfree(swl);
2487         kfree(new_fcport);
2488
2489         if (!list_empty(new_fcports))
2490                 ha->device_flags |= DFLG_FABRIC_DEVICES;
2491
2492         return (rval);
2493 }
2494
2495 /*
2496  * qla2x00_find_new_loop_id
2497  *      Scan through our port list and find a new usable loop ID.
2498  *
2499  * Input:
2500  *      ha:     adapter state pointer.
2501  *      dev:    port structure pointer.
2502  *
2503  * Returns:
2504  *      qla2x00 local function return status code.
2505  *
2506  * Context:
2507  *      Kernel context.
2508  */
2509 int
2510 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2511 {
2512         int     rval;
2513         int     found;
2514         fc_port_t *fcport;
2515         uint16_t first_loop_id;
2516
2517         rval = QLA_SUCCESS;
2518
2519         /* Save starting loop ID. */
2520         first_loop_id = dev->loop_id;
2521
2522         for (;;) {
2523                 /* Skip loop ID if already used by adapter. */
2524                 if (dev->loop_id == ha->loop_id) {
2525                         dev->loop_id++;
2526                 }
2527
2528                 /* Skip reserved loop IDs. */
2529                 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2530                         dev->loop_id++;
2531                 }
2532
2533                 /* Reset loop ID if passed the end. */
2534                 if (dev->loop_id > ha->last_loop_id) {
2535                         /* first loop ID. */
2536                         dev->loop_id = ha->min_external_loopid;
2537                 }
2538
2539                 /* Check for loop ID being already in use. */
2540                 found = 0;
2541                 fcport = NULL;
2542                 list_for_each_entry(fcport, &ha->fcports, list) {
2543                         if (fcport->loop_id == dev->loop_id && fcport != dev) {
2544                                 /* ID possibly in use */
2545                                 found++;
2546                                 break;
2547                         }
2548                 }
2549
2550                 /* If not in use then it is free to use. */
2551                 if (!found) {
2552                         break;
2553                 }
2554
2555                 /* ID in use. Try next value. */
2556                 dev->loop_id++;
2557
2558                 /* If wrap around. No free ID to use. */
2559                 if (dev->loop_id == first_loop_id) {
2560                         dev->loop_id = FC_NO_LOOP_ID;
2561                         rval = QLA_FUNCTION_FAILED;
2562                         break;
2563                 }
2564         }
2565
2566         return (rval);
2567 }
2568
2569 /*
2570  * qla2x00_device_resync
2571  *      Marks devices in the database that needs resynchronization.
2572  *
2573  * Input:
2574  *      ha = adapter block pointer.
2575  *
2576  * Context:
2577  *      Kernel context.
2578  */
2579 static int
2580 qla2x00_device_resync(scsi_qla_host_t *ha)
2581 {
2582         int     rval;
2583         int     rval2;
2584         uint32_t mask;
2585         fc_port_t *fcport;
2586         uint32_t rscn_entry;
2587         uint8_t rscn_out_iter;
2588         uint8_t format;
2589         port_id_t d_id;
2590
2591         rval = QLA_RSCNS_HANDLED;
2592
2593         while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2594             ha->flags.rscn_queue_overflow) {
2595
2596                 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2597                 format = MSB(MSW(rscn_entry));
2598                 d_id.b.domain = LSB(MSW(rscn_entry));
2599                 d_id.b.area = MSB(LSW(rscn_entry));
2600                 d_id.b.al_pa = LSB(LSW(rscn_entry));
2601
2602                 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2603                     "[%02x/%02x%02x%02x].\n",
2604                     ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2605                     d_id.b.area, d_id.b.al_pa));
2606
2607                 ha->rscn_out_ptr++;
2608                 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2609                         ha->rscn_out_ptr = 0;
2610
2611                 /* Skip duplicate entries. */
2612                 for (rscn_out_iter = ha->rscn_out_ptr;
2613                     !ha->flags.rscn_queue_overflow &&
2614                     rscn_out_iter != ha->rscn_in_ptr;
2615                     rscn_out_iter = (rscn_out_iter ==
2616                         (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2617
2618                         if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2619                                 break;
2620
2621                         DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2622                             "entry found at [%d].\n", ha->host_no,
2623                             rscn_out_iter));
2624
2625                         ha->rscn_out_ptr = rscn_out_iter;
2626                 }
2627
2628                 /* Queue overflow, set switch default case. */
2629                 if (ha->flags.rscn_queue_overflow) {
2630                         DEBUG(printk("scsi(%ld): device_resync: rscn "
2631                             "overflow.\n", ha->host_no));
2632
2633                         format = 3;
2634                         ha->flags.rscn_queue_overflow = 0;
2635                 }
2636
2637                 switch (format) {
2638                 case 0:
2639                         if (ql2xprocessrscn &&
2640                             !IS_QLA2100(ha) && !IS_QLA2200(ha) &&
2641                             !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
2642                             !IS_QLA24XX(ha) && !IS_QLA54XX(ha) &&
2643                             ha->flags.init_done) {
2644                                 /* Handle port RSCN via asyncronous IOCBs */
2645                                 rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
2646                                     NULL, 0);
2647                                 if (rval2 == QLA_SUCCESS)
2648                                         continue;
2649                         }
2650                         mask = 0xffffff;
2651                         break;
2652                 case 1:
2653                         mask = 0xffff00;
2654                         break;
2655                 case 2:
2656                         mask = 0xff0000;
2657                         break;
2658                 default:
2659                         mask = 0x0;
2660                         d_id.b24 = 0;
2661                         ha->rscn_out_ptr = ha->rscn_in_ptr;
2662                         break;
2663                 }
2664
2665                 rval = QLA_SUCCESS;
2666
2667                 /* Abort any outstanding IO descriptors. */
2668                 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2669                         qla2x00_cancel_io_descriptors(ha);
2670
2671                 list_for_each_entry(fcport, &ha->fcports, list) {
2672                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2673                             (fcport->d_id.b24 & mask) != d_id.b24 ||
2674                             fcport->port_type == FCT_BROADCAST)
2675                                 continue;
2676
2677                         if (atomic_read(&fcport->state) == FCS_ONLINE) {
2678                                 if (format != 3 ||
2679                                     fcport->port_type != FCT_INITIATOR) {
2680                                         qla2x00_mark_device_lost(ha, fcport,
2681                                             0, 0);
2682                                 }
2683                         }
2684                         fcport->flags &= ~FCF_FARP_DONE;
2685                 }
2686         }
2687         return (rval);
2688 }
2689
2690 /*
2691  * qla2x00_fabric_dev_login
2692  *      Login fabric target device and update FC port database.
2693  *
2694  * Input:
2695  *      ha:             adapter state pointer.
2696  *      fcport:         port structure list pointer.
2697  *      next_loopid:    contains value of a new loop ID that can be used
2698  *                      by the next login attempt.
2699  *
2700  * Returns:
2701  *      qla2x00 local function return status code.
2702  *
2703  * Context:
2704  *      Kernel context.
2705  */
2706 static int
2707 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2708     uint16_t *next_loopid)
2709 {
2710         int     rval;
2711         int     retry;
2712         uint8_t opts;
2713
2714         rval = QLA_SUCCESS;
2715         retry = 0;
2716
2717         rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2718         if (rval == QLA_SUCCESS) {
2719                 /* Send an ADISC to tape devices.*/
2720                 opts = 0;
2721                 if (fcport->flags & FCF_TAPE_PRESENT)
2722                         opts |= BIT_1;
2723                 rval = qla2x00_get_port_database(ha, fcport, opts);
2724                 if (rval != QLA_SUCCESS) {
2725                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2726                             fcport->d_id.b.domain, fcport->d_id.b.area,
2727                             fcport->d_id.b.al_pa);
2728                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2729                 } else {
2730                         qla2x00_update_fcport(ha, fcport);
2731                 }
2732         }
2733
2734         return (rval);
2735 }
2736
2737 /*
2738  * qla2x00_fabric_login
2739  *      Issue fabric login command.
2740  *
2741  * Input:
2742  *      ha = adapter block pointer.
2743  *      device = pointer to FC device type structure.
2744  *
2745  * Returns:
2746  *      0 - Login successfully
2747  *      1 - Login failed
2748  *      2 - Initiator device
2749  *      3 - Fatal error
2750  */
2751 int
2752 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2753     uint16_t *next_loopid)
2754 {
2755         int     rval;
2756         int     retry;
2757         uint16_t tmp_loopid;
2758         uint16_t mb[MAILBOX_REGISTER_COUNT];
2759
2760         retry = 0;
2761         tmp_loopid = 0;
2762
2763         for (;;) {
2764                 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2765                     "for port %02x%02x%02x.\n",
2766                     ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2767                     fcport->d_id.b.area, fcport->d_id.b.al_pa));
2768
2769                 /* Login fcport on switch. */
2770                 ha->isp_ops.fabric_login(ha, fcport->loop_id,
2771                     fcport->d_id.b.domain, fcport->d_id.b.area,
2772                     fcport->d_id.b.al_pa, mb, BIT_0);
2773                 if (mb[0] == MBS_PORT_ID_USED) {
2774                         /*
2775                          * Device has another loop ID.  The firmware team
2776                          * recommends the driver perform an implicit login with
2777                          * the specified ID again. The ID we just used is save
2778                          * here so we return with an ID that can be tried by
2779                          * the next login.
2780                          */
2781                         retry++;
2782                         tmp_loopid = fcport->loop_id;
2783                         fcport->loop_id = mb[1];
2784
2785                         DEBUG(printk("Fabric Login: port in use - next "
2786                             "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2787                             fcport->loop_id, fcport->d_id.b.domain,
2788                             fcport->d_id.b.area, fcport->d_id.b.al_pa));
2789
2790                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2791                         /*
2792                          * Login succeeded.
2793                          */
2794                         if (retry) {
2795                                 /* A retry occurred before. */
2796                                 *next_loopid = tmp_loopid;
2797                         } else {
2798                                 /*
2799                                  * No retry occurred before. Just increment the
2800                                  * ID value for next login.
2801                                  */
2802                                 *next_loopid = (fcport->loop_id + 1);
2803                         }
2804
2805                         if (mb[1] & BIT_0) {
2806                                 fcport->port_type = FCT_INITIATOR;
2807                         } else {
2808                                 fcport->port_type = FCT_TARGET;
2809                                 if (mb[1] & BIT_1) {
2810                                         fcport->flags |= FCF_TAPE_PRESENT;
2811                                 }
2812                         }
2813
2814                         if (mb[10] & BIT_0)
2815                                 fcport->supported_classes |= FC_COS_CLASS2;
2816                         if (mb[10] & BIT_1)
2817                                 fcport->supported_classes |= FC_COS_CLASS3;
2818
2819                         rval = QLA_SUCCESS;
2820                         break;
2821                 } else if (mb[0] == MBS_LOOP_ID_USED) {
2822                         /*
2823                          * Loop ID already used, try next loop ID.
2824                          */
2825                         fcport->loop_id++;
2826                         rval = qla2x00_find_new_loop_id(ha, fcport);
2827                         if (rval != QLA_SUCCESS) {
2828                                 /* Ran out of loop IDs to use */
2829                                 break;
2830                         }
2831                 } else if (mb[0] == MBS_COMMAND_ERROR) {
2832                         /*
2833                          * Firmware possibly timed out during login. If NO
2834                          * retries are left to do then the device is declared
2835                          * dead.
2836                          */
2837                         *next_loopid = fcport->loop_id;
2838                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2839                             fcport->d_id.b.domain, fcport->d_id.b.area,
2840                             fcport->d_id.b.al_pa);
2841                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2842
2843                         rval = 1;
2844                         break;
2845                 } else {
2846                         /*
2847                          * unrecoverable / not handled error
2848                          */
2849                         DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
2850                             "loop_id=%x jiffies=%lx.\n",
2851                             __func__, ha->host_no, mb[0],
2852                             fcport->d_id.b.domain, fcport->d_id.b.area,
2853                             fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2854
2855                         *next_loopid = fcport->loop_id;
2856                         ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2857                             fcport->d_id.b.domain, fcport->d_id.b.area,
2858                             fcport->d_id.b.al_pa);
2859                         fcport->loop_id = FC_NO_LOOP_ID;
2860                         fcport->login_retry = 0;
2861
2862                         rval = 3;
2863                         break;
2864                 }
2865         }
2866
2867         return (rval);
2868 }
2869
2870 /*
2871  * qla2x00_local_device_login
2872  *      Issue local device login command.
2873  *
2874  * Input:
2875  *      ha = adapter block pointer.
2876  *      loop_id = loop id of device to login to.
2877  *
2878  * Returns (Where's the #define!!!!):
2879  *      0 - Login successfully
2880  *      1 - Login failed
2881  *      3 - Fatal error
2882  */
2883 int
2884 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
2885 {
2886         int             rval;
2887         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2888
2889         memset(mb, 0, sizeof(mb));
2890         rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
2891         if (rval == QLA_SUCCESS) {
2892                 /* Interrogate mailbox registers for any errors */
2893                 if (mb[0] == MBS_COMMAND_ERROR)
2894                         rval = 1;
2895                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2896                         /* device not in PCB table */
2897                         rval = 3;
2898         }
2899
2900         return (rval);
2901 }
2902
2903 /*
2904  *  qla2x00_loop_resync
2905  *      Resync with fibre channel devices.
2906  *
2907  * Input:
2908  *      ha = adapter block pointer.
2909  *
2910  * Returns:
2911  *      0 = success
2912  */
2913 int
2914 qla2x00_loop_resync(scsi_qla_host_t *ha)
2915 {
2916         int   rval;
2917         uint32_t wait_time;
2918
2919         rval = QLA_SUCCESS;
2920
2921         atomic_set(&ha->loop_state, LOOP_UPDATE);
2922         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2923         if (ha->flags.online) {
2924                 if (!(rval = qla2x00_fw_ready(ha))) {
2925                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
2926                         wait_time = 256;
2927                         do {
2928                                 atomic_set(&ha->loop_state, LOOP_UPDATE);
2929
2930                                 /* Issue a marker after FW becomes ready. */
2931                                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2932                                 ha->marker_needed = 0;
2933
2934                                 /* Remap devices on Loop. */
2935                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2936
2937                                 qla2x00_configure_loop(ha);
2938                                 wait_time--;
2939                         } while (!atomic_read(&ha->loop_down_timer) &&
2940                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2941                                 wait_time &&
2942                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2943                 }
2944         }
2945
2946         if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2947                 return (QLA_FUNCTION_FAILED);
2948         }
2949
2950         if (rval) {
2951                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
2952         }
2953
2954         return (rval);
2955 }
2956
2957 void
2958 qla2x00_rescan_fcports(scsi_qla_host_t *ha)
2959 {
2960         int rescan_done;
2961         fc_port_t *fcport;
2962
2963         rescan_done = 0;
2964         list_for_each_entry(fcport, &ha->fcports, list) {
2965                 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
2966                         continue;
2967
2968                 qla2x00_update_fcport(ha, fcport);
2969                 fcport->flags &= ~FCF_RESCAN_NEEDED;
2970
2971                 rescan_done = 1;
2972         }
2973         qla2x00_probe_for_all_luns(ha);
2974 }
2975
2976 void
2977 qla2x00_update_fcports(scsi_qla_host_t *ha)
2978 {
2979         fc_port_t *fcport;
2980
2981         /* Go with deferred removal of rport references. */
2982         list_for_each_entry(fcport, &ha->fcports, list)
2983                 if (fcport->drport)
2984                         qla2x00_rport_del(fcport);
2985 }
2986
2987 /*
2988 *  qla2x00_abort_isp
2989 *      Resets ISP and aborts all outstanding commands.
2990 *
2991 * Input:
2992 *      ha           = adapter block pointer.
2993 *
2994 * Returns:
2995 *      0 = success
2996 */
2997 int
2998 qla2x00_abort_isp(scsi_qla_host_t *ha)
2999 {
3000         unsigned long flags = 0;
3001         uint16_t       cnt;
3002         srb_t          *sp;
3003         uint8_t        status = 0;
3004
3005         if (ha->flags.online) {
3006                 ha->flags.online = 0;
3007                 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3008
3009                 qla_printk(KERN_INFO, ha,
3010                     "Performing ISP error recovery - ha= %p.\n", ha);
3011                 ha->isp_ops.reset_chip(ha);
3012
3013                 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3014                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3015                         atomic_set(&ha->loop_state, LOOP_DOWN);
3016                         qla2x00_mark_all_devices_lost(ha, 0);
3017                 } else {
3018                         if (!atomic_read(&ha->loop_down_timer))
3019                                 atomic_set(&ha->loop_down_timer,
3020                                     LOOP_DOWN_TIME);
3021                 }
3022
3023                 spin_lock_irqsave(&ha->hardware_lock, flags);
3024                 /* Requeue all commands in outstanding command list. */
3025                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3026                         sp = ha->outstanding_cmds[cnt];
3027                         if (sp) {
3028                                 ha->outstanding_cmds[cnt] = NULL;
3029                                 sp->flags = 0;
3030                                 sp->cmd->result = DID_RESET << 16;
3031                                 sp->cmd->host_scribble = (unsigned char *)NULL;
3032                                 qla2x00_sp_compl(ha, sp);
3033                         }
3034                 }
3035                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3036
3037                 ha->isp_ops.nvram_config(ha);
3038
3039                 if (!qla2x00_restart_isp(ha)) {
3040                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3041
3042                         if (!atomic_read(&ha->loop_down_timer)) {
3043                                 /*
3044                                  * Issue marker command only when we are going
3045                                  * to start the I/O .
3046                                  */
3047                                 ha->marker_needed = 1;
3048                         }
3049
3050                         ha->flags.online = 1;
3051
3052                         ha->isp_ops.enable_intrs(ha);
3053
3054                         ha->isp_abort_cnt = 0;
3055                         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3056                 } else {        /* failed the ISP abort */
3057                         ha->flags.online = 1;
3058                         if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3059                                 if (ha->isp_abort_cnt == 0) {
3060                                         qla_printk(KERN_WARNING, ha,
3061                                             "ISP error recovery failed - "
3062                                             "board disabled\n");
3063                                         /*
3064                                          * The next call disables the board
3065                                          * completely.
3066                                          */
3067                                         ha->isp_ops.reset_adapter(ha);
3068                                         ha->flags.online = 0;
3069                                         clear_bit(ISP_ABORT_RETRY,
3070                                             &ha->dpc_flags);
3071                                         status = 0;
3072                                 } else { /* schedule another ISP abort */
3073                                         ha->isp_abort_cnt--;
3074                                         DEBUG(printk("qla%ld: ISP abort - "
3075                                             "retry remaining %d\n",
3076                                             ha->host_no, ha->isp_abort_cnt);)
3077                                         status = 1;
3078                                 }
3079                         } else {
3080                                 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3081                                 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3082                                     "- retrying (%d) more times\n",
3083                                     ha->host_no, ha->isp_abort_cnt);)
3084                                 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3085                                 status = 1;
3086                         }
3087                 }
3088
3089         }
3090
3091         if (status) {
3092                 qla_printk(KERN_INFO, ha,
3093                         "qla2x00_abort_isp: **** FAILED ****\n");
3094         } else {
3095                 DEBUG(printk(KERN_INFO
3096                                 "qla2x00_abort_isp(%ld): exiting.\n",
3097                                 ha->host_no);)
3098         }
3099
3100         return(status);
3101 }
3102
3103 /*
3104 *  qla2x00_restart_isp
3105 *      restarts the ISP after a reset
3106 *
3107 * Input:
3108 *      ha = adapter block pointer.
3109 *
3110 * Returns:
3111 *      0 = success
3112 */
3113 static int
3114 qla2x00_restart_isp(scsi_qla_host_t *ha)
3115 {
3116         uint8_t         status = 0;
3117         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3118         unsigned long   flags = 0;
3119         uint32_t wait_time;
3120
3121         /* If firmware needs to be loaded */
3122         if (qla2x00_isp_firmware(ha)) {
3123                 ha->flags.online = 0;
3124                 if (!(status = ha->isp_ops.chip_diag(ha))) {
3125                         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3126                                 status = qla2x00_setup_chip(ha);
3127                                 goto done;
3128                         }
3129
3130                         spin_lock_irqsave(&ha->hardware_lock, flags);
3131
3132                         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3133                                 /*
3134                                  * Disable SRAM, Instruction RAM and GP RAM
3135                                  * parity.
3136                                  */
3137                                 WRT_REG_WORD(&reg->hccr,
3138                                     (HCCR_ENABLE_PARITY + 0x0));
3139                                 RD_REG_WORD(&reg->hccr);
3140                         }
3141
3142                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3143
3144                         status = qla2x00_setup_chip(ha);
3145
3146                         spin_lock_irqsave(&ha->hardware_lock, flags);
3147
3148                         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
3149                                 /* Enable proper parity */
3150                                 if (IS_QLA2300(ha))
3151                                         /* SRAM parity */
3152                                         WRT_REG_WORD(&reg->hccr,
3153                                             (HCCR_ENABLE_PARITY + 0x1));
3154                                 else
3155                                         /*
3156                                          * SRAM, Instruction RAM and GP RAM
3157                                          * parity.
3158                                          */
3159                                         WRT_REG_WORD(&reg->hccr,
3160                                             (HCCR_ENABLE_PARITY + 0x7));
3161                                 RD_REG_WORD(&reg->hccr);
3162                         }
3163
3164                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3165                 }
3166         }
3167
3168  done:
3169         if (!status && !(status = qla2x00_init_rings(ha))) {
3170                 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3171                 if (!(status = qla2x00_fw_ready(ha))) {
3172                         DEBUG(printk("%s(): Start configure loop, "
3173                             "status = %d\n", __func__, status);)
3174
3175                         /* Issue a marker after FW becomes ready. */
3176                         qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3177
3178                         ha->flags.online = 1;
3179                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3180                         wait_time = 256;
3181                         do {
3182                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3183                                 qla2x00_configure_loop(ha);
3184                                 wait_time--;
3185                         } while (!atomic_read(&ha->loop_down_timer) &&
3186                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3187                                 wait_time &&
3188                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3189                 }
3190
3191                 /* if no cable then assume it's good */
3192                 if ((ha->device_flags & DFLG_NO_CABLE))
3193                         status = 0;
3194
3195                 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3196                                 __func__,
3197                                 status);)
3198         }
3199         return (status);
3200 }
3201
3202 /*
3203 * qla2x00_reset_adapter
3204 *      Reset adapter.
3205 *
3206 * Input:
3207 *      ha = adapter block pointer.
3208 */
3209 void
3210 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3211 {
3212         unsigned long flags = 0;
3213         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3214
3215         ha->flags.online = 0;
3216         ha->isp_ops.disable_intrs(ha);
3217
3218         spin_lock_irqsave(&ha->hardware_lock, flags);
3219         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3220         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3221         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3222         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3223         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3224 }
3225
3226 void
3227 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3228 {
3229         unsigned long flags = 0;
3230         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3231
3232         ha->flags.online = 0;
3233         ha->isp_ops.disable_intrs(ha);
3234
3235         spin_lock_irqsave(&ha->hardware_lock, flags);
3236         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3237         RD_REG_DWORD(&reg->hccr);
3238         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3239         RD_REG_DWORD(&reg->hccr);
3240         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3241 }
3242
3243 int
3244 qla24xx_nvram_config(scsi_qla_host_t *ha)
3245 {
3246         int   rval;
3247         struct init_cb_24xx *icb;
3248         struct nvram_24xx *nv;
3249         uint32_t *dptr;
3250         uint8_t  *dptr1, *dptr2;
3251         uint32_t chksum;
3252         uint16_t cnt;
3253
3254         rval = QLA_SUCCESS;
3255         icb = (struct init_cb_24xx *)ha->init_cb;
3256         nv = (struct nvram_24xx *)ha->request_ring;
3257
3258         /* Determine NVRAM starting address. */
3259         ha->nvram_size = sizeof(struct nvram_24xx);
3260         ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3261         ha->vpd_size = FA_NVRAM_VPD_SIZE;
3262         ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3263         if (PCI_FUNC(ha->pdev->devfn)) {
3264                 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3265                 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3266         }
3267
3268         /* Get NVRAM data and calculate checksum. */
3269         dptr = (uint32_t *)nv;
3270         ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3271             ha->nvram_size);
3272         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3273                 chksum += le32_to_cpu(*dptr++);
3274
3275         DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3276         DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3277             ha->nvram_size));
3278
3279         /* Bad NVRAM data, set defaults parameters. */
3280         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3281             || nv->id[3] != ' ' ||
3282             nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3283                 /* Reset NVRAM data. */
3284                 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3285                     "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3286                     le16_to_cpu(nv->nvram_version));
3287                 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3288                     "invalid -- WWPN) defaults.\n");
3289
3290                 /*
3291                  * Set default initialization control block.
3292                  */
3293                 memset(nv, 0, ha->nvram_size);
3294                 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3295                 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3296                 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3297                 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3298                 nv->exchange_count = __constant_cpu_to_le16(0);
3299                 nv->hard_address = __constant_cpu_to_le16(124);
3300                 nv->port_name[0] = 0x21;
3301                 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3302                 nv->port_name[2] = 0x00;
3303                 nv->port_name[3] = 0xe0;
3304                 nv->port_name[4] = 0x8b;
3305                 nv->port_name[5] = 0x1c;
3306                 nv->port_name[6] = 0x55;
3307                 nv->port_name[7] = 0x86;
3308                 nv->node_name[0] = 0x20;
3309                 nv->node_name[1] = 0x00;
3310                 nv->node_name[2] = 0x00;
3311                 nv->node_name[3] = 0xe0;
3312                 nv->node_name[4] = 0x8b;
3313                 nv->node_name[5] = 0x1c;
3314                 nv->node_name[6] = 0x55;
3315                 nv->node_name[7] = 0x86;
3316                 nv->login_retry_count = __constant_cpu_to_le16(8);
3317                 nv->link_down_timeout = __constant_cpu_to_le16(200);
3318                 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3319                 nv->login_timeout = __constant_cpu_to_le16(0);
3320                 nv->firmware_options_1 =
3321                     __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3322                 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3323                 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3324                 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3325                 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3326                 nv->efi_parameters = __constant_cpu_to_le32(0);
3327                 nv->reset_delay = 5;
3328                 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3329                 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3330                 nv->link_down_timeout = __constant_cpu_to_le16(30);
3331
3332                 rval = 1;
3333         }
3334
3335         /* Reset Initialization control block */
3336         memset(icb, 0, sizeof(struct init_cb_24xx));
3337
3338         /* Copy 1st segment. */
3339         dptr1 = (uint8_t *)icb;
3340         dptr2 = (uint8_t *)&nv->version;
3341         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3342         while (cnt--)
3343                 *dptr1++ = *dptr2++;
3344
3345         icb->login_retry_count = nv->login_retry_count;
3346         icb->link_down_timeout = nv->link_down_timeout;
3347
3348         /* Copy 2nd segment. */
3349         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3350         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3351         cnt = (uint8_t *)&icb->reserved_3 -
3352             (uint8_t *)&icb->interrupt_delay_timer;
3353         while (cnt--)
3354                 *dptr1++ = *dptr2++;
3355
3356         /*
3357          * Setup driver NVRAM options.
3358          */
3359         if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3360                 char *st, *en;
3361                 uint16_t index;
3362
3363                 strncpy(ha->model_number, nv->model_name,
3364                     sizeof(nv->model_name));
3365                 st = en = ha->model_number;
3366                 en += sizeof(nv->model_name) - 1;
3367                 while (en > st) {
3368                         if (*en != 0x20 && *en != 0x00)
3369                                 break;
3370                         *en-- = '\0';
3371                 }
3372
3373                 index = (ha->pdev->subsystem_device & 0xff);
3374                 if (index < QLA_MODEL_NAMES)
3375                         ha->model_desc = qla2x00_model_name[index * 2 + 1];
3376         } else
3377                 strcpy(ha->model_number, "QLA2462");
3378
3379         /* Use alternate WWN? */
3380         if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3381                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3382                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3383         }
3384
3385         /* Prepare nodename */
3386         if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
3387                 /*
3388                  * Firmware will apply the following mask if the nodename was
3389                  * not provided.
3390                  */
3391                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3392                 icb->node_name[0] &= 0xF0;
3393         }
3394
3395         /* Set host adapter parameters. */
3396         ha->flags.disable_risc_code_load = 0;
3397         ha->flags.enable_lip_reset = 1;
3398         ha->flags.enable_lip_full_login = 1;
3399         ha->flags.enable_target_reset = 1;
3400         ha->flags.enable_led_scheme = 0;
3401
3402         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3403             (BIT_6 | BIT_5 | BIT_4)) >> 4;
3404
3405         memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3406             sizeof(ha->fw_seriallink_options24));
3407
3408         /* save HBA serial number */
3409         ha->serial0 = icb->port_name[5];
3410         ha->serial1 = icb->port_name[6];
3411         ha->serial2 = icb->port_name[7];
3412         ha->node_name = icb->node_name;
3413         ha->port_name = icb->port_name;
3414
3415         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3416
3417         ha->retry_count = le16_to_cpu(nv->login_retry_count);
3418
3419         /* Set minimum login_timeout to 4 seconds. */
3420         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3421                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3422         if (le16_to_cpu(nv->login_timeout) < 4)
3423                 nv->login_timeout = __constant_cpu_to_le16(4);
3424         ha->login_timeout = le16_to_cpu(nv->login_timeout);
3425         icb->login_timeout = cpu_to_le16(nv->login_timeout);
3426
3427         /* Set minimum RATOV to 200 tenths of a second. */
3428         ha->r_a_tov = 200;
3429
3430         ha->loop_reset_delay = nv->reset_delay;
3431
3432         /* Link Down Timeout = 0:
3433          *
3434          *      When Port Down timer expires we will start returning
3435          *      I/O's to OS with "DID_NO_CONNECT".
3436          *
3437          * Link Down Timeout != 0:
3438          *
3439          *       The driver waits for the link to come up after link down
3440          *       before returning I/Os to OS with "DID_NO_CONNECT".
3441          */
3442         if (le16_to_cpu(nv->link_down_timeout) == 0) {
3443                 ha->loop_down_abort_time =
3444                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3445         } else {
3446                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3447                 ha->loop_down_abort_time =
3448                     (LOOP_DOWN_TIME - ha->link_down_timeout);
3449         }
3450
3451         /* Need enough time to try and get the port back. */
3452         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3453         if (qlport_down_retry)
3454                 ha->port_down_retry_count = qlport_down_retry;
3455
3456         /* Set login_retry_count */
3457         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
3458         if (ha->port_down_retry_count ==
3459             le16_to_cpu(nv->port_down_retry_count) &&
3460             ha->port_down_retry_count > 3)
3461                 ha->login_retry_count = ha->port_down_retry_count;
3462         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3463                 ha->login_retry_count = ha->port_down_retry_count;
3464         if (ql2xloginretrycount)
3465                 ha->login_retry_count = ql2xloginretrycount;
3466
3467         /* Enable ZIO. */
3468         if (!ha->flags.init_done) {
3469                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3470                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3471                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3472                     le16_to_cpu(icb->interrupt_delay_timer): 2;
3473         }
3474         icb->firmware_options_2 &= __constant_cpu_to_le32(
3475             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3476         ha->flags.process_response_queue = 0;
3477         if (ha->zio_mode != QLA_ZIO_DISABLED) {
3478                 ha->zio_mode = QLA_ZIO_MODE_6;
3479
3480                 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3481                     "(%d us).\n", ha->host_no, ha->zio_mode,
3482                     ha->zio_timer * 100));
3483                 qla_printk(KERN_INFO, ha,
3484                     "ZIO mode %d enabled; timer delay (%d us).\n",
3485                     ha->zio_mode, ha->zio_timer * 100);
3486
3487                 icb->firmware_options_2 |= cpu_to_le32(
3488                     (uint32_t)ha->zio_mode);
3489                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3490                 ha->flags.process_response_queue = 1;
3491         }
3492
3493         if (rval) {
3494                 DEBUG2_3(printk(KERN_WARNING
3495                     "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3496         }
3497         return (rval);
3498 }
3499
3500 int
3501 qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3502 {
3503         int     rval;
3504         int     segments, fragment;
3505         uint32_t faddr;
3506         uint32_t *dcode, dlen;
3507         uint32_t risc_addr;
3508         uint32_t risc_size;
3509         uint32_t i;
3510
3511         rval = QLA_SUCCESS;
3512
3513         segments = FA_RISC_CODE_SEGMENTS;
3514         faddr = FA_RISC_CODE_ADDR;
3515         dcode = (uint32_t *)ha->request_ring;
3516         *srisc_addr = 0;
3517
3518         /* Validate firmware image by checking version. */
3519         qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3520         for (i = 0; i < 4; i++)
3521                 dcode[i] = be32_to_cpu(dcode[i]);
3522         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3523             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3524             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3525                 dcode[3] == 0)) {
3526                 qla_printk(KERN_WARNING, ha,
3527                     "Unable to verify integrity of flash firmware image!\n");
3528                 qla_printk(KERN_WARNING, ha,
3529                     "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3530                     dcode[1], dcode[2], dcode[3]);
3531
3532                 return QLA_FUNCTION_FAILED;
3533         }
3534
3535         while (segments && rval == QLA_SUCCESS) {
3536                 /* Read segment's load information. */
3537                 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3538
3539                 risc_addr = be32_to_cpu(dcode[2]);
3540                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3541                 risc_size = be32_to_cpu(dcode[3]);
3542
3543                 fragment = 0;
3544                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3545                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3546                         if (dlen > risc_size)
3547                                 dlen = risc_size;
3548
3549                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3550                             "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3551                             ha->host_no, risc_addr, dlen, faddr));
3552
3553                         qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3554                         for (i = 0; i < dlen; i++)
3555                                 dcode[i] = swab32(dcode[i]);
3556
3557                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3558                             dlen);
3559                         if (rval) {
3560                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3561                                     "segment %d of firmware\n", ha->host_no,
3562                                     fragment));
3563                                 qla_printk(KERN_WARNING, ha,
3564                                     "[ERROR] Failed to load segment %d of "
3565                                     "firmware\n", fragment);
3566                                 break;
3567                         }
3568
3569                         faddr += dlen;
3570                         risc_addr += dlen;
3571                         risc_size -= dlen;
3572                         fragment++;
3573                 }
3574
3575                 /* Next segment. */
3576                 segments--;
3577         }
3578
3579         return rval;
3580 }
3581
3582 #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
3583
3584 int
3585 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3586 {
3587         int     rval, num, i;
3588         uint32_t cnt;
3589         uint16_t *risc_code;
3590         uint32_t risc_addr, risc_size;
3591         uint16_t *req_ring;
3592         struct qla_fw_info *fw_iter;
3593
3594         rval = QLA_SUCCESS;
3595
3596         /* Load firmware sequences */
3597         fw_iter = ha->brd_info->fw_info;
3598         *srisc_addr = *ha->brd_info->fw_info->fwstart;
3599         while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
3600                 risc_code = fw_iter->fwcode;
3601                 risc_size = *fw_iter->fwlen;
3602                 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL)
3603                         risc_addr = *fw_iter->fwstart;
3604                 else
3605                         risc_addr = *fw_iter->lfwstart;
3606
3607                 num = 0;
3608                 rval = 0;
3609                 while (risc_size > 0 && !rval) {
3610                         cnt = (uint16_t)(ha->fw_transfer_size >> 1);
3611                         if (cnt > risc_size)
3612                                 cnt = risc_size;
3613
3614                         DEBUG7(printk("scsi(%ld): Loading risc segment@ "
3615                             "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
3616                             ha->host_no, risc_code, cnt, risc_addr));
3617
3618                         req_ring = (uint16_t *)ha->request_ring;
3619                         for (i = 0; i < cnt; i++)
3620                                 req_ring[i] = cpu_to_le16(risc_code[i]);
3621
3622                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3623                             cnt);
3624                         if (rval) {
3625                                 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
3626                                     "load segment %d of firmware\n",
3627                                     ha->host_no, num));
3628                                 qla_printk(KERN_WARNING, ha,
3629                                     "[ERROR] Failed to load segment %d of "
3630                                     "firmware\n", num);
3631
3632                                 qla2x00_dump_regs(ha);
3633                                 break;
3634                         }
3635
3636                         risc_code += cnt;
3637                         risc_addr += cnt;
3638                         risc_size -= cnt;
3639                         num++;
3640                 }
3641
3642                 /* Next firmware sequence */
3643                 fw_iter++;
3644         }
3645         return rval;
3646 }
3647
3648 int
3649 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3650 {
3651         int     rval, num, i;
3652         uint32_t cnt;
3653         uint32_t *risc_code;
3654         uint32_t risc_addr, risc_size;
3655         uint32_t *req_ring;
3656         struct qla_fw_info *fw_iter;
3657
3658         rval = QLA_SUCCESS;
3659
3660         /* Load firmware sequences */
3661         fw_iter = ha->brd_info->fw_info;
3662         *srisc_addr = *((uint32_t *)fw_iter->lfwstart);
3663         while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
3664                 risc_code = (uint32_t *)fw_iter->fwcode;
3665                 risc_size = *((uint32_t *)fw_iter->fwlen);
3666                 risc_addr = *((uint32_t *)fw_iter->lfwstart);
3667
3668                 num = 0;
3669                 rval = 0;
3670                 while (risc_size > 0 && !rval) {
3671                         cnt = (uint32_t)(ha->fw_transfer_size >> 2);
3672                         if (cnt > risc_size)
3673                                 cnt = risc_size;
3674
3675                         DEBUG7(printk("scsi(%ld): Loading risc segment@ "
3676                             "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
3677                             ha->host_no, risc_code, cnt, risc_addr));
3678
3679                         req_ring = (uint32_t *)ha->request_ring;
3680                         for (i = 0; i < cnt; i++)
3681                                 req_ring[i] = cpu_to_le32(risc_code[i]);
3682
3683                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3684                             cnt);
3685                         if (rval) {
3686                                 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
3687                                     "load segment %d of firmware\n",
3688                                     ha->host_no, num));
3689                                 qla_printk(KERN_WARNING, ha,
3690                                     "[ERROR] Failed to load segment %d of "
3691                                     "firmware\n", num);
3692
3693                                 qla2x00_dump_regs(ha);
3694                                 break;
3695                         }
3696
3697                         risc_code += cnt;
3698                         risc_addr += cnt;
3699                         risc_size -= cnt;
3700                         num++;
3701                 }
3702
3703                 /* Next firmware sequence */
3704                 fw_iter++;
3705         }
3706         return rval;
3707 }
3708
3709 #else   /* !defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE) */
3710
3711 #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3712
3713 int
3714 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3715 {
3716         int     rval;
3717         int     i, fragment;
3718         uint16_t *wcode, *fwcode;
3719         uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3720         struct fw_blob *blob;
3721
3722         /* Load firmware blob. */
3723         blob = qla2x00_request_firmware(ha);
3724         if (!blob) {
3725                 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3726                 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3727                     "from: " QLA_FW_URL ".\n");
3728                 return QLA_FUNCTION_FAILED;
3729         }
3730
3731         rval = QLA_SUCCESS;
3732
3733         wcode = (uint16_t *)ha->request_ring;
3734         *srisc_addr = 0;
3735         fwcode = (uint16_t *)blob->fw->data;
3736         fwclen = 0;
3737
3738         /* Validate firmware image by checking version. */
3739         if (blob->fw->size < 8 * sizeof(uint16_t)) {
3740                 qla_printk(KERN_WARNING, ha,
3741                     "Unable to verify integrity of firmware image (%Zd)!\n",
3742                     blob->fw->size);
3743                 goto fail_fw_integrity;
3744         }
3745         for (i = 0; i < 4; i++)
3746                 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3747         if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3748             wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3749                 wcode[2] == 0 && wcode[3] == 0)) {
3750                 qla_printk(KERN_WARNING, ha,
3751                     "Unable to verify integrity of firmware image!\n");
3752                 qla_printk(KERN_WARNING, ha,
3753                     "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3754                     wcode[1], wcode[2], wcode[3]);
3755                 goto fail_fw_integrity;
3756         }
3757
3758         seg = blob->segs;
3759         while (*seg && rval == QLA_SUCCESS) {
3760                 risc_addr = *seg;
3761                 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3762                 risc_size = be16_to_cpu(fwcode[3]);
3763
3764                 /* Validate firmware image size. */
3765                 fwclen += risc_size * sizeof(uint16_t);
3766                 if (blob->fw->size < fwclen) {
3767                         qla_printk(KERN_WARNING, ha,
3768                             "Unable to verify integrity of firmware image "
3769                             "(%Zd)!\n", blob->fw->size);
3770                         goto fail_fw_integrity;
3771                 }
3772
3773                 fragment = 0;
3774                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3775                         wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3776                         if (wlen > risc_size)
3777                                 wlen = risc_size;
3778
3779                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3780                             "addr %x, number of words 0x%x.\n", ha->host_no,
3781                             risc_addr, wlen));
3782
3783                         for (i = 0; i < wlen; i++)
3784                                 wcode[i] = swab16(fwcode[i]);
3785
3786                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3787                             wlen);
3788                         if (rval) {
3789                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3790                                     "segment %d of firmware\n", ha->host_no,
3791                                     fragment));
3792                                 qla_printk(KERN_WARNING, ha,
3793                                     "[ERROR] Failed to load segment %d of "
3794                                     "firmware\n", fragment);
3795                                 break;
3796                         }
3797
3798                         fwcode += wlen;
3799                         risc_addr += wlen;
3800                         risc_size -= wlen;
3801                         fragment++;
3802                 }
3803
3804                 /* Next segment. */
3805                 seg++;
3806         }
3807         return rval;
3808
3809 fail_fw_integrity:
3810         return QLA_FUNCTION_FAILED;
3811 }
3812
3813 int
3814 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3815 {
3816         int     rval;
3817         int     segments, fragment;
3818         uint32_t *dcode, dlen;
3819         uint32_t risc_addr;
3820         uint32_t risc_size;
3821         uint32_t i;
3822         struct fw_blob *blob;
3823         uint32_t *fwcode, fwclen;
3824
3825         /* Load firmware blob. */
3826         blob = qla2x00_request_firmware(ha);
3827         if (!blob) {
3828                 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3829                 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3830                     "from: " QLA_FW_URL ".\n");
3831
3832                 /* Try to load RISC code from flash. */
3833                 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3834                     "outdated) firmware from flash.\n");
3835                 return qla24xx_load_risc_flash(ha, srisc_addr);
3836         }
3837
3838         rval = QLA_SUCCESS;
3839
3840         segments = FA_RISC_CODE_SEGMENTS;
3841         dcode = (uint32_t *)ha->request_ring;
3842         *srisc_addr = 0;
3843         fwcode = (uint32_t *)blob->fw->data;
3844         fwclen = 0;
3845
3846         /* Validate firmware image by checking version. */
3847         if (blob->fw->size < 8 * sizeof(uint32_t)) {
3848                 qla_printk(KERN_WARNING, ha,
3849                     "Unable to verify integrity of firmware image (%Zd)!\n",
3850                     blob->fw->size);
3851                 goto fail_fw_integrity;
3852         }
3853         for (i = 0; i < 4; i++)
3854                 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3855         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3856             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3857             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3858                 dcode[3] == 0)) {
3859                 qla_printk(KERN_WARNING, ha,
3860                     "Unable to verify integrity of firmware image!\n");
3861                 qla_printk(KERN_WARNING, ha,
3862                     "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3863                     dcode[1], dcode[2], dcode[3]);
3864                 goto fail_fw_integrity;
3865         }
3866
3867         while (segments && rval == QLA_SUCCESS) {
3868                 risc_addr = be32_to_cpu(fwcode[2]);
3869                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3870                 risc_size = be32_to_cpu(fwcode[3]);
3871
3872                 /* Validate firmware image size. */
3873                 fwclen += risc_size * sizeof(uint32_t);
3874                 if (blob->fw->size < fwclen) {
3875                         qla_printk(KERN_WARNING, ha,
3876                             "Unable to verify integrity of firmware image "
3877                             "(%Zd)!\n", blob->fw->size);
3878
3879                         goto fail_fw_integrity;
3880                 }
3881
3882                 fragment = 0;
3883                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3884                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3885                         if (dlen > risc_size)
3886                                 dlen = risc_size;
3887
3888                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3889                             "addr %x, number of dwords 0x%x.\n", ha->host_no,
3890                             risc_addr, dlen));
3891
3892                         for (i = 0; i < dlen; i++)
3893                                 dcode[i] = swab32(fwcode[i]);
3894
3895                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3896                             dlen);
3897                         if (rval) {
3898                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3899                                     "segment %d of firmware\n", ha->host_no,
3900                                     fragment));
3901                                 qla_printk(KERN_WARNING, ha,
3902                                     "[ERROR] Failed to load segment %d of "
3903                                     "firmware\n", fragment);
3904                                 break;
3905                         }
3906
3907                         fwcode += dlen;
3908                         risc_addr += dlen;
3909                         risc_size -= dlen;
3910                         fragment++;
3911                 }
3912
3913                 /* Next segment. */
3914                 segments--;
3915         }
3916         return rval;
3917
3918 fail_fw_integrity:
3919         return QLA_FUNCTION_FAILED;
3920 }
3921 #endif