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