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