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