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