[SCSI] qla2xxx: Add Flash Descriptor Table layout support.
[safe/jmp/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 /*
19 *  QLogic ISP2x00 Hardware Support Function Prototypes.
20 */
21 static int qla2x00_isp_firmware(scsi_qla_host_t *);
22 static void qla2x00_resize_request_q(scsi_qla_host_t *);
23 static int qla2x00_setup_chip(scsi_qla_host_t *);
24 static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
25 static int qla2x00_init_rings(scsi_qla_host_t *);
26 static int qla2x00_fw_ready(scsi_qla_host_t *);
27 static int qla2x00_configure_hba(scsi_qla_host_t *);
28 static int qla2x00_configure_loop(scsi_qla_host_t *);
29 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
30 static int qla2x00_configure_fabric(scsi_qla_host_t *);
31 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
32 static int qla2x00_device_resync(scsi_qla_host_t *);
33 static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
34     uint16_t *);
35
36 static int qla2x00_restart_isp(scsi_qla_host_t *);
37
38 static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
39
40 /****************************************************************************/
41 /*                QLogic ISP2x00 Hardware Support Functions.                */
42 /****************************************************************************/
43
44 /*
45 * qla2x00_initialize_adapter
46 *      Initialize board.
47 *
48 * Input:
49 *      ha = adapter block pointer.
50 *
51 * Returns:
52 *      0 = success
53 */
54 int
55 qla2x00_initialize_adapter(scsi_qla_host_t *ha)
56 {
57         int     rval;
58
59         /* Clear adapter flags. */
60         ha->flags.online = 0;
61         ha->flags.reset_active = 0;
62         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
63         atomic_set(&ha->loop_state, LOOP_DOWN);
64         ha->device_flags = DFLG_NO_CABLE;
65         ha->dpc_flags = 0;
66         ha->flags.management_server_logged_in = 0;
67         ha->marker_needed = 0;
68         ha->mbx_flags = 0;
69         ha->isp_abort_cnt = 0;
70         ha->beacon_blink_led = 0;
71         set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
72
73         qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
74         rval = ha->isp_ops->pci_config(ha);
75         if (rval) {
76                 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
77                     ha->host_no));
78                 return (rval);
79         }
80
81         ha->isp_ops->reset_chip(ha);
82
83         ha->isp_ops->get_flash_version(ha, ha->request_ring);
84
85         qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
86
87         ha->isp_ops->nvram_config(ha);
88
89         if (ha->flags.disable_serdes) {
90                 /* Mask HBA via NVRAM settings? */
91                 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
92                     "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
93                     ha->port_name[0], ha->port_name[1],
94                     ha->port_name[2], ha->port_name[3],
95                     ha->port_name[4], ha->port_name[5],
96                     ha->port_name[6], ha->port_name[7]);
97                 return QLA_FUNCTION_FAILED;
98         }
99
100         qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
101
102         if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
103                 rval = ha->isp_ops->chip_diag(ha);
104                 if (rval)
105                         return (rval);
106                 rval = qla2x00_setup_chip(ha);
107                 if (rval)
108                         return (rval);
109                 qla2xxx_get_flash_info(ha);
110         }
111         rval = qla2x00_init_rings(ha);
112
113         return (rval);
114 }
115
116 /**
117  * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
118  * @ha: HA context
119  *
120  * Returns 0 on success.
121  */
122 int
123 qla2100_pci_config(scsi_qla_host_t *ha)
124 {
125         uint16_t w;
126         uint32_t d;
127         unsigned long flags;
128         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
129
130         pci_set_master(ha->pdev);
131         pci_try_set_mwi(ha->pdev);
132
133         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
134         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
135         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
136
137         /* Reset expansion ROM address decode enable */
138         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
139         d &= ~PCI_ROM_ADDRESS_ENABLE;
140         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
141
142         /* Get PCI bus information. */
143         spin_lock_irqsave(&ha->hardware_lock, flags);
144         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
145         spin_unlock_irqrestore(&ha->hardware_lock, flags);
146
147         return QLA_SUCCESS;
148 }
149
150 /**
151  * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
152  * @ha: HA context
153  *
154  * Returns 0 on success.
155  */
156 int
157 qla2300_pci_config(scsi_qla_host_t *ha)
158 {
159         uint16_t        w;
160         uint32_t        d;
161         unsigned long   flags = 0;
162         uint32_t        cnt;
163         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
164
165         pci_set_master(ha->pdev);
166         pci_try_set_mwi(ha->pdev);
167
168         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
169         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
170
171         if (IS_QLA2322(ha) || IS_QLA6322(ha))
172                 w &= ~PCI_COMMAND_INTX_DISABLE;
173         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
174
175         /*
176          * If this is a 2300 card and not 2312, reset the
177          * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
178          * the 2310 also reports itself as a 2300 so we need to get the
179          * fb revision level -- a 6 indicates it really is a 2300 and
180          * not a 2310.
181          */
182         if (IS_QLA2300(ha)) {
183                 spin_lock_irqsave(&ha->hardware_lock, flags);
184
185                 /* Pause RISC. */
186                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
187                 for (cnt = 0; cnt < 30000; cnt++) {
188                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
189                                 break;
190
191                         udelay(10);
192                 }
193
194                 /* Select FPM registers. */
195                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
196                 RD_REG_WORD(&reg->ctrl_status);
197
198                 /* Get the fb rev level */
199                 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
200
201                 if (ha->fb_rev == FPM_2300)
202                         pci_clear_mwi(ha->pdev);
203
204                 /* Deselect FPM registers. */
205                 WRT_REG_WORD(&reg->ctrl_status, 0x0);
206                 RD_REG_WORD(&reg->ctrl_status);
207
208                 /* Release RISC module. */
209                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
210                 for (cnt = 0; cnt < 30000; cnt++) {
211                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
212                                 break;
213
214                         udelay(10);
215                 }
216
217                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
218         }
219
220         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
221
222         /* Reset expansion ROM address decode enable */
223         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
224         d &= ~PCI_ROM_ADDRESS_ENABLE;
225         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
226
227         /* Get PCI bus information. */
228         spin_lock_irqsave(&ha->hardware_lock, flags);
229         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
230         spin_unlock_irqrestore(&ha->hardware_lock, flags);
231
232         return QLA_SUCCESS;
233 }
234
235 /**
236  * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
237  * @ha: HA context
238  *
239  * Returns 0 on success.
240  */
241 int
242 qla24xx_pci_config(scsi_qla_host_t *ha)
243 {
244         uint16_t w;
245         uint32_t d;
246         unsigned long flags = 0;
247         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
248
249         pci_set_master(ha->pdev);
250         pci_try_set_mwi(ha->pdev);
251
252         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
253         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
254         w &= ~PCI_COMMAND_INTX_DISABLE;
255         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
256
257         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
258
259         /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
260         if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
261                 pcix_set_mmrbc(ha->pdev, 2048);
262
263         /* PCIe -- adjust Maximum Read Request Size (2048). */
264         if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
265                 pcie_set_readrq(ha->pdev, 2048);
266
267         /* Reset expansion ROM address decode enable */
268         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
269         d &= ~PCI_ROM_ADDRESS_ENABLE;
270         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
271
272         ha->chip_revision = ha->pdev->revision;
273
274         /* Get PCI bus information. */
275         spin_lock_irqsave(&ha->hardware_lock, flags);
276         ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
277         spin_unlock_irqrestore(&ha->hardware_lock, flags);
278
279         return QLA_SUCCESS;
280 }
281
282 /**
283  * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
284  * @ha: HA context
285  *
286  * Returns 0 on success.
287  */
288 int
289 qla25xx_pci_config(scsi_qla_host_t *ha)
290 {
291         uint16_t w;
292         uint32_t d;
293
294         pci_set_master(ha->pdev);
295         pci_try_set_mwi(ha->pdev);
296
297         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
298         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
299         w &= ~PCI_COMMAND_INTX_DISABLE;
300         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
301
302         /* PCIe -- adjust Maximum Read Request Size (2048). */
303         if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
304                 pcie_set_readrq(ha->pdev, 2048);
305
306         /* Reset expansion ROM address decode enable */
307         pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
308         d &= ~PCI_ROM_ADDRESS_ENABLE;
309         pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
310
311         ha->chip_revision = ha->pdev->revision;
312
313         return QLA_SUCCESS;
314 }
315
316 /**
317  * qla2x00_isp_firmware() - Choose firmware image.
318  * @ha: HA context
319  *
320  * Returns 0 on success.
321  */
322 static int
323 qla2x00_isp_firmware(scsi_qla_host_t *ha)
324 {
325         int  rval;
326
327         /* Assume loading risc code */
328         rval = QLA_FUNCTION_FAILED;
329
330         if (ha->flags.disable_risc_code_load) {
331                 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
332                     ha->host_no));
333                 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
334
335                 /* Verify checksum of loaded RISC code. */
336                 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
337         }
338
339         if (rval) {
340                 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
341                     ha->host_no));
342         }
343
344         return (rval);
345 }
346
347 /**
348  * qla2x00_reset_chip() - Reset ISP chip.
349  * @ha: HA context
350  *
351  * Returns 0 on success.
352  */
353 void
354 qla2x00_reset_chip(scsi_qla_host_t *ha)
355 {
356         unsigned long   flags = 0;
357         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
358         uint32_t        cnt;
359         uint16_t        cmd;
360
361         ha->isp_ops->disable_intrs(ha);
362
363         spin_lock_irqsave(&ha->hardware_lock, flags);
364
365         /* Turn off master enable */
366         cmd = 0;
367         pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
368         cmd &= ~PCI_COMMAND_MASTER;
369         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
370
371         if (!IS_QLA2100(ha)) {
372                 /* Pause RISC. */
373                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
374                 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
375                         for (cnt = 0; cnt < 30000; cnt++) {
376                                 if ((RD_REG_WORD(&reg->hccr) &
377                                     HCCR_RISC_PAUSE) != 0)
378                                         break;
379                                 udelay(100);
380                         }
381                 } else {
382                         RD_REG_WORD(&reg->hccr);        /* PCI Posting. */
383                         udelay(10);
384                 }
385
386                 /* Select FPM registers. */
387                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
388                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
389
390                 /* FPM Soft Reset. */
391                 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
392                 RD_REG_WORD(&reg->fpm_diag_config);     /* PCI Posting. */
393
394                 /* Toggle Fpm Reset. */
395                 if (!IS_QLA2200(ha)) {
396                         WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
397                         RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
398                 }
399
400                 /* Select frame buffer registers. */
401                 WRT_REG_WORD(&reg->ctrl_status, 0x10);
402                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
403
404                 /* Reset frame buffer FIFOs. */
405                 if (IS_QLA2200(ha)) {
406                         WRT_FB_CMD_REG(ha, reg, 0xa000);
407                         RD_FB_CMD_REG(ha, reg);         /* PCI Posting. */
408                 } else {
409                         WRT_FB_CMD_REG(ha, reg, 0x00fc);
410
411                         /* Read back fb_cmd until zero or 3 seconds max */
412                         for (cnt = 0; cnt < 3000; cnt++) {
413                                 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
414                                         break;
415                                 udelay(100);
416                         }
417                 }
418
419                 /* Select RISC module registers. */
420                 WRT_REG_WORD(&reg->ctrl_status, 0);
421                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
422
423                 /* Reset RISC processor. */
424                 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
425                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
426
427                 /* Release RISC processor. */
428                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
429                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
430         }
431
432         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
433         WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
434
435         /* Reset ISP chip. */
436         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
437
438         /* Wait for RISC to recover from reset. */
439         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
440                 /*
441                  * It is necessary to for a delay here since the card doesn't
442                  * respond to PCI reads during a reset. On some architectures
443                  * this will result in an MCA.
444                  */
445                 udelay(20);
446                 for (cnt = 30000; cnt; cnt--) {
447                         if ((RD_REG_WORD(&reg->ctrl_status) &
448                             CSR_ISP_SOFT_RESET) == 0)
449                                 break;
450                         udelay(100);
451                 }
452         } else
453                 udelay(10);
454
455         /* Reset RISC processor. */
456         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
457
458         WRT_REG_WORD(&reg->semaphore, 0);
459
460         /* Release RISC processor. */
461         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
462         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
463
464         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
465                 for (cnt = 0; cnt < 30000; cnt++) {
466                         if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
467                                 break;
468
469                         udelay(100);
470                 }
471         } else
472                 udelay(100);
473
474         /* Turn on master enable */
475         cmd |= PCI_COMMAND_MASTER;
476         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
477
478         /* Disable RISC pause on FPM parity error. */
479         if (!IS_QLA2100(ha)) {
480                 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
481                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
482         }
483
484         spin_unlock_irqrestore(&ha->hardware_lock, flags);
485 }
486
487 /**
488  * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
489  * @ha: HA context
490  *
491  * Returns 0 on success.
492  */
493 static inline void
494 qla24xx_reset_risc(scsi_qla_host_t *ha)
495 {
496         int hw_evt = 0;
497         unsigned long flags = 0;
498         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
499         uint32_t cnt, d2;
500         uint16_t wd;
501
502         spin_lock_irqsave(&ha->hardware_lock, flags);
503
504         /* Reset RISC. */
505         WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
506         for (cnt = 0; cnt < 30000; cnt++) {
507                 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
508                         break;
509
510                 udelay(10);
511         }
512
513         WRT_REG_DWORD(&reg->ctrl_status,
514             CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
515         pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
516
517         udelay(100);
518         /* Wait for firmware to complete NVRAM accesses. */
519         d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
520         for (cnt = 10000 ; cnt && d2; cnt--) {
521                 udelay(5);
522                 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
523                 barrier();
524         }
525         if (cnt == 0)
526                 hw_evt = 1;
527
528         /* Wait for soft-reset to complete. */
529         d2 = RD_REG_DWORD(&reg->ctrl_status);
530         for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
531                 udelay(5);
532                 d2 = RD_REG_DWORD(&reg->ctrl_status);
533                 barrier();
534         }
535         if (cnt == 0 || hw_evt)
536                 qla2xxx_hw_event_log(ha, HW_EVENT_RESET_ERR,
537                     RD_REG_WORD(&reg->mailbox1), RD_REG_WORD(&reg->mailbox2),
538                     RD_REG_WORD(&reg->mailbox3));
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                 if (chksum)
1559                         qla2xxx_hw_event_log(ha, HW_EVENT_NVRAM_CHKSUM_ERR, 0,
1560                             MSW(chksum), LSW(chksum));
1561
1562                 /*
1563                  * Set default initialization control block.
1564                  */
1565                 memset(nv, 0, ha->nvram_size);
1566                 nv->parameter_block_version = ICB_VERSION;
1567
1568                 if (IS_QLA23XX(ha)) {
1569                         nv->firmware_options[0] = BIT_2 | BIT_1;
1570                         nv->firmware_options[1] = BIT_7 | BIT_5;
1571                         nv->add_firmware_options[0] = BIT_5;
1572                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
1573                         nv->frame_payload_size = __constant_cpu_to_le16(2048);
1574                         nv->special_options[1] = BIT_7;
1575                 } else if (IS_QLA2200(ha)) {
1576                         nv->firmware_options[0] = BIT_2 | BIT_1;
1577                         nv->firmware_options[1] = BIT_7 | BIT_5;
1578                         nv->add_firmware_options[0] = BIT_5;
1579                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
1580                         nv->frame_payload_size = __constant_cpu_to_le16(1024);
1581                 } else if (IS_QLA2100(ha)) {
1582                         nv->firmware_options[0] = BIT_3 | BIT_1;
1583                         nv->firmware_options[1] = BIT_5;
1584                         nv->frame_payload_size = __constant_cpu_to_le16(1024);
1585                 }
1586
1587                 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1588                 nv->execution_throttle = __constant_cpu_to_le16(16);
1589                 nv->retry_count = 8;
1590                 nv->retry_delay = 1;
1591
1592                 nv->port_name[0] = 33;
1593                 nv->port_name[3] = 224;
1594                 nv->port_name[4] = 139;
1595
1596                 qla2xxx_nvram_wwn_from_ofw(ha, nv);
1597
1598                 nv->login_timeout = 4;
1599
1600                 /*
1601                  * Set default host adapter parameters
1602                  */
1603                 nv->host_p[1] = BIT_2;
1604                 nv->reset_delay = 5;
1605                 nv->port_down_retry_count = 8;
1606                 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1607                 nv->link_down_timeout = 60;
1608
1609                 rval = 1;
1610         }
1611
1612 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1613         /*
1614          * The SN2 does not provide BIOS emulation which means you can't change
1615          * potentially bogus BIOS settings. Force the use of default settings
1616          * for link rate and frame size.  Hope that the rest of the settings
1617          * are valid.
1618          */
1619         if (ia64_platform_is("sn2")) {
1620                 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1621                 if (IS_QLA23XX(ha))
1622                         nv->special_options[1] = BIT_7;
1623         }
1624 #endif
1625
1626         /* Reset Initialization control block */
1627         memset(icb, 0, ha->init_cb_size);
1628
1629         /*
1630          * Setup driver NVRAM options.
1631          */
1632         nv->firmware_options[0] |= (BIT_6 | BIT_1);
1633         nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1634         nv->firmware_options[1] |= (BIT_5 | BIT_0);
1635         nv->firmware_options[1] &= ~BIT_4;
1636
1637         if (IS_QLA23XX(ha)) {
1638                 nv->firmware_options[0] |= BIT_2;
1639                 nv->firmware_options[0] &= ~BIT_3;
1640                 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
1641
1642                 if (IS_QLA2300(ha)) {
1643                         if (ha->fb_rev == FPM_2310) {
1644                                 strcpy(ha->model_number, "QLA2310");
1645                         } else {
1646                                 strcpy(ha->model_number, "QLA2300");
1647                         }
1648                 } else {
1649                         qla2x00_set_model_info(ha, nv->model_number,
1650                             sizeof(nv->model_number), "QLA23xx");
1651                 }
1652         } else if (IS_QLA2200(ha)) {
1653                 nv->firmware_options[0] |= BIT_2;
1654                 /*
1655                  * 'Point-to-point preferred, else loop' is not a safe
1656                  * connection mode setting.
1657                  */
1658                 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1659                     (BIT_5 | BIT_4)) {
1660                         /* Force 'loop preferred, else point-to-point'. */
1661                         nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1662                         nv->add_firmware_options[0] |= BIT_5;
1663                 }
1664                 strcpy(ha->model_number, "QLA22xx");
1665         } else /*if (IS_QLA2100(ha))*/ {
1666                 strcpy(ha->model_number, "QLA2100");
1667         }
1668
1669         /*
1670          * Copy over NVRAM RISC parameter block to initialization control block.
1671          */
1672         dptr1 = (uint8_t *)icb;
1673         dptr2 = (uint8_t *)&nv->parameter_block_version;
1674         cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1675         while (cnt--)
1676                 *dptr1++ = *dptr2++;
1677
1678         /* Copy 2nd half. */
1679         dptr1 = (uint8_t *)icb->add_firmware_options;
1680         cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1681         while (cnt--)
1682                 *dptr1++ = *dptr2++;
1683
1684         /* Use alternate WWN? */
1685         if (nv->host_p[1] & BIT_7) {
1686                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1687                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1688         }
1689
1690         /* Prepare nodename */
1691         if ((icb->firmware_options[1] & BIT_6) == 0) {
1692                 /*
1693                  * Firmware will apply the following mask if the nodename was
1694                  * not provided.
1695                  */
1696                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1697                 icb->node_name[0] &= 0xF0;
1698         }
1699
1700         /*
1701          * Set host adapter parameters.
1702          */
1703         if (nv->host_p[0] & BIT_7)
1704                 ql2xextended_error_logging = 1;
1705         ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1706         /* Always load RISC code on non ISP2[12]00 chips. */
1707         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1708                 ha->flags.disable_risc_code_load = 0;
1709         ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1710         ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1711         ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
1712         ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
1713         ha->flags.disable_serdes = 0;
1714
1715         ha->operating_mode =
1716             (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1717
1718         memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1719             sizeof(ha->fw_seriallink_options));
1720
1721         /* save HBA serial number */
1722         ha->serial0 = icb->port_name[5];
1723         ha->serial1 = icb->port_name[6];
1724         ha->serial2 = icb->port_name[7];
1725         ha->node_name = icb->node_name;
1726         ha->port_name = icb->port_name;
1727
1728         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1729
1730         ha->retry_count = nv->retry_count;
1731
1732         /* Set minimum login_timeout to 4 seconds. */
1733         if (nv->login_timeout < ql2xlogintimeout)
1734                 nv->login_timeout = ql2xlogintimeout;
1735         if (nv->login_timeout < 4)
1736                 nv->login_timeout = 4;
1737         ha->login_timeout = nv->login_timeout;
1738         icb->login_timeout = nv->login_timeout;
1739
1740         /* Set minimum RATOV to 100 tenths of a second. */
1741         ha->r_a_tov = 100;
1742
1743         ha->loop_reset_delay = nv->reset_delay;
1744
1745         /* Link Down Timeout = 0:
1746          *
1747          *      When Port Down timer expires we will start returning
1748          *      I/O's to OS with "DID_NO_CONNECT".
1749          *
1750          * Link Down Timeout != 0:
1751          *
1752          *       The driver waits for the link to come up after link down
1753          *       before returning I/Os to OS with "DID_NO_CONNECT".
1754          */
1755         if (nv->link_down_timeout == 0) {
1756                 ha->loop_down_abort_time =
1757                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
1758         } else {
1759                 ha->link_down_timeout =  nv->link_down_timeout;
1760                 ha->loop_down_abort_time =
1761                     (LOOP_DOWN_TIME - ha->link_down_timeout);
1762         }
1763
1764         /*
1765          * Need enough time to try and get the port back.
1766          */
1767         ha->port_down_retry_count = nv->port_down_retry_count;
1768         if (qlport_down_retry)
1769                 ha->port_down_retry_count = qlport_down_retry;
1770         /* Set login_retry_count */
1771         ha->login_retry_count  = nv->retry_count;
1772         if (ha->port_down_retry_count == nv->port_down_retry_count &&
1773             ha->port_down_retry_count > 3)
1774                 ha->login_retry_count = ha->port_down_retry_count;
1775         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1776                 ha->login_retry_count = ha->port_down_retry_count;
1777         if (ql2xloginretrycount)
1778                 ha->login_retry_count = ql2xloginretrycount;
1779
1780         icb->lun_enables = __constant_cpu_to_le16(0);
1781         icb->command_resource_count = 0;
1782         icb->immediate_notify_resource_count = 0;
1783         icb->timeout = __constant_cpu_to_le16(0);
1784
1785         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1786                 /* Enable RIO */
1787                 icb->firmware_options[0] &= ~BIT_3;
1788                 icb->add_firmware_options[0] &=
1789                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1790                 icb->add_firmware_options[0] |= BIT_2;
1791                 icb->response_accumulation_timer = 3;
1792                 icb->interrupt_delay_timer = 5;
1793
1794                 ha->flags.process_response_queue = 1;
1795         } else {
1796                 /* Enable ZIO. */
1797                 if (!ha->flags.init_done) {
1798                         ha->zio_mode = icb->add_firmware_options[0] &
1799                             (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1800                         ha->zio_timer = icb->interrupt_delay_timer ?
1801                             icb->interrupt_delay_timer: 2;
1802                 }
1803                 icb->add_firmware_options[0] &=
1804                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1805                 ha->flags.process_response_queue = 0;
1806                 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1807                         ha->zio_mode = QLA_ZIO_MODE_6;
1808
1809                         DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1810                             "delay (%d us).\n", ha->host_no, ha->zio_mode,
1811                             ha->zio_timer * 100));
1812                         qla_printk(KERN_INFO, ha,
1813                             "ZIO mode %d enabled; timer delay (%d us).\n",
1814                             ha->zio_mode, ha->zio_timer * 100);
1815
1816                         icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1817                         icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
1818                         ha->flags.process_response_queue = 1;
1819                 }
1820         }
1821
1822         if (rval) {
1823                 DEBUG2_3(printk(KERN_WARNING
1824                     "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1825         }
1826         return (rval);
1827 }
1828
1829 static void
1830 qla2x00_rport_del(void *data)
1831 {
1832         fc_port_t *fcport = data;
1833         struct fc_rport *rport;
1834         unsigned long flags;
1835
1836         spin_lock_irqsave(&fcport->rport_lock, flags);
1837         rport = fcport->drport;
1838         fcport->drport = NULL;
1839         spin_unlock_irqrestore(&fcport->rport_lock, flags);
1840         if (rport)
1841                 fc_remote_port_delete(rport);
1842 }
1843
1844 /**
1845  * qla2x00_alloc_fcport() - Allocate a generic fcport.
1846  * @ha: HA context
1847  * @flags: allocation flags
1848  *
1849  * Returns a pointer to the allocated fcport, or NULL, if none available.
1850  */
1851 static fc_port_t *
1852 qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
1853 {
1854         fc_port_t *fcport;
1855
1856         fcport = kzalloc(sizeof(fc_port_t), flags);
1857         if (!fcport)
1858                 return NULL;
1859
1860         /* Setup fcport template structure. */
1861         fcport->ha = ha;
1862         fcport->vp_idx = ha->vp_idx;
1863         fcport->port_type = FCT_UNKNOWN;
1864         fcport->loop_id = FC_NO_LOOP_ID;
1865         atomic_set(&fcport->state, FCS_UNCONFIGURED);
1866         fcport->flags = FCF_RLC_SUPPORT;
1867         fcport->supported_classes = FC_COS_UNSPECIFIED;
1868         spin_lock_init(&fcport->rport_lock);
1869
1870         return fcport;
1871 }
1872
1873 /*
1874  * qla2x00_configure_loop
1875  *      Updates Fibre Channel Device Database with what is actually on loop.
1876  *
1877  * Input:
1878  *      ha                = adapter block pointer.
1879  *
1880  * Returns:
1881  *      0 = success.
1882  *      1 = error.
1883  *      2 = database was full and device was not configured.
1884  */
1885 static int
1886 qla2x00_configure_loop(scsi_qla_host_t *ha)
1887 {
1888         int  rval;
1889         unsigned long flags, save_flags;
1890
1891         rval = QLA_SUCCESS;
1892
1893         /* Get Initiator ID */
1894         if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1895                 rval = qla2x00_configure_hba(ha);
1896                 if (rval != QLA_SUCCESS) {
1897                         DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1898                             ha->host_no));
1899                         return (rval);
1900                 }
1901         }
1902
1903         save_flags = flags = ha->dpc_flags;
1904         DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1905             ha->host_no, flags));
1906
1907         /*
1908          * If we have both an RSCN and PORT UPDATE pending then handle them
1909          * both at the same time.
1910          */
1911         clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1912         clear_bit(RSCN_UPDATE, &ha->dpc_flags);
1913
1914         /* Determine what we need to do */
1915         if (ha->current_topology == ISP_CFG_FL &&
1916             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1917
1918                 ha->flags.rscn_queue_overflow = 1;
1919                 set_bit(RSCN_UPDATE, &flags);
1920
1921         } else if (ha->current_topology == ISP_CFG_F &&
1922             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1923
1924                 ha->flags.rscn_queue_overflow = 1;
1925                 set_bit(RSCN_UPDATE, &flags);
1926                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1927
1928         } else if (ha->current_topology == ISP_CFG_N) {
1929                 clear_bit(RSCN_UPDATE, &flags);
1930
1931         } else if (!ha->flags.online ||
1932             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1933
1934                 ha->flags.rscn_queue_overflow = 1;
1935                 set_bit(RSCN_UPDATE, &flags);
1936                 set_bit(LOCAL_LOOP_UPDATE, &flags);
1937         }
1938
1939         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1940                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1941                         rval = QLA_FUNCTION_FAILED;
1942                 } else {
1943                         rval = qla2x00_configure_local_loop(ha);
1944                 }
1945         }
1946
1947         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
1948                 if (LOOP_TRANSITION(ha)) {
1949                         rval = QLA_FUNCTION_FAILED;
1950                 } else {
1951                         rval = qla2x00_configure_fabric(ha);
1952                 }
1953         }
1954
1955         if (rval == QLA_SUCCESS) {
1956                 if (atomic_read(&ha->loop_down_timer) ||
1957                     test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1958                         rval = QLA_FUNCTION_FAILED;
1959                 } else {
1960                         atomic_set(&ha->loop_state, LOOP_READY);
1961
1962                         DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1963                 }
1964         }
1965
1966         if (rval) {
1967                 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1968                     __func__, ha->host_no));
1969         } else {
1970                 DEBUG3(printk("%s: exiting normally\n", __func__));
1971         }
1972
1973         /* Restore state if a resync event occured during processing */
1974         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1975                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1976                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1977                 if (test_bit(RSCN_UPDATE, &save_flags))
1978                         set_bit(RSCN_UPDATE, &ha->dpc_flags);
1979         }
1980
1981         return (rval);
1982 }
1983
1984
1985
1986 /*
1987  * qla2x00_configure_local_loop
1988  *      Updates Fibre Channel Device Database with local loop devices.
1989  *
1990  * Input:
1991  *      ha = adapter block pointer.
1992  *
1993  * Returns:
1994  *      0 = success.
1995  */
1996 static int
1997 qla2x00_configure_local_loop(scsi_qla_host_t *ha)
1998 {
1999         int             rval, rval2;
2000         int             found_devs;
2001         int             found;
2002         fc_port_t       *fcport, *new_fcport;
2003
2004         uint16_t        index;
2005         uint16_t        entries;
2006         char            *id_iter;
2007         uint16_t        loop_id;
2008         uint8_t         domain, area, al_pa;
2009         scsi_qla_host_t *pha = to_qla_parent(ha);
2010
2011         found_devs = 0;
2012         new_fcport = NULL;
2013         entries = MAX_FIBRE_DEVICES;
2014
2015         DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
2016         DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
2017
2018         /* Get list of logged in devices. */
2019         memset(ha->gid_list, 0, GID_LIST_SIZE);
2020         rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
2021             &entries);
2022         if (rval != QLA_SUCCESS)
2023                 goto cleanup_allocation;
2024
2025         DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
2026             ha->host_no, entries));
2027         DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2028             entries * sizeof(struct gid_list_info)));
2029
2030         /* Allocate temporary fcport for any new fcports discovered. */
2031         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2032         if (new_fcport == NULL) {
2033                 rval = QLA_MEMORY_ALLOC_FAILED;
2034                 goto cleanup_allocation;
2035         }
2036         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2037
2038         /*
2039          * Mark local devices that were present with FCF_DEVICE_LOST for now.
2040          */
2041         list_for_each_entry(fcport, &pha->fcports, list) {
2042                 if (fcport->vp_idx != ha->vp_idx)
2043                         continue;
2044
2045                 if (atomic_read(&fcport->state) == FCS_ONLINE &&
2046                     fcport->port_type != FCT_BROADCAST &&
2047                     (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2048
2049                         DEBUG(printk("scsi(%ld): Marking port lost, "
2050                             "loop_id=0x%04x\n",
2051                             ha->host_no, fcport->loop_id));
2052
2053                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
2054                         fcport->flags &= ~FCF_FARP_DONE;
2055                 }
2056         }
2057
2058         /* Add devices to port list. */
2059         id_iter = (char *)ha->gid_list;
2060         for (index = 0; index < entries; index++) {
2061                 domain = ((struct gid_list_info *)id_iter)->domain;
2062                 area = ((struct gid_list_info *)id_iter)->area;
2063                 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
2064                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
2065                         loop_id = (uint16_t)
2066                             ((struct gid_list_info *)id_iter)->loop_id_2100;
2067                 else
2068                         loop_id = le16_to_cpu(
2069                             ((struct gid_list_info *)id_iter)->loop_id);
2070                 id_iter += ha->gid_list_info_size;
2071
2072                 /* Bypass reserved domain fields. */
2073                 if ((domain & 0xf0) == 0xf0)
2074                         continue;
2075
2076                 /* Bypass if not same domain and area of adapter. */
2077                 if (area && domain &&
2078                     (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
2079                         continue;
2080
2081                 /* Bypass invalid local loop ID. */
2082                 if (loop_id > LAST_LOCAL_LOOP_ID)
2083                         continue;
2084
2085                 /* Fill in member data. */
2086                 new_fcport->d_id.b.domain = domain;
2087                 new_fcport->d_id.b.area = area;
2088                 new_fcport->d_id.b.al_pa = al_pa;
2089                 new_fcport->loop_id = loop_id;
2090                 new_fcport->vp_idx = ha->vp_idx;
2091                 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2092                 if (rval2 != QLA_SUCCESS) {
2093                         DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2094                             "information -- get_port_database=%x, "
2095                             "loop_id=0x%04x\n",
2096                             ha->host_no, rval2, new_fcport->loop_id));
2097                         DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2098                             ha->host_no));
2099                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2100                         continue;
2101                 }
2102
2103                 /* Check for matching device in port list. */
2104                 found = 0;
2105                 fcport = NULL;
2106                 list_for_each_entry(fcport, &pha->fcports, list) {
2107                         if (fcport->vp_idx != ha->vp_idx)
2108                                 continue;
2109
2110                         if (memcmp(new_fcport->port_name, fcport->port_name,
2111                             WWN_SIZE))
2112                                 continue;
2113
2114                         fcport->flags &= ~(FCF_FABRIC_DEVICE |
2115                             FCF_PERSISTENT_BOUND);
2116                         fcport->loop_id = new_fcport->loop_id;
2117                         fcport->port_type = new_fcport->port_type;
2118                         fcport->d_id.b24 = new_fcport->d_id.b24;
2119                         memcpy(fcport->node_name, new_fcport->node_name,
2120                             WWN_SIZE);
2121
2122                         found++;
2123                         break;
2124                 }
2125
2126                 if (!found) {
2127                         /* New device, add to fcports list. */
2128                         new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2129                         if (ha->parent) {
2130                                 new_fcport->ha = ha;
2131                                 new_fcport->vp_idx = ha->vp_idx;
2132                                 list_add_tail(&new_fcport->vp_fcport,
2133                                     &ha->vp_fcports);
2134                         }
2135                         list_add_tail(&new_fcport->list, &pha->fcports);
2136
2137                         /* Allocate a new replacement fcport. */
2138                         fcport = new_fcport;
2139                         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2140                         if (new_fcport == NULL) {
2141                                 rval = QLA_MEMORY_ALLOC_FAILED;
2142                                 goto cleanup_allocation;
2143                         }
2144                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2145                 }
2146
2147                 /* Base iIDMA settings on HBA port speed. */
2148                 fcport->fp_speed = ha->link_data_rate;
2149
2150                 qla2x00_update_fcport(ha, fcport);
2151
2152                 found_devs++;
2153         }
2154
2155 cleanup_allocation:
2156         kfree(new_fcport);
2157
2158         if (rval != QLA_SUCCESS) {
2159                 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2160                     "rval=%x\n", ha->host_no, rval));
2161         }
2162
2163         if (found_devs) {
2164                 ha->device_flags |= DFLG_LOCAL_DEVICES;
2165                 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2166         }
2167
2168         return (rval);
2169 }
2170
2171 static void
2172 qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2173 {
2174 #define LS_UNKNOWN      2
2175         static char *link_speeds[5] = { "1", "2", "?", "4", "8" };
2176         int rval;
2177         uint16_t mb[6];
2178
2179         if (!IS_IIDMA_CAPABLE(ha))
2180                 return;
2181
2182         if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2183             fcport->fp_speed > ha->link_data_rate)
2184                 return;
2185
2186         rval = qla2x00_set_idma_speed(ha, fcport->loop_id, fcport->fp_speed,
2187             mb);
2188         if (rval != QLA_SUCCESS) {
2189                 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2190                     "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2191                     ha->host_no, fcport->port_name[0], fcport->port_name[1],
2192                     fcport->port_name[2], fcport->port_name[3],
2193                     fcport->port_name[4], fcport->port_name[5],
2194                     fcport->port_name[6], fcport->port_name[7], rval,
2195                     fcport->fp_speed, mb[0], mb[1]));
2196         } else {
2197                 DEBUG2(qla_printk(KERN_INFO, ha,
2198                     "iIDMA adjusted to %s GB/s on "
2199                     "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2200                     link_speeds[fcport->fp_speed], fcport->port_name[0],
2201                     fcport->port_name[1], fcport->port_name[2],
2202                     fcport->port_name[3], fcport->port_name[4],
2203                     fcport->port_name[5], fcport->port_name[6],
2204                     fcport->port_name[7]));
2205         }
2206 }
2207
2208 static void
2209 qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2210 {
2211         struct fc_rport_identifiers rport_ids;
2212         struct fc_rport *rport;
2213         unsigned long flags;
2214
2215         if (fcport->drport)
2216                 qla2x00_rport_del(fcport);
2217         if (fcport->rport)
2218                 return;
2219
2220         rport_ids.node_name = wwn_to_u64(fcport->node_name);
2221         rport_ids.port_name = wwn_to_u64(fcport->port_name);
2222         rport_ids.port_id = fcport->d_id.b.domain << 16 |
2223             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2224         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2225         rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2226         if (!rport) {
2227                 qla_printk(KERN_WARNING, ha,
2228                     "Unable to allocate fc remote port!\n");
2229                 return;
2230         }
2231         spin_lock_irqsave(&fcport->rport_lock, flags);
2232         fcport->rport = rport;
2233         *((fc_port_t **)rport->dd_data) = fcport;
2234         spin_unlock_irqrestore(&fcport->rport_lock, flags);
2235
2236         rport->supported_classes = fcport->supported_classes;
2237
2238         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2239         if (fcport->port_type == FCT_INITIATOR)
2240                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2241         if (fcport->port_type == FCT_TARGET)
2242                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2243         fc_remote_port_rolechg(rport, rport_ids.roles);
2244 }
2245
2246 /*
2247  * qla2x00_update_fcport
2248  *      Updates device on list.
2249  *
2250  * Input:
2251  *      ha = adapter block pointer.
2252  *      fcport = port structure pointer.
2253  *
2254  * Return:
2255  *      0  - Success
2256  *  BIT_0 - error
2257  *
2258  * Context:
2259  *      Kernel context.
2260  */
2261 void
2262 qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2263 {
2264         scsi_qla_host_t *pha = to_qla_parent(ha);
2265
2266         fcport->ha = ha;
2267         fcport->login_retry = 0;
2268         fcport->port_login_retry_count = pha->port_down_retry_count *
2269             PORT_RETRY_TIME;
2270         atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
2271             PORT_RETRY_TIME);
2272         fcport->flags &= ~FCF_LOGIN_NEEDED;
2273
2274         qla2x00_iidma_fcport(ha, fcport);
2275
2276         atomic_set(&fcport->state, FCS_ONLINE);
2277
2278         qla2x00_reg_remote_port(ha, fcport);
2279 }
2280
2281 /*
2282  * qla2x00_configure_fabric
2283  *      Setup SNS devices with loop ID's.
2284  *
2285  * Input:
2286  *      ha = adapter block pointer.
2287  *
2288  * Returns:
2289  *      0 = success.
2290  *      BIT_0 = error
2291  */
2292 static int
2293 qla2x00_configure_fabric(scsi_qla_host_t *ha)
2294 {
2295         int     rval, rval2;
2296         fc_port_t       *fcport, *fcptemp;
2297         uint16_t        next_loopid;
2298         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2299         uint16_t        loop_id;
2300         LIST_HEAD(new_fcports);
2301         scsi_qla_host_t *pha = to_qla_parent(ha);
2302
2303         /* If FL port exists, then SNS is present */
2304         if (IS_FWI2_CAPABLE(ha))
2305                 loop_id = NPH_F_PORT;
2306         else
2307                 loop_id = SNS_FL_PORT;
2308         rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
2309         if (rval != QLA_SUCCESS) {
2310                 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2311                     "Port\n", ha->host_no));
2312
2313                 ha->device_flags &= ~SWITCH_FOUND;
2314                 return (QLA_SUCCESS);
2315         }
2316         ha->device_flags |= SWITCH_FOUND;
2317
2318         /* Mark devices that need re-synchronization. */
2319         rval2 = qla2x00_device_resync(ha);
2320         if (rval2 == QLA_RSCNS_HANDLED) {
2321                 /* No point doing the scan, just continue. */
2322                 return (QLA_SUCCESS);
2323         }
2324         do {
2325                 /* FDMI support. */
2326                 if (ql2xfdmienable &&
2327                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2328                         qla2x00_fdmi_register(ha);
2329
2330                 /* Ensure we are logged into the SNS. */
2331                 if (IS_FWI2_CAPABLE(ha))
2332                         loop_id = NPH_SNS;
2333                 else
2334                         loop_id = SIMPLE_NAME_SERVER;
2335                 ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
2336                     0xfc, mb, BIT_1 | BIT_0);
2337                 if (mb[0] != MBS_COMMAND_COMPLETE) {
2338                         DEBUG2(qla_printk(KERN_INFO, ha,
2339                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2340                             "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2341                             mb[0], mb[1], mb[2], mb[6], mb[7]));
2342                         return (QLA_SUCCESS);
2343                 }
2344
2345                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2346                         if (qla2x00_rft_id(ha)) {
2347                                 /* EMPTY */
2348                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2349                                     "TYPE failed.\n", ha->host_no));
2350                         }
2351                         if (qla2x00_rff_id(ha)) {
2352                                 /* EMPTY */
2353                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2354                                     "Features failed.\n", ha->host_no));
2355                         }
2356                         if (qla2x00_rnn_id(ha)) {
2357                                 /* EMPTY */
2358                                 DEBUG2(printk("scsi(%ld): Register Node Name "
2359                                     "failed.\n", ha->host_no));
2360                         } else if (qla2x00_rsnn_nn(ha)) {
2361                                 /* EMPTY */
2362                                 DEBUG2(printk("scsi(%ld): Register Symbolic "
2363                                     "Node Name failed.\n", ha->host_no));
2364                         }
2365                 }
2366
2367                 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2368                 if (rval != QLA_SUCCESS)
2369                         break;
2370
2371                 /*
2372                  * Logout all previous fabric devices marked lost, except
2373                  * tape devices.
2374                  */
2375                 list_for_each_entry(fcport, &pha->fcports, list) {
2376                         if (fcport->vp_idx !=ha->vp_idx)
2377                                 continue;
2378
2379                         if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2380                                 break;
2381
2382                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2383                                 continue;
2384
2385                         if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2386                                 qla2x00_mark_device_lost(ha, fcport,
2387                                     ql2xplogiabsentdevice, 0);
2388                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
2389                                     (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2390                                     fcport->port_type != FCT_INITIATOR &&
2391                                     fcport->port_type != FCT_BROADCAST) {
2392                                         ha->isp_ops->fabric_logout(ha,
2393                                             fcport->loop_id,
2394                                             fcport->d_id.b.domain,
2395                                             fcport->d_id.b.area,
2396                                             fcport->d_id.b.al_pa);
2397                                         fcport->loop_id = FC_NO_LOOP_ID;
2398                                 }
2399                         }
2400                 }
2401
2402                 /* Starting free loop ID. */
2403                 next_loopid = pha->min_external_loopid;
2404
2405                 /*
2406                  * Scan through our port list and login entries that need to be
2407                  * logged in.
2408                  */
2409                 list_for_each_entry(fcport, &pha->fcports, list) {
2410                         if (fcport->vp_idx != ha->vp_idx)
2411                                 continue;
2412
2413                         if (atomic_read(&ha->loop_down_timer) ||
2414                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2415                                 break;
2416
2417                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2418                             (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2419                                 continue;
2420
2421                         if (fcport->loop_id == FC_NO_LOOP_ID) {
2422                                 fcport->loop_id = next_loopid;
2423                                 rval = qla2x00_find_new_loop_id(ha, fcport);
2424                                 if (rval != QLA_SUCCESS) {
2425                                         /* Ran out of IDs to use */
2426                                         break;
2427                                 }
2428                         }
2429                         /* Login and update database */
2430                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2431                 }
2432
2433                 /* Exit if out of loop IDs. */
2434                 if (rval != QLA_SUCCESS) {
2435                         break;
2436                 }
2437
2438                 /*
2439                  * Login and add the new devices to our port list.
2440                  */
2441                 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2442                         if (atomic_read(&ha->loop_down_timer) ||
2443                             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2444                                 break;
2445
2446                         /* Find a new loop ID to use. */
2447                         fcport->loop_id = next_loopid;
2448                         rval = qla2x00_find_new_loop_id(ha, fcport);
2449                         if (rval != QLA_SUCCESS) {
2450                                 /* Ran out of IDs to use */
2451                                 break;
2452                         }
2453
2454                         /* Login and update database */
2455                         qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2456
2457                         if (ha->parent) {
2458                                 fcport->ha = ha;
2459                                 fcport->vp_idx = ha->vp_idx;
2460                                 list_add_tail(&fcport->vp_fcport,
2461                                     &ha->vp_fcports);
2462                                 list_move_tail(&fcport->list,
2463                                     &ha->parent->fcports);
2464                         } else
2465                                 list_move_tail(&fcport->list, &ha->fcports);
2466                 }
2467         } while (0);
2468
2469         /* Free all new device structures not processed. */
2470         list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2471                 list_del(&fcport->list);
2472                 kfree(fcport);
2473         }
2474
2475         if (rval) {
2476                 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2477                     "rval=%d\n", ha->host_no, rval));
2478         }
2479
2480         return (rval);
2481 }
2482
2483
2484 /*
2485  * qla2x00_find_all_fabric_devs
2486  *
2487  * Input:
2488  *      ha = adapter block pointer.
2489  *      dev = database device entry pointer.
2490  *
2491  * Returns:
2492  *      0 = success.
2493  *
2494  * Context:
2495  *      Kernel context.
2496  */
2497 static int
2498 qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2499 {
2500         int             rval;
2501         uint16_t        loop_id;
2502         fc_port_t       *fcport, *new_fcport, *fcptemp;
2503         int             found;
2504
2505         sw_info_t       *swl;
2506         int             swl_idx;
2507         int             first_dev, last_dev;
2508         port_id_t       wrap, nxt_d_id;
2509         int             vp_index;
2510         int             empty_vp_index;
2511         int             found_vp;
2512         scsi_qla_host_t *vha;
2513         scsi_qla_host_t *pha = to_qla_parent(ha);
2514
2515         rval = QLA_SUCCESS;
2516
2517         /* Try GID_PT to get device list, else GAN. */
2518         swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_ATOMIC);
2519         if (!swl) {
2520                 /*EMPTY*/
2521                 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2522                     "on GA_NXT\n", ha->host_no));
2523         } else {
2524                 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2525                         kfree(swl);
2526                         swl = NULL;
2527                 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2528                         kfree(swl);
2529                         swl = NULL;
2530                 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2531                         kfree(swl);
2532                         swl = NULL;
2533                 } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
2534                         qla2x00_gpsc(ha, swl);
2535                 }
2536         }
2537         swl_idx = 0;
2538
2539         /* Allocate temporary fcport for any new fcports discovered. */
2540         new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2541         if (new_fcport == NULL) {
2542                 kfree(swl);
2543                 return (QLA_MEMORY_ALLOC_FAILED);
2544         }
2545         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2546         new_fcport->vp_idx = ha->vp_idx;
2547         /* Set start port ID scan at adapter ID. */
2548         first_dev = 1;
2549         last_dev = 0;
2550
2551         /* Starting free loop ID. */
2552         loop_id = pha->min_external_loopid;
2553         for (; loop_id <= ha->last_loop_id; loop_id++) {
2554                 if (qla2x00_is_reserved_id(ha, loop_id))
2555                         continue;
2556
2557                 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
2558                         break;
2559
2560                 if (swl != NULL) {
2561                         if (last_dev) {
2562                                 wrap.b24 = new_fcport->d_id.b24;
2563                         } else {
2564                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2565                                 memcpy(new_fcport->node_name,
2566                                     swl[swl_idx].node_name, WWN_SIZE);
2567                                 memcpy(new_fcport->port_name,
2568                                     swl[swl_idx].port_name, WWN_SIZE);
2569                                 memcpy(new_fcport->fabric_port_name,
2570                                     swl[swl_idx].fabric_port_name, WWN_SIZE);
2571                                 new_fcport->fp_speed = swl[swl_idx].fp_speed;
2572
2573                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2574                                         last_dev = 1;
2575                                 }
2576                                 swl_idx++;
2577                         }
2578                 } else {
2579                         /* Send GA_NXT to the switch */
2580                         rval = qla2x00_ga_nxt(ha, new_fcport);
2581                         if (rval != QLA_SUCCESS) {
2582                                 qla_printk(KERN_WARNING, ha,
2583                                     "SNS scan failed -- assuming zero-entry "
2584                                     "result...\n");
2585                                 list_for_each_entry_safe(fcport, fcptemp,
2586                                     new_fcports, list) {
2587                                         list_del(&fcport->list);
2588                                         kfree(fcport);
2589                                 }
2590                                 rval = QLA_SUCCESS;
2591                                 break;
2592                         }
2593                 }
2594
2595                 /* If wrap on switch device list, exit. */
2596                 if (first_dev) {
2597                         wrap.b24 = new_fcport->d_id.b24;
2598                         first_dev = 0;
2599                 } else if (new_fcport->d_id.b24 == wrap.b24) {
2600                         DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2601                             ha->host_no, new_fcport->d_id.b.domain,
2602                             new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2603                         break;
2604                 }
2605
2606                 /* Bypass if same physical adapter. */
2607                 if (new_fcport->d_id.b24 == pha->d_id.b24)
2608                         continue;
2609
2610                 /* Bypass virtual ports of the same host. */
2611                 if (pha->num_vhosts) {
2612                         for_each_mapped_vp_idx(pha, vp_index) {
2613                                 empty_vp_index = 1;
2614                                 found_vp = 0;
2615                                 list_for_each_entry(vha, &pha->vp_list,
2616                                     vp_list) {
2617                                         if (vp_index == vha->vp_idx) {
2618                                                 empty_vp_index = 0;
2619                                                 found_vp = 1;
2620                                                 break;
2621                                         }
2622                                 }
2623
2624                                 if (empty_vp_index)
2625                                         continue;
2626
2627                                 if (found_vp &&
2628                                     new_fcport->d_id.b24 == vha->d_id.b24)
2629                                         break;
2630                         }
2631
2632                         if (vp_index <= pha->max_npiv_vports)
2633                                 continue;
2634                 }
2635
2636                 /* Bypass if same domain and area of adapter. */
2637                 if (((new_fcport->d_id.b24 & 0xffff00) ==
2638                     (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2639                         ISP_CFG_FL)
2640                             continue;
2641
2642                 /* Bypass reserved domain fields. */
2643                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2644                         continue;
2645
2646                 /* Locate matching device in database. */
2647                 found = 0;
2648                 list_for_each_entry(fcport, &pha->fcports, list) {
2649                         if (new_fcport->vp_idx != fcport->vp_idx)
2650                                 continue;
2651                         if (memcmp(new_fcport->port_name, fcport->port_name,
2652                             WWN_SIZE))
2653                                 continue;
2654
2655                         found++;
2656
2657                         /* Update port state. */
2658                         memcpy(fcport->fabric_port_name,
2659                             new_fcport->fabric_port_name, WWN_SIZE);
2660                         fcport->fp_speed = new_fcport->fp_speed;
2661
2662                         /*
2663                          * If address the same and state FCS_ONLINE, nothing
2664                          * changed.
2665                          */
2666                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2667                             atomic_read(&fcport->state) == FCS_ONLINE) {
2668                                 break;
2669                         }
2670
2671                         /*
2672                          * If device was not a fabric device before.
2673                          */
2674                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2675                                 fcport->d_id.b24 = new_fcport->d_id.b24;
2676                                 fcport->loop_id = FC_NO_LOOP_ID;
2677                                 fcport->flags |= (FCF_FABRIC_DEVICE |
2678                                     FCF_LOGIN_NEEDED);
2679                                 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2680                                 break;
2681                         }
2682
2683                         /*
2684                          * Port ID changed or device was marked to be updated;
2685                          * Log it out if still logged in and mark it for
2686                          * relogin later.
2687                          */
2688                         fcport->d_id.b24 = new_fcport->d_id.b24;
2689                         fcport->flags |= FCF_LOGIN_NEEDED;
2690                         if (fcport->loop_id != FC_NO_LOOP_ID &&
2691                             (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2692                             fcport->port_type != FCT_INITIATOR &&
2693                             fcport->port_type != FCT_BROADCAST) {
2694                                 ha->isp_ops->fabric_logout(ha, fcport->loop_id,
2695                                     fcport->d_id.b.domain, fcport->d_id.b.area,
2696                                     fcport->d_id.b.al_pa);
2697                                 fcport->loop_id = FC_NO_LOOP_ID;
2698                         }
2699
2700                         break;
2701                 }
2702
2703                 if (found)
2704                         continue;
2705
2706                 /* If device was not in our fcports list, then add it. */
2707                 list_add_tail(&new_fcport->list, new_fcports);
2708
2709                 /* Allocate a new replacement fcport. */
2710                 nxt_d_id.b24 = new_fcport->d_id.b24;
2711                 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2712                 if (new_fcport == NULL) {
2713                         kfree(swl);
2714                         return (QLA_MEMORY_ALLOC_FAILED);
2715                 }
2716                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2717                 new_fcport->d_id.b24 = nxt_d_id.b24;
2718                 new_fcport->vp_idx = ha->vp_idx;
2719         }
2720
2721         kfree(swl);
2722         kfree(new_fcport);
2723
2724         if (!list_empty(new_fcports))
2725                 ha->device_flags |= DFLG_FABRIC_DEVICES;
2726
2727         return (rval);
2728 }
2729
2730 /*
2731  * qla2x00_find_new_loop_id
2732  *      Scan through our port list and find a new usable loop ID.
2733  *
2734  * Input:
2735  *      ha:     adapter state pointer.
2736  *      dev:    port structure pointer.
2737  *
2738  * Returns:
2739  *      qla2x00 local function return status code.
2740  *
2741  * Context:
2742  *      Kernel context.
2743  */
2744 static int
2745 qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2746 {
2747         int     rval;
2748         int     found;
2749         fc_port_t *fcport;
2750         uint16_t first_loop_id;
2751         scsi_qla_host_t *pha = to_qla_parent(ha);
2752
2753         rval = QLA_SUCCESS;
2754
2755         /* Save starting loop ID. */
2756         first_loop_id = dev->loop_id;
2757
2758         for (;;) {
2759                 /* Skip loop ID if already used by adapter. */
2760                 if (dev->loop_id == ha->loop_id) {
2761                         dev->loop_id++;
2762                 }
2763
2764                 /* Skip reserved loop IDs. */
2765                 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
2766                         dev->loop_id++;
2767                 }
2768
2769                 /* Reset loop ID if passed the end. */
2770                 if (dev->loop_id > ha->last_loop_id) {
2771                         /* first loop ID. */
2772                         dev->loop_id = ha->min_external_loopid;
2773                 }
2774
2775                 /* Check for loop ID being already in use. */
2776                 found = 0;
2777                 fcport = NULL;
2778                 list_for_each_entry(fcport, &pha->fcports, list) {
2779                         if (fcport->loop_id == dev->loop_id && fcport != dev) {
2780                                 /* ID possibly in use */
2781                                 found++;
2782                                 break;
2783                         }
2784                 }
2785
2786                 /* If not in use then it is free to use. */
2787                 if (!found) {
2788                         break;
2789                 }
2790
2791                 /* ID in use. Try next value. */
2792                 dev->loop_id++;
2793
2794                 /* If wrap around. No free ID to use. */
2795                 if (dev->loop_id == first_loop_id) {
2796                         dev->loop_id = FC_NO_LOOP_ID;
2797                         rval = QLA_FUNCTION_FAILED;
2798                         break;
2799                 }
2800         }
2801
2802         return (rval);
2803 }
2804
2805 /*
2806  * qla2x00_device_resync
2807  *      Marks devices in the database that needs resynchronization.
2808  *
2809  * Input:
2810  *      ha = adapter block pointer.
2811  *
2812  * Context:
2813  *      Kernel context.
2814  */
2815 static int
2816 qla2x00_device_resync(scsi_qla_host_t *ha)
2817 {
2818         int     rval;
2819         uint32_t mask;
2820         fc_port_t *fcport;
2821         uint32_t rscn_entry;
2822         uint8_t rscn_out_iter;
2823         uint8_t format;
2824         port_id_t d_id;
2825         scsi_qla_host_t *pha = to_qla_parent(ha);
2826
2827         rval = QLA_RSCNS_HANDLED;
2828
2829         while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2830             ha->flags.rscn_queue_overflow) {
2831
2832                 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2833                 format = MSB(MSW(rscn_entry));
2834                 d_id.b.domain = LSB(MSW(rscn_entry));
2835                 d_id.b.area = MSB(LSW(rscn_entry));
2836                 d_id.b.al_pa = LSB(LSW(rscn_entry));
2837
2838                 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2839                     "[%02x/%02x%02x%02x].\n",
2840                     ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2841                     d_id.b.area, d_id.b.al_pa));
2842
2843                 ha->rscn_out_ptr++;
2844                 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2845                         ha->rscn_out_ptr = 0;
2846
2847                 /* Skip duplicate entries. */
2848                 for (rscn_out_iter = ha->rscn_out_ptr;
2849                     !ha->flags.rscn_queue_overflow &&
2850                     rscn_out_iter != ha->rscn_in_ptr;
2851                     rscn_out_iter = (rscn_out_iter ==
2852                         (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2853
2854                         if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2855                                 break;
2856
2857                         DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2858                             "entry found at [%d].\n", ha->host_no,
2859                             rscn_out_iter));
2860
2861                         ha->rscn_out_ptr = rscn_out_iter;
2862                 }
2863
2864                 /* Queue overflow, set switch default case. */
2865                 if (ha->flags.rscn_queue_overflow) {
2866                         DEBUG(printk("scsi(%ld): device_resync: rscn "
2867                             "overflow.\n", ha->host_no));
2868
2869                         format = 3;
2870                         ha->flags.rscn_queue_overflow = 0;
2871                 }
2872
2873                 switch (format) {
2874                 case 0:
2875                         mask = 0xffffff;
2876                         break;
2877                 case 1:
2878                         mask = 0xffff00;
2879                         break;
2880                 case 2:
2881                         mask = 0xff0000;
2882                         break;
2883                 default:
2884                         mask = 0x0;
2885                         d_id.b24 = 0;
2886                         ha->rscn_out_ptr = ha->rscn_in_ptr;
2887                         break;
2888                 }
2889
2890                 rval = QLA_SUCCESS;
2891
2892                 list_for_each_entry(fcport, &pha->fcports, list) {
2893                         if (fcport->vp_idx != ha->vp_idx)
2894                                 continue;
2895
2896                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2897                             (fcport->d_id.b24 & mask) != d_id.b24 ||
2898                             fcport->port_type == FCT_BROADCAST)
2899                                 continue;
2900
2901                         if (atomic_read(&fcport->state) == FCS_ONLINE) {
2902                                 if (format != 3 ||
2903                                     fcport->port_type != FCT_INITIATOR) {
2904                                         qla2x00_mark_device_lost(ha, fcport,
2905                                             0, 0);
2906                                 }
2907                         }
2908                         fcport->flags &= ~FCF_FARP_DONE;
2909                 }
2910         }
2911         return (rval);
2912 }
2913
2914 /*
2915  * qla2x00_fabric_dev_login
2916  *      Login fabric target device and update FC port database.
2917  *
2918  * Input:
2919  *      ha:             adapter state pointer.
2920  *      fcport:         port structure list pointer.
2921  *      next_loopid:    contains value of a new loop ID that can be used
2922  *                      by the next login attempt.
2923  *
2924  * Returns:
2925  *      qla2x00 local function return status code.
2926  *
2927  * Context:
2928  *      Kernel context.
2929  */
2930 static int
2931 qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2932     uint16_t *next_loopid)
2933 {
2934         int     rval;
2935         int     retry;
2936         uint8_t opts;
2937
2938         rval = QLA_SUCCESS;
2939         retry = 0;
2940
2941         rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2942         if (rval == QLA_SUCCESS) {
2943                 /* Send an ADISC to tape devices.*/
2944                 opts = 0;
2945                 if (fcport->flags & FCF_TAPE_PRESENT)
2946                         opts |= BIT_1;
2947                 rval = qla2x00_get_port_database(ha, fcport, opts);
2948                 if (rval != QLA_SUCCESS) {
2949                         ha->isp_ops->fabric_logout(ha, fcport->loop_id,
2950                             fcport->d_id.b.domain, fcport->d_id.b.area,
2951                             fcport->d_id.b.al_pa);
2952                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
2953                 } else {
2954                         qla2x00_update_fcport(ha, fcport);
2955                 }
2956         }
2957
2958         return (rval);
2959 }
2960
2961 /*
2962  * qla2x00_fabric_login
2963  *      Issue fabric login command.
2964  *
2965  * Input:
2966  *      ha = adapter block pointer.
2967  *      device = pointer to FC device type structure.
2968  *
2969  * Returns:
2970  *      0 - Login successfully
2971  *      1 - Login failed
2972  *      2 - Initiator device
2973  *      3 - Fatal error
2974  */
2975 int
2976 qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2977     uint16_t *next_loopid)
2978 {
2979         int     rval;
2980         int     retry;
2981         uint16_t tmp_loopid;
2982         uint16_t mb[MAILBOX_REGISTER_COUNT];
2983
2984         retry = 0;
2985         tmp_loopid = 0;
2986
2987         for (;;) {
2988                 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2989                     "for port %02x%02x%02x.\n",
2990                     ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2991                     fcport->d_id.b.area, fcport->d_id.b.al_pa));
2992
2993                 /* Login fcport on switch. */
2994                 ha->isp_ops->fabric_login(ha, fcport->loop_id,
2995                     fcport->d_id.b.domain, fcport->d_id.b.area,
2996                     fcport->d_id.b.al_pa, mb, BIT_0);
2997                 if (mb[0] == MBS_PORT_ID_USED) {
2998                         /*
2999                          * Device has another loop ID.  The firmware team
3000                          * recommends the driver perform an implicit login with
3001                          * the specified ID again. The ID we just used is save
3002                          * here so we return with an ID that can be tried by
3003                          * the next login.
3004                          */
3005                         retry++;
3006                         tmp_loopid = fcport->loop_id;
3007                         fcport->loop_id = mb[1];
3008
3009                         DEBUG(printk("Fabric Login: port in use - next "
3010                             "loop id=0x%04x, port Id=%02x%02x%02x.\n",
3011                             fcport->loop_id, fcport->d_id.b.domain,
3012                             fcport->d_id.b.area, fcport->d_id.b.al_pa));
3013
3014                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
3015                         /*
3016                          * Login succeeded.
3017                          */
3018                         if (retry) {
3019                                 /* A retry occurred before. */
3020                                 *next_loopid = tmp_loopid;
3021                         } else {
3022                                 /*
3023                                  * No retry occurred before. Just increment the
3024                                  * ID value for next login.
3025                                  */
3026                                 *next_loopid = (fcport->loop_id + 1);
3027                         }
3028
3029                         if (mb[1] & BIT_0) {
3030                                 fcport->port_type = FCT_INITIATOR;
3031                         } else {
3032                                 fcport->port_type = FCT_TARGET;
3033                                 if (mb[1] & BIT_1) {
3034                                         fcport->flags |= FCF_TAPE_PRESENT;
3035                                 }
3036                         }
3037
3038                         if (mb[10] & BIT_0)
3039                                 fcport->supported_classes |= FC_COS_CLASS2;
3040                         if (mb[10] & BIT_1)
3041                                 fcport->supported_classes |= FC_COS_CLASS3;
3042
3043                         rval = QLA_SUCCESS;
3044                         break;
3045                 } else if (mb[0] == MBS_LOOP_ID_USED) {
3046                         /*
3047                          * Loop ID already used, try next loop ID.
3048                          */
3049                         fcport->loop_id++;
3050                         rval = qla2x00_find_new_loop_id(ha, fcport);
3051                         if (rval != QLA_SUCCESS) {
3052                                 /* Ran out of loop IDs to use */
3053                                 break;
3054                         }
3055                 } else if (mb[0] == MBS_COMMAND_ERROR) {
3056                         /*
3057                          * Firmware possibly timed out during login. If NO
3058                          * retries are left to do then the device is declared
3059                          * dead.
3060                          */
3061                         *next_loopid = fcport->loop_id;
3062                         ha->isp_ops->fabric_logout(ha, fcport->loop_id,
3063                             fcport->d_id.b.domain, fcport->d_id.b.area,
3064                             fcport->d_id.b.al_pa);
3065                         qla2x00_mark_device_lost(ha, fcport, 1, 0);
3066
3067                         rval = 1;
3068                         break;
3069                 } else {
3070                         /*
3071                          * unrecoverable / not handled error
3072                          */
3073                         DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
3074                             "loop_id=%x jiffies=%lx.\n",
3075                             __func__, ha->host_no, mb[0],
3076                             fcport->d_id.b.domain, fcport->d_id.b.area,
3077                             fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
3078
3079                         *next_loopid = fcport->loop_id;
3080                         ha->isp_ops->fabric_logout(ha, fcport->loop_id,
3081                             fcport->d_id.b.domain, fcport->d_id.b.area,
3082                             fcport->d_id.b.al_pa);
3083                         fcport->loop_id = FC_NO_LOOP_ID;
3084                         fcport->login_retry = 0;
3085
3086                         rval = 3;
3087                         break;
3088                 }
3089         }
3090
3091         return (rval);
3092 }
3093
3094 /*
3095  * qla2x00_local_device_login
3096  *      Issue local device login command.
3097  *
3098  * Input:
3099  *      ha = adapter block pointer.
3100  *      loop_id = loop id of device to login to.
3101  *
3102  * Returns (Where's the #define!!!!):
3103  *      0 - Login successfully
3104  *      1 - Login failed
3105  *      3 - Fatal error
3106  */
3107 int
3108 qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
3109 {
3110         int             rval;
3111         uint16_t        mb[MAILBOX_REGISTER_COUNT];
3112
3113         memset(mb, 0, sizeof(mb));
3114         rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
3115         if (rval == QLA_SUCCESS) {
3116                 /* Interrogate mailbox registers for any errors */
3117                 if (mb[0] == MBS_COMMAND_ERROR)
3118                         rval = 1;
3119                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
3120                         /* device not in PCB table */
3121                         rval = 3;
3122         }
3123
3124         return (rval);
3125 }
3126
3127 /*
3128  *  qla2x00_loop_resync
3129  *      Resync with fibre channel devices.
3130  *
3131  * Input:
3132  *      ha = adapter block pointer.
3133  *
3134  * Returns:
3135  *      0 = success
3136  */
3137 int
3138 qla2x00_loop_resync(scsi_qla_host_t *ha)
3139 {
3140         int   rval;
3141         uint32_t wait_time;
3142
3143         rval = QLA_SUCCESS;
3144
3145         atomic_set(&ha->loop_state, LOOP_UPDATE);
3146         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3147         if (ha->flags.online) {
3148                 if (!(rval = qla2x00_fw_ready(ha))) {
3149                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3150                         wait_time = 256;
3151                         do {
3152                                 atomic_set(&ha->loop_state, LOOP_UPDATE);
3153
3154                                 /* Issue a marker after FW becomes ready. */
3155                                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3156                                 ha->marker_needed = 0;
3157
3158                                 /* Remap devices on Loop. */
3159                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3160
3161                                 qla2x00_configure_loop(ha);
3162                                 wait_time--;
3163                         } while (!atomic_read(&ha->loop_down_timer) &&
3164                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3165                                 wait_time &&
3166                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3167                 }
3168         }
3169
3170         if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3171                 return (QLA_FUNCTION_FAILED);
3172         }
3173
3174         if (rval) {
3175                 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3176         }
3177
3178         return (rval);
3179 }
3180
3181 void
3182 qla2x00_update_fcports(scsi_qla_host_t *ha)
3183 {
3184         fc_port_t *fcport;
3185
3186         /* Go with deferred removal of rport references. */
3187         list_for_each_entry(fcport, &ha->fcports, list)
3188                 if (fcport->drport)
3189                         qla2x00_rport_del(fcport);
3190 }
3191
3192 /*
3193 *  qla2x00_abort_isp
3194 *      Resets ISP and aborts all outstanding commands.
3195 *
3196 * Input:
3197 *      ha           = adapter block pointer.
3198 *
3199 * Returns:
3200 *      0 = success
3201 */
3202 int
3203 qla2x00_abort_isp(scsi_qla_host_t *ha)
3204 {
3205         int rval;
3206         uint8_t        status = 0;
3207
3208         if (ha->flags.online) {
3209                 ha->flags.online = 0;
3210                 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3211
3212                 qla_printk(KERN_INFO, ha,
3213                     "Performing ISP error recovery - ha= %p.\n", ha);
3214                 ha->isp_ops->reset_chip(ha);
3215
3216                 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3217                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3218                         atomic_set(&ha->loop_state, LOOP_DOWN);
3219                         qla2x00_mark_all_devices_lost(ha, 0);
3220                 } else {
3221                         if (!atomic_read(&ha->loop_down_timer))
3222                                 atomic_set(&ha->loop_down_timer,
3223                                     LOOP_DOWN_TIME);
3224                 }
3225
3226                 /* Requeue all commands in outstanding command list. */
3227                 qla2x00_abort_all_cmds(ha, DID_RESET << 16);
3228
3229                 ha->isp_ops->get_flash_version(ha, ha->request_ring);
3230
3231                 ha->isp_ops->nvram_config(ha);
3232
3233                 if (!qla2x00_restart_isp(ha)) {
3234                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3235
3236                         if (!atomic_read(&ha->loop_down_timer)) {
3237                                 /*
3238                                  * Issue marker command only when we are going
3239                                  * to start the I/O .
3240                                  */
3241                                 ha->marker_needed = 1;
3242                         }
3243
3244                         ha->flags.online = 1;
3245
3246                         ha->isp_ops->enable_intrs(ha);
3247
3248                         ha->isp_abort_cnt = 0;
3249                         clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3250
3251                         if (ha->eft) {
3252                                 memset(ha->eft, 0, EFT_SIZE);
3253                                 rval = qla2x00_enable_eft_trace(ha,
3254                                     ha->eft_dma, EFT_NUM_BUFFERS);
3255                                 if (rval) {
3256                                         qla_printk(KERN_WARNING, ha,
3257                                             "Unable to reinitialize EFT "
3258                                             "(%d).\n", rval);
3259                                 }
3260                         }
3261
3262                         if (ha->fce) {
3263                                 ha->flags.fce_enabled = 1;
3264                                 memset(ha->fce, 0,
3265                                     fce_calc_size(ha->fce_bufs));
3266                                 rval = qla2x00_enable_fce_trace(ha,
3267                                     ha->fce_dma, ha->fce_bufs, ha->fce_mb,
3268                                     &ha->fce_bufs);
3269                                 if (rval) {
3270                                         qla_printk(KERN_WARNING, ha,
3271                                             "Unable to reinitialize FCE "
3272                                             "(%d).\n", rval);
3273                                         ha->flags.fce_enabled = 0;
3274                                 }
3275                         }
3276                 } else {        /* failed the ISP abort */
3277                         ha->flags.online = 1;
3278                         if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3279                                 if (ha->isp_abort_cnt == 0) {
3280                                         qla_printk(KERN_WARNING, ha,
3281                                             "ISP error recovery failed - "
3282                                             "board disabled\n");
3283                                         /*
3284                                          * The next call disables the board
3285                                          * completely.
3286                                          */
3287                                         ha->isp_ops->reset_adapter(ha);
3288                                         ha->flags.online = 0;
3289                                         clear_bit(ISP_ABORT_RETRY,
3290                                             &ha->dpc_flags);
3291                                         status = 0;
3292                                 } else { /* schedule another ISP abort */
3293                                         ha->isp_abort_cnt--;
3294                                         DEBUG(printk("qla%ld: ISP abort - "
3295                                             "retry remaining %d\n",
3296                                             ha->host_no, ha->isp_abort_cnt));
3297                                         status = 1;
3298                                 }
3299                         } else {
3300                                 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3301                                 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3302                                     "- retrying (%d) more times\n",
3303                                     ha->host_no, ha->isp_abort_cnt));
3304                                 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3305                                 status = 1;
3306                         }
3307                 }
3308
3309         }
3310
3311         if (status) {
3312                 qla_printk(KERN_INFO, ha,
3313                         "qla2x00_abort_isp: **** FAILED ****\n");
3314         } else {
3315                 DEBUG(printk(KERN_INFO
3316                                 "qla2x00_abort_isp(%ld): exiting.\n",
3317                                 ha->host_no));
3318         }
3319
3320         return(status);
3321 }
3322
3323 /*
3324 *  qla2x00_restart_isp
3325 *      restarts the ISP after a reset
3326 *
3327 * Input:
3328 *      ha = adapter block pointer.
3329 *
3330 * Returns:
3331 *      0 = success
3332 */
3333 static int
3334 qla2x00_restart_isp(scsi_qla_host_t *ha)
3335 {
3336         uint8_t         status = 0;
3337         uint32_t wait_time;
3338
3339         /* If firmware needs to be loaded */
3340         if (qla2x00_isp_firmware(ha)) {
3341                 ha->flags.online = 0;
3342                 if (!(status = ha->isp_ops->chip_diag(ha)))
3343                         status = qla2x00_setup_chip(ha);
3344         }
3345
3346         if (!status && !(status = qla2x00_init_rings(ha))) {
3347                 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3348                 if (!(status = qla2x00_fw_ready(ha))) {
3349                         DEBUG(printk("%s(): Start configure loop, "
3350                             "status = %d\n", __func__, status));
3351
3352                         /* Issue a marker after FW becomes ready. */
3353                         qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3354
3355                         ha->flags.online = 1;
3356                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
3357                         wait_time = 256;
3358                         do {
3359                                 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3360                                 qla2x00_configure_loop(ha);
3361                                 wait_time--;
3362                         } while (!atomic_read(&ha->loop_down_timer) &&
3363                                 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3364                                 wait_time &&
3365                                 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3366                 }
3367
3368                 /* if no cable then assume it's good */
3369                 if ((ha->device_flags & DFLG_NO_CABLE))
3370                         status = 0;
3371
3372                 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3373                                 __func__,
3374                                 status));
3375         }
3376         return (status);
3377 }
3378
3379 /*
3380 * qla2x00_reset_adapter
3381 *      Reset adapter.
3382 *
3383 * Input:
3384 *      ha = adapter block pointer.
3385 */
3386 void
3387 qla2x00_reset_adapter(scsi_qla_host_t *ha)
3388 {
3389         unsigned long flags = 0;
3390         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3391
3392         ha->flags.online = 0;
3393         ha->isp_ops->disable_intrs(ha);
3394
3395         spin_lock_irqsave(&ha->hardware_lock, flags);
3396         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3397         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3398         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3399         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
3400         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3401 }
3402
3403 void
3404 qla24xx_reset_adapter(scsi_qla_host_t *ha)
3405 {
3406         unsigned long flags = 0;
3407         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3408
3409         ha->flags.online = 0;
3410         ha->isp_ops->disable_intrs(ha);
3411
3412         spin_lock_irqsave(&ha->hardware_lock, flags);
3413         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3414         RD_REG_DWORD(&reg->hccr);
3415         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3416         RD_REG_DWORD(&reg->hccr);
3417         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3418 }
3419
3420 /* On sparc systems, obtain port and node WWN from firmware
3421  * properties.
3422  */
3423 static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
3424 {
3425 #ifdef CONFIG_SPARC
3426         struct pci_dev *pdev = ha->pdev;
3427         struct device_node *dp = pci_device_to_OF_node(pdev);
3428         const u8 *val;
3429         int len;
3430
3431         val = of_get_property(dp, "port-wwn", &len);
3432         if (val && len >= WWN_SIZE)
3433                 memcpy(nv->port_name, val, WWN_SIZE);
3434
3435         val = of_get_property(dp, "node-wwn", &len);
3436         if (val && len >= WWN_SIZE)
3437                 memcpy(nv->node_name, val, WWN_SIZE);
3438 #endif
3439 }
3440
3441 int
3442 qla24xx_nvram_config(scsi_qla_host_t *ha)
3443 {
3444         int   rval;
3445         struct init_cb_24xx *icb;
3446         struct nvram_24xx *nv;
3447         uint32_t *dptr;
3448         uint8_t  *dptr1, *dptr2;
3449         uint32_t chksum;
3450         uint16_t cnt;
3451
3452         rval = QLA_SUCCESS;
3453         icb = (struct init_cb_24xx *)ha->init_cb;
3454         nv = ha->nvram;
3455
3456         /* Determine NVRAM starting address. */
3457         ha->nvram_size = sizeof(struct nvram_24xx);
3458         ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3459         ha->vpd_size = FA_NVRAM_VPD_SIZE;
3460         ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3461         if (PCI_FUNC(ha->pdev->devfn)) {
3462                 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3463                 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3464         }
3465
3466         /* Get VPD data into cache */
3467         ha->vpd = ha->nvram + VPD_OFFSET;
3468         ha->isp_ops->read_nvram(ha, (uint8_t *)ha->vpd,
3469             ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
3470
3471         /* Get NVRAM data into cache and calculate checksum. */
3472         dptr = (uint32_t *)nv;
3473         ha->isp_ops->read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3474             ha->nvram_size);
3475         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3476                 chksum += le32_to_cpu(*dptr++);
3477
3478         DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3479         DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
3480
3481         /* Bad NVRAM data, set defaults parameters. */
3482         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3483             || nv->id[3] != ' ' ||
3484             nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3485                 /* Reset NVRAM data. */
3486                 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3487                     "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3488                     le16_to_cpu(nv->nvram_version));
3489                 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3490                     "invalid -- WWPN) defaults.\n");
3491
3492                 /*
3493                  * Set default initialization control block.
3494                  */
3495                 memset(nv, 0, ha->nvram_size);
3496                 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3497                 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3498                 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3499                 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3500                 nv->exchange_count = __constant_cpu_to_le16(0);
3501                 nv->hard_address = __constant_cpu_to_le16(124);
3502                 nv->port_name[0] = 0x21;
3503                 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3504                 nv->port_name[2] = 0x00;
3505                 nv->port_name[3] = 0xe0;
3506                 nv->port_name[4] = 0x8b;
3507                 nv->port_name[5] = 0x1c;
3508                 nv->port_name[6] = 0x55;
3509                 nv->port_name[7] = 0x86;
3510                 nv->node_name[0] = 0x20;
3511                 nv->node_name[1] = 0x00;
3512                 nv->node_name[2] = 0x00;
3513                 nv->node_name[3] = 0xe0;
3514                 nv->node_name[4] = 0x8b;
3515                 nv->node_name[5] = 0x1c;
3516                 nv->node_name[6] = 0x55;
3517                 nv->node_name[7] = 0x86;
3518                 qla24xx_nvram_wwn_from_ofw(ha, nv);
3519                 nv->login_retry_count = __constant_cpu_to_le16(8);
3520                 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3521                 nv->login_timeout = __constant_cpu_to_le16(0);
3522                 nv->firmware_options_1 =
3523                     __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3524                 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3525                 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3526                 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3527                 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3528                 nv->efi_parameters = __constant_cpu_to_le32(0);
3529                 nv->reset_delay = 5;
3530                 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3531                 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3532                 nv->link_down_timeout = __constant_cpu_to_le16(30);
3533
3534                 rval = 1;
3535         }
3536
3537         /* Reset Initialization control block */
3538         memset(icb, 0, sizeof(struct init_cb_24xx));
3539
3540         /* Copy 1st segment. */
3541         dptr1 = (uint8_t *)icb;
3542         dptr2 = (uint8_t *)&nv->version;
3543         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3544         while (cnt--)
3545                 *dptr1++ = *dptr2++;
3546
3547         icb->login_retry_count = nv->login_retry_count;
3548         icb->link_down_on_nos = nv->link_down_on_nos;
3549
3550         /* Copy 2nd segment. */
3551         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3552         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3553         cnt = (uint8_t *)&icb->reserved_3 -
3554             (uint8_t *)&icb->interrupt_delay_timer;
3555         while (cnt--)
3556                 *dptr1++ = *dptr2++;
3557
3558         /*
3559          * Setup driver NVRAM options.
3560          */
3561         qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
3562             "QLA2462");
3563
3564         /* Use alternate WWN? */
3565         if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3566                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3567                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3568         }
3569
3570         /* Prepare nodename */
3571         if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
3572                 /*
3573                  * Firmware will apply the following mask if the nodename was
3574                  * not provided.
3575                  */
3576                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3577                 icb->node_name[0] &= 0xF0;
3578         }
3579
3580         /* Set host adapter parameters. */
3581         ha->flags.disable_risc_code_load = 0;
3582         ha->flags.enable_lip_reset = 0;
3583         ha->flags.enable_lip_full_login =
3584             le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
3585         ha->flags.enable_target_reset =
3586             le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
3587         ha->flags.enable_led_scheme = 0;
3588         ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
3589
3590         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3591             (BIT_6 | BIT_5 | BIT_4)) >> 4;
3592
3593         memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3594             sizeof(ha->fw_seriallink_options24));
3595
3596         /* save HBA serial number */
3597         ha->serial0 = icb->port_name[5];
3598         ha->serial1 = icb->port_name[6];
3599         ha->serial2 = icb->port_name[7];
3600         ha->node_name = icb->node_name;
3601         ha->port_name = icb->port_name;
3602
3603         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3604
3605         ha->retry_count = le16_to_cpu(nv->login_retry_count);
3606
3607         /* Set minimum login_timeout to 4 seconds. */
3608         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3609                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3610         if (le16_to_cpu(nv->login_timeout) < 4)
3611                 nv->login_timeout = __constant_cpu_to_le16(4);
3612         ha->login_timeout = le16_to_cpu(nv->login_timeout);
3613         icb->login_timeout = cpu_to_le16(nv->login_timeout);
3614
3615         /* Set minimum RATOV to 100 tenths of a second. */
3616         ha->r_a_tov = 100;
3617
3618         ha->loop_reset_delay = nv->reset_delay;
3619
3620         /* Link Down Timeout = 0:
3621          *
3622          *      When Port Down timer expires we will start returning
3623          *      I/O's to OS with "DID_NO_CONNECT".
3624          *
3625          * Link Down Timeout != 0:
3626          *
3627          *       The driver waits for the link to come up after link down
3628          *       before returning I/Os to OS with "DID_NO_CONNECT".
3629          */
3630         if (le16_to_cpu(nv->link_down_timeout) == 0) {
3631                 ha->loop_down_abort_time =
3632                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3633         } else {
3634                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3635                 ha->loop_down_abort_time =
3636                     (LOOP_DOWN_TIME - ha->link_down_timeout);
3637         }
3638
3639         /* Need enough time to try and get the port back. */
3640         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3641         if (qlport_down_retry)
3642                 ha->port_down_retry_count = qlport_down_retry;
3643
3644         /* Set login_retry_count */
3645         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
3646         if (ha->port_down_retry_count ==
3647             le16_to_cpu(nv->port_down_retry_count) &&
3648             ha->port_down_retry_count > 3)
3649                 ha->login_retry_count = ha->port_down_retry_count;
3650         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3651                 ha->login_retry_count = ha->port_down_retry_count;
3652         if (ql2xloginretrycount)
3653                 ha->login_retry_count = ql2xloginretrycount;
3654
3655         /* Enable ZIO. */
3656         if (!ha->flags.init_done) {
3657                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3658                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3659                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3660                     le16_to_cpu(icb->interrupt_delay_timer): 2;
3661         }
3662         icb->firmware_options_2 &= __constant_cpu_to_le32(
3663             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3664         ha->flags.process_response_queue = 0;
3665         if (ha->zio_mode != QLA_ZIO_DISABLED) {
3666                 ha->zio_mode = QLA_ZIO_MODE_6;
3667
3668                 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3669                     "(%d us).\n", ha->host_no, ha->zio_mode,
3670                     ha->zio_timer * 100));
3671                 qla_printk(KERN_INFO, ha,
3672                     "ZIO mode %d enabled; timer delay (%d us).\n",
3673                     ha->zio_mode, ha->zio_timer * 100);
3674
3675                 icb->firmware_options_2 |= cpu_to_le32(
3676                     (uint32_t)ha->zio_mode);
3677                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3678                 ha->flags.process_response_queue = 1;
3679         }
3680
3681         if (rval) {
3682                 DEBUG2_3(printk(KERN_WARNING
3683                     "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3684         }
3685         return (rval);
3686 }
3687
3688 static int
3689 qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3690 {
3691         int     rval;
3692         int     segments, fragment;
3693         uint32_t faddr;
3694         uint32_t *dcode, dlen;
3695         uint32_t risc_addr;
3696         uint32_t risc_size;
3697         uint32_t i;
3698
3699         rval = QLA_SUCCESS;
3700
3701         segments = FA_RISC_CODE_SEGMENTS;
3702         faddr = FA_RISC_CODE_ADDR;
3703         dcode = (uint32_t *)ha->request_ring;
3704         *srisc_addr = 0;
3705
3706         /* Validate firmware image by checking version. */
3707         qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3708         for (i = 0; i < 4; i++)
3709                 dcode[i] = be32_to_cpu(dcode[i]);
3710         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3711             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3712             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3713                 dcode[3] == 0)) {
3714                 qla_printk(KERN_WARNING, ha,
3715                     "Unable to verify integrity of flash firmware image!\n");
3716                 qla_printk(KERN_WARNING, ha,
3717                     "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3718                     dcode[1], dcode[2], dcode[3]);
3719
3720                 return QLA_FUNCTION_FAILED;
3721         }
3722
3723         while (segments && rval == QLA_SUCCESS) {
3724                 /* Read segment's load information. */
3725                 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3726
3727                 risc_addr = be32_to_cpu(dcode[2]);
3728                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3729                 risc_size = be32_to_cpu(dcode[3]);
3730
3731                 fragment = 0;
3732                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3733                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3734                         if (dlen > risc_size)
3735                                 dlen = risc_size;
3736
3737                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3738                             "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3739                             ha->host_no, risc_addr, dlen, faddr));
3740
3741                         qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3742                         for (i = 0; i < dlen; i++)
3743                                 dcode[i] = swab32(dcode[i]);
3744
3745                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3746                             dlen);
3747                         if (rval) {
3748                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3749                                     "segment %d of firmware\n", ha->host_no,
3750                                     fragment));
3751                                 qla_printk(KERN_WARNING, ha,
3752                                     "[ERROR] Failed to load segment %d of "
3753                                     "firmware\n", fragment);
3754                                 break;
3755                         }
3756
3757                         faddr += dlen;
3758                         risc_addr += dlen;
3759                         risc_size -= dlen;
3760                         fragment++;
3761                 }
3762
3763                 /* Next segment. */
3764                 segments--;
3765         }
3766
3767         return rval;
3768 }
3769
3770 #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3771
3772 int
3773 qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3774 {
3775         int     rval;
3776         int     i, fragment;
3777         uint16_t *wcode, *fwcode;
3778         uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3779         struct fw_blob *blob;
3780
3781         /* Load firmware blob. */
3782         blob = qla2x00_request_firmware(ha);
3783         if (!blob) {
3784                 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3785                 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3786                     "from: " QLA_FW_URL ".\n");
3787                 return QLA_FUNCTION_FAILED;
3788         }
3789
3790         rval = QLA_SUCCESS;
3791
3792         wcode = (uint16_t *)ha->request_ring;
3793         *srisc_addr = 0;
3794         fwcode = (uint16_t *)blob->fw->data;
3795         fwclen = 0;
3796
3797         /* Validate firmware image by checking version. */
3798         if (blob->fw->size < 8 * sizeof(uint16_t)) {
3799                 qla_printk(KERN_WARNING, ha,
3800                     "Unable to verify integrity of firmware image (%Zd)!\n",
3801                     blob->fw->size);
3802                 goto fail_fw_integrity;
3803         }
3804         for (i = 0; i < 4; i++)
3805                 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3806         if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3807             wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3808                 wcode[2] == 0 && wcode[3] == 0)) {
3809                 qla_printk(KERN_WARNING, ha,
3810                     "Unable to verify integrity of firmware image!\n");
3811                 qla_printk(KERN_WARNING, ha,
3812                     "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3813                     wcode[1], wcode[2], wcode[3]);
3814                 goto fail_fw_integrity;
3815         }
3816
3817         seg = blob->segs;
3818         while (*seg && rval == QLA_SUCCESS) {
3819                 risc_addr = *seg;
3820                 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3821                 risc_size = be16_to_cpu(fwcode[3]);
3822
3823                 /* Validate firmware image size. */
3824                 fwclen += risc_size * sizeof(uint16_t);
3825                 if (blob->fw->size < fwclen) {
3826                         qla_printk(KERN_WARNING, ha,
3827                             "Unable to verify integrity of firmware image "
3828                             "(%Zd)!\n", blob->fw->size);
3829                         goto fail_fw_integrity;
3830                 }
3831
3832                 fragment = 0;
3833                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3834                         wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3835                         if (wlen > risc_size)
3836                                 wlen = risc_size;
3837
3838                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3839                             "addr %x, number of words 0x%x.\n", ha->host_no,
3840                             risc_addr, wlen));
3841
3842                         for (i = 0; i < wlen; i++)
3843                                 wcode[i] = swab16(fwcode[i]);
3844
3845                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3846                             wlen);
3847                         if (rval) {
3848                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3849                                     "segment %d of firmware\n", ha->host_no,
3850                                     fragment));
3851                                 qla_printk(KERN_WARNING, ha,
3852                                     "[ERROR] Failed to load segment %d of "
3853                                     "firmware\n", fragment);
3854                                 break;
3855                         }
3856
3857                         fwcode += wlen;
3858                         risc_addr += wlen;
3859                         risc_size -= wlen;
3860                         fragment++;
3861                 }
3862
3863                 /* Next segment. */
3864                 seg++;
3865         }
3866         return rval;
3867
3868 fail_fw_integrity:
3869         return QLA_FUNCTION_FAILED;
3870 }
3871
3872 int
3873 qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3874 {
3875         int     rval;
3876         int     segments, fragment;
3877         uint32_t *dcode, dlen;
3878         uint32_t risc_addr;
3879         uint32_t risc_size;
3880         uint32_t i;
3881         struct fw_blob *blob;
3882         uint32_t *fwcode, fwclen;
3883
3884         /* Load firmware blob. */
3885         blob = qla2x00_request_firmware(ha);
3886         if (!blob) {
3887                 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3888                 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3889                     "from: " QLA_FW_URL ".\n");
3890
3891                 /* Try to load RISC code from flash. */
3892                 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3893                     "outdated) firmware from flash.\n");
3894                 return qla24xx_load_risc_flash(ha, srisc_addr);
3895         }
3896
3897         rval = QLA_SUCCESS;
3898
3899         segments = FA_RISC_CODE_SEGMENTS;
3900         dcode = (uint32_t *)ha->request_ring;
3901         *srisc_addr = 0;
3902         fwcode = (uint32_t *)blob->fw->data;
3903         fwclen = 0;
3904
3905         /* Validate firmware image by checking version. */
3906         if (blob->fw->size < 8 * sizeof(uint32_t)) {
3907                 qla_printk(KERN_WARNING, ha,
3908                     "Unable to verify integrity of firmware image (%Zd)!\n",
3909                     blob->fw->size);
3910                 goto fail_fw_integrity;
3911         }
3912         for (i = 0; i < 4; i++)
3913                 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3914         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3915             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3916             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3917                 dcode[3] == 0)) {
3918                 qla_printk(KERN_WARNING, ha,
3919                     "Unable to verify integrity of firmware image!\n");
3920                 qla_printk(KERN_WARNING, ha,
3921                     "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3922                     dcode[1], dcode[2], dcode[3]);
3923                 goto fail_fw_integrity;
3924         }
3925
3926         while (segments && rval == QLA_SUCCESS) {
3927                 risc_addr = be32_to_cpu(fwcode[2]);
3928                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3929                 risc_size = be32_to_cpu(fwcode[3]);
3930
3931                 /* Validate firmware image size. */
3932                 fwclen += risc_size * sizeof(uint32_t);
3933                 if (blob->fw->size < fwclen) {
3934                         qla_printk(KERN_WARNING, ha,
3935                             "Unable to verify integrity of firmware image "
3936                             "(%Zd)!\n", blob->fw->size);
3937
3938                         goto fail_fw_integrity;
3939                 }
3940
3941                 fragment = 0;
3942                 while (risc_size > 0 && rval == QLA_SUCCESS) {
3943                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3944                         if (dlen > risc_size)
3945                                 dlen = risc_size;
3946
3947                         DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3948                             "addr %x, number of dwords 0x%x.\n", ha->host_no,
3949                             risc_addr, dlen));
3950
3951                         for (i = 0; i < dlen; i++)
3952                                 dcode[i] = swab32(fwcode[i]);
3953
3954                         rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3955                             dlen);
3956                         if (rval) {
3957                                 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3958                                     "segment %d of firmware\n", ha->host_no,
3959                                     fragment));
3960                                 qla_printk(KERN_WARNING, ha,
3961                                     "[ERROR] Failed to load segment %d of "
3962                                     "firmware\n", fragment);
3963                                 break;
3964                         }
3965
3966                         fwcode += dlen;
3967                         risc_addr += dlen;
3968                         risc_size -= dlen;
3969                         fragment++;
3970                 }
3971
3972                 /* Next segment. */
3973                 segments--;
3974         }
3975         return rval;
3976
3977 fail_fw_integrity:
3978         return QLA_FUNCTION_FAILED;
3979 }
3980
3981 void
3982 qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
3983 {
3984         int ret, retries;
3985
3986         if (!IS_FWI2_CAPABLE(ha))
3987                 return;
3988         if (!ha->fw_major_version)
3989                 return;
3990
3991         ret = qla2x00_stop_firmware(ha);
3992         for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
3993             retries ; retries--) {
3994                 qla2x00_reset_chip(ha);
3995                 if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
3996                         continue;
3997                 if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
3998                         continue;
3999                 qla_printk(KERN_INFO, ha,
4000                     "Attempting retry of stop-firmware command...\n");
4001                 ret = qla2x00_stop_firmware(ha);
4002         }
4003 }
4004
4005 int
4006 qla24xx_configure_vhba(scsi_qla_host_t *ha)
4007 {
4008         int rval = QLA_SUCCESS;
4009         uint16_t mb[MAILBOX_REGISTER_COUNT];
4010
4011         if (!ha->parent)
4012                 return -EINVAL;
4013
4014         rval = qla2x00_fw_ready(ha);
4015         if (rval == QLA_SUCCESS) {
4016                 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
4017                 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
4018         }
4019
4020         ha->flags.management_server_logged_in = 0;
4021
4022         /* Login to SNS first */
4023         qla24xx_login_fabric(ha, NPH_SNS, 0xff, 0xff, 0xfc,
4024             mb, BIT_1);
4025         if (mb[0] != MBS_COMMAND_COMPLETE) {
4026                 DEBUG15(qla_printk(KERN_INFO, ha,
4027                     "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
4028                     "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
4029                     mb[0], mb[1], mb[2], mb[6], mb[7]));
4030                 return (QLA_FUNCTION_FAILED);
4031         }
4032
4033         atomic_set(&ha->loop_down_timer, 0);
4034         atomic_set(&ha->loop_state, LOOP_UP);
4035         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
4036         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
4037         rval = qla2x00_loop_resync(ha);
4038
4039         return rval;
4040 }