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