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