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