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