xref: /linux/drivers/scsi/qla2xxx/qla_init.c (revision e0fcfb086fbbb6233de1062d4b2f05e9afedab3b)
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 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 #include <target/target_core_base.h>
21 #include "qla_target.h"
22 
23 /*
24 *  QLogic ISP2x00 Hardware Support Function Prototypes.
25 */
26 static int qla2x00_isp_firmware(scsi_qla_host_t *);
27 static int qla2x00_setup_chip(scsi_qla_host_t *);
28 static int qla2x00_fw_ready(scsi_qla_host_t *);
29 static int qla2x00_configure_hba(scsi_qla_host_t *);
30 static int qla2x00_configure_loop(scsi_qla_host_t *);
31 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
32 static int qla2x00_configure_fabric(scsi_qla_host_t *);
33 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *);
34 static int qla2x00_restart_isp(scsi_qla_host_t *);
35 
36 static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
37 static int qla84xx_init_chip(scsi_qla_host_t *);
38 static int qla25xx_init_queues(struct qla_hw_data *);
39 static int qla24xx_post_prli_work(struct scsi_qla_host*, fc_port_t *);
40 static void qla24xx_handle_plogi_done_event(struct scsi_qla_host *,
41     struct event_arg *);
42 static void qla24xx_handle_prli_done_event(struct scsi_qla_host *,
43     struct event_arg *);
44 static void __qla24xx_handle_gpdb_event(scsi_qla_host_t *, struct event_arg *);
45 
46 /* SRB Extensions ---------------------------------------------------------- */
47 
48 void
49 qla2x00_sp_timeout(struct timer_list *t)
50 {
51 	srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
52 	struct srb_iocb *iocb;
53 	struct req_que *req;
54 	unsigned long flags;
55 	struct qla_hw_data *ha = sp->vha->hw;
56 
57 	WARN_ON_ONCE(irqs_disabled());
58 	spin_lock_irqsave(&ha->hardware_lock, flags);
59 	req = sp->qpair->req;
60 	req->outstanding_cmds[sp->handle] = NULL;
61 	iocb = &sp->u.iocb_cmd;
62 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
63 	iocb->timeout(sp);
64 }
65 
66 void
67 qla2x00_sp_free(void *ptr)
68 {
69 	srb_t *sp = ptr;
70 	struct srb_iocb *iocb = &sp->u.iocb_cmd;
71 
72 	del_timer(&iocb->timer);
73 	qla2x00_rel_sp(sp);
74 }
75 
76 /* Asynchronous Login/Logout Routines -------------------------------------- */
77 
78 unsigned long
79 qla2x00_get_async_timeout(struct scsi_qla_host *vha)
80 {
81 	unsigned long tmo;
82 	struct qla_hw_data *ha = vha->hw;
83 
84 	/* Firmware should use switch negotiated r_a_tov for timeout. */
85 	tmo = ha->r_a_tov / 10 * 2;
86 	if (IS_QLAFX00(ha)) {
87 		tmo = FX00_DEF_RATOV * 2;
88 	} else if (!IS_FWI2_CAPABLE(ha)) {
89 		/*
90 		 * Except for earlier ISPs where the timeout is seeded from the
91 		 * initialization control block.
92 		 */
93 		tmo = ha->login_timeout;
94 	}
95 	return tmo;
96 }
97 
98 static void qla24xx_abort_iocb_timeout(void *data)
99 {
100 	srb_t *sp = data;
101 	struct srb_iocb *abt = &sp->u.iocb_cmd;
102 
103 	abt->u.abt.comp_status = CS_TIMEOUT;
104 	sp->done(sp, QLA_FUNCTION_TIMEOUT);
105 }
106 
107 static void qla24xx_abort_sp_done(void *ptr, int res)
108 {
109 	srb_t *sp = ptr;
110 	struct srb_iocb *abt = &sp->u.iocb_cmd;
111 
112 	if (del_timer(&sp->u.iocb_cmd.timer)) {
113 		if (sp->flags & SRB_WAKEUP_ON_COMP)
114 			complete(&abt->u.abt.comp);
115 		else
116 			sp->free(sp);
117 	}
118 }
119 
120 static int qla24xx_async_abort_cmd(srb_t *cmd_sp, bool wait)
121 {
122 	scsi_qla_host_t *vha = cmd_sp->vha;
123 	struct srb_iocb *abt_iocb;
124 	srb_t *sp;
125 	int rval = QLA_FUNCTION_FAILED;
126 
127 	sp = qla2xxx_get_qpair_sp(cmd_sp->vha, cmd_sp->qpair, cmd_sp->fcport,
128 				  GFP_ATOMIC);
129 	if (!sp)
130 		goto done;
131 
132 	abt_iocb = &sp->u.iocb_cmd;
133 	sp->type = SRB_ABT_CMD;
134 	sp->name = "abort";
135 	sp->qpair = cmd_sp->qpair;
136 	if (wait)
137 		sp->flags = SRB_WAKEUP_ON_COMP;
138 
139 	abt_iocb->timeout = qla24xx_abort_iocb_timeout;
140 	init_completion(&abt_iocb->u.abt.comp);
141 	/* FW can send 2 x ABTS's timeout/20s */
142 	qla2x00_init_timer(sp, 42);
143 
144 	abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
145 	abt_iocb->u.abt.req_que_no = cpu_to_le16(cmd_sp->qpair->req->id);
146 
147 	sp->done = qla24xx_abort_sp_done;
148 
149 	ql_dbg(ql_dbg_async, vha, 0x507c,
150 	       "Abort command issued - hdl=%x, type=%x\n", cmd_sp->handle,
151 	       cmd_sp->type);
152 
153 	rval = qla2x00_start_sp(sp);
154 	if (rval != QLA_SUCCESS)
155 		goto done_free_sp;
156 
157 	if (wait) {
158 		wait_for_completion(&abt_iocb->u.abt.comp);
159 		rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
160 			QLA_SUCCESS : QLA_FUNCTION_FAILED;
161 	} else {
162 		goto done;
163 	}
164 
165 done_free_sp:
166 	sp->free(sp);
167 done:
168 	return rval;
169 }
170 
171 void
172 qla2x00_async_iocb_timeout(void *data)
173 {
174 	srb_t *sp = data;
175 	fc_port_t *fcport = sp->fcport;
176 	struct srb_iocb *lio = &sp->u.iocb_cmd;
177 	int rc, h;
178 	unsigned long flags;
179 
180 	if (fcport) {
181 		ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
182 		    "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
183 		    sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
184 
185 		fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
186 	} else {
187 		pr_info("Async-%s timeout - hdl=%x.\n",
188 		    sp->name, sp->handle);
189 	}
190 
191 	switch (sp->type) {
192 	case SRB_LOGIN_CMD:
193 		rc = qla24xx_async_abort_cmd(sp, false);
194 		if (rc) {
195 			/* Retry as needed. */
196 			lio->u.logio.data[0] = MBS_COMMAND_ERROR;
197 			lio->u.logio.data[1] =
198 				lio->u.logio.flags & SRB_LOGIN_RETRIED ?
199 				QLA_LOGIO_LOGIN_RETRIED : 0;
200 			spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
201 			for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
202 			    h++) {
203 				if (sp->qpair->req->outstanding_cmds[h] ==
204 				    sp) {
205 					sp->qpair->req->outstanding_cmds[h] =
206 					    NULL;
207 					break;
208 				}
209 			}
210 			spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
211 			sp->done(sp, QLA_FUNCTION_TIMEOUT);
212 		}
213 		break;
214 	case SRB_LOGOUT_CMD:
215 	case SRB_CT_PTHRU_CMD:
216 	case SRB_MB_IOCB:
217 	case SRB_NACK_PLOGI:
218 	case SRB_NACK_PRLI:
219 	case SRB_NACK_LOGO:
220 	case SRB_CTRL_VP:
221 		rc = qla24xx_async_abort_cmd(sp, false);
222 		if (rc) {
223 			spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
224 			for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
225 			    h++) {
226 				if (sp->qpair->req->outstanding_cmds[h] ==
227 				    sp) {
228 					sp->qpair->req->outstanding_cmds[h] =
229 					    NULL;
230 					break;
231 				}
232 			}
233 			spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
234 			sp->done(sp, QLA_FUNCTION_TIMEOUT);
235 		}
236 		break;
237 	}
238 }
239 
240 static void
241 qla2x00_async_login_sp_done(void *ptr, int res)
242 {
243 	srb_t *sp = ptr;
244 	struct scsi_qla_host *vha = sp->vha;
245 	struct srb_iocb *lio = &sp->u.iocb_cmd;
246 	struct event_arg ea;
247 
248 	ql_dbg(ql_dbg_disc, vha, 0x20dd,
249 	    "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
250 
251 	sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
252 
253 	if (!test_bit(UNLOADING, &vha->dpc_flags)) {
254 		memset(&ea, 0, sizeof(ea));
255 		ea.event = FCME_PLOGI_DONE;
256 		ea.fcport = sp->fcport;
257 		ea.data[0] = lio->u.logio.data[0];
258 		ea.data[1] = lio->u.logio.data[1];
259 		ea.iop[0] = lio->u.logio.iop[0];
260 		ea.iop[1] = lio->u.logio.iop[1];
261 		ea.sp = sp;
262 		qla2x00_fcport_event_handler(vha, &ea);
263 	}
264 
265 	sp->free(sp);
266 }
267 
268 static inline bool
269 fcport_is_smaller(fc_port_t *fcport)
270 {
271 	if (wwn_to_u64(fcport->port_name) <
272 	    wwn_to_u64(fcport->vha->port_name))
273 		return true;
274 	else
275 		return false;
276 }
277 
278 static inline bool
279 fcport_is_bigger(fc_port_t *fcport)
280 {
281 	return !fcport_is_smaller(fcport);
282 }
283 
284 int
285 qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
286     uint16_t *data)
287 {
288 	srb_t *sp;
289 	struct srb_iocb *lio;
290 	int rval = QLA_FUNCTION_FAILED;
291 
292 	if (!vha->flags.online)
293 		goto done;
294 
295 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
296 	if (!sp)
297 		goto done;
298 
299 	fcport->flags |= FCF_ASYNC_SENT;
300 	fcport->logout_completed = 0;
301 
302 	fcport->disc_state = DSC_LOGIN_PEND;
303 	sp->type = SRB_LOGIN_CMD;
304 	sp->name = "login";
305 	sp->gen1 = fcport->rscn_gen;
306 	sp->gen2 = fcport->login_gen;
307 
308 	lio = &sp->u.iocb_cmd;
309 	lio->timeout = qla2x00_async_iocb_timeout;
310 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
311 
312 	sp->done = qla2x00_async_login_sp_done;
313 	if (N2N_TOPO(fcport->vha->hw) && fcport_is_bigger(fcport))
314 		lio->u.logio.flags |= SRB_LOGIN_PRLI_ONLY;
315 	else
316 		lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
317 
318 	if (fcport->fc4f_nvme)
319 		lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
320 
321 	ql_dbg(ql_dbg_disc, vha, 0x2072,
322 	    "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
323 		"retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
324 	    fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
325 	    fcport->login_retry);
326 
327 	rval = qla2x00_start_sp(sp);
328 	if (rval != QLA_SUCCESS) {
329 		fcport->flags |= FCF_LOGIN_NEEDED;
330 		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
331 		goto done_free_sp;
332 	}
333 
334 	return rval;
335 
336 done_free_sp:
337 	sp->free(sp);
338 	fcport->flags &= ~FCF_ASYNC_SENT;
339 done:
340 	fcport->flags &= ~FCF_ASYNC_ACTIVE;
341 	return rval;
342 }
343 
344 static void
345 qla2x00_async_logout_sp_done(void *ptr, int res)
346 {
347 	srb_t *sp = ptr;
348 
349 	sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
350 	sp->fcport->login_gen++;
351 	qlt_logo_completion_handler(sp->fcport, res);
352 	sp->free(sp);
353 }
354 
355 int
356 qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
357 {
358 	srb_t *sp;
359 	struct srb_iocb *lio;
360 	int rval = QLA_FUNCTION_FAILED;
361 
362 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
363 		return rval;
364 
365 	fcport->flags |= FCF_ASYNC_SENT;
366 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
367 	if (!sp)
368 		goto done;
369 
370 	sp->type = SRB_LOGOUT_CMD;
371 	sp->name = "logout";
372 
373 	lio = &sp->u.iocb_cmd;
374 	lio->timeout = qla2x00_async_iocb_timeout;
375 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
376 
377 	sp->done = qla2x00_async_logout_sp_done;
378 
379 	ql_dbg(ql_dbg_disc, vha, 0x2070,
380 	    "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
381 	    sp->handle, fcport->loop_id, fcport->d_id.b.domain,
382 		fcport->d_id.b.area, fcport->d_id.b.al_pa,
383 		fcport->port_name);
384 
385 	rval = qla2x00_start_sp(sp);
386 	if (rval != QLA_SUCCESS)
387 		goto done_free_sp;
388 	return rval;
389 
390 done_free_sp:
391 	sp->free(sp);
392 done:
393 	fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
394 	return rval;
395 }
396 
397 void
398 qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
399     uint16_t *data)
400 {
401 	fcport->flags &= ~FCF_ASYNC_ACTIVE;
402 	/* Don't re-login in target mode */
403 	if (!fcport->tgt_session)
404 		qla2x00_mark_device_lost(vha, fcport, 1, 0);
405 	qlt_logo_completion_handler(fcport, data[0]);
406 }
407 
408 static void
409 qla2x00_async_prlo_sp_done(void *s, int res)
410 {
411 	srb_t *sp = (srb_t *)s;
412 	struct srb_iocb *lio = &sp->u.iocb_cmd;
413 	struct scsi_qla_host *vha = sp->vha;
414 
415 	sp->fcport->flags &= ~FCF_ASYNC_ACTIVE;
416 	if (!test_bit(UNLOADING, &vha->dpc_flags))
417 		qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
418 		    lio->u.logio.data);
419 	sp->free(sp);
420 }
421 
422 int
423 qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
424 {
425 	srb_t *sp;
426 	struct srb_iocb *lio;
427 	int rval;
428 
429 	rval = QLA_FUNCTION_FAILED;
430 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
431 	if (!sp)
432 		goto done;
433 
434 	sp->type = SRB_PRLO_CMD;
435 	sp->name = "prlo";
436 
437 	lio = &sp->u.iocb_cmd;
438 	lio->timeout = qla2x00_async_iocb_timeout;
439 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
440 
441 	sp->done = qla2x00_async_prlo_sp_done;
442 
443 	ql_dbg(ql_dbg_disc, vha, 0x2070,
444 	    "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
445 	    sp->handle, fcport->loop_id, fcport->d_id.b.domain,
446 	    fcport->d_id.b.area, fcport->d_id.b.al_pa);
447 
448 	rval = qla2x00_start_sp(sp);
449 	if (rval != QLA_SUCCESS)
450 		goto done_free_sp;
451 
452 	return rval;
453 
454 done_free_sp:
455 	sp->free(sp);
456 done:
457 	fcport->flags &= ~FCF_ASYNC_ACTIVE;
458 	return rval;
459 }
460 
461 static
462 void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
463 {
464 	struct fc_port *fcport = ea->fcport;
465 
466 	ql_dbg(ql_dbg_disc, vha, 0x20d2,
467 	    "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
468 	    __func__, fcport->port_name, fcport->disc_state,
469 	    fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
470 	    fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
471 
472 	if (ea->data[0] != MBS_COMMAND_COMPLETE) {
473 		ql_dbg(ql_dbg_disc, vha, 0x2066,
474 		    "%s %8phC: adisc fail: post delete\n",
475 		    __func__, ea->fcport->port_name);
476 		/* deleted = 0 & logout_on_delete = force fw cleanup */
477 		fcport->deleted = 0;
478 		fcport->logout_on_delete = 1;
479 		qlt_schedule_sess_for_deletion(ea->fcport);
480 		return;
481 	}
482 
483 	if (ea->fcport->disc_state == DSC_DELETE_PEND)
484 		return;
485 
486 	if (ea->sp->gen2 != ea->fcport->login_gen) {
487 		/* target side must have changed it. */
488 		ql_dbg(ql_dbg_disc, vha, 0x20d3,
489 		    "%s %8phC generation changed\n",
490 		    __func__, ea->fcport->port_name);
491 		return;
492 	} else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
493 		qla_rscn_replay(fcport);
494 		qlt_schedule_sess_for_deletion(fcport);
495 		return;
496 	}
497 
498 	__qla24xx_handle_gpdb_event(vha, ea);
499 }
500 
501 static int qla_post_els_plogi_work(struct scsi_qla_host *vha, fc_port_t *fcport)
502 {
503 	struct qla_work_evt *e;
504 
505 	e = qla2x00_alloc_work(vha, QLA_EVT_ELS_PLOGI);
506 	if (!e)
507 		return QLA_FUNCTION_FAILED;
508 
509 	e->u.fcport.fcport = fcport;
510 	fcport->flags |= FCF_ASYNC_ACTIVE;
511 	return qla2x00_post_work(vha, e);
512 }
513 
514 static void
515 qla2x00_async_adisc_sp_done(void *ptr, int res)
516 {
517 	srb_t *sp = ptr;
518 	struct scsi_qla_host *vha = sp->vha;
519 	struct event_arg ea;
520 	struct srb_iocb *lio = &sp->u.iocb_cmd;
521 
522 	ql_dbg(ql_dbg_disc, vha, 0x2066,
523 	    "Async done-%s res %x %8phC\n",
524 	    sp->name, res, sp->fcport->port_name);
525 
526 	sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
527 
528 	memset(&ea, 0, sizeof(ea));
529 	ea.event = FCME_ADISC_DONE;
530 	ea.rc = res;
531 	ea.data[0] = lio->u.logio.data[0];
532 	ea.data[1] = lio->u.logio.data[1];
533 	ea.iop[0] = lio->u.logio.iop[0];
534 	ea.iop[1] = lio->u.logio.iop[1];
535 	ea.fcport = sp->fcport;
536 	ea.sp = sp;
537 
538 	qla2x00_fcport_event_handler(vha, &ea);
539 
540 	sp->free(sp);
541 }
542 
543 int
544 qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
545     uint16_t *data)
546 {
547 	srb_t *sp;
548 	struct srb_iocb *lio;
549 	int rval = QLA_FUNCTION_FAILED;
550 
551 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
552 		return rval;
553 
554 	fcport->flags |= FCF_ASYNC_SENT;
555 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
556 	if (!sp)
557 		goto done;
558 
559 	sp->type = SRB_ADISC_CMD;
560 	sp->name = "adisc";
561 
562 	lio = &sp->u.iocb_cmd;
563 	lio->timeout = qla2x00_async_iocb_timeout;
564 	sp->gen1 = fcport->rscn_gen;
565 	sp->gen2 = fcport->login_gen;
566 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
567 
568 	sp->done = qla2x00_async_adisc_sp_done;
569 	if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
570 		lio->u.logio.flags |= SRB_LOGIN_RETRIED;
571 
572 	ql_dbg(ql_dbg_disc, vha, 0x206f,
573 	    "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
574 	    sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
575 
576 	rval = qla2x00_start_sp(sp);
577 	if (rval != QLA_SUCCESS)
578 		goto done_free_sp;
579 
580 	return rval;
581 
582 done_free_sp:
583 	sp->free(sp);
584 done:
585 	fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
586 	qla2x00_post_async_adisc_work(vha, fcport, data);
587 	return rval;
588 }
589 
590 static bool qla2x00_is_reserved_id(scsi_qla_host_t *vha, uint16_t loop_id)
591 {
592 	struct qla_hw_data *ha = vha->hw;
593 
594 	if (IS_FWI2_CAPABLE(ha))
595 		return loop_id > NPH_LAST_HANDLE;
596 
597 	return (loop_id > ha->max_loop_id && loop_id < SNS_FIRST_LOOP_ID) ||
598 		loop_id == MANAGEMENT_SERVER || loop_id == BROADCAST;
599 }
600 
601 /**
602  * qla2x00_find_new_loop_id - scan through our port list and find a new usable loop ID
603  * @vha: adapter state pointer.
604  * @dev: port structure pointer.
605  *
606  * Returns:
607  *	qla2x00 local function return status code.
608  *
609  * Context:
610  *	Kernel context.
611  */
612 static int qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
613 {
614 	int	rval;
615 	struct qla_hw_data *ha = vha->hw;
616 	unsigned long flags = 0;
617 
618 	rval = QLA_SUCCESS;
619 
620 	spin_lock_irqsave(&ha->vport_slock, flags);
621 
622 	dev->loop_id = find_first_zero_bit(ha->loop_id_map, LOOPID_MAP_SIZE);
623 	if (dev->loop_id >= LOOPID_MAP_SIZE ||
624 	    qla2x00_is_reserved_id(vha, dev->loop_id)) {
625 		dev->loop_id = FC_NO_LOOP_ID;
626 		rval = QLA_FUNCTION_FAILED;
627 	} else {
628 		set_bit(dev->loop_id, ha->loop_id_map);
629 	}
630 	spin_unlock_irqrestore(&ha->vport_slock, flags);
631 
632 	if (rval == QLA_SUCCESS)
633 		ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
634 		       "Assigning new loopid=%x, portid=%x.\n",
635 		       dev->loop_id, dev->d_id.b24);
636 	else
637 		ql_log(ql_log_warn, dev->vha, 0x2087,
638 		       "No loop_id's available, portid=%x.\n",
639 		       dev->d_id.b24);
640 
641 	return rval;
642 }
643 
644 void qla2x00_clear_loop_id(fc_port_t *fcport)
645 {
646 	struct qla_hw_data *ha = fcport->vha->hw;
647 
648 	if (fcport->loop_id == FC_NO_LOOP_ID ||
649 	    qla2x00_is_reserved_id(fcport->vha, fcport->loop_id))
650 		return;
651 
652 	clear_bit(fcport->loop_id, ha->loop_id_map);
653 	fcport->loop_id = FC_NO_LOOP_ID;
654 }
655 
656 static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
657 	struct event_arg *ea)
658 {
659 	fc_port_t *fcport, *conflict_fcport;
660 	struct get_name_list_extended *e;
661 	u16 i, n, found = 0, loop_id;
662 	port_id_t id;
663 	u64 wwn;
664 	u16 data[2];
665 	u8 current_login_state;
666 
667 	fcport = ea->fcport;
668 	ql_dbg(ql_dbg_disc, vha, 0xffff,
669 	    "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
670 	    __func__, fcport->port_name, fcport->disc_state,
671 	    fcport->fw_login_state, ea->rc,
672 	    fcport->login_gen, fcport->last_login_gen,
673 	    fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
674 
675 	if (fcport->disc_state == DSC_DELETE_PEND)
676 		return;
677 
678 	if (ea->rc) { /* rval */
679 		if (fcport->login_retry == 0) {
680 			ql_dbg(ql_dbg_disc, vha, 0x20de,
681 			    "GNL failed Port login retry %8phN, retry cnt=%d.\n",
682 			    fcport->port_name, fcport->login_retry);
683 		}
684 		return;
685 	}
686 
687 	if (fcport->last_rscn_gen != fcport->rscn_gen) {
688 		qla_rscn_replay(fcport);
689 		qlt_schedule_sess_for_deletion(fcport);
690 		return;
691 	} else if (fcport->last_login_gen != fcport->login_gen) {
692 		ql_dbg(ql_dbg_disc, vha, 0x20e0,
693 		    "%s %8phC login gen changed\n",
694 		    __func__, fcport->port_name);
695 		return;
696 	}
697 
698 	n = ea->data[0] / sizeof(struct get_name_list_extended);
699 
700 	ql_dbg(ql_dbg_disc, vha, 0x20e1,
701 	    "%s %d %8phC n %d %02x%02x%02x lid %d \n",
702 	    __func__, __LINE__, fcport->port_name, n,
703 	    fcport->d_id.b.domain, fcport->d_id.b.area,
704 	    fcport->d_id.b.al_pa, fcport->loop_id);
705 
706 	for (i = 0; i < n; i++) {
707 		e = &vha->gnl.l[i];
708 		wwn = wwn_to_u64(e->port_name);
709 		id.b.domain = e->port_id[2];
710 		id.b.area = e->port_id[1];
711 		id.b.al_pa = e->port_id[0];
712 		id.b.rsvd_1 = 0;
713 
714 		if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
715 			continue;
716 
717 		if (IS_SW_RESV_ADDR(id))
718 			continue;
719 
720 		found = 1;
721 
722 		loop_id = le16_to_cpu(e->nport_handle);
723 		loop_id = (loop_id & 0x7fff);
724 		if  (fcport->fc4f_nvme)
725 			current_login_state = e->current_login_state >> 4;
726 		else
727 			current_login_state = e->current_login_state & 0xf;
728 
729 
730 		ql_dbg(ql_dbg_disc, vha, 0x20e2,
731 		    "%s found %8phC CLS [%x|%x] nvme %d ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
732 		    __func__, fcport->port_name,
733 		    e->current_login_state, fcport->fw_login_state,
734 		    fcport->fc4f_nvme, id.b.domain, id.b.area, id.b.al_pa,
735 		    fcport->d_id.b.domain, fcport->d_id.b.area,
736 		    fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
737 
738 		switch (fcport->disc_state) {
739 		case DSC_DELETE_PEND:
740 		case DSC_DELETED:
741 			break;
742 		default:
743 			if ((id.b24 != fcport->d_id.b24 &&
744 			    fcport->d_id.b24) ||
745 			    (fcport->loop_id != FC_NO_LOOP_ID &&
746 				fcport->loop_id != loop_id)) {
747 				ql_dbg(ql_dbg_disc, vha, 0x20e3,
748 				    "%s %d %8phC post del sess\n",
749 				    __func__, __LINE__, fcport->port_name);
750 				qlt_schedule_sess_for_deletion(fcport);
751 				return;
752 			}
753 			break;
754 		}
755 
756 		fcport->loop_id = loop_id;
757 
758 		wwn = wwn_to_u64(fcport->port_name);
759 		qlt_find_sess_invalidate_other(vha, wwn,
760 			id, loop_id, &conflict_fcport);
761 
762 		if (conflict_fcport) {
763 			/*
764 			 * Another share fcport share the same loop_id &
765 			 * nport id. Conflict fcport needs to finish
766 			 * cleanup before this fcport can proceed to login.
767 			 */
768 			conflict_fcport->conflict = fcport;
769 			fcport->login_pause = 1;
770 		}
771 
772 		switch (vha->hw->current_topology) {
773 		default:
774 			switch (current_login_state) {
775 			case DSC_LS_PRLI_COMP:
776 				ql_dbg(ql_dbg_disc + ql_dbg_verbose,
777 				    vha, 0x20e4, "%s %d %8phC post gpdb\n",
778 				    __func__, __LINE__, fcport->port_name);
779 
780 				if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
781 					fcport->port_type = FCT_INITIATOR;
782 				else
783 					fcport->port_type = FCT_TARGET;
784 				data[0] = data[1] = 0;
785 				qla2x00_post_async_adisc_work(vha, fcport,
786 				    data);
787 				break;
788 			case DSC_LS_PORT_UNAVAIL:
789 			default:
790 				if (fcport->loop_id == FC_NO_LOOP_ID) {
791 					qla2x00_find_new_loop_id(vha, fcport);
792 					fcport->fw_login_state =
793 					    DSC_LS_PORT_UNAVAIL;
794 				}
795 				ql_dbg(ql_dbg_disc, vha, 0x20e5,
796 				    "%s %d %8phC\n", __func__, __LINE__,
797 				    fcport->port_name);
798 				qla24xx_fcport_handle_login(vha, fcport);
799 				break;
800 			}
801 			break;
802 		case ISP_CFG_N:
803 			fcport->fw_login_state = current_login_state;
804 			fcport->d_id = id;
805 			switch (current_login_state) {
806 			case DSC_LS_PRLI_COMP:
807 				if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
808 					fcport->port_type = FCT_INITIATOR;
809 				else
810 					fcport->port_type = FCT_TARGET;
811 
812 				data[0] = data[1] = 0;
813 				qla2x00_post_async_adisc_work(vha, fcport,
814 				    data);
815 				break;
816 			case DSC_LS_PLOGI_COMP:
817 				if (fcport_is_bigger(fcport)) {
818 					/* local adapter is smaller */
819 					if (fcport->loop_id != FC_NO_LOOP_ID)
820 						qla2x00_clear_loop_id(fcport);
821 
822 					fcport->loop_id = loop_id;
823 					qla24xx_fcport_handle_login(vha,
824 					    fcport);
825 					break;
826 				}
827 				/* fall through */
828 			default:
829 				if (fcport_is_smaller(fcport)) {
830 					/* local adapter is bigger */
831 					if (fcport->loop_id != FC_NO_LOOP_ID)
832 						qla2x00_clear_loop_id(fcport);
833 
834 					fcport->loop_id = loop_id;
835 					qla24xx_fcport_handle_login(vha,
836 					    fcport);
837 				}
838 				break;
839 			}
840 			break;
841 		} /* switch (ha->current_topology) */
842 	}
843 
844 	if (!found) {
845 		switch (vha->hw->current_topology) {
846 		case ISP_CFG_F:
847 		case ISP_CFG_FL:
848 			for (i = 0; i < n; i++) {
849 				e = &vha->gnl.l[i];
850 				id.b.domain = e->port_id[0];
851 				id.b.area = e->port_id[1];
852 				id.b.al_pa = e->port_id[2];
853 				id.b.rsvd_1 = 0;
854 				loop_id = le16_to_cpu(e->nport_handle);
855 
856 				if (fcport->d_id.b24 == id.b24) {
857 					conflict_fcport =
858 					    qla2x00_find_fcport_by_wwpn(vha,
859 						e->port_name, 0);
860 					if (conflict_fcport) {
861 						ql_dbg(ql_dbg_disc + ql_dbg_verbose,
862 						    vha, 0x20e5,
863 						    "%s %d %8phC post del sess\n",
864 						    __func__, __LINE__,
865 						    conflict_fcport->port_name);
866 						qlt_schedule_sess_for_deletion
867 							(conflict_fcport);
868 					}
869 				}
870 				/*
871 				 * FW already picked this loop id for
872 				 * another fcport
873 				 */
874 				if (fcport->loop_id == loop_id)
875 					fcport->loop_id = FC_NO_LOOP_ID;
876 			}
877 			qla24xx_fcport_handle_login(vha, fcport);
878 			break;
879 		case ISP_CFG_N:
880 			fcport->disc_state = DSC_DELETED;
881 			if (time_after_eq(jiffies, fcport->dm_login_expire)) {
882 				if (fcport->n2n_link_reset_cnt < 2) {
883 					fcport->n2n_link_reset_cnt++;
884 					/*
885 					 * remote port is not sending PLOGI.
886 					 * Reset link to kick start his state
887 					 * machine
888 					 */
889 					set_bit(N2N_LINK_RESET,
890 					    &vha->dpc_flags);
891 				} else {
892 					if (fcport->n2n_chip_reset < 1) {
893 						ql_log(ql_log_info, vha, 0x705d,
894 						    "Chip reset to bring laser down");
895 						set_bit(ISP_ABORT_NEEDED,
896 						    &vha->dpc_flags);
897 						fcport->n2n_chip_reset++;
898 					} else {
899 						ql_log(ql_log_info, vha, 0x705d,
900 						    "Remote port %8ph is not coming back\n",
901 						    fcport->port_name);
902 						fcport->scan_state = 0;
903 					}
904 				}
905 				qla2xxx_wake_dpc(vha);
906 			} else {
907 				/*
908 				 * report port suppose to do PLOGI. Give him
909 				 * more time. FW will catch it.
910 				 */
911 				set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
912 			}
913 			break;
914 		default:
915 			break;
916 		}
917 	}
918 } /* gnl_event */
919 
920 static void
921 qla24xx_async_gnl_sp_done(void *s, int res)
922 {
923 	struct srb *sp = s;
924 	struct scsi_qla_host *vha = sp->vha;
925 	unsigned long flags;
926 	struct fc_port *fcport = NULL, *tf;
927 	u16 i, n = 0, loop_id;
928 	struct event_arg ea;
929 	struct get_name_list_extended *e;
930 	u64 wwn;
931 	struct list_head h;
932 	bool found = false;
933 
934 	ql_dbg(ql_dbg_disc, vha, 0x20e7,
935 	    "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
936 	    sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
937 	    sp->u.iocb_cmd.u.mbx.in_mb[2]);
938 
939 	if (res == QLA_FUNCTION_TIMEOUT)
940 		return;
941 
942 	sp->fcport->flags &= ~(FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE);
943 	memset(&ea, 0, sizeof(ea));
944 	ea.sp = sp;
945 	ea.rc = res;
946 	ea.event = FCME_GNL_DONE;
947 
948 	if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
949 	    sizeof(struct get_name_list_extended)) {
950 		n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
951 		    sizeof(struct get_name_list_extended);
952 		ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
953 	}
954 
955 	for (i = 0; i < n; i++) {
956 		e = &vha->gnl.l[i];
957 		loop_id = le16_to_cpu(e->nport_handle);
958 		/* mask out reserve bit */
959 		loop_id = (loop_id & 0x7fff);
960 		set_bit(loop_id, vha->hw->loop_id_map);
961 		wwn = wwn_to_u64(e->port_name);
962 
963 		ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
964 		    "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
965 		    __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
966 		    e->port_id[0], e->current_login_state, e->last_login_state,
967 		    (loop_id & 0x7fff));
968 	}
969 
970 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
971 
972 	INIT_LIST_HEAD(&h);
973 	fcport = tf = NULL;
974 	if (!list_empty(&vha->gnl.fcports))
975 		list_splice_init(&vha->gnl.fcports, &h);
976 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
977 
978 	list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
979 		list_del_init(&fcport->gnl_entry);
980 		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
981 		fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
982 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
983 		ea.fcport = fcport;
984 
985 		qla2x00_fcport_event_handler(vha, &ea);
986 	}
987 
988 	/* create new fcport if fw has knowledge of new sessions */
989 	for (i = 0; i < n; i++) {
990 		port_id_t id;
991 		u64 wwnn;
992 
993 		e = &vha->gnl.l[i];
994 		wwn = wwn_to_u64(e->port_name);
995 
996 		found = false;
997 		list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
998 			if (!memcmp((u8 *)&wwn, fcport->port_name,
999 			    WWN_SIZE)) {
1000 				found = true;
1001 				break;
1002 			}
1003 		}
1004 
1005 		id.b.domain = e->port_id[2];
1006 		id.b.area = e->port_id[1];
1007 		id.b.al_pa = e->port_id[0];
1008 		id.b.rsvd_1 = 0;
1009 
1010 		if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
1011 			ql_dbg(ql_dbg_disc, vha, 0x2065,
1012 			    "%s %d %8phC %06x post new sess\n",
1013 			    __func__, __LINE__, (u8 *)&wwn, id.b24);
1014 			wwnn = wwn_to_u64(e->node_name);
1015 			qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
1016 			    (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
1017 		}
1018 	}
1019 
1020 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1021 	vha->gnl.sent = 0;
1022 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1023 
1024 	sp->free(sp);
1025 }
1026 
1027 int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
1028 {
1029 	srb_t *sp;
1030 	struct srb_iocb *mbx;
1031 	int rval = QLA_FUNCTION_FAILED;
1032 	unsigned long flags;
1033 	u16 *mb;
1034 
1035 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
1036 		return rval;
1037 
1038 	ql_dbg(ql_dbg_disc, vha, 0x20d9,
1039 	    "Async-gnlist WWPN %8phC \n", fcport->port_name);
1040 
1041 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1042 	fcport->flags |= FCF_ASYNC_SENT;
1043 	fcport->disc_state = DSC_GNL;
1044 	fcport->last_rscn_gen = fcport->rscn_gen;
1045 	fcport->last_login_gen = fcport->login_gen;
1046 
1047 	list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
1048 	if (vha->gnl.sent) {
1049 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1050 		return QLA_SUCCESS;
1051 	}
1052 	vha->gnl.sent = 1;
1053 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1054 
1055 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1056 	if (!sp)
1057 		goto done;
1058 
1059 	sp->type = SRB_MB_IOCB;
1060 	sp->name = "gnlist";
1061 	sp->gen1 = fcport->rscn_gen;
1062 	sp->gen2 = fcport->login_gen;
1063 
1064 	mbx = &sp->u.iocb_cmd;
1065 	mbx->timeout = qla2x00_async_iocb_timeout;
1066 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
1067 
1068 	mb = sp->u.iocb_cmd.u.mbx.out_mb;
1069 	mb[0] = MBC_PORT_NODE_NAME_LIST;
1070 	mb[1] = BIT_2 | BIT_3;
1071 	mb[2] = MSW(vha->gnl.ldma);
1072 	mb[3] = LSW(vha->gnl.ldma);
1073 	mb[6] = MSW(MSD(vha->gnl.ldma));
1074 	mb[7] = LSW(MSD(vha->gnl.ldma));
1075 	mb[8] = vha->gnl.size;
1076 	mb[9] = vha->vp_idx;
1077 
1078 	sp->done = qla24xx_async_gnl_sp_done;
1079 
1080 	ql_dbg(ql_dbg_disc, vha, 0x20da,
1081 	    "Async-%s - OUT WWPN %8phC hndl %x\n",
1082 	    sp->name, fcport->port_name, sp->handle);
1083 
1084 	rval = qla2x00_start_sp(sp);
1085 	if (rval != QLA_SUCCESS)
1086 		goto done_free_sp;
1087 
1088 	return rval;
1089 
1090 done_free_sp:
1091 	sp->free(sp);
1092 	fcport->flags &= ~FCF_ASYNC_SENT;
1093 done:
1094 	return rval;
1095 }
1096 
1097 int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
1098 {
1099 	struct qla_work_evt *e;
1100 
1101 	e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
1102 	if (!e)
1103 		return QLA_FUNCTION_FAILED;
1104 
1105 	e->u.fcport.fcport = fcport;
1106 	fcport->flags |= FCF_ASYNC_ACTIVE;
1107 	return qla2x00_post_work(vha, e);
1108 }
1109 
1110 static
1111 void qla24xx_async_gpdb_sp_done(void *s, int res)
1112 {
1113 	struct srb *sp = s;
1114 	struct scsi_qla_host *vha = sp->vha;
1115 	struct qla_hw_data *ha = vha->hw;
1116 	fc_port_t *fcport = sp->fcport;
1117 	u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
1118 	struct event_arg ea;
1119 
1120 	ql_dbg(ql_dbg_disc, vha, 0x20db,
1121 	    "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
1122 	    sp->name, res, fcport->port_name, mb[1], mb[2]);
1123 
1124 	if (res == QLA_FUNCTION_TIMEOUT) {
1125 		dma_pool_free(sp->vha->hw->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
1126 			sp->u.iocb_cmd.u.mbx.in_dma);
1127 		return;
1128 	}
1129 
1130 	fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
1131 	memset(&ea, 0, sizeof(ea));
1132 	ea.event = FCME_GPDB_DONE;
1133 	ea.fcport = fcport;
1134 	ea.sp = sp;
1135 
1136 	qla2x00_fcport_event_handler(vha, &ea);
1137 
1138 	dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
1139 		sp->u.iocb_cmd.u.mbx.in_dma);
1140 
1141 	sp->free(sp);
1142 }
1143 
1144 static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
1145 {
1146 	struct qla_work_evt *e;
1147 
1148 	e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
1149 	if (!e)
1150 		return QLA_FUNCTION_FAILED;
1151 
1152 	e->u.fcport.fcport = fcport;
1153 
1154 	return qla2x00_post_work(vha, e);
1155 }
1156 
1157 static void
1158 qla2x00_async_prli_sp_done(void *ptr, int res)
1159 {
1160 	srb_t *sp = ptr;
1161 	struct scsi_qla_host *vha = sp->vha;
1162 	struct srb_iocb *lio = &sp->u.iocb_cmd;
1163 	struct event_arg ea;
1164 
1165 	ql_dbg(ql_dbg_disc, vha, 0x2129,
1166 	    "%s %8phC res %d \n", __func__,
1167 	    sp->fcport->port_name, res);
1168 
1169 	sp->fcport->flags &= ~FCF_ASYNC_SENT;
1170 
1171 	if (!test_bit(UNLOADING, &vha->dpc_flags)) {
1172 		memset(&ea, 0, sizeof(ea));
1173 		ea.event = FCME_PRLI_DONE;
1174 		ea.fcport = sp->fcport;
1175 		ea.data[0] = lio->u.logio.data[0];
1176 		ea.data[1] = lio->u.logio.data[1];
1177 		ea.iop[0] = lio->u.logio.iop[0];
1178 		ea.iop[1] = lio->u.logio.iop[1];
1179 		ea.sp = sp;
1180 
1181 		qla2x00_fcport_event_handler(vha, &ea);
1182 	}
1183 
1184 	sp->free(sp);
1185 }
1186 
1187 int
1188 qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
1189 {
1190 	srb_t *sp;
1191 	struct srb_iocb *lio;
1192 	int rval = QLA_FUNCTION_FAILED;
1193 
1194 	if (!vha->flags.online)
1195 		return rval;
1196 
1197 	if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
1198 	    fcport->fw_login_state == DSC_LS_PRLI_PEND)
1199 		return rval;
1200 
1201 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1202 	if (!sp)
1203 		return rval;
1204 
1205 	fcport->flags |= FCF_ASYNC_SENT;
1206 	fcport->logout_completed = 0;
1207 
1208 	sp->type = SRB_PRLI_CMD;
1209 	sp->name = "prli";
1210 
1211 	lio = &sp->u.iocb_cmd;
1212 	lio->timeout = qla2x00_async_iocb_timeout;
1213 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
1214 
1215 	sp->done = qla2x00_async_prli_sp_done;
1216 	lio->u.logio.flags = 0;
1217 
1218 	if  (fcport->fc4f_nvme)
1219 		lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
1220 
1221 	ql_dbg(ql_dbg_disc, vha, 0x211b,
1222 	    "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d %s.\n",
1223 	    fcport->port_name, sp->handle, fcport->loop_id, fcport->d_id.b24,
1224 	    fcport->login_retry, fcport->fc4f_nvme ? "nvme" : "fc");
1225 
1226 	rval = qla2x00_start_sp(sp);
1227 	if (rval != QLA_SUCCESS) {
1228 		fcport->flags |= FCF_LOGIN_NEEDED;
1229 		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1230 		goto done_free_sp;
1231 	}
1232 
1233 	return rval;
1234 
1235 done_free_sp:
1236 	sp->free(sp);
1237 	fcport->flags &= ~FCF_ASYNC_SENT;
1238 	return rval;
1239 }
1240 
1241 int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1242 {
1243 	struct qla_work_evt *e;
1244 
1245 	e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
1246 	if (!e)
1247 		return QLA_FUNCTION_FAILED;
1248 
1249 	e->u.fcport.fcport = fcport;
1250 	e->u.fcport.opt = opt;
1251 	fcport->flags |= FCF_ASYNC_ACTIVE;
1252 	return qla2x00_post_work(vha, e);
1253 }
1254 
1255 int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1256 {
1257 	srb_t *sp;
1258 	struct srb_iocb *mbx;
1259 	int rval = QLA_FUNCTION_FAILED;
1260 	u16 *mb;
1261 	dma_addr_t pd_dma;
1262 	struct port_database_24xx *pd;
1263 	struct qla_hw_data *ha = vha->hw;
1264 
1265 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
1266 		return rval;
1267 
1268 	fcport->disc_state = DSC_GPDB;
1269 
1270 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1271 	if (!sp)
1272 		goto done;
1273 
1274 	fcport->flags |= FCF_ASYNC_SENT;
1275 	sp->type = SRB_MB_IOCB;
1276 	sp->name = "gpdb";
1277 	sp->gen1 = fcport->rscn_gen;
1278 	sp->gen2 = fcport->login_gen;
1279 
1280 	mbx = &sp->u.iocb_cmd;
1281 	mbx->timeout = qla2x00_async_iocb_timeout;
1282 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
1283 
1284 	pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1285 	if (pd == NULL) {
1286 		ql_log(ql_log_warn, vha, 0xd043,
1287 		    "Failed to allocate port database structure.\n");
1288 		goto done_free_sp;
1289 	}
1290 
1291 	mb = sp->u.iocb_cmd.u.mbx.out_mb;
1292 	mb[0] = MBC_GET_PORT_DATABASE;
1293 	mb[1] = fcport->loop_id;
1294 	mb[2] = MSW(pd_dma);
1295 	mb[3] = LSW(pd_dma);
1296 	mb[6] = MSW(MSD(pd_dma));
1297 	mb[7] = LSW(MSD(pd_dma));
1298 	mb[9] = vha->vp_idx;
1299 	mb[10] = opt;
1300 
1301 	mbx->u.mbx.in = (void *)pd;
1302 	mbx->u.mbx.in_dma = pd_dma;
1303 
1304 	sp->done = qla24xx_async_gpdb_sp_done;
1305 
1306 	ql_dbg(ql_dbg_disc, vha, 0x20dc,
1307 	    "Async-%s %8phC hndl %x opt %x\n",
1308 	    sp->name, fcport->port_name, sp->handle, opt);
1309 
1310 	rval = qla2x00_start_sp(sp);
1311 	if (rval != QLA_SUCCESS)
1312 		goto done_free_sp;
1313 	return rval;
1314 
1315 done_free_sp:
1316 	if (pd)
1317 		dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1318 
1319 	sp->free(sp);
1320 	fcport->flags &= ~FCF_ASYNC_SENT;
1321 done:
1322 	qla24xx_post_gpdb_work(vha, fcport, opt);
1323 	return rval;
1324 }
1325 
1326 static
1327 void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1328 {
1329 	unsigned long flags;
1330 
1331 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1332 	ea->fcport->login_gen++;
1333 	ea->fcport->deleted = 0;
1334 	ea->fcport->logout_on_delete = 1;
1335 
1336 	if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1337 		vha->fcport_count++;
1338 		ea->fcport->login_succ = 1;
1339 
1340 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1341 		qla24xx_sched_upd_fcport(ea->fcport);
1342 		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1343 	} else if (ea->fcport->login_succ) {
1344 		/*
1345 		 * We have an existing session. A late RSCN delivery
1346 		 * must have triggered the session to be re-validate.
1347 		 * Session is still valid.
1348 		 */
1349 		ql_dbg(ql_dbg_disc, vha, 0x20d6,
1350 		    "%s %d %8phC session revalidate success\n",
1351 		    __func__, __LINE__, ea->fcport->port_name);
1352 		ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
1353 	}
1354 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1355 }
1356 
1357 static
1358 void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1359 {
1360 	fc_port_t *fcport = ea->fcport;
1361 	struct port_database_24xx *pd;
1362 	struct srb *sp = ea->sp;
1363 	uint8_t	ls;
1364 
1365 	pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1366 
1367 	fcport->flags &= ~FCF_ASYNC_SENT;
1368 
1369 	ql_dbg(ql_dbg_disc, vha, 0x20d2,
1370 	    "%s %8phC DS %d LS %d nvme %x rc %d\n", __func__, fcport->port_name,
1371 	    fcport->disc_state, pd->current_login_state, fcport->fc4f_nvme,
1372 	    ea->rc);
1373 
1374 	if (fcport->disc_state == DSC_DELETE_PEND)
1375 		return;
1376 
1377 	if (fcport->fc4f_nvme)
1378 		ls = pd->current_login_state >> 4;
1379 	else
1380 		ls = pd->current_login_state & 0xf;
1381 
1382 	if (ea->sp->gen2 != fcport->login_gen) {
1383 		/* target side must have changed it. */
1384 
1385 		ql_dbg(ql_dbg_disc, vha, 0x20d3,
1386 		    "%s %8phC generation changed\n",
1387 		    __func__, fcport->port_name);
1388 		return;
1389 	} else if (ea->sp->gen1 != fcport->rscn_gen) {
1390 		qla_rscn_replay(fcport);
1391 		qlt_schedule_sess_for_deletion(fcport);
1392 		return;
1393 	}
1394 
1395 	switch (ls) {
1396 	case PDS_PRLI_COMPLETE:
1397 		__qla24xx_parse_gpdb(vha, fcport, pd);
1398 		break;
1399 	case PDS_PLOGI_PENDING:
1400 	case PDS_PLOGI_COMPLETE:
1401 	case PDS_PRLI_PENDING:
1402 	case PDS_PRLI2_PENDING:
1403 		/* Set discovery state back to GNL to Relogin attempt */
1404 		if (qla_dual_mode_enabled(vha) ||
1405 		    qla_ini_mode_enabled(vha)) {
1406 			fcport->disc_state = DSC_GNL;
1407 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1408 		}
1409 		return;
1410 	case PDS_LOGO_PENDING:
1411 	case PDS_PORT_UNAVAILABLE:
1412 	default:
1413 		ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1414 		    __func__, __LINE__, fcport->port_name);
1415 		qlt_schedule_sess_for_deletion(fcport);
1416 		return;
1417 	}
1418 	__qla24xx_handle_gpdb_event(vha, ea);
1419 } /* gpdb event */
1420 
1421 static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1422 {
1423 	u8 login = 0;
1424 	int rc;
1425 
1426 	if (qla_tgt_mode_enabled(vha))
1427 		return;
1428 
1429 	if (qla_dual_mode_enabled(vha)) {
1430 		if (N2N_TOPO(vha->hw)) {
1431 			u64 mywwn, wwn;
1432 
1433 			mywwn = wwn_to_u64(vha->port_name);
1434 			wwn = wwn_to_u64(fcport->port_name);
1435 			if (mywwn > wwn)
1436 				login = 1;
1437 			else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1438 			    && time_after_eq(jiffies,
1439 				    fcport->plogi_nack_done_deadline))
1440 				login = 1;
1441 		} else {
1442 			login = 1;
1443 		}
1444 	} else {
1445 		/* initiator mode */
1446 		login = 1;
1447 	}
1448 
1449 	if (login && fcport->login_retry) {
1450 		fcport->login_retry--;
1451 		if (fcport->loop_id == FC_NO_LOOP_ID) {
1452 			fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1453 			rc = qla2x00_find_new_loop_id(vha, fcport);
1454 			if (rc) {
1455 				ql_dbg(ql_dbg_disc, vha, 0x20e6,
1456 				    "%s %d %8phC post del sess - out of loopid\n",
1457 				    __func__, __LINE__, fcport->port_name);
1458 				fcport->scan_state = 0;
1459 				qlt_schedule_sess_for_deletion(fcport);
1460 				return;
1461 			}
1462 		}
1463 		ql_dbg(ql_dbg_disc, vha, 0x20bf,
1464 		    "%s %d %8phC post login\n",
1465 		    __func__, __LINE__, fcport->port_name);
1466 		qla2x00_post_async_login_work(vha, fcport, NULL);
1467 	}
1468 }
1469 
1470 int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1471 {
1472 	u16 data[2];
1473 	u64 wwn;
1474 	u16 sec;
1475 
1476 	ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20d8,
1477 	    "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d lid %d scan %d\n",
1478 	    __func__, fcport->port_name, fcport->disc_state,
1479 	    fcport->fw_login_state, fcport->login_pause, fcport->flags,
1480 	    fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
1481 	    fcport->login_gen, fcport->loop_id, fcport->scan_state);
1482 
1483 	if (fcport->scan_state != QLA_FCPORT_FOUND)
1484 		return 0;
1485 
1486 	if ((fcport->loop_id != FC_NO_LOOP_ID) &&
1487 	    ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1488 	     (fcport->fw_login_state == DSC_LS_PRLI_PEND)))
1489 		return 0;
1490 
1491 	if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
1492 		if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1493 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1494 			return 0;
1495 		}
1496 	}
1497 
1498 	/* for pure Target Mode. Login will not be initiated */
1499 	if (vha->host->active_mode == MODE_TARGET)
1500 		return 0;
1501 
1502 	if (fcport->flags & FCF_ASYNC_SENT) {
1503 		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1504 		return 0;
1505 	}
1506 
1507 	switch (fcport->disc_state) {
1508 	case DSC_DELETED:
1509 		wwn = wwn_to_u64(fcport->node_name);
1510 		switch (vha->hw->current_topology) {
1511 		case ISP_CFG_N:
1512 			if (fcport_is_smaller(fcport)) {
1513 				/* this adapter is bigger */
1514 				if (fcport->login_retry) {
1515 					if (fcport->loop_id == FC_NO_LOOP_ID) {
1516 						qla2x00_find_new_loop_id(vha,
1517 						    fcport);
1518 						fcport->fw_login_state =
1519 						    DSC_LS_PORT_UNAVAIL;
1520 					}
1521 					fcport->login_retry--;
1522 					qla_post_els_plogi_work(vha, fcport);
1523 				} else {
1524 					ql_log(ql_log_info, vha, 0x705d,
1525 					    "Unable to reach remote port %8phC",
1526 					    fcport->port_name);
1527 				}
1528 			} else {
1529 				qla24xx_post_gnl_work(vha, fcport);
1530 			}
1531 			break;
1532 		default:
1533 			if (wwn == 0)    {
1534 				ql_dbg(ql_dbg_disc, vha, 0xffff,
1535 				    "%s %d %8phC post GNNID\n",
1536 				    __func__, __LINE__, fcport->port_name);
1537 				qla24xx_post_gnnid_work(vha, fcport);
1538 			} else if (fcport->loop_id == FC_NO_LOOP_ID) {
1539 				ql_dbg(ql_dbg_disc, vha, 0x20bd,
1540 				    "%s %d %8phC post gnl\n",
1541 				    __func__, __LINE__, fcport->port_name);
1542 				qla24xx_post_gnl_work(vha, fcport);
1543 			} else {
1544 				qla_chk_n2n_b4_login(vha, fcport);
1545 			}
1546 			break;
1547 		}
1548 		break;
1549 
1550 	case DSC_GNL:
1551 		switch (vha->hw->current_topology) {
1552 		case ISP_CFG_N:
1553 			if ((fcport->current_login_state & 0xf) == 0x6) {
1554 				ql_dbg(ql_dbg_disc, vha, 0x2118,
1555 				    "%s %d %8phC post GPDB work\n",
1556 				    __func__, __LINE__, fcport->port_name);
1557 				fcport->chip_reset =
1558 					vha->hw->base_qpair->chip_reset;
1559 				qla24xx_post_gpdb_work(vha, fcport, 0);
1560 			}  else {
1561 				ql_dbg(ql_dbg_disc, vha, 0x2118,
1562 				    "%s %d %8phC post NVMe PRLI\n",
1563 				    __func__, __LINE__, fcport->port_name);
1564 				qla24xx_post_prli_work(vha, fcport);
1565 			}
1566 			break;
1567 		default:
1568 			if (fcport->login_pause) {
1569 				fcport->last_rscn_gen = fcport->rscn_gen;
1570 				fcport->last_login_gen = fcport->login_gen;
1571 				set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1572 				break;
1573 			}
1574 			qla_chk_n2n_b4_login(vha, fcport);
1575 			break;
1576 		}
1577 		break;
1578 
1579 	case DSC_LOGIN_FAILED:
1580 		if (N2N_TOPO(vha->hw))
1581 			qla_chk_n2n_b4_login(vha, fcport);
1582 		else
1583 			qlt_schedule_sess_for_deletion(fcport);
1584 		break;
1585 
1586 	case DSC_LOGIN_COMPLETE:
1587 		/* recheck login state */
1588 		data[0] = data[1] = 0;
1589 		qla2x00_post_async_adisc_work(vha, fcport, data);
1590 		break;
1591 
1592 	case DSC_LOGIN_PEND:
1593 		if (fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1594 			qla24xx_post_prli_work(vha, fcport);
1595 		break;
1596 
1597 	case DSC_UPD_FCPORT:
1598 		sec =  jiffies_to_msecs(jiffies -
1599 		    fcport->jiffies_at_registration)/1000;
1600 		if (fcport->sec_since_registration < sec && sec &&
1601 		    !(sec % 60)) {
1602 			fcport->sec_since_registration = sec;
1603 			ql_dbg(ql_dbg_disc, fcport->vha, 0xffff,
1604 			    "%s %8phC - Slow Rport registration(%d Sec)\n",
1605 			    __func__, fcport->port_name, sec);
1606 		}
1607 
1608 		if (fcport->next_disc_state != DSC_DELETE_PEND)
1609 			fcport->next_disc_state = DSC_ADISC;
1610 		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1611 		break;
1612 
1613 	default:
1614 		break;
1615 	}
1616 
1617 	return 0;
1618 }
1619 
1620 int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
1621     u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
1622 {
1623 	struct qla_work_evt *e;
1624 
1625 	e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1626 	if (!e)
1627 		return QLA_FUNCTION_FAILED;
1628 
1629 	e->u.new_sess.id = *id;
1630 	e->u.new_sess.pla = pla;
1631 	e->u.new_sess.fc4_type = fc4_type;
1632 	memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
1633 	if (node_name)
1634 		memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
1635 
1636 	return qla2x00_post_work(vha, e);
1637 }
1638 
1639 static
1640 void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1641 	struct event_arg *ea)
1642 {
1643 	fc_port_t *fcport = ea->fcport;
1644 
1645 	ql_dbg(ql_dbg_disc, vha, 0x2102,
1646 	    "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1647 	    __func__, fcport->port_name, fcport->disc_state,
1648 	    fcport->fw_login_state, fcport->login_pause,
1649 	    fcport->deleted, fcport->conflict,
1650 	    fcport->last_rscn_gen, fcport->rscn_gen,
1651 	    fcport->last_login_gen, fcport->login_gen,
1652 	    fcport->flags);
1653 
1654 	if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1655 	    (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1656 		return;
1657 
1658 	if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
1659 		if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1660 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1661 			return;
1662 		}
1663 	}
1664 
1665 	if (fcport->last_rscn_gen != fcport->rscn_gen) {
1666 		ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
1667 		    __func__, __LINE__, fcport->port_name);
1668 
1669 		return;
1670 	}
1671 
1672 	qla24xx_fcport_handle_login(vha, fcport);
1673 }
1674 
1675 
1676 static void qla_handle_els_plogi_done(scsi_qla_host_t *vha,
1677 				      struct event_arg *ea)
1678 {
1679 	ql_dbg(ql_dbg_disc, vha, 0x2118,
1680 	    "%s %d %8phC post PRLI\n",
1681 	    __func__, __LINE__, ea->fcport->port_name);
1682 	qla24xx_post_prli_work(vha, ea->fcport);
1683 }
1684 
1685 void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
1686 {
1687 	fc_port_t *fcport;
1688 
1689 	switch (ea->event) {
1690 	case FCME_RELOGIN:
1691 		if (test_bit(UNLOADING, &vha->dpc_flags))
1692 			return;
1693 
1694 		qla24xx_handle_relogin_event(vha, ea);
1695 		break;
1696 	case FCME_RSCN:
1697 		if (test_bit(UNLOADING, &vha->dpc_flags))
1698 			return;
1699 		{
1700 			unsigned long flags;
1701 
1702 			fcport = qla2x00_find_fcport_by_nportid
1703 				(vha, &ea->id, 1);
1704 			if (fcport) {
1705 				fcport->scan_needed = 1;
1706 				fcport->rscn_gen++;
1707 			}
1708 
1709 			spin_lock_irqsave(&vha->work_lock, flags);
1710 			if (vha->scan.scan_flags == 0) {
1711 				ql_dbg(ql_dbg_disc, vha, 0xffff,
1712 				    "%s: schedule\n", __func__);
1713 				vha->scan.scan_flags |= SF_QUEUED;
1714 				schedule_delayed_work(&vha->scan.scan_work, 5);
1715 			}
1716 			spin_unlock_irqrestore(&vha->work_lock, flags);
1717 		}
1718 		break;
1719 	case FCME_GNL_DONE:
1720 		qla24xx_handle_gnl_done_event(vha, ea);
1721 		break;
1722 	case FCME_GPSC_DONE:
1723 		qla24xx_handle_gpsc_event(vha, ea);
1724 		break;
1725 	case FCME_PLOGI_DONE:	/* Initiator side sent LLIOCB */
1726 		qla24xx_handle_plogi_done_event(vha, ea);
1727 		break;
1728 	case FCME_PRLI_DONE:
1729 		qla24xx_handle_prli_done_event(vha, ea);
1730 		break;
1731 	case FCME_GPDB_DONE:
1732 		qla24xx_handle_gpdb_event(vha, ea);
1733 		break;
1734 	case FCME_GPNID_DONE:
1735 		qla24xx_handle_gpnid_event(vha, ea);
1736 		break;
1737 	case FCME_GFFID_DONE:
1738 		qla24xx_handle_gffid_event(vha, ea);
1739 		break;
1740 	case FCME_ADISC_DONE:
1741 		qla24xx_handle_adisc_event(vha, ea);
1742 		break;
1743 	case FCME_GNNID_DONE:
1744 		qla24xx_handle_gnnid_event(vha, ea);
1745 		break;
1746 	case FCME_GFPNID_DONE:
1747 		qla24xx_handle_gfpnid_event(vha, ea);
1748 		break;
1749 	case FCME_ELS_PLOGI_DONE:
1750 		qla_handle_els_plogi_done(vha, ea);
1751 		break;
1752 	default:
1753 		BUG_ON(1);
1754 		break;
1755 	}
1756 }
1757 
1758 /*
1759  * RSCN(s) came in for this fcport, but the RSCN(s) was not able
1760  * to be consumed by the fcport
1761  */
1762 void qla_rscn_replay(fc_port_t *fcport)
1763 {
1764 	struct event_arg ea;
1765 
1766 	switch (fcport->disc_state) {
1767 	case DSC_DELETE_PEND:
1768 		return;
1769 	default:
1770 		break;
1771 	}
1772 
1773 	if (fcport->scan_needed) {
1774 		memset(&ea, 0, sizeof(ea));
1775 		ea.event = FCME_RSCN;
1776 		ea.id = fcport->d_id;
1777 		ea.id.b.rsvd_1 = RSCN_PORT_ADDR;
1778 		qla2x00_fcport_event_handler(fcport->vha, &ea);
1779 	}
1780 }
1781 
1782 static void
1783 qla2x00_tmf_iocb_timeout(void *data)
1784 {
1785 	srb_t *sp = data;
1786 	struct srb_iocb *tmf = &sp->u.iocb_cmd;
1787 
1788 	tmf->u.tmf.comp_status = CS_TIMEOUT;
1789 	complete(&tmf->u.tmf.comp);
1790 }
1791 
1792 static void
1793 qla2x00_tmf_sp_done(void *ptr, int res)
1794 {
1795 	srb_t *sp = ptr;
1796 	struct srb_iocb *tmf = &sp->u.iocb_cmd;
1797 
1798 	complete(&tmf->u.tmf.comp);
1799 }
1800 
1801 int
1802 qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
1803 	uint32_t tag)
1804 {
1805 	struct scsi_qla_host *vha = fcport->vha;
1806 	struct srb_iocb *tm_iocb;
1807 	srb_t *sp;
1808 	int rval = QLA_FUNCTION_FAILED;
1809 
1810 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1811 	if (!sp)
1812 		goto done;
1813 
1814 	tm_iocb = &sp->u.iocb_cmd;
1815 	sp->type = SRB_TM_CMD;
1816 	sp->name = "tmf";
1817 
1818 	tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1819 	init_completion(&tm_iocb->u.tmf.comp);
1820 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1821 
1822 	tm_iocb->u.tmf.flags = flags;
1823 	tm_iocb->u.tmf.lun = lun;
1824 	tm_iocb->u.tmf.data = tag;
1825 	sp->done = qla2x00_tmf_sp_done;
1826 
1827 	ql_dbg(ql_dbg_taskm, vha, 0x802f,
1828 	    "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1829 	    sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1830 	    fcport->d_id.b.area, fcport->d_id.b.al_pa);
1831 
1832 	rval = qla2x00_start_sp(sp);
1833 	if (rval != QLA_SUCCESS)
1834 		goto done_free_sp;
1835 	wait_for_completion(&tm_iocb->u.tmf.comp);
1836 
1837 	rval = tm_iocb->u.tmf.data;
1838 
1839 	if (rval != QLA_SUCCESS) {
1840 		ql_log(ql_log_warn, vha, 0x8030,
1841 		    "TM IOCB failed (%x).\n", rval);
1842 	}
1843 
1844 	if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1845 		flags = tm_iocb->u.tmf.flags;
1846 		lun = (uint16_t)tm_iocb->u.tmf.lun;
1847 
1848 		/* Issue Marker IOCB */
1849 		qla2x00_marker(vha, vha->hw->base_qpair,
1850 		    fcport->loop_id, lun,
1851 		    flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1852 	}
1853 
1854 done_free_sp:
1855 	sp->free(sp);
1856 	fcport->flags &= ~FCF_ASYNC_SENT;
1857 done:
1858 	return rval;
1859 }
1860 
1861 int
1862 qla24xx_async_abort_command(srb_t *sp)
1863 {
1864 	unsigned long   flags = 0;
1865 
1866 	uint32_t	handle;
1867 	fc_port_t	*fcport = sp->fcport;
1868 	struct qla_qpair *qpair = sp->qpair;
1869 	struct scsi_qla_host *vha = fcport->vha;
1870 	struct req_que *req = qpair->req;
1871 
1872 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1873 	for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1874 		if (req->outstanding_cmds[handle] == sp)
1875 			break;
1876 	}
1877 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1878 
1879 	if (handle == req->num_outstanding_cmds) {
1880 		/* Command not found. */
1881 		return QLA_FUNCTION_FAILED;
1882 	}
1883 	if (sp->type == SRB_FXIOCB_DCMD)
1884 		return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1885 		    FXDISC_ABORT_IOCTL);
1886 
1887 	return qla24xx_async_abort_cmd(sp, true);
1888 }
1889 
1890 static void
1891 qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1892 {
1893 	switch (ea->data[0]) {
1894 	case MBS_COMMAND_COMPLETE:
1895 		ql_dbg(ql_dbg_disc, vha, 0x2118,
1896 		    "%s %d %8phC post gpdb\n",
1897 		    __func__, __LINE__, ea->fcport->port_name);
1898 
1899 		ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1900 		ea->fcport->logout_on_delete = 1;
1901 		ea->fcport->nvme_prli_service_param = ea->iop[0];
1902 		if (ea->iop[0] & NVME_PRLI_SP_FIRST_BURST)
1903 			ea->fcport->nvme_first_burst_size =
1904 			    (ea->iop[1] & 0xffff) * 512;
1905 		else
1906 			ea->fcport->nvme_first_burst_size = 0;
1907 		qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1908 		break;
1909 	default:
1910 		if ((ea->iop[0] == LSC_SCODE_ELS_REJECT) &&
1911 		    (ea->iop[1] == 0x50000)) {   /* reson 5=busy expl:0x0 */
1912 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1913 			ea->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
1914 			break;
1915 		}
1916 
1917 		if (ea->fcport->n2n_flag) {
1918 			ql_dbg(ql_dbg_disc, vha, 0x2118,
1919 				"%s %d %8phC post fc4 prli\n",
1920 				__func__, __LINE__, ea->fcport->port_name);
1921 			ea->fcport->fc4f_nvme = 0;
1922 			ea->fcport->n2n_flag = 0;
1923 			qla24xx_post_prli_work(vha, ea->fcport);
1924 		}
1925 		ql_dbg(ql_dbg_disc, vha, 0x2119,
1926 		    "%s %d %8phC unhandle event of %x\n",
1927 		    __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1928 		break;
1929 	}
1930 }
1931 
1932 static void
1933 qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1934 {
1935 	port_id_t cid;	/* conflict Nport id */
1936 	u16 lid;
1937 	struct fc_port *conflict_fcport;
1938 	unsigned long flags;
1939 	struct fc_port *fcport = ea->fcport;
1940 
1941 	ql_dbg(ql_dbg_disc, vha, 0xffff,
1942 	    "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
1943 	    __func__, fcport->port_name, fcport->disc_state,
1944 	    fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
1945 	    ea->sp->gen1, fcport->rscn_gen,
1946 	    ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
1947 
1948 	if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1949 	    (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1950 		ql_dbg(ql_dbg_disc, vha, 0x20ea,
1951 		    "%s %d %8phC Remote is trying to login\n",
1952 		    __func__, __LINE__, fcport->port_name);
1953 		return;
1954 	}
1955 
1956 	if (fcport->disc_state == DSC_DELETE_PEND)
1957 		return;
1958 
1959 	if (ea->sp->gen2 != fcport->login_gen) {
1960 		/* target side must have changed it. */
1961 		ql_dbg(ql_dbg_disc, vha, 0x20d3,
1962 		    "%s %8phC generation changed\n",
1963 		    __func__, fcport->port_name);
1964 		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1965 		return;
1966 	} else if (ea->sp->gen1 != fcport->rscn_gen) {
1967 		ql_dbg(ql_dbg_disc, vha, 0x20d3,
1968 		    "%s %8phC RSCN generation changed\n",
1969 		    __func__, fcport->port_name);
1970 		qla_rscn_replay(fcport);
1971 		qlt_schedule_sess_for_deletion(fcport);
1972 		return;
1973 	}
1974 
1975 	switch (ea->data[0]) {
1976 	case MBS_COMMAND_COMPLETE:
1977 		/*
1978 		 * Driver must validate login state - If PRLI not complete,
1979 		 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1980 		 * requests.
1981 		 */
1982 		if (ea->fcport->fc4f_nvme) {
1983 			ql_dbg(ql_dbg_disc, vha, 0x2117,
1984 				"%s %d %8phC post prli\n",
1985 				__func__, __LINE__, ea->fcport->port_name);
1986 			qla24xx_post_prli_work(vha, ea->fcport);
1987 		} else {
1988 			ql_dbg(ql_dbg_disc, vha, 0x20ea,
1989 			    "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1990 			    __func__, __LINE__, ea->fcport->port_name,
1991 			    ea->fcport->loop_id, ea->fcport->d_id.b24);
1992 
1993 			set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1994 			spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1995 			ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1996 			ea->fcport->logout_on_delete = 1;
1997 			ea->fcport->send_els_logo = 0;
1998 			ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1999 			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
2000 
2001 			qla24xx_post_gpdb_work(vha, ea->fcport, 0);
2002 		}
2003 		break;
2004 	case MBS_COMMAND_ERROR:
2005 		ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
2006 		    __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
2007 
2008 		ea->fcport->flags &= ~FCF_ASYNC_SENT;
2009 		ea->fcport->disc_state = DSC_LOGIN_FAILED;
2010 		if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
2011 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2012 		else
2013 			qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
2014 		break;
2015 	case MBS_LOOP_ID_USED:
2016 		/* data[1] = IO PARAM 1 = nport ID  */
2017 		cid.b.domain = (ea->iop[1] >> 16) & 0xff;
2018 		cid.b.area   = (ea->iop[1] >>  8) & 0xff;
2019 		cid.b.al_pa  = ea->iop[1] & 0xff;
2020 		cid.b.rsvd_1 = 0;
2021 
2022 		ql_dbg(ql_dbg_disc, vha, 0x20ec,
2023 		    "%s %d %8phC lid %#x in use with pid %06x post gnl\n",
2024 		    __func__, __LINE__, ea->fcport->port_name,
2025 		    ea->fcport->loop_id, cid.b24);
2026 
2027 		set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
2028 		ea->fcport->loop_id = FC_NO_LOOP_ID;
2029 		qla24xx_post_gnl_work(vha, ea->fcport);
2030 		break;
2031 	case MBS_PORT_ID_USED:
2032 		lid = ea->iop[1] & 0xffff;
2033 		qlt_find_sess_invalidate_other(vha,
2034 		    wwn_to_u64(ea->fcport->port_name),
2035 		    ea->fcport->d_id, lid, &conflict_fcport);
2036 
2037 		if (conflict_fcport) {
2038 			/*
2039 			 * Another fcport share the same loop_id/nport id.
2040 			 * Conflict fcport needs to finish cleanup before this
2041 			 * fcport can proceed to login.
2042 			 */
2043 			conflict_fcport->conflict = ea->fcport;
2044 			ea->fcport->login_pause = 1;
2045 
2046 			ql_dbg(ql_dbg_disc, vha, 0x20ed,
2047 			    "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
2048 			    __func__, __LINE__, ea->fcport->port_name,
2049 			    ea->fcport->d_id.b24, lid);
2050 		} else {
2051 			ql_dbg(ql_dbg_disc, vha, 0x20ed,
2052 			    "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
2053 			    __func__, __LINE__, ea->fcport->port_name,
2054 			    ea->fcport->d_id.b24, lid);
2055 
2056 			qla2x00_clear_loop_id(ea->fcport);
2057 			set_bit(lid, vha->hw->loop_id_map);
2058 			ea->fcport->loop_id = lid;
2059 			ea->fcport->keep_nport_handle = 0;
2060 			qlt_schedule_sess_for_deletion(ea->fcport);
2061 		}
2062 		break;
2063 	}
2064 	return;
2065 }
2066 
2067 void
2068 qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
2069     uint16_t *data)
2070 {
2071 	qlt_logo_completion_handler(fcport, data[0]);
2072 	fcport->login_gen++;
2073 	fcport->flags &= ~FCF_ASYNC_ACTIVE;
2074 	return;
2075 }
2076 
2077 /****************************************************************************/
2078 /*                QLogic ISP2x00 Hardware Support Functions.                */
2079 /****************************************************************************/
2080 
2081 static int
2082 qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
2083 {
2084 	int rval = QLA_SUCCESS;
2085 	struct qla_hw_data *ha = vha->hw;
2086 	uint32_t idc_major_ver, idc_minor_ver;
2087 	uint16_t config[4];
2088 
2089 	qla83xx_idc_lock(vha, 0);
2090 
2091 	/* SV: TODO: Assign initialization timeout from
2092 	 * flash-info / other param
2093 	 */
2094 	ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
2095 	ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
2096 
2097 	/* Set our fcoe function presence */
2098 	if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
2099 		ql_dbg(ql_dbg_p3p, vha, 0xb077,
2100 		    "Error while setting DRV-Presence.\n");
2101 		rval = QLA_FUNCTION_FAILED;
2102 		goto exit;
2103 	}
2104 
2105 	/* Decide the reset ownership */
2106 	qla83xx_reset_ownership(vha);
2107 
2108 	/*
2109 	 * On first protocol driver load:
2110 	 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
2111 	 * register.
2112 	 * Others: Check compatibility with current IDC Major version.
2113 	 */
2114 	qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
2115 	if (ha->flags.nic_core_reset_owner) {
2116 		/* Set IDC Major version */
2117 		idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
2118 		qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
2119 
2120 		/* Clearing IDC-Lock-Recovery register */
2121 		qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
2122 	} else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
2123 		/*
2124 		 * Clear further IDC participation if we are not compatible with
2125 		 * the current IDC Major Version.
2126 		 */
2127 		ql_log(ql_log_warn, vha, 0xb07d,
2128 		    "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
2129 		    idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
2130 		__qla83xx_clear_drv_presence(vha);
2131 		rval = QLA_FUNCTION_FAILED;
2132 		goto exit;
2133 	}
2134 	/* Each function sets its supported Minor version. */
2135 	qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
2136 	idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
2137 	qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
2138 
2139 	if (ha->flags.nic_core_reset_owner) {
2140 		memset(config, 0, sizeof(config));
2141 		if (!qla81xx_get_port_config(vha, config))
2142 			qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
2143 			    QLA8XXX_DEV_READY);
2144 	}
2145 
2146 	rval = qla83xx_idc_state_handler(vha);
2147 
2148 exit:
2149 	qla83xx_idc_unlock(vha, 0);
2150 
2151 	return rval;
2152 }
2153 
2154 /*
2155 * qla2x00_initialize_adapter
2156 *      Initialize board.
2157 *
2158 * Input:
2159 *      ha = adapter block pointer.
2160 *
2161 * Returns:
2162 *      0 = success
2163 */
2164 int
2165 qla2x00_initialize_adapter(scsi_qla_host_t *vha)
2166 {
2167 	int	rval;
2168 	struct qla_hw_data *ha = vha->hw;
2169 	struct req_que *req = ha->req_q_map[0];
2170 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2171 
2172 	memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
2173 	memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
2174 
2175 	/* Clear adapter flags. */
2176 	vha->flags.online = 0;
2177 	ha->flags.chip_reset_done = 0;
2178 	vha->flags.reset_active = 0;
2179 	ha->flags.pci_channel_io_perm_failure = 0;
2180 	ha->flags.eeh_busy = 0;
2181 	vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
2182 	atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
2183 	atomic_set(&vha->loop_state, LOOP_DOWN);
2184 	vha->device_flags = DFLG_NO_CABLE;
2185 	vha->dpc_flags = 0;
2186 	vha->flags.management_server_logged_in = 0;
2187 	vha->marker_needed = 0;
2188 	ha->isp_abort_cnt = 0;
2189 	ha->beacon_blink_led = 0;
2190 
2191 	set_bit(0, ha->req_qid_map);
2192 	set_bit(0, ha->rsp_qid_map);
2193 
2194 	ql_dbg(ql_dbg_init, vha, 0x0040,
2195 	    "Configuring PCI space...\n");
2196 	rval = ha->isp_ops->pci_config(vha);
2197 	if (rval) {
2198 		ql_log(ql_log_warn, vha, 0x0044,
2199 		    "Unable to configure PCI space.\n");
2200 		return (rval);
2201 	}
2202 
2203 	ha->isp_ops->reset_chip(vha);
2204 
2205 	/* Check for secure flash support */
2206 	if (IS_QLA28XX(ha)) {
2207 		if (RD_REG_DWORD(&reg->mailbox12) & BIT_0) {
2208 			ql_log(ql_log_info, vha, 0xffff, "Adapter is Secure\n");
2209 			ha->flags.secure_adapter = 1;
2210 		}
2211 	}
2212 
2213 
2214 	rval = qla2xxx_get_flash_info(vha);
2215 	if (rval) {
2216 		ql_log(ql_log_fatal, vha, 0x004f,
2217 		    "Unable to validate FLASH data.\n");
2218 		return rval;
2219 	}
2220 
2221 	if (IS_QLA8044(ha)) {
2222 		qla8044_read_reset_template(vha);
2223 
2224 		/* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
2225 		 * If DONRESET_BIT0 is set, drivers should not set dev_state
2226 		 * to NEED_RESET. But if NEED_RESET is set, drivers should
2227 		 * should honor the reset. */
2228 		if (ql2xdontresethba == 1)
2229 			qla8044_set_idc_dontreset(vha);
2230 	}
2231 
2232 	ha->isp_ops->get_flash_version(vha, req->ring);
2233 	ql_dbg(ql_dbg_init, vha, 0x0061,
2234 	    "Configure NVRAM parameters...\n");
2235 
2236 	ha->isp_ops->nvram_config(vha);
2237 
2238 	if (ha->flags.disable_serdes) {
2239 		/* Mask HBA via NVRAM settings? */
2240 		ql_log(ql_log_info, vha, 0x0077,
2241 		    "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
2242 		return QLA_FUNCTION_FAILED;
2243 	}
2244 
2245 	ql_dbg(ql_dbg_init, vha, 0x0078,
2246 	    "Verifying loaded RISC code...\n");
2247 
2248 	if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
2249 		rval = ha->isp_ops->chip_diag(vha);
2250 		if (rval)
2251 			return (rval);
2252 		rval = qla2x00_setup_chip(vha);
2253 		if (rval)
2254 			return (rval);
2255 	}
2256 
2257 	if (IS_QLA84XX(ha)) {
2258 		ha->cs84xx = qla84xx_get_chip(vha);
2259 		if (!ha->cs84xx) {
2260 			ql_log(ql_log_warn, vha, 0x00d0,
2261 			    "Unable to configure ISP84XX.\n");
2262 			return QLA_FUNCTION_FAILED;
2263 		}
2264 	}
2265 
2266 	if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
2267 		rval = qla2x00_init_rings(vha);
2268 
2269 	ha->flags.chip_reset_done = 1;
2270 
2271 	if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
2272 		/* Issue verify 84xx FW IOCB to complete 84xx initialization */
2273 		rval = qla84xx_init_chip(vha);
2274 		if (rval != QLA_SUCCESS) {
2275 			ql_log(ql_log_warn, vha, 0x00d4,
2276 			    "Unable to initialize ISP84XX.\n");
2277 			qla84xx_put_chip(vha);
2278 		}
2279 	}
2280 
2281 	/* Load the NIC Core f/w if we are the first protocol driver. */
2282 	if (IS_QLA8031(ha)) {
2283 		rval = qla83xx_nic_core_fw_load(vha);
2284 		if (rval)
2285 			ql_log(ql_log_warn, vha, 0x0124,
2286 			    "Error in initializing NIC Core f/w.\n");
2287 	}
2288 
2289 	if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2290 		qla24xx_read_fcp_prio_cfg(vha);
2291 
2292 	if (IS_P3P_TYPE(ha))
2293 		qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2294 	else
2295 		qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2296 
2297 	return (rval);
2298 }
2299 
2300 /**
2301  * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
2302  * @vha: HA context
2303  *
2304  * Returns 0 on success.
2305  */
2306 int
2307 qla2100_pci_config(scsi_qla_host_t *vha)
2308 {
2309 	uint16_t w;
2310 	unsigned long flags;
2311 	struct qla_hw_data *ha = vha->hw;
2312 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2313 
2314 	pci_set_master(ha->pdev);
2315 	pci_try_set_mwi(ha->pdev);
2316 
2317 	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2318 	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2319 	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2320 
2321 	pci_disable_rom(ha->pdev);
2322 
2323 	/* Get PCI bus information. */
2324 	spin_lock_irqsave(&ha->hardware_lock, flags);
2325 	ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
2326 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2327 
2328 	return QLA_SUCCESS;
2329 }
2330 
2331 /**
2332  * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
2333  * @vha: HA context
2334  *
2335  * Returns 0 on success.
2336  */
2337 int
2338 qla2300_pci_config(scsi_qla_host_t *vha)
2339 {
2340 	uint16_t	w;
2341 	unsigned long   flags = 0;
2342 	uint32_t	cnt;
2343 	struct qla_hw_data *ha = vha->hw;
2344 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2345 
2346 	pci_set_master(ha->pdev);
2347 	pci_try_set_mwi(ha->pdev);
2348 
2349 	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2350 	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2351 
2352 	if (IS_QLA2322(ha) || IS_QLA6322(ha))
2353 		w &= ~PCI_COMMAND_INTX_DISABLE;
2354 	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2355 
2356 	/*
2357 	 * If this is a 2300 card and not 2312, reset the
2358 	 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2359 	 * the 2310 also reports itself as a 2300 so we need to get the
2360 	 * fb revision level -- a 6 indicates it really is a 2300 and
2361 	 * not a 2310.
2362 	 */
2363 	if (IS_QLA2300(ha)) {
2364 		spin_lock_irqsave(&ha->hardware_lock, flags);
2365 
2366 		/* Pause RISC. */
2367 		WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2368 		for (cnt = 0; cnt < 30000; cnt++) {
2369 			if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
2370 				break;
2371 
2372 			udelay(10);
2373 		}
2374 
2375 		/* Select FPM registers. */
2376 		WRT_REG_WORD(&reg->ctrl_status, 0x20);
2377 		RD_REG_WORD(&reg->ctrl_status);
2378 
2379 		/* Get the fb rev level */
2380 		ha->fb_rev = RD_FB_CMD_REG(ha, reg);
2381 
2382 		if (ha->fb_rev == FPM_2300)
2383 			pci_clear_mwi(ha->pdev);
2384 
2385 		/* Deselect FPM registers. */
2386 		WRT_REG_WORD(&reg->ctrl_status, 0x0);
2387 		RD_REG_WORD(&reg->ctrl_status);
2388 
2389 		/* Release RISC module. */
2390 		WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2391 		for (cnt = 0; cnt < 30000; cnt++) {
2392 			if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
2393 				break;
2394 
2395 			udelay(10);
2396 		}
2397 
2398 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
2399 	}
2400 
2401 	pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2402 
2403 	pci_disable_rom(ha->pdev);
2404 
2405 	/* Get PCI bus information. */
2406 	spin_lock_irqsave(&ha->hardware_lock, flags);
2407 	ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
2408 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2409 
2410 	return QLA_SUCCESS;
2411 }
2412 
2413 /**
2414  * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
2415  * @vha: HA context
2416  *
2417  * Returns 0 on success.
2418  */
2419 int
2420 qla24xx_pci_config(scsi_qla_host_t *vha)
2421 {
2422 	uint16_t w;
2423 	unsigned long flags = 0;
2424 	struct qla_hw_data *ha = vha->hw;
2425 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2426 
2427 	pci_set_master(ha->pdev);
2428 	pci_try_set_mwi(ha->pdev);
2429 
2430 	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2431 	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2432 	w &= ~PCI_COMMAND_INTX_DISABLE;
2433 	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2434 
2435 	pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2436 
2437 	/* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
2438 	if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2439 		pcix_set_mmrbc(ha->pdev, 2048);
2440 
2441 	/* PCIe -- adjust Maximum Read Request Size (2048). */
2442 	if (pci_is_pcie(ha->pdev))
2443 		pcie_set_readrq(ha->pdev, 4096);
2444 
2445 	pci_disable_rom(ha->pdev);
2446 
2447 	ha->chip_revision = ha->pdev->revision;
2448 
2449 	/* Get PCI bus information. */
2450 	spin_lock_irqsave(&ha->hardware_lock, flags);
2451 	ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2452 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2453 
2454 	return QLA_SUCCESS;
2455 }
2456 
2457 /**
2458  * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
2459  * @vha: HA context
2460  *
2461  * Returns 0 on success.
2462  */
2463 int
2464 qla25xx_pci_config(scsi_qla_host_t *vha)
2465 {
2466 	uint16_t w;
2467 	struct qla_hw_data *ha = vha->hw;
2468 
2469 	pci_set_master(ha->pdev);
2470 	pci_try_set_mwi(ha->pdev);
2471 
2472 	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2473 	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2474 	w &= ~PCI_COMMAND_INTX_DISABLE;
2475 	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2476 
2477 	/* PCIe -- adjust Maximum Read Request Size (2048). */
2478 	if (pci_is_pcie(ha->pdev))
2479 		pcie_set_readrq(ha->pdev, 4096);
2480 
2481 	pci_disable_rom(ha->pdev);
2482 
2483 	ha->chip_revision = ha->pdev->revision;
2484 
2485 	return QLA_SUCCESS;
2486 }
2487 
2488 /**
2489  * qla2x00_isp_firmware() - Choose firmware image.
2490  * @vha: HA context
2491  *
2492  * Returns 0 on success.
2493  */
2494 static int
2495 qla2x00_isp_firmware(scsi_qla_host_t *vha)
2496 {
2497 	int  rval;
2498 	uint16_t loop_id, topo, sw_cap;
2499 	uint8_t domain, area, al_pa;
2500 	struct qla_hw_data *ha = vha->hw;
2501 
2502 	/* Assume loading risc code */
2503 	rval = QLA_FUNCTION_FAILED;
2504 
2505 	if (ha->flags.disable_risc_code_load) {
2506 		ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
2507 
2508 		/* Verify checksum of loaded RISC code. */
2509 		rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
2510 		if (rval == QLA_SUCCESS) {
2511 			/* And, verify we are not in ROM code. */
2512 			rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
2513 			    &area, &domain, &topo, &sw_cap);
2514 		}
2515 	}
2516 
2517 	if (rval)
2518 		ql_dbg(ql_dbg_init, vha, 0x007a,
2519 		    "**** Load RISC code ****.\n");
2520 
2521 	return (rval);
2522 }
2523 
2524 /**
2525  * qla2x00_reset_chip() - Reset ISP chip.
2526  * @vha: HA context
2527  *
2528  * Returns 0 on success.
2529  */
2530 int
2531 qla2x00_reset_chip(scsi_qla_host_t *vha)
2532 {
2533 	unsigned long   flags = 0;
2534 	struct qla_hw_data *ha = vha->hw;
2535 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2536 	uint32_t	cnt;
2537 	uint16_t	cmd;
2538 	int rval = QLA_FUNCTION_FAILED;
2539 
2540 	if (unlikely(pci_channel_offline(ha->pdev)))
2541 		return rval;
2542 
2543 	ha->isp_ops->disable_intrs(ha);
2544 
2545 	spin_lock_irqsave(&ha->hardware_lock, flags);
2546 
2547 	/* Turn off master enable */
2548 	cmd = 0;
2549 	pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2550 	cmd &= ~PCI_COMMAND_MASTER;
2551 	pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2552 
2553 	if (!IS_QLA2100(ha)) {
2554 		/* Pause RISC. */
2555 		WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2556 		if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2557 			for (cnt = 0; cnt < 30000; cnt++) {
2558 				if ((RD_REG_WORD(&reg->hccr) &
2559 				    HCCR_RISC_PAUSE) != 0)
2560 					break;
2561 				udelay(100);
2562 			}
2563 		} else {
2564 			RD_REG_WORD(&reg->hccr);	/* PCI Posting. */
2565 			udelay(10);
2566 		}
2567 
2568 		/* Select FPM registers. */
2569 		WRT_REG_WORD(&reg->ctrl_status, 0x20);
2570 		RD_REG_WORD(&reg->ctrl_status);		/* PCI Posting. */
2571 
2572 		/* FPM Soft Reset. */
2573 		WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2574 		RD_REG_WORD(&reg->fpm_diag_config);	/* PCI Posting. */
2575 
2576 		/* Toggle Fpm Reset. */
2577 		if (!IS_QLA2200(ha)) {
2578 			WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2579 			RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2580 		}
2581 
2582 		/* Select frame buffer registers. */
2583 		WRT_REG_WORD(&reg->ctrl_status, 0x10);
2584 		RD_REG_WORD(&reg->ctrl_status);		/* PCI Posting. */
2585 
2586 		/* Reset frame buffer FIFOs. */
2587 		if (IS_QLA2200(ha)) {
2588 			WRT_FB_CMD_REG(ha, reg, 0xa000);
2589 			RD_FB_CMD_REG(ha, reg);		/* PCI Posting. */
2590 		} else {
2591 			WRT_FB_CMD_REG(ha, reg, 0x00fc);
2592 
2593 			/* Read back fb_cmd until zero or 3 seconds max */
2594 			for (cnt = 0; cnt < 3000; cnt++) {
2595 				if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2596 					break;
2597 				udelay(100);
2598 			}
2599 		}
2600 
2601 		/* Select RISC module registers. */
2602 		WRT_REG_WORD(&reg->ctrl_status, 0);
2603 		RD_REG_WORD(&reg->ctrl_status);		/* PCI Posting. */
2604 
2605 		/* Reset RISC processor. */
2606 		WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2607 		RD_REG_WORD(&reg->hccr);		/* PCI Posting. */
2608 
2609 		/* Release RISC processor. */
2610 		WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2611 		RD_REG_WORD(&reg->hccr);		/* PCI Posting. */
2612 	}
2613 
2614 	WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2615 	WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2616 
2617 	/* Reset ISP chip. */
2618 	WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2619 
2620 	/* Wait for RISC to recover from reset. */
2621 	if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2622 		/*
2623 		 * It is necessary to for a delay here since the card doesn't
2624 		 * respond to PCI reads during a reset. On some architectures
2625 		 * this will result in an MCA.
2626 		 */
2627 		udelay(20);
2628 		for (cnt = 30000; cnt; cnt--) {
2629 			if ((RD_REG_WORD(&reg->ctrl_status) &
2630 			    CSR_ISP_SOFT_RESET) == 0)
2631 				break;
2632 			udelay(100);
2633 		}
2634 	} else
2635 		udelay(10);
2636 
2637 	/* Reset RISC processor. */
2638 	WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2639 
2640 	WRT_REG_WORD(&reg->semaphore, 0);
2641 
2642 	/* Release RISC processor. */
2643 	WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2644 	RD_REG_WORD(&reg->hccr);			/* PCI Posting. */
2645 
2646 	if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2647 		for (cnt = 0; cnt < 30000; cnt++) {
2648 			if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
2649 				break;
2650 
2651 			udelay(100);
2652 		}
2653 	} else
2654 		udelay(100);
2655 
2656 	/* Turn on master enable */
2657 	cmd |= PCI_COMMAND_MASTER;
2658 	pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2659 
2660 	/* Disable RISC pause on FPM parity error. */
2661 	if (!IS_QLA2100(ha)) {
2662 		WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2663 		RD_REG_WORD(&reg->hccr);		/* PCI Posting. */
2664 	}
2665 
2666 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2667 
2668 	return QLA_SUCCESS;
2669 }
2670 
2671 /**
2672  * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2673  * @vha: HA context
2674  *
2675  * Returns 0 on success.
2676  */
2677 static int
2678 qla81xx_reset_mpi(scsi_qla_host_t *vha)
2679 {
2680 	uint16_t mb[4] = {0x1010, 0, 1, 0};
2681 
2682 	if (!IS_QLA81XX(vha->hw))
2683 		return QLA_SUCCESS;
2684 
2685 	return qla81xx_write_mpi_register(vha, mb);
2686 }
2687 
2688 /**
2689  * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
2690  * @vha: HA context
2691  *
2692  * Returns 0 on success.
2693  */
2694 static inline int
2695 qla24xx_reset_risc(scsi_qla_host_t *vha)
2696 {
2697 	unsigned long flags = 0;
2698 	struct qla_hw_data *ha = vha->hw;
2699 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2700 	uint32_t cnt;
2701 	uint16_t wd;
2702 	static int abts_cnt; /* ISP abort retry counts */
2703 	int rval = QLA_SUCCESS;
2704 
2705 	spin_lock_irqsave(&ha->hardware_lock, flags);
2706 
2707 	/* Reset RISC. */
2708 	WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2709 	for (cnt = 0; cnt < 30000; cnt++) {
2710 		if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2711 			break;
2712 
2713 		udelay(10);
2714 	}
2715 
2716 	if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2717 		set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2718 
2719 	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2720 	    "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2721 	    RD_REG_DWORD(&reg->hccr),
2722 	    RD_REG_DWORD(&reg->ctrl_status),
2723 	    (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2724 
2725 	WRT_REG_DWORD(&reg->ctrl_status,
2726 	    CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2727 	pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
2728 
2729 	udelay(100);
2730 
2731 	/* Wait for firmware to complete NVRAM accesses. */
2732 	RD_REG_WORD(&reg->mailbox0);
2733 	for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2734 	    rval == QLA_SUCCESS; cnt--) {
2735 		barrier();
2736 		if (cnt)
2737 			udelay(5);
2738 		else
2739 			rval = QLA_FUNCTION_TIMEOUT;
2740 	}
2741 
2742 	if (rval == QLA_SUCCESS)
2743 		set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2744 
2745 	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2746 	    "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2747 	    RD_REG_DWORD(&reg->hccr),
2748 	    RD_REG_DWORD(&reg->mailbox0));
2749 
2750 	/* Wait for soft-reset to complete. */
2751 	RD_REG_DWORD(&reg->ctrl_status);
2752 	for (cnt = 0; cnt < 60; cnt++) {
2753 		barrier();
2754 		if ((RD_REG_DWORD(&reg->ctrl_status) &
2755 		    CSRX_ISP_SOFT_RESET) == 0)
2756 			break;
2757 
2758 		udelay(5);
2759 	}
2760 	if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2761 		set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2762 
2763 	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2764 	    "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2765 	    RD_REG_DWORD(&reg->hccr),
2766 	    RD_REG_DWORD(&reg->ctrl_status));
2767 
2768 	/* If required, do an MPI FW reset now */
2769 	if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2770 		if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2771 			if (++abts_cnt < 5) {
2772 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2773 				set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2774 			} else {
2775 				/*
2776 				 * We exhausted the ISP abort retries. We have to
2777 				 * set the board offline.
2778 				 */
2779 				abts_cnt = 0;
2780 				vha->flags.online = 0;
2781 			}
2782 		}
2783 	}
2784 
2785 	WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2786 	RD_REG_DWORD(&reg->hccr);
2787 
2788 	WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2789 	RD_REG_DWORD(&reg->hccr);
2790 
2791 	WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2792 	RD_REG_DWORD(&reg->hccr);
2793 
2794 	RD_REG_WORD(&reg->mailbox0);
2795 	for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
2796 	    rval == QLA_SUCCESS; cnt--) {
2797 		barrier();
2798 		if (cnt)
2799 			udelay(5);
2800 		else
2801 			rval = QLA_FUNCTION_TIMEOUT;
2802 	}
2803 	if (rval == QLA_SUCCESS)
2804 		set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2805 
2806 	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2807 	    "Host Risc 0x%x, mailbox0 0x%x\n",
2808 	    RD_REG_DWORD(&reg->hccr),
2809 	     RD_REG_WORD(&reg->mailbox0));
2810 
2811 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2812 
2813 	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2814 	    "Driver in %s mode\n",
2815 	    IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2816 
2817 	if (IS_NOPOLLING_TYPE(ha))
2818 		ha->isp_ops->enable_intrs(ha);
2819 
2820 	return rval;
2821 }
2822 
2823 static void
2824 qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2825 {
2826 	struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2827 
2828 	WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2829 	*data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2830 
2831 }
2832 
2833 static void
2834 qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2835 {
2836 	struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2837 
2838 	WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2839 	WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2840 }
2841 
2842 static void
2843 qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2844 {
2845 	uint32_t wd32 = 0;
2846 	uint delta_msec = 100;
2847 	uint elapsed_msec = 0;
2848 	uint timeout_msec;
2849 	ulong n;
2850 
2851 	if (vha->hw->pdev->subsystem_device != 0x0175 &&
2852 	    vha->hw->pdev->subsystem_device != 0x0240)
2853 		return;
2854 
2855 	WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2856 	udelay(100);
2857 
2858 attempt:
2859 	timeout_msec = TIMEOUT_SEMAPHORE;
2860 	n = timeout_msec / delta_msec;
2861 	while (n--) {
2862 		qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2863 		qla25xx_read_risc_sema_reg(vha, &wd32);
2864 		if (wd32 & RISC_SEMAPHORE)
2865 			break;
2866 		msleep(delta_msec);
2867 		elapsed_msec += delta_msec;
2868 		if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2869 			goto force;
2870 	}
2871 
2872 	if (!(wd32 & RISC_SEMAPHORE))
2873 		goto force;
2874 
2875 	if (!(wd32 & RISC_SEMAPHORE_FORCE))
2876 		goto acquired;
2877 
2878 	qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2879 	timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2880 	n = timeout_msec / delta_msec;
2881 	while (n--) {
2882 		qla25xx_read_risc_sema_reg(vha, &wd32);
2883 		if (!(wd32 & RISC_SEMAPHORE_FORCE))
2884 			break;
2885 		msleep(delta_msec);
2886 		elapsed_msec += delta_msec;
2887 		if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2888 			goto force;
2889 	}
2890 
2891 	if (wd32 & RISC_SEMAPHORE_FORCE)
2892 		qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2893 
2894 	goto attempt;
2895 
2896 force:
2897 	qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2898 
2899 acquired:
2900 	return;
2901 }
2902 
2903 /**
2904  * qla24xx_reset_chip() - Reset ISP24xx chip.
2905  * @vha: HA context
2906  *
2907  * Returns 0 on success.
2908  */
2909 int
2910 qla24xx_reset_chip(scsi_qla_host_t *vha)
2911 {
2912 	struct qla_hw_data *ha = vha->hw;
2913 	int rval = QLA_FUNCTION_FAILED;
2914 
2915 	if (pci_channel_offline(ha->pdev) &&
2916 	    ha->flags.pci_channel_io_perm_failure) {
2917 		return rval;
2918 	}
2919 
2920 	ha->isp_ops->disable_intrs(ha);
2921 
2922 	qla25xx_manipulate_risc_semaphore(vha);
2923 
2924 	/* Perform RISC reset. */
2925 	rval = qla24xx_reset_risc(vha);
2926 
2927 	return rval;
2928 }
2929 
2930 /**
2931  * qla2x00_chip_diag() - Test chip for proper operation.
2932  * @vha: HA context
2933  *
2934  * Returns 0 on success.
2935  */
2936 int
2937 qla2x00_chip_diag(scsi_qla_host_t *vha)
2938 {
2939 	int		rval;
2940 	struct qla_hw_data *ha = vha->hw;
2941 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2942 	unsigned long	flags = 0;
2943 	uint16_t	data;
2944 	uint32_t	cnt;
2945 	uint16_t	mb[5];
2946 	struct req_que *req = ha->req_q_map[0];
2947 
2948 	/* Assume a failed state */
2949 	rval = QLA_FUNCTION_FAILED;
2950 
2951 	ql_dbg(ql_dbg_init, vha, 0x007b, "Testing device at %p.\n",
2952 	       &reg->flash_address);
2953 
2954 	spin_lock_irqsave(&ha->hardware_lock, flags);
2955 
2956 	/* Reset ISP chip. */
2957 	WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2958 
2959 	/*
2960 	 * We need to have a delay here since the card will not respond while
2961 	 * in reset causing an MCA on some architectures.
2962 	 */
2963 	udelay(20);
2964 	data = qla2x00_debounce_register(&reg->ctrl_status);
2965 	for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2966 		udelay(5);
2967 		data = RD_REG_WORD(&reg->ctrl_status);
2968 		barrier();
2969 	}
2970 
2971 	if (!cnt)
2972 		goto chip_diag_failed;
2973 
2974 	ql_dbg(ql_dbg_init, vha, 0x007c,
2975 	    "Reset register cleared by chip reset.\n");
2976 
2977 	/* Reset RISC processor. */
2978 	WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2979 	WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2980 
2981 	/* Workaround for QLA2312 PCI parity error */
2982 	if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2983 		data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2984 		for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2985 			udelay(5);
2986 			data = RD_MAILBOX_REG(ha, reg, 0);
2987 			barrier();
2988 		}
2989 	} else
2990 		udelay(10);
2991 
2992 	if (!cnt)
2993 		goto chip_diag_failed;
2994 
2995 	/* Check product ID of chip */
2996 	ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
2997 
2998 	mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2999 	mb[2] = RD_MAILBOX_REG(ha, reg, 2);
3000 	mb[3] = RD_MAILBOX_REG(ha, reg, 3);
3001 	mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
3002 	if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
3003 	    mb[3] != PROD_ID_3) {
3004 		ql_log(ql_log_warn, vha, 0x0062,
3005 		    "Wrong product ID = 0x%x,0x%x,0x%x.\n",
3006 		    mb[1], mb[2], mb[3]);
3007 
3008 		goto chip_diag_failed;
3009 	}
3010 	ha->product_id[0] = mb[1];
3011 	ha->product_id[1] = mb[2];
3012 	ha->product_id[2] = mb[3];
3013 	ha->product_id[3] = mb[4];
3014 
3015 	/* Adjust fw RISC transfer size */
3016 	if (req->length > 1024)
3017 		ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
3018 	else
3019 		ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
3020 		    req->length;
3021 
3022 	if (IS_QLA2200(ha) &&
3023 	    RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
3024 		/* Limit firmware transfer size with a 2200A */
3025 		ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
3026 
3027 		ha->device_type |= DT_ISP2200A;
3028 		ha->fw_transfer_size = 128;
3029 	}
3030 
3031 	/* Wrap Incoming Mailboxes Test. */
3032 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3033 
3034 	ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
3035 	rval = qla2x00_mbx_reg_test(vha);
3036 	if (rval)
3037 		ql_log(ql_log_warn, vha, 0x0080,
3038 		    "Failed mailbox send register test.\n");
3039 	else
3040 		/* Flag a successful rval */
3041 		rval = QLA_SUCCESS;
3042 	spin_lock_irqsave(&ha->hardware_lock, flags);
3043 
3044 chip_diag_failed:
3045 	if (rval)
3046 		ql_log(ql_log_info, vha, 0x0081,
3047 		    "Chip diagnostics **** FAILED ****.\n");
3048 
3049 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3050 
3051 	return (rval);
3052 }
3053 
3054 /**
3055  * qla24xx_chip_diag() - Test ISP24xx for proper operation.
3056  * @vha: HA context
3057  *
3058  * Returns 0 on success.
3059  */
3060 int
3061 qla24xx_chip_diag(scsi_qla_host_t *vha)
3062 {
3063 	int rval;
3064 	struct qla_hw_data *ha = vha->hw;
3065 	struct req_que *req = ha->req_q_map[0];
3066 
3067 	if (IS_P3P_TYPE(ha))
3068 		return QLA_SUCCESS;
3069 
3070 	ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
3071 
3072 	rval = qla2x00_mbx_reg_test(vha);
3073 	if (rval) {
3074 		ql_log(ql_log_warn, vha, 0x0082,
3075 		    "Failed mailbox send register test.\n");
3076 	} else {
3077 		/* Flag a successful rval */
3078 		rval = QLA_SUCCESS;
3079 	}
3080 
3081 	return rval;
3082 }
3083 
3084 static void
3085 qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
3086 {
3087 	int rval;
3088 	dma_addr_t tc_dma;
3089 	void *tc;
3090 	struct qla_hw_data *ha = vha->hw;
3091 
3092 	if (ha->eft) {
3093 		ql_dbg(ql_dbg_init, vha, 0x00bd,
3094 		    "%s: Offload Mem is already allocated.\n",
3095 		    __func__);
3096 		return;
3097 	}
3098 
3099 	if (IS_FWI2_CAPABLE(ha)) {
3100 		/* Allocate memory for Fibre Channel Event Buffer. */
3101 		if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
3102 		    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3103 			goto try_eft;
3104 
3105 		if (ha->fce)
3106 			dma_free_coherent(&ha->pdev->dev,
3107 			    FCE_SIZE, ha->fce, ha->fce_dma);
3108 
3109 		/* Allocate memory for Fibre Channel Event Buffer. */
3110 		tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
3111 					GFP_KERNEL);
3112 		if (!tc) {
3113 			ql_log(ql_log_warn, vha, 0x00be,
3114 			    "Unable to allocate (%d KB) for FCE.\n",
3115 			    FCE_SIZE / 1024);
3116 			goto try_eft;
3117 		}
3118 
3119 		rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
3120 		    ha->fce_mb, &ha->fce_bufs);
3121 		if (rval) {
3122 			ql_log(ql_log_warn, vha, 0x00bf,
3123 			    "Unable to initialize FCE (%d).\n", rval);
3124 			dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
3125 			    tc_dma);
3126 			ha->flags.fce_enabled = 0;
3127 			goto try_eft;
3128 		}
3129 		ql_dbg(ql_dbg_init, vha, 0x00c0,
3130 		    "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
3131 
3132 		ha->flags.fce_enabled = 1;
3133 		ha->fce_dma = tc_dma;
3134 		ha->fce = tc;
3135 
3136 try_eft:
3137 		if (ha->eft)
3138 			dma_free_coherent(&ha->pdev->dev,
3139 			    EFT_SIZE, ha->eft, ha->eft_dma);
3140 
3141 		/* Allocate memory for Extended Trace Buffer. */
3142 		tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
3143 					GFP_KERNEL);
3144 		if (!tc) {
3145 			ql_log(ql_log_warn, vha, 0x00c1,
3146 			    "Unable to allocate (%d KB) for EFT.\n",
3147 			    EFT_SIZE / 1024);
3148 			goto eft_err;
3149 		}
3150 
3151 		rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
3152 		if (rval) {
3153 			ql_log(ql_log_warn, vha, 0x00c2,
3154 			    "Unable to initialize EFT (%d).\n", rval);
3155 			dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
3156 			    tc_dma);
3157 			goto eft_err;
3158 		}
3159 		ql_dbg(ql_dbg_init, vha, 0x00c3,
3160 		    "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
3161 
3162 		ha->eft_dma = tc_dma;
3163 		ha->eft = tc;
3164 	}
3165 
3166 eft_err:
3167 	return;
3168 }
3169 
3170 void
3171 qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
3172 {
3173 	int rval;
3174 	uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
3175 	    eft_size, fce_size, mq_size;
3176 	struct qla_hw_data *ha = vha->hw;
3177 	struct req_que *req = ha->req_q_map[0];
3178 	struct rsp_que *rsp = ha->rsp_q_map[0];
3179 	struct qla2xxx_fw_dump *fw_dump;
3180 	dma_addr_t tc_dma;
3181 	void *tc;
3182 
3183 	dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
3184 	req_q_size = rsp_q_size = 0;
3185 
3186 	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3187 		fixed_size = sizeof(struct qla2100_fw_dump);
3188 	} else if (IS_QLA23XX(ha)) {
3189 		fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
3190 		mem_size = (ha->fw_memory_size - 0x11000 + 1) *
3191 		    sizeof(uint16_t);
3192 	} else if (IS_FWI2_CAPABLE(ha)) {
3193 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
3194 			fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
3195 		else if (IS_QLA81XX(ha))
3196 			fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
3197 		else if (IS_QLA25XX(ha))
3198 			fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
3199 		else
3200 			fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
3201 
3202 		mem_size = (ha->fw_memory_size - 0x100000 + 1) *
3203 		    sizeof(uint32_t);
3204 		if (ha->mqenable) {
3205 			if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha) &&
3206 			    !IS_QLA28XX(ha))
3207 				mq_size = sizeof(struct qla2xxx_mq_chain);
3208 			/*
3209 			 * Allocate maximum buffer size for all queues - Q0.
3210 			 * Resizing must be done at end-of-dump processing.
3211 			 */
3212 			mq_size += (ha->max_req_queues - 1) *
3213 			    (req->length * sizeof(request_t));
3214 			mq_size += (ha->max_rsp_queues - 1) *
3215 			    (rsp->length * sizeof(response_t));
3216 		}
3217 		if (ha->tgt.atio_ring)
3218 			mq_size += ha->tgt.atio_q_length * sizeof(request_t);
3219 		/* Allocate memory for Fibre Channel Event Buffer. */
3220 		if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
3221 		    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3222 			goto try_eft;
3223 
3224 		fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
3225 try_eft:
3226 		if (ha->eft)
3227 			dma_free_coherent(&ha->pdev->dev,
3228 			    EFT_SIZE, ha->eft, ha->eft_dma);
3229 
3230 		/* Allocate memory for Extended Trace Buffer. */
3231 		tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
3232 					 GFP_KERNEL);
3233 		if (!tc) {
3234 			ql_log(ql_log_warn, vha, 0x00c1,
3235 			    "Unable to allocate (%d KB) for EFT.\n",
3236 			    EFT_SIZE / 1024);
3237 			goto allocate;
3238 		}
3239 
3240 		rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
3241 		if (rval) {
3242 			ql_log(ql_log_warn, vha, 0x00c2,
3243 			    "Unable to initialize EFT (%d).\n", rval);
3244 			dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
3245 			    tc_dma);
3246 		}
3247 		ql_dbg(ql_dbg_init, vha, 0x00c3,
3248 		    "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
3249 		eft_size = EFT_SIZE;
3250 	}
3251 
3252 	if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
3253 		struct fwdt *fwdt = ha->fwdt;
3254 		uint j;
3255 
3256 		for (j = 0; j < 2; j++, fwdt++) {
3257 			if (!fwdt->template) {
3258 				ql_log(ql_log_warn, vha, 0x00ba,
3259 				    "-> fwdt%u no template\n", j);
3260 				continue;
3261 			}
3262 			ql_dbg(ql_dbg_init, vha, 0x00fa,
3263 			    "-> fwdt%u calculating fwdump size...\n", j);
3264 			fwdt->dump_size = qla27xx_fwdt_calculate_dump_size(
3265 			    vha, fwdt->template);
3266 			ql_dbg(ql_dbg_init, vha, 0x00fa,
3267 			    "-> fwdt%u calculated fwdump size = %#lx bytes\n",
3268 			    j, fwdt->dump_size);
3269 			dump_size += fwdt->dump_size;
3270 		}
3271 		goto allocate;
3272 	}
3273 
3274 	req_q_size = req->length * sizeof(request_t);
3275 	rsp_q_size = rsp->length * sizeof(response_t);
3276 	dump_size = offsetof(struct qla2xxx_fw_dump, isp);
3277 	dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
3278 	ha->chain_offset = dump_size;
3279 	dump_size += mq_size + fce_size;
3280 
3281 	if (ha->exchoffld_buf)
3282 		dump_size += sizeof(struct qla2xxx_offld_chain) +
3283 			ha->exchoffld_size;
3284 	if (ha->exlogin_buf)
3285 		dump_size += sizeof(struct qla2xxx_offld_chain) +
3286 			ha->exlogin_size;
3287 
3288 allocate:
3289 	if (!ha->fw_dump_len || dump_size > ha->fw_dump_alloc_len) {
3290 
3291 		ql_dbg(ql_dbg_init, vha, 0x00c5,
3292 		    "%s dump_size %d fw_dump_len %d fw_dump_alloc_len %d\n",
3293 		    __func__, dump_size, ha->fw_dump_len,
3294 		    ha->fw_dump_alloc_len);
3295 
3296 		fw_dump = vmalloc(dump_size);
3297 		if (!fw_dump) {
3298 			ql_log(ql_log_warn, vha, 0x00c4,
3299 			    "Unable to allocate (%d KB) for firmware dump.\n",
3300 			    dump_size / 1024);
3301 		} else {
3302 			mutex_lock(&ha->optrom_mutex);
3303 			if (ha->fw_dumped) {
3304 				memcpy(fw_dump, ha->fw_dump, ha->fw_dump_len);
3305 				vfree(ha->fw_dump);
3306 				ha->fw_dump = fw_dump;
3307 				ha->fw_dump_alloc_len =  dump_size;
3308 				ql_dbg(ql_dbg_init, vha, 0x00c5,
3309 				    "Re-Allocated (%d KB) and save firmware dump.\n",
3310 				    dump_size / 1024);
3311 			} else {
3312 				if (ha->fw_dump)
3313 					vfree(ha->fw_dump);
3314 				ha->fw_dump = fw_dump;
3315 
3316 				ha->fw_dump_len = ha->fw_dump_alloc_len =
3317 				    dump_size;
3318 				ql_dbg(ql_dbg_init, vha, 0x00c5,
3319 				    "Allocated (%d KB) for firmware dump.\n",
3320 				    dump_size / 1024);
3321 
3322 				if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
3323 					mutex_unlock(&ha->optrom_mutex);
3324 					return;
3325 				}
3326 
3327 				ha->fw_dump->signature[0] = 'Q';
3328 				ha->fw_dump->signature[1] = 'L';
3329 				ha->fw_dump->signature[2] = 'G';
3330 				ha->fw_dump->signature[3] = 'C';
3331 				ha->fw_dump->version = htonl(1);
3332 
3333 				ha->fw_dump->fixed_size = htonl(fixed_size);
3334 				ha->fw_dump->mem_size = htonl(mem_size);
3335 				ha->fw_dump->req_q_size = htonl(req_q_size);
3336 				ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3337 
3338 				ha->fw_dump->eft_size = htonl(eft_size);
3339 				ha->fw_dump->eft_addr_l =
3340 				    htonl(LSD(ha->eft_dma));
3341 				ha->fw_dump->eft_addr_h =
3342 				    htonl(MSD(ha->eft_dma));
3343 
3344 				ha->fw_dump->header_size =
3345 					htonl(offsetof
3346 					    (struct qla2xxx_fw_dump, isp));
3347 			}
3348 			mutex_unlock(&ha->optrom_mutex);
3349 		}
3350 	}
3351 }
3352 
3353 static int
3354 qla81xx_mpi_sync(scsi_qla_host_t *vha)
3355 {
3356 #define MPS_MASK	0xe0
3357 	int rval;
3358 	uint16_t dc;
3359 	uint32_t dw;
3360 
3361 	if (!IS_QLA81XX(vha->hw))
3362 		return QLA_SUCCESS;
3363 
3364 	rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3365 	if (rval != QLA_SUCCESS) {
3366 		ql_log(ql_log_warn, vha, 0x0105,
3367 		    "Unable to acquire semaphore.\n");
3368 		goto done;
3369 	}
3370 
3371 	pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3372 	rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3373 	if (rval != QLA_SUCCESS) {
3374 		ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
3375 		goto done_release;
3376 	}
3377 
3378 	dc &= MPS_MASK;
3379 	if (dc == (dw & MPS_MASK))
3380 		goto done_release;
3381 
3382 	dw &= ~MPS_MASK;
3383 	dw |= dc;
3384 	rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3385 	if (rval != QLA_SUCCESS) {
3386 		ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
3387 	}
3388 
3389 done_release:
3390 	rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3391 	if (rval != QLA_SUCCESS) {
3392 		ql_log(ql_log_warn, vha, 0x006d,
3393 		    "Unable to release semaphore.\n");
3394 	}
3395 
3396 done:
3397 	return rval;
3398 }
3399 
3400 int
3401 qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3402 {
3403 	/* Don't try to reallocate the array */
3404 	if (req->outstanding_cmds)
3405 		return QLA_SUCCESS;
3406 
3407 	if (!IS_FWI2_CAPABLE(ha))
3408 		req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3409 	else {
3410 		if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3411 			req->num_outstanding_cmds = ha->cur_fw_xcb_count;
3412 		else
3413 			req->num_outstanding_cmds = ha->cur_fw_iocb_count;
3414 	}
3415 
3416 	req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
3417 					sizeof(srb_t *),
3418 					GFP_KERNEL);
3419 
3420 	if (!req->outstanding_cmds) {
3421 		/*
3422 		 * Try to allocate a minimal size just so we can get through
3423 		 * initialization.
3424 		 */
3425 		req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3426 		req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
3427 						sizeof(srb_t *),
3428 						GFP_KERNEL);
3429 
3430 		if (!req->outstanding_cmds) {
3431 			ql_log(ql_log_fatal, NULL, 0x0126,
3432 			    "Failed to allocate memory for "
3433 			    "outstanding_cmds for req_que %p.\n", req);
3434 			req->num_outstanding_cmds = 0;
3435 			return QLA_FUNCTION_FAILED;
3436 		}
3437 	}
3438 
3439 	return QLA_SUCCESS;
3440 }
3441 
3442 #define PRINT_FIELD(_field, _flag, _str) {		\
3443 	if (a0->_field & _flag) {\
3444 		if (p) {\
3445 			strcat(ptr, "|");\
3446 			ptr++;\
3447 			leftover--;\
3448 		} \
3449 		len = snprintf(ptr, leftover, "%s", _str);	\
3450 		p = 1;\
3451 		leftover -= len;\
3452 		ptr += len; \
3453 	} \
3454 }
3455 
3456 static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3457 {
3458 #define STR_LEN 64
3459 	struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3460 	u8 str[STR_LEN], *ptr, p;
3461 	int leftover, len;
3462 
3463 	memset(str, 0, STR_LEN);
3464 	snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3465 	ql_dbg(ql_dbg_init, vha, 0x015a,
3466 	    "SFP MFG Name: %s\n", str);
3467 
3468 	memset(str, 0, STR_LEN);
3469 	snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3470 	ql_dbg(ql_dbg_init, vha, 0x015c,
3471 	    "SFP Part Name: %s\n", str);
3472 
3473 	/* media */
3474 	memset(str, 0, STR_LEN);
3475 	ptr = str;
3476 	leftover = STR_LEN;
3477 	p = len = 0;
3478 	PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3479 	PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3480 	PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3481 	PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3482 	PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3483 	PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3484 	PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3485 	ql_dbg(ql_dbg_init, vha, 0x0160,
3486 	    "SFP Media: %s\n", str);
3487 
3488 	/* link length */
3489 	memset(str, 0, STR_LEN);
3490 	ptr = str;
3491 	leftover = STR_LEN;
3492 	p = len = 0;
3493 	PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3494 	PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3495 	PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3496 	PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3497 	PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3498 	ql_dbg(ql_dbg_init, vha, 0x0196,
3499 	    "SFP Link Length: %s\n", str);
3500 
3501 	memset(str, 0, STR_LEN);
3502 	ptr = str;
3503 	leftover = STR_LEN;
3504 	p = len = 0;
3505 	PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3506 	PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3507 	PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3508 	PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3509 	PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3510 	ql_dbg(ql_dbg_init, vha, 0x016e,
3511 	    "SFP FC Link Tech: %s\n", str);
3512 
3513 	if (a0->length_km)
3514 		ql_dbg(ql_dbg_init, vha, 0x016f,
3515 		    "SFP Distant: %d km\n", a0->length_km);
3516 	if (a0->length_100m)
3517 		ql_dbg(ql_dbg_init, vha, 0x0170,
3518 		    "SFP Distant: %d m\n", a0->length_100m*100);
3519 	if (a0->length_50um_10m)
3520 		ql_dbg(ql_dbg_init, vha, 0x0189,
3521 		    "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3522 	if (a0->length_62um_10m)
3523 		ql_dbg(ql_dbg_init, vha, 0x018a,
3524 		  "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3525 	if (a0->length_om4_10m)
3526 		ql_dbg(ql_dbg_init, vha, 0x0194,
3527 		    "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3528 	if (a0->length_om3_10m)
3529 		ql_dbg(ql_dbg_init, vha, 0x0195,
3530 		    "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3531 }
3532 
3533 
3534 /*
3535  * Return Code:
3536  *   QLA_SUCCESS: no action
3537  *   QLA_INTERFACE_ERROR: SFP is not there.
3538  *   QLA_FUNCTION_FAILED: detected New SFP
3539  */
3540 int
3541 qla24xx_detect_sfp(scsi_qla_host_t *vha)
3542 {
3543 	int rc = QLA_SUCCESS;
3544 	struct sff_8247_a0 *a;
3545 	struct qla_hw_data *ha = vha->hw;
3546 
3547 	if (!AUTO_DETECT_SFP_SUPPORT(vha))
3548 		goto out;
3549 
3550 	rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3551 	if (rc)
3552 		goto out;
3553 
3554 	a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3555 	qla2xxx_print_sfp_info(vha);
3556 
3557 	if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
3558 		/* long range */
3559 		ha->flags.detected_lr_sfp = 1;
3560 
3561 		if (a->length_km > 5 || a->length_100m > 50)
3562 			ha->long_range_distance = LR_DISTANCE_10K;
3563 		else
3564 			ha->long_range_distance = LR_DISTANCE_5K;
3565 
3566 		if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
3567 			ql_dbg(ql_dbg_async, vha, 0x507b,
3568 			    "Detected Long Range SFP.\n");
3569 	} else {
3570 		/* short range */
3571 		ha->flags.detected_lr_sfp = 0;
3572 		if (ha->flags.using_lr_setting)
3573 			ql_dbg(ql_dbg_async, vha, 0x5084,
3574 			    "Detected Short Range SFP.\n");
3575 	}
3576 
3577 	if (!vha->flags.init_done)
3578 		rc = QLA_SUCCESS;
3579 out:
3580 	return rc;
3581 }
3582 
3583 /**
3584  * qla2x00_setup_chip() - Load and start RISC firmware.
3585  * @vha: HA context
3586  *
3587  * Returns 0 on success.
3588  */
3589 static int
3590 qla2x00_setup_chip(scsi_qla_host_t *vha)
3591 {
3592 	int rval;
3593 	uint32_t srisc_address = 0;
3594 	struct qla_hw_data *ha = vha->hw;
3595 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3596 	unsigned long flags;
3597 	uint16_t fw_major_version;
3598 
3599 	if (IS_P3P_TYPE(ha)) {
3600 		rval = ha->isp_ops->load_risc(vha, &srisc_address);
3601 		if (rval == QLA_SUCCESS) {
3602 			qla2x00_stop_firmware(vha);
3603 			goto enable_82xx_npiv;
3604 		} else
3605 			goto failed;
3606 	}
3607 
3608 	if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3609 		/* Disable SRAM, Instruction RAM and GP RAM parity.  */
3610 		spin_lock_irqsave(&ha->hardware_lock, flags);
3611 		WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3612 		RD_REG_WORD(&reg->hccr);
3613 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
3614 	}
3615 
3616 	qla81xx_mpi_sync(vha);
3617 
3618 	/* Load firmware sequences */
3619 	rval = ha->isp_ops->load_risc(vha, &srisc_address);
3620 	if (rval == QLA_SUCCESS) {
3621 		ql_dbg(ql_dbg_init, vha, 0x00c9,
3622 		    "Verifying Checksum of loaded RISC code.\n");
3623 
3624 		rval = qla2x00_verify_checksum(vha, srisc_address);
3625 		if (rval == QLA_SUCCESS) {
3626 			/* Start firmware execution. */
3627 			ql_dbg(ql_dbg_init, vha, 0x00ca,
3628 			    "Starting firmware.\n");
3629 
3630 			if (ql2xexlogins)
3631 				ha->flags.exlogins_enabled = 1;
3632 
3633 			if (qla_is_exch_offld_enabled(vha))
3634 				ha->flags.exchoffld_enabled = 1;
3635 
3636 			rval = qla2x00_execute_fw(vha, srisc_address);
3637 			/* Retrieve firmware information. */
3638 			if (rval == QLA_SUCCESS) {
3639 				qla24xx_detect_sfp(vha);
3640 
3641 				if ((IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3642 				    IS_QLA28XX(ha)) &&
3643 				    (ha->zio_mode == QLA_ZIO_MODE_6))
3644 					qla27xx_set_zio_threshold(vha,
3645 					    ha->last_zio_threshold);
3646 
3647 				rval = qla2x00_set_exlogins_buffer(vha);
3648 				if (rval != QLA_SUCCESS)
3649 					goto failed;
3650 
3651 				rval = qla2x00_set_exchoffld_buffer(vha);
3652 				if (rval != QLA_SUCCESS)
3653 					goto failed;
3654 
3655 enable_82xx_npiv:
3656 				fw_major_version = ha->fw_major_version;
3657 				if (IS_P3P_TYPE(ha))
3658 					qla82xx_check_md_needed(vha);
3659 				else
3660 					rval = qla2x00_get_fw_version(vha);
3661 				if (rval != QLA_SUCCESS)
3662 					goto failed;
3663 				ha->flags.npiv_supported = 0;
3664 				if (IS_QLA2XXX_MIDTYPE(ha) &&
3665 					 (ha->fw_attributes & BIT_2)) {
3666 					ha->flags.npiv_supported = 1;
3667 					if ((!ha->max_npiv_vports) ||
3668 					    ((ha->max_npiv_vports + 1) %
3669 					    MIN_MULTI_ID_FABRIC))
3670 						ha->max_npiv_vports =
3671 						    MIN_MULTI_ID_FABRIC - 1;
3672 				}
3673 				qla2x00_get_resource_cnts(vha);
3674 
3675 				/*
3676 				 * Allocate the array of outstanding commands
3677 				 * now that we know the firmware resources.
3678 				 */
3679 				rval = qla2x00_alloc_outstanding_cmds(ha,
3680 				    vha->req);
3681 				if (rval != QLA_SUCCESS)
3682 					goto failed;
3683 
3684 				if (!fw_major_version && !(IS_P3P_TYPE(ha)))
3685 					qla2x00_alloc_offload_mem(vha);
3686 
3687 				if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
3688 					qla2x00_alloc_fw_dump(vha);
3689 
3690 			} else {
3691 				goto failed;
3692 			}
3693 		} else {
3694 			ql_log(ql_log_fatal, vha, 0x00cd,
3695 			    "ISP Firmware failed checksum.\n");
3696 			goto failed;
3697 		}
3698 	} else
3699 		goto failed;
3700 
3701 	if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3702 		/* Enable proper parity. */
3703 		spin_lock_irqsave(&ha->hardware_lock, flags);
3704 		if (IS_QLA2300(ha))
3705 			/* SRAM parity */
3706 			WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3707 		else
3708 			/* SRAM, Instruction RAM and GP RAM parity */
3709 			WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3710 		RD_REG_WORD(&reg->hccr);
3711 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
3712 	}
3713 
3714 	if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
3715 		ha->flags.fac_supported = 1;
3716 	else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
3717 		uint32_t size;
3718 
3719 		rval = qla81xx_fac_get_sector_size(vha, &size);
3720 		if (rval == QLA_SUCCESS) {
3721 			ha->flags.fac_supported = 1;
3722 			ha->fdt_block_size = size << 2;
3723 		} else {
3724 			ql_log(ql_log_warn, vha, 0x00ce,
3725 			    "Unsupported FAC firmware (%d.%02d.%02d).\n",
3726 			    ha->fw_major_version, ha->fw_minor_version,
3727 			    ha->fw_subminor_version);
3728 
3729 			if (IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3730 			    IS_QLA28XX(ha)) {
3731 				ha->flags.fac_supported = 0;
3732 				rval = QLA_SUCCESS;
3733 			}
3734 		}
3735 	}
3736 failed:
3737 	if (rval) {
3738 		ql_log(ql_log_fatal, vha, 0x00cf,
3739 		    "Setup chip ****FAILED****.\n");
3740 	}
3741 
3742 	return (rval);
3743 }
3744 
3745 /**
3746  * qla2x00_init_response_q_entries() - Initializes response queue entries.
3747  * @rsp: response queue
3748  *
3749  * Beginning of request ring has initialization control block already built
3750  * by nvram config routine.
3751  *
3752  * Returns 0 on success.
3753  */
3754 void
3755 qla2x00_init_response_q_entries(struct rsp_que *rsp)
3756 {
3757 	uint16_t cnt;
3758 	response_t *pkt;
3759 
3760 	rsp->ring_ptr = rsp->ring;
3761 	rsp->ring_index    = 0;
3762 	rsp->status_srb = NULL;
3763 	pkt = rsp->ring_ptr;
3764 	for (cnt = 0; cnt < rsp->length; cnt++) {
3765 		pkt->signature = RESPONSE_PROCESSED;
3766 		pkt++;
3767 	}
3768 }
3769 
3770 /**
3771  * qla2x00_update_fw_options() - Read and process firmware options.
3772  * @vha: HA context
3773  *
3774  * Returns 0 on success.
3775  */
3776 void
3777 qla2x00_update_fw_options(scsi_qla_host_t *vha)
3778 {
3779 	uint16_t swing, emphasis, tx_sens, rx_sens;
3780 	struct qla_hw_data *ha = vha->hw;
3781 
3782 	memset(ha->fw_options, 0, sizeof(ha->fw_options));
3783 	qla2x00_get_fw_options(vha, ha->fw_options);
3784 
3785 	if (IS_QLA2100(ha) || IS_QLA2200(ha))
3786 		return;
3787 
3788 	/* Serial Link options. */
3789 	ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3790 	    "Serial link options.\n");
3791 	ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3792 	    ha->fw_seriallink_options, sizeof(ha->fw_seriallink_options));
3793 
3794 	ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3795 	if (ha->fw_seriallink_options[3] & BIT_2) {
3796 		ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3797 
3798 		/*  1G settings */
3799 		swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3800 		emphasis = (ha->fw_seriallink_options[2] &
3801 		    (BIT_4 | BIT_3)) >> 3;
3802 		tx_sens = ha->fw_seriallink_options[0] &
3803 		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3804 		rx_sens = (ha->fw_seriallink_options[0] &
3805 		    (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3806 		ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3807 		if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3808 			if (rx_sens == 0x0)
3809 				rx_sens = 0x3;
3810 			ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3811 		} else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3812 			ha->fw_options[10] |= BIT_5 |
3813 			    ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3814 			    (tx_sens & (BIT_1 | BIT_0));
3815 
3816 		/*  2G settings */
3817 		swing = (ha->fw_seriallink_options[2] &
3818 		    (BIT_7 | BIT_6 | BIT_5)) >> 5;
3819 		emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3820 		tx_sens = ha->fw_seriallink_options[1] &
3821 		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3822 		rx_sens = (ha->fw_seriallink_options[1] &
3823 		    (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3824 		ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3825 		if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3826 			if (rx_sens == 0x0)
3827 				rx_sens = 0x3;
3828 			ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3829 		} else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3830 			ha->fw_options[11] |= BIT_5 |
3831 			    ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3832 			    (tx_sens & (BIT_1 | BIT_0));
3833 	}
3834 
3835 	/* FCP2 options. */
3836 	/*  Return command IOCBs without waiting for an ABTS to complete. */
3837 	ha->fw_options[3] |= BIT_13;
3838 
3839 	/* LED scheme. */
3840 	if (ha->flags.enable_led_scheme)
3841 		ha->fw_options[2] |= BIT_12;
3842 
3843 	/* Detect ISP6312. */
3844 	if (IS_QLA6312(ha))
3845 		ha->fw_options[2] |= BIT_13;
3846 
3847 	/* Set Retry FLOGI in case of P2P connection */
3848 	if (ha->operating_mode == P2P) {
3849 		ha->fw_options[2] |= BIT_3;
3850 		ql_dbg(ql_dbg_disc, vha, 0x2100,
3851 		    "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3852 			__func__, ha->fw_options[2]);
3853 	}
3854 
3855 	/* Update firmware options. */
3856 	qla2x00_set_fw_options(vha, ha->fw_options);
3857 }
3858 
3859 void
3860 qla24xx_update_fw_options(scsi_qla_host_t *vha)
3861 {
3862 	int rval;
3863 	struct qla_hw_data *ha = vha->hw;
3864 
3865 	if (IS_P3P_TYPE(ha))
3866 		return;
3867 
3868 	/*  Hold status IOCBs until ABTS response received. */
3869 	if (ql2xfwholdabts)
3870 		ha->fw_options[3] |= BIT_12;
3871 
3872 	/* Set Retry FLOGI in case of P2P connection */
3873 	if (ha->operating_mode == P2P) {
3874 		ha->fw_options[2] |= BIT_3;
3875 		ql_dbg(ql_dbg_disc, vha, 0x2101,
3876 		    "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3877 			__func__, ha->fw_options[2]);
3878 	}
3879 
3880 	/* Move PUREX, ABTS RX & RIDA to ATIOQ */
3881 	if (ql2xmvasynctoatio &&
3882 	    (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))) {
3883 		if (qla_tgt_mode_enabled(vha) ||
3884 		    qla_dual_mode_enabled(vha))
3885 			ha->fw_options[2] |= BIT_11;
3886 		else
3887 			ha->fw_options[2] &= ~BIT_11;
3888 	}
3889 
3890 	if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3891 	    IS_QLA28XX(ha)) {
3892 		/*
3893 		 * Tell FW to track each exchange to prevent
3894 		 * driver from using stale exchange.
3895 		 */
3896 		if (qla_tgt_mode_enabled(vha) ||
3897 		    qla_dual_mode_enabled(vha))
3898 			ha->fw_options[2] |= BIT_4;
3899 		else
3900 			ha->fw_options[2] &= ~BIT_4;
3901 
3902 		/* Reserve 1/2 of emergency exchanges for ELS.*/
3903 		if (qla2xuseresexchforels)
3904 			ha->fw_options[2] |= BIT_8;
3905 		else
3906 			ha->fw_options[2] &= ~BIT_8;
3907 	}
3908 
3909 	ql_dbg(ql_dbg_init, vha, 0x00e8,
3910 	    "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3911 	    __func__, ha->fw_options[1], ha->fw_options[2],
3912 	    ha->fw_options[3], vha->host->active_mode);
3913 
3914 	if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3915 		qla2x00_set_fw_options(vha, ha->fw_options);
3916 
3917 	/* Update Serial Link options. */
3918 	if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
3919 		return;
3920 
3921 	rval = qla2x00_set_serdes_params(vha,
3922 	    le16_to_cpu(ha->fw_seriallink_options24[1]),
3923 	    le16_to_cpu(ha->fw_seriallink_options24[2]),
3924 	    le16_to_cpu(ha->fw_seriallink_options24[3]));
3925 	if (rval != QLA_SUCCESS) {
3926 		ql_log(ql_log_warn, vha, 0x0104,
3927 		    "Unable to update Serial Link options (%x).\n", rval);
3928 	}
3929 }
3930 
3931 void
3932 qla2x00_config_rings(struct scsi_qla_host *vha)
3933 {
3934 	struct qla_hw_data *ha = vha->hw;
3935 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3936 	struct req_que *req = ha->req_q_map[0];
3937 	struct rsp_que *rsp = ha->rsp_q_map[0];
3938 
3939 	/* Setup ring parameters in initialization control block. */
3940 	ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3941 	ha->init_cb->response_q_inpointer = cpu_to_le16(0);
3942 	ha->init_cb->request_q_length = cpu_to_le16(req->length);
3943 	ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3944 	put_unaligned_le64(req->dma, &ha->init_cb->request_q_address);
3945 	put_unaligned_le64(rsp->dma, &ha->init_cb->response_q_address);
3946 
3947 	WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3948 	WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3949 	WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3950 	WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3951 	RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg));		/* PCI Posting. */
3952 }
3953 
3954 void
3955 qla24xx_config_rings(struct scsi_qla_host *vha)
3956 {
3957 	struct qla_hw_data *ha = vha->hw;
3958 	device_reg_t *reg = ISP_QUE_REG(ha, 0);
3959 	struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3960 	struct qla_msix_entry *msix;
3961 	struct init_cb_24xx *icb;
3962 	uint16_t rid = 0;
3963 	struct req_que *req = ha->req_q_map[0];
3964 	struct rsp_que *rsp = ha->rsp_q_map[0];
3965 
3966 	/* Setup ring parameters in initialization control block. */
3967 	icb = (struct init_cb_24xx *)ha->init_cb;
3968 	icb->request_q_outpointer = cpu_to_le16(0);
3969 	icb->response_q_inpointer = cpu_to_le16(0);
3970 	icb->request_q_length = cpu_to_le16(req->length);
3971 	icb->response_q_length = cpu_to_le16(rsp->length);
3972 	put_unaligned_le64(req->dma, &icb->request_q_address);
3973 	put_unaligned_le64(rsp->dma, &icb->response_q_address);
3974 
3975 	/* Setup ATIO queue dma pointers for target mode */
3976 	icb->atio_q_inpointer = cpu_to_le16(0);
3977 	icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3978 	put_unaligned_le64(ha->tgt.atio_dma, &icb->atio_q_address);
3979 
3980 	if (IS_SHADOW_REG_CAPABLE(ha))
3981 		icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
3982 
3983 	if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3984 	    IS_QLA28XX(ha)) {
3985 		icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3986 		icb->rid = cpu_to_le16(rid);
3987 		if (ha->flags.msix_enabled) {
3988 			msix = &ha->msix_entries[1];
3989 			ql_dbg(ql_dbg_init, vha, 0x0019,
3990 			    "Registering vector 0x%x for base que.\n",
3991 			    msix->entry);
3992 			icb->msix = cpu_to_le16(msix->entry);
3993 		}
3994 		/* Use alternate PCI bus number */
3995 		if (MSB(rid))
3996 			icb->firmware_options_2 |= cpu_to_le32(BIT_19);
3997 		/* Use alternate PCI devfn */
3998 		if (LSB(rid))
3999 			icb->firmware_options_2 |= cpu_to_le32(BIT_18);
4000 
4001 		/* Use Disable MSIX Handshake mode for capable adapters */
4002 		if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
4003 		    (ha->flags.msix_enabled)) {
4004 			icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
4005 			ha->flags.disable_msix_handshake = 1;
4006 			ql_dbg(ql_dbg_init, vha, 0x00fe,
4007 			    "MSIX Handshake Disable Mode turned on.\n");
4008 		} else {
4009 			icb->firmware_options_2 |= cpu_to_le32(BIT_22);
4010 		}
4011 		icb->firmware_options_2 |= cpu_to_le32(BIT_23);
4012 
4013 		WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
4014 		WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
4015 		WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
4016 		WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
4017 	} else {
4018 		WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
4019 		WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
4020 		WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
4021 		WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
4022 	}
4023 
4024 	qlt_24xx_config_rings(vha);
4025 
4026 	/* If the user has configured the speed, set it here */
4027 	if (ha->set_data_rate) {
4028 		ql_dbg(ql_dbg_init, vha, 0x00fd,
4029 		    "Speed set by user : %s Gbps \n",
4030 		    qla2x00_get_link_speed_str(ha, ha->set_data_rate));
4031 		icb->firmware_options_3 = (ha->set_data_rate << 13);
4032 	}
4033 
4034 	/* PCI posting */
4035 	RD_REG_DWORD(&ioreg->hccr);
4036 }
4037 
4038 /**
4039  * qla2x00_init_rings() - Initializes firmware.
4040  * @vha: HA context
4041  *
4042  * Beginning of request ring has initialization control block already built
4043  * by nvram config routine.
4044  *
4045  * Returns 0 on success.
4046  */
4047 int
4048 qla2x00_init_rings(scsi_qla_host_t *vha)
4049 {
4050 	int	rval;
4051 	unsigned long flags = 0;
4052 	int cnt, que;
4053 	struct qla_hw_data *ha = vha->hw;
4054 	struct req_que *req;
4055 	struct rsp_que *rsp;
4056 	struct mid_init_cb_24xx *mid_init_cb =
4057 	    (struct mid_init_cb_24xx *) ha->init_cb;
4058 
4059 	spin_lock_irqsave(&ha->hardware_lock, flags);
4060 
4061 	/* Clear outstanding commands array. */
4062 	for (que = 0; que < ha->max_req_queues; que++) {
4063 		req = ha->req_q_map[que];
4064 		if (!req || !test_bit(que, ha->req_qid_map))
4065 			continue;
4066 		req->out_ptr = (void *)(req->ring + req->length);
4067 		*req->out_ptr = 0;
4068 		for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
4069 			req->outstanding_cmds[cnt] = NULL;
4070 
4071 		req->current_outstanding_cmd = 1;
4072 
4073 		/* Initialize firmware. */
4074 		req->ring_ptr  = req->ring;
4075 		req->ring_index    = 0;
4076 		req->cnt      = req->length;
4077 	}
4078 
4079 	for (que = 0; que < ha->max_rsp_queues; que++) {
4080 		rsp = ha->rsp_q_map[que];
4081 		if (!rsp || !test_bit(que, ha->rsp_qid_map))
4082 			continue;
4083 		rsp->in_ptr = (void *)(rsp->ring + rsp->length);
4084 		*rsp->in_ptr = 0;
4085 		/* Initialize response queue entries */
4086 		if (IS_QLAFX00(ha))
4087 			qlafx00_init_response_q_entries(rsp);
4088 		else
4089 			qla2x00_init_response_q_entries(rsp);
4090 	}
4091 
4092 	ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
4093 	ha->tgt.atio_ring_index = 0;
4094 	/* Initialize ATIO queue entries */
4095 	qlt_init_atio_q_entries(vha);
4096 
4097 	ha->isp_ops->config_rings(vha);
4098 
4099 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4100 
4101 	ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
4102 
4103 	if (IS_QLAFX00(ha)) {
4104 		rval = qlafx00_init_firmware(vha, ha->init_cb_size);
4105 		goto next_check;
4106 	}
4107 
4108 	/* Update any ISP specific firmware options before initialization. */
4109 	ha->isp_ops->update_fw_options(vha);
4110 
4111 	if (ha->flags.npiv_supported) {
4112 		if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
4113 			ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
4114 		mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
4115 	}
4116 
4117 	if (IS_FWI2_CAPABLE(ha)) {
4118 		mid_init_cb->options = cpu_to_le16(BIT_1);
4119 		mid_init_cb->init_cb.execution_throttle =
4120 		    cpu_to_le16(ha->cur_fw_xcb_count);
4121 		ha->flags.dport_enabled =
4122 		    (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
4123 		ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
4124 		    (ha->flags.dport_enabled) ? "enabled" : "disabled");
4125 		/* FA-WWPN Status */
4126 		ha->flags.fawwpn_enabled =
4127 		    (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
4128 		ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
4129 		    (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
4130 	}
4131 
4132 	rval = qla2x00_init_firmware(vha, ha->init_cb_size);
4133 next_check:
4134 	if (rval) {
4135 		ql_log(ql_log_fatal, vha, 0x00d2,
4136 		    "Init Firmware **** FAILED ****.\n");
4137 	} else {
4138 		ql_dbg(ql_dbg_init, vha, 0x00d3,
4139 		    "Init Firmware -- success.\n");
4140 		QLA_FW_STARTED(ha);
4141 		vha->u_ql2xexchoffld = vha->u_ql2xiniexchg = 0;
4142 	}
4143 
4144 	return (rval);
4145 }
4146 
4147 /**
4148  * qla2x00_fw_ready() - Waits for firmware ready.
4149  * @vha: HA context
4150  *
4151  * Returns 0 on success.
4152  */
4153 static int
4154 qla2x00_fw_ready(scsi_qla_host_t *vha)
4155 {
4156 	int		rval;
4157 	unsigned long	wtime, mtime, cs84xx_time;
4158 	uint16_t	min_wait;	/* Minimum wait time if loop is down */
4159 	uint16_t	wait_time;	/* Wait time if loop is coming ready */
4160 	uint16_t	state[6];
4161 	struct qla_hw_data *ha = vha->hw;
4162 
4163 	if (IS_QLAFX00(vha->hw))
4164 		return qlafx00_fw_ready(vha);
4165 
4166 	rval = QLA_SUCCESS;
4167 
4168 	/* Time to wait for loop down */
4169 	if (IS_P3P_TYPE(ha))
4170 		min_wait = 30;
4171 	else
4172 		min_wait = 20;
4173 
4174 	/*
4175 	 * Firmware should take at most one RATOV to login, plus 5 seconds for
4176 	 * our own processing.
4177 	 */
4178 	if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
4179 		wait_time = min_wait;
4180 	}
4181 
4182 	/* Min wait time if loop down */
4183 	mtime = jiffies + (min_wait * HZ);
4184 
4185 	/* wait time before firmware ready */
4186 	wtime = jiffies + (wait_time * HZ);
4187 
4188 	/* Wait for ISP to finish LIP */
4189 	if (!vha->flags.init_done)
4190 		ql_log(ql_log_info, vha, 0x801e,
4191 		    "Waiting for LIP to complete.\n");
4192 
4193 	do {
4194 		memset(state, -1, sizeof(state));
4195 		rval = qla2x00_get_firmware_state(vha, state);
4196 		if (rval == QLA_SUCCESS) {
4197 			if (state[0] < FSTATE_LOSS_OF_SYNC) {
4198 				vha->device_flags &= ~DFLG_NO_CABLE;
4199 			}
4200 			if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
4201 				ql_dbg(ql_dbg_taskm, vha, 0x801f,
4202 				    "fw_state=%x 84xx=%x.\n", state[0],
4203 				    state[2]);
4204 				if ((state[2] & FSTATE_LOGGED_IN) &&
4205 				     (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
4206 					ql_dbg(ql_dbg_taskm, vha, 0x8028,
4207 					    "Sending verify iocb.\n");
4208 
4209 					cs84xx_time = jiffies;
4210 					rval = qla84xx_init_chip(vha);
4211 					if (rval != QLA_SUCCESS) {
4212 						ql_log(ql_log_warn,
4213 						    vha, 0x8007,
4214 						    "Init chip failed.\n");
4215 						break;
4216 					}
4217 
4218 					/* Add time taken to initialize. */
4219 					cs84xx_time = jiffies - cs84xx_time;
4220 					wtime += cs84xx_time;
4221 					mtime += cs84xx_time;
4222 					ql_dbg(ql_dbg_taskm, vha, 0x8008,
4223 					    "Increasing wait time by %ld. "
4224 					    "New time %ld.\n", cs84xx_time,
4225 					    wtime);
4226 				}
4227 			} else if (state[0] == FSTATE_READY) {
4228 				ql_dbg(ql_dbg_taskm, vha, 0x8037,
4229 				    "F/W Ready - OK.\n");
4230 
4231 				qla2x00_get_retry_cnt(vha, &ha->retry_count,
4232 				    &ha->login_timeout, &ha->r_a_tov);
4233 
4234 				rval = QLA_SUCCESS;
4235 				break;
4236 			}
4237 
4238 			rval = QLA_FUNCTION_FAILED;
4239 
4240 			if (atomic_read(&vha->loop_down_timer) &&
4241 			    state[0] != FSTATE_READY) {
4242 				/* Loop down. Timeout on min_wait for states
4243 				 * other than Wait for Login.
4244 				 */
4245 				if (time_after_eq(jiffies, mtime)) {
4246 					ql_log(ql_log_info, vha, 0x8038,
4247 					    "Cable is unplugged...\n");
4248 
4249 					vha->device_flags |= DFLG_NO_CABLE;
4250 					break;
4251 				}
4252 			}
4253 		} else {
4254 			/* Mailbox cmd failed. Timeout on min_wait. */
4255 			if (time_after_eq(jiffies, mtime) ||
4256 				ha->flags.isp82xx_fw_hung)
4257 				break;
4258 		}
4259 
4260 		if (time_after_eq(jiffies, wtime))
4261 			break;
4262 
4263 		/* Delay for a while */
4264 		msleep(500);
4265 	} while (1);
4266 
4267 	ql_dbg(ql_dbg_taskm, vha, 0x803a,
4268 	    "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
4269 	    state[1], state[2], state[3], state[4], state[5], jiffies);
4270 
4271 	if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
4272 		ql_log(ql_log_warn, vha, 0x803b,
4273 		    "Firmware ready **** FAILED ****.\n");
4274 	}
4275 
4276 	return (rval);
4277 }
4278 
4279 /*
4280 *  qla2x00_configure_hba
4281 *      Setup adapter context.
4282 *
4283 * Input:
4284 *      ha = adapter state pointer.
4285 *
4286 * Returns:
4287 *      0 = success
4288 *
4289 * Context:
4290 *      Kernel context.
4291 */
4292 static int
4293 qla2x00_configure_hba(scsi_qla_host_t *vha)
4294 {
4295 	int       rval;
4296 	uint16_t      loop_id;
4297 	uint16_t      topo;
4298 	uint16_t      sw_cap;
4299 	uint8_t       al_pa;
4300 	uint8_t       area;
4301 	uint8_t       domain;
4302 	char		connect_type[22];
4303 	struct qla_hw_data *ha = vha->hw;
4304 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
4305 	port_id_t id;
4306 	unsigned long flags;
4307 
4308 	/* Get host addresses. */
4309 	rval = qla2x00_get_adapter_id(vha,
4310 	    &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
4311 	if (rval != QLA_SUCCESS) {
4312 		if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
4313 		    IS_CNA_CAPABLE(ha) ||
4314 		    (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
4315 			ql_dbg(ql_dbg_disc, vha, 0x2008,
4316 			    "Loop is in a transition state.\n");
4317 		} else {
4318 			ql_log(ql_log_warn, vha, 0x2009,
4319 			    "Unable to get host loop ID.\n");
4320 			if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
4321 			    (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
4322 				ql_log(ql_log_warn, vha, 0x1151,
4323 				    "Doing link init.\n");
4324 				if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
4325 					return rval;
4326 			}
4327 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4328 		}
4329 		return (rval);
4330 	}
4331 
4332 	if (topo == 4) {
4333 		ql_log(ql_log_info, vha, 0x200a,
4334 		    "Cannot get topology - retrying.\n");
4335 		return (QLA_FUNCTION_FAILED);
4336 	}
4337 
4338 	vha->loop_id = loop_id;
4339 
4340 	/* initialize */
4341 	ha->min_external_loopid = SNS_FIRST_LOOP_ID;
4342 	ha->operating_mode = LOOP;
4343 	ha->switch_cap = 0;
4344 
4345 	switch (topo) {
4346 	case 0:
4347 		ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
4348 		ha->current_topology = ISP_CFG_NL;
4349 		strcpy(connect_type, "(Loop)");
4350 		break;
4351 
4352 	case 1:
4353 		ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
4354 		ha->switch_cap = sw_cap;
4355 		ha->current_topology = ISP_CFG_FL;
4356 		strcpy(connect_type, "(FL_Port)");
4357 		break;
4358 
4359 	case 2:
4360 		ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
4361 		ha->operating_mode = P2P;
4362 		ha->current_topology = ISP_CFG_N;
4363 		strcpy(connect_type, "(N_Port-to-N_Port)");
4364 		break;
4365 
4366 	case 3:
4367 		ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
4368 		ha->switch_cap = sw_cap;
4369 		ha->operating_mode = P2P;
4370 		ha->current_topology = ISP_CFG_F;
4371 		strcpy(connect_type, "(F_Port)");
4372 		break;
4373 
4374 	default:
4375 		ql_dbg(ql_dbg_disc, vha, 0x200f,
4376 		    "HBA in unknown topology %x, using NL.\n", topo);
4377 		ha->current_topology = ISP_CFG_NL;
4378 		strcpy(connect_type, "(Loop)");
4379 		break;
4380 	}
4381 
4382 	/* Save Host port and loop ID. */
4383 	/* byte order - Big Endian */
4384 	id.b.domain = domain;
4385 	id.b.area = area;
4386 	id.b.al_pa = al_pa;
4387 	id.b.rsvd_1 = 0;
4388 	spin_lock_irqsave(&ha->hardware_lock, flags);
4389 	if (!(topo == 2 && ha->flags.n2n_bigger))
4390 		qlt_update_host_map(vha, id);
4391 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4392 
4393 	if (!vha->flags.init_done)
4394 		ql_log(ql_log_info, vha, 0x2010,
4395 		    "Topology - %s, Host Loop address 0x%x.\n",
4396 		    connect_type, vha->loop_id);
4397 
4398 	return(rval);
4399 }
4400 
4401 inline void
4402 qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4403 	char *def)
4404 {
4405 	char *st, *en;
4406 	uint16_t index;
4407 	uint64_t zero[2] = { 0 };
4408 	struct qla_hw_data *ha = vha->hw;
4409 	int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
4410 	    !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
4411 
4412 	if (len > sizeof(zero))
4413 		len = sizeof(zero);
4414 	if (memcmp(model, &zero, len) != 0) {
4415 		strncpy(ha->model_number, model, len);
4416 		st = en = ha->model_number;
4417 		en += len - 1;
4418 		while (en > st) {
4419 			if (*en != 0x20 && *en != 0x00)
4420 				break;
4421 			*en-- = '\0';
4422 		}
4423 
4424 		index = (ha->pdev->subsystem_device & 0xff);
4425 		if (use_tbl &&
4426 		    ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
4427 		    index < QLA_MODEL_NAMES)
4428 			strncpy(ha->model_desc,
4429 			    qla2x00_model_name[index * 2 + 1],
4430 			    sizeof(ha->model_desc) - 1);
4431 	} else {
4432 		index = (ha->pdev->subsystem_device & 0xff);
4433 		if (use_tbl &&
4434 		    ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
4435 		    index < QLA_MODEL_NAMES) {
4436 			strcpy(ha->model_number,
4437 			    qla2x00_model_name[index * 2]);
4438 			strncpy(ha->model_desc,
4439 			    qla2x00_model_name[index * 2 + 1],
4440 			    sizeof(ha->model_desc) - 1);
4441 		} else {
4442 			strcpy(ha->model_number, def);
4443 		}
4444 	}
4445 	if (IS_FWI2_CAPABLE(ha))
4446 		qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
4447 		    sizeof(ha->model_desc));
4448 }
4449 
4450 /* On sparc systems, obtain port and node WWN from firmware
4451  * properties.
4452  */
4453 static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
4454 {
4455 #ifdef CONFIG_SPARC
4456 	struct qla_hw_data *ha = vha->hw;
4457 	struct pci_dev *pdev = ha->pdev;
4458 	struct device_node *dp = pci_device_to_OF_node(pdev);
4459 	const u8 *val;
4460 	int len;
4461 
4462 	val = of_get_property(dp, "port-wwn", &len);
4463 	if (val && len >= WWN_SIZE)
4464 		memcpy(nv->port_name, val, WWN_SIZE);
4465 
4466 	val = of_get_property(dp, "node-wwn", &len);
4467 	if (val && len >= WWN_SIZE)
4468 		memcpy(nv->node_name, val, WWN_SIZE);
4469 #endif
4470 }
4471 
4472 /*
4473 * NVRAM configuration for ISP 2xxx
4474 *
4475 * Input:
4476 *      ha                = adapter block pointer.
4477 *
4478 * Output:
4479 *      initialization control block in response_ring
4480 *      host adapters parameters in host adapter block
4481 *
4482 * Returns:
4483 *      0 = success.
4484 */
4485 int
4486 qla2x00_nvram_config(scsi_qla_host_t *vha)
4487 {
4488 	int             rval;
4489 	uint8_t         chksum = 0;
4490 	uint16_t        cnt;
4491 	uint8_t         *dptr1, *dptr2;
4492 	struct qla_hw_data *ha = vha->hw;
4493 	init_cb_t       *icb = ha->init_cb;
4494 	nvram_t         *nv = ha->nvram;
4495 	uint8_t         *ptr = ha->nvram;
4496 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
4497 
4498 	rval = QLA_SUCCESS;
4499 
4500 	/* Determine NVRAM starting address. */
4501 	ha->nvram_size = sizeof(*nv);
4502 	ha->nvram_base = 0;
4503 	if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4504 		if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
4505 			ha->nvram_base = 0x80;
4506 
4507 	/* Get NVRAM data and calculate checksum. */
4508 	ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
4509 	for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4510 		chksum += *ptr++;
4511 
4512 	ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4513 	    "Contents of NVRAM.\n");
4514 	ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4515 	    nv, ha->nvram_size);
4516 
4517 	/* Bad NVRAM data, set defaults parameters. */
4518 	if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
4519 	    nv->nvram_version < 1) {
4520 		/* Reset NVRAM data. */
4521 		ql_log(ql_log_warn, vha, 0x0064,
4522 		    "Inconsistent NVRAM detected: checksum=%#x id=%.4s version=%#x.\n",
4523 		    chksum, nv->id, nv->nvram_version);
4524 		ql_log(ql_log_warn, vha, 0x0065,
4525 		    "Falling back to "
4526 		    "functioning (yet invalid -- WWPN) defaults.\n");
4527 
4528 		/*
4529 		 * Set default initialization control block.
4530 		 */
4531 		memset(nv, 0, ha->nvram_size);
4532 		nv->parameter_block_version = ICB_VERSION;
4533 
4534 		if (IS_QLA23XX(ha)) {
4535 			nv->firmware_options[0] = BIT_2 | BIT_1;
4536 			nv->firmware_options[1] = BIT_7 | BIT_5;
4537 			nv->add_firmware_options[0] = BIT_5;
4538 			nv->add_firmware_options[1] = BIT_5 | BIT_4;
4539 			nv->frame_payload_size = 2048;
4540 			nv->special_options[1] = BIT_7;
4541 		} else if (IS_QLA2200(ha)) {
4542 			nv->firmware_options[0] = BIT_2 | BIT_1;
4543 			nv->firmware_options[1] = BIT_7 | BIT_5;
4544 			nv->add_firmware_options[0] = BIT_5;
4545 			nv->add_firmware_options[1] = BIT_5 | BIT_4;
4546 			nv->frame_payload_size = 1024;
4547 		} else if (IS_QLA2100(ha)) {
4548 			nv->firmware_options[0] = BIT_3 | BIT_1;
4549 			nv->firmware_options[1] = BIT_5;
4550 			nv->frame_payload_size = 1024;
4551 		}
4552 
4553 		nv->max_iocb_allocation = cpu_to_le16(256);
4554 		nv->execution_throttle = cpu_to_le16(16);
4555 		nv->retry_count = 8;
4556 		nv->retry_delay = 1;
4557 
4558 		nv->port_name[0] = 33;
4559 		nv->port_name[3] = 224;
4560 		nv->port_name[4] = 139;
4561 
4562 		qla2xxx_nvram_wwn_from_ofw(vha, nv);
4563 
4564 		nv->login_timeout = 4;
4565 
4566 		/*
4567 		 * Set default host adapter parameters
4568 		 */
4569 		nv->host_p[1] = BIT_2;
4570 		nv->reset_delay = 5;
4571 		nv->port_down_retry_count = 8;
4572 		nv->max_luns_per_target = cpu_to_le16(8);
4573 		nv->link_down_timeout = 60;
4574 
4575 		rval = 1;
4576 	}
4577 
4578 	/* Reset Initialization control block */
4579 	memset(icb, 0, ha->init_cb_size);
4580 
4581 	/*
4582 	 * Setup driver NVRAM options.
4583 	 */
4584 	nv->firmware_options[0] |= (BIT_6 | BIT_1);
4585 	nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4586 	nv->firmware_options[1] |= (BIT_5 | BIT_0);
4587 	nv->firmware_options[1] &= ~BIT_4;
4588 
4589 	if (IS_QLA23XX(ha)) {
4590 		nv->firmware_options[0] |= BIT_2;
4591 		nv->firmware_options[0] &= ~BIT_3;
4592 		nv->special_options[0] &= ~BIT_6;
4593 		nv->add_firmware_options[1] |= BIT_5 | BIT_4;
4594 
4595 		if (IS_QLA2300(ha)) {
4596 			if (ha->fb_rev == FPM_2310) {
4597 				strcpy(ha->model_number, "QLA2310");
4598 			} else {
4599 				strcpy(ha->model_number, "QLA2300");
4600 			}
4601 		} else {
4602 			qla2x00_set_model_info(vha, nv->model_number,
4603 			    sizeof(nv->model_number), "QLA23xx");
4604 		}
4605 	} else if (IS_QLA2200(ha)) {
4606 		nv->firmware_options[0] |= BIT_2;
4607 		/*
4608 		 * 'Point-to-point preferred, else loop' is not a safe
4609 		 * connection mode setting.
4610 		 */
4611 		if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4612 		    (BIT_5 | BIT_4)) {
4613 			/* Force 'loop preferred, else point-to-point'. */
4614 			nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4615 			nv->add_firmware_options[0] |= BIT_5;
4616 		}
4617 		strcpy(ha->model_number, "QLA22xx");
4618 	} else /*if (IS_QLA2100(ha))*/ {
4619 		strcpy(ha->model_number, "QLA2100");
4620 	}
4621 
4622 	/*
4623 	 * Copy over NVRAM RISC parameter block to initialization control block.
4624 	 */
4625 	dptr1 = (uint8_t *)icb;
4626 	dptr2 = (uint8_t *)&nv->parameter_block_version;
4627 	cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4628 	while (cnt--)
4629 		*dptr1++ = *dptr2++;
4630 
4631 	/* Copy 2nd half. */
4632 	dptr1 = (uint8_t *)icb->add_firmware_options;
4633 	cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4634 	while (cnt--)
4635 		*dptr1++ = *dptr2++;
4636 	ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
4637 	/* Use alternate WWN? */
4638 	if (nv->host_p[1] & BIT_7) {
4639 		memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4640 		memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4641 	}
4642 
4643 	/* Prepare nodename */
4644 	if ((icb->firmware_options[1] & BIT_6) == 0) {
4645 		/*
4646 		 * Firmware will apply the following mask if the nodename was
4647 		 * not provided.
4648 		 */
4649 		memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4650 		icb->node_name[0] &= 0xF0;
4651 	}
4652 
4653 	/*
4654 	 * Set host adapter parameters.
4655 	 */
4656 
4657 	/*
4658 	 * BIT_7 in the host-parameters section allows for modification to
4659 	 * internal driver logging.
4660 	 */
4661 	if (nv->host_p[0] & BIT_7)
4662 		ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
4663 	ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4664 	/* Always load RISC code on non ISP2[12]00 chips. */
4665 	if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4666 		ha->flags.disable_risc_code_load = 0;
4667 	ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4668 	ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4669 	ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
4670 	ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
4671 	ha->flags.disable_serdes = 0;
4672 
4673 	ha->operating_mode =
4674 	    (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4675 
4676 	memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4677 	    sizeof(ha->fw_seriallink_options));
4678 
4679 	/* save HBA serial number */
4680 	ha->serial0 = icb->port_name[5];
4681 	ha->serial1 = icb->port_name[6];
4682 	ha->serial2 = icb->port_name[7];
4683 	memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4684 	memcpy(vha->port_name, icb->port_name, WWN_SIZE);
4685 
4686 	icb->execution_throttle = cpu_to_le16(0xFFFF);
4687 
4688 	ha->retry_count = nv->retry_count;
4689 
4690 	/* Set minimum login_timeout to 4 seconds. */
4691 	if (nv->login_timeout != ql2xlogintimeout)
4692 		nv->login_timeout = ql2xlogintimeout;
4693 	if (nv->login_timeout < 4)
4694 		nv->login_timeout = 4;
4695 	ha->login_timeout = nv->login_timeout;
4696 
4697 	/* Set minimum RATOV to 100 tenths of a second. */
4698 	ha->r_a_tov = 100;
4699 
4700 	ha->loop_reset_delay = nv->reset_delay;
4701 
4702 	/* Link Down Timeout = 0:
4703 	 *
4704 	 * 	When Port Down timer expires we will start returning
4705 	 *	I/O's to OS with "DID_NO_CONNECT".
4706 	 *
4707 	 * Link Down Timeout != 0:
4708 	 *
4709 	 *	 The driver waits for the link to come up after link down
4710 	 *	 before returning I/Os to OS with "DID_NO_CONNECT".
4711 	 */
4712 	if (nv->link_down_timeout == 0) {
4713 		ha->loop_down_abort_time =
4714 		    (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
4715 	} else {
4716 		ha->link_down_timeout =	 nv->link_down_timeout;
4717 		ha->loop_down_abort_time =
4718 		    (LOOP_DOWN_TIME - ha->link_down_timeout);
4719 	}
4720 
4721 	/*
4722 	 * Need enough time to try and get the port back.
4723 	 */
4724 	ha->port_down_retry_count = nv->port_down_retry_count;
4725 	if (qlport_down_retry)
4726 		ha->port_down_retry_count = qlport_down_retry;
4727 	/* Set login_retry_count */
4728 	ha->login_retry_count  = nv->retry_count;
4729 	if (ha->port_down_retry_count == nv->port_down_retry_count &&
4730 	    ha->port_down_retry_count > 3)
4731 		ha->login_retry_count = ha->port_down_retry_count;
4732 	else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4733 		ha->login_retry_count = ha->port_down_retry_count;
4734 	if (ql2xloginretrycount)
4735 		ha->login_retry_count = ql2xloginretrycount;
4736 
4737 	icb->lun_enables = cpu_to_le16(0);
4738 	icb->command_resource_count = 0;
4739 	icb->immediate_notify_resource_count = 0;
4740 	icb->timeout = cpu_to_le16(0);
4741 
4742 	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4743 		/* Enable RIO */
4744 		icb->firmware_options[0] &= ~BIT_3;
4745 		icb->add_firmware_options[0] &=
4746 		    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4747 		icb->add_firmware_options[0] |= BIT_2;
4748 		icb->response_accumulation_timer = 3;
4749 		icb->interrupt_delay_timer = 5;
4750 
4751 		vha->flags.process_response_queue = 1;
4752 	} else {
4753 		/* Enable ZIO. */
4754 		if (!vha->flags.init_done) {
4755 			ha->zio_mode = icb->add_firmware_options[0] &
4756 			    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4757 			ha->zio_timer = icb->interrupt_delay_timer ?
4758 			    icb->interrupt_delay_timer : 2;
4759 		}
4760 		icb->add_firmware_options[0] &=
4761 		    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4762 		vha->flags.process_response_queue = 0;
4763 		if (ha->zio_mode != QLA_ZIO_DISABLED) {
4764 			ha->zio_mode = QLA_ZIO_MODE_6;
4765 
4766 			ql_log(ql_log_info, vha, 0x0068,
4767 			    "ZIO mode %d enabled; timer delay (%d us).\n",
4768 			    ha->zio_mode, ha->zio_timer * 100);
4769 
4770 			icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4771 			icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
4772 			vha->flags.process_response_queue = 1;
4773 		}
4774 	}
4775 
4776 	if (rval) {
4777 		ql_log(ql_log_warn, vha, 0x0069,
4778 		    "NVRAM configuration failed.\n");
4779 	}
4780 	return (rval);
4781 }
4782 
4783 static void
4784 qla2x00_rport_del(void *data)
4785 {
4786 	fc_port_t *fcport = data;
4787 	struct fc_rport *rport;
4788 	unsigned long flags;
4789 
4790 	spin_lock_irqsave(fcport->vha->host->host_lock, flags);
4791 	rport = fcport->drport ? fcport->drport : fcport->rport;
4792 	fcport->drport = NULL;
4793 	spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
4794 	if (rport) {
4795 		ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4796 		    "%s %8phN. rport %p roles %x\n",
4797 		    __func__, fcport->port_name, rport,
4798 		    rport->roles);
4799 
4800 		fc_remote_port_delete(rport);
4801 	}
4802 }
4803 
4804 void qla2x00_set_fcport_state(fc_port_t *fcport, int state)
4805 {
4806 	int old_state;
4807 
4808 	old_state = atomic_read(&fcport->state);
4809 	atomic_set(&fcport->state, state);
4810 
4811 	/* Don't print state transitions during initial allocation of fcport */
4812 	if (old_state && old_state != state) {
4813 		ql_dbg(ql_dbg_disc, fcport->vha, 0x207d,
4814 		       "FCPort %8phC state transitioned from %s to %s - portid=%02x%02x%02x.\n",
4815 		       fcport->port_name, port_state_str[old_state],
4816 		       port_state_str[state], fcport->d_id.b.domain,
4817 		       fcport->d_id.b.area, fcport->d_id.b.al_pa);
4818 	}
4819 }
4820 
4821 /**
4822  * qla2x00_alloc_fcport() - Allocate a generic fcport.
4823  * @vha: HA context
4824  * @flags: allocation flags
4825  *
4826  * Returns a pointer to the allocated fcport, or NULL, if none available.
4827  */
4828 fc_port_t *
4829 qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
4830 {
4831 	fc_port_t *fcport;
4832 
4833 	fcport = kzalloc(sizeof(fc_port_t), flags);
4834 	if (!fcport)
4835 		return NULL;
4836 
4837 	fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4838 		sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
4839 		flags);
4840 	if (!fcport->ct_desc.ct_sns) {
4841 		ql_log(ql_log_warn, vha, 0xd049,
4842 		    "Failed to allocate ct_sns request.\n");
4843 		kfree(fcport);
4844 		return NULL;
4845 	}
4846 
4847 	/* Setup fcport template structure. */
4848 	fcport->vha = vha;
4849 	fcport->port_type = FCT_UNKNOWN;
4850 	fcport->loop_id = FC_NO_LOOP_ID;
4851 	qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
4852 	fcport->supported_classes = FC_COS_UNSPECIFIED;
4853 	fcport->fp_speed = PORT_SPEED_UNKNOWN;
4854 
4855 	fcport->disc_state = DSC_DELETED;
4856 	fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4857 	fcport->deleted = QLA_SESS_DELETED;
4858 	fcport->login_retry = vha->hw->login_retry_count;
4859 	fcport->chip_reset = vha->hw->base_qpair->chip_reset;
4860 	fcport->logout_on_delete = 1;
4861 
4862 	if (!fcport->ct_desc.ct_sns) {
4863 		ql_log(ql_log_warn, vha, 0xd049,
4864 		    "Failed to allocate ct_sns request.\n");
4865 		kfree(fcport);
4866 		return NULL;
4867 	}
4868 
4869 	INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4870 	INIT_WORK(&fcport->reg_work, qla_register_fcport_fn);
4871 	INIT_LIST_HEAD(&fcport->gnl_entry);
4872 	INIT_LIST_HEAD(&fcport->list);
4873 
4874 	return fcport;
4875 }
4876 
4877 void
4878 qla2x00_free_fcport(fc_port_t *fcport)
4879 {
4880 	if (fcport->ct_desc.ct_sns) {
4881 		dma_free_coherent(&fcport->vha->hw->pdev->dev,
4882 			sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4883 			fcport->ct_desc.ct_sns_dma);
4884 
4885 		fcport->ct_desc.ct_sns = NULL;
4886 	}
4887 	list_del(&fcport->list);
4888 	qla2x00_clear_loop_id(fcport);
4889 	kfree(fcport);
4890 }
4891 
4892 /*
4893  * qla2x00_configure_loop
4894  *      Updates Fibre Channel Device Database with what is actually on loop.
4895  *
4896  * Input:
4897  *      ha                = adapter block pointer.
4898  *
4899  * Returns:
4900  *      0 = success.
4901  *      1 = error.
4902  *      2 = database was full and device was not configured.
4903  */
4904 static int
4905 qla2x00_configure_loop(scsi_qla_host_t *vha)
4906 {
4907 	int  rval;
4908 	unsigned long flags, save_flags;
4909 	struct qla_hw_data *ha = vha->hw;
4910 
4911 	rval = QLA_SUCCESS;
4912 
4913 	/* Get Initiator ID */
4914 	if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4915 		rval = qla2x00_configure_hba(vha);
4916 		if (rval != QLA_SUCCESS) {
4917 			ql_dbg(ql_dbg_disc, vha, 0x2013,
4918 			    "Unable to configure HBA.\n");
4919 			return (rval);
4920 		}
4921 	}
4922 
4923 	save_flags = flags = vha->dpc_flags;
4924 	ql_dbg(ql_dbg_disc, vha, 0x2014,
4925 	    "Configure loop -- dpc flags = 0x%lx.\n", flags);
4926 
4927 	/*
4928 	 * If we have both an RSCN and PORT UPDATE pending then handle them
4929 	 * both at the same time.
4930 	 */
4931 	clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4932 	clear_bit(RSCN_UPDATE, &vha->dpc_flags);
4933 
4934 	qla2x00_get_data_rate(vha);
4935 
4936 	/* Determine what we need to do */
4937 	if (ha->current_topology == ISP_CFG_FL &&
4938 	    (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4939 
4940 		set_bit(RSCN_UPDATE, &flags);
4941 
4942 	} else if (ha->current_topology == ISP_CFG_F &&
4943 	    (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4944 
4945 		set_bit(RSCN_UPDATE, &flags);
4946 		clear_bit(LOCAL_LOOP_UPDATE, &flags);
4947 
4948 	} else if (ha->current_topology == ISP_CFG_N) {
4949 		clear_bit(RSCN_UPDATE, &flags);
4950 		if (qla_tgt_mode_enabled(vha)) {
4951 			/* allow the other side to start the login */
4952 			clear_bit(LOCAL_LOOP_UPDATE, &flags);
4953 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4954 		}
4955 	} else if (ha->current_topology == ISP_CFG_NL) {
4956 		clear_bit(RSCN_UPDATE, &flags);
4957 		set_bit(LOCAL_LOOP_UPDATE, &flags);
4958 	} else if (!vha->flags.online ||
4959 	    (test_bit(ABORT_ISP_ACTIVE, &flags))) {
4960 		set_bit(RSCN_UPDATE, &flags);
4961 		set_bit(LOCAL_LOOP_UPDATE, &flags);
4962 	}
4963 
4964 	if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
4965 		if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4966 			ql_dbg(ql_dbg_disc, vha, 0x2015,
4967 			    "Loop resync needed, failing.\n");
4968 			rval = QLA_FUNCTION_FAILED;
4969 		} else
4970 			rval = qla2x00_configure_local_loop(vha);
4971 	}
4972 
4973 	if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
4974 		if (LOOP_TRANSITION(vha)) {
4975 			ql_dbg(ql_dbg_disc, vha, 0x2099,
4976 			    "Needs RSCN update and loop transition.\n");
4977 			rval = QLA_FUNCTION_FAILED;
4978 		}
4979 		else
4980 			rval = qla2x00_configure_fabric(vha);
4981 	}
4982 
4983 	if (rval == QLA_SUCCESS) {
4984 		if (atomic_read(&vha->loop_down_timer) ||
4985 		    test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4986 			rval = QLA_FUNCTION_FAILED;
4987 		} else {
4988 			atomic_set(&vha->loop_state, LOOP_READY);
4989 			ql_dbg(ql_dbg_disc, vha, 0x2069,
4990 			    "LOOP READY.\n");
4991 			ha->flags.fw_init_done = 1;
4992 
4993 			/*
4994 			 * Process any ATIO queue entries that came in
4995 			 * while we weren't online.
4996 			 */
4997 			if (qla_tgt_mode_enabled(vha) ||
4998 			    qla_dual_mode_enabled(vha)) {
4999 				spin_lock_irqsave(&ha->tgt.atio_lock, flags);
5000 				qlt_24xx_process_atio_queue(vha, 0);
5001 				spin_unlock_irqrestore(&ha->tgt.atio_lock,
5002 				    flags);
5003 			}
5004 		}
5005 	}
5006 
5007 	if (rval) {
5008 		ql_dbg(ql_dbg_disc, vha, 0x206a,
5009 		    "%s *** FAILED ***.\n", __func__);
5010 	} else {
5011 		ql_dbg(ql_dbg_disc, vha, 0x206b,
5012 		    "%s: exiting normally.\n", __func__);
5013 	}
5014 
5015 	/* Restore state if a resync event occurred during processing */
5016 	if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
5017 		if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
5018 			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5019 		if (test_bit(RSCN_UPDATE, &save_flags)) {
5020 			set_bit(RSCN_UPDATE, &vha->dpc_flags);
5021 		}
5022 	}
5023 
5024 	return (rval);
5025 }
5026 
5027 /*
5028  * qla2x00_configure_local_loop
5029  *	Updates Fibre Channel Device Database with local loop devices.
5030  *
5031  * Input:
5032  *	ha = adapter block pointer.
5033  *
5034  * Returns:
5035  *	0 = success.
5036  */
5037 static int
5038 qla2x00_configure_local_loop(scsi_qla_host_t *vha)
5039 {
5040 	int		rval, rval2;
5041 	int		found_devs;
5042 	int		found;
5043 	fc_port_t	*fcport, *new_fcport;
5044 
5045 	uint16_t	index;
5046 	uint16_t	entries;
5047 	char		*id_iter;
5048 	uint16_t	loop_id;
5049 	uint8_t		domain, area, al_pa;
5050 	struct qla_hw_data *ha = vha->hw;
5051 	unsigned long flags;
5052 
5053 	/* Inititae N2N login. */
5054 	if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags)) {
5055 		/* borrowing */
5056 		u32 *bp, i, sz;
5057 
5058 		memset(ha->init_cb, 0, ha->init_cb_size);
5059 		sz = min_t(int, sizeof(struct els_plogi_payload),
5060 		    ha->init_cb_size);
5061 		rval = qla24xx_get_port_login_templ(vha, ha->init_cb_dma,
5062 		    (void *)ha->init_cb, sz);
5063 		if (rval == QLA_SUCCESS) {
5064 			bp = (uint32_t *)ha->init_cb;
5065 			for (i = 0; i < sz/4 ; i++, bp++)
5066 				*bp = cpu_to_be32(*bp);
5067 
5068 			memcpy(&ha->plogi_els_payld.data, (void *)ha->init_cb,
5069 			    sizeof(ha->plogi_els_payld.data));
5070 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
5071 		} else {
5072 			ql_dbg(ql_dbg_init, vha, 0x00d1,
5073 			    "PLOGI ELS param read fail.\n");
5074 		}
5075 		return QLA_SUCCESS;
5076 	}
5077 
5078 	found_devs = 0;
5079 	new_fcport = NULL;
5080 	entries = MAX_FIBRE_DEVICES_LOOP;
5081 
5082 	/* Get list of logged in devices. */
5083 	memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
5084 	rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
5085 	    &entries);
5086 	if (rval != QLA_SUCCESS)
5087 		goto cleanup_allocation;
5088 
5089 	ql_dbg(ql_dbg_disc, vha, 0x2011,
5090 	    "Entries in ID list (%d).\n", entries);
5091 	ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
5092 	    ha->gid_list, entries * sizeof(*ha->gid_list));
5093 
5094 	if (entries == 0) {
5095 		spin_lock_irqsave(&vha->work_lock, flags);
5096 		vha->scan.scan_retry++;
5097 		spin_unlock_irqrestore(&vha->work_lock, flags);
5098 
5099 		if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
5100 			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5101 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5102 		}
5103 	} else {
5104 		vha->scan.scan_retry = 0;
5105 	}
5106 
5107 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
5108 		fcport->scan_state = QLA_FCPORT_SCAN;
5109 	}
5110 
5111 	/* Allocate temporary fcport for any new fcports discovered. */
5112 	new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5113 	if (new_fcport == NULL) {
5114 		ql_log(ql_log_warn, vha, 0x2012,
5115 		    "Memory allocation failed for fcport.\n");
5116 		rval = QLA_MEMORY_ALLOC_FAILED;
5117 		goto cleanup_allocation;
5118 	}
5119 	new_fcport->flags &= ~FCF_FABRIC_DEVICE;
5120 
5121 	/* Add devices to port list. */
5122 	id_iter = (char *)ha->gid_list;
5123 	for (index = 0; index < entries; index++) {
5124 		domain = ((struct gid_list_info *)id_iter)->domain;
5125 		area = ((struct gid_list_info *)id_iter)->area;
5126 		al_pa = ((struct gid_list_info *)id_iter)->al_pa;
5127 		if (IS_QLA2100(ha) || IS_QLA2200(ha))
5128 			loop_id = (uint16_t)
5129 			    ((struct gid_list_info *)id_iter)->loop_id_2100;
5130 		else
5131 			loop_id = le16_to_cpu(
5132 			    ((struct gid_list_info *)id_iter)->loop_id);
5133 		id_iter += ha->gid_list_info_size;
5134 
5135 		/* Bypass reserved domain fields. */
5136 		if ((domain & 0xf0) == 0xf0)
5137 			continue;
5138 
5139 		/* Bypass if not same domain and area of adapter. */
5140 		if (area && domain && ((area != vha->d_id.b.area) ||
5141 		    (domain != vha->d_id.b.domain)) &&
5142 		    (ha->current_topology == ISP_CFG_NL))
5143 			continue;
5144 
5145 
5146 		/* Bypass invalid local loop ID. */
5147 		if (loop_id > LAST_LOCAL_LOOP_ID)
5148 			continue;
5149 
5150 		memset(new_fcport->port_name, 0, WWN_SIZE);
5151 
5152 		/* Fill in member data. */
5153 		new_fcport->d_id.b.domain = domain;
5154 		new_fcport->d_id.b.area = area;
5155 		new_fcport->d_id.b.al_pa = al_pa;
5156 		new_fcport->loop_id = loop_id;
5157 		new_fcport->scan_state = QLA_FCPORT_FOUND;
5158 
5159 		rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
5160 		if (rval2 != QLA_SUCCESS) {
5161 			ql_dbg(ql_dbg_disc, vha, 0x2097,
5162 			    "Failed to retrieve fcport information "
5163 			    "-- get_port_database=%x, loop_id=0x%04x.\n",
5164 			    rval2, new_fcport->loop_id);
5165 			/* Skip retry if N2N */
5166 			if (ha->current_topology != ISP_CFG_N) {
5167 				ql_dbg(ql_dbg_disc, vha, 0x2105,
5168 				    "Scheduling resync.\n");
5169 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5170 				continue;
5171 			}
5172 		}
5173 
5174 		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5175 		/* Check for matching device in port list. */
5176 		found = 0;
5177 		fcport = NULL;
5178 		list_for_each_entry(fcport, &vha->vp_fcports, list) {
5179 			if (memcmp(new_fcport->port_name, fcport->port_name,
5180 			    WWN_SIZE))
5181 				continue;
5182 
5183 			fcport->flags &= ~FCF_FABRIC_DEVICE;
5184 			fcport->loop_id = new_fcport->loop_id;
5185 			fcport->port_type = new_fcport->port_type;
5186 			fcport->d_id.b24 = new_fcport->d_id.b24;
5187 			memcpy(fcport->node_name, new_fcport->node_name,
5188 			    WWN_SIZE);
5189 			fcport->scan_state = QLA_FCPORT_FOUND;
5190 			found++;
5191 			break;
5192 		}
5193 
5194 		if (!found) {
5195 			/* New device, add to fcports list. */
5196 			list_add_tail(&new_fcport->list, &vha->vp_fcports);
5197 
5198 			/* Allocate a new replacement fcport. */
5199 			fcport = new_fcport;
5200 
5201 			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5202 
5203 			new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5204 
5205 			if (new_fcport == NULL) {
5206 				ql_log(ql_log_warn, vha, 0xd031,
5207 				    "Failed to allocate memory for fcport.\n");
5208 				rval = QLA_MEMORY_ALLOC_FAILED;
5209 				goto cleanup_allocation;
5210 			}
5211 			spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5212 			new_fcport->flags &= ~FCF_FABRIC_DEVICE;
5213 		}
5214 
5215 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5216 
5217 		/* Base iIDMA settings on HBA port speed. */
5218 		fcport->fp_speed = ha->link_data_rate;
5219 
5220 		found_devs++;
5221 	}
5222 
5223 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
5224 		if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5225 			break;
5226 
5227 		if (fcport->scan_state == QLA_FCPORT_SCAN) {
5228 			if ((qla_dual_mode_enabled(vha) ||
5229 			    qla_ini_mode_enabled(vha)) &&
5230 			    atomic_read(&fcport->state) == FCS_ONLINE) {
5231 				qla2x00_mark_device_lost(vha, fcport,
5232 					ql2xplogiabsentdevice, 0);
5233 				if (fcport->loop_id != FC_NO_LOOP_ID &&
5234 				    (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5235 				    fcport->port_type != FCT_INITIATOR &&
5236 				    fcport->port_type != FCT_BROADCAST) {
5237 					ql_dbg(ql_dbg_disc, vha, 0x20f0,
5238 					    "%s %d %8phC post del sess\n",
5239 					    __func__, __LINE__,
5240 					    fcport->port_name);
5241 
5242 					qlt_schedule_sess_for_deletion(fcport);
5243 					continue;
5244 				}
5245 			}
5246 		}
5247 
5248 		if (fcport->scan_state == QLA_FCPORT_FOUND)
5249 			qla24xx_fcport_handle_login(vha, fcport);
5250 	}
5251 
5252 cleanup_allocation:
5253 	kfree(new_fcport);
5254 
5255 	if (rval != QLA_SUCCESS) {
5256 		ql_dbg(ql_dbg_disc, vha, 0x2098,
5257 		    "Configure local loop error exit: rval=%x.\n", rval);
5258 	}
5259 
5260 	return (rval);
5261 }
5262 
5263 static void
5264 qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
5265 {
5266 	int rval;
5267 	uint16_t mb[MAILBOX_REGISTER_COUNT];
5268 	struct qla_hw_data *ha = vha->hw;
5269 
5270 	if (!IS_IIDMA_CAPABLE(ha))
5271 		return;
5272 
5273 	if (atomic_read(&fcport->state) != FCS_ONLINE)
5274 		return;
5275 
5276 	if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
5277 	    fcport->fp_speed > ha->link_data_rate ||
5278 	    !ha->flags.gpsc_supported)
5279 		return;
5280 
5281 	rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
5282 	    mb);
5283 	if (rval != QLA_SUCCESS) {
5284 		ql_dbg(ql_dbg_disc, vha, 0x2004,
5285 		    "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
5286 		    fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
5287 	} else {
5288 		ql_dbg(ql_dbg_disc, vha, 0x2005,
5289 		    "iIDMA adjusted to %s GB/s (%X) on %8phN.\n",
5290 		    qla2x00_get_link_speed_str(ha, fcport->fp_speed),
5291 		    fcport->fp_speed, fcport->port_name);
5292 	}
5293 }
5294 
5295 void qla_do_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
5296 {
5297 	qla2x00_iidma_fcport(vha, fcport);
5298 	qla24xx_update_fcport_fcp_prio(vha, fcport);
5299 }
5300 
5301 int qla_post_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
5302 {
5303 	struct qla_work_evt *e;
5304 
5305 	e = qla2x00_alloc_work(vha, QLA_EVT_IIDMA);
5306 	if (!e)
5307 		return QLA_FUNCTION_FAILED;
5308 
5309 	e->u.fcport.fcport = fcport;
5310 	return qla2x00_post_work(vha, e);
5311 }
5312 
5313 /* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
5314 static void
5315 qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
5316 {
5317 	struct fc_rport_identifiers rport_ids;
5318 	struct fc_rport *rport;
5319 	unsigned long flags;
5320 
5321 	if (atomic_read(&fcport->state) == FCS_ONLINE)
5322 		return;
5323 
5324 	rport_ids.node_name = wwn_to_u64(fcport->node_name);
5325 	rport_ids.port_name = wwn_to_u64(fcport->port_name);
5326 	rport_ids.port_id = fcport->d_id.b.domain << 16 |
5327 	    fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5328 	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
5329 	fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
5330 	if (!rport) {
5331 		ql_log(ql_log_warn, vha, 0x2006,
5332 		    "Unable to allocate fc remote port.\n");
5333 		return;
5334 	}
5335 
5336 	spin_lock_irqsave(fcport->vha->host->host_lock, flags);
5337 	*((fc_port_t **)rport->dd_data) = fcport;
5338 	spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
5339 
5340 	rport->supported_classes = fcport->supported_classes;
5341 
5342 	rport_ids.roles = FC_PORT_ROLE_UNKNOWN;
5343 	if (fcport->port_type == FCT_INITIATOR)
5344 		rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
5345 	if (fcport->port_type == FCT_TARGET)
5346 		rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
5347 	if (fcport->port_type & FCT_NVME_INITIATOR)
5348 		rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
5349 	if (fcport->port_type & FCT_NVME_TARGET)
5350 		rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
5351 	if (fcport->port_type & FCT_NVME_DISCOVERY)
5352 		rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
5353 
5354 	ql_dbg(ql_dbg_disc, vha, 0x20ee,
5355 	    "%s %8phN. rport %p is %s mode\n",
5356 	    __func__, fcport->port_name, rport,
5357 	    (fcport->port_type == FCT_TARGET) ? "tgt" :
5358 	    ((fcport->port_type & FCT_NVME) ? "nvme" :"ini"));
5359 
5360 	fc_remote_port_rolechg(rport, rport_ids.roles);
5361 }
5362 
5363 /*
5364  * qla2x00_update_fcport
5365  *	Updates device on list.
5366  *
5367  * Input:
5368  *	ha = adapter block pointer.
5369  *	fcport = port structure pointer.
5370  *
5371  * Return:
5372  *	0  - Success
5373  *  BIT_0 - error
5374  *
5375  * Context:
5376  *	Kernel context.
5377  */
5378 void
5379 qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
5380 {
5381 	if (IS_SW_RESV_ADDR(fcport->d_id))
5382 		return;
5383 
5384 	ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
5385 	    __func__, fcport->port_name);
5386 
5387 	fcport->disc_state = DSC_UPD_FCPORT;
5388 	fcport->login_retry = vha->hw->login_retry_count;
5389 	fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
5390 	fcport->deleted = 0;
5391 	fcport->logout_on_delete = 1;
5392 	fcport->n2n_chip_reset = fcport->n2n_link_reset_cnt = 0;
5393 
5394 	switch (vha->hw->current_topology) {
5395 	case ISP_CFG_N:
5396 	case ISP_CFG_NL:
5397 		fcport->keep_nport_handle = 1;
5398 		break;
5399 	default:
5400 		break;
5401 	}
5402 
5403 	qla2x00_iidma_fcport(vha, fcport);
5404 
5405 	if (fcport->fc4f_nvme) {
5406 		qla_nvme_register_remote(vha, fcport);
5407 		fcport->disc_state = DSC_LOGIN_COMPLETE;
5408 		qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5409 		return;
5410 	}
5411 
5412 	qla24xx_update_fcport_fcp_prio(vha, fcport);
5413 
5414 	switch (vha->host->active_mode) {
5415 	case MODE_INITIATOR:
5416 		qla2x00_reg_remote_port(vha, fcport);
5417 		break;
5418 	case MODE_TARGET:
5419 		if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5420 			!vha->vha_tgt.qla_tgt->tgt_stopped)
5421 			qlt_fc_port_added(vha, fcport);
5422 		break;
5423 	case MODE_DUAL:
5424 		qla2x00_reg_remote_port(vha, fcport);
5425 		if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5426 			!vha->vha_tgt.qla_tgt->tgt_stopped)
5427 			qlt_fc_port_added(vha, fcport);
5428 		break;
5429 	default:
5430 		break;
5431 	}
5432 
5433 	qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5434 
5435 	if (IS_IIDMA_CAPABLE(vha->hw) && vha->hw->flags.gpsc_supported) {
5436 		if (fcport->id_changed) {
5437 			fcport->id_changed = 0;
5438 			ql_dbg(ql_dbg_disc, vha, 0x20d7,
5439 			    "%s %d %8phC post gfpnid fcp_cnt %d\n",
5440 			    __func__, __LINE__, fcport->port_name,
5441 			    vha->fcport_count);
5442 			qla24xx_post_gfpnid_work(vha, fcport);
5443 		} else {
5444 			ql_dbg(ql_dbg_disc, vha, 0x20d7,
5445 			    "%s %d %8phC post gpsc fcp_cnt %d\n",
5446 			    __func__, __LINE__, fcport->port_name,
5447 			    vha->fcport_count);
5448 			qla24xx_post_gpsc_work(vha, fcport);
5449 		}
5450 	}
5451 
5452 	fcport->disc_state = DSC_LOGIN_COMPLETE;
5453 }
5454 
5455 void qla_register_fcport_fn(struct work_struct *work)
5456 {
5457 	fc_port_t *fcport = container_of(work, struct fc_port, reg_work);
5458 	u32 rscn_gen = fcport->rscn_gen;
5459 	u16 data[2];
5460 
5461 	if (IS_SW_RESV_ADDR(fcport->d_id))
5462 		return;
5463 
5464 	qla2x00_update_fcport(fcport->vha, fcport);
5465 
5466 	if (rscn_gen != fcport->rscn_gen) {
5467 		/* RSCN(s) came in while registration */
5468 		switch (fcport->next_disc_state) {
5469 		case DSC_DELETE_PEND:
5470 			qlt_schedule_sess_for_deletion(fcport);
5471 			break;
5472 		case DSC_ADISC:
5473 			data[0] = data[1] = 0;
5474 			qla2x00_post_async_adisc_work(fcport->vha, fcport,
5475 			    data);
5476 			break;
5477 		default:
5478 			break;
5479 		}
5480 	}
5481 }
5482 
5483 /*
5484  * qla2x00_configure_fabric
5485  *      Setup SNS devices with loop ID's.
5486  *
5487  * Input:
5488  *      ha = adapter block pointer.
5489  *
5490  * Returns:
5491  *      0 = success.
5492  *      BIT_0 = error
5493  */
5494 static int
5495 qla2x00_configure_fabric(scsi_qla_host_t *vha)
5496 {
5497 	int	rval;
5498 	fc_port_t	*fcport;
5499 	uint16_t	mb[MAILBOX_REGISTER_COUNT];
5500 	uint16_t	loop_id;
5501 	LIST_HEAD(new_fcports);
5502 	struct qla_hw_data *ha = vha->hw;
5503 	int		discovery_gen;
5504 
5505 	/* If FL port exists, then SNS is present */
5506 	if (IS_FWI2_CAPABLE(ha))
5507 		loop_id = NPH_F_PORT;
5508 	else
5509 		loop_id = SNS_FL_PORT;
5510 	rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
5511 	if (rval != QLA_SUCCESS) {
5512 		ql_dbg(ql_dbg_disc, vha, 0x20a0,
5513 		    "MBX_GET_PORT_NAME failed, No FL Port.\n");
5514 
5515 		vha->device_flags &= ~SWITCH_FOUND;
5516 		return (QLA_SUCCESS);
5517 	}
5518 	vha->device_flags |= SWITCH_FOUND;
5519 
5520 
5521 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5522 		rval = qla2x00_send_change_request(vha, 0x3, 0);
5523 		if (rval != QLA_SUCCESS)
5524 			ql_log(ql_log_warn, vha, 0x121,
5525 				"Failed to enable receiving of RSCN requests: 0x%x.\n",
5526 				rval);
5527 	}
5528 
5529 
5530 	do {
5531 		qla2x00_mgmt_svr_login(vha);
5532 
5533 		/* FDMI support. */
5534 		if (ql2xfdmienable &&
5535 		    test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5536 			qla2x00_fdmi_register(vha);
5537 
5538 		/* Ensure we are logged into the SNS. */
5539 		loop_id = NPH_SNS_LID(ha);
5540 		rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5541 		    0xfc, mb, BIT_1|BIT_0);
5542 		if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5543 			ql_dbg(ql_dbg_disc, vha, 0x20a1,
5544 			    "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5545 			    loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
5546 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5547 			return rval;
5548 		}
5549 		if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5550 			if (qla2x00_rft_id(vha)) {
5551 				/* EMPTY */
5552 				ql_dbg(ql_dbg_disc, vha, 0x20a2,
5553 				    "Register FC-4 TYPE failed.\n");
5554 				if (test_bit(LOOP_RESYNC_NEEDED,
5555 				    &vha->dpc_flags))
5556 					break;
5557 			}
5558 			if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
5559 				/* EMPTY */
5560 				ql_dbg(ql_dbg_disc, vha, 0x209a,
5561 				    "Register FC-4 Features failed.\n");
5562 				if (test_bit(LOOP_RESYNC_NEEDED,
5563 				    &vha->dpc_flags))
5564 					break;
5565 			}
5566 			if (vha->flags.nvme_enabled) {
5567 				if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5568 					ql_dbg(ql_dbg_disc, vha, 0x2049,
5569 					    "Register NVME FC Type Features failed.\n");
5570 				}
5571 			}
5572 			if (qla2x00_rnn_id(vha)) {
5573 				/* EMPTY */
5574 				ql_dbg(ql_dbg_disc, vha, 0x2104,
5575 				    "Register Node Name failed.\n");
5576 				if (test_bit(LOOP_RESYNC_NEEDED,
5577 				    &vha->dpc_flags))
5578 					break;
5579 			} else if (qla2x00_rsnn_nn(vha)) {
5580 				/* EMPTY */
5581 				ql_dbg(ql_dbg_disc, vha, 0x209b,
5582 				    "Register Symbolic Node Name failed.\n");
5583 				if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5584 					break;
5585 			}
5586 		}
5587 
5588 
5589 		/* Mark the time right before querying FW for connected ports.
5590 		 * This process is long, asynchronous and by the time it's done,
5591 		 * collected information might not be accurate anymore. E.g.
5592 		 * disconnected port might have re-connected and a brand new
5593 		 * session has been created. In this case session's generation
5594 		 * will be newer than discovery_gen. */
5595 		qlt_do_generation_tick(vha, &discovery_gen);
5596 
5597 		if (USE_ASYNC_SCAN(ha)) {
5598 			rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI,
5599 			    NULL);
5600 			if (rval)
5601 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5602 		} else  {
5603 			list_for_each_entry(fcport, &vha->vp_fcports, list)
5604 				fcport->scan_state = QLA_FCPORT_SCAN;
5605 
5606 			rval = qla2x00_find_all_fabric_devs(vha);
5607 		}
5608 		if (rval != QLA_SUCCESS)
5609 			break;
5610 	} while (0);
5611 
5612 	if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5613 		qla_nvme_register_hba(vha);
5614 
5615 	if (rval)
5616 		ql_dbg(ql_dbg_disc, vha, 0x2068,
5617 		    "Configure fabric error exit rval=%d.\n", rval);
5618 
5619 	return (rval);
5620 }
5621 
5622 /*
5623  * qla2x00_find_all_fabric_devs
5624  *
5625  * Input:
5626  *	ha = adapter block pointer.
5627  *	dev = database device entry pointer.
5628  *
5629  * Returns:
5630  *	0 = success.
5631  *
5632  * Context:
5633  *	Kernel context.
5634  */
5635 static int
5636 qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
5637 {
5638 	int		rval;
5639 	uint16_t	loop_id;
5640 	fc_port_t	*fcport, *new_fcport;
5641 	int		found;
5642 
5643 	sw_info_t	*swl;
5644 	int		swl_idx;
5645 	int		first_dev, last_dev;
5646 	port_id_t	wrap = {}, nxt_d_id;
5647 	struct qla_hw_data *ha = vha->hw;
5648 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
5649 	unsigned long flags;
5650 
5651 	rval = QLA_SUCCESS;
5652 
5653 	/* Try GID_PT to get device list, else GAN. */
5654 	if (!ha->swl)
5655 		ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
5656 		    GFP_KERNEL);
5657 	swl = ha->swl;
5658 	if (!swl) {
5659 		/*EMPTY*/
5660 		ql_dbg(ql_dbg_disc, vha, 0x209c,
5661 		    "GID_PT allocations failed, fallback on GA_NXT.\n");
5662 	} else {
5663 		memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
5664 		if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
5665 			swl = NULL;
5666 			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5667 				return rval;
5668 		} else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
5669 			swl = NULL;
5670 			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5671 				return rval;
5672 		} else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
5673 			swl = NULL;
5674 			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5675 				return rval;
5676 		} else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5677 			swl = NULL;
5678 			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5679 				return rval;
5680 		}
5681 
5682 		/* If other queries succeeded probe for FC-4 type */
5683 		if (swl) {
5684 			qla2x00_gff_id(vha, swl);
5685 			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5686 				return rval;
5687 		}
5688 	}
5689 	swl_idx = 0;
5690 
5691 	/* Allocate temporary fcport for any new fcports discovered. */
5692 	new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5693 	if (new_fcport == NULL) {
5694 		ql_log(ql_log_warn, vha, 0x209d,
5695 		    "Failed to allocate memory for fcport.\n");
5696 		return (QLA_MEMORY_ALLOC_FAILED);
5697 	}
5698 	new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5699 	/* Set start port ID scan at adapter ID. */
5700 	first_dev = 1;
5701 	last_dev = 0;
5702 
5703 	/* Starting free loop ID. */
5704 	loop_id = ha->min_external_loopid;
5705 	for (; loop_id <= ha->max_loop_id; loop_id++) {
5706 		if (qla2x00_is_reserved_id(vha, loop_id))
5707 			continue;
5708 
5709 		if (ha->current_topology == ISP_CFG_FL &&
5710 		    (atomic_read(&vha->loop_down_timer) ||
5711 		     LOOP_TRANSITION(vha))) {
5712 			atomic_set(&vha->loop_down_timer, 0);
5713 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5714 			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5715 			break;
5716 		}
5717 
5718 		if (swl != NULL) {
5719 			if (last_dev) {
5720 				wrap.b24 = new_fcport->d_id.b24;
5721 			} else {
5722 				new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5723 				memcpy(new_fcport->node_name,
5724 				    swl[swl_idx].node_name, WWN_SIZE);
5725 				memcpy(new_fcport->port_name,
5726 				    swl[swl_idx].port_name, WWN_SIZE);
5727 				memcpy(new_fcport->fabric_port_name,
5728 				    swl[swl_idx].fabric_port_name, WWN_SIZE);
5729 				new_fcport->fp_speed = swl[swl_idx].fp_speed;
5730 				new_fcport->fc4_type = swl[swl_idx].fc4_type;
5731 
5732 				new_fcport->nvme_flag = 0;
5733 				new_fcport->fc4f_nvme = 0;
5734 				if (vha->flags.nvme_enabled &&
5735 				    swl[swl_idx].fc4f_nvme) {
5736 					new_fcport->fc4f_nvme =
5737 					    swl[swl_idx].fc4f_nvme;
5738 					ql_log(ql_log_info, vha, 0x2131,
5739 					    "FOUND: NVME port %8phC as FC Type 28h\n",
5740 					    new_fcport->port_name);
5741 				}
5742 
5743 				if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5744 					last_dev = 1;
5745 				}
5746 				swl_idx++;
5747 			}
5748 		} else {
5749 			/* Send GA_NXT to the switch */
5750 			rval = qla2x00_ga_nxt(vha, new_fcport);
5751 			if (rval != QLA_SUCCESS) {
5752 				ql_log(ql_log_warn, vha, 0x209e,
5753 				    "SNS scan failed -- assuming "
5754 				    "zero-entry result.\n");
5755 				rval = QLA_SUCCESS;
5756 				break;
5757 			}
5758 		}
5759 
5760 		/* If wrap on switch device list, exit. */
5761 		if (first_dev) {
5762 			wrap.b24 = new_fcport->d_id.b24;
5763 			first_dev = 0;
5764 		} else if (new_fcport->d_id.b24 == wrap.b24) {
5765 			ql_dbg(ql_dbg_disc, vha, 0x209f,
5766 			    "Device wrap (%02x%02x%02x).\n",
5767 			    new_fcport->d_id.b.domain,
5768 			    new_fcport->d_id.b.area,
5769 			    new_fcport->d_id.b.al_pa);
5770 			break;
5771 		}
5772 
5773 		/* Bypass if same physical adapter. */
5774 		if (new_fcport->d_id.b24 == base_vha->d_id.b24)
5775 			continue;
5776 
5777 		/* Bypass virtual ports of the same host. */
5778 		if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5779 			continue;
5780 
5781 		/* Bypass if same domain and area of adapter. */
5782 		if (((new_fcport->d_id.b24 & 0xffff00) ==
5783 		    (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
5784 			ISP_CFG_FL)
5785 			    continue;
5786 
5787 		/* Bypass reserved domain fields. */
5788 		if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5789 			continue;
5790 
5791 		/* Bypass ports whose FCP-4 type is not FCP_SCSI */
5792 		if (ql2xgffidenable &&
5793 		    (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5794 		    new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
5795 			continue;
5796 
5797 		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5798 
5799 		/* Locate matching device in database. */
5800 		found = 0;
5801 		list_for_each_entry(fcport, &vha->vp_fcports, list) {
5802 			if (memcmp(new_fcport->port_name, fcport->port_name,
5803 			    WWN_SIZE))
5804 				continue;
5805 
5806 			fcport->scan_state = QLA_FCPORT_FOUND;
5807 
5808 			found++;
5809 
5810 			/* Update port state. */
5811 			memcpy(fcport->fabric_port_name,
5812 			    new_fcport->fabric_port_name, WWN_SIZE);
5813 			fcport->fp_speed = new_fcport->fp_speed;
5814 
5815 			/*
5816 			 * If address the same and state FCS_ONLINE
5817 			 * (or in target mode), nothing changed.
5818 			 */
5819 			if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
5820 			    (atomic_read(&fcport->state) == FCS_ONLINE ||
5821 			     (vha->host->active_mode == MODE_TARGET))) {
5822 				break;
5823 			}
5824 
5825 			/*
5826 			 * If device was not a fabric device before.
5827 			 */
5828 			if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5829 				fcport->d_id.b24 = new_fcport->d_id.b24;
5830 				qla2x00_clear_loop_id(fcport);
5831 				fcport->flags |= (FCF_FABRIC_DEVICE |
5832 				    FCF_LOGIN_NEEDED);
5833 				break;
5834 			}
5835 
5836 			/*
5837 			 * Port ID changed or device was marked to be updated;
5838 			 * Log it out if still logged in and mark it for
5839 			 * relogin later.
5840 			 */
5841 			if (qla_tgt_mode_enabled(base_vha)) {
5842 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5843 					 "port changed FC ID, %8phC"
5844 					 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5845 					 fcport->port_name,
5846 					 fcport->d_id.b.domain,
5847 					 fcport->d_id.b.area,
5848 					 fcport->d_id.b.al_pa,
5849 					 fcport->loop_id,
5850 					 new_fcport->d_id.b.domain,
5851 					 new_fcport->d_id.b.area,
5852 					 new_fcport->d_id.b.al_pa);
5853 				fcport->d_id.b24 = new_fcport->d_id.b24;
5854 				break;
5855 			}
5856 
5857 			fcport->d_id.b24 = new_fcport->d_id.b24;
5858 			fcport->flags |= FCF_LOGIN_NEEDED;
5859 			break;
5860 		}
5861 
5862 		if (fcport->fc4f_nvme) {
5863 			if (fcport->disc_state == DSC_DELETE_PEND) {
5864 				fcport->disc_state = DSC_GNL;
5865 				vha->fcport_count--;
5866 				fcport->login_succ = 0;
5867 			}
5868 		}
5869 
5870 		if (found) {
5871 			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5872 			continue;
5873 		}
5874 		/* If device was not in our fcports list, then add it. */
5875 		new_fcport->scan_state = QLA_FCPORT_FOUND;
5876 		list_add_tail(&new_fcport->list, &vha->vp_fcports);
5877 
5878 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5879 
5880 
5881 		/* Allocate a new replacement fcport. */
5882 		nxt_d_id.b24 = new_fcport->d_id.b24;
5883 		new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5884 		if (new_fcport == NULL) {
5885 			ql_log(ql_log_warn, vha, 0xd032,
5886 			    "Memory allocation failed for fcport.\n");
5887 			return (QLA_MEMORY_ALLOC_FAILED);
5888 		}
5889 		new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5890 		new_fcport->d_id.b24 = nxt_d_id.b24;
5891 	}
5892 
5893 	qla2x00_free_fcport(new_fcport);
5894 
5895 	/*
5896 	 * Logout all previous fabric dev marked lost, except FCP2 devices.
5897 	 */
5898 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
5899 		if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5900 			break;
5901 
5902 		if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5903 		    (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5904 			continue;
5905 
5906 		if (fcport->scan_state == QLA_FCPORT_SCAN) {
5907 			if ((qla_dual_mode_enabled(vha) ||
5908 			    qla_ini_mode_enabled(vha)) &&
5909 			    atomic_read(&fcport->state) == FCS_ONLINE) {
5910 				qla2x00_mark_device_lost(vha, fcport,
5911 					ql2xplogiabsentdevice, 0);
5912 				if (fcport->loop_id != FC_NO_LOOP_ID &&
5913 				    (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5914 				    fcport->port_type != FCT_INITIATOR &&
5915 				    fcport->port_type != FCT_BROADCAST) {
5916 					ql_dbg(ql_dbg_disc, vha, 0x20f0,
5917 					    "%s %d %8phC post del sess\n",
5918 					    __func__, __LINE__,
5919 					    fcport->port_name);
5920 					qlt_schedule_sess_for_deletion(fcport);
5921 					continue;
5922 				}
5923 			}
5924 		}
5925 
5926 		if (fcport->scan_state == QLA_FCPORT_FOUND)
5927 			qla24xx_fcport_handle_login(vha, fcport);
5928 	}
5929 	return (rval);
5930 }
5931 
5932 /* FW does not set aside Loop id for MGMT Server/FFFFFAh */
5933 int
5934 qla2x00_reserve_mgmt_server_loop_id(scsi_qla_host_t *vha)
5935 {
5936 	int loop_id = FC_NO_LOOP_ID;
5937 	int lid = NPH_MGMT_SERVER - vha->vp_idx;
5938 	unsigned long flags;
5939 	struct qla_hw_data *ha = vha->hw;
5940 
5941 	if (vha->vp_idx == 0) {
5942 		set_bit(NPH_MGMT_SERVER, ha->loop_id_map);
5943 		return NPH_MGMT_SERVER;
5944 	}
5945 
5946 	/* pick id from high and work down to low */
5947 	spin_lock_irqsave(&ha->vport_slock, flags);
5948 	for (; lid > 0; lid--) {
5949 		if (!test_bit(lid, vha->hw->loop_id_map)) {
5950 			set_bit(lid, vha->hw->loop_id_map);
5951 			loop_id = lid;
5952 			break;
5953 		}
5954 	}
5955 	spin_unlock_irqrestore(&ha->vport_slock, flags);
5956 
5957 	return loop_id;
5958 }
5959 
5960 /*
5961  * qla2x00_fabric_login
5962  *	Issue fabric login command.
5963  *
5964  * Input:
5965  *	ha = adapter block pointer.
5966  *	device = pointer to FC device type structure.
5967  *
5968  * Returns:
5969  *      0 - Login successfully
5970  *      1 - Login failed
5971  *      2 - Initiator device
5972  *      3 - Fatal error
5973  */
5974 int
5975 qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
5976     uint16_t *next_loopid)
5977 {
5978 	int	rval;
5979 	int	retry;
5980 	uint16_t tmp_loopid;
5981 	uint16_t mb[MAILBOX_REGISTER_COUNT];
5982 	struct qla_hw_data *ha = vha->hw;
5983 
5984 	retry = 0;
5985 	tmp_loopid = 0;
5986 
5987 	for (;;) {
5988 		ql_dbg(ql_dbg_disc, vha, 0x2000,
5989 		    "Trying Fabric Login w/loop id 0x%04x for port "
5990 		    "%02x%02x%02x.\n",
5991 		    fcport->loop_id, fcport->d_id.b.domain,
5992 		    fcport->d_id.b.area, fcport->d_id.b.al_pa);
5993 
5994 		/* Login fcport on switch. */
5995 		rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
5996 		    fcport->d_id.b.domain, fcport->d_id.b.area,
5997 		    fcport->d_id.b.al_pa, mb, BIT_0);
5998 		if (rval != QLA_SUCCESS) {
5999 			return rval;
6000 		}
6001 		if (mb[0] == MBS_PORT_ID_USED) {
6002 			/*
6003 			 * Device has another loop ID.  The firmware team
6004 			 * recommends the driver perform an implicit login with
6005 			 * the specified ID again. The ID we just used is save
6006 			 * here so we return with an ID that can be tried by
6007 			 * the next login.
6008 			 */
6009 			retry++;
6010 			tmp_loopid = fcport->loop_id;
6011 			fcport->loop_id = mb[1];
6012 
6013 			ql_dbg(ql_dbg_disc, vha, 0x2001,
6014 			    "Fabric Login: port in use - next loop "
6015 			    "id=0x%04x, port id= %02x%02x%02x.\n",
6016 			    fcport->loop_id, fcport->d_id.b.domain,
6017 			    fcport->d_id.b.area, fcport->d_id.b.al_pa);
6018 
6019 		} else if (mb[0] == MBS_COMMAND_COMPLETE) {
6020 			/*
6021 			 * Login succeeded.
6022 			 */
6023 			if (retry) {
6024 				/* A retry occurred before. */
6025 				*next_loopid = tmp_loopid;
6026 			} else {
6027 				/*
6028 				 * No retry occurred before. Just increment the
6029 				 * ID value for next login.
6030 				 */
6031 				*next_loopid = (fcport->loop_id + 1);
6032 			}
6033 
6034 			if (mb[1] & BIT_0) {
6035 				fcport->port_type = FCT_INITIATOR;
6036 			} else {
6037 				fcport->port_type = FCT_TARGET;
6038 				if (mb[1] & BIT_1) {
6039 					fcport->flags |= FCF_FCP2_DEVICE;
6040 				}
6041 			}
6042 
6043 			if (mb[10] & BIT_0)
6044 				fcport->supported_classes |= FC_COS_CLASS2;
6045 			if (mb[10] & BIT_1)
6046 				fcport->supported_classes |= FC_COS_CLASS3;
6047 
6048 			if (IS_FWI2_CAPABLE(ha)) {
6049 				if (mb[10] & BIT_7)
6050 					fcport->flags |=
6051 					    FCF_CONF_COMP_SUPPORTED;
6052 			}
6053 
6054 			rval = QLA_SUCCESS;
6055 			break;
6056 		} else if (mb[0] == MBS_LOOP_ID_USED) {
6057 			/*
6058 			 * Loop ID already used, try next loop ID.
6059 			 */
6060 			fcport->loop_id++;
6061 			rval = qla2x00_find_new_loop_id(vha, fcport);
6062 			if (rval != QLA_SUCCESS) {
6063 				/* Ran out of loop IDs to use */
6064 				break;
6065 			}
6066 		} else if (mb[0] == MBS_COMMAND_ERROR) {
6067 			/*
6068 			 * Firmware possibly timed out during login. If NO
6069 			 * retries are left to do then the device is declared
6070 			 * dead.
6071 			 */
6072 			*next_loopid = fcport->loop_id;
6073 			ha->isp_ops->fabric_logout(vha, fcport->loop_id,
6074 			    fcport->d_id.b.domain, fcport->d_id.b.area,
6075 			    fcport->d_id.b.al_pa);
6076 			qla2x00_mark_device_lost(vha, fcport, 1, 0);
6077 
6078 			rval = 1;
6079 			break;
6080 		} else {
6081 			/*
6082 			 * unrecoverable / not handled error
6083 			 */
6084 			ql_dbg(ql_dbg_disc, vha, 0x2002,
6085 			    "Failed=%x port_id=%02x%02x%02x loop_id=%x "
6086 			    "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
6087 			    fcport->d_id.b.area, fcport->d_id.b.al_pa,
6088 			    fcport->loop_id, jiffies);
6089 
6090 			*next_loopid = fcport->loop_id;
6091 			ha->isp_ops->fabric_logout(vha, fcport->loop_id,
6092 			    fcport->d_id.b.domain, fcport->d_id.b.area,
6093 			    fcport->d_id.b.al_pa);
6094 			qla2x00_clear_loop_id(fcport);
6095 			fcport->login_retry = 0;
6096 
6097 			rval = 3;
6098 			break;
6099 		}
6100 	}
6101 
6102 	return (rval);
6103 }
6104 
6105 /*
6106  * qla2x00_local_device_login
6107  *	Issue local device login command.
6108  *
6109  * Input:
6110  *	ha = adapter block pointer.
6111  *	loop_id = loop id of device to login to.
6112  *
6113  * Returns (Where's the #define!!!!):
6114  *      0 - Login successfully
6115  *      1 - Login failed
6116  *      3 - Fatal error
6117  */
6118 int
6119 qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
6120 {
6121 	int		rval;
6122 	uint16_t	mb[MAILBOX_REGISTER_COUNT];
6123 
6124 	memset(mb, 0, sizeof(mb));
6125 	rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
6126 	if (rval == QLA_SUCCESS) {
6127 		/* Interrogate mailbox registers for any errors */
6128 		if (mb[0] == MBS_COMMAND_ERROR)
6129 			rval = 1;
6130 		else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
6131 			/* device not in PCB table */
6132 			rval = 3;
6133 	}
6134 
6135 	return (rval);
6136 }
6137 
6138 /*
6139  *  qla2x00_loop_resync
6140  *      Resync with fibre channel devices.
6141  *
6142  * Input:
6143  *      ha = adapter block pointer.
6144  *
6145  * Returns:
6146  *      0 = success
6147  */
6148 int
6149 qla2x00_loop_resync(scsi_qla_host_t *vha)
6150 {
6151 	int rval = QLA_SUCCESS;
6152 	uint32_t wait_time;
6153 
6154 	clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6155 	if (vha->flags.online) {
6156 		if (!(rval = qla2x00_fw_ready(vha))) {
6157 			/* Wait at most MAX_TARGET RSCNs for a stable link. */
6158 			wait_time = 256;
6159 			do {
6160 				if (!IS_QLAFX00(vha->hw)) {
6161 					/*
6162 					 * Issue a marker after FW becomes
6163 					 * ready.
6164 					 */
6165 					qla2x00_marker(vha, vha->hw->base_qpair,
6166 					    0, 0, MK_SYNC_ALL);
6167 					vha->marker_needed = 0;
6168 				}
6169 
6170 				/* Remap devices on Loop. */
6171 				clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6172 
6173 				if (IS_QLAFX00(vha->hw))
6174 					qlafx00_configure_devices(vha);
6175 				else
6176 					qla2x00_configure_loop(vha);
6177 
6178 				wait_time--;
6179 			} while (!atomic_read(&vha->loop_down_timer) &&
6180 				!(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
6181 				&& wait_time && (test_bit(LOOP_RESYNC_NEEDED,
6182 				&vha->dpc_flags)));
6183 		}
6184 	}
6185 
6186 	if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
6187 		return (QLA_FUNCTION_FAILED);
6188 
6189 	if (rval)
6190 		ql_dbg(ql_dbg_disc, vha, 0x206c,
6191 		    "%s *** FAILED ***.\n", __func__);
6192 
6193 	return (rval);
6194 }
6195 
6196 /*
6197 * qla2x00_perform_loop_resync
6198 * Description: This function will set the appropriate flags and call
6199 *              qla2x00_loop_resync. If successful loop will be resynced
6200 * Arguments : scsi_qla_host_t pointer
6201 * returm    : Success or Failure
6202 */
6203 
6204 int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
6205 {
6206 	int32_t rval = 0;
6207 
6208 	if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
6209 		/*Configure the flags so that resync happens properly*/
6210 		atomic_set(&ha->loop_down_timer, 0);
6211 		if (!(ha->device_flags & DFLG_NO_CABLE)) {
6212 			atomic_set(&ha->loop_state, LOOP_UP);
6213 			set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
6214 			set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
6215 			set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
6216 
6217 			rval = qla2x00_loop_resync(ha);
6218 		} else
6219 			atomic_set(&ha->loop_state, LOOP_DEAD);
6220 
6221 		clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
6222 	}
6223 
6224 	return rval;
6225 }
6226 
6227 void
6228 qla2x00_update_fcports(scsi_qla_host_t *base_vha)
6229 {
6230 	fc_port_t *fcport;
6231 	struct scsi_qla_host *vha;
6232 	struct qla_hw_data *ha = base_vha->hw;
6233 	unsigned long flags;
6234 
6235 	spin_lock_irqsave(&ha->vport_slock, flags);
6236 	/* Go with deferred removal of rport references. */
6237 	list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
6238 		atomic_inc(&vha->vref_count);
6239 		list_for_each_entry(fcport, &vha->vp_fcports, list) {
6240 			if (fcport->drport &&
6241 			    atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
6242 				spin_unlock_irqrestore(&ha->vport_slock, flags);
6243 				qla2x00_rport_del(fcport);
6244 
6245 				spin_lock_irqsave(&ha->vport_slock, flags);
6246 			}
6247 		}
6248 		atomic_dec(&vha->vref_count);
6249 		wake_up(&vha->vref_waitq);
6250 	}
6251 	spin_unlock_irqrestore(&ha->vport_slock, flags);
6252 }
6253 
6254 /* Assumes idc_lock always held on entry */
6255 void
6256 qla83xx_reset_ownership(scsi_qla_host_t *vha)
6257 {
6258 	struct qla_hw_data *ha = vha->hw;
6259 	uint32_t drv_presence, drv_presence_mask;
6260 	uint32_t dev_part_info1, dev_part_info2, class_type;
6261 	uint32_t class_type_mask = 0x3;
6262 	uint16_t fcoe_other_function = 0xffff, i;
6263 
6264 	if (IS_QLA8044(ha)) {
6265 		drv_presence = qla8044_rd_direct(vha,
6266 		    QLA8044_CRB_DRV_ACTIVE_INDEX);
6267 		dev_part_info1 = qla8044_rd_direct(vha,
6268 		    QLA8044_CRB_DEV_PART_INFO_INDEX);
6269 		dev_part_info2 = qla8044_rd_direct(vha,
6270 		    QLA8044_CRB_DEV_PART_INFO2);
6271 	} else {
6272 		qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6273 		qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
6274 		qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
6275 	}
6276 	for (i = 0; i < 8; i++) {
6277 		class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
6278 		if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
6279 		    (i != ha->portnum)) {
6280 			fcoe_other_function = i;
6281 			break;
6282 		}
6283 	}
6284 	if (fcoe_other_function == 0xffff) {
6285 		for (i = 0; i < 8; i++) {
6286 			class_type = ((dev_part_info2 >> (i * 4)) &
6287 			    class_type_mask);
6288 			if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
6289 			    ((i + 8) != ha->portnum)) {
6290 				fcoe_other_function = i + 8;
6291 				break;
6292 			}
6293 		}
6294 	}
6295 	/*
6296 	 * Prepare drv-presence mask based on fcoe functions present.
6297 	 * However consider only valid physical fcoe function numbers (0-15).
6298 	 */
6299 	drv_presence_mask = ~((1 << (ha->portnum)) |
6300 			((fcoe_other_function == 0xffff) ?
6301 			 0 : (1 << (fcoe_other_function))));
6302 
6303 	/* We are the reset owner iff:
6304 	 *    - No other protocol drivers present.
6305 	 *    - This is the lowest among fcoe functions. */
6306 	if (!(drv_presence & drv_presence_mask) &&
6307 			(ha->portnum < fcoe_other_function)) {
6308 		ql_dbg(ql_dbg_p3p, vha, 0xb07f,
6309 		    "This host is Reset owner.\n");
6310 		ha->flags.nic_core_reset_owner = 1;
6311 	}
6312 }
6313 
6314 static int
6315 __qla83xx_set_drv_ack(scsi_qla_host_t *vha)
6316 {
6317 	int rval = QLA_SUCCESS;
6318 	struct qla_hw_data *ha = vha->hw;
6319 	uint32_t drv_ack;
6320 
6321 	rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6322 	if (rval == QLA_SUCCESS) {
6323 		drv_ack |= (1 << ha->portnum);
6324 		rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6325 	}
6326 
6327 	return rval;
6328 }
6329 
6330 static int
6331 __qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
6332 {
6333 	int rval = QLA_SUCCESS;
6334 	struct qla_hw_data *ha = vha->hw;
6335 	uint32_t drv_ack;
6336 
6337 	rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6338 	if (rval == QLA_SUCCESS) {
6339 		drv_ack &= ~(1 << ha->portnum);
6340 		rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6341 	}
6342 
6343 	return rval;
6344 }
6345 
6346 static const char *
6347 qla83xx_dev_state_to_string(uint32_t dev_state)
6348 {
6349 	switch (dev_state) {
6350 	case QLA8XXX_DEV_COLD:
6351 		return "COLD/RE-INIT";
6352 	case QLA8XXX_DEV_INITIALIZING:
6353 		return "INITIALIZING";
6354 	case QLA8XXX_DEV_READY:
6355 		return "READY";
6356 	case QLA8XXX_DEV_NEED_RESET:
6357 		return "NEED RESET";
6358 	case QLA8XXX_DEV_NEED_QUIESCENT:
6359 		return "NEED QUIESCENT";
6360 	case QLA8XXX_DEV_FAILED:
6361 		return "FAILED";
6362 	case QLA8XXX_DEV_QUIESCENT:
6363 		return "QUIESCENT";
6364 	default:
6365 		return "Unknown";
6366 	}
6367 }
6368 
6369 /* Assumes idc-lock always held on entry */
6370 void
6371 qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6372 {
6373 	struct qla_hw_data *ha = vha->hw;
6374 	uint32_t idc_audit_reg = 0, duration_secs = 0;
6375 
6376 	switch (audit_type) {
6377 	case IDC_AUDIT_TIMESTAMP:
6378 		ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6379 		idc_audit_reg = (ha->portnum) |
6380 		    (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6381 		qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6382 		break;
6383 
6384 	case IDC_AUDIT_COMPLETION:
6385 		duration_secs = ((jiffies_to_msecs(jiffies) -
6386 		    jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6387 		idc_audit_reg = (ha->portnum) |
6388 		    (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6389 		qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6390 		break;
6391 
6392 	default:
6393 		ql_log(ql_log_warn, vha, 0xb078,
6394 		    "Invalid audit type specified.\n");
6395 		break;
6396 	}
6397 }
6398 
6399 /* Assumes idc_lock always held on entry */
6400 static int
6401 qla83xx_initiating_reset(scsi_qla_host_t *vha)
6402 {
6403 	struct qla_hw_data *ha = vha->hw;
6404 	uint32_t  idc_control, dev_state;
6405 
6406 	__qla83xx_get_idc_control(vha, &idc_control);
6407 	if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6408 		ql_log(ql_log_info, vha, 0xb080,
6409 		    "NIC Core reset has been disabled. idc-control=0x%x\n",
6410 		    idc_control);
6411 		return QLA_FUNCTION_FAILED;
6412 	}
6413 
6414 	/* Set NEED-RESET iff in READY state and we are the reset-owner */
6415 	qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6416 	if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6417 		qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6418 		    QLA8XXX_DEV_NEED_RESET);
6419 		ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6420 		qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6421 	} else {
6422 		const char *state = qla83xx_dev_state_to_string(dev_state);
6423 
6424 		ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6425 
6426 		/* SV: XXX: Is timeout required here? */
6427 		/* Wait for IDC state change READY -> NEED_RESET */
6428 		while (dev_state == QLA8XXX_DEV_READY) {
6429 			qla83xx_idc_unlock(vha, 0);
6430 			msleep(200);
6431 			qla83xx_idc_lock(vha, 0);
6432 			qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6433 		}
6434 	}
6435 
6436 	/* Send IDC ack by writing to drv-ack register */
6437 	__qla83xx_set_drv_ack(vha);
6438 
6439 	return QLA_SUCCESS;
6440 }
6441 
6442 int
6443 __qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6444 {
6445 	return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6446 }
6447 
6448 int
6449 __qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6450 {
6451 	return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6452 }
6453 
6454 static int
6455 qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6456 {
6457 	uint32_t drv_presence = 0;
6458 	struct qla_hw_data *ha = vha->hw;
6459 
6460 	qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6461 	if (drv_presence & (1 << ha->portnum))
6462 		return QLA_SUCCESS;
6463 	else
6464 		return QLA_TEST_FAILED;
6465 }
6466 
6467 int
6468 qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6469 {
6470 	int rval = QLA_SUCCESS;
6471 	struct qla_hw_data *ha = vha->hw;
6472 
6473 	ql_dbg(ql_dbg_p3p, vha, 0xb058,
6474 	    "Entered  %s().\n", __func__);
6475 
6476 	if (vha->device_flags & DFLG_DEV_FAILED) {
6477 		ql_log(ql_log_warn, vha, 0xb059,
6478 		    "Device in unrecoverable FAILED state.\n");
6479 		return QLA_FUNCTION_FAILED;
6480 	}
6481 
6482 	qla83xx_idc_lock(vha, 0);
6483 
6484 	if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6485 		ql_log(ql_log_warn, vha, 0xb05a,
6486 		    "Function=0x%x has been removed from IDC participation.\n",
6487 		    ha->portnum);
6488 		rval = QLA_FUNCTION_FAILED;
6489 		goto exit;
6490 	}
6491 
6492 	qla83xx_reset_ownership(vha);
6493 
6494 	rval = qla83xx_initiating_reset(vha);
6495 
6496 	/*
6497 	 * Perform reset if we are the reset-owner,
6498 	 * else wait till IDC state changes to READY/FAILED.
6499 	 */
6500 	if (rval == QLA_SUCCESS) {
6501 		rval = qla83xx_idc_state_handler(vha);
6502 
6503 		if (rval == QLA_SUCCESS)
6504 			ha->flags.nic_core_hung = 0;
6505 		__qla83xx_clear_drv_ack(vha);
6506 	}
6507 
6508 exit:
6509 	qla83xx_idc_unlock(vha, 0);
6510 
6511 	ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6512 
6513 	return rval;
6514 }
6515 
6516 int
6517 qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6518 {
6519 	struct qla_hw_data *ha = vha->hw;
6520 	int rval = QLA_FUNCTION_FAILED;
6521 
6522 	if (!IS_MCTP_CAPABLE(ha)) {
6523 		/* This message can be removed from the final version */
6524 		ql_log(ql_log_info, vha, 0x506d,
6525 		    "This board is not MCTP capable\n");
6526 		return rval;
6527 	}
6528 
6529 	if (!ha->mctp_dump) {
6530 		ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6531 		    MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6532 
6533 		if (!ha->mctp_dump) {
6534 			ql_log(ql_log_warn, vha, 0x506e,
6535 			    "Failed to allocate memory for mctp dump\n");
6536 			return rval;
6537 		}
6538 	}
6539 
6540 #define MCTP_DUMP_STR_ADDR	0x00000000
6541 	rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6542 	    MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6543 	if (rval != QLA_SUCCESS) {
6544 		ql_log(ql_log_warn, vha, 0x506f,
6545 		    "Failed to capture mctp dump\n");
6546 	} else {
6547 		ql_log(ql_log_info, vha, 0x5070,
6548 		    "Mctp dump capture for host (%ld/%p).\n",
6549 		    vha->host_no, ha->mctp_dump);
6550 		ha->mctp_dumped = 1;
6551 	}
6552 
6553 	if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
6554 		ha->flags.nic_core_reset_hdlr_active = 1;
6555 		rval = qla83xx_restart_nic_firmware(vha);
6556 		if (rval)
6557 			/* NIC Core reset failed. */
6558 			ql_log(ql_log_warn, vha, 0x5071,
6559 			    "Failed to restart nic firmware\n");
6560 		else
6561 			ql_dbg(ql_dbg_p3p, vha, 0xb084,
6562 			    "Restarted NIC firmware successfully.\n");
6563 		ha->flags.nic_core_reset_hdlr_active = 0;
6564 	}
6565 
6566 	return rval;
6567 
6568 }
6569 
6570 /*
6571 * qla2x00_quiesce_io
6572 * Description: This function will block the new I/Os
6573 *              Its not aborting any I/Os as context
6574 *              is not destroyed during quiescence
6575 * Arguments: scsi_qla_host_t
6576 * return   : void
6577 */
6578 void
6579 qla2x00_quiesce_io(scsi_qla_host_t *vha)
6580 {
6581 	struct qla_hw_data *ha = vha->hw;
6582 	struct scsi_qla_host *vp;
6583 
6584 	ql_dbg(ql_dbg_dpc, vha, 0x401d,
6585 	    "Quiescing I/O - ha=%p.\n", ha);
6586 
6587 	atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6588 	if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6589 		atomic_set(&vha->loop_state, LOOP_DOWN);
6590 		qla2x00_mark_all_devices_lost(vha, 0);
6591 		list_for_each_entry(vp, &ha->vp_list, list)
6592 			qla2x00_mark_all_devices_lost(vp, 0);
6593 	} else {
6594 		if (!atomic_read(&vha->loop_down_timer))
6595 			atomic_set(&vha->loop_down_timer,
6596 					LOOP_DOWN_TIME);
6597 	}
6598 	/* Wait for pending cmds to complete */
6599 	qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6600 }
6601 
6602 void
6603 qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6604 {
6605 	struct qla_hw_data *ha = vha->hw;
6606 	struct scsi_qla_host *vp;
6607 	unsigned long flags;
6608 	fc_port_t *fcport;
6609 	u16 i;
6610 
6611 	/* For ISP82XX, driver waits for completion of the commands.
6612 	 * online flag should be set.
6613 	 */
6614 	if (!(IS_P3P_TYPE(ha)))
6615 		vha->flags.online = 0;
6616 	ha->flags.chip_reset_done = 0;
6617 	clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6618 	vha->qla_stats.total_isp_aborts++;
6619 
6620 	ql_log(ql_log_info, vha, 0x00af,
6621 	    "Performing ISP error recovery - ha=%p.\n", ha);
6622 
6623 	ha->flags.purge_mbox = 1;
6624 	/* For ISP82XX, reset_chip is just disabling interrupts.
6625 	 * Driver waits for the completion of the commands.
6626 	 * the interrupts need to be enabled.
6627 	 */
6628 	if (!(IS_P3P_TYPE(ha)))
6629 		ha->isp_ops->reset_chip(vha);
6630 
6631 	ha->link_data_rate = PORT_SPEED_UNKNOWN;
6632 	SAVE_TOPO(ha);
6633 	ha->flags.rida_fmt2 = 0;
6634 	ha->flags.n2n_ae = 0;
6635 	ha->flags.lip_ae = 0;
6636 	ha->current_topology = 0;
6637 	ha->flags.fw_started = 0;
6638 	ha->flags.fw_init_done = 0;
6639 	ha->chip_reset++;
6640 	ha->base_qpair->chip_reset = ha->chip_reset;
6641 	for (i = 0; i < ha->max_qpairs; i++) {
6642 		if (ha->queue_pair_map[i])
6643 			ha->queue_pair_map[i]->chip_reset =
6644 				ha->base_qpair->chip_reset;
6645 	}
6646 
6647 	/* purge MBox commands */
6648 	if (atomic_read(&ha->num_pend_mbx_stage3)) {
6649 		clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
6650 		complete(&ha->mbx_intr_comp);
6651 	}
6652 
6653 	i = 0;
6654 	while (atomic_read(&ha->num_pend_mbx_stage3) ||
6655 	    atomic_read(&ha->num_pend_mbx_stage2) ||
6656 	    atomic_read(&ha->num_pend_mbx_stage1)) {
6657 		msleep(20);
6658 		i++;
6659 		if (i > 50)
6660 			break;
6661 	}
6662 	ha->flags.purge_mbox = 0;
6663 
6664 	atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6665 	if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6666 		atomic_set(&vha->loop_state, LOOP_DOWN);
6667 		qla2x00_mark_all_devices_lost(vha, 0);
6668 
6669 		spin_lock_irqsave(&ha->vport_slock, flags);
6670 		list_for_each_entry(vp, &ha->vp_list, list) {
6671 			atomic_inc(&vp->vref_count);
6672 			spin_unlock_irqrestore(&ha->vport_slock, flags);
6673 
6674 			qla2x00_mark_all_devices_lost(vp, 0);
6675 
6676 			spin_lock_irqsave(&ha->vport_slock, flags);
6677 			atomic_dec(&vp->vref_count);
6678 		}
6679 		spin_unlock_irqrestore(&ha->vport_slock, flags);
6680 	} else {
6681 		if (!atomic_read(&vha->loop_down_timer))
6682 			atomic_set(&vha->loop_down_timer,
6683 			    LOOP_DOWN_TIME);
6684 	}
6685 
6686 	/* Clear all async request states across all VPs. */
6687 	list_for_each_entry(fcport, &vha->vp_fcports, list)
6688 		fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6689 	spin_lock_irqsave(&ha->vport_slock, flags);
6690 	list_for_each_entry(vp, &ha->vp_list, list) {
6691 		atomic_inc(&vp->vref_count);
6692 		spin_unlock_irqrestore(&ha->vport_slock, flags);
6693 
6694 		list_for_each_entry(fcport, &vp->vp_fcports, list)
6695 			fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6696 
6697 		spin_lock_irqsave(&ha->vport_slock, flags);
6698 		atomic_dec(&vp->vref_count);
6699 	}
6700 	spin_unlock_irqrestore(&ha->vport_slock, flags);
6701 
6702 	if (!ha->flags.eeh_busy) {
6703 		/* Make sure for ISP 82XX IO DMA is complete */
6704 		if (IS_P3P_TYPE(ha)) {
6705 			qla82xx_chip_reset_cleanup(vha);
6706 			ql_log(ql_log_info, vha, 0x00b4,
6707 			    "Done chip reset cleanup.\n");
6708 
6709 			/* Done waiting for pending commands.
6710 			 * Reset the online flag.
6711 			 */
6712 			vha->flags.online = 0;
6713 		}
6714 
6715 		/* Requeue all commands in outstanding command list. */
6716 		qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6717 	}
6718 	/* memory barrier */
6719 	wmb();
6720 }
6721 
6722 /*
6723 *  qla2x00_abort_isp
6724 *      Resets ISP and aborts all outstanding commands.
6725 *
6726 * Input:
6727 *      ha           = adapter block pointer.
6728 *
6729 * Returns:
6730 *      0 = success
6731 */
6732 int
6733 qla2x00_abort_isp(scsi_qla_host_t *vha)
6734 {
6735 	int rval;
6736 	uint8_t        status = 0;
6737 	struct qla_hw_data *ha = vha->hw;
6738 	struct scsi_qla_host *vp;
6739 	struct req_que *req = ha->req_q_map[0];
6740 	unsigned long flags;
6741 
6742 	if (vha->flags.online) {
6743 		qla2x00_abort_isp_cleanup(vha);
6744 
6745 		if (test_and_clear_bit(ISP_ABORT_TO_ROM, &vha->dpc_flags)) {
6746 			ha->flags.chip_reset_done = 1;
6747 			vha->flags.online = 1;
6748 			status = 0;
6749 			clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6750 			return status;
6751 		}
6752 
6753 		if (IS_QLA8031(ha)) {
6754 			ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6755 			    "Clearing fcoe driver presence.\n");
6756 			if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6757 				ql_dbg(ql_dbg_p3p, vha, 0xb073,
6758 				    "Error while clearing DRV-Presence.\n");
6759 		}
6760 
6761 		if (unlikely(pci_channel_offline(ha->pdev) &&
6762 		    ha->flags.pci_channel_io_perm_failure)) {
6763 			clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6764 			status = 0;
6765 			return status;
6766 		}
6767 
6768 		switch (vha->qlini_mode) {
6769 		case QLA2XXX_INI_MODE_DISABLED:
6770 			if (!qla_tgt_mode_enabled(vha))
6771 				return 0;
6772 			break;
6773 		case QLA2XXX_INI_MODE_DUAL:
6774 			if (!qla_dual_mode_enabled(vha))
6775 				return 0;
6776 			break;
6777 		case QLA2XXX_INI_MODE_ENABLED:
6778 		default:
6779 			break;
6780 		}
6781 
6782 		ha->isp_ops->get_flash_version(vha, req->ring);
6783 
6784 		ha->isp_ops->nvram_config(vha);
6785 
6786 		if (!qla2x00_restart_isp(vha)) {
6787 			clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
6788 
6789 			if (!atomic_read(&vha->loop_down_timer)) {
6790 				/*
6791 				 * Issue marker command only when we are going
6792 				 * to start the I/O .
6793 				 */
6794 				vha->marker_needed = 1;
6795 			}
6796 
6797 			vha->flags.online = 1;
6798 
6799 			ha->isp_ops->enable_intrs(ha);
6800 
6801 			ha->isp_abort_cnt = 0;
6802 			clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6803 
6804 			if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6805 				qla2x00_get_fw_version(vha);
6806 			if (ha->fce) {
6807 				ha->flags.fce_enabled = 1;
6808 				memset(ha->fce, 0,
6809 				    fce_calc_size(ha->fce_bufs));
6810 				rval = qla2x00_enable_fce_trace(vha,
6811 				    ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6812 				    &ha->fce_bufs);
6813 				if (rval) {
6814 					ql_log(ql_log_warn, vha, 0x8033,
6815 					    "Unable to reinitialize FCE "
6816 					    "(%d).\n", rval);
6817 					ha->flags.fce_enabled = 0;
6818 				}
6819 			}
6820 
6821 			if (ha->eft) {
6822 				memset(ha->eft, 0, EFT_SIZE);
6823 				rval = qla2x00_enable_eft_trace(vha,
6824 				    ha->eft_dma, EFT_NUM_BUFFERS);
6825 				if (rval) {
6826 					ql_log(ql_log_warn, vha, 0x8034,
6827 					    "Unable to reinitialize EFT "
6828 					    "(%d).\n", rval);
6829 				}
6830 			}
6831 		} else {	/* failed the ISP abort */
6832 			vha->flags.online = 1;
6833 			if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
6834 				if (ha->isp_abort_cnt == 0) {
6835 					ql_log(ql_log_fatal, vha, 0x8035,
6836 					    "ISP error recover failed - "
6837 					    "board disabled.\n");
6838 					/*
6839 					 * The next call disables the board
6840 					 * completely.
6841 					 */
6842 					qla2x00_abort_isp_cleanup(vha);
6843 					vha->flags.online = 0;
6844 					clear_bit(ISP_ABORT_RETRY,
6845 					    &vha->dpc_flags);
6846 					status = 0;
6847 				} else { /* schedule another ISP abort */
6848 					ha->isp_abort_cnt--;
6849 					ql_dbg(ql_dbg_taskm, vha, 0x8020,
6850 					    "ISP abort - retry remaining %d.\n",
6851 					    ha->isp_abort_cnt);
6852 					status = 1;
6853 				}
6854 			} else {
6855 				ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
6856 				ql_dbg(ql_dbg_taskm, vha, 0x8021,
6857 				    "ISP error recovery - retrying (%d) "
6858 				    "more times.\n", ha->isp_abort_cnt);
6859 				set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6860 				status = 1;
6861 			}
6862 		}
6863 
6864 	}
6865 
6866 	if (!status) {
6867 		ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
6868 		qla2x00_configure_hba(vha);
6869 		spin_lock_irqsave(&ha->vport_slock, flags);
6870 		list_for_each_entry(vp, &ha->vp_list, list) {
6871 			if (vp->vp_idx) {
6872 				atomic_inc(&vp->vref_count);
6873 				spin_unlock_irqrestore(&ha->vport_slock, flags);
6874 
6875 				qla2x00_vp_abort_isp(vp);
6876 
6877 				spin_lock_irqsave(&ha->vport_slock, flags);
6878 				atomic_dec(&vp->vref_count);
6879 			}
6880 		}
6881 		spin_unlock_irqrestore(&ha->vport_slock, flags);
6882 
6883 		if (IS_QLA8031(ha)) {
6884 			ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6885 			    "Setting back fcoe driver presence.\n");
6886 			if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6887 				ql_dbg(ql_dbg_p3p, vha, 0xb074,
6888 				    "Error while setting DRV-Presence.\n");
6889 		}
6890 	} else {
6891 		ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6892 		       __func__);
6893 	}
6894 
6895 	return(status);
6896 }
6897 
6898 /*
6899 *  qla2x00_restart_isp
6900 *      restarts the ISP after a reset
6901 *
6902 * Input:
6903 *      ha = adapter block pointer.
6904 *
6905 * Returns:
6906 *      0 = success
6907 */
6908 static int
6909 qla2x00_restart_isp(scsi_qla_host_t *vha)
6910 {
6911 	int status = 0;
6912 	struct qla_hw_data *ha = vha->hw;
6913 
6914 	/* If firmware needs to be loaded */
6915 	if (qla2x00_isp_firmware(vha)) {
6916 		vha->flags.online = 0;
6917 		status = ha->isp_ops->chip_diag(vha);
6918 		if (!status)
6919 			status = qla2x00_setup_chip(vha);
6920 	}
6921 
6922 	if (!status && !(status = qla2x00_init_rings(vha))) {
6923 		clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
6924 		ha->flags.chip_reset_done = 1;
6925 
6926 		/* Initialize the queues in use */
6927 		qla25xx_init_queues(ha);
6928 
6929 		status = qla2x00_fw_ready(vha);
6930 		if (!status) {
6931 			/* Issue a marker after FW becomes ready. */
6932 			qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
6933 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6934 		}
6935 
6936 		/* if no cable then assume it's good */
6937 		if ((vha->device_flags & DFLG_NO_CABLE))
6938 			status = 0;
6939 	}
6940 	return (status);
6941 }
6942 
6943 static int
6944 qla25xx_init_queues(struct qla_hw_data *ha)
6945 {
6946 	struct rsp_que *rsp = NULL;
6947 	struct req_que *req = NULL;
6948 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6949 	int ret = -1;
6950 	int i;
6951 
6952 	for (i = 1; i < ha->max_rsp_queues; i++) {
6953 		rsp = ha->rsp_q_map[i];
6954 		if (rsp && test_bit(i, ha->rsp_qid_map)) {
6955 			rsp->options &= ~BIT_0;
6956 			ret = qla25xx_init_rsp_que(base_vha, rsp);
6957 			if (ret != QLA_SUCCESS)
6958 				ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6959 				    "%s Rsp que: %d init failed.\n",
6960 				    __func__, rsp->id);
6961 			else
6962 				ql_dbg(ql_dbg_init, base_vha, 0x0100,
6963 				    "%s Rsp que: %d inited.\n",
6964 				    __func__, rsp->id);
6965 		}
6966 	}
6967 	for (i = 1; i < ha->max_req_queues; i++) {
6968 		req = ha->req_q_map[i];
6969 		if (req && test_bit(i, ha->req_qid_map)) {
6970 			/* Clear outstanding commands array. */
6971 			req->options &= ~BIT_0;
6972 			ret = qla25xx_init_req_que(base_vha, req);
6973 			if (ret != QLA_SUCCESS)
6974 				ql_dbg(ql_dbg_init, base_vha, 0x0101,
6975 				    "%s Req que: %d init failed.\n",
6976 				    __func__, req->id);
6977 			else
6978 				ql_dbg(ql_dbg_init, base_vha, 0x0102,
6979 				    "%s Req que: %d inited.\n",
6980 				    __func__, req->id);
6981 		}
6982 	}
6983 	return ret;
6984 }
6985 
6986 /*
6987 * qla2x00_reset_adapter
6988 *      Reset adapter.
6989 *
6990 * Input:
6991 *      ha = adapter block pointer.
6992 */
6993 int
6994 qla2x00_reset_adapter(scsi_qla_host_t *vha)
6995 {
6996 	unsigned long flags = 0;
6997 	struct qla_hw_data *ha = vha->hw;
6998 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
6999 
7000 	vha->flags.online = 0;
7001 	ha->isp_ops->disable_intrs(ha);
7002 
7003 	spin_lock_irqsave(&ha->hardware_lock, flags);
7004 	WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
7005 	RD_REG_WORD(&reg->hccr);			/* PCI Posting. */
7006 	WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
7007 	RD_REG_WORD(&reg->hccr);			/* PCI Posting. */
7008 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7009 
7010 	return QLA_SUCCESS;
7011 }
7012 
7013 int
7014 qla24xx_reset_adapter(scsi_qla_host_t *vha)
7015 {
7016 	unsigned long flags = 0;
7017 	struct qla_hw_data *ha = vha->hw;
7018 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
7019 	int rval = QLA_SUCCESS;
7020 
7021 	if (IS_P3P_TYPE(ha))
7022 		return rval;
7023 
7024 	vha->flags.online = 0;
7025 	ha->isp_ops->disable_intrs(ha);
7026 
7027 	spin_lock_irqsave(&ha->hardware_lock, flags);
7028 	WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
7029 	RD_REG_DWORD(&reg->hccr);
7030 	WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
7031 	RD_REG_DWORD(&reg->hccr);
7032 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7033 
7034 	if (IS_NOPOLLING_TYPE(ha))
7035 		ha->isp_ops->enable_intrs(ha);
7036 
7037 	return rval;
7038 }
7039 
7040 /* On sparc systems, obtain port and node WWN from firmware
7041  * properties.
7042  */
7043 static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
7044 	struct nvram_24xx *nv)
7045 {
7046 #ifdef CONFIG_SPARC
7047 	struct qla_hw_data *ha = vha->hw;
7048 	struct pci_dev *pdev = ha->pdev;
7049 	struct device_node *dp = pci_device_to_OF_node(pdev);
7050 	const u8 *val;
7051 	int len;
7052 
7053 	val = of_get_property(dp, "port-wwn", &len);
7054 	if (val && len >= WWN_SIZE)
7055 		memcpy(nv->port_name, val, WWN_SIZE);
7056 
7057 	val = of_get_property(dp, "node-wwn", &len);
7058 	if (val && len >= WWN_SIZE)
7059 		memcpy(nv->node_name, val, WWN_SIZE);
7060 #endif
7061 }
7062 
7063 int
7064 qla24xx_nvram_config(scsi_qla_host_t *vha)
7065 {
7066 	int   rval;
7067 	struct init_cb_24xx *icb;
7068 	struct nvram_24xx *nv;
7069 	uint32_t *dptr;
7070 	uint8_t  *dptr1, *dptr2;
7071 	uint32_t chksum;
7072 	uint16_t cnt;
7073 	struct qla_hw_data *ha = vha->hw;
7074 
7075 	rval = QLA_SUCCESS;
7076 	icb = (struct init_cb_24xx *)ha->init_cb;
7077 	nv = ha->nvram;
7078 
7079 	/* Determine NVRAM starting address. */
7080 	if (ha->port_no == 0) {
7081 		ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
7082 		ha->vpd_base = FA_NVRAM_VPD0_ADDR;
7083 	} else {
7084 		ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
7085 		ha->vpd_base = FA_NVRAM_VPD1_ADDR;
7086 	}
7087 
7088 	ha->nvram_size = sizeof(*nv);
7089 	ha->vpd_size = FA_NVRAM_VPD_SIZE;
7090 
7091 	/* Get VPD data into cache */
7092 	ha->vpd = ha->nvram + VPD_OFFSET;
7093 	ha->isp_ops->read_nvram(vha, ha->vpd,
7094 	    ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
7095 
7096 	/* Get NVRAM data into cache and calculate checksum. */
7097 	dptr = (uint32_t *)nv;
7098 	ha->isp_ops->read_nvram(vha, dptr, ha->nvram_base, ha->nvram_size);
7099 	for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7100 		chksum += le32_to_cpu(*dptr);
7101 
7102 	ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
7103 	    "Contents of NVRAM\n");
7104 	ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
7105 	    nv, ha->nvram_size);
7106 
7107 	/* Bad NVRAM data, set defaults parameters. */
7108 	if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
7109 	    le16_to_cpu(nv->nvram_version) < ICB_VERSION) {
7110 		/* Reset NVRAM data. */
7111 		ql_log(ql_log_warn, vha, 0x006b,
7112 		    "Inconsistent NVRAM checksum=%#x id=%.4s version=%#x.\n",
7113 		    chksum, nv->id, nv->nvram_version);
7114 		ql_dump_buffer(ql_dbg_init, vha, 0x006b, nv, sizeof(*nv));
7115 		ql_log(ql_log_warn, vha, 0x006c,
7116 		    "Falling back to functioning (yet invalid -- WWPN) "
7117 		    "defaults.\n");
7118 
7119 		/*
7120 		 * Set default initialization control block.
7121 		 */
7122 		memset(nv, 0, ha->nvram_size);
7123 		nv->nvram_version = cpu_to_le16(ICB_VERSION);
7124 		nv->version = cpu_to_le16(ICB_VERSION);
7125 		nv->frame_payload_size = 2048;
7126 		nv->execution_throttle = cpu_to_le16(0xFFFF);
7127 		nv->exchange_count = cpu_to_le16(0);
7128 		nv->hard_address = cpu_to_le16(124);
7129 		nv->port_name[0] = 0x21;
7130 		nv->port_name[1] = 0x00 + ha->port_no + 1;
7131 		nv->port_name[2] = 0x00;
7132 		nv->port_name[3] = 0xe0;
7133 		nv->port_name[4] = 0x8b;
7134 		nv->port_name[5] = 0x1c;
7135 		nv->port_name[6] = 0x55;
7136 		nv->port_name[7] = 0x86;
7137 		nv->node_name[0] = 0x20;
7138 		nv->node_name[1] = 0x00;
7139 		nv->node_name[2] = 0x00;
7140 		nv->node_name[3] = 0xe0;
7141 		nv->node_name[4] = 0x8b;
7142 		nv->node_name[5] = 0x1c;
7143 		nv->node_name[6] = 0x55;
7144 		nv->node_name[7] = 0x86;
7145 		qla24xx_nvram_wwn_from_ofw(vha, nv);
7146 		nv->login_retry_count = cpu_to_le16(8);
7147 		nv->interrupt_delay_timer = cpu_to_le16(0);
7148 		nv->login_timeout = cpu_to_le16(0);
7149 		nv->firmware_options_1 =
7150 		    cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7151 		nv->firmware_options_2 = cpu_to_le32(2 << 4);
7152 		nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7153 		nv->firmware_options_3 = cpu_to_le32(2 << 13);
7154 		nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7155 		nv->efi_parameters = cpu_to_le32(0);
7156 		nv->reset_delay = 5;
7157 		nv->max_luns_per_target = cpu_to_le16(128);
7158 		nv->port_down_retry_count = cpu_to_le16(30);
7159 		nv->link_down_timeout = cpu_to_le16(30);
7160 
7161 		rval = 1;
7162 	}
7163 
7164 	if (qla_tgt_mode_enabled(vha)) {
7165 		/* Don't enable full login after initial LIP */
7166 		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7167 		/* Don't enable LIP full login for initiator */
7168 		nv->host_p &= cpu_to_le32(~BIT_10);
7169 	}
7170 
7171 	qlt_24xx_config_nvram_stage1(vha, nv);
7172 
7173 	/* Reset Initialization control block */
7174 	memset(icb, 0, ha->init_cb_size);
7175 
7176 	/* Copy 1st segment. */
7177 	dptr1 = (uint8_t *)icb;
7178 	dptr2 = (uint8_t *)&nv->version;
7179 	cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7180 	while (cnt--)
7181 		*dptr1++ = *dptr2++;
7182 
7183 	icb->login_retry_count = nv->login_retry_count;
7184 	icb->link_down_on_nos = nv->link_down_on_nos;
7185 
7186 	/* Copy 2nd segment. */
7187 	dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7188 	dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7189 	cnt = (uint8_t *)&icb->reserved_3 -
7190 	    (uint8_t *)&icb->interrupt_delay_timer;
7191 	while (cnt--)
7192 		*dptr1++ = *dptr2++;
7193 	ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
7194 	/*
7195 	 * Setup driver NVRAM options.
7196 	 */
7197 	qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
7198 	    "QLA2462");
7199 
7200 	qlt_24xx_config_nvram_stage2(vha, icb);
7201 
7202 	if (nv->host_p & cpu_to_le32(BIT_15)) {
7203 		/* Use alternate WWN? */
7204 		memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7205 		memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7206 	}
7207 
7208 	/* Prepare nodename */
7209 	if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
7210 		/*
7211 		 * Firmware will apply the following mask if the nodename was
7212 		 * not provided.
7213 		 */
7214 		memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7215 		icb->node_name[0] &= 0xF0;
7216 	}
7217 
7218 	/* Set host adapter parameters. */
7219 	ha->flags.disable_risc_code_load = 0;
7220 	ha->flags.enable_lip_reset = 0;
7221 	ha->flags.enable_lip_full_login =
7222 	    le32_to_cpu(nv->host_p) & BIT_10 ? 1 : 0;
7223 	ha->flags.enable_target_reset =
7224 	    le32_to_cpu(nv->host_p) & BIT_11 ? 1 : 0;
7225 	ha->flags.enable_led_scheme = 0;
7226 	ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1 : 0;
7227 
7228 	ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7229 	    (BIT_6 | BIT_5 | BIT_4)) >> 4;
7230 
7231 	memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
7232 	    sizeof(ha->fw_seriallink_options24));
7233 
7234 	/* save HBA serial number */
7235 	ha->serial0 = icb->port_name[5];
7236 	ha->serial1 = icb->port_name[6];
7237 	ha->serial2 = icb->port_name[7];
7238 	memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7239 	memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7240 
7241 	icb->execution_throttle = cpu_to_le16(0xFFFF);
7242 
7243 	ha->retry_count = le16_to_cpu(nv->login_retry_count);
7244 
7245 	/* Set minimum login_timeout to 4 seconds. */
7246 	if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7247 		nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7248 	if (le16_to_cpu(nv->login_timeout) < 4)
7249 		nv->login_timeout = cpu_to_le16(4);
7250 	ha->login_timeout = le16_to_cpu(nv->login_timeout);
7251 
7252 	/* Set minimum RATOV to 100 tenths of a second. */
7253 	ha->r_a_tov = 100;
7254 
7255 	ha->loop_reset_delay = nv->reset_delay;
7256 
7257 	/* Link Down Timeout = 0:
7258 	 *
7259 	 * 	When Port Down timer expires we will start returning
7260 	 *	I/O's to OS with "DID_NO_CONNECT".
7261 	 *
7262 	 * Link Down Timeout != 0:
7263 	 *
7264 	 *	 The driver waits for the link to come up after link down
7265 	 *	 before returning I/Os to OS with "DID_NO_CONNECT".
7266 	 */
7267 	if (le16_to_cpu(nv->link_down_timeout) == 0) {
7268 		ha->loop_down_abort_time =
7269 		    (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7270 	} else {
7271 		ha->link_down_timeout =	le16_to_cpu(nv->link_down_timeout);
7272 		ha->loop_down_abort_time =
7273 		    (LOOP_DOWN_TIME - ha->link_down_timeout);
7274 	}
7275 
7276 	/* Need enough time to try and get the port back. */
7277 	ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7278 	if (qlport_down_retry)
7279 		ha->port_down_retry_count = qlport_down_retry;
7280 
7281 	/* Set login_retry_count */
7282 	ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
7283 	if (ha->port_down_retry_count ==
7284 	    le16_to_cpu(nv->port_down_retry_count) &&
7285 	    ha->port_down_retry_count > 3)
7286 		ha->login_retry_count = ha->port_down_retry_count;
7287 	else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7288 		ha->login_retry_count = ha->port_down_retry_count;
7289 	if (ql2xloginretrycount)
7290 		ha->login_retry_count = ql2xloginretrycount;
7291 
7292 	/* N2N: driver will initiate Login instead of FW */
7293 	icb->firmware_options_3 |= BIT_8;
7294 
7295 	/* Enable ZIO. */
7296 	if (!vha->flags.init_done) {
7297 		ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7298 		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7299 		ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7300 		    le16_to_cpu(icb->interrupt_delay_timer) : 2;
7301 	}
7302 	icb->firmware_options_2 &= cpu_to_le32(
7303 	    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7304 	if (ha->zio_mode != QLA_ZIO_DISABLED) {
7305 		ha->zio_mode = QLA_ZIO_MODE_6;
7306 
7307 		ql_log(ql_log_info, vha, 0x006f,
7308 		    "ZIO mode %d enabled; timer delay (%d us).\n",
7309 		    ha->zio_mode, ha->zio_timer * 100);
7310 
7311 		icb->firmware_options_2 |= cpu_to_le32(
7312 		    (uint32_t)ha->zio_mode);
7313 		icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7314 	}
7315 
7316 	if (rval) {
7317 		ql_log(ql_log_warn, vha, 0x0070,
7318 		    "NVRAM configuration failed.\n");
7319 	}
7320 	return (rval);
7321 }
7322 
7323 static void
7324 qla27xx_print_image(struct scsi_qla_host *vha, char *name,
7325     struct qla27xx_image_status *image_status)
7326 {
7327 	ql_dbg(ql_dbg_init, vha, 0x018b,
7328 	    "%s %s: mask=%#02x gen=%#04x ver=%u.%u map=%#01x sum=%#08x sig=%#08x\n",
7329 	    name, "status",
7330 	    image_status->image_status_mask,
7331 	    le16_to_cpu(image_status->generation),
7332 	    image_status->ver_major,
7333 	    image_status->ver_minor,
7334 	    image_status->bitmap,
7335 	    le32_to_cpu(image_status->checksum),
7336 	    le32_to_cpu(image_status->signature));
7337 }
7338 
7339 static bool
7340 qla28xx_check_aux_image_status_signature(
7341     struct qla27xx_image_status *image_status)
7342 {
7343 	ulong signature = le32_to_cpu(image_status->signature);
7344 
7345 	return signature != QLA28XX_AUX_IMG_STATUS_SIGN;
7346 }
7347 
7348 static bool
7349 qla27xx_check_image_status_signature(struct qla27xx_image_status *image_status)
7350 {
7351 	ulong signature = le32_to_cpu(image_status->signature);
7352 
7353 	return
7354 	    signature != QLA27XX_IMG_STATUS_SIGN &&
7355 	    signature != QLA28XX_IMG_STATUS_SIGN;
7356 }
7357 
7358 static ulong
7359 qla27xx_image_status_checksum(struct qla27xx_image_status *image_status)
7360 {
7361 	uint32_t *p = (void *)image_status;
7362 	uint n = sizeof(*image_status) / sizeof(*p);
7363 	uint32_t sum = 0;
7364 
7365 	for ( ; n--; p++)
7366 		sum += le32_to_cpup(p);
7367 
7368 	return sum;
7369 }
7370 
7371 static inline uint
7372 qla28xx_component_bitmask(struct qla27xx_image_status *aux, uint bitmask)
7373 {
7374 	return aux->bitmap & bitmask ?
7375 	    QLA27XX_SECONDARY_IMAGE : QLA27XX_PRIMARY_IMAGE;
7376 }
7377 
7378 static void
7379 qla28xx_component_status(
7380     struct active_regions *active_regions, struct qla27xx_image_status *aux)
7381 {
7382 	active_regions->aux.board_config =
7383 	    qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_BOARD_CONFIG);
7384 
7385 	active_regions->aux.vpd_nvram =
7386 	    qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_VPD_NVRAM);
7387 
7388 	active_regions->aux.npiv_config_0_1 =
7389 	    qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_NPIV_CONFIG_0_1);
7390 
7391 	active_regions->aux.npiv_config_2_3 =
7392 	    qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_NPIV_CONFIG_2_3);
7393 }
7394 
7395 static int
7396 qla27xx_compare_image_generation(
7397     struct qla27xx_image_status *pri_image_status,
7398     struct qla27xx_image_status *sec_image_status)
7399 {
7400 	/* calculate generation delta as uint16 (this accounts for wrap) */
7401 	int16_t delta =
7402 	    le16_to_cpu(pri_image_status->generation) -
7403 	    le16_to_cpu(sec_image_status->generation);
7404 
7405 	ql_dbg(ql_dbg_init, NULL, 0x0180, "generation delta = %d\n", delta);
7406 
7407 	return delta;
7408 }
7409 
7410 void
7411 qla28xx_get_aux_images(
7412 	struct scsi_qla_host *vha, struct active_regions *active_regions)
7413 {
7414 	struct qla_hw_data *ha = vha->hw;
7415 	struct qla27xx_image_status pri_aux_image_status, sec_aux_image_status;
7416 	bool valid_pri_image = false, valid_sec_image = false;
7417 	bool active_pri_image = false, active_sec_image = false;
7418 
7419 	if (!ha->flt_region_aux_img_status_pri) {
7420 		ql_dbg(ql_dbg_init, vha, 0x018a, "Primary aux image not addressed\n");
7421 		goto check_sec_image;
7422 	}
7423 
7424 	qla24xx_read_flash_data(vha, (void *)&pri_aux_image_status,
7425 	    ha->flt_region_aux_img_status_pri,
7426 	    sizeof(pri_aux_image_status) >> 2);
7427 	qla27xx_print_image(vha, "Primary aux image", &pri_aux_image_status);
7428 
7429 	if (qla28xx_check_aux_image_status_signature(&pri_aux_image_status)) {
7430 		ql_dbg(ql_dbg_init, vha, 0x018b,
7431 		    "Primary aux image signature (%#x) not valid\n",
7432 		    le32_to_cpu(pri_aux_image_status.signature));
7433 		goto check_sec_image;
7434 	}
7435 
7436 	if (qla27xx_image_status_checksum(&pri_aux_image_status)) {
7437 		ql_dbg(ql_dbg_init, vha, 0x018c,
7438 		    "Primary aux image checksum failed\n");
7439 		goto check_sec_image;
7440 	}
7441 
7442 	valid_pri_image = true;
7443 
7444 	if (pri_aux_image_status.image_status_mask & 1) {
7445 		ql_dbg(ql_dbg_init, vha, 0x018d,
7446 		    "Primary aux image is active\n");
7447 		active_pri_image = true;
7448 	}
7449 
7450 check_sec_image:
7451 	if (!ha->flt_region_aux_img_status_sec) {
7452 		ql_dbg(ql_dbg_init, vha, 0x018a,
7453 		    "Secondary aux image not addressed\n");
7454 		goto check_valid_image;
7455 	}
7456 
7457 	qla24xx_read_flash_data(vha, (void *)&sec_aux_image_status,
7458 	    ha->flt_region_aux_img_status_sec,
7459 	    sizeof(sec_aux_image_status) >> 2);
7460 	qla27xx_print_image(vha, "Secondary aux image", &sec_aux_image_status);
7461 
7462 	if (qla28xx_check_aux_image_status_signature(&sec_aux_image_status)) {
7463 		ql_dbg(ql_dbg_init, vha, 0x018b,
7464 		    "Secondary aux image signature (%#x) not valid\n",
7465 		    le32_to_cpu(sec_aux_image_status.signature));
7466 		goto check_valid_image;
7467 	}
7468 
7469 	if (qla27xx_image_status_checksum(&sec_aux_image_status)) {
7470 		ql_dbg(ql_dbg_init, vha, 0x018c,
7471 		    "Secondary aux image checksum failed\n");
7472 		goto check_valid_image;
7473 	}
7474 
7475 	valid_sec_image = true;
7476 
7477 	if (sec_aux_image_status.image_status_mask & 1) {
7478 		ql_dbg(ql_dbg_init, vha, 0x018d,
7479 		    "Secondary aux image is active\n");
7480 		active_sec_image = true;
7481 	}
7482 
7483 check_valid_image:
7484 	if (valid_pri_image && active_pri_image &&
7485 	    valid_sec_image && active_sec_image) {
7486 		if (qla27xx_compare_image_generation(&pri_aux_image_status,
7487 		    &sec_aux_image_status) >= 0) {
7488 			qla28xx_component_status(active_regions,
7489 			    &pri_aux_image_status);
7490 		} else {
7491 			qla28xx_component_status(active_regions,
7492 			    &sec_aux_image_status);
7493 		}
7494 	} else if (valid_pri_image && active_pri_image) {
7495 		qla28xx_component_status(active_regions, &pri_aux_image_status);
7496 	} else if (valid_sec_image && active_sec_image) {
7497 		qla28xx_component_status(active_regions, &sec_aux_image_status);
7498 	}
7499 
7500 	ql_dbg(ql_dbg_init, vha, 0x018f,
7501 	    "aux images active: BCFG=%u VPD/NVR=%u NPIV0/1=%u NPIV2/3=%u\n",
7502 	    active_regions->aux.board_config,
7503 	    active_regions->aux.vpd_nvram,
7504 	    active_regions->aux.npiv_config_0_1,
7505 	    active_regions->aux.npiv_config_2_3);
7506 }
7507 
7508 void
7509 qla27xx_get_active_image(struct scsi_qla_host *vha,
7510     struct active_regions *active_regions)
7511 {
7512 	struct qla_hw_data *ha = vha->hw;
7513 	struct qla27xx_image_status pri_image_status, sec_image_status;
7514 	bool valid_pri_image = false, valid_sec_image = false;
7515 	bool active_pri_image = false, active_sec_image = false;
7516 
7517 	if (!ha->flt_region_img_status_pri) {
7518 		ql_dbg(ql_dbg_init, vha, 0x018a, "Primary image not addressed\n");
7519 		goto check_sec_image;
7520 	}
7521 
7522 	qla24xx_read_flash_data(vha, (void *)(&pri_image_status),
7523 	    ha->flt_region_img_status_pri, sizeof(pri_image_status) >> 2);
7524 	qla27xx_print_image(vha, "Primary image", &pri_image_status);
7525 
7526 	if (qla27xx_check_image_status_signature(&pri_image_status)) {
7527 		ql_dbg(ql_dbg_init, vha, 0x018b,
7528 		    "Primary image signature (%#x) not valid\n",
7529 		    le32_to_cpu(pri_image_status.signature));
7530 		goto check_sec_image;
7531 	}
7532 
7533 	if (qla27xx_image_status_checksum(&pri_image_status)) {
7534 		ql_dbg(ql_dbg_init, vha, 0x018c,
7535 		    "Primary image checksum failed\n");
7536 		goto check_sec_image;
7537 	}
7538 
7539 	valid_pri_image = true;
7540 
7541 	if (pri_image_status.image_status_mask & 1) {
7542 		ql_dbg(ql_dbg_init, vha, 0x018d,
7543 		    "Primary image is active\n");
7544 		active_pri_image = true;
7545 	}
7546 
7547 check_sec_image:
7548 	if (!ha->flt_region_img_status_sec) {
7549 		ql_dbg(ql_dbg_init, vha, 0x018a, "Secondary image not addressed\n");
7550 		goto check_valid_image;
7551 	}
7552 
7553 	qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
7554 	    ha->flt_region_img_status_sec, sizeof(sec_image_status) >> 2);
7555 	qla27xx_print_image(vha, "Secondary image", &sec_image_status);
7556 
7557 	if (qla27xx_check_image_status_signature(&sec_image_status)) {
7558 		ql_dbg(ql_dbg_init, vha, 0x018b,
7559 		    "Secondary image signature (%#x) not valid\n",
7560 		    le32_to_cpu(sec_image_status.signature));
7561 		goto check_valid_image;
7562 	}
7563 
7564 	if (qla27xx_image_status_checksum(&sec_image_status)) {
7565 		ql_dbg(ql_dbg_init, vha, 0x018c,
7566 		    "Secondary image checksum failed\n");
7567 		goto check_valid_image;
7568 	}
7569 
7570 	valid_sec_image = true;
7571 
7572 	if (sec_image_status.image_status_mask & 1) {
7573 		ql_dbg(ql_dbg_init, vha, 0x018d,
7574 		    "Secondary image is active\n");
7575 		active_sec_image = true;
7576 	}
7577 
7578 check_valid_image:
7579 	if (valid_pri_image && active_pri_image)
7580 		active_regions->global = QLA27XX_PRIMARY_IMAGE;
7581 
7582 	if (valid_sec_image && active_sec_image) {
7583 		if (!active_regions->global ||
7584 		    qla27xx_compare_image_generation(
7585 			&pri_image_status, &sec_image_status) < 0) {
7586 			active_regions->global = QLA27XX_SECONDARY_IMAGE;
7587 		}
7588 	}
7589 
7590 	ql_dbg(ql_dbg_init, vha, 0x018f, "active image %s (%u)\n",
7591 	    active_regions->global == QLA27XX_DEFAULT_IMAGE ?
7592 		"default (boot/fw)" :
7593 	    active_regions->global == QLA27XX_PRIMARY_IMAGE ?
7594 		"primary" :
7595 	    active_regions->global == QLA27XX_SECONDARY_IMAGE ?
7596 		"secondary" : "invalid",
7597 	    active_regions->global);
7598 }
7599 
7600 bool qla24xx_risc_firmware_invalid(uint32_t *dword)
7601 {
7602 	return
7603 	    !(dword[4] | dword[5] | dword[6] | dword[7]) ||
7604 	    !(~dword[4] | ~dword[5] | ~dword[6] | ~dword[7]);
7605 }
7606 
7607 static int
7608 qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7609     uint32_t faddr)
7610 {
7611 	int rval;
7612 	uint templates, segments, fragment;
7613 	ulong i;
7614 	uint j;
7615 	ulong dlen;
7616 	uint32_t *dcode;
7617 	uint32_t risc_addr, risc_size, risc_attr = 0;
7618 	struct qla_hw_data *ha = vha->hw;
7619 	struct req_que *req = ha->req_q_map[0];
7620 	struct fwdt *fwdt = ha->fwdt;
7621 
7622 	ql_dbg(ql_dbg_init, vha, 0x008b,
7623 	    "FW: Loading firmware from flash (%x).\n", faddr);
7624 
7625 	dcode = (void *)req->ring;
7626 	qla24xx_read_flash_data(vha, dcode, faddr, 8);
7627 	if (qla24xx_risc_firmware_invalid(dcode)) {
7628 		ql_log(ql_log_fatal, vha, 0x008c,
7629 		    "Unable to verify the integrity of flash firmware "
7630 		    "image.\n");
7631 		ql_log(ql_log_fatal, vha, 0x008d,
7632 		    "Firmware data: %08x %08x %08x %08x.\n",
7633 		    dcode[0], dcode[1], dcode[2], dcode[3]);
7634 
7635 		return QLA_FUNCTION_FAILED;
7636 	}
7637 
7638 	dcode = (void *)req->ring;
7639 	*srisc_addr = 0;
7640 	segments = FA_RISC_CODE_SEGMENTS;
7641 	for (j = 0; j < segments; j++) {
7642 		ql_dbg(ql_dbg_init, vha, 0x008d,
7643 		    "-> Loading segment %u...\n", j);
7644 		qla24xx_read_flash_data(vha, dcode, faddr, 10);
7645 		risc_addr = be32_to_cpu(dcode[2]);
7646 		risc_size = be32_to_cpu(dcode[3]);
7647 		if (!*srisc_addr) {
7648 			*srisc_addr = risc_addr;
7649 			risc_attr = be32_to_cpu(dcode[9]);
7650 		}
7651 
7652 		dlen = ha->fw_transfer_size >> 2;
7653 		for (fragment = 0; risc_size; fragment++) {
7654 			if (dlen > risc_size)
7655 				dlen = risc_size;
7656 
7657 			ql_dbg(ql_dbg_init, vha, 0x008e,
7658 			    "-> Loading fragment %u: %#x <- %#x (%#lx dwords)...\n",
7659 			    fragment, risc_addr, faddr, dlen);
7660 			qla24xx_read_flash_data(vha, dcode, faddr, dlen);
7661 			for (i = 0; i < dlen; i++)
7662 				dcode[i] = swab32(dcode[i]);
7663 
7664 			rval = qla2x00_load_ram(vha, req->dma, risc_addr, dlen);
7665 			if (rval) {
7666 				ql_log(ql_log_fatal, vha, 0x008f,
7667 				    "-> Failed load firmware fragment %u.\n",
7668 				    fragment);
7669 				return QLA_FUNCTION_FAILED;
7670 			}
7671 
7672 			faddr += dlen;
7673 			risc_addr += dlen;
7674 			risc_size -= dlen;
7675 		}
7676 	}
7677 
7678 	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
7679 		return QLA_SUCCESS;
7680 
7681 	templates = (risc_attr & BIT_9) ? 2 : 1;
7682 	ql_dbg(ql_dbg_init, vha, 0x0160, "-> templates = %u\n", templates);
7683 	for (j = 0; j < templates; j++, fwdt++) {
7684 		if (fwdt->template)
7685 			vfree(fwdt->template);
7686 		fwdt->template = NULL;
7687 		fwdt->length = 0;
7688 
7689 		dcode = (void *)req->ring;
7690 		qla24xx_read_flash_data(vha, dcode, faddr, 7);
7691 		risc_size = be32_to_cpu(dcode[2]);
7692 		ql_dbg(ql_dbg_init, vha, 0x0161,
7693 		    "-> fwdt%u template array at %#x (%#x dwords)\n",
7694 		    j, faddr, risc_size);
7695 		if (!risc_size || !~risc_size) {
7696 			ql_dbg(ql_dbg_init, vha, 0x0162,
7697 			    "-> fwdt%u failed to read array\n", j);
7698 			goto failed;
7699 		}
7700 
7701 		/* skip header and ignore checksum */
7702 		faddr += 7;
7703 		risc_size -= 8;
7704 
7705 		ql_dbg(ql_dbg_init, vha, 0x0163,
7706 		    "-> fwdt%u template allocate template %#x words...\n",
7707 		    j, risc_size);
7708 		fwdt->template = vmalloc(risc_size * sizeof(*dcode));
7709 		if (!fwdt->template) {
7710 			ql_log(ql_log_warn, vha, 0x0164,
7711 			    "-> fwdt%u failed allocate template.\n", j);
7712 			goto failed;
7713 		}
7714 
7715 		dcode = fwdt->template;
7716 		qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7717 
7718 		if (!qla27xx_fwdt_template_valid(dcode)) {
7719 			ql_log(ql_log_warn, vha, 0x0165,
7720 			    "-> fwdt%u failed template validate\n", j);
7721 			goto failed;
7722 		}
7723 
7724 		dlen = qla27xx_fwdt_template_size(dcode);
7725 		ql_dbg(ql_dbg_init, vha, 0x0166,
7726 		    "-> fwdt%u template size %#lx bytes (%#lx words)\n",
7727 		    j, dlen, dlen / sizeof(*dcode));
7728 		if (dlen > risc_size * sizeof(*dcode)) {
7729 			ql_log(ql_log_warn, vha, 0x0167,
7730 			    "-> fwdt%u template exceeds array (%-lu bytes)\n",
7731 			    j, dlen - risc_size * sizeof(*dcode));
7732 			goto failed;
7733 		}
7734 
7735 		fwdt->length = dlen;
7736 		ql_dbg(ql_dbg_init, vha, 0x0168,
7737 		    "-> fwdt%u loaded template ok\n", j);
7738 
7739 		faddr += risc_size + 1;
7740 	}
7741 
7742 	return QLA_SUCCESS;
7743 
7744 failed:
7745 	if (fwdt->template)
7746 		vfree(fwdt->template);
7747 	fwdt->template = NULL;
7748 	fwdt->length = 0;
7749 
7750 	return QLA_SUCCESS;
7751 }
7752 
7753 #define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
7754 
7755 int
7756 qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7757 {
7758 	int	rval;
7759 	int	i, fragment;
7760 	uint16_t *wcode, *fwcode;
7761 	uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7762 	struct fw_blob *blob;
7763 	struct qla_hw_data *ha = vha->hw;
7764 	struct req_que *req = ha->req_q_map[0];
7765 
7766 	/* Load firmware blob. */
7767 	blob = qla2x00_request_firmware(vha);
7768 	if (!blob) {
7769 		ql_log(ql_log_info, vha, 0x0083,
7770 		    "Firmware image unavailable.\n");
7771 		ql_log(ql_log_info, vha, 0x0084,
7772 		    "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
7773 		return QLA_FUNCTION_FAILED;
7774 	}
7775 
7776 	rval = QLA_SUCCESS;
7777 
7778 	wcode = (uint16_t *)req->ring;
7779 	*srisc_addr = 0;
7780 	fwcode = (uint16_t *)blob->fw->data;
7781 	fwclen = 0;
7782 
7783 	/* Validate firmware image by checking version. */
7784 	if (blob->fw->size < 8 * sizeof(uint16_t)) {
7785 		ql_log(ql_log_fatal, vha, 0x0085,
7786 		    "Unable to verify integrity of firmware image (%zd).\n",
7787 		    blob->fw->size);
7788 		goto fail_fw_integrity;
7789 	}
7790 	for (i = 0; i < 4; i++)
7791 		wcode[i] = be16_to_cpu(fwcode[i + 4]);
7792 	if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7793 	    wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7794 		wcode[2] == 0 && wcode[3] == 0)) {
7795 		ql_log(ql_log_fatal, vha, 0x0086,
7796 		    "Unable to verify integrity of firmware image.\n");
7797 		ql_log(ql_log_fatal, vha, 0x0087,
7798 		    "Firmware data: %04x %04x %04x %04x.\n",
7799 		    wcode[0], wcode[1], wcode[2], wcode[3]);
7800 		goto fail_fw_integrity;
7801 	}
7802 
7803 	seg = blob->segs;
7804 	while (*seg && rval == QLA_SUCCESS) {
7805 		risc_addr = *seg;
7806 		*srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7807 		risc_size = be16_to_cpu(fwcode[3]);
7808 
7809 		/* Validate firmware image size. */
7810 		fwclen += risc_size * sizeof(uint16_t);
7811 		if (blob->fw->size < fwclen) {
7812 			ql_log(ql_log_fatal, vha, 0x0088,
7813 			    "Unable to verify integrity of firmware image "
7814 			    "(%zd).\n", blob->fw->size);
7815 			goto fail_fw_integrity;
7816 		}
7817 
7818 		fragment = 0;
7819 		while (risc_size > 0 && rval == QLA_SUCCESS) {
7820 			wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7821 			if (wlen > risc_size)
7822 				wlen = risc_size;
7823 			ql_dbg(ql_dbg_init, vha, 0x0089,
7824 			    "Loading risc segment@ risc addr %x number of "
7825 			    "words 0x%x.\n", risc_addr, wlen);
7826 
7827 			for (i = 0; i < wlen; i++)
7828 				wcode[i] = swab16(fwcode[i]);
7829 
7830 			rval = qla2x00_load_ram(vha, req->dma, risc_addr,
7831 			    wlen);
7832 			if (rval) {
7833 				ql_log(ql_log_fatal, vha, 0x008a,
7834 				    "Failed to load segment %d of firmware.\n",
7835 				    fragment);
7836 				break;
7837 			}
7838 
7839 			fwcode += wlen;
7840 			risc_addr += wlen;
7841 			risc_size -= wlen;
7842 			fragment++;
7843 		}
7844 
7845 		/* Next segment. */
7846 		seg++;
7847 	}
7848 	return rval;
7849 
7850 fail_fw_integrity:
7851 	return QLA_FUNCTION_FAILED;
7852 }
7853 
7854 static int
7855 qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7856 {
7857 	int	rval;
7858 	uint templates, segments, fragment;
7859 	uint32_t *dcode;
7860 	ulong dlen;
7861 	uint32_t risc_addr, risc_size, risc_attr = 0;
7862 	ulong i;
7863 	uint j;
7864 	struct fw_blob *blob;
7865 	uint32_t *fwcode;
7866 	struct qla_hw_data *ha = vha->hw;
7867 	struct req_que *req = ha->req_q_map[0];
7868 	struct fwdt *fwdt = ha->fwdt;
7869 
7870 	ql_dbg(ql_dbg_init, vha, 0x0090,
7871 	    "-> FW: Loading via request-firmware.\n");
7872 
7873 	blob = qla2x00_request_firmware(vha);
7874 	if (!blob) {
7875 		ql_log(ql_log_warn, vha, 0x0092,
7876 		    "-> Firmware file not found.\n");
7877 
7878 		return QLA_FUNCTION_FAILED;
7879 	}
7880 
7881 	fwcode = (void *)blob->fw->data;
7882 	dcode = fwcode;
7883 	if (qla24xx_risc_firmware_invalid(dcode)) {
7884 		ql_log(ql_log_fatal, vha, 0x0093,
7885 		    "Unable to verify integrity of firmware image (%zd).\n",
7886 		    blob->fw->size);
7887 		ql_log(ql_log_fatal, vha, 0x0095,
7888 		    "Firmware data: %08x %08x %08x %08x.\n",
7889 		    dcode[0], dcode[1], dcode[2], dcode[3]);
7890 		return QLA_FUNCTION_FAILED;
7891 	}
7892 
7893 	dcode = (void *)req->ring;
7894 	*srisc_addr = 0;
7895 	segments = FA_RISC_CODE_SEGMENTS;
7896 	for (j = 0; j < segments; j++) {
7897 		ql_dbg(ql_dbg_init, vha, 0x0096,
7898 		    "-> Loading segment %u...\n", j);
7899 		risc_addr = be32_to_cpu(fwcode[2]);
7900 		risc_size = be32_to_cpu(fwcode[3]);
7901 
7902 		if (!*srisc_addr) {
7903 			*srisc_addr = risc_addr;
7904 			risc_attr = be32_to_cpu(fwcode[9]);
7905 		}
7906 
7907 		dlen = ha->fw_transfer_size >> 2;
7908 		for (fragment = 0; risc_size; fragment++) {
7909 			if (dlen > risc_size)
7910 				dlen = risc_size;
7911 
7912 			ql_dbg(ql_dbg_init, vha, 0x0097,
7913 			    "-> Loading fragment %u: %#x <- %#x (%#lx words)...\n",
7914 			    fragment, risc_addr,
7915 			    (uint32_t)(fwcode - (typeof(fwcode))blob->fw->data),
7916 			    dlen);
7917 
7918 			for (i = 0; i < dlen; i++)
7919 				dcode[i] = swab32(fwcode[i]);
7920 
7921 			rval = qla2x00_load_ram(vha, req->dma, risc_addr, dlen);
7922 			if (rval) {
7923 				ql_log(ql_log_fatal, vha, 0x0098,
7924 				    "-> Failed load firmware fragment %u.\n",
7925 				    fragment);
7926 				return QLA_FUNCTION_FAILED;
7927 			}
7928 
7929 			fwcode += dlen;
7930 			risc_addr += dlen;
7931 			risc_size -= dlen;
7932 		}
7933 	}
7934 
7935 	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
7936 		return QLA_SUCCESS;
7937 
7938 	templates = (risc_attr & BIT_9) ? 2 : 1;
7939 	ql_dbg(ql_dbg_init, vha, 0x0170, "-> templates = %u\n", templates);
7940 	for (j = 0; j < templates; j++, fwdt++) {
7941 		if (fwdt->template)
7942 			vfree(fwdt->template);
7943 		fwdt->template = NULL;
7944 		fwdt->length = 0;
7945 
7946 		risc_size = be32_to_cpu(fwcode[2]);
7947 		ql_dbg(ql_dbg_init, vha, 0x0171,
7948 		    "-> fwdt%u template array at %#x (%#x dwords)\n",
7949 		    j, (uint32_t)((void *)fwcode - (void *)blob->fw->data),
7950 		    risc_size);
7951 		if (!risc_size || !~risc_size) {
7952 			ql_dbg(ql_dbg_init, vha, 0x0172,
7953 			    "-> fwdt%u failed to read array\n", j);
7954 			goto failed;
7955 		}
7956 
7957 		/* skip header and ignore checksum */
7958 		fwcode += 7;
7959 		risc_size -= 8;
7960 
7961 		ql_dbg(ql_dbg_init, vha, 0x0173,
7962 		    "-> fwdt%u template allocate template %#x words...\n",
7963 		    j, risc_size);
7964 		fwdt->template = vmalloc(risc_size * sizeof(*dcode));
7965 		if (!fwdt->template) {
7966 			ql_log(ql_log_warn, vha, 0x0174,
7967 			    "-> fwdt%u failed allocate template.\n", j);
7968 			goto failed;
7969 		}
7970 
7971 		dcode = fwdt->template;
7972 		for (i = 0; i < risc_size; i++)
7973 			dcode[i] = fwcode[i];
7974 
7975 		if (!qla27xx_fwdt_template_valid(dcode)) {
7976 			ql_log(ql_log_warn, vha, 0x0175,
7977 			    "-> fwdt%u failed template validate\n", j);
7978 			goto failed;
7979 		}
7980 
7981 		dlen = qla27xx_fwdt_template_size(dcode);
7982 		ql_dbg(ql_dbg_init, vha, 0x0176,
7983 		    "-> fwdt%u template size %#lx bytes (%#lx words)\n",
7984 		    j, dlen, dlen / sizeof(*dcode));
7985 		if (dlen > risc_size * sizeof(*dcode)) {
7986 			ql_log(ql_log_warn, vha, 0x0177,
7987 			    "-> fwdt%u template exceeds array (%-lu bytes)\n",
7988 			    j, dlen - risc_size * sizeof(*dcode));
7989 			goto failed;
7990 		}
7991 
7992 		fwdt->length = dlen;
7993 		ql_dbg(ql_dbg_init, vha, 0x0178,
7994 		    "-> fwdt%u loaded template ok\n", j);
7995 
7996 		fwcode += risc_size + 1;
7997 	}
7998 
7999 	return QLA_SUCCESS;
8000 
8001 failed:
8002 	if (fwdt->template)
8003 		vfree(fwdt->template);
8004 	fwdt->template = NULL;
8005 	fwdt->length = 0;
8006 
8007 	return QLA_SUCCESS;
8008 }
8009 
8010 int
8011 qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8012 {
8013 	int rval;
8014 
8015 	if (ql2xfwloadbin == 1)
8016 		return qla81xx_load_risc(vha, srisc_addr);
8017 
8018 	/*
8019 	 * FW Load priority:
8020 	 * 1) Firmware via request-firmware interface (.bin file).
8021 	 * 2) Firmware residing in flash.
8022 	 */
8023 	rval = qla24xx_load_risc_blob(vha, srisc_addr);
8024 	if (rval == QLA_SUCCESS)
8025 		return rval;
8026 
8027 	return qla24xx_load_risc_flash(vha, srisc_addr,
8028 	    vha->hw->flt_region_fw);
8029 }
8030 
8031 int
8032 qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8033 {
8034 	int rval;
8035 	struct qla_hw_data *ha = vha->hw;
8036 	struct active_regions active_regions = { };
8037 
8038 	if (ql2xfwloadbin == 2)
8039 		goto try_blob_fw;
8040 
8041 	/* FW Load priority:
8042 	 * 1) Firmware residing in flash.
8043 	 * 2) Firmware via request-firmware interface (.bin file).
8044 	 * 3) Golden-Firmware residing in flash -- (limited operation).
8045 	 */
8046 
8047 	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
8048 		goto try_primary_fw;
8049 
8050 	qla27xx_get_active_image(vha, &active_regions);
8051 
8052 	if (active_regions.global != QLA27XX_SECONDARY_IMAGE)
8053 		goto try_primary_fw;
8054 
8055 	ql_dbg(ql_dbg_init, vha, 0x008b,
8056 	    "Loading secondary firmware image.\n");
8057 	rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw_sec);
8058 	if (!rval)
8059 		return rval;
8060 
8061 try_primary_fw:
8062 	ql_dbg(ql_dbg_init, vha, 0x008b,
8063 	    "Loading primary firmware image.\n");
8064 	rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
8065 	if (!rval)
8066 		return rval;
8067 
8068 try_blob_fw:
8069 	rval = qla24xx_load_risc_blob(vha, srisc_addr);
8070 	if (!rval || !ha->flt_region_gold_fw)
8071 		return rval;
8072 
8073 	ql_log(ql_log_info, vha, 0x0099,
8074 	    "Attempting to fallback to golden firmware.\n");
8075 	rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
8076 	if (rval)
8077 		return rval;
8078 
8079 	ql_log(ql_log_info, vha, 0x009a, "Need firmware flash update.\n");
8080 	ha->flags.running_gold_fw = 1;
8081 	return rval;
8082 }
8083 
8084 void
8085 qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
8086 {
8087 	int ret, retries;
8088 	struct qla_hw_data *ha = vha->hw;
8089 
8090 	if (ha->flags.pci_channel_io_perm_failure)
8091 		return;
8092 	if (!IS_FWI2_CAPABLE(ha))
8093 		return;
8094 	if (!ha->fw_major_version)
8095 		return;
8096 	if (!ha->flags.fw_started)
8097 		return;
8098 
8099 	ret = qla2x00_stop_firmware(vha);
8100 	for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
8101 	    ret != QLA_INVALID_COMMAND && retries ; retries--) {
8102 		ha->isp_ops->reset_chip(vha);
8103 		if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
8104 			continue;
8105 		if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
8106 			continue;
8107 		ql_log(ql_log_info, vha, 0x8015,
8108 		    "Attempting retry of stop-firmware command.\n");
8109 		ret = qla2x00_stop_firmware(vha);
8110 	}
8111 
8112 	QLA_FW_STOPPED(ha);
8113 	ha->flags.fw_init_done = 0;
8114 }
8115 
8116 int
8117 qla24xx_configure_vhba(scsi_qla_host_t *vha)
8118 {
8119 	int rval = QLA_SUCCESS;
8120 	int rval2;
8121 	uint16_t mb[MAILBOX_REGISTER_COUNT];
8122 	struct qla_hw_data *ha = vha->hw;
8123 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
8124 
8125 	if (!vha->vp_idx)
8126 		return -EINVAL;
8127 
8128 	rval = qla2x00_fw_ready(base_vha);
8129 
8130 	if (rval == QLA_SUCCESS) {
8131 		clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8132 		qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
8133 	}
8134 
8135 	vha->flags.management_server_logged_in = 0;
8136 
8137 	/* Login to SNS first */
8138 	rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
8139 	    BIT_1);
8140 	if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
8141 		if (rval2 == QLA_MEMORY_ALLOC_FAILED)
8142 			ql_dbg(ql_dbg_init, vha, 0x0120,
8143 			    "Failed SNS login: loop_id=%x, rval2=%d\n",
8144 			    NPH_SNS, rval2);
8145 		else
8146 			ql_dbg(ql_dbg_init, vha, 0x0103,
8147 			    "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
8148 			    "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
8149 			    NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
8150 		return (QLA_FUNCTION_FAILED);
8151 	}
8152 
8153 	atomic_set(&vha->loop_down_timer, 0);
8154 	atomic_set(&vha->loop_state, LOOP_UP);
8155 	set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
8156 	set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
8157 	rval = qla2x00_loop_resync(base_vha);
8158 
8159 	return rval;
8160 }
8161 
8162 /* 84XX Support **************************************************************/
8163 
8164 static LIST_HEAD(qla_cs84xx_list);
8165 static DEFINE_MUTEX(qla_cs84xx_mutex);
8166 
8167 static struct qla_chip_state_84xx *
8168 qla84xx_get_chip(struct scsi_qla_host *vha)
8169 {
8170 	struct qla_chip_state_84xx *cs84xx;
8171 	struct qla_hw_data *ha = vha->hw;
8172 
8173 	mutex_lock(&qla_cs84xx_mutex);
8174 
8175 	/* Find any shared 84xx chip. */
8176 	list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
8177 		if (cs84xx->bus == ha->pdev->bus) {
8178 			kref_get(&cs84xx->kref);
8179 			goto done;
8180 		}
8181 	}
8182 
8183 	cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
8184 	if (!cs84xx)
8185 		goto done;
8186 
8187 	kref_init(&cs84xx->kref);
8188 	spin_lock_init(&cs84xx->access_lock);
8189 	mutex_init(&cs84xx->fw_update_mutex);
8190 	cs84xx->bus = ha->pdev->bus;
8191 
8192 	list_add_tail(&cs84xx->list, &qla_cs84xx_list);
8193 done:
8194 	mutex_unlock(&qla_cs84xx_mutex);
8195 	return cs84xx;
8196 }
8197 
8198 static void
8199 __qla84xx_chip_release(struct kref *kref)
8200 {
8201 	struct qla_chip_state_84xx *cs84xx =
8202 	    container_of(kref, struct qla_chip_state_84xx, kref);
8203 
8204 	mutex_lock(&qla_cs84xx_mutex);
8205 	list_del(&cs84xx->list);
8206 	mutex_unlock(&qla_cs84xx_mutex);
8207 	kfree(cs84xx);
8208 }
8209 
8210 void
8211 qla84xx_put_chip(struct scsi_qla_host *vha)
8212 {
8213 	struct qla_hw_data *ha = vha->hw;
8214 
8215 	if (ha->cs84xx)
8216 		kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
8217 }
8218 
8219 static int
8220 qla84xx_init_chip(scsi_qla_host_t *vha)
8221 {
8222 	int rval;
8223 	uint16_t status[2];
8224 	struct qla_hw_data *ha = vha->hw;
8225 
8226 	mutex_lock(&ha->cs84xx->fw_update_mutex);
8227 
8228 	rval = qla84xx_verify_chip(vha, status);
8229 
8230 	mutex_unlock(&ha->cs84xx->fw_update_mutex);
8231 
8232 	return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED :
8233 	    QLA_SUCCESS;
8234 }
8235 
8236 /* 81XX Support **************************************************************/
8237 
8238 int
8239 qla81xx_nvram_config(scsi_qla_host_t *vha)
8240 {
8241 	int   rval;
8242 	struct init_cb_81xx *icb;
8243 	struct nvram_81xx *nv;
8244 	uint32_t *dptr;
8245 	uint8_t  *dptr1, *dptr2;
8246 	uint32_t chksum;
8247 	uint16_t cnt;
8248 	struct qla_hw_data *ha = vha->hw;
8249 	uint32_t faddr;
8250 	struct active_regions active_regions = { };
8251 
8252 	rval = QLA_SUCCESS;
8253 	icb = (struct init_cb_81xx *)ha->init_cb;
8254 	nv = ha->nvram;
8255 
8256 	/* Determine NVRAM starting address. */
8257 	ha->nvram_size = sizeof(*nv);
8258 	ha->vpd_size = FA_NVRAM_VPD_SIZE;
8259 	if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
8260 		ha->vpd_size = FA_VPD_SIZE_82XX;
8261 
8262 	if (IS_QLA28XX(ha) || IS_QLA27XX(ha))
8263 		qla28xx_get_aux_images(vha, &active_regions);
8264 
8265 	/* Get VPD data into cache */
8266 	ha->vpd = ha->nvram + VPD_OFFSET;
8267 
8268 	faddr = ha->flt_region_vpd;
8269 	if (IS_QLA28XX(ha)) {
8270 		if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
8271 			faddr = ha->flt_region_vpd_sec;
8272 		ql_dbg(ql_dbg_init, vha, 0x0110,
8273 		    "Loading %s nvram image.\n",
8274 		    active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
8275 		    "primary" : "secondary");
8276 	}
8277 	qla24xx_read_flash_data(vha, ha->vpd, faddr, ha->vpd_size >> 2);
8278 
8279 	/* Get NVRAM data into cache and calculate checksum. */
8280 	faddr = ha->flt_region_nvram;
8281 	if (IS_QLA28XX(ha)) {
8282 		if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
8283 			faddr = ha->flt_region_nvram_sec;
8284 	}
8285 	ql_dbg(ql_dbg_init, vha, 0x0110,
8286 	    "Loading %s nvram image.\n",
8287 	    active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
8288 	    "primary" : "secondary");
8289 	qla24xx_read_flash_data(vha, ha->nvram, faddr, ha->nvram_size >> 2);
8290 
8291 	dptr = (uint32_t *)nv;
8292 	for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
8293 		chksum += le32_to_cpu(*dptr);
8294 
8295 	ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
8296 	    "Contents of NVRAM:\n");
8297 	ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
8298 	    nv, ha->nvram_size);
8299 
8300 	/* Bad NVRAM data, set defaults parameters. */
8301 	if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
8302 	    le16_to_cpu(nv->nvram_version) < ICB_VERSION) {
8303 		/* Reset NVRAM data. */
8304 		ql_log(ql_log_info, vha, 0x0073,
8305 		    "Inconsistent NVRAM checksum=%#x id=%.4s version=%#x.\n",
8306 		    chksum, nv->id, le16_to_cpu(nv->nvram_version));
8307 		ql_dump_buffer(ql_dbg_init, vha, 0x0073, nv, sizeof(*nv));
8308 		ql_log(ql_log_info, vha, 0x0074,
8309 		    "Falling back to functioning (yet invalid -- WWPN) "
8310 		    "defaults.\n");
8311 
8312 		/*
8313 		 * Set default initialization control block.
8314 		 */
8315 		memset(nv, 0, ha->nvram_size);
8316 		nv->nvram_version = cpu_to_le16(ICB_VERSION);
8317 		nv->version = cpu_to_le16(ICB_VERSION);
8318 		nv->frame_payload_size = 2048;
8319 		nv->execution_throttle = cpu_to_le16(0xFFFF);
8320 		nv->exchange_count = cpu_to_le16(0);
8321 		nv->port_name[0] = 0x21;
8322 		nv->port_name[1] = 0x00 + ha->port_no + 1;
8323 		nv->port_name[2] = 0x00;
8324 		nv->port_name[3] = 0xe0;
8325 		nv->port_name[4] = 0x8b;
8326 		nv->port_name[5] = 0x1c;
8327 		nv->port_name[6] = 0x55;
8328 		nv->port_name[7] = 0x86;
8329 		nv->node_name[0] = 0x20;
8330 		nv->node_name[1] = 0x00;
8331 		nv->node_name[2] = 0x00;
8332 		nv->node_name[3] = 0xe0;
8333 		nv->node_name[4] = 0x8b;
8334 		nv->node_name[5] = 0x1c;
8335 		nv->node_name[6] = 0x55;
8336 		nv->node_name[7] = 0x86;
8337 		nv->login_retry_count = cpu_to_le16(8);
8338 		nv->interrupt_delay_timer = cpu_to_le16(0);
8339 		nv->login_timeout = cpu_to_le16(0);
8340 		nv->firmware_options_1 =
8341 		    cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
8342 		nv->firmware_options_2 = cpu_to_le32(2 << 4);
8343 		nv->firmware_options_2 |= cpu_to_le32(BIT_12);
8344 		nv->firmware_options_3 = cpu_to_le32(2 << 13);
8345 		nv->host_p = cpu_to_le32(BIT_11|BIT_10);
8346 		nv->efi_parameters = cpu_to_le32(0);
8347 		nv->reset_delay = 5;
8348 		nv->max_luns_per_target = cpu_to_le16(128);
8349 		nv->port_down_retry_count = cpu_to_le16(30);
8350 		nv->link_down_timeout = cpu_to_le16(180);
8351 		nv->enode_mac[0] = 0x00;
8352 		nv->enode_mac[1] = 0xC0;
8353 		nv->enode_mac[2] = 0xDD;
8354 		nv->enode_mac[3] = 0x04;
8355 		nv->enode_mac[4] = 0x05;
8356 		nv->enode_mac[5] = 0x06 + ha->port_no + 1;
8357 
8358 		rval = 1;
8359 	}
8360 
8361 	if (IS_T10_PI_CAPABLE(ha))
8362 		nv->frame_payload_size &= ~7;
8363 
8364 	qlt_81xx_config_nvram_stage1(vha, nv);
8365 
8366 	/* Reset Initialization control block */
8367 	memset(icb, 0, ha->init_cb_size);
8368 
8369 	/* Copy 1st segment. */
8370 	dptr1 = (uint8_t *)icb;
8371 	dptr2 = (uint8_t *)&nv->version;
8372 	cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
8373 	while (cnt--)
8374 		*dptr1++ = *dptr2++;
8375 
8376 	icb->login_retry_count = nv->login_retry_count;
8377 
8378 	/* Copy 2nd segment. */
8379 	dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
8380 	dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
8381 	cnt = (uint8_t *)&icb->reserved_5 -
8382 	    (uint8_t *)&icb->interrupt_delay_timer;
8383 	while (cnt--)
8384 		*dptr1++ = *dptr2++;
8385 
8386 	memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
8387 	/* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
8388 	if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
8389 		icb->enode_mac[0] = 0x00;
8390 		icb->enode_mac[1] = 0xC0;
8391 		icb->enode_mac[2] = 0xDD;
8392 		icb->enode_mac[3] = 0x04;
8393 		icb->enode_mac[4] = 0x05;
8394 		icb->enode_mac[5] = 0x06 + ha->port_no + 1;
8395 	}
8396 
8397 	/* Use extended-initialization control block. */
8398 	memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
8399 	ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
8400 	/*
8401 	 * Setup driver NVRAM options.
8402 	 */
8403 	qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
8404 	    "QLE8XXX");
8405 
8406 	qlt_81xx_config_nvram_stage2(vha, icb);
8407 
8408 	/* Use alternate WWN? */
8409 	if (nv->host_p & cpu_to_le32(BIT_15)) {
8410 		memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
8411 		memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
8412 	}
8413 
8414 	/* Prepare nodename */
8415 	if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
8416 		/*
8417 		 * Firmware will apply the following mask if the nodename was
8418 		 * not provided.
8419 		 */
8420 		memcpy(icb->node_name, icb->port_name, WWN_SIZE);
8421 		icb->node_name[0] &= 0xF0;
8422 	}
8423 
8424 	/* Set host adapter parameters. */
8425 	ha->flags.disable_risc_code_load = 0;
8426 	ha->flags.enable_lip_reset = 0;
8427 	ha->flags.enable_lip_full_login =
8428 	    le32_to_cpu(nv->host_p) & BIT_10 ? 1 : 0;
8429 	ha->flags.enable_target_reset =
8430 	    le32_to_cpu(nv->host_p) & BIT_11 ? 1 : 0;
8431 	ha->flags.enable_led_scheme = 0;
8432 	ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1 : 0;
8433 
8434 	ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
8435 	    (BIT_6 | BIT_5 | BIT_4)) >> 4;
8436 
8437 	/* save HBA serial number */
8438 	ha->serial0 = icb->port_name[5];
8439 	ha->serial1 = icb->port_name[6];
8440 	ha->serial2 = icb->port_name[7];
8441 	memcpy(vha->node_name, icb->node_name, WWN_SIZE);
8442 	memcpy(vha->port_name, icb->port_name, WWN_SIZE);
8443 
8444 	icb->execution_throttle = cpu_to_le16(0xFFFF);
8445 
8446 	ha->retry_count = le16_to_cpu(nv->login_retry_count);
8447 
8448 	/* Set minimum login_timeout to 4 seconds. */
8449 	if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
8450 		nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
8451 	if (le16_to_cpu(nv->login_timeout) < 4)
8452 		nv->login_timeout = cpu_to_le16(4);
8453 	ha->login_timeout = le16_to_cpu(nv->login_timeout);
8454 
8455 	/* Set minimum RATOV to 100 tenths of a second. */
8456 	ha->r_a_tov = 100;
8457 
8458 	ha->loop_reset_delay = nv->reset_delay;
8459 
8460 	/* Link Down Timeout = 0:
8461 	 *
8462 	 *	When Port Down timer expires we will start returning
8463 	 *	I/O's to OS with "DID_NO_CONNECT".
8464 	 *
8465 	 * Link Down Timeout != 0:
8466 	 *
8467 	 *	 The driver waits for the link to come up after link down
8468 	 *	 before returning I/Os to OS with "DID_NO_CONNECT".
8469 	 */
8470 	if (le16_to_cpu(nv->link_down_timeout) == 0) {
8471 		ha->loop_down_abort_time =
8472 		    (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
8473 	} else {
8474 		ha->link_down_timeout =	le16_to_cpu(nv->link_down_timeout);
8475 		ha->loop_down_abort_time =
8476 		    (LOOP_DOWN_TIME - ha->link_down_timeout);
8477 	}
8478 
8479 	/* Need enough time to try and get the port back. */
8480 	ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
8481 	if (qlport_down_retry)
8482 		ha->port_down_retry_count = qlport_down_retry;
8483 
8484 	/* Set login_retry_count */
8485 	ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
8486 	if (ha->port_down_retry_count ==
8487 	    le16_to_cpu(nv->port_down_retry_count) &&
8488 	    ha->port_down_retry_count > 3)
8489 		ha->login_retry_count = ha->port_down_retry_count;
8490 	else if (ha->port_down_retry_count > (int)ha->login_retry_count)
8491 		ha->login_retry_count = ha->port_down_retry_count;
8492 	if (ql2xloginretrycount)
8493 		ha->login_retry_count = ql2xloginretrycount;
8494 
8495 	/* if not running MSI-X we need handshaking on interrupts */
8496 	if (!vha->hw->flags.msix_enabled &&
8497 	    (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)))
8498 		icb->firmware_options_2 |= cpu_to_le32(BIT_22);
8499 
8500 	/* Enable ZIO. */
8501 	if (!vha->flags.init_done) {
8502 		ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
8503 		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
8504 		ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
8505 		    le16_to_cpu(icb->interrupt_delay_timer) : 2;
8506 	}
8507 	icb->firmware_options_2 &= cpu_to_le32(
8508 	    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
8509 	vha->flags.process_response_queue = 0;
8510 	if (ha->zio_mode != QLA_ZIO_DISABLED) {
8511 		ha->zio_mode = QLA_ZIO_MODE_6;
8512 
8513 		ql_log(ql_log_info, vha, 0x0075,
8514 		    "ZIO mode %d enabled; timer delay (%d us).\n",
8515 		    ha->zio_mode,
8516 		    ha->zio_timer * 100);
8517 
8518 		icb->firmware_options_2 |= cpu_to_le32(
8519 		    (uint32_t)ha->zio_mode);
8520 		icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
8521 		vha->flags.process_response_queue = 1;
8522 	}
8523 
8524 	 /* enable RIDA Format2 */
8525 	icb->firmware_options_3 |= BIT_0;
8526 
8527 	/* N2N: driver will initiate Login instead of FW */
8528 	icb->firmware_options_3 |= BIT_8;
8529 
8530 	if (rval) {
8531 		ql_log(ql_log_warn, vha, 0x0076,
8532 		    "NVRAM configuration failed.\n");
8533 	}
8534 	return (rval);
8535 }
8536 
8537 int
8538 qla82xx_restart_isp(scsi_qla_host_t *vha)
8539 {
8540 	int status, rval;
8541 	struct qla_hw_data *ha = vha->hw;
8542 	struct scsi_qla_host *vp;
8543 	unsigned long flags;
8544 
8545 	status = qla2x00_init_rings(vha);
8546 	if (!status) {
8547 		clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8548 		ha->flags.chip_reset_done = 1;
8549 
8550 		status = qla2x00_fw_ready(vha);
8551 		if (!status) {
8552 			/* Issue a marker after FW becomes ready. */
8553 			qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
8554 			vha->flags.online = 1;
8555 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
8556 		}
8557 
8558 		/* if no cable then assume it's good */
8559 		if ((vha->device_flags & DFLG_NO_CABLE))
8560 			status = 0;
8561 	}
8562 
8563 	if (!status) {
8564 		clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8565 
8566 		if (!atomic_read(&vha->loop_down_timer)) {
8567 			/*
8568 			 * Issue marker command only when we are going
8569 			 * to start the I/O .
8570 			 */
8571 			vha->marker_needed = 1;
8572 		}
8573 
8574 		ha->isp_ops->enable_intrs(ha);
8575 
8576 		ha->isp_abort_cnt = 0;
8577 		clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8578 
8579 		/* Update the firmware version */
8580 		status = qla82xx_check_md_needed(vha);
8581 
8582 		if (ha->fce) {
8583 			ha->flags.fce_enabled = 1;
8584 			memset(ha->fce, 0,
8585 			    fce_calc_size(ha->fce_bufs));
8586 			rval = qla2x00_enable_fce_trace(vha,
8587 			    ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8588 			    &ha->fce_bufs);
8589 			if (rval) {
8590 				ql_log(ql_log_warn, vha, 0x8001,
8591 				    "Unable to reinitialize FCE (%d).\n",
8592 				    rval);
8593 				ha->flags.fce_enabled = 0;
8594 			}
8595 		}
8596 
8597 		if (ha->eft) {
8598 			memset(ha->eft, 0, EFT_SIZE);
8599 			rval = qla2x00_enable_eft_trace(vha,
8600 			    ha->eft_dma, EFT_NUM_BUFFERS);
8601 			if (rval) {
8602 				ql_log(ql_log_warn, vha, 0x8010,
8603 				    "Unable to reinitialize EFT (%d).\n",
8604 				    rval);
8605 			}
8606 		}
8607 	}
8608 
8609 	if (!status) {
8610 		ql_dbg(ql_dbg_taskm, vha, 0x8011,
8611 		    "qla82xx_restart_isp succeeded.\n");
8612 
8613 		spin_lock_irqsave(&ha->vport_slock, flags);
8614 		list_for_each_entry(vp, &ha->vp_list, list) {
8615 			if (vp->vp_idx) {
8616 				atomic_inc(&vp->vref_count);
8617 				spin_unlock_irqrestore(&ha->vport_slock, flags);
8618 
8619 				qla2x00_vp_abort_isp(vp);
8620 
8621 				spin_lock_irqsave(&ha->vport_slock, flags);
8622 				atomic_dec(&vp->vref_count);
8623 			}
8624 		}
8625 		spin_unlock_irqrestore(&ha->vport_slock, flags);
8626 
8627 	} else {
8628 		ql_log(ql_log_warn, vha, 0x8016,
8629 		    "qla82xx_restart_isp **** FAILED ****.\n");
8630 	}
8631 
8632 	return status;
8633 }
8634 
8635 void
8636 qla81xx_update_fw_options(scsi_qla_host_t *vha)
8637 {
8638 	struct qla_hw_data *ha = vha->hw;
8639 
8640 	/*  Hold status IOCBs until ABTS response received. */
8641 	if (ql2xfwholdabts)
8642 		ha->fw_options[3] |= BIT_12;
8643 
8644 	/* Set Retry FLOGI in case of P2P connection */
8645 	if (ha->operating_mode == P2P) {
8646 		ha->fw_options[2] |= BIT_3;
8647 		ql_dbg(ql_dbg_disc, vha, 0x2103,
8648 		    "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8649 			__func__, ha->fw_options[2]);
8650 	}
8651 
8652 	/* Move PUREX, ABTS RX & RIDA to ATIOQ */
8653 	if (ql2xmvasynctoatio) {
8654 		if (qla_tgt_mode_enabled(vha) ||
8655 		    qla_dual_mode_enabled(vha))
8656 			ha->fw_options[2] |= BIT_11;
8657 		else
8658 			ha->fw_options[2] &= ~BIT_11;
8659 	}
8660 
8661 	if (qla_tgt_mode_enabled(vha) ||
8662 	    qla_dual_mode_enabled(vha)) {
8663 		/* FW auto send SCSI status during */
8664 		ha->fw_options[1] |= BIT_8;
8665 		ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8666 
8667 		/* FW perform Exchange validation */
8668 		ha->fw_options[2] |= BIT_4;
8669 	} else {
8670 		ha->fw_options[1]  &= ~BIT_8;
8671 		ha->fw_options[10] &= 0x00ff;
8672 
8673 		ha->fw_options[2] &= ~BIT_4;
8674 	}
8675 
8676 	if (ql2xetsenable) {
8677 		/* Enable ETS Burst. */
8678 		memset(ha->fw_options, 0, sizeof(ha->fw_options));
8679 		ha->fw_options[2] |= BIT_9;
8680 	}
8681 
8682 	ql_dbg(ql_dbg_init, vha, 0x00e9,
8683 	    "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8684 	    __func__, ha->fw_options[1], ha->fw_options[2],
8685 	    ha->fw_options[3], vha->host->active_mode);
8686 
8687 	qla2x00_set_fw_options(vha, ha->fw_options);
8688 }
8689 
8690 /*
8691  * qla24xx_get_fcp_prio
8692  *	Gets the fcp cmd priority value for the logged in port.
8693  *	Looks for a match of the port descriptors within
8694  *	each of the fcp prio config entries. If a match is found,
8695  *	the tag (priority) value is returned.
8696  *
8697  * Input:
8698  *	vha = scsi host structure pointer.
8699  *	fcport = port structure pointer.
8700  *
8701  * Return:
8702  *	non-zero (if found)
8703  *	-1 (if not found)
8704  *
8705  * Context:
8706  * 	Kernel context
8707  */
8708 static int
8709 qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8710 {
8711 	int i, entries;
8712 	uint8_t pid_match, wwn_match;
8713 	int priority;
8714 	uint32_t pid1, pid2;
8715 	uint64_t wwn1, wwn2;
8716 	struct qla_fcp_prio_entry *pri_entry;
8717 	struct qla_hw_data *ha = vha->hw;
8718 
8719 	if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
8720 		return -1;
8721 
8722 	priority = -1;
8723 	entries = ha->fcp_prio_cfg->num_entries;
8724 	pri_entry = &ha->fcp_prio_cfg->entry[0];
8725 
8726 	for (i = 0; i < entries; i++) {
8727 		pid_match = wwn_match = 0;
8728 
8729 		if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8730 			pri_entry++;
8731 			continue;
8732 		}
8733 
8734 		/* check source pid for a match */
8735 		if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8736 			pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8737 			pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8738 			if (pid1 == INVALID_PORT_ID)
8739 				pid_match++;
8740 			else if (pid1 == pid2)
8741 				pid_match++;
8742 		}
8743 
8744 		/* check destination pid for a match */
8745 		if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8746 			pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8747 			pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
8748 			if (pid1 == INVALID_PORT_ID)
8749 				pid_match++;
8750 			else if (pid1 == pid2)
8751 				pid_match++;
8752 		}
8753 
8754 		/* check source WWN for a match */
8755 		if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8756 			wwn1 = wwn_to_u64(vha->port_name);
8757 			wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8758 			if (wwn2 == (uint64_t)-1)
8759 				wwn_match++;
8760 			else if (wwn1 == wwn2)
8761 				wwn_match++;
8762 		}
8763 
8764 		/* check destination WWN for a match */
8765 		if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8766 			wwn1 = wwn_to_u64(fcport->port_name);
8767 			wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8768 			if (wwn2 == (uint64_t)-1)
8769 				wwn_match++;
8770 			else if (wwn1 == wwn2)
8771 				wwn_match++;
8772 		}
8773 
8774 		if (pid_match == 2 || wwn_match == 2) {
8775 			/* Found a matching entry */
8776 			if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8777 				priority = pri_entry->tag;
8778 			break;
8779 		}
8780 
8781 		pri_entry++;
8782 	}
8783 
8784 	return priority;
8785 }
8786 
8787 /*
8788  * qla24xx_update_fcport_fcp_prio
8789  *	Activates fcp priority for the logged in fc port
8790  *
8791  * Input:
8792  *	vha = scsi host structure pointer.
8793  *	fcp = port structure pointer.
8794  *
8795  * Return:
8796  *	QLA_SUCCESS or QLA_FUNCTION_FAILED
8797  *
8798  * Context:
8799  *	Kernel context.
8800  */
8801 int
8802 qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8803 {
8804 	int ret;
8805 	int priority;
8806 	uint16_t mb[5];
8807 
8808 	if (fcport->port_type != FCT_TARGET ||
8809 	    fcport->loop_id == FC_NO_LOOP_ID)
8810 		return QLA_FUNCTION_FAILED;
8811 
8812 	priority = qla24xx_get_fcp_prio(vha, fcport);
8813 	if (priority < 0)
8814 		return QLA_FUNCTION_FAILED;
8815 
8816 	if (IS_P3P_TYPE(vha->hw)) {
8817 		fcport->fcp_prio = priority & 0xf;
8818 		return QLA_SUCCESS;
8819 	}
8820 
8821 	ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
8822 	if (ret == QLA_SUCCESS) {
8823 		if (fcport->fcp_prio != priority)
8824 			ql_dbg(ql_dbg_user, vha, 0x709e,
8825 			    "Updated FCP_CMND priority - value=%d loop_id=%d "
8826 			    "port_id=%02x%02x%02x.\n", priority,
8827 			    fcport->loop_id, fcport->d_id.b.domain,
8828 			    fcport->d_id.b.area, fcport->d_id.b.al_pa);
8829 		fcport->fcp_prio = priority & 0xf;
8830 	} else
8831 		ql_dbg(ql_dbg_user, vha, 0x704f,
8832 		    "Unable to update FCP_CMND priority - ret=0x%x for "
8833 		    "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8834 		    fcport->d_id.b.domain, fcport->d_id.b.area,
8835 		    fcport->d_id.b.al_pa);
8836 	return  ret;
8837 }
8838 
8839 /*
8840  * qla24xx_update_all_fcp_prio
8841  *	Activates fcp priority for all the logged in ports
8842  *
8843  * Input:
8844  *	ha = adapter block pointer.
8845  *
8846  * Return:
8847  *	QLA_SUCCESS or QLA_FUNCTION_FAILED
8848  *
8849  * Context:
8850  *	Kernel context.
8851  */
8852 int
8853 qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8854 {
8855 	int ret;
8856 	fc_port_t *fcport;
8857 
8858 	ret = QLA_FUNCTION_FAILED;
8859 	/* We need to set priority for all logged in ports */
8860 	list_for_each_entry(fcport, &vha->vp_fcports, list)
8861 		ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8862 
8863 	return ret;
8864 }
8865 
8866 struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8867 	int vp_idx, bool startqp)
8868 {
8869 	int rsp_id = 0;
8870 	int  req_id = 0;
8871 	int i;
8872 	struct qla_hw_data *ha = vha->hw;
8873 	uint16_t qpair_id = 0;
8874 	struct qla_qpair *qpair = NULL;
8875 	struct qla_msix_entry *msix;
8876 
8877 	if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8878 		ql_log(ql_log_warn, vha, 0x00181,
8879 		    "FW/Driver is not multi-queue capable.\n");
8880 		return NULL;
8881 	}
8882 
8883 	if (ql2xmqsupport || ql2xnvmeenable) {
8884 		qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8885 		if (qpair == NULL) {
8886 			ql_log(ql_log_warn, vha, 0x0182,
8887 			    "Failed to allocate memory for queue pair.\n");
8888 			return NULL;
8889 		}
8890 
8891 		qpair->hw = vha->hw;
8892 		qpair->vha = vha;
8893 		qpair->qp_lock_ptr = &qpair->qp_lock;
8894 		spin_lock_init(&qpair->qp_lock);
8895 		qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
8896 
8897 		/* Assign available que pair id */
8898 		mutex_lock(&ha->mq_lock);
8899 		qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
8900 		if (ha->num_qpairs >= ha->max_qpairs) {
8901 			mutex_unlock(&ha->mq_lock);
8902 			ql_log(ql_log_warn, vha, 0x0183,
8903 			    "No resources to create additional q pair.\n");
8904 			goto fail_qid_map;
8905 		}
8906 		ha->num_qpairs++;
8907 		set_bit(qpair_id, ha->qpair_qid_map);
8908 		ha->queue_pair_map[qpair_id] = qpair;
8909 		qpair->id = qpair_id;
8910 		qpair->vp_idx = vp_idx;
8911 		qpair->fw_started = ha->flags.fw_started;
8912 		INIT_LIST_HEAD(&qpair->hints_list);
8913 		qpair->chip_reset = ha->base_qpair->chip_reset;
8914 		qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8915 		qpair->enable_explicit_conf =
8916 		    ha->base_qpair->enable_explicit_conf;
8917 
8918 		for (i = 0; i < ha->msix_count; i++) {
8919 			msix = &ha->msix_entries[i];
8920 			if (msix->in_use)
8921 				continue;
8922 			qpair->msix = msix;
8923 			ql_dbg(ql_dbg_multiq, vha, 0xc00f,
8924 			    "Vector %x selected for qpair\n", msix->vector);
8925 			break;
8926 		}
8927 		if (!qpair->msix) {
8928 			ql_log(ql_log_warn, vha, 0x0184,
8929 			    "Out of MSI-X vectors!.\n");
8930 			goto fail_msix;
8931 		}
8932 
8933 		qpair->msix->in_use = 1;
8934 		list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
8935 		qpair->pdev = ha->pdev;
8936 		if (IS_QLA27XX(ha) || IS_QLA83XX(ha) || IS_QLA28XX(ha))
8937 			qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
8938 
8939 		mutex_unlock(&ha->mq_lock);
8940 
8941 		/* Create response queue first */
8942 		rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
8943 		if (!rsp_id) {
8944 			ql_log(ql_log_warn, vha, 0x0185,
8945 			    "Failed to create response queue.\n");
8946 			goto fail_rsp;
8947 		}
8948 
8949 		qpair->rsp = ha->rsp_q_map[rsp_id];
8950 
8951 		/* Create request queue */
8952 		req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8953 		    startqp);
8954 		if (!req_id) {
8955 			ql_log(ql_log_warn, vha, 0x0186,
8956 			    "Failed to create request queue.\n");
8957 			goto fail_req;
8958 		}
8959 
8960 		qpair->req = ha->req_q_map[req_id];
8961 		qpair->rsp->req = qpair->req;
8962 		qpair->rsp->qpair = qpair;
8963 		/* init qpair to this cpu. Will adjust at run time. */
8964 		qla_cpu_update(qpair, smp_processor_id());
8965 
8966 		if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8967 			if (ha->fw_attributes & BIT_4)
8968 				qpair->difdix_supported = 1;
8969 		}
8970 
8971 		qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8972 		if (!qpair->srb_mempool) {
8973 			ql_log(ql_log_warn, vha, 0xd036,
8974 			    "Failed to create srb mempool for qpair %d\n",
8975 			    qpair->id);
8976 			goto fail_mempool;
8977 		}
8978 
8979 		/* Mark as online */
8980 		qpair->online = 1;
8981 
8982 		if (!vha->flags.qpairs_available)
8983 			vha->flags.qpairs_available = 1;
8984 
8985 		ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8986 		    "Request/Response queue pair created, id %d\n",
8987 		    qpair->id);
8988 		ql_dbg(ql_dbg_init, vha, 0x0187,
8989 		    "Request/Response queue pair created, id %d\n",
8990 		    qpair->id);
8991 	}
8992 	return qpair;
8993 
8994 fail_mempool:
8995 fail_req:
8996 	qla25xx_delete_rsp_que(vha, qpair->rsp);
8997 fail_rsp:
8998 	mutex_lock(&ha->mq_lock);
8999 	qpair->msix->in_use = 0;
9000 	list_del(&qpair->qp_list_elem);
9001 	if (list_empty(&vha->qp_list))
9002 		vha->flags.qpairs_available = 0;
9003 fail_msix:
9004 	ha->queue_pair_map[qpair_id] = NULL;
9005 	clear_bit(qpair_id, ha->qpair_qid_map);
9006 	ha->num_qpairs--;
9007 	mutex_unlock(&ha->mq_lock);
9008 fail_qid_map:
9009 	kfree(qpair);
9010 	return NULL;
9011 }
9012 
9013 int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
9014 {
9015 	int ret = QLA_FUNCTION_FAILED;
9016 	struct qla_hw_data *ha = qpair->hw;
9017 
9018 	qpair->delete_in_progress = 1;
9019 	while (atomic_read(&qpair->ref_count))
9020 		msleep(500);
9021 
9022 	ret = qla25xx_delete_req_que(vha, qpair->req);
9023 	if (ret != QLA_SUCCESS)
9024 		goto fail;
9025 
9026 	ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
9027 	if (ret != QLA_SUCCESS)
9028 		goto fail;
9029 
9030 	mutex_lock(&ha->mq_lock);
9031 	ha->queue_pair_map[qpair->id] = NULL;
9032 	clear_bit(qpair->id, ha->qpair_qid_map);
9033 	ha->num_qpairs--;
9034 	list_del(&qpair->qp_list_elem);
9035 	if (list_empty(&vha->qp_list)) {
9036 		vha->flags.qpairs_available = 0;
9037 		vha->flags.qpairs_req_created = 0;
9038 		vha->flags.qpairs_rsp_created = 0;
9039 	}
9040 	mempool_destroy(qpair->srb_mempool);
9041 	kfree(qpair);
9042 	mutex_unlock(&ha->mq_lock);
9043 
9044 	return QLA_SUCCESS;
9045 fail:
9046 	return ret;
9047 }
9048