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