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