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