18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * QLogic Fibre Channel HBA Driver
48c2ecf20Sopenharmony_ci * Copyright (c)  2003-2014 QLogic Corporation
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include "qla_def.h"
78c2ecf20Sopenharmony_ci#include "qla_gbl.h"
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "qla_devtbl.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#ifdef CONFIG_SPARC
168c2ecf20Sopenharmony_ci#include <asm/prom.h>
178c2ecf20Sopenharmony_ci#endif
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "qla_target.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci*  QLogic ISP2x00 Hardware Support Function Prototypes.
238c2ecf20Sopenharmony_ci*/
248c2ecf20Sopenharmony_cistatic int qla2x00_isp_firmware(scsi_qla_host_t *);
258c2ecf20Sopenharmony_cistatic int qla2x00_setup_chip(scsi_qla_host_t *);
268c2ecf20Sopenharmony_cistatic int qla2x00_fw_ready(scsi_qla_host_t *);
278c2ecf20Sopenharmony_cistatic int qla2x00_configure_hba(scsi_qla_host_t *);
288c2ecf20Sopenharmony_cistatic int qla2x00_configure_loop(scsi_qla_host_t *);
298c2ecf20Sopenharmony_cistatic int qla2x00_configure_local_loop(scsi_qla_host_t *);
308c2ecf20Sopenharmony_cistatic int qla2x00_configure_fabric(scsi_qla_host_t *);
318c2ecf20Sopenharmony_cistatic int qla2x00_find_all_fabric_devs(scsi_qla_host_t *);
328c2ecf20Sopenharmony_cistatic int qla2x00_restart_isp(scsi_qla_host_t *);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
358c2ecf20Sopenharmony_cistatic int qla84xx_init_chip(scsi_qla_host_t *);
368c2ecf20Sopenharmony_cistatic int qla25xx_init_queues(struct qla_hw_data *);
378c2ecf20Sopenharmony_cistatic int qla24xx_post_prli_work(struct scsi_qla_host*, fc_port_t *);
388c2ecf20Sopenharmony_cistatic void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha,
398c2ecf20Sopenharmony_ci				      struct event_arg *ea);
408c2ecf20Sopenharmony_cistatic void qla24xx_handle_prli_done_event(struct scsi_qla_host *,
418c2ecf20Sopenharmony_ci    struct event_arg *);
428c2ecf20Sopenharmony_cistatic void __qla24xx_handle_gpdb_event(scsi_qla_host_t *, struct event_arg *);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* SRB Extensions ---------------------------------------------------------- */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_civoid
478c2ecf20Sopenharmony_ciqla2x00_sp_timeout(struct timer_list *t)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
508c2ecf20Sopenharmony_ci	struct srb_iocb *iocb;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	WARN_ON(irqs_disabled());
538c2ecf20Sopenharmony_ci	iocb = &sp->u.iocb_cmd;
548c2ecf20Sopenharmony_ci	iocb->timeout(sp);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_civoid qla2x00_sp_free(srb_t *sp)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct srb_iocb *iocb = &sp->u.iocb_cmd;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	del_timer(&iocb->timer);
628c2ecf20Sopenharmony_ci	qla2x00_rel_sp(sp);
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_civoid qla2xxx_rel_done_warning(srb_t *sp, int res)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	WARN_ONCE(1, "Calling done() of an already freed srb %p object\n", sp);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_civoid qla2xxx_rel_free_warning(srb_t *sp)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	WARN_ONCE(1, "Calling free() of an already freed srb %p object\n", sp);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* Asynchronous Login/Logout Routines -------------------------------------- */
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciunsigned long
788c2ecf20Sopenharmony_ciqla2x00_get_async_timeout(struct scsi_qla_host *vha)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	unsigned long tmo;
818c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/* Firmware should use switch negotiated r_a_tov for timeout. */
848c2ecf20Sopenharmony_ci	tmo = ha->r_a_tov / 10 * 2;
858c2ecf20Sopenharmony_ci	if (IS_QLAFX00(ha)) {
868c2ecf20Sopenharmony_ci		tmo = FX00_DEF_RATOV * 2;
878c2ecf20Sopenharmony_ci	} else if (!IS_FWI2_CAPABLE(ha)) {
888c2ecf20Sopenharmony_ci		/*
898c2ecf20Sopenharmony_ci		 * Except for earlier ISPs where the timeout is seeded from the
908c2ecf20Sopenharmony_ci		 * initialization control block.
918c2ecf20Sopenharmony_ci		 */
928c2ecf20Sopenharmony_ci		tmo = ha->login_timeout;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci	return tmo;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic void qla24xx_abort_iocb_timeout(void *data)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	srb_t *sp = data;
1008c2ecf20Sopenharmony_ci	struct srb_iocb *abt = &sp->u.iocb_cmd;
1018c2ecf20Sopenharmony_ci	struct qla_qpair *qpair = sp->qpair;
1028c2ecf20Sopenharmony_ci	u32 handle;
1038c2ecf20Sopenharmony_ci	unsigned long flags;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (sp->cmd_sp)
1068c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_async, sp->vha, 0x507c,
1078c2ecf20Sopenharmony_ci		    "Abort timeout - cmd hdl=%x, cmd type=%x hdl=%x, type=%x\n",
1088c2ecf20Sopenharmony_ci		    sp->cmd_sp->handle, sp->cmd_sp->type,
1098c2ecf20Sopenharmony_ci		    sp->handle, sp->type);
1108c2ecf20Sopenharmony_ci	else
1118c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_async, sp->vha, 0x507c,
1128c2ecf20Sopenharmony_ci		    "Abort timeout 2 - hdl=%x, type=%x\n",
1138c2ecf20Sopenharmony_ci		    sp->handle, sp->type);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1168c2ecf20Sopenharmony_ci	for (handle = 1; handle < qpair->req->num_outstanding_cmds; handle++) {
1178c2ecf20Sopenharmony_ci		if (sp->cmd_sp && (qpair->req->outstanding_cmds[handle] ==
1188c2ecf20Sopenharmony_ci		    sp->cmd_sp))
1198c2ecf20Sopenharmony_ci			qpair->req->outstanding_cmds[handle] = NULL;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci		/* removing the abort */
1228c2ecf20Sopenharmony_ci		if (qpair->req->outstanding_cmds[handle] == sp) {
1238c2ecf20Sopenharmony_ci			qpair->req->outstanding_cmds[handle] = NULL;
1248c2ecf20Sopenharmony_ci			break;
1258c2ecf20Sopenharmony_ci		}
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	if (sp->cmd_sp)
1308c2ecf20Sopenharmony_ci		sp->cmd_sp->done(sp->cmd_sp, QLA_OS_TIMER_EXPIRED);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	abt->u.abt.comp_status = cpu_to_le16(CS_TIMEOUT);
1338c2ecf20Sopenharmony_ci	sp->done(sp, QLA_OS_TIMER_EXPIRED);
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic void qla24xx_abort_sp_done(srb_t *sp, int res)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	struct srb_iocb *abt = &sp->u.iocb_cmd;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	del_timer(&sp->u.iocb_cmd.timer);
1418c2ecf20Sopenharmony_ci	if (sp->flags & SRB_WAKEUP_ON_COMP)
1428c2ecf20Sopenharmony_ci		complete(&abt->u.abt.comp);
1438c2ecf20Sopenharmony_ci	else
1448c2ecf20Sopenharmony_ci		sp->free(sp);
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ciint qla24xx_async_abort_cmd(srb_t *cmd_sp, bool wait)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	scsi_qla_host_t *vha = cmd_sp->vha;
1508c2ecf20Sopenharmony_ci	struct srb_iocb *abt_iocb;
1518c2ecf20Sopenharmony_ci	srb_t *sp;
1528c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	sp = qla2xxx_get_qpair_sp(cmd_sp->vha, cmd_sp->qpair, cmd_sp->fcport,
1558c2ecf20Sopenharmony_ci				  GFP_ATOMIC);
1568c2ecf20Sopenharmony_ci	if (!sp)
1578c2ecf20Sopenharmony_ci		return rval;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	abt_iocb = &sp->u.iocb_cmd;
1608c2ecf20Sopenharmony_ci	sp->type = SRB_ABT_CMD;
1618c2ecf20Sopenharmony_ci	sp->name = "abort";
1628c2ecf20Sopenharmony_ci	sp->qpair = cmd_sp->qpair;
1638c2ecf20Sopenharmony_ci	sp->cmd_sp = cmd_sp;
1648c2ecf20Sopenharmony_ci	if (wait)
1658c2ecf20Sopenharmony_ci		sp->flags = SRB_WAKEUP_ON_COMP;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1688c2ecf20Sopenharmony_ci	init_completion(&abt_iocb->u.abt.comp);
1698c2ecf20Sopenharmony_ci	/* FW can send 2 x ABTS's timeout/20s */
1708c2ecf20Sopenharmony_ci	qla2x00_init_timer(sp, 42);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
1738c2ecf20Sopenharmony_ci	abt_iocb->u.abt.req_que_no = cpu_to_le16(cmd_sp->qpair->req->id);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	sp->done = qla24xx_abort_sp_done;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_async, vha, 0x507c,
1788c2ecf20Sopenharmony_ci	       "Abort command issued - hdl=%x, type=%x\n", cmd_sp->handle,
1798c2ecf20Sopenharmony_ci	       cmd_sp->type);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	rval = qla2x00_start_sp(sp);
1828c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
1838c2ecf20Sopenharmony_ci		sp->free(sp);
1848c2ecf20Sopenharmony_ci		return rval;
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (wait) {
1888c2ecf20Sopenharmony_ci		wait_for_completion(&abt_iocb->u.abt.comp);
1898c2ecf20Sopenharmony_ci		rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1908c2ecf20Sopenharmony_ci			QLA_SUCCESS : QLA_FUNCTION_FAILED;
1918c2ecf20Sopenharmony_ci		sp->free(sp);
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return rval;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_civoid
1988c2ecf20Sopenharmony_ciqla2x00_async_iocb_timeout(void *data)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	srb_t *sp = data;
2018c2ecf20Sopenharmony_ci	fc_port_t *fcport = sp->fcport;
2028c2ecf20Sopenharmony_ci	struct srb_iocb *lio = &sp->u.iocb_cmd;
2038c2ecf20Sopenharmony_ci	int rc, h;
2048c2ecf20Sopenharmony_ci	unsigned long flags;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	if (fcport) {
2078c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
2088c2ecf20Sopenharmony_ci		    "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
2098c2ecf20Sopenharmony_ci		    sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci		fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
2128c2ecf20Sopenharmony_ci	} else {
2138c2ecf20Sopenharmony_ci		pr_info("Async-%s timeout - hdl=%x.\n",
2148c2ecf20Sopenharmony_ci		    sp->name, sp->handle);
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	switch (sp->type) {
2188c2ecf20Sopenharmony_ci	case SRB_LOGIN_CMD:
2198c2ecf20Sopenharmony_ci		rc = qla24xx_async_abort_cmd(sp, false);
2208c2ecf20Sopenharmony_ci		if (rc) {
2218c2ecf20Sopenharmony_ci			/* Retry as needed. */
2228c2ecf20Sopenharmony_ci			lio->u.logio.data[0] = MBS_COMMAND_ERROR;
2238c2ecf20Sopenharmony_ci			lio->u.logio.data[1] =
2248c2ecf20Sopenharmony_ci				lio->u.logio.flags & SRB_LOGIN_RETRIED ?
2258c2ecf20Sopenharmony_ci				QLA_LOGIO_LOGIN_RETRIED : 0;
2268c2ecf20Sopenharmony_ci			spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
2278c2ecf20Sopenharmony_ci			for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
2288c2ecf20Sopenharmony_ci			    h++) {
2298c2ecf20Sopenharmony_ci				if (sp->qpair->req->outstanding_cmds[h] ==
2308c2ecf20Sopenharmony_ci				    sp) {
2318c2ecf20Sopenharmony_ci					sp->qpair->req->outstanding_cmds[h] =
2328c2ecf20Sopenharmony_ci					    NULL;
2338c2ecf20Sopenharmony_ci					break;
2348c2ecf20Sopenharmony_ci				}
2358c2ecf20Sopenharmony_ci			}
2368c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
2378c2ecf20Sopenharmony_ci			sp->done(sp, QLA_FUNCTION_TIMEOUT);
2388c2ecf20Sopenharmony_ci		}
2398c2ecf20Sopenharmony_ci		break;
2408c2ecf20Sopenharmony_ci	case SRB_LOGOUT_CMD:
2418c2ecf20Sopenharmony_ci	case SRB_CT_PTHRU_CMD:
2428c2ecf20Sopenharmony_ci	case SRB_MB_IOCB:
2438c2ecf20Sopenharmony_ci	case SRB_NACK_PLOGI:
2448c2ecf20Sopenharmony_ci	case SRB_NACK_PRLI:
2458c2ecf20Sopenharmony_ci	case SRB_NACK_LOGO:
2468c2ecf20Sopenharmony_ci	case SRB_CTRL_VP:
2478c2ecf20Sopenharmony_ci	default:
2488c2ecf20Sopenharmony_ci		rc = qla24xx_async_abort_cmd(sp, false);
2498c2ecf20Sopenharmony_ci		if (rc) {
2508c2ecf20Sopenharmony_ci			spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
2518c2ecf20Sopenharmony_ci			for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
2528c2ecf20Sopenharmony_ci			    h++) {
2538c2ecf20Sopenharmony_ci				if (sp->qpair->req->outstanding_cmds[h] ==
2548c2ecf20Sopenharmony_ci				    sp) {
2558c2ecf20Sopenharmony_ci					sp->qpair->req->outstanding_cmds[h] =
2568c2ecf20Sopenharmony_ci					    NULL;
2578c2ecf20Sopenharmony_ci					break;
2588c2ecf20Sopenharmony_ci				}
2598c2ecf20Sopenharmony_ci			}
2608c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
2618c2ecf20Sopenharmony_ci			sp->done(sp, QLA_FUNCTION_TIMEOUT);
2628c2ecf20Sopenharmony_ci		}
2638c2ecf20Sopenharmony_ci		break;
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic void qla2x00_async_login_sp_done(srb_t *sp, int res)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = sp->vha;
2708c2ecf20Sopenharmony_ci	struct srb_iocb *lio = &sp->u.iocb_cmd;
2718c2ecf20Sopenharmony_ci	struct event_arg ea;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20dd,
2748c2ecf20Sopenharmony_ci	    "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	if (!test_bit(UNLOADING, &vha->dpc_flags)) {
2798c2ecf20Sopenharmony_ci		memset(&ea, 0, sizeof(ea));
2808c2ecf20Sopenharmony_ci		ea.fcport = sp->fcport;
2818c2ecf20Sopenharmony_ci		ea.data[0] = lio->u.logio.data[0];
2828c2ecf20Sopenharmony_ci		ea.data[1] = lio->u.logio.data[1];
2838c2ecf20Sopenharmony_ci		ea.iop[0] = lio->u.logio.iop[0];
2848c2ecf20Sopenharmony_ci		ea.iop[1] = lio->u.logio.iop[1];
2858c2ecf20Sopenharmony_ci		ea.sp = sp;
2868c2ecf20Sopenharmony_ci		qla24xx_handle_plogi_done_event(vha, &ea);
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	sp->free(sp);
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic inline bool
2938c2ecf20Sopenharmony_cifcport_is_smaller(fc_port_t *fcport)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	if (wwn_to_u64(fcport->port_name) <
2968c2ecf20Sopenharmony_ci	    wwn_to_u64(fcport->vha->port_name))
2978c2ecf20Sopenharmony_ci		return true;
2988c2ecf20Sopenharmony_ci	else
2998c2ecf20Sopenharmony_ci		return false;
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic inline bool
3038c2ecf20Sopenharmony_cifcport_is_bigger(fc_port_t *fcport)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	return !fcport_is_smaller(fcport);
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ciint
3098c2ecf20Sopenharmony_ciqla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
3108c2ecf20Sopenharmony_ci    uint16_t *data)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	srb_t *sp;
3138c2ecf20Sopenharmony_ci	struct srb_iocb *lio;
3148c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT) ||
3178c2ecf20Sopenharmony_ci	    fcport->loop_id == FC_NO_LOOP_ID) {
3188c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xffff,
3198c2ecf20Sopenharmony_ci		    "%s: %8phC - not sending command.\n",
3208c2ecf20Sopenharmony_ci		    __func__, fcport->port_name);
3218c2ecf20Sopenharmony_ci		return rval;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
3258c2ecf20Sopenharmony_ci	if (!sp)
3268c2ecf20Sopenharmony_ci		goto done;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_PEND);
3298c2ecf20Sopenharmony_ci	fcport->flags |= FCF_ASYNC_SENT;
3308c2ecf20Sopenharmony_ci	fcport->logout_completed = 0;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	sp->type = SRB_LOGIN_CMD;
3338c2ecf20Sopenharmony_ci	sp->name = "login";
3348c2ecf20Sopenharmony_ci	sp->gen1 = fcport->rscn_gen;
3358c2ecf20Sopenharmony_ci	sp->gen2 = fcport->login_gen;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	lio = &sp->u.iocb_cmd;
3388c2ecf20Sopenharmony_ci	lio->timeout = qla2x00_async_iocb_timeout;
3398c2ecf20Sopenharmony_ci	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	sp->done = qla2x00_async_login_sp_done;
3428c2ecf20Sopenharmony_ci	if (N2N_TOPO(fcport->vha->hw) && fcport_is_bigger(fcport))
3438c2ecf20Sopenharmony_ci		lio->u.logio.flags |= SRB_LOGIN_PRLI_ONLY;
3448c2ecf20Sopenharmony_ci	else
3458c2ecf20Sopenharmony_ci		lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	if (NVME_TARGET(vha->hw, fcport))
3488c2ecf20Sopenharmony_ci		lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2072,
3518c2ecf20Sopenharmony_ci	    "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
3528c2ecf20Sopenharmony_ci		"retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
3538c2ecf20Sopenharmony_ci	    fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
3548c2ecf20Sopenharmony_ci	    fcport->login_retry);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	rval = qla2x00_start_sp(sp);
3578c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
3588c2ecf20Sopenharmony_ci		fcport->flags |= FCF_LOGIN_NEEDED;
3598c2ecf20Sopenharmony_ci		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
3608c2ecf20Sopenharmony_ci		goto done_free_sp;
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	return rval;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cidone_free_sp:
3668c2ecf20Sopenharmony_ci	sp->free(sp);
3678c2ecf20Sopenharmony_ci	fcport->flags &= ~FCF_ASYNC_SENT;
3688c2ecf20Sopenharmony_cidone:
3698c2ecf20Sopenharmony_ci	fcport->flags &= ~FCF_ASYNC_ACTIVE;
3708c2ecf20Sopenharmony_ci	return rval;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic void qla2x00_async_logout_sp_done(srb_t *sp, int res)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
3768c2ecf20Sopenharmony_ci	sp->fcport->login_gen++;
3778c2ecf20Sopenharmony_ci	qlt_logo_completion_handler(sp->fcport, res);
3788c2ecf20Sopenharmony_ci	sp->free(sp);
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ciint
3828c2ecf20Sopenharmony_ciqla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	srb_t *sp;
3858c2ecf20Sopenharmony_ci	struct srb_iocb *lio;
3868c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	fcport->flags |= FCF_ASYNC_SENT;
3898c2ecf20Sopenharmony_ci	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
3908c2ecf20Sopenharmony_ci	if (!sp)
3918c2ecf20Sopenharmony_ci		goto done;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	sp->type = SRB_LOGOUT_CMD;
3948c2ecf20Sopenharmony_ci	sp->name = "logout";
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	lio = &sp->u.iocb_cmd;
3978c2ecf20Sopenharmony_ci	lio->timeout = qla2x00_async_iocb_timeout;
3988c2ecf20Sopenharmony_ci	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	sp->done = qla2x00_async_logout_sp_done;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2070,
4038c2ecf20Sopenharmony_ci	    "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
4048c2ecf20Sopenharmony_ci	    sp->handle, fcport->loop_id, fcport->d_id.b.domain,
4058c2ecf20Sopenharmony_ci		fcport->d_id.b.area, fcport->d_id.b.al_pa,
4068c2ecf20Sopenharmony_ci		fcport->port_name);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	rval = qla2x00_start_sp(sp);
4098c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS)
4108c2ecf20Sopenharmony_ci		goto done_free_sp;
4118c2ecf20Sopenharmony_ci	return rval;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cidone_free_sp:
4148c2ecf20Sopenharmony_ci	sp->free(sp);
4158c2ecf20Sopenharmony_cidone:
4168c2ecf20Sopenharmony_ci	fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
4178c2ecf20Sopenharmony_ci	return rval;
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_civoid
4218c2ecf20Sopenharmony_ciqla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
4228c2ecf20Sopenharmony_ci    uint16_t *data)
4238c2ecf20Sopenharmony_ci{
4248c2ecf20Sopenharmony_ci	fcport->flags &= ~FCF_ASYNC_ACTIVE;
4258c2ecf20Sopenharmony_ci	/* Don't re-login in target mode */
4268c2ecf20Sopenharmony_ci	if (!fcport->tgt_session)
4278c2ecf20Sopenharmony_ci		qla2x00_mark_device_lost(vha, fcport, 1);
4288c2ecf20Sopenharmony_ci	qlt_logo_completion_handler(fcport, data[0]);
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic void qla2x00_async_prlo_sp_done(srb_t *sp, int res)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	struct srb_iocb *lio = &sp->u.iocb_cmd;
4348c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = sp->vha;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	sp->fcport->flags &= ~FCF_ASYNC_ACTIVE;
4378c2ecf20Sopenharmony_ci	if (!test_bit(UNLOADING, &vha->dpc_flags))
4388c2ecf20Sopenharmony_ci		qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
4398c2ecf20Sopenharmony_ci		    lio->u.logio.data);
4408c2ecf20Sopenharmony_ci	sp->free(sp);
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ciint
4448c2ecf20Sopenharmony_ciqla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	srb_t *sp;
4478c2ecf20Sopenharmony_ci	struct srb_iocb *lio;
4488c2ecf20Sopenharmony_ci	int rval;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	rval = QLA_FUNCTION_FAILED;
4518c2ecf20Sopenharmony_ci	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
4528c2ecf20Sopenharmony_ci	if (!sp)
4538c2ecf20Sopenharmony_ci		goto done;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	sp->type = SRB_PRLO_CMD;
4568c2ecf20Sopenharmony_ci	sp->name = "prlo";
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	lio = &sp->u.iocb_cmd;
4598c2ecf20Sopenharmony_ci	lio->timeout = qla2x00_async_iocb_timeout;
4608c2ecf20Sopenharmony_ci	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	sp->done = qla2x00_async_prlo_sp_done;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2070,
4658c2ecf20Sopenharmony_ci	    "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
4668c2ecf20Sopenharmony_ci	    sp->handle, fcport->loop_id, fcport->d_id.b.domain,
4678c2ecf20Sopenharmony_ci	    fcport->d_id.b.area, fcport->d_id.b.al_pa);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	rval = qla2x00_start_sp(sp);
4708c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS)
4718c2ecf20Sopenharmony_ci		goto done_free_sp;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	return rval;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_cidone_free_sp:
4768c2ecf20Sopenharmony_ci	sp->free(sp);
4778c2ecf20Sopenharmony_cidone:
4788c2ecf20Sopenharmony_ci	fcport->flags &= ~FCF_ASYNC_ACTIVE;
4798c2ecf20Sopenharmony_ci	return rval;
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic
4838c2ecf20Sopenharmony_civoid qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct fc_port *fcport = ea->fcport;
4868c2ecf20Sopenharmony_ci	unsigned long flags;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20d2,
4898c2ecf20Sopenharmony_ci	    "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
4908c2ecf20Sopenharmony_ci	    __func__, fcport->port_name, fcport->disc_state,
4918c2ecf20Sopenharmony_ci	    fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
4928c2ecf20Sopenharmony_ci	    fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	WARN_ONCE(!qla2xxx_is_valid_mbs(ea->data[0]), "mbs: %#x\n",
4958c2ecf20Sopenharmony_ci		  ea->data[0]);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	if (ea->data[0] != MBS_COMMAND_COMPLETE) {
4988c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x2066,
4998c2ecf20Sopenharmony_ci		    "%s %8phC: adisc fail: post delete\n",
5008c2ecf20Sopenharmony_ci		    __func__, ea->fcport->port_name);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci		spin_lock_irqsave(&vha->work_lock, flags);
5038c2ecf20Sopenharmony_ci		/* deleted = 0 & logout_on_delete = force fw cleanup */
5048c2ecf20Sopenharmony_ci		if (fcport->deleted == QLA_SESS_DELETED)
5058c2ecf20Sopenharmony_ci			fcport->deleted = 0;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci		fcport->logout_on_delete = 1;
5088c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vha->work_lock, flags);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci		qlt_schedule_sess_for_deletion(ea->fcport);
5118c2ecf20Sopenharmony_ci		return;
5128c2ecf20Sopenharmony_ci	}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	if (ea->fcport->disc_state == DSC_DELETE_PEND)
5158c2ecf20Sopenharmony_ci		return;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	if (ea->sp->gen2 != ea->fcport->login_gen) {
5188c2ecf20Sopenharmony_ci		/* target side must have changed it. */
5198c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20d3,
5208c2ecf20Sopenharmony_ci		    "%s %8phC generation changed\n",
5218c2ecf20Sopenharmony_ci		    __func__, ea->fcport->port_name);
5228c2ecf20Sopenharmony_ci		return;
5238c2ecf20Sopenharmony_ci	} else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
5248c2ecf20Sopenharmony_ci		qla_rscn_replay(fcport);
5258c2ecf20Sopenharmony_ci		qlt_schedule_sess_for_deletion(fcport);
5268c2ecf20Sopenharmony_ci		return;
5278c2ecf20Sopenharmony_ci	}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	__qla24xx_handle_gpdb_event(vha, ea);
5308c2ecf20Sopenharmony_ci}
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_cistatic int qla_post_els_plogi_work(struct scsi_qla_host *vha, fc_port_t *fcport)
5338c2ecf20Sopenharmony_ci{
5348c2ecf20Sopenharmony_ci	struct qla_work_evt *e;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	e = qla2x00_alloc_work(vha, QLA_EVT_ELS_PLOGI);
5378c2ecf20Sopenharmony_ci	if (!e)
5388c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	e->u.fcport.fcport = fcport;
5418c2ecf20Sopenharmony_ci	fcport->flags |= FCF_ASYNC_ACTIVE;
5428c2ecf20Sopenharmony_ci	qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_PEND);
5438c2ecf20Sopenharmony_ci	return qla2x00_post_work(vha, e);
5448c2ecf20Sopenharmony_ci}
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_cistatic void qla2x00_async_adisc_sp_done(srb_t *sp, int res)
5478c2ecf20Sopenharmony_ci{
5488c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = sp->vha;
5498c2ecf20Sopenharmony_ci	struct event_arg ea;
5508c2ecf20Sopenharmony_ci	struct srb_iocb *lio = &sp->u.iocb_cmd;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2066,
5538c2ecf20Sopenharmony_ci	    "Async done-%s res %x %8phC\n",
5548c2ecf20Sopenharmony_ci	    sp->name, res, sp->fcport->port_name);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	memset(&ea, 0, sizeof(ea));
5598c2ecf20Sopenharmony_ci	ea.rc = res;
5608c2ecf20Sopenharmony_ci	ea.data[0] = lio->u.logio.data[0];
5618c2ecf20Sopenharmony_ci	ea.data[1] = lio->u.logio.data[1];
5628c2ecf20Sopenharmony_ci	ea.iop[0] = lio->u.logio.iop[0];
5638c2ecf20Sopenharmony_ci	ea.iop[1] = lio->u.logio.iop[1];
5648c2ecf20Sopenharmony_ci	ea.fcport = sp->fcport;
5658c2ecf20Sopenharmony_ci	ea.sp = sp;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	qla24xx_handle_adisc_event(vha, &ea);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	sp->free(sp);
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ciint
5738c2ecf20Sopenharmony_ciqla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
5748c2ecf20Sopenharmony_ci    uint16_t *data)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	srb_t *sp;
5778c2ecf20Sopenharmony_ci	struct srb_iocb *lio;
5788c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	if (IS_SESSION_DELETED(fcport)) {
5818c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xffff,
5828c2ecf20Sopenharmony_ci		       "%s: %8phC is being delete - not sending command.\n",
5838c2ecf20Sopenharmony_ci		       __func__, fcport->port_name);
5848c2ecf20Sopenharmony_ci		fcport->flags &= ~FCF_ASYNC_ACTIVE;
5858c2ecf20Sopenharmony_ci		return rval;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
5898c2ecf20Sopenharmony_ci		return rval;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	fcport->flags |= FCF_ASYNC_SENT;
5928c2ecf20Sopenharmony_ci	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
5938c2ecf20Sopenharmony_ci	if (!sp)
5948c2ecf20Sopenharmony_ci		goto done;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	sp->type = SRB_ADISC_CMD;
5978c2ecf20Sopenharmony_ci	sp->name = "adisc";
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	lio = &sp->u.iocb_cmd;
6008c2ecf20Sopenharmony_ci	lio->timeout = qla2x00_async_iocb_timeout;
6018c2ecf20Sopenharmony_ci	sp->gen1 = fcport->rscn_gen;
6028c2ecf20Sopenharmony_ci	sp->gen2 = fcport->login_gen;
6038c2ecf20Sopenharmony_ci	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	sp->done = qla2x00_async_adisc_sp_done;
6068c2ecf20Sopenharmony_ci	if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
6078c2ecf20Sopenharmony_ci		lio->u.logio.flags |= SRB_LOGIN_RETRIED;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x206f,
6108c2ecf20Sopenharmony_ci	    "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
6118c2ecf20Sopenharmony_ci	    sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	rval = qla2x00_start_sp(sp);
6148c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS)
6158c2ecf20Sopenharmony_ci		goto done_free_sp;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	return rval;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_cidone_free_sp:
6208c2ecf20Sopenharmony_ci	sp->free(sp);
6218c2ecf20Sopenharmony_cidone:
6228c2ecf20Sopenharmony_ci	fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
6238c2ecf20Sopenharmony_ci	qla2x00_post_async_adisc_work(vha, fcport, data);
6248c2ecf20Sopenharmony_ci	return rval;
6258c2ecf20Sopenharmony_ci}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_cistatic bool qla2x00_is_reserved_id(scsi_qla_host_t *vha, uint16_t loop_id)
6288c2ecf20Sopenharmony_ci{
6298c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	if (IS_FWI2_CAPABLE(ha))
6328c2ecf20Sopenharmony_ci		return loop_id > NPH_LAST_HANDLE;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	return (loop_id > ha->max_loop_id && loop_id < SNS_FIRST_LOOP_ID) ||
6358c2ecf20Sopenharmony_ci		loop_id == MANAGEMENT_SERVER || loop_id == BROADCAST;
6368c2ecf20Sopenharmony_ci}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci/**
6398c2ecf20Sopenharmony_ci * qla2x00_find_new_loop_id - scan through our port list and find a new usable loop ID
6408c2ecf20Sopenharmony_ci * @vha: adapter state pointer.
6418c2ecf20Sopenharmony_ci * @dev: port structure pointer.
6428c2ecf20Sopenharmony_ci *
6438c2ecf20Sopenharmony_ci * Returns:
6448c2ecf20Sopenharmony_ci *	qla2x00 local function return status code.
6458c2ecf20Sopenharmony_ci *
6468c2ecf20Sopenharmony_ci * Context:
6478c2ecf20Sopenharmony_ci *	Kernel context.
6488c2ecf20Sopenharmony_ci */
6498c2ecf20Sopenharmony_cistatic int qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	int	rval;
6528c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
6538c2ecf20Sopenharmony_ci	unsigned long flags = 0;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	rval = QLA_SUCCESS;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->vport_slock, flags);
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	dev->loop_id = find_first_zero_bit(ha->loop_id_map, LOOPID_MAP_SIZE);
6608c2ecf20Sopenharmony_ci	if (dev->loop_id >= LOOPID_MAP_SIZE ||
6618c2ecf20Sopenharmony_ci	    qla2x00_is_reserved_id(vha, dev->loop_id)) {
6628c2ecf20Sopenharmony_ci		dev->loop_id = FC_NO_LOOP_ID;
6638c2ecf20Sopenharmony_ci		rval = QLA_FUNCTION_FAILED;
6648c2ecf20Sopenharmony_ci	} else {
6658c2ecf20Sopenharmony_ci		set_bit(dev->loop_id, ha->loop_id_map);
6668c2ecf20Sopenharmony_ci	}
6678c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->vport_slock, flags);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS)
6708c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
6718c2ecf20Sopenharmony_ci		       "Assigning new loopid=%x, portid=%x.\n",
6728c2ecf20Sopenharmony_ci		       dev->loop_id, dev->d_id.b24);
6738c2ecf20Sopenharmony_ci	else
6748c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, dev->vha, 0x2087,
6758c2ecf20Sopenharmony_ci		       "No loop_id's available, portid=%x.\n",
6768c2ecf20Sopenharmony_ci		       dev->d_id.b24);
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	return rval;
6798c2ecf20Sopenharmony_ci}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_civoid qla2x00_clear_loop_id(fc_port_t *fcport)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = fcport->vha->hw;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	if (fcport->loop_id == FC_NO_LOOP_ID ||
6868c2ecf20Sopenharmony_ci	    qla2x00_is_reserved_id(fcport->vha, fcport->loop_id))
6878c2ecf20Sopenharmony_ci		return;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	clear_bit(fcport->loop_id, ha->loop_id_map);
6908c2ecf20Sopenharmony_ci	fcport->loop_id = FC_NO_LOOP_ID;
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_cistatic void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
6948c2ecf20Sopenharmony_ci	struct event_arg *ea)
6958c2ecf20Sopenharmony_ci{
6968c2ecf20Sopenharmony_ci	fc_port_t *fcport, *conflict_fcport;
6978c2ecf20Sopenharmony_ci	struct get_name_list_extended *e;
6988c2ecf20Sopenharmony_ci	u16 i, n, found = 0, loop_id;
6998c2ecf20Sopenharmony_ci	port_id_t id;
7008c2ecf20Sopenharmony_ci	u64 wwn;
7018c2ecf20Sopenharmony_ci	u16 data[2];
7028c2ecf20Sopenharmony_ci	u8 current_login_state, nvme_cls;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	fcport = ea->fcport;
7058c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0xffff,
7068c2ecf20Sopenharmony_ci	    "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
7078c2ecf20Sopenharmony_ci	    __func__, fcport->port_name, fcport->disc_state,
7088c2ecf20Sopenharmony_ci	    fcport->fw_login_state, ea->rc,
7098c2ecf20Sopenharmony_ci	    fcport->login_gen, fcport->last_login_gen,
7108c2ecf20Sopenharmony_ci	    fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	if (fcport->disc_state == DSC_DELETE_PEND)
7138c2ecf20Sopenharmony_ci		return;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	if (ea->rc) { /* rval */
7168c2ecf20Sopenharmony_ci		if (fcport->login_retry == 0) {
7178c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x20de,
7188c2ecf20Sopenharmony_ci			    "GNL failed Port login retry %8phN, retry cnt=%d.\n",
7198c2ecf20Sopenharmony_ci			    fcport->port_name, fcport->login_retry);
7208c2ecf20Sopenharmony_ci		}
7218c2ecf20Sopenharmony_ci		return;
7228c2ecf20Sopenharmony_ci	}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	if (fcport->last_rscn_gen != fcport->rscn_gen) {
7258c2ecf20Sopenharmony_ci		qla_rscn_replay(fcport);
7268c2ecf20Sopenharmony_ci		qlt_schedule_sess_for_deletion(fcport);
7278c2ecf20Sopenharmony_ci		return;
7288c2ecf20Sopenharmony_ci	} else if (fcport->last_login_gen != fcport->login_gen) {
7298c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20e0,
7308c2ecf20Sopenharmony_ci		    "%s %8phC login gen changed\n",
7318c2ecf20Sopenharmony_ci		    __func__, fcport->port_name);
7328c2ecf20Sopenharmony_ci		return;
7338c2ecf20Sopenharmony_ci	}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	n = ea->data[0] / sizeof(struct get_name_list_extended);
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20e1,
7388c2ecf20Sopenharmony_ci	    "%s %d %8phC n %d %02x%02x%02x lid %d \n",
7398c2ecf20Sopenharmony_ci	    __func__, __LINE__, fcport->port_name, n,
7408c2ecf20Sopenharmony_ci	    fcport->d_id.b.domain, fcport->d_id.b.area,
7418c2ecf20Sopenharmony_ci	    fcport->d_id.b.al_pa, fcport->loop_id);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++) {
7448c2ecf20Sopenharmony_ci		e = &vha->gnl.l[i];
7458c2ecf20Sopenharmony_ci		wwn = wwn_to_u64(e->port_name);
7468c2ecf20Sopenharmony_ci		id.b.domain = e->port_id[2];
7478c2ecf20Sopenharmony_ci		id.b.area = e->port_id[1];
7488c2ecf20Sopenharmony_ci		id.b.al_pa = e->port_id[0];
7498c2ecf20Sopenharmony_ci		id.b.rsvd_1 = 0;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci		if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
7528c2ecf20Sopenharmony_ci			continue;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci		if (IS_SW_RESV_ADDR(id))
7558c2ecf20Sopenharmony_ci			continue;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci		found = 1;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci		loop_id = le16_to_cpu(e->nport_handle);
7608c2ecf20Sopenharmony_ci		loop_id = (loop_id & 0x7fff);
7618c2ecf20Sopenharmony_ci		nvme_cls = e->current_login_state >> 4;
7628c2ecf20Sopenharmony_ci		current_login_state = e->current_login_state & 0xf;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci		if (PRLI_PHASE(nvme_cls)) {
7658c2ecf20Sopenharmony_ci			current_login_state = nvme_cls;
7668c2ecf20Sopenharmony_ci			fcport->fc4_type &= ~FS_FC4TYPE_FCP;
7678c2ecf20Sopenharmony_ci			fcport->fc4_type |= FS_FC4TYPE_NVME;
7688c2ecf20Sopenharmony_ci		} else if (PRLI_PHASE(current_login_state)) {
7698c2ecf20Sopenharmony_ci			fcport->fc4_type |= FS_FC4TYPE_FCP;
7708c2ecf20Sopenharmony_ci			fcport->fc4_type &= ~FS_FC4TYPE_NVME;
7718c2ecf20Sopenharmony_ci		}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20e2,
7748c2ecf20Sopenharmony_ci		    "%s found %8phC CLS [%x|%x] fc4_type %d ID[%06x|%06x] lid[%d|%d]\n",
7758c2ecf20Sopenharmony_ci		    __func__, fcport->port_name,
7768c2ecf20Sopenharmony_ci		    e->current_login_state, fcport->fw_login_state,
7778c2ecf20Sopenharmony_ci		    fcport->fc4_type, id.b24, fcport->d_id.b24,
7788c2ecf20Sopenharmony_ci		    loop_id, fcport->loop_id);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci		switch (fcport->disc_state) {
7818c2ecf20Sopenharmony_ci		case DSC_DELETE_PEND:
7828c2ecf20Sopenharmony_ci		case DSC_DELETED:
7838c2ecf20Sopenharmony_ci			break;
7848c2ecf20Sopenharmony_ci		default:
7858c2ecf20Sopenharmony_ci			if ((id.b24 != fcport->d_id.b24 &&
7868c2ecf20Sopenharmony_ci			    fcport->d_id.b24 &&
7878c2ecf20Sopenharmony_ci			    fcport->loop_id != FC_NO_LOOP_ID) ||
7888c2ecf20Sopenharmony_ci			    (fcport->loop_id != FC_NO_LOOP_ID &&
7898c2ecf20Sopenharmony_ci				fcport->loop_id != loop_id)) {
7908c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x20e3,
7918c2ecf20Sopenharmony_ci				    "%s %d %8phC post del sess\n",
7928c2ecf20Sopenharmony_ci				    __func__, __LINE__, fcport->port_name);
7938c2ecf20Sopenharmony_ci				if (fcport->n2n_flag)
7948c2ecf20Sopenharmony_ci					fcport->d_id.b24 = 0;
7958c2ecf20Sopenharmony_ci				qlt_schedule_sess_for_deletion(fcport);
7968c2ecf20Sopenharmony_ci				return;
7978c2ecf20Sopenharmony_ci			}
7988c2ecf20Sopenharmony_ci			break;
7998c2ecf20Sopenharmony_ci		}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci		fcport->loop_id = loop_id;
8028c2ecf20Sopenharmony_ci		if (fcport->n2n_flag)
8038c2ecf20Sopenharmony_ci			fcport->d_id.b24 = id.b24;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci		wwn = wwn_to_u64(fcport->port_name);
8068c2ecf20Sopenharmony_ci		qlt_find_sess_invalidate_other(vha, wwn,
8078c2ecf20Sopenharmony_ci			id, loop_id, &conflict_fcport);
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci		if (conflict_fcport) {
8108c2ecf20Sopenharmony_ci			/*
8118c2ecf20Sopenharmony_ci			 * Another share fcport share the same loop_id &
8128c2ecf20Sopenharmony_ci			 * nport id. Conflict fcport needs to finish
8138c2ecf20Sopenharmony_ci			 * cleanup before this fcport can proceed to login.
8148c2ecf20Sopenharmony_ci			 */
8158c2ecf20Sopenharmony_ci			conflict_fcport->conflict = fcport;
8168c2ecf20Sopenharmony_ci			fcport->login_pause = 1;
8178c2ecf20Sopenharmony_ci		}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci		switch (vha->hw->current_topology) {
8208c2ecf20Sopenharmony_ci		default:
8218c2ecf20Sopenharmony_ci			switch (current_login_state) {
8228c2ecf20Sopenharmony_ci			case DSC_LS_PRLI_COMP:
8238c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc + ql_dbg_verbose,
8248c2ecf20Sopenharmony_ci				    vha, 0x20e4, "%s %d %8phC post gpdb\n",
8258c2ecf20Sopenharmony_ci				    __func__, __LINE__, fcport->port_name);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci				if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
8288c2ecf20Sopenharmony_ci					fcport->port_type = FCT_INITIATOR;
8298c2ecf20Sopenharmony_ci				else
8308c2ecf20Sopenharmony_ci					fcport->port_type = FCT_TARGET;
8318c2ecf20Sopenharmony_ci				data[0] = data[1] = 0;
8328c2ecf20Sopenharmony_ci				qla2x00_post_async_adisc_work(vha, fcport,
8338c2ecf20Sopenharmony_ci				    data);
8348c2ecf20Sopenharmony_ci				break;
8358c2ecf20Sopenharmony_ci			case DSC_LS_PORT_UNAVAIL:
8368c2ecf20Sopenharmony_ci			default:
8378c2ecf20Sopenharmony_ci				if (fcport->loop_id == FC_NO_LOOP_ID) {
8388c2ecf20Sopenharmony_ci					qla2x00_find_new_loop_id(vha, fcport);
8398c2ecf20Sopenharmony_ci					fcport->fw_login_state =
8408c2ecf20Sopenharmony_ci					    DSC_LS_PORT_UNAVAIL;
8418c2ecf20Sopenharmony_ci				}
8428c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x20e5,
8438c2ecf20Sopenharmony_ci				    "%s %d %8phC\n", __func__, __LINE__,
8448c2ecf20Sopenharmony_ci				    fcport->port_name);
8458c2ecf20Sopenharmony_ci				qla24xx_fcport_handle_login(vha, fcport);
8468c2ecf20Sopenharmony_ci				break;
8478c2ecf20Sopenharmony_ci			}
8488c2ecf20Sopenharmony_ci			break;
8498c2ecf20Sopenharmony_ci		case ISP_CFG_N:
8508c2ecf20Sopenharmony_ci			fcport->fw_login_state = current_login_state;
8518c2ecf20Sopenharmony_ci			fcport->d_id = id;
8528c2ecf20Sopenharmony_ci			switch (current_login_state) {
8538c2ecf20Sopenharmony_ci			case DSC_LS_PRLI_PEND:
8548c2ecf20Sopenharmony_ci				/*
8558c2ecf20Sopenharmony_ci				 * In the middle of PRLI. Let it finish.
8568c2ecf20Sopenharmony_ci				 * Allow relogin code to recheck state again
8578c2ecf20Sopenharmony_ci				 * with GNL. Push disc_state back to DELETED
8588c2ecf20Sopenharmony_ci				 * so GNL can go out again
8598c2ecf20Sopenharmony_ci				 */
8608c2ecf20Sopenharmony_ci				qla2x00_set_fcport_disc_state(fcport,
8618c2ecf20Sopenharmony_ci				    DSC_DELETED);
8628c2ecf20Sopenharmony_ci				break;
8638c2ecf20Sopenharmony_ci			case DSC_LS_PRLI_COMP:
8648c2ecf20Sopenharmony_ci				if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
8658c2ecf20Sopenharmony_ci					fcport->port_type = FCT_INITIATOR;
8668c2ecf20Sopenharmony_ci				else
8678c2ecf20Sopenharmony_ci					fcport->port_type = FCT_TARGET;
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci				data[0] = data[1] = 0;
8708c2ecf20Sopenharmony_ci				qla2x00_post_async_adisc_work(vha, fcport,
8718c2ecf20Sopenharmony_ci				    data);
8728c2ecf20Sopenharmony_ci				break;
8738c2ecf20Sopenharmony_ci			case DSC_LS_PLOGI_COMP:
8748c2ecf20Sopenharmony_ci				if (fcport_is_bigger(fcport)) {
8758c2ecf20Sopenharmony_ci					/* local adapter is smaller */
8768c2ecf20Sopenharmony_ci					if (fcport->loop_id != FC_NO_LOOP_ID)
8778c2ecf20Sopenharmony_ci						qla2x00_clear_loop_id(fcport);
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci					fcport->loop_id = loop_id;
8808c2ecf20Sopenharmony_ci					qla24xx_fcport_handle_login(vha,
8818c2ecf20Sopenharmony_ci					    fcport);
8828c2ecf20Sopenharmony_ci					break;
8838c2ecf20Sopenharmony_ci				}
8848c2ecf20Sopenharmony_ci				fallthrough;
8858c2ecf20Sopenharmony_ci			default:
8868c2ecf20Sopenharmony_ci				if (fcport_is_smaller(fcport)) {
8878c2ecf20Sopenharmony_ci					/* local adapter is bigger */
8888c2ecf20Sopenharmony_ci					if (fcport->loop_id != FC_NO_LOOP_ID)
8898c2ecf20Sopenharmony_ci						qla2x00_clear_loop_id(fcport);
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci					fcport->loop_id = loop_id;
8928c2ecf20Sopenharmony_ci					qla24xx_fcport_handle_login(vha,
8938c2ecf20Sopenharmony_ci					    fcport);
8948c2ecf20Sopenharmony_ci				}
8958c2ecf20Sopenharmony_ci				break;
8968c2ecf20Sopenharmony_ci			}
8978c2ecf20Sopenharmony_ci			break;
8988c2ecf20Sopenharmony_ci		} /* switch (ha->current_topology) */
8998c2ecf20Sopenharmony_ci	}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	if (!found) {
9028c2ecf20Sopenharmony_ci		switch (vha->hw->current_topology) {
9038c2ecf20Sopenharmony_ci		case ISP_CFG_F:
9048c2ecf20Sopenharmony_ci		case ISP_CFG_FL:
9058c2ecf20Sopenharmony_ci			for (i = 0; i < n; i++) {
9068c2ecf20Sopenharmony_ci				e = &vha->gnl.l[i];
9078c2ecf20Sopenharmony_ci				id.b.domain = e->port_id[0];
9088c2ecf20Sopenharmony_ci				id.b.area = e->port_id[1];
9098c2ecf20Sopenharmony_ci				id.b.al_pa = e->port_id[2];
9108c2ecf20Sopenharmony_ci				id.b.rsvd_1 = 0;
9118c2ecf20Sopenharmony_ci				loop_id = le16_to_cpu(e->nport_handle);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci				if (fcport->d_id.b24 == id.b24) {
9148c2ecf20Sopenharmony_ci					conflict_fcport =
9158c2ecf20Sopenharmony_ci					    qla2x00_find_fcport_by_wwpn(vha,
9168c2ecf20Sopenharmony_ci						e->port_name, 0);
9178c2ecf20Sopenharmony_ci					if (conflict_fcport) {
9188c2ecf20Sopenharmony_ci						ql_dbg(ql_dbg_disc + ql_dbg_verbose,
9198c2ecf20Sopenharmony_ci						    vha, 0x20e5,
9208c2ecf20Sopenharmony_ci						    "%s %d %8phC post del sess\n",
9218c2ecf20Sopenharmony_ci						    __func__, __LINE__,
9228c2ecf20Sopenharmony_ci						    conflict_fcport->port_name);
9238c2ecf20Sopenharmony_ci						qlt_schedule_sess_for_deletion
9248c2ecf20Sopenharmony_ci							(conflict_fcport);
9258c2ecf20Sopenharmony_ci					}
9268c2ecf20Sopenharmony_ci				}
9278c2ecf20Sopenharmony_ci				/*
9288c2ecf20Sopenharmony_ci				 * FW already picked this loop id for
9298c2ecf20Sopenharmony_ci				 * another fcport
9308c2ecf20Sopenharmony_ci				 */
9318c2ecf20Sopenharmony_ci				if (fcport->loop_id == loop_id)
9328c2ecf20Sopenharmony_ci					fcport->loop_id = FC_NO_LOOP_ID;
9338c2ecf20Sopenharmony_ci			}
9348c2ecf20Sopenharmony_ci			qla24xx_fcport_handle_login(vha, fcport);
9358c2ecf20Sopenharmony_ci			break;
9368c2ecf20Sopenharmony_ci		case ISP_CFG_N:
9378c2ecf20Sopenharmony_ci			qla2x00_set_fcport_disc_state(fcport, DSC_DELETED);
9388c2ecf20Sopenharmony_ci			if (time_after_eq(jiffies, fcport->dm_login_expire)) {
9398c2ecf20Sopenharmony_ci				if (fcport->n2n_link_reset_cnt < 2) {
9408c2ecf20Sopenharmony_ci					fcport->n2n_link_reset_cnt++;
9418c2ecf20Sopenharmony_ci					/*
9428c2ecf20Sopenharmony_ci					 * remote port is not sending PLOGI.
9438c2ecf20Sopenharmony_ci					 * Reset link to kick start his state
9448c2ecf20Sopenharmony_ci					 * machine
9458c2ecf20Sopenharmony_ci					 */
9468c2ecf20Sopenharmony_ci					set_bit(N2N_LINK_RESET,
9478c2ecf20Sopenharmony_ci					    &vha->dpc_flags);
9488c2ecf20Sopenharmony_ci				} else {
9498c2ecf20Sopenharmony_ci					if (fcport->n2n_chip_reset < 1) {
9508c2ecf20Sopenharmony_ci						ql_log(ql_log_info, vha, 0x705d,
9518c2ecf20Sopenharmony_ci						    "Chip reset to bring laser down");
9528c2ecf20Sopenharmony_ci						set_bit(ISP_ABORT_NEEDED,
9538c2ecf20Sopenharmony_ci						    &vha->dpc_flags);
9548c2ecf20Sopenharmony_ci						fcport->n2n_chip_reset++;
9558c2ecf20Sopenharmony_ci					} else {
9568c2ecf20Sopenharmony_ci						ql_log(ql_log_info, vha, 0x705d,
9578c2ecf20Sopenharmony_ci						    "Remote port %8ph is not coming back\n",
9588c2ecf20Sopenharmony_ci						    fcport->port_name);
9598c2ecf20Sopenharmony_ci						fcport->scan_state = 0;
9608c2ecf20Sopenharmony_ci					}
9618c2ecf20Sopenharmony_ci				}
9628c2ecf20Sopenharmony_ci				qla2xxx_wake_dpc(vha);
9638c2ecf20Sopenharmony_ci			} else {
9648c2ecf20Sopenharmony_ci				/*
9658c2ecf20Sopenharmony_ci				 * report port suppose to do PLOGI. Give him
9668c2ecf20Sopenharmony_ci				 * more time. FW will catch it.
9678c2ecf20Sopenharmony_ci				 */
9688c2ecf20Sopenharmony_ci				set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
9698c2ecf20Sopenharmony_ci			}
9708c2ecf20Sopenharmony_ci			break;
9718c2ecf20Sopenharmony_ci		case ISP_CFG_NL:
9728c2ecf20Sopenharmony_ci			qla24xx_fcport_handle_login(vha, fcport);
9738c2ecf20Sopenharmony_ci			break;
9748c2ecf20Sopenharmony_ci		default:
9758c2ecf20Sopenharmony_ci			break;
9768c2ecf20Sopenharmony_ci		}
9778c2ecf20Sopenharmony_ci	}
9788c2ecf20Sopenharmony_ci} /* gnl_event */
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_cistatic void qla24xx_async_gnl_sp_done(srb_t *sp, int res)
9818c2ecf20Sopenharmony_ci{
9828c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = sp->vha;
9838c2ecf20Sopenharmony_ci	unsigned long flags;
9848c2ecf20Sopenharmony_ci	struct fc_port *fcport = NULL, *tf;
9858c2ecf20Sopenharmony_ci	u16 i, n = 0, loop_id;
9868c2ecf20Sopenharmony_ci	struct event_arg ea;
9878c2ecf20Sopenharmony_ci	struct get_name_list_extended *e;
9888c2ecf20Sopenharmony_ci	u64 wwn;
9898c2ecf20Sopenharmony_ci	struct list_head h;
9908c2ecf20Sopenharmony_ci	bool found = false;
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20e7,
9938c2ecf20Sopenharmony_ci	    "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
9948c2ecf20Sopenharmony_ci	    sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
9958c2ecf20Sopenharmony_ci	    sp->u.iocb_cmd.u.mbx.in_mb[2]);
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	sp->fcport->flags &= ~(FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE);
9998c2ecf20Sopenharmony_ci	memset(&ea, 0, sizeof(ea));
10008c2ecf20Sopenharmony_ci	ea.sp = sp;
10018c2ecf20Sopenharmony_ci	ea.rc = res;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
10048c2ecf20Sopenharmony_ci	    sizeof(struct get_name_list_extended)) {
10058c2ecf20Sopenharmony_ci		n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
10068c2ecf20Sopenharmony_ci		    sizeof(struct get_name_list_extended);
10078c2ecf20Sopenharmony_ci		ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
10088c2ecf20Sopenharmony_ci	}
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++) {
10118c2ecf20Sopenharmony_ci		e = &vha->gnl.l[i];
10128c2ecf20Sopenharmony_ci		loop_id = le16_to_cpu(e->nport_handle);
10138c2ecf20Sopenharmony_ci		/* mask out reserve bit */
10148c2ecf20Sopenharmony_ci		loop_id = (loop_id & 0x7fff);
10158c2ecf20Sopenharmony_ci		set_bit(loop_id, vha->hw->loop_id_map);
10168c2ecf20Sopenharmony_ci		wwn = wwn_to_u64(e->port_name);
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20e8,
10198c2ecf20Sopenharmony_ci		    "%s %8phC %02x:%02x:%02x CLS %x/%x lid %x \n",
10208c2ecf20Sopenharmony_ci		    __func__, &wwn, e->port_id[2], e->port_id[1],
10218c2ecf20Sopenharmony_ci		    e->port_id[0], e->current_login_state, e->last_login_state,
10228c2ecf20Sopenharmony_ci		    (loop_id & 0x7fff));
10238c2ecf20Sopenharmony_ci	}
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&h);
10288c2ecf20Sopenharmony_ci	fcport = tf = NULL;
10298c2ecf20Sopenharmony_ci	if (!list_empty(&vha->gnl.fcports))
10308c2ecf20Sopenharmony_ci		list_splice_init(&vha->gnl.fcports, &h);
10318c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
10348c2ecf20Sopenharmony_ci		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
10358c2ecf20Sopenharmony_ci		list_del_init(&fcport->gnl_entry);
10368c2ecf20Sopenharmony_ci		fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
10378c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
10388c2ecf20Sopenharmony_ci		ea.fcport = fcport;
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci		qla24xx_handle_gnl_done_event(vha, &ea);
10418c2ecf20Sopenharmony_ci	}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	/* create new fcport if fw has knowledge of new sessions */
10448c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++) {
10458c2ecf20Sopenharmony_ci		port_id_t id;
10468c2ecf20Sopenharmony_ci		u64 wwnn;
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci		e = &vha->gnl.l[i];
10498c2ecf20Sopenharmony_ci		wwn = wwn_to_u64(e->port_name);
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci		found = false;
10528c2ecf20Sopenharmony_ci		list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
10538c2ecf20Sopenharmony_ci			if (!memcmp((u8 *)&wwn, fcport->port_name,
10548c2ecf20Sopenharmony_ci			    WWN_SIZE)) {
10558c2ecf20Sopenharmony_ci				found = true;
10568c2ecf20Sopenharmony_ci				break;
10578c2ecf20Sopenharmony_ci			}
10588c2ecf20Sopenharmony_ci		}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci		id.b.domain = e->port_id[2];
10618c2ecf20Sopenharmony_ci		id.b.area = e->port_id[1];
10628c2ecf20Sopenharmony_ci		id.b.al_pa = e->port_id[0];
10638c2ecf20Sopenharmony_ci		id.b.rsvd_1 = 0;
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci		if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
10668c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2065,
10678c2ecf20Sopenharmony_ci			    "%s %d %8phC %06x post new sess\n",
10688c2ecf20Sopenharmony_ci			    __func__, __LINE__, (u8 *)&wwn, id.b24);
10698c2ecf20Sopenharmony_ci			wwnn = wwn_to_u64(e->node_name);
10708c2ecf20Sopenharmony_ci			qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
10718c2ecf20Sopenharmony_ci			    (u8 *)&wwnn, NULL, 0);
10728c2ecf20Sopenharmony_ci		}
10738c2ecf20Sopenharmony_ci	}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
10768c2ecf20Sopenharmony_ci	vha->gnl.sent = 0;
10778c2ecf20Sopenharmony_ci	if (!list_empty(&vha->gnl.fcports)) {
10788c2ecf20Sopenharmony_ci		/* retrigger gnl */
10798c2ecf20Sopenharmony_ci		list_for_each_entry_safe(fcport, tf, &vha->gnl.fcports,
10808c2ecf20Sopenharmony_ci		    gnl_entry) {
10818c2ecf20Sopenharmony_ci			list_del_init(&fcport->gnl_entry);
10828c2ecf20Sopenharmony_ci			fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
10838c2ecf20Sopenharmony_ci			if (qla24xx_post_gnl_work(vha, fcport) == QLA_SUCCESS)
10848c2ecf20Sopenharmony_ci				break;
10858c2ecf20Sopenharmony_ci		}
10868c2ecf20Sopenharmony_ci	}
10878c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	sp->free(sp);
10908c2ecf20Sopenharmony_ci}
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ciint qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
10938c2ecf20Sopenharmony_ci{
10948c2ecf20Sopenharmony_ci	srb_t *sp;
10958c2ecf20Sopenharmony_ci	struct srb_iocb *mbx;
10968c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
10978c2ecf20Sopenharmony_ci	unsigned long flags;
10988c2ecf20Sopenharmony_ci	u16 *mb;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
11018c2ecf20Sopenharmony_ci		return rval;
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20d9,
11048c2ecf20Sopenharmony_ci	    "Async-gnlist WWPN %8phC \n", fcport->port_name);
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
11078c2ecf20Sopenharmony_ci	fcport->flags |= FCF_ASYNC_SENT;
11088c2ecf20Sopenharmony_ci	qla2x00_set_fcport_disc_state(fcport, DSC_GNL);
11098c2ecf20Sopenharmony_ci	fcport->last_rscn_gen = fcport->rscn_gen;
11108c2ecf20Sopenharmony_ci	fcport->last_login_gen = fcport->login_gen;
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
11138c2ecf20Sopenharmony_ci	if (vha->gnl.sent) {
11148c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
11158c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
11168c2ecf20Sopenharmony_ci	}
11178c2ecf20Sopenharmony_ci	vha->gnl.sent = 1;
11188c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
11218c2ecf20Sopenharmony_ci	if (!sp)
11228c2ecf20Sopenharmony_ci		goto done;
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	sp->type = SRB_MB_IOCB;
11258c2ecf20Sopenharmony_ci	sp->name = "gnlist";
11268c2ecf20Sopenharmony_ci	sp->gen1 = fcport->rscn_gen;
11278c2ecf20Sopenharmony_ci	sp->gen2 = fcport->login_gen;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	mbx = &sp->u.iocb_cmd;
11308c2ecf20Sopenharmony_ci	mbx->timeout = qla2x00_async_iocb_timeout;
11318c2ecf20Sopenharmony_ci	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	mb = sp->u.iocb_cmd.u.mbx.out_mb;
11348c2ecf20Sopenharmony_ci	mb[0] = MBC_PORT_NODE_NAME_LIST;
11358c2ecf20Sopenharmony_ci	mb[1] = BIT_2 | BIT_3;
11368c2ecf20Sopenharmony_ci	mb[2] = MSW(vha->gnl.ldma);
11378c2ecf20Sopenharmony_ci	mb[3] = LSW(vha->gnl.ldma);
11388c2ecf20Sopenharmony_ci	mb[6] = MSW(MSD(vha->gnl.ldma));
11398c2ecf20Sopenharmony_ci	mb[7] = LSW(MSD(vha->gnl.ldma));
11408c2ecf20Sopenharmony_ci	mb[8] = vha->gnl.size;
11418c2ecf20Sopenharmony_ci	mb[9] = vha->vp_idx;
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	sp->done = qla24xx_async_gnl_sp_done;
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20da,
11468c2ecf20Sopenharmony_ci	    "Async-%s - OUT WWPN %8phC hndl %x\n",
11478c2ecf20Sopenharmony_ci	    sp->name, fcport->port_name, sp->handle);
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	rval = qla2x00_start_sp(sp);
11508c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS)
11518c2ecf20Sopenharmony_ci		goto done_free_sp;
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci	return rval;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_cidone_free_sp:
11568c2ecf20Sopenharmony_ci	sp->free(sp);
11578c2ecf20Sopenharmony_cidone:
11588c2ecf20Sopenharmony_ci	fcport->flags &= ~(FCF_ASYNC_ACTIVE | FCF_ASYNC_SENT);
11598c2ecf20Sopenharmony_ci	return rval;
11608c2ecf20Sopenharmony_ci}
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ciint qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
11638c2ecf20Sopenharmony_ci{
11648c2ecf20Sopenharmony_ci	struct qla_work_evt *e;
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
11678c2ecf20Sopenharmony_ci	if (!e)
11688c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	e->u.fcport.fcport = fcport;
11718c2ecf20Sopenharmony_ci	fcport->flags |= FCF_ASYNC_ACTIVE;
11728c2ecf20Sopenharmony_ci	return qla2x00_post_work(vha, e);
11738c2ecf20Sopenharmony_ci}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_cistatic void qla24xx_async_gpdb_sp_done(srb_t *sp, int res)
11768c2ecf20Sopenharmony_ci{
11778c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = sp->vha;
11788c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
11798c2ecf20Sopenharmony_ci	fc_port_t *fcport = sp->fcport;
11808c2ecf20Sopenharmony_ci	u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
11818c2ecf20Sopenharmony_ci	struct event_arg ea;
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20db,
11848c2ecf20Sopenharmony_ci	    "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
11858c2ecf20Sopenharmony_ci	    sp->name, res, fcport->port_name, mb[1], mb[2]);
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	if (res == QLA_FUNCTION_TIMEOUT)
11908c2ecf20Sopenharmony_ci		goto done;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	memset(&ea, 0, sizeof(ea));
11938c2ecf20Sopenharmony_ci	ea.fcport = fcport;
11948c2ecf20Sopenharmony_ci	ea.sp = sp;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	qla24xx_handle_gpdb_event(vha, &ea);
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_cidone:
11998c2ecf20Sopenharmony_ci	dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
12008c2ecf20Sopenharmony_ci		sp->u.iocb_cmd.u.mbx.in_dma);
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	sp->free(sp);
12038c2ecf20Sopenharmony_ci}
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_cistatic int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	struct qla_work_evt *e;
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci	if (vha->host->active_mode == MODE_TARGET)
12108c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
12138c2ecf20Sopenharmony_ci	if (!e)
12148c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	e->u.fcport.fcport = fcport;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	return qla2x00_post_work(vha, e);
12198c2ecf20Sopenharmony_ci}
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_cistatic void qla2x00_async_prli_sp_done(srb_t *sp, int res)
12228c2ecf20Sopenharmony_ci{
12238c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = sp->vha;
12248c2ecf20Sopenharmony_ci	struct srb_iocb *lio = &sp->u.iocb_cmd;
12258c2ecf20Sopenharmony_ci	struct event_arg ea;
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2129,
12288c2ecf20Sopenharmony_ci	    "%s %8phC res %d \n", __func__,
12298c2ecf20Sopenharmony_ci	    sp->fcport->port_name, res);
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	sp->fcport->flags &= ~FCF_ASYNC_SENT;
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	if (!test_bit(UNLOADING, &vha->dpc_flags)) {
12348c2ecf20Sopenharmony_ci		memset(&ea, 0, sizeof(ea));
12358c2ecf20Sopenharmony_ci		ea.fcport = sp->fcport;
12368c2ecf20Sopenharmony_ci		ea.data[0] = lio->u.logio.data[0];
12378c2ecf20Sopenharmony_ci		ea.data[1] = lio->u.logio.data[1];
12388c2ecf20Sopenharmony_ci		ea.iop[0] = lio->u.logio.iop[0];
12398c2ecf20Sopenharmony_ci		ea.iop[1] = lio->u.logio.iop[1];
12408c2ecf20Sopenharmony_ci		ea.sp = sp;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci		qla24xx_handle_prli_done_event(vha, &ea);
12438c2ecf20Sopenharmony_ci	}
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	sp->free(sp);
12468c2ecf20Sopenharmony_ci}
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ciint
12498c2ecf20Sopenharmony_ciqla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
12508c2ecf20Sopenharmony_ci{
12518c2ecf20Sopenharmony_ci	srb_t *sp;
12528c2ecf20Sopenharmony_ci	struct srb_iocb *lio;
12538c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	if (!vha->flags.online) {
12568c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0xffff, "%s %d %8phC exit\n",
12578c2ecf20Sopenharmony_ci		    __func__, __LINE__, fcport->port_name);
12588c2ecf20Sopenharmony_ci		return rval;
12598c2ecf20Sopenharmony_ci	}
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
12628c2ecf20Sopenharmony_ci	    fcport->fw_login_state == DSC_LS_PRLI_PEND) &&
12638c2ecf20Sopenharmony_ci	    qla_dual_mode_enabled(vha)) {
12648c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0xffff, "%s %d %8phC exit\n",
12658c2ecf20Sopenharmony_ci		    __func__, __LINE__, fcport->port_name);
12668c2ecf20Sopenharmony_ci		return rval;
12678c2ecf20Sopenharmony_ci	}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
12708c2ecf20Sopenharmony_ci	if (!sp)
12718c2ecf20Sopenharmony_ci		return rval;
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	fcport->flags |= FCF_ASYNC_SENT;
12748c2ecf20Sopenharmony_ci	fcport->logout_completed = 0;
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	sp->type = SRB_PRLI_CMD;
12778c2ecf20Sopenharmony_ci	sp->name = "prli";
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	lio = &sp->u.iocb_cmd;
12808c2ecf20Sopenharmony_ci	lio->timeout = qla2x00_async_iocb_timeout;
12818c2ecf20Sopenharmony_ci	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	sp->done = qla2x00_async_prli_sp_done;
12848c2ecf20Sopenharmony_ci	lio->u.logio.flags = 0;
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	if (NVME_TARGET(vha->hw, fcport))
12878c2ecf20Sopenharmony_ci		lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x211b,
12908c2ecf20Sopenharmony_ci	    "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d fc4type %x priority %x %s.\n",
12918c2ecf20Sopenharmony_ci	    fcport->port_name, sp->handle, fcport->loop_id, fcport->d_id.b24,
12928c2ecf20Sopenharmony_ci	    fcport->login_retry, fcport->fc4_type, vha->hw->fc4_type_priority,
12938c2ecf20Sopenharmony_ci	    NVME_TARGET(vha->hw, fcport) ? "nvme" : "fcp");
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	rval = qla2x00_start_sp(sp);
12968c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
12978c2ecf20Sopenharmony_ci		fcport->flags |= FCF_LOGIN_NEEDED;
12988c2ecf20Sopenharmony_ci		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
12998c2ecf20Sopenharmony_ci		goto done_free_sp;
13008c2ecf20Sopenharmony_ci	}
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci	return rval;
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_cidone_free_sp:
13058c2ecf20Sopenharmony_ci	sp->free(sp);
13068c2ecf20Sopenharmony_ci	fcport->flags &= ~FCF_ASYNC_SENT;
13078c2ecf20Sopenharmony_ci	return rval;
13088c2ecf20Sopenharmony_ci}
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ciint qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
13118c2ecf20Sopenharmony_ci{
13128c2ecf20Sopenharmony_ci	struct qla_work_evt *e;
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
13158c2ecf20Sopenharmony_ci	if (!e)
13168c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci	e->u.fcport.fcport = fcport;
13198c2ecf20Sopenharmony_ci	e->u.fcport.opt = opt;
13208c2ecf20Sopenharmony_ci	fcport->flags |= FCF_ASYNC_ACTIVE;
13218c2ecf20Sopenharmony_ci	return qla2x00_post_work(vha, e);
13228c2ecf20Sopenharmony_ci}
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ciint qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
13258c2ecf20Sopenharmony_ci{
13268c2ecf20Sopenharmony_ci	srb_t *sp;
13278c2ecf20Sopenharmony_ci	struct srb_iocb *mbx;
13288c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
13298c2ecf20Sopenharmony_ci	u16 *mb;
13308c2ecf20Sopenharmony_ci	dma_addr_t pd_dma;
13318c2ecf20Sopenharmony_ci	struct port_database_24xx *pd;
13328c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	if (IS_SESSION_DELETED(fcport)) {
13358c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xffff,
13368c2ecf20Sopenharmony_ci		       "%s: %8phC is being delete - not sending command.\n",
13378c2ecf20Sopenharmony_ci		       __func__, fcport->port_name);
13388c2ecf20Sopenharmony_ci		fcport->flags &= ~FCF_ASYNC_ACTIVE;
13398c2ecf20Sopenharmony_ci		return rval;
13408c2ecf20Sopenharmony_ci	}
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	if (!vha->flags.online || fcport->flags & FCF_ASYNC_SENT) {
13438c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xffff,
13448c2ecf20Sopenharmony_ci		    "%s: %8phC online %d flags %x - not sending command.\n",
13458c2ecf20Sopenharmony_ci		    __func__, fcport->port_name, vha->flags.online, fcport->flags);
13468c2ecf20Sopenharmony_ci		goto done;
13478c2ecf20Sopenharmony_ci	}
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ci	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
13508c2ecf20Sopenharmony_ci	if (!sp)
13518c2ecf20Sopenharmony_ci		goto done;
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	qla2x00_set_fcport_disc_state(fcport, DSC_GPDB);
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	fcport->flags |= FCF_ASYNC_SENT;
13568c2ecf20Sopenharmony_ci	sp->type = SRB_MB_IOCB;
13578c2ecf20Sopenharmony_ci	sp->name = "gpdb";
13588c2ecf20Sopenharmony_ci	sp->gen1 = fcport->rscn_gen;
13598c2ecf20Sopenharmony_ci	sp->gen2 = fcport->login_gen;
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	mbx = &sp->u.iocb_cmd;
13628c2ecf20Sopenharmony_ci	mbx->timeout = qla2x00_async_iocb_timeout;
13638c2ecf20Sopenharmony_ci	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
13668c2ecf20Sopenharmony_ci	if (pd == NULL) {
13678c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xd043,
13688c2ecf20Sopenharmony_ci		    "Failed to allocate port database structure.\n");
13698c2ecf20Sopenharmony_ci		goto done_free_sp;
13708c2ecf20Sopenharmony_ci	}
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci	mb = sp->u.iocb_cmd.u.mbx.out_mb;
13738c2ecf20Sopenharmony_ci	mb[0] = MBC_GET_PORT_DATABASE;
13748c2ecf20Sopenharmony_ci	mb[1] = fcport->loop_id;
13758c2ecf20Sopenharmony_ci	mb[2] = MSW(pd_dma);
13768c2ecf20Sopenharmony_ci	mb[3] = LSW(pd_dma);
13778c2ecf20Sopenharmony_ci	mb[6] = MSW(MSD(pd_dma));
13788c2ecf20Sopenharmony_ci	mb[7] = LSW(MSD(pd_dma));
13798c2ecf20Sopenharmony_ci	mb[9] = vha->vp_idx;
13808c2ecf20Sopenharmony_ci	mb[10] = opt;
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	mbx->u.mbx.in = pd;
13838c2ecf20Sopenharmony_ci	mbx->u.mbx.in_dma = pd_dma;
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci	sp->done = qla24xx_async_gpdb_sp_done;
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20dc,
13888c2ecf20Sopenharmony_ci	    "Async-%s %8phC hndl %x opt %x\n",
13898c2ecf20Sopenharmony_ci	    sp->name, fcport->port_name, sp->handle, opt);
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	rval = qla2x00_start_sp(sp);
13928c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS)
13938c2ecf20Sopenharmony_ci		goto done_free_sp;
13948c2ecf20Sopenharmony_ci	return rval;
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_cidone_free_sp:
13978c2ecf20Sopenharmony_ci	if (pd)
13988c2ecf20Sopenharmony_ci		dma_pool_free(ha->s_dma_pool, pd, pd_dma);
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	sp->free(sp);
14018c2ecf20Sopenharmony_ci	fcport->flags &= ~FCF_ASYNC_SENT;
14028c2ecf20Sopenharmony_cidone:
14038c2ecf20Sopenharmony_ci	fcport->flags &= ~FCF_ASYNC_ACTIVE;
14048c2ecf20Sopenharmony_ci	qla24xx_post_gpdb_work(vha, fcport, opt);
14058c2ecf20Sopenharmony_ci	return rval;
14068c2ecf20Sopenharmony_ci}
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_cistatic
14098c2ecf20Sopenharmony_civoid __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
14108c2ecf20Sopenharmony_ci{
14118c2ecf20Sopenharmony_ci	unsigned long flags;
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
14148c2ecf20Sopenharmony_ci	ea->fcport->login_gen++;
14158c2ecf20Sopenharmony_ci	ea->fcport->logout_on_delete = 1;
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
14188c2ecf20Sopenharmony_ci		vha->fcport_count++;
14198c2ecf20Sopenharmony_ci		ea->fcport->login_succ = 1;
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
14228c2ecf20Sopenharmony_ci		qla24xx_sched_upd_fcport(ea->fcport);
14238c2ecf20Sopenharmony_ci		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
14248c2ecf20Sopenharmony_ci	} else if (ea->fcport->login_succ) {
14258c2ecf20Sopenharmony_ci		/*
14268c2ecf20Sopenharmony_ci		 * We have an existing session. A late RSCN delivery
14278c2ecf20Sopenharmony_ci		 * must have triggered the session to be re-validate.
14288c2ecf20Sopenharmony_ci		 * Session is still valid.
14298c2ecf20Sopenharmony_ci		 */
14308c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20d6,
14318c2ecf20Sopenharmony_ci		    "%s %d %8phC session revalidate success\n",
14328c2ecf20Sopenharmony_ci		    __func__, __LINE__, ea->fcport->port_name);
14338c2ecf20Sopenharmony_ci		qla2x00_set_fcport_disc_state(ea->fcport, DSC_LOGIN_COMPLETE);
14348c2ecf20Sopenharmony_ci	}
14358c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
14368c2ecf20Sopenharmony_ci}
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_cistatic
14398c2ecf20Sopenharmony_civoid qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
14408c2ecf20Sopenharmony_ci{
14418c2ecf20Sopenharmony_ci	fc_port_t *fcport = ea->fcport;
14428c2ecf20Sopenharmony_ci	struct port_database_24xx *pd;
14438c2ecf20Sopenharmony_ci	struct srb *sp = ea->sp;
14448c2ecf20Sopenharmony_ci	uint8_t	ls;
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	fcport->flags &= ~FCF_ASYNC_SENT;
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20d2,
14518c2ecf20Sopenharmony_ci	    "%s %8phC DS %d LS %d fc4_type %x rc %d\n", __func__,
14528c2ecf20Sopenharmony_ci	    fcport->port_name, fcport->disc_state, pd->current_login_state,
14538c2ecf20Sopenharmony_ci	    fcport->fc4_type, ea->rc);
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci	if (fcport->disc_state == DSC_DELETE_PEND)
14568c2ecf20Sopenharmony_ci		return;
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	if (NVME_TARGET(vha->hw, fcport))
14598c2ecf20Sopenharmony_ci		ls = pd->current_login_state >> 4;
14608c2ecf20Sopenharmony_ci	else
14618c2ecf20Sopenharmony_ci		ls = pd->current_login_state & 0xf;
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	if (ea->sp->gen2 != fcport->login_gen) {
14648c2ecf20Sopenharmony_ci		/* target side must have changed it. */
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20d3,
14678c2ecf20Sopenharmony_ci		    "%s %8phC generation changed\n",
14688c2ecf20Sopenharmony_ci		    __func__, fcport->port_name);
14698c2ecf20Sopenharmony_ci		return;
14708c2ecf20Sopenharmony_ci	} else if (ea->sp->gen1 != fcport->rscn_gen) {
14718c2ecf20Sopenharmony_ci		qla_rscn_replay(fcport);
14728c2ecf20Sopenharmony_ci		qlt_schedule_sess_for_deletion(fcport);
14738c2ecf20Sopenharmony_ci		return;
14748c2ecf20Sopenharmony_ci	}
14758c2ecf20Sopenharmony_ci
14768c2ecf20Sopenharmony_ci	switch (ls) {
14778c2ecf20Sopenharmony_ci	case PDS_PRLI_COMPLETE:
14788c2ecf20Sopenharmony_ci		__qla24xx_parse_gpdb(vha, fcport, pd);
14798c2ecf20Sopenharmony_ci		break;
14808c2ecf20Sopenharmony_ci	case PDS_PLOGI_PENDING:
14818c2ecf20Sopenharmony_ci	case PDS_PLOGI_COMPLETE:
14828c2ecf20Sopenharmony_ci	case PDS_PRLI_PENDING:
14838c2ecf20Sopenharmony_ci	case PDS_PRLI2_PENDING:
14848c2ecf20Sopenharmony_ci		/* Set discovery state back to GNL to Relogin attempt */
14858c2ecf20Sopenharmony_ci		if (qla_dual_mode_enabled(vha) ||
14868c2ecf20Sopenharmony_ci		    qla_ini_mode_enabled(vha)) {
14878c2ecf20Sopenharmony_ci			qla2x00_set_fcport_disc_state(fcport, DSC_GNL);
14888c2ecf20Sopenharmony_ci			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
14898c2ecf20Sopenharmony_ci		}
14908c2ecf20Sopenharmony_ci		return;
14918c2ecf20Sopenharmony_ci	case PDS_LOGO_PENDING:
14928c2ecf20Sopenharmony_ci	case PDS_PORT_UNAVAILABLE:
14938c2ecf20Sopenharmony_ci	default:
14948c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
14958c2ecf20Sopenharmony_ci		    __func__, __LINE__, fcport->port_name);
14968c2ecf20Sopenharmony_ci		qlt_schedule_sess_for_deletion(fcport);
14978c2ecf20Sopenharmony_ci		return;
14988c2ecf20Sopenharmony_ci	}
14998c2ecf20Sopenharmony_ci	__qla24xx_handle_gpdb_event(vha, ea);
15008c2ecf20Sopenharmony_ci} /* gpdb event */
15018c2ecf20Sopenharmony_ci
15028c2ecf20Sopenharmony_cistatic void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
15038c2ecf20Sopenharmony_ci{
15048c2ecf20Sopenharmony_ci	u8 login = 0;
15058c2ecf20Sopenharmony_ci	int rc;
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x307b,
15088c2ecf20Sopenharmony_ci	    "%s %8phC DS %d LS %d lid %d retries=%d\n",
15098c2ecf20Sopenharmony_ci	    __func__, fcport->port_name, fcport->disc_state,
15108c2ecf20Sopenharmony_ci	    fcport->fw_login_state, fcport->loop_id, fcport->login_retry);
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	if (qla_tgt_mode_enabled(vha))
15138c2ecf20Sopenharmony_ci		return;
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci	if (qla_dual_mode_enabled(vha)) {
15168c2ecf20Sopenharmony_ci		if (N2N_TOPO(vha->hw)) {
15178c2ecf20Sopenharmony_ci			u64 mywwn, wwn;
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_ci			mywwn = wwn_to_u64(vha->port_name);
15208c2ecf20Sopenharmony_ci			wwn = wwn_to_u64(fcport->port_name);
15218c2ecf20Sopenharmony_ci			if (mywwn > wwn)
15228c2ecf20Sopenharmony_ci				login = 1;
15238c2ecf20Sopenharmony_ci			else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
15248c2ecf20Sopenharmony_ci			    && time_after_eq(jiffies,
15258c2ecf20Sopenharmony_ci				    fcport->plogi_nack_done_deadline))
15268c2ecf20Sopenharmony_ci				login = 1;
15278c2ecf20Sopenharmony_ci		} else {
15288c2ecf20Sopenharmony_ci			login = 1;
15298c2ecf20Sopenharmony_ci		}
15308c2ecf20Sopenharmony_ci	} else {
15318c2ecf20Sopenharmony_ci		/* initiator mode */
15328c2ecf20Sopenharmony_ci		login = 1;
15338c2ecf20Sopenharmony_ci	}
15348c2ecf20Sopenharmony_ci
15358c2ecf20Sopenharmony_ci	if (login && fcport->login_retry) {
15368c2ecf20Sopenharmony_ci		fcport->login_retry--;
15378c2ecf20Sopenharmony_ci		if (fcport->loop_id == FC_NO_LOOP_ID) {
15388c2ecf20Sopenharmony_ci			fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
15398c2ecf20Sopenharmony_ci			rc = qla2x00_find_new_loop_id(vha, fcport);
15408c2ecf20Sopenharmony_ci			if (rc) {
15418c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x20e6,
15428c2ecf20Sopenharmony_ci				    "%s %d %8phC post del sess - out of loopid\n",
15438c2ecf20Sopenharmony_ci				    __func__, __LINE__, fcport->port_name);
15448c2ecf20Sopenharmony_ci				fcport->scan_state = 0;
15458c2ecf20Sopenharmony_ci				qlt_schedule_sess_for_deletion(fcport);
15468c2ecf20Sopenharmony_ci				return;
15478c2ecf20Sopenharmony_ci			}
15488c2ecf20Sopenharmony_ci		}
15498c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20bf,
15508c2ecf20Sopenharmony_ci		    "%s %d %8phC post login\n",
15518c2ecf20Sopenharmony_ci		    __func__, __LINE__, fcport->port_name);
15528c2ecf20Sopenharmony_ci		qla2x00_post_async_login_work(vha, fcport, NULL);
15538c2ecf20Sopenharmony_ci	}
15548c2ecf20Sopenharmony_ci}
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ciint qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
15578c2ecf20Sopenharmony_ci{
15588c2ecf20Sopenharmony_ci	u16 data[2];
15598c2ecf20Sopenharmony_ci	u64 wwn;
15608c2ecf20Sopenharmony_ci	u16 sec;
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20d8,
15638c2ecf20Sopenharmony_ci	    "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d lid %d scan %d\n",
15648c2ecf20Sopenharmony_ci	    __func__, fcport->port_name, fcport->disc_state,
15658c2ecf20Sopenharmony_ci	    fcport->fw_login_state, fcport->login_pause, fcport->flags,
15668c2ecf20Sopenharmony_ci	    fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
15678c2ecf20Sopenharmony_ci	    fcport->login_gen, fcport->loop_id, fcport->scan_state);
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci	if (fcport->scan_state != QLA_FCPORT_FOUND ||
15708c2ecf20Sopenharmony_ci	    fcport->disc_state == DSC_DELETE_PEND)
15718c2ecf20Sopenharmony_ci		return 0;
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_ci	if ((fcport->loop_id != FC_NO_LOOP_ID) &&
15748c2ecf20Sopenharmony_ci	    qla_dual_mode_enabled(vha) &&
15758c2ecf20Sopenharmony_ci	    ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
15768c2ecf20Sopenharmony_ci	     (fcport->fw_login_state == DSC_LS_PRLI_PEND)))
15778c2ecf20Sopenharmony_ci		return 0;
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci	if (fcport->fw_login_state == DSC_LS_PLOGI_COMP &&
15808c2ecf20Sopenharmony_ci	    !N2N_TOPO(vha->hw)) {
15818c2ecf20Sopenharmony_ci		if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
15828c2ecf20Sopenharmony_ci			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
15838c2ecf20Sopenharmony_ci			return 0;
15848c2ecf20Sopenharmony_ci		}
15858c2ecf20Sopenharmony_ci	}
15868c2ecf20Sopenharmony_ci
15878c2ecf20Sopenharmony_ci	/* Target won't initiate port login if fabric is present */
15888c2ecf20Sopenharmony_ci	if (vha->host->active_mode == MODE_TARGET && !N2N_TOPO(vha->hw))
15898c2ecf20Sopenharmony_ci		return 0;
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	if (fcport->flags & (FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE)) {
15928c2ecf20Sopenharmony_ci		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
15938c2ecf20Sopenharmony_ci		return 0;
15948c2ecf20Sopenharmony_ci	}
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci	switch (fcport->disc_state) {
15978c2ecf20Sopenharmony_ci	case DSC_DELETED:
15988c2ecf20Sopenharmony_ci		wwn = wwn_to_u64(fcport->node_name);
15998c2ecf20Sopenharmony_ci		switch (vha->hw->current_topology) {
16008c2ecf20Sopenharmony_ci		case ISP_CFG_N:
16018c2ecf20Sopenharmony_ci			if (fcport_is_smaller(fcport)) {
16028c2ecf20Sopenharmony_ci				/* this adapter is bigger */
16038c2ecf20Sopenharmony_ci				if (fcport->login_retry) {
16048c2ecf20Sopenharmony_ci					if (fcport->loop_id == FC_NO_LOOP_ID) {
16058c2ecf20Sopenharmony_ci						qla2x00_find_new_loop_id(vha,
16068c2ecf20Sopenharmony_ci						    fcport);
16078c2ecf20Sopenharmony_ci						fcport->fw_login_state =
16088c2ecf20Sopenharmony_ci						    DSC_LS_PORT_UNAVAIL;
16098c2ecf20Sopenharmony_ci					}
16108c2ecf20Sopenharmony_ci					fcport->login_retry--;
16118c2ecf20Sopenharmony_ci					qla_post_els_plogi_work(vha, fcport);
16128c2ecf20Sopenharmony_ci				} else {
16138c2ecf20Sopenharmony_ci					ql_log(ql_log_info, vha, 0x705d,
16148c2ecf20Sopenharmony_ci					    "Unable to reach remote port %8phC",
16158c2ecf20Sopenharmony_ci					    fcport->port_name);
16168c2ecf20Sopenharmony_ci				}
16178c2ecf20Sopenharmony_ci			} else {
16188c2ecf20Sopenharmony_ci				qla24xx_post_gnl_work(vha, fcport);
16198c2ecf20Sopenharmony_ci			}
16208c2ecf20Sopenharmony_ci			break;
16218c2ecf20Sopenharmony_ci		default:
16228c2ecf20Sopenharmony_ci			if (wwn == 0)    {
16238c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0xffff,
16248c2ecf20Sopenharmony_ci				    "%s %d %8phC post GNNID\n",
16258c2ecf20Sopenharmony_ci				    __func__, __LINE__, fcport->port_name);
16268c2ecf20Sopenharmony_ci				qla24xx_post_gnnid_work(vha, fcport);
16278c2ecf20Sopenharmony_ci			} else if (fcport->loop_id == FC_NO_LOOP_ID) {
16288c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x20bd,
16298c2ecf20Sopenharmony_ci				    "%s %d %8phC post gnl\n",
16308c2ecf20Sopenharmony_ci				    __func__, __LINE__, fcport->port_name);
16318c2ecf20Sopenharmony_ci				qla24xx_post_gnl_work(vha, fcport);
16328c2ecf20Sopenharmony_ci			} else {
16338c2ecf20Sopenharmony_ci				qla_chk_n2n_b4_login(vha, fcport);
16348c2ecf20Sopenharmony_ci			}
16358c2ecf20Sopenharmony_ci			break;
16368c2ecf20Sopenharmony_ci		}
16378c2ecf20Sopenharmony_ci		break;
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci	case DSC_GNL:
16408c2ecf20Sopenharmony_ci		switch (vha->hw->current_topology) {
16418c2ecf20Sopenharmony_ci		case ISP_CFG_N:
16428c2ecf20Sopenharmony_ci			if ((fcport->current_login_state & 0xf) == 0x6) {
16438c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x2118,
16448c2ecf20Sopenharmony_ci				    "%s %d %8phC post GPDB work\n",
16458c2ecf20Sopenharmony_ci				    __func__, __LINE__, fcport->port_name);
16468c2ecf20Sopenharmony_ci				fcport->chip_reset =
16478c2ecf20Sopenharmony_ci					vha->hw->base_qpair->chip_reset;
16488c2ecf20Sopenharmony_ci				qla24xx_post_gpdb_work(vha, fcport, 0);
16498c2ecf20Sopenharmony_ci			}  else {
16508c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x2118,
16518c2ecf20Sopenharmony_ci				    "%s %d %8phC post %s PRLI\n",
16528c2ecf20Sopenharmony_ci				    __func__, __LINE__, fcport->port_name,
16538c2ecf20Sopenharmony_ci				    NVME_TARGET(vha->hw, fcport) ? "NVME" :
16548c2ecf20Sopenharmony_ci				    "FC");
16558c2ecf20Sopenharmony_ci				qla24xx_post_prli_work(vha, fcport);
16568c2ecf20Sopenharmony_ci			}
16578c2ecf20Sopenharmony_ci			break;
16588c2ecf20Sopenharmony_ci		default:
16598c2ecf20Sopenharmony_ci			if (fcport->login_pause) {
16608c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x20d8,
16618c2ecf20Sopenharmony_ci				    "%s %d %8phC exit\n",
16628c2ecf20Sopenharmony_ci				    __func__, __LINE__,
16638c2ecf20Sopenharmony_ci				    fcport->port_name);
16648c2ecf20Sopenharmony_ci				fcport->last_rscn_gen = fcport->rscn_gen;
16658c2ecf20Sopenharmony_ci				fcport->last_login_gen = fcport->login_gen;
16668c2ecf20Sopenharmony_ci				set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
16678c2ecf20Sopenharmony_ci				break;
16688c2ecf20Sopenharmony_ci			}
16698c2ecf20Sopenharmony_ci			qla_chk_n2n_b4_login(vha, fcport);
16708c2ecf20Sopenharmony_ci			break;
16718c2ecf20Sopenharmony_ci		}
16728c2ecf20Sopenharmony_ci		break;
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	case DSC_LOGIN_FAILED:
16758c2ecf20Sopenharmony_ci		if (N2N_TOPO(vha->hw))
16768c2ecf20Sopenharmony_ci			qla_chk_n2n_b4_login(vha, fcport);
16778c2ecf20Sopenharmony_ci		else
16788c2ecf20Sopenharmony_ci			qlt_schedule_sess_for_deletion(fcport);
16798c2ecf20Sopenharmony_ci		break;
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	case DSC_LOGIN_COMPLETE:
16828c2ecf20Sopenharmony_ci		/* recheck login state */
16838c2ecf20Sopenharmony_ci		data[0] = data[1] = 0;
16848c2ecf20Sopenharmony_ci		qla2x00_post_async_adisc_work(vha, fcport, data);
16858c2ecf20Sopenharmony_ci		break;
16868c2ecf20Sopenharmony_ci
16878c2ecf20Sopenharmony_ci	case DSC_LOGIN_PEND:
16888c2ecf20Sopenharmony_ci		if (fcport->fw_login_state == DSC_LS_PLOGI_COMP)
16898c2ecf20Sopenharmony_ci			qla24xx_post_prli_work(vha, fcport);
16908c2ecf20Sopenharmony_ci		break;
16918c2ecf20Sopenharmony_ci
16928c2ecf20Sopenharmony_ci	case DSC_UPD_FCPORT:
16938c2ecf20Sopenharmony_ci		sec =  jiffies_to_msecs(jiffies -
16948c2ecf20Sopenharmony_ci		    fcport->jiffies_at_registration)/1000;
16958c2ecf20Sopenharmony_ci		if (fcport->sec_since_registration < sec && sec &&
16968c2ecf20Sopenharmony_ci		    !(sec % 60)) {
16978c2ecf20Sopenharmony_ci			fcport->sec_since_registration = sec;
16988c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, fcport->vha, 0xffff,
16998c2ecf20Sopenharmony_ci			    "%s %8phC - Slow Rport registration(%d Sec)\n",
17008c2ecf20Sopenharmony_ci			    __func__, fcport->port_name, sec);
17018c2ecf20Sopenharmony_ci		}
17028c2ecf20Sopenharmony_ci
17038c2ecf20Sopenharmony_ci		if (fcport->next_disc_state != DSC_DELETE_PEND)
17048c2ecf20Sopenharmony_ci			fcport->next_disc_state = DSC_ADISC;
17058c2ecf20Sopenharmony_ci		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
17068c2ecf20Sopenharmony_ci		break;
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_ci	default:
17098c2ecf20Sopenharmony_ci		break;
17108c2ecf20Sopenharmony_ci	}
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_ci	return 0;
17138c2ecf20Sopenharmony_ci}
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ciint qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
17168c2ecf20Sopenharmony_ci    u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
17178c2ecf20Sopenharmony_ci{
17188c2ecf20Sopenharmony_ci	struct qla_work_evt *e;
17198c2ecf20Sopenharmony_ci
17208c2ecf20Sopenharmony_ci	e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
17218c2ecf20Sopenharmony_ci	if (!e)
17228c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
17238c2ecf20Sopenharmony_ci
17248c2ecf20Sopenharmony_ci	e->u.new_sess.id = *id;
17258c2ecf20Sopenharmony_ci	e->u.new_sess.pla = pla;
17268c2ecf20Sopenharmony_ci	e->u.new_sess.fc4_type = fc4_type;
17278c2ecf20Sopenharmony_ci	memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
17288c2ecf20Sopenharmony_ci	if (node_name)
17298c2ecf20Sopenharmony_ci		memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci	return qla2x00_post_work(vha, e);
17328c2ecf20Sopenharmony_ci}
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_civoid qla2x00_handle_rscn(scsi_qla_host_t *vha, struct event_arg *ea)
17358c2ecf20Sopenharmony_ci{
17368c2ecf20Sopenharmony_ci	fc_port_t *fcport;
17378c2ecf20Sopenharmony_ci	unsigned long flags;
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci	switch (ea->id.b.rsvd_1) {
17408c2ecf20Sopenharmony_ci	case RSCN_PORT_ADDR:
17418c2ecf20Sopenharmony_ci		fcport = qla2x00_find_fcport_by_nportid(vha, &ea->id, 1);
17428c2ecf20Sopenharmony_ci		if (fcport) {
17438c2ecf20Sopenharmony_ci			if (fcport->flags & FCF_FCP2_DEVICE &&
17448c2ecf20Sopenharmony_ci			    atomic_read(&fcport->state) == FCS_ONLINE) {
17458c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x2115,
17468c2ecf20Sopenharmony_ci				       "Delaying session delete for FCP2 portid=%06x %8phC ",
17478c2ecf20Sopenharmony_ci					fcport->d_id.b24, fcport->port_name);
17488c2ecf20Sopenharmony_ci				return;
17498c2ecf20Sopenharmony_ci			}
17508c2ecf20Sopenharmony_ci			fcport->scan_needed = 1;
17518c2ecf20Sopenharmony_ci			fcport->rscn_gen++;
17528c2ecf20Sopenharmony_ci		}
17538c2ecf20Sopenharmony_ci		break;
17548c2ecf20Sopenharmony_ci	case RSCN_AREA_ADDR:
17558c2ecf20Sopenharmony_ci		list_for_each_entry(fcport, &vha->vp_fcports, list) {
17568c2ecf20Sopenharmony_ci			if (fcport->flags & FCF_FCP2_DEVICE &&
17578c2ecf20Sopenharmony_ci			    atomic_read(&fcport->state) == FCS_ONLINE)
17588c2ecf20Sopenharmony_ci				continue;
17598c2ecf20Sopenharmony_ci
17608c2ecf20Sopenharmony_ci			if ((ea->id.b24 & 0xffff00) == (fcport->d_id.b24 & 0xffff00)) {
17618c2ecf20Sopenharmony_ci				fcport->scan_needed = 1;
17628c2ecf20Sopenharmony_ci				fcport->rscn_gen++;
17638c2ecf20Sopenharmony_ci			}
17648c2ecf20Sopenharmony_ci		}
17658c2ecf20Sopenharmony_ci		break;
17668c2ecf20Sopenharmony_ci	case RSCN_DOM_ADDR:
17678c2ecf20Sopenharmony_ci		list_for_each_entry(fcport, &vha->vp_fcports, list) {
17688c2ecf20Sopenharmony_ci			if (fcport->flags & FCF_FCP2_DEVICE &&
17698c2ecf20Sopenharmony_ci			    atomic_read(&fcport->state) == FCS_ONLINE)
17708c2ecf20Sopenharmony_ci				continue;
17718c2ecf20Sopenharmony_ci
17728c2ecf20Sopenharmony_ci			if ((ea->id.b24 & 0xff0000) == (fcport->d_id.b24 & 0xff0000)) {
17738c2ecf20Sopenharmony_ci				fcport->scan_needed = 1;
17748c2ecf20Sopenharmony_ci				fcport->rscn_gen++;
17758c2ecf20Sopenharmony_ci			}
17768c2ecf20Sopenharmony_ci		}
17778c2ecf20Sopenharmony_ci		break;
17788c2ecf20Sopenharmony_ci	case RSCN_FAB_ADDR:
17798c2ecf20Sopenharmony_ci	default:
17808c2ecf20Sopenharmony_ci		list_for_each_entry(fcport, &vha->vp_fcports, list) {
17818c2ecf20Sopenharmony_ci			if (fcport->flags & FCF_FCP2_DEVICE &&
17828c2ecf20Sopenharmony_ci			    atomic_read(&fcport->state) == FCS_ONLINE)
17838c2ecf20Sopenharmony_ci				continue;
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ci			fcport->scan_needed = 1;
17868c2ecf20Sopenharmony_ci			fcport->rscn_gen++;
17878c2ecf20Sopenharmony_ci		}
17888c2ecf20Sopenharmony_ci		break;
17898c2ecf20Sopenharmony_ci	}
17908c2ecf20Sopenharmony_ci
17918c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vha->work_lock, flags);
17928c2ecf20Sopenharmony_ci	if (vha->scan.scan_flags == 0) {
17938c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0xffff, "%s: schedule\n", __func__);
17948c2ecf20Sopenharmony_ci		vha->scan.scan_flags |= SF_QUEUED;
17958c2ecf20Sopenharmony_ci		schedule_delayed_work(&vha->scan.scan_work, 5);
17968c2ecf20Sopenharmony_ci	}
17978c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vha->work_lock, flags);
17988c2ecf20Sopenharmony_ci}
17998c2ecf20Sopenharmony_ci
18008c2ecf20Sopenharmony_civoid qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
18018c2ecf20Sopenharmony_ci	struct event_arg *ea)
18028c2ecf20Sopenharmony_ci{
18038c2ecf20Sopenharmony_ci	fc_port_t *fcport = ea->fcport;
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_ci	if (test_bit(UNLOADING, &vha->dpc_flags))
18068c2ecf20Sopenharmony_ci		return;
18078c2ecf20Sopenharmony_ci
18088c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2102,
18098c2ecf20Sopenharmony_ci	    "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
18108c2ecf20Sopenharmony_ci	    __func__, fcport->port_name, fcport->disc_state,
18118c2ecf20Sopenharmony_ci	    fcport->fw_login_state, fcport->login_pause,
18128c2ecf20Sopenharmony_ci	    fcport->deleted, fcport->conflict,
18138c2ecf20Sopenharmony_ci	    fcport->last_rscn_gen, fcport->rscn_gen,
18148c2ecf20Sopenharmony_ci	    fcport->last_login_gen, fcport->login_gen,
18158c2ecf20Sopenharmony_ci	    fcport->flags);
18168c2ecf20Sopenharmony_ci
18178c2ecf20Sopenharmony_ci	if (fcport->last_rscn_gen != fcport->rscn_gen) {
18188c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gnl\n",
18198c2ecf20Sopenharmony_ci		    __func__, __LINE__, fcport->port_name);
18208c2ecf20Sopenharmony_ci		qla24xx_post_gnl_work(vha, fcport);
18218c2ecf20Sopenharmony_ci		return;
18228c2ecf20Sopenharmony_ci	}
18238c2ecf20Sopenharmony_ci
18248c2ecf20Sopenharmony_ci	qla24xx_fcport_handle_login(vha, fcport);
18258c2ecf20Sopenharmony_ci}
18268c2ecf20Sopenharmony_ci
18278c2ecf20Sopenharmony_civoid qla_handle_els_plogi_done(scsi_qla_host_t *vha,
18288c2ecf20Sopenharmony_ci				      struct event_arg *ea)
18298c2ecf20Sopenharmony_ci{
18308c2ecf20Sopenharmony_ci	/* for pure Target Mode, PRLI will not be initiated */
18318c2ecf20Sopenharmony_ci	if (vha->host->active_mode == MODE_TARGET)
18328c2ecf20Sopenharmony_ci		return;
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2118,
18358c2ecf20Sopenharmony_ci	    "%s %d %8phC post PRLI\n",
18368c2ecf20Sopenharmony_ci	    __func__, __LINE__, ea->fcport->port_name);
18378c2ecf20Sopenharmony_ci	qla24xx_post_prli_work(vha, ea->fcport);
18388c2ecf20Sopenharmony_ci}
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci/*
18418c2ecf20Sopenharmony_ci * RSCN(s) came in for this fcport, but the RSCN(s) was not able
18428c2ecf20Sopenharmony_ci * to be consumed by the fcport
18438c2ecf20Sopenharmony_ci */
18448c2ecf20Sopenharmony_civoid qla_rscn_replay(fc_port_t *fcport)
18458c2ecf20Sopenharmony_ci{
18468c2ecf20Sopenharmony_ci	struct event_arg ea;
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci	switch (fcport->disc_state) {
18498c2ecf20Sopenharmony_ci	case DSC_DELETE_PEND:
18508c2ecf20Sopenharmony_ci		return;
18518c2ecf20Sopenharmony_ci	default:
18528c2ecf20Sopenharmony_ci		break;
18538c2ecf20Sopenharmony_ci	}
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_ci	if (fcport->scan_needed) {
18568c2ecf20Sopenharmony_ci		memset(&ea, 0, sizeof(ea));
18578c2ecf20Sopenharmony_ci		ea.id = fcport->d_id;
18588c2ecf20Sopenharmony_ci		ea.id.b.rsvd_1 = RSCN_PORT_ADDR;
18598c2ecf20Sopenharmony_ci		qla2x00_handle_rscn(fcport->vha, &ea);
18608c2ecf20Sopenharmony_ci	}
18618c2ecf20Sopenharmony_ci}
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_cistatic void
18648c2ecf20Sopenharmony_ciqla2x00_tmf_iocb_timeout(void *data)
18658c2ecf20Sopenharmony_ci{
18668c2ecf20Sopenharmony_ci	srb_t *sp = data;
18678c2ecf20Sopenharmony_ci	struct srb_iocb *tmf = &sp->u.iocb_cmd;
18688c2ecf20Sopenharmony_ci	int rc, h;
18698c2ecf20Sopenharmony_ci	unsigned long flags;
18708c2ecf20Sopenharmony_ci
18718c2ecf20Sopenharmony_ci	rc = qla24xx_async_abort_cmd(sp, false);
18728c2ecf20Sopenharmony_ci	if (rc) {
18738c2ecf20Sopenharmony_ci		spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
18748c2ecf20Sopenharmony_ci		for (h = 1; h < sp->qpair->req->num_outstanding_cmds; h++) {
18758c2ecf20Sopenharmony_ci			if (sp->qpair->req->outstanding_cmds[h] == sp) {
18768c2ecf20Sopenharmony_ci				sp->qpair->req->outstanding_cmds[h] = NULL;
18778c2ecf20Sopenharmony_ci				break;
18788c2ecf20Sopenharmony_ci			}
18798c2ecf20Sopenharmony_ci		}
18808c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
18818c2ecf20Sopenharmony_ci		tmf->u.tmf.comp_status = cpu_to_le16(CS_TIMEOUT);
18828c2ecf20Sopenharmony_ci		tmf->u.tmf.data = QLA_FUNCTION_FAILED;
18838c2ecf20Sopenharmony_ci		complete(&tmf->u.tmf.comp);
18848c2ecf20Sopenharmony_ci	}
18858c2ecf20Sopenharmony_ci}
18868c2ecf20Sopenharmony_ci
18878c2ecf20Sopenharmony_cistatic void qla2x00_tmf_sp_done(srb_t *sp, int res)
18888c2ecf20Sopenharmony_ci{
18898c2ecf20Sopenharmony_ci	struct srb_iocb *tmf = &sp->u.iocb_cmd;
18908c2ecf20Sopenharmony_ci
18918c2ecf20Sopenharmony_ci	complete(&tmf->u.tmf.comp);
18928c2ecf20Sopenharmony_ci}
18938c2ecf20Sopenharmony_ci
18948c2ecf20Sopenharmony_ciint
18958c2ecf20Sopenharmony_ciqla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
18968c2ecf20Sopenharmony_ci	uint32_t tag)
18978c2ecf20Sopenharmony_ci{
18988c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = fcport->vha;
18998c2ecf20Sopenharmony_ci	struct srb_iocb *tm_iocb;
19008c2ecf20Sopenharmony_ci	srb_t *sp;
19018c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
19028c2ecf20Sopenharmony_ci
19038c2ecf20Sopenharmony_ci	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
19048c2ecf20Sopenharmony_ci	if (!sp)
19058c2ecf20Sopenharmony_ci		goto done;
19068c2ecf20Sopenharmony_ci
19078c2ecf20Sopenharmony_ci	tm_iocb = &sp->u.iocb_cmd;
19088c2ecf20Sopenharmony_ci	sp->type = SRB_TM_CMD;
19098c2ecf20Sopenharmony_ci	sp->name = "tmf";
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci	tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
19128c2ecf20Sopenharmony_ci	init_completion(&tm_iocb->u.tmf.comp);
19138c2ecf20Sopenharmony_ci	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
19148c2ecf20Sopenharmony_ci
19158c2ecf20Sopenharmony_ci	tm_iocb->u.tmf.flags = flags;
19168c2ecf20Sopenharmony_ci	tm_iocb->u.tmf.lun = lun;
19178c2ecf20Sopenharmony_ci	tm_iocb->u.tmf.data = tag;
19188c2ecf20Sopenharmony_ci	sp->done = qla2x00_tmf_sp_done;
19198c2ecf20Sopenharmony_ci
19208c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_taskm, vha, 0x802f,
19218c2ecf20Sopenharmony_ci	    "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
19228c2ecf20Sopenharmony_ci	    sp->handle, fcport->loop_id, fcport->d_id.b.domain,
19238c2ecf20Sopenharmony_ci	    fcport->d_id.b.area, fcport->d_id.b.al_pa);
19248c2ecf20Sopenharmony_ci
19258c2ecf20Sopenharmony_ci	rval = qla2x00_start_sp(sp);
19268c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS)
19278c2ecf20Sopenharmony_ci		goto done_free_sp;
19288c2ecf20Sopenharmony_ci	wait_for_completion(&tm_iocb->u.tmf.comp);
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_ci	rval = tm_iocb->u.tmf.data;
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
19338c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x8030,
19348c2ecf20Sopenharmony_ci		    "TM IOCB failed (%x).\n", rval);
19358c2ecf20Sopenharmony_ci	}
19368c2ecf20Sopenharmony_ci
19378c2ecf20Sopenharmony_ci	if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
19388c2ecf20Sopenharmony_ci		flags = tm_iocb->u.tmf.flags;
19398c2ecf20Sopenharmony_ci		lun = (uint16_t)tm_iocb->u.tmf.lun;
19408c2ecf20Sopenharmony_ci
19418c2ecf20Sopenharmony_ci		/* Issue Marker IOCB */
19428c2ecf20Sopenharmony_ci		qla2x00_marker(vha, vha->hw->base_qpair,
19438c2ecf20Sopenharmony_ci		    fcport->loop_id, lun,
19448c2ecf20Sopenharmony_ci		    flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
19458c2ecf20Sopenharmony_ci	}
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_cidone_free_sp:
19488c2ecf20Sopenharmony_ci	sp->free(sp);
19498c2ecf20Sopenharmony_ci	fcport->flags &= ~FCF_ASYNC_SENT;
19508c2ecf20Sopenharmony_cidone:
19518c2ecf20Sopenharmony_ci	return rval;
19528c2ecf20Sopenharmony_ci}
19538c2ecf20Sopenharmony_ci
19548c2ecf20Sopenharmony_ciint
19558c2ecf20Sopenharmony_ciqla24xx_async_abort_command(srb_t *sp)
19568c2ecf20Sopenharmony_ci{
19578c2ecf20Sopenharmony_ci	unsigned long   flags = 0;
19588c2ecf20Sopenharmony_ci
19598c2ecf20Sopenharmony_ci	uint32_t	handle;
19608c2ecf20Sopenharmony_ci	fc_port_t	*fcport = sp->fcport;
19618c2ecf20Sopenharmony_ci	struct qla_qpair *qpair = sp->qpair;
19628c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = fcport->vha;
19638c2ecf20Sopenharmony_ci	struct req_que *req = qpair->req;
19648c2ecf20Sopenharmony_ci
19658c2ecf20Sopenharmony_ci	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
19668c2ecf20Sopenharmony_ci	for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
19678c2ecf20Sopenharmony_ci		if (req->outstanding_cmds[handle] == sp)
19688c2ecf20Sopenharmony_ci			break;
19698c2ecf20Sopenharmony_ci	}
19708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
19718c2ecf20Sopenharmony_ci
19728c2ecf20Sopenharmony_ci	if (handle == req->num_outstanding_cmds) {
19738c2ecf20Sopenharmony_ci		/* Command not found. */
19748c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
19758c2ecf20Sopenharmony_ci	}
19768c2ecf20Sopenharmony_ci	if (sp->type == SRB_FXIOCB_DCMD)
19778c2ecf20Sopenharmony_ci		return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
19788c2ecf20Sopenharmony_ci		    FXDISC_ABORT_IOCTL);
19798c2ecf20Sopenharmony_ci
19808c2ecf20Sopenharmony_ci	return qla24xx_async_abort_cmd(sp, true);
19818c2ecf20Sopenharmony_ci}
19828c2ecf20Sopenharmony_ci
19838c2ecf20Sopenharmony_cistatic void
19848c2ecf20Sopenharmony_ciqla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
19858c2ecf20Sopenharmony_ci{
19868c2ecf20Sopenharmony_ci	WARN_ONCE(!qla2xxx_is_valid_mbs(ea->data[0]), "mbs: %#x\n",
19878c2ecf20Sopenharmony_ci		  ea->data[0]);
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_ci	switch (ea->data[0]) {
19908c2ecf20Sopenharmony_ci	case MBS_COMMAND_COMPLETE:
19918c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x2118,
19928c2ecf20Sopenharmony_ci		    "%s %d %8phC post gpdb\n",
19938c2ecf20Sopenharmony_ci		    __func__, __LINE__, ea->fcport->port_name);
19948c2ecf20Sopenharmony_ci
19958c2ecf20Sopenharmony_ci		ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
19968c2ecf20Sopenharmony_ci		ea->fcport->logout_on_delete = 1;
19978c2ecf20Sopenharmony_ci		ea->fcport->nvme_prli_service_param = ea->iop[0];
19988c2ecf20Sopenharmony_ci		if (ea->iop[0] & NVME_PRLI_SP_FIRST_BURST)
19998c2ecf20Sopenharmony_ci			ea->fcport->nvme_first_burst_size =
20008c2ecf20Sopenharmony_ci			    (ea->iop[1] & 0xffff) * 512;
20018c2ecf20Sopenharmony_ci		else
20028c2ecf20Sopenharmony_ci			ea->fcport->nvme_first_burst_size = 0;
20038c2ecf20Sopenharmony_ci		qla24xx_post_gpdb_work(vha, ea->fcport, 0);
20048c2ecf20Sopenharmony_ci		break;
20058c2ecf20Sopenharmony_ci	default:
20068c2ecf20Sopenharmony_ci		if ((ea->iop[0] == LSC_SCODE_ELS_REJECT) &&
20078c2ecf20Sopenharmony_ci		    (ea->iop[1] == 0x50000)) {   /* reson 5=busy expl:0x0 */
20088c2ecf20Sopenharmony_ci			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
20098c2ecf20Sopenharmony_ci			ea->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
20108c2ecf20Sopenharmony_ci			break;
20118c2ecf20Sopenharmony_ci		}
20128c2ecf20Sopenharmony_ci
20138c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x2118,
20148c2ecf20Sopenharmony_ci		       "%s %d %8phC priority %s, fc4type %x\n",
20158c2ecf20Sopenharmony_ci		       __func__, __LINE__, ea->fcport->port_name,
20168c2ecf20Sopenharmony_ci		       vha->hw->fc4_type_priority == FC4_PRIORITY_FCP ?
20178c2ecf20Sopenharmony_ci		       "FCP" : "NVMe", ea->fcport->fc4_type);
20188c2ecf20Sopenharmony_ci
20198c2ecf20Sopenharmony_ci		if (N2N_TOPO(vha->hw)) {
20208c2ecf20Sopenharmony_ci			if (vha->hw->fc4_type_priority == FC4_PRIORITY_NVME) {
20218c2ecf20Sopenharmony_ci				ea->fcport->fc4_type &= ~FS_FC4TYPE_NVME;
20228c2ecf20Sopenharmony_ci				ea->fcport->fc4_type |= FS_FC4TYPE_FCP;
20238c2ecf20Sopenharmony_ci			} else {
20248c2ecf20Sopenharmony_ci				ea->fcport->fc4_type &= ~FS_FC4TYPE_FCP;
20258c2ecf20Sopenharmony_ci				ea->fcport->fc4_type |= FS_FC4TYPE_NVME;
20268c2ecf20Sopenharmony_ci			}
20278c2ecf20Sopenharmony_ci
20288c2ecf20Sopenharmony_ci			if (ea->fcport->n2n_link_reset_cnt < 3) {
20298c2ecf20Sopenharmony_ci				ea->fcport->n2n_link_reset_cnt++;
20308c2ecf20Sopenharmony_ci				vha->relogin_jif = jiffies + 2 * HZ;
20318c2ecf20Sopenharmony_ci				/*
20328c2ecf20Sopenharmony_ci				 * PRLI failed. Reset link to kick start
20338c2ecf20Sopenharmony_ci				 * state machine
20348c2ecf20Sopenharmony_ci				 */
20358c2ecf20Sopenharmony_ci				set_bit(N2N_LINK_RESET, &vha->dpc_flags);
20368c2ecf20Sopenharmony_ci			} else {
20378c2ecf20Sopenharmony_ci				ql_log(ql_log_warn, vha, 0x2119,
20388c2ecf20Sopenharmony_ci				       "%s %d %8phC Unable to reconnect\n",
20398c2ecf20Sopenharmony_ci				       __func__, __LINE__,
20408c2ecf20Sopenharmony_ci				       ea->fcport->port_name);
20418c2ecf20Sopenharmony_ci			}
20428c2ecf20Sopenharmony_ci		} else {
20438c2ecf20Sopenharmony_ci			/*
20448c2ecf20Sopenharmony_ci			 * switch connect. login failed. Take connection down
20458c2ecf20Sopenharmony_ci			 * and allow relogin to retrigger
20468c2ecf20Sopenharmony_ci			 */
20478c2ecf20Sopenharmony_ci			if (NVME_FCP_TARGET(ea->fcport)) {
20488c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x2118,
20498c2ecf20Sopenharmony_ci				       "%s %d %8phC post %s prli\n",
20508c2ecf20Sopenharmony_ci				       __func__, __LINE__,
20518c2ecf20Sopenharmony_ci				       ea->fcport->port_name,
20528c2ecf20Sopenharmony_ci				       (ea->fcport->fc4_type & FS_FC4TYPE_NVME)
20538c2ecf20Sopenharmony_ci				       ? "NVMe" : "FCP");
20548c2ecf20Sopenharmony_ci				if (vha->hw->fc4_type_priority == FC4_PRIORITY_NVME)
20558c2ecf20Sopenharmony_ci					ea->fcport->fc4_type &= ~FS_FC4TYPE_NVME;
20568c2ecf20Sopenharmony_ci				else
20578c2ecf20Sopenharmony_ci					ea->fcport->fc4_type &= ~FS_FC4TYPE_FCP;
20588c2ecf20Sopenharmony_ci			}
20598c2ecf20Sopenharmony_ci
20608c2ecf20Sopenharmony_ci			ea->fcport->flags &= ~FCF_ASYNC_SENT;
20618c2ecf20Sopenharmony_ci			ea->fcport->keep_nport_handle = 0;
20628c2ecf20Sopenharmony_ci			ea->fcport->logout_on_delete = 1;
20638c2ecf20Sopenharmony_ci			qlt_schedule_sess_for_deletion(ea->fcport);
20648c2ecf20Sopenharmony_ci		}
20658c2ecf20Sopenharmony_ci		break;
20668c2ecf20Sopenharmony_ci	}
20678c2ecf20Sopenharmony_ci}
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_civoid
20708c2ecf20Sopenharmony_ciqla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
20718c2ecf20Sopenharmony_ci{
20728c2ecf20Sopenharmony_ci	port_id_t cid;	/* conflict Nport id */
20738c2ecf20Sopenharmony_ci	u16 lid;
20748c2ecf20Sopenharmony_ci	struct fc_port *conflict_fcport;
20758c2ecf20Sopenharmony_ci	unsigned long flags;
20768c2ecf20Sopenharmony_ci	struct fc_port *fcport = ea->fcport;
20778c2ecf20Sopenharmony_ci
20788c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0xffff,
20798c2ecf20Sopenharmony_ci	    "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
20808c2ecf20Sopenharmony_ci	    __func__, fcport->port_name, fcport->disc_state,
20818c2ecf20Sopenharmony_ci	    fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
20828c2ecf20Sopenharmony_ci	    ea->sp->gen1, fcport->rscn_gen,
20838c2ecf20Sopenharmony_ci	    ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
20848c2ecf20Sopenharmony_ci
20858c2ecf20Sopenharmony_ci	if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
20868c2ecf20Sopenharmony_ci	    (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
20878c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20ea,
20888c2ecf20Sopenharmony_ci		    "%s %d %8phC Remote is trying to login\n",
20898c2ecf20Sopenharmony_ci		    __func__, __LINE__, fcport->port_name);
20908c2ecf20Sopenharmony_ci		return;
20918c2ecf20Sopenharmony_ci	}
20928c2ecf20Sopenharmony_ci
20938c2ecf20Sopenharmony_ci	if ((fcport->disc_state == DSC_DELETE_PEND) ||
20948c2ecf20Sopenharmony_ci	    (fcport->disc_state == DSC_DELETED)) {
20958c2ecf20Sopenharmony_ci		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
20968c2ecf20Sopenharmony_ci		return;
20978c2ecf20Sopenharmony_ci	}
20988c2ecf20Sopenharmony_ci
20998c2ecf20Sopenharmony_ci	if (ea->sp->gen2 != fcport->login_gen) {
21008c2ecf20Sopenharmony_ci		/* target side must have changed it. */
21018c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20d3,
21028c2ecf20Sopenharmony_ci		    "%s %8phC generation changed\n",
21038c2ecf20Sopenharmony_ci		    __func__, fcport->port_name);
21048c2ecf20Sopenharmony_ci		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
21058c2ecf20Sopenharmony_ci		return;
21068c2ecf20Sopenharmony_ci	} else if (ea->sp->gen1 != fcport->rscn_gen) {
21078c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20d3,
21088c2ecf20Sopenharmony_ci		    "%s %8phC RSCN generation changed\n",
21098c2ecf20Sopenharmony_ci		    __func__, fcport->port_name);
21108c2ecf20Sopenharmony_ci		qla_rscn_replay(fcport);
21118c2ecf20Sopenharmony_ci		qlt_schedule_sess_for_deletion(fcport);
21128c2ecf20Sopenharmony_ci		return;
21138c2ecf20Sopenharmony_ci	}
21148c2ecf20Sopenharmony_ci
21158c2ecf20Sopenharmony_ci	WARN_ONCE(!qla2xxx_is_valid_mbs(ea->data[0]), "mbs: %#x\n",
21168c2ecf20Sopenharmony_ci		  ea->data[0]);
21178c2ecf20Sopenharmony_ci
21188c2ecf20Sopenharmony_ci	switch (ea->data[0]) {
21198c2ecf20Sopenharmony_ci	case MBS_COMMAND_COMPLETE:
21208c2ecf20Sopenharmony_ci		/*
21218c2ecf20Sopenharmony_ci		 * Driver must validate login state - If PRLI not complete,
21228c2ecf20Sopenharmony_ci		 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
21238c2ecf20Sopenharmony_ci		 * requests.
21248c2ecf20Sopenharmony_ci		 */
21258c2ecf20Sopenharmony_ci		if (NVME_TARGET(vha->hw, ea->fcport)) {
21268c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2117,
21278c2ecf20Sopenharmony_ci				"%s %d %8phC post prli\n",
21288c2ecf20Sopenharmony_ci				__func__, __LINE__, ea->fcport->port_name);
21298c2ecf20Sopenharmony_ci			qla24xx_post_prli_work(vha, ea->fcport);
21308c2ecf20Sopenharmony_ci		} else {
21318c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x20ea,
21328c2ecf20Sopenharmony_ci			    "%s %d %8phC LoopID 0x%x in use with %06x. post gpdb\n",
21338c2ecf20Sopenharmony_ci			    __func__, __LINE__, ea->fcport->port_name,
21348c2ecf20Sopenharmony_ci			    ea->fcport->loop_id, ea->fcport->d_id.b24);
21358c2ecf20Sopenharmony_ci
21368c2ecf20Sopenharmony_ci			set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
21378c2ecf20Sopenharmony_ci			spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
21388c2ecf20Sopenharmony_ci			ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
21398c2ecf20Sopenharmony_ci			ea->fcport->logout_on_delete = 1;
21408c2ecf20Sopenharmony_ci			ea->fcport->send_els_logo = 0;
21418c2ecf20Sopenharmony_ci			ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
21428c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
21438c2ecf20Sopenharmony_ci
21448c2ecf20Sopenharmony_ci			qla24xx_post_gpdb_work(vha, ea->fcport, 0);
21458c2ecf20Sopenharmony_ci		}
21468c2ecf20Sopenharmony_ci		break;
21478c2ecf20Sopenharmony_ci	case MBS_COMMAND_ERROR:
21488c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
21498c2ecf20Sopenharmony_ci		    __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
21508c2ecf20Sopenharmony_ci
21518c2ecf20Sopenharmony_ci		qlt_schedule_sess_for_deletion(ea->fcport);
21528c2ecf20Sopenharmony_ci		break;
21538c2ecf20Sopenharmony_ci	case MBS_LOOP_ID_USED:
21548c2ecf20Sopenharmony_ci		/* data[1] = IO PARAM 1 = nport ID  */
21558c2ecf20Sopenharmony_ci		cid.b.domain = (ea->iop[1] >> 16) & 0xff;
21568c2ecf20Sopenharmony_ci		cid.b.area   = (ea->iop[1] >>  8) & 0xff;
21578c2ecf20Sopenharmony_ci		cid.b.al_pa  = ea->iop[1] & 0xff;
21588c2ecf20Sopenharmony_ci		cid.b.rsvd_1 = 0;
21598c2ecf20Sopenharmony_ci
21608c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20ec,
21618c2ecf20Sopenharmony_ci		    "%s %d %8phC lid %#x in use with pid %06x post gnl\n",
21628c2ecf20Sopenharmony_ci		    __func__, __LINE__, ea->fcport->port_name,
21638c2ecf20Sopenharmony_ci		    ea->fcport->loop_id, cid.b24);
21648c2ecf20Sopenharmony_ci
21658c2ecf20Sopenharmony_ci		set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
21668c2ecf20Sopenharmony_ci		ea->fcport->loop_id = FC_NO_LOOP_ID;
21678c2ecf20Sopenharmony_ci		qla24xx_post_gnl_work(vha, ea->fcport);
21688c2ecf20Sopenharmony_ci		break;
21698c2ecf20Sopenharmony_ci	case MBS_PORT_ID_USED:
21708c2ecf20Sopenharmony_ci		lid = ea->iop[1] & 0xffff;
21718c2ecf20Sopenharmony_ci		qlt_find_sess_invalidate_other(vha,
21728c2ecf20Sopenharmony_ci		    wwn_to_u64(ea->fcport->port_name),
21738c2ecf20Sopenharmony_ci		    ea->fcport->d_id, lid, &conflict_fcport);
21748c2ecf20Sopenharmony_ci
21758c2ecf20Sopenharmony_ci		if (conflict_fcport) {
21768c2ecf20Sopenharmony_ci			/*
21778c2ecf20Sopenharmony_ci			 * Another fcport share the same loop_id/nport id.
21788c2ecf20Sopenharmony_ci			 * Conflict fcport needs to finish cleanup before this
21798c2ecf20Sopenharmony_ci			 * fcport can proceed to login.
21808c2ecf20Sopenharmony_ci			 */
21818c2ecf20Sopenharmony_ci			conflict_fcport->conflict = ea->fcport;
21828c2ecf20Sopenharmony_ci			ea->fcport->login_pause = 1;
21838c2ecf20Sopenharmony_ci
21848c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x20ed,
21858c2ecf20Sopenharmony_ci			    "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
21868c2ecf20Sopenharmony_ci			    __func__, __LINE__, ea->fcport->port_name,
21878c2ecf20Sopenharmony_ci			    ea->fcport->d_id.b24, lid);
21888c2ecf20Sopenharmony_ci		} else {
21898c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x20ed,
21908c2ecf20Sopenharmony_ci			    "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
21918c2ecf20Sopenharmony_ci			    __func__, __LINE__, ea->fcport->port_name,
21928c2ecf20Sopenharmony_ci			    ea->fcport->d_id.b24, lid);
21938c2ecf20Sopenharmony_ci
21948c2ecf20Sopenharmony_ci			qla2x00_clear_loop_id(ea->fcport);
21958c2ecf20Sopenharmony_ci			set_bit(lid, vha->hw->loop_id_map);
21968c2ecf20Sopenharmony_ci			ea->fcport->loop_id = lid;
21978c2ecf20Sopenharmony_ci			ea->fcport->keep_nport_handle = 0;
21988c2ecf20Sopenharmony_ci			ea->fcport->logout_on_delete = 1;
21998c2ecf20Sopenharmony_ci			qlt_schedule_sess_for_deletion(ea->fcport);
22008c2ecf20Sopenharmony_ci		}
22018c2ecf20Sopenharmony_ci		break;
22028c2ecf20Sopenharmony_ci	}
22038c2ecf20Sopenharmony_ci	return;
22048c2ecf20Sopenharmony_ci}
22058c2ecf20Sopenharmony_ci
22068c2ecf20Sopenharmony_ci/****************************************************************************/
22078c2ecf20Sopenharmony_ci/*                QLogic ISP2x00 Hardware Support Functions.                */
22088c2ecf20Sopenharmony_ci/****************************************************************************/
22098c2ecf20Sopenharmony_ci
22108c2ecf20Sopenharmony_cistatic int
22118c2ecf20Sopenharmony_ciqla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
22128c2ecf20Sopenharmony_ci{
22138c2ecf20Sopenharmony_ci	int rval = QLA_SUCCESS;
22148c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
22158c2ecf20Sopenharmony_ci	uint32_t idc_major_ver, idc_minor_ver;
22168c2ecf20Sopenharmony_ci	uint16_t config[4];
22178c2ecf20Sopenharmony_ci
22188c2ecf20Sopenharmony_ci	qla83xx_idc_lock(vha, 0);
22198c2ecf20Sopenharmony_ci
22208c2ecf20Sopenharmony_ci	/* SV: TODO: Assign initialization timeout from
22218c2ecf20Sopenharmony_ci	 * flash-info / other param
22228c2ecf20Sopenharmony_ci	 */
22238c2ecf20Sopenharmony_ci	ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
22248c2ecf20Sopenharmony_ci	ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
22258c2ecf20Sopenharmony_ci
22268c2ecf20Sopenharmony_ci	/* Set our fcoe function presence */
22278c2ecf20Sopenharmony_ci	if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
22288c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_p3p, vha, 0xb077,
22298c2ecf20Sopenharmony_ci		    "Error while setting DRV-Presence.\n");
22308c2ecf20Sopenharmony_ci		rval = QLA_FUNCTION_FAILED;
22318c2ecf20Sopenharmony_ci		goto exit;
22328c2ecf20Sopenharmony_ci	}
22338c2ecf20Sopenharmony_ci
22348c2ecf20Sopenharmony_ci	/* Decide the reset ownership */
22358c2ecf20Sopenharmony_ci	qla83xx_reset_ownership(vha);
22368c2ecf20Sopenharmony_ci
22378c2ecf20Sopenharmony_ci	/*
22388c2ecf20Sopenharmony_ci	 * On first protocol driver load:
22398c2ecf20Sopenharmony_ci	 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
22408c2ecf20Sopenharmony_ci	 * register.
22418c2ecf20Sopenharmony_ci	 * Others: Check compatibility with current IDC Major version.
22428c2ecf20Sopenharmony_ci	 */
22438c2ecf20Sopenharmony_ci	qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
22448c2ecf20Sopenharmony_ci	if (ha->flags.nic_core_reset_owner) {
22458c2ecf20Sopenharmony_ci		/* Set IDC Major version */
22468c2ecf20Sopenharmony_ci		idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
22478c2ecf20Sopenharmony_ci		qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
22488c2ecf20Sopenharmony_ci
22498c2ecf20Sopenharmony_ci		/* Clearing IDC-Lock-Recovery register */
22508c2ecf20Sopenharmony_ci		qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
22518c2ecf20Sopenharmony_ci	} else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
22528c2ecf20Sopenharmony_ci		/*
22538c2ecf20Sopenharmony_ci		 * Clear further IDC participation if we are not compatible with
22548c2ecf20Sopenharmony_ci		 * the current IDC Major Version.
22558c2ecf20Sopenharmony_ci		 */
22568c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xb07d,
22578c2ecf20Sopenharmony_ci		    "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
22588c2ecf20Sopenharmony_ci		    idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
22598c2ecf20Sopenharmony_ci		__qla83xx_clear_drv_presence(vha);
22608c2ecf20Sopenharmony_ci		rval = QLA_FUNCTION_FAILED;
22618c2ecf20Sopenharmony_ci		goto exit;
22628c2ecf20Sopenharmony_ci	}
22638c2ecf20Sopenharmony_ci	/* Each function sets its supported Minor version. */
22648c2ecf20Sopenharmony_ci	qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
22658c2ecf20Sopenharmony_ci	idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
22668c2ecf20Sopenharmony_ci	qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
22678c2ecf20Sopenharmony_ci
22688c2ecf20Sopenharmony_ci	if (ha->flags.nic_core_reset_owner) {
22698c2ecf20Sopenharmony_ci		memset(config, 0, sizeof(config));
22708c2ecf20Sopenharmony_ci		if (!qla81xx_get_port_config(vha, config))
22718c2ecf20Sopenharmony_ci			qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
22728c2ecf20Sopenharmony_ci			    QLA8XXX_DEV_READY);
22738c2ecf20Sopenharmony_ci	}
22748c2ecf20Sopenharmony_ci
22758c2ecf20Sopenharmony_ci	rval = qla83xx_idc_state_handler(vha);
22768c2ecf20Sopenharmony_ci
22778c2ecf20Sopenharmony_ciexit:
22788c2ecf20Sopenharmony_ci	qla83xx_idc_unlock(vha, 0);
22798c2ecf20Sopenharmony_ci
22808c2ecf20Sopenharmony_ci	return rval;
22818c2ecf20Sopenharmony_ci}
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci/*
22848c2ecf20Sopenharmony_ci* qla2x00_initialize_adapter
22858c2ecf20Sopenharmony_ci*      Initialize board.
22868c2ecf20Sopenharmony_ci*
22878c2ecf20Sopenharmony_ci* Input:
22888c2ecf20Sopenharmony_ci*      ha = adapter block pointer.
22898c2ecf20Sopenharmony_ci*
22908c2ecf20Sopenharmony_ci* Returns:
22918c2ecf20Sopenharmony_ci*      0 = success
22928c2ecf20Sopenharmony_ci*/
22938c2ecf20Sopenharmony_ciint
22948c2ecf20Sopenharmony_ciqla2x00_initialize_adapter(scsi_qla_host_t *vha)
22958c2ecf20Sopenharmony_ci{
22968c2ecf20Sopenharmony_ci	int	rval;
22978c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
22988c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
22998c2ecf20Sopenharmony_ci	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
23008c2ecf20Sopenharmony_ci
23018c2ecf20Sopenharmony_ci	memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
23028c2ecf20Sopenharmony_ci	memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
23038c2ecf20Sopenharmony_ci
23048c2ecf20Sopenharmony_ci	/* Clear adapter flags. */
23058c2ecf20Sopenharmony_ci	vha->flags.online = 0;
23068c2ecf20Sopenharmony_ci	ha->flags.chip_reset_done = 0;
23078c2ecf20Sopenharmony_ci	vha->flags.reset_active = 0;
23088c2ecf20Sopenharmony_ci	ha->flags.pci_channel_io_perm_failure = 0;
23098c2ecf20Sopenharmony_ci	ha->flags.eeh_busy = 0;
23108c2ecf20Sopenharmony_ci	vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
23118c2ecf20Sopenharmony_ci	atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
23128c2ecf20Sopenharmony_ci	atomic_set(&vha->loop_state, LOOP_DOWN);
23138c2ecf20Sopenharmony_ci	vha->device_flags = DFLG_NO_CABLE;
23148c2ecf20Sopenharmony_ci	vha->dpc_flags = 0;
23158c2ecf20Sopenharmony_ci	vha->flags.management_server_logged_in = 0;
23168c2ecf20Sopenharmony_ci	vha->marker_needed = 0;
23178c2ecf20Sopenharmony_ci	ha->isp_abort_cnt = 0;
23188c2ecf20Sopenharmony_ci	ha->beacon_blink_led = 0;
23198c2ecf20Sopenharmony_ci
23208c2ecf20Sopenharmony_ci	set_bit(0, ha->req_qid_map);
23218c2ecf20Sopenharmony_ci	set_bit(0, ha->rsp_qid_map);
23228c2ecf20Sopenharmony_ci
23238c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x0040,
23248c2ecf20Sopenharmony_ci	    "Configuring PCI space...\n");
23258c2ecf20Sopenharmony_ci	rval = ha->isp_ops->pci_config(vha);
23268c2ecf20Sopenharmony_ci	if (rval) {
23278c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0044,
23288c2ecf20Sopenharmony_ci		    "Unable to configure PCI space.\n");
23298c2ecf20Sopenharmony_ci		return (rval);
23308c2ecf20Sopenharmony_ci	}
23318c2ecf20Sopenharmony_ci
23328c2ecf20Sopenharmony_ci	ha->isp_ops->reset_chip(vha);
23338c2ecf20Sopenharmony_ci
23348c2ecf20Sopenharmony_ci	/* Check for secure flash support */
23358c2ecf20Sopenharmony_ci	if (IS_QLA28XX(ha)) {
23368c2ecf20Sopenharmony_ci		if (rd_reg_word(&reg->mailbox12) & BIT_0)
23378c2ecf20Sopenharmony_ci			ha->flags.secure_adapter = 1;
23388c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0xffff, "Secure Adapter: %s\n",
23398c2ecf20Sopenharmony_ci		    (ha->flags.secure_adapter) ? "Yes" : "No");
23408c2ecf20Sopenharmony_ci	}
23418c2ecf20Sopenharmony_ci
23428c2ecf20Sopenharmony_ci
23438c2ecf20Sopenharmony_ci	rval = qla2xxx_get_flash_info(vha);
23448c2ecf20Sopenharmony_ci	if (rval) {
23458c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x004f,
23468c2ecf20Sopenharmony_ci		    "Unable to validate FLASH data.\n");
23478c2ecf20Sopenharmony_ci		return rval;
23488c2ecf20Sopenharmony_ci	}
23498c2ecf20Sopenharmony_ci
23508c2ecf20Sopenharmony_ci	if (IS_QLA8044(ha)) {
23518c2ecf20Sopenharmony_ci		qla8044_read_reset_template(vha);
23528c2ecf20Sopenharmony_ci
23538c2ecf20Sopenharmony_ci		/* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
23548c2ecf20Sopenharmony_ci		 * If DONRESET_BIT0 is set, drivers should not set dev_state
23558c2ecf20Sopenharmony_ci		 * to NEED_RESET. But if NEED_RESET is set, drivers should
23568c2ecf20Sopenharmony_ci		 * should honor the reset. */
23578c2ecf20Sopenharmony_ci		if (ql2xdontresethba == 1)
23588c2ecf20Sopenharmony_ci			qla8044_set_idc_dontreset(vha);
23598c2ecf20Sopenharmony_ci	}
23608c2ecf20Sopenharmony_ci
23618c2ecf20Sopenharmony_ci	ha->isp_ops->get_flash_version(vha, req->ring);
23628c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x0061,
23638c2ecf20Sopenharmony_ci	    "Configure NVRAM parameters...\n");
23648c2ecf20Sopenharmony_ci
23658c2ecf20Sopenharmony_ci	/* Let priority default to FCP, can be overridden by nvram_config */
23668c2ecf20Sopenharmony_ci	ha->fc4_type_priority = FC4_PRIORITY_FCP;
23678c2ecf20Sopenharmony_ci
23688c2ecf20Sopenharmony_ci	ha->isp_ops->nvram_config(vha);
23698c2ecf20Sopenharmony_ci
23708c2ecf20Sopenharmony_ci	if (ha->fc4_type_priority != FC4_PRIORITY_FCP &&
23718c2ecf20Sopenharmony_ci	    ha->fc4_type_priority != FC4_PRIORITY_NVME)
23728c2ecf20Sopenharmony_ci		ha->fc4_type_priority = FC4_PRIORITY_FCP;
23738c2ecf20Sopenharmony_ci
23748c2ecf20Sopenharmony_ci	ql_log(ql_log_info, vha, 0xffff, "FC4 priority set to %s\n",
23758c2ecf20Sopenharmony_ci	       ha->fc4_type_priority == FC4_PRIORITY_FCP ? "FCP" : "NVMe");
23768c2ecf20Sopenharmony_ci
23778c2ecf20Sopenharmony_ci	if (ha->flags.disable_serdes) {
23788c2ecf20Sopenharmony_ci		/* Mask HBA via NVRAM settings? */
23798c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x0077,
23808c2ecf20Sopenharmony_ci		    "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
23818c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
23828c2ecf20Sopenharmony_ci	}
23838c2ecf20Sopenharmony_ci
23848c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x0078,
23858c2ecf20Sopenharmony_ci	    "Verifying loaded RISC code...\n");
23868c2ecf20Sopenharmony_ci
23878c2ecf20Sopenharmony_ci	/* If smartsan enabled then require fdmi and rdp enabled */
23888c2ecf20Sopenharmony_ci	if (ql2xsmartsan) {
23898c2ecf20Sopenharmony_ci		ql2xfdmienable = 1;
23908c2ecf20Sopenharmony_ci		ql2xrdpenable = 1;
23918c2ecf20Sopenharmony_ci	}
23928c2ecf20Sopenharmony_ci
23938c2ecf20Sopenharmony_ci	if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
23948c2ecf20Sopenharmony_ci		rval = ha->isp_ops->chip_diag(vha);
23958c2ecf20Sopenharmony_ci		if (rval)
23968c2ecf20Sopenharmony_ci			return (rval);
23978c2ecf20Sopenharmony_ci		rval = qla2x00_setup_chip(vha);
23988c2ecf20Sopenharmony_ci		if (rval)
23998c2ecf20Sopenharmony_ci			return (rval);
24008c2ecf20Sopenharmony_ci	}
24018c2ecf20Sopenharmony_ci
24028c2ecf20Sopenharmony_ci	if (IS_QLA84XX(ha)) {
24038c2ecf20Sopenharmony_ci		ha->cs84xx = qla84xx_get_chip(vha);
24048c2ecf20Sopenharmony_ci		if (!ha->cs84xx) {
24058c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x00d0,
24068c2ecf20Sopenharmony_ci			    "Unable to configure ISP84XX.\n");
24078c2ecf20Sopenharmony_ci			return QLA_FUNCTION_FAILED;
24088c2ecf20Sopenharmony_ci		}
24098c2ecf20Sopenharmony_ci	}
24108c2ecf20Sopenharmony_ci
24118c2ecf20Sopenharmony_ci	if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
24128c2ecf20Sopenharmony_ci		rval = qla2x00_init_rings(vha);
24138c2ecf20Sopenharmony_ci
24148c2ecf20Sopenharmony_ci	/* No point in continuing if firmware initialization failed. */
24158c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS)
24168c2ecf20Sopenharmony_ci		return rval;
24178c2ecf20Sopenharmony_ci
24188c2ecf20Sopenharmony_ci	ha->flags.chip_reset_done = 1;
24198c2ecf20Sopenharmony_ci
24208c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
24218c2ecf20Sopenharmony_ci		/* Issue verify 84xx FW IOCB to complete 84xx initialization */
24228c2ecf20Sopenharmony_ci		rval = qla84xx_init_chip(vha);
24238c2ecf20Sopenharmony_ci		if (rval != QLA_SUCCESS) {
24248c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x00d4,
24258c2ecf20Sopenharmony_ci			    "Unable to initialize ISP84XX.\n");
24268c2ecf20Sopenharmony_ci			qla84xx_put_chip(vha);
24278c2ecf20Sopenharmony_ci		}
24288c2ecf20Sopenharmony_ci	}
24298c2ecf20Sopenharmony_ci
24308c2ecf20Sopenharmony_ci	/* Load the NIC Core f/w if we are the first protocol driver. */
24318c2ecf20Sopenharmony_ci	if (IS_QLA8031(ha)) {
24328c2ecf20Sopenharmony_ci		rval = qla83xx_nic_core_fw_load(vha);
24338c2ecf20Sopenharmony_ci		if (rval)
24348c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0124,
24358c2ecf20Sopenharmony_ci			    "Error in initializing NIC Core f/w.\n");
24368c2ecf20Sopenharmony_ci	}
24378c2ecf20Sopenharmony_ci
24388c2ecf20Sopenharmony_ci	if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
24398c2ecf20Sopenharmony_ci		qla24xx_read_fcp_prio_cfg(vha);
24408c2ecf20Sopenharmony_ci
24418c2ecf20Sopenharmony_ci	if (IS_P3P_TYPE(ha))
24428c2ecf20Sopenharmony_ci		qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
24438c2ecf20Sopenharmony_ci	else
24448c2ecf20Sopenharmony_ci		qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
24458c2ecf20Sopenharmony_ci
24468c2ecf20Sopenharmony_ci	return (rval);
24478c2ecf20Sopenharmony_ci}
24488c2ecf20Sopenharmony_ci
24498c2ecf20Sopenharmony_ci/**
24508c2ecf20Sopenharmony_ci * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
24518c2ecf20Sopenharmony_ci * @vha: HA context
24528c2ecf20Sopenharmony_ci *
24538c2ecf20Sopenharmony_ci * Returns 0 on success.
24548c2ecf20Sopenharmony_ci */
24558c2ecf20Sopenharmony_ciint
24568c2ecf20Sopenharmony_ciqla2100_pci_config(scsi_qla_host_t *vha)
24578c2ecf20Sopenharmony_ci{
24588c2ecf20Sopenharmony_ci	uint16_t w;
24598c2ecf20Sopenharmony_ci	unsigned long flags;
24608c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
24618c2ecf20Sopenharmony_ci	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
24628c2ecf20Sopenharmony_ci
24638c2ecf20Sopenharmony_ci	pci_set_master(ha->pdev);
24648c2ecf20Sopenharmony_ci	pci_try_set_mwi(ha->pdev);
24658c2ecf20Sopenharmony_ci
24668c2ecf20Sopenharmony_ci	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
24678c2ecf20Sopenharmony_ci	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
24688c2ecf20Sopenharmony_ci	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
24698c2ecf20Sopenharmony_ci
24708c2ecf20Sopenharmony_ci	pci_disable_rom(ha->pdev);
24718c2ecf20Sopenharmony_ci
24728c2ecf20Sopenharmony_ci	/* Get PCI bus information. */
24738c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
24748c2ecf20Sopenharmony_ci	ha->pci_attr = rd_reg_word(&reg->ctrl_status);
24758c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
24768c2ecf20Sopenharmony_ci
24778c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
24788c2ecf20Sopenharmony_ci}
24798c2ecf20Sopenharmony_ci
24808c2ecf20Sopenharmony_ci/**
24818c2ecf20Sopenharmony_ci * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
24828c2ecf20Sopenharmony_ci * @vha: HA context
24838c2ecf20Sopenharmony_ci *
24848c2ecf20Sopenharmony_ci * Returns 0 on success.
24858c2ecf20Sopenharmony_ci */
24868c2ecf20Sopenharmony_ciint
24878c2ecf20Sopenharmony_ciqla2300_pci_config(scsi_qla_host_t *vha)
24888c2ecf20Sopenharmony_ci{
24898c2ecf20Sopenharmony_ci	uint16_t	w;
24908c2ecf20Sopenharmony_ci	unsigned long   flags = 0;
24918c2ecf20Sopenharmony_ci	uint32_t	cnt;
24928c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
24938c2ecf20Sopenharmony_ci	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
24948c2ecf20Sopenharmony_ci
24958c2ecf20Sopenharmony_ci	pci_set_master(ha->pdev);
24968c2ecf20Sopenharmony_ci	pci_try_set_mwi(ha->pdev);
24978c2ecf20Sopenharmony_ci
24988c2ecf20Sopenharmony_ci	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
24998c2ecf20Sopenharmony_ci	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
25008c2ecf20Sopenharmony_ci
25018c2ecf20Sopenharmony_ci	if (IS_QLA2322(ha) || IS_QLA6322(ha))
25028c2ecf20Sopenharmony_ci		w &= ~PCI_COMMAND_INTX_DISABLE;
25038c2ecf20Sopenharmony_ci	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
25048c2ecf20Sopenharmony_ci
25058c2ecf20Sopenharmony_ci	/*
25068c2ecf20Sopenharmony_ci	 * If this is a 2300 card and not 2312, reset the
25078c2ecf20Sopenharmony_ci	 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
25088c2ecf20Sopenharmony_ci	 * the 2310 also reports itself as a 2300 so we need to get the
25098c2ecf20Sopenharmony_ci	 * fb revision level -- a 6 indicates it really is a 2300 and
25108c2ecf20Sopenharmony_ci	 * not a 2310.
25118c2ecf20Sopenharmony_ci	 */
25128c2ecf20Sopenharmony_ci	if (IS_QLA2300(ha)) {
25138c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->hardware_lock, flags);
25148c2ecf20Sopenharmony_ci
25158c2ecf20Sopenharmony_ci		/* Pause RISC. */
25168c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->hccr, HCCR_PAUSE_RISC);
25178c2ecf20Sopenharmony_ci		for (cnt = 0; cnt < 30000; cnt++) {
25188c2ecf20Sopenharmony_ci			if ((rd_reg_word(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
25198c2ecf20Sopenharmony_ci				break;
25208c2ecf20Sopenharmony_ci
25218c2ecf20Sopenharmony_ci			udelay(10);
25228c2ecf20Sopenharmony_ci		}
25238c2ecf20Sopenharmony_ci
25248c2ecf20Sopenharmony_ci		/* Select FPM registers. */
25258c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->ctrl_status, 0x20);
25268c2ecf20Sopenharmony_ci		rd_reg_word(&reg->ctrl_status);
25278c2ecf20Sopenharmony_ci
25288c2ecf20Sopenharmony_ci		/* Get the fb rev level */
25298c2ecf20Sopenharmony_ci		ha->fb_rev = RD_FB_CMD_REG(ha, reg);
25308c2ecf20Sopenharmony_ci
25318c2ecf20Sopenharmony_ci		if (ha->fb_rev == FPM_2300)
25328c2ecf20Sopenharmony_ci			pci_clear_mwi(ha->pdev);
25338c2ecf20Sopenharmony_ci
25348c2ecf20Sopenharmony_ci		/* Deselect FPM registers. */
25358c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->ctrl_status, 0x0);
25368c2ecf20Sopenharmony_ci		rd_reg_word(&reg->ctrl_status);
25378c2ecf20Sopenharmony_ci
25388c2ecf20Sopenharmony_ci		/* Release RISC module. */
25398c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
25408c2ecf20Sopenharmony_ci		for (cnt = 0; cnt < 30000; cnt++) {
25418c2ecf20Sopenharmony_ci			if ((rd_reg_word(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
25428c2ecf20Sopenharmony_ci				break;
25438c2ecf20Sopenharmony_ci
25448c2ecf20Sopenharmony_ci			udelay(10);
25458c2ecf20Sopenharmony_ci		}
25468c2ecf20Sopenharmony_ci
25478c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->hardware_lock, flags);
25488c2ecf20Sopenharmony_ci	}
25498c2ecf20Sopenharmony_ci
25508c2ecf20Sopenharmony_ci	pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
25518c2ecf20Sopenharmony_ci
25528c2ecf20Sopenharmony_ci	pci_disable_rom(ha->pdev);
25538c2ecf20Sopenharmony_ci
25548c2ecf20Sopenharmony_ci	/* Get PCI bus information. */
25558c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
25568c2ecf20Sopenharmony_ci	ha->pci_attr = rd_reg_word(&reg->ctrl_status);
25578c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
25588c2ecf20Sopenharmony_ci
25598c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
25608c2ecf20Sopenharmony_ci}
25618c2ecf20Sopenharmony_ci
25628c2ecf20Sopenharmony_ci/**
25638c2ecf20Sopenharmony_ci * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
25648c2ecf20Sopenharmony_ci * @vha: HA context
25658c2ecf20Sopenharmony_ci *
25668c2ecf20Sopenharmony_ci * Returns 0 on success.
25678c2ecf20Sopenharmony_ci */
25688c2ecf20Sopenharmony_ciint
25698c2ecf20Sopenharmony_ciqla24xx_pci_config(scsi_qla_host_t *vha)
25708c2ecf20Sopenharmony_ci{
25718c2ecf20Sopenharmony_ci	uint16_t w;
25728c2ecf20Sopenharmony_ci	unsigned long flags = 0;
25738c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
25748c2ecf20Sopenharmony_ci	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
25758c2ecf20Sopenharmony_ci
25768c2ecf20Sopenharmony_ci	pci_set_master(ha->pdev);
25778c2ecf20Sopenharmony_ci	pci_try_set_mwi(ha->pdev);
25788c2ecf20Sopenharmony_ci
25798c2ecf20Sopenharmony_ci	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
25808c2ecf20Sopenharmony_ci	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
25818c2ecf20Sopenharmony_ci	w &= ~PCI_COMMAND_INTX_DISABLE;
25828c2ecf20Sopenharmony_ci	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
25838c2ecf20Sopenharmony_ci
25848c2ecf20Sopenharmony_ci	pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
25858c2ecf20Sopenharmony_ci
25868c2ecf20Sopenharmony_ci	/* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
25878c2ecf20Sopenharmony_ci	if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
25888c2ecf20Sopenharmony_ci		pcix_set_mmrbc(ha->pdev, 2048);
25898c2ecf20Sopenharmony_ci
25908c2ecf20Sopenharmony_ci	/* PCIe -- adjust Maximum Read Request Size (2048). */
25918c2ecf20Sopenharmony_ci	if (pci_is_pcie(ha->pdev))
25928c2ecf20Sopenharmony_ci		pcie_set_readrq(ha->pdev, 4096);
25938c2ecf20Sopenharmony_ci
25948c2ecf20Sopenharmony_ci	pci_disable_rom(ha->pdev);
25958c2ecf20Sopenharmony_ci
25968c2ecf20Sopenharmony_ci	ha->chip_revision = ha->pdev->revision;
25978c2ecf20Sopenharmony_ci
25988c2ecf20Sopenharmony_ci	/* Get PCI bus information. */
25998c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
26008c2ecf20Sopenharmony_ci	ha->pci_attr = rd_reg_dword(&reg->ctrl_status);
26018c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
26028c2ecf20Sopenharmony_ci
26038c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
26048c2ecf20Sopenharmony_ci}
26058c2ecf20Sopenharmony_ci
26068c2ecf20Sopenharmony_ci/**
26078c2ecf20Sopenharmony_ci * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
26088c2ecf20Sopenharmony_ci * @vha: HA context
26098c2ecf20Sopenharmony_ci *
26108c2ecf20Sopenharmony_ci * Returns 0 on success.
26118c2ecf20Sopenharmony_ci */
26128c2ecf20Sopenharmony_ciint
26138c2ecf20Sopenharmony_ciqla25xx_pci_config(scsi_qla_host_t *vha)
26148c2ecf20Sopenharmony_ci{
26158c2ecf20Sopenharmony_ci	uint16_t w;
26168c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
26178c2ecf20Sopenharmony_ci
26188c2ecf20Sopenharmony_ci	pci_set_master(ha->pdev);
26198c2ecf20Sopenharmony_ci	pci_try_set_mwi(ha->pdev);
26208c2ecf20Sopenharmony_ci
26218c2ecf20Sopenharmony_ci	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
26228c2ecf20Sopenharmony_ci	w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
26238c2ecf20Sopenharmony_ci	w &= ~PCI_COMMAND_INTX_DISABLE;
26248c2ecf20Sopenharmony_ci	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
26258c2ecf20Sopenharmony_ci
26268c2ecf20Sopenharmony_ci	/* PCIe -- adjust Maximum Read Request Size (2048). */
26278c2ecf20Sopenharmony_ci	if (pci_is_pcie(ha->pdev))
26288c2ecf20Sopenharmony_ci		pcie_set_readrq(ha->pdev, 4096);
26298c2ecf20Sopenharmony_ci
26308c2ecf20Sopenharmony_ci	pci_disable_rom(ha->pdev);
26318c2ecf20Sopenharmony_ci
26328c2ecf20Sopenharmony_ci	ha->chip_revision = ha->pdev->revision;
26338c2ecf20Sopenharmony_ci
26348c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
26358c2ecf20Sopenharmony_ci}
26368c2ecf20Sopenharmony_ci
26378c2ecf20Sopenharmony_ci/**
26388c2ecf20Sopenharmony_ci * qla2x00_isp_firmware() - Choose firmware image.
26398c2ecf20Sopenharmony_ci * @vha: HA context
26408c2ecf20Sopenharmony_ci *
26418c2ecf20Sopenharmony_ci * Returns 0 on success.
26428c2ecf20Sopenharmony_ci */
26438c2ecf20Sopenharmony_cistatic int
26448c2ecf20Sopenharmony_ciqla2x00_isp_firmware(scsi_qla_host_t *vha)
26458c2ecf20Sopenharmony_ci{
26468c2ecf20Sopenharmony_ci	int  rval;
26478c2ecf20Sopenharmony_ci	uint16_t loop_id, topo, sw_cap;
26488c2ecf20Sopenharmony_ci	uint8_t domain, area, al_pa;
26498c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
26508c2ecf20Sopenharmony_ci
26518c2ecf20Sopenharmony_ci	/* Assume loading risc code */
26528c2ecf20Sopenharmony_ci	rval = QLA_FUNCTION_FAILED;
26538c2ecf20Sopenharmony_ci
26548c2ecf20Sopenharmony_ci	if (ha->flags.disable_risc_code_load) {
26558c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
26568c2ecf20Sopenharmony_ci
26578c2ecf20Sopenharmony_ci		/* Verify checksum of loaded RISC code. */
26588c2ecf20Sopenharmony_ci		rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
26598c2ecf20Sopenharmony_ci		if (rval == QLA_SUCCESS) {
26608c2ecf20Sopenharmony_ci			/* And, verify we are not in ROM code. */
26618c2ecf20Sopenharmony_ci			rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
26628c2ecf20Sopenharmony_ci			    &area, &domain, &topo, &sw_cap);
26638c2ecf20Sopenharmony_ci		}
26648c2ecf20Sopenharmony_ci	}
26658c2ecf20Sopenharmony_ci
26668c2ecf20Sopenharmony_ci	if (rval)
26678c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x007a,
26688c2ecf20Sopenharmony_ci		    "**** Load RISC code ****.\n");
26698c2ecf20Sopenharmony_ci
26708c2ecf20Sopenharmony_ci	return (rval);
26718c2ecf20Sopenharmony_ci}
26728c2ecf20Sopenharmony_ci
26738c2ecf20Sopenharmony_ci/**
26748c2ecf20Sopenharmony_ci * qla2x00_reset_chip() - Reset ISP chip.
26758c2ecf20Sopenharmony_ci * @vha: HA context
26768c2ecf20Sopenharmony_ci *
26778c2ecf20Sopenharmony_ci * Returns 0 on success.
26788c2ecf20Sopenharmony_ci */
26798c2ecf20Sopenharmony_ciint
26808c2ecf20Sopenharmony_ciqla2x00_reset_chip(scsi_qla_host_t *vha)
26818c2ecf20Sopenharmony_ci{
26828c2ecf20Sopenharmony_ci	unsigned long   flags = 0;
26838c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
26848c2ecf20Sopenharmony_ci	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
26858c2ecf20Sopenharmony_ci	uint32_t	cnt;
26868c2ecf20Sopenharmony_ci	uint16_t	cmd;
26878c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
26888c2ecf20Sopenharmony_ci
26898c2ecf20Sopenharmony_ci	if (unlikely(pci_channel_offline(ha->pdev)))
26908c2ecf20Sopenharmony_ci		return rval;
26918c2ecf20Sopenharmony_ci
26928c2ecf20Sopenharmony_ci	ha->isp_ops->disable_intrs(ha);
26938c2ecf20Sopenharmony_ci
26948c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
26958c2ecf20Sopenharmony_ci
26968c2ecf20Sopenharmony_ci	/* Turn off master enable */
26978c2ecf20Sopenharmony_ci	cmd = 0;
26988c2ecf20Sopenharmony_ci	pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
26998c2ecf20Sopenharmony_ci	cmd &= ~PCI_COMMAND_MASTER;
27008c2ecf20Sopenharmony_ci	pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
27018c2ecf20Sopenharmony_ci
27028c2ecf20Sopenharmony_ci	if (!IS_QLA2100(ha)) {
27038c2ecf20Sopenharmony_ci		/* Pause RISC. */
27048c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->hccr, HCCR_PAUSE_RISC);
27058c2ecf20Sopenharmony_ci		if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
27068c2ecf20Sopenharmony_ci			for (cnt = 0; cnt < 30000; cnt++) {
27078c2ecf20Sopenharmony_ci				if ((rd_reg_word(&reg->hccr) &
27088c2ecf20Sopenharmony_ci				    HCCR_RISC_PAUSE) != 0)
27098c2ecf20Sopenharmony_ci					break;
27108c2ecf20Sopenharmony_ci				udelay(100);
27118c2ecf20Sopenharmony_ci			}
27128c2ecf20Sopenharmony_ci		} else {
27138c2ecf20Sopenharmony_ci			rd_reg_word(&reg->hccr);	/* PCI Posting. */
27148c2ecf20Sopenharmony_ci			udelay(10);
27158c2ecf20Sopenharmony_ci		}
27168c2ecf20Sopenharmony_ci
27178c2ecf20Sopenharmony_ci		/* Select FPM registers. */
27188c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->ctrl_status, 0x20);
27198c2ecf20Sopenharmony_ci		rd_reg_word(&reg->ctrl_status);		/* PCI Posting. */
27208c2ecf20Sopenharmony_ci
27218c2ecf20Sopenharmony_ci		/* FPM Soft Reset. */
27228c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->fpm_diag_config, 0x100);
27238c2ecf20Sopenharmony_ci		rd_reg_word(&reg->fpm_diag_config);	/* PCI Posting. */
27248c2ecf20Sopenharmony_ci
27258c2ecf20Sopenharmony_ci		/* Toggle Fpm Reset. */
27268c2ecf20Sopenharmony_ci		if (!IS_QLA2200(ha)) {
27278c2ecf20Sopenharmony_ci			wrt_reg_word(&reg->fpm_diag_config, 0x0);
27288c2ecf20Sopenharmony_ci			rd_reg_word(&reg->fpm_diag_config); /* PCI Posting. */
27298c2ecf20Sopenharmony_ci		}
27308c2ecf20Sopenharmony_ci
27318c2ecf20Sopenharmony_ci		/* Select frame buffer registers. */
27328c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->ctrl_status, 0x10);
27338c2ecf20Sopenharmony_ci		rd_reg_word(&reg->ctrl_status);		/* PCI Posting. */
27348c2ecf20Sopenharmony_ci
27358c2ecf20Sopenharmony_ci		/* Reset frame buffer FIFOs. */
27368c2ecf20Sopenharmony_ci		if (IS_QLA2200(ha)) {
27378c2ecf20Sopenharmony_ci			WRT_FB_CMD_REG(ha, reg, 0xa000);
27388c2ecf20Sopenharmony_ci			RD_FB_CMD_REG(ha, reg);		/* PCI Posting. */
27398c2ecf20Sopenharmony_ci		} else {
27408c2ecf20Sopenharmony_ci			WRT_FB_CMD_REG(ha, reg, 0x00fc);
27418c2ecf20Sopenharmony_ci
27428c2ecf20Sopenharmony_ci			/* Read back fb_cmd until zero or 3 seconds max */
27438c2ecf20Sopenharmony_ci			for (cnt = 0; cnt < 3000; cnt++) {
27448c2ecf20Sopenharmony_ci				if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
27458c2ecf20Sopenharmony_ci					break;
27468c2ecf20Sopenharmony_ci				udelay(100);
27478c2ecf20Sopenharmony_ci			}
27488c2ecf20Sopenharmony_ci		}
27498c2ecf20Sopenharmony_ci
27508c2ecf20Sopenharmony_ci		/* Select RISC module registers. */
27518c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->ctrl_status, 0);
27528c2ecf20Sopenharmony_ci		rd_reg_word(&reg->ctrl_status);		/* PCI Posting. */
27538c2ecf20Sopenharmony_ci
27548c2ecf20Sopenharmony_ci		/* Reset RISC processor. */
27558c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
27568c2ecf20Sopenharmony_ci		rd_reg_word(&reg->hccr);		/* PCI Posting. */
27578c2ecf20Sopenharmony_ci
27588c2ecf20Sopenharmony_ci		/* Release RISC processor. */
27598c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
27608c2ecf20Sopenharmony_ci		rd_reg_word(&reg->hccr);		/* PCI Posting. */
27618c2ecf20Sopenharmony_ci	}
27628c2ecf20Sopenharmony_ci
27638c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->hccr, HCCR_CLR_RISC_INT);
27648c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->hccr, HCCR_CLR_HOST_INT);
27658c2ecf20Sopenharmony_ci
27668c2ecf20Sopenharmony_ci	/* Reset ISP chip. */
27678c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
27688c2ecf20Sopenharmony_ci
27698c2ecf20Sopenharmony_ci	/* Wait for RISC to recover from reset. */
27708c2ecf20Sopenharmony_ci	if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
27718c2ecf20Sopenharmony_ci		/*
27728c2ecf20Sopenharmony_ci		 * It is necessary to for a delay here since the card doesn't
27738c2ecf20Sopenharmony_ci		 * respond to PCI reads during a reset. On some architectures
27748c2ecf20Sopenharmony_ci		 * this will result in an MCA.
27758c2ecf20Sopenharmony_ci		 */
27768c2ecf20Sopenharmony_ci		udelay(20);
27778c2ecf20Sopenharmony_ci		for (cnt = 30000; cnt; cnt--) {
27788c2ecf20Sopenharmony_ci			if ((rd_reg_word(&reg->ctrl_status) &
27798c2ecf20Sopenharmony_ci			    CSR_ISP_SOFT_RESET) == 0)
27808c2ecf20Sopenharmony_ci				break;
27818c2ecf20Sopenharmony_ci			udelay(100);
27828c2ecf20Sopenharmony_ci		}
27838c2ecf20Sopenharmony_ci	} else
27848c2ecf20Sopenharmony_ci		udelay(10);
27858c2ecf20Sopenharmony_ci
27868c2ecf20Sopenharmony_ci	/* Reset RISC processor. */
27878c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
27888c2ecf20Sopenharmony_ci
27898c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->semaphore, 0);
27908c2ecf20Sopenharmony_ci
27918c2ecf20Sopenharmony_ci	/* Release RISC processor. */
27928c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
27938c2ecf20Sopenharmony_ci	rd_reg_word(&reg->hccr);			/* PCI Posting. */
27948c2ecf20Sopenharmony_ci
27958c2ecf20Sopenharmony_ci	if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
27968c2ecf20Sopenharmony_ci		for (cnt = 0; cnt < 30000; cnt++) {
27978c2ecf20Sopenharmony_ci			if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
27988c2ecf20Sopenharmony_ci				break;
27998c2ecf20Sopenharmony_ci
28008c2ecf20Sopenharmony_ci			udelay(100);
28018c2ecf20Sopenharmony_ci		}
28028c2ecf20Sopenharmony_ci	} else
28038c2ecf20Sopenharmony_ci		udelay(100);
28048c2ecf20Sopenharmony_ci
28058c2ecf20Sopenharmony_ci	/* Turn on master enable */
28068c2ecf20Sopenharmony_ci	cmd |= PCI_COMMAND_MASTER;
28078c2ecf20Sopenharmony_ci	pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
28088c2ecf20Sopenharmony_ci
28098c2ecf20Sopenharmony_ci	/* Disable RISC pause on FPM parity error. */
28108c2ecf20Sopenharmony_ci	if (!IS_QLA2100(ha)) {
28118c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
28128c2ecf20Sopenharmony_ci		rd_reg_word(&reg->hccr);		/* PCI Posting. */
28138c2ecf20Sopenharmony_ci	}
28148c2ecf20Sopenharmony_ci
28158c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
28168c2ecf20Sopenharmony_ci
28178c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
28188c2ecf20Sopenharmony_ci}
28198c2ecf20Sopenharmony_ci
28208c2ecf20Sopenharmony_ci/**
28218c2ecf20Sopenharmony_ci * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
28228c2ecf20Sopenharmony_ci * @vha: HA context
28238c2ecf20Sopenharmony_ci *
28248c2ecf20Sopenharmony_ci * Returns 0 on success.
28258c2ecf20Sopenharmony_ci */
28268c2ecf20Sopenharmony_cistatic int
28278c2ecf20Sopenharmony_ciqla81xx_reset_mpi(scsi_qla_host_t *vha)
28288c2ecf20Sopenharmony_ci{
28298c2ecf20Sopenharmony_ci	uint16_t mb[4] = {0x1010, 0, 1, 0};
28308c2ecf20Sopenharmony_ci
28318c2ecf20Sopenharmony_ci	if (!IS_QLA81XX(vha->hw))
28328c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
28338c2ecf20Sopenharmony_ci
28348c2ecf20Sopenharmony_ci	return qla81xx_write_mpi_register(vha, mb);
28358c2ecf20Sopenharmony_ci}
28368c2ecf20Sopenharmony_ci
28378c2ecf20Sopenharmony_ci/**
28388c2ecf20Sopenharmony_ci * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
28398c2ecf20Sopenharmony_ci * @vha: HA context
28408c2ecf20Sopenharmony_ci *
28418c2ecf20Sopenharmony_ci * Returns 0 on success.
28428c2ecf20Sopenharmony_ci */
28438c2ecf20Sopenharmony_cistatic inline int
28448c2ecf20Sopenharmony_ciqla24xx_reset_risc(scsi_qla_host_t *vha)
28458c2ecf20Sopenharmony_ci{
28468c2ecf20Sopenharmony_ci	unsigned long flags = 0;
28478c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
28488c2ecf20Sopenharmony_ci	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
28498c2ecf20Sopenharmony_ci	uint32_t cnt;
28508c2ecf20Sopenharmony_ci	uint16_t wd;
28518c2ecf20Sopenharmony_ci	static int abts_cnt; /* ISP abort retry counts */
28528c2ecf20Sopenharmony_ci	int rval = QLA_SUCCESS;
28538c2ecf20Sopenharmony_ci
28548c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
28558c2ecf20Sopenharmony_ci
28568c2ecf20Sopenharmony_ci	/* Reset RISC. */
28578c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
28588c2ecf20Sopenharmony_ci	for (cnt = 0; cnt < 30000; cnt++) {
28598c2ecf20Sopenharmony_ci		if ((rd_reg_dword(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
28608c2ecf20Sopenharmony_ci			break;
28618c2ecf20Sopenharmony_ci
28628c2ecf20Sopenharmony_ci		udelay(10);
28638c2ecf20Sopenharmony_ci	}
28648c2ecf20Sopenharmony_ci
28658c2ecf20Sopenharmony_ci	if (!(rd_reg_dword(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
28668c2ecf20Sopenharmony_ci		set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
28678c2ecf20Sopenharmony_ci
28688c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
28698c2ecf20Sopenharmony_ci	    "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
28708c2ecf20Sopenharmony_ci	    rd_reg_dword(&reg->hccr),
28718c2ecf20Sopenharmony_ci	    rd_reg_dword(&reg->ctrl_status),
28728c2ecf20Sopenharmony_ci	    (rd_reg_dword(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
28738c2ecf20Sopenharmony_ci
28748c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->ctrl_status,
28758c2ecf20Sopenharmony_ci	    CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
28768c2ecf20Sopenharmony_ci	pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
28778c2ecf20Sopenharmony_ci
28788c2ecf20Sopenharmony_ci	udelay(100);
28798c2ecf20Sopenharmony_ci
28808c2ecf20Sopenharmony_ci	/* Wait for firmware to complete NVRAM accesses. */
28818c2ecf20Sopenharmony_ci	rd_reg_word(&reg->mailbox0);
28828c2ecf20Sopenharmony_ci	for (cnt = 10000; rd_reg_word(&reg->mailbox0) != 0 &&
28838c2ecf20Sopenharmony_ci	    rval == QLA_SUCCESS; cnt--) {
28848c2ecf20Sopenharmony_ci		barrier();
28858c2ecf20Sopenharmony_ci		if (cnt)
28868c2ecf20Sopenharmony_ci			udelay(5);
28878c2ecf20Sopenharmony_ci		else
28888c2ecf20Sopenharmony_ci			rval = QLA_FUNCTION_TIMEOUT;
28898c2ecf20Sopenharmony_ci	}
28908c2ecf20Sopenharmony_ci
28918c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS)
28928c2ecf20Sopenharmony_ci		set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
28938c2ecf20Sopenharmony_ci
28948c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
28958c2ecf20Sopenharmony_ci	    "HCCR: 0x%x, MailBox0 Status 0x%x\n",
28968c2ecf20Sopenharmony_ci	    rd_reg_dword(&reg->hccr),
28978c2ecf20Sopenharmony_ci	    rd_reg_word(&reg->mailbox0));
28988c2ecf20Sopenharmony_ci
28998c2ecf20Sopenharmony_ci	/* Wait for soft-reset to complete. */
29008c2ecf20Sopenharmony_ci	rd_reg_dword(&reg->ctrl_status);
29018c2ecf20Sopenharmony_ci	for (cnt = 0; cnt < 60; cnt++) {
29028c2ecf20Sopenharmony_ci		barrier();
29038c2ecf20Sopenharmony_ci		if ((rd_reg_dword(&reg->ctrl_status) &
29048c2ecf20Sopenharmony_ci		    CSRX_ISP_SOFT_RESET) == 0)
29058c2ecf20Sopenharmony_ci			break;
29068c2ecf20Sopenharmony_ci
29078c2ecf20Sopenharmony_ci		udelay(5);
29088c2ecf20Sopenharmony_ci	}
29098c2ecf20Sopenharmony_ci	if (!(rd_reg_dword(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
29108c2ecf20Sopenharmony_ci		set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
29118c2ecf20Sopenharmony_ci
29128c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
29138c2ecf20Sopenharmony_ci	    "HCCR: 0x%x, Soft Reset status: 0x%x\n",
29148c2ecf20Sopenharmony_ci	    rd_reg_dword(&reg->hccr),
29158c2ecf20Sopenharmony_ci	    rd_reg_dword(&reg->ctrl_status));
29168c2ecf20Sopenharmony_ci
29178c2ecf20Sopenharmony_ci	/* If required, do an MPI FW reset now */
29188c2ecf20Sopenharmony_ci	if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
29198c2ecf20Sopenharmony_ci		if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
29208c2ecf20Sopenharmony_ci			if (++abts_cnt < 5) {
29218c2ecf20Sopenharmony_ci				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
29228c2ecf20Sopenharmony_ci				set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
29238c2ecf20Sopenharmony_ci			} else {
29248c2ecf20Sopenharmony_ci				/*
29258c2ecf20Sopenharmony_ci				 * We exhausted the ISP abort retries. We have to
29268c2ecf20Sopenharmony_ci				 * set the board offline.
29278c2ecf20Sopenharmony_ci				 */
29288c2ecf20Sopenharmony_ci				abts_cnt = 0;
29298c2ecf20Sopenharmony_ci				vha->flags.online = 0;
29308c2ecf20Sopenharmony_ci			}
29318c2ecf20Sopenharmony_ci		}
29328c2ecf20Sopenharmony_ci	}
29338c2ecf20Sopenharmony_ci
29348c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->hccr, HCCRX_SET_RISC_RESET);
29358c2ecf20Sopenharmony_ci	rd_reg_dword(&reg->hccr);
29368c2ecf20Sopenharmony_ci
29378c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->hccr, HCCRX_REL_RISC_PAUSE);
29388c2ecf20Sopenharmony_ci	rd_reg_dword(&reg->hccr);
29398c2ecf20Sopenharmony_ci
29408c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->hccr, HCCRX_CLR_RISC_RESET);
29418c2ecf20Sopenharmony_ci	rd_reg_dword(&reg->hccr);
29428c2ecf20Sopenharmony_ci
29438c2ecf20Sopenharmony_ci	rd_reg_word(&reg->mailbox0);
29448c2ecf20Sopenharmony_ci	for (cnt = 60; rd_reg_word(&reg->mailbox0) != 0 &&
29458c2ecf20Sopenharmony_ci	    rval == QLA_SUCCESS; cnt--) {
29468c2ecf20Sopenharmony_ci		barrier();
29478c2ecf20Sopenharmony_ci		if (cnt)
29488c2ecf20Sopenharmony_ci			udelay(5);
29498c2ecf20Sopenharmony_ci		else
29508c2ecf20Sopenharmony_ci			rval = QLA_FUNCTION_TIMEOUT;
29518c2ecf20Sopenharmony_ci	}
29528c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS)
29538c2ecf20Sopenharmony_ci		set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
29548c2ecf20Sopenharmony_ci
29558c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
29568c2ecf20Sopenharmony_ci	    "Host Risc 0x%x, mailbox0 0x%x\n",
29578c2ecf20Sopenharmony_ci	    rd_reg_dword(&reg->hccr),
29588c2ecf20Sopenharmony_ci	     rd_reg_word(&reg->mailbox0));
29598c2ecf20Sopenharmony_ci
29608c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
29618c2ecf20Sopenharmony_ci
29628c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
29638c2ecf20Sopenharmony_ci	    "Driver in %s mode\n",
29648c2ecf20Sopenharmony_ci	    IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
29658c2ecf20Sopenharmony_ci
29668c2ecf20Sopenharmony_ci	if (IS_NOPOLLING_TYPE(ha))
29678c2ecf20Sopenharmony_ci		ha->isp_ops->enable_intrs(ha);
29688c2ecf20Sopenharmony_ci
29698c2ecf20Sopenharmony_ci	return rval;
29708c2ecf20Sopenharmony_ci}
29718c2ecf20Sopenharmony_ci
29728c2ecf20Sopenharmony_cistatic void
29738c2ecf20Sopenharmony_ciqla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
29748c2ecf20Sopenharmony_ci{
29758c2ecf20Sopenharmony_ci	struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
29768c2ecf20Sopenharmony_ci
29778c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
29788c2ecf20Sopenharmony_ci	*data = rd_reg_dword(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFSET);
29798c2ecf20Sopenharmony_ci}
29808c2ecf20Sopenharmony_ci
29818c2ecf20Sopenharmony_cistatic void
29828c2ecf20Sopenharmony_ciqla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
29838c2ecf20Sopenharmony_ci{
29848c2ecf20Sopenharmony_ci	struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
29858c2ecf20Sopenharmony_ci
29868c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
29878c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFSET, data);
29888c2ecf20Sopenharmony_ci}
29898c2ecf20Sopenharmony_ci
29908c2ecf20Sopenharmony_cistatic void
29918c2ecf20Sopenharmony_ciqla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
29928c2ecf20Sopenharmony_ci{
29938c2ecf20Sopenharmony_ci	uint32_t wd32 = 0;
29948c2ecf20Sopenharmony_ci	uint delta_msec = 100;
29958c2ecf20Sopenharmony_ci	uint elapsed_msec = 0;
29968c2ecf20Sopenharmony_ci	uint timeout_msec;
29978c2ecf20Sopenharmony_ci	ulong n;
29988c2ecf20Sopenharmony_ci
29998c2ecf20Sopenharmony_ci	if (vha->hw->pdev->subsystem_device != 0x0175 &&
30008c2ecf20Sopenharmony_ci	    vha->hw->pdev->subsystem_device != 0x0240)
30018c2ecf20Sopenharmony_ci		return;
30028c2ecf20Sopenharmony_ci
30038c2ecf20Sopenharmony_ci	wrt_reg_dword(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
30048c2ecf20Sopenharmony_ci	udelay(100);
30058c2ecf20Sopenharmony_ci
30068c2ecf20Sopenharmony_ciattempt:
30078c2ecf20Sopenharmony_ci	timeout_msec = TIMEOUT_SEMAPHORE;
30088c2ecf20Sopenharmony_ci	n = timeout_msec / delta_msec;
30098c2ecf20Sopenharmony_ci	while (n--) {
30108c2ecf20Sopenharmony_ci		qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
30118c2ecf20Sopenharmony_ci		qla25xx_read_risc_sema_reg(vha, &wd32);
30128c2ecf20Sopenharmony_ci		if (wd32 & RISC_SEMAPHORE)
30138c2ecf20Sopenharmony_ci			break;
30148c2ecf20Sopenharmony_ci		msleep(delta_msec);
30158c2ecf20Sopenharmony_ci		elapsed_msec += delta_msec;
30168c2ecf20Sopenharmony_ci		if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
30178c2ecf20Sopenharmony_ci			goto force;
30188c2ecf20Sopenharmony_ci	}
30198c2ecf20Sopenharmony_ci
30208c2ecf20Sopenharmony_ci	if (!(wd32 & RISC_SEMAPHORE))
30218c2ecf20Sopenharmony_ci		goto force;
30228c2ecf20Sopenharmony_ci
30238c2ecf20Sopenharmony_ci	if (!(wd32 & RISC_SEMAPHORE_FORCE))
30248c2ecf20Sopenharmony_ci		goto acquired;
30258c2ecf20Sopenharmony_ci
30268c2ecf20Sopenharmony_ci	qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
30278c2ecf20Sopenharmony_ci	timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
30288c2ecf20Sopenharmony_ci	n = timeout_msec / delta_msec;
30298c2ecf20Sopenharmony_ci	while (n--) {
30308c2ecf20Sopenharmony_ci		qla25xx_read_risc_sema_reg(vha, &wd32);
30318c2ecf20Sopenharmony_ci		if (!(wd32 & RISC_SEMAPHORE_FORCE))
30328c2ecf20Sopenharmony_ci			break;
30338c2ecf20Sopenharmony_ci		msleep(delta_msec);
30348c2ecf20Sopenharmony_ci		elapsed_msec += delta_msec;
30358c2ecf20Sopenharmony_ci		if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
30368c2ecf20Sopenharmony_ci			goto force;
30378c2ecf20Sopenharmony_ci	}
30388c2ecf20Sopenharmony_ci
30398c2ecf20Sopenharmony_ci	if (wd32 & RISC_SEMAPHORE_FORCE)
30408c2ecf20Sopenharmony_ci		qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
30418c2ecf20Sopenharmony_ci
30428c2ecf20Sopenharmony_ci	goto attempt;
30438c2ecf20Sopenharmony_ci
30448c2ecf20Sopenharmony_ciforce:
30458c2ecf20Sopenharmony_ci	qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
30468c2ecf20Sopenharmony_ci
30478c2ecf20Sopenharmony_ciacquired:
30488c2ecf20Sopenharmony_ci	return;
30498c2ecf20Sopenharmony_ci}
30508c2ecf20Sopenharmony_ci
30518c2ecf20Sopenharmony_ci/**
30528c2ecf20Sopenharmony_ci * qla24xx_reset_chip() - Reset ISP24xx chip.
30538c2ecf20Sopenharmony_ci * @vha: HA context
30548c2ecf20Sopenharmony_ci *
30558c2ecf20Sopenharmony_ci * Returns 0 on success.
30568c2ecf20Sopenharmony_ci */
30578c2ecf20Sopenharmony_ciint
30588c2ecf20Sopenharmony_ciqla24xx_reset_chip(scsi_qla_host_t *vha)
30598c2ecf20Sopenharmony_ci{
30608c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
30618c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
30628c2ecf20Sopenharmony_ci
30638c2ecf20Sopenharmony_ci	if (pci_channel_offline(ha->pdev) &&
30648c2ecf20Sopenharmony_ci	    ha->flags.pci_channel_io_perm_failure) {
30658c2ecf20Sopenharmony_ci		return rval;
30668c2ecf20Sopenharmony_ci	}
30678c2ecf20Sopenharmony_ci
30688c2ecf20Sopenharmony_ci	ha->isp_ops->disable_intrs(ha);
30698c2ecf20Sopenharmony_ci
30708c2ecf20Sopenharmony_ci	qla25xx_manipulate_risc_semaphore(vha);
30718c2ecf20Sopenharmony_ci
30728c2ecf20Sopenharmony_ci	/* Perform RISC reset. */
30738c2ecf20Sopenharmony_ci	rval = qla24xx_reset_risc(vha);
30748c2ecf20Sopenharmony_ci
30758c2ecf20Sopenharmony_ci	return rval;
30768c2ecf20Sopenharmony_ci}
30778c2ecf20Sopenharmony_ci
30788c2ecf20Sopenharmony_ci/**
30798c2ecf20Sopenharmony_ci * qla2x00_chip_diag() - Test chip for proper operation.
30808c2ecf20Sopenharmony_ci * @vha: HA context
30818c2ecf20Sopenharmony_ci *
30828c2ecf20Sopenharmony_ci * Returns 0 on success.
30838c2ecf20Sopenharmony_ci */
30848c2ecf20Sopenharmony_ciint
30858c2ecf20Sopenharmony_ciqla2x00_chip_diag(scsi_qla_host_t *vha)
30868c2ecf20Sopenharmony_ci{
30878c2ecf20Sopenharmony_ci	int		rval;
30888c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
30898c2ecf20Sopenharmony_ci	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
30908c2ecf20Sopenharmony_ci	unsigned long	flags = 0;
30918c2ecf20Sopenharmony_ci	uint16_t	data;
30928c2ecf20Sopenharmony_ci	uint32_t	cnt;
30938c2ecf20Sopenharmony_ci	uint16_t	mb[5];
30948c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
30958c2ecf20Sopenharmony_ci
30968c2ecf20Sopenharmony_ci	/* Assume a failed state */
30978c2ecf20Sopenharmony_ci	rval = QLA_FUNCTION_FAILED;
30988c2ecf20Sopenharmony_ci
30998c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x007b, "Testing device at %p.\n",
31008c2ecf20Sopenharmony_ci	       &reg->flash_address);
31018c2ecf20Sopenharmony_ci
31028c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
31038c2ecf20Sopenharmony_ci
31048c2ecf20Sopenharmony_ci	/* Reset ISP chip. */
31058c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
31068c2ecf20Sopenharmony_ci
31078c2ecf20Sopenharmony_ci	/*
31088c2ecf20Sopenharmony_ci	 * We need to have a delay here since the card will not respond while
31098c2ecf20Sopenharmony_ci	 * in reset causing an MCA on some architectures.
31108c2ecf20Sopenharmony_ci	 */
31118c2ecf20Sopenharmony_ci	udelay(20);
31128c2ecf20Sopenharmony_ci	data = qla2x00_debounce_register(&reg->ctrl_status);
31138c2ecf20Sopenharmony_ci	for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
31148c2ecf20Sopenharmony_ci		udelay(5);
31158c2ecf20Sopenharmony_ci		data = rd_reg_word(&reg->ctrl_status);
31168c2ecf20Sopenharmony_ci		barrier();
31178c2ecf20Sopenharmony_ci	}
31188c2ecf20Sopenharmony_ci
31198c2ecf20Sopenharmony_ci	if (!cnt)
31208c2ecf20Sopenharmony_ci		goto chip_diag_failed;
31218c2ecf20Sopenharmony_ci
31228c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x007c,
31238c2ecf20Sopenharmony_ci	    "Reset register cleared by chip reset.\n");
31248c2ecf20Sopenharmony_ci
31258c2ecf20Sopenharmony_ci	/* Reset RISC processor. */
31268c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
31278c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
31288c2ecf20Sopenharmony_ci
31298c2ecf20Sopenharmony_ci	/* Workaround for QLA2312 PCI parity error */
31308c2ecf20Sopenharmony_ci	if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
31318c2ecf20Sopenharmony_ci		data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
31328c2ecf20Sopenharmony_ci		for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
31338c2ecf20Sopenharmony_ci			udelay(5);
31348c2ecf20Sopenharmony_ci			data = RD_MAILBOX_REG(ha, reg, 0);
31358c2ecf20Sopenharmony_ci			barrier();
31368c2ecf20Sopenharmony_ci		}
31378c2ecf20Sopenharmony_ci	} else
31388c2ecf20Sopenharmony_ci		udelay(10);
31398c2ecf20Sopenharmony_ci
31408c2ecf20Sopenharmony_ci	if (!cnt)
31418c2ecf20Sopenharmony_ci		goto chip_diag_failed;
31428c2ecf20Sopenharmony_ci
31438c2ecf20Sopenharmony_ci	/* Check product ID of chip */
31448c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
31458c2ecf20Sopenharmony_ci
31468c2ecf20Sopenharmony_ci	mb[1] = RD_MAILBOX_REG(ha, reg, 1);
31478c2ecf20Sopenharmony_ci	mb[2] = RD_MAILBOX_REG(ha, reg, 2);
31488c2ecf20Sopenharmony_ci	mb[3] = RD_MAILBOX_REG(ha, reg, 3);
31498c2ecf20Sopenharmony_ci	mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
31508c2ecf20Sopenharmony_ci	if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
31518c2ecf20Sopenharmony_ci	    mb[3] != PROD_ID_3) {
31528c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0062,
31538c2ecf20Sopenharmony_ci		    "Wrong product ID = 0x%x,0x%x,0x%x.\n",
31548c2ecf20Sopenharmony_ci		    mb[1], mb[2], mb[3]);
31558c2ecf20Sopenharmony_ci
31568c2ecf20Sopenharmony_ci		goto chip_diag_failed;
31578c2ecf20Sopenharmony_ci	}
31588c2ecf20Sopenharmony_ci	ha->product_id[0] = mb[1];
31598c2ecf20Sopenharmony_ci	ha->product_id[1] = mb[2];
31608c2ecf20Sopenharmony_ci	ha->product_id[2] = mb[3];
31618c2ecf20Sopenharmony_ci	ha->product_id[3] = mb[4];
31628c2ecf20Sopenharmony_ci
31638c2ecf20Sopenharmony_ci	/* Adjust fw RISC transfer size */
31648c2ecf20Sopenharmony_ci	if (req->length > 1024)
31658c2ecf20Sopenharmony_ci		ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
31668c2ecf20Sopenharmony_ci	else
31678c2ecf20Sopenharmony_ci		ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
31688c2ecf20Sopenharmony_ci		    req->length;
31698c2ecf20Sopenharmony_ci
31708c2ecf20Sopenharmony_ci	if (IS_QLA2200(ha) &&
31718c2ecf20Sopenharmony_ci	    RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
31728c2ecf20Sopenharmony_ci		/* Limit firmware transfer size with a 2200A */
31738c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
31748c2ecf20Sopenharmony_ci
31758c2ecf20Sopenharmony_ci		ha->device_type |= DT_ISP2200A;
31768c2ecf20Sopenharmony_ci		ha->fw_transfer_size = 128;
31778c2ecf20Sopenharmony_ci	}
31788c2ecf20Sopenharmony_ci
31798c2ecf20Sopenharmony_ci	/* Wrap Incoming Mailboxes Test. */
31808c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
31818c2ecf20Sopenharmony_ci
31828c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
31838c2ecf20Sopenharmony_ci	rval = qla2x00_mbx_reg_test(vha);
31848c2ecf20Sopenharmony_ci	if (rval)
31858c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0080,
31868c2ecf20Sopenharmony_ci		    "Failed mailbox send register test.\n");
31878c2ecf20Sopenharmony_ci	else
31888c2ecf20Sopenharmony_ci		/* Flag a successful rval */
31898c2ecf20Sopenharmony_ci		rval = QLA_SUCCESS;
31908c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
31918c2ecf20Sopenharmony_ci
31928c2ecf20Sopenharmony_cichip_diag_failed:
31938c2ecf20Sopenharmony_ci	if (rval)
31948c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x0081,
31958c2ecf20Sopenharmony_ci		    "Chip diagnostics **** FAILED ****.\n");
31968c2ecf20Sopenharmony_ci
31978c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
31988c2ecf20Sopenharmony_ci
31998c2ecf20Sopenharmony_ci	return (rval);
32008c2ecf20Sopenharmony_ci}
32018c2ecf20Sopenharmony_ci
32028c2ecf20Sopenharmony_ci/**
32038c2ecf20Sopenharmony_ci * qla24xx_chip_diag() - Test ISP24xx for proper operation.
32048c2ecf20Sopenharmony_ci * @vha: HA context
32058c2ecf20Sopenharmony_ci *
32068c2ecf20Sopenharmony_ci * Returns 0 on success.
32078c2ecf20Sopenharmony_ci */
32088c2ecf20Sopenharmony_ciint
32098c2ecf20Sopenharmony_ciqla24xx_chip_diag(scsi_qla_host_t *vha)
32108c2ecf20Sopenharmony_ci{
32118c2ecf20Sopenharmony_ci	int rval;
32128c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
32138c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
32148c2ecf20Sopenharmony_ci
32158c2ecf20Sopenharmony_ci	if (IS_P3P_TYPE(ha))
32168c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
32178c2ecf20Sopenharmony_ci
32188c2ecf20Sopenharmony_ci	ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
32198c2ecf20Sopenharmony_ci
32208c2ecf20Sopenharmony_ci	rval = qla2x00_mbx_reg_test(vha);
32218c2ecf20Sopenharmony_ci	if (rval) {
32228c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0082,
32238c2ecf20Sopenharmony_ci		    "Failed mailbox send register test.\n");
32248c2ecf20Sopenharmony_ci	} else {
32258c2ecf20Sopenharmony_ci		/* Flag a successful rval */
32268c2ecf20Sopenharmony_ci		rval = QLA_SUCCESS;
32278c2ecf20Sopenharmony_ci	}
32288c2ecf20Sopenharmony_ci
32298c2ecf20Sopenharmony_ci	return rval;
32308c2ecf20Sopenharmony_ci}
32318c2ecf20Sopenharmony_ci
32328c2ecf20Sopenharmony_cistatic void
32338c2ecf20Sopenharmony_ciqla2x00_init_fce_trace(scsi_qla_host_t *vha)
32348c2ecf20Sopenharmony_ci{
32358c2ecf20Sopenharmony_ci	int rval;
32368c2ecf20Sopenharmony_ci	dma_addr_t tc_dma;
32378c2ecf20Sopenharmony_ci	void *tc;
32388c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
32398c2ecf20Sopenharmony_ci
32408c2ecf20Sopenharmony_ci	if (!IS_FWI2_CAPABLE(ha))
32418c2ecf20Sopenharmony_ci		return;
32428c2ecf20Sopenharmony_ci
32438c2ecf20Sopenharmony_ci	if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
32448c2ecf20Sopenharmony_ci	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
32458c2ecf20Sopenharmony_ci		return;
32468c2ecf20Sopenharmony_ci
32478c2ecf20Sopenharmony_ci	if (ha->fce) {
32488c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x00bd,
32498c2ecf20Sopenharmony_ci		       "%s: FCE Mem is already allocated.\n",
32508c2ecf20Sopenharmony_ci		       __func__);
32518c2ecf20Sopenharmony_ci		return;
32528c2ecf20Sopenharmony_ci	}
32538c2ecf20Sopenharmony_ci
32548c2ecf20Sopenharmony_ci	/* Allocate memory for Fibre Channel Event Buffer. */
32558c2ecf20Sopenharmony_ci	tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
32568c2ecf20Sopenharmony_ci				GFP_KERNEL);
32578c2ecf20Sopenharmony_ci	if (!tc) {
32588c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x00be,
32598c2ecf20Sopenharmony_ci		       "Unable to allocate (%d KB) for FCE.\n",
32608c2ecf20Sopenharmony_ci		       FCE_SIZE / 1024);
32618c2ecf20Sopenharmony_ci		return;
32628c2ecf20Sopenharmony_ci	}
32638c2ecf20Sopenharmony_ci
32648c2ecf20Sopenharmony_ci	rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
32658c2ecf20Sopenharmony_ci					ha->fce_mb, &ha->fce_bufs);
32668c2ecf20Sopenharmony_ci	if (rval) {
32678c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x00bf,
32688c2ecf20Sopenharmony_ci		       "Unable to initialize FCE (%d).\n", rval);
32698c2ecf20Sopenharmony_ci		dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc, tc_dma);
32708c2ecf20Sopenharmony_ci		return;
32718c2ecf20Sopenharmony_ci	}
32728c2ecf20Sopenharmony_ci
32738c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x00c0,
32748c2ecf20Sopenharmony_ci	       "Allocated (%d KB) for FCE...\n", FCE_SIZE / 1024);
32758c2ecf20Sopenharmony_ci
32768c2ecf20Sopenharmony_ci	ha->flags.fce_enabled = 1;
32778c2ecf20Sopenharmony_ci	ha->fce_dma = tc_dma;
32788c2ecf20Sopenharmony_ci	ha->fce = tc;
32798c2ecf20Sopenharmony_ci}
32808c2ecf20Sopenharmony_ci
32818c2ecf20Sopenharmony_cistatic void
32828c2ecf20Sopenharmony_ciqla2x00_init_eft_trace(scsi_qla_host_t *vha)
32838c2ecf20Sopenharmony_ci{
32848c2ecf20Sopenharmony_ci	int rval;
32858c2ecf20Sopenharmony_ci	dma_addr_t tc_dma;
32868c2ecf20Sopenharmony_ci	void *tc;
32878c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
32888c2ecf20Sopenharmony_ci
32898c2ecf20Sopenharmony_ci	if (!IS_FWI2_CAPABLE(ha))
32908c2ecf20Sopenharmony_ci		return;
32918c2ecf20Sopenharmony_ci
32928c2ecf20Sopenharmony_ci	if (ha->eft) {
32938c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x00bd,
32948c2ecf20Sopenharmony_ci		    "%s: EFT Mem is already allocated.\n",
32958c2ecf20Sopenharmony_ci		    __func__);
32968c2ecf20Sopenharmony_ci		return;
32978c2ecf20Sopenharmony_ci	}
32988c2ecf20Sopenharmony_ci
32998c2ecf20Sopenharmony_ci	/* Allocate memory for Extended Trace Buffer. */
33008c2ecf20Sopenharmony_ci	tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
33018c2ecf20Sopenharmony_ci				GFP_KERNEL);
33028c2ecf20Sopenharmony_ci	if (!tc) {
33038c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x00c1,
33048c2ecf20Sopenharmony_ci		       "Unable to allocate (%d KB) for EFT.\n",
33058c2ecf20Sopenharmony_ci		       EFT_SIZE / 1024);
33068c2ecf20Sopenharmony_ci		return;
33078c2ecf20Sopenharmony_ci	}
33088c2ecf20Sopenharmony_ci
33098c2ecf20Sopenharmony_ci	rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
33108c2ecf20Sopenharmony_ci	if (rval) {
33118c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x00c2,
33128c2ecf20Sopenharmony_ci		       "Unable to initialize EFT (%d).\n", rval);
33138c2ecf20Sopenharmony_ci		dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc, tc_dma);
33148c2ecf20Sopenharmony_ci		return;
33158c2ecf20Sopenharmony_ci	}
33168c2ecf20Sopenharmony_ci
33178c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x00c3,
33188c2ecf20Sopenharmony_ci	       "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
33198c2ecf20Sopenharmony_ci
33208c2ecf20Sopenharmony_ci	ha->eft_dma = tc_dma;
33218c2ecf20Sopenharmony_ci	ha->eft = tc;
33228c2ecf20Sopenharmony_ci}
33238c2ecf20Sopenharmony_ci
33248c2ecf20Sopenharmony_cistatic void
33258c2ecf20Sopenharmony_ciqla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
33268c2ecf20Sopenharmony_ci{
33278c2ecf20Sopenharmony_ci	qla2x00_init_fce_trace(vha);
33288c2ecf20Sopenharmony_ci	qla2x00_init_eft_trace(vha);
33298c2ecf20Sopenharmony_ci}
33308c2ecf20Sopenharmony_ci
33318c2ecf20Sopenharmony_civoid
33328c2ecf20Sopenharmony_ciqla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
33338c2ecf20Sopenharmony_ci{
33348c2ecf20Sopenharmony_ci	uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
33358c2ecf20Sopenharmony_ci	    eft_size, fce_size, mq_size;
33368c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
33378c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
33388c2ecf20Sopenharmony_ci	struct rsp_que *rsp = ha->rsp_q_map[0];
33398c2ecf20Sopenharmony_ci	struct qla2xxx_fw_dump *fw_dump;
33408c2ecf20Sopenharmony_ci
33418c2ecf20Sopenharmony_ci	if (ha->fw_dump) {
33428c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x00bd,
33438c2ecf20Sopenharmony_ci		    "Firmware dump already allocated.\n");
33448c2ecf20Sopenharmony_ci		return;
33458c2ecf20Sopenharmony_ci	}
33468c2ecf20Sopenharmony_ci
33478c2ecf20Sopenharmony_ci	ha->fw_dumped = 0;
33488c2ecf20Sopenharmony_ci	ha->fw_dump_cap_flags = 0;
33498c2ecf20Sopenharmony_ci	dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
33508c2ecf20Sopenharmony_ci	req_q_size = rsp_q_size = 0;
33518c2ecf20Sopenharmony_ci
33528c2ecf20Sopenharmony_ci	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
33538c2ecf20Sopenharmony_ci		fixed_size = sizeof(struct qla2100_fw_dump);
33548c2ecf20Sopenharmony_ci	} else if (IS_QLA23XX(ha)) {
33558c2ecf20Sopenharmony_ci		fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
33568c2ecf20Sopenharmony_ci		mem_size = (ha->fw_memory_size - 0x11000 + 1) *
33578c2ecf20Sopenharmony_ci		    sizeof(uint16_t);
33588c2ecf20Sopenharmony_ci	} else if (IS_FWI2_CAPABLE(ha)) {
33598c2ecf20Sopenharmony_ci		if (IS_QLA83XX(ha))
33608c2ecf20Sopenharmony_ci			fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
33618c2ecf20Sopenharmony_ci		else if (IS_QLA81XX(ha))
33628c2ecf20Sopenharmony_ci			fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
33638c2ecf20Sopenharmony_ci		else if (IS_QLA25XX(ha))
33648c2ecf20Sopenharmony_ci			fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
33658c2ecf20Sopenharmony_ci		else
33668c2ecf20Sopenharmony_ci			fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
33678c2ecf20Sopenharmony_ci
33688c2ecf20Sopenharmony_ci		mem_size = (ha->fw_memory_size - 0x100000 + 1) *
33698c2ecf20Sopenharmony_ci		    sizeof(uint32_t);
33708c2ecf20Sopenharmony_ci		if (ha->mqenable) {
33718c2ecf20Sopenharmony_ci			if (!IS_QLA83XX(ha))
33728c2ecf20Sopenharmony_ci				mq_size = sizeof(struct qla2xxx_mq_chain);
33738c2ecf20Sopenharmony_ci			/*
33748c2ecf20Sopenharmony_ci			 * Allocate maximum buffer size for all queues - Q0.
33758c2ecf20Sopenharmony_ci			 * Resizing must be done at end-of-dump processing.
33768c2ecf20Sopenharmony_ci			 */
33778c2ecf20Sopenharmony_ci			mq_size += (ha->max_req_queues - 1) *
33788c2ecf20Sopenharmony_ci			    (req->length * sizeof(request_t));
33798c2ecf20Sopenharmony_ci			mq_size += (ha->max_rsp_queues - 1) *
33808c2ecf20Sopenharmony_ci			    (rsp->length * sizeof(response_t));
33818c2ecf20Sopenharmony_ci		}
33828c2ecf20Sopenharmony_ci		if (ha->tgt.atio_ring)
33838c2ecf20Sopenharmony_ci			mq_size += ha->tgt.atio_q_length * sizeof(request_t);
33848c2ecf20Sopenharmony_ci
33858c2ecf20Sopenharmony_ci		qla2x00_init_fce_trace(vha);
33868c2ecf20Sopenharmony_ci		if (ha->fce)
33878c2ecf20Sopenharmony_ci			fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
33888c2ecf20Sopenharmony_ci		qla2x00_init_eft_trace(vha);
33898c2ecf20Sopenharmony_ci		if (ha->eft)
33908c2ecf20Sopenharmony_ci			eft_size = EFT_SIZE;
33918c2ecf20Sopenharmony_ci	}
33928c2ecf20Sopenharmony_ci
33938c2ecf20Sopenharmony_ci	if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
33948c2ecf20Sopenharmony_ci		struct fwdt *fwdt = ha->fwdt;
33958c2ecf20Sopenharmony_ci		uint j;
33968c2ecf20Sopenharmony_ci
33978c2ecf20Sopenharmony_ci		for (j = 0; j < 2; j++, fwdt++) {
33988c2ecf20Sopenharmony_ci			if (!fwdt->template) {
33998c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_init, vha, 0x00ba,
34008c2ecf20Sopenharmony_ci				    "-> fwdt%u no template\n", j);
34018c2ecf20Sopenharmony_ci				continue;
34028c2ecf20Sopenharmony_ci			}
34038c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x00fa,
34048c2ecf20Sopenharmony_ci			    "-> fwdt%u calculating fwdump size...\n", j);
34058c2ecf20Sopenharmony_ci			fwdt->dump_size = qla27xx_fwdt_calculate_dump_size(
34068c2ecf20Sopenharmony_ci			    vha, fwdt->template);
34078c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x00fa,
34088c2ecf20Sopenharmony_ci			    "-> fwdt%u calculated fwdump size = %#lx bytes\n",
34098c2ecf20Sopenharmony_ci			    j, fwdt->dump_size);
34108c2ecf20Sopenharmony_ci			dump_size += fwdt->dump_size;
34118c2ecf20Sopenharmony_ci		}
34128c2ecf20Sopenharmony_ci		/* Add space for spare MPI fw dump. */
34138c2ecf20Sopenharmony_ci		dump_size += ha->fwdt[1].dump_size;
34148c2ecf20Sopenharmony_ci	} else {
34158c2ecf20Sopenharmony_ci		req_q_size = req->length * sizeof(request_t);
34168c2ecf20Sopenharmony_ci		rsp_q_size = rsp->length * sizeof(response_t);
34178c2ecf20Sopenharmony_ci		dump_size = offsetof(struct qla2xxx_fw_dump, isp);
34188c2ecf20Sopenharmony_ci		dump_size += fixed_size + mem_size + req_q_size + rsp_q_size
34198c2ecf20Sopenharmony_ci			+ eft_size;
34208c2ecf20Sopenharmony_ci		ha->chain_offset = dump_size;
34218c2ecf20Sopenharmony_ci		dump_size += mq_size + fce_size;
34228c2ecf20Sopenharmony_ci		if (ha->exchoffld_buf)
34238c2ecf20Sopenharmony_ci			dump_size += sizeof(struct qla2xxx_offld_chain) +
34248c2ecf20Sopenharmony_ci				ha->exchoffld_size;
34258c2ecf20Sopenharmony_ci		if (ha->exlogin_buf)
34268c2ecf20Sopenharmony_ci			dump_size += sizeof(struct qla2xxx_offld_chain) +
34278c2ecf20Sopenharmony_ci				ha->exlogin_size;
34288c2ecf20Sopenharmony_ci	}
34298c2ecf20Sopenharmony_ci
34308c2ecf20Sopenharmony_ci	if (!ha->fw_dump_len || dump_size > ha->fw_dump_alloc_len) {
34318c2ecf20Sopenharmony_ci
34328c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x00c5,
34338c2ecf20Sopenharmony_ci		    "%s dump_size %d fw_dump_len %d fw_dump_alloc_len %d\n",
34348c2ecf20Sopenharmony_ci		    __func__, dump_size, ha->fw_dump_len,
34358c2ecf20Sopenharmony_ci		    ha->fw_dump_alloc_len);
34368c2ecf20Sopenharmony_ci
34378c2ecf20Sopenharmony_ci		fw_dump = vmalloc(dump_size);
34388c2ecf20Sopenharmony_ci		if (!fw_dump) {
34398c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x00c4,
34408c2ecf20Sopenharmony_ci			    "Unable to allocate (%d KB) for firmware dump.\n",
34418c2ecf20Sopenharmony_ci			    dump_size / 1024);
34428c2ecf20Sopenharmony_ci		} else {
34438c2ecf20Sopenharmony_ci			mutex_lock(&ha->optrom_mutex);
34448c2ecf20Sopenharmony_ci			if (ha->fw_dumped) {
34458c2ecf20Sopenharmony_ci				memcpy(fw_dump, ha->fw_dump, ha->fw_dump_len);
34468c2ecf20Sopenharmony_ci				vfree(ha->fw_dump);
34478c2ecf20Sopenharmony_ci				ha->fw_dump = fw_dump;
34488c2ecf20Sopenharmony_ci				ha->fw_dump_alloc_len =  dump_size;
34498c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_init, vha, 0x00c5,
34508c2ecf20Sopenharmony_ci				    "Re-Allocated (%d KB) and save firmware dump.\n",
34518c2ecf20Sopenharmony_ci				    dump_size / 1024);
34528c2ecf20Sopenharmony_ci			} else {
34538c2ecf20Sopenharmony_ci				if (ha->fw_dump)
34548c2ecf20Sopenharmony_ci					vfree(ha->fw_dump);
34558c2ecf20Sopenharmony_ci				ha->fw_dump = fw_dump;
34568c2ecf20Sopenharmony_ci
34578c2ecf20Sopenharmony_ci				ha->fw_dump_len = ha->fw_dump_alloc_len =
34588c2ecf20Sopenharmony_ci				    dump_size;
34598c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_init, vha, 0x00c5,
34608c2ecf20Sopenharmony_ci				    "Allocated (%d KB) for firmware dump.\n",
34618c2ecf20Sopenharmony_ci				    dump_size / 1024);
34628c2ecf20Sopenharmony_ci
34638c2ecf20Sopenharmony_ci				if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
34648c2ecf20Sopenharmony_ci					ha->mpi_fw_dump = (char *)fw_dump +
34658c2ecf20Sopenharmony_ci						ha->fwdt[1].dump_size;
34668c2ecf20Sopenharmony_ci					mutex_unlock(&ha->optrom_mutex);
34678c2ecf20Sopenharmony_ci					return;
34688c2ecf20Sopenharmony_ci				}
34698c2ecf20Sopenharmony_ci
34708c2ecf20Sopenharmony_ci				ha->fw_dump->signature[0] = 'Q';
34718c2ecf20Sopenharmony_ci				ha->fw_dump->signature[1] = 'L';
34728c2ecf20Sopenharmony_ci				ha->fw_dump->signature[2] = 'G';
34738c2ecf20Sopenharmony_ci				ha->fw_dump->signature[3] = 'C';
34748c2ecf20Sopenharmony_ci				ha->fw_dump->version = htonl(1);
34758c2ecf20Sopenharmony_ci
34768c2ecf20Sopenharmony_ci				ha->fw_dump->fixed_size = htonl(fixed_size);
34778c2ecf20Sopenharmony_ci				ha->fw_dump->mem_size = htonl(mem_size);
34788c2ecf20Sopenharmony_ci				ha->fw_dump->req_q_size = htonl(req_q_size);
34798c2ecf20Sopenharmony_ci				ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
34808c2ecf20Sopenharmony_ci
34818c2ecf20Sopenharmony_ci				ha->fw_dump->eft_size = htonl(eft_size);
34828c2ecf20Sopenharmony_ci				ha->fw_dump->eft_addr_l =
34838c2ecf20Sopenharmony_ci				    htonl(LSD(ha->eft_dma));
34848c2ecf20Sopenharmony_ci				ha->fw_dump->eft_addr_h =
34858c2ecf20Sopenharmony_ci				    htonl(MSD(ha->eft_dma));
34868c2ecf20Sopenharmony_ci
34878c2ecf20Sopenharmony_ci				ha->fw_dump->header_size =
34888c2ecf20Sopenharmony_ci					htonl(offsetof
34898c2ecf20Sopenharmony_ci					    (struct qla2xxx_fw_dump, isp));
34908c2ecf20Sopenharmony_ci			}
34918c2ecf20Sopenharmony_ci			mutex_unlock(&ha->optrom_mutex);
34928c2ecf20Sopenharmony_ci		}
34938c2ecf20Sopenharmony_ci	}
34948c2ecf20Sopenharmony_ci}
34958c2ecf20Sopenharmony_ci
34968c2ecf20Sopenharmony_cistatic int
34978c2ecf20Sopenharmony_ciqla81xx_mpi_sync(scsi_qla_host_t *vha)
34988c2ecf20Sopenharmony_ci{
34998c2ecf20Sopenharmony_ci#define MPS_MASK	0xe0
35008c2ecf20Sopenharmony_ci	int rval;
35018c2ecf20Sopenharmony_ci	uint16_t dc;
35028c2ecf20Sopenharmony_ci	uint32_t dw;
35038c2ecf20Sopenharmony_ci
35048c2ecf20Sopenharmony_ci	if (!IS_QLA81XX(vha->hw))
35058c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
35068c2ecf20Sopenharmony_ci
35078c2ecf20Sopenharmony_ci	rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
35088c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
35098c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0105,
35108c2ecf20Sopenharmony_ci		    "Unable to acquire semaphore.\n");
35118c2ecf20Sopenharmony_ci		goto done;
35128c2ecf20Sopenharmony_ci	}
35138c2ecf20Sopenharmony_ci
35148c2ecf20Sopenharmony_ci	pci_read_config_word(vha->hw->pdev, 0x54, &dc);
35158c2ecf20Sopenharmony_ci	rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
35168c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
35178c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
35188c2ecf20Sopenharmony_ci		goto done_release;
35198c2ecf20Sopenharmony_ci	}
35208c2ecf20Sopenharmony_ci
35218c2ecf20Sopenharmony_ci	dc &= MPS_MASK;
35228c2ecf20Sopenharmony_ci	if (dc == (dw & MPS_MASK))
35238c2ecf20Sopenharmony_ci		goto done_release;
35248c2ecf20Sopenharmony_ci
35258c2ecf20Sopenharmony_ci	dw &= ~MPS_MASK;
35268c2ecf20Sopenharmony_ci	dw |= dc;
35278c2ecf20Sopenharmony_ci	rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
35288c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
35298c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
35308c2ecf20Sopenharmony_ci	}
35318c2ecf20Sopenharmony_ci
35328c2ecf20Sopenharmony_cidone_release:
35338c2ecf20Sopenharmony_ci	rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
35348c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
35358c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x006d,
35368c2ecf20Sopenharmony_ci		    "Unable to release semaphore.\n");
35378c2ecf20Sopenharmony_ci	}
35388c2ecf20Sopenharmony_ci
35398c2ecf20Sopenharmony_cidone:
35408c2ecf20Sopenharmony_ci	return rval;
35418c2ecf20Sopenharmony_ci}
35428c2ecf20Sopenharmony_ci
35438c2ecf20Sopenharmony_ciint
35448c2ecf20Sopenharmony_ciqla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
35458c2ecf20Sopenharmony_ci{
35468c2ecf20Sopenharmony_ci	/* Don't try to reallocate the array */
35478c2ecf20Sopenharmony_ci	if (req->outstanding_cmds)
35488c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
35498c2ecf20Sopenharmony_ci
35508c2ecf20Sopenharmony_ci	if (!IS_FWI2_CAPABLE(ha))
35518c2ecf20Sopenharmony_ci		req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
35528c2ecf20Sopenharmony_ci	else {
35538c2ecf20Sopenharmony_ci		if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
35548c2ecf20Sopenharmony_ci			req->num_outstanding_cmds = ha->cur_fw_xcb_count;
35558c2ecf20Sopenharmony_ci		else
35568c2ecf20Sopenharmony_ci			req->num_outstanding_cmds = ha->cur_fw_iocb_count;
35578c2ecf20Sopenharmony_ci	}
35588c2ecf20Sopenharmony_ci
35598c2ecf20Sopenharmony_ci	req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
35608c2ecf20Sopenharmony_ci					sizeof(srb_t *),
35618c2ecf20Sopenharmony_ci					GFP_KERNEL);
35628c2ecf20Sopenharmony_ci
35638c2ecf20Sopenharmony_ci	if (!req->outstanding_cmds) {
35648c2ecf20Sopenharmony_ci		/*
35658c2ecf20Sopenharmony_ci		 * Try to allocate a minimal size just so we can get through
35668c2ecf20Sopenharmony_ci		 * initialization.
35678c2ecf20Sopenharmony_ci		 */
35688c2ecf20Sopenharmony_ci		req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
35698c2ecf20Sopenharmony_ci		req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
35708c2ecf20Sopenharmony_ci						sizeof(srb_t *),
35718c2ecf20Sopenharmony_ci						GFP_KERNEL);
35728c2ecf20Sopenharmony_ci
35738c2ecf20Sopenharmony_ci		if (!req->outstanding_cmds) {
35748c2ecf20Sopenharmony_ci			ql_log(ql_log_fatal, NULL, 0x0126,
35758c2ecf20Sopenharmony_ci			    "Failed to allocate memory for "
35768c2ecf20Sopenharmony_ci			    "outstanding_cmds for req_que %p.\n", req);
35778c2ecf20Sopenharmony_ci			req->num_outstanding_cmds = 0;
35788c2ecf20Sopenharmony_ci			return QLA_FUNCTION_FAILED;
35798c2ecf20Sopenharmony_ci		}
35808c2ecf20Sopenharmony_ci	}
35818c2ecf20Sopenharmony_ci
35828c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
35838c2ecf20Sopenharmony_ci}
35848c2ecf20Sopenharmony_ci
35858c2ecf20Sopenharmony_ci#define PRINT_FIELD(_field, _flag, _str) {		\
35868c2ecf20Sopenharmony_ci	if (a0->_field & _flag) {\
35878c2ecf20Sopenharmony_ci		if (p) {\
35888c2ecf20Sopenharmony_ci			strcat(ptr, "|");\
35898c2ecf20Sopenharmony_ci			ptr++;\
35908c2ecf20Sopenharmony_ci			leftover--;\
35918c2ecf20Sopenharmony_ci		} \
35928c2ecf20Sopenharmony_ci		len = snprintf(ptr, leftover, "%s", _str);	\
35938c2ecf20Sopenharmony_ci		p = 1;\
35948c2ecf20Sopenharmony_ci		leftover -= len;\
35958c2ecf20Sopenharmony_ci		ptr += len; \
35968c2ecf20Sopenharmony_ci	} \
35978c2ecf20Sopenharmony_ci}
35988c2ecf20Sopenharmony_ci
35998c2ecf20Sopenharmony_cistatic void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
36008c2ecf20Sopenharmony_ci{
36018c2ecf20Sopenharmony_ci#define STR_LEN 64
36028c2ecf20Sopenharmony_ci	struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
36038c2ecf20Sopenharmony_ci	u8 str[STR_LEN], *ptr, p;
36048c2ecf20Sopenharmony_ci	int leftover, len;
36058c2ecf20Sopenharmony_ci
36068c2ecf20Sopenharmony_ci	memset(str, 0, STR_LEN);
36078c2ecf20Sopenharmony_ci	snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
36088c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x015a,
36098c2ecf20Sopenharmony_ci	    "SFP MFG Name: %s\n", str);
36108c2ecf20Sopenharmony_ci
36118c2ecf20Sopenharmony_ci	memset(str, 0, STR_LEN);
36128c2ecf20Sopenharmony_ci	snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
36138c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x015c,
36148c2ecf20Sopenharmony_ci	    "SFP Part Name: %s\n", str);
36158c2ecf20Sopenharmony_ci
36168c2ecf20Sopenharmony_ci	/* media */
36178c2ecf20Sopenharmony_ci	memset(str, 0, STR_LEN);
36188c2ecf20Sopenharmony_ci	ptr = str;
36198c2ecf20Sopenharmony_ci	leftover = STR_LEN;
36208c2ecf20Sopenharmony_ci	p = len = 0;
36218c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
36228c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
36238c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
36248c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
36258c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
36268c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
36278c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
36288c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x0160,
36298c2ecf20Sopenharmony_ci	    "SFP Media: %s\n", str);
36308c2ecf20Sopenharmony_ci
36318c2ecf20Sopenharmony_ci	/* link length */
36328c2ecf20Sopenharmony_ci	memset(str, 0, STR_LEN);
36338c2ecf20Sopenharmony_ci	ptr = str;
36348c2ecf20Sopenharmony_ci	leftover = STR_LEN;
36358c2ecf20Sopenharmony_ci	p = len = 0;
36368c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
36378c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
36388c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
36398c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
36408c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
36418c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x0196,
36428c2ecf20Sopenharmony_ci	    "SFP Link Length: %s\n", str);
36438c2ecf20Sopenharmony_ci
36448c2ecf20Sopenharmony_ci	memset(str, 0, STR_LEN);
36458c2ecf20Sopenharmony_ci	ptr = str;
36468c2ecf20Sopenharmony_ci	leftover = STR_LEN;
36478c2ecf20Sopenharmony_ci	p = len = 0;
36488c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
36498c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
36508c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
36518c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
36528c2ecf20Sopenharmony_ci	PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
36538c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x016e,
36548c2ecf20Sopenharmony_ci	    "SFP FC Link Tech: %s\n", str);
36558c2ecf20Sopenharmony_ci
36568c2ecf20Sopenharmony_ci	if (a0->length_km)
36578c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x016f,
36588c2ecf20Sopenharmony_ci		    "SFP Distant: %d km\n", a0->length_km);
36598c2ecf20Sopenharmony_ci	if (a0->length_100m)
36608c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0170,
36618c2ecf20Sopenharmony_ci		    "SFP Distant: %d m\n", a0->length_100m*100);
36628c2ecf20Sopenharmony_ci	if (a0->length_50um_10m)
36638c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0189,
36648c2ecf20Sopenharmony_ci		    "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
36658c2ecf20Sopenharmony_ci	if (a0->length_62um_10m)
36668c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018a,
36678c2ecf20Sopenharmony_ci		  "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
36688c2ecf20Sopenharmony_ci	if (a0->length_om4_10m)
36698c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0194,
36708c2ecf20Sopenharmony_ci		    "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
36718c2ecf20Sopenharmony_ci	if (a0->length_om3_10m)
36728c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0195,
36738c2ecf20Sopenharmony_ci		    "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
36748c2ecf20Sopenharmony_ci}
36758c2ecf20Sopenharmony_ci
36768c2ecf20Sopenharmony_ci
36778c2ecf20Sopenharmony_ci/**
36788c2ecf20Sopenharmony_ci * qla24xx_detect_sfp()
36798c2ecf20Sopenharmony_ci *
36808c2ecf20Sopenharmony_ci * @vha: adapter state pointer.
36818c2ecf20Sopenharmony_ci *
36828c2ecf20Sopenharmony_ci * @return
36838c2ecf20Sopenharmony_ci *	0 -- Configure firmware to use short-range settings -- normal
36848c2ecf20Sopenharmony_ci *	     buffer-to-buffer credits.
36858c2ecf20Sopenharmony_ci *
36868c2ecf20Sopenharmony_ci *	1 -- Configure firmware to use long-range settings -- extra
36878c2ecf20Sopenharmony_ci *	     buffer-to-buffer credits should be allocated with
36888c2ecf20Sopenharmony_ci *	     ha->lr_distance containing distance settings from NVRAM or SFP
36898c2ecf20Sopenharmony_ci *	     (if supported).
36908c2ecf20Sopenharmony_ci */
36918c2ecf20Sopenharmony_ciint
36928c2ecf20Sopenharmony_ciqla24xx_detect_sfp(scsi_qla_host_t *vha)
36938c2ecf20Sopenharmony_ci{
36948c2ecf20Sopenharmony_ci	int rc, used_nvram;
36958c2ecf20Sopenharmony_ci	struct sff_8247_a0 *a;
36968c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
36978c2ecf20Sopenharmony_ci	struct nvram_81xx *nv = ha->nvram;
36988c2ecf20Sopenharmony_ci#define LR_DISTANCE_UNKNOWN	2
36998c2ecf20Sopenharmony_ci	static const char * const types[] = { "Short", "Long" };
37008c2ecf20Sopenharmony_ci	static const char * const lengths[] = { "(10km)", "(5km)", "" };
37018c2ecf20Sopenharmony_ci	u8 ll = 0;
37028c2ecf20Sopenharmony_ci
37038c2ecf20Sopenharmony_ci	/* Seed with NVRAM settings. */
37048c2ecf20Sopenharmony_ci	used_nvram = 0;
37058c2ecf20Sopenharmony_ci	ha->flags.lr_detected = 0;
37068c2ecf20Sopenharmony_ci	if (IS_BPM_RANGE_CAPABLE(ha) &&
37078c2ecf20Sopenharmony_ci	    (nv->enhanced_features & NEF_LR_DIST_ENABLE)) {
37088c2ecf20Sopenharmony_ci		used_nvram = 1;
37098c2ecf20Sopenharmony_ci		ha->flags.lr_detected = 1;
37108c2ecf20Sopenharmony_ci		ha->lr_distance =
37118c2ecf20Sopenharmony_ci		    (nv->enhanced_features >> LR_DIST_NV_POS)
37128c2ecf20Sopenharmony_ci		     & LR_DIST_NV_MASK;
37138c2ecf20Sopenharmony_ci	}
37148c2ecf20Sopenharmony_ci
37158c2ecf20Sopenharmony_ci	if (!IS_BPM_ENABLED(vha))
37168c2ecf20Sopenharmony_ci		goto out;
37178c2ecf20Sopenharmony_ci	/* Determine SR/LR capabilities of SFP/Transceiver. */
37188c2ecf20Sopenharmony_ci	rc = qla2x00_read_sfp_dev(vha, NULL, 0);
37198c2ecf20Sopenharmony_ci	if (rc)
37208c2ecf20Sopenharmony_ci		goto out;
37218c2ecf20Sopenharmony_ci
37228c2ecf20Sopenharmony_ci	used_nvram = 0;
37238c2ecf20Sopenharmony_ci	a = (struct sff_8247_a0 *)vha->hw->sfp_data;
37248c2ecf20Sopenharmony_ci	qla2xxx_print_sfp_info(vha);
37258c2ecf20Sopenharmony_ci
37268c2ecf20Sopenharmony_ci	ha->flags.lr_detected = 0;
37278c2ecf20Sopenharmony_ci	ll = a->fc_ll_cc7;
37288c2ecf20Sopenharmony_ci	if (ll & FC_LL_VL || ll & FC_LL_L) {
37298c2ecf20Sopenharmony_ci		/* Long range, track length. */
37308c2ecf20Sopenharmony_ci		ha->flags.lr_detected = 1;
37318c2ecf20Sopenharmony_ci
37328c2ecf20Sopenharmony_ci		if (a->length_km > 5 || a->length_100m > 50)
37338c2ecf20Sopenharmony_ci			ha->lr_distance = LR_DISTANCE_10K;
37348c2ecf20Sopenharmony_ci		else
37358c2ecf20Sopenharmony_ci			ha->lr_distance = LR_DISTANCE_5K;
37368c2ecf20Sopenharmony_ci	}
37378c2ecf20Sopenharmony_ci
37388c2ecf20Sopenharmony_ciout:
37398c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_async, vha, 0x507b,
37408c2ecf20Sopenharmony_ci	    "SFP detect: %s-Range SFP %s (nvr=%x ll=%x lr=%x lrd=%x).\n",
37418c2ecf20Sopenharmony_ci	    types[ha->flags.lr_detected],
37428c2ecf20Sopenharmony_ci	    ha->flags.lr_detected ? lengths[ha->lr_distance] :
37438c2ecf20Sopenharmony_ci	       lengths[LR_DISTANCE_UNKNOWN],
37448c2ecf20Sopenharmony_ci	    used_nvram, ll, ha->flags.lr_detected, ha->lr_distance);
37458c2ecf20Sopenharmony_ci	return ha->flags.lr_detected;
37468c2ecf20Sopenharmony_ci}
37478c2ecf20Sopenharmony_ci
37488c2ecf20Sopenharmony_civoid qla_init_iocb_limit(scsi_qla_host_t *vha)
37498c2ecf20Sopenharmony_ci{
37508c2ecf20Sopenharmony_ci	u16 i, num_qps;
37518c2ecf20Sopenharmony_ci	u32 limit;
37528c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
37538c2ecf20Sopenharmony_ci
37548c2ecf20Sopenharmony_ci	num_qps = ha->num_qpairs + 1;
37558c2ecf20Sopenharmony_ci	limit = (ha->orig_fw_iocb_count * QLA_IOCB_PCT_LIMIT) / 100;
37568c2ecf20Sopenharmony_ci
37578c2ecf20Sopenharmony_ci	ha->base_qpair->fwres.iocbs_total = ha->orig_fw_iocb_count;
37588c2ecf20Sopenharmony_ci	ha->base_qpair->fwres.iocbs_limit = limit;
37598c2ecf20Sopenharmony_ci	ha->base_qpair->fwres.iocbs_qp_limit = limit / num_qps;
37608c2ecf20Sopenharmony_ci	ha->base_qpair->fwres.iocbs_used = 0;
37618c2ecf20Sopenharmony_ci	for (i = 0; i < ha->max_qpairs; i++) {
37628c2ecf20Sopenharmony_ci		if (ha->queue_pair_map[i])  {
37638c2ecf20Sopenharmony_ci			ha->queue_pair_map[i]->fwres.iocbs_total =
37648c2ecf20Sopenharmony_ci				ha->orig_fw_iocb_count;
37658c2ecf20Sopenharmony_ci			ha->queue_pair_map[i]->fwres.iocbs_limit = limit;
37668c2ecf20Sopenharmony_ci			ha->queue_pair_map[i]->fwres.iocbs_qp_limit =
37678c2ecf20Sopenharmony_ci				limit / num_qps;
37688c2ecf20Sopenharmony_ci			ha->queue_pair_map[i]->fwres.iocbs_used = 0;
37698c2ecf20Sopenharmony_ci		}
37708c2ecf20Sopenharmony_ci	}
37718c2ecf20Sopenharmony_ci}
37728c2ecf20Sopenharmony_ci
37738c2ecf20Sopenharmony_ci/**
37748c2ecf20Sopenharmony_ci * qla2x00_setup_chip() - Load and start RISC firmware.
37758c2ecf20Sopenharmony_ci * @vha: HA context
37768c2ecf20Sopenharmony_ci *
37778c2ecf20Sopenharmony_ci * Returns 0 on success.
37788c2ecf20Sopenharmony_ci */
37798c2ecf20Sopenharmony_cistatic int
37808c2ecf20Sopenharmony_ciqla2x00_setup_chip(scsi_qla_host_t *vha)
37818c2ecf20Sopenharmony_ci{
37828c2ecf20Sopenharmony_ci	int rval;
37838c2ecf20Sopenharmony_ci	uint32_t srisc_address = 0;
37848c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
37858c2ecf20Sopenharmony_ci	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
37868c2ecf20Sopenharmony_ci	unsigned long flags;
37878c2ecf20Sopenharmony_ci	uint16_t fw_major_version;
37888c2ecf20Sopenharmony_ci	int done_once = 0;
37898c2ecf20Sopenharmony_ci
37908c2ecf20Sopenharmony_ci	if (IS_P3P_TYPE(ha)) {
37918c2ecf20Sopenharmony_ci		rval = ha->isp_ops->load_risc(vha, &srisc_address);
37928c2ecf20Sopenharmony_ci		if (rval == QLA_SUCCESS) {
37938c2ecf20Sopenharmony_ci			qla2x00_stop_firmware(vha);
37948c2ecf20Sopenharmony_ci			goto enable_82xx_npiv;
37958c2ecf20Sopenharmony_ci		} else
37968c2ecf20Sopenharmony_ci			goto failed;
37978c2ecf20Sopenharmony_ci	}
37988c2ecf20Sopenharmony_ci
37998c2ecf20Sopenharmony_ci	if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
38008c2ecf20Sopenharmony_ci		/* Disable SRAM, Instruction RAM and GP RAM parity.  */
38018c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->hardware_lock, flags);
38028c2ecf20Sopenharmony_ci		wrt_reg_word(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
38038c2ecf20Sopenharmony_ci		rd_reg_word(&reg->hccr);
38048c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->hardware_lock, flags);
38058c2ecf20Sopenharmony_ci	}
38068c2ecf20Sopenharmony_ci
38078c2ecf20Sopenharmony_ci	qla81xx_mpi_sync(vha);
38088c2ecf20Sopenharmony_ci
38098c2ecf20Sopenharmony_ciexecute_fw_with_lr:
38108c2ecf20Sopenharmony_ci	/* Load firmware sequences */
38118c2ecf20Sopenharmony_ci	rval = ha->isp_ops->load_risc(vha, &srisc_address);
38128c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS) {
38138c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x00c9,
38148c2ecf20Sopenharmony_ci		    "Verifying Checksum of loaded RISC code.\n");
38158c2ecf20Sopenharmony_ci
38168c2ecf20Sopenharmony_ci		rval = qla2x00_verify_checksum(vha, srisc_address);
38178c2ecf20Sopenharmony_ci		if (rval == QLA_SUCCESS) {
38188c2ecf20Sopenharmony_ci			/* Start firmware execution. */
38198c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x00ca,
38208c2ecf20Sopenharmony_ci			    "Starting firmware.\n");
38218c2ecf20Sopenharmony_ci
38228c2ecf20Sopenharmony_ci			if (ql2xexlogins)
38238c2ecf20Sopenharmony_ci				ha->flags.exlogins_enabled = 1;
38248c2ecf20Sopenharmony_ci
38258c2ecf20Sopenharmony_ci			if (qla_is_exch_offld_enabled(vha))
38268c2ecf20Sopenharmony_ci				ha->flags.exchoffld_enabled = 1;
38278c2ecf20Sopenharmony_ci
38288c2ecf20Sopenharmony_ci			rval = qla2x00_execute_fw(vha, srisc_address);
38298c2ecf20Sopenharmony_ci			/* Retrieve firmware information. */
38308c2ecf20Sopenharmony_ci			if (rval == QLA_SUCCESS) {
38318c2ecf20Sopenharmony_ci				/* Enable BPM support? */
38328c2ecf20Sopenharmony_ci				if (!done_once++ && qla24xx_detect_sfp(vha)) {
38338c2ecf20Sopenharmony_ci					ql_dbg(ql_dbg_init, vha, 0x00ca,
38348c2ecf20Sopenharmony_ci					    "Re-starting firmware -- BPM.\n");
38358c2ecf20Sopenharmony_ci					/* Best-effort - re-init. */
38368c2ecf20Sopenharmony_ci					ha->isp_ops->reset_chip(vha);
38378c2ecf20Sopenharmony_ci					ha->isp_ops->chip_diag(vha);
38388c2ecf20Sopenharmony_ci					goto execute_fw_with_lr;
38398c2ecf20Sopenharmony_ci				}
38408c2ecf20Sopenharmony_ci
38418c2ecf20Sopenharmony_ci				if (IS_ZIO_THRESHOLD_CAPABLE(ha))
38428c2ecf20Sopenharmony_ci					qla27xx_set_zio_threshold(vha,
38438c2ecf20Sopenharmony_ci					    ha->last_zio_threshold);
38448c2ecf20Sopenharmony_ci
38458c2ecf20Sopenharmony_ci				rval = qla2x00_set_exlogins_buffer(vha);
38468c2ecf20Sopenharmony_ci				if (rval != QLA_SUCCESS)
38478c2ecf20Sopenharmony_ci					goto failed;
38488c2ecf20Sopenharmony_ci
38498c2ecf20Sopenharmony_ci				rval = qla2x00_set_exchoffld_buffer(vha);
38508c2ecf20Sopenharmony_ci				if (rval != QLA_SUCCESS)
38518c2ecf20Sopenharmony_ci					goto failed;
38528c2ecf20Sopenharmony_ci
38538c2ecf20Sopenharmony_cienable_82xx_npiv:
38548c2ecf20Sopenharmony_ci				fw_major_version = ha->fw_major_version;
38558c2ecf20Sopenharmony_ci				if (IS_P3P_TYPE(ha))
38568c2ecf20Sopenharmony_ci					qla82xx_check_md_needed(vha);
38578c2ecf20Sopenharmony_ci				else
38588c2ecf20Sopenharmony_ci					rval = qla2x00_get_fw_version(vha);
38598c2ecf20Sopenharmony_ci				if (rval != QLA_SUCCESS)
38608c2ecf20Sopenharmony_ci					goto failed;
38618c2ecf20Sopenharmony_ci				ha->flags.npiv_supported = 0;
38628c2ecf20Sopenharmony_ci				if (IS_QLA2XXX_MIDTYPE(ha) &&
38638c2ecf20Sopenharmony_ci					 (ha->fw_attributes & BIT_2)) {
38648c2ecf20Sopenharmony_ci					ha->flags.npiv_supported = 1;
38658c2ecf20Sopenharmony_ci					if ((!ha->max_npiv_vports) ||
38668c2ecf20Sopenharmony_ci					    ((ha->max_npiv_vports + 1) %
38678c2ecf20Sopenharmony_ci					    MIN_MULTI_ID_FABRIC))
38688c2ecf20Sopenharmony_ci						ha->max_npiv_vports =
38698c2ecf20Sopenharmony_ci						    MIN_MULTI_ID_FABRIC - 1;
38708c2ecf20Sopenharmony_ci				}
38718c2ecf20Sopenharmony_ci				qla2x00_get_resource_cnts(vha);
38728c2ecf20Sopenharmony_ci				qla_init_iocb_limit(vha);
38738c2ecf20Sopenharmony_ci
38748c2ecf20Sopenharmony_ci				/*
38758c2ecf20Sopenharmony_ci				 * Allocate the array of outstanding commands
38768c2ecf20Sopenharmony_ci				 * now that we know the firmware resources.
38778c2ecf20Sopenharmony_ci				 */
38788c2ecf20Sopenharmony_ci				rval = qla2x00_alloc_outstanding_cmds(ha,
38798c2ecf20Sopenharmony_ci				    vha->req);
38808c2ecf20Sopenharmony_ci				if (rval != QLA_SUCCESS)
38818c2ecf20Sopenharmony_ci					goto failed;
38828c2ecf20Sopenharmony_ci
38838c2ecf20Sopenharmony_ci				if (!fw_major_version && !(IS_P3P_TYPE(ha)))
38848c2ecf20Sopenharmony_ci					qla2x00_alloc_offload_mem(vha);
38858c2ecf20Sopenharmony_ci
38868c2ecf20Sopenharmony_ci				if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
38878c2ecf20Sopenharmony_ci					qla2x00_alloc_fw_dump(vha);
38888c2ecf20Sopenharmony_ci
38898c2ecf20Sopenharmony_ci			} else {
38908c2ecf20Sopenharmony_ci				goto failed;
38918c2ecf20Sopenharmony_ci			}
38928c2ecf20Sopenharmony_ci		} else {
38938c2ecf20Sopenharmony_ci			ql_log(ql_log_fatal, vha, 0x00cd,
38948c2ecf20Sopenharmony_ci			    "ISP Firmware failed checksum.\n");
38958c2ecf20Sopenharmony_ci			goto failed;
38968c2ecf20Sopenharmony_ci		}
38978c2ecf20Sopenharmony_ci
38988c2ecf20Sopenharmony_ci		/* Enable PUREX PASSTHRU */
38998c2ecf20Sopenharmony_ci		if (ql2xrdpenable || ha->flags.scm_supported_f)
39008c2ecf20Sopenharmony_ci			qla25xx_set_els_cmds_supported(vha);
39018c2ecf20Sopenharmony_ci	} else
39028c2ecf20Sopenharmony_ci		goto failed;
39038c2ecf20Sopenharmony_ci
39048c2ecf20Sopenharmony_ci	if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
39058c2ecf20Sopenharmony_ci		/* Enable proper parity. */
39068c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->hardware_lock, flags);
39078c2ecf20Sopenharmony_ci		if (IS_QLA2300(ha))
39088c2ecf20Sopenharmony_ci			/* SRAM parity */
39098c2ecf20Sopenharmony_ci			wrt_reg_word(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
39108c2ecf20Sopenharmony_ci		else
39118c2ecf20Sopenharmony_ci			/* SRAM, Instruction RAM and GP RAM parity */
39128c2ecf20Sopenharmony_ci			wrt_reg_word(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
39138c2ecf20Sopenharmony_ci		rd_reg_word(&reg->hccr);
39148c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->hardware_lock, flags);
39158c2ecf20Sopenharmony_ci	}
39168c2ecf20Sopenharmony_ci
39178c2ecf20Sopenharmony_ci	if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
39188c2ecf20Sopenharmony_ci		ha->flags.fac_supported = 1;
39198c2ecf20Sopenharmony_ci	else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
39208c2ecf20Sopenharmony_ci		uint32_t size;
39218c2ecf20Sopenharmony_ci
39228c2ecf20Sopenharmony_ci		rval = qla81xx_fac_get_sector_size(vha, &size);
39238c2ecf20Sopenharmony_ci		if (rval == QLA_SUCCESS) {
39248c2ecf20Sopenharmony_ci			ha->flags.fac_supported = 1;
39258c2ecf20Sopenharmony_ci			ha->fdt_block_size = size << 2;
39268c2ecf20Sopenharmony_ci		} else {
39278c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x00ce,
39288c2ecf20Sopenharmony_ci			    "Unsupported FAC firmware (%d.%02d.%02d).\n",
39298c2ecf20Sopenharmony_ci			    ha->fw_major_version, ha->fw_minor_version,
39308c2ecf20Sopenharmony_ci			    ha->fw_subminor_version);
39318c2ecf20Sopenharmony_ci
39328c2ecf20Sopenharmony_ci			if (IS_QLA83XX(ha)) {
39338c2ecf20Sopenharmony_ci				ha->flags.fac_supported = 0;
39348c2ecf20Sopenharmony_ci				rval = QLA_SUCCESS;
39358c2ecf20Sopenharmony_ci			}
39368c2ecf20Sopenharmony_ci		}
39378c2ecf20Sopenharmony_ci	}
39388c2ecf20Sopenharmony_cifailed:
39398c2ecf20Sopenharmony_ci	if (rval) {
39408c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x00cf,
39418c2ecf20Sopenharmony_ci		    "Setup chip ****FAILED****.\n");
39428c2ecf20Sopenharmony_ci	}
39438c2ecf20Sopenharmony_ci
39448c2ecf20Sopenharmony_ci	return (rval);
39458c2ecf20Sopenharmony_ci}
39468c2ecf20Sopenharmony_ci
39478c2ecf20Sopenharmony_ci/**
39488c2ecf20Sopenharmony_ci * qla2x00_init_response_q_entries() - Initializes response queue entries.
39498c2ecf20Sopenharmony_ci * @rsp: response queue
39508c2ecf20Sopenharmony_ci *
39518c2ecf20Sopenharmony_ci * Beginning of request ring has initialization control block already built
39528c2ecf20Sopenharmony_ci * by nvram config routine.
39538c2ecf20Sopenharmony_ci *
39548c2ecf20Sopenharmony_ci * Returns 0 on success.
39558c2ecf20Sopenharmony_ci */
39568c2ecf20Sopenharmony_civoid
39578c2ecf20Sopenharmony_ciqla2x00_init_response_q_entries(struct rsp_que *rsp)
39588c2ecf20Sopenharmony_ci{
39598c2ecf20Sopenharmony_ci	uint16_t cnt;
39608c2ecf20Sopenharmony_ci	response_t *pkt;
39618c2ecf20Sopenharmony_ci
39628c2ecf20Sopenharmony_ci	rsp->ring_ptr = rsp->ring;
39638c2ecf20Sopenharmony_ci	rsp->ring_index    = 0;
39648c2ecf20Sopenharmony_ci	rsp->status_srb = NULL;
39658c2ecf20Sopenharmony_ci	pkt = rsp->ring_ptr;
39668c2ecf20Sopenharmony_ci	for (cnt = 0; cnt < rsp->length; cnt++) {
39678c2ecf20Sopenharmony_ci		pkt->signature = RESPONSE_PROCESSED;
39688c2ecf20Sopenharmony_ci		pkt++;
39698c2ecf20Sopenharmony_ci	}
39708c2ecf20Sopenharmony_ci}
39718c2ecf20Sopenharmony_ci
39728c2ecf20Sopenharmony_ci/**
39738c2ecf20Sopenharmony_ci * qla2x00_update_fw_options() - Read and process firmware options.
39748c2ecf20Sopenharmony_ci * @vha: HA context
39758c2ecf20Sopenharmony_ci *
39768c2ecf20Sopenharmony_ci * Returns 0 on success.
39778c2ecf20Sopenharmony_ci */
39788c2ecf20Sopenharmony_civoid
39798c2ecf20Sopenharmony_ciqla2x00_update_fw_options(scsi_qla_host_t *vha)
39808c2ecf20Sopenharmony_ci{
39818c2ecf20Sopenharmony_ci	uint16_t swing, emphasis, tx_sens, rx_sens;
39828c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
39838c2ecf20Sopenharmony_ci
39848c2ecf20Sopenharmony_ci	memset(ha->fw_options, 0, sizeof(ha->fw_options));
39858c2ecf20Sopenharmony_ci	qla2x00_get_fw_options(vha, ha->fw_options);
39868c2ecf20Sopenharmony_ci
39878c2ecf20Sopenharmony_ci	if (IS_QLA2100(ha) || IS_QLA2200(ha))
39888c2ecf20Sopenharmony_ci		return;
39898c2ecf20Sopenharmony_ci
39908c2ecf20Sopenharmony_ci	/* Serial Link options. */
39918c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
39928c2ecf20Sopenharmony_ci	    "Serial link options.\n");
39938c2ecf20Sopenharmony_ci	ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
39948c2ecf20Sopenharmony_ci	    ha->fw_seriallink_options, sizeof(ha->fw_seriallink_options));
39958c2ecf20Sopenharmony_ci
39968c2ecf20Sopenharmony_ci	ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
39978c2ecf20Sopenharmony_ci	if (ha->fw_seriallink_options[3] & BIT_2) {
39988c2ecf20Sopenharmony_ci		ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
39998c2ecf20Sopenharmony_ci
40008c2ecf20Sopenharmony_ci		/*  1G settings */
40018c2ecf20Sopenharmony_ci		swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
40028c2ecf20Sopenharmony_ci		emphasis = (ha->fw_seriallink_options[2] &
40038c2ecf20Sopenharmony_ci		    (BIT_4 | BIT_3)) >> 3;
40048c2ecf20Sopenharmony_ci		tx_sens = ha->fw_seriallink_options[0] &
40058c2ecf20Sopenharmony_ci		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
40068c2ecf20Sopenharmony_ci		rx_sens = (ha->fw_seriallink_options[0] &
40078c2ecf20Sopenharmony_ci		    (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
40088c2ecf20Sopenharmony_ci		ha->fw_options[10] = (emphasis << 14) | (swing << 8);
40098c2ecf20Sopenharmony_ci		if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
40108c2ecf20Sopenharmony_ci			if (rx_sens == 0x0)
40118c2ecf20Sopenharmony_ci				rx_sens = 0x3;
40128c2ecf20Sopenharmony_ci			ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
40138c2ecf20Sopenharmony_ci		} else if (IS_QLA2322(ha) || IS_QLA6322(ha))
40148c2ecf20Sopenharmony_ci			ha->fw_options[10] |= BIT_5 |
40158c2ecf20Sopenharmony_ci			    ((rx_sens & (BIT_1 | BIT_0)) << 2) |
40168c2ecf20Sopenharmony_ci			    (tx_sens & (BIT_1 | BIT_0));
40178c2ecf20Sopenharmony_ci
40188c2ecf20Sopenharmony_ci		/*  2G settings */
40198c2ecf20Sopenharmony_ci		swing = (ha->fw_seriallink_options[2] &
40208c2ecf20Sopenharmony_ci		    (BIT_7 | BIT_6 | BIT_5)) >> 5;
40218c2ecf20Sopenharmony_ci		emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
40228c2ecf20Sopenharmony_ci		tx_sens = ha->fw_seriallink_options[1] &
40238c2ecf20Sopenharmony_ci		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
40248c2ecf20Sopenharmony_ci		rx_sens = (ha->fw_seriallink_options[1] &
40258c2ecf20Sopenharmony_ci		    (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
40268c2ecf20Sopenharmony_ci		ha->fw_options[11] = (emphasis << 14) | (swing << 8);
40278c2ecf20Sopenharmony_ci		if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
40288c2ecf20Sopenharmony_ci			if (rx_sens == 0x0)
40298c2ecf20Sopenharmony_ci				rx_sens = 0x3;
40308c2ecf20Sopenharmony_ci			ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
40318c2ecf20Sopenharmony_ci		} else if (IS_QLA2322(ha) || IS_QLA6322(ha))
40328c2ecf20Sopenharmony_ci			ha->fw_options[11] |= BIT_5 |
40338c2ecf20Sopenharmony_ci			    ((rx_sens & (BIT_1 | BIT_0)) << 2) |
40348c2ecf20Sopenharmony_ci			    (tx_sens & (BIT_1 | BIT_0));
40358c2ecf20Sopenharmony_ci	}
40368c2ecf20Sopenharmony_ci
40378c2ecf20Sopenharmony_ci	/* FCP2 options. */
40388c2ecf20Sopenharmony_ci	/*  Return command IOCBs without waiting for an ABTS to complete. */
40398c2ecf20Sopenharmony_ci	ha->fw_options[3] |= BIT_13;
40408c2ecf20Sopenharmony_ci
40418c2ecf20Sopenharmony_ci	/* LED scheme. */
40428c2ecf20Sopenharmony_ci	if (ha->flags.enable_led_scheme)
40438c2ecf20Sopenharmony_ci		ha->fw_options[2] |= BIT_12;
40448c2ecf20Sopenharmony_ci
40458c2ecf20Sopenharmony_ci	/* Detect ISP6312. */
40468c2ecf20Sopenharmony_ci	if (IS_QLA6312(ha))
40478c2ecf20Sopenharmony_ci		ha->fw_options[2] |= BIT_13;
40488c2ecf20Sopenharmony_ci
40498c2ecf20Sopenharmony_ci	/* Set Retry FLOGI in case of P2P connection */
40508c2ecf20Sopenharmony_ci	if (ha->operating_mode == P2P) {
40518c2ecf20Sopenharmony_ci		ha->fw_options[2] |= BIT_3;
40528c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x2100,
40538c2ecf20Sopenharmony_ci		    "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
40548c2ecf20Sopenharmony_ci			__func__, ha->fw_options[2]);
40558c2ecf20Sopenharmony_ci	}
40568c2ecf20Sopenharmony_ci
40578c2ecf20Sopenharmony_ci	/* Update firmware options. */
40588c2ecf20Sopenharmony_ci	qla2x00_set_fw_options(vha, ha->fw_options);
40598c2ecf20Sopenharmony_ci}
40608c2ecf20Sopenharmony_ci
40618c2ecf20Sopenharmony_civoid
40628c2ecf20Sopenharmony_ciqla24xx_update_fw_options(scsi_qla_host_t *vha)
40638c2ecf20Sopenharmony_ci{
40648c2ecf20Sopenharmony_ci	int rval;
40658c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
40668c2ecf20Sopenharmony_ci
40678c2ecf20Sopenharmony_ci	if (IS_P3P_TYPE(ha))
40688c2ecf20Sopenharmony_ci		return;
40698c2ecf20Sopenharmony_ci
40708c2ecf20Sopenharmony_ci	/*  Hold status IOCBs until ABTS response received. */
40718c2ecf20Sopenharmony_ci	if (ql2xfwholdabts)
40728c2ecf20Sopenharmony_ci		ha->fw_options[3] |= BIT_12;
40738c2ecf20Sopenharmony_ci
40748c2ecf20Sopenharmony_ci	/* Set Retry FLOGI in case of P2P connection */
40758c2ecf20Sopenharmony_ci	if (ha->operating_mode == P2P) {
40768c2ecf20Sopenharmony_ci		ha->fw_options[2] |= BIT_3;
40778c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x2101,
40788c2ecf20Sopenharmony_ci		    "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
40798c2ecf20Sopenharmony_ci			__func__, ha->fw_options[2]);
40808c2ecf20Sopenharmony_ci	}
40818c2ecf20Sopenharmony_ci
40828c2ecf20Sopenharmony_ci	/* Move PUREX, ABTS RX & RIDA to ATIOQ */
40838c2ecf20Sopenharmony_ci	if (ql2xmvasynctoatio &&
40848c2ecf20Sopenharmony_ci	    (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))) {
40858c2ecf20Sopenharmony_ci		if (qla_tgt_mode_enabled(vha) ||
40868c2ecf20Sopenharmony_ci		    qla_dual_mode_enabled(vha))
40878c2ecf20Sopenharmony_ci			ha->fw_options[2] |= BIT_11;
40888c2ecf20Sopenharmony_ci		else
40898c2ecf20Sopenharmony_ci			ha->fw_options[2] &= ~BIT_11;
40908c2ecf20Sopenharmony_ci	}
40918c2ecf20Sopenharmony_ci
40928c2ecf20Sopenharmony_ci	if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
40938c2ecf20Sopenharmony_ci	    IS_QLA28XX(ha)) {
40948c2ecf20Sopenharmony_ci		/*
40958c2ecf20Sopenharmony_ci		 * Tell FW to track each exchange to prevent
40968c2ecf20Sopenharmony_ci		 * driver from using stale exchange.
40978c2ecf20Sopenharmony_ci		 */
40988c2ecf20Sopenharmony_ci		if (qla_tgt_mode_enabled(vha) ||
40998c2ecf20Sopenharmony_ci		    qla_dual_mode_enabled(vha))
41008c2ecf20Sopenharmony_ci			ha->fw_options[2] |= BIT_4;
41018c2ecf20Sopenharmony_ci		else
41028c2ecf20Sopenharmony_ci			ha->fw_options[2] &= ~BIT_4;
41038c2ecf20Sopenharmony_ci
41048c2ecf20Sopenharmony_ci		/* Reserve 1/2 of emergency exchanges for ELS.*/
41058c2ecf20Sopenharmony_ci		if (qla2xuseresexchforels)
41068c2ecf20Sopenharmony_ci			ha->fw_options[2] |= BIT_8;
41078c2ecf20Sopenharmony_ci		else
41088c2ecf20Sopenharmony_ci			ha->fw_options[2] &= ~BIT_8;
41098c2ecf20Sopenharmony_ci	}
41108c2ecf20Sopenharmony_ci
41118c2ecf20Sopenharmony_ci	if (ql2xrdpenable || ha->flags.scm_supported_f)
41128c2ecf20Sopenharmony_ci		ha->fw_options[1] |= ADD_FO1_ENABLE_PUREX_IOCB;
41138c2ecf20Sopenharmony_ci
41148c2ecf20Sopenharmony_ci	/* Enable Async 8130/8131 events -- transceiver insertion/removal */
41158c2ecf20Sopenharmony_ci	if (IS_BPM_RANGE_CAPABLE(ha))
41168c2ecf20Sopenharmony_ci		ha->fw_options[3] |= BIT_10;
41178c2ecf20Sopenharmony_ci
41188c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x00e8,
41198c2ecf20Sopenharmony_ci	    "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
41208c2ecf20Sopenharmony_ci	    __func__, ha->fw_options[1], ha->fw_options[2],
41218c2ecf20Sopenharmony_ci	    ha->fw_options[3], vha->host->active_mode);
41228c2ecf20Sopenharmony_ci
41238c2ecf20Sopenharmony_ci	if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
41248c2ecf20Sopenharmony_ci		qla2x00_set_fw_options(vha, ha->fw_options);
41258c2ecf20Sopenharmony_ci
41268c2ecf20Sopenharmony_ci	/* Update Serial Link options. */
41278c2ecf20Sopenharmony_ci	if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
41288c2ecf20Sopenharmony_ci		return;
41298c2ecf20Sopenharmony_ci
41308c2ecf20Sopenharmony_ci	rval = qla2x00_set_serdes_params(vha,
41318c2ecf20Sopenharmony_ci	    le16_to_cpu(ha->fw_seriallink_options24[1]),
41328c2ecf20Sopenharmony_ci	    le16_to_cpu(ha->fw_seriallink_options24[2]),
41338c2ecf20Sopenharmony_ci	    le16_to_cpu(ha->fw_seriallink_options24[3]));
41348c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
41358c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0104,
41368c2ecf20Sopenharmony_ci		    "Unable to update Serial Link options (%x).\n", rval);
41378c2ecf20Sopenharmony_ci	}
41388c2ecf20Sopenharmony_ci}
41398c2ecf20Sopenharmony_ci
41408c2ecf20Sopenharmony_civoid
41418c2ecf20Sopenharmony_ciqla2x00_config_rings(struct scsi_qla_host *vha)
41428c2ecf20Sopenharmony_ci{
41438c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
41448c2ecf20Sopenharmony_ci	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
41458c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
41468c2ecf20Sopenharmony_ci	struct rsp_que *rsp = ha->rsp_q_map[0];
41478c2ecf20Sopenharmony_ci
41488c2ecf20Sopenharmony_ci	/* Setup ring parameters in initialization control block. */
41498c2ecf20Sopenharmony_ci	ha->init_cb->request_q_outpointer = cpu_to_le16(0);
41508c2ecf20Sopenharmony_ci	ha->init_cb->response_q_inpointer = cpu_to_le16(0);
41518c2ecf20Sopenharmony_ci	ha->init_cb->request_q_length = cpu_to_le16(req->length);
41528c2ecf20Sopenharmony_ci	ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
41538c2ecf20Sopenharmony_ci	put_unaligned_le64(req->dma, &ha->init_cb->request_q_address);
41548c2ecf20Sopenharmony_ci	put_unaligned_le64(rsp->dma, &ha->init_cb->response_q_address);
41558c2ecf20Sopenharmony_ci
41568c2ecf20Sopenharmony_ci	wrt_reg_word(ISP_REQ_Q_IN(ha, reg), 0);
41578c2ecf20Sopenharmony_ci	wrt_reg_word(ISP_REQ_Q_OUT(ha, reg), 0);
41588c2ecf20Sopenharmony_ci	wrt_reg_word(ISP_RSP_Q_IN(ha, reg), 0);
41598c2ecf20Sopenharmony_ci	wrt_reg_word(ISP_RSP_Q_OUT(ha, reg), 0);
41608c2ecf20Sopenharmony_ci	rd_reg_word(ISP_RSP_Q_OUT(ha, reg));		/* PCI Posting. */
41618c2ecf20Sopenharmony_ci}
41628c2ecf20Sopenharmony_ci
41638c2ecf20Sopenharmony_civoid
41648c2ecf20Sopenharmony_ciqla24xx_config_rings(struct scsi_qla_host *vha)
41658c2ecf20Sopenharmony_ci{
41668c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
41678c2ecf20Sopenharmony_ci	device_reg_t *reg = ISP_QUE_REG(ha, 0);
41688c2ecf20Sopenharmony_ci	struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
41698c2ecf20Sopenharmony_ci	struct qla_msix_entry *msix;
41708c2ecf20Sopenharmony_ci	struct init_cb_24xx *icb;
41718c2ecf20Sopenharmony_ci	uint16_t rid = 0;
41728c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
41738c2ecf20Sopenharmony_ci	struct rsp_que *rsp = ha->rsp_q_map[0];
41748c2ecf20Sopenharmony_ci
41758c2ecf20Sopenharmony_ci	/* Setup ring parameters in initialization control block. */
41768c2ecf20Sopenharmony_ci	icb = (struct init_cb_24xx *)ha->init_cb;
41778c2ecf20Sopenharmony_ci	icb->request_q_outpointer = cpu_to_le16(0);
41788c2ecf20Sopenharmony_ci	icb->response_q_inpointer = cpu_to_le16(0);
41798c2ecf20Sopenharmony_ci	icb->request_q_length = cpu_to_le16(req->length);
41808c2ecf20Sopenharmony_ci	icb->response_q_length = cpu_to_le16(rsp->length);
41818c2ecf20Sopenharmony_ci	put_unaligned_le64(req->dma, &icb->request_q_address);
41828c2ecf20Sopenharmony_ci	put_unaligned_le64(rsp->dma, &icb->response_q_address);
41838c2ecf20Sopenharmony_ci
41848c2ecf20Sopenharmony_ci	/* Setup ATIO queue dma pointers for target mode */
41858c2ecf20Sopenharmony_ci	icb->atio_q_inpointer = cpu_to_le16(0);
41868c2ecf20Sopenharmony_ci	icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
41878c2ecf20Sopenharmony_ci	put_unaligned_le64(ha->tgt.atio_dma, &icb->atio_q_address);
41888c2ecf20Sopenharmony_ci
41898c2ecf20Sopenharmony_ci	if (IS_SHADOW_REG_CAPABLE(ha))
41908c2ecf20Sopenharmony_ci		icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
41918c2ecf20Sopenharmony_ci
41928c2ecf20Sopenharmony_ci	if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
41938c2ecf20Sopenharmony_ci	    IS_QLA28XX(ha)) {
41948c2ecf20Sopenharmony_ci		icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
41958c2ecf20Sopenharmony_ci		icb->rid = cpu_to_le16(rid);
41968c2ecf20Sopenharmony_ci		if (ha->flags.msix_enabled) {
41978c2ecf20Sopenharmony_ci			msix = &ha->msix_entries[1];
41988c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x0019,
41998c2ecf20Sopenharmony_ci			    "Registering vector 0x%x for base que.\n",
42008c2ecf20Sopenharmony_ci			    msix->entry);
42018c2ecf20Sopenharmony_ci			icb->msix = cpu_to_le16(msix->entry);
42028c2ecf20Sopenharmony_ci		}
42038c2ecf20Sopenharmony_ci		/* Use alternate PCI bus number */
42048c2ecf20Sopenharmony_ci		if (MSB(rid))
42058c2ecf20Sopenharmony_ci			icb->firmware_options_2 |= cpu_to_le32(BIT_19);
42068c2ecf20Sopenharmony_ci		/* Use alternate PCI devfn */
42078c2ecf20Sopenharmony_ci		if (LSB(rid))
42088c2ecf20Sopenharmony_ci			icb->firmware_options_2 |= cpu_to_le32(BIT_18);
42098c2ecf20Sopenharmony_ci
42108c2ecf20Sopenharmony_ci		/* Use Disable MSIX Handshake mode for capable adapters */
42118c2ecf20Sopenharmony_ci		if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
42128c2ecf20Sopenharmony_ci		    (ha->flags.msix_enabled)) {
42138c2ecf20Sopenharmony_ci			icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
42148c2ecf20Sopenharmony_ci			ha->flags.disable_msix_handshake = 1;
42158c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x00fe,
42168c2ecf20Sopenharmony_ci			    "MSIX Handshake Disable Mode turned on.\n");
42178c2ecf20Sopenharmony_ci		} else {
42188c2ecf20Sopenharmony_ci			icb->firmware_options_2 |= cpu_to_le32(BIT_22);
42198c2ecf20Sopenharmony_ci		}
42208c2ecf20Sopenharmony_ci		icb->firmware_options_2 |= cpu_to_le32(BIT_23);
42218c2ecf20Sopenharmony_ci
42228c2ecf20Sopenharmony_ci		wrt_reg_dword(&reg->isp25mq.req_q_in, 0);
42238c2ecf20Sopenharmony_ci		wrt_reg_dword(&reg->isp25mq.req_q_out, 0);
42248c2ecf20Sopenharmony_ci		wrt_reg_dword(&reg->isp25mq.rsp_q_in, 0);
42258c2ecf20Sopenharmony_ci		wrt_reg_dword(&reg->isp25mq.rsp_q_out, 0);
42268c2ecf20Sopenharmony_ci	} else {
42278c2ecf20Sopenharmony_ci		wrt_reg_dword(&reg->isp24.req_q_in, 0);
42288c2ecf20Sopenharmony_ci		wrt_reg_dword(&reg->isp24.req_q_out, 0);
42298c2ecf20Sopenharmony_ci		wrt_reg_dword(&reg->isp24.rsp_q_in, 0);
42308c2ecf20Sopenharmony_ci		wrt_reg_dword(&reg->isp24.rsp_q_out, 0);
42318c2ecf20Sopenharmony_ci	}
42328c2ecf20Sopenharmony_ci
42338c2ecf20Sopenharmony_ci	qlt_24xx_config_rings(vha);
42348c2ecf20Sopenharmony_ci
42358c2ecf20Sopenharmony_ci	/* If the user has configured the speed, set it here */
42368c2ecf20Sopenharmony_ci	if (ha->set_data_rate) {
42378c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x00fd,
42388c2ecf20Sopenharmony_ci		    "Speed set by user : %s Gbps \n",
42398c2ecf20Sopenharmony_ci		    qla2x00_get_link_speed_str(ha, ha->set_data_rate));
42408c2ecf20Sopenharmony_ci		icb->firmware_options_3 = cpu_to_le32(ha->set_data_rate << 13);
42418c2ecf20Sopenharmony_ci	}
42428c2ecf20Sopenharmony_ci
42438c2ecf20Sopenharmony_ci	/* PCI posting */
42448c2ecf20Sopenharmony_ci	rd_reg_word(&ioreg->hccr);
42458c2ecf20Sopenharmony_ci}
42468c2ecf20Sopenharmony_ci
42478c2ecf20Sopenharmony_ci/**
42488c2ecf20Sopenharmony_ci * qla2x00_init_rings() - Initializes firmware.
42498c2ecf20Sopenharmony_ci * @vha: HA context
42508c2ecf20Sopenharmony_ci *
42518c2ecf20Sopenharmony_ci * Beginning of request ring has initialization control block already built
42528c2ecf20Sopenharmony_ci * by nvram config routine.
42538c2ecf20Sopenharmony_ci *
42548c2ecf20Sopenharmony_ci * Returns 0 on success.
42558c2ecf20Sopenharmony_ci */
42568c2ecf20Sopenharmony_ciint
42578c2ecf20Sopenharmony_ciqla2x00_init_rings(scsi_qla_host_t *vha)
42588c2ecf20Sopenharmony_ci{
42598c2ecf20Sopenharmony_ci	int	rval;
42608c2ecf20Sopenharmony_ci	unsigned long flags = 0;
42618c2ecf20Sopenharmony_ci	int cnt, que;
42628c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
42638c2ecf20Sopenharmony_ci	struct req_que *req;
42648c2ecf20Sopenharmony_ci	struct rsp_que *rsp;
42658c2ecf20Sopenharmony_ci	struct mid_init_cb_24xx *mid_init_cb =
42668c2ecf20Sopenharmony_ci	    (struct mid_init_cb_24xx *) ha->init_cb;
42678c2ecf20Sopenharmony_ci
42688c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
42698c2ecf20Sopenharmony_ci
42708c2ecf20Sopenharmony_ci	/* Clear outstanding commands array. */
42718c2ecf20Sopenharmony_ci	for (que = 0; que < ha->max_req_queues; que++) {
42728c2ecf20Sopenharmony_ci		req = ha->req_q_map[que];
42738c2ecf20Sopenharmony_ci		if (!req || !test_bit(que, ha->req_qid_map))
42748c2ecf20Sopenharmony_ci			continue;
42758c2ecf20Sopenharmony_ci		req->out_ptr = (uint16_t *)(req->ring + req->length);
42768c2ecf20Sopenharmony_ci		*req->out_ptr = 0;
42778c2ecf20Sopenharmony_ci		for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
42788c2ecf20Sopenharmony_ci			req->outstanding_cmds[cnt] = NULL;
42798c2ecf20Sopenharmony_ci
42808c2ecf20Sopenharmony_ci		req->current_outstanding_cmd = 1;
42818c2ecf20Sopenharmony_ci
42828c2ecf20Sopenharmony_ci		/* Initialize firmware. */
42838c2ecf20Sopenharmony_ci		req->ring_ptr  = req->ring;
42848c2ecf20Sopenharmony_ci		req->ring_index    = 0;
42858c2ecf20Sopenharmony_ci		req->cnt      = req->length;
42868c2ecf20Sopenharmony_ci	}
42878c2ecf20Sopenharmony_ci
42888c2ecf20Sopenharmony_ci	for (que = 0; que < ha->max_rsp_queues; que++) {
42898c2ecf20Sopenharmony_ci		rsp = ha->rsp_q_map[que];
42908c2ecf20Sopenharmony_ci		if (!rsp || !test_bit(que, ha->rsp_qid_map))
42918c2ecf20Sopenharmony_ci			continue;
42928c2ecf20Sopenharmony_ci		rsp->in_ptr = (uint16_t *)(rsp->ring + rsp->length);
42938c2ecf20Sopenharmony_ci		*rsp->in_ptr = 0;
42948c2ecf20Sopenharmony_ci		/* Initialize response queue entries */
42958c2ecf20Sopenharmony_ci		if (IS_QLAFX00(ha))
42968c2ecf20Sopenharmony_ci			qlafx00_init_response_q_entries(rsp);
42978c2ecf20Sopenharmony_ci		else
42988c2ecf20Sopenharmony_ci			qla2x00_init_response_q_entries(rsp);
42998c2ecf20Sopenharmony_ci	}
43008c2ecf20Sopenharmony_ci
43018c2ecf20Sopenharmony_ci	ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
43028c2ecf20Sopenharmony_ci	ha->tgt.atio_ring_index = 0;
43038c2ecf20Sopenharmony_ci	/* Initialize ATIO queue entries */
43048c2ecf20Sopenharmony_ci	qlt_init_atio_q_entries(vha);
43058c2ecf20Sopenharmony_ci
43068c2ecf20Sopenharmony_ci	ha->isp_ops->config_rings(vha);
43078c2ecf20Sopenharmony_ci
43088c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
43098c2ecf20Sopenharmony_ci
43108c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
43118c2ecf20Sopenharmony_ci
43128c2ecf20Sopenharmony_ci	if (IS_QLAFX00(ha)) {
43138c2ecf20Sopenharmony_ci		rval = qlafx00_init_firmware(vha, ha->init_cb_size);
43148c2ecf20Sopenharmony_ci		goto next_check;
43158c2ecf20Sopenharmony_ci	}
43168c2ecf20Sopenharmony_ci
43178c2ecf20Sopenharmony_ci	/* Update any ISP specific firmware options before initialization. */
43188c2ecf20Sopenharmony_ci	ha->isp_ops->update_fw_options(vha);
43198c2ecf20Sopenharmony_ci
43208c2ecf20Sopenharmony_ci	if (ha->flags.npiv_supported) {
43218c2ecf20Sopenharmony_ci		if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
43228c2ecf20Sopenharmony_ci			ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
43238c2ecf20Sopenharmony_ci		mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
43248c2ecf20Sopenharmony_ci	}
43258c2ecf20Sopenharmony_ci
43268c2ecf20Sopenharmony_ci	if (IS_FWI2_CAPABLE(ha)) {
43278c2ecf20Sopenharmony_ci		mid_init_cb->options = cpu_to_le16(BIT_1);
43288c2ecf20Sopenharmony_ci		mid_init_cb->init_cb.execution_throttle =
43298c2ecf20Sopenharmony_ci		    cpu_to_le16(ha->cur_fw_xcb_count);
43308c2ecf20Sopenharmony_ci		ha->flags.dport_enabled =
43318c2ecf20Sopenharmony_ci			(le32_to_cpu(mid_init_cb->init_cb.firmware_options_1) &
43328c2ecf20Sopenharmony_ci			 BIT_7) != 0;
43338c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
43348c2ecf20Sopenharmony_ci		    (ha->flags.dport_enabled) ? "enabled" : "disabled");
43358c2ecf20Sopenharmony_ci		/* FA-WWPN Status */
43368c2ecf20Sopenharmony_ci		ha->flags.fawwpn_enabled =
43378c2ecf20Sopenharmony_ci			(le32_to_cpu(mid_init_cb->init_cb.firmware_options_1) &
43388c2ecf20Sopenharmony_ci			 BIT_6) != 0;
43398c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
43408c2ecf20Sopenharmony_ci		    (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
43418c2ecf20Sopenharmony_ci		/* Init_cb will be reused for other command(s).  Save a backup copy of port_name */
43428c2ecf20Sopenharmony_ci		memcpy(ha->port_name, ha->init_cb->port_name, WWN_SIZE);
43438c2ecf20Sopenharmony_ci	}
43448c2ecf20Sopenharmony_ci
43458c2ecf20Sopenharmony_ci	QLA_FW_STARTED(ha);
43468c2ecf20Sopenharmony_ci	rval = qla2x00_init_firmware(vha, ha->init_cb_size);
43478c2ecf20Sopenharmony_cinext_check:
43488c2ecf20Sopenharmony_ci	if (rval) {
43498c2ecf20Sopenharmony_ci		QLA_FW_STOPPED(ha);
43508c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x00d2,
43518c2ecf20Sopenharmony_ci		    "Init Firmware **** FAILED ****.\n");
43528c2ecf20Sopenharmony_ci	} else {
43538c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x00d3,
43548c2ecf20Sopenharmony_ci		    "Init Firmware -- success.\n");
43558c2ecf20Sopenharmony_ci		vha->u_ql2xexchoffld = vha->u_ql2xiniexchg = 0;
43568c2ecf20Sopenharmony_ci	}
43578c2ecf20Sopenharmony_ci
43588c2ecf20Sopenharmony_ci	return (rval);
43598c2ecf20Sopenharmony_ci}
43608c2ecf20Sopenharmony_ci
43618c2ecf20Sopenharmony_ci/**
43628c2ecf20Sopenharmony_ci * qla2x00_fw_ready() - Waits for firmware ready.
43638c2ecf20Sopenharmony_ci * @vha: HA context
43648c2ecf20Sopenharmony_ci *
43658c2ecf20Sopenharmony_ci * Returns 0 on success.
43668c2ecf20Sopenharmony_ci */
43678c2ecf20Sopenharmony_cistatic int
43688c2ecf20Sopenharmony_ciqla2x00_fw_ready(scsi_qla_host_t *vha)
43698c2ecf20Sopenharmony_ci{
43708c2ecf20Sopenharmony_ci	int		rval;
43718c2ecf20Sopenharmony_ci	unsigned long	wtime, mtime, cs84xx_time;
43728c2ecf20Sopenharmony_ci	uint16_t	min_wait;	/* Minimum wait time if loop is down */
43738c2ecf20Sopenharmony_ci	uint16_t	wait_time;	/* Wait time if loop is coming ready */
43748c2ecf20Sopenharmony_ci	uint16_t	state[6];
43758c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
43768c2ecf20Sopenharmony_ci
43778c2ecf20Sopenharmony_ci	if (IS_QLAFX00(vha->hw))
43788c2ecf20Sopenharmony_ci		return qlafx00_fw_ready(vha);
43798c2ecf20Sopenharmony_ci
43808c2ecf20Sopenharmony_ci	rval = QLA_SUCCESS;
43818c2ecf20Sopenharmony_ci
43828c2ecf20Sopenharmony_ci	/* Time to wait for loop down */
43838c2ecf20Sopenharmony_ci	if (IS_P3P_TYPE(ha))
43848c2ecf20Sopenharmony_ci		min_wait = 30;
43858c2ecf20Sopenharmony_ci	else
43868c2ecf20Sopenharmony_ci		min_wait = 20;
43878c2ecf20Sopenharmony_ci
43888c2ecf20Sopenharmony_ci	/*
43898c2ecf20Sopenharmony_ci	 * Firmware should take at most one RATOV to login, plus 5 seconds for
43908c2ecf20Sopenharmony_ci	 * our own processing.
43918c2ecf20Sopenharmony_ci	 */
43928c2ecf20Sopenharmony_ci	if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
43938c2ecf20Sopenharmony_ci		wait_time = min_wait;
43948c2ecf20Sopenharmony_ci	}
43958c2ecf20Sopenharmony_ci
43968c2ecf20Sopenharmony_ci	/* Min wait time if loop down */
43978c2ecf20Sopenharmony_ci	mtime = jiffies + (min_wait * HZ);
43988c2ecf20Sopenharmony_ci
43998c2ecf20Sopenharmony_ci	/* wait time before firmware ready */
44008c2ecf20Sopenharmony_ci	wtime = jiffies + (wait_time * HZ);
44018c2ecf20Sopenharmony_ci
44028c2ecf20Sopenharmony_ci	/* Wait for ISP to finish LIP */
44038c2ecf20Sopenharmony_ci	if (!vha->flags.init_done)
44048c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x801e,
44058c2ecf20Sopenharmony_ci		    "Waiting for LIP to complete.\n");
44068c2ecf20Sopenharmony_ci
44078c2ecf20Sopenharmony_ci	do {
44088c2ecf20Sopenharmony_ci		memset(state, -1, sizeof(state));
44098c2ecf20Sopenharmony_ci		rval = qla2x00_get_firmware_state(vha, state);
44108c2ecf20Sopenharmony_ci		if (rval == QLA_SUCCESS) {
44118c2ecf20Sopenharmony_ci			if (state[0] < FSTATE_LOSS_OF_SYNC) {
44128c2ecf20Sopenharmony_ci				vha->device_flags &= ~DFLG_NO_CABLE;
44138c2ecf20Sopenharmony_ci			}
44148c2ecf20Sopenharmony_ci			if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
44158c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_taskm, vha, 0x801f,
44168c2ecf20Sopenharmony_ci				    "fw_state=%x 84xx=%x.\n", state[0],
44178c2ecf20Sopenharmony_ci				    state[2]);
44188c2ecf20Sopenharmony_ci				if ((state[2] & FSTATE_LOGGED_IN) &&
44198c2ecf20Sopenharmony_ci				     (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
44208c2ecf20Sopenharmony_ci					ql_dbg(ql_dbg_taskm, vha, 0x8028,
44218c2ecf20Sopenharmony_ci					    "Sending verify iocb.\n");
44228c2ecf20Sopenharmony_ci
44238c2ecf20Sopenharmony_ci					cs84xx_time = jiffies;
44248c2ecf20Sopenharmony_ci					rval = qla84xx_init_chip(vha);
44258c2ecf20Sopenharmony_ci					if (rval != QLA_SUCCESS) {
44268c2ecf20Sopenharmony_ci						ql_log(ql_log_warn,
44278c2ecf20Sopenharmony_ci						    vha, 0x8007,
44288c2ecf20Sopenharmony_ci						    "Init chip failed.\n");
44298c2ecf20Sopenharmony_ci						break;
44308c2ecf20Sopenharmony_ci					}
44318c2ecf20Sopenharmony_ci
44328c2ecf20Sopenharmony_ci					/* Add time taken to initialize. */
44338c2ecf20Sopenharmony_ci					cs84xx_time = jiffies - cs84xx_time;
44348c2ecf20Sopenharmony_ci					wtime += cs84xx_time;
44358c2ecf20Sopenharmony_ci					mtime += cs84xx_time;
44368c2ecf20Sopenharmony_ci					ql_dbg(ql_dbg_taskm, vha, 0x8008,
44378c2ecf20Sopenharmony_ci					    "Increasing wait time by %ld. "
44388c2ecf20Sopenharmony_ci					    "New time %ld.\n", cs84xx_time,
44398c2ecf20Sopenharmony_ci					    wtime);
44408c2ecf20Sopenharmony_ci				}
44418c2ecf20Sopenharmony_ci			} else if (state[0] == FSTATE_READY) {
44428c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_taskm, vha, 0x8037,
44438c2ecf20Sopenharmony_ci				    "F/W Ready - OK.\n");
44448c2ecf20Sopenharmony_ci
44458c2ecf20Sopenharmony_ci				qla2x00_get_retry_cnt(vha, &ha->retry_count,
44468c2ecf20Sopenharmony_ci				    &ha->login_timeout, &ha->r_a_tov);
44478c2ecf20Sopenharmony_ci
44488c2ecf20Sopenharmony_ci				rval = QLA_SUCCESS;
44498c2ecf20Sopenharmony_ci				break;
44508c2ecf20Sopenharmony_ci			}
44518c2ecf20Sopenharmony_ci
44528c2ecf20Sopenharmony_ci			rval = QLA_FUNCTION_FAILED;
44538c2ecf20Sopenharmony_ci
44548c2ecf20Sopenharmony_ci			if (atomic_read(&vha->loop_down_timer) &&
44558c2ecf20Sopenharmony_ci			    state[0] != FSTATE_READY) {
44568c2ecf20Sopenharmony_ci				/* Loop down. Timeout on min_wait for states
44578c2ecf20Sopenharmony_ci				 * other than Wait for Login.
44588c2ecf20Sopenharmony_ci				 */
44598c2ecf20Sopenharmony_ci				if (time_after_eq(jiffies, mtime)) {
44608c2ecf20Sopenharmony_ci					ql_log(ql_log_info, vha, 0x8038,
44618c2ecf20Sopenharmony_ci					    "Cable is unplugged...\n");
44628c2ecf20Sopenharmony_ci
44638c2ecf20Sopenharmony_ci					vha->device_flags |= DFLG_NO_CABLE;
44648c2ecf20Sopenharmony_ci					break;
44658c2ecf20Sopenharmony_ci				}
44668c2ecf20Sopenharmony_ci			}
44678c2ecf20Sopenharmony_ci		} else {
44688c2ecf20Sopenharmony_ci			/* Mailbox cmd failed. Timeout on min_wait. */
44698c2ecf20Sopenharmony_ci			if (time_after_eq(jiffies, mtime) ||
44708c2ecf20Sopenharmony_ci				ha->flags.isp82xx_fw_hung)
44718c2ecf20Sopenharmony_ci				break;
44728c2ecf20Sopenharmony_ci		}
44738c2ecf20Sopenharmony_ci
44748c2ecf20Sopenharmony_ci		if (time_after_eq(jiffies, wtime))
44758c2ecf20Sopenharmony_ci			break;
44768c2ecf20Sopenharmony_ci
44778c2ecf20Sopenharmony_ci		/* Delay for a while */
44788c2ecf20Sopenharmony_ci		msleep(500);
44798c2ecf20Sopenharmony_ci	} while (1);
44808c2ecf20Sopenharmony_ci
44818c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_taskm, vha, 0x803a,
44828c2ecf20Sopenharmony_ci	    "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
44838c2ecf20Sopenharmony_ci	    state[1], state[2], state[3], state[4], state[5], jiffies);
44848c2ecf20Sopenharmony_ci
44858c2ecf20Sopenharmony_ci	if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
44868c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x803b,
44878c2ecf20Sopenharmony_ci		    "Firmware ready **** FAILED ****.\n");
44888c2ecf20Sopenharmony_ci	}
44898c2ecf20Sopenharmony_ci
44908c2ecf20Sopenharmony_ci	return (rval);
44918c2ecf20Sopenharmony_ci}
44928c2ecf20Sopenharmony_ci
44938c2ecf20Sopenharmony_ci/*
44948c2ecf20Sopenharmony_ci*  qla2x00_configure_hba
44958c2ecf20Sopenharmony_ci*      Setup adapter context.
44968c2ecf20Sopenharmony_ci*
44978c2ecf20Sopenharmony_ci* Input:
44988c2ecf20Sopenharmony_ci*      ha = adapter state pointer.
44998c2ecf20Sopenharmony_ci*
45008c2ecf20Sopenharmony_ci* Returns:
45018c2ecf20Sopenharmony_ci*      0 = success
45028c2ecf20Sopenharmony_ci*
45038c2ecf20Sopenharmony_ci* Context:
45048c2ecf20Sopenharmony_ci*      Kernel context.
45058c2ecf20Sopenharmony_ci*/
45068c2ecf20Sopenharmony_cistatic int
45078c2ecf20Sopenharmony_ciqla2x00_configure_hba(scsi_qla_host_t *vha)
45088c2ecf20Sopenharmony_ci{
45098c2ecf20Sopenharmony_ci	int       rval;
45108c2ecf20Sopenharmony_ci	uint16_t      loop_id;
45118c2ecf20Sopenharmony_ci	uint16_t      topo;
45128c2ecf20Sopenharmony_ci	uint16_t      sw_cap;
45138c2ecf20Sopenharmony_ci	uint8_t       al_pa;
45148c2ecf20Sopenharmony_ci	uint8_t       area;
45158c2ecf20Sopenharmony_ci	uint8_t       domain;
45168c2ecf20Sopenharmony_ci	char		connect_type[22];
45178c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
45188c2ecf20Sopenharmony_ci	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
45198c2ecf20Sopenharmony_ci	port_id_t id;
45208c2ecf20Sopenharmony_ci	unsigned long flags;
45218c2ecf20Sopenharmony_ci
45228c2ecf20Sopenharmony_ci	/* Get host addresses. */
45238c2ecf20Sopenharmony_ci	rval = qla2x00_get_adapter_id(vha,
45248c2ecf20Sopenharmony_ci	    &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
45258c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
45268c2ecf20Sopenharmony_ci		if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
45278c2ecf20Sopenharmony_ci		    IS_CNA_CAPABLE(ha) ||
45288c2ecf20Sopenharmony_ci		    (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
45298c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2008,
45308c2ecf20Sopenharmony_ci			    "Loop is in a transition state.\n");
45318c2ecf20Sopenharmony_ci		} else {
45328c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x2009,
45338c2ecf20Sopenharmony_ci			    "Unable to get host loop ID.\n");
45348c2ecf20Sopenharmony_ci			if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
45358c2ecf20Sopenharmony_ci			    (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
45368c2ecf20Sopenharmony_ci				ql_log(ql_log_warn, vha, 0x1151,
45378c2ecf20Sopenharmony_ci				    "Doing link init.\n");
45388c2ecf20Sopenharmony_ci				if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
45398c2ecf20Sopenharmony_ci					return rval;
45408c2ecf20Sopenharmony_ci			}
45418c2ecf20Sopenharmony_ci			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
45428c2ecf20Sopenharmony_ci		}
45438c2ecf20Sopenharmony_ci		return (rval);
45448c2ecf20Sopenharmony_ci	}
45458c2ecf20Sopenharmony_ci
45468c2ecf20Sopenharmony_ci	if (topo == 4) {
45478c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x200a,
45488c2ecf20Sopenharmony_ci		    "Cannot get topology - retrying.\n");
45498c2ecf20Sopenharmony_ci		return (QLA_FUNCTION_FAILED);
45508c2ecf20Sopenharmony_ci	}
45518c2ecf20Sopenharmony_ci
45528c2ecf20Sopenharmony_ci	vha->loop_id = loop_id;
45538c2ecf20Sopenharmony_ci
45548c2ecf20Sopenharmony_ci	/* initialize */
45558c2ecf20Sopenharmony_ci	ha->min_external_loopid = SNS_FIRST_LOOP_ID;
45568c2ecf20Sopenharmony_ci	ha->operating_mode = LOOP;
45578c2ecf20Sopenharmony_ci	ha->switch_cap = 0;
45588c2ecf20Sopenharmony_ci
45598c2ecf20Sopenharmony_ci	switch (topo) {
45608c2ecf20Sopenharmony_ci	case 0:
45618c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
45628c2ecf20Sopenharmony_ci		ha->current_topology = ISP_CFG_NL;
45638c2ecf20Sopenharmony_ci		strcpy(connect_type, "(Loop)");
45648c2ecf20Sopenharmony_ci		break;
45658c2ecf20Sopenharmony_ci
45668c2ecf20Sopenharmony_ci	case 1:
45678c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
45688c2ecf20Sopenharmony_ci		ha->switch_cap = sw_cap;
45698c2ecf20Sopenharmony_ci		ha->current_topology = ISP_CFG_FL;
45708c2ecf20Sopenharmony_ci		strcpy(connect_type, "(FL_Port)");
45718c2ecf20Sopenharmony_ci		break;
45728c2ecf20Sopenharmony_ci
45738c2ecf20Sopenharmony_ci	case 2:
45748c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
45758c2ecf20Sopenharmony_ci		ha->operating_mode = P2P;
45768c2ecf20Sopenharmony_ci		ha->current_topology = ISP_CFG_N;
45778c2ecf20Sopenharmony_ci		strcpy(connect_type, "(N_Port-to-N_Port)");
45788c2ecf20Sopenharmony_ci		break;
45798c2ecf20Sopenharmony_ci
45808c2ecf20Sopenharmony_ci	case 3:
45818c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
45828c2ecf20Sopenharmony_ci		ha->switch_cap = sw_cap;
45838c2ecf20Sopenharmony_ci		ha->operating_mode = P2P;
45848c2ecf20Sopenharmony_ci		ha->current_topology = ISP_CFG_F;
45858c2ecf20Sopenharmony_ci		strcpy(connect_type, "(F_Port)");
45868c2ecf20Sopenharmony_ci		break;
45878c2ecf20Sopenharmony_ci
45888c2ecf20Sopenharmony_ci	default:
45898c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x200f,
45908c2ecf20Sopenharmony_ci		    "HBA in unknown topology %x, using NL.\n", topo);
45918c2ecf20Sopenharmony_ci		ha->current_topology = ISP_CFG_NL;
45928c2ecf20Sopenharmony_ci		strcpy(connect_type, "(Loop)");
45938c2ecf20Sopenharmony_ci		break;
45948c2ecf20Sopenharmony_ci	}
45958c2ecf20Sopenharmony_ci
45968c2ecf20Sopenharmony_ci	/* Save Host port and loop ID. */
45978c2ecf20Sopenharmony_ci	/* byte order - Big Endian */
45988c2ecf20Sopenharmony_ci	id.b.domain = domain;
45998c2ecf20Sopenharmony_ci	id.b.area = area;
46008c2ecf20Sopenharmony_ci	id.b.al_pa = al_pa;
46018c2ecf20Sopenharmony_ci	id.b.rsvd_1 = 0;
46028c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
46038c2ecf20Sopenharmony_ci	if (!(topo == 2 && ha->flags.n2n_bigger))
46048c2ecf20Sopenharmony_ci		qlt_update_host_map(vha, id);
46058c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
46068c2ecf20Sopenharmony_ci
46078c2ecf20Sopenharmony_ci	if (!vha->flags.init_done)
46088c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x2010,
46098c2ecf20Sopenharmony_ci		    "Topology - %s, Host Loop address 0x%x.\n",
46108c2ecf20Sopenharmony_ci		    connect_type, vha->loop_id);
46118c2ecf20Sopenharmony_ci
46128c2ecf20Sopenharmony_ci	return(rval);
46138c2ecf20Sopenharmony_ci}
46148c2ecf20Sopenharmony_ci
46158c2ecf20Sopenharmony_ciinline void
46168c2ecf20Sopenharmony_ciqla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
46178c2ecf20Sopenharmony_ci		       const char *def)
46188c2ecf20Sopenharmony_ci{
46198c2ecf20Sopenharmony_ci	char *st, *en;
46208c2ecf20Sopenharmony_ci	uint16_t index;
46218c2ecf20Sopenharmony_ci	uint64_t zero[2] = { 0 };
46228c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
46238c2ecf20Sopenharmony_ci	int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
46248c2ecf20Sopenharmony_ci	    !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
46258c2ecf20Sopenharmony_ci
46268c2ecf20Sopenharmony_ci	if (len > sizeof(zero))
46278c2ecf20Sopenharmony_ci		len = sizeof(zero);
46288c2ecf20Sopenharmony_ci	if (memcmp(model, &zero, len) != 0) {
46298c2ecf20Sopenharmony_ci		memcpy(ha->model_number, model, len);
46308c2ecf20Sopenharmony_ci		st = en = ha->model_number;
46318c2ecf20Sopenharmony_ci		en += len - 1;
46328c2ecf20Sopenharmony_ci		while (en > st) {
46338c2ecf20Sopenharmony_ci			if (*en != 0x20 && *en != 0x00)
46348c2ecf20Sopenharmony_ci				break;
46358c2ecf20Sopenharmony_ci			*en-- = '\0';
46368c2ecf20Sopenharmony_ci		}
46378c2ecf20Sopenharmony_ci
46388c2ecf20Sopenharmony_ci		index = (ha->pdev->subsystem_device & 0xff);
46398c2ecf20Sopenharmony_ci		if (use_tbl &&
46408c2ecf20Sopenharmony_ci		    ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
46418c2ecf20Sopenharmony_ci		    index < QLA_MODEL_NAMES)
46428c2ecf20Sopenharmony_ci			strlcpy(ha->model_desc,
46438c2ecf20Sopenharmony_ci			    qla2x00_model_name[index * 2 + 1],
46448c2ecf20Sopenharmony_ci			    sizeof(ha->model_desc));
46458c2ecf20Sopenharmony_ci	} else {
46468c2ecf20Sopenharmony_ci		index = (ha->pdev->subsystem_device & 0xff);
46478c2ecf20Sopenharmony_ci		if (use_tbl &&
46488c2ecf20Sopenharmony_ci		    ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
46498c2ecf20Sopenharmony_ci		    index < QLA_MODEL_NAMES) {
46508c2ecf20Sopenharmony_ci			strlcpy(ha->model_number,
46518c2ecf20Sopenharmony_ci				qla2x00_model_name[index * 2],
46528c2ecf20Sopenharmony_ci				sizeof(ha->model_number));
46538c2ecf20Sopenharmony_ci			strlcpy(ha->model_desc,
46548c2ecf20Sopenharmony_ci			    qla2x00_model_name[index * 2 + 1],
46558c2ecf20Sopenharmony_ci			    sizeof(ha->model_desc));
46568c2ecf20Sopenharmony_ci		} else {
46578c2ecf20Sopenharmony_ci			strlcpy(ha->model_number, def,
46588c2ecf20Sopenharmony_ci				sizeof(ha->model_number));
46598c2ecf20Sopenharmony_ci		}
46608c2ecf20Sopenharmony_ci	}
46618c2ecf20Sopenharmony_ci	if (IS_FWI2_CAPABLE(ha))
46628c2ecf20Sopenharmony_ci		qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
46638c2ecf20Sopenharmony_ci		    sizeof(ha->model_desc));
46648c2ecf20Sopenharmony_ci}
46658c2ecf20Sopenharmony_ci
46668c2ecf20Sopenharmony_ci/* On sparc systems, obtain port and node WWN from firmware
46678c2ecf20Sopenharmony_ci * properties.
46688c2ecf20Sopenharmony_ci */
46698c2ecf20Sopenharmony_cistatic void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
46708c2ecf20Sopenharmony_ci{
46718c2ecf20Sopenharmony_ci#ifdef CONFIG_SPARC
46728c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
46738c2ecf20Sopenharmony_ci	struct pci_dev *pdev = ha->pdev;
46748c2ecf20Sopenharmony_ci	struct device_node *dp = pci_device_to_OF_node(pdev);
46758c2ecf20Sopenharmony_ci	const u8 *val;
46768c2ecf20Sopenharmony_ci	int len;
46778c2ecf20Sopenharmony_ci
46788c2ecf20Sopenharmony_ci	val = of_get_property(dp, "port-wwn", &len);
46798c2ecf20Sopenharmony_ci	if (val && len >= WWN_SIZE)
46808c2ecf20Sopenharmony_ci		memcpy(nv->port_name, val, WWN_SIZE);
46818c2ecf20Sopenharmony_ci
46828c2ecf20Sopenharmony_ci	val = of_get_property(dp, "node-wwn", &len);
46838c2ecf20Sopenharmony_ci	if (val && len >= WWN_SIZE)
46848c2ecf20Sopenharmony_ci		memcpy(nv->node_name, val, WWN_SIZE);
46858c2ecf20Sopenharmony_ci#endif
46868c2ecf20Sopenharmony_ci}
46878c2ecf20Sopenharmony_ci
46888c2ecf20Sopenharmony_ci/*
46898c2ecf20Sopenharmony_ci* NVRAM configuration for ISP 2xxx
46908c2ecf20Sopenharmony_ci*
46918c2ecf20Sopenharmony_ci* Input:
46928c2ecf20Sopenharmony_ci*      ha                = adapter block pointer.
46938c2ecf20Sopenharmony_ci*
46948c2ecf20Sopenharmony_ci* Output:
46958c2ecf20Sopenharmony_ci*      initialization control block in response_ring
46968c2ecf20Sopenharmony_ci*      host adapters parameters in host adapter block
46978c2ecf20Sopenharmony_ci*
46988c2ecf20Sopenharmony_ci* Returns:
46998c2ecf20Sopenharmony_ci*      0 = success.
47008c2ecf20Sopenharmony_ci*/
47018c2ecf20Sopenharmony_ciint
47028c2ecf20Sopenharmony_ciqla2x00_nvram_config(scsi_qla_host_t *vha)
47038c2ecf20Sopenharmony_ci{
47048c2ecf20Sopenharmony_ci	int             rval;
47058c2ecf20Sopenharmony_ci	uint8_t         chksum = 0;
47068c2ecf20Sopenharmony_ci	uint16_t        cnt;
47078c2ecf20Sopenharmony_ci	uint8_t         *dptr1, *dptr2;
47088c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
47098c2ecf20Sopenharmony_ci	init_cb_t       *icb = ha->init_cb;
47108c2ecf20Sopenharmony_ci	nvram_t         *nv = ha->nvram;
47118c2ecf20Sopenharmony_ci	uint8_t         *ptr = ha->nvram;
47128c2ecf20Sopenharmony_ci	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
47138c2ecf20Sopenharmony_ci
47148c2ecf20Sopenharmony_ci	rval = QLA_SUCCESS;
47158c2ecf20Sopenharmony_ci
47168c2ecf20Sopenharmony_ci	/* Determine NVRAM starting address. */
47178c2ecf20Sopenharmony_ci	ha->nvram_size = sizeof(*nv);
47188c2ecf20Sopenharmony_ci	ha->nvram_base = 0;
47198c2ecf20Sopenharmony_ci	if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
47208c2ecf20Sopenharmony_ci		if ((rd_reg_word(&reg->ctrl_status) >> 14) == 1)
47218c2ecf20Sopenharmony_ci			ha->nvram_base = 0x80;
47228c2ecf20Sopenharmony_ci
47238c2ecf20Sopenharmony_ci	/* Get NVRAM data and calculate checksum. */
47248c2ecf20Sopenharmony_ci	ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
47258c2ecf20Sopenharmony_ci	for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
47268c2ecf20Sopenharmony_ci		chksum += *ptr++;
47278c2ecf20Sopenharmony_ci
47288c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
47298c2ecf20Sopenharmony_ci	    "Contents of NVRAM.\n");
47308c2ecf20Sopenharmony_ci	ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
47318c2ecf20Sopenharmony_ci	    nv, ha->nvram_size);
47328c2ecf20Sopenharmony_ci
47338c2ecf20Sopenharmony_ci	/* Bad NVRAM data, set defaults parameters. */
47348c2ecf20Sopenharmony_ci	if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
47358c2ecf20Sopenharmony_ci	    nv->nvram_version < 1) {
47368c2ecf20Sopenharmony_ci		/* Reset NVRAM data. */
47378c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0064,
47388c2ecf20Sopenharmony_ci		    "Inconsistent NVRAM detected: checksum=%#x id=%.4s version=%#x.\n",
47398c2ecf20Sopenharmony_ci		    chksum, nv->id, nv->nvram_version);
47408c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0065,
47418c2ecf20Sopenharmony_ci		    "Falling back to "
47428c2ecf20Sopenharmony_ci		    "functioning (yet invalid -- WWPN) defaults.\n");
47438c2ecf20Sopenharmony_ci
47448c2ecf20Sopenharmony_ci		/*
47458c2ecf20Sopenharmony_ci		 * Set default initialization control block.
47468c2ecf20Sopenharmony_ci		 */
47478c2ecf20Sopenharmony_ci		memset(nv, 0, ha->nvram_size);
47488c2ecf20Sopenharmony_ci		nv->parameter_block_version = ICB_VERSION;
47498c2ecf20Sopenharmony_ci
47508c2ecf20Sopenharmony_ci		if (IS_QLA23XX(ha)) {
47518c2ecf20Sopenharmony_ci			nv->firmware_options[0] = BIT_2 | BIT_1;
47528c2ecf20Sopenharmony_ci			nv->firmware_options[1] = BIT_7 | BIT_5;
47538c2ecf20Sopenharmony_ci			nv->add_firmware_options[0] = BIT_5;
47548c2ecf20Sopenharmony_ci			nv->add_firmware_options[1] = BIT_5 | BIT_4;
47558c2ecf20Sopenharmony_ci			nv->frame_payload_size = cpu_to_le16(2048);
47568c2ecf20Sopenharmony_ci			nv->special_options[1] = BIT_7;
47578c2ecf20Sopenharmony_ci		} else if (IS_QLA2200(ha)) {
47588c2ecf20Sopenharmony_ci			nv->firmware_options[0] = BIT_2 | BIT_1;
47598c2ecf20Sopenharmony_ci			nv->firmware_options[1] = BIT_7 | BIT_5;
47608c2ecf20Sopenharmony_ci			nv->add_firmware_options[0] = BIT_5;
47618c2ecf20Sopenharmony_ci			nv->add_firmware_options[1] = BIT_5 | BIT_4;
47628c2ecf20Sopenharmony_ci			nv->frame_payload_size = cpu_to_le16(1024);
47638c2ecf20Sopenharmony_ci		} else if (IS_QLA2100(ha)) {
47648c2ecf20Sopenharmony_ci			nv->firmware_options[0] = BIT_3 | BIT_1;
47658c2ecf20Sopenharmony_ci			nv->firmware_options[1] = BIT_5;
47668c2ecf20Sopenharmony_ci			nv->frame_payload_size = cpu_to_le16(1024);
47678c2ecf20Sopenharmony_ci		}
47688c2ecf20Sopenharmony_ci
47698c2ecf20Sopenharmony_ci		nv->max_iocb_allocation = cpu_to_le16(256);
47708c2ecf20Sopenharmony_ci		nv->execution_throttle = cpu_to_le16(16);
47718c2ecf20Sopenharmony_ci		nv->retry_count = 8;
47728c2ecf20Sopenharmony_ci		nv->retry_delay = 1;
47738c2ecf20Sopenharmony_ci
47748c2ecf20Sopenharmony_ci		nv->port_name[0] = 33;
47758c2ecf20Sopenharmony_ci		nv->port_name[3] = 224;
47768c2ecf20Sopenharmony_ci		nv->port_name[4] = 139;
47778c2ecf20Sopenharmony_ci
47788c2ecf20Sopenharmony_ci		qla2xxx_nvram_wwn_from_ofw(vha, nv);
47798c2ecf20Sopenharmony_ci
47808c2ecf20Sopenharmony_ci		nv->login_timeout = 4;
47818c2ecf20Sopenharmony_ci
47828c2ecf20Sopenharmony_ci		/*
47838c2ecf20Sopenharmony_ci		 * Set default host adapter parameters
47848c2ecf20Sopenharmony_ci		 */
47858c2ecf20Sopenharmony_ci		nv->host_p[1] = BIT_2;
47868c2ecf20Sopenharmony_ci		nv->reset_delay = 5;
47878c2ecf20Sopenharmony_ci		nv->port_down_retry_count = 8;
47888c2ecf20Sopenharmony_ci		nv->max_luns_per_target = cpu_to_le16(8);
47898c2ecf20Sopenharmony_ci		nv->link_down_timeout = 60;
47908c2ecf20Sopenharmony_ci
47918c2ecf20Sopenharmony_ci		rval = 1;
47928c2ecf20Sopenharmony_ci	}
47938c2ecf20Sopenharmony_ci
47948c2ecf20Sopenharmony_ci	/* Reset Initialization control block */
47958c2ecf20Sopenharmony_ci	memset(icb, 0, ha->init_cb_size);
47968c2ecf20Sopenharmony_ci
47978c2ecf20Sopenharmony_ci	/*
47988c2ecf20Sopenharmony_ci	 * Setup driver NVRAM options.
47998c2ecf20Sopenharmony_ci	 */
48008c2ecf20Sopenharmony_ci	nv->firmware_options[0] |= (BIT_6 | BIT_1);
48018c2ecf20Sopenharmony_ci	nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
48028c2ecf20Sopenharmony_ci	nv->firmware_options[1] |= (BIT_5 | BIT_0);
48038c2ecf20Sopenharmony_ci	nv->firmware_options[1] &= ~BIT_4;
48048c2ecf20Sopenharmony_ci
48058c2ecf20Sopenharmony_ci	if (IS_QLA23XX(ha)) {
48068c2ecf20Sopenharmony_ci		nv->firmware_options[0] |= BIT_2;
48078c2ecf20Sopenharmony_ci		nv->firmware_options[0] &= ~BIT_3;
48088c2ecf20Sopenharmony_ci		nv->special_options[0] &= ~BIT_6;
48098c2ecf20Sopenharmony_ci		nv->add_firmware_options[1] |= BIT_5 | BIT_4;
48108c2ecf20Sopenharmony_ci
48118c2ecf20Sopenharmony_ci		if (IS_QLA2300(ha)) {
48128c2ecf20Sopenharmony_ci			if (ha->fb_rev == FPM_2310) {
48138c2ecf20Sopenharmony_ci				strcpy(ha->model_number, "QLA2310");
48148c2ecf20Sopenharmony_ci			} else {
48158c2ecf20Sopenharmony_ci				strcpy(ha->model_number, "QLA2300");
48168c2ecf20Sopenharmony_ci			}
48178c2ecf20Sopenharmony_ci		} else {
48188c2ecf20Sopenharmony_ci			qla2x00_set_model_info(vha, nv->model_number,
48198c2ecf20Sopenharmony_ci			    sizeof(nv->model_number), "QLA23xx");
48208c2ecf20Sopenharmony_ci		}
48218c2ecf20Sopenharmony_ci	} else if (IS_QLA2200(ha)) {
48228c2ecf20Sopenharmony_ci		nv->firmware_options[0] |= BIT_2;
48238c2ecf20Sopenharmony_ci		/*
48248c2ecf20Sopenharmony_ci		 * 'Point-to-point preferred, else loop' is not a safe
48258c2ecf20Sopenharmony_ci		 * connection mode setting.
48268c2ecf20Sopenharmony_ci		 */
48278c2ecf20Sopenharmony_ci		if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
48288c2ecf20Sopenharmony_ci		    (BIT_5 | BIT_4)) {
48298c2ecf20Sopenharmony_ci			/* Force 'loop preferred, else point-to-point'. */
48308c2ecf20Sopenharmony_ci			nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
48318c2ecf20Sopenharmony_ci			nv->add_firmware_options[0] |= BIT_5;
48328c2ecf20Sopenharmony_ci		}
48338c2ecf20Sopenharmony_ci		strcpy(ha->model_number, "QLA22xx");
48348c2ecf20Sopenharmony_ci	} else /*if (IS_QLA2100(ha))*/ {
48358c2ecf20Sopenharmony_ci		strcpy(ha->model_number, "QLA2100");
48368c2ecf20Sopenharmony_ci	}
48378c2ecf20Sopenharmony_ci
48388c2ecf20Sopenharmony_ci	/*
48398c2ecf20Sopenharmony_ci	 * Copy over NVRAM RISC parameter block to initialization control block.
48408c2ecf20Sopenharmony_ci	 */
48418c2ecf20Sopenharmony_ci	dptr1 = (uint8_t *)icb;
48428c2ecf20Sopenharmony_ci	dptr2 = (uint8_t *)&nv->parameter_block_version;
48438c2ecf20Sopenharmony_ci	cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
48448c2ecf20Sopenharmony_ci	while (cnt--)
48458c2ecf20Sopenharmony_ci		*dptr1++ = *dptr2++;
48468c2ecf20Sopenharmony_ci
48478c2ecf20Sopenharmony_ci	/* Copy 2nd half. */
48488c2ecf20Sopenharmony_ci	dptr1 = (uint8_t *)icb->add_firmware_options;
48498c2ecf20Sopenharmony_ci	cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
48508c2ecf20Sopenharmony_ci	while (cnt--)
48518c2ecf20Sopenharmony_ci		*dptr1++ = *dptr2++;
48528c2ecf20Sopenharmony_ci	ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
48538c2ecf20Sopenharmony_ci	/* Use alternate WWN? */
48548c2ecf20Sopenharmony_ci	if (nv->host_p[1] & BIT_7) {
48558c2ecf20Sopenharmony_ci		memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
48568c2ecf20Sopenharmony_ci		memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
48578c2ecf20Sopenharmony_ci	}
48588c2ecf20Sopenharmony_ci
48598c2ecf20Sopenharmony_ci	/* Prepare nodename */
48608c2ecf20Sopenharmony_ci	if ((icb->firmware_options[1] & BIT_6) == 0) {
48618c2ecf20Sopenharmony_ci		/*
48628c2ecf20Sopenharmony_ci		 * Firmware will apply the following mask if the nodename was
48638c2ecf20Sopenharmony_ci		 * not provided.
48648c2ecf20Sopenharmony_ci		 */
48658c2ecf20Sopenharmony_ci		memcpy(icb->node_name, icb->port_name, WWN_SIZE);
48668c2ecf20Sopenharmony_ci		icb->node_name[0] &= 0xF0;
48678c2ecf20Sopenharmony_ci	}
48688c2ecf20Sopenharmony_ci
48698c2ecf20Sopenharmony_ci	/*
48708c2ecf20Sopenharmony_ci	 * Set host adapter parameters.
48718c2ecf20Sopenharmony_ci	 */
48728c2ecf20Sopenharmony_ci
48738c2ecf20Sopenharmony_ci	/*
48748c2ecf20Sopenharmony_ci	 * BIT_7 in the host-parameters section allows for modification to
48758c2ecf20Sopenharmony_ci	 * internal driver logging.
48768c2ecf20Sopenharmony_ci	 */
48778c2ecf20Sopenharmony_ci	if (nv->host_p[0] & BIT_7)
48788c2ecf20Sopenharmony_ci		ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
48798c2ecf20Sopenharmony_ci	ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
48808c2ecf20Sopenharmony_ci	/* Always load RISC code on non ISP2[12]00 chips. */
48818c2ecf20Sopenharmony_ci	if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
48828c2ecf20Sopenharmony_ci		ha->flags.disable_risc_code_load = 0;
48838c2ecf20Sopenharmony_ci	ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
48848c2ecf20Sopenharmony_ci	ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
48858c2ecf20Sopenharmony_ci	ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
48868c2ecf20Sopenharmony_ci	ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
48878c2ecf20Sopenharmony_ci	ha->flags.disable_serdes = 0;
48888c2ecf20Sopenharmony_ci
48898c2ecf20Sopenharmony_ci	ha->operating_mode =
48908c2ecf20Sopenharmony_ci	    (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
48918c2ecf20Sopenharmony_ci
48928c2ecf20Sopenharmony_ci	memcpy(ha->fw_seriallink_options, nv->seriallink_options,
48938c2ecf20Sopenharmony_ci	    sizeof(ha->fw_seriallink_options));
48948c2ecf20Sopenharmony_ci
48958c2ecf20Sopenharmony_ci	/* save HBA serial number */
48968c2ecf20Sopenharmony_ci	ha->serial0 = icb->port_name[5];
48978c2ecf20Sopenharmony_ci	ha->serial1 = icb->port_name[6];
48988c2ecf20Sopenharmony_ci	ha->serial2 = icb->port_name[7];
48998c2ecf20Sopenharmony_ci	memcpy(vha->node_name, icb->node_name, WWN_SIZE);
49008c2ecf20Sopenharmony_ci	memcpy(vha->port_name, icb->port_name, WWN_SIZE);
49018c2ecf20Sopenharmony_ci
49028c2ecf20Sopenharmony_ci	icb->execution_throttle = cpu_to_le16(0xFFFF);
49038c2ecf20Sopenharmony_ci
49048c2ecf20Sopenharmony_ci	ha->retry_count = nv->retry_count;
49058c2ecf20Sopenharmony_ci
49068c2ecf20Sopenharmony_ci	/* Set minimum login_timeout to 4 seconds. */
49078c2ecf20Sopenharmony_ci	if (nv->login_timeout != ql2xlogintimeout)
49088c2ecf20Sopenharmony_ci		nv->login_timeout = ql2xlogintimeout;
49098c2ecf20Sopenharmony_ci	if (nv->login_timeout < 4)
49108c2ecf20Sopenharmony_ci		nv->login_timeout = 4;
49118c2ecf20Sopenharmony_ci	ha->login_timeout = nv->login_timeout;
49128c2ecf20Sopenharmony_ci
49138c2ecf20Sopenharmony_ci	/* Set minimum RATOV to 100 tenths of a second. */
49148c2ecf20Sopenharmony_ci	ha->r_a_tov = 100;
49158c2ecf20Sopenharmony_ci
49168c2ecf20Sopenharmony_ci	ha->loop_reset_delay = nv->reset_delay;
49178c2ecf20Sopenharmony_ci
49188c2ecf20Sopenharmony_ci	/* Link Down Timeout = 0:
49198c2ecf20Sopenharmony_ci	 *
49208c2ecf20Sopenharmony_ci	 * 	When Port Down timer expires we will start returning
49218c2ecf20Sopenharmony_ci	 *	I/O's to OS with "DID_NO_CONNECT".
49228c2ecf20Sopenharmony_ci	 *
49238c2ecf20Sopenharmony_ci	 * Link Down Timeout != 0:
49248c2ecf20Sopenharmony_ci	 *
49258c2ecf20Sopenharmony_ci	 *	 The driver waits for the link to come up after link down
49268c2ecf20Sopenharmony_ci	 *	 before returning I/Os to OS with "DID_NO_CONNECT".
49278c2ecf20Sopenharmony_ci	 */
49288c2ecf20Sopenharmony_ci	if (nv->link_down_timeout == 0) {
49298c2ecf20Sopenharmony_ci		ha->loop_down_abort_time =
49308c2ecf20Sopenharmony_ci		    (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
49318c2ecf20Sopenharmony_ci	} else {
49328c2ecf20Sopenharmony_ci		ha->link_down_timeout =	 nv->link_down_timeout;
49338c2ecf20Sopenharmony_ci		ha->loop_down_abort_time =
49348c2ecf20Sopenharmony_ci		    (LOOP_DOWN_TIME - ha->link_down_timeout);
49358c2ecf20Sopenharmony_ci	}
49368c2ecf20Sopenharmony_ci
49378c2ecf20Sopenharmony_ci	/*
49388c2ecf20Sopenharmony_ci	 * Need enough time to try and get the port back.
49398c2ecf20Sopenharmony_ci	 */
49408c2ecf20Sopenharmony_ci	ha->port_down_retry_count = nv->port_down_retry_count;
49418c2ecf20Sopenharmony_ci	if (qlport_down_retry)
49428c2ecf20Sopenharmony_ci		ha->port_down_retry_count = qlport_down_retry;
49438c2ecf20Sopenharmony_ci	/* Set login_retry_count */
49448c2ecf20Sopenharmony_ci	ha->login_retry_count  = nv->retry_count;
49458c2ecf20Sopenharmony_ci	if (ha->port_down_retry_count == nv->port_down_retry_count &&
49468c2ecf20Sopenharmony_ci	    ha->port_down_retry_count > 3)
49478c2ecf20Sopenharmony_ci		ha->login_retry_count = ha->port_down_retry_count;
49488c2ecf20Sopenharmony_ci	else if (ha->port_down_retry_count > (int)ha->login_retry_count)
49498c2ecf20Sopenharmony_ci		ha->login_retry_count = ha->port_down_retry_count;
49508c2ecf20Sopenharmony_ci	if (ql2xloginretrycount)
49518c2ecf20Sopenharmony_ci		ha->login_retry_count = ql2xloginretrycount;
49528c2ecf20Sopenharmony_ci
49538c2ecf20Sopenharmony_ci	icb->lun_enables = cpu_to_le16(0);
49548c2ecf20Sopenharmony_ci	icb->command_resource_count = 0;
49558c2ecf20Sopenharmony_ci	icb->immediate_notify_resource_count = 0;
49568c2ecf20Sopenharmony_ci	icb->timeout = cpu_to_le16(0);
49578c2ecf20Sopenharmony_ci
49588c2ecf20Sopenharmony_ci	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
49598c2ecf20Sopenharmony_ci		/* Enable RIO */
49608c2ecf20Sopenharmony_ci		icb->firmware_options[0] &= ~BIT_3;
49618c2ecf20Sopenharmony_ci		icb->add_firmware_options[0] &=
49628c2ecf20Sopenharmony_ci		    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
49638c2ecf20Sopenharmony_ci		icb->add_firmware_options[0] |= BIT_2;
49648c2ecf20Sopenharmony_ci		icb->response_accumulation_timer = 3;
49658c2ecf20Sopenharmony_ci		icb->interrupt_delay_timer = 5;
49668c2ecf20Sopenharmony_ci
49678c2ecf20Sopenharmony_ci		vha->flags.process_response_queue = 1;
49688c2ecf20Sopenharmony_ci	} else {
49698c2ecf20Sopenharmony_ci		/* Enable ZIO. */
49708c2ecf20Sopenharmony_ci		if (!vha->flags.init_done) {
49718c2ecf20Sopenharmony_ci			ha->zio_mode = icb->add_firmware_options[0] &
49728c2ecf20Sopenharmony_ci			    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
49738c2ecf20Sopenharmony_ci			ha->zio_timer = icb->interrupt_delay_timer ?
49748c2ecf20Sopenharmony_ci			    icb->interrupt_delay_timer : 2;
49758c2ecf20Sopenharmony_ci		}
49768c2ecf20Sopenharmony_ci		icb->add_firmware_options[0] &=
49778c2ecf20Sopenharmony_ci		    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
49788c2ecf20Sopenharmony_ci		vha->flags.process_response_queue = 0;
49798c2ecf20Sopenharmony_ci		if (ha->zio_mode != QLA_ZIO_DISABLED) {
49808c2ecf20Sopenharmony_ci			ha->zio_mode = QLA_ZIO_MODE_6;
49818c2ecf20Sopenharmony_ci
49828c2ecf20Sopenharmony_ci			ql_log(ql_log_info, vha, 0x0068,
49838c2ecf20Sopenharmony_ci			    "ZIO mode %d enabled; timer delay (%d us).\n",
49848c2ecf20Sopenharmony_ci			    ha->zio_mode, ha->zio_timer * 100);
49858c2ecf20Sopenharmony_ci
49868c2ecf20Sopenharmony_ci			icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
49878c2ecf20Sopenharmony_ci			icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
49888c2ecf20Sopenharmony_ci			vha->flags.process_response_queue = 1;
49898c2ecf20Sopenharmony_ci		}
49908c2ecf20Sopenharmony_ci	}
49918c2ecf20Sopenharmony_ci
49928c2ecf20Sopenharmony_ci	if (rval) {
49938c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0069,
49948c2ecf20Sopenharmony_ci		    "NVRAM configuration failed.\n");
49958c2ecf20Sopenharmony_ci	}
49968c2ecf20Sopenharmony_ci	return (rval);
49978c2ecf20Sopenharmony_ci}
49988c2ecf20Sopenharmony_ci
49998c2ecf20Sopenharmony_cistatic void
50008c2ecf20Sopenharmony_ciqla2x00_rport_del(void *data)
50018c2ecf20Sopenharmony_ci{
50028c2ecf20Sopenharmony_ci	fc_port_t *fcport = data;
50038c2ecf20Sopenharmony_ci	struct fc_rport *rport;
50048c2ecf20Sopenharmony_ci	unsigned long flags;
50058c2ecf20Sopenharmony_ci
50068c2ecf20Sopenharmony_ci	spin_lock_irqsave(fcport->vha->host->host_lock, flags);
50078c2ecf20Sopenharmony_ci	rport = fcport->drport ? fcport->drport : fcport->rport;
50088c2ecf20Sopenharmony_ci	fcport->drport = NULL;
50098c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
50108c2ecf20Sopenharmony_ci	if (rport) {
50118c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
50128c2ecf20Sopenharmony_ci		    "%s %8phN. rport %p roles %x\n",
50138c2ecf20Sopenharmony_ci		    __func__, fcport->port_name, rport,
50148c2ecf20Sopenharmony_ci		    rport->roles);
50158c2ecf20Sopenharmony_ci
50168c2ecf20Sopenharmony_ci		fc_remote_port_delete(rport);
50178c2ecf20Sopenharmony_ci	}
50188c2ecf20Sopenharmony_ci}
50198c2ecf20Sopenharmony_ci
50208c2ecf20Sopenharmony_civoid qla2x00_set_fcport_state(fc_port_t *fcport, int state)
50218c2ecf20Sopenharmony_ci{
50228c2ecf20Sopenharmony_ci	int old_state;
50238c2ecf20Sopenharmony_ci
50248c2ecf20Sopenharmony_ci	old_state = atomic_read(&fcport->state);
50258c2ecf20Sopenharmony_ci	atomic_set(&fcport->state, state);
50268c2ecf20Sopenharmony_ci
50278c2ecf20Sopenharmony_ci	/* Don't print state transitions during initial allocation of fcport */
50288c2ecf20Sopenharmony_ci	if (old_state && old_state != state) {
50298c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, fcport->vha, 0x207d,
50308c2ecf20Sopenharmony_ci		       "FCPort %8phC state transitioned from %s to %s - portid=%02x%02x%02x.\n",
50318c2ecf20Sopenharmony_ci		       fcport->port_name, port_state_str[old_state],
50328c2ecf20Sopenharmony_ci		       port_state_str[state], fcport->d_id.b.domain,
50338c2ecf20Sopenharmony_ci		       fcport->d_id.b.area, fcport->d_id.b.al_pa);
50348c2ecf20Sopenharmony_ci	}
50358c2ecf20Sopenharmony_ci}
50368c2ecf20Sopenharmony_ci
50378c2ecf20Sopenharmony_ci/**
50388c2ecf20Sopenharmony_ci * qla2x00_alloc_fcport() - Allocate a generic fcport.
50398c2ecf20Sopenharmony_ci * @vha: HA context
50408c2ecf20Sopenharmony_ci * @flags: allocation flags
50418c2ecf20Sopenharmony_ci *
50428c2ecf20Sopenharmony_ci * Returns a pointer to the allocated fcport, or NULL, if none available.
50438c2ecf20Sopenharmony_ci */
50448c2ecf20Sopenharmony_cifc_port_t *
50458c2ecf20Sopenharmony_ciqla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
50468c2ecf20Sopenharmony_ci{
50478c2ecf20Sopenharmony_ci	fc_port_t *fcport;
50488c2ecf20Sopenharmony_ci
50498c2ecf20Sopenharmony_ci	fcport = kzalloc(sizeof(fc_port_t), flags);
50508c2ecf20Sopenharmony_ci	if (!fcport)
50518c2ecf20Sopenharmony_ci		return NULL;
50528c2ecf20Sopenharmony_ci
50538c2ecf20Sopenharmony_ci	fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
50548c2ecf20Sopenharmony_ci		sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
50558c2ecf20Sopenharmony_ci		flags);
50568c2ecf20Sopenharmony_ci	if (!fcport->ct_desc.ct_sns) {
50578c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xd049,
50588c2ecf20Sopenharmony_ci		    "Failed to allocate ct_sns request.\n");
50598c2ecf20Sopenharmony_ci		kfree(fcport);
50608c2ecf20Sopenharmony_ci		return NULL;
50618c2ecf20Sopenharmony_ci	}
50628c2ecf20Sopenharmony_ci
50638c2ecf20Sopenharmony_ci	/* Setup fcport template structure. */
50648c2ecf20Sopenharmony_ci	fcport->vha = vha;
50658c2ecf20Sopenharmony_ci	fcport->port_type = FCT_UNKNOWN;
50668c2ecf20Sopenharmony_ci	fcport->loop_id = FC_NO_LOOP_ID;
50678c2ecf20Sopenharmony_ci	qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
50688c2ecf20Sopenharmony_ci	fcport->supported_classes = FC_COS_UNSPECIFIED;
50698c2ecf20Sopenharmony_ci	fcport->fp_speed = PORT_SPEED_UNKNOWN;
50708c2ecf20Sopenharmony_ci
50718c2ecf20Sopenharmony_ci	fcport->disc_state = DSC_DELETED;
50728c2ecf20Sopenharmony_ci	fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
50738c2ecf20Sopenharmony_ci	fcport->deleted = QLA_SESS_DELETED;
50748c2ecf20Sopenharmony_ci	fcport->login_retry = vha->hw->login_retry_count;
50758c2ecf20Sopenharmony_ci	fcport->chip_reset = vha->hw->base_qpair->chip_reset;
50768c2ecf20Sopenharmony_ci	fcport->logout_on_delete = 1;
50778c2ecf20Sopenharmony_ci
50788c2ecf20Sopenharmony_ci	if (!fcport->ct_desc.ct_sns) {
50798c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xd049,
50808c2ecf20Sopenharmony_ci		    "Failed to allocate ct_sns request.\n");
50818c2ecf20Sopenharmony_ci		kfree(fcport);
50828c2ecf20Sopenharmony_ci		return NULL;
50838c2ecf20Sopenharmony_ci	}
50848c2ecf20Sopenharmony_ci
50858c2ecf20Sopenharmony_ci	INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
50868c2ecf20Sopenharmony_ci	INIT_WORK(&fcport->free_work, qlt_free_session_done);
50878c2ecf20Sopenharmony_ci	INIT_WORK(&fcport->reg_work, qla_register_fcport_fn);
50888c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&fcport->gnl_entry);
50898c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&fcport->list);
50908c2ecf20Sopenharmony_ci
50918c2ecf20Sopenharmony_ci	return fcport;
50928c2ecf20Sopenharmony_ci}
50938c2ecf20Sopenharmony_ci
50948c2ecf20Sopenharmony_civoid
50958c2ecf20Sopenharmony_ciqla2x00_free_fcport(fc_port_t *fcport)
50968c2ecf20Sopenharmony_ci{
50978c2ecf20Sopenharmony_ci	if (fcport->ct_desc.ct_sns) {
50988c2ecf20Sopenharmony_ci		dma_free_coherent(&fcport->vha->hw->pdev->dev,
50998c2ecf20Sopenharmony_ci			sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
51008c2ecf20Sopenharmony_ci			fcport->ct_desc.ct_sns_dma);
51018c2ecf20Sopenharmony_ci
51028c2ecf20Sopenharmony_ci		fcport->ct_desc.ct_sns = NULL;
51038c2ecf20Sopenharmony_ci	}
51048c2ecf20Sopenharmony_ci	list_del(&fcport->list);
51058c2ecf20Sopenharmony_ci	qla2x00_clear_loop_id(fcport);
51068c2ecf20Sopenharmony_ci	kfree(fcport);
51078c2ecf20Sopenharmony_ci}
51088c2ecf20Sopenharmony_ci
51098c2ecf20Sopenharmony_cistatic void qla_get_login_template(scsi_qla_host_t *vha)
51108c2ecf20Sopenharmony_ci{
51118c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
51128c2ecf20Sopenharmony_ci	int rval;
51138c2ecf20Sopenharmony_ci	u32 *bp, sz;
51148c2ecf20Sopenharmony_ci	__be32 *q;
51158c2ecf20Sopenharmony_ci
51168c2ecf20Sopenharmony_ci	memset(ha->init_cb, 0, ha->init_cb_size);
51178c2ecf20Sopenharmony_ci	sz = min_t(int, sizeof(struct fc_els_flogi), ha->init_cb_size);
51188c2ecf20Sopenharmony_ci	rval = qla24xx_get_port_login_templ(vha, ha->init_cb_dma,
51198c2ecf20Sopenharmony_ci					    ha->init_cb, sz);
51208c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
51218c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x00d1,
51228c2ecf20Sopenharmony_ci		       "PLOGI ELS param read fail.\n");
51238c2ecf20Sopenharmony_ci		return;
51248c2ecf20Sopenharmony_ci	}
51258c2ecf20Sopenharmony_ci	q = (__be32 *)&ha->plogi_els_payld.fl_csp;
51268c2ecf20Sopenharmony_ci
51278c2ecf20Sopenharmony_ci	bp = (uint32_t *)ha->init_cb;
51288c2ecf20Sopenharmony_ci	cpu_to_be32_array(q, bp, sz / 4);
51298c2ecf20Sopenharmony_ci	ha->flags.plogi_template_valid = 1;
51308c2ecf20Sopenharmony_ci}
51318c2ecf20Sopenharmony_ci
51328c2ecf20Sopenharmony_ci/*
51338c2ecf20Sopenharmony_ci * qla2x00_configure_loop
51348c2ecf20Sopenharmony_ci *      Updates Fibre Channel Device Database with what is actually on loop.
51358c2ecf20Sopenharmony_ci *
51368c2ecf20Sopenharmony_ci * Input:
51378c2ecf20Sopenharmony_ci *      ha                = adapter block pointer.
51388c2ecf20Sopenharmony_ci *
51398c2ecf20Sopenharmony_ci * Returns:
51408c2ecf20Sopenharmony_ci *      0 = success.
51418c2ecf20Sopenharmony_ci *      1 = error.
51428c2ecf20Sopenharmony_ci *      2 = database was full and device was not configured.
51438c2ecf20Sopenharmony_ci */
51448c2ecf20Sopenharmony_cistatic int
51458c2ecf20Sopenharmony_ciqla2x00_configure_loop(scsi_qla_host_t *vha)
51468c2ecf20Sopenharmony_ci{
51478c2ecf20Sopenharmony_ci	int  rval;
51488c2ecf20Sopenharmony_ci	unsigned long flags, save_flags;
51498c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
51508c2ecf20Sopenharmony_ci
51518c2ecf20Sopenharmony_ci	rval = QLA_SUCCESS;
51528c2ecf20Sopenharmony_ci
51538c2ecf20Sopenharmony_ci	/* Get Initiator ID */
51548c2ecf20Sopenharmony_ci	if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
51558c2ecf20Sopenharmony_ci		rval = qla2x00_configure_hba(vha);
51568c2ecf20Sopenharmony_ci		if (rval != QLA_SUCCESS) {
51578c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2013,
51588c2ecf20Sopenharmony_ci			    "Unable to configure HBA.\n");
51598c2ecf20Sopenharmony_ci			return (rval);
51608c2ecf20Sopenharmony_ci		}
51618c2ecf20Sopenharmony_ci	}
51628c2ecf20Sopenharmony_ci
51638c2ecf20Sopenharmony_ci	save_flags = flags = vha->dpc_flags;
51648c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2014,
51658c2ecf20Sopenharmony_ci	    "Configure loop -- dpc flags = 0x%lx.\n", flags);
51668c2ecf20Sopenharmony_ci
51678c2ecf20Sopenharmony_ci	/*
51688c2ecf20Sopenharmony_ci	 * If we have both an RSCN and PORT UPDATE pending then handle them
51698c2ecf20Sopenharmony_ci	 * both at the same time.
51708c2ecf20Sopenharmony_ci	 */
51718c2ecf20Sopenharmony_ci	clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
51728c2ecf20Sopenharmony_ci	clear_bit(RSCN_UPDATE, &vha->dpc_flags);
51738c2ecf20Sopenharmony_ci
51748c2ecf20Sopenharmony_ci	qla2x00_get_data_rate(vha);
51758c2ecf20Sopenharmony_ci	qla_get_login_template(vha);
51768c2ecf20Sopenharmony_ci
51778c2ecf20Sopenharmony_ci	/* Determine what we need to do */
51788c2ecf20Sopenharmony_ci	if ((ha->current_topology == ISP_CFG_FL ||
51798c2ecf20Sopenharmony_ci	    ha->current_topology == ISP_CFG_F) &&
51808c2ecf20Sopenharmony_ci	    (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
51818c2ecf20Sopenharmony_ci
51828c2ecf20Sopenharmony_ci		set_bit(RSCN_UPDATE, &flags);
51838c2ecf20Sopenharmony_ci		clear_bit(LOCAL_LOOP_UPDATE, &flags);
51848c2ecf20Sopenharmony_ci
51858c2ecf20Sopenharmony_ci	} else if (ha->current_topology == ISP_CFG_NL ||
51868c2ecf20Sopenharmony_ci		   ha->current_topology == ISP_CFG_N) {
51878c2ecf20Sopenharmony_ci		clear_bit(RSCN_UPDATE, &flags);
51888c2ecf20Sopenharmony_ci		set_bit(LOCAL_LOOP_UPDATE, &flags);
51898c2ecf20Sopenharmony_ci	} else if (!vha->flags.online ||
51908c2ecf20Sopenharmony_ci	    (test_bit(ABORT_ISP_ACTIVE, &flags))) {
51918c2ecf20Sopenharmony_ci		set_bit(RSCN_UPDATE, &flags);
51928c2ecf20Sopenharmony_ci		set_bit(LOCAL_LOOP_UPDATE, &flags);
51938c2ecf20Sopenharmony_ci	}
51948c2ecf20Sopenharmony_ci
51958c2ecf20Sopenharmony_ci	if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
51968c2ecf20Sopenharmony_ci		if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
51978c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2015,
51988c2ecf20Sopenharmony_ci			    "Loop resync needed, failing.\n");
51998c2ecf20Sopenharmony_ci			rval = QLA_FUNCTION_FAILED;
52008c2ecf20Sopenharmony_ci		} else
52018c2ecf20Sopenharmony_ci			rval = qla2x00_configure_local_loop(vha);
52028c2ecf20Sopenharmony_ci	}
52038c2ecf20Sopenharmony_ci
52048c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
52058c2ecf20Sopenharmony_ci		if (LOOP_TRANSITION(vha)) {
52068c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2099,
52078c2ecf20Sopenharmony_ci			    "Needs RSCN update and loop transition.\n");
52088c2ecf20Sopenharmony_ci			rval = QLA_FUNCTION_FAILED;
52098c2ecf20Sopenharmony_ci		}
52108c2ecf20Sopenharmony_ci		else
52118c2ecf20Sopenharmony_ci			rval = qla2x00_configure_fabric(vha);
52128c2ecf20Sopenharmony_ci	}
52138c2ecf20Sopenharmony_ci
52148c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS) {
52158c2ecf20Sopenharmony_ci		if (atomic_read(&vha->loop_down_timer) ||
52168c2ecf20Sopenharmony_ci		    test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
52178c2ecf20Sopenharmony_ci			rval = QLA_FUNCTION_FAILED;
52188c2ecf20Sopenharmony_ci		} else {
52198c2ecf20Sopenharmony_ci			atomic_set(&vha->loop_state, LOOP_READY);
52208c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2069,
52218c2ecf20Sopenharmony_ci			    "LOOP READY.\n");
52228c2ecf20Sopenharmony_ci			ha->flags.fw_init_done = 1;
52238c2ecf20Sopenharmony_ci
52248c2ecf20Sopenharmony_ci			/*
52258c2ecf20Sopenharmony_ci			 * Process any ATIO queue entries that came in
52268c2ecf20Sopenharmony_ci			 * while we weren't online.
52278c2ecf20Sopenharmony_ci			 */
52288c2ecf20Sopenharmony_ci			if (qla_tgt_mode_enabled(vha) ||
52298c2ecf20Sopenharmony_ci			    qla_dual_mode_enabled(vha)) {
52308c2ecf20Sopenharmony_ci				spin_lock_irqsave(&ha->tgt.atio_lock, flags);
52318c2ecf20Sopenharmony_ci				qlt_24xx_process_atio_queue(vha, 0);
52328c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&ha->tgt.atio_lock,
52338c2ecf20Sopenharmony_ci				    flags);
52348c2ecf20Sopenharmony_ci			}
52358c2ecf20Sopenharmony_ci		}
52368c2ecf20Sopenharmony_ci	}
52378c2ecf20Sopenharmony_ci
52388c2ecf20Sopenharmony_ci	if (rval) {
52398c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x206a,
52408c2ecf20Sopenharmony_ci		    "%s *** FAILED ***.\n", __func__);
52418c2ecf20Sopenharmony_ci	} else {
52428c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x206b,
52438c2ecf20Sopenharmony_ci		    "%s: exiting normally.\n", __func__);
52448c2ecf20Sopenharmony_ci	}
52458c2ecf20Sopenharmony_ci
52468c2ecf20Sopenharmony_ci	/* Restore state if a resync event occurred during processing */
52478c2ecf20Sopenharmony_ci	if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
52488c2ecf20Sopenharmony_ci		if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
52498c2ecf20Sopenharmony_ci			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
52508c2ecf20Sopenharmony_ci		if (test_bit(RSCN_UPDATE, &save_flags)) {
52518c2ecf20Sopenharmony_ci			set_bit(RSCN_UPDATE, &vha->dpc_flags);
52528c2ecf20Sopenharmony_ci		}
52538c2ecf20Sopenharmony_ci	}
52548c2ecf20Sopenharmony_ci
52558c2ecf20Sopenharmony_ci	return (rval);
52568c2ecf20Sopenharmony_ci}
52578c2ecf20Sopenharmony_ci
52588c2ecf20Sopenharmony_cistatic int qla2x00_configure_n2n_loop(scsi_qla_host_t *vha)
52598c2ecf20Sopenharmony_ci{
52608c2ecf20Sopenharmony_ci	unsigned long flags;
52618c2ecf20Sopenharmony_ci	fc_port_t *fcport;
52628c2ecf20Sopenharmony_ci
52638c2ecf20Sopenharmony_ci	if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags))
52648c2ecf20Sopenharmony_ci		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
52658c2ecf20Sopenharmony_ci
52668c2ecf20Sopenharmony_ci	list_for_each_entry(fcport, &vha->vp_fcports, list) {
52678c2ecf20Sopenharmony_ci		if (fcport->n2n_flag) {
52688c2ecf20Sopenharmony_ci			qla24xx_fcport_handle_login(vha, fcport);
52698c2ecf20Sopenharmony_ci			return QLA_SUCCESS;
52708c2ecf20Sopenharmony_ci		}
52718c2ecf20Sopenharmony_ci	}
52728c2ecf20Sopenharmony_ci
52738c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vha->work_lock, flags);
52748c2ecf20Sopenharmony_ci	vha->scan.scan_retry++;
52758c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vha->work_lock, flags);
52768c2ecf20Sopenharmony_ci
52778c2ecf20Sopenharmony_ci	if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
52788c2ecf20Sopenharmony_ci		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
52798c2ecf20Sopenharmony_ci		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
52808c2ecf20Sopenharmony_ci	}
52818c2ecf20Sopenharmony_ci	return QLA_FUNCTION_FAILED;
52828c2ecf20Sopenharmony_ci}
52838c2ecf20Sopenharmony_ci
52848c2ecf20Sopenharmony_cistatic void
52858c2ecf20Sopenharmony_ciqla_reinitialize_link(scsi_qla_host_t *vha)
52868c2ecf20Sopenharmony_ci{
52878c2ecf20Sopenharmony_ci	int rval;
52888c2ecf20Sopenharmony_ci
52898c2ecf20Sopenharmony_ci	atomic_set(&vha->loop_state, LOOP_DOWN);
52908c2ecf20Sopenharmony_ci	atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
52918c2ecf20Sopenharmony_ci	rval = qla2x00_full_login_lip(vha);
52928c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS) {
52938c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0xd050, "Link reinitialized\n");
52948c2ecf20Sopenharmony_ci	} else {
52958c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0xd051,
52968c2ecf20Sopenharmony_ci			"Link reinitialization failed (%d)\n", rval);
52978c2ecf20Sopenharmony_ci	}
52988c2ecf20Sopenharmony_ci}
52998c2ecf20Sopenharmony_ci
53008c2ecf20Sopenharmony_ci/*
53018c2ecf20Sopenharmony_ci * qla2x00_configure_local_loop
53028c2ecf20Sopenharmony_ci *	Updates Fibre Channel Device Database with local loop devices.
53038c2ecf20Sopenharmony_ci *
53048c2ecf20Sopenharmony_ci * Input:
53058c2ecf20Sopenharmony_ci *	ha = adapter block pointer.
53068c2ecf20Sopenharmony_ci *
53078c2ecf20Sopenharmony_ci * Returns:
53088c2ecf20Sopenharmony_ci *	0 = success.
53098c2ecf20Sopenharmony_ci */
53108c2ecf20Sopenharmony_cistatic int
53118c2ecf20Sopenharmony_ciqla2x00_configure_local_loop(scsi_qla_host_t *vha)
53128c2ecf20Sopenharmony_ci{
53138c2ecf20Sopenharmony_ci	int		rval, rval2;
53148c2ecf20Sopenharmony_ci	int		found_devs;
53158c2ecf20Sopenharmony_ci	int		found;
53168c2ecf20Sopenharmony_ci	fc_port_t	*fcport, *new_fcport;
53178c2ecf20Sopenharmony_ci	uint16_t	index;
53188c2ecf20Sopenharmony_ci	uint16_t	entries;
53198c2ecf20Sopenharmony_ci	struct gid_list_info *gid;
53208c2ecf20Sopenharmony_ci	uint16_t	loop_id;
53218c2ecf20Sopenharmony_ci	uint8_t		domain, area, al_pa;
53228c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
53238c2ecf20Sopenharmony_ci	unsigned long flags;
53248c2ecf20Sopenharmony_ci
53258c2ecf20Sopenharmony_ci	/* Inititae N2N login. */
53268c2ecf20Sopenharmony_ci	if (N2N_TOPO(ha))
53278c2ecf20Sopenharmony_ci		return qla2x00_configure_n2n_loop(vha);
53288c2ecf20Sopenharmony_ci
53298c2ecf20Sopenharmony_ci	found_devs = 0;
53308c2ecf20Sopenharmony_ci	new_fcport = NULL;
53318c2ecf20Sopenharmony_ci	entries = MAX_FIBRE_DEVICES_LOOP;
53328c2ecf20Sopenharmony_ci
53338c2ecf20Sopenharmony_ci	/* Get list of logged in devices. */
53348c2ecf20Sopenharmony_ci	memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
53358c2ecf20Sopenharmony_ci	rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
53368c2ecf20Sopenharmony_ci	    &entries);
53378c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS)
53388c2ecf20Sopenharmony_ci		goto err;
53398c2ecf20Sopenharmony_ci
53408c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2011,
53418c2ecf20Sopenharmony_ci	    "Entries in ID list (%d).\n", entries);
53428c2ecf20Sopenharmony_ci	ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
53438c2ecf20Sopenharmony_ci	    ha->gid_list, entries * sizeof(*ha->gid_list));
53448c2ecf20Sopenharmony_ci
53458c2ecf20Sopenharmony_ci	if (entries == 0) {
53468c2ecf20Sopenharmony_ci		spin_lock_irqsave(&vha->work_lock, flags);
53478c2ecf20Sopenharmony_ci		vha->scan.scan_retry++;
53488c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vha->work_lock, flags);
53498c2ecf20Sopenharmony_ci
53508c2ecf20Sopenharmony_ci		if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
53518c2ecf20Sopenharmony_ci			u8 loop_map_entries = 0;
53528c2ecf20Sopenharmony_ci			int rc;
53538c2ecf20Sopenharmony_ci
53548c2ecf20Sopenharmony_ci			rc = qla2x00_get_fcal_position_map(vha, NULL,
53558c2ecf20Sopenharmony_ci						&loop_map_entries);
53568c2ecf20Sopenharmony_ci			if (rc == QLA_SUCCESS && loop_map_entries > 1) {
53578c2ecf20Sopenharmony_ci				/*
53588c2ecf20Sopenharmony_ci				 * There are devices that are still not logged
53598c2ecf20Sopenharmony_ci				 * in. Reinitialize to give them a chance.
53608c2ecf20Sopenharmony_ci				 */
53618c2ecf20Sopenharmony_ci				qla_reinitialize_link(vha);
53628c2ecf20Sopenharmony_ci				return QLA_FUNCTION_FAILED;
53638c2ecf20Sopenharmony_ci			}
53648c2ecf20Sopenharmony_ci			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
53658c2ecf20Sopenharmony_ci			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
53668c2ecf20Sopenharmony_ci		}
53678c2ecf20Sopenharmony_ci	} else {
53688c2ecf20Sopenharmony_ci		vha->scan.scan_retry = 0;
53698c2ecf20Sopenharmony_ci	}
53708c2ecf20Sopenharmony_ci
53718c2ecf20Sopenharmony_ci	list_for_each_entry(fcport, &vha->vp_fcports, list) {
53728c2ecf20Sopenharmony_ci		fcport->scan_state = QLA_FCPORT_SCAN;
53738c2ecf20Sopenharmony_ci	}
53748c2ecf20Sopenharmony_ci
53758c2ecf20Sopenharmony_ci	/* Allocate temporary fcport for any new fcports discovered. */
53768c2ecf20Sopenharmony_ci	new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
53778c2ecf20Sopenharmony_ci	if (new_fcport == NULL) {
53788c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x2012,
53798c2ecf20Sopenharmony_ci		    "Memory allocation failed for fcport.\n");
53808c2ecf20Sopenharmony_ci		rval = QLA_MEMORY_ALLOC_FAILED;
53818c2ecf20Sopenharmony_ci		goto err;
53828c2ecf20Sopenharmony_ci	}
53838c2ecf20Sopenharmony_ci	new_fcport->flags &= ~FCF_FABRIC_DEVICE;
53848c2ecf20Sopenharmony_ci
53858c2ecf20Sopenharmony_ci	/* Add devices to port list. */
53868c2ecf20Sopenharmony_ci	gid = ha->gid_list;
53878c2ecf20Sopenharmony_ci	for (index = 0; index < entries; index++) {
53888c2ecf20Sopenharmony_ci		domain = gid->domain;
53898c2ecf20Sopenharmony_ci		area = gid->area;
53908c2ecf20Sopenharmony_ci		al_pa = gid->al_pa;
53918c2ecf20Sopenharmony_ci		if (IS_QLA2100(ha) || IS_QLA2200(ha))
53928c2ecf20Sopenharmony_ci			loop_id = gid->loop_id_2100;
53938c2ecf20Sopenharmony_ci		else
53948c2ecf20Sopenharmony_ci			loop_id = le16_to_cpu(gid->loop_id);
53958c2ecf20Sopenharmony_ci		gid = (void *)gid + ha->gid_list_info_size;
53968c2ecf20Sopenharmony_ci
53978c2ecf20Sopenharmony_ci		/* Bypass reserved domain fields. */
53988c2ecf20Sopenharmony_ci		if ((domain & 0xf0) == 0xf0)
53998c2ecf20Sopenharmony_ci			continue;
54008c2ecf20Sopenharmony_ci
54018c2ecf20Sopenharmony_ci		/* Bypass if not same domain and area of adapter. */
54028c2ecf20Sopenharmony_ci		if (area && domain && ((area != vha->d_id.b.area) ||
54038c2ecf20Sopenharmony_ci		    (domain != vha->d_id.b.domain)) &&
54048c2ecf20Sopenharmony_ci		    (ha->current_topology == ISP_CFG_NL))
54058c2ecf20Sopenharmony_ci			continue;
54068c2ecf20Sopenharmony_ci
54078c2ecf20Sopenharmony_ci
54088c2ecf20Sopenharmony_ci		/* Bypass invalid local loop ID. */
54098c2ecf20Sopenharmony_ci		if (loop_id > LAST_LOCAL_LOOP_ID)
54108c2ecf20Sopenharmony_ci			continue;
54118c2ecf20Sopenharmony_ci
54128c2ecf20Sopenharmony_ci		memset(new_fcport->port_name, 0, WWN_SIZE);
54138c2ecf20Sopenharmony_ci
54148c2ecf20Sopenharmony_ci		/* Fill in member data. */
54158c2ecf20Sopenharmony_ci		new_fcport->d_id.b.domain = domain;
54168c2ecf20Sopenharmony_ci		new_fcport->d_id.b.area = area;
54178c2ecf20Sopenharmony_ci		new_fcport->d_id.b.al_pa = al_pa;
54188c2ecf20Sopenharmony_ci		new_fcport->loop_id = loop_id;
54198c2ecf20Sopenharmony_ci		new_fcport->scan_state = QLA_FCPORT_FOUND;
54208c2ecf20Sopenharmony_ci
54218c2ecf20Sopenharmony_ci		rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
54228c2ecf20Sopenharmony_ci		if (rval2 != QLA_SUCCESS) {
54238c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2097,
54248c2ecf20Sopenharmony_ci			    "Failed to retrieve fcport information "
54258c2ecf20Sopenharmony_ci			    "-- get_port_database=%x, loop_id=0x%04x.\n",
54268c2ecf20Sopenharmony_ci			    rval2, new_fcport->loop_id);
54278c2ecf20Sopenharmony_ci			/* Skip retry if N2N */
54288c2ecf20Sopenharmony_ci			if (ha->current_topology != ISP_CFG_N) {
54298c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x2105,
54308c2ecf20Sopenharmony_ci				    "Scheduling resync.\n");
54318c2ecf20Sopenharmony_ci				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
54328c2ecf20Sopenharmony_ci				continue;
54338c2ecf20Sopenharmony_ci			}
54348c2ecf20Sopenharmony_ci		}
54358c2ecf20Sopenharmony_ci
54368c2ecf20Sopenharmony_ci		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
54378c2ecf20Sopenharmony_ci		/* Check for matching device in port list. */
54388c2ecf20Sopenharmony_ci		found = 0;
54398c2ecf20Sopenharmony_ci		fcport = NULL;
54408c2ecf20Sopenharmony_ci		list_for_each_entry(fcport, &vha->vp_fcports, list) {
54418c2ecf20Sopenharmony_ci			if (memcmp(new_fcport->port_name, fcport->port_name,
54428c2ecf20Sopenharmony_ci			    WWN_SIZE))
54438c2ecf20Sopenharmony_ci				continue;
54448c2ecf20Sopenharmony_ci
54458c2ecf20Sopenharmony_ci			fcport->flags &= ~FCF_FABRIC_DEVICE;
54468c2ecf20Sopenharmony_ci			fcport->loop_id = new_fcport->loop_id;
54478c2ecf20Sopenharmony_ci			fcport->port_type = new_fcport->port_type;
54488c2ecf20Sopenharmony_ci			fcport->d_id.b24 = new_fcport->d_id.b24;
54498c2ecf20Sopenharmony_ci			memcpy(fcport->node_name, new_fcport->node_name,
54508c2ecf20Sopenharmony_ci			    WWN_SIZE);
54518c2ecf20Sopenharmony_ci			fcport->scan_state = QLA_FCPORT_FOUND;
54528c2ecf20Sopenharmony_ci			if (fcport->login_retry == 0) {
54538c2ecf20Sopenharmony_ci				fcport->login_retry = vha->hw->login_retry_count;
54548c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x2135,
54558c2ecf20Sopenharmony_ci				    "Port login retry %8phN, lid 0x%04x retry cnt=%d.\n",
54568c2ecf20Sopenharmony_ci				    fcport->port_name, fcport->loop_id,
54578c2ecf20Sopenharmony_ci				    fcport->login_retry);
54588c2ecf20Sopenharmony_ci			}
54598c2ecf20Sopenharmony_ci			found++;
54608c2ecf20Sopenharmony_ci			break;
54618c2ecf20Sopenharmony_ci		}
54628c2ecf20Sopenharmony_ci
54638c2ecf20Sopenharmony_ci		if (!found) {
54648c2ecf20Sopenharmony_ci			/* New device, add to fcports list. */
54658c2ecf20Sopenharmony_ci			list_add_tail(&new_fcport->list, &vha->vp_fcports);
54668c2ecf20Sopenharmony_ci
54678c2ecf20Sopenharmony_ci			/* Allocate a new replacement fcport. */
54688c2ecf20Sopenharmony_ci			fcport = new_fcport;
54698c2ecf20Sopenharmony_ci
54708c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
54718c2ecf20Sopenharmony_ci
54728c2ecf20Sopenharmony_ci			new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
54738c2ecf20Sopenharmony_ci
54748c2ecf20Sopenharmony_ci			if (new_fcport == NULL) {
54758c2ecf20Sopenharmony_ci				ql_log(ql_log_warn, vha, 0xd031,
54768c2ecf20Sopenharmony_ci				    "Failed to allocate memory for fcport.\n");
54778c2ecf20Sopenharmony_ci				rval = QLA_MEMORY_ALLOC_FAILED;
54788c2ecf20Sopenharmony_ci				goto err;
54798c2ecf20Sopenharmony_ci			}
54808c2ecf20Sopenharmony_ci			spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
54818c2ecf20Sopenharmony_ci			new_fcport->flags &= ~FCF_FABRIC_DEVICE;
54828c2ecf20Sopenharmony_ci		}
54838c2ecf20Sopenharmony_ci
54848c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
54858c2ecf20Sopenharmony_ci
54868c2ecf20Sopenharmony_ci		/* Base iIDMA settings on HBA port speed. */
54878c2ecf20Sopenharmony_ci		fcport->fp_speed = ha->link_data_rate;
54888c2ecf20Sopenharmony_ci
54898c2ecf20Sopenharmony_ci		found_devs++;
54908c2ecf20Sopenharmony_ci	}
54918c2ecf20Sopenharmony_ci
54928c2ecf20Sopenharmony_ci	list_for_each_entry(fcport, &vha->vp_fcports, list) {
54938c2ecf20Sopenharmony_ci		if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
54948c2ecf20Sopenharmony_ci			break;
54958c2ecf20Sopenharmony_ci
54968c2ecf20Sopenharmony_ci		if (fcport->scan_state == QLA_FCPORT_SCAN) {
54978c2ecf20Sopenharmony_ci			if ((qla_dual_mode_enabled(vha) ||
54988c2ecf20Sopenharmony_ci			    qla_ini_mode_enabled(vha)) &&
54998c2ecf20Sopenharmony_ci			    atomic_read(&fcport->state) == FCS_ONLINE) {
55008c2ecf20Sopenharmony_ci				qla2x00_mark_device_lost(vha, fcport,
55018c2ecf20Sopenharmony_ci					ql2xplogiabsentdevice);
55028c2ecf20Sopenharmony_ci				if (fcport->loop_id != FC_NO_LOOP_ID &&
55038c2ecf20Sopenharmony_ci				    (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
55048c2ecf20Sopenharmony_ci				    fcport->port_type != FCT_INITIATOR &&
55058c2ecf20Sopenharmony_ci				    fcport->port_type != FCT_BROADCAST) {
55068c2ecf20Sopenharmony_ci					ql_dbg(ql_dbg_disc, vha, 0x20f0,
55078c2ecf20Sopenharmony_ci					    "%s %d %8phC post del sess\n",
55088c2ecf20Sopenharmony_ci					    __func__, __LINE__,
55098c2ecf20Sopenharmony_ci					    fcport->port_name);
55108c2ecf20Sopenharmony_ci
55118c2ecf20Sopenharmony_ci					qlt_schedule_sess_for_deletion(fcport);
55128c2ecf20Sopenharmony_ci					continue;
55138c2ecf20Sopenharmony_ci				}
55148c2ecf20Sopenharmony_ci			}
55158c2ecf20Sopenharmony_ci		}
55168c2ecf20Sopenharmony_ci
55178c2ecf20Sopenharmony_ci		if (fcport->scan_state == QLA_FCPORT_FOUND)
55188c2ecf20Sopenharmony_ci			qla24xx_fcport_handle_login(vha, fcport);
55198c2ecf20Sopenharmony_ci	}
55208c2ecf20Sopenharmony_ci
55218c2ecf20Sopenharmony_ci	qla2x00_free_fcport(new_fcport);
55228c2ecf20Sopenharmony_ci
55238c2ecf20Sopenharmony_ci	return rval;
55248c2ecf20Sopenharmony_ci
55258c2ecf20Sopenharmony_cierr:
55268c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x2098,
55278c2ecf20Sopenharmony_ci	       "Configure local loop error exit: rval=%x.\n", rval);
55288c2ecf20Sopenharmony_ci	return rval;
55298c2ecf20Sopenharmony_ci}
55308c2ecf20Sopenharmony_ci
55318c2ecf20Sopenharmony_cistatic void
55328c2ecf20Sopenharmony_ciqla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
55338c2ecf20Sopenharmony_ci{
55348c2ecf20Sopenharmony_ci	int rval;
55358c2ecf20Sopenharmony_ci	uint16_t mb[MAILBOX_REGISTER_COUNT];
55368c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
55378c2ecf20Sopenharmony_ci
55388c2ecf20Sopenharmony_ci	if (!IS_IIDMA_CAPABLE(ha))
55398c2ecf20Sopenharmony_ci		return;
55408c2ecf20Sopenharmony_ci
55418c2ecf20Sopenharmony_ci	if (atomic_read(&fcport->state) != FCS_ONLINE)
55428c2ecf20Sopenharmony_ci		return;
55438c2ecf20Sopenharmony_ci
55448c2ecf20Sopenharmony_ci	if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
55458c2ecf20Sopenharmony_ci	    fcport->fp_speed > ha->link_data_rate ||
55468c2ecf20Sopenharmony_ci	    !ha->flags.gpsc_supported)
55478c2ecf20Sopenharmony_ci		return;
55488c2ecf20Sopenharmony_ci
55498c2ecf20Sopenharmony_ci	rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
55508c2ecf20Sopenharmony_ci	    mb);
55518c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
55528c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x2004,
55538c2ecf20Sopenharmony_ci		    "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
55548c2ecf20Sopenharmony_ci		    fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
55558c2ecf20Sopenharmony_ci	} else {
55568c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x2005,
55578c2ecf20Sopenharmony_ci		    "iIDMA adjusted to %s GB/s (%X) on %8phN.\n",
55588c2ecf20Sopenharmony_ci		    qla2x00_get_link_speed_str(ha, fcport->fp_speed),
55598c2ecf20Sopenharmony_ci		    fcport->fp_speed, fcport->port_name);
55608c2ecf20Sopenharmony_ci	}
55618c2ecf20Sopenharmony_ci}
55628c2ecf20Sopenharmony_ci
55638c2ecf20Sopenharmony_civoid qla_do_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
55648c2ecf20Sopenharmony_ci{
55658c2ecf20Sopenharmony_ci	qla2x00_iidma_fcport(vha, fcport);
55668c2ecf20Sopenharmony_ci	qla24xx_update_fcport_fcp_prio(vha, fcport);
55678c2ecf20Sopenharmony_ci}
55688c2ecf20Sopenharmony_ci
55698c2ecf20Sopenharmony_ciint qla_post_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
55708c2ecf20Sopenharmony_ci{
55718c2ecf20Sopenharmony_ci	struct qla_work_evt *e;
55728c2ecf20Sopenharmony_ci
55738c2ecf20Sopenharmony_ci	e = qla2x00_alloc_work(vha, QLA_EVT_IIDMA);
55748c2ecf20Sopenharmony_ci	if (!e)
55758c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
55768c2ecf20Sopenharmony_ci
55778c2ecf20Sopenharmony_ci	e->u.fcport.fcport = fcport;
55788c2ecf20Sopenharmony_ci	return qla2x00_post_work(vha, e);
55798c2ecf20Sopenharmony_ci}
55808c2ecf20Sopenharmony_ci
55818c2ecf20Sopenharmony_ci/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
55828c2ecf20Sopenharmony_cistatic void
55838c2ecf20Sopenharmony_ciqla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
55848c2ecf20Sopenharmony_ci{
55858c2ecf20Sopenharmony_ci	struct fc_rport_identifiers rport_ids;
55868c2ecf20Sopenharmony_ci	struct fc_rport *rport;
55878c2ecf20Sopenharmony_ci	unsigned long flags;
55888c2ecf20Sopenharmony_ci
55898c2ecf20Sopenharmony_ci	if (atomic_read(&fcport->state) == FCS_ONLINE)
55908c2ecf20Sopenharmony_ci		return;
55918c2ecf20Sopenharmony_ci
55928c2ecf20Sopenharmony_ci	rport_ids.node_name = wwn_to_u64(fcport->node_name);
55938c2ecf20Sopenharmony_ci	rport_ids.port_name = wwn_to_u64(fcport->port_name);
55948c2ecf20Sopenharmony_ci	rport_ids.port_id = fcport->d_id.b.domain << 16 |
55958c2ecf20Sopenharmony_ci	    fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
55968c2ecf20Sopenharmony_ci	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
55978c2ecf20Sopenharmony_ci	fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
55988c2ecf20Sopenharmony_ci	if (!rport) {
55998c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x2006,
56008c2ecf20Sopenharmony_ci		    "Unable to allocate fc remote port.\n");
56018c2ecf20Sopenharmony_ci		return;
56028c2ecf20Sopenharmony_ci	}
56038c2ecf20Sopenharmony_ci
56048c2ecf20Sopenharmony_ci	spin_lock_irqsave(fcport->vha->host->host_lock, flags);
56058c2ecf20Sopenharmony_ci	*((fc_port_t **)rport->dd_data) = fcport;
56068c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
56078c2ecf20Sopenharmony_ci
56088c2ecf20Sopenharmony_ci	rport->supported_classes = fcport->supported_classes;
56098c2ecf20Sopenharmony_ci
56108c2ecf20Sopenharmony_ci	rport_ids.roles = FC_PORT_ROLE_UNKNOWN;
56118c2ecf20Sopenharmony_ci	if (fcport->port_type == FCT_INITIATOR)
56128c2ecf20Sopenharmony_ci		rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
56138c2ecf20Sopenharmony_ci	if (fcport->port_type == FCT_TARGET)
56148c2ecf20Sopenharmony_ci		rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
56158c2ecf20Sopenharmony_ci	if (fcport->port_type & FCT_NVME_INITIATOR)
56168c2ecf20Sopenharmony_ci		rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
56178c2ecf20Sopenharmony_ci	if (fcport->port_type & FCT_NVME_TARGET)
56188c2ecf20Sopenharmony_ci		rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
56198c2ecf20Sopenharmony_ci	if (fcport->port_type & FCT_NVME_DISCOVERY)
56208c2ecf20Sopenharmony_ci		rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
56218c2ecf20Sopenharmony_ci
56228c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20ee,
56238c2ecf20Sopenharmony_ci	    "%s %8phN. rport %p is %s mode\n",
56248c2ecf20Sopenharmony_ci	    __func__, fcport->port_name, rport,
56258c2ecf20Sopenharmony_ci	    (fcport->port_type == FCT_TARGET) ? "tgt" :
56268c2ecf20Sopenharmony_ci	    ((fcport->port_type & FCT_NVME) ? "nvme" : "ini"));
56278c2ecf20Sopenharmony_ci
56288c2ecf20Sopenharmony_ci	fc_remote_port_rolechg(rport, rport_ids.roles);
56298c2ecf20Sopenharmony_ci}
56308c2ecf20Sopenharmony_ci
56318c2ecf20Sopenharmony_ci/*
56328c2ecf20Sopenharmony_ci * qla2x00_update_fcport
56338c2ecf20Sopenharmony_ci *	Updates device on list.
56348c2ecf20Sopenharmony_ci *
56358c2ecf20Sopenharmony_ci * Input:
56368c2ecf20Sopenharmony_ci *	ha = adapter block pointer.
56378c2ecf20Sopenharmony_ci *	fcport = port structure pointer.
56388c2ecf20Sopenharmony_ci *
56398c2ecf20Sopenharmony_ci * Return:
56408c2ecf20Sopenharmony_ci *	0  - Success
56418c2ecf20Sopenharmony_ci *  BIT_0 - error
56428c2ecf20Sopenharmony_ci *
56438c2ecf20Sopenharmony_ci * Context:
56448c2ecf20Sopenharmony_ci *	Kernel context.
56458c2ecf20Sopenharmony_ci */
56468c2ecf20Sopenharmony_civoid
56478c2ecf20Sopenharmony_ciqla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
56488c2ecf20Sopenharmony_ci{
56498c2ecf20Sopenharmony_ci	unsigned long flags;
56508c2ecf20Sopenharmony_ci
56518c2ecf20Sopenharmony_ci	if (IS_SW_RESV_ADDR(fcport->d_id))
56528c2ecf20Sopenharmony_ci		return;
56538c2ecf20Sopenharmony_ci
56548c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
56558c2ecf20Sopenharmony_ci	    __func__, fcport->port_name);
56568c2ecf20Sopenharmony_ci
56578c2ecf20Sopenharmony_ci	qla2x00_set_fcport_disc_state(fcport, DSC_UPD_FCPORT);
56588c2ecf20Sopenharmony_ci	fcport->login_retry = vha->hw->login_retry_count;
56598c2ecf20Sopenharmony_ci	fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
56608c2ecf20Sopenharmony_ci
56618c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vha->work_lock, flags);
56628c2ecf20Sopenharmony_ci	fcport->deleted = 0;
56638c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vha->work_lock, flags);
56648c2ecf20Sopenharmony_ci
56658c2ecf20Sopenharmony_ci	if (vha->hw->current_topology == ISP_CFG_NL)
56668c2ecf20Sopenharmony_ci		fcport->logout_on_delete = 0;
56678c2ecf20Sopenharmony_ci	else
56688c2ecf20Sopenharmony_ci		fcport->logout_on_delete = 1;
56698c2ecf20Sopenharmony_ci	fcport->n2n_chip_reset = fcport->n2n_link_reset_cnt = 0;
56708c2ecf20Sopenharmony_ci
56718c2ecf20Sopenharmony_ci	switch (vha->hw->current_topology) {
56728c2ecf20Sopenharmony_ci	case ISP_CFG_N:
56738c2ecf20Sopenharmony_ci	case ISP_CFG_NL:
56748c2ecf20Sopenharmony_ci		fcport->keep_nport_handle = 1;
56758c2ecf20Sopenharmony_ci		break;
56768c2ecf20Sopenharmony_ci	default:
56778c2ecf20Sopenharmony_ci		break;
56788c2ecf20Sopenharmony_ci	}
56798c2ecf20Sopenharmony_ci
56808c2ecf20Sopenharmony_ci	qla2x00_iidma_fcport(vha, fcport);
56818c2ecf20Sopenharmony_ci
56828c2ecf20Sopenharmony_ci	qla2x00_dfs_create_rport(vha, fcport);
56838c2ecf20Sopenharmony_ci
56848c2ecf20Sopenharmony_ci	if (NVME_TARGET(vha->hw, fcport)) {
56858c2ecf20Sopenharmony_ci		qla_nvme_register_remote(vha, fcport);
56868c2ecf20Sopenharmony_ci		qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
56878c2ecf20Sopenharmony_ci		qla2x00_set_fcport_state(fcport, FCS_ONLINE);
56888c2ecf20Sopenharmony_ci		return;
56898c2ecf20Sopenharmony_ci	}
56908c2ecf20Sopenharmony_ci
56918c2ecf20Sopenharmony_ci	qla24xx_update_fcport_fcp_prio(vha, fcport);
56928c2ecf20Sopenharmony_ci
56938c2ecf20Sopenharmony_ci	switch (vha->host->active_mode) {
56948c2ecf20Sopenharmony_ci	case MODE_INITIATOR:
56958c2ecf20Sopenharmony_ci		qla2x00_reg_remote_port(vha, fcport);
56968c2ecf20Sopenharmony_ci		break;
56978c2ecf20Sopenharmony_ci	case MODE_TARGET:
56988c2ecf20Sopenharmony_ci		if (!vha->vha_tgt.qla_tgt->tgt_stop &&
56998c2ecf20Sopenharmony_ci			!vha->vha_tgt.qla_tgt->tgt_stopped)
57008c2ecf20Sopenharmony_ci			qlt_fc_port_added(vha, fcport);
57018c2ecf20Sopenharmony_ci		break;
57028c2ecf20Sopenharmony_ci	case MODE_DUAL:
57038c2ecf20Sopenharmony_ci		qla2x00_reg_remote_port(vha, fcport);
57048c2ecf20Sopenharmony_ci		if (!vha->vha_tgt.qla_tgt->tgt_stop &&
57058c2ecf20Sopenharmony_ci			!vha->vha_tgt.qla_tgt->tgt_stopped)
57068c2ecf20Sopenharmony_ci			qlt_fc_port_added(vha, fcport);
57078c2ecf20Sopenharmony_ci		break;
57088c2ecf20Sopenharmony_ci	default:
57098c2ecf20Sopenharmony_ci		break;
57108c2ecf20Sopenharmony_ci	}
57118c2ecf20Sopenharmony_ci
57128c2ecf20Sopenharmony_ci	qla2x00_set_fcport_state(fcport, FCS_ONLINE);
57138c2ecf20Sopenharmony_ci
57148c2ecf20Sopenharmony_ci	if (IS_IIDMA_CAPABLE(vha->hw) && vha->hw->flags.gpsc_supported) {
57158c2ecf20Sopenharmony_ci		if (fcport->id_changed) {
57168c2ecf20Sopenharmony_ci			fcport->id_changed = 0;
57178c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x20d7,
57188c2ecf20Sopenharmony_ci			    "%s %d %8phC post gfpnid fcp_cnt %d\n",
57198c2ecf20Sopenharmony_ci			    __func__, __LINE__, fcport->port_name,
57208c2ecf20Sopenharmony_ci			    vha->fcport_count);
57218c2ecf20Sopenharmony_ci			qla24xx_post_gfpnid_work(vha, fcport);
57228c2ecf20Sopenharmony_ci		} else {
57238c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x20d7,
57248c2ecf20Sopenharmony_ci			    "%s %d %8phC post gpsc fcp_cnt %d\n",
57258c2ecf20Sopenharmony_ci			    __func__, __LINE__, fcport->port_name,
57268c2ecf20Sopenharmony_ci			    vha->fcport_count);
57278c2ecf20Sopenharmony_ci			qla24xx_post_gpsc_work(vha, fcport);
57288c2ecf20Sopenharmony_ci		}
57298c2ecf20Sopenharmony_ci	}
57308c2ecf20Sopenharmony_ci
57318c2ecf20Sopenharmony_ci	qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
57328c2ecf20Sopenharmony_ci}
57338c2ecf20Sopenharmony_ci
57348c2ecf20Sopenharmony_civoid qla_register_fcport_fn(struct work_struct *work)
57358c2ecf20Sopenharmony_ci{
57368c2ecf20Sopenharmony_ci	fc_port_t *fcport = container_of(work, struct fc_port, reg_work);
57378c2ecf20Sopenharmony_ci	u32 rscn_gen = fcport->rscn_gen;
57388c2ecf20Sopenharmony_ci	u16 data[2];
57398c2ecf20Sopenharmony_ci
57408c2ecf20Sopenharmony_ci	if (IS_SW_RESV_ADDR(fcport->d_id))
57418c2ecf20Sopenharmony_ci		return;
57428c2ecf20Sopenharmony_ci
57438c2ecf20Sopenharmony_ci	qla2x00_update_fcport(fcport->vha, fcport);
57448c2ecf20Sopenharmony_ci
57458c2ecf20Sopenharmony_ci	if (rscn_gen != fcport->rscn_gen) {
57468c2ecf20Sopenharmony_ci		/* RSCN(s) came in while registration */
57478c2ecf20Sopenharmony_ci		switch (fcport->next_disc_state) {
57488c2ecf20Sopenharmony_ci		case DSC_DELETE_PEND:
57498c2ecf20Sopenharmony_ci			qlt_schedule_sess_for_deletion(fcport);
57508c2ecf20Sopenharmony_ci			break;
57518c2ecf20Sopenharmony_ci		case DSC_ADISC:
57528c2ecf20Sopenharmony_ci			data[0] = data[1] = 0;
57538c2ecf20Sopenharmony_ci			qla2x00_post_async_adisc_work(fcport->vha, fcport,
57548c2ecf20Sopenharmony_ci			    data);
57558c2ecf20Sopenharmony_ci			break;
57568c2ecf20Sopenharmony_ci		default:
57578c2ecf20Sopenharmony_ci			break;
57588c2ecf20Sopenharmony_ci		}
57598c2ecf20Sopenharmony_ci	}
57608c2ecf20Sopenharmony_ci}
57618c2ecf20Sopenharmony_ci
57628c2ecf20Sopenharmony_ci/*
57638c2ecf20Sopenharmony_ci * qla2x00_configure_fabric
57648c2ecf20Sopenharmony_ci *      Setup SNS devices with loop ID's.
57658c2ecf20Sopenharmony_ci *
57668c2ecf20Sopenharmony_ci * Input:
57678c2ecf20Sopenharmony_ci *      ha = adapter block pointer.
57688c2ecf20Sopenharmony_ci *
57698c2ecf20Sopenharmony_ci * Returns:
57708c2ecf20Sopenharmony_ci *      0 = success.
57718c2ecf20Sopenharmony_ci *      BIT_0 = error
57728c2ecf20Sopenharmony_ci */
57738c2ecf20Sopenharmony_cistatic int
57748c2ecf20Sopenharmony_ciqla2x00_configure_fabric(scsi_qla_host_t *vha)
57758c2ecf20Sopenharmony_ci{
57768c2ecf20Sopenharmony_ci	int	rval;
57778c2ecf20Sopenharmony_ci	fc_port_t	*fcport;
57788c2ecf20Sopenharmony_ci	uint16_t	mb[MAILBOX_REGISTER_COUNT];
57798c2ecf20Sopenharmony_ci	uint16_t	loop_id;
57808c2ecf20Sopenharmony_ci	LIST_HEAD(new_fcports);
57818c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
57828c2ecf20Sopenharmony_ci	int		discovery_gen;
57838c2ecf20Sopenharmony_ci
57848c2ecf20Sopenharmony_ci	/* If FL port exists, then SNS is present */
57858c2ecf20Sopenharmony_ci	if (IS_FWI2_CAPABLE(ha))
57868c2ecf20Sopenharmony_ci		loop_id = NPH_F_PORT;
57878c2ecf20Sopenharmony_ci	else
57888c2ecf20Sopenharmony_ci		loop_id = SNS_FL_PORT;
57898c2ecf20Sopenharmony_ci	rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
57908c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
57918c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20a0,
57928c2ecf20Sopenharmony_ci		    "MBX_GET_PORT_NAME failed, No FL Port.\n");
57938c2ecf20Sopenharmony_ci
57948c2ecf20Sopenharmony_ci		vha->device_flags &= ~SWITCH_FOUND;
57958c2ecf20Sopenharmony_ci		return (QLA_SUCCESS);
57968c2ecf20Sopenharmony_ci	}
57978c2ecf20Sopenharmony_ci	vha->device_flags |= SWITCH_FOUND;
57988c2ecf20Sopenharmony_ci
57998c2ecf20Sopenharmony_ci	rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_port_name, 0);
58008c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS)
58018c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x20ff,
58028c2ecf20Sopenharmony_ci		    "Failed to get Fabric Port Name\n");
58038c2ecf20Sopenharmony_ci
58048c2ecf20Sopenharmony_ci	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
58058c2ecf20Sopenharmony_ci		rval = qla2x00_send_change_request(vha, 0x3, 0);
58068c2ecf20Sopenharmony_ci		if (rval != QLA_SUCCESS)
58078c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x121,
58088c2ecf20Sopenharmony_ci			    "Failed to enable receiving of RSCN requests: 0x%x.\n",
58098c2ecf20Sopenharmony_ci			    rval);
58108c2ecf20Sopenharmony_ci	}
58118c2ecf20Sopenharmony_ci
58128c2ecf20Sopenharmony_ci	do {
58138c2ecf20Sopenharmony_ci		qla2x00_mgmt_svr_login(vha);
58148c2ecf20Sopenharmony_ci
58158c2ecf20Sopenharmony_ci		/* Ensure we are logged into the SNS. */
58168c2ecf20Sopenharmony_ci		loop_id = NPH_SNS_LID(ha);
58178c2ecf20Sopenharmony_ci		rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
58188c2ecf20Sopenharmony_ci		    0xfc, mb, BIT_1|BIT_0);
58198c2ecf20Sopenharmony_ci		if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
58208c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x20a1,
58218c2ecf20Sopenharmony_ci			    "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
58228c2ecf20Sopenharmony_ci			    loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
58238c2ecf20Sopenharmony_ci			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
58248c2ecf20Sopenharmony_ci			return rval;
58258c2ecf20Sopenharmony_ci		}
58268c2ecf20Sopenharmony_ci
58278c2ecf20Sopenharmony_ci		/* FDMI support. */
58288c2ecf20Sopenharmony_ci		if (ql2xfdmienable &&
58298c2ecf20Sopenharmony_ci		    test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
58308c2ecf20Sopenharmony_ci			qla2x00_fdmi_register(vha);
58318c2ecf20Sopenharmony_ci
58328c2ecf20Sopenharmony_ci		if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
58338c2ecf20Sopenharmony_ci			if (qla2x00_rft_id(vha)) {
58348c2ecf20Sopenharmony_ci				/* EMPTY */
58358c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x20a2,
58368c2ecf20Sopenharmony_ci				    "Register FC-4 TYPE failed.\n");
58378c2ecf20Sopenharmony_ci				if (test_bit(LOOP_RESYNC_NEEDED,
58388c2ecf20Sopenharmony_ci				    &vha->dpc_flags))
58398c2ecf20Sopenharmony_ci					break;
58408c2ecf20Sopenharmony_ci			}
58418c2ecf20Sopenharmony_ci			if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
58428c2ecf20Sopenharmony_ci				/* EMPTY */
58438c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x209a,
58448c2ecf20Sopenharmony_ci				    "Register FC-4 Features failed.\n");
58458c2ecf20Sopenharmony_ci				if (test_bit(LOOP_RESYNC_NEEDED,
58468c2ecf20Sopenharmony_ci				    &vha->dpc_flags))
58478c2ecf20Sopenharmony_ci					break;
58488c2ecf20Sopenharmony_ci			}
58498c2ecf20Sopenharmony_ci			if (vha->flags.nvme_enabled) {
58508c2ecf20Sopenharmony_ci				if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
58518c2ecf20Sopenharmony_ci					ql_dbg(ql_dbg_disc, vha, 0x2049,
58528c2ecf20Sopenharmony_ci					    "Register NVME FC Type Features failed.\n");
58538c2ecf20Sopenharmony_ci				}
58548c2ecf20Sopenharmony_ci			}
58558c2ecf20Sopenharmony_ci			if (qla2x00_rnn_id(vha)) {
58568c2ecf20Sopenharmony_ci				/* EMPTY */
58578c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x2104,
58588c2ecf20Sopenharmony_ci				    "Register Node Name failed.\n");
58598c2ecf20Sopenharmony_ci				if (test_bit(LOOP_RESYNC_NEEDED,
58608c2ecf20Sopenharmony_ci				    &vha->dpc_flags))
58618c2ecf20Sopenharmony_ci					break;
58628c2ecf20Sopenharmony_ci			} else if (qla2x00_rsnn_nn(vha)) {
58638c2ecf20Sopenharmony_ci				/* EMPTY */
58648c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_disc, vha, 0x209b,
58658c2ecf20Sopenharmony_ci				    "Register Symbolic Node Name failed.\n");
58668c2ecf20Sopenharmony_ci				if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
58678c2ecf20Sopenharmony_ci					break;
58688c2ecf20Sopenharmony_ci			}
58698c2ecf20Sopenharmony_ci		}
58708c2ecf20Sopenharmony_ci
58718c2ecf20Sopenharmony_ci
58728c2ecf20Sopenharmony_ci		/* Mark the time right before querying FW for connected ports.
58738c2ecf20Sopenharmony_ci		 * This process is long, asynchronous and by the time it's done,
58748c2ecf20Sopenharmony_ci		 * collected information might not be accurate anymore. E.g.
58758c2ecf20Sopenharmony_ci		 * disconnected port might have re-connected and a brand new
58768c2ecf20Sopenharmony_ci		 * session has been created. In this case session's generation
58778c2ecf20Sopenharmony_ci		 * will be newer than discovery_gen. */
58788c2ecf20Sopenharmony_ci		qlt_do_generation_tick(vha, &discovery_gen);
58798c2ecf20Sopenharmony_ci
58808c2ecf20Sopenharmony_ci		if (USE_ASYNC_SCAN(ha)) {
58818c2ecf20Sopenharmony_ci			rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI,
58828c2ecf20Sopenharmony_ci			    NULL);
58838c2ecf20Sopenharmony_ci			if (rval)
58848c2ecf20Sopenharmony_ci				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
58858c2ecf20Sopenharmony_ci		} else  {
58868c2ecf20Sopenharmony_ci			list_for_each_entry(fcport, &vha->vp_fcports, list)
58878c2ecf20Sopenharmony_ci				fcport->scan_state = QLA_FCPORT_SCAN;
58888c2ecf20Sopenharmony_ci
58898c2ecf20Sopenharmony_ci			rval = qla2x00_find_all_fabric_devs(vha);
58908c2ecf20Sopenharmony_ci		}
58918c2ecf20Sopenharmony_ci		if (rval != QLA_SUCCESS)
58928c2ecf20Sopenharmony_ci			break;
58938c2ecf20Sopenharmony_ci	} while (0);
58948c2ecf20Sopenharmony_ci
58958c2ecf20Sopenharmony_ci	if (!vha->nvme_local_port && vha->flags.nvme_enabled)
58968c2ecf20Sopenharmony_ci		qla_nvme_register_hba(vha);
58978c2ecf20Sopenharmony_ci
58988c2ecf20Sopenharmony_ci	if (rval)
58998c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x2068,
59008c2ecf20Sopenharmony_ci		    "Configure fabric error exit rval=%d.\n", rval);
59018c2ecf20Sopenharmony_ci
59028c2ecf20Sopenharmony_ci	return (rval);
59038c2ecf20Sopenharmony_ci}
59048c2ecf20Sopenharmony_ci
59058c2ecf20Sopenharmony_ci/*
59068c2ecf20Sopenharmony_ci * qla2x00_find_all_fabric_devs
59078c2ecf20Sopenharmony_ci *
59088c2ecf20Sopenharmony_ci * Input:
59098c2ecf20Sopenharmony_ci *	ha = adapter block pointer.
59108c2ecf20Sopenharmony_ci *	dev = database device entry pointer.
59118c2ecf20Sopenharmony_ci *
59128c2ecf20Sopenharmony_ci * Returns:
59138c2ecf20Sopenharmony_ci *	0 = success.
59148c2ecf20Sopenharmony_ci *
59158c2ecf20Sopenharmony_ci * Context:
59168c2ecf20Sopenharmony_ci *	Kernel context.
59178c2ecf20Sopenharmony_ci */
59188c2ecf20Sopenharmony_cistatic int
59198c2ecf20Sopenharmony_ciqla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
59208c2ecf20Sopenharmony_ci{
59218c2ecf20Sopenharmony_ci	int		rval;
59228c2ecf20Sopenharmony_ci	uint16_t	loop_id;
59238c2ecf20Sopenharmony_ci	fc_port_t	*fcport, *new_fcport;
59248c2ecf20Sopenharmony_ci	int		found;
59258c2ecf20Sopenharmony_ci
59268c2ecf20Sopenharmony_ci	sw_info_t	*swl;
59278c2ecf20Sopenharmony_ci	int		swl_idx;
59288c2ecf20Sopenharmony_ci	int		first_dev, last_dev;
59298c2ecf20Sopenharmony_ci	port_id_t	wrap = {}, nxt_d_id;
59308c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
59318c2ecf20Sopenharmony_ci	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
59328c2ecf20Sopenharmony_ci	unsigned long flags;
59338c2ecf20Sopenharmony_ci
59348c2ecf20Sopenharmony_ci	rval = QLA_SUCCESS;
59358c2ecf20Sopenharmony_ci
59368c2ecf20Sopenharmony_ci	/* Try GID_PT to get device list, else GAN. */
59378c2ecf20Sopenharmony_ci	if (!ha->swl)
59388c2ecf20Sopenharmony_ci		ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
59398c2ecf20Sopenharmony_ci		    GFP_KERNEL);
59408c2ecf20Sopenharmony_ci	swl = ha->swl;
59418c2ecf20Sopenharmony_ci	if (!swl) {
59428c2ecf20Sopenharmony_ci		/*EMPTY*/
59438c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x209c,
59448c2ecf20Sopenharmony_ci		    "GID_PT allocations failed, fallback on GA_NXT.\n");
59458c2ecf20Sopenharmony_ci	} else {
59468c2ecf20Sopenharmony_ci		memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
59478c2ecf20Sopenharmony_ci		if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
59488c2ecf20Sopenharmony_ci			swl = NULL;
59498c2ecf20Sopenharmony_ci			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
59508c2ecf20Sopenharmony_ci				return rval;
59518c2ecf20Sopenharmony_ci		} else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
59528c2ecf20Sopenharmony_ci			swl = NULL;
59538c2ecf20Sopenharmony_ci			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
59548c2ecf20Sopenharmony_ci				return rval;
59558c2ecf20Sopenharmony_ci		} else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
59568c2ecf20Sopenharmony_ci			swl = NULL;
59578c2ecf20Sopenharmony_ci			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
59588c2ecf20Sopenharmony_ci				return rval;
59598c2ecf20Sopenharmony_ci		} else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
59608c2ecf20Sopenharmony_ci			swl = NULL;
59618c2ecf20Sopenharmony_ci			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
59628c2ecf20Sopenharmony_ci				return rval;
59638c2ecf20Sopenharmony_ci		}
59648c2ecf20Sopenharmony_ci
59658c2ecf20Sopenharmony_ci		/* If other queries succeeded probe for FC-4 type */
59668c2ecf20Sopenharmony_ci		if (swl) {
59678c2ecf20Sopenharmony_ci			qla2x00_gff_id(vha, swl);
59688c2ecf20Sopenharmony_ci			if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
59698c2ecf20Sopenharmony_ci				return rval;
59708c2ecf20Sopenharmony_ci		}
59718c2ecf20Sopenharmony_ci	}
59728c2ecf20Sopenharmony_ci	swl_idx = 0;
59738c2ecf20Sopenharmony_ci
59748c2ecf20Sopenharmony_ci	/* Allocate temporary fcport for any new fcports discovered. */
59758c2ecf20Sopenharmony_ci	new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
59768c2ecf20Sopenharmony_ci	if (new_fcport == NULL) {
59778c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x209d,
59788c2ecf20Sopenharmony_ci		    "Failed to allocate memory for fcport.\n");
59798c2ecf20Sopenharmony_ci		return (QLA_MEMORY_ALLOC_FAILED);
59808c2ecf20Sopenharmony_ci	}
59818c2ecf20Sopenharmony_ci	new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
59828c2ecf20Sopenharmony_ci	/* Set start port ID scan at adapter ID. */
59838c2ecf20Sopenharmony_ci	first_dev = 1;
59848c2ecf20Sopenharmony_ci	last_dev = 0;
59858c2ecf20Sopenharmony_ci
59868c2ecf20Sopenharmony_ci	/* Starting free loop ID. */
59878c2ecf20Sopenharmony_ci	loop_id = ha->min_external_loopid;
59888c2ecf20Sopenharmony_ci	for (; loop_id <= ha->max_loop_id; loop_id++) {
59898c2ecf20Sopenharmony_ci		if (qla2x00_is_reserved_id(vha, loop_id))
59908c2ecf20Sopenharmony_ci			continue;
59918c2ecf20Sopenharmony_ci
59928c2ecf20Sopenharmony_ci		if (ha->current_topology == ISP_CFG_FL &&
59938c2ecf20Sopenharmony_ci		    (atomic_read(&vha->loop_down_timer) ||
59948c2ecf20Sopenharmony_ci		     LOOP_TRANSITION(vha))) {
59958c2ecf20Sopenharmony_ci			atomic_set(&vha->loop_down_timer, 0);
59968c2ecf20Sopenharmony_ci			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
59978c2ecf20Sopenharmony_ci			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
59988c2ecf20Sopenharmony_ci			break;
59998c2ecf20Sopenharmony_ci		}
60008c2ecf20Sopenharmony_ci
60018c2ecf20Sopenharmony_ci		if (swl != NULL) {
60028c2ecf20Sopenharmony_ci			if (last_dev) {
60038c2ecf20Sopenharmony_ci				wrap.b24 = new_fcport->d_id.b24;
60048c2ecf20Sopenharmony_ci			} else {
60058c2ecf20Sopenharmony_ci				new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
60068c2ecf20Sopenharmony_ci				memcpy(new_fcport->node_name,
60078c2ecf20Sopenharmony_ci				    swl[swl_idx].node_name, WWN_SIZE);
60088c2ecf20Sopenharmony_ci				memcpy(new_fcport->port_name,
60098c2ecf20Sopenharmony_ci				    swl[swl_idx].port_name, WWN_SIZE);
60108c2ecf20Sopenharmony_ci				memcpy(new_fcport->fabric_port_name,
60118c2ecf20Sopenharmony_ci				    swl[swl_idx].fabric_port_name, WWN_SIZE);
60128c2ecf20Sopenharmony_ci				new_fcport->fp_speed = swl[swl_idx].fp_speed;
60138c2ecf20Sopenharmony_ci				new_fcport->fc4_type = swl[swl_idx].fc4_type;
60148c2ecf20Sopenharmony_ci
60158c2ecf20Sopenharmony_ci				new_fcport->nvme_flag = 0;
60168c2ecf20Sopenharmony_ci				if (vha->flags.nvme_enabled &&
60178c2ecf20Sopenharmony_ci				    swl[swl_idx].fc4_type & FS_FC4TYPE_NVME) {
60188c2ecf20Sopenharmony_ci					ql_log(ql_log_info, vha, 0x2131,
60198c2ecf20Sopenharmony_ci					    "FOUND: NVME port %8phC as FC Type 28h\n",
60208c2ecf20Sopenharmony_ci					    new_fcport->port_name);
60218c2ecf20Sopenharmony_ci				}
60228c2ecf20Sopenharmony_ci
60238c2ecf20Sopenharmony_ci				if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
60248c2ecf20Sopenharmony_ci					last_dev = 1;
60258c2ecf20Sopenharmony_ci				}
60268c2ecf20Sopenharmony_ci				swl_idx++;
60278c2ecf20Sopenharmony_ci			}
60288c2ecf20Sopenharmony_ci		} else {
60298c2ecf20Sopenharmony_ci			/* Send GA_NXT to the switch */
60308c2ecf20Sopenharmony_ci			rval = qla2x00_ga_nxt(vha, new_fcport);
60318c2ecf20Sopenharmony_ci			if (rval != QLA_SUCCESS) {
60328c2ecf20Sopenharmony_ci				ql_log(ql_log_warn, vha, 0x209e,
60338c2ecf20Sopenharmony_ci				    "SNS scan failed -- assuming "
60348c2ecf20Sopenharmony_ci				    "zero-entry result.\n");
60358c2ecf20Sopenharmony_ci				rval = QLA_SUCCESS;
60368c2ecf20Sopenharmony_ci				break;
60378c2ecf20Sopenharmony_ci			}
60388c2ecf20Sopenharmony_ci		}
60398c2ecf20Sopenharmony_ci
60408c2ecf20Sopenharmony_ci		/* If wrap on switch device list, exit. */
60418c2ecf20Sopenharmony_ci		if (first_dev) {
60428c2ecf20Sopenharmony_ci			wrap.b24 = new_fcport->d_id.b24;
60438c2ecf20Sopenharmony_ci			first_dev = 0;
60448c2ecf20Sopenharmony_ci		} else if (new_fcport->d_id.b24 == wrap.b24) {
60458c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x209f,
60468c2ecf20Sopenharmony_ci			    "Device wrap (%02x%02x%02x).\n",
60478c2ecf20Sopenharmony_ci			    new_fcport->d_id.b.domain,
60488c2ecf20Sopenharmony_ci			    new_fcport->d_id.b.area,
60498c2ecf20Sopenharmony_ci			    new_fcport->d_id.b.al_pa);
60508c2ecf20Sopenharmony_ci			break;
60518c2ecf20Sopenharmony_ci		}
60528c2ecf20Sopenharmony_ci
60538c2ecf20Sopenharmony_ci		/* Bypass if same physical adapter. */
60548c2ecf20Sopenharmony_ci		if (new_fcport->d_id.b24 == base_vha->d_id.b24)
60558c2ecf20Sopenharmony_ci			continue;
60568c2ecf20Sopenharmony_ci
60578c2ecf20Sopenharmony_ci		/* Bypass virtual ports of the same host. */
60588c2ecf20Sopenharmony_ci		if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
60598c2ecf20Sopenharmony_ci			continue;
60608c2ecf20Sopenharmony_ci
60618c2ecf20Sopenharmony_ci		/* Bypass if same domain and area of adapter. */
60628c2ecf20Sopenharmony_ci		if (((new_fcport->d_id.b24 & 0xffff00) ==
60638c2ecf20Sopenharmony_ci		    (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
60648c2ecf20Sopenharmony_ci			ISP_CFG_FL)
60658c2ecf20Sopenharmony_ci			    continue;
60668c2ecf20Sopenharmony_ci
60678c2ecf20Sopenharmony_ci		/* Bypass reserved domain fields. */
60688c2ecf20Sopenharmony_ci		if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
60698c2ecf20Sopenharmony_ci			continue;
60708c2ecf20Sopenharmony_ci
60718c2ecf20Sopenharmony_ci		/* Bypass ports whose FCP-4 type is not FCP_SCSI */
60728c2ecf20Sopenharmony_ci		if (ql2xgffidenable &&
60738c2ecf20Sopenharmony_ci		    (!(new_fcport->fc4_type & FS_FC4TYPE_FCP) &&
60748c2ecf20Sopenharmony_ci		    new_fcport->fc4_type != 0))
60758c2ecf20Sopenharmony_ci			continue;
60768c2ecf20Sopenharmony_ci
60778c2ecf20Sopenharmony_ci		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
60788c2ecf20Sopenharmony_ci
60798c2ecf20Sopenharmony_ci		/* Locate matching device in database. */
60808c2ecf20Sopenharmony_ci		found = 0;
60818c2ecf20Sopenharmony_ci		list_for_each_entry(fcport, &vha->vp_fcports, list) {
60828c2ecf20Sopenharmony_ci			if (memcmp(new_fcport->port_name, fcport->port_name,
60838c2ecf20Sopenharmony_ci			    WWN_SIZE))
60848c2ecf20Sopenharmony_ci				continue;
60858c2ecf20Sopenharmony_ci
60868c2ecf20Sopenharmony_ci			fcport->scan_state = QLA_FCPORT_FOUND;
60878c2ecf20Sopenharmony_ci
60888c2ecf20Sopenharmony_ci			found++;
60898c2ecf20Sopenharmony_ci
60908c2ecf20Sopenharmony_ci			/* Update port state. */
60918c2ecf20Sopenharmony_ci			memcpy(fcport->fabric_port_name,
60928c2ecf20Sopenharmony_ci			    new_fcport->fabric_port_name, WWN_SIZE);
60938c2ecf20Sopenharmony_ci			fcport->fp_speed = new_fcport->fp_speed;
60948c2ecf20Sopenharmony_ci
60958c2ecf20Sopenharmony_ci			/*
60968c2ecf20Sopenharmony_ci			 * If address the same and state FCS_ONLINE
60978c2ecf20Sopenharmony_ci			 * (or in target mode), nothing changed.
60988c2ecf20Sopenharmony_ci			 */
60998c2ecf20Sopenharmony_ci			if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
61008c2ecf20Sopenharmony_ci			    (atomic_read(&fcport->state) == FCS_ONLINE ||
61018c2ecf20Sopenharmony_ci			     (vha->host->active_mode == MODE_TARGET))) {
61028c2ecf20Sopenharmony_ci				break;
61038c2ecf20Sopenharmony_ci			}
61048c2ecf20Sopenharmony_ci
61058c2ecf20Sopenharmony_ci			/*
61068c2ecf20Sopenharmony_ci			 * If device was not a fabric device before.
61078c2ecf20Sopenharmony_ci			 */
61088c2ecf20Sopenharmony_ci			if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
61098c2ecf20Sopenharmony_ci				fcport->d_id.b24 = new_fcport->d_id.b24;
61108c2ecf20Sopenharmony_ci				qla2x00_clear_loop_id(fcport);
61118c2ecf20Sopenharmony_ci				fcport->flags |= (FCF_FABRIC_DEVICE |
61128c2ecf20Sopenharmony_ci				    FCF_LOGIN_NEEDED);
61138c2ecf20Sopenharmony_ci				break;
61148c2ecf20Sopenharmony_ci			}
61158c2ecf20Sopenharmony_ci
61168c2ecf20Sopenharmony_ci			/*
61178c2ecf20Sopenharmony_ci			 * Port ID changed or device was marked to be updated;
61188c2ecf20Sopenharmony_ci			 * Log it out if still logged in and mark it for
61198c2ecf20Sopenharmony_ci			 * relogin later.
61208c2ecf20Sopenharmony_ci			 */
61218c2ecf20Sopenharmony_ci			if (qla_tgt_mode_enabled(base_vha)) {
61228c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
61238c2ecf20Sopenharmony_ci					 "port changed FC ID, %8phC"
61248c2ecf20Sopenharmony_ci					 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
61258c2ecf20Sopenharmony_ci					 fcport->port_name,
61268c2ecf20Sopenharmony_ci					 fcport->d_id.b.domain,
61278c2ecf20Sopenharmony_ci					 fcport->d_id.b.area,
61288c2ecf20Sopenharmony_ci					 fcport->d_id.b.al_pa,
61298c2ecf20Sopenharmony_ci					 fcport->loop_id,
61308c2ecf20Sopenharmony_ci					 new_fcport->d_id.b.domain,
61318c2ecf20Sopenharmony_ci					 new_fcport->d_id.b.area,
61328c2ecf20Sopenharmony_ci					 new_fcport->d_id.b.al_pa);
61338c2ecf20Sopenharmony_ci				fcport->d_id.b24 = new_fcport->d_id.b24;
61348c2ecf20Sopenharmony_ci				break;
61358c2ecf20Sopenharmony_ci			}
61368c2ecf20Sopenharmony_ci
61378c2ecf20Sopenharmony_ci			fcport->d_id.b24 = new_fcport->d_id.b24;
61388c2ecf20Sopenharmony_ci			fcport->flags |= FCF_LOGIN_NEEDED;
61398c2ecf20Sopenharmony_ci			break;
61408c2ecf20Sopenharmony_ci		}
61418c2ecf20Sopenharmony_ci
61428c2ecf20Sopenharmony_ci		if (found && NVME_TARGET(vha->hw, fcport)) {
61438c2ecf20Sopenharmony_ci			if (fcport->disc_state == DSC_DELETE_PEND) {
61448c2ecf20Sopenharmony_ci				qla2x00_set_fcport_disc_state(fcport, DSC_GNL);
61458c2ecf20Sopenharmony_ci				vha->fcport_count--;
61468c2ecf20Sopenharmony_ci				fcport->login_succ = 0;
61478c2ecf20Sopenharmony_ci			}
61488c2ecf20Sopenharmony_ci		}
61498c2ecf20Sopenharmony_ci
61508c2ecf20Sopenharmony_ci		if (found) {
61518c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
61528c2ecf20Sopenharmony_ci			continue;
61538c2ecf20Sopenharmony_ci		}
61548c2ecf20Sopenharmony_ci		/* If device was not in our fcports list, then add it. */
61558c2ecf20Sopenharmony_ci		new_fcport->scan_state = QLA_FCPORT_FOUND;
61568c2ecf20Sopenharmony_ci		list_add_tail(&new_fcport->list, &vha->vp_fcports);
61578c2ecf20Sopenharmony_ci
61588c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
61598c2ecf20Sopenharmony_ci
61608c2ecf20Sopenharmony_ci
61618c2ecf20Sopenharmony_ci		/* Allocate a new replacement fcport. */
61628c2ecf20Sopenharmony_ci		nxt_d_id.b24 = new_fcport->d_id.b24;
61638c2ecf20Sopenharmony_ci		new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
61648c2ecf20Sopenharmony_ci		if (new_fcport == NULL) {
61658c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0xd032,
61668c2ecf20Sopenharmony_ci			    "Memory allocation failed for fcport.\n");
61678c2ecf20Sopenharmony_ci			return (QLA_MEMORY_ALLOC_FAILED);
61688c2ecf20Sopenharmony_ci		}
61698c2ecf20Sopenharmony_ci		new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
61708c2ecf20Sopenharmony_ci		new_fcport->d_id.b24 = nxt_d_id.b24;
61718c2ecf20Sopenharmony_ci	}
61728c2ecf20Sopenharmony_ci
61738c2ecf20Sopenharmony_ci	qla2x00_free_fcport(new_fcport);
61748c2ecf20Sopenharmony_ci
61758c2ecf20Sopenharmony_ci	/*
61768c2ecf20Sopenharmony_ci	 * Logout all previous fabric dev marked lost, except FCP2 devices.
61778c2ecf20Sopenharmony_ci	 */
61788c2ecf20Sopenharmony_ci	list_for_each_entry(fcport, &vha->vp_fcports, list) {
61798c2ecf20Sopenharmony_ci		if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
61808c2ecf20Sopenharmony_ci			break;
61818c2ecf20Sopenharmony_ci
61828c2ecf20Sopenharmony_ci		if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
61838c2ecf20Sopenharmony_ci			continue;
61848c2ecf20Sopenharmony_ci
61858c2ecf20Sopenharmony_ci		if (fcport->scan_state == QLA_FCPORT_SCAN) {
61868c2ecf20Sopenharmony_ci			if ((qla_dual_mode_enabled(vha) ||
61878c2ecf20Sopenharmony_ci			    qla_ini_mode_enabled(vha)) &&
61888c2ecf20Sopenharmony_ci			    atomic_read(&fcport->state) == FCS_ONLINE) {
61898c2ecf20Sopenharmony_ci				qla2x00_mark_device_lost(vha, fcport,
61908c2ecf20Sopenharmony_ci					ql2xplogiabsentdevice);
61918c2ecf20Sopenharmony_ci				if (fcport->loop_id != FC_NO_LOOP_ID &&
61928c2ecf20Sopenharmony_ci				    (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
61938c2ecf20Sopenharmony_ci				    fcport->port_type != FCT_INITIATOR &&
61948c2ecf20Sopenharmony_ci				    fcport->port_type != FCT_BROADCAST) {
61958c2ecf20Sopenharmony_ci					ql_dbg(ql_dbg_disc, vha, 0x20f0,
61968c2ecf20Sopenharmony_ci					    "%s %d %8phC post del sess\n",
61978c2ecf20Sopenharmony_ci					    __func__, __LINE__,
61988c2ecf20Sopenharmony_ci					    fcport->port_name);
61998c2ecf20Sopenharmony_ci					qlt_schedule_sess_for_deletion(fcport);
62008c2ecf20Sopenharmony_ci					continue;
62018c2ecf20Sopenharmony_ci				}
62028c2ecf20Sopenharmony_ci			}
62038c2ecf20Sopenharmony_ci		}
62048c2ecf20Sopenharmony_ci
62058c2ecf20Sopenharmony_ci		if (fcport->scan_state == QLA_FCPORT_FOUND &&
62068c2ecf20Sopenharmony_ci		    (fcport->flags & FCF_LOGIN_NEEDED) != 0)
62078c2ecf20Sopenharmony_ci			qla24xx_fcport_handle_login(vha, fcport);
62088c2ecf20Sopenharmony_ci	}
62098c2ecf20Sopenharmony_ci	return (rval);
62108c2ecf20Sopenharmony_ci}
62118c2ecf20Sopenharmony_ci
62128c2ecf20Sopenharmony_ci/* FW does not set aside Loop id for MGMT Server/FFFFFAh */
62138c2ecf20Sopenharmony_ciint
62148c2ecf20Sopenharmony_ciqla2x00_reserve_mgmt_server_loop_id(scsi_qla_host_t *vha)
62158c2ecf20Sopenharmony_ci{
62168c2ecf20Sopenharmony_ci	int loop_id = FC_NO_LOOP_ID;
62178c2ecf20Sopenharmony_ci	int lid = NPH_MGMT_SERVER - vha->vp_idx;
62188c2ecf20Sopenharmony_ci	unsigned long flags;
62198c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
62208c2ecf20Sopenharmony_ci
62218c2ecf20Sopenharmony_ci	if (vha->vp_idx == 0) {
62228c2ecf20Sopenharmony_ci		set_bit(NPH_MGMT_SERVER, ha->loop_id_map);
62238c2ecf20Sopenharmony_ci		return NPH_MGMT_SERVER;
62248c2ecf20Sopenharmony_ci	}
62258c2ecf20Sopenharmony_ci
62268c2ecf20Sopenharmony_ci	/* pick id from high and work down to low */
62278c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->vport_slock, flags);
62288c2ecf20Sopenharmony_ci	for (; lid > 0; lid--) {
62298c2ecf20Sopenharmony_ci		if (!test_bit(lid, vha->hw->loop_id_map)) {
62308c2ecf20Sopenharmony_ci			set_bit(lid, vha->hw->loop_id_map);
62318c2ecf20Sopenharmony_ci			loop_id = lid;
62328c2ecf20Sopenharmony_ci			break;
62338c2ecf20Sopenharmony_ci		}
62348c2ecf20Sopenharmony_ci	}
62358c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->vport_slock, flags);
62368c2ecf20Sopenharmony_ci
62378c2ecf20Sopenharmony_ci	return loop_id;
62388c2ecf20Sopenharmony_ci}
62398c2ecf20Sopenharmony_ci
62408c2ecf20Sopenharmony_ci/*
62418c2ecf20Sopenharmony_ci * qla2x00_fabric_login
62428c2ecf20Sopenharmony_ci *	Issue fabric login command.
62438c2ecf20Sopenharmony_ci *
62448c2ecf20Sopenharmony_ci * Input:
62458c2ecf20Sopenharmony_ci *	ha = adapter block pointer.
62468c2ecf20Sopenharmony_ci *	device = pointer to FC device type structure.
62478c2ecf20Sopenharmony_ci *
62488c2ecf20Sopenharmony_ci * Returns:
62498c2ecf20Sopenharmony_ci *      0 - Login successfully
62508c2ecf20Sopenharmony_ci *      1 - Login failed
62518c2ecf20Sopenharmony_ci *      2 - Initiator device
62528c2ecf20Sopenharmony_ci *      3 - Fatal error
62538c2ecf20Sopenharmony_ci */
62548c2ecf20Sopenharmony_ciint
62558c2ecf20Sopenharmony_ciqla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
62568c2ecf20Sopenharmony_ci    uint16_t *next_loopid)
62578c2ecf20Sopenharmony_ci{
62588c2ecf20Sopenharmony_ci	int	rval;
62598c2ecf20Sopenharmony_ci	int	retry;
62608c2ecf20Sopenharmony_ci	uint16_t tmp_loopid;
62618c2ecf20Sopenharmony_ci	uint16_t mb[MAILBOX_REGISTER_COUNT];
62628c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
62638c2ecf20Sopenharmony_ci
62648c2ecf20Sopenharmony_ci	retry = 0;
62658c2ecf20Sopenharmony_ci	tmp_loopid = 0;
62668c2ecf20Sopenharmony_ci
62678c2ecf20Sopenharmony_ci	for (;;) {
62688c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x2000,
62698c2ecf20Sopenharmony_ci		    "Trying Fabric Login w/loop id 0x%04x for port "
62708c2ecf20Sopenharmony_ci		    "%02x%02x%02x.\n",
62718c2ecf20Sopenharmony_ci		    fcport->loop_id, fcport->d_id.b.domain,
62728c2ecf20Sopenharmony_ci		    fcport->d_id.b.area, fcport->d_id.b.al_pa);
62738c2ecf20Sopenharmony_ci
62748c2ecf20Sopenharmony_ci		/* Login fcport on switch. */
62758c2ecf20Sopenharmony_ci		rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
62768c2ecf20Sopenharmony_ci		    fcport->d_id.b.domain, fcport->d_id.b.area,
62778c2ecf20Sopenharmony_ci		    fcport->d_id.b.al_pa, mb, BIT_0);
62788c2ecf20Sopenharmony_ci		if (rval != QLA_SUCCESS) {
62798c2ecf20Sopenharmony_ci			return rval;
62808c2ecf20Sopenharmony_ci		}
62818c2ecf20Sopenharmony_ci		if (mb[0] == MBS_PORT_ID_USED) {
62828c2ecf20Sopenharmony_ci			/*
62838c2ecf20Sopenharmony_ci			 * Device has another loop ID.  The firmware team
62848c2ecf20Sopenharmony_ci			 * recommends the driver perform an implicit login with
62858c2ecf20Sopenharmony_ci			 * the specified ID again. The ID we just used is save
62868c2ecf20Sopenharmony_ci			 * here so we return with an ID that can be tried by
62878c2ecf20Sopenharmony_ci			 * the next login.
62888c2ecf20Sopenharmony_ci			 */
62898c2ecf20Sopenharmony_ci			retry++;
62908c2ecf20Sopenharmony_ci			tmp_loopid = fcport->loop_id;
62918c2ecf20Sopenharmony_ci			fcport->loop_id = mb[1];
62928c2ecf20Sopenharmony_ci
62938c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2001,
62948c2ecf20Sopenharmony_ci			    "Fabric Login: port in use - next loop "
62958c2ecf20Sopenharmony_ci			    "id=0x%04x, port id= %02x%02x%02x.\n",
62968c2ecf20Sopenharmony_ci			    fcport->loop_id, fcport->d_id.b.domain,
62978c2ecf20Sopenharmony_ci			    fcport->d_id.b.area, fcport->d_id.b.al_pa);
62988c2ecf20Sopenharmony_ci
62998c2ecf20Sopenharmony_ci		} else if (mb[0] == MBS_COMMAND_COMPLETE) {
63008c2ecf20Sopenharmony_ci			/*
63018c2ecf20Sopenharmony_ci			 * Login succeeded.
63028c2ecf20Sopenharmony_ci			 */
63038c2ecf20Sopenharmony_ci			if (retry) {
63048c2ecf20Sopenharmony_ci				/* A retry occurred before. */
63058c2ecf20Sopenharmony_ci				*next_loopid = tmp_loopid;
63068c2ecf20Sopenharmony_ci			} else {
63078c2ecf20Sopenharmony_ci				/*
63088c2ecf20Sopenharmony_ci				 * No retry occurred before. Just increment the
63098c2ecf20Sopenharmony_ci				 * ID value for next login.
63108c2ecf20Sopenharmony_ci				 */
63118c2ecf20Sopenharmony_ci				*next_loopid = (fcport->loop_id + 1);
63128c2ecf20Sopenharmony_ci			}
63138c2ecf20Sopenharmony_ci
63148c2ecf20Sopenharmony_ci			if (mb[1] & BIT_0) {
63158c2ecf20Sopenharmony_ci				fcport->port_type = FCT_INITIATOR;
63168c2ecf20Sopenharmony_ci			} else {
63178c2ecf20Sopenharmony_ci				fcport->port_type = FCT_TARGET;
63188c2ecf20Sopenharmony_ci				if (mb[1] & BIT_1) {
63198c2ecf20Sopenharmony_ci					fcport->flags |= FCF_FCP2_DEVICE;
63208c2ecf20Sopenharmony_ci				}
63218c2ecf20Sopenharmony_ci			}
63228c2ecf20Sopenharmony_ci
63238c2ecf20Sopenharmony_ci			if (mb[10] & BIT_0)
63248c2ecf20Sopenharmony_ci				fcport->supported_classes |= FC_COS_CLASS2;
63258c2ecf20Sopenharmony_ci			if (mb[10] & BIT_1)
63268c2ecf20Sopenharmony_ci				fcport->supported_classes |= FC_COS_CLASS3;
63278c2ecf20Sopenharmony_ci
63288c2ecf20Sopenharmony_ci			if (IS_FWI2_CAPABLE(ha)) {
63298c2ecf20Sopenharmony_ci				if (mb[10] & BIT_7)
63308c2ecf20Sopenharmony_ci					fcport->flags |=
63318c2ecf20Sopenharmony_ci					    FCF_CONF_COMP_SUPPORTED;
63328c2ecf20Sopenharmony_ci			}
63338c2ecf20Sopenharmony_ci
63348c2ecf20Sopenharmony_ci			rval = QLA_SUCCESS;
63358c2ecf20Sopenharmony_ci			break;
63368c2ecf20Sopenharmony_ci		} else if (mb[0] == MBS_LOOP_ID_USED) {
63378c2ecf20Sopenharmony_ci			/*
63388c2ecf20Sopenharmony_ci			 * Loop ID already used, try next loop ID.
63398c2ecf20Sopenharmony_ci			 */
63408c2ecf20Sopenharmony_ci			fcport->loop_id++;
63418c2ecf20Sopenharmony_ci			rval = qla2x00_find_new_loop_id(vha, fcport);
63428c2ecf20Sopenharmony_ci			if (rval != QLA_SUCCESS) {
63438c2ecf20Sopenharmony_ci				/* Ran out of loop IDs to use */
63448c2ecf20Sopenharmony_ci				break;
63458c2ecf20Sopenharmony_ci			}
63468c2ecf20Sopenharmony_ci		} else if (mb[0] == MBS_COMMAND_ERROR) {
63478c2ecf20Sopenharmony_ci			/*
63488c2ecf20Sopenharmony_ci			 * Firmware possibly timed out during login. If NO
63498c2ecf20Sopenharmony_ci			 * retries are left to do then the device is declared
63508c2ecf20Sopenharmony_ci			 * dead.
63518c2ecf20Sopenharmony_ci			 */
63528c2ecf20Sopenharmony_ci			*next_loopid = fcport->loop_id;
63538c2ecf20Sopenharmony_ci			ha->isp_ops->fabric_logout(vha, fcport->loop_id,
63548c2ecf20Sopenharmony_ci			    fcport->d_id.b.domain, fcport->d_id.b.area,
63558c2ecf20Sopenharmony_ci			    fcport->d_id.b.al_pa);
63568c2ecf20Sopenharmony_ci			qla2x00_mark_device_lost(vha, fcport, 1);
63578c2ecf20Sopenharmony_ci
63588c2ecf20Sopenharmony_ci			rval = 1;
63598c2ecf20Sopenharmony_ci			break;
63608c2ecf20Sopenharmony_ci		} else {
63618c2ecf20Sopenharmony_ci			/*
63628c2ecf20Sopenharmony_ci			 * unrecoverable / not handled error
63638c2ecf20Sopenharmony_ci			 */
63648c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_disc, vha, 0x2002,
63658c2ecf20Sopenharmony_ci			    "Failed=%x port_id=%02x%02x%02x loop_id=%x "
63668c2ecf20Sopenharmony_ci			    "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
63678c2ecf20Sopenharmony_ci			    fcport->d_id.b.area, fcport->d_id.b.al_pa,
63688c2ecf20Sopenharmony_ci			    fcport->loop_id, jiffies);
63698c2ecf20Sopenharmony_ci
63708c2ecf20Sopenharmony_ci			*next_loopid = fcport->loop_id;
63718c2ecf20Sopenharmony_ci			ha->isp_ops->fabric_logout(vha, fcport->loop_id,
63728c2ecf20Sopenharmony_ci			    fcport->d_id.b.domain, fcport->d_id.b.area,
63738c2ecf20Sopenharmony_ci			    fcport->d_id.b.al_pa);
63748c2ecf20Sopenharmony_ci			qla2x00_clear_loop_id(fcport);
63758c2ecf20Sopenharmony_ci			fcport->login_retry = 0;
63768c2ecf20Sopenharmony_ci
63778c2ecf20Sopenharmony_ci			rval = 3;
63788c2ecf20Sopenharmony_ci			break;
63798c2ecf20Sopenharmony_ci		}
63808c2ecf20Sopenharmony_ci	}
63818c2ecf20Sopenharmony_ci
63828c2ecf20Sopenharmony_ci	return (rval);
63838c2ecf20Sopenharmony_ci}
63848c2ecf20Sopenharmony_ci
63858c2ecf20Sopenharmony_ci/*
63868c2ecf20Sopenharmony_ci * qla2x00_local_device_login
63878c2ecf20Sopenharmony_ci *	Issue local device login command.
63888c2ecf20Sopenharmony_ci *
63898c2ecf20Sopenharmony_ci * Input:
63908c2ecf20Sopenharmony_ci *	ha = adapter block pointer.
63918c2ecf20Sopenharmony_ci *	loop_id = loop id of device to login to.
63928c2ecf20Sopenharmony_ci *
63938c2ecf20Sopenharmony_ci * Returns (Where's the #define!!!!):
63948c2ecf20Sopenharmony_ci *      0 - Login successfully
63958c2ecf20Sopenharmony_ci *      1 - Login failed
63968c2ecf20Sopenharmony_ci *      3 - Fatal error
63978c2ecf20Sopenharmony_ci */
63988c2ecf20Sopenharmony_ciint
63998c2ecf20Sopenharmony_ciqla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
64008c2ecf20Sopenharmony_ci{
64018c2ecf20Sopenharmony_ci	int		rval;
64028c2ecf20Sopenharmony_ci	uint16_t	mb[MAILBOX_REGISTER_COUNT];
64038c2ecf20Sopenharmony_ci
64048c2ecf20Sopenharmony_ci	memset(mb, 0, sizeof(mb));
64058c2ecf20Sopenharmony_ci	rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
64068c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS) {
64078c2ecf20Sopenharmony_ci		/* Interrogate mailbox registers for any errors */
64088c2ecf20Sopenharmony_ci		if (mb[0] == MBS_COMMAND_ERROR)
64098c2ecf20Sopenharmony_ci			rval = 1;
64108c2ecf20Sopenharmony_ci		else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
64118c2ecf20Sopenharmony_ci			/* device not in PCB table */
64128c2ecf20Sopenharmony_ci			rval = 3;
64138c2ecf20Sopenharmony_ci	}
64148c2ecf20Sopenharmony_ci
64158c2ecf20Sopenharmony_ci	return (rval);
64168c2ecf20Sopenharmony_ci}
64178c2ecf20Sopenharmony_ci
64188c2ecf20Sopenharmony_ci/*
64198c2ecf20Sopenharmony_ci *  qla2x00_loop_resync
64208c2ecf20Sopenharmony_ci *      Resync with fibre channel devices.
64218c2ecf20Sopenharmony_ci *
64228c2ecf20Sopenharmony_ci * Input:
64238c2ecf20Sopenharmony_ci *      ha = adapter block pointer.
64248c2ecf20Sopenharmony_ci *
64258c2ecf20Sopenharmony_ci * Returns:
64268c2ecf20Sopenharmony_ci *      0 = success
64278c2ecf20Sopenharmony_ci */
64288c2ecf20Sopenharmony_ciint
64298c2ecf20Sopenharmony_ciqla2x00_loop_resync(scsi_qla_host_t *vha)
64308c2ecf20Sopenharmony_ci{
64318c2ecf20Sopenharmony_ci	int rval = QLA_SUCCESS;
64328c2ecf20Sopenharmony_ci	uint32_t wait_time;
64338c2ecf20Sopenharmony_ci
64348c2ecf20Sopenharmony_ci	clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
64358c2ecf20Sopenharmony_ci	if (vha->flags.online) {
64368c2ecf20Sopenharmony_ci		if (!(rval = qla2x00_fw_ready(vha))) {
64378c2ecf20Sopenharmony_ci			/* Wait at most MAX_TARGET RSCNs for a stable link. */
64388c2ecf20Sopenharmony_ci			wait_time = 256;
64398c2ecf20Sopenharmony_ci			do {
64408c2ecf20Sopenharmony_ci				if (!IS_QLAFX00(vha->hw)) {
64418c2ecf20Sopenharmony_ci					/*
64428c2ecf20Sopenharmony_ci					 * Issue a marker after FW becomes
64438c2ecf20Sopenharmony_ci					 * ready.
64448c2ecf20Sopenharmony_ci					 */
64458c2ecf20Sopenharmony_ci					qla2x00_marker(vha, vha->hw->base_qpair,
64468c2ecf20Sopenharmony_ci					    0, 0, MK_SYNC_ALL);
64478c2ecf20Sopenharmony_ci					vha->marker_needed = 0;
64488c2ecf20Sopenharmony_ci				}
64498c2ecf20Sopenharmony_ci
64508c2ecf20Sopenharmony_ci				/* Remap devices on Loop. */
64518c2ecf20Sopenharmony_ci				clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
64528c2ecf20Sopenharmony_ci
64538c2ecf20Sopenharmony_ci				if (IS_QLAFX00(vha->hw))
64548c2ecf20Sopenharmony_ci					qlafx00_configure_devices(vha);
64558c2ecf20Sopenharmony_ci				else
64568c2ecf20Sopenharmony_ci					qla2x00_configure_loop(vha);
64578c2ecf20Sopenharmony_ci
64588c2ecf20Sopenharmony_ci				wait_time--;
64598c2ecf20Sopenharmony_ci			} while (!atomic_read(&vha->loop_down_timer) &&
64608c2ecf20Sopenharmony_ci				!(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
64618c2ecf20Sopenharmony_ci				&& wait_time && (test_bit(LOOP_RESYNC_NEEDED,
64628c2ecf20Sopenharmony_ci				&vha->dpc_flags)));
64638c2ecf20Sopenharmony_ci		}
64648c2ecf20Sopenharmony_ci	}
64658c2ecf20Sopenharmony_ci
64668c2ecf20Sopenharmony_ci	if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
64678c2ecf20Sopenharmony_ci		return (QLA_FUNCTION_FAILED);
64688c2ecf20Sopenharmony_ci
64698c2ecf20Sopenharmony_ci	if (rval)
64708c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_disc, vha, 0x206c,
64718c2ecf20Sopenharmony_ci		    "%s *** FAILED ***.\n", __func__);
64728c2ecf20Sopenharmony_ci
64738c2ecf20Sopenharmony_ci	return (rval);
64748c2ecf20Sopenharmony_ci}
64758c2ecf20Sopenharmony_ci
64768c2ecf20Sopenharmony_ci/*
64778c2ecf20Sopenharmony_ci* qla2x00_perform_loop_resync
64788c2ecf20Sopenharmony_ci* Description: This function will set the appropriate flags and call
64798c2ecf20Sopenharmony_ci*              qla2x00_loop_resync. If successful loop will be resynced
64808c2ecf20Sopenharmony_ci* Arguments : scsi_qla_host_t pointer
64818c2ecf20Sopenharmony_ci* returm    : Success or Failure
64828c2ecf20Sopenharmony_ci*/
64838c2ecf20Sopenharmony_ci
64848c2ecf20Sopenharmony_ciint qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
64858c2ecf20Sopenharmony_ci{
64868c2ecf20Sopenharmony_ci	int32_t rval = 0;
64878c2ecf20Sopenharmony_ci
64888c2ecf20Sopenharmony_ci	if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
64898c2ecf20Sopenharmony_ci		/*Configure the flags so that resync happens properly*/
64908c2ecf20Sopenharmony_ci		atomic_set(&ha->loop_down_timer, 0);
64918c2ecf20Sopenharmony_ci		if (!(ha->device_flags & DFLG_NO_CABLE)) {
64928c2ecf20Sopenharmony_ci			atomic_set(&ha->loop_state, LOOP_UP);
64938c2ecf20Sopenharmony_ci			set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
64948c2ecf20Sopenharmony_ci			set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
64958c2ecf20Sopenharmony_ci			set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
64968c2ecf20Sopenharmony_ci
64978c2ecf20Sopenharmony_ci			rval = qla2x00_loop_resync(ha);
64988c2ecf20Sopenharmony_ci		} else
64998c2ecf20Sopenharmony_ci			atomic_set(&ha->loop_state, LOOP_DEAD);
65008c2ecf20Sopenharmony_ci
65018c2ecf20Sopenharmony_ci		clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
65028c2ecf20Sopenharmony_ci	}
65038c2ecf20Sopenharmony_ci
65048c2ecf20Sopenharmony_ci	return rval;
65058c2ecf20Sopenharmony_ci}
65068c2ecf20Sopenharmony_ci
65078c2ecf20Sopenharmony_civoid
65088c2ecf20Sopenharmony_ciqla2x00_update_fcports(scsi_qla_host_t *base_vha)
65098c2ecf20Sopenharmony_ci{
65108c2ecf20Sopenharmony_ci	fc_port_t *fcport;
65118c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha;
65128c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = base_vha->hw;
65138c2ecf20Sopenharmony_ci	unsigned long flags;
65148c2ecf20Sopenharmony_ci
65158c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->vport_slock, flags);
65168c2ecf20Sopenharmony_ci	/* Go with deferred removal of rport references. */
65178c2ecf20Sopenharmony_ci	list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
65188c2ecf20Sopenharmony_ci		atomic_inc(&vha->vref_count);
65198c2ecf20Sopenharmony_ci		list_for_each_entry(fcport, &vha->vp_fcports, list) {
65208c2ecf20Sopenharmony_ci			if (fcport->drport &&
65218c2ecf20Sopenharmony_ci			    atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
65228c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&ha->vport_slock, flags);
65238c2ecf20Sopenharmony_ci				qla2x00_rport_del(fcport);
65248c2ecf20Sopenharmony_ci
65258c2ecf20Sopenharmony_ci				spin_lock_irqsave(&ha->vport_slock, flags);
65268c2ecf20Sopenharmony_ci			}
65278c2ecf20Sopenharmony_ci		}
65288c2ecf20Sopenharmony_ci		atomic_dec(&vha->vref_count);
65298c2ecf20Sopenharmony_ci		wake_up(&vha->vref_waitq);
65308c2ecf20Sopenharmony_ci	}
65318c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->vport_slock, flags);
65328c2ecf20Sopenharmony_ci}
65338c2ecf20Sopenharmony_ci
65348c2ecf20Sopenharmony_ci/* Assumes idc_lock always held on entry */
65358c2ecf20Sopenharmony_civoid
65368c2ecf20Sopenharmony_ciqla83xx_reset_ownership(scsi_qla_host_t *vha)
65378c2ecf20Sopenharmony_ci{
65388c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
65398c2ecf20Sopenharmony_ci	uint32_t drv_presence, drv_presence_mask;
65408c2ecf20Sopenharmony_ci	uint32_t dev_part_info1, dev_part_info2, class_type;
65418c2ecf20Sopenharmony_ci	uint32_t class_type_mask = 0x3;
65428c2ecf20Sopenharmony_ci	uint16_t fcoe_other_function = 0xffff, i;
65438c2ecf20Sopenharmony_ci
65448c2ecf20Sopenharmony_ci	if (IS_QLA8044(ha)) {
65458c2ecf20Sopenharmony_ci		drv_presence = qla8044_rd_direct(vha,
65468c2ecf20Sopenharmony_ci		    QLA8044_CRB_DRV_ACTIVE_INDEX);
65478c2ecf20Sopenharmony_ci		dev_part_info1 = qla8044_rd_direct(vha,
65488c2ecf20Sopenharmony_ci		    QLA8044_CRB_DEV_PART_INFO_INDEX);
65498c2ecf20Sopenharmony_ci		dev_part_info2 = qla8044_rd_direct(vha,
65508c2ecf20Sopenharmony_ci		    QLA8044_CRB_DEV_PART_INFO2);
65518c2ecf20Sopenharmony_ci	} else {
65528c2ecf20Sopenharmony_ci		qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
65538c2ecf20Sopenharmony_ci		qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
65548c2ecf20Sopenharmony_ci		qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
65558c2ecf20Sopenharmony_ci	}
65568c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++) {
65578c2ecf20Sopenharmony_ci		class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
65588c2ecf20Sopenharmony_ci		if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
65598c2ecf20Sopenharmony_ci		    (i != ha->portnum)) {
65608c2ecf20Sopenharmony_ci			fcoe_other_function = i;
65618c2ecf20Sopenharmony_ci			break;
65628c2ecf20Sopenharmony_ci		}
65638c2ecf20Sopenharmony_ci	}
65648c2ecf20Sopenharmony_ci	if (fcoe_other_function == 0xffff) {
65658c2ecf20Sopenharmony_ci		for (i = 0; i < 8; i++) {
65668c2ecf20Sopenharmony_ci			class_type = ((dev_part_info2 >> (i * 4)) &
65678c2ecf20Sopenharmony_ci			    class_type_mask);
65688c2ecf20Sopenharmony_ci			if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
65698c2ecf20Sopenharmony_ci			    ((i + 8) != ha->portnum)) {
65708c2ecf20Sopenharmony_ci				fcoe_other_function = i + 8;
65718c2ecf20Sopenharmony_ci				break;
65728c2ecf20Sopenharmony_ci			}
65738c2ecf20Sopenharmony_ci		}
65748c2ecf20Sopenharmony_ci	}
65758c2ecf20Sopenharmony_ci	/*
65768c2ecf20Sopenharmony_ci	 * Prepare drv-presence mask based on fcoe functions present.
65778c2ecf20Sopenharmony_ci	 * However consider only valid physical fcoe function numbers (0-15).
65788c2ecf20Sopenharmony_ci	 */
65798c2ecf20Sopenharmony_ci	drv_presence_mask = ~((1 << (ha->portnum)) |
65808c2ecf20Sopenharmony_ci			((fcoe_other_function == 0xffff) ?
65818c2ecf20Sopenharmony_ci			 0 : (1 << (fcoe_other_function))));
65828c2ecf20Sopenharmony_ci
65838c2ecf20Sopenharmony_ci	/* We are the reset owner iff:
65848c2ecf20Sopenharmony_ci	 *    - No other protocol drivers present.
65858c2ecf20Sopenharmony_ci	 *    - This is the lowest among fcoe functions. */
65868c2ecf20Sopenharmony_ci	if (!(drv_presence & drv_presence_mask) &&
65878c2ecf20Sopenharmony_ci			(ha->portnum < fcoe_other_function)) {
65888c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_p3p, vha, 0xb07f,
65898c2ecf20Sopenharmony_ci		    "This host is Reset owner.\n");
65908c2ecf20Sopenharmony_ci		ha->flags.nic_core_reset_owner = 1;
65918c2ecf20Sopenharmony_ci	}
65928c2ecf20Sopenharmony_ci}
65938c2ecf20Sopenharmony_ci
65948c2ecf20Sopenharmony_cistatic int
65958c2ecf20Sopenharmony_ci__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
65968c2ecf20Sopenharmony_ci{
65978c2ecf20Sopenharmony_ci	int rval = QLA_SUCCESS;
65988c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
65998c2ecf20Sopenharmony_ci	uint32_t drv_ack;
66008c2ecf20Sopenharmony_ci
66018c2ecf20Sopenharmony_ci	rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
66028c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS) {
66038c2ecf20Sopenharmony_ci		drv_ack |= (1 << ha->portnum);
66048c2ecf20Sopenharmony_ci		rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
66058c2ecf20Sopenharmony_ci	}
66068c2ecf20Sopenharmony_ci
66078c2ecf20Sopenharmony_ci	return rval;
66088c2ecf20Sopenharmony_ci}
66098c2ecf20Sopenharmony_ci
66108c2ecf20Sopenharmony_cistatic int
66118c2ecf20Sopenharmony_ci__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
66128c2ecf20Sopenharmony_ci{
66138c2ecf20Sopenharmony_ci	int rval = QLA_SUCCESS;
66148c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
66158c2ecf20Sopenharmony_ci	uint32_t drv_ack;
66168c2ecf20Sopenharmony_ci
66178c2ecf20Sopenharmony_ci	rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
66188c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS) {
66198c2ecf20Sopenharmony_ci		drv_ack &= ~(1 << ha->portnum);
66208c2ecf20Sopenharmony_ci		rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
66218c2ecf20Sopenharmony_ci	}
66228c2ecf20Sopenharmony_ci
66238c2ecf20Sopenharmony_ci	return rval;
66248c2ecf20Sopenharmony_ci}
66258c2ecf20Sopenharmony_ci
66268c2ecf20Sopenharmony_cistatic const char *
66278c2ecf20Sopenharmony_ciqla83xx_dev_state_to_string(uint32_t dev_state)
66288c2ecf20Sopenharmony_ci{
66298c2ecf20Sopenharmony_ci	switch (dev_state) {
66308c2ecf20Sopenharmony_ci	case QLA8XXX_DEV_COLD:
66318c2ecf20Sopenharmony_ci		return "COLD/RE-INIT";
66328c2ecf20Sopenharmony_ci	case QLA8XXX_DEV_INITIALIZING:
66338c2ecf20Sopenharmony_ci		return "INITIALIZING";
66348c2ecf20Sopenharmony_ci	case QLA8XXX_DEV_READY:
66358c2ecf20Sopenharmony_ci		return "READY";
66368c2ecf20Sopenharmony_ci	case QLA8XXX_DEV_NEED_RESET:
66378c2ecf20Sopenharmony_ci		return "NEED RESET";
66388c2ecf20Sopenharmony_ci	case QLA8XXX_DEV_NEED_QUIESCENT:
66398c2ecf20Sopenharmony_ci		return "NEED QUIESCENT";
66408c2ecf20Sopenharmony_ci	case QLA8XXX_DEV_FAILED:
66418c2ecf20Sopenharmony_ci		return "FAILED";
66428c2ecf20Sopenharmony_ci	case QLA8XXX_DEV_QUIESCENT:
66438c2ecf20Sopenharmony_ci		return "QUIESCENT";
66448c2ecf20Sopenharmony_ci	default:
66458c2ecf20Sopenharmony_ci		return "Unknown";
66468c2ecf20Sopenharmony_ci	}
66478c2ecf20Sopenharmony_ci}
66488c2ecf20Sopenharmony_ci
66498c2ecf20Sopenharmony_ci/* Assumes idc-lock always held on entry */
66508c2ecf20Sopenharmony_civoid
66518c2ecf20Sopenharmony_ciqla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
66528c2ecf20Sopenharmony_ci{
66538c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
66548c2ecf20Sopenharmony_ci	uint32_t idc_audit_reg = 0, duration_secs = 0;
66558c2ecf20Sopenharmony_ci
66568c2ecf20Sopenharmony_ci	switch (audit_type) {
66578c2ecf20Sopenharmony_ci	case IDC_AUDIT_TIMESTAMP:
66588c2ecf20Sopenharmony_ci		ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
66598c2ecf20Sopenharmony_ci		idc_audit_reg = (ha->portnum) |
66608c2ecf20Sopenharmony_ci		    (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
66618c2ecf20Sopenharmony_ci		qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
66628c2ecf20Sopenharmony_ci		break;
66638c2ecf20Sopenharmony_ci
66648c2ecf20Sopenharmony_ci	case IDC_AUDIT_COMPLETION:
66658c2ecf20Sopenharmony_ci		duration_secs = ((jiffies_to_msecs(jiffies) -
66668c2ecf20Sopenharmony_ci		    jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
66678c2ecf20Sopenharmony_ci		idc_audit_reg = (ha->portnum) |
66688c2ecf20Sopenharmony_ci		    (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
66698c2ecf20Sopenharmony_ci		qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
66708c2ecf20Sopenharmony_ci		break;
66718c2ecf20Sopenharmony_ci
66728c2ecf20Sopenharmony_ci	default:
66738c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xb078,
66748c2ecf20Sopenharmony_ci		    "Invalid audit type specified.\n");
66758c2ecf20Sopenharmony_ci		break;
66768c2ecf20Sopenharmony_ci	}
66778c2ecf20Sopenharmony_ci}
66788c2ecf20Sopenharmony_ci
66798c2ecf20Sopenharmony_ci/* Assumes idc_lock always held on entry */
66808c2ecf20Sopenharmony_cistatic int
66818c2ecf20Sopenharmony_ciqla83xx_initiating_reset(scsi_qla_host_t *vha)
66828c2ecf20Sopenharmony_ci{
66838c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
66848c2ecf20Sopenharmony_ci	uint32_t  idc_control, dev_state;
66858c2ecf20Sopenharmony_ci
66868c2ecf20Sopenharmony_ci	__qla83xx_get_idc_control(vha, &idc_control);
66878c2ecf20Sopenharmony_ci	if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
66888c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0xb080,
66898c2ecf20Sopenharmony_ci		    "NIC Core reset has been disabled. idc-control=0x%x\n",
66908c2ecf20Sopenharmony_ci		    idc_control);
66918c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
66928c2ecf20Sopenharmony_ci	}
66938c2ecf20Sopenharmony_ci
66948c2ecf20Sopenharmony_ci	/* Set NEED-RESET iff in READY state and we are the reset-owner */
66958c2ecf20Sopenharmony_ci	qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
66968c2ecf20Sopenharmony_ci	if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
66978c2ecf20Sopenharmony_ci		qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
66988c2ecf20Sopenharmony_ci		    QLA8XXX_DEV_NEED_RESET);
66998c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
67008c2ecf20Sopenharmony_ci		qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
67018c2ecf20Sopenharmony_ci	} else {
67028c2ecf20Sopenharmony_ci		const char *state = qla83xx_dev_state_to_string(dev_state);
67038c2ecf20Sopenharmony_ci
67048c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
67058c2ecf20Sopenharmony_ci
67068c2ecf20Sopenharmony_ci		/* SV: XXX: Is timeout required here? */
67078c2ecf20Sopenharmony_ci		/* Wait for IDC state change READY -> NEED_RESET */
67088c2ecf20Sopenharmony_ci		while (dev_state == QLA8XXX_DEV_READY) {
67098c2ecf20Sopenharmony_ci			qla83xx_idc_unlock(vha, 0);
67108c2ecf20Sopenharmony_ci			msleep(200);
67118c2ecf20Sopenharmony_ci			qla83xx_idc_lock(vha, 0);
67128c2ecf20Sopenharmony_ci			qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
67138c2ecf20Sopenharmony_ci		}
67148c2ecf20Sopenharmony_ci	}
67158c2ecf20Sopenharmony_ci
67168c2ecf20Sopenharmony_ci	/* Send IDC ack by writing to drv-ack register */
67178c2ecf20Sopenharmony_ci	__qla83xx_set_drv_ack(vha);
67188c2ecf20Sopenharmony_ci
67198c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
67208c2ecf20Sopenharmony_ci}
67218c2ecf20Sopenharmony_ci
67228c2ecf20Sopenharmony_ciint
67238c2ecf20Sopenharmony_ci__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
67248c2ecf20Sopenharmony_ci{
67258c2ecf20Sopenharmony_ci	return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
67268c2ecf20Sopenharmony_ci}
67278c2ecf20Sopenharmony_ci
67288c2ecf20Sopenharmony_ciint
67298c2ecf20Sopenharmony_ci__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
67308c2ecf20Sopenharmony_ci{
67318c2ecf20Sopenharmony_ci	return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
67328c2ecf20Sopenharmony_ci}
67338c2ecf20Sopenharmony_ci
67348c2ecf20Sopenharmony_cistatic int
67358c2ecf20Sopenharmony_ciqla83xx_check_driver_presence(scsi_qla_host_t *vha)
67368c2ecf20Sopenharmony_ci{
67378c2ecf20Sopenharmony_ci	uint32_t drv_presence = 0;
67388c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
67398c2ecf20Sopenharmony_ci
67408c2ecf20Sopenharmony_ci	qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
67418c2ecf20Sopenharmony_ci	if (drv_presence & (1 << ha->portnum))
67428c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
67438c2ecf20Sopenharmony_ci	else
67448c2ecf20Sopenharmony_ci		return QLA_TEST_FAILED;
67458c2ecf20Sopenharmony_ci}
67468c2ecf20Sopenharmony_ci
67478c2ecf20Sopenharmony_ciint
67488c2ecf20Sopenharmony_ciqla83xx_nic_core_reset(scsi_qla_host_t *vha)
67498c2ecf20Sopenharmony_ci{
67508c2ecf20Sopenharmony_ci	int rval = QLA_SUCCESS;
67518c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
67528c2ecf20Sopenharmony_ci
67538c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_p3p, vha, 0xb058,
67548c2ecf20Sopenharmony_ci	    "Entered  %s().\n", __func__);
67558c2ecf20Sopenharmony_ci
67568c2ecf20Sopenharmony_ci	if (vha->device_flags & DFLG_DEV_FAILED) {
67578c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xb059,
67588c2ecf20Sopenharmony_ci		    "Device in unrecoverable FAILED state.\n");
67598c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
67608c2ecf20Sopenharmony_ci	}
67618c2ecf20Sopenharmony_ci
67628c2ecf20Sopenharmony_ci	qla83xx_idc_lock(vha, 0);
67638c2ecf20Sopenharmony_ci
67648c2ecf20Sopenharmony_ci	if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
67658c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0xb05a,
67668c2ecf20Sopenharmony_ci		    "Function=0x%x has been removed from IDC participation.\n",
67678c2ecf20Sopenharmony_ci		    ha->portnum);
67688c2ecf20Sopenharmony_ci		rval = QLA_FUNCTION_FAILED;
67698c2ecf20Sopenharmony_ci		goto exit;
67708c2ecf20Sopenharmony_ci	}
67718c2ecf20Sopenharmony_ci
67728c2ecf20Sopenharmony_ci	qla83xx_reset_ownership(vha);
67738c2ecf20Sopenharmony_ci
67748c2ecf20Sopenharmony_ci	rval = qla83xx_initiating_reset(vha);
67758c2ecf20Sopenharmony_ci
67768c2ecf20Sopenharmony_ci	/*
67778c2ecf20Sopenharmony_ci	 * Perform reset if we are the reset-owner,
67788c2ecf20Sopenharmony_ci	 * else wait till IDC state changes to READY/FAILED.
67798c2ecf20Sopenharmony_ci	 */
67808c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS) {
67818c2ecf20Sopenharmony_ci		rval = qla83xx_idc_state_handler(vha);
67828c2ecf20Sopenharmony_ci
67838c2ecf20Sopenharmony_ci		if (rval == QLA_SUCCESS)
67848c2ecf20Sopenharmony_ci			ha->flags.nic_core_hung = 0;
67858c2ecf20Sopenharmony_ci		__qla83xx_clear_drv_ack(vha);
67868c2ecf20Sopenharmony_ci	}
67878c2ecf20Sopenharmony_ci
67888c2ecf20Sopenharmony_ciexit:
67898c2ecf20Sopenharmony_ci	qla83xx_idc_unlock(vha, 0);
67908c2ecf20Sopenharmony_ci
67918c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
67928c2ecf20Sopenharmony_ci
67938c2ecf20Sopenharmony_ci	return rval;
67948c2ecf20Sopenharmony_ci}
67958c2ecf20Sopenharmony_ci
67968c2ecf20Sopenharmony_ciint
67978c2ecf20Sopenharmony_ciqla2xxx_mctp_dump(scsi_qla_host_t *vha)
67988c2ecf20Sopenharmony_ci{
67998c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
68008c2ecf20Sopenharmony_ci	int rval = QLA_FUNCTION_FAILED;
68018c2ecf20Sopenharmony_ci
68028c2ecf20Sopenharmony_ci	if (!IS_MCTP_CAPABLE(ha)) {
68038c2ecf20Sopenharmony_ci		/* This message can be removed from the final version */
68048c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x506d,
68058c2ecf20Sopenharmony_ci		    "This board is not MCTP capable\n");
68068c2ecf20Sopenharmony_ci		return rval;
68078c2ecf20Sopenharmony_ci	}
68088c2ecf20Sopenharmony_ci
68098c2ecf20Sopenharmony_ci	if (!ha->mctp_dump) {
68108c2ecf20Sopenharmony_ci		ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
68118c2ecf20Sopenharmony_ci		    MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
68128c2ecf20Sopenharmony_ci
68138c2ecf20Sopenharmony_ci		if (!ha->mctp_dump) {
68148c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x506e,
68158c2ecf20Sopenharmony_ci			    "Failed to allocate memory for mctp dump\n");
68168c2ecf20Sopenharmony_ci			return rval;
68178c2ecf20Sopenharmony_ci		}
68188c2ecf20Sopenharmony_ci	}
68198c2ecf20Sopenharmony_ci
68208c2ecf20Sopenharmony_ci#define MCTP_DUMP_STR_ADDR	0x00000000
68218c2ecf20Sopenharmony_ci	rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
68228c2ecf20Sopenharmony_ci	    MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
68238c2ecf20Sopenharmony_ci	if (rval != QLA_SUCCESS) {
68248c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x506f,
68258c2ecf20Sopenharmony_ci		    "Failed to capture mctp dump\n");
68268c2ecf20Sopenharmony_ci	} else {
68278c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x5070,
68288c2ecf20Sopenharmony_ci		    "Mctp dump capture for host (%ld/%p).\n",
68298c2ecf20Sopenharmony_ci		    vha->host_no, ha->mctp_dump);
68308c2ecf20Sopenharmony_ci		ha->mctp_dumped = 1;
68318c2ecf20Sopenharmony_ci	}
68328c2ecf20Sopenharmony_ci
68338c2ecf20Sopenharmony_ci	if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
68348c2ecf20Sopenharmony_ci		ha->flags.nic_core_reset_hdlr_active = 1;
68358c2ecf20Sopenharmony_ci		rval = qla83xx_restart_nic_firmware(vha);
68368c2ecf20Sopenharmony_ci		if (rval)
68378c2ecf20Sopenharmony_ci			/* NIC Core reset failed. */
68388c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x5071,
68398c2ecf20Sopenharmony_ci			    "Failed to restart nic firmware\n");
68408c2ecf20Sopenharmony_ci		else
68418c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_p3p, vha, 0xb084,
68428c2ecf20Sopenharmony_ci			    "Restarted NIC firmware successfully.\n");
68438c2ecf20Sopenharmony_ci		ha->flags.nic_core_reset_hdlr_active = 0;
68448c2ecf20Sopenharmony_ci	}
68458c2ecf20Sopenharmony_ci
68468c2ecf20Sopenharmony_ci	return rval;
68478c2ecf20Sopenharmony_ci
68488c2ecf20Sopenharmony_ci}
68498c2ecf20Sopenharmony_ci
68508c2ecf20Sopenharmony_ci/*
68518c2ecf20Sopenharmony_ci* qla2x00_quiesce_io
68528c2ecf20Sopenharmony_ci* Description: This function will block the new I/Os
68538c2ecf20Sopenharmony_ci*              Its not aborting any I/Os as context
68548c2ecf20Sopenharmony_ci*              is not destroyed during quiescence
68558c2ecf20Sopenharmony_ci* Arguments: scsi_qla_host_t
68568c2ecf20Sopenharmony_ci* return   : void
68578c2ecf20Sopenharmony_ci*/
68588c2ecf20Sopenharmony_civoid
68598c2ecf20Sopenharmony_ciqla2x00_quiesce_io(scsi_qla_host_t *vha)
68608c2ecf20Sopenharmony_ci{
68618c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
68628c2ecf20Sopenharmony_ci	struct scsi_qla_host *vp;
68638c2ecf20Sopenharmony_ci
68648c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_dpc, vha, 0x401d,
68658c2ecf20Sopenharmony_ci	    "Quiescing I/O - ha=%p.\n", ha);
68668c2ecf20Sopenharmony_ci
68678c2ecf20Sopenharmony_ci	atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
68688c2ecf20Sopenharmony_ci	if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
68698c2ecf20Sopenharmony_ci		atomic_set(&vha->loop_state, LOOP_DOWN);
68708c2ecf20Sopenharmony_ci		qla2x00_mark_all_devices_lost(vha);
68718c2ecf20Sopenharmony_ci		list_for_each_entry(vp, &ha->vp_list, list)
68728c2ecf20Sopenharmony_ci			qla2x00_mark_all_devices_lost(vp);
68738c2ecf20Sopenharmony_ci	} else {
68748c2ecf20Sopenharmony_ci		if (!atomic_read(&vha->loop_down_timer))
68758c2ecf20Sopenharmony_ci			atomic_set(&vha->loop_down_timer,
68768c2ecf20Sopenharmony_ci					LOOP_DOWN_TIME);
68778c2ecf20Sopenharmony_ci	}
68788c2ecf20Sopenharmony_ci	/* Wait for pending cmds to complete */
68798c2ecf20Sopenharmony_ci	WARN_ON_ONCE(qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST)
68808c2ecf20Sopenharmony_ci		     != QLA_SUCCESS);
68818c2ecf20Sopenharmony_ci}
68828c2ecf20Sopenharmony_ci
68838c2ecf20Sopenharmony_civoid
68848c2ecf20Sopenharmony_ciqla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
68858c2ecf20Sopenharmony_ci{
68868c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
68878c2ecf20Sopenharmony_ci	struct scsi_qla_host *vp;
68888c2ecf20Sopenharmony_ci	unsigned long flags;
68898c2ecf20Sopenharmony_ci	fc_port_t *fcport;
68908c2ecf20Sopenharmony_ci	u16 i;
68918c2ecf20Sopenharmony_ci
68928c2ecf20Sopenharmony_ci	/* For ISP82XX, driver waits for completion of the commands.
68938c2ecf20Sopenharmony_ci	 * online flag should be set.
68948c2ecf20Sopenharmony_ci	 */
68958c2ecf20Sopenharmony_ci	if (!(IS_P3P_TYPE(ha)))
68968c2ecf20Sopenharmony_ci		vha->flags.online = 0;
68978c2ecf20Sopenharmony_ci	ha->flags.chip_reset_done = 0;
68988c2ecf20Sopenharmony_ci	clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
68998c2ecf20Sopenharmony_ci	vha->qla_stats.total_isp_aborts++;
69008c2ecf20Sopenharmony_ci
69018c2ecf20Sopenharmony_ci	ql_log(ql_log_info, vha, 0x00af,
69028c2ecf20Sopenharmony_ci	    "Performing ISP error recovery - ha=%p.\n", ha);
69038c2ecf20Sopenharmony_ci
69048c2ecf20Sopenharmony_ci	ha->flags.purge_mbox = 1;
69058c2ecf20Sopenharmony_ci	/* For ISP82XX, reset_chip is just disabling interrupts.
69068c2ecf20Sopenharmony_ci	 * Driver waits for the completion of the commands.
69078c2ecf20Sopenharmony_ci	 * the interrupts need to be enabled.
69088c2ecf20Sopenharmony_ci	 */
69098c2ecf20Sopenharmony_ci	if (!(IS_P3P_TYPE(ha)))
69108c2ecf20Sopenharmony_ci		ha->isp_ops->reset_chip(vha);
69118c2ecf20Sopenharmony_ci
69128c2ecf20Sopenharmony_ci	ha->link_data_rate = PORT_SPEED_UNKNOWN;
69138c2ecf20Sopenharmony_ci	SAVE_TOPO(ha);
69148c2ecf20Sopenharmony_ci	ha->flags.rida_fmt2 = 0;
69158c2ecf20Sopenharmony_ci	ha->flags.n2n_ae = 0;
69168c2ecf20Sopenharmony_ci	ha->flags.lip_ae = 0;
69178c2ecf20Sopenharmony_ci	ha->current_topology = 0;
69188c2ecf20Sopenharmony_ci	QLA_FW_STOPPED(ha);
69198c2ecf20Sopenharmony_ci	ha->flags.fw_init_done = 0;
69208c2ecf20Sopenharmony_ci	ha->chip_reset++;
69218c2ecf20Sopenharmony_ci	ha->base_qpair->chip_reset = ha->chip_reset;
69228c2ecf20Sopenharmony_ci	for (i = 0; i < ha->max_qpairs; i++) {
69238c2ecf20Sopenharmony_ci		if (ha->queue_pair_map[i])
69248c2ecf20Sopenharmony_ci			ha->queue_pair_map[i]->chip_reset =
69258c2ecf20Sopenharmony_ci				ha->base_qpair->chip_reset;
69268c2ecf20Sopenharmony_ci	}
69278c2ecf20Sopenharmony_ci
69288c2ecf20Sopenharmony_ci	/* purge MBox commands */
69298c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
69308c2ecf20Sopenharmony_ci	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags)) {
69318c2ecf20Sopenharmony_ci		clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
69328c2ecf20Sopenharmony_ci		complete(&ha->mbx_intr_comp);
69338c2ecf20Sopenharmony_ci	}
69348c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
69358c2ecf20Sopenharmony_ci
69368c2ecf20Sopenharmony_ci	i = 0;
69378c2ecf20Sopenharmony_ci	while (atomic_read(&ha->num_pend_mbx_stage2) ||
69388c2ecf20Sopenharmony_ci	    atomic_read(&ha->num_pend_mbx_stage1)) {
69398c2ecf20Sopenharmony_ci		msleep(20);
69408c2ecf20Sopenharmony_ci		i++;
69418c2ecf20Sopenharmony_ci		if (i > 50)
69428c2ecf20Sopenharmony_ci			break;
69438c2ecf20Sopenharmony_ci	}
69448c2ecf20Sopenharmony_ci	ha->flags.purge_mbox = 0;
69458c2ecf20Sopenharmony_ci
69468c2ecf20Sopenharmony_ci	atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
69478c2ecf20Sopenharmony_ci	if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
69488c2ecf20Sopenharmony_ci		atomic_set(&vha->loop_state, LOOP_DOWN);
69498c2ecf20Sopenharmony_ci		qla2x00_mark_all_devices_lost(vha);
69508c2ecf20Sopenharmony_ci
69518c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->vport_slock, flags);
69528c2ecf20Sopenharmony_ci		list_for_each_entry(vp, &ha->vp_list, list) {
69538c2ecf20Sopenharmony_ci			atomic_inc(&vp->vref_count);
69548c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&ha->vport_slock, flags);
69558c2ecf20Sopenharmony_ci
69568c2ecf20Sopenharmony_ci			qla2x00_mark_all_devices_lost(vp);
69578c2ecf20Sopenharmony_ci
69588c2ecf20Sopenharmony_ci			spin_lock_irqsave(&ha->vport_slock, flags);
69598c2ecf20Sopenharmony_ci			atomic_dec(&vp->vref_count);
69608c2ecf20Sopenharmony_ci		}
69618c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->vport_slock, flags);
69628c2ecf20Sopenharmony_ci	} else {
69638c2ecf20Sopenharmony_ci		if (!atomic_read(&vha->loop_down_timer))
69648c2ecf20Sopenharmony_ci			atomic_set(&vha->loop_down_timer,
69658c2ecf20Sopenharmony_ci			    LOOP_DOWN_TIME);
69668c2ecf20Sopenharmony_ci	}
69678c2ecf20Sopenharmony_ci
69688c2ecf20Sopenharmony_ci	/* Clear all async request states across all VPs. */
69698c2ecf20Sopenharmony_ci	list_for_each_entry(fcport, &vha->vp_fcports, list) {
69708c2ecf20Sopenharmony_ci		fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
69718c2ecf20Sopenharmony_ci		fcport->scan_state = 0;
69728c2ecf20Sopenharmony_ci	}
69738c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->vport_slock, flags);
69748c2ecf20Sopenharmony_ci	list_for_each_entry(vp, &ha->vp_list, list) {
69758c2ecf20Sopenharmony_ci		atomic_inc(&vp->vref_count);
69768c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->vport_slock, flags);
69778c2ecf20Sopenharmony_ci
69788c2ecf20Sopenharmony_ci		list_for_each_entry(fcport, &vp->vp_fcports, list)
69798c2ecf20Sopenharmony_ci			fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
69808c2ecf20Sopenharmony_ci
69818c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->vport_slock, flags);
69828c2ecf20Sopenharmony_ci		atomic_dec(&vp->vref_count);
69838c2ecf20Sopenharmony_ci	}
69848c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->vport_slock, flags);
69858c2ecf20Sopenharmony_ci
69868c2ecf20Sopenharmony_ci	/* Make sure for ISP 82XX IO DMA is complete */
69878c2ecf20Sopenharmony_ci	if (IS_P3P_TYPE(ha)) {
69888c2ecf20Sopenharmony_ci		qla82xx_chip_reset_cleanup(vha);
69898c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x00b4,
69908c2ecf20Sopenharmony_ci		       "Done chip reset cleanup.\n");
69918c2ecf20Sopenharmony_ci
69928c2ecf20Sopenharmony_ci		/* Done waiting for pending commands. Reset online flag */
69938c2ecf20Sopenharmony_ci		vha->flags.online = 0;
69948c2ecf20Sopenharmony_ci	}
69958c2ecf20Sopenharmony_ci
69968c2ecf20Sopenharmony_ci	/* Requeue all commands in outstanding command list. */
69978c2ecf20Sopenharmony_ci	qla2x00_abort_all_cmds(vha, DID_RESET << 16);
69988c2ecf20Sopenharmony_ci	/* memory barrier */
69998c2ecf20Sopenharmony_ci	wmb();
70008c2ecf20Sopenharmony_ci}
70018c2ecf20Sopenharmony_ci
70028c2ecf20Sopenharmony_ci/*
70038c2ecf20Sopenharmony_ci*  qla2x00_abort_isp
70048c2ecf20Sopenharmony_ci*      Resets ISP and aborts all outstanding commands.
70058c2ecf20Sopenharmony_ci*
70068c2ecf20Sopenharmony_ci* Input:
70078c2ecf20Sopenharmony_ci*      ha           = adapter block pointer.
70088c2ecf20Sopenharmony_ci*
70098c2ecf20Sopenharmony_ci* Returns:
70108c2ecf20Sopenharmony_ci*      0 = success
70118c2ecf20Sopenharmony_ci*/
70128c2ecf20Sopenharmony_ciint
70138c2ecf20Sopenharmony_ciqla2x00_abort_isp(scsi_qla_host_t *vha)
70148c2ecf20Sopenharmony_ci{
70158c2ecf20Sopenharmony_ci	int rval;
70168c2ecf20Sopenharmony_ci	uint8_t        status = 0;
70178c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
70188c2ecf20Sopenharmony_ci	struct scsi_qla_host *vp;
70198c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
70208c2ecf20Sopenharmony_ci	unsigned long flags;
70218c2ecf20Sopenharmony_ci
70228c2ecf20Sopenharmony_ci	if (vha->flags.online) {
70238c2ecf20Sopenharmony_ci		qla2x00_abort_isp_cleanup(vha);
70248c2ecf20Sopenharmony_ci
70258c2ecf20Sopenharmony_ci		if (qla2x00_isp_reg_stat(ha)) {
70268c2ecf20Sopenharmony_ci			ql_log(ql_log_info, vha, 0x803f,
70278c2ecf20Sopenharmony_ci			       "ISP Abort - ISP reg disconnect, exiting.\n");
70288c2ecf20Sopenharmony_ci			return status;
70298c2ecf20Sopenharmony_ci		}
70308c2ecf20Sopenharmony_ci
70318c2ecf20Sopenharmony_ci		if (test_and_clear_bit(ISP_ABORT_TO_ROM, &vha->dpc_flags)) {
70328c2ecf20Sopenharmony_ci			ha->flags.chip_reset_done = 1;
70338c2ecf20Sopenharmony_ci			vha->flags.online = 1;
70348c2ecf20Sopenharmony_ci			status = 0;
70358c2ecf20Sopenharmony_ci			clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
70368c2ecf20Sopenharmony_ci			return status;
70378c2ecf20Sopenharmony_ci		}
70388c2ecf20Sopenharmony_ci
70398c2ecf20Sopenharmony_ci		if (IS_QLA8031(ha)) {
70408c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_p3p, vha, 0xb05c,
70418c2ecf20Sopenharmony_ci			    "Clearing fcoe driver presence.\n");
70428c2ecf20Sopenharmony_ci			if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
70438c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_p3p, vha, 0xb073,
70448c2ecf20Sopenharmony_ci				    "Error while clearing DRV-Presence.\n");
70458c2ecf20Sopenharmony_ci		}
70468c2ecf20Sopenharmony_ci
70478c2ecf20Sopenharmony_ci		if (unlikely(pci_channel_offline(ha->pdev) &&
70488c2ecf20Sopenharmony_ci		    ha->flags.pci_channel_io_perm_failure)) {
70498c2ecf20Sopenharmony_ci			clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
70508c2ecf20Sopenharmony_ci			status = 0;
70518c2ecf20Sopenharmony_ci			return status;
70528c2ecf20Sopenharmony_ci		}
70538c2ecf20Sopenharmony_ci
70548c2ecf20Sopenharmony_ci		switch (vha->qlini_mode) {
70558c2ecf20Sopenharmony_ci		case QLA2XXX_INI_MODE_DISABLED:
70568c2ecf20Sopenharmony_ci			if (!qla_tgt_mode_enabled(vha))
70578c2ecf20Sopenharmony_ci				return 0;
70588c2ecf20Sopenharmony_ci			break;
70598c2ecf20Sopenharmony_ci		case QLA2XXX_INI_MODE_DUAL:
70608c2ecf20Sopenharmony_ci			if (!qla_dual_mode_enabled(vha) &&
70618c2ecf20Sopenharmony_ci			    !qla_ini_mode_enabled(vha))
70628c2ecf20Sopenharmony_ci				return 0;
70638c2ecf20Sopenharmony_ci			break;
70648c2ecf20Sopenharmony_ci		case QLA2XXX_INI_MODE_ENABLED:
70658c2ecf20Sopenharmony_ci		default:
70668c2ecf20Sopenharmony_ci			break;
70678c2ecf20Sopenharmony_ci		}
70688c2ecf20Sopenharmony_ci
70698c2ecf20Sopenharmony_ci		ha->isp_ops->get_flash_version(vha, req->ring);
70708c2ecf20Sopenharmony_ci
70718c2ecf20Sopenharmony_ci		if (qla2x00_isp_reg_stat(ha)) {
70728c2ecf20Sopenharmony_ci			ql_log(ql_log_info, vha, 0x803f,
70738c2ecf20Sopenharmony_ci			       "ISP Abort - ISP reg disconnect pre nvram config, exiting.\n");
70748c2ecf20Sopenharmony_ci			return status;
70758c2ecf20Sopenharmony_ci		}
70768c2ecf20Sopenharmony_ci		ha->isp_ops->nvram_config(vha);
70778c2ecf20Sopenharmony_ci
70788c2ecf20Sopenharmony_ci		if (qla2x00_isp_reg_stat(ha)) {
70798c2ecf20Sopenharmony_ci			ql_log(ql_log_info, vha, 0x803f,
70808c2ecf20Sopenharmony_ci			       "ISP Abort - ISP reg disconnect post nvmram config, exiting.\n");
70818c2ecf20Sopenharmony_ci			return status;
70828c2ecf20Sopenharmony_ci		}
70838c2ecf20Sopenharmony_ci		if (!qla2x00_restart_isp(vha)) {
70848c2ecf20Sopenharmony_ci			clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
70858c2ecf20Sopenharmony_ci
70868c2ecf20Sopenharmony_ci			if (!atomic_read(&vha->loop_down_timer)) {
70878c2ecf20Sopenharmony_ci				/*
70888c2ecf20Sopenharmony_ci				 * Issue marker command only when we are going
70898c2ecf20Sopenharmony_ci				 * to start the I/O .
70908c2ecf20Sopenharmony_ci				 */
70918c2ecf20Sopenharmony_ci				vha->marker_needed = 1;
70928c2ecf20Sopenharmony_ci			}
70938c2ecf20Sopenharmony_ci
70948c2ecf20Sopenharmony_ci			vha->flags.online = 1;
70958c2ecf20Sopenharmony_ci
70968c2ecf20Sopenharmony_ci			ha->isp_ops->enable_intrs(ha);
70978c2ecf20Sopenharmony_ci
70988c2ecf20Sopenharmony_ci			ha->isp_abort_cnt = 0;
70998c2ecf20Sopenharmony_ci			clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
71008c2ecf20Sopenharmony_ci
71018c2ecf20Sopenharmony_ci			if (IS_QLA81XX(ha) || IS_QLA8031(ha))
71028c2ecf20Sopenharmony_ci				qla2x00_get_fw_version(vha);
71038c2ecf20Sopenharmony_ci			if (ha->fce) {
71048c2ecf20Sopenharmony_ci				ha->flags.fce_enabled = 1;
71058c2ecf20Sopenharmony_ci				memset(ha->fce, 0,
71068c2ecf20Sopenharmony_ci				    fce_calc_size(ha->fce_bufs));
71078c2ecf20Sopenharmony_ci				rval = qla2x00_enable_fce_trace(vha,
71088c2ecf20Sopenharmony_ci				    ha->fce_dma, ha->fce_bufs, ha->fce_mb,
71098c2ecf20Sopenharmony_ci				    &ha->fce_bufs);
71108c2ecf20Sopenharmony_ci				if (rval) {
71118c2ecf20Sopenharmony_ci					ql_log(ql_log_warn, vha, 0x8033,
71128c2ecf20Sopenharmony_ci					    "Unable to reinitialize FCE "
71138c2ecf20Sopenharmony_ci					    "(%d).\n", rval);
71148c2ecf20Sopenharmony_ci					ha->flags.fce_enabled = 0;
71158c2ecf20Sopenharmony_ci				}
71168c2ecf20Sopenharmony_ci			}
71178c2ecf20Sopenharmony_ci
71188c2ecf20Sopenharmony_ci			if (ha->eft) {
71198c2ecf20Sopenharmony_ci				memset(ha->eft, 0, EFT_SIZE);
71208c2ecf20Sopenharmony_ci				rval = qla2x00_enable_eft_trace(vha,
71218c2ecf20Sopenharmony_ci				    ha->eft_dma, EFT_NUM_BUFFERS);
71228c2ecf20Sopenharmony_ci				if (rval) {
71238c2ecf20Sopenharmony_ci					ql_log(ql_log_warn, vha, 0x8034,
71248c2ecf20Sopenharmony_ci					    "Unable to reinitialize EFT "
71258c2ecf20Sopenharmony_ci					    "(%d).\n", rval);
71268c2ecf20Sopenharmony_ci				}
71278c2ecf20Sopenharmony_ci			}
71288c2ecf20Sopenharmony_ci		} else {	/* failed the ISP abort */
71298c2ecf20Sopenharmony_ci			vha->flags.online = 1;
71308c2ecf20Sopenharmony_ci			if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
71318c2ecf20Sopenharmony_ci				if (ha->isp_abort_cnt == 0) {
71328c2ecf20Sopenharmony_ci					ql_log(ql_log_fatal, vha, 0x8035,
71338c2ecf20Sopenharmony_ci					    "ISP error recover failed - "
71348c2ecf20Sopenharmony_ci					    "board disabled.\n");
71358c2ecf20Sopenharmony_ci					/*
71368c2ecf20Sopenharmony_ci					 * The next call disables the board
71378c2ecf20Sopenharmony_ci					 * completely.
71388c2ecf20Sopenharmony_ci					 */
71398c2ecf20Sopenharmony_ci					qla2x00_abort_isp_cleanup(vha);
71408c2ecf20Sopenharmony_ci					vha->flags.online = 0;
71418c2ecf20Sopenharmony_ci					clear_bit(ISP_ABORT_RETRY,
71428c2ecf20Sopenharmony_ci					    &vha->dpc_flags);
71438c2ecf20Sopenharmony_ci					status = 0;
71448c2ecf20Sopenharmony_ci				} else { /* schedule another ISP abort */
71458c2ecf20Sopenharmony_ci					ha->isp_abort_cnt--;
71468c2ecf20Sopenharmony_ci					ql_dbg(ql_dbg_taskm, vha, 0x8020,
71478c2ecf20Sopenharmony_ci					    "ISP abort - retry remaining %d.\n",
71488c2ecf20Sopenharmony_ci					    ha->isp_abort_cnt);
71498c2ecf20Sopenharmony_ci					status = 1;
71508c2ecf20Sopenharmony_ci				}
71518c2ecf20Sopenharmony_ci			} else {
71528c2ecf20Sopenharmony_ci				ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
71538c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_taskm, vha, 0x8021,
71548c2ecf20Sopenharmony_ci				    "ISP error recovery - retrying (%d) "
71558c2ecf20Sopenharmony_ci				    "more times.\n", ha->isp_abort_cnt);
71568c2ecf20Sopenharmony_ci				set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
71578c2ecf20Sopenharmony_ci				status = 1;
71588c2ecf20Sopenharmony_ci			}
71598c2ecf20Sopenharmony_ci		}
71608c2ecf20Sopenharmony_ci
71618c2ecf20Sopenharmony_ci	}
71628c2ecf20Sopenharmony_ci
71638c2ecf20Sopenharmony_ci	if (!status) {
71648c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
71658c2ecf20Sopenharmony_ci		qla2x00_configure_hba(vha);
71668c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->vport_slock, flags);
71678c2ecf20Sopenharmony_ci		list_for_each_entry(vp, &ha->vp_list, list) {
71688c2ecf20Sopenharmony_ci			if (vp->vp_idx) {
71698c2ecf20Sopenharmony_ci				atomic_inc(&vp->vref_count);
71708c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&ha->vport_slock, flags);
71718c2ecf20Sopenharmony_ci
71728c2ecf20Sopenharmony_ci				qla2x00_vp_abort_isp(vp);
71738c2ecf20Sopenharmony_ci
71748c2ecf20Sopenharmony_ci				spin_lock_irqsave(&ha->vport_slock, flags);
71758c2ecf20Sopenharmony_ci				atomic_dec(&vp->vref_count);
71768c2ecf20Sopenharmony_ci			}
71778c2ecf20Sopenharmony_ci		}
71788c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->vport_slock, flags);
71798c2ecf20Sopenharmony_ci
71808c2ecf20Sopenharmony_ci		if (IS_QLA8031(ha)) {
71818c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_p3p, vha, 0xb05d,
71828c2ecf20Sopenharmony_ci			    "Setting back fcoe driver presence.\n");
71838c2ecf20Sopenharmony_ci			if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
71848c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_p3p, vha, 0xb074,
71858c2ecf20Sopenharmony_ci				    "Error while setting DRV-Presence.\n");
71868c2ecf20Sopenharmony_ci		}
71878c2ecf20Sopenharmony_ci	} else {
71888c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
71898c2ecf20Sopenharmony_ci		       __func__);
71908c2ecf20Sopenharmony_ci	}
71918c2ecf20Sopenharmony_ci
71928c2ecf20Sopenharmony_ci	return(status);
71938c2ecf20Sopenharmony_ci}
71948c2ecf20Sopenharmony_ci
71958c2ecf20Sopenharmony_ci/*
71968c2ecf20Sopenharmony_ci*  qla2x00_restart_isp
71978c2ecf20Sopenharmony_ci*      restarts the ISP after a reset
71988c2ecf20Sopenharmony_ci*
71998c2ecf20Sopenharmony_ci* Input:
72008c2ecf20Sopenharmony_ci*      ha = adapter block pointer.
72018c2ecf20Sopenharmony_ci*
72028c2ecf20Sopenharmony_ci* Returns:
72038c2ecf20Sopenharmony_ci*      0 = success
72048c2ecf20Sopenharmony_ci*/
72058c2ecf20Sopenharmony_cistatic int
72068c2ecf20Sopenharmony_ciqla2x00_restart_isp(scsi_qla_host_t *vha)
72078c2ecf20Sopenharmony_ci{
72088c2ecf20Sopenharmony_ci	int status;
72098c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
72108c2ecf20Sopenharmony_ci
72118c2ecf20Sopenharmony_ci	/* If firmware needs to be loaded */
72128c2ecf20Sopenharmony_ci	if (qla2x00_isp_firmware(vha)) {
72138c2ecf20Sopenharmony_ci		vha->flags.online = 0;
72148c2ecf20Sopenharmony_ci		status = ha->isp_ops->chip_diag(vha);
72158c2ecf20Sopenharmony_ci		if (status)
72168c2ecf20Sopenharmony_ci			return status;
72178c2ecf20Sopenharmony_ci		status = qla2x00_setup_chip(vha);
72188c2ecf20Sopenharmony_ci		if (status)
72198c2ecf20Sopenharmony_ci			return status;
72208c2ecf20Sopenharmony_ci	}
72218c2ecf20Sopenharmony_ci
72228c2ecf20Sopenharmony_ci	status = qla2x00_init_rings(vha);
72238c2ecf20Sopenharmony_ci	if (status)
72248c2ecf20Sopenharmony_ci		return status;
72258c2ecf20Sopenharmony_ci
72268c2ecf20Sopenharmony_ci	clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
72278c2ecf20Sopenharmony_ci	ha->flags.chip_reset_done = 1;
72288c2ecf20Sopenharmony_ci
72298c2ecf20Sopenharmony_ci	/* Initialize the queues in use */
72308c2ecf20Sopenharmony_ci	qla25xx_init_queues(ha);
72318c2ecf20Sopenharmony_ci
72328c2ecf20Sopenharmony_ci	status = qla2x00_fw_ready(vha);
72338c2ecf20Sopenharmony_ci	if (status) {
72348c2ecf20Sopenharmony_ci		/* if no cable then assume it's good */
72358c2ecf20Sopenharmony_ci		return vha->device_flags & DFLG_NO_CABLE ? 0 : status;
72368c2ecf20Sopenharmony_ci	}
72378c2ecf20Sopenharmony_ci
72388c2ecf20Sopenharmony_ci	/* Issue a marker after FW becomes ready. */
72398c2ecf20Sopenharmony_ci	qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
72408c2ecf20Sopenharmony_ci	set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
72418c2ecf20Sopenharmony_ci
72428c2ecf20Sopenharmony_ci	return 0;
72438c2ecf20Sopenharmony_ci}
72448c2ecf20Sopenharmony_ci
72458c2ecf20Sopenharmony_cistatic int
72468c2ecf20Sopenharmony_ciqla25xx_init_queues(struct qla_hw_data *ha)
72478c2ecf20Sopenharmony_ci{
72488c2ecf20Sopenharmony_ci	struct rsp_que *rsp = NULL;
72498c2ecf20Sopenharmony_ci	struct req_que *req = NULL;
72508c2ecf20Sopenharmony_ci	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
72518c2ecf20Sopenharmony_ci	int ret = -1;
72528c2ecf20Sopenharmony_ci	int i;
72538c2ecf20Sopenharmony_ci
72548c2ecf20Sopenharmony_ci	for (i = 1; i < ha->max_rsp_queues; i++) {
72558c2ecf20Sopenharmony_ci		rsp = ha->rsp_q_map[i];
72568c2ecf20Sopenharmony_ci		if (rsp && test_bit(i, ha->rsp_qid_map)) {
72578c2ecf20Sopenharmony_ci			rsp->options &= ~BIT_0;
72588c2ecf20Sopenharmony_ci			ret = qla25xx_init_rsp_que(base_vha, rsp);
72598c2ecf20Sopenharmony_ci			if (ret != QLA_SUCCESS)
72608c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_init, base_vha, 0x00ff,
72618c2ecf20Sopenharmony_ci				    "%s Rsp que: %d init failed.\n",
72628c2ecf20Sopenharmony_ci				    __func__, rsp->id);
72638c2ecf20Sopenharmony_ci			else
72648c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_init, base_vha, 0x0100,
72658c2ecf20Sopenharmony_ci				    "%s Rsp que: %d inited.\n",
72668c2ecf20Sopenharmony_ci				    __func__, rsp->id);
72678c2ecf20Sopenharmony_ci		}
72688c2ecf20Sopenharmony_ci	}
72698c2ecf20Sopenharmony_ci	for (i = 1; i < ha->max_req_queues; i++) {
72708c2ecf20Sopenharmony_ci		req = ha->req_q_map[i];
72718c2ecf20Sopenharmony_ci		if (req && test_bit(i, ha->req_qid_map)) {
72728c2ecf20Sopenharmony_ci			/* Clear outstanding commands array. */
72738c2ecf20Sopenharmony_ci			req->options &= ~BIT_0;
72748c2ecf20Sopenharmony_ci			ret = qla25xx_init_req_que(base_vha, req);
72758c2ecf20Sopenharmony_ci			if (ret != QLA_SUCCESS)
72768c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_init, base_vha, 0x0101,
72778c2ecf20Sopenharmony_ci				    "%s Req que: %d init failed.\n",
72788c2ecf20Sopenharmony_ci				    __func__, req->id);
72798c2ecf20Sopenharmony_ci			else
72808c2ecf20Sopenharmony_ci				ql_dbg(ql_dbg_init, base_vha, 0x0102,
72818c2ecf20Sopenharmony_ci				    "%s Req que: %d inited.\n",
72828c2ecf20Sopenharmony_ci				    __func__, req->id);
72838c2ecf20Sopenharmony_ci		}
72848c2ecf20Sopenharmony_ci	}
72858c2ecf20Sopenharmony_ci	return ret;
72868c2ecf20Sopenharmony_ci}
72878c2ecf20Sopenharmony_ci
72888c2ecf20Sopenharmony_ci/*
72898c2ecf20Sopenharmony_ci* qla2x00_reset_adapter
72908c2ecf20Sopenharmony_ci*      Reset adapter.
72918c2ecf20Sopenharmony_ci*
72928c2ecf20Sopenharmony_ci* Input:
72938c2ecf20Sopenharmony_ci*      ha = adapter block pointer.
72948c2ecf20Sopenharmony_ci*/
72958c2ecf20Sopenharmony_ciint
72968c2ecf20Sopenharmony_ciqla2x00_reset_adapter(scsi_qla_host_t *vha)
72978c2ecf20Sopenharmony_ci{
72988c2ecf20Sopenharmony_ci	unsigned long flags = 0;
72998c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
73008c2ecf20Sopenharmony_ci	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
73018c2ecf20Sopenharmony_ci
73028c2ecf20Sopenharmony_ci	vha->flags.online = 0;
73038c2ecf20Sopenharmony_ci	ha->isp_ops->disable_intrs(ha);
73048c2ecf20Sopenharmony_ci
73058c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
73068c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
73078c2ecf20Sopenharmony_ci	rd_reg_word(&reg->hccr);			/* PCI Posting. */
73088c2ecf20Sopenharmony_ci	wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
73098c2ecf20Sopenharmony_ci	rd_reg_word(&reg->hccr);			/* PCI Posting. */
73108c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
73118c2ecf20Sopenharmony_ci
73128c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
73138c2ecf20Sopenharmony_ci}
73148c2ecf20Sopenharmony_ci
73158c2ecf20Sopenharmony_ciint
73168c2ecf20Sopenharmony_ciqla24xx_reset_adapter(scsi_qla_host_t *vha)
73178c2ecf20Sopenharmony_ci{
73188c2ecf20Sopenharmony_ci	unsigned long flags = 0;
73198c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
73208c2ecf20Sopenharmony_ci	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
73218c2ecf20Sopenharmony_ci
73228c2ecf20Sopenharmony_ci	if (IS_P3P_TYPE(ha))
73238c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
73248c2ecf20Sopenharmony_ci
73258c2ecf20Sopenharmony_ci	vha->flags.online = 0;
73268c2ecf20Sopenharmony_ci	ha->isp_ops->disable_intrs(ha);
73278c2ecf20Sopenharmony_ci
73288c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
73298c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->hccr, HCCRX_SET_RISC_RESET);
73308c2ecf20Sopenharmony_ci	rd_reg_dword(&reg->hccr);
73318c2ecf20Sopenharmony_ci	wrt_reg_dword(&reg->hccr, HCCRX_REL_RISC_PAUSE);
73328c2ecf20Sopenharmony_ci	rd_reg_dword(&reg->hccr);
73338c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
73348c2ecf20Sopenharmony_ci
73358c2ecf20Sopenharmony_ci	if (IS_NOPOLLING_TYPE(ha))
73368c2ecf20Sopenharmony_ci		ha->isp_ops->enable_intrs(ha);
73378c2ecf20Sopenharmony_ci
73388c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
73398c2ecf20Sopenharmony_ci}
73408c2ecf20Sopenharmony_ci
73418c2ecf20Sopenharmony_ci/* On sparc systems, obtain port and node WWN from firmware
73428c2ecf20Sopenharmony_ci * properties.
73438c2ecf20Sopenharmony_ci */
73448c2ecf20Sopenharmony_cistatic void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
73458c2ecf20Sopenharmony_ci	struct nvram_24xx *nv)
73468c2ecf20Sopenharmony_ci{
73478c2ecf20Sopenharmony_ci#ifdef CONFIG_SPARC
73488c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
73498c2ecf20Sopenharmony_ci	struct pci_dev *pdev = ha->pdev;
73508c2ecf20Sopenharmony_ci	struct device_node *dp = pci_device_to_OF_node(pdev);
73518c2ecf20Sopenharmony_ci	const u8 *val;
73528c2ecf20Sopenharmony_ci	int len;
73538c2ecf20Sopenharmony_ci
73548c2ecf20Sopenharmony_ci	val = of_get_property(dp, "port-wwn", &len);
73558c2ecf20Sopenharmony_ci	if (val && len >= WWN_SIZE)
73568c2ecf20Sopenharmony_ci		memcpy(nv->port_name, val, WWN_SIZE);
73578c2ecf20Sopenharmony_ci
73588c2ecf20Sopenharmony_ci	val = of_get_property(dp, "node-wwn", &len);
73598c2ecf20Sopenharmony_ci	if (val && len >= WWN_SIZE)
73608c2ecf20Sopenharmony_ci		memcpy(nv->node_name, val, WWN_SIZE);
73618c2ecf20Sopenharmony_ci#endif
73628c2ecf20Sopenharmony_ci}
73638c2ecf20Sopenharmony_ci
73648c2ecf20Sopenharmony_ciint
73658c2ecf20Sopenharmony_ciqla24xx_nvram_config(scsi_qla_host_t *vha)
73668c2ecf20Sopenharmony_ci{
73678c2ecf20Sopenharmony_ci	int   rval;
73688c2ecf20Sopenharmony_ci	struct init_cb_24xx *icb;
73698c2ecf20Sopenharmony_ci	struct nvram_24xx *nv;
73708c2ecf20Sopenharmony_ci	__le32 *dptr;
73718c2ecf20Sopenharmony_ci	uint8_t  *dptr1, *dptr2;
73728c2ecf20Sopenharmony_ci	uint32_t chksum;
73738c2ecf20Sopenharmony_ci	uint16_t cnt;
73748c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
73758c2ecf20Sopenharmony_ci
73768c2ecf20Sopenharmony_ci	rval = QLA_SUCCESS;
73778c2ecf20Sopenharmony_ci	icb = (struct init_cb_24xx *)ha->init_cb;
73788c2ecf20Sopenharmony_ci	nv = ha->nvram;
73798c2ecf20Sopenharmony_ci
73808c2ecf20Sopenharmony_ci	/* Determine NVRAM starting address. */
73818c2ecf20Sopenharmony_ci	if (ha->port_no == 0) {
73828c2ecf20Sopenharmony_ci		ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
73838c2ecf20Sopenharmony_ci		ha->vpd_base = FA_NVRAM_VPD0_ADDR;
73848c2ecf20Sopenharmony_ci	} else {
73858c2ecf20Sopenharmony_ci		ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
73868c2ecf20Sopenharmony_ci		ha->vpd_base = FA_NVRAM_VPD1_ADDR;
73878c2ecf20Sopenharmony_ci	}
73888c2ecf20Sopenharmony_ci
73898c2ecf20Sopenharmony_ci	ha->nvram_size = sizeof(*nv);
73908c2ecf20Sopenharmony_ci	ha->vpd_size = FA_NVRAM_VPD_SIZE;
73918c2ecf20Sopenharmony_ci
73928c2ecf20Sopenharmony_ci	/* Get VPD data into cache */
73938c2ecf20Sopenharmony_ci	ha->vpd = ha->nvram + VPD_OFFSET;
73948c2ecf20Sopenharmony_ci	ha->isp_ops->read_nvram(vha, ha->vpd,
73958c2ecf20Sopenharmony_ci	    ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
73968c2ecf20Sopenharmony_ci
73978c2ecf20Sopenharmony_ci	/* Get NVRAM data into cache and calculate checksum. */
73988c2ecf20Sopenharmony_ci	dptr = (__force __le32 *)nv;
73998c2ecf20Sopenharmony_ci	ha->isp_ops->read_nvram(vha, dptr, ha->nvram_base, ha->nvram_size);
74008c2ecf20Sopenharmony_ci	for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
74018c2ecf20Sopenharmony_ci		chksum += le32_to_cpu(*dptr);
74028c2ecf20Sopenharmony_ci
74038c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
74048c2ecf20Sopenharmony_ci	    "Contents of NVRAM\n");
74058c2ecf20Sopenharmony_ci	ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
74068c2ecf20Sopenharmony_ci	    nv, ha->nvram_size);
74078c2ecf20Sopenharmony_ci
74088c2ecf20Sopenharmony_ci	/* Bad NVRAM data, set defaults parameters. */
74098c2ecf20Sopenharmony_ci	if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
74108c2ecf20Sopenharmony_ci	    le16_to_cpu(nv->nvram_version) < ICB_VERSION) {
74118c2ecf20Sopenharmony_ci		/* Reset NVRAM data. */
74128c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x006b,
74138c2ecf20Sopenharmony_ci		    "Inconsistent NVRAM checksum=%#x id=%.4s version=%#x.\n",
74148c2ecf20Sopenharmony_ci		    chksum, nv->id, nv->nvram_version);
74158c2ecf20Sopenharmony_ci		ql_dump_buffer(ql_dbg_init, vha, 0x006b, nv, sizeof(*nv));
74168c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x006c,
74178c2ecf20Sopenharmony_ci		    "Falling back to functioning (yet invalid -- WWPN) "
74188c2ecf20Sopenharmony_ci		    "defaults.\n");
74198c2ecf20Sopenharmony_ci
74208c2ecf20Sopenharmony_ci		/*
74218c2ecf20Sopenharmony_ci		 * Set default initialization control block.
74228c2ecf20Sopenharmony_ci		 */
74238c2ecf20Sopenharmony_ci		memset(nv, 0, ha->nvram_size);
74248c2ecf20Sopenharmony_ci		nv->nvram_version = cpu_to_le16(ICB_VERSION);
74258c2ecf20Sopenharmony_ci		nv->version = cpu_to_le16(ICB_VERSION);
74268c2ecf20Sopenharmony_ci		nv->frame_payload_size = cpu_to_le16(2048);
74278c2ecf20Sopenharmony_ci		nv->execution_throttle = cpu_to_le16(0xFFFF);
74288c2ecf20Sopenharmony_ci		nv->exchange_count = cpu_to_le16(0);
74298c2ecf20Sopenharmony_ci		nv->hard_address = cpu_to_le16(124);
74308c2ecf20Sopenharmony_ci		nv->port_name[0] = 0x21;
74318c2ecf20Sopenharmony_ci		nv->port_name[1] = 0x00 + ha->port_no + 1;
74328c2ecf20Sopenharmony_ci		nv->port_name[2] = 0x00;
74338c2ecf20Sopenharmony_ci		nv->port_name[3] = 0xe0;
74348c2ecf20Sopenharmony_ci		nv->port_name[4] = 0x8b;
74358c2ecf20Sopenharmony_ci		nv->port_name[5] = 0x1c;
74368c2ecf20Sopenharmony_ci		nv->port_name[6] = 0x55;
74378c2ecf20Sopenharmony_ci		nv->port_name[7] = 0x86;
74388c2ecf20Sopenharmony_ci		nv->node_name[0] = 0x20;
74398c2ecf20Sopenharmony_ci		nv->node_name[1] = 0x00;
74408c2ecf20Sopenharmony_ci		nv->node_name[2] = 0x00;
74418c2ecf20Sopenharmony_ci		nv->node_name[3] = 0xe0;
74428c2ecf20Sopenharmony_ci		nv->node_name[4] = 0x8b;
74438c2ecf20Sopenharmony_ci		nv->node_name[5] = 0x1c;
74448c2ecf20Sopenharmony_ci		nv->node_name[6] = 0x55;
74458c2ecf20Sopenharmony_ci		nv->node_name[7] = 0x86;
74468c2ecf20Sopenharmony_ci		qla24xx_nvram_wwn_from_ofw(vha, nv);
74478c2ecf20Sopenharmony_ci		nv->login_retry_count = cpu_to_le16(8);
74488c2ecf20Sopenharmony_ci		nv->interrupt_delay_timer = cpu_to_le16(0);
74498c2ecf20Sopenharmony_ci		nv->login_timeout = cpu_to_le16(0);
74508c2ecf20Sopenharmony_ci		nv->firmware_options_1 =
74518c2ecf20Sopenharmony_ci		    cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
74528c2ecf20Sopenharmony_ci		nv->firmware_options_2 = cpu_to_le32(2 << 4);
74538c2ecf20Sopenharmony_ci		nv->firmware_options_2 |= cpu_to_le32(BIT_12);
74548c2ecf20Sopenharmony_ci		nv->firmware_options_3 = cpu_to_le32(2 << 13);
74558c2ecf20Sopenharmony_ci		nv->host_p = cpu_to_le32(BIT_11|BIT_10);
74568c2ecf20Sopenharmony_ci		nv->efi_parameters = cpu_to_le32(0);
74578c2ecf20Sopenharmony_ci		nv->reset_delay = 5;
74588c2ecf20Sopenharmony_ci		nv->max_luns_per_target = cpu_to_le16(128);
74598c2ecf20Sopenharmony_ci		nv->port_down_retry_count = cpu_to_le16(30);
74608c2ecf20Sopenharmony_ci		nv->link_down_timeout = cpu_to_le16(30);
74618c2ecf20Sopenharmony_ci
74628c2ecf20Sopenharmony_ci		rval = 1;
74638c2ecf20Sopenharmony_ci	}
74648c2ecf20Sopenharmony_ci
74658c2ecf20Sopenharmony_ci	if (qla_tgt_mode_enabled(vha)) {
74668c2ecf20Sopenharmony_ci		/* Don't enable full login after initial LIP */
74678c2ecf20Sopenharmony_ci		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
74688c2ecf20Sopenharmony_ci		/* Don't enable LIP full login for initiator */
74698c2ecf20Sopenharmony_ci		nv->host_p &= cpu_to_le32(~BIT_10);
74708c2ecf20Sopenharmony_ci	}
74718c2ecf20Sopenharmony_ci
74728c2ecf20Sopenharmony_ci	qlt_24xx_config_nvram_stage1(vha, nv);
74738c2ecf20Sopenharmony_ci
74748c2ecf20Sopenharmony_ci	/* Reset Initialization control block */
74758c2ecf20Sopenharmony_ci	memset(icb, 0, ha->init_cb_size);
74768c2ecf20Sopenharmony_ci
74778c2ecf20Sopenharmony_ci	/* Copy 1st segment. */
74788c2ecf20Sopenharmony_ci	dptr1 = (uint8_t *)icb;
74798c2ecf20Sopenharmony_ci	dptr2 = (uint8_t *)&nv->version;
74808c2ecf20Sopenharmony_ci	cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
74818c2ecf20Sopenharmony_ci	while (cnt--)
74828c2ecf20Sopenharmony_ci		*dptr1++ = *dptr2++;
74838c2ecf20Sopenharmony_ci
74848c2ecf20Sopenharmony_ci	icb->login_retry_count = nv->login_retry_count;
74858c2ecf20Sopenharmony_ci	icb->link_down_on_nos = nv->link_down_on_nos;
74868c2ecf20Sopenharmony_ci
74878c2ecf20Sopenharmony_ci	/* Copy 2nd segment. */
74888c2ecf20Sopenharmony_ci	dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
74898c2ecf20Sopenharmony_ci	dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
74908c2ecf20Sopenharmony_ci	cnt = (uint8_t *)&icb->reserved_3 -
74918c2ecf20Sopenharmony_ci	    (uint8_t *)&icb->interrupt_delay_timer;
74928c2ecf20Sopenharmony_ci	while (cnt--)
74938c2ecf20Sopenharmony_ci		*dptr1++ = *dptr2++;
74948c2ecf20Sopenharmony_ci	ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
74958c2ecf20Sopenharmony_ci	/*
74968c2ecf20Sopenharmony_ci	 * Setup driver NVRAM options.
74978c2ecf20Sopenharmony_ci	 */
74988c2ecf20Sopenharmony_ci	qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
74998c2ecf20Sopenharmony_ci	    "QLA2462");
75008c2ecf20Sopenharmony_ci
75018c2ecf20Sopenharmony_ci	qlt_24xx_config_nvram_stage2(vha, icb);
75028c2ecf20Sopenharmony_ci
75038c2ecf20Sopenharmony_ci	if (nv->host_p & cpu_to_le32(BIT_15)) {
75048c2ecf20Sopenharmony_ci		/* Use alternate WWN? */
75058c2ecf20Sopenharmony_ci		memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
75068c2ecf20Sopenharmony_ci		memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
75078c2ecf20Sopenharmony_ci	}
75088c2ecf20Sopenharmony_ci
75098c2ecf20Sopenharmony_ci	/* Prepare nodename */
75108c2ecf20Sopenharmony_ci	if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
75118c2ecf20Sopenharmony_ci		/*
75128c2ecf20Sopenharmony_ci		 * Firmware will apply the following mask if the nodename was
75138c2ecf20Sopenharmony_ci		 * not provided.
75148c2ecf20Sopenharmony_ci		 */
75158c2ecf20Sopenharmony_ci		memcpy(icb->node_name, icb->port_name, WWN_SIZE);
75168c2ecf20Sopenharmony_ci		icb->node_name[0] &= 0xF0;
75178c2ecf20Sopenharmony_ci	}
75188c2ecf20Sopenharmony_ci
75198c2ecf20Sopenharmony_ci	/* Set host adapter parameters. */
75208c2ecf20Sopenharmony_ci	ha->flags.disable_risc_code_load = 0;
75218c2ecf20Sopenharmony_ci	ha->flags.enable_lip_reset = 0;
75228c2ecf20Sopenharmony_ci	ha->flags.enable_lip_full_login =
75238c2ecf20Sopenharmony_ci	    le32_to_cpu(nv->host_p) & BIT_10 ? 1 : 0;
75248c2ecf20Sopenharmony_ci	ha->flags.enable_target_reset =
75258c2ecf20Sopenharmony_ci	    le32_to_cpu(nv->host_p) & BIT_11 ? 1 : 0;
75268c2ecf20Sopenharmony_ci	ha->flags.enable_led_scheme = 0;
75278c2ecf20Sopenharmony_ci	ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1 : 0;
75288c2ecf20Sopenharmony_ci
75298c2ecf20Sopenharmony_ci	ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
75308c2ecf20Sopenharmony_ci	    (BIT_6 | BIT_5 | BIT_4)) >> 4;
75318c2ecf20Sopenharmony_ci
75328c2ecf20Sopenharmony_ci	memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
75338c2ecf20Sopenharmony_ci	    sizeof(ha->fw_seriallink_options24));
75348c2ecf20Sopenharmony_ci
75358c2ecf20Sopenharmony_ci	/* save HBA serial number */
75368c2ecf20Sopenharmony_ci	ha->serial0 = icb->port_name[5];
75378c2ecf20Sopenharmony_ci	ha->serial1 = icb->port_name[6];
75388c2ecf20Sopenharmony_ci	ha->serial2 = icb->port_name[7];
75398c2ecf20Sopenharmony_ci	memcpy(vha->node_name, icb->node_name, WWN_SIZE);
75408c2ecf20Sopenharmony_ci	memcpy(vha->port_name, icb->port_name, WWN_SIZE);
75418c2ecf20Sopenharmony_ci
75428c2ecf20Sopenharmony_ci	icb->execution_throttle = cpu_to_le16(0xFFFF);
75438c2ecf20Sopenharmony_ci
75448c2ecf20Sopenharmony_ci	ha->retry_count = le16_to_cpu(nv->login_retry_count);
75458c2ecf20Sopenharmony_ci
75468c2ecf20Sopenharmony_ci	/* Set minimum login_timeout to 4 seconds. */
75478c2ecf20Sopenharmony_ci	if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
75488c2ecf20Sopenharmony_ci		nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
75498c2ecf20Sopenharmony_ci	if (le16_to_cpu(nv->login_timeout) < 4)
75508c2ecf20Sopenharmony_ci		nv->login_timeout = cpu_to_le16(4);
75518c2ecf20Sopenharmony_ci	ha->login_timeout = le16_to_cpu(nv->login_timeout);
75528c2ecf20Sopenharmony_ci
75538c2ecf20Sopenharmony_ci	/* Set minimum RATOV to 100 tenths of a second. */
75548c2ecf20Sopenharmony_ci	ha->r_a_tov = 100;
75558c2ecf20Sopenharmony_ci
75568c2ecf20Sopenharmony_ci	ha->loop_reset_delay = nv->reset_delay;
75578c2ecf20Sopenharmony_ci
75588c2ecf20Sopenharmony_ci	/* Link Down Timeout = 0:
75598c2ecf20Sopenharmony_ci	 *
75608c2ecf20Sopenharmony_ci	 * 	When Port Down timer expires we will start returning
75618c2ecf20Sopenharmony_ci	 *	I/O's to OS with "DID_NO_CONNECT".
75628c2ecf20Sopenharmony_ci	 *
75638c2ecf20Sopenharmony_ci	 * Link Down Timeout != 0:
75648c2ecf20Sopenharmony_ci	 *
75658c2ecf20Sopenharmony_ci	 *	 The driver waits for the link to come up after link down
75668c2ecf20Sopenharmony_ci	 *	 before returning I/Os to OS with "DID_NO_CONNECT".
75678c2ecf20Sopenharmony_ci	 */
75688c2ecf20Sopenharmony_ci	if (le16_to_cpu(nv->link_down_timeout) == 0) {
75698c2ecf20Sopenharmony_ci		ha->loop_down_abort_time =
75708c2ecf20Sopenharmony_ci		    (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
75718c2ecf20Sopenharmony_ci	} else {
75728c2ecf20Sopenharmony_ci		ha->link_down_timeout =	le16_to_cpu(nv->link_down_timeout);
75738c2ecf20Sopenharmony_ci		ha->loop_down_abort_time =
75748c2ecf20Sopenharmony_ci		    (LOOP_DOWN_TIME - ha->link_down_timeout);
75758c2ecf20Sopenharmony_ci	}
75768c2ecf20Sopenharmony_ci
75778c2ecf20Sopenharmony_ci	/* Need enough time to try and get the port back. */
75788c2ecf20Sopenharmony_ci	ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
75798c2ecf20Sopenharmony_ci	if (qlport_down_retry)
75808c2ecf20Sopenharmony_ci		ha->port_down_retry_count = qlport_down_retry;
75818c2ecf20Sopenharmony_ci
75828c2ecf20Sopenharmony_ci	/* Set login_retry_count */
75838c2ecf20Sopenharmony_ci	ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
75848c2ecf20Sopenharmony_ci	if (ha->port_down_retry_count ==
75858c2ecf20Sopenharmony_ci	    le16_to_cpu(nv->port_down_retry_count) &&
75868c2ecf20Sopenharmony_ci	    ha->port_down_retry_count > 3)
75878c2ecf20Sopenharmony_ci		ha->login_retry_count = ha->port_down_retry_count;
75888c2ecf20Sopenharmony_ci	else if (ha->port_down_retry_count > (int)ha->login_retry_count)
75898c2ecf20Sopenharmony_ci		ha->login_retry_count = ha->port_down_retry_count;
75908c2ecf20Sopenharmony_ci	if (ql2xloginretrycount)
75918c2ecf20Sopenharmony_ci		ha->login_retry_count = ql2xloginretrycount;
75928c2ecf20Sopenharmony_ci
75938c2ecf20Sopenharmony_ci	/* N2N: driver will initiate Login instead of FW */
75948c2ecf20Sopenharmony_ci	icb->firmware_options_3 |= cpu_to_le32(BIT_8);
75958c2ecf20Sopenharmony_ci
75968c2ecf20Sopenharmony_ci	/* Enable ZIO. */
75978c2ecf20Sopenharmony_ci	if (!vha->flags.init_done) {
75988c2ecf20Sopenharmony_ci		ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
75998c2ecf20Sopenharmony_ci		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
76008c2ecf20Sopenharmony_ci		ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
76018c2ecf20Sopenharmony_ci		    le16_to_cpu(icb->interrupt_delay_timer) : 2;
76028c2ecf20Sopenharmony_ci	}
76038c2ecf20Sopenharmony_ci	icb->firmware_options_2 &= cpu_to_le32(
76048c2ecf20Sopenharmony_ci	    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
76058c2ecf20Sopenharmony_ci	if (ha->zio_mode != QLA_ZIO_DISABLED) {
76068c2ecf20Sopenharmony_ci		ha->zio_mode = QLA_ZIO_MODE_6;
76078c2ecf20Sopenharmony_ci
76088c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x006f,
76098c2ecf20Sopenharmony_ci		    "ZIO mode %d enabled; timer delay (%d us).\n",
76108c2ecf20Sopenharmony_ci		    ha->zio_mode, ha->zio_timer * 100);
76118c2ecf20Sopenharmony_ci
76128c2ecf20Sopenharmony_ci		icb->firmware_options_2 |= cpu_to_le32(
76138c2ecf20Sopenharmony_ci		    (uint32_t)ha->zio_mode);
76148c2ecf20Sopenharmony_ci		icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
76158c2ecf20Sopenharmony_ci	}
76168c2ecf20Sopenharmony_ci
76178c2ecf20Sopenharmony_ci	if (rval) {
76188c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0070,
76198c2ecf20Sopenharmony_ci		    "NVRAM configuration failed.\n");
76208c2ecf20Sopenharmony_ci	}
76218c2ecf20Sopenharmony_ci	return (rval);
76228c2ecf20Sopenharmony_ci}
76238c2ecf20Sopenharmony_ci
76248c2ecf20Sopenharmony_cistatic void
76258c2ecf20Sopenharmony_ciqla27xx_print_image(struct scsi_qla_host *vha, char *name,
76268c2ecf20Sopenharmony_ci    struct qla27xx_image_status *image_status)
76278c2ecf20Sopenharmony_ci{
76288c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x018b,
76298c2ecf20Sopenharmony_ci	    "%s %s: mask=%#02x gen=%#04x ver=%u.%u map=%#01x sum=%#08x sig=%#08x\n",
76308c2ecf20Sopenharmony_ci	    name, "status",
76318c2ecf20Sopenharmony_ci	    image_status->image_status_mask,
76328c2ecf20Sopenharmony_ci	    le16_to_cpu(image_status->generation),
76338c2ecf20Sopenharmony_ci	    image_status->ver_major,
76348c2ecf20Sopenharmony_ci	    image_status->ver_minor,
76358c2ecf20Sopenharmony_ci	    image_status->bitmap,
76368c2ecf20Sopenharmony_ci	    le32_to_cpu(image_status->checksum),
76378c2ecf20Sopenharmony_ci	    le32_to_cpu(image_status->signature));
76388c2ecf20Sopenharmony_ci}
76398c2ecf20Sopenharmony_ci
76408c2ecf20Sopenharmony_cistatic bool
76418c2ecf20Sopenharmony_ciqla28xx_check_aux_image_status_signature(
76428c2ecf20Sopenharmony_ci    struct qla27xx_image_status *image_status)
76438c2ecf20Sopenharmony_ci{
76448c2ecf20Sopenharmony_ci	ulong signature = le32_to_cpu(image_status->signature);
76458c2ecf20Sopenharmony_ci
76468c2ecf20Sopenharmony_ci	return signature != QLA28XX_AUX_IMG_STATUS_SIGN;
76478c2ecf20Sopenharmony_ci}
76488c2ecf20Sopenharmony_ci
76498c2ecf20Sopenharmony_cistatic bool
76508c2ecf20Sopenharmony_ciqla27xx_check_image_status_signature(struct qla27xx_image_status *image_status)
76518c2ecf20Sopenharmony_ci{
76528c2ecf20Sopenharmony_ci	ulong signature = le32_to_cpu(image_status->signature);
76538c2ecf20Sopenharmony_ci
76548c2ecf20Sopenharmony_ci	return
76558c2ecf20Sopenharmony_ci	    signature != QLA27XX_IMG_STATUS_SIGN &&
76568c2ecf20Sopenharmony_ci	    signature != QLA28XX_IMG_STATUS_SIGN;
76578c2ecf20Sopenharmony_ci}
76588c2ecf20Sopenharmony_ci
76598c2ecf20Sopenharmony_cistatic ulong
76608c2ecf20Sopenharmony_ciqla27xx_image_status_checksum(struct qla27xx_image_status *image_status)
76618c2ecf20Sopenharmony_ci{
76628c2ecf20Sopenharmony_ci	__le32 *p = (__force __le32 *)image_status;
76638c2ecf20Sopenharmony_ci	uint n = sizeof(*image_status) / sizeof(*p);
76648c2ecf20Sopenharmony_ci	uint32_t sum = 0;
76658c2ecf20Sopenharmony_ci
76668c2ecf20Sopenharmony_ci	for ( ; n--; p++)
76678c2ecf20Sopenharmony_ci		sum += le32_to_cpup(p);
76688c2ecf20Sopenharmony_ci
76698c2ecf20Sopenharmony_ci	return sum;
76708c2ecf20Sopenharmony_ci}
76718c2ecf20Sopenharmony_ci
76728c2ecf20Sopenharmony_cistatic inline uint
76738c2ecf20Sopenharmony_ciqla28xx_component_bitmask(struct qla27xx_image_status *aux, uint bitmask)
76748c2ecf20Sopenharmony_ci{
76758c2ecf20Sopenharmony_ci	return aux->bitmap & bitmask ?
76768c2ecf20Sopenharmony_ci	    QLA27XX_SECONDARY_IMAGE : QLA27XX_PRIMARY_IMAGE;
76778c2ecf20Sopenharmony_ci}
76788c2ecf20Sopenharmony_ci
76798c2ecf20Sopenharmony_cistatic void
76808c2ecf20Sopenharmony_ciqla28xx_component_status(
76818c2ecf20Sopenharmony_ci    struct active_regions *active_regions, struct qla27xx_image_status *aux)
76828c2ecf20Sopenharmony_ci{
76838c2ecf20Sopenharmony_ci	active_regions->aux.board_config =
76848c2ecf20Sopenharmony_ci	    qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_BOARD_CONFIG);
76858c2ecf20Sopenharmony_ci
76868c2ecf20Sopenharmony_ci	active_regions->aux.vpd_nvram =
76878c2ecf20Sopenharmony_ci	    qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_VPD_NVRAM);
76888c2ecf20Sopenharmony_ci
76898c2ecf20Sopenharmony_ci	active_regions->aux.npiv_config_0_1 =
76908c2ecf20Sopenharmony_ci	    qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_NPIV_CONFIG_0_1);
76918c2ecf20Sopenharmony_ci
76928c2ecf20Sopenharmony_ci	active_regions->aux.npiv_config_2_3 =
76938c2ecf20Sopenharmony_ci	    qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_NPIV_CONFIG_2_3);
76948c2ecf20Sopenharmony_ci}
76958c2ecf20Sopenharmony_ci
76968c2ecf20Sopenharmony_cistatic int
76978c2ecf20Sopenharmony_ciqla27xx_compare_image_generation(
76988c2ecf20Sopenharmony_ci    struct qla27xx_image_status *pri_image_status,
76998c2ecf20Sopenharmony_ci    struct qla27xx_image_status *sec_image_status)
77008c2ecf20Sopenharmony_ci{
77018c2ecf20Sopenharmony_ci	/* calculate generation delta as uint16 (this accounts for wrap) */
77028c2ecf20Sopenharmony_ci	int16_t delta =
77038c2ecf20Sopenharmony_ci	    le16_to_cpu(pri_image_status->generation) -
77048c2ecf20Sopenharmony_ci	    le16_to_cpu(sec_image_status->generation);
77058c2ecf20Sopenharmony_ci
77068c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, NULL, 0x0180, "generation delta = %d\n", delta);
77078c2ecf20Sopenharmony_ci
77088c2ecf20Sopenharmony_ci	return delta;
77098c2ecf20Sopenharmony_ci}
77108c2ecf20Sopenharmony_ci
77118c2ecf20Sopenharmony_civoid
77128c2ecf20Sopenharmony_ciqla28xx_get_aux_images(
77138c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha, struct active_regions *active_regions)
77148c2ecf20Sopenharmony_ci{
77158c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
77168c2ecf20Sopenharmony_ci	struct qla27xx_image_status pri_aux_image_status, sec_aux_image_status;
77178c2ecf20Sopenharmony_ci	bool valid_pri_image = false, valid_sec_image = false;
77188c2ecf20Sopenharmony_ci	bool active_pri_image = false, active_sec_image = false;
77198c2ecf20Sopenharmony_ci
77208c2ecf20Sopenharmony_ci	if (!ha->flt_region_aux_img_status_pri) {
77218c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018a, "Primary aux image not addressed\n");
77228c2ecf20Sopenharmony_ci		goto check_sec_image;
77238c2ecf20Sopenharmony_ci	}
77248c2ecf20Sopenharmony_ci
77258c2ecf20Sopenharmony_ci	qla24xx_read_flash_data(vha, (uint32_t *)&pri_aux_image_status,
77268c2ecf20Sopenharmony_ci	    ha->flt_region_aux_img_status_pri,
77278c2ecf20Sopenharmony_ci	    sizeof(pri_aux_image_status) >> 2);
77288c2ecf20Sopenharmony_ci	qla27xx_print_image(vha, "Primary aux image", &pri_aux_image_status);
77298c2ecf20Sopenharmony_ci
77308c2ecf20Sopenharmony_ci	if (qla28xx_check_aux_image_status_signature(&pri_aux_image_status)) {
77318c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018b,
77328c2ecf20Sopenharmony_ci		    "Primary aux image signature (%#x) not valid\n",
77338c2ecf20Sopenharmony_ci		    le32_to_cpu(pri_aux_image_status.signature));
77348c2ecf20Sopenharmony_ci		goto check_sec_image;
77358c2ecf20Sopenharmony_ci	}
77368c2ecf20Sopenharmony_ci
77378c2ecf20Sopenharmony_ci	if (qla27xx_image_status_checksum(&pri_aux_image_status)) {
77388c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018c,
77398c2ecf20Sopenharmony_ci		    "Primary aux image checksum failed\n");
77408c2ecf20Sopenharmony_ci		goto check_sec_image;
77418c2ecf20Sopenharmony_ci	}
77428c2ecf20Sopenharmony_ci
77438c2ecf20Sopenharmony_ci	valid_pri_image = true;
77448c2ecf20Sopenharmony_ci
77458c2ecf20Sopenharmony_ci	if (pri_aux_image_status.image_status_mask & 1) {
77468c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018d,
77478c2ecf20Sopenharmony_ci		    "Primary aux image is active\n");
77488c2ecf20Sopenharmony_ci		active_pri_image = true;
77498c2ecf20Sopenharmony_ci	}
77508c2ecf20Sopenharmony_ci
77518c2ecf20Sopenharmony_cicheck_sec_image:
77528c2ecf20Sopenharmony_ci	if (!ha->flt_region_aux_img_status_sec) {
77538c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018a,
77548c2ecf20Sopenharmony_ci		    "Secondary aux image not addressed\n");
77558c2ecf20Sopenharmony_ci		goto check_valid_image;
77568c2ecf20Sopenharmony_ci	}
77578c2ecf20Sopenharmony_ci
77588c2ecf20Sopenharmony_ci	qla24xx_read_flash_data(vha, (uint32_t *)&sec_aux_image_status,
77598c2ecf20Sopenharmony_ci	    ha->flt_region_aux_img_status_sec,
77608c2ecf20Sopenharmony_ci	    sizeof(sec_aux_image_status) >> 2);
77618c2ecf20Sopenharmony_ci	qla27xx_print_image(vha, "Secondary aux image", &sec_aux_image_status);
77628c2ecf20Sopenharmony_ci
77638c2ecf20Sopenharmony_ci	if (qla28xx_check_aux_image_status_signature(&sec_aux_image_status)) {
77648c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018b,
77658c2ecf20Sopenharmony_ci		    "Secondary aux image signature (%#x) not valid\n",
77668c2ecf20Sopenharmony_ci		    le32_to_cpu(sec_aux_image_status.signature));
77678c2ecf20Sopenharmony_ci		goto check_valid_image;
77688c2ecf20Sopenharmony_ci	}
77698c2ecf20Sopenharmony_ci
77708c2ecf20Sopenharmony_ci	if (qla27xx_image_status_checksum(&sec_aux_image_status)) {
77718c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018c,
77728c2ecf20Sopenharmony_ci		    "Secondary aux image checksum failed\n");
77738c2ecf20Sopenharmony_ci		goto check_valid_image;
77748c2ecf20Sopenharmony_ci	}
77758c2ecf20Sopenharmony_ci
77768c2ecf20Sopenharmony_ci	valid_sec_image = true;
77778c2ecf20Sopenharmony_ci
77788c2ecf20Sopenharmony_ci	if (sec_aux_image_status.image_status_mask & 1) {
77798c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018d,
77808c2ecf20Sopenharmony_ci		    "Secondary aux image is active\n");
77818c2ecf20Sopenharmony_ci		active_sec_image = true;
77828c2ecf20Sopenharmony_ci	}
77838c2ecf20Sopenharmony_ci
77848c2ecf20Sopenharmony_cicheck_valid_image:
77858c2ecf20Sopenharmony_ci	if (valid_pri_image && active_pri_image &&
77868c2ecf20Sopenharmony_ci	    valid_sec_image && active_sec_image) {
77878c2ecf20Sopenharmony_ci		if (qla27xx_compare_image_generation(&pri_aux_image_status,
77888c2ecf20Sopenharmony_ci		    &sec_aux_image_status) >= 0) {
77898c2ecf20Sopenharmony_ci			qla28xx_component_status(active_regions,
77908c2ecf20Sopenharmony_ci			    &pri_aux_image_status);
77918c2ecf20Sopenharmony_ci		} else {
77928c2ecf20Sopenharmony_ci			qla28xx_component_status(active_regions,
77938c2ecf20Sopenharmony_ci			    &sec_aux_image_status);
77948c2ecf20Sopenharmony_ci		}
77958c2ecf20Sopenharmony_ci	} else if (valid_pri_image && active_pri_image) {
77968c2ecf20Sopenharmony_ci		qla28xx_component_status(active_regions, &pri_aux_image_status);
77978c2ecf20Sopenharmony_ci	} else if (valid_sec_image && active_sec_image) {
77988c2ecf20Sopenharmony_ci		qla28xx_component_status(active_regions, &sec_aux_image_status);
77998c2ecf20Sopenharmony_ci	}
78008c2ecf20Sopenharmony_ci
78018c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x018f,
78028c2ecf20Sopenharmony_ci	    "aux images active: BCFG=%u VPD/NVR=%u NPIV0/1=%u NPIV2/3=%u\n",
78038c2ecf20Sopenharmony_ci	    active_regions->aux.board_config,
78048c2ecf20Sopenharmony_ci	    active_regions->aux.vpd_nvram,
78058c2ecf20Sopenharmony_ci	    active_regions->aux.npiv_config_0_1,
78068c2ecf20Sopenharmony_ci	    active_regions->aux.npiv_config_2_3);
78078c2ecf20Sopenharmony_ci}
78088c2ecf20Sopenharmony_ci
78098c2ecf20Sopenharmony_civoid
78108c2ecf20Sopenharmony_ciqla27xx_get_active_image(struct scsi_qla_host *vha,
78118c2ecf20Sopenharmony_ci    struct active_regions *active_regions)
78128c2ecf20Sopenharmony_ci{
78138c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
78148c2ecf20Sopenharmony_ci	struct qla27xx_image_status pri_image_status, sec_image_status;
78158c2ecf20Sopenharmony_ci	bool valid_pri_image = false, valid_sec_image = false;
78168c2ecf20Sopenharmony_ci	bool active_pri_image = false, active_sec_image = false;
78178c2ecf20Sopenharmony_ci
78188c2ecf20Sopenharmony_ci	if (!ha->flt_region_img_status_pri) {
78198c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018a, "Primary image not addressed\n");
78208c2ecf20Sopenharmony_ci		goto check_sec_image;
78218c2ecf20Sopenharmony_ci	}
78228c2ecf20Sopenharmony_ci
78238c2ecf20Sopenharmony_ci	if (qla24xx_read_flash_data(vha, (uint32_t *)&pri_image_status,
78248c2ecf20Sopenharmony_ci	    ha->flt_region_img_status_pri, sizeof(pri_image_status) >> 2) !=
78258c2ecf20Sopenharmony_ci	    QLA_SUCCESS) {
78268c2ecf20Sopenharmony_ci		WARN_ON_ONCE(true);
78278c2ecf20Sopenharmony_ci		goto check_sec_image;
78288c2ecf20Sopenharmony_ci	}
78298c2ecf20Sopenharmony_ci	qla27xx_print_image(vha, "Primary image", &pri_image_status);
78308c2ecf20Sopenharmony_ci
78318c2ecf20Sopenharmony_ci	if (qla27xx_check_image_status_signature(&pri_image_status)) {
78328c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018b,
78338c2ecf20Sopenharmony_ci		    "Primary image signature (%#x) not valid\n",
78348c2ecf20Sopenharmony_ci		    le32_to_cpu(pri_image_status.signature));
78358c2ecf20Sopenharmony_ci		goto check_sec_image;
78368c2ecf20Sopenharmony_ci	}
78378c2ecf20Sopenharmony_ci
78388c2ecf20Sopenharmony_ci	if (qla27xx_image_status_checksum(&pri_image_status)) {
78398c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018c,
78408c2ecf20Sopenharmony_ci		    "Primary image checksum failed\n");
78418c2ecf20Sopenharmony_ci		goto check_sec_image;
78428c2ecf20Sopenharmony_ci	}
78438c2ecf20Sopenharmony_ci
78448c2ecf20Sopenharmony_ci	valid_pri_image = true;
78458c2ecf20Sopenharmony_ci
78468c2ecf20Sopenharmony_ci	if (pri_image_status.image_status_mask & 1) {
78478c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018d,
78488c2ecf20Sopenharmony_ci		    "Primary image is active\n");
78498c2ecf20Sopenharmony_ci		active_pri_image = true;
78508c2ecf20Sopenharmony_ci	}
78518c2ecf20Sopenharmony_ci
78528c2ecf20Sopenharmony_cicheck_sec_image:
78538c2ecf20Sopenharmony_ci	if (!ha->flt_region_img_status_sec) {
78548c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018a, "Secondary image not addressed\n");
78558c2ecf20Sopenharmony_ci		goto check_valid_image;
78568c2ecf20Sopenharmony_ci	}
78578c2ecf20Sopenharmony_ci
78588c2ecf20Sopenharmony_ci	qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
78598c2ecf20Sopenharmony_ci	    ha->flt_region_img_status_sec, sizeof(sec_image_status) >> 2);
78608c2ecf20Sopenharmony_ci	qla27xx_print_image(vha, "Secondary image", &sec_image_status);
78618c2ecf20Sopenharmony_ci
78628c2ecf20Sopenharmony_ci	if (qla27xx_check_image_status_signature(&sec_image_status)) {
78638c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018b,
78648c2ecf20Sopenharmony_ci		    "Secondary image signature (%#x) not valid\n",
78658c2ecf20Sopenharmony_ci		    le32_to_cpu(sec_image_status.signature));
78668c2ecf20Sopenharmony_ci		goto check_valid_image;
78678c2ecf20Sopenharmony_ci	}
78688c2ecf20Sopenharmony_ci
78698c2ecf20Sopenharmony_ci	if (qla27xx_image_status_checksum(&sec_image_status)) {
78708c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018c,
78718c2ecf20Sopenharmony_ci		    "Secondary image checksum failed\n");
78728c2ecf20Sopenharmony_ci		goto check_valid_image;
78738c2ecf20Sopenharmony_ci	}
78748c2ecf20Sopenharmony_ci
78758c2ecf20Sopenharmony_ci	valid_sec_image = true;
78768c2ecf20Sopenharmony_ci
78778c2ecf20Sopenharmony_ci	if (sec_image_status.image_status_mask & 1) {
78788c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x018d,
78798c2ecf20Sopenharmony_ci		    "Secondary image is active\n");
78808c2ecf20Sopenharmony_ci		active_sec_image = true;
78818c2ecf20Sopenharmony_ci	}
78828c2ecf20Sopenharmony_ci
78838c2ecf20Sopenharmony_cicheck_valid_image:
78848c2ecf20Sopenharmony_ci	if (valid_pri_image && active_pri_image)
78858c2ecf20Sopenharmony_ci		active_regions->global = QLA27XX_PRIMARY_IMAGE;
78868c2ecf20Sopenharmony_ci
78878c2ecf20Sopenharmony_ci	if (valid_sec_image && active_sec_image) {
78888c2ecf20Sopenharmony_ci		if (!active_regions->global ||
78898c2ecf20Sopenharmony_ci		    qla27xx_compare_image_generation(
78908c2ecf20Sopenharmony_ci			&pri_image_status, &sec_image_status) < 0) {
78918c2ecf20Sopenharmony_ci			active_regions->global = QLA27XX_SECONDARY_IMAGE;
78928c2ecf20Sopenharmony_ci		}
78938c2ecf20Sopenharmony_ci	}
78948c2ecf20Sopenharmony_ci
78958c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x018f, "active image %s (%u)\n",
78968c2ecf20Sopenharmony_ci	    active_regions->global == QLA27XX_DEFAULT_IMAGE ?
78978c2ecf20Sopenharmony_ci		"default (boot/fw)" :
78988c2ecf20Sopenharmony_ci	    active_regions->global == QLA27XX_PRIMARY_IMAGE ?
78998c2ecf20Sopenharmony_ci		"primary" :
79008c2ecf20Sopenharmony_ci	    active_regions->global == QLA27XX_SECONDARY_IMAGE ?
79018c2ecf20Sopenharmony_ci		"secondary" : "invalid",
79028c2ecf20Sopenharmony_ci	    active_regions->global);
79038c2ecf20Sopenharmony_ci}
79048c2ecf20Sopenharmony_ci
79058c2ecf20Sopenharmony_cibool qla24xx_risc_firmware_invalid(uint32_t *dword)
79068c2ecf20Sopenharmony_ci{
79078c2ecf20Sopenharmony_ci	return
79088c2ecf20Sopenharmony_ci	    !(dword[4] | dword[5] | dword[6] | dword[7]) ||
79098c2ecf20Sopenharmony_ci	    !(~dword[4] | ~dword[5] | ~dword[6] | ~dword[7]);
79108c2ecf20Sopenharmony_ci}
79118c2ecf20Sopenharmony_ci
79128c2ecf20Sopenharmony_cistatic int
79138c2ecf20Sopenharmony_ciqla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
79148c2ecf20Sopenharmony_ci    uint32_t faddr)
79158c2ecf20Sopenharmony_ci{
79168c2ecf20Sopenharmony_ci	int rval;
79178c2ecf20Sopenharmony_ci	uint templates, segments, fragment;
79188c2ecf20Sopenharmony_ci	ulong i;
79198c2ecf20Sopenharmony_ci	uint j;
79208c2ecf20Sopenharmony_ci	ulong dlen;
79218c2ecf20Sopenharmony_ci	uint32_t *dcode;
79228c2ecf20Sopenharmony_ci	uint32_t risc_addr, risc_size, risc_attr = 0;
79238c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
79248c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
79258c2ecf20Sopenharmony_ci	struct fwdt *fwdt = ha->fwdt;
79268c2ecf20Sopenharmony_ci
79278c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x008b,
79288c2ecf20Sopenharmony_ci	    "FW: Loading firmware from flash (%x).\n", faddr);
79298c2ecf20Sopenharmony_ci
79308c2ecf20Sopenharmony_ci	dcode = (uint32_t *)req->ring;
79318c2ecf20Sopenharmony_ci	qla24xx_read_flash_data(vha, dcode, faddr, 8);
79328c2ecf20Sopenharmony_ci	if (qla24xx_risc_firmware_invalid(dcode)) {
79338c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x008c,
79348c2ecf20Sopenharmony_ci		    "Unable to verify the integrity of flash firmware "
79358c2ecf20Sopenharmony_ci		    "image.\n");
79368c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x008d,
79378c2ecf20Sopenharmony_ci		    "Firmware data: %08x %08x %08x %08x.\n",
79388c2ecf20Sopenharmony_ci		    dcode[0], dcode[1], dcode[2], dcode[3]);
79398c2ecf20Sopenharmony_ci
79408c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
79418c2ecf20Sopenharmony_ci	}
79428c2ecf20Sopenharmony_ci
79438c2ecf20Sopenharmony_ci	dcode = (uint32_t *)req->ring;
79448c2ecf20Sopenharmony_ci	*srisc_addr = 0;
79458c2ecf20Sopenharmony_ci	segments = FA_RISC_CODE_SEGMENTS;
79468c2ecf20Sopenharmony_ci	for (j = 0; j < segments; j++) {
79478c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x008d,
79488c2ecf20Sopenharmony_ci		    "-> Loading segment %u...\n", j);
79498c2ecf20Sopenharmony_ci		qla24xx_read_flash_data(vha, dcode, faddr, 10);
79508c2ecf20Sopenharmony_ci		risc_addr = be32_to_cpu((__force __be32)dcode[2]);
79518c2ecf20Sopenharmony_ci		risc_size = be32_to_cpu((__force __be32)dcode[3]);
79528c2ecf20Sopenharmony_ci		if (!*srisc_addr) {
79538c2ecf20Sopenharmony_ci			*srisc_addr = risc_addr;
79548c2ecf20Sopenharmony_ci			risc_attr = be32_to_cpu((__force __be32)dcode[9]);
79558c2ecf20Sopenharmony_ci		}
79568c2ecf20Sopenharmony_ci
79578c2ecf20Sopenharmony_ci		dlen = ha->fw_transfer_size >> 2;
79588c2ecf20Sopenharmony_ci		for (fragment = 0; risc_size; fragment++) {
79598c2ecf20Sopenharmony_ci			if (dlen > risc_size)
79608c2ecf20Sopenharmony_ci				dlen = risc_size;
79618c2ecf20Sopenharmony_ci
79628c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x008e,
79638c2ecf20Sopenharmony_ci			    "-> Loading fragment %u: %#x <- %#x (%#lx dwords)...\n",
79648c2ecf20Sopenharmony_ci			    fragment, risc_addr, faddr, dlen);
79658c2ecf20Sopenharmony_ci			qla24xx_read_flash_data(vha, dcode, faddr, dlen);
79668c2ecf20Sopenharmony_ci			for (i = 0; i < dlen; i++)
79678c2ecf20Sopenharmony_ci				dcode[i] = swab32(dcode[i]);
79688c2ecf20Sopenharmony_ci
79698c2ecf20Sopenharmony_ci			rval = qla2x00_load_ram(vha, req->dma, risc_addr, dlen);
79708c2ecf20Sopenharmony_ci			if (rval) {
79718c2ecf20Sopenharmony_ci				ql_log(ql_log_fatal, vha, 0x008f,
79728c2ecf20Sopenharmony_ci				    "-> Failed load firmware fragment %u.\n",
79738c2ecf20Sopenharmony_ci				    fragment);
79748c2ecf20Sopenharmony_ci				return QLA_FUNCTION_FAILED;
79758c2ecf20Sopenharmony_ci			}
79768c2ecf20Sopenharmony_ci
79778c2ecf20Sopenharmony_ci			faddr += dlen;
79788c2ecf20Sopenharmony_ci			risc_addr += dlen;
79798c2ecf20Sopenharmony_ci			risc_size -= dlen;
79808c2ecf20Sopenharmony_ci		}
79818c2ecf20Sopenharmony_ci	}
79828c2ecf20Sopenharmony_ci
79838c2ecf20Sopenharmony_ci	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
79848c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
79858c2ecf20Sopenharmony_ci
79868c2ecf20Sopenharmony_ci	templates = (risc_attr & BIT_9) ? 2 : 1;
79878c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x0160, "-> templates = %u\n", templates);
79888c2ecf20Sopenharmony_ci	for (j = 0; j < templates; j++, fwdt++) {
79898c2ecf20Sopenharmony_ci		if (fwdt->template)
79908c2ecf20Sopenharmony_ci			vfree(fwdt->template);
79918c2ecf20Sopenharmony_ci		fwdt->template = NULL;
79928c2ecf20Sopenharmony_ci		fwdt->length = 0;
79938c2ecf20Sopenharmony_ci
79948c2ecf20Sopenharmony_ci		dcode = (uint32_t *)req->ring;
79958c2ecf20Sopenharmony_ci		qla24xx_read_flash_data(vha, dcode, faddr, 7);
79968c2ecf20Sopenharmony_ci		risc_size = be32_to_cpu((__force __be32)dcode[2]);
79978c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0161,
79988c2ecf20Sopenharmony_ci		    "-> fwdt%u template array at %#x (%#x dwords)\n",
79998c2ecf20Sopenharmony_ci		    j, faddr, risc_size);
80008c2ecf20Sopenharmony_ci		if (!risc_size || !~risc_size) {
80018c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x0162,
80028c2ecf20Sopenharmony_ci			    "-> fwdt%u failed to read array\n", j);
80038c2ecf20Sopenharmony_ci			goto failed;
80048c2ecf20Sopenharmony_ci		}
80058c2ecf20Sopenharmony_ci
80068c2ecf20Sopenharmony_ci		/* skip header and ignore checksum */
80078c2ecf20Sopenharmony_ci		faddr += 7;
80088c2ecf20Sopenharmony_ci		risc_size -= 8;
80098c2ecf20Sopenharmony_ci
80108c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0163,
80118c2ecf20Sopenharmony_ci		    "-> fwdt%u template allocate template %#x words...\n",
80128c2ecf20Sopenharmony_ci		    j, risc_size);
80138c2ecf20Sopenharmony_ci		fwdt->template = vmalloc(risc_size * sizeof(*dcode));
80148c2ecf20Sopenharmony_ci		if (!fwdt->template) {
80158c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0164,
80168c2ecf20Sopenharmony_ci			    "-> fwdt%u failed allocate template.\n", j);
80178c2ecf20Sopenharmony_ci			goto failed;
80188c2ecf20Sopenharmony_ci		}
80198c2ecf20Sopenharmony_ci
80208c2ecf20Sopenharmony_ci		dcode = fwdt->template;
80218c2ecf20Sopenharmony_ci		qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
80228c2ecf20Sopenharmony_ci
80238c2ecf20Sopenharmony_ci		if (!qla27xx_fwdt_template_valid(dcode)) {
80248c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0165,
80258c2ecf20Sopenharmony_ci			    "-> fwdt%u failed template validate\n", j);
80268c2ecf20Sopenharmony_ci			goto failed;
80278c2ecf20Sopenharmony_ci		}
80288c2ecf20Sopenharmony_ci
80298c2ecf20Sopenharmony_ci		dlen = qla27xx_fwdt_template_size(dcode);
80308c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0166,
80318c2ecf20Sopenharmony_ci		    "-> fwdt%u template size %#lx bytes (%#lx words)\n",
80328c2ecf20Sopenharmony_ci		    j, dlen, dlen / sizeof(*dcode));
80338c2ecf20Sopenharmony_ci		if (dlen > risc_size * sizeof(*dcode)) {
80348c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0167,
80358c2ecf20Sopenharmony_ci			    "-> fwdt%u template exceeds array (%-lu bytes)\n",
80368c2ecf20Sopenharmony_ci			    j, dlen - risc_size * sizeof(*dcode));
80378c2ecf20Sopenharmony_ci			goto failed;
80388c2ecf20Sopenharmony_ci		}
80398c2ecf20Sopenharmony_ci
80408c2ecf20Sopenharmony_ci		fwdt->length = dlen;
80418c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0168,
80428c2ecf20Sopenharmony_ci		    "-> fwdt%u loaded template ok\n", j);
80438c2ecf20Sopenharmony_ci
80448c2ecf20Sopenharmony_ci		faddr += risc_size + 1;
80458c2ecf20Sopenharmony_ci	}
80468c2ecf20Sopenharmony_ci
80478c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
80488c2ecf20Sopenharmony_ci
80498c2ecf20Sopenharmony_cifailed:
80508c2ecf20Sopenharmony_ci	if (fwdt->template)
80518c2ecf20Sopenharmony_ci		vfree(fwdt->template);
80528c2ecf20Sopenharmony_ci	fwdt->template = NULL;
80538c2ecf20Sopenharmony_ci	fwdt->length = 0;
80548c2ecf20Sopenharmony_ci
80558c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
80568c2ecf20Sopenharmony_ci}
80578c2ecf20Sopenharmony_ci
80588c2ecf20Sopenharmony_ci#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
80598c2ecf20Sopenharmony_ci
80608c2ecf20Sopenharmony_ciint
80618c2ecf20Sopenharmony_ciqla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
80628c2ecf20Sopenharmony_ci{
80638c2ecf20Sopenharmony_ci	int	rval;
80648c2ecf20Sopenharmony_ci	int	i, fragment;
80658c2ecf20Sopenharmony_ci	uint16_t *wcode;
80668c2ecf20Sopenharmony_ci	__be16	 *fwcode;
80678c2ecf20Sopenharmony_ci	uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
80688c2ecf20Sopenharmony_ci	struct fw_blob *blob;
80698c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
80708c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
80718c2ecf20Sopenharmony_ci
80728c2ecf20Sopenharmony_ci	/* Load firmware blob. */
80738c2ecf20Sopenharmony_ci	blob = qla2x00_request_firmware(vha);
80748c2ecf20Sopenharmony_ci	if (!blob) {
80758c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x0083,
80768c2ecf20Sopenharmony_ci		    "Firmware image unavailable.\n");
80778c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x0084,
80788c2ecf20Sopenharmony_ci		    "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
80798c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
80808c2ecf20Sopenharmony_ci	}
80818c2ecf20Sopenharmony_ci
80828c2ecf20Sopenharmony_ci	rval = QLA_SUCCESS;
80838c2ecf20Sopenharmony_ci
80848c2ecf20Sopenharmony_ci	wcode = (uint16_t *)req->ring;
80858c2ecf20Sopenharmony_ci	*srisc_addr = 0;
80868c2ecf20Sopenharmony_ci	fwcode = (__force __be16 *)blob->fw->data;
80878c2ecf20Sopenharmony_ci	fwclen = 0;
80888c2ecf20Sopenharmony_ci
80898c2ecf20Sopenharmony_ci	/* Validate firmware image by checking version. */
80908c2ecf20Sopenharmony_ci	if (blob->fw->size < 8 * sizeof(uint16_t)) {
80918c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x0085,
80928c2ecf20Sopenharmony_ci		    "Unable to verify integrity of firmware image (%zd).\n",
80938c2ecf20Sopenharmony_ci		    blob->fw->size);
80948c2ecf20Sopenharmony_ci		goto fail_fw_integrity;
80958c2ecf20Sopenharmony_ci	}
80968c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++)
80978c2ecf20Sopenharmony_ci		wcode[i] = be16_to_cpu(fwcode[i + 4]);
80988c2ecf20Sopenharmony_ci	if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
80998c2ecf20Sopenharmony_ci	    wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
81008c2ecf20Sopenharmony_ci		wcode[2] == 0 && wcode[3] == 0)) {
81018c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x0086,
81028c2ecf20Sopenharmony_ci		    "Unable to verify integrity of firmware image.\n");
81038c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x0087,
81048c2ecf20Sopenharmony_ci		    "Firmware data: %04x %04x %04x %04x.\n",
81058c2ecf20Sopenharmony_ci		    wcode[0], wcode[1], wcode[2], wcode[3]);
81068c2ecf20Sopenharmony_ci		goto fail_fw_integrity;
81078c2ecf20Sopenharmony_ci	}
81088c2ecf20Sopenharmony_ci
81098c2ecf20Sopenharmony_ci	seg = blob->segs;
81108c2ecf20Sopenharmony_ci	while (*seg && rval == QLA_SUCCESS) {
81118c2ecf20Sopenharmony_ci		risc_addr = *seg;
81128c2ecf20Sopenharmony_ci		*srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
81138c2ecf20Sopenharmony_ci		risc_size = be16_to_cpu(fwcode[3]);
81148c2ecf20Sopenharmony_ci
81158c2ecf20Sopenharmony_ci		/* Validate firmware image size. */
81168c2ecf20Sopenharmony_ci		fwclen += risc_size * sizeof(uint16_t);
81178c2ecf20Sopenharmony_ci		if (blob->fw->size < fwclen) {
81188c2ecf20Sopenharmony_ci			ql_log(ql_log_fatal, vha, 0x0088,
81198c2ecf20Sopenharmony_ci			    "Unable to verify integrity of firmware image "
81208c2ecf20Sopenharmony_ci			    "(%zd).\n", blob->fw->size);
81218c2ecf20Sopenharmony_ci			goto fail_fw_integrity;
81228c2ecf20Sopenharmony_ci		}
81238c2ecf20Sopenharmony_ci
81248c2ecf20Sopenharmony_ci		fragment = 0;
81258c2ecf20Sopenharmony_ci		while (risc_size > 0 && rval == QLA_SUCCESS) {
81268c2ecf20Sopenharmony_ci			wlen = (uint16_t)(ha->fw_transfer_size >> 1);
81278c2ecf20Sopenharmony_ci			if (wlen > risc_size)
81288c2ecf20Sopenharmony_ci				wlen = risc_size;
81298c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x0089,
81308c2ecf20Sopenharmony_ci			    "Loading risc segment@ risc addr %x number of "
81318c2ecf20Sopenharmony_ci			    "words 0x%x.\n", risc_addr, wlen);
81328c2ecf20Sopenharmony_ci
81338c2ecf20Sopenharmony_ci			for (i = 0; i < wlen; i++)
81348c2ecf20Sopenharmony_ci				wcode[i] = swab16((__force u32)fwcode[i]);
81358c2ecf20Sopenharmony_ci
81368c2ecf20Sopenharmony_ci			rval = qla2x00_load_ram(vha, req->dma, risc_addr,
81378c2ecf20Sopenharmony_ci			    wlen);
81388c2ecf20Sopenharmony_ci			if (rval) {
81398c2ecf20Sopenharmony_ci				ql_log(ql_log_fatal, vha, 0x008a,
81408c2ecf20Sopenharmony_ci				    "Failed to load segment %d of firmware.\n",
81418c2ecf20Sopenharmony_ci				    fragment);
81428c2ecf20Sopenharmony_ci				break;
81438c2ecf20Sopenharmony_ci			}
81448c2ecf20Sopenharmony_ci
81458c2ecf20Sopenharmony_ci			fwcode += wlen;
81468c2ecf20Sopenharmony_ci			risc_addr += wlen;
81478c2ecf20Sopenharmony_ci			risc_size -= wlen;
81488c2ecf20Sopenharmony_ci			fragment++;
81498c2ecf20Sopenharmony_ci		}
81508c2ecf20Sopenharmony_ci
81518c2ecf20Sopenharmony_ci		/* Next segment. */
81528c2ecf20Sopenharmony_ci		seg++;
81538c2ecf20Sopenharmony_ci	}
81548c2ecf20Sopenharmony_ci	return rval;
81558c2ecf20Sopenharmony_ci
81568c2ecf20Sopenharmony_cifail_fw_integrity:
81578c2ecf20Sopenharmony_ci	return QLA_FUNCTION_FAILED;
81588c2ecf20Sopenharmony_ci}
81598c2ecf20Sopenharmony_ci
81608c2ecf20Sopenharmony_cistatic int
81618c2ecf20Sopenharmony_ciqla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
81628c2ecf20Sopenharmony_ci{
81638c2ecf20Sopenharmony_ci	int	rval;
81648c2ecf20Sopenharmony_ci	uint templates, segments, fragment;
81658c2ecf20Sopenharmony_ci	uint32_t *dcode;
81668c2ecf20Sopenharmony_ci	ulong dlen;
81678c2ecf20Sopenharmony_ci	uint32_t risc_addr, risc_size, risc_attr = 0;
81688c2ecf20Sopenharmony_ci	ulong i;
81698c2ecf20Sopenharmony_ci	uint j;
81708c2ecf20Sopenharmony_ci	struct fw_blob *blob;
81718c2ecf20Sopenharmony_ci	__be32 *fwcode;
81728c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
81738c2ecf20Sopenharmony_ci	struct req_que *req = ha->req_q_map[0];
81748c2ecf20Sopenharmony_ci	struct fwdt *fwdt = ha->fwdt;
81758c2ecf20Sopenharmony_ci
81768c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x0090,
81778c2ecf20Sopenharmony_ci	    "-> FW: Loading via request-firmware.\n");
81788c2ecf20Sopenharmony_ci
81798c2ecf20Sopenharmony_ci	blob = qla2x00_request_firmware(vha);
81808c2ecf20Sopenharmony_ci	if (!blob) {
81818c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0092,
81828c2ecf20Sopenharmony_ci		    "-> Firmware file not found.\n");
81838c2ecf20Sopenharmony_ci
81848c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
81858c2ecf20Sopenharmony_ci	}
81868c2ecf20Sopenharmony_ci
81878c2ecf20Sopenharmony_ci	fwcode = (__force __be32 *)blob->fw->data;
81888c2ecf20Sopenharmony_ci	dcode = (__force uint32_t *)fwcode;
81898c2ecf20Sopenharmony_ci	if (qla24xx_risc_firmware_invalid(dcode)) {
81908c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x0093,
81918c2ecf20Sopenharmony_ci		    "Unable to verify integrity of firmware image (%zd).\n",
81928c2ecf20Sopenharmony_ci		    blob->fw->size);
81938c2ecf20Sopenharmony_ci		ql_log(ql_log_fatal, vha, 0x0095,
81948c2ecf20Sopenharmony_ci		    "Firmware data: %08x %08x %08x %08x.\n",
81958c2ecf20Sopenharmony_ci		    dcode[0], dcode[1], dcode[2], dcode[3]);
81968c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
81978c2ecf20Sopenharmony_ci	}
81988c2ecf20Sopenharmony_ci
81998c2ecf20Sopenharmony_ci	dcode = (uint32_t *)req->ring;
82008c2ecf20Sopenharmony_ci	*srisc_addr = 0;
82018c2ecf20Sopenharmony_ci	segments = FA_RISC_CODE_SEGMENTS;
82028c2ecf20Sopenharmony_ci	for (j = 0; j < segments; j++) {
82038c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0096,
82048c2ecf20Sopenharmony_ci		    "-> Loading segment %u...\n", j);
82058c2ecf20Sopenharmony_ci		risc_addr = be32_to_cpu(fwcode[2]);
82068c2ecf20Sopenharmony_ci		risc_size = be32_to_cpu(fwcode[3]);
82078c2ecf20Sopenharmony_ci
82088c2ecf20Sopenharmony_ci		if (!*srisc_addr) {
82098c2ecf20Sopenharmony_ci			*srisc_addr = risc_addr;
82108c2ecf20Sopenharmony_ci			risc_attr = be32_to_cpu(fwcode[9]);
82118c2ecf20Sopenharmony_ci		}
82128c2ecf20Sopenharmony_ci
82138c2ecf20Sopenharmony_ci		dlen = ha->fw_transfer_size >> 2;
82148c2ecf20Sopenharmony_ci		for (fragment = 0; risc_size; fragment++) {
82158c2ecf20Sopenharmony_ci			if (dlen > risc_size)
82168c2ecf20Sopenharmony_ci				dlen = risc_size;
82178c2ecf20Sopenharmony_ci
82188c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x0097,
82198c2ecf20Sopenharmony_ci			    "-> Loading fragment %u: %#x <- %#x (%#lx words)...\n",
82208c2ecf20Sopenharmony_ci			    fragment, risc_addr,
82218c2ecf20Sopenharmony_ci			    (uint32_t)(fwcode - (typeof(fwcode))blob->fw->data),
82228c2ecf20Sopenharmony_ci			    dlen);
82238c2ecf20Sopenharmony_ci
82248c2ecf20Sopenharmony_ci			for (i = 0; i < dlen; i++)
82258c2ecf20Sopenharmony_ci				dcode[i] = swab32((__force u32)fwcode[i]);
82268c2ecf20Sopenharmony_ci
82278c2ecf20Sopenharmony_ci			rval = qla2x00_load_ram(vha, req->dma, risc_addr, dlen);
82288c2ecf20Sopenharmony_ci			if (rval) {
82298c2ecf20Sopenharmony_ci				ql_log(ql_log_fatal, vha, 0x0098,
82308c2ecf20Sopenharmony_ci				    "-> Failed load firmware fragment %u.\n",
82318c2ecf20Sopenharmony_ci				    fragment);
82328c2ecf20Sopenharmony_ci				return QLA_FUNCTION_FAILED;
82338c2ecf20Sopenharmony_ci			}
82348c2ecf20Sopenharmony_ci
82358c2ecf20Sopenharmony_ci			fwcode += dlen;
82368c2ecf20Sopenharmony_ci			risc_addr += dlen;
82378c2ecf20Sopenharmony_ci			risc_size -= dlen;
82388c2ecf20Sopenharmony_ci		}
82398c2ecf20Sopenharmony_ci	}
82408c2ecf20Sopenharmony_ci
82418c2ecf20Sopenharmony_ci	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
82428c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
82438c2ecf20Sopenharmony_ci
82448c2ecf20Sopenharmony_ci	templates = (risc_attr & BIT_9) ? 2 : 1;
82458c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x0170, "-> templates = %u\n", templates);
82468c2ecf20Sopenharmony_ci	for (j = 0; j < templates; j++, fwdt++) {
82478c2ecf20Sopenharmony_ci		if (fwdt->template)
82488c2ecf20Sopenharmony_ci			vfree(fwdt->template);
82498c2ecf20Sopenharmony_ci		fwdt->template = NULL;
82508c2ecf20Sopenharmony_ci		fwdt->length = 0;
82518c2ecf20Sopenharmony_ci
82528c2ecf20Sopenharmony_ci		risc_size = be32_to_cpu(fwcode[2]);
82538c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0171,
82548c2ecf20Sopenharmony_ci		    "-> fwdt%u template array at %#x (%#x dwords)\n",
82558c2ecf20Sopenharmony_ci		    j, (uint32_t)((void *)fwcode - (void *)blob->fw->data),
82568c2ecf20Sopenharmony_ci		    risc_size);
82578c2ecf20Sopenharmony_ci		if (!risc_size || !~risc_size) {
82588c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x0172,
82598c2ecf20Sopenharmony_ci			    "-> fwdt%u failed to read array\n", j);
82608c2ecf20Sopenharmony_ci			goto failed;
82618c2ecf20Sopenharmony_ci		}
82628c2ecf20Sopenharmony_ci
82638c2ecf20Sopenharmony_ci		/* skip header and ignore checksum */
82648c2ecf20Sopenharmony_ci		fwcode += 7;
82658c2ecf20Sopenharmony_ci		risc_size -= 8;
82668c2ecf20Sopenharmony_ci
82678c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0173,
82688c2ecf20Sopenharmony_ci		    "-> fwdt%u template allocate template %#x words...\n",
82698c2ecf20Sopenharmony_ci		    j, risc_size);
82708c2ecf20Sopenharmony_ci		fwdt->template = vmalloc(risc_size * sizeof(*dcode));
82718c2ecf20Sopenharmony_ci		if (!fwdt->template) {
82728c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0174,
82738c2ecf20Sopenharmony_ci			    "-> fwdt%u failed allocate template.\n", j);
82748c2ecf20Sopenharmony_ci			goto failed;
82758c2ecf20Sopenharmony_ci		}
82768c2ecf20Sopenharmony_ci
82778c2ecf20Sopenharmony_ci		dcode = fwdt->template;
82788c2ecf20Sopenharmony_ci		for (i = 0; i < risc_size; i++)
82798c2ecf20Sopenharmony_ci			dcode[i] = (__force u32)fwcode[i];
82808c2ecf20Sopenharmony_ci
82818c2ecf20Sopenharmony_ci		if (!qla27xx_fwdt_template_valid(dcode)) {
82828c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0175,
82838c2ecf20Sopenharmony_ci			    "-> fwdt%u failed template validate\n", j);
82848c2ecf20Sopenharmony_ci			goto failed;
82858c2ecf20Sopenharmony_ci		}
82868c2ecf20Sopenharmony_ci
82878c2ecf20Sopenharmony_ci		dlen = qla27xx_fwdt_template_size(dcode);
82888c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0176,
82898c2ecf20Sopenharmony_ci		    "-> fwdt%u template size %#lx bytes (%#lx words)\n",
82908c2ecf20Sopenharmony_ci		    j, dlen, dlen / sizeof(*dcode));
82918c2ecf20Sopenharmony_ci		if (dlen > risc_size * sizeof(*dcode)) {
82928c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0177,
82938c2ecf20Sopenharmony_ci			    "-> fwdt%u template exceeds array (%-lu bytes)\n",
82948c2ecf20Sopenharmony_ci			    j, dlen - risc_size * sizeof(*dcode));
82958c2ecf20Sopenharmony_ci			goto failed;
82968c2ecf20Sopenharmony_ci		}
82978c2ecf20Sopenharmony_ci
82988c2ecf20Sopenharmony_ci		fwdt->length = dlen;
82998c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0178,
83008c2ecf20Sopenharmony_ci		    "-> fwdt%u loaded template ok\n", j);
83018c2ecf20Sopenharmony_ci
83028c2ecf20Sopenharmony_ci		fwcode += risc_size + 1;
83038c2ecf20Sopenharmony_ci	}
83048c2ecf20Sopenharmony_ci
83058c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
83068c2ecf20Sopenharmony_ci
83078c2ecf20Sopenharmony_cifailed:
83088c2ecf20Sopenharmony_ci	if (fwdt->template)
83098c2ecf20Sopenharmony_ci		vfree(fwdt->template);
83108c2ecf20Sopenharmony_ci	fwdt->template = NULL;
83118c2ecf20Sopenharmony_ci	fwdt->length = 0;
83128c2ecf20Sopenharmony_ci
83138c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
83148c2ecf20Sopenharmony_ci}
83158c2ecf20Sopenharmony_ci
83168c2ecf20Sopenharmony_ciint
83178c2ecf20Sopenharmony_ciqla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
83188c2ecf20Sopenharmony_ci{
83198c2ecf20Sopenharmony_ci	int rval;
83208c2ecf20Sopenharmony_ci
83218c2ecf20Sopenharmony_ci	if (ql2xfwloadbin == 1)
83228c2ecf20Sopenharmony_ci		return qla81xx_load_risc(vha, srisc_addr);
83238c2ecf20Sopenharmony_ci
83248c2ecf20Sopenharmony_ci	/*
83258c2ecf20Sopenharmony_ci	 * FW Load priority:
83268c2ecf20Sopenharmony_ci	 * 1) Firmware via request-firmware interface (.bin file).
83278c2ecf20Sopenharmony_ci	 * 2) Firmware residing in flash.
83288c2ecf20Sopenharmony_ci	 */
83298c2ecf20Sopenharmony_ci	rval = qla24xx_load_risc_blob(vha, srisc_addr);
83308c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS)
83318c2ecf20Sopenharmony_ci		return rval;
83328c2ecf20Sopenharmony_ci
83338c2ecf20Sopenharmony_ci	return qla24xx_load_risc_flash(vha, srisc_addr,
83348c2ecf20Sopenharmony_ci	    vha->hw->flt_region_fw);
83358c2ecf20Sopenharmony_ci}
83368c2ecf20Sopenharmony_ci
83378c2ecf20Sopenharmony_ciint
83388c2ecf20Sopenharmony_ciqla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
83398c2ecf20Sopenharmony_ci{
83408c2ecf20Sopenharmony_ci	int rval;
83418c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
83428c2ecf20Sopenharmony_ci	struct active_regions active_regions = { };
83438c2ecf20Sopenharmony_ci
83448c2ecf20Sopenharmony_ci	if (ql2xfwloadbin == 2)
83458c2ecf20Sopenharmony_ci		goto try_blob_fw;
83468c2ecf20Sopenharmony_ci
83478c2ecf20Sopenharmony_ci	/* FW Load priority:
83488c2ecf20Sopenharmony_ci	 * 1) Firmware residing in flash.
83498c2ecf20Sopenharmony_ci	 * 2) Firmware via request-firmware interface (.bin file).
83508c2ecf20Sopenharmony_ci	 * 3) Golden-Firmware residing in flash -- (limited operation).
83518c2ecf20Sopenharmony_ci	 */
83528c2ecf20Sopenharmony_ci
83538c2ecf20Sopenharmony_ci	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
83548c2ecf20Sopenharmony_ci		goto try_primary_fw;
83558c2ecf20Sopenharmony_ci
83568c2ecf20Sopenharmony_ci	qla27xx_get_active_image(vha, &active_regions);
83578c2ecf20Sopenharmony_ci
83588c2ecf20Sopenharmony_ci	if (active_regions.global != QLA27XX_SECONDARY_IMAGE)
83598c2ecf20Sopenharmony_ci		goto try_primary_fw;
83608c2ecf20Sopenharmony_ci
83618c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x008b,
83628c2ecf20Sopenharmony_ci	    "Loading secondary firmware image.\n");
83638c2ecf20Sopenharmony_ci	rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw_sec);
83648c2ecf20Sopenharmony_ci	if (!rval)
83658c2ecf20Sopenharmony_ci		return rval;
83668c2ecf20Sopenharmony_ci
83678c2ecf20Sopenharmony_citry_primary_fw:
83688c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x008b,
83698c2ecf20Sopenharmony_ci	    "Loading primary firmware image.\n");
83708c2ecf20Sopenharmony_ci	rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
83718c2ecf20Sopenharmony_ci	if (!rval)
83728c2ecf20Sopenharmony_ci		return rval;
83738c2ecf20Sopenharmony_ci
83748c2ecf20Sopenharmony_citry_blob_fw:
83758c2ecf20Sopenharmony_ci	rval = qla24xx_load_risc_blob(vha, srisc_addr);
83768c2ecf20Sopenharmony_ci	if (!rval || !ha->flt_region_gold_fw)
83778c2ecf20Sopenharmony_ci		return rval;
83788c2ecf20Sopenharmony_ci
83798c2ecf20Sopenharmony_ci	ql_log(ql_log_info, vha, 0x0099,
83808c2ecf20Sopenharmony_ci	    "Attempting to fallback to golden firmware.\n");
83818c2ecf20Sopenharmony_ci	rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
83828c2ecf20Sopenharmony_ci	if (rval)
83838c2ecf20Sopenharmony_ci		return rval;
83848c2ecf20Sopenharmony_ci
83858c2ecf20Sopenharmony_ci	ql_log(ql_log_info, vha, 0x009a, "Need firmware flash update.\n");
83868c2ecf20Sopenharmony_ci	ha->flags.running_gold_fw = 1;
83878c2ecf20Sopenharmony_ci	return rval;
83888c2ecf20Sopenharmony_ci}
83898c2ecf20Sopenharmony_ci
83908c2ecf20Sopenharmony_civoid
83918c2ecf20Sopenharmony_ciqla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
83928c2ecf20Sopenharmony_ci{
83938c2ecf20Sopenharmony_ci	int ret, retries;
83948c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
83958c2ecf20Sopenharmony_ci
83968c2ecf20Sopenharmony_ci	if (ha->flags.pci_channel_io_perm_failure)
83978c2ecf20Sopenharmony_ci		return;
83988c2ecf20Sopenharmony_ci	if (!IS_FWI2_CAPABLE(ha))
83998c2ecf20Sopenharmony_ci		return;
84008c2ecf20Sopenharmony_ci	if (!ha->fw_major_version)
84018c2ecf20Sopenharmony_ci		return;
84028c2ecf20Sopenharmony_ci	if (!ha->flags.fw_started)
84038c2ecf20Sopenharmony_ci		return;
84048c2ecf20Sopenharmony_ci
84058c2ecf20Sopenharmony_ci	ret = qla2x00_stop_firmware(vha);
84068c2ecf20Sopenharmony_ci	for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
84078c2ecf20Sopenharmony_ci	    ret != QLA_INVALID_COMMAND && retries ; retries--) {
84088c2ecf20Sopenharmony_ci		ha->isp_ops->reset_chip(vha);
84098c2ecf20Sopenharmony_ci		if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
84108c2ecf20Sopenharmony_ci			continue;
84118c2ecf20Sopenharmony_ci		if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
84128c2ecf20Sopenharmony_ci			continue;
84138c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x8015,
84148c2ecf20Sopenharmony_ci		    "Attempting retry of stop-firmware command.\n");
84158c2ecf20Sopenharmony_ci		ret = qla2x00_stop_firmware(vha);
84168c2ecf20Sopenharmony_ci	}
84178c2ecf20Sopenharmony_ci
84188c2ecf20Sopenharmony_ci	QLA_FW_STOPPED(ha);
84198c2ecf20Sopenharmony_ci	ha->flags.fw_init_done = 0;
84208c2ecf20Sopenharmony_ci}
84218c2ecf20Sopenharmony_ci
84228c2ecf20Sopenharmony_ciint
84238c2ecf20Sopenharmony_ciqla24xx_configure_vhba(scsi_qla_host_t *vha)
84248c2ecf20Sopenharmony_ci{
84258c2ecf20Sopenharmony_ci	int rval = QLA_SUCCESS;
84268c2ecf20Sopenharmony_ci	int rval2;
84278c2ecf20Sopenharmony_ci	uint16_t mb[MAILBOX_REGISTER_COUNT];
84288c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
84298c2ecf20Sopenharmony_ci	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
84308c2ecf20Sopenharmony_ci
84318c2ecf20Sopenharmony_ci	if (!vha->vp_idx)
84328c2ecf20Sopenharmony_ci		return -EINVAL;
84338c2ecf20Sopenharmony_ci
84348c2ecf20Sopenharmony_ci	rval = qla2x00_fw_ready(base_vha);
84358c2ecf20Sopenharmony_ci
84368c2ecf20Sopenharmony_ci	if (rval == QLA_SUCCESS) {
84378c2ecf20Sopenharmony_ci		clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
84388c2ecf20Sopenharmony_ci		qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
84398c2ecf20Sopenharmony_ci	}
84408c2ecf20Sopenharmony_ci
84418c2ecf20Sopenharmony_ci	vha->flags.management_server_logged_in = 0;
84428c2ecf20Sopenharmony_ci
84438c2ecf20Sopenharmony_ci	/* Login to SNS first */
84448c2ecf20Sopenharmony_ci	rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
84458c2ecf20Sopenharmony_ci	    BIT_1);
84468c2ecf20Sopenharmony_ci	if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
84478c2ecf20Sopenharmony_ci		if (rval2 == QLA_MEMORY_ALLOC_FAILED)
84488c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x0120,
84498c2ecf20Sopenharmony_ci			    "Failed SNS login: loop_id=%x, rval2=%d\n",
84508c2ecf20Sopenharmony_ci			    NPH_SNS, rval2);
84518c2ecf20Sopenharmony_ci		else
84528c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_init, vha, 0x0103,
84538c2ecf20Sopenharmony_ci			    "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
84548c2ecf20Sopenharmony_ci			    "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
84558c2ecf20Sopenharmony_ci			    NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
84568c2ecf20Sopenharmony_ci		return (QLA_FUNCTION_FAILED);
84578c2ecf20Sopenharmony_ci	}
84588c2ecf20Sopenharmony_ci
84598c2ecf20Sopenharmony_ci	atomic_set(&vha->loop_down_timer, 0);
84608c2ecf20Sopenharmony_ci	atomic_set(&vha->loop_state, LOOP_UP);
84618c2ecf20Sopenharmony_ci	set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
84628c2ecf20Sopenharmony_ci	set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
84638c2ecf20Sopenharmony_ci	rval = qla2x00_loop_resync(base_vha);
84648c2ecf20Sopenharmony_ci
84658c2ecf20Sopenharmony_ci	return rval;
84668c2ecf20Sopenharmony_ci}
84678c2ecf20Sopenharmony_ci
84688c2ecf20Sopenharmony_ci/* 84XX Support **************************************************************/
84698c2ecf20Sopenharmony_ci
84708c2ecf20Sopenharmony_cistatic LIST_HEAD(qla_cs84xx_list);
84718c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(qla_cs84xx_mutex);
84728c2ecf20Sopenharmony_ci
84738c2ecf20Sopenharmony_cistatic struct qla_chip_state_84xx *
84748c2ecf20Sopenharmony_ciqla84xx_get_chip(struct scsi_qla_host *vha)
84758c2ecf20Sopenharmony_ci{
84768c2ecf20Sopenharmony_ci	struct qla_chip_state_84xx *cs84xx;
84778c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
84788c2ecf20Sopenharmony_ci
84798c2ecf20Sopenharmony_ci	mutex_lock(&qla_cs84xx_mutex);
84808c2ecf20Sopenharmony_ci
84818c2ecf20Sopenharmony_ci	/* Find any shared 84xx chip. */
84828c2ecf20Sopenharmony_ci	list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
84838c2ecf20Sopenharmony_ci		if (cs84xx->bus == ha->pdev->bus) {
84848c2ecf20Sopenharmony_ci			kref_get(&cs84xx->kref);
84858c2ecf20Sopenharmony_ci			goto done;
84868c2ecf20Sopenharmony_ci		}
84878c2ecf20Sopenharmony_ci	}
84888c2ecf20Sopenharmony_ci
84898c2ecf20Sopenharmony_ci	cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
84908c2ecf20Sopenharmony_ci	if (!cs84xx)
84918c2ecf20Sopenharmony_ci		goto done;
84928c2ecf20Sopenharmony_ci
84938c2ecf20Sopenharmony_ci	kref_init(&cs84xx->kref);
84948c2ecf20Sopenharmony_ci	spin_lock_init(&cs84xx->access_lock);
84958c2ecf20Sopenharmony_ci	mutex_init(&cs84xx->fw_update_mutex);
84968c2ecf20Sopenharmony_ci	cs84xx->bus = ha->pdev->bus;
84978c2ecf20Sopenharmony_ci
84988c2ecf20Sopenharmony_ci	list_add_tail(&cs84xx->list, &qla_cs84xx_list);
84998c2ecf20Sopenharmony_cidone:
85008c2ecf20Sopenharmony_ci	mutex_unlock(&qla_cs84xx_mutex);
85018c2ecf20Sopenharmony_ci	return cs84xx;
85028c2ecf20Sopenharmony_ci}
85038c2ecf20Sopenharmony_ci
85048c2ecf20Sopenharmony_cistatic void
85058c2ecf20Sopenharmony_ci__qla84xx_chip_release(struct kref *kref)
85068c2ecf20Sopenharmony_ci{
85078c2ecf20Sopenharmony_ci	struct qla_chip_state_84xx *cs84xx =
85088c2ecf20Sopenharmony_ci	    container_of(kref, struct qla_chip_state_84xx, kref);
85098c2ecf20Sopenharmony_ci
85108c2ecf20Sopenharmony_ci	mutex_lock(&qla_cs84xx_mutex);
85118c2ecf20Sopenharmony_ci	list_del(&cs84xx->list);
85128c2ecf20Sopenharmony_ci	mutex_unlock(&qla_cs84xx_mutex);
85138c2ecf20Sopenharmony_ci	kfree(cs84xx);
85148c2ecf20Sopenharmony_ci}
85158c2ecf20Sopenharmony_ci
85168c2ecf20Sopenharmony_civoid
85178c2ecf20Sopenharmony_ciqla84xx_put_chip(struct scsi_qla_host *vha)
85188c2ecf20Sopenharmony_ci{
85198c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
85208c2ecf20Sopenharmony_ci
85218c2ecf20Sopenharmony_ci	if (ha->cs84xx)
85228c2ecf20Sopenharmony_ci		kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
85238c2ecf20Sopenharmony_ci}
85248c2ecf20Sopenharmony_ci
85258c2ecf20Sopenharmony_cistatic int
85268c2ecf20Sopenharmony_ciqla84xx_init_chip(scsi_qla_host_t *vha)
85278c2ecf20Sopenharmony_ci{
85288c2ecf20Sopenharmony_ci	int rval;
85298c2ecf20Sopenharmony_ci	uint16_t status[2];
85308c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
85318c2ecf20Sopenharmony_ci
85328c2ecf20Sopenharmony_ci	mutex_lock(&ha->cs84xx->fw_update_mutex);
85338c2ecf20Sopenharmony_ci
85348c2ecf20Sopenharmony_ci	rval = qla84xx_verify_chip(vha, status);
85358c2ecf20Sopenharmony_ci
85368c2ecf20Sopenharmony_ci	mutex_unlock(&ha->cs84xx->fw_update_mutex);
85378c2ecf20Sopenharmony_ci
85388c2ecf20Sopenharmony_ci	return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED :
85398c2ecf20Sopenharmony_ci	    QLA_SUCCESS;
85408c2ecf20Sopenharmony_ci}
85418c2ecf20Sopenharmony_ci
85428c2ecf20Sopenharmony_ci/* 81XX Support **************************************************************/
85438c2ecf20Sopenharmony_ci
85448c2ecf20Sopenharmony_ciint
85458c2ecf20Sopenharmony_ciqla81xx_nvram_config(scsi_qla_host_t *vha)
85468c2ecf20Sopenharmony_ci{
85478c2ecf20Sopenharmony_ci	int   rval;
85488c2ecf20Sopenharmony_ci	struct init_cb_81xx *icb;
85498c2ecf20Sopenharmony_ci	struct nvram_81xx *nv;
85508c2ecf20Sopenharmony_ci	__le32 *dptr;
85518c2ecf20Sopenharmony_ci	uint8_t  *dptr1, *dptr2;
85528c2ecf20Sopenharmony_ci	uint32_t chksum;
85538c2ecf20Sopenharmony_ci	uint16_t cnt;
85548c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
85558c2ecf20Sopenharmony_ci	uint32_t faddr;
85568c2ecf20Sopenharmony_ci	struct active_regions active_regions = { };
85578c2ecf20Sopenharmony_ci
85588c2ecf20Sopenharmony_ci	rval = QLA_SUCCESS;
85598c2ecf20Sopenharmony_ci	icb = (struct init_cb_81xx *)ha->init_cb;
85608c2ecf20Sopenharmony_ci	nv = ha->nvram;
85618c2ecf20Sopenharmony_ci
85628c2ecf20Sopenharmony_ci	/* Determine NVRAM starting address. */
85638c2ecf20Sopenharmony_ci	ha->nvram_size = sizeof(*nv);
85648c2ecf20Sopenharmony_ci	ha->vpd_size = FA_NVRAM_VPD_SIZE;
85658c2ecf20Sopenharmony_ci	if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
85668c2ecf20Sopenharmony_ci		ha->vpd_size = FA_VPD_SIZE_82XX;
85678c2ecf20Sopenharmony_ci
85688c2ecf20Sopenharmony_ci	if (IS_QLA28XX(ha) || IS_QLA27XX(ha))
85698c2ecf20Sopenharmony_ci		qla28xx_get_aux_images(vha, &active_regions);
85708c2ecf20Sopenharmony_ci
85718c2ecf20Sopenharmony_ci	/* Get VPD data into cache */
85728c2ecf20Sopenharmony_ci	ha->vpd = ha->nvram + VPD_OFFSET;
85738c2ecf20Sopenharmony_ci
85748c2ecf20Sopenharmony_ci	faddr = ha->flt_region_vpd;
85758c2ecf20Sopenharmony_ci	if (IS_QLA28XX(ha)) {
85768c2ecf20Sopenharmony_ci		if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
85778c2ecf20Sopenharmony_ci			faddr = ha->flt_region_vpd_sec;
85788c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0110,
85798c2ecf20Sopenharmony_ci		    "Loading %s nvram image.\n",
85808c2ecf20Sopenharmony_ci		    active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
85818c2ecf20Sopenharmony_ci		    "primary" : "secondary");
85828c2ecf20Sopenharmony_ci	}
85838c2ecf20Sopenharmony_ci	ha->isp_ops->read_optrom(vha, ha->vpd, faddr << 2, ha->vpd_size);
85848c2ecf20Sopenharmony_ci
85858c2ecf20Sopenharmony_ci	/* Get NVRAM data into cache and calculate checksum. */
85868c2ecf20Sopenharmony_ci	faddr = ha->flt_region_nvram;
85878c2ecf20Sopenharmony_ci	if (IS_QLA28XX(ha)) {
85888c2ecf20Sopenharmony_ci		if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
85898c2ecf20Sopenharmony_ci			faddr = ha->flt_region_nvram_sec;
85908c2ecf20Sopenharmony_ci	}
85918c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init, vha, 0x0110,
85928c2ecf20Sopenharmony_ci	    "Loading %s nvram image.\n",
85938c2ecf20Sopenharmony_ci	    active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
85948c2ecf20Sopenharmony_ci	    "primary" : "secondary");
85958c2ecf20Sopenharmony_ci	ha->isp_ops->read_optrom(vha, ha->nvram, faddr << 2, ha->nvram_size);
85968c2ecf20Sopenharmony_ci
85978c2ecf20Sopenharmony_ci	dptr = (__force __le32 *)nv;
85988c2ecf20Sopenharmony_ci	for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
85998c2ecf20Sopenharmony_ci		chksum += le32_to_cpu(*dptr);
86008c2ecf20Sopenharmony_ci
86018c2ecf20Sopenharmony_ci	ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
86028c2ecf20Sopenharmony_ci	    "Contents of NVRAM:\n");
86038c2ecf20Sopenharmony_ci	ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
86048c2ecf20Sopenharmony_ci	    nv, ha->nvram_size);
86058c2ecf20Sopenharmony_ci
86068c2ecf20Sopenharmony_ci	/* Bad NVRAM data, set defaults parameters. */
86078c2ecf20Sopenharmony_ci	if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
86088c2ecf20Sopenharmony_ci	    le16_to_cpu(nv->nvram_version) < ICB_VERSION) {
86098c2ecf20Sopenharmony_ci		/* Reset NVRAM data. */
86108c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x0073,
86118c2ecf20Sopenharmony_ci		    "Inconsistent NVRAM checksum=%#x id=%.4s version=%#x.\n",
86128c2ecf20Sopenharmony_ci		    chksum, nv->id, le16_to_cpu(nv->nvram_version));
86138c2ecf20Sopenharmony_ci		ql_dump_buffer(ql_dbg_init, vha, 0x0073, nv, sizeof(*nv));
86148c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x0074,
86158c2ecf20Sopenharmony_ci		    "Falling back to functioning (yet invalid -- WWPN) "
86168c2ecf20Sopenharmony_ci		    "defaults.\n");
86178c2ecf20Sopenharmony_ci
86188c2ecf20Sopenharmony_ci		/*
86198c2ecf20Sopenharmony_ci		 * Set default initialization control block.
86208c2ecf20Sopenharmony_ci		 */
86218c2ecf20Sopenharmony_ci		memset(nv, 0, ha->nvram_size);
86228c2ecf20Sopenharmony_ci		nv->nvram_version = cpu_to_le16(ICB_VERSION);
86238c2ecf20Sopenharmony_ci		nv->version = cpu_to_le16(ICB_VERSION);
86248c2ecf20Sopenharmony_ci		nv->frame_payload_size = cpu_to_le16(2048);
86258c2ecf20Sopenharmony_ci		nv->execution_throttle = cpu_to_le16(0xFFFF);
86268c2ecf20Sopenharmony_ci		nv->exchange_count = cpu_to_le16(0);
86278c2ecf20Sopenharmony_ci		nv->port_name[0] = 0x21;
86288c2ecf20Sopenharmony_ci		nv->port_name[1] = 0x00 + ha->port_no + 1;
86298c2ecf20Sopenharmony_ci		nv->port_name[2] = 0x00;
86308c2ecf20Sopenharmony_ci		nv->port_name[3] = 0xe0;
86318c2ecf20Sopenharmony_ci		nv->port_name[4] = 0x8b;
86328c2ecf20Sopenharmony_ci		nv->port_name[5] = 0x1c;
86338c2ecf20Sopenharmony_ci		nv->port_name[6] = 0x55;
86348c2ecf20Sopenharmony_ci		nv->port_name[7] = 0x86;
86358c2ecf20Sopenharmony_ci		nv->node_name[0] = 0x20;
86368c2ecf20Sopenharmony_ci		nv->node_name[1] = 0x00;
86378c2ecf20Sopenharmony_ci		nv->node_name[2] = 0x00;
86388c2ecf20Sopenharmony_ci		nv->node_name[3] = 0xe0;
86398c2ecf20Sopenharmony_ci		nv->node_name[4] = 0x8b;
86408c2ecf20Sopenharmony_ci		nv->node_name[5] = 0x1c;
86418c2ecf20Sopenharmony_ci		nv->node_name[6] = 0x55;
86428c2ecf20Sopenharmony_ci		nv->node_name[7] = 0x86;
86438c2ecf20Sopenharmony_ci		nv->login_retry_count = cpu_to_le16(8);
86448c2ecf20Sopenharmony_ci		nv->interrupt_delay_timer = cpu_to_le16(0);
86458c2ecf20Sopenharmony_ci		nv->login_timeout = cpu_to_le16(0);
86468c2ecf20Sopenharmony_ci		nv->firmware_options_1 =
86478c2ecf20Sopenharmony_ci		    cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
86488c2ecf20Sopenharmony_ci		nv->firmware_options_2 = cpu_to_le32(2 << 4);
86498c2ecf20Sopenharmony_ci		nv->firmware_options_2 |= cpu_to_le32(BIT_12);
86508c2ecf20Sopenharmony_ci		nv->firmware_options_3 = cpu_to_le32(2 << 13);
86518c2ecf20Sopenharmony_ci		nv->host_p = cpu_to_le32(BIT_11|BIT_10);
86528c2ecf20Sopenharmony_ci		nv->efi_parameters = cpu_to_le32(0);
86538c2ecf20Sopenharmony_ci		nv->reset_delay = 5;
86548c2ecf20Sopenharmony_ci		nv->max_luns_per_target = cpu_to_le16(128);
86558c2ecf20Sopenharmony_ci		nv->port_down_retry_count = cpu_to_le16(30);
86568c2ecf20Sopenharmony_ci		nv->link_down_timeout = cpu_to_le16(180);
86578c2ecf20Sopenharmony_ci		nv->enode_mac[0] = 0x00;
86588c2ecf20Sopenharmony_ci		nv->enode_mac[1] = 0xC0;
86598c2ecf20Sopenharmony_ci		nv->enode_mac[2] = 0xDD;
86608c2ecf20Sopenharmony_ci		nv->enode_mac[3] = 0x04;
86618c2ecf20Sopenharmony_ci		nv->enode_mac[4] = 0x05;
86628c2ecf20Sopenharmony_ci		nv->enode_mac[5] = 0x06 + ha->port_no + 1;
86638c2ecf20Sopenharmony_ci
86648c2ecf20Sopenharmony_ci		rval = 1;
86658c2ecf20Sopenharmony_ci	}
86668c2ecf20Sopenharmony_ci
86678c2ecf20Sopenharmony_ci	if (IS_T10_PI_CAPABLE(ha))
86688c2ecf20Sopenharmony_ci		nv->frame_payload_size &= cpu_to_le16(~7);
86698c2ecf20Sopenharmony_ci
86708c2ecf20Sopenharmony_ci	qlt_81xx_config_nvram_stage1(vha, nv);
86718c2ecf20Sopenharmony_ci
86728c2ecf20Sopenharmony_ci	/* Reset Initialization control block */
86738c2ecf20Sopenharmony_ci	memset(icb, 0, ha->init_cb_size);
86748c2ecf20Sopenharmony_ci
86758c2ecf20Sopenharmony_ci	/* Copy 1st segment. */
86768c2ecf20Sopenharmony_ci	dptr1 = (uint8_t *)icb;
86778c2ecf20Sopenharmony_ci	dptr2 = (uint8_t *)&nv->version;
86788c2ecf20Sopenharmony_ci	cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
86798c2ecf20Sopenharmony_ci	while (cnt--)
86808c2ecf20Sopenharmony_ci		*dptr1++ = *dptr2++;
86818c2ecf20Sopenharmony_ci
86828c2ecf20Sopenharmony_ci	icb->login_retry_count = nv->login_retry_count;
86838c2ecf20Sopenharmony_ci
86848c2ecf20Sopenharmony_ci	/* Copy 2nd segment. */
86858c2ecf20Sopenharmony_ci	dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
86868c2ecf20Sopenharmony_ci	dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
86878c2ecf20Sopenharmony_ci	cnt = (uint8_t *)&icb->reserved_5 -
86888c2ecf20Sopenharmony_ci	    (uint8_t *)&icb->interrupt_delay_timer;
86898c2ecf20Sopenharmony_ci	while (cnt--)
86908c2ecf20Sopenharmony_ci		*dptr1++ = *dptr2++;
86918c2ecf20Sopenharmony_ci
86928c2ecf20Sopenharmony_ci	memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
86938c2ecf20Sopenharmony_ci	/* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
86948c2ecf20Sopenharmony_ci	if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
86958c2ecf20Sopenharmony_ci		icb->enode_mac[0] = 0x00;
86968c2ecf20Sopenharmony_ci		icb->enode_mac[1] = 0xC0;
86978c2ecf20Sopenharmony_ci		icb->enode_mac[2] = 0xDD;
86988c2ecf20Sopenharmony_ci		icb->enode_mac[3] = 0x04;
86998c2ecf20Sopenharmony_ci		icb->enode_mac[4] = 0x05;
87008c2ecf20Sopenharmony_ci		icb->enode_mac[5] = 0x06 + ha->port_no + 1;
87018c2ecf20Sopenharmony_ci	}
87028c2ecf20Sopenharmony_ci
87038c2ecf20Sopenharmony_ci	/* Use extended-initialization control block. */
87048c2ecf20Sopenharmony_ci	memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
87058c2ecf20Sopenharmony_ci	ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
87068c2ecf20Sopenharmony_ci	/*
87078c2ecf20Sopenharmony_ci	 * Setup driver NVRAM options.
87088c2ecf20Sopenharmony_ci	 */
87098c2ecf20Sopenharmony_ci	qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
87108c2ecf20Sopenharmony_ci	    "QLE8XXX");
87118c2ecf20Sopenharmony_ci
87128c2ecf20Sopenharmony_ci	qlt_81xx_config_nvram_stage2(vha, icb);
87138c2ecf20Sopenharmony_ci
87148c2ecf20Sopenharmony_ci	/* Use alternate WWN? */
87158c2ecf20Sopenharmony_ci	if (nv->host_p & cpu_to_le32(BIT_15)) {
87168c2ecf20Sopenharmony_ci		memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
87178c2ecf20Sopenharmony_ci		memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
87188c2ecf20Sopenharmony_ci	}
87198c2ecf20Sopenharmony_ci
87208c2ecf20Sopenharmony_ci	/* Prepare nodename */
87218c2ecf20Sopenharmony_ci	if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
87228c2ecf20Sopenharmony_ci		/*
87238c2ecf20Sopenharmony_ci		 * Firmware will apply the following mask if the nodename was
87248c2ecf20Sopenharmony_ci		 * not provided.
87258c2ecf20Sopenharmony_ci		 */
87268c2ecf20Sopenharmony_ci		memcpy(icb->node_name, icb->port_name, WWN_SIZE);
87278c2ecf20Sopenharmony_ci		icb->node_name[0] &= 0xF0;
87288c2ecf20Sopenharmony_ci	}
87298c2ecf20Sopenharmony_ci
87308c2ecf20Sopenharmony_ci	if (IS_QLA28XX(ha) || IS_QLA27XX(ha)) {
87318c2ecf20Sopenharmony_ci		if ((nv->enhanced_features & BIT_7) == 0)
87328c2ecf20Sopenharmony_ci			ha->flags.scm_supported_a = 1;
87338c2ecf20Sopenharmony_ci	}
87348c2ecf20Sopenharmony_ci
87358c2ecf20Sopenharmony_ci	/* Set host adapter parameters. */
87368c2ecf20Sopenharmony_ci	ha->flags.disable_risc_code_load = 0;
87378c2ecf20Sopenharmony_ci	ha->flags.enable_lip_reset = 0;
87388c2ecf20Sopenharmony_ci	ha->flags.enable_lip_full_login =
87398c2ecf20Sopenharmony_ci	    le32_to_cpu(nv->host_p) & BIT_10 ? 1 : 0;
87408c2ecf20Sopenharmony_ci	ha->flags.enable_target_reset =
87418c2ecf20Sopenharmony_ci	    le32_to_cpu(nv->host_p) & BIT_11 ? 1 : 0;
87428c2ecf20Sopenharmony_ci	ha->flags.enable_led_scheme = 0;
87438c2ecf20Sopenharmony_ci	ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1 : 0;
87448c2ecf20Sopenharmony_ci
87458c2ecf20Sopenharmony_ci	ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
87468c2ecf20Sopenharmony_ci	    (BIT_6 | BIT_5 | BIT_4)) >> 4;
87478c2ecf20Sopenharmony_ci
87488c2ecf20Sopenharmony_ci	/* save HBA serial number */
87498c2ecf20Sopenharmony_ci	ha->serial0 = icb->port_name[5];
87508c2ecf20Sopenharmony_ci	ha->serial1 = icb->port_name[6];
87518c2ecf20Sopenharmony_ci	ha->serial2 = icb->port_name[7];
87528c2ecf20Sopenharmony_ci	memcpy(vha->node_name, icb->node_name, WWN_SIZE);
87538c2ecf20Sopenharmony_ci	memcpy(vha->port_name, icb->port_name, WWN_SIZE);
87548c2ecf20Sopenharmony_ci
87558c2ecf20Sopenharmony_ci	icb->execution_throttle = cpu_to_le16(0xFFFF);
87568c2ecf20Sopenharmony_ci
87578c2ecf20Sopenharmony_ci	ha->retry_count = le16_to_cpu(nv->login_retry_count);
87588c2ecf20Sopenharmony_ci
87598c2ecf20Sopenharmony_ci	/* Set minimum login_timeout to 4 seconds. */
87608c2ecf20Sopenharmony_ci	if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
87618c2ecf20Sopenharmony_ci		nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
87628c2ecf20Sopenharmony_ci	if (le16_to_cpu(nv->login_timeout) < 4)
87638c2ecf20Sopenharmony_ci		nv->login_timeout = cpu_to_le16(4);
87648c2ecf20Sopenharmony_ci	ha->login_timeout = le16_to_cpu(nv->login_timeout);
87658c2ecf20Sopenharmony_ci
87668c2ecf20Sopenharmony_ci	/* Set minimum RATOV to 100 tenths of a second. */
87678c2ecf20Sopenharmony_ci	ha->r_a_tov = 100;
87688c2ecf20Sopenharmony_ci
87698c2ecf20Sopenharmony_ci	ha->loop_reset_delay = nv->reset_delay;
87708c2ecf20Sopenharmony_ci
87718c2ecf20Sopenharmony_ci	/* Link Down Timeout = 0:
87728c2ecf20Sopenharmony_ci	 *
87738c2ecf20Sopenharmony_ci	 *	When Port Down timer expires we will start returning
87748c2ecf20Sopenharmony_ci	 *	I/O's to OS with "DID_NO_CONNECT".
87758c2ecf20Sopenharmony_ci	 *
87768c2ecf20Sopenharmony_ci	 * Link Down Timeout != 0:
87778c2ecf20Sopenharmony_ci	 *
87788c2ecf20Sopenharmony_ci	 *	 The driver waits for the link to come up after link down
87798c2ecf20Sopenharmony_ci	 *	 before returning I/Os to OS with "DID_NO_CONNECT".
87808c2ecf20Sopenharmony_ci	 */
87818c2ecf20Sopenharmony_ci	if (le16_to_cpu(nv->link_down_timeout) == 0) {
87828c2ecf20Sopenharmony_ci		ha->loop_down_abort_time =
87838c2ecf20Sopenharmony_ci		    (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
87848c2ecf20Sopenharmony_ci	} else {
87858c2ecf20Sopenharmony_ci		ha->link_down_timeout =	le16_to_cpu(nv->link_down_timeout);
87868c2ecf20Sopenharmony_ci		ha->loop_down_abort_time =
87878c2ecf20Sopenharmony_ci		    (LOOP_DOWN_TIME - ha->link_down_timeout);
87888c2ecf20Sopenharmony_ci	}
87898c2ecf20Sopenharmony_ci
87908c2ecf20Sopenharmony_ci	/* Need enough time to try and get the port back. */
87918c2ecf20Sopenharmony_ci	ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
87928c2ecf20Sopenharmony_ci	if (qlport_down_retry)
87938c2ecf20Sopenharmony_ci		ha->port_down_retry_count = qlport_down_retry;
87948c2ecf20Sopenharmony_ci
87958c2ecf20Sopenharmony_ci	/* Set login_retry_count */
87968c2ecf20Sopenharmony_ci	ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
87978c2ecf20Sopenharmony_ci	if (ha->port_down_retry_count ==
87988c2ecf20Sopenharmony_ci	    le16_to_cpu(nv->port_down_retry_count) &&
87998c2ecf20Sopenharmony_ci	    ha->port_down_retry_count > 3)
88008c2ecf20Sopenharmony_ci		ha->login_retry_count = ha->port_down_retry_count;
88018c2ecf20Sopenharmony_ci	else if (ha->port_down_retry_count > (int)ha->login_retry_count)
88028c2ecf20Sopenharmony_ci		ha->login_retry_count = ha->port_down_retry_count;
88038c2ecf20Sopenharmony_ci	if (ql2xloginretrycount)
88048c2ecf20Sopenharmony_ci		ha->login_retry_count = ql2xloginretrycount;
88058c2ecf20Sopenharmony_ci
88068c2ecf20Sopenharmony_ci	/* if not running MSI-X we need handshaking on interrupts */
88078c2ecf20Sopenharmony_ci	if (!vha->hw->flags.msix_enabled &&
88088c2ecf20Sopenharmony_ci	    (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)))
88098c2ecf20Sopenharmony_ci		icb->firmware_options_2 |= cpu_to_le32(BIT_22);
88108c2ecf20Sopenharmony_ci
88118c2ecf20Sopenharmony_ci	/* Enable ZIO. */
88128c2ecf20Sopenharmony_ci	if (!vha->flags.init_done) {
88138c2ecf20Sopenharmony_ci		ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
88148c2ecf20Sopenharmony_ci		    (BIT_3 | BIT_2 | BIT_1 | BIT_0);
88158c2ecf20Sopenharmony_ci		ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
88168c2ecf20Sopenharmony_ci		    le16_to_cpu(icb->interrupt_delay_timer) : 2;
88178c2ecf20Sopenharmony_ci	}
88188c2ecf20Sopenharmony_ci	icb->firmware_options_2 &= cpu_to_le32(
88198c2ecf20Sopenharmony_ci	    ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
88208c2ecf20Sopenharmony_ci	vha->flags.process_response_queue = 0;
88218c2ecf20Sopenharmony_ci	if (ha->zio_mode != QLA_ZIO_DISABLED) {
88228c2ecf20Sopenharmony_ci		ha->zio_mode = QLA_ZIO_MODE_6;
88238c2ecf20Sopenharmony_ci
88248c2ecf20Sopenharmony_ci		ql_log(ql_log_info, vha, 0x0075,
88258c2ecf20Sopenharmony_ci		    "ZIO mode %d enabled; timer delay (%d us).\n",
88268c2ecf20Sopenharmony_ci		    ha->zio_mode,
88278c2ecf20Sopenharmony_ci		    ha->zio_timer * 100);
88288c2ecf20Sopenharmony_ci
88298c2ecf20Sopenharmony_ci		icb->firmware_options_2 |= cpu_to_le32(
88308c2ecf20Sopenharmony_ci		    (uint32_t)ha->zio_mode);
88318c2ecf20Sopenharmony_ci		icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
88328c2ecf20Sopenharmony_ci		vha->flags.process_response_queue = 1;
88338c2ecf20Sopenharmony_ci	}
88348c2ecf20Sopenharmony_ci
88358c2ecf20Sopenharmony_ci	 /* enable RIDA Format2 */
88368c2ecf20Sopenharmony_ci	icb->firmware_options_3 |= cpu_to_le32(BIT_0);
88378c2ecf20Sopenharmony_ci
88388c2ecf20Sopenharmony_ci	/* N2N: driver will initiate Login instead of FW */
88398c2ecf20Sopenharmony_ci	icb->firmware_options_3 |= cpu_to_le32(BIT_8);
88408c2ecf20Sopenharmony_ci
88418c2ecf20Sopenharmony_ci	/* Determine NVMe/FCP priority for target ports */
88428c2ecf20Sopenharmony_ci	ha->fc4_type_priority = qla2xxx_get_fc4_priority(vha);
88438c2ecf20Sopenharmony_ci
88448c2ecf20Sopenharmony_ci	if (rval) {
88458c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x0076,
88468c2ecf20Sopenharmony_ci		    "NVRAM configuration failed.\n");
88478c2ecf20Sopenharmony_ci	}
88488c2ecf20Sopenharmony_ci	return (rval);
88498c2ecf20Sopenharmony_ci}
88508c2ecf20Sopenharmony_ci
88518c2ecf20Sopenharmony_ciint
88528c2ecf20Sopenharmony_ciqla82xx_restart_isp(scsi_qla_host_t *vha)
88538c2ecf20Sopenharmony_ci{
88548c2ecf20Sopenharmony_ci	int status, rval;
88558c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
88568c2ecf20Sopenharmony_ci	struct scsi_qla_host *vp;
88578c2ecf20Sopenharmony_ci	unsigned long flags;
88588c2ecf20Sopenharmony_ci
88598c2ecf20Sopenharmony_ci	status = qla2x00_init_rings(vha);
88608c2ecf20Sopenharmony_ci	if (!status) {
88618c2ecf20Sopenharmony_ci		clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
88628c2ecf20Sopenharmony_ci		ha->flags.chip_reset_done = 1;
88638c2ecf20Sopenharmony_ci
88648c2ecf20Sopenharmony_ci		status = qla2x00_fw_ready(vha);
88658c2ecf20Sopenharmony_ci		if (!status) {
88668c2ecf20Sopenharmony_ci			/* Issue a marker after FW becomes ready. */
88678c2ecf20Sopenharmony_ci			qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
88688c2ecf20Sopenharmony_ci			vha->flags.online = 1;
88698c2ecf20Sopenharmony_ci			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
88708c2ecf20Sopenharmony_ci		}
88718c2ecf20Sopenharmony_ci
88728c2ecf20Sopenharmony_ci		/* if no cable then assume it's good */
88738c2ecf20Sopenharmony_ci		if ((vha->device_flags & DFLG_NO_CABLE))
88748c2ecf20Sopenharmony_ci			status = 0;
88758c2ecf20Sopenharmony_ci	}
88768c2ecf20Sopenharmony_ci
88778c2ecf20Sopenharmony_ci	if (!status) {
88788c2ecf20Sopenharmony_ci		clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
88798c2ecf20Sopenharmony_ci
88808c2ecf20Sopenharmony_ci		if (!atomic_read(&vha->loop_down_timer)) {
88818c2ecf20Sopenharmony_ci			/*
88828c2ecf20Sopenharmony_ci			 * Issue marker command only when we are going
88838c2ecf20Sopenharmony_ci			 * to start the I/O .
88848c2ecf20Sopenharmony_ci			 */
88858c2ecf20Sopenharmony_ci			vha->marker_needed = 1;
88868c2ecf20Sopenharmony_ci		}
88878c2ecf20Sopenharmony_ci
88888c2ecf20Sopenharmony_ci		ha->isp_ops->enable_intrs(ha);
88898c2ecf20Sopenharmony_ci
88908c2ecf20Sopenharmony_ci		ha->isp_abort_cnt = 0;
88918c2ecf20Sopenharmony_ci		clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
88928c2ecf20Sopenharmony_ci
88938c2ecf20Sopenharmony_ci		/* Update the firmware version */
88948c2ecf20Sopenharmony_ci		status = qla82xx_check_md_needed(vha);
88958c2ecf20Sopenharmony_ci
88968c2ecf20Sopenharmony_ci		if (ha->fce) {
88978c2ecf20Sopenharmony_ci			ha->flags.fce_enabled = 1;
88988c2ecf20Sopenharmony_ci			memset(ha->fce, 0,
88998c2ecf20Sopenharmony_ci			    fce_calc_size(ha->fce_bufs));
89008c2ecf20Sopenharmony_ci			rval = qla2x00_enable_fce_trace(vha,
89018c2ecf20Sopenharmony_ci			    ha->fce_dma, ha->fce_bufs, ha->fce_mb,
89028c2ecf20Sopenharmony_ci			    &ha->fce_bufs);
89038c2ecf20Sopenharmony_ci			if (rval) {
89048c2ecf20Sopenharmony_ci				ql_log(ql_log_warn, vha, 0x8001,
89058c2ecf20Sopenharmony_ci				    "Unable to reinitialize FCE (%d).\n",
89068c2ecf20Sopenharmony_ci				    rval);
89078c2ecf20Sopenharmony_ci				ha->flags.fce_enabled = 0;
89088c2ecf20Sopenharmony_ci			}
89098c2ecf20Sopenharmony_ci		}
89108c2ecf20Sopenharmony_ci
89118c2ecf20Sopenharmony_ci		if (ha->eft) {
89128c2ecf20Sopenharmony_ci			memset(ha->eft, 0, EFT_SIZE);
89138c2ecf20Sopenharmony_ci			rval = qla2x00_enable_eft_trace(vha,
89148c2ecf20Sopenharmony_ci			    ha->eft_dma, EFT_NUM_BUFFERS);
89158c2ecf20Sopenharmony_ci			if (rval) {
89168c2ecf20Sopenharmony_ci				ql_log(ql_log_warn, vha, 0x8010,
89178c2ecf20Sopenharmony_ci				    "Unable to reinitialize EFT (%d).\n",
89188c2ecf20Sopenharmony_ci				    rval);
89198c2ecf20Sopenharmony_ci			}
89208c2ecf20Sopenharmony_ci		}
89218c2ecf20Sopenharmony_ci	}
89228c2ecf20Sopenharmony_ci
89238c2ecf20Sopenharmony_ci	if (!status) {
89248c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_taskm, vha, 0x8011,
89258c2ecf20Sopenharmony_ci		    "qla82xx_restart_isp succeeded.\n");
89268c2ecf20Sopenharmony_ci
89278c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->vport_slock, flags);
89288c2ecf20Sopenharmony_ci		list_for_each_entry(vp, &ha->vp_list, list) {
89298c2ecf20Sopenharmony_ci			if (vp->vp_idx) {
89308c2ecf20Sopenharmony_ci				atomic_inc(&vp->vref_count);
89318c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&ha->vport_slock, flags);
89328c2ecf20Sopenharmony_ci
89338c2ecf20Sopenharmony_ci				qla2x00_vp_abort_isp(vp);
89348c2ecf20Sopenharmony_ci
89358c2ecf20Sopenharmony_ci				spin_lock_irqsave(&ha->vport_slock, flags);
89368c2ecf20Sopenharmony_ci				atomic_dec(&vp->vref_count);
89378c2ecf20Sopenharmony_ci			}
89388c2ecf20Sopenharmony_ci		}
89398c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->vport_slock, flags);
89408c2ecf20Sopenharmony_ci
89418c2ecf20Sopenharmony_ci	} else {
89428c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x8016,
89438c2ecf20Sopenharmony_ci		    "qla82xx_restart_isp **** FAILED ****.\n");
89448c2ecf20Sopenharmony_ci	}
89458c2ecf20Sopenharmony_ci
89468c2ecf20Sopenharmony_ci	return status;
89478c2ecf20Sopenharmony_ci}
89488c2ecf20Sopenharmony_ci
89498c2ecf20Sopenharmony_ci/*
89508c2ecf20Sopenharmony_ci * qla24xx_get_fcp_prio
89518c2ecf20Sopenharmony_ci *	Gets the fcp cmd priority value for the logged in port.
89528c2ecf20Sopenharmony_ci *	Looks for a match of the port descriptors within
89538c2ecf20Sopenharmony_ci *	each of the fcp prio config entries. If a match is found,
89548c2ecf20Sopenharmony_ci *	the tag (priority) value is returned.
89558c2ecf20Sopenharmony_ci *
89568c2ecf20Sopenharmony_ci * Input:
89578c2ecf20Sopenharmony_ci *	vha = scsi host structure pointer.
89588c2ecf20Sopenharmony_ci *	fcport = port structure pointer.
89598c2ecf20Sopenharmony_ci *
89608c2ecf20Sopenharmony_ci * Return:
89618c2ecf20Sopenharmony_ci *	non-zero (if found)
89628c2ecf20Sopenharmony_ci *	-1 (if not found)
89638c2ecf20Sopenharmony_ci *
89648c2ecf20Sopenharmony_ci * Context:
89658c2ecf20Sopenharmony_ci * 	Kernel context
89668c2ecf20Sopenharmony_ci */
89678c2ecf20Sopenharmony_cistatic int
89688c2ecf20Sopenharmony_ciqla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
89698c2ecf20Sopenharmony_ci{
89708c2ecf20Sopenharmony_ci	int i, entries;
89718c2ecf20Sopenharmony_ci	uint8_t pid_match, wwn_match;
89728c2ecf20Sopenharmony_ci	int priority;
89738c2ecf20Sopenharmony_ci	uint32_t pid1, pid2;
89748c2ecf20Sopenharmony_ci	uint64_t wwn1, wwn2;
89758c2ecf20Sopenharmony_ci	struct qla_fcp_prio_entry *pri_entry;
89768c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
89778c2ecf20Sopenharmony_ci
89788c2ecf20Sopenharmony_ci	if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
89798c2ecf20Sopenharmony_ci		return -1;
89808c2ecf20Sopenharmony_ci
89818c2ecf20Sopenharmony_ci	priority = -1;
89828c2ecf20Sopenharmony_ci	entries = ha->fcp_prio_cfg->num_entries;
89838c2ecf20Sopenharmony_ci	pri_entry = &ha->fcp_prio_cfg->entry[0];
89848c2ecf20Sopenharmony_ci
89858c2ecf20Sopenharmony_ci	for (i = 0; i < entries; i++) {
89868c2ecf20Sopenharmony_ci		pid_match = wwn_match = 0;
89878c2ecf20Sopenharmony_ci
89888c2ecf20Sopenharmony_ci		if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
89898c2ecf20Sopenharmony_ci			pri_entry++;
89908c2ecf20Sopenharmony_ci			continue;
89918c2ecf20Sopenharmony_ci		}
89928c2ecf20Sopenharmony_ci
89938c2ecf20Sopenharmony_ci		/* check source pid for a match */
89948c2ecf20Sopenharmony_ci		if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
89958c2ecf20Sopenharmony_ci			pid1 = pri_entry->src_pid & INVALID_PORT_ID;
89968c2ecf20Sopenharmony_ci			pid2 = vha->d_id.b24 & INVALID_PORT_ID;
89978c2ecf20Sopenharmony_ci			if (pid1 == INVALID_PORT_ID)
89988c2ecf20Sopenharmony_ci				pid_match++;
89998c2ecf20Sopenharmony_ci			else if (pid1 == pid2)
90008c2ecf20Sopenharmony_ci				pid_match++;
90018c2ecf20Sopenharmony_ci		}
90028c2ecf20Sopenharmony_ci
90038c2ecf20Sopenharmony_ci		/* check destination pid for a match */
90048c2ecf20Sopenharmony_ci		if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
90058c2ecf20Sopenharmony_ci			pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
90068c2ecf20Sopenharmony_ci			pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
90078c2ecf20Sopenharmony_ci			if (pid1 == INVALID_PORT_ID)
90088c2ecf20Sopenharmony_ci				pid_match++;
90098c2ecf20Sopenharmony_ci			else if (pid1 == pid2)
90108c2ecf20Sopenharmony_ci				pid_match++;
90118c2ecf20Sopenharmony_ci		}
90128c2ecf20Sopenharmony_ci
90138c2ecf20Sopenharmony_ci		/* check source WWN for a match */
90148c2ecf20Sopenharmony_ci		if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
90158c2ecf20Sopenharmony_ci			wwn1 = wwn_to_u64(vha->port_name);
90168c2ecf20Sopenharmony_ci			wwn2 = wwn_to_u64(pri_entry->src_wwpn);
90178c2ecf20Sopenharmony_ci			if (wwn2 == (uint64_t)-1)
90188c2ecf20Sopenharmony_ci				wwn_match++;
90198c2ecf20Sopenharmony_ci			else if (wwn1 == wwn2)
90208c2ecf20Sopenharmony_ci				wwn_match++;
90218c2ecf20Sopenharmony_ci		}
90228c2ecf20Sopenharmony_ci
90238c2ecf20Sopenharmony_ci		/* check destination WWN for a match */
90248c2ecf20Sopenharmony_ci		if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
90258c2ecf20Sopenharmony_ci			wwn1 = wwn_to_u64(fcport->port_name);
90268c2ecf20Sopenharmony_ci			wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
90278c2ecf20Sopenharmony_ci			if (wwn2 == (uint64_t)-1)
90288c2ecf20Sopenharmony_ci				wwn_match++;
90298c2ecf20Sopenharmony_ci			else if (wwn1 == wwn2)
90308c2ecf20Sopenharmony_ci				wwn_match++;
90318c2ecf20Sopenharmony_ci		}
90328c2ecf20Sopenharmony_ci
90338c2ecf20Sopenharmony_ci		if (pid_match == 2 || wwn_match == 2) {
90348c2ecf20Sopenharmony_ci			/* Found a matching entry */
90358c2ecf20Sopenharmony_ci			if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
90368c2ecf20Sopenharmony_ci				priority = pri_entry->tag;
90378c2ecf20Sopenharmony_ci			break;
90388c2ecf20Sopenharmony_ci		}
90398c2ecf20Sopenharmony_ci
90408c2ecf20Sopenharmony_ci		pri_entry++;
90418c2ecf20Sopenharmony_ci	}
90428c2ecf20Sopenharmony_ci
90438c2ecf20Sopenharmony_ci	return priority;
90448c2ecf20Sopenharmony_ci}
90458c2ecf20Sopenharmony_ci
90468c2ecf20Sopenharmony_ci/*
90478c2ecf20Sopenharmony_ci * qla24xx_update_fcport_fcp_prio
90488c2ecf20Sopenharmony_ci *	Activates fcp priority for the logged in fc port
90498c2ecf20Sopenharmony_ci *
90508c2ecf20Sopenharmony_ci * Input:
90518c2ecf20Sopenharmony_ci *	vha = scsi host structure pointer.
90528c2ecf20Sopenharmony_ci *	fcp = port structure pointer.
90538c2ecf20Sopenharmony_ci *
90548c2ecf20Sopenharmony_ci * Return:
90558c2ecf20Sopenharmony_ci *	QLA_SUCCESS or QLA_FUNCTION_FAILED
90568c2ecf20Sopenharmony_ci *
90578c2ecf20Sopenharmony_ci * Context:
90588c2ecf20Sopenharmony_ci *	Kernel context.
90598c2ecf20Sopenharmony_ci */
90608c2ecf20Sopenharmony_ciint
90618c2ecf20Sopenharmony_ciqla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
90628c2ecf20Sopenharmony_ci{
90638c2ecf20Sopenharmony_ci	int ret;
90648c2ecf20Sopenharmony_ci	int priority;
90658c2ecf20Sopenharmony_ci	uint16_t mb[5];
90668c2ecf20Sopenharmony_ci
90678c2ecf20Sopenharmony_ci	if (fcport->port_type != FCT_TARGET ||
90688c2ecf20Sopenharmony_ci	    fcport->loop_id == FC_NO_LOOP_ID)
90698c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
90708c2ecf20Sopenharmony_ci
90718c2ecf20Sopenharmony_ci	priority = qla24xx_get_fcp_prio(vha, fcport);
90728c2ecf20Sopenharmony_ci	if (priority < 0)
90738c2ecf20Sopenharmony_ci		return QLA_FUNCTION_FAILED;
90748c2ecf20Sopenharmony_ci
90758c2ecf20Sopenharmony_ci	if (IS_P3P_TYPE(vha->hw)) {
90768c2ecf20Sopenharmony_ci		fcport->fcp_prio = priority & 0xf;
90778c2ecf20Sopenharmony_ci		return QLA_SUCCESS;
90788c2ecf20Sopenharmony_ci	}
90798c2ecf20Sopenharmony_ci
90808c2ecf20Sopenharmony_ci	ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
90818c2ecf20Sopenharmony_ci	if (ret == QLA_SUCCESS) {
90828c2ecf20Sopenharmony_ci		if (fcport->fcp_prio != priority)
90838c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_user, vha, 0x709e,
90848c2ecf20Sopenharmony_ci			    "Updated FCP_CMND priority - value=%d loop_id=%d "
90858c2ecf20Sopenharmony_ci			    "port_id=%02x%02x%02x.\n", priority,
90868c2ecf20Sopenharmony_ci			    fcport->loop_id, fcport->d_id.b.domain,
90878c2ecf20Sopenharmony_ci			    fcport->d_id.b.area, fcport->d_id.b.al_pa);
90888c2ecf20Sopenharmony_ci		fcport->fcp_prio = priority & 0xf;
90898c2ecf20Sopenharmony_ci	} else
90908c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_user, vha, 0x704f,
90918c2ecf20Sopenharmony_ci		    "Unable to update FCP_CMND priority - ret=0x%x for "
90928c2ecf20Sopenharmony_ci		    "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
90938c2ecf20Sopenharmony_ci		    fcport->d_id.b.domain, fcport->d_id.b.area,
90948c2ecf20Sopenharmony_ci		    fcport->d_id.b.al_pa);
90958c2ecf20Sopenharmony_ci	return  ret;
90968c2ecf20Sopenharmony_ci}
90978c2ecf20Sopenharmony_ci
90988c2ecf20Sopenharmony_ci/*
90998c2ecf20Sopenharmony_ci * qla24xx_update_all_fcp_prio
91008c2ecf20Sopenharmony_ci *	Activates fcp priority for all the logged in ports
91018c2ecf20Sopenharmony_ci *
91028c2ecf20Sopenharmony_ci * Input:
91038c2ecf20Sopenharmony_ci *	ha = adapter block pointer.
91048c2ecf20Sopenharmony_ci *
91058c2ecf20Sopenharmony_ci * Return:
91068c2ecf20Sopenharmony_ci *	QLA_SUCCESS or QLA_FUNCTION_FAILED
91078c2ecf20Sopenharmony_ci *
91088c2ecf20Sopenharmony_ci * Context:
91098c2ecf20Sopenharmony_ci *	Kernel context.
91108c2ecf20Sopenharmony_ci */
91118c2ecf20Sopenharmony_ciint
91128c2ecf20Sopenharmony_ciqla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
91138c2ecf20Sopenharmony_ci{
91148c2ecf20Sopenharmony_ci	int ret;
91158c2ecf20Sopenharmony_ci	fc_port_t *fcport;
91168c2ecf20Sopenharmony_ci
91178c2ecf20Sopenharmony_ci	ret = QLA_FUNCTION_FAILED;
91188c2ecf20Sopenharmony_ci	/* We need to set priority for all logged in ports */
91198c2ecf20Sopenharmony_ci	list_for_each_entry(fcport, &vha->vp_fcports, list)
91208c2ecf20Sopenharmony_ci		ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
91218c2ecf20Sopenharmony_ci
91228c2ecf20Sopenharmony_ci	return ret;
91238c2ecf20Sopenharmony_ci}
91248c2ecf20Sopenharmony_ci
91258c2ecf20Sopenharmony_cistruct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
91268c2ecf20Sopenharmony_ci	int vp_idx, bool startqp)
91278c2ecf20Sopenharmony_ci{
91288c2ecf20Sopenharmony_ci	int rsp_id = 0;
91298c2ecf20Sopenharmony_ci	int  req_id = 0;
91308c2ecf20Sopenharmony_ci	int i;
91318c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
91328c2ecf20Sopenharmony_ci	uint16_t qpair_id = 0;
91338c2ecf20Sopenharmony_ci	struct qla_qpair *qpair = NULL;
91348c2ecf20Sopenharmony_ci	struct qla_msix_entry *msix;
91358c2ecf20Sopenharmony_ci
91368c2ecf20Sopenharmony_ci	if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
91378c2ecf20Sopenharmony_ci		ql_log(ql_log_warn, vha, 0x00181,
91388c2ecf20Sopenharmony_ci		    "FW/Driver is not multi-queue capable.\n");
91398c2ecf20Sopenharmony_ci		return NULL;
91408c2ecf20Sopenharmony_ci	}
91418c2ecf20Sopenharmony_ci
91428c2ecf20Sopenharmony_ci	if (ql2xmqsupport || ql2xnvmeenable) {
91438c2ecf20Sopenharmony_ci		qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
91448c2ecf20Sopenharmony_ci		if (qpair == NULL) {
91458c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0182,
91468c2ecf20Sopenharmony_ci			    "Failed to allocate memory for queue pair.\n");
91478c2ecf20Sopenharmony_ci			return NULL;
91488c2ecf20Sopenharmony_ci		}
91498c2ecf20Sopenharmony_ci
91508c2ecf20Sopenharmony_ci		qpair->hw = vha->hw;
91518c2ecf20Sopenharmony_ci		qpair->vha = vha;
91528c2ecf20Sopenharmony_ci		qpair->qp_lock_ptr = &qpair->qp_lock;
91538c2ecf20Sopenharmony_ci		spin_lock_init(&qpair->qp_lock);
91548c2ecf20Sopenharmony_ci		qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
91558c2ecf20Sopenharmony_ci
91568c2ecf20Sopenharmony_ci		/* Assign available que pair id */
91578c2ecf20Sopenharmony_ci		mutex_lock(&ha->mq_lock);
91588c2ecf20Sopenharmony_ci		qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
91598c2ecf20Sopenharmony_ci		if (ha->num_qpairs >= ha->max_qpairs) {
91608c2ecf20Sopenharmony_ci			mutex_unlock(&ha->mq_lock);
91618c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0183,
91628c2ecf20Sopenharmony_ci			    "No resources to create additional q pair.\n");
91638c2ecf20Sopenharmony_ci			goto fail_qid_map;
91648c2ecf20Sopenharmony_ci		}
91658c2ecf20Sopenharmony_ci		ha->num_qpairs++;
91668c2ecf20Sopenharmony_ci		set_bit(qpair_id, ha->qpair_qid_map);
91678c2ecf20Sopenharmony_ci		ha->queue_pair_map[qpair_id] = qpair;
91688c2ecf20Sopenharmony_ci		qpair->id = qpair_id;
91698c2ecf20Sopenharmony_ci		qpair->vp_idx = vp_idx;
91708c2ecf20Sopenharmony_ci		qpair->fw_started = ha->flags.fw_started;
91718c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&qpair->hints_list);
91728c2ecf20Sopenharmony_ci		qpair->chip_reset = ha->base_qpair->chip_reset;
91738c2ecf20Sopenharmony_ci		qpair->enable_class_2 = ha->base_qpair->enable_class_2;
91748c2ecf20Sopenharmony_ci		qpair->enable_explicit_conf =
91758c2ecf20Sopenharmony_ci		    ha->base_qpair->enable_explicit_conf;
91768c2ecf20Sopenharmony_ci
91778c2ecf20Sopenharmony_ci		for (i = 0; i < ha->msix_count; i++) {
91788c2ecf20Sopenharmony_ci			msix = &ha->msix_entries[i];
91798c2ecf20Sopenharmony_ci			if (msix->in_use)
91808c2ecf20Sopenharmony_ci				continue;
91818c2ecf20Sopenharmony_ci			qpair->msix = msix;
91828c2ecf20Sopenharmony_ci			ql_dbg(ql_dbg_multiq, vha, 0xc00f,
91838c2ecf20Sopenharmony_ci			    "Vector %x selected for qpair\n", msix->vector);
91848c2ecf20Sopenharmony_ci			break;
91858c2ecf20Sopenharmony_ci		}
91868c2ecf20Sopenharmony_ci		if (!qpair->msix) {
91878c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0184,
91888c2ecf20Sopenharmony_ci			    "Out of MSI-X vectors!.\n");
91898c2ecf20Sopenharmony_ci			goto fail_msix;
91908c2ecf20Sopenharmony_ci		}
91918c2ecf20Sopenharmony_ci
91928c2ecf20Sopenharmony_ci		qpair->msix->in_use = 1;
91938c2ecf20Sopenharmony_ci		list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
91948c2ecf20Sopenharmony_ci		qpair->pdev = ha->pdev;
91958c2ecf20Sopenharmony_ci		if (IS_QLA27XX(ha) || IS_QLA83XX(ha) || IS_QLA28XX(ha))
91968c2ecf20Sopenharmony_ci			qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
91978c2ecf20Sopenharmony_ci
91988c2ecf20Sopenharmony_ci		mutex_unlock(&ha->mq_lock);
91998c2ecf20Sopenharmony_ci
92008c2ecf20Sopenharmony_ci		/* Create response queue first */
92018c2ecf20Sopenharmony_ci		rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
92028c2ecf20Sopenharmony_ci		if (!rsp_id) {
92038c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0185,
92048c2ecf20Sopenharmony_ci			    "Failed to create response queue.\n");
92058c2ecf20Sopenharmony_ci			goto fail_rsp;
92068c2ecf20Sopenharmony_ci		}
92078c2ecf20Sopenharmony_ci
92088c2ecf20Sopenharmony_ci		qpair->rsp = ha->rsp_q_map[rsp_id];
92098c2ecf20Sopenharmony_ci
92108c2ecf20Sopenharmony_ci		/* Create request queue */
92118c2ecf20Sopenharmony_ci		req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
92128c2ecf20Sopenharmony_ci		    startqp);
92138c2ecf20Sopenharmony_ci		if (!req_id) {
92148c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0x0186,
92158c2ecf20Sopenharmony_ci			    "Failed to create request queue.\n");
92168c2ecf20Sopenharmony_ci			goto fail_req;
92178c2ecf20Sopenharmony_ci		}
92188c2ecf20Sopenharmony_ci
92198c2ecf20Sopenharmony_ci		qpair->req = ha->req_q_map[req_id];
92208c2ecf20Sopenharmony_ci		qpair->rsp->req = qpair->req;
92218c2ecf20Sopenharmony_ci		qpair->rsp->qpair = qpair;
92228c2ecf20Sopenharmony_ci		/* init qpair to this cpu. Will adjust at run time. */
92238c2ecf20Sopenharmony_ci		qla_cpu_update(qpair, raw_smp_processor_id());
92248c2ecf20Sopenharmony_ci
92258c2ecf20Sopenharmony_ci		if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
92268c2ecf20Sopenharmony_ci			if (ha->fw_attributes & BIT_4)
92278c2ecf20Sopenharmony_ci				qpair->difdix_supported = 1;
92288c2ecf20Sopenharmony_ci		}
92298c2ecf20Sopenharmony_ci
92308c2ecf20Sopenharmony_ci		qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
92318c2ecf20Sopenharmony_ci		if (!qpair->srb_mempool) {
92328c2ecf20Sopenharmony_ci			ql_log(ql_log_warn, vha, 0xd036,
92338c2ecf20Sopenharmony_ci			    "Failed to create srb mempool for qpair %d\n",
92348c2ecf20Sopenharmony_ci			    qpair->id);
92358c2ecf20Sopenharmony_ci			goto fail_mempool;
92368c2ecf20Sopenharmony_ci		}
92378c2ecf20Sopenharmony_ci
92388c2ecf20Sopenharmony_ci		/* Mark as online */
92398c2ecf20Sopenharmony_ci		qpair->online = 1;
92408c2ecf20Sopenharmony_ci
92418c2ecf20Sopenharmony_ci		if (!vha->flags.qpairs_available)
92428c2ecf20Sopenharmony_ci			vha->flags.qpairs_available = 1;
92438c2ecf20Sopenharmony_ci
92448c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_multiq, vha, 0xc00d,
92458c2ecf20Sopenharmony_ci		    "Request/Response queue pair created, id %d\n",
92468c2ecf20Sopenharmony_ci		    qpair->id);
92478c2ecf20Sopenharmony_ci		ql_dbg(ql_dbg_init, vha, 0x0187,
92488c2ecf20Sopenharmony_ci		    "Request/Response queue pair created, id %d\n",
92498c2ecf20Sopenharmony_ci		    qpair->id);
92508c2ecf20Sopenharmony_ci	}
92518c2ecf20Sopenharmony_ci	return qpair;
92528c2ecf20Sopenharmony_ci
92538c2ecf20Sopenharmony_cifail_mempool:
92548c2ecf20Sopenharmony_cifail_req:
92558c2ecf20Sopenharmony_ci	qla25xx_delete_rsp_que(vha, qpair->rsp);
92568c2ecf20Sopenharmony_cifail_rsp:
92578c2ecf20Sopenharmony_ci	mutex_lock(&ha->mq_lock);
92588c2ecf20Sopenharmony_ci	qpair->msix->in_use = 0;
92598c2ecf20Sopenharmony_ci	list_del(&qpair->qp_list_elem);
92608c2ecf20Sopenharmony_ci	if (list_empty(&vha->qp_list))
92618c2ecf20Sopenharmony_ci		vha->flags.qpairs_available = 0;
92628c2ecf20Sopenharmony_cifail_msix:
92638c2ecf20Sopenharmony_ci	ha->queue_pair_map[qpair_id] = NULL;
92648c2ecf20Sopenharmony_ci	clear_bit(qpair_id, ha->qpair_qid_map);
92658c2ecf20Sopenharmony_ci	ha->num_qpairs--;
92668c2ecf20Sopenharmony_ci	mutex_unlock(&ha->mq_lock);
92678c2ecf20Sopenharmony_cifail_qid_map:
92688c2ecf20Sopenharmony_ci	kfree(qpair);
92698c2ecf20Sopenharmony_ci	return NULL;
92708c2ecf20Sopenharmony_ci}
92718c2ecf20Sopenharmony_ci
92728c2ecf20Sopenharmony_ciint qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
92738c2ecf20Sopenharmony_ci{
92748c2ecf20Sopenharmony_ci	int ret = QLA_FUNCTION_FAILED;
92758c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = qpair->hw;
92768c2ecf20Sopenharmony_ci
92778c2ecf20Sopenharmony_ci	qpair->delete_in_progress = 1;
92788c2ecf20Sopenharmony_ci
92798c2ecf20Sopenharmony_ci	ret = qla25xx_delete_req_que(vha, qpair->req);
92808c2ecf20Sopenharmony_ci	if (ret != QLA_SUCCESS)
92818c2ecf20Sopenharmony_ci		goto fail;
92828c2ecf20Sopenharmony_ci
92838c2ecf20Sopenharmony_ci	ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
92848c2ecf20Sopenharmony_ci	if (ret != QLA_SUCCESS)
92858c2ecf20Sopenharmony_ci		goto fail;
92868c2ecf20Sopenharmony_ci
92878c2ecf20Sopenharmony_ci	mutex_lock(&ha->mq_lock);
92888c2ecf20Sopenharmony_ci	ha->queue_pair_map[qpair->id] = NULL;
92898c2ecf20Sopenharmony_ci	clear_bit(qpair->id, ha->qpair_qid_map);
92908c2ecf20Sopenharmony_ci	ha->num_qpairs--;
92918c2ecf20Sopenharmony_ci	list_del(&qpair->qp_list_elem);
92928c2ecf20Sopenharmony_ci	if (list_empty(&vha->qp_list)) {
92938c2ecf20Sopenharmony_ci		vha->flags.qpairs_available = 0;
92948c2ecf20Sopenharmony_ci		vha->flags.qpairs_req_created = 0;
92958c2ecf20Sopenharmony_ci		vha->flags.qpairs_rsp_created = 0;
92968c2ecf20Sopenharmony_ci	}
92978c2ecf20Sopenharmony_ci	mempool_destroy(qpair->srb_mempool);
92988c2ecf20Sopenharmony_ci	kfree(qpair);
92998c2ecf20Sopenharmony_ci	mutex_unlock(&ha->mq_lock);
93008c2ecf20Sopenharmony_ci
93018c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
93028c2ecf20Sopenharmony_cifail:
93038c2ecf20Sopenharmony_ci	return ret;
93048c2ecf20Sopenharmony_ci}
9305