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