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