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