Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[linux-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         ha->flags.pci_channel_io_perm_failure = 0;
273         ha->flags.eeh_busy = 0;
274         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
275         atomic_set(&vha->loop_state, LOOP_DOWN);
276         vha->device_flags = DFLG_NO_CABLE;
277         vha->dpc_flags = 0;
278         vha->flags.management_server_logged_in = 0;
279         vha->marker_needed = 0;
280         ha->isp_abort_cnt = 0;
281         ha->beacon_blink_led = 0;
282
283         set_bit(0, ha->req_qid_map);
284         set_bit(0, ha->rsp_qid_map);
285
286         qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
287         rval = ha->isp_ops->pci_config(vha);
288         if (rval) {
289                 DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
290                     vha->host_no));
291                 return (rval);
292         }
293
294         ha->isp_ops->reset_chip(vha);
295
296         rval = qla2xxx_get_flash_info(vha);
297         if (rval) {
298                 DEBUG2(printk("scsi(%ld): Unable to validate FLASH data.\n",
299                     vha->host_no));
300                 return (rval);
301         }
302
303         ha->isp_ops->get_flash_version(vha, req->ring);
304
305         qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
306
307         ha->isp_ops->nvram_config(vha);
308
309         if (ha->flags.disable_serdes) {
310                 /* Mask HBA via NVRAM settings? */
311                 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
312                     "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
313                     vha->port_name[0], vha->port_name[1],
314                     vha->port_name[2], vha->port_name[3],
315                     vha->port_name[4], vha->port_name[5],
316                     vha->port_name[6], vha->port_name[7]);
317                 return QLA_FUNCTION_FAILED;
318         }
319
320         qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
321
322         if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
323                 rval = ha->isp_ops->chip_diag(vha);
324                 if (rval)
325                         return (rval);
326                 rval = qla2x00_setup_chip(vha);
327                 if (rval)
328                         return (rval);
329         }
330         if (IS_QLA84XX(ha)) {
331                 ha->cs84xx = qla84xx_get_chip(vha);
332                 if (!ha->cs84xx) {
333                         qla_printk(KERN_ERR, ha,
334                             "Unable to configure ISP84XX.\n");
335                         return QLA_FUNCTION_FAILED;
336                 }
337         }
338         rval = qla2x00_init_rings(vha);
339         ha->flags.chip_reset_done = 1;
340
341         return (rval);
342 }
343
344 /**
345  * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
346  * @ha: HA context
347  *
348  * Returns 0 on success.
349  */
350 int
351 qla2100_pci_config(scsi_qla_host_t *vha)
352 {
353         uint16_t w;
354         unsigned long flags;
355         struct qla_hw_data *ha = vha->hw;
356         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
357
358         pci_set_master(ha->pdev);
359         pci_try_set_mwi(ha->pdev);
360
361         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
362         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
363         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
364
365         pci_disable_rom(ha->pdev);
366
367         /* Get PCI bus information. */
368         spin_lock_irqsave(&ha->hardware_lock, flags);
369         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
370         spin_unlock_irqrestore(&ha->hardware_lock, flags);
371
372         return QLA_SUCCESS;
373 }
374
375 /**
376  * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
377  * @ha: HA context
378  *
379  * Returns 0 on success.
380  */
381 int
382 qla2300_pci_config(scsi_qla_host_t *vha)
383 {
384         uint16_t        w;
385         unsigned long   flags = 0;
386         uint32_t        cnt;
387         struct qla_hw_data *ha = vha->hw;
388         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
389
390         pci_set_master(ha->pdev);
391         pci_try_set_mwi(ha->pdev);
392
393         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
394         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
395
396         if (IS_QLA2322(ha) || IS_QLA6322(ha))
397                 w &= ~PCI_COMMAND_INTX_DISABLE;
398         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
399
400         /*
401          * If this is a 2300 card and not 2312, reset the
402          * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
403          * the 2310 also reports itself as a 2300 so we need to get the
404          * fb revision level -- a 6 indicates it really is a 2300 and
405          * not a 2310.
406          */
407         if (IS_QLA2300(ha)) {
408                 spin_lock_irqsave(&ha->hardware_lock, flags);
409
410                 /* Pause RISC. */
411                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
412                 for (cnt = 0; cnt < 30000; cnt++) {
413                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
414                                 break;
415
416                         udelay(10);
417                 }
418
419                 /* Select FPM registers. */
420                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
421                 RD_REG_WORD(&reg->ctrl_status);
422
423                 /* Get the fb rev level */
424                 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
425
426                 if (ha->fb_rev == FPM_2300)
427                         pci_clear_mwi(ha->pdev);
428
429                 /* Deselect FPM registers. */
430                 WRT_REG_WORD(&reg->ctrl_status, 0x0);
431                 RD_REG_WORD(&reg->ctrl_status);
432
433                 /* Release RISC module. */
434                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
435                 for (cnt = 0; cnt < 30000; cnt++) {
436                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
437                                 break;
438
439                         udelay(10);
440                 }
441
442                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
443         }
444
445         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
446
447         pci_disable_rom(ha->pdev);
448
449         /* Get PCI bus information. */
450         spin_lock_irqsave(&ha->hardware_lock, flags);
451         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
452         spin_unlock_irqrestore(&ha->hardware_lock, flags);
453
454         return QLA_SUCCESS;
455 }
456
457 /**
458  * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
459  * @ha: HA context
460  *
461  * Returns 0 on success.
462  */
463 int
464 qla24xx_pci_config(scsi_qla_host_t *vha)
465 {
466         uint16_t w;
467         unsigned long flags = 0;
468         struct qla_hw_data *ha = vha->hw;
469         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
470
471         pci_set_master(ha->pdev);
472         pci_try_set_mwi(ha->pdev);
473
474         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
475         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
476         w &= ~PCI_COMMAND_INTX_DISABLE;
477         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
478
479         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
480
481         /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
482         if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
483                 pcix_set_mmrbc(ha->pdev, 2048);
484
485         /* PCIe -- adjust Maximum Read Request Size (2048). */
486         if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
487                 pcie_set_readrq(ha->pdev, 2048);
488
489         pci_disable_rom(ha->pdev);
490
491         ha->chip_revision = ha->pdev->revision;
492
493         /* Get PCI bus information. */
494         spin_lock_irqsave(&ha->hardware_lock, flags);
495         ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
496         spin_unlock_irqrestore(&ha->hardware_lock, flags);
497
498         return QLA_SUCCESS;
499 }
500
501 /**
502  * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
503  * @ha: HA context
504  *
505  * Returns 0 on success.
506  */
507 int
508 qla25xx_pci_config(scsi_qla_host_t *vha)
509 {
510         uint16_t w;
511         struct qla_hw_data *ha = vha->hw;
512
513         pci_set_master(ha->pdev);
514         pci_try_set_mwi(ha->pdev);
515
516         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
517         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
518         w &= ~PCI_COMMAND_INTX_DISABLE;
519         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
520
521         /* PCIe -- adjust Maximum Read Request Size (2048). */
522         if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
523                 pcie_set_readrq(ha->pdev, 2048);
524
525         pci_disable_rom(ha->pdev);
526
527         ha->chip_revision = ha->pdev->revision;
528
529         return QLA_SUCCESS;
530 }
531
532 /**
533  * qla2x00_isp_firmware() - Choose firmware image.
534  * @ha: HA context
535  *
536  * Returns 0 on success.
537  */
538 static int
539 qla2x00_isp_firmware(scsi_qla_host_t *vha)
540 {
541         int  rval;
542         uint16_t loop_id, topo, sw_cap;
543         uint8_t domain, area, al_pa;
544         struct qla_hw_data *ha = vha->hw;
545
546         /* Assume loading risc code */
547         rval = QLA_FUNCTION_FAILED;
548
549         if (ha->flags.disable_risc_code_load) {
550                 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
551                     vha->host_no));
552                 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
553
554                 /* Verify checksum of loaded RISC code. */
555                 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
556                 if (rval == QLA_SUCCESS) {
557                         /* And, verify we are not in ROM code. */
558                         rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
559                             &area, &domain, &topo, &sw_cap);
560                 }
561         }
562
563         if (rval) {
564                 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
565                     vha->host_no));
566         }
567
568         return (rval);
569 }
570
571 /**
572  * qla2x00_reset_chip() - Reset ISP chip.
573  * @ha: HA context
574  *
575  * Returns 0 on success.
576  */
577 void
578 qla2x00_reset_chip(scsi_qla_host_t *vha)
579 {
580         unsigned long   flags = 0;
581         struct qla_hw_data *ha = vha->hw;
582         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
583         uint32_t        cnt;
584         uint16_t        cmd;
585
586         if (unlikely(pci_channel_offline(ha->pdev)))
587                 return;
588
589         ha->isp_ops->disable_intrs(ha);
590
591         spin_lock_irqsave(&ha->hardware_lock, flags);
592
593         /* Turn off master enable */
594         cmd = 0;
595         pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
596         cmd &= ~PCI_COMMAND_MASTER;
597         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
598
599         if (!IS_QLA2100(ha)) {
600                 /* Pause RISC. */
601                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
602                 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
603                         for (cnt = 0; cnt < 30000; cnt++) {
604                                 if ((RD_REG_WORD(&reg->hccr) &
605                                     HCCR_RISC_PAUSE) != 0)
606                                         break;
607                                 udelay(100);
608                         }
609                 } else {
610                         RD_REG_WORD(&reg->hccr);        /* PCI Posting. */
611                         udelay(10);
612                 }
613
614                 /* Select FPM registers. */
615                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
616                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
617
618                 /* FPM Soft Reset. */
619                 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
620                 RD_REG_WORD(&reg->fpm_diag_config);     /* PCI Posting. */
621
622                 /* Toggle Fpm Reset. */
623                 if (!IS_QLA2200(ha)) {
624                         WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
625                         RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
626                 }
627
628                 /* Select frame buffer registers. */
629                 WRT_REG_WORD(&reg->ctrl_status, 0x10);
630                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
631
632                 /* Reset frame buffer FIFOs. */
633                 if (IS_QLA2200(ha)) {
634                         WRT_FB_CMD_REG(ha, reg, 0xa000);
635                         RD_FB_CMD_REG(ha, reg);         /* PCI Posting. */
636                 } else {
637                         WRT_FB_CMD_REG(ha, reg, 0x00fc);
638
639                         /* Read back fb_cmd until zero or 3 seconds max */
640                         for (cnt = 0; cnt < 3000; cnt++) {
641                                 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
642                                         break;
643                                 udelay(100);
644                         }
645                 }
646
647                 /* Select RISC module registers. */
648                 WRT_REG_WORD(&reg->ctrl_status, 0);
649                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
650
651                 /* Reset RISC processor. */
652                 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
653                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
654
655                 /* Release RISC processor. */
656                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
657                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
658         }
659
660         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
661         WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
662
663         /* Reset ISP chip. */
664         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
665
666         /* Wait for RISC to recover from reset. */
667         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
668                 /*
669                  * It is necessary to for a delay here since the card doesn't
670                  * respond to PCI reads during a reset. On some architectures
671                  * this will result in an MCA.
672                  */
673                 udelay(20);
674                 for (cnt = 30000; cnt; cnt--) {
675                         if ((RD_REG_WORD(&reg->ctrl_status) &
676                             CSR_ISP_SOFT_RESET) == 0)
677                                 break;
678                         udelay(100);
679                 }
680         } else
681                 udelay(10);
682
683         /* Reset RISC processor. */
684         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
685
686         WRT_REG_WORD(&reg->semaphore, 0);
687
688         /* Release RISC processor. */
689         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
690         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
691
692         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
693                 for (cnt = 0; cnt < 30000; cnt++) {
694                         if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
695                                 break;
696
697                         udelay(100);
698                 }
699         } else
700                 udelay(100);
701
702         /* Turn on master enable */
703         cmd |= PCI_COMMAND_MASTER;
704         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
705
706         /* Disable RISC pause on FPM parity error. */
707         if (!IS_QLA2100(ha)) {
708                 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
709                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
710         }
711
712         spin_unlock_irqrestore(&ha->hardware_lock, flags);
713 }
714
715 /**
716  * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
717  * @ha: HA context
718  *
719  * Returns 0 on success.
720  */
721 static inline void
722 qla24xx_reset_risc(scsi_qla_host_t *vha)
723 {
724         unsigned long flags = 0;
725         struct qla_hw_data *ha = vha->hw;
726         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
727         uint32_t cnt, d2;
728         uint16_t wd;
729
730         spin_lock_irqsave(&ha->hardware_lock, flags);
731
732         /* Reset RISC. */
733         WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
734         for (cnt = 0; cnt < 30000; cnt++) {
735                 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
736                         break;
737
738                 udelay(10);
739         }
740
741         WRT_REG_DWORD(&reg->ctrl_status,
742             CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
743         pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
744
745         udelay(100);
746         /* Wait for firmware to complete NVRAM accesses. */
747         d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
748         for (cnt = 10000 ; cnt && d2; cnt--) {
749                 udelay(5);
750                 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
751                 barrier();
752         }
753
754         /* Wait for soft-reset to complete. */
755         d2 = RD_REG_DWORD(&reg->ctrl_status);
756         for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
757                 udelay(5);
758                 d2 = RD_REG_DWORD(&reg->ctrl_status);
759                 barrier();
760         }
761
762         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
763         RD_REG_DWORD(&reg->hccr);
764
765         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
766         RD_REG_DWORD(&reg->hccr);
767
768         WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
769         RD_REG_DWORD(&reg->hccr);
770
771         d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
772         for (cnt = 6000000 ; cnt && d2; cnt--) {
773                 udelay(5);
774                 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
775                 barrier();
776         }
777
778         spin_unlock_irqrestore(&ha->hardware_lock, flags);
779
780         if (IS_NOPOLLING_TYPE(ha))
781                 ha->isp_ops->enable_intrs(ha);
782 }
783
784 /**
785  * qla24xx_reset_chip() - Reset ISP24xx chip.
786  * @ha: HA context
787  *
788  * Returns 0 on success.
789  */
790 void
791 qla24xx_reset_chip(scsi_qla_host_t *vha)
792 {
793         struct qla_hw_data *ha = vha->hw;
794
795         if (pci_channel_offline(ha->pdev) &&
796             ha->flags.pci_channel_io_perm_failure) {
797                 return;
798         }
799
800         ha->isp_ops->disable_intrs(ha);
801
802         /* Perform RISC reset. */
803         qla24xx_reset_risc(vha);
804 }
805
806 /**
807  * qla2x00_chip_diag() - Test chip for proper operation.
808  * @ha: HA context
809  *
810  * Returns 0 on success.
811  */
812 int
813 qla2x00_chip_diag(scsi_qla_host_t *vha)
814 {
815         int             rval;
816         struct qla_hw_data *ha = vha->hw;
817         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
818         unsigned long   flags = 0;
819         uint16_t        data;
820         uint32_t        cnt;
821         uint16_t        mb[5];
822         struct req_que *req = ha->req_q_map[0];
823
824         /* Assume a failed state */
825         rval = QLA_FUNCTION_FAILED;
826
827         DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
828             vha->host_no, (u_long)&reg->flash_address));
829
830         spin_lock_irqsave(&ha->hardware_lock, flags);
831
832         /* Reset ISP chip. */
833         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
834
835         /*
836          * We need to have a delay here since the card will not respond while
837          * in reset causing an MCA on some architectures.
838          */
839         udelay(20);
840         data = qla2x00_debounce_register(&reg->ctrl_status);
841         for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
842                 udelay(5);
843                 data = RD_REG_WORD(&reg->ctrl_status);
844                 barrier();
845         }
846
847         if (!cnt)
848                 goto chip_diag_failed;
849
850         DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
851             vha->host_no));
852
853         /* Reset RISC processor. */
854         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
855         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
856
857         /* Workaround for QLA2312 PCI parity error */
858         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
859                 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
860                 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
861                         udelay(5);
862                         data = RD_MAILBOX_REG(ha, reg, 0);
863                         barrier();
864                 }
865         } else
866                 udelay(10);
867
868         if (!cnt)
869                 goto chip_diag_failed;
870
871         /* Check product ID of chip */
872         DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", vha->host_no));
873
874         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
875         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
876         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
877         mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
878         if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
879             mb[3] != PROD_ID_3) {
880                 qla_printk(KERN_WARNING, ha,
881                     "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
882
883                 goto chip_diag_failed;
884         }
885         ha->product_id[0] = mb[1];
886         ha->product_id[1] = mb[2];
887         ha->product_id[2] = mb[3];
888         ha->product_id[3] = mb[4];
889
890         /* Adjust fw RISC transfer size */
891         if (req->length > 1024)
892                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
893         else
894                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
895                     req->length;
896
897         if (IS_QLA2200(ha) &&
898             RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
899                 /* Limit firmware transfer size with a 2200A */
900                 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
901                     vha->host_no));
902
903                 ha->device_type |= DT_ISP2200A;
904                 ha->fw_transfer_size = 128;
905         }
906
907         /* Wrap Incoming Mailboxes Test. */
908         spin_unlock_irqrestore(&ha->hardware_lock, flags);
909
910         DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", vha->host_no));
911         rval = qla2x00_mbx_reg_test(vha);
912         if (rval) {
913                 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
914                     vha->host_no));
915                 qla_printk(KERN_WARNING, ha,
916                     "Failed mailbox send register test\n");
917         }
918         else {
919                 /* Flag a successful rval */
920                 rval = QLA_SUCCESS;
921         }
922         spin_lock_irqsave(&ha->hardware_lock, flags);
923
924 chip_diag_failed:
925         if (rval)
926                 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
927                     "****\n", vha->host_no));
928
929         spin_unlock_irqrestore(&ha->hardware_lock, flags);
930
931         return (rval);
932 }
933
934 /**
935  * qla24xx_chip_diag() - Test ISP24xx for proper operation.
936  * @ha: HA context
937  *
938  * Returns 0 on success.
939  */
940 int
941 qla24xx_chip_diag(scsi_qla_host_t *vha)
942 {
943         int rval;
944         struct qla_hw_data *ha = vha->hw;
945         struct req_que *req = ha->req_q_map[0];
946
947         ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
948
949         rval = qla2x00_mbx_reg_test(vha);
950         if (rval) {
951                 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
952                     vha->host_no));
953                 qla_printk(KERN_WARNING, ha,
954                     "Failed mailbox send register test\n");
955         } else {
956                 /* Flag a successful rval */
957                 rval = QLA_SUCCESS;
958         }
959
960         return rval;
961 }
962
963 void
964 qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
965 {
966         int rval;
967         uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
968             eft_size, fce_size, mq_size;
969         dma_addr_t tc_dma;
970         void *tc;
971         struct qla_hw_data *ha = vha->hw;
972         struct req_que *req = ha->req_q_map[0];
973         struct rsp_que *rsp = ha->rsp_q_map[0];
974
975         if (ha->fw_dump) {
976                 qla_printk(KERN_WARNING, ha,
977                     "Firmware dump previously allocated.\n");
978                 return;
979         }
980
981         ha->fw_dumped = 0;
982         fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
983         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
984                 fixed_size = sizeof(struct qla2100_fw_dump);
985         } else if (IS_QLA23XX(ha)) {
986                 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
987                 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
988                     sizeof(uint16_t);
989         } else if (IS_FWI2_CAPABLE(ha)) {
990                 if (IS_QLA81XX(ha))
991                         fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
992                 else if (IS_QLA25XX(ha))
993                         fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
994                 else
995                         fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
996                 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
997                     sizeof(uint32_t);
998                 if (ha->mqenable)
999                         mq_size = sizeof(struct qla2xxx_mq_chain);
1000                 /* Allocate memory for Fibre Channel Event Buffer. */
1001                 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha))
1002                         goto try_eft;
1003
1004                 tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
1005                     GFP_KERNEL);
1006                 if (!tc) {
1007                         qla_printk(KERN_WARNING, ha, "Unable to allocate "
1008                             "(%d KB) for FCE.\n", FCE_SIZE / 1024);
1009                         goto try_eft;
1010                 }
1011
1012                 memset(tc, 0, FCE_SIZE);
1013                 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
1014                     ha->fce_mb, &ha->fce_bufs);
1015                 if (rval) {
1016                         qla_printk(KERN_WARNING, ha, "Unable to initialize "
1017                             "FCE (%d).\n", rval);
1018                         dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
1019                             tc_dma);
1020                         ha->flags.fce_enabled = 0;
1021                         goto try_eft;
1022                 }
1023
1024                 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for FCE...\n",
1025                     FCE_SIZE / 1024);
1026
1027                 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
1028                 ha->flags.fce_enabled = 1;
1029                 ha->fce_dma = tc_dma;
1030                 ha->fce = tc;
1031 try_eft:
1032                 /* Allocate memory for Extended Trace Buffer. */
1033                 tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
1034                     GFP_KERNEL);
1035                 if (!tc) {
1036                         qla_printk(KERN_WARNING, ha, "Unable to allocate "
1037                             "(%d KB) for EFT.\n", EFT_SIZE / 1024);
1038                         goto cont_alloc;
1039                 }
1040
1041                 memset(tc, 0, EFT_SIZE);
1042                 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
1043                 if (rval) {
1044                         qla_printk(KERN_WARNING, ha, "Unable to initialize "
1045                             "EFT (%d).\n", rval);
1046                         dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
1047                             tc_dma);
1048                         goto cont_alloc;
1049                 }
1050
1051                 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
1052                     EFT_SIZE / 1024);
1053
1054                 eft_size = EFT_SIZE;
1055                 ha->eft_dma = tc_dma;
1056                 ha->eft = tc;
1057         }
1058 cont_alloc:
1059         req_q_size = req->length * sizeof(request_t);
1060         rsp_q_size = rsp->length * sizeof(response_t);
1061
1062         dump_size = offsetof(struct qla2xxx_fw_dump, isp);
1063         dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
1064         ha->chain_offset = dump_size;
1065         dump_size += mq_size + fce_size;
1066
1067         ha->fw_dump = vmalloc(dump_size);
1068         if (!ha->fw_dump) {
1069                 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
1070                     "firmware dump!!!\n", dump_size / 1024);
1071
1072                 if (ha->eft) {
1073                         dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
1074                             ha->eft_dma);
1075                         ha->eft = NULL;
1076                         ha->eft_dma = 0;
1077                 }
1078                 return;
1079         }
1080         qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
1081             dump_size / 1024);
1082
1083         ha->fw_dump_len = dump_size;
1084         ha->fw_dump->signature[0] = 'Q';
1085         ha->fw_dump->signature[1] = 'L';
1086         ha->fw_dump->signature[2] = 'G';
1087         ha->fw_dump->signature[3] = 'C';
1088         ha->fw_dump->version = __constant_htonl(1);
1089
1090         ha->fw_dump->fixed_size = htonl(fixed_size);
1091         ha->fw_dump->mem_size = htonl(mem_size);
1092         ha->fw_dump->req_q_size = htonl(req_q_size);
1093         ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
1094
1095         ha->fw_dump->eft_size = htonl(eft_size);
1096         ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
1097         ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
1098
1099         ha->fw_dump->header_size =
1100             htonl(offsetof(struct qla2xxx_fw_dump, isp));
1101 }
1102
1103 static int
1104 qla81xx_mpi_sync(scsi_qla_host_t *vha)
1105 {
1106 #define MPS_MASK        0xe0
1107         int rval;
1108         uint16_t dc;
1109         uint32_t dw;
1110         struct qla_hw_data *ha = vha->hw;
1111
1112         if (!IS_QLA81XX(vha->hw))
1113                 return QLA_SUCCESS;
1114
1115         rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
1116         if (rval != QLA_SUCCESS) {
1117                 DEBUG2(qla_printk(KERN_WARNING, ha,
1118                     "Sync-MPI: Unable to acquire semaphore.\n"));
1119                 goto done;
1120         }
1121
1122         pci_read_config_word(vha->hw->pdev, 0x54, &dc);
1123         rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
1124         if (rval != QLA_SUCCESS) {
1125                 DEBUG2(qla_printk(KERN_WARNING, ha,
1126                     "Sync-MPI: Unable to read sync.\n"));
1127                 goto done_release;
1128         }
1129
1130         dc &= MPS_MASK;
1131         if (dc == (dw & MPS_MASK))
1132                 goto done_release;
1133
1134         dw &= ~MPS_MASK;
1135         dw |= dc;
1136         rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
1137         if (rval != QLA_SUCCESS) {
1138                 DEBUG2(qla_printk(KERN_WARNING, ha,
1139                     "Sync-MPI: Unable to gain sync.\n"));
1140         }
1141
1142 done_release:
1143         rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
1144         if (rval != QLA_SUCCESS) {
1145                 DEBUG2(qla_printk(KERN_WARNING, ha,
1146                     "Sync-MPI: Unable to release semaphore.\n"));
1147         }
1148
1149 done:
1150         return rval;
1151 }
1152
1153 /**
1154  * qla2x00_setup_chip() - Load and start RISC firmware.
1155  * @ha: HA context
1156  *
1157  * Returns 0 on success.
1158  */
1159 static int
1160 qla2x00_setup_chip(scsi_qla_host_t *vha)
1161 {
1162         int rval;
1163         uint32_t srisc_address = 0;
1164         struct qla_hw_data *ha = vha->hw;
1165         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1166         unsigned long flags;
1167         uint16_t fw_major_version;
1168
1169         if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1170                 /* Disable SRAM, Instruction RAM and GP RAM parity.  */
1171                 spin_lock_irqsave(&ha->hardware_lock, flags);
1172                 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
1173                 RD_REG_WORD(&reg->hccr);
1174                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1175         }
1176
1177         qla81xx_mpi_sync(vha);
1178
1179         /* Load firmware sequences */
1180         rval = ha->isp_ops->load_risc(vha, &srisc_address);
1181         if (rval == QLA_SUCCESS) {
1182                 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
1183                     "code.\n", vha->host_no));
1184
1185                 rval = qla2x00_verify_checksum(vha, srisc_address);
1186                 if (rval == QLA_SUCCESS) {
1187                         /* Start firmware execution. */
1188                         DEBUG(printk("scsi(%ld): Checksum OK, start "
1189                             "firmware.\n", vha->host_no));
1190
1191                         rval = qla2x00_execute_fw(vha, srisc_address);
1192                         /* Retrieve firmware information. */
1193                         if (rval == QLA_SUCCESS) {
1194                                 fw_major_version = ha->fw_major_version;
1195                                 rval = qla2x00_get_fw_version(vha,
1196                                     &ha->fw_major_version,
1197                                     &ha->fw_minor_version,
1198                                     &ha->fw_subminor_version,
1199                                     &ha->fw_attributes, &ha->fw_memory_size,
1200                                     ha->mpi_version, &ha->mpi_capabilities,
1201                                     ha->phy_version);
1202                                 if (rval != QLA_SUCCESS)
1203                                         goto failed;
1204                                 ha->flags.npiv_supported = 0;
1205                                 if (IS_QLA2XXX_MIDTYPE(ha) &&
1206                                          (ha->fw_attributes & BIT_2)) {
1207                                         ha->flags.npiv_supported = 1;
1208                                         if ((!ha->max_npiv_vports) ||
1209                                             ((ha->max_npiv_vports + 1) %
1210                                             MIN_MULTI_ID_FABRIC))
1211                                                 ha->max_npiv_vports =
1212                                                     MIN_MULTI_ID_FABRIC - 1;
1213                                 }
1214                                 qla2x00_get_resource_cnts(vha, NULL,
1215                                     &ha->fw_xcb_count, NULL, NULL,
1216                                     &ha->max_npiv_vports, NULL);
1217
1218                                 if (!fw_major_version && ql2xallocfwdump)
1219                                         qla2x00_alloc_fw_dump(vha);
1220                         }
1221                 } else {
1222                         DEBUG2(printk(KERN_INFO
1223                             "scsi(%ld): ISP Firmware failed checksum.\n",
1224                             vha->host_no));
1225                 }
1226         }
1227
1228         if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1229                 /* Enable proper parity. */
1230                 spin_lock_irqsave(&ha->hardware_lock, flags);
1231                 if (IS_QLA2300(ha))
1232                         /* SRAM parity */
1233                         WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
1234                 else
1235                         /* SRAM, Instruction RAM and GP RAM parity */
1236                         WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
1237                 RD_REG_WORD(&reg->hccr);
1238                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1239         }
1240
1241         if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
1242                 uint32_t size;
1243
1244                 rval = qla81xx_fac_get_sector_size(vha, &size);
1245                 if (rval == QLA_SUCCESS) {
1246                         ha->flags.fac_supported = 1;
1247                         ha->fdt_block_size = size << 2;
1248                 } else {
1249                         qla_printk(KERN_ERR, ha,
1250                             "Unsupported FAC firmware (%d.%02d.%02d).\n",
1251                             ha->fw_major_version, ha->fw_minor_version,
1252                             ha->fw_subminor_version);
1253                 }
1254         }
1255 failed:
1256         if (rval) {
1257                 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
1258                     vha->host_no));
1259         }
1260
1261         return (rval);
1262 }
1263
1264 /**
1265  * qla2x00_init_response_q_entries() - Initializes response queue entries.
1266  * @ha: HA context
1267  *
1268  * Beginning of request ring has initialization control block already built
1269  * by nvram config routine.
1270  *
1271  * Returns 0 on success.
1272  */
1273 void
1274 qla2x00_init_response_q_entries(struct rsp_que *rsp)
1275 {
1276         uint16_t cnt;
1277         response_t *pkt;
1278
1279         rsp->ring_ptr = rsp->ring;
1280         rsp->ring_index    = 0;
1281         rsp->status_srb = NULL;
1282         pkt = rsp->ring_ptr;
1283         for (cnt = 0; cnt < rsp->length; cnt++) {
1284                 pkt->signature = RESPONSE_PROCESSED;
1285                 pkt++;
1286         }
1287 }
1288
1289 /**
1290  * qla2x00_update_fw_options() - Read and process firmware options.
1291  * @ha: HA context
1292  *
1293  * Returns 0 on success.
1294  */
1295 void
1296 qla2x00_update_fw_options(scsi_qla_host_t *vha)
1297 {
1298         uint16_t swing, emphasis, tx_sens, rx_sens;
1299         struct qla_hw_data *ha = vha->hw;
1300
1301         memset(ha->fw_options, 0, sizeof(ha->fw_options));
1302         qla2x00_get_fw_options(vha, ha->fw_options);
1303
1304         if (IS_QLA2100(ha) || IS_QLA2200(ha))
1305                 return;
1306
1307         /* Serial Link options. */
1308         DEBUG3(printk("scsi(%ld): Serial link options:\n",
1309             vha->host_no));
1310         DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1311             sizeof(ha->fw_seriallink_options)));
1312
1313         ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1314         if (ha->fw_seriallink_options[3] & BIT_2) {
1315                 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1316
1317                 /*  1G settings */
1318                 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1319                 emphasis = (ha->fw_seriallink_options[2] &
1320                     (BIT_4 | BIT_3)) >> 3;
1321                 tx_sens = ha->fw_seriallink_options[0] &
1322                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1323                 rx_sens = (ha->fw_seriallink_options[0] &
1324                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1325                 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1326                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1327                         if (rx_sens == 0x0)
1328                                 rx_sens = 0x3;
1329                         ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1330                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1331                         ha->fw_options[10] |= BIT_5 |
1332                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1333                             (tx_sens & (BIT_1 | BIT_0));
1334
1335                 /*  2G settings */
1336                 swing = (ha->fw_seriallink_options[2] &
1337                     (BIT_7 | BIT_6 | BIT_5)) >> 5;
1338                 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1339                 tx_sens = ha->fw_seriallink_options[1] &
1340                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1341                 rx_sens = (ha->fw_seriallink_options[1] &
1342                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1343                 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1344                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1345                         if (rx_sens == 0x0)
1346                                 rx_sens = 0x3;
1347                         ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1348                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1349                         ha->fw_options[11] |= BIT_5 |
1350                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1351                             (tx_sens & (BIT_1 | BIT_0));
1352         }
1353
1354         /* FCP2 options. */
1355         /*  Return command IOCBs without waiting for an ABTS to complete. */
1356         ha->fw_options[3] |= BIT_13;
1357
1358         /* LED scheme. */
1359         if (ha->flags.enable_led_scheme)
1360                 ha->fw_options[2] |= BIT_12;
1361
1362         /* Detect ISP6312. */
1363         if (IS_QLA6312(ha))
1364                 ha->fw_options[2] |= BIT_13;
1365
1366         /* Update firmware options. */
1367         qla2x00_set_fw_options(vha, ha->fw_options);
1368 }
1369
1370 void
1371 qla24xx_update_fw_options(scsi_qla_host_t *vha)
1372 {
1373         int rval;
1374         struct qla_hw_data *ha = vha->hw;
1375
1376         /* Update Serial Link options. */
1377         if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
1378                 return;
1379
1380         rval = qla2x00_set_serdes_params(vha,
1381             le16_to_cpu(ha->fw_seriallink_options24[1]),
1382             le16_to_cpu(ha->fw_seriallink_options24[2]),
1383             le16_to_cpu(ha->fw_seriallink_options24[3]));
1384         if (rval != QLA_SUCCESS) {
1385                 qla_printk(KERN_WARNING, ha,
1386                     "Unable to update Serial Link options (%x).\n", rval);
1387         }
1388 }
1389
1390 void
1391 qla2x00_config_rings(struct scsi_qla_host *vha)
1392 {
1393         struct qla_hw_data *ha = vha->hw;
1394         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1395         struct req_que *req = ha->req_q_map[0];
1396         struct rsp_que *rsp = ha->rsp_q_map[0];
1397
1398         /* Setup ring parameters in initialization control block. */
1399         ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1400         ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1401         ha->init_cb->request_q_length = cpu_to_le16(req->length);
1402         ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
1403         ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
1404         ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
1405         ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
1406         ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
1407
1408         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1409         WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1410         WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1411         WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1412         RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg));            /* PCI Posting. */
1413 }
1414
1415 void
1416 qla24xx_config_rings(struct scsi_qla_host *vha)
1417 {
1418         struct qla_hw_data *ha = vha->hw;
1419         device_reg_t __iomem *reg = ISP_QUE_REG(ha, 0);
1420         struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
1421         struct qla_msix_entry *msix;
1422         struct init_cb_24xx *icb;
1423         uint16_t rid = 0;
1424         struct req_que *req = ha->req_q_map[0];
1425         struct rsp_que *rsp = ha->rsp_q_map[0];
1426
1427 /* Setup ring parameters in initialization control block. */
1428         icb = (struct init_cb_24xx *)ha->init_cb;
1429         icb->request_q_outpointer = __constant_cpu_to_le16(0);
1430         icb->response_q_inpointer = __constant_cpu_to_le16(0);
1431         icb->request_q_length = cpu_to_le16(req->length);
1432         icb->response_q_length = cpu_to_le16(rsp->length);
1433         icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
1434         icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
1435         icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
1436         icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
1437
1438         if (ha->mqenable) {
1439                 icb->qos = __constant_cpu_to_le16(QLA_DEFAULT_QUE_QOS);
1440                 icb->rid = __constant_cpu_to_le16(rid);
1441                 if (ha->flags.msix_enabled) {
1442                         msix = &ha->msix_entries[1];
1443                         DEBUG2_17(printk(KERN_INFO
1444                         "Registering vector 0x%x for base que\n", msix->entry));
1445                         icb->msix = cpu_to_le16(msix->entry);
1446                 }
1447                 /* Use alternate PCI bus number */
1448                 if (MSB(rid))
1449                         icb->firmware_options_2 |=
1450                                 __constant_cpu_to_le32(BIT_19);
1451                 /* Use alternate PCI devfn */
1452                 if (LSB(rid))
1453                         icb->firmware_options_2 |=
1454                                 __constant_cpu_to_le32(BIT_18);
1455
1456                 /* Use Disable MSIX Handshake mode for capable adapters */
1457                 if (IS_MSIX_NACK_CAPABLE(ha)) {
1458                         icb->firmware_options_2 &=
1459                                 __constant_cpu_to_le32(~BIT_22);
1460                         ha->flags.disable_msix_handshake = 1;
1461                         qla_printk(KERN_INFO, ha,
1462                                 "MSIX Handshake Disable Mode turned on\n");
1463                 } else {
1464                         icb->firmware_options_2 |=
1465                                 __constant_cpu_to_le32(BIT_22);
1466                 }
1467                 icb->firmware_options_2 |= __constant_cpu_to_le32(BIT_23);
1468
1469                 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
1470                 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
1471                 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
1472                 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
1473         } else {
1474                 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
1475                 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
1476                 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
1477                 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
1478         }
1479         /* PCI posting */
1480         RD_REG_DWORD(&ioreg->hccr);
1481 }
1482
1483 /**
1484  * qla2x00_init_rings() - Initializes firmware.
1485  * @ha: HA context
1486  *
1487  * Beginning of request ring has initialization control block already built
1488  * by nvram config routine.
1489  *
1490  * Returns 0 on success.
1491  */
1492 static int
1493 qla2x00_init_rings(scsi_qla_host_t *vha)
1494 {
1495         int     rval;
1496         unsigned long flags = 0;
1497         int cnt, que;
1498         struct qla_hw_data *ha = vha->hw;
1499         struct req_que *req;
1500         struct rsp_que *rsp;
1501         struct scsi_qla_host *vp;
1502         struct mid_init_cb_24xx *mid_init_cb =
1503             (struct mid_init_cb_24xx *) ha->init_cb;
1504
1505         spin_lock_irqsave(&ha->hardware_lock, flags);
1506
1507         /* Clear outstanding commands array. */
1508         for (que = 0; que < ha->max_req_queues; que++) {
1509                 req = ha->req_q_map[que];
1510                 if (!req)
1511                         continue;
1512                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1513                         req->outstanding_cmds[cnt] = NULL;
1514
1515                 req->current_outstanding_cmd = 1;
1516
1517                 /* Initialize firmware. */
1518                 req->ring_ptr  = req->ring;
1519                 req->ring_index    = 0;
1520                 req->cnt      = req->length;
1521         }
1522
1523         for (que = 0; que < ha->max_rsp_queues; que++) {
1524                 rsp = ha->rsp_q_map[que];
1525                 if (!rsp)
1526                         continue;
1527                 /* Initialize response queue entries */
1528                 qla2x00_init_response_q_entries(rsp);
1529         }
1530
1531         /* Clear RSCN queue. */
1532         list_for_each_entry(vp, &ha->vp_list, list) {
1533                 vp->rscn_in_ptr = 0;
1534                 vp->rscn_out_ptr = 0;
1535         }
1536         ha->isp_ops->config_rings(vha);
1537
1538         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1539
1540         /* Update any ISP specific firmware options before initialization. */
1541         ha->isp_ops->update_fw_options(vha);
1542
1543         DEBUG(printk("scsi(%ld): Issue init firmware.\n", vha->host_no));
1544
1545         if (ha->flags.npiv_supported) {
1546                 if (ha->operating_mode == LOOP)
1547                         ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
1548                 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
1549         }
1550
1551         if (IS_FWI2_CAPABLE(ha)) {
1552                 mid_init_cb->options = __constant_cpu_to_le16(BIT_1);
1553                 mid_init_cb->init_cb.execution_throttle =
1554                     cpu_to_le16(ha->fw_xcb_count);
1555         }
1556
1557         rval = qla2x00_init_firmware(vha, ha->init_cb_size);
1558         if (rval) {
1559                 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1560                     vha->host_no));
1561         } else {
1562                 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1563                     vha->host_no));
1564         }
1565
1566         return (rval);
1567 }
1568
1569 /**
1570  * qla2x00_fw_ready() - Waits for firmware ready.
1571  * @ha: HA context
1572  *
1573  * Returns 0 on success.
1574  */
1575 static int
1576 qla2x00_fw_ready(scsi_qla_host_t *vha)
1577 {
1578         int             rval;
1579         unsigned long   wtime, mtime, cs84xx_time;
1580         uint16_t        min_wait;       /* Minimum wait time if loop is down */
1581         uint16_t        wait_time;      /* Wait time if loop is coming ready */
1582         uint16_t        state[5];
1583         struct qla_hw_data *ha = vha->hw;
1584
1585         rval = QLA_SUCCESS;
1586
1587         /* 20 seconds for loop down. */
1588         min_wait = 20;
1589
1590         /*
1591          * Firmware should take at most one RATOV to login, plus 5 seconds for
1592          * our own processing.
1593          */
1594         if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1595                 wait_time = min_wait;
1596         }
1597
1598         /* Min wait time if loop down */
1599         mtime = jiffies + (min_wait * HZ);
1600
1601         /* wait time before firmware ready */
1602         wtime = jiffies + (wait_time * HZ);
1603
1604         /* Wait for ISP to finish LIP */
1605         if (!vha->flags.init_done)
1606                 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1607
1608         DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1609             vha->host_no));
1610
1611         do {
1612                 rval = qla2x00_get_firmware_state(vha, state);
1613                 if (rval == QLA_SUCCESS) {
1614                         if (state[0] < FSTATE_LOSS_OF_SYNC) {
1615                                 vha->device_flags &= ~DFLG_NO_CABLE;
1616                         }
1617                         if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
1618                                 DEBUG16(printk("scsi(%ld): fw_state=%x "
1619                                     "84xx=%x.\n", vha->host_no, state[0],
1620                                     state[2]));
1621                                 if ((state[2] & FSTATE_LOGGED_IN) &&
1622                                      (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
1623                                         DEBUG16(printk("scsi(%ld): Sending "
1624                                             "verify iocb.\n", vha->host_no));
1625
1626                                         cs84xx_time = jiffies;
1627                                         rval = qla84xx_init_chip(vha);
1628                                         if (rval != QLA_SUCCESS)
1629                                                 break;
1630
1631                                         /* Add time taken to initialize. */
1632                                         cs84xx_time = jiffies - cs84xx_time;
1633                                         wtime += cs84xx_time;
1634                                         mtime += cs84xx_time;
1635                                         DEBUG16(printk("scsi(%ld): Increasing "
1636                                             "wait time by %ld. New time %ld\n",
1637                                             vha->host_no, cs84xx_time, wtime));
1638                                 }
1639                         } else if (state[0] == FSTATE_READY) {
1640                                 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1641                                     vha->host_no));
1642
1643                                 qla2x00_get_retry_cnt(vha, &ha->retry_count,
1644                                     &ha->login_timeout, &ha->r_a_tov);
1645
1646                                 rval = QLA_SUCCESS;
1647                                 break;
1648                         }
1649
1650                         rval = QLA_FUNCTION_FAILED;
1651
1652                         if (atomic_read(&vha->loop_down_timer) &&
1653                             state[0] != FSTATE_READY) {
1654                                 /* Loop down. Timeout on min_wait for states
1655                                  * other than Wait for Login.
1656                                  */
1657                                 if (time_after_eq(jiffies, mtime)) {
1658                                         qla_printk(KERN_INFO, ha,
1659                                             "Cable is unplugged...\n");
1660
1661                                         vha->device_flags |= DFLG_NO_CABLE;
1662                                         break;
1663                                 }
1664                         }
1665                 } else {
1666                         /* Mailbox cmd failed. Timeout on min_wait. */
1667                         if (time_after_eq(jiffies, mtime))
1668                                 break;
1669                 }
1670
1671                 if (time_after_eq(jiffies, wtime))
1672                         break;
1673
1674                 /* Delay for a while */
1675                 msleep(500);
1676
1677                 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1678                     vha->host_no, state[0], jiffies));
1679         } while (1);
1680
1681         DEBUG(printk("scsi(%ld): fw_state=%x (%x, %x, %x, %x) curr time=%lx.\n",
1682             vha->host_no, state[0], state[1], state[2], state[3], state[4],
1683             jiffies));
1684
1685         if (rval) {
1686                 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1687                     vha->host_no));
1688         }
1689
1690         return (rval);
1691 }
1692
1693 /*
1694 *  qla2x00_configure_hba
1695 *      Setup adapter context.
1696 *
1697 * Input:
1698 *      ha = adapter state pointer.
1699 *
1700 * Returns:
1701 *      0 = success
1702 *
1703 * Context:
1704 *      Kernel context.
1705 */
1706 static int
1707 qla2x00_configure_hba(scsi_qla_host_t *vha)
1708 {
1709         int       rval;
1710         uint16_t      loop_id;
1711         uint16_t      topo;
1712         uint16_t      sw_cap;
1713         uint8_t       al_pa;
1714         uint8_t       area;
1715         uint8_t       domain;
1716         char            connect_type[22];
1717         struct qla_hw_data *ha = vha->hw;
1718
1719         /* Get host addresses. */
1720         rval = qla2x00_get_adapter_id(vha,
1721             &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
1722         if (rval != QLA_SUCCESS) {
1723                 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
1724                     (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1725                         DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1726                             __func__, vha->host_no));
1727                 } else {
1728                         qla_printk(KERN_WARNING, ha,
1729                             "ERROR -- Unable to get host loop ID.\n");
1730                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1731                 }
1732                 return (rval);
1733         }
1734
1735         if (topo == 4) {
1736                 qla_printk(KERN_INFO, ha,
1737                         "Cannot get topology - retrying.\n");
1738                 return (QLA_FUNCTION_FAILED);
1739         }
1740
1741         vha->loop_id = loop_id;
1742
1743         /* initialize */
1744         ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1745         ha->operating_mode = LOOP;
1746         ha->switch_cap = 0;
1747
1748         switch (topo) {
1749         case 0:
1750                 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1751                     vha->host_no));
1752                 ha->current_topology = ISP_CFG_NL;
1753                 strcpy(connect_type, "(Loop)");
1754                 break;
1755
1756         case 1:
1757                 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1758                     vha->host_no));
1759                 ha->switch_cap = sw_cap;
1760                 ha->current_topology = ISP_CFG_FL;
1761                 strcpy(connect_type, "(FL_Port)");
1762                 break;
1763
1764         case 2:
1765                 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1766                     vha->host_no));
1767                 ha->operating_mode = P2P;
1768                 ha->current_topology = ISP_CFG_N;
1769                 strcpy(connect_type, "(N_Port-to-N_Port)");
1770                 break;
1771
1772         case 3:
1773                 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1774                     vha->host_no));
1775                 ha->switch_cap = sw_cap;
1776                 ha->operating_mode = P2P;
1777                 ha->current_topology = ISP_CFG_F;
1778                 strcpy(connect_type, "(F_Port)");
1779                 break;
1780
1781         default:
1782                 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1783                     "Using NL.\n",
1784                     vha->host_no, topo));
1785                 ha->current_topology = ISP_CFG_NL;
1786                 strcpy(connect_type, "(Loop)");
1787                 break;
1788         }
1789
1790         /* Save Host port and loop ID. */
1791         /* byte order - Big Endian */
1792         vha->d_id.b.domain = domain;
1793         vha->d_id.b.area = area;
1794         vha->d_id.b.al_pa = al_pa;
1795
1796         if (!vha->flags.init_done)
1797                 qla_printk(KERN_INFO, ha,
1798                     "Topology - %s, Host Loop address 0x%x\n",
1799                     connect_type, vha->loop_id);
1800
1801         if (rval) {
1802                 DEBUG2_3(printk("scsi(%ld): FAILED.\n", vha->host_no));
1803         } else {
1804                 DEBUG3(printk("scsi(%ld): exiting normally.\n", vha->host_no));
1805         }
1806
1807         return(rval);
1808 }
1809
1810 static inline void
1811 qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
1812         char *def)
1813 {
1814         char *st, *en;
1815         uint16_t index;
1816         struct qla_hw_data *ha = vha->hw;
1817         int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
1818             !IS_QLA81XX(ha);
1819
1820         if (memcmp(model, BINZERO, len) != 0) {
1821                 strncpy(ha->model_number, model, len);
1822                 st = en = ha->model_number;
1823                 en += len - 1;
1824                 while (en > st) {
1825                         if (*en != 0x20 && *en != 0x00)
1826                                 break;
1827                         *en-- = '\0';
1828                 }
1829
1830                 index = (ha->pdev->subsystem_device & 0xff);
1831                 if (use_tbl &&
1832                     ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1833                     index < QLA_MODEL_NAMES)
1834                         strncpy(ha->model_desc,
1835                             qla2x00_model_name[index * 2 + 1],
1836                             sizeof(ha->model_desc) - 1);
1837         } else {
1838                 index = (ha->pdev->subsystem_device & 0xff);
1839                 if (use_tbl &&
1840                     ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
1841                     index < QLA_MODEL_NAMES) {
1842                         strcpy(ha->model_number,
1843                             qla2x00_model_name[index * 2]);
1844                         strncpy(ha->model_desc,
1845                             qla2x00_model_name[index * 2 + 1],
1846                             sizeof(ha->model_desc) - 1);
1847                 } else {
1848                         strcpy(ha->model_number, def);
1849                 }
1850         }
1851         if (IS_FWI2_CAPABLE(ha))
1852                 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
1853                     sizeof(ha->model_desc));
1854 }
1855
1856 /* On sparc systems, obtain port and node WWN from firmware
1857  * properties.
1858  */
1859 static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
1860 {
1861 #ifdef CONFIG_SPARC
1862         struct qla_hw_data *ha = vha->hw;
1863         struct pci_dev *pdev = ha->pdev;
1864         struct device_node *dp = pci_device_to_OF_node(pdev);
1865         const u8 *val;
1866         int len;
1867
1868         val = of_get_property(dp, "port-wwn", &len);
1869         if (val && len >= WWN_SIZE)
1870                 memcpy(nv->port_name, val, WWN_SIZE);
1871
1872         val = of_get_property(dp, "node-wwn", &len);
1873         if (val && len >= WWN_SIZE)
1874                 memcpy(nv->node_name, val, WWN_SIZE);
1875 #endif
1876 }
1877
1878 /*
1879 * NVRAM configuration for ISP 2xxx
1880 *
1881 * Input:
1882 *      ha                = adapter block pointer.
1883 *
1884 * Output:
1885 *      initialization control block in response_ring
1886 *      host adapters parameters in host adapter block
1887 *
1888 * Returns:
1889 *      0 = success.
1890 */
1891 int
1892 qla2x00_nvram_config(scsi_qla_host_t *vha)
1893 {
1894         int             rval;
1895         uint8_t         chksum = 0;
1896         uint16_t        cnt;
1897         uint8_t         *dptr1, *dptr2;
1898         struct qla_hw_data *ha = vha->hw;
1899         init_cb_t       *icb = ha->init_cb;
1900         nvram_t         *nv = ha->nvram;
1901         uint8_t         *ptr = ha->nvram;
1902         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1903
1904         rval = QLA_SUCCESS;
1905
1906         /* Determine NVRAM starting address. */
1907         ha->nvram_size = sizeof(nvram_t);
1908         ha->nvram_base = 0;
1909         if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1910                 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1911                         ha->nvram_base = 0x80;
1912
1913         /* Get NVRAM data and calculate checksum. */
1914         ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
1915         for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1916                 chksum += *ptr++;
1917
1918         DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", vha->host_no));
1919         DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
1920
1921         /* Bad NVRAM data, set defaults parameters. */
1922         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1923             nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1924                 /* Reset NVRAM data. */
1925                 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1926                     "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1927                     nv->nvram_version);
1928                 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1929                     "invalid -- WWPN) defaults.\n");
1930
1931                 /*
1932                  * Set default initialization control block.
1933                  */
1934                 memset(nv, 0, ha->nvram_size);
1935                 nv->parameter_block_version = ICB_VERSION;
1936
1937                 if (IS_QLA23XX(ha)) {
1938                         nv->firmware_options[0] = BIT_2 | BIT_1;
1939                         nv->firmware_options[1] = BIT_7 | BIT_5;
1940                         nv->add_firmware_options[0] = BIT_5;
1941                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
1942                         nv->frame_payload_size = __constant_cpu_to_le16(2048);
1943                         nv->special_options[1] = BIT_7;
1944                 } else if (IS_QLA2200(ha)) {
1945                         nv->firmware_options[0] = BIT_2 | BIT_1;
1946                         nv->firmware_options[1] = BIT_7 | BIT_5;
1947                         nv->add_firmware_options[0] = BIT_5;
1948                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
1949                         nv->frame_payload_size = __constant_cpu_to_le16(1024);
1950                 } else if (IS_QLA2100(ha)) {
1951                         nv->firmware_options[0] = BIT_3 | BIT_1;
1952                         nv->firmware_options[1] = BIT_5;
1953                         nv->frame_payload_size = __constant_cpu_to_le16(1024);
1954                 }
1955
1956                 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1957                 nv->execution_throttle = __constant_cpu_to_le16(16);
1958                 nv->retry_count = 8;
1959                 nv->retry_delay = 1;
1960
1961                 nv->port_name[0] = 33;
1962                 nv->port_name[3] = 224;
1963                 nv->port_name[4] = 139;
1964
1965                 qla2xxx_nvram_wwn_from_ofw(vha, nv);
1966
1967                 nv->login_timeout = 4;
1968
1969                 /*
1970                  * Set default host adapter parameters
1971                  */
1972                 nv->host_p[1] = BIT_2;
1973                 nv->reset_delay = 5;
1974                 nv->port_down_retry_count = 8;
1975                 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1976                 nv->link_down_timeout = 60;
1977
1978                 rval = 1;
1979         }
1980
1981 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1982         /*
1983          * The SN2 does not provide BIOS emulation which means you can't change
1984          * potentially bogus BIOS settings. Force the use of default settings
1985          * for link rate and frame size.  Hope that the rest of the settings
1986          * are valid.
1987          */
1988         if (ia64_platform_is("sn2")) {
1989                 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1990                 if (IS_QLA23XX(ha))
1991                         nv->special_options[1] = BIT_7;
1992         }
1993 #endif
1994
1995         /* Reset Initialization control block */
1996         memset(icb, 0, ha->init_cb_size);
1997
1998         /*
1999          * Setup driver NVRAM options.
2000          */
2001         nv->firmware_options[0] |= (BIT_6 | BIT_1);
2002         nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
2003         nv->firmware_options[1] |= (BIT_5 | BIT_0);
2004         nv->firmware_options[1] &= ~BIT_4;
2005
2006         if (IS_QLA23XX(ha)) {
2007                 nv->firmware_options[0] |= BIT_2;
2008                 nv->firmware_options[0] &= ~BIT_3;
2009                 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
2010
2011                 if (IS_QLA2300(ha)) {
2012                         if (ha->fb_rev == FPM_2310) {
2013                                 strcpy(ha->model_number, "QLA2310");
2014                         } else {
2015                                 strcpy(ha->model_number, "QLA2300");
2016                         }
2017                 } else {
2018                         qla2x00_set_model_info(vha, nv->model_number,
2019                             sizeof(nv->model_number), "QLA23xx");
2020                 }
2021         } else if (IS_QLA2200(ha)) {
2022                 nv->firmware_options[0] |= BIT_2;
2023                 /*
2024                  * 'Point-to-point preferred, else loop' is not a safe
2025                  * connection mode setting.
2026                  */
2027                 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
2028                     (BIT_5 | BIT_4)) {
2029                         /* Force 'loop preferred, else point-to-point'. */
2030                         nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
2031                         nv->add_firmware_options[0] |= BIT_5;
2032                 }
2033                 strcpy(ha->model_number, "QLA22xx");
2034         } else /*if (IS_QLA2100(ha))*/ {
2035                 strcpy(ha->model_number, "QLA2100");
2036         }
2037
2038         /*
2039          * Copy over NVRAM RISC parameter block to initialization control block.
2040          */
2041         dptr1 = (uint8_t *)icb;
2042         dptr2 = (uint8_t *)&nv->parameter_block_version;
2043         cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
2044         while (cnt--)
2045                 *dptr1++ = *dptr2++;
2046
2047         /* Copy 2nd half. */
2048         dptr1 = (uint8_t *)icb->add_firmware_options;
2049         cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
2050         while (cnt--)
2051                 *dptr1++ = *dptr2++;
2052
2053         /* Use alternate WWN? */
2054         if (nv->host_p[1] & BIT_7) {
2055                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
2056                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
2057         }
2058
2059         /* Prepare nodename */
2060         if ((icb->firmware_options[1] & BIT_6) == 0) {
2061                 /*
2062                  * Firmware will apply the following mask if the nodename was
2063                  * not provided.
2064                  */
2065                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
2066                 icb->node_name[0] &= 0xF0;
2067         }
2068
2069         /*
2070          * Set host adapter parameters.
2071          */
2072         if (nv->host_p[0] & BIT_7)
2073                 ql2xextended_error_logging = 1;
2074         ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
2075         /* Always load RISC code on non ISP2[12]00 chips. */
2076         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2077                 ha->flags.disable_risc_code_load = 0;
2078         ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
2079         ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
2080         ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
2081         ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
2082         ha->flags.disable_serdes = 0;
2083
2084         ha->operating_mode =
2085             (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
2086
2087         memcpy(ha->fw_seriallink_options, nv->seriallink_options,
2088             sizeof(ha->fw_seriallink_options));
2089
2090         /* save HBA serial number */
2091         ha->serial0 = icb->port_name[5];
2092         ha->serial1 = icb->port_name[6];
2093         ha->serial2 = icb->port_name[7];
2094         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
2095         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
2096
2097         icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
2098
2099         ha->retry_count = nv->retry_count;
2100
2101         /* Set minimum login_timeout to 4 seconds. */
2102         if (nv->login_timeout < ql2xlogintimeout)
2103                 nv->login_timeout = ql2xlogintimeout;
2104         if (nv->login_timeout < 4)
2105                 nv->login_timeout = 4;
2106         ha->login_timeout = nv->login_timeout;
2107         icb->login_timeout = nv->login_timeout;
2108
2109         /* Set minimum RATOV to 100 tenths of a second. */
2110         ha->r_a_tov = 100;
2111
2112         ha->loop_reset_delay = nv->reset_delay;
2113
2114         /* Link Down Timeout = 0:
2115          *
2116          *      When Port Down timer expires we will start returning
2117          *      I/O's to OS with "DID_NO_CONNECT".
2118          *
2119          * Link Down Timeout != 0:
2120          *
2121          *       The driver waits for the link to come up after link down
2122          *       before returning I/Os to OS with "DID_NO_CONNECT".
2123          */
2124         if (nv->link_down_timeout == 0) {
2125                 ha->loop_down_abort_time =
2126                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
2127         } else {
2128                 ha->link_down_timeout =  nv->link_down_timeout;
2129                 ha->loop_down_abort_time =
2130                     (LOOP_DOWN_TIME - ha->link_down_timeout);
2131         }
2132
2133         /*
2134          * Need enough time to try and get the port back.
2135          */
2136         ha->port_down_retry_count = nv->port_down_retry_count;
2137         if (qlport_down_retry)
2138                 ha->port_down_retry_count = qlport_down_retry;
2139         /* Set login_retry_count */
2140         ha->login_retry_count  = nv->retry_count;
2141         if (ha->port_down_retry_count == nv->port_down_retry_count &&
2142             ha->port_down_retry_count > 3)
2143                 ha->login_retry_count = ha->port_down_retry_count;
2144         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
2145                 ha->login_retry_count = ha->port_down_retry_count;
2146         if (ql2xloginretrycount)
2147                 ha->login_retry_count = ql2xloginretrycount;
2148
2149         icb->lun_enables = __constant_cpu_to_le16(0);
2150         icb->command_resource_count = 0;
2151         icb->immediate_notify_resource_count = 0;
2152         icb->timeout = __constant_cpu_to_le16(0);
2153
2154         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2155                 /* Enable RIO */
2156                 icb->firmware_options[0] &= ~BIT_3;
2157                 icb->add_firmware_options[0] &=
2158                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
2159                 icb->add_firmware_options[0] |= BIT_2;
2160                 icb->response_accumulation_timer = 3;
2161                 icb->interrupt_delay_timer = 5;
2162
2163                 vha->flags.process_response_queue = 1;
2164         } else {
2165                 /* Enable ZIO. */
2166                 if (!vha->flags.init_done) {
2167                         ha->zio_mode = icb->add_firmware_options[0] &
2168                             (BIT_3 | BIT_2 | BIT_1 | BIT_0);
2169                         ha->zio_timer = icb->interrupt_delay_timer ?
2170                             icb->interrupt_delay_timer: 2;
2171                 }
2172                 icb->add_firmware_options[0] &=
2173                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
2174                 vha->flags.process_response_queue = 0;
2175                 if (ha->zio_mode != QLA_ZIO_DISABLED) {
2176                         ha->zio_mode = QLA_ZIO_MODE_6;
2177
2178                         DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
2179                             "delay (%d us).\n", vha->host_no, ha->zio_mode,
2180                             ha->zio_timer * 100));
2181                         qla_printk(KERN_INFO, ha,
2182                             "ZIO mode %d enabled; timer delay (%d us).\n",
2183                             ha->zio_mode, ha->zio_timer * 100);
2184
2185                         icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
2186                         icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
2187                         vha->flags.process_response_queue = 1;
2188                 }
2189         }
2190
2191         if (rval) {
2192                 DEBUG2_3(printk(KERN_WARNING
2193                     "scsi(%ld): NVRAM configuration failed!\n", vha->host_no));
2194         }
2195         return (rval);
2196 }
2197
2198 static void
2199 qla2x00_rport_del(void *data)
2200 {
2201         fc_port_t *fcport = data;
2202         struct fc_rport *rport;
2203
2204         spin_lock_irq(fcport->vha->host->host_lock);
2205         rport = fcport->drport ? fcport->drport: fcport->rport;
2206         fcport->drport = NULL;
2207         spin_unlock_irq(fcport->vha->host->host_lock);
2208         if (rport)
2209                 fc_remote_port_delete(rport);
2210 }
2211
2212 /**
2213  * qla2x00_alloc_fcport() - Allocate a generic fcport.
2214  * @ha: HA context
2215  * @flags: allocation flags
2216  *
2217  * Returns a pointer to the allocated fcport, or NULL, if none available.
2218  */
2219 static fc_port_t *
2220 qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
2221 {
2222         fc_port_t *fcport;
2223
2224         fcport = kzalloc(sizeof(fc_port_t), flags);
2225         if (!fcport)
2226                 return NULL;
2227
2228         /* Setup fcport template structure. */
2229         fcport->vha = vha;
2230         fcport->vp_idx = vha->vp_idx;
2231         fcport->port_type = FCT_UNKNOWN;
2232         fcport->loop_id = FC_NO_LOOP_ID;
2233         atomic_set(&fcport->state, FCS_UNCONFIGURED);
2234         fcport->supported_classes = FC_COS_UNSPECIFIED;
2235
2236         return fcport;
2237 }
2238
2239 /*
2240  * qla2x00_configure_loop
2241  *      Updates Fibre Channel Device Database with what is actually on loop.
2242  *
2243  * Input:
2244  *      ha                = adapter block pointer.
2245  *
2246  * Returns:
2247  *      0 = success.
2248  *      1 = error.
2249  *      2 = database was full and device was not configured.
2250  */
2251 static int
2252 qla2x00_configure_loop(scsi_qla_host_t *vha)
2253 {
2254         int  rval;
2255         unsigned long flags, save_flags;
2256         struct qla_hw_data *ha = vha->hw;
2257         rval = QLA_SUCCESS;
2258
2259         /* Get Initiator ID */
2260         if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
2261                 rval = qla2x00_configure_hba(vha);
2262                 if (rval != QLA_SUCCESS) {
2263                         DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
2264                             vha->host_no));
2265                         return (rval);
2266                 }
2267         }
2268
2269         save_flags = flags = vha->dpc_flags;
2270         DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
2271             vha->host_no, flags));
2272
2273         /*
2274          * If we have both an RSCN and PORT UPDATE pending then handle them
2275          * both at the same time.
2276          */
2277         clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
2278         clear_bit(RSCN_UPDATE, &vha->dpc_flags);
2279
2280         qla2x00_get_data_rate(vha);
2281
2282         /* Determine what we need to do */
2283         if (ha->current_topology == ISP_CFG_FL &&
2284             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2285
2286                 vha->flags.rscn_queue_overflow = 1;
2287                 set_bit(RSCN_UPDATE, &flags);
2288
2289         } else if (ha->current_topology == ISP_CFG_F &&
2290             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
2291
2292                 vha->flags.rscn_queue_overflow = 1;
2293                 set_bit(RSCN_UPDATE, &flags);
2294                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
2295
2296         } else if (ha->current_topology == ISP_CFG_N) {
2297                 clear_bit(RSCN_UPDATE, &flags);
2298
2299         } else if (!vha->flags.online ||
2300             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
2301
2302                 vha->flags.rscn_queue_overflow = 1;
2303                 set_bit(RSCN_UPDATE, &flags);
2304                 set_bit(LOCAL_LOOP_UPDATE, &flags);
2305         }
2306
2307         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
2308                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2309                         rval = QLA_FUNCTION_FAILED;
2310                 else
2311                         rval = qla2x00_configure_local_loop(vha);
2312         }
2313
2314         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
2315                 if (LOOP_TRANSITION(vha))
2316                         rval = QLA_FUNCTION_FAILED;
2317                 else
2318                         rval = qla2x00_configure_fabric(vha);
2319         }
2320
2321         if (rval == QLA_SUCCESS) {
2322                 if (atomic_read(&vha->loop_down_timer) ||
2323                     test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
2324                         rval = QLA_FUNCTION_FAILED;
2325                 } else {
2326                         atomic_set(&vha->loop_state, LOOP_READY);
2327
2328                         DEBUG(printk("scsi(%ld): LOOP READY\n", vha->host_no));
2329                 }
2330         }
2331
2332         if (rval) {
2333                 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
2334                     __func__, vha->host_no));
2335         } else {
2336                 DEBUG3(printk("%s: exiting normally\n", __func__));
2337         }
2338
2339         /* Restore state if a resync event occurred during processing */
2340         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
2341                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
2342                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
2343                 if (test_bit(RSCN_UPDATE, &save_flags)) {
2344                         set_bit(RSCN_UPDATE, &vha->dpc_flags);
2345                         vha->flags.rscn_queue_overflow = 1;
2346                 }
2347         }
2348
2349         return (rval);
2350 }
2351
2352
2353
2354 /*
2355  * qla2x00_configure_local_loop
2356  *      Updates Fibre Channel Device Database with local loop devices.
2357  *
2358  * Input:
2359  *      ha = adapter block pointer.
2360  *
2361  * Returns:
2362  *      0 = success.
2363  */
2364 static int
2365 qla2x00_configure_local_loop(scsi_qla_host_t *vha)
2366 {
2367         int             rval, rval2;
2368         int             found_devs;
2369         int             found;
2370         fc_port_t       *fcport, *new_fcport;
2371
2372         uint16_t        index;
2373         uint16_t        entries;
2374         char            *id_iter;
2375         uint16_t        loop_id;
2376         uint8_t         domain, area, al_pa;
2377         struct qla_hw_data *ha = vha->hw;
2378
2379         found_devs = 0;
2380         new_fcport = NULL;
2381         entries = MAX_FIBRE_DEVICES;
2382
2383         DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", vha->host_no));
2384         DEBUG3(qla2x00_get_fcal_position_map(vha, NULL));
2385
2386         /* Get list of logged in devices. */
2387         memset(ha->gid_list, 0, GID_LIST_SIZE);
2388         rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
2389             &entries);
2390         if (rval != QLA_SUCCESS)
2391                 goto cleanup_allocation;
2392
2393         DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
2394             vha->host_no, entries));
2395         DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
2396             entries * sizeof(struct gid_list_info)));
2397
2398         /* Allocate temporary fcport for any new fcports discovered. */
2399         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2400         if (new_fcport == NULL) {
2401                 rval = QLA_MEMORY_ALLOC_FAILED;
2402                 goto cleanup_allocation;
2403         }
2404         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2405
2406         /*
2407          * Mark local devices that were present with FCF_DEVICE_LOST for now.
2408          */
2409         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2410                 if (atomic_read(&fcport->state) == FCS_ONLINE &&
2411                     fcport->port_type != FCT_BROADCAST &&
2412                     (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2413
2414                         DEBUG(printk("scsi(%ld): Marking port lost, "
2415                             "loop_id=0x%04x\n",
2416                             vha->host_no, fcport->loop_id));
2417
2418                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
2419                 }
2420         }
2421
2422         /* Add devices to port list. */
2423         id_iter = (char *)ha->gid_list;
2424         for (index = 0; index < entries; index++) {
2425                 domain = ((struct gid_list_info *)id_iter)->domain;
2426                 area = ((struct gid_list_info *)id_iter)->area;
2427                 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
2428                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
2429                         loop_id = (uint16_t)
2430                             ((struct gid_list_info *)id_iter)->loop_id_2100;
2431                 else
2432                         loop_id = le16_to_cpu(
2433                             ((struct gid_list_info *)id_iter)->loop_id);
2434                 id_iter += ha->gid_list_info_size;
2435
2436                 /* Bypass reserved domain fields. */
2437                 if ((domain & 0xf0) == 0xf0)
2438                         continue;
2439
2440                 /* Bypass if not same domain and area of adapter. */
2441                 if (area && domain &&
2442                     (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
2443                         continue;
2444
2445                 /* Bypass invalid local loop ID. */
2446                 if (loop_id > LAST_LOCAL_LOOP_ID)
2447                         continue;
2448
2449                 /* Fill in member data. */
2450                 new_fcport->d_id.b.domain = domain;
2451                 new_fcport->d_id.b.area = area;
2452                 new_fcport->d_id.b.al_pa = al_pa;
2453                 new_fcport->loop_id = loop_id;
2454                 new_fcport->vp_idx = vha->vp_idx;
2455                 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
2456                 if (rval2 != QLA_SUCCESS) {
2457                         DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2458                             "information -- get_port_database=%x, "
2459                             "loop_id=0x%04x\n",
2460                             vha->host_no, rval2, new_fcport->loop_id));
2461                         DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2462                             vha->host_no));
2463                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
2464                         continue;
2465                 }
2466
2467                 /* Check for matching device in port list. */
2468                 found = 0;
2469                 fcport = NULL;
2470                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2471                         if (memcmp(new_fcport->port_name, fcport->port_name,
2472                             WWN_SIZE))
2473                                 continue;
2474
2475                         fcport->flags &= ~FCF_FABRIC_DEVICE;
2476                         fcport->loop_id = new_fcport->loop_id;
2477                         fcport->port_type = new_fcport->port_type;
2478                         fcport->d_id.b24 = new_fcport->d_id.b24;
2479                         memcpy(fcport->node_name, new_fcport->node_name,
2480                             WWN_SIZE);
2481
2482                         found++;
2483                         break;
2484                 }
2485
2486                 if (!found) {
2487                         /* New device, add to fcports list. */
2488                         if (vha->vp_idx) {
2489                                 new_fcport->vha = vha;
2490                                 new_fcport->vp_idx = vha->vp_idx;
2491                         }
2492                         list_add_tail(&new_fcport->list, &vha->vp_fcports);
2493
2494                         /* Allocate a new replacement fcport. */
2495                         fcport = new_fcport;
2496                         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2497                         if (new_fcport == NULL) {
2498                                 rval = QLA_MEMORY_ALLOC_FAILED;
2499                                 goto cleanup_allocation;
2500                         }
2501                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2502                 }
2503
2504                 /* Base iIDMA settings on HBA port speed. */
2505                 fcport->fp_speed = ha->link_data_rate;
2506
2507                 qla2x00_update_fcport(vha, fcport);
2508
2509                 found_devs++;
2510         }
2511
2512 cleanup_allocation:
2513         kfree(new_fcport);
2514
2515         if (rval != QLA_SUCCESS) {
2516                 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2517                     "rval=%x\n", vha->host_no, rval));
2518         }
2519
2520         return (rval);
2521 }
2522
2523 static void
2524 qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
2525 {
2526 #define LS_UNKNOWN      2
2527         static char *link_speeds[] = { "1", "2", "?", "4", "8", "10" };
2528         char *link_speed;
2529         int rval;
2530         uint16_t mb[4];
2531         struct qla_hw_data *ha = vha->hw;
2532
2533         if (!IS_IIDMA_CAPABLE(ha))
2534                 return;
2535
2536         if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
2537             fcport->fp_speed > ha->link_data_rate)
2538                 return;
2539
2540         rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
2541             mb);
2542         if (rval != QLA_SUCCESS) {
2543                 DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
2544                     "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
2545                     vha->host_no, fcport->port_name[0], fcport->port_name[1],
2546                     fcport->port_name[2], fcport->port_name[3],
2547                     fcport->port_name[4], fcport->port_name[5],
2548                     fcport->port_name[6], fcport->port_name[7], rval,
2549                     fcport->fp_speed, mb[0], mb[1]));
2550         } else {
2551                 link_speed = link_speeds[LS_UNKNOWN];
2552                 if (fcport->fp_speed < 5)
2553                         link_speed = link_speeds[fcport->fp_speed];
2554                 else if (fcport->fp_speed == 0x13)
2555                         link_speed = link_speeds[5];
2556                 DEBUG2(qla_printk(KERN_INFO, ha,
2557                     "iIDMA adjusted to %s GB/s on "
2558                     "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
2559                     link_speed, fcport->port_name[0],
2560                     fcport->port_name[1], fcport->port_name[2],
2561                     fcport->port_name[3], fcport->port_name[4],
2562                     fcport->port_name[5], fcport->port_name[6],
2563                     fcport->port_name[7]));
2564         }
2565 }
2566
2567 static void
2568 qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
2569 {
2570         struct fc_rport_identifiers rport_ids;
2571         struct fc_rport *rport;
2572         struct qla_hw_data *ha = vha->hw;
2573
2574         qla2x00_rport_del(fcport);
2575
2576         rport_ids.node_name = wwn_to_u64(fcport->node_name);
2577         rport_ids.port_name = wwn_to_u64(fcport->port_name);
2578         rport_ids.port_id = fcport->d_id.b.domain << 16 |
2579             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2580         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2581         fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
2582         if (!rport) {
2583                 qla_printk(KERN_WARNING, ha,
2584                     "Unable to allocate fc remote port!\n");
2585                 return;
2586         }
2587         spin_lock_irq(fcport->vha->host->host_lock);
2588         *((fc_port_t **)rport->dd_data) = fcport;
2589         spin_unlock_irq(fcport->vha->host->host_lock);
2590
2591         rport->supported_classes = fcport->supported_classes;
2592
2593         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2594         if (fcport->port_type == FCT_INITIATOR)
2595                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2596         if (fcport->port_type == FCT_TARGET)
2597                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2598         fc_remote_port_rolechg(rport, rport_ids.roles);
2599 }
2600
2601 /*
2602  * qla2x00_update_fcport
2603  *      Updates device on list.
2604  *
2605  * Input:
2606  *      ha = adapter block pointer.
2607  *      fcport = port structure pointer.
2608  *
2609  * Return:
2610  *      0  - Success
2611  *  BIT_0 - error
2612  *
2613  * Context:
2614  *      Kernel context.
2615  */
2616 void
2617 qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
2618 {
2619         struct qla_hw_data *ha = vha->hw;
2620
2621         fcport->vha = vha;
2622         fcport->login_retry = 0;
2623         fcport->port_login_retry_count = ha->port_down_retry_count *
2624             PORT_RETRY_TIME;
2625         atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2626             PORT_RETRY_TIME);
2627         fcport->flags &= ~FCF_LOGIN_NEEDED;
2628
2629         qla2x00_iidma_fcport(vha, fcport);
2630
2631         atomic_set(&fcport->state, FCS_ONLINE);
2632
2633         qla2x00_reg_remote_port(vha, fcport);
2634 }
2635
2636 /*
2637  * qla2x00_configure_fabric
2638  *      Setup SNS devices with loop ID's.
2639  *
2640  * Input:
2641  *      ha = adapter block pointer.
2642  *
2643  * Returns:
2644  *      0 = success.
2645  *      BIT_0 = error
2646  */
2647 static int
2648 qla2x00_configure_fabric(scsi_qla_host_t *vha)
2649 {
2650         int     rval, rval2;
2651         fc_port_t       *fcport, *fcptemp;
2652         uint16_t        next_loopid;
2653         uint16_t        mb[MAILBOX_REGISTER_COUNT];
2654         uint16_t        loop_id;
2655         LIST_HEAD(new_fcports);
2656         struct qla_hw_data *ha = vha->hw;
2657         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2658
2659         /* If FL port exists, then SNS is present */
2660         if (IS_FWI2_CAPABLE(ha))
2661                 loop_id = NPH_F_PORT;
2662         else
2663                 loop_id = SNS_FL_PORT;
2664         rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
2665         if (rval != QLA_SUCCESS) {
2666                 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2667                     "Port\n", vha->host_no));
2668
2669                 vha->device_flags &= ~SWITCH_FOUND;
2670                 return (QLA_SUCCESS);
2671         }
2672         vha->device_flags |= SWITCH_FOUND;
2673
2674         /* Mark devices that need re-synchronization. */
2675         rval2 = qla2x00_device_resync(vha);
2676         if (rval2 == QLA_RSCNS_HANDLED) {
2677                 /* No point doing the scan, just continue. */
2678                 return (QLA_SUCCESS);
2679         }
2680         do {
2681                 /* FDMI support. */
2682                 if (ql2xfdmienable &&
2683                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
2684                         qla2x00_fdmi_register(vha);
2685
2686                 /* Ensure we are logged into the SNS. */
2687                 if (IS_FWI2_CAPABLE(ha))
2688                         loop_id = NPH_SNS;
2689                 else
2690                         loop_id = SIMPLE_NAME_SERVER;
2691                 ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
2692                     0xfc, mb, BIT_1 | BIT_0);
2693                 if (mb[0] != MBS_COMMAND_COMPLETE) {
2694                         DEBUG2(qla_printk(KERN_INFO, ha,
2695                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
2696                             "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
2697                             mb[0], mb[1], mb[2], mb[6], mb[7]));
2698                         return (QLA_SUCCESS);
2699                 }
2700
2701                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
2702                         if (qla2x00_rft_id(vha)) {
2703                                 /* EMPTY */
2704                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2705                                     "TYPE failed.\n", vha->host_no));
2706                         }
2707                         if (qla2x00_rff_id(vha)) {
2708                                 /* EMPTY */
2709                                 DEBUG2(printk("scsi(%ld): Register FC-4 "
2710                                     "Features failed.\n", vha->host_no));
2711                         }
2712                         if (qla2x00_rnn_id(vha)) {
2713                                 /* EMPTY */
2714                                 DEBUG2(printk("scsi(%ld): Register Node Name "
2715                                     "failed.\n", vha->host_no));
2716                         } else if (qla2x00_rsnn_nn(vha)) {
2717                                 /* EMPTY */
2718                                 DEBUG2(printk("scsi(%ld): Register Symbolic "
2719                                     "Node Name failed.\n", vha->host_no));
2720                         }
2721                 }
2722
2723                 rval = qla2x00_find_all_fabric_devs(vha, &new_fcports);
2724                 if (rval != QLA_SUCCESS)
2725                         break;
2726
2727                 /*
2728                  * Logout all previous fabric devices marked lost, except
2729                  * tape devices.
2730                  */
2731                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2732                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2733                                 break;
2734
2735                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2736                                 continue;
2737
2738                         if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2739                                 qla2x00_mark_device_lost(vha, fcport,
2740                                     ql2xplogiabsentdevice, 0);
2741                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
2742                                     (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2743                                     fcport->port_type != FCT_INITIATOR &&
2744                                     fcport->port_type != FCT_BROADCAST) {
2745                                         ha->isp_ops->fabric_logout(vha,
2746                                             fcport->loop_id,
2747                                             fcport->d_id.b.domain,
2748                                             fcport->d_id.b.area,
2749                                             fcport->d_id.b.al_pa);
2750                                         fcport->loop_id = FC_NO_LOOP_ID;
2751                                 }
2752                         }
2753                 }
2754
2755                 /* Starting free loop ID. */
2756                 next_loopid = ha->min_external_loopid;
2757
2758                 /*
2759                  * Scan through our port list and login entries that need to be
2760                  * logged in.
2761                  */
2762                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2763                         if (atomic_read(&vha->loop_down_timer) ||
2764                             test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2765                                 break;
2766
2767                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2768                             (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2769                                 continue;
2770
2771                         if (fcport->loop_id == FC_NO_LOOP_ID) {
2772                                 fcport->loop_id = next_loopid;
2773                                 rval = qla2x00_find_new_loop_id(
2774                                     base_vha, fcport);
2775                                 if (rval != QLA_SUCCESS) {
2776                                         /* Ran out of IDs to use */
2777                                         break;
2778                                 }
2779                         }
2780                         /* Login and update database */
2781                         qla2x00_fabric_dev_login(vha, fcport, &next_loopid);
2782                 }
2783
2784                 /* Exit if out of loop IDs. */
2785                 if (rval != QLA_SUCCESS) {
2786                         break;
2787                 }
2788
2789                 /*
2790                  * Login and add the new devices to our port list.
2791                  */
2792                 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2793                         if (atomic_read(&vha->loop_down_timer) ||
2794                             test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2795                                 break;
2796
2797                         /* Find a new loop ID to use. */
2798                         fcport->loop_id = next_loopid;
2799                         rval = qla2x00_find_new_loop_id(base_vha, fcport);
2800                         if (rval != QLA_SUCCESS) {
2801                                 /* Ran out of IDs to use */
2802                                 break;
2803                         }
2804
2805                         /* Login and update database */
2806                         qla2x00_fabric_dev_login(vha, fcport, &next_loopid);
2807
2808                         if (vha->vp_idx) {
2809                                 fcport->vha = vha;
2810                                 fcport->vp_idx = vha->vp_idx;
2811                         }
2812                         list_move_tail(&fcport->list, &vha->vp_fcports);
2813                 }
2814         } while (0);
2815
2816         /* Free all new device structures not processed. */
2817         list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2818                 list_del(&fcport->list);
2819                 kfree(fcport);
2820         }
2821
2822         if (rval) {
2823                 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2824                     "rval=%d\n", vha->host_no, rval));
2825         }
2826
2827         return (rval);
2828 }
2829
2830
2831 /*
2832  * qla2x00_find_all_fabric_devs
2833  *
2834  * Input:
2835  *      ha = adapter block pointer.
2836  *      dev = database device entry pointer.
2837  *
2838  * Returns:
2839  *      0 = success.
2840  *
2841  * Context:
2842  *      Kernel context.
2843  */
2844 static int
2845 qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha,
2846         struct list_head *new_fcports)
2847 {
2848         int             rval;
2849         uint16_t        loop_id;
2850         fc_port_t       *fcport, *new_fcport, *fcptemp;
2851         int             found;
2852
2853         sw_info_t       *swl;
2854         int             swl_idx;
2855         int             first_dev, last_dev;
2856         port_id_t       wrap, nxt_d_id;
2857         struct qla_hw_data *ha = vha->hw;
2858         struct scsi_qla_host *vp, *base_vha = pci_get_drvdata(ha->pdev);
2859         struct scsi_qla_host *tvp;
2860
2861         rval = QLA_SUCCESS;
2862
2863         /* Try GID_PT to get device list, else GAN. */
2864         swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_KERNEL);
2865         if (!swl) {
2866                 /*EMPTY*/
2867                 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2868                     "on GA_NXT\n", vha->host_no));
2869         } else {
2870                 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
2871                         kfree(swl);
2872                         swl = NULL;
2873                 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
2874                         kfree(swl);
2875                         swl = NULL;
2876                 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
2877                         kfree(swl);
2878                         swl = NULL;
2879                 } else if (ql2xiidmaenable &&
2880                     qla2x00_gfpn_id(vha, swl) == QLA_SUCCESS) {
2881                         qla2x00_gpsc(vha, swl);
2882                 }
2883         }
2884         swl_idx = 0;
2885
2886         /* Allocate temporary fcport for any new fcports discovered. */
2887         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2888         if (new_fcport == NULL) {
2889                 kfree(swl);
2890                 return (QLA_MEMORY_ALLOC_FAILED);
2891         }
2892         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2893         /* Set start port ID scan at adapter ID. */
2894         first_dev = 1;
2895         last_dev = 0;
2896
2897         /* Starting free loop ID. */
2898         loop_id = ha->min_external_loopid;
2899         for (; loop_id <= ha->max_loop_id; loop_id++) {
2900                 if (qla2x00_is_reserved_id(vha, loop_id))
2901                         continue;
2902
2903                 if (atomic_read(&vha->loop_down_timer) || LOOP_TRANSITION(vha))
2904                         break;
2905
2906                 if (swl != NULL) {
2907                         if (last_dev) {
2908                                 wrap.b24 = new_fcport->d_id.b24;
2909                         } else {
2910                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2911                                 memcpy(new_fcport->node_name,
2912                                     swl[swl_idx].node_name, WWN_SIZE);
2913                                 memcpy(new_fcport->port_name,
2914                                     swl[swl_idx].port_name, WWN_SIZE);
2915                                 memcpy(new_fcport->fabric_port_name,
2916                                     swl[swl_idx].fabric_port_name, WWN_SIZE);
2917                                 new_fcport->fp_speed = swl[swl_idx].fp_speed;
2918
2919                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2920                                         last_dev = 1;
2921                                 }
2922                                 swl_idx++;
2923                         }
2924                 } else {
2925                         /* Send GA_NXT to the switch */
2926                         rval = qla2x00_ga_nxt(vha, new_fcport);
2927                         if (rval != QLA_SUCCESS) {
2928                                 qla_printk(KERN_WARNING, ha,
2929                                     "SNS scan failed -- assuming zero-entry "
2930                                     "result...\n");
2931                                 list_for_each_entry_safe(fcport, fcptemp,
2932                                     new_fcports, list) {
2933                                         list_del(&fcport->list);
2934                                         kfree(fcport);
2935                                 }
2936                                 rval = QLA_SUCCESS;
2937                                 break;
2938                         }
2939                 }
2940
2941                 /* If wrap on switch device list, exit. */
2942                 if (first_dev) {
2943                         wrap.b24 = new_fcport->d_id.b24;
2944                         first_dev = 0;
2945                 } else if (new_fcport->d_id.b24 == wrap.b24) {
2946                         DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2947                             vha->host_no, new_fcport->d_id.b.domain,
2948                             new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2949                         break;
2950                 }
2951
2952                 /* Bypass if same physical adapter. */
2953                 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
2954                         continue;
2955
2956                 /* Bypass virtual ports of the same host. */
2957                 found = 0;
2958                 if (ha->num_vhosts) {
2959                         list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
2960                                 if (new_fcport->d_id.b24 == vp->d_id.b24) {
2961                                         found = 1;
2962                                         break;
2963                                 }
2964                         }
2965                         if (found)
2966                                 continue;
2967                 }
2968
2969                 /* Bypass if same domain and area of adapter. */
2970                 if (((new_fcport->d_id.b24 & 0xffff00) ==
2971                     (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2972                         ISP_CFG_FL)
2973                             continue;
2974
2975                 /* Bypass reserved domain fields. */
2976                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2977                         continue;
2978
2979                 /* Locate matching device in database. */
2980                 found = 0;
2981                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2982                         if (memcmp(new_fcport->port_name, fcport->port_name,
2983                             WWN_SIZE))
2984                                 continue;
2985
2986                         found++;
2987
2988                         /* Update port state. */
2989                         memcpy(fcport->fabric_port_name,
2990                             new_fcport->fabric_port_name, WWN_SIZE);
2991                         fcport->fp_speed = new_fcport->fp_speed;
2992
2993                         /*
2994                          * If address the same and state FCS_ONLINE, nothing
2995                          * changed.
2996                          */
2997                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2998                             atomic_read(&fcport->state) == FCS_ONLINE) {
2999                                 break;
3000                         }
3001
3002                         /*
3003                          * If device was not a fabric device before.
3004                          */
3005                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
3006                                 fcport->d_id.b24 = new_fcport->d_id.b24;
3007                                 fcport->loop_id = FC_NO_LOOP_ID;
3008                                 fcport->flags |= (FCF_FABRIC_DEVICE |
3009                                     FCF_LOGIN_NEEDED);
3010                                 break;
3011                         }
3012
3013                         /*
3014                          * Port ID changed or device was marked to be updated;
3015                          * Log it out if still logged in and mark it for
3016                          * relogin later.
3017                          */
3018                         fcport->d_id.b24 = new_fcport->d_id.b24;
3019                         fcport->flags |= FCF_LOGIN_NEEDED;
3020                         if (fcport->loop_id != FC_NO_LOOP_ID &&
3021                             (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
3022                             fcport->port_type != FCT_INITIATOR &&
3023                             fcport->port_type != FCT_BROADCAST) {
3024                                 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
3025                                     fcport->d_id.b.domain, fcport->d_id.b.area,
3026                                     fcport->d_id.b.al_pa);
3027                                 fcport->loop_id = FC_NO_LOOP_ID;
3028                         }
3029
3030                         break;
3031                 }
3032
3033                 if (found)
3034                         continue;
3035                 /* If device was not in our fcports list, then add it. */
3036                 list_add_tail(&new_fcport->list, new_fcports);
3037
3038                 /* Allocate a new replacement fcport. */
3039                 nxt_d_id.b24 = new_fcport->d_id.b24;
3040                 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
3041                 if (new_fcport == NULL) {
3042                         kfree(swl);
3043                         return (QLA_MEMORY_ALLOC_FAILED);
3044                 }
3045                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
3046                 new_fcport->d_id.b24 = nxt_d_id.b24;
3047         }
3048
3049         kfree(swl);
3050         kfree(new_fcport);
3051
3052         return (rval);
3053 }
3054
3055 /*
3056  * qla2x00_find_new_loop_id
3057  *      Scan through our port list and find a new usable loop ID.
3058  *
3059  * Input:
3060  *      ha:     adapter state pointer.
3061  *      dev:    port structure pointer.
3062  *
3063  * Returns:
3064  *      qla2x00 local function return status code.
3065  *
3066  * Context:
3067  *      Kernel context.
3068  */
3069 static int
3070 qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
3071 {
3072         int     rval;
3073         int     found;
3074         fc_port_t *fcport;
3075         uint16_t first_loop_id;
3076         struct qla_hw_data *ha = vha->hw;
3077         struct scsi_qla_host *vp;
3078         struct scsi_qla_host *tvp;
3079
3080         rval = QLA_SUCCESS;
3081
3082         /* Save starting loop ID. */
3083         first_loop_id = dev->loop_id;
3084
3085         for (;;) {
3086                 /* Skip loop ID if already used by adapter. */
3087                 if (dev->loop_id == vha->loop_id)
3088                         dev->loop_id++;
3089
3090                 /* Skip reserved loop IDs. */
3091                 while (qla2x00_is_reserved_id(vha, dev->loop_id))
3092                         dev->loop_id++;
3093
3094                 /* Reset loop ID if passed the end. */
3095                 if (dev->loop_id > ha->max_loop_id) {
3096                         /* first loop ID. */
3097                         dev->loop_id = ha->min_external_loopid;
3098                 }
3099
3100                 /* Check for loop ID being already in use. */
3101                 found = 0;
3102                 fcport = NULL;
3103                 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
3104                         list_for_each_entry(fcport, &vp->vp_fcports, list) {
3105                                 if (fcport->loop_id == dev->loop_id &&
3106                                                                 fcport != dev) {
3107                                         /* ID possibly in use */
3108                                         found++;
3109                                         break;
3110                                 }
3111                         }
3112                         if (found)
3113                                 break;
3114                 }
3115
3116                 /* If not in use then it is free to use. */
3117                 if (!found) {
3118                         break;
3119                 }
3120
3121                 /* ID in use. Try next value. */
3122                 dev->loop_id++;
3123
3124                 /* If wrap around. No free ID to use. */
3125                 if (dev->loop_id == first_loop_id) {
3126                         dev->loop_id = FC_NO_LOOP_ID;
3127                         rval = QLA_FUNCTION_FAILED;
3128                         break;
3129                 }
3130         }
3131
3132         return (rval);
3133 }
3134
3135 /*
3136  * qla2x00_device_resync
3137  *      Marks devices in the database that needs resynchronization.
3138  *
3139  * Input:
3140  *      ha = adapter block pointer.
3141  *