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