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