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