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