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