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