18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) IBM Corporation, 2008
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
128c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
138c2ecf20Sopenharmony_ci#include <linux/dmapool.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
168c2ecf20Sopenharmony_ci#include <linux/kthread.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/of.h>
198c2ecf20Sopenharmony_ci#include <linux/pm.h>
208c2ecf20Sopenharmony_ci#include <linux/stringify.h>
218c2ecf20Sopenharmony_ci#include <linux/bsg-lib.h>
228c2ecf20Sopenharmony_ci#include <asm/firmware.h>
238c2ecf20Sopenharmony_ci#include <asm/irq.h>
248c2ecf20Sopenharmony_ci#include <asm/vio.h>
258c2ecf20Sopenharmony_ci#include <scsi/scsi.h>
268c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
278c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
288c2ecf20Sopenharmony_ci#include <scsi/scsi_device.h>
298c2ecf20Sopenharmony_ci#include <scsi/scsi_tcq.h>
308c2ecf20Sopenharmony_ci#include <scsi/scsi_transport_fc.h>
318c2ecf20Sopenharmony_ci#include <scsi/scsi_bsg_fc.h>
328c2ecf20Sopenharmony_ci#include "ibmvfc.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
358c2ecf20Sopenharmony_cistatic unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
368c2ecf20Sopenharmony_cistatic u64 max_lun = IBMVFC_MAX_LUN;
378c2ecf20Sopenharmony_cistatic unsigned int max_targets = IBMVFC_MAX_TARGETS;
388c2ecf20Sopenharmony_cistatic unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
398c2ecf20Sopenharmony_cistatic unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
408c2ecf20Sopenharmony_cistatic unsigned int ibmvfc_debug = IBMVFC_DEBUG;
418c2ecf20Sopenharmony_cistatic unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
428c2ecf20Sopenharmony_cistatic unsigned int cls3_error = IBMVFC_CLS3_ERROR;
438c2ecf20Sopenharmony_cistatic LIST_HEAD(ibmvfc_head);
448c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(ibmvfc_driver_lock);
458c2ecf20Sopenharmony_cistatic struct scsi_transport_template *ibmvfc_transport_template;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
488c2ecf20Sopenharmony_ciMODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
498c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
508c2ecf20Sopenharmony_ciMODULE_VERSION(IBMVFC_DRIVER_VERSION);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cimodule_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
538c2ecf20Sopenharmony_ciMODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
548c2ecf20Sopenharmony_ci		 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
558c2ecf20Sopenharmony_cimodule_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
568c2ecf20Sopenharmony_ciMODULE_PARM_DESC(default_timeout,
578c2ecf20Sopenharmony_ci		 "Default timeout in seconds for initialization and EH commands. "
588c2ecf20Sopenharmony_ci		 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
598c2ecf20Sopenharmony_cimodule_param_named(max_requests, max_requests, uint, S_IRUGO);
608c2ecf20Sopenharmony_ciMODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
618c2ecf20Sopenharmony_ci		 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
628c2ecf20Sopenharmony_cimodule_param_named(max_lun, max_lun, ullong, S_IRUGO);
638c2ecf20Sopenharmony_ciMODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
648c2ecf20Sopenharmony_ci		 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
658c2ecf20Sopenharmony_cimodule_param_named(max_targets, max_targets, uint, S_IRUGO);
668c2ecf20Sopenharmony_ciMODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
678c2ecf20Sopenharmony_ci		 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
688c2ecf20Sopenharmony_cimodule_param_named(disc_threads, disc_threads, uint, S_IRUGO);
698c2ecf20Sopenharmony_ciMODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
708c2ecf20Sopenharmony_ci		 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
718c2ecf20Sopenharmony_cimodule_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
728c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Enable driver debug information. "
738c2ecf20Sopenharmony_ci		 "[Default=" __stringify(IBMVFC_DEBUG) "]");
748c2ecf20Sopenharmony_cimodule_param_named(log_level, log_level, uint, 0);
758c2ecf20Sopenharmony_ciMODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
768c2ecf20Sopenharmony_ci		 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
778c2ecf20Sopenharmony_cimodule_param_named(cls3_error, cls3_error, uint, 0);
788c2ecf20Sopenharmony_ciMODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
798c2ecf20Sopenharmony_ci		 "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic const struct {
828c2ecf20Sopenharmony_ci	u16 status;
838c2ecf20Sopenharmony_ci	u16 error;
848c2ecf20Sopenharmony_ci	u8 result;
858c2ecf20Sopenharmony_ci	u8 retry;
868c2ecf20Sopenharmony_ci	int log;
878c2ecf20Sopenharmony_ci	char *name;
888c2ecf20Sopenharmony_ci} cmd_status [] = {
898c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
908c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
918c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
928c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
938c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
948c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
958c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
968c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
978c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
988c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
998c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
1008c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
1018c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
1028c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
1058c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
1068c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
1078c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
1088c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
1098c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
1108c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
1118c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
1128c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
1138c2ecf20Sopenharmony_ci	{ IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
1168c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
1178c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
1188c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
1198c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
1208c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
1218c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
1228c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
1238c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
1248c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
1258c2ecf20Sopenharmony_ci	{ IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	{ IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
1288c2ecf20Sopenharmony_ci	{ IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." },
1298c2ecf20Sopenharmony_ci};
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic void ibmvfc_npiv_login(struct ibmvfc_host *);
1328c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
1338c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
1348c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_query_target(struct ibmvfc_target *);
1358c2ecf20Sopenharmony_cistatic void ibmvfc_npiv_logout(struct ibmvfc_host *);
1368c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *);
1378c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_move_login(struct ibmvfc_target *);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic const char *unknown_error = "unknown error";
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#ifdef CONFIG_SCSI_IBMVFC_TRACE
1428c2ecf20Sopenharmony_ci/**
1438c2ecf20Sopenharmony_ci * ibmvfc_trc_start - Log a start trace entry
1448c2ecf20Sopenharmony_ci * @evt:		ibmvfc event struct
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci **/
1478c2ecf20Sopenharmony_cistatic void ibmvfc_trc_start(struct ibmvfc_event *evt)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
1508c2ecf20Sopenharmony_ci	struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
1518c2ecf20Sopenharmony_ci	struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
1528c2ecf20Sopenharmony_ci	struct ibmvfc_trace_entry *entry;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	entry = &vhost->trace[vhost->trace_index++];
1558c2ecf20Sopenharmony_ci	entry->evt = evt;
1568c2ecf20Sopenharmony_ci	entry->time = jiffies;
1578c2ecf20Sopenharmony_ci	entry->fmt = evt->crq.format;
1588c2ecf20Sopenharmony_ci	entry->type = IBMVFC_TRC_START;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	switch (entry->fmt) {
1618c2ecf20Sopenharmony_ci	case IBMVFC_CMD_FORMAT:
1628c2ecf20Sopenharmony_ci		entry->op_code = vfc_cmd->iu.cdb[0];
1638c2ecf20Sopenharmony_ci		entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
1648c2ecf20Sopenharmony_ci		entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
1658c2ecf20Sopenharmony_ci		entry->tmf_flags = vfc_cmd->iu.tmf_flags;
1668c2ecf20Sopenharmony_ci		entry->u.start.xfer_len = be32_to_cpu(vfc_cmd->iu.xfer_len);
1678c2ecf20Sopenharmony_ci		break;
1688c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FORMAT:
1698c2ecf20Sopenharmony_ci		entry->op_code = be32_to_cpu(mad->opcode);
1708c2ecf20Sopenharmony_ci		break;
1718c2ecf20Sopenharmony_ci	default:
1728c2ecf20Sopenharmony_ci		break;
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/**
1778c2ecf20Sopenharmony_ci * ibmvfc_trc_end - Log an end trace entry
1788c2ecf20Sopenharmony_ci * @evt:		ibmvfc event struct
1798c2ecf20Sopenharmony_ci *
1808c2ecf20Sopenharmony_ci **/
1818c2ecf20Sopenharmony_cistatic void ibmvfc_trc_end(struct ibmvfc_event *evt)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
1848c2ecf20Sopenharmony_ci	struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1858c2ecf20Sopenharmony_ci	struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
1868c2ecf20Sopenharmony_ci	struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	entry->evt = evt;
1898c2ecf20Sopenharmony_ci	entry->time = jiffies;
1908c2ecf20Sopenharmony_ci	entry->fmt = evt->crq.format;
1918c2ecf20Sopenharmony_ci	entry->type = IBMVFC_TRC_END;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	switch (entry->fmt) {
1948c2ecf20Sopenharmony_ci	case IBMVFC_CMD_FORMAT:
1958c2ecf20Sopenharmony_ci		entry->op_code = vfc_cmd->iu.cdb[0];
1968c2ecf20Sopenharmony_ci		entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
1978c2ecf20Sopenharmony_ci		entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
1988c2ecf20Sopenharmony_ci		entry->tmf_flags = vfc_cmd->iu.tmf_flags;
1998c2ecf20Sopenharmony_ci		entry->u.end.status = be16_to_cpu(vfc_cmd->status);
2008c2ecf20Sopenharmony_ci		entry->u.end.error = be16_to_cpu(vfc_cmd->error);
2018c2ecf20Sopenharmony_ci		entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
2028c2ecf20Sopenharmony_ci		entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
2038c2ecf20Sopenharmony_ci		entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
2048c2ecf20Sopenharmony_ci		break;
2058c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FORMAT:
2068c2ecf20Sopenharmony_ci		entry->op_code = be32_to_cpu(mad->opcode);
2078c2ecf20Sopenharmony_ci		entry->u.end.status = be16_to_cpu(mad->status);
2088c2ecf20Sopenharmony_ci		break;
2098c2ecf20Sopenharmony_ci	default:
2108c2ecf20Sopenharmony_ci		break;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci#else
2168c2ecf20Sopenharmony_ci#define ibmvfc_trc_start(evt) do { } while (0)
2178c2ecf20Sopenharmony_ci#define ibmvfc_trc_end(evt) do { } while (0)
2188c2ecf20Sopenharmony_ci#endif
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/**
2218c2ecf20Sopenharmony_ci * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
2228c2ecf20Sopenharmony_ci * @status:		status / error class
2238c2ecf20Sopenharmony_ci * @error:		error
2248c2ecf20Sopenharmony_ci *
2258c2ecf20Sopenharmony_ci * Return value:
2268c2ecf20Sopenharmony_ci *	index into cmd_status / -EINVAL on failure
2278c2ecf20Sopenharmony_ci **/
2288c2ecf20Sopenharmony_cistatic int ibmvfc_get_err_index(u16 status, u16 error)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	int i;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
2338c2ecf20Sopenharmony_ci		if ((cmd_status[i].status & status) == cmd_status[i].status &&
2348c2ecf20Sopenharmony_ci		    cmd_status[i].error == error)
2358c2ecf20Sopenharmony_ci			return i;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	return -EINVAL;
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci/**
2418c2ecf20Sopenharmony_ci * ibmvfc_get_cmd_error - Find the error description for the fcp response
2428c2ecf20Sopenharmony_ci * @status:		status / error class
2438c2ecf20Sopenharmony_ci * @error:		error
2448c2ecf20Sopenharmony_ci *
2458c2ecf20Sopenharmony_ci * Return value:
2468c2ecf20Sopenharmony_ci *	error description string
2478c2ecf20Sopenharmony_ci **/
2488c2ecf20Sopenharmony_cistatic const char *ibmvfc_get_cmd_error(u16 status, u16 error)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	int rc = ibmvfc_get_err_index(status, error);
2518c2ecf20Sopenharmony_ci	if (rc >= 0)
2528c2ecf20Sopenharmony_ci		return cmd_status[rc].name;
2538c2ecf20Sopenharmony_ci	return unknown_error;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/**
2578c2ecf20Sopenharmony_ci * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
2588c2ecf20Sopenharmony_ci * @vfc_cmd:	ibmvfc command struct
2598c2ecf20Sopenharmony_ci *
2608c2ecf20Sopenharmony_ci * Return value:
2618c2ecf20Sopenharmony_ci *	SCSI result value to return for completed command
2628c2ecf20Sopenharmony_ci **/
2638c2ecf20Sopenharmony_cistatic int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	int err;
2668c2ecf20Sopenharmony_ci	struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
2678c2ecf20Sopenharmony_ci	int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	if ((rsp->flags & FCP_RSP_LEN_VALID) &&
2708c2ecf20Sopenharmony_ci	    ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
2718c2ecf20Sopenharmony_ci	     rsp->data.info.rsp_code))
2728c2ecf20Sopenharmony_ci		return DID_ERROR << 16;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
2758c2ecf20Sopenharmony_ci	if (err >= 0)
2768c2ecf20Sopenharmony_ci		return rsp->scsi_status | (cmd_status[err].result << 16);
2778c2ecf20Sopenharmony_ci	return rsp->scsi_status | (DID_ERROR << 16);
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci/**
2818c2ecf20Sopenharmony_ci * ibmvfc_retry_cmd - Determine if error status is retryable
2828c2ecf20Sopenharmony_ci * @status:		status / error class
2838c2ecf20Sopenharmony_ci * @error:		error
2848c2ecf20Sopenharmony_ci *
2858c2ecf20Sopenharmony_ci * Return value:
2868c2ecf20Sopenharmony_ci *	1 if error should be retried / 0 if it should not
2878c2ecf20Sopenharmony_ci **/
2888c2ecf20Sopenharmony_cistatic int ibmvfc_retry_cmd(u16 status, u16 error)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	int rc = ibmvfc_get_err_index(status, error);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	if (rc >= 0)
2938c2ecf20Sopenharmony_ci		return cmd_status[rc].retry;
2948c2ecf20Sopenharmony_ci	return 1;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic const char *unknown_fc_explain = "unknown fc explain";
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic const struct {
3008c2ecf20Sopenharmony_ci	u16 fc_explain;
3018c2ecf20Sopenharmony_ci	char *name;
3028c2ecf20Sopenharmony_ci} ls_explain [] = {
3038c2ecf20Sopenharmony_ci	{ 0x00, "no additional explanation" },
3048c2ecf20Sopenharmony_ci	{ 0x01, "service parameter error - options" },
3058c2ecf20Sopenharmony_ci	{ 0x03, "service parameter error - initiator control" },
3068c2ecf20Sopenharmony_ci	{ 0x05, "service parameter error - recipient control" },
3078c2ecf20Sopenharmony_ci	{ 0x07, "service parameter error - received data field size" },
3088c2ecf20Sopenharmony_ci	{ 0x09, "service parameter error - concurrent seq" },
3098c2ecf20Sopenharmony_ci	{ 0x0B, "service parameter error - credit" },
3108c2ecf20Sopenharmony_ci	{ 0x0D, "invalid N_Port/F_Port_Name" },
3118c2ecf20Sopenharmony_ci	{ 0x0E, "invalid node/Fabric Name" },
3128c2ecf20Sopenharmony_ci	{ 0x0F, "invalid common service parameters" },
3138c2ecf20Sopenharmony_ci	{ 0x11, "invalid association header" },
3148c2ecf20Sopenharmony_ci	{ 0x13, "association header required" },
3158c2ecf20Sopenharmony_ci	{ 0x15, "invalid originator S_ID" },
3168c2ecf20Sopenharmony_ci	{ 0x17, "invalid OX_ID-RX-ID combination" },
3178c2ecf20Sopenharmony_ci	{ 0x19, "command (request) already in progress" },
3188c2ecf20Sopenharmony_ci	{ 0x1E, "N_Port Login requested" },
3198c2ecf20Sopenharmony_ci	{ 0x1F, "Invalid N_Port_ID" },
3208c2ecf20Sopenharmony_ci};
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic const struct {
3238c2ecf20Sopenharmony_ci	u16 fc_explain;
3248c2ecf20Sopenharmony_ci	char *name;
3258c2ecf20Sopenharmony_ci} gs_explain [] = {
3268c2ecf20Sopenharmony_ci	{ 0x00, "no additional explanation" },
3278c2ecf20Sopenharmony_ci	{ 0x01, "port identifier not registered" },
3288c2ecf20Sopenharmony_ci	{ 0x02, "port name not registered" },
3298c2ecf20Sopenharmony_ci	{ 0x03, "node name not registered" },
3308c2ecf20Sopenharmony_ci	{ 0x04, "class of service not registered" },
3318c2ecf20Sopenharmony_ci	{ 0x06, "initial process associator not registered" },
3328c2ecf20Sopenharmony_ci	{ 0x07, "FC-4 TYPEs not registered" },
3338c2ecf20Sopenharmony_ci	{ 0x08, "symbolic port name not registered" },
3348c2ecf20Sopenharmony_ci	{ 0x09, "symbolic node name not registered" },
3358c2ecf20Sopenharmony_ci	{ 0x0A, "port type not registered" },
3368c2ecf20Sopenharmony_ci	{ 0xF0, "authorization exception" },
3378c2ecf20Sopenharmony_ci	{ 0xF1, "authentication exception" },
3388c2ecf20Sopenharmony_ci	{ 0xF2, "data base full" },
3398c2ecf20Sopenharmony_ci	{ 0xF3, "data base empty" },
3408c2ecf20Sopenharmony_ci	{ 0xF4, "processing request" },
3418c2ecf20Sopenharmony_ci	{ 0xF5, "unable to verify connection" },
3428c2ecf20Sopenharmony_ci	{ 0xF6, "devices not in a common zone" },
3438c2ecf20Sopenharmony_ci};
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci/**
3468c2ecf20Sopenharmony_ci * ibmvfc_get_ls_explain - Return the FC Explain description text
3478c2ecf20Sopenharmony_ci * @status:	FC Explain status
3488c2ecf20Sopenharmony_ci *
3498c2ecf20Sopenharmony_ci * Returns:
3508c2ecf20Sopenharmony_ci *	error string
3518c2ecf20Sopenharmony_ci **/
3528c2ecf20Sopenharmony_cistatic const char *ibmvfc_get_ls_explain(u16 status)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	int i;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
3578c2ecf20Sopenharmony_ci		if (ls_explain[i].fc_explain == status)
3588c2ecf20Sopenharmony_ci			return ls_explain[i].name;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	return unknown_fc_explain;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci/**
3648c2ecf20Sopenharmony_ci * ibmvfc_get_gs_explain - Return the FC Explain description text
3658c2ecf20Sopenharmony_ci * @status:	FC Explain status
3668c2ecf20Sopenharmony_ci *
3678c2ecf20Sopenharmony_ci * Returns:
3688c2ecf20Sopenharmony_ci *	error string
3698c2ecf20Sopenharmony_ci **/
3708c2ecf20Sopenharmony_cistatic const char *ibmvfc_get_gs_explain(u16 status)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	int i;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
3758c2ecf20Sopenharmony_ci		if (gs_explain[i].fc_explain == status)
3768c2ecf20Sopenharmony_ci			return gs_explain[i].name;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	return unknown_fc_explain;
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic const struct {
3828c2ecf20Sopenharmony_ci	enum ibmvfc_fc_type fc_type;
3838c2ecf20Sopenharmony_ci	char *name;
3848c2ecf20Sopenharmony_ci} fc_type [] = {
3858c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_REJECT, "fabric reject" },
3868c2ecf20Sopenharmony_ci	{ IBMVFC_PORT_REJECT, "port reject" },
3878c2ecf20Sopenharmony_ci	{ IBMVFC_LS_REJECT, "ELS reject" },
3888c2ecf20Sopenharmony_ci	{ IBMVFC_FABRIC_BUSY, "fabric busy" },
3898c2ecf20Sopenharmony_ci	{ IBMVFC_PORT_BUSY, "port busy" },
3908c2ecf20Sopenharmony_ci	{ IBMVFC_BASIC_REJECT, "basic reject" },
3918c2ecf20Sopenharmony_ci};
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_cistatic const char *unknown_fc_type = "unknown fc type";
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci/**
3968c2ecf20Sopenharmony_ci * ibmvfc_get_fc_type - Return the FC Type description text
3978c2ecf20Sopenharmony_ci * @status:	FC Type error status
3988c2ecf20Sopenharmony_ci *
3998c2ecf20Sopenharmony_ci * Returns:
4008c2ecf20Sopenharmony_ci *	error string
4018c2ecf20Sopenharmony_ci **/
4028c2ecf20Sopenharmony_cistatic const char *ibmvfc_get_fc_type(u16 status)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	int i;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(fc_type); i++)
4078c2ecf20Sopenharmony_ci		if (fc_type[i].fc_type == status)
4088c2ecf20Sopenharmony_ci			return fc_type[i].name;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	return unknown_fc_type;
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci/**
4148c2ecf20Sopenharmony_ci * ibmvfc_set_tgt_action - Set the next init action for the target
4158c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
4168c2ecf20Sopenharmony_ci * @action:		action to perform
4178c2ecf20Sopenharmony_ci *
4188c2ecf20Sopenharmony_ci * Returns:
4198c2ecf20Sopenharmony_ci *	0 if action changed / non-zero if not changed
4208c2ecf20Sopenharmony_ci **/
4218c2ecf20Sopenharmony_cistatic int ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
4228c2ecf20Sopenharmony_ci				  enum ibmvfc_target_action action)
4238c2ecf20Sopenharmony_ci{
4248c2ecf20Sopenharmony_ci	int rc = -EINVAL;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	switch (tgt->action) {
4278c2ecf20Sopenharmony_ci	case IBMVFC_TGT_ACTION_LOGOUT_RPORT:
4288c2ecf20Sopenharmony_ci		if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT ||
4298c2ecf20Sopenharmony_ci		    action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4308c2ecf20Sopenharmony_ci			tgt->action = action;
4318c2ecf20Sopenharmony_ci			rc = 0;
4328c2ecf20Sopenharmony_ci		}
4338c2ecf20Sopenharmony_ci		break;
4348c2ecf20Sopenharmony_ci	case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT:
4358c2ecf20Sopenharmony_ci		if (action == IBMVFC_TGT_ACTION_DEL_RPORT ||
4368c2ecf20Sopenharmony_ci		    action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
4378c2ecf20Sopenharmony_ci			tgt->action = action;
4388c2ecf20Sopenharmony_ci			rc = 0;
4398c2ecf20Sopenharmony_ci		}
4408c2ecf20Sopenharmony_ci		break;
4418c2ecf20Sopenharmony_ci	case IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT:
4428c2ecf20Sopenharmony_ci		if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
4438c2ecf20Sopenharmony_ci			tgt->action = action;
4448c2ecf20Sopenharmony_ci			rc = 0;
4458c2ecf20Sopenharmony_ci		}
4468c2ecf20Sopenharmony_ci		break;
4478c2ecf20Sopenharmony_ci	case IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT:
4488c2ecf20Sopenharmony_ci		if (action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4498c2ecf20Sopenharmony_ci			tgt->action = action;
4508c2ecf20Sopenharmony_ci			rc = 0;
4518c2ecf20Sopenharmony_ci		}
4528c2ecf20Sopenharmony_ci		break;
4538c2ecf20Sopenharmony_ci	case IBMVFC_TGT_ACTION_DEL_RPORT:
4548c2ecf20Sopenharmony_ci		if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4558c2ecf20Sopenharmony_ci			tgt->action = action;
4568c2ecf20Sopenharmony_ci			rc = 0;
4578c2ecf20Sopenharmony_ci		}
4588c2ecf20Sopenharmony_ci		break;
4598c2ecf20Sopenharmony_ci	case IBMVFC_TGT_ACTION_DELETED_RPORT:
4608c2ecf20Sopenharmony_ci		break;
4618c2ecf20Sopenharmony_ci	default:
4628c2ecf20Sopenharmony_ci		tgt->action = action;
4638c2ecf20Sopenharmony_ci		rc = 0;
4648c2ecf20Sopenharmony_ci		break;
4658c2ecf20Sopenharmony_ci	}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	if (action >= IBMVFC_TGT_ACTION_LOGOUT_RPORT)
4688c2ecf20Sopenharmony_ci		tgt->add_rport = 0;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	return rc;
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci/**
4748c2ecf20Sopenharmony_ci * ibmvfc_set_host_state - Set the state for the host
4758c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
4768c2ecf20Sopenharmony_ci * @state:		state to set host to
4778c2ecf20Sopenharmony_ci *
4788c2ecf20Sopenharmony_ci * Returns:
4798c2ecf20Sopenharmony_ci *	0 if state changed / non-zero if not changed
4808c2ecf20Sopenharmony_ci **/
4818c2ecf20Sopenharmony_cistatic int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
4828c2ecf20Sopenharmony_ci				  enum ibmvfc_host_state state)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	int rc = 0;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	switch (vhost->state) {
4878c2ecf20Sopenharmony_ci	case IBMVFC_HOST_OFFLINE:
4888c2ecf20Sopenharmony_ci		rc = -EINVAL;
4898c2ecf20Sopenharmony_ci		break;
4908c2ecf20Sopenharmony_ci	default:
4918c2ecf20Sopenharmony_ci		vhost->state = state;
4928c2ecf20Sopenharmony_ci		break;
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	return rc;
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci/**
4998c2ecf20Sopenharmony_ci * ibmvfc_set_host_action - Set the next init action for the host
5008c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
5018c2ecf20Sopenharmony_ci * @action:		action to perform
5028c2ecf20Sopenharmony_ci *
5038c2ecf20Sopenharmony_ci **/
5048c2ecf20Sopenharmony_cistatic void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
5058c2ecf20Sopenharmony_ci				   enum ibmvfc_host_action action)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	switch (action) {
5088c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5098c2ecf20Sopenharmony_ci		if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
5108c2ecf20Sopenharmony_ci			vhost->action = action;
5118c2ecf20Sopenharmony_ci		break;
5128c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_LOGO_WAIT:
5138c2ecf20Sopenharmony_ci		if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
5148c2ecf20Sopenharmony_ci			vhost->action = action;
5158c2ecf20Sopenharmony_ci		break;
5168c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_INIT_WAIT:
5178c2ecf20Sopenharmony_ci		if (vhost->action == IBMVFC_HOST_ACTION_INIT)
5188c2ecf20Sopenharmony_ci			vhost->action = action;
5198c2ecf20Sopenharmony_ci		break;
5208c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_QUERY:
5218c2ecf20Sopenharmony_ci		switch (vhost->action) {
5228c2ecf20Sopenharmony_ci		case IBMVFC_HOST_ACTION_INIT_WAIT:
5238c2ecf20Sopenharmony_ci		case IBMVFC_HOST_ACTION_NONE:
5248c2ecf20Sopenharmony_ci		case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5258c2ecf20Sopenharmony_ci			vhost->action = action;
5268c2ecf20Sopenharmony_ci			break;
5278c2ecf20Sopenharmony_ci		default:
5288c2ecf20Sopenharmony_ci			break;
5298c2ecf20Sopenharmony_ci		}
5308c2ecf20Sopenharmony_ci		break;
5318c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_TGT_INIT:
5328c2ecf20Sopenharmony_ci		if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
5338c2ecf20Sopenharmony_ci			vhost->action = action;
5348c2ecf20Sopenharmony_ci		break;
5358c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_REENABLE:
5368c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_RESET:
5378c2ecf20Sopenharmony_ci		vhost->action = action;
5388c2ecf20Sopenharmony_ci		break;
5398c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_INIT:
5408c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_TGT_DEL:
5418c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_LOGO:
5428c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_QUERY_TGTS:
5438c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5448c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_NONE:
5458c2ecf20Sopenharmony_ci	default:
5468c2ecf20Sopenharmony_ci		switch (vhost->action) {
5478c2ecf20Sopenharmony_ci		case IBMVFC_HOST_ACTION_RESET:
5488c2ecf20Sopenharmony_ci		case IBMVFC_HOST_ACTION_REENABLE:
5498c2ecf20Sopenharmony_ci			break;
5508c2ecf20Sopenharmony_ci		default:
5518c2ecf20Sopenharmony_ci			vhost->action = action;
5528c2ecf20Sopenharmony_ci			break;
5538c2ecf20Sopenharmony_ci		}
5548c2ecf20Sopenharmony_ci		break;
5558c2ecf20Sopenharmony_ci	}
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci/**
5598c2ecf20Sopenharmony_ci * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
5608c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
5618c2ecf20Sopenharmony_ci *
5628c2ecf20Sopenharmony_ci * Return value:
5638c2ecf20Sopenharmony_ci *	nothing
5648c2ecf20Sopenharmony_ci **/
5658c2ecf20Sopenharmony_cistatic void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	if (vhost->action == IBMVFC_HOST_ACTION_NONE &&
5688c2ecf20Sopenharmony_ci	    vhost->state == IBMVFC_ACTIVE) {
5698c2ecf20Sopenharmony_ci		if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
5708c2ecf20Sopenharmony_ci			scsi_block_requests(vhost->host);
5718c2ecf20Sopenharmony_ci			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5728c2ecf20Sopenharmony_ci		}
5738c2ecf20Sopenharmony_ci	} else
5748c2ecf20Sopenharmony_ci		vhost->reinit = 1;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
5778c2ecf20Sopenharmony_ci}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci/**
5808c2ecf20Sopenharmony_ci * ibmvfc_del_tgt - Schedule cleanup and removal of the target
5818c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
5828c2ecf20Sopenharmony_ci * @job_step:	job step to perform
5838c2ecf20Sopenharmony_ci *
5848c2ecf20Sopenharmony_ci **/
5858c2ecf20Sopenharmony_cistatic void ibmvfc_del_tgt(struct ibmvfc_target *tgt)
5868c2ecf20Sopenharmony_ci{
5878c2ecf20Sopenharmony_ci	if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT))
5888c2ecf20Sopenharmony_ci		tgt->job_step = ibmvfc_tgt_implicit_logout_and_del;
5898c2ecf20Sopenharmony_ci	wake_up(&tgt->vhost->work_wait_q);
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci/**
5938c2ecf20Sopenharmony_ci * ibmvfc_link_down - Handle a link down event from the adapter
5948c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
5958c2ecf20Sopenharmony_ci * @state:	ibmvfc host state to enter
5968c2ecf20Sopenharmony_ci *
5978c2ecf20Sopenharmony_ci **/
5988c2ecf20Sopenharmony_cistatic void ibmvfc_link_down(struct ibmvfc_host *vhost,
5998c2ecf20Sopenharmony_ci			     enum ibmvfc_host_state state)
6008c2ecf20Sopenharmony_ci{
6018c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	ENTER;
6048c2ecf20Sopenharmony_ci	scsi_block_requests(vhost->host);
6058c2ecf20Sopenharmony_ci	list_for_each_entry(tgt, &vhost->targets, queue)
6068c2ecf20Sopenharmony_ci		ibmvfc_del_tgt(tgt);
6078c2ecf20Sopenharmony_ci	ibmvfc_set_host_state(vhost, state);
6088c2ecf20Sopenharmony_ci	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
6098c2ecf20Sopenharmony_ci	vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
6108c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
6118c2ecf20Sopenharmony_ci	LEAVE;
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci/**
6158c2ecf20Sopenharmony_ci * ibmvfc_init_host - Start host initialization
6168c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
6178c2ecf20Sopenharmony_ci *
6188c2ecf20Sopenharmony_ci * Return value:
6198c2ecf20Sopenharmony_ci *	nothing
6208c2ecf20Sopenharmony_ci **/
6218c2ecf20Sopenharmony_cistatic void ibmvfc_init_host(struct ibmvfc_host *vhost)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
6268c2ecf20Sopenharmony_ci		if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
6278c2ecf20Sopenharmony_ci			dev_err(vhost->dev,
6288c2ecf20Sopenharmony_ci				"Host initialization retries exceeded. Taking adapter offline\n");
6298c2ecf20Sopenharmony_ci			ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
6308c2ecf20Sopenharmony_ci			return;
6318c2ecf20Sopenharmony_ci		}
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
6358c2ecf20Sopenharmony_ci		memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
6368c2ecf20Sopenharmony_ci		vhost->async_crq.cur = 0;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue) {
6398c2ecf20Sopenharmony_ci			if (vhost->client_migrated)
6408c2ecf20Sopenharmony_ci				tgt->need_login = 1;
6418c2ecf20Sopenharmony_ci			else
6428c2ecf20Sopenharmony_ci				ibmvfc_del_tgt(tgt);
6438c2ecf20Sopenharmony_ci		}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci		scsi_block_requests(vhost->host);
6468c2ecf20Sopenharmony_ci		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
6478c2ecf20Sopenharmony_ci		vhost->job_step = ibmvfc_npiv_login;
6488c2ecf20Sopenharmony_ci		wake_up(&vhost->work_wait_q);
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci/**
6538c2ecf20Sopenharmony_ci * ibmvfc_send_crq - Send a CRQ
6548c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
6558c2ecf20Sopenharmony_ci * @word1:	the first 64 bits of the data
6568c2ecf20Sopenharmony_ci * @word2:	the second 64 bits of the data
6578c2ecf20Sopenharmony_ci *
6588c2ecf20Sopenharmony_ci * Return value:
6598c2ecf20Sopenharmony_ci *	0 on success / other on failure
6608c2ecf20Sopenharmony_ci **/
6618c2ecf20Sopenharmony_cistatic int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	struct vio_dev *vdev = to_vio_dev(vhost->dev);
6648c2ecf20Sopenharmony_ci	return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci/**
6688c2ecf20Sopenharmony_ci * ibmvfc_send_crq_init - Send a CRQ init message
6698c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
6708c2ecf20Sopenharmony_ci *
6718c2ecf20Sopenharmony_ci * Return value:
6728c2ecf20Sopenharmony_ci *	0 on success / other on failure
6738c2ecf20Sopenharmony_ci **/
6748c2ecf20Sopenharmony_cistatic int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	ibmvfc_dbg(vhost, "Sending CRQ init\n");
6778c2ecf20Sopenharmony_ci	return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
6788c2ecf20Sopenharmony_ci}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci/**
6818c2ecf20Sopenharmony_ci * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
6828c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
6838c2ecf20Sopenharmony_ci *
6848c2ecf20Sopenharmony_ci * Return value:
6858c2ecf20Sopenharmony_ci *	0 on success / other on failure
6868c2ecf20Sopenharmony_ci **/
6878c2ecf20Sopenharmony_cistatic int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
6908c2ecf20Sopenharmony_ci	return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci/**
6948c2ecf20Sopenharmony_ci * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
6958c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
6968c2ecf20Sopenharmony_ci *
6978c2ecf20Sopenharmony_ci * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
6988c2ecf20Sopenharmony_ci * the crq with the hypervisor.
6998c2ecf20Sopenharmony_ci **/
7008c2ecf20Sopenharmony_cistatic void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
7018c2ecf20Sopenharmony_ci{
7028c2ecf20Sopenharmony_ci	long rc = 0;
7038c2ecf20Sopenharmony_ci	struct vio_dev *vdev = to_vio_dev(vhost->dev);
7048c2ecf20Sopenharmony_ci	struct ibmvfc_crq_queue *crq = &vhost->crq;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	ibmvfc_dbg(vhost, "Releasing CRQ\n");
7078c2ecf20Sopenharmony_ci	free_irq(vdev->irq, vhost);
7088c2ecf20Sopenharmony_ci	tasklet_kill(&vhost->tasklet);
7098c2ecf20Sopenharmony_ci	do {
7108c2ecf20Sopenharmony_ci		if (rc)
7118c2ecf20Sopenharmony_ci			msleep(100);
7128c2ecf20Sopenharmony_ci		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
7138c2ecf20Sopenharmony_ci	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	vhost->state = IBMVFC_NO_CRQ;
7168c2ecf20Sopenharmony_ci	vhost->logged_in = 0;
7178c2ecf20Sopenharmony_ci	dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
7188c2ecf20Sopenharmony_ci	free_page((unsigned long)crq->msgs);
7198c2ecf20Sopenharmony_ci}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci/**
7228c2ecf20Sopenharmony_ci * ibmvfc_reenable_crq_queue - reenables the CRQ
7238c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
7248c2ecf20Sopenharmony_ci *
7258c2ecf20Sopenharmony_ci * Return value:
7268c2ecf20Sopenharmony_ci *	0 on success / other on failure
7278c2ecf20Sopenharmony_ci **/
7288c2ecf20Sopenharmony_cistatic int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
7298c2ecf20Sopenharmony_ci{
7308c2ecf20Sopenharmony_ci	int rc = 0;
7318c2ecf20Sopenharmony_ci	struct vio_dev *vdev = to_vio_dev(vhost->dev);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	/* Re-enable the CRQ */
7348c2ecf20Sopenharmony_ci	do {
7358c2ecf20Sopenharmony_ci		if (rc)
7368c2ecf20Sopenharmony_ci			msleep(100);
7378c2ecf20Sopenharmony_ci		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
7388c2ecf20Sopenharmony_ci	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	if (rc)
7418c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	return rc;
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci/**
7478c2ecf20Sopenharmony_ci * ibmvfc_reset_crq - resets a crq after a failure
7488c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
7498c2ecf20Sopenharmony_ci *
7508c2ecf20Sopenharmony_ci * Return value:
7518c2ecf20Sopenharmony_ci *	0 on success / other on failure
7528c2ecf20Sopenharmony_ci **/
7538c2ecf20Sopenharmony_cistatic int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
7548c2ecf20Sopenharmony_ci{
7558c2ecf20Sopenharmony_ci	int rc = 0;
7568c2ecf20Sopenharmony_ci	unsigned long flags;
7578c2ecf20Sopenharmony_ci	struct vio_dev *vdev = to_vio_dev(vhost->dev);
7588c2ecf20Sopenharmony_ci	struct ibmvfc_crq_queue *crq = &vhost->crq;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	/* Close the CRQ */
7618c2ecf20Sopenharmony_ci	do {
7628c2ecf20Sopenharmony_ci		if (rc)
7638c2ecf20Sopenharmony_ci			msleep(100);
7648c2ecf20Sopenharmony_ci		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
7658c2ecf20Sopenharmony_ci	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
7688c2ecf20Sopenharmony_ci	vhost->state = IBMVFC_NO_CRQ;
7698c2ecf20Sopenharmony_ci	vhost->logged_in = 0;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	/* Clean out the queue */
7728c2ecf20Sopenharmony_ci	memset(crq->msgs, 0, PAGE_SIZE);
7738c2ecf20Sopenharmony_ci	crq->cur = 0;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	/* And re-open it again */
7768c2ecf20Sopenharmony_ci	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
7778c2ecf20Sopenharmony_ci				crq->msg_token, PAGE_SIZE);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	if (rc == H_CLOSED)
7808c2ecf20Sopenharmony_ci		/* Adapter is good, but other end is not ready */
7818c2ecf20Sopenharmony_ci		dev_warn(vhost->dev, "Partner adapter not ready\n");
7828c2ecf20Sopenharmony_ci	else if (rc != 0)
7838c2ecf20Sopenharmony_ci		dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
7848c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	return rc;
7878c2ecf20Sopenharmony_ci}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci/**
7908c2ecf20Sopenharmony_ci * ibmvfc_valid_event - Determines if event is valid.
7918c2ecf20Sopenharmony_ci * @pool:	event_pool that contains the event
7928c2ecf20Sopenharmony_ci * @evt:	ibmvfc event to be checked for validity
7938c2ecf20Sopenharmony_ci *
7948c2ecf20Sopenharmony_ci * Return value:
7958c2ecf20Sopenharmony_ci *	1 if event is valid / 0 if event is not valid
7968c2ecf20Sopenharmony_ci **/
7978c2ecf20Sopenharmony_cistatic int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
7988c2ecf20Sopenharmony_ci			      struct ibmvfc_event *evt)
7998c2ecf20Sopenharmony_ci{
8008c2ecf20Sopenharmony_ci	int index = evt - pool->events;
8018c2ecf20Sopenharmony_ci	if (index < 0 || index >= pool->size)	/* outside of bounds */
8028c2ecf20Sopenharmony_ci		return 0;
8038c2ecf20Sopenharmony_ci	if (evt != pool->events + index)	/* unaligned */
8048c2ecf20Sopenharmony_ci		return 0;
8058c2ecf20Sopenharmony_ci	return 1;
8068c2ecf20Sopenharmony_ci}
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci/**
8098c2ecf20Sopenharmony_ci * ibmvfc_free_event - Free the specified event
8108c2ecf20Sopenharmony_ci * @evt:	ibmvfc_event to be freed
8118c2ecf20Sopenharmony_ci *
8128c2ecf20Sopenharmony_ci **/
8138c2ecf20Sopenharmony_cistatic void ibmvfc_free_event(struct ibmvfc_event *evt)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
8168c2ecf20Sopenharmony_ci	struct ibmvfc_event_pool *pool = &vhost->pool;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	BUG_ON(!ibmvfc_valid_event(pool, evt));
8198c2ecf20Sopenharmony_ci	BUG_ON(atomic_inc_return(&evt->free) != 1);
8208c2ecf20Sopenharmony_ci	list_add_tail(&evt->queue, &vhost->free);
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci/**
8248c2ecf20Sopenharmony_ci * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
8258c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
8268c2ecf20Sopenharmony_ci *
8278c2ecf20Sopenharmony_ci * This function does not setup any error status, that must be done
8288c2ecf20Sopenharmony_ci * before this function gets called.
8298c2ecf20Sopenharmony_ci **/
8308c2ecf20Sopenharmony_cistatic void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
8318c2ecf20Sopenharmony_ci{
8328c2ecf20Sopenharmony_ci	struct scsi_cmnd *cmnd = evt->cmnd;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	if (cmnd) {
8358c2ecf20Sopenharmony_ci		scsi_dma_unmap(cmnd);
8368c2ecf20Sopenharmony_ci		cmnd->scsi_done(cmnd);
8378c2ecf20Sopenharmony_ci	}
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	if (evt->eh_comp)
8408c2ecf20Sopenharmony_ci		complete(evt->eh_comp);
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci/**
8468c2ecf20Sopenharmony_ci * ibmvfc_fail_request - Fail request with specified error code
8478c2ecf20Sopenharmony_ci * @evt:		ibmvfc event struct
8488c2ecf20Sopenharmony_ci * @error_code:	error code to fail request with
8498c2ecf20Sopenharmony_ci *
8508c2ecf20Sopenharmony_ci * Return value:
8518c2ecf20Sopenharmony_ci *	none
8528c2ecf20Sopenharmony_ci **/
8538c2ecf20Sopenharmony_cistatic void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
8548c2ecf20Sopenharmony_ci{
8558c2ecf20Sopenharmony_ci	if (evt->cmnd) {
8568c2ecf20Sopenharmony_ci		evt->cmnd->result = (error_code << 16);
8578c2ecf20Sopenharmony_ci		evt->done = ibmvfc_scsi_eh_done;
8588c2ecf20Sopenharmony_ci	} else
8598c2ecf20Sopenharmony_ci		evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	list_del(&evt->queue);
8628c2ecf20Sopenharmony_ci	del_timer(&evt->timer);
8638c2ecf20Sopenharmony_ci	ibmvfc_trc_end(evt);
8648c2ecf20Sopenharmony_ci	evt->done(evt);
8658c2ecf20Sopenharmony_ci}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci/**
8688c2ecf20Sopenharmony_ci * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
8698c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
8708c2ecf20Sopenharmony_ci * @error_code:	error code to fail requests with
8718c2ecf20Sopenharmony_ci *
8728c2ecf20Sopenharmony_ci * Return value:
8738c2ecf20Sopenharmony_ci *	none
8748c2ecf20Sopenharmony_ci **/
8758c2ecf20Sopenharmony_cistatic void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
8768c2ecf20Sopenharmony_ci{
8778c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt, *pos;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	ibmvfc_dbg(vhost, "Purging all requests\n");
8808c2ecf20Sopenharmony_ci	list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
8818c2ecf20Sopenharmony_ci		ibmvfc_fail_request(evt, error_code);
8828c2ecf20Sopenharmony_ci}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci/**
8858c2ecf20Sopenharmony_ci * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
8868c2ecf20Sopenharmony_ci * @vhost:	struct ibmvfc host to reset
8878c2ecf20Sopenharmony_ci **/
8888c2ecf20Sopenharmony_cistatic void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
8898c2ecf20Sopenharmony_ci{
8908c2ecf20Sopenharmony_ci	ibmvfc_purge_requests(vhost, DID_ERROR);
8918c2ecf20Sopenharmony_ci	ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
8928c2ecf20Sopenharmony_ci	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
8938c2ecf20Sopenharmony_ci}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci/**
8968c2ecf20Sopenharmony_ci * __ibmvfc_reset_host - Reset the connection to the server (no locking)
8978c2ecf20Sopenharmony_ci * @vhost:	struct ibmvfc host to reset
8988c2ecf20Sopenharmony_ci **/
8998c2ecf20Sopenharmony_cistatic void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
9008c2ecf20Sopenharmony_ci{
9018c2ecf20Sopenharmony_ci	if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
9028c2ecf20Sopenharmony_ci	    !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
9038c2ecf20Sopenharmony_ci		scsi_block_requests(vhost->host);
9048c2ecf20Sopenharmony_ci		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
9058c2ecf20Sopenharmony_ci		vhost->job_step = ibmvfc_npiv_logout;
9068c2ecf20Sopenharmony_ci		wake_up(&vhost->work_wait_q);
9078c2ecf20Sopenharmony_ci	} else
9088c2ecf20Sopenharmony_ci		ibmvfc_hard_reset_host(vhost);
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci/**
9128c2ecf20Sopenharmony_ci * ibmvfc_reset_host - Reset the connection to the server
9138c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
9148c2ecf20Sopenharmony_ci **/
9158c2ecf20Sopenharmony_cistatic void ibmvfc_reset_host(struct ibmvfc_host *vhost)
9168c2ecf20Sopenharmony_ci{
9178c2ecf20Sopenharmony_ci	unsigned long flags;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
9208c2ecf20Sopenharmony_ci	__ibmvfc_reset_host(vhost);
9218c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
9228c2ecf20Sopenharmony_ci}
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci/**
9258c2ecf20Sopenharmony_ci * ibmvfc_retry_host_init - Retry host initialization if allowed
9268c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
9278c2ecf20Sopenharmony_ci *
9288c2ecf20Sopenharmony_ci * Returns: 1 if init will be retried / 0 if not
9298c2ecf20Sopenharmony_ci *
9308c2ecf20Sopenharmony_ci **/
9318c2ecf20Sopenharmony_cistatic int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	int retry = 0;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
9368c2ecf20Sopenharmony_ci		vhost->delay_init = 1;
9378c2ecf20Sopenharmony_ci		if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
9388c2ecf20Sopenharmony_ci			dev_err(vhost->dev,
9398c2ecf20Sopenharmony_ci				"Host initialization retries exceeded. Taking adapter offline\n");
9408c2ecf20Sopenharmony_ci			ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
9418c2ecf20Sopenharmony_ci		} else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
9428c2ecf20Sopenharmony_ci			__ibmvfc_reset_host(vhost);
9438c2ecf20Sopenharmony_ci		else {
9448c2ecf20Sopenharmony_ci			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
9458c2ecf20Sopenharmony_ci			retry = 1;
9468c2ecf20Sopenharmony_ci		}
9478c2ecf20Sopenharmony_ci	}
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
9508c2ecf20Sopenharmony_ci	return retry;
9518c2ecf20Sopenharmony_ci}
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci/**
9548c2ecf20Sopenharmony_ci * __ibmvfc_get_target - Find the specified scsi_target (no locking)
9558c2ecf20Sopenharmony_ci * @starget:	scsi target struct
9568c2ecf20Sopenharmony_ci *
9578c2ecf20Sopenharmony_ci * Return value:
9588c2ecf20Sopenharmony_ci *	ibmvfc_target struct / NULL if not found
9598c2ecf20Sopenharmony_ci **/
9608c2ecf20Sopenharmony_cistatic struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
9618c2ecf20Sopenharmony_ci{
9628c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
9638c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
9648c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	list_for_each_entry(tgt, &vhost->targets, queue)
9678c2ecf20Sopenharmony_ci		if (tgt->target_id == starget->id) {
9688c2ecf20Sopenharmony_ci			kref_get(&tgt->kref);
9698c2ecf20Sopenharmony_ci			return tgt;
9708c2ecf20Sopenharmony_ci		}
9718c2ecf20Sopenharmony_ci	return NULL;
9728c2ecf20Sopenharmony_ci}
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci/**
9758c2ecf20Sopenharmony_ci * ibmvfc_get_target - Find the specified scsi_target
9768c2ecf20Sopenharmony_ci * @starget:	scsi target struct
9778c2ecf20Sopenharmony_ci *
9788c2ecf20Sopenharmony_ci * Return value:
9798c2ecf20Sopenharmony_ci *	ibmvfc_target struct / NULL if not found
9808c2ecf20Sopenharmony_ci **/
9818c2ecf20Sopenharmony_cistatic struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
9848c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
9858c2ecf20Sopenharmony_ci	unsigned long flags;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
9888c2ecf20Sopenharmony_ci	tgt = __ibmvfc_get_target(starget);
9898c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
9908c2ecf20Sopenharmony_ci	return tgt;
9918c2ecf20Sopenharmony_ci}
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci/**
9948c2ecf20Sopenharmony_ci * ibmvfc_get_host_speed - Get host port speed
9958c2ecf20Sopenharmony_ci * @shost:		scsi host struct
9968c2ecf20Sopenharmony_ci *
9978c2ecf20Sopenharmony_ci * Return value:
9988c2ecf20Sopenharmony_ci * 	none
9998c2ecf20Sopenharmony_ci **/
10008c2ecf20Sopenharmony_cistatic void ibmvfc_get_host_speed(struct Scsi_Host *shost)
10018c2ecf20Sopenharmony_ci{
10028c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
10038c2ecf20Sopenharmony_ci	unsigned long flags;
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
10068c2ecf20Sopenharmony_ci	if (vhost->state == IBMVFC_ACTIVE) {
10078c2ecf20Sopenharmony_ci		switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
10088c2ecf20Sopenharmony_ci		case 1:
10098c2ecf20Sopenharmony_ci			fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
10108c2ecf20Sopenharmony_ci			break;
10118c2ecf20Sopenharmony_ci		case 2:
10128c2ecf20Sopenharmony_ci			fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
10138c2ecf20Sopenharmony_ci			break;
10148c2ecf20Sopenharmony_ci		case 4:
10158c2ecf20Sopenharmony_ci			fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
10168c2ecf20Sopenharmony_ci			break;
10178c2ecf20Sopenharmony_ci		case 8:
10188c2ecf20Sopenharmony_ci			fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
10198c2ecf20Sopenharmony_ci			break;
10208c2ecf20Sopenharmony_ci		case 10:
10218c2ecf20Sopenharmony_ci			fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
10228c2ecf20Sopenharmony_ci			break;
10238c2ecf20Sopenharmony_ci		case 16:
10248c2ecf20Sopenharmony_ci			fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
10258c2ecf20Sopenharmony_ci			break;
10268c2ecf20Sopenharmony_ci		default:
10278c2ecf20Sopenharmony_ci			ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
10288c2ecf20Sopenharmony_ci				   be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
10298c2ecf20Sopenharmony_ci			fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
10308c2ecf20Sopenharmony_ci			break;
10318c2ecf20Sopenharmony_ci		}
10328c2ecf20Sopenharmony_ci	} else
10338c2ecf20Sopenharmony_ci		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
10348c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
10358c2ecf20Sopenharmony_ci}
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci/**
10388c2ecf20Sopenharmony_ci * ibmvfc_get_host_port_state - Get host port state
10398c2ecf20Sopenharmony_ci * @shost:		scsi host struct
10408c2ecf20Sopenharmony_ci *
10418c2ecf20Sopenharmony_ci * Return value:
10428c2ecf20Sopenharmony_ci * 	none
10438c2ecf20Sopenharmony_ci **/
10448c2ecf20Sopenharmony_cistatic void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
10458c2ecf20Sopenharmony_ci{
10468c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
10478c2ecf20Sopenharmony_ci	unsigned long flags;
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
10508c2ecf20Sopenharmony_ci	switch (vhost->state) {
10518c2ecf20Sopenharmony_ci	case IBMVFC_INITIALIZING:
10528c2ecf20Sopenharmony_ci	case IBMVFC_ACTIVE:
10538c2ecf20Sopenharmony_ci		fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
10548c2ecf20Sopenharmony_ci		break;
10558c2ecf20Sopenharmony_ci	case IBMVFC_LINK_DOWN:
10568c2ecf20Sopenharmony_ci		fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
10578c2ecf20Sopenharmony_ci		break;
10588c2ecf20Sopenharmony_ci	case IBMVFC_LINK_DEAD:
10598c2ecf20Sopenharmony_ci	case IBMVFC_HOST_OFFLINE:
10608c2ecf20Sopenharmony_ci		fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
10618c2ecf20Sopenharmony_ci		break;
10628c2ecf20Sopenharmony_ci	case IBMVFC_HALTED:
10638c2ecf20Sopenharmony_ci		fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
10648c2ecf20Sopenharmony_ci		break;
10658c2ecf20Sopenharmony_ci	case IBMVFC_NO_CRQ:
10668c2ecf20Sopenharmony_ci		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
10678c2ecf20Sopenharmony_ci		break;
10688c2ecf20Sopenharmony_ci	default:
10698c2ecf20Sopenharmony_ci		ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
10708c2ecf20Sopenharmony_ci		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
10718c2ecf20Sopenharmony_ci		break;
10728c2ecf20Sopenharmony_ci	}
10738c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
10748c2ecf20Sopenharmony_ci}
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci/**
10778c2ecf20Sopenharmony_ci * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
10788c2ecf20Sopenharmony_ci * @rport:		rport struct
10798c2ecf20Sopenharmony_ci * @timeout:	timeout value
10808c2ecf20Sopenharmony_ci *
10818c2ecf20Sopenharmony_ci * Return value:
10828c2ecf20Sopenharmony_ci * 	none
10838c2ecf20Sopenharmony_ci **/
10848c2ecf20Sopenharmony_cistatic void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
10858c2ecf20Sopenharmony_ci{
10868c2ecf20Sopenharmony_ci	if (timeout)
10878c2ecf20Sopenharmony_ci		rport->dev_loss_tmo = timeout;
10888c2ecf20Sopenharmony_ci	else
10898c2ecf20Sopenharmony_ci		rport->dev_loss_tmo = 1;
10908c2ecf20Sopenharmony_ci}
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci/**
10938c2ecf20Sopenharmony_ci * ibmvfc_release_tgt - Free memory allocated for a target
10948c2ecf20Sopenharmony_ci * @kref:		kref struct
10958c2ecf20Sopenharmony_ci *
10968c2ecf20Sopenharmony_ci **/
10978c2ecf20Sopenharmony_cistatic void ibmvfc_release_tgt(struct kref *kref)
10988c2ecf20Sopenharmony_ci{
10998c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
11008c2ecf20Sopenharmony_ci	kfree(tgt);
11018c2ecf20Sopenharmony_ci}
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci/**
11048c2ecf20Sopenharmony_ci * ibmvfc_get_starget_node_name - Get SCSI target's node name
11058c2ecf20Sopenharmony_ci * @starget:	scsi target struct
11068c2ecf20Sopenharmony_ci *
11078c2ecf20Sopenharmony_ci * Return value:
11088c2ecf20Sopenharmony_ci * 	none
11098c2ecf20Sopenharmony_ci **/
11108c2ecf20Sopenharmony_cistatic void ibmvfc_get_starget_node_name(struct scsi_target *starget)
11118c2ecf20Sopenharmony_ci{
11128c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
11138c2ecf20Sopenharmony_ci	fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
11148c2ecf20Sopenharmony_ci	if (tgt)
11158c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
11168c2ecf20Sopenharmony_ci}
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci/**
11198c2ecf20Sopenharmony_ci * ibmvfc_get_starget_port_name - Get SCSI target's port name
11208c2ecf20Sopenharmony_ci * @starget:	scsi target struct
11218c2ecf20Sopenharmony_ci *
11228c2ecf20Sopenharmony_ci * Return value:
11238c2ecf20Sopenharmony_ci * 	none
11248c2ecf20Sopenharmony_ci **/
11258c2ecf20Sopenharmony_cistatic void ibmvfc_get_starget_port_name(struct scsi_target *starget)
11268c2ecf20Sopenharmony_ci{
11278c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
11288c2ecf20Sopenharmony_ci	fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
11298c2ecf20Sopenharmony_ci	if (tgt)
11308c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
11318c2ecf20Sopenharmony_ci}
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci/**
11348c2ecf20Sopenharmony_ci * ibmvfc_get_starget_port_id - Get SCSI target's port ID
11358c2ecf20Sopenharmony_ci * @starget:	scsi target struct
11368c2ecf20Sopenharmony_ci *
11378c2ecf20Sopenharmony_ci * Return value:
11388c2ecf20Sopenharmony_ci * 	none
11398c2ecf20Sopenharmony_ci **/
11408c2ecf20Sopenharmony_cistatic void ibmvfc_get_starget_port_id(struct scsi_target *starget)
11418c2ecf20Sopenharmony_ci{
11428c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
11438c2ecf20Sopenharmony_ci	fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
11448c2ecf20Sopenharmony_ci	if (tgt)
11458c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
11468c2ecf20Sopenharmony_ci}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci/**
11498c2ecf20Sopenharmony_ci * ibmvfc_wait_while_resetting - Wait while the host resets
11508c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
11518c2ecf20Sopenharmony_ci *
11528c2ecf20Sopenharmony_ci * Return value:
11538c2ecf20Sopenharmony_ci * 	0 on success / other on failure
11548c2ecf20Sopenharmony_ci **/
11558c2ecf20Sopenharmony_cistatic int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
11568c2ecf20Sopenharmony_ci{
11578c2ecf20Sopenharmony_ci	long timeout = wait_event_timeout(vhost->init_wait_q,
11588c2ecf20Sopenharmony_ci					  ((vhost->state == IBMVFC_ACTIVE ||
11598c2ecf20Sopenharmony_ci					    vhost->state == IBMVFC_HOST_OFFLINE ||
11608c2ecf20Sopenharmony_ci					    vhost->state == IBMVFC_LINK_DEAD) &&
11618c2ecf20Sopenharmony_ci					   vhost->action == IBMVFC_HOST_ACTION_NONE),
11628c2ecf20Sopenharmony_ci					  (init_timeout * HZ));
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	return timeout ? 0 : -EIO;
11658c2ecf20Sopenharmony_ci}
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci/**
11688c2ecf20Sopenharmony_ci * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
11698c2ecf20Sopenharmony_ci * @shost:		scsi host struct
11708c2ecf20Sopenharmony_ci *
11718c2ecf20Sopenharmony_ci * Return value:
11728c2ecf20Sopenharmony_ci * 	0 on success / other on failure
11738c2ecf20Sopenharmony_ci **/
11748c2ecf20Sopenharmony_cistatic int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
11758c2ecf20Sopenharmony_ci{
11768c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci	dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
11798c2ecf20Sopenharmony_ci	ibmvfc_reset_host(vhost);
11808c2ecf20Sopenharmony_ci	return ibmvfc_wait_while_resetting(vhost);
11818c2ecf20Sopenharmony_ci}
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci/**
11848c2ecf20Sopenharmony_ci * ibmvfc_gather_partition_info - Gather info about the LPAR
11858c2ecf20Sopenharmony_ci *
11868c2ecf20Sopenharmony_ci * Return value:
11878c2ecf20Sopenharmony_ci *	none
11888c2ecf20Sopenharmony_ci **/
11898c2ecf20Sopenharmony_cistatic void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
11908c2ecf20Sopenharmony_ci{
11918c2ecf20Sopenharmony_ci	struct device_node *rootdn;
11928c2ecf20Sopenharmony_ci	const char *name;
11938c2ecf20Sopenharmony_ci	const unsigned int *num;
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	rootdn = of_find_node_by_path("/");
11968c2ecf20Sopenharmony_ci	if (!rootdn)
11978c2ecf20Sopenharmony_ci		return;
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci	name = of_get_property(rootdn, "ibm,partition-name", NULL);
12008c2ecf20Sopenharmony_ci	if (name)
12018c2ecf20Sopenharmony_ci		strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
12028c2ecf20Sopenharmony_ci	num = of_get_property(rootdn, "ibm,partition-no", NULL);
12038c2ecf20Sopenharmony_ci	if (num)
12048c2ecf20Sopenharmony_ci		vhost->partition_number = *num;
12058c2ecf20Sopenharmony_ci	of_node_put(rootdn);
12068c2ecf20Sopenharmony_ci}
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci/**
12098c2ecf20Sopenharmony_ci * ibmvfc_set_login_info - Setup info for NPIV login
12108c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
12118c2ecf20Sopenharmony_ci *
12128c2ecf20Sopenharmony_ci * Return value:
12138c2ecf20Sopenharmony_ci *	none
12148c2ecf20Sopenharmony_ci **/
12158c2ecf20Sopenharmony_cistatic void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
12168c2ecf20Sopenharmony_ci{
12178c2ecf20Sopenharmony_ci	struct ibmvfc_npiv_login *login_info = &vhost->login_info;
12188c2ecf20Sopenharmony_ci	struct device_node *of_node = vhost->dev->of_node;
12198c2ecf20Sopenharmony_ci	const char *location;
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	memset(login_info, 0, sizeof(*login_info));
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
12248c2ecf20Sopenharmony_ci	login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
12258c2ecf20Sopenharmony_ci	login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
12268c2ecf20Sopenharmony_ci	login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
12278c2ecf20Sopenharmony_ci	login_info->partition_num = cpu_to_be32(vhost->partition_number);
12288c2ecf20Sopenharmony_ci	login_info->vfc_frame_version = cpu_to_be32(1);
12298c2ecf20Sopenharmony_ci	login_info->fcp_version = cpu_to_be16(3);
12308c2ecf20Sopenharmony_ci	login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
12318c2ecf20Sopenharmony_ci	if (vhost->client_migrated)
12328c2ecf20Sopenharmony_ci		login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
12358c2ecf20Sopenharmony_ci	login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE);
12368c2ecf20Sopenharmony_ci	login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
12378c2ecf20Sopenharmony_ci	login_info->async.len = cpu_to_be32(vhost->async_crq.size * sizeof(*vhost->async_crq.msgs));
12388c2ecf20Sopenharmony_ci	strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
12398c2ecf20Sopenharmony_ci	strncpy(login_info->device_name,
12408c2ecf20Sopenharmony_ci		dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	location = of_get_property(of_node, "ibm,loc-code", NULL);
12438c2ecf20Sopenharmony_ci	location = location ? location : dev_name(vhost->dev);
12448c2ecf20Sopenharmony_ci	strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
12458c2ecf20Sopenharmony_ci}
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci/**
12488c2ecf20Sopenharmony_ci * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
12498c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host who owns the event pool
12508c2ecf20Sopenharmony_ci *
12518c2ecf20Sopenharmony_ci * Returns zero on success.
12528c2ecf20Sopenharmony_ci **/
12538c2ecf20Sopenharmony_cistatic int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
12548c2ecf20Sopenharmony_ci{
12558c2ecf20Sopenharmony_ci	int i;
12568c2ecf20Sopenharmony_ci	struct ibmvfc_event_pool *pool = &vhost->pool;
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci	ENTER;
12598c2ecf20Sopenharmony_ci	pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
12608c2ecf20Sopenharmony_ci	pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
12618c2ecf20Sopenharmony_ci	if (!pool->events)
12628c2ecf20Sopenharmony_ci		return -ENOMEM;
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	pool->iu_storage = dma_alloc_coherent(vhost->dev,
12658c2ecf20Sopenharmony_ci					      pool->size * sizeof(*pool->iu_storage),
12668c2ecf20Sopenharmony_ci					      &pool->iu_token, 0);
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	if (!pool->iu_storage) {
12698c2ecf20Sopenharmony_ci		kfree(pool->events);
12708c2ecf20Sopenharmony_ci		return -ENOMEM;
12718c2ecf20Sopenharmony_ci	}
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	for (i = 0; i < pool->size; ++i) {
12748c2ecf20Sopenharmony_ci		struct ibmvfc_event *evt = &pool->events[i];
12758c2ecf20Sopenharmony_ci		atomic_set(&evt->free, 1);
12768c2ecf20Sopenharmony_ci		evt->crq.valid = 0x80;
12778c2ecf20Sopenharmony_ci		evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
12788c2ecf20Sopenharmony_ci		evt->xfer_iu = pool->iu_storage + i;
12798c2ecf20Sopenharmony_ci		evt->vhost = vhost;
12808c2ecf20Sopenharmony_ci		evt->ext_list = NULL;
12818c2ecf20Sopenharmony_ci		list_add_tail(&evt->queue, &vhost->free);
12828c2ecf20Sopenharmony_ci	}
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	LEAVE;
12858c2ecf20Sopenharmony_ci	return 0;
12868c2ecf20Sopenharmony_ci}
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci/**
12898c2ecf20Sopenharmony_ci * ibmvfc_free_event_pool - Frees memory of the event pool of a host
12908c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host who owns the event pool
12918c2ecf20Sopenharmony_ci *
12928c2ecf20Sopenharmony_ci **/
12938c2ecf20Sopenharmony_cistatic void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
12948c2ecf20Sopenharmony_ci{
12958c2ecf20Sopenharmony_ci	int i;
12968c2ecf20Sopenharmony_ci	struct ibmvfc_event_pool *pool = &vhost->pool;
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	ENTER;
12998c2ecf20Sopenharmony_ci	for (i = 0; i < pool->size; ++i) {
13008c2ecf20Sopenharmony_ci		list_del(&pool->events[i].queue);
13018c2ecf20Sopenharmony_ci		BUG_ON(atomic_read(&pool->events[i].free) != 1);
13028c2ecf20Sopenharmony_ci		if (pool->events[i].ext_list)
13038c2ecf20Sopenharmony_ci			dma_pool_free(vhost->sg_pool,
13048c2ecf20Sopenharmony_ci				      pool->events[i].ext_list,
13058c2ecf20Sopenharmony_ci				      pool->events[i].ext_list_token);
13068c2ecf20Sopenharmony_ci	}
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	kfree(pool->events);
13098c2ecf20Sopenharmony_ci	dma_free_coherent(vhost->dev,
13108c2ecf20Sopenharmony_ci			  pool->size * sizeof(*pool->iu_storage),
13118c2ecf20Sopenharmony_ci			  pool->iu_storage, pool->iu_token);
13128c2ecf20Sopenharmony_ci	LEAVE;
13138c2ecf20Sopenharmony_ci}
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci/**
13168c2ecf20Sopenharmony_ci * ibmvfc_get_event - Gets the next free event in pool
13178c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
13188c2ecf20Sopenharmony_ci *
13198c2ecf20Sopenharmony_ci * Returns a free event from the pool.
13208c2ecf20Sopenharmony_ci **/
13218c2ecf20Sopenharmony_cistatic struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
13228c2ecf20Sopenharmony_ci{
13238c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci	BUG_ON(list_empty(&vhost->free));
13268c2ecf20Sopenharmony_ci	evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
13278c2ecf20Sopenharmony_ci	atomic_set(&evt->free, 0);
13288c2ecf20Sopenharmony_ci	list_del(&evt->queue);
13298c2ecf20Sopenharmony_ci	return evt;
13308c2ecf20Sopenharmony_ci}
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci/**
13338c2ecf20Sopenharmony_ci * ibmvfc_init_event - Initialize fields in an event struct that are always
13348c2ecf20Sopenharmony_ci *				required.
13358c2ecf20Sopenharmony_ci * @evt:	The event
13368c2ecf20Sopenharmony_ci * @done:	Routine to call when the event is responded to
13378c2ecf20Sopenharmony_ci * @format:	SRP or MAD format
13388c2ecf20Sopenharmony_ci **/
13398c2ecf20Sopenharmony_cistatic void ibmvfc_init_event(struct ibmvfc_event *evt,
13408c2ecf20Sopenharmony_ci			      void (*done) (struct ibmvfc_event *), u8 format)
13418c2ecf20Sopenharmony_ci{
13428c2ecf20Sopenharmony_ci	evt->cmnd = NULL;
13438c2ecf20Sopenharmony_ci	evt->sync_iu = NULL;
13448c2ecf20Sopenharmony_ci	evt->crq.format = format;
13458c2ecf20Sopenharmony_ci	evt->done = done;
13468c2ecf20Sopenharmony_ci	evt->eh_comp = NULL;
13478c2ecf20Sopenharmony_ci}
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ci/**
13508c2ecf20Sopenharmony_ci * ibmvfc_map_sg_list - Initialize scatterlist
13518c2ecf20Sopenharmony_ci * @scmd:	scsi command struct
13528c2ecf20Sopenharmony_ci * @nseg:	number of scatterlist segments
13538c2ecf20Sopenharmony_ci * @md:	memory descriptor list to initialize
13548c2ecf20Sopenharmony_ci **/
13558c2ecf20Sopenharmony_cistatic void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
13568c2ecf20Sopenharmony_ci			       struct srp_direct_buf *md)
13578c2ecf20Sopenharmony_ci{
13588c2ecf20Sopenharmony_ci	int i;
13598c2ecf20Sopenharmony_ci	struct scatterlist *sg;
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	scsi_for_each_sg(scmd, sg, nseg, i) {
13628c2ecf20Sopenharmony_ci		md[i].va = cpu_to_be64(sg_dma_address(sg));
13638c2ecf20Sopenharmony_ci		md[i].len = cpu_to_be32(sg_dma_len(sg));
13648c2ecf20Sopenharmony_ci		md[i].key = 0;
13658c2ecf20Sopenharmony_ci	}
13668c2ecf20Sopenharmony_ci}
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci/**
13698c2ecf20Sopenharmony_ci * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
13708c2ecf20Sopenharmony_ci * @scmd:		struct scsi_cmnd with the scatterlist
13718c2ecf20Sopenharmony_ci * @evt:		ibmvfc event struct
13728c2ecf20Sopenharmony_ci * @vfc_cmd:	vfc_cmd that contains the memory descriptor
13738c2ecf20Sopenharmony_ci * @dev:		device for which to map dma memory
13748c2ecf20Sopenharmony_ci *
13758c2ecf20Sopenharmony_ci * Returns:
13768c2ecf20Sopenharmony_ci *	0 on success / non-zero on failure
13778c2ecf20Sopenharmony_ci **/
13788c2ecf20Sopenharmony_cistatic int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
13798c2ecf20Sopenharmony_ci			      struct ibmvfc_event *evt,
13808c2ecf20Sopenharmony_ci			      struct ibmvfc_cmd *vfc_cmd, struct device *dev)
13818c2ecf20Sopenharmony_ci{
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci	int sg_mapped;
13848c2ecf20Sopenharmony_ci	struct srp_direct_buf *data = &vfc_cmd->ioba;
13858c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = dev_get_drvdata(dev);
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci	if (cls3_error)
13888c2ecf20Sopenharmony_ci		vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci	sg_mapped = scsi_dma_map(scmd);
13918c2ecf20Sopenharmony_ci	if (!sg_mapped) {
13928c2ecf20Sopenharmony_ci		vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
13938c2ecf20Sopenharmony_ci		return 0;
13948c2ecf20Sopenharmony_ci	} else if (unlikely(sg_mapped < 0)) {
13958c2ecf20Sopenharmony_ci		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
13968c2ecf20Sopenharmony_ci			scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
13978c2ecf20Sopenharmony_ci		return sg_mapped;
13988c2ecf20Sopenharmony_ci	}
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	if (scmd->sc_data_direction == DMA_TO_DEVICE) {
14018c2ecf20Sopenharmony_ci		vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
14028c2ecf20Sopenharmony_ci		vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
14038c2ecf20Sopenharmony_ci	} else {
14048c2ecf20Sopenharmony_ci		vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
14058c2ecf20Sopenharmony_ci		vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
14068c2ecf20Sopenharmony_ci	}
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci	if (sg_mapped == 1) {
14098c2ecf20Sopenharmony_ci		ibmvfc_map_sg_list(scmd, sg_mapped, data);
14108c2ecf20Sopenharmony_ci		return 0;
14118c2ecf20Sopenharmony_ci	}
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci	if (!evt->ext_list) {
14168c2ecf20Sopenharmony_ci		evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
14178c2ecf20Sopenharmony_ci					       &evt->ext_list_token);
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci		if (!evt->ext_list) {
14208c2ecf20Sopenharmony_ci			scsi_dma_unmap(scmd);
14218c2ecf20Sopenharmony_ci			if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
14228c2ecf20Sopenharmony_ci				scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
14238c2ecf20Sopenharmony_ci			return -ENOMEM;
14248c2ecf20Sopenharmony_ci		}
14258c2ecf20Sopenharmony_ci	}
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	data->va = cpu_to_be64(evt->ext_list_token);
14308c2ecf20Sopenharmony_ci	data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
14318c2ecf20Sopenharmony_ci	data->key = 0;
14328c2ecf20Sopenharmony_ci	return 0;
14338c2ecf20Sopenharmony_ci}
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_ci/**
14368c2ecf20Sopenharmony_ci * ibmvfc_timeout - Internal command timeout handler
14378c2ecf20Sopenharmony_ci * @evt:	struct ibmvfc_event that timed out
14388c2ecf20Sopenharmony_ci *
14398c2ecf20Sopenharmony_ci * Called when an internally generated command times out
14408c2ecf20Sopenharmony_ci **/
14418c2ecf20Sopenharmony_cistatic void ibmvfc_timeout(struct timer_list *t)
14428c2ecf20Sopenharmony_ci{
14438c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt = from_timer(evt, t, timer);
14448c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
14458c2ecf20Sopenharmony_ci	dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
14468c2ecf20Sopenharmony_ci	ibmvfc_reset_host(vhost);
14478c2ecf20Sopenharmony_ci}
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci/**
14508c2ecf20Sopenharmony_ci * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
14518c2ecf20Sopenharmony_ci * @evt:		event to be sent
14528c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
14538c2ecf20Sopenharmony_ci * @timeout:	timeout in seconds - 0 means do not time command
14548c2ecf20Sopenharmony_ci *
14558c2ecf20Sopenharmony_ci * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
14568c2ecf20Sopenharmony_ci **/
14578c2ecf20Sopenharmony_cistatic int ibmvfc_send_event(struct ibmvfc_event *evt,
14588c2ecf20Sopenharmony_ci			     struct ibmvfc_host *vhost, unsigned long timeout)
14598c2ecf20Sopenharmony_ci{
14608c2ecf20Sopenharmony_ci	__be64 *crq_as_u64 = (__be64 *) &evt->crq;
14618c2ecf20Sopenharmony_ci	int rc;
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	/* Copy the IU into the transfer area */
14648c2ecf20Sopenharmony_ci	*evt->xfer_iu = evt->iu;
14658c2ecf20Sopenharmony_ci	if (evt->crq.format == IBMVFC_CMD_FORMAT)
14668c2ecf20Sopenharmony_ci		evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
14678c2ecf20Sopenharmony_ci	else if (evt->crq.format == IBMVFC_MAD_FORMAT)
14688c2ecf20Sopenharmony_ci		evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
14698c2ecf20Sopenharmony_ci	else
14708c2ecf20Sopenharmony_ci		BUG();
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_ci	list_add_tail(&evt->queue, &vhost->sent);
14738c2ecf20Sopenharmony_ci	timer_setup(&evt->timer, ibmvfc_timeout, 0);
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	if (timeout) {
14768c2ecf20Sopenharmony_ci		evt->timer.expires = jiffies + (timeout * HZ);
14778c2ecf20Sopenharmony_ci		add_timer(&evt->timer);
14788c2ecf20Sopenharmony_ci	}
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci	mb();
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
14838c2ecf20Sopenharmony_ci				  be64_to_cpu(crq_as_u64[1])))) {
14848c2ecf20Sopenharmony_ci		list_del(&evt->queue);
14858c2ecf20Sopenharmony_ci		del_timer(&evt->timer);
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci		/* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
14888c2ecf20Sopenharmony_ci		 * Firmware will send a CRQ with a transport event (0xFF) to
14898c2ecf20Sopenharmony_ci		 * tell this client what has happened to the transport. This
14908c2ecf20Sopenharmony_ci		 * will be handled in ibmvfc_handle_crq()
14918c2ecf20Sopenharmony_ci		 */
14928c2ecf20Sopenharmony_ci		if (rc == H_CLOSED) {
14938c2ecf20Sopenharmony_ci			if (printk_ratelimit())
14948c2ecf20Sopenharmony_ci				dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
14958c2ecf20Sopenharmony_ci			if (evt->cmnd)
14968c2ecf20Sopenharmony_ci				scsi_dma_unmap(evt->cmnd);
14978c2ecf20Sopenharmony_ci			ibmvfc_free_event(evt);
14988c2ecf20Sopenharmony_ci			return SCSI_MLQUEUE_HOST_BUSY;
14998c2ecf20Sopenharmony_ci		}
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
15028c2ecf20Sopenharmony_ci		if (evt->cmnd) {
15038c2ecf20Sopenharmony_ci			evt->cmnd->result = DID_ERROR << 16;
15048c2ecf20Sopenharmony_ci			evt->done = ibmvfc_scsi_eh_done;
15058c2ecf20Sopenharmony_ci		} else
15068c2ecf20Sopenharmony_ci			evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ci		evt->done(evt);
15098c2ecf20Sopenharmony_ci	} else
15108c2ecf20Sopenharmony_ci		ibmvfc_trc_start(evt);
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	return 0;
15138c2ecf20Sopenharmony_ci}
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci/**
15168c2ecf20Sopenharmony_ci * ibmvfc_log_error - Log an error for the failed command if appropriate
15178c2ecf20Sopenharmony_ci * @evt:	ibmvfc event to log
15188c2ecf20Sopenharmony_ci *
15198c2ecf20Sopenharmony_ci **/
15208c2ecf20Sopenharmony_cistatic void ibmvfc_log_error(struct ibmvfc_event *evt)
15218c2ecf20Sopenharmony_ci{
15228c2ecf20Sopenharmony_ci	struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
15238c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
15248c2ecf20Sopenharmony_ci	struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
15258c2ecf20Sopenharmony_ci	struct scsi_cmnd *cmnd = evt->cmnd;
15268c2ecf20Sopenharmony_ci	const char *err = unknown_error;
15278c2ecf20Sopenharmony_ci	int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
15288c2ecf20Sopenharmony_ci	int logerr = 0;
15298c2ecf20Sopenharmony_ci	int rsp_code = 0;
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci	if (index >= 0) {
15328c2ecf20Sopenharmony_ci		logerr = cmd_status[index].log;
15338c2ecf20Sopenharmony_ci		err = cmd_status[index].name;
15348c2ecf20Sopenharmony_ci	}
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci	if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
15378c2ecf20Sopenharmony_ci		return;
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_ci	if (rsp->flags & FCP_RSP_LEN_VALID)
15408c2ecf20Sopenharmony_ci		rsp_code = rsp->data.info.rsp_code;
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
15438c2ecf20Sopenharmony_ci		    "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
15448c2ecf20Sopenharmony_ci		    cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
15458c2ecf20Sopenharmony_ci		    rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
15468c2ecf20Sopenharmony_ci}
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci/**
15498c2ecf20Sopenharmony_ci * ibmvfc_relogin - Log back into the specified device
15508c2ecf20Sopenharmony_ci * @sdev:	scsi device struct
15518c2ecf20Sopenharmony_ci *
15528c2ecf20Sopenharmony_ci **/
15538c2ecf20Sopenharmony_cistatic void ibmvfc_relogin(struct scsi_device *sdev)
15548c2ecf20Sopenharmony_ci{
15558c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(sdev->host);
15568c2ecf20Sopenharmony_ci	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
15578c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	list_for_each_entry(tgt, &vhost->targets, queue) {
15608c2ecf20Sopenharmony_ci		if (rport == tgt->rport) {
15618c2ecf20Sopenharmony_ci			ibmvfc_del_tgt(tgt);
15628c2ecf20Sopenharmony_ci			break;
15638c2ecf20Sopenharmony_ci		}
15648c2ecf20Sopenharmony_ci	}
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci	ibmvfc_reinit_host(vhost);
15678c2ecf20Sopenharmony_ci}
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci/**
15708c2ecf20Sopenharmony_ci * ibmvfc_scsi_done - Handle responses from commands
15718c2ecf20Sopenharmony_ci * @evt:	ibmvfc event to be handled
15728c2ecf20Sopenharmony_ci *
15738c2ecf20Sopenharmony_ci * Used as a callback when sending scsi cmds.
15748c2ecf20Sopenharmony_ci **/
15758c2ecf20Sopenharmony_cistatic void ibmvfc_scsi_done(struct ibmvfc_event *evt)
15768c2ecf20Sopenharmony_ci{
15778c2ecf20Sopenharmony_ci	struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
15788c2ecf20Sopenharmony_ci	struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
15798c2ecf20Sopenharmony_ci	struct scsi_cmnd *cmnd = evt->cmnd;
15808c2ecf20Sopenharmony_ci	u32 rsp_len = 0;
15818c2ecf20Sopenharmony_ci	u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci	if (cmnd) {
15848c2ecf20Sopenharmony_ci		if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
15858c2ecf20Sopenharmony_ci			scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
15868c2ecf20Sopenharmony_ci		else if (rsp->flags & FCP_RESID_UNDER)
15878c2ecf20Sopenharmony_ci			scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
15888c2ecf20Sopenharmony_ci		else
15898c2ecf20Sopenharmony_ci			scsi_set_resid(cmnd, 0);
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci		if (vfc_cmd->status) {
15928c2ecf20Sopenharmony_ci			cmnd->result = ibmvfc_get_err_result(vfc_cmd);
15938c2ecf20Sopenharmony_ci
15948c2ecf20Sopenharmony_ci			if (rsp->flags & FCP_RSP_LEN_VALID)
15958c2ecf20Sopenharmony_ci				rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
15968c2ecf20Sopenharmony_ci			if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
15978c2ecf20Sopenharmony_ci				sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
15988c2ecf20Sopenharmony_ci			if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
15998c2ecf20Sopenharmony_ci				memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
16008c2ecf20Sopenharmony_ci			if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
16018c2ecf20Sopenharmony_ci			    (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
16028c2ecf20Sopenharmony_ci				ibmvfc_relogin(cmnd->device);
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_ci			if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
16058c2ecf20Sopenharmony_ci				cmnd->result = (DID_ERROR << 16);
16068c2ecf20Sopenharmony_ci
16078c2ecf20Sopenharmony_ci			ibmvfc_log_error(evt);
16088c2ecf20Sopenharmony_ci		}
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_ci		if (!cmnd->result &&
16118c2ecf20Sopenharmony_ci		    (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
16128c2ecf20Sopenharmony_ci			cmnd->result = (DID_ERROR << 16);
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_ci		scsi_dma_unmap(cmnd);
16158c2ecf20Sopenharmony_ci		cmnd->scsi_done(cmnd);
16168c2ecf20Sopenharmony_ci	}
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_ci	if (evt->eh_comp)
16198c2ecf20Sopenharmony_ci		complete(evt->eh_comp);
16208c2ecf20Sopenharmony_ci
16218c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
16228c2ecf20Sopenharmony_ci}
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_ci/**
16258c2ecf20Sopenharmony_ci * ibmvfc_host_chkready - Check if the host can accept commands
16268c2ecf20Sopenharmony_ci * @vhost:	 struct ibmvfc host
16278c2ecf20Sopenharmony_ci *
16288c2ecf20Sopenharmony_ci * Returns:
16298c2ecf20Sopenharmony_ci *	1 if host can accept command / 0 if not
16308c2ecf20Sopenharmony_ci **/
16318c2ecf20Sopenharmony_cistatic inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
16328c2ecf20Sopenharmony_ci{
16338c2ecf20Sopenharmony_ci	int result = 0;
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_ci	switch (vhost->state) {
16368c2ecf20Sopenharmony_ci	case IBMVFC_LINK_DEAD:
16378c2ecf20Sopenharmony_ci	case IBMVFC_HOST_OFFLINE:
16388c2ecf20Sopenharmony_ci		result = DID_NO_CONNECT << 16;
16398c2ecf20Sopenharmony_ci		break;
16408c2ecf20Sopenharmony_ci	case IBMVFC_NO_CRQ:
16418c2ecf20Sopenharmony_ci	case IBMVFC_INITIALIZING:
16428c2ecf20Sopenharmony_ci	case IBMVFC_HALTED:
16438c2ecf20Sopenharmony_ci	case IBMVFC_LINK_DOWN:
16448c2ecf20Sopenharmony_ci		result = DID_REQUEUE << 16;
16458c2ecf20Sopenharmony_ci		break;
16468c2ecf20Sopenharmony_ci	case IBMVFC_ACTIVE:
16478c2ecf20Sopenharmony_ci		result = 0;
16488c2ecf20Sopenharmony_ci		break;
16498c2ecf20Sopenharmony_ci	}
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	return result;
16528c2ecf20Sopenharmony_ci}
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci/**
16558c2ecf20Sopenharmony_ci * ibmvfc_queuecommand - The queuecommand function of the scsi template
16568c2ecf20Sopenharmony_ci * @cmnd:	struct scsi_cmnd to be executed
16578c2ecf20Sopenharmony_ci * @done:	Callback function to be called when cmnd is completed
16588c2ecf20Sopenharmony_ci *
16598c2ecf20Sopenharmony_ci * Returns:
16608c2ecf20Sopenharmony_ci *	0 on success / other on failure
16618c2ecf20Sopenharmony_ci **/
16628c2ecf20Sopenharmony_cistatic int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
16638c2ecf20Sopenharmony_ci			       void (*done) (struct scsi_cmnd *))
16648c2ecf20Sopenharmony_ci{
16658c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
16668c2ecf20Sopenharmony_ci	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
16678c2ecf20Sopenharmony_ci	struct ibmvfc_cmd *vfc_cmd;
16688c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
16698c2ecf20Sopenharmony_ci	int rc;
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	if (unlikely((rc = fc_remote_port_chkready(rport))) ||
16728c2ecf20Sopenharmony_ci	    unlikely((rc = ibmvfc_host_chkready(vhost)))) {
16738c2ecf20Sopenharmony_ci		cmnd->result = rc;
16748c2ecf20Sopenharmony_ci		done(cmnd);
16758c2ecf20Sopenharmony_ci		return 0;
16768c2ecf20Sopenharmony_ci	}
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_ci	cmnd->result = (DID_OK << 16);
16798c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
16808c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
16818c2ecf20Sopenharmony_ci	evt->cmnd = cmnd;
16828c2ecf20Sopenharmony_ci	cmnd->scsi_done = done;
16838c2ecf20Sopenharmony_ci	vfc_cmd = &evt->iu.cmd;
16848c2ecf20Sopenharmony_ci	memset(vfc_cmd, 0, sizeof(*vfc_cmd));
16858c2ecf20Sopenharmony_ci	vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
16868c2ecf20Sopenharmony_ci	vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
16878c2ecf20Sopenharmony_ci	vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
16888c2ecf20Sopenharmony_ci	vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
16898c2ecf20Sopenharmony_ci	vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
16908c2ecf20Sopenharmony_ci	vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
16918c2ecf20Sopenharmony_ci	vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
16928c2ecf20Sopenharmony_ci	vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
16938c2ecf20Sopenharmony_ci	int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
16948c2ecf20Sopenharmony_ci	memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ci	if (cmnd->flags & SCMD_TAGGED) {
16978c2ecf20Sopenharmony_ci		vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
16988c2ecf20Sopenharmony_ci		vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
16998c2ecf20Sopenharmony_ci	}
17008c2ecf20Sopenharmony_ci
17018c2ecf20Sopenharmony_ci	if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
17028c2ecf20Sopenharmony_ci		return ibmvfc_send_event(evt, vhost, 0);
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
17058c2ecf20Sopenharmony_ci	if (rc == -ENOMEM)
17068c2ecf20Sopenharmony_ci		return SCSI_MLQUEUE_HOST_BUSY;
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_ci	if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
17098c2ecf20Sopenharmony_ci		scmd_printk(KERN_ERR, cmnd,
17108c2ecf20Sopenharmony_ci			    "Failed to map DMA buffer for command. rc=%d\n", rc);
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_ci	cmnd->result = DID_ERROR << 16;
17138c2ecf20Sopenharmony_ci	done(cmnd);
17148c2ecf20Sopenharmony_ci	return 0;
17158c2ecf20Sopenharmony_ci}
17168c2ecf20Sopenharmony_ci
17178c2ecf20Sopenharmony_cistatic DEF_SCSI_QCMD(ibmvfc_queuecommand)
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci/**
17208c2ecf20Sopenharmony_ci * ibmvfc_sync_completion - Signal that a synchronous command has completed
17218c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
17228c2ecf20Sopenharmony_ci *
17238c2ecf20Sopenharmony_ci **/
17248c2ecf20Sopenharmony_cistatic void ibmvfc_sync_completion(struct ibmvfc_event *evt)
17258c2ecf20Sopenharmony_ci{
17268c2ecf20Sopenharmony_ci	/* copy the response back */
17278c2ecf20Sopenharmony_ci	if (evt->sync_iu)
17288c2ecf20Sopenharmony_ci		*evt->sync_iu = *evt->xfer_iu;
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci	complete(&evt->comp);
17318c2ecf20Sopenharmony_ci}
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci/**
17348c2ecf20Sopenharmony_ci * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
17358c2ecf20Sopenharmony_ci * @evt:	struct ibmvfc_event
17368c2ecf20Sopenharmony_ci *
17378c2ecf20Sopenharmony_ci **/
17388c2ecf20Sopenharmony_cistatic void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
17398c2ecf20Sopenharmony_ci{
17408c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
17418c2ecf20Sopenharmony_ci
17428c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
17438c2ecf20Sopenharmony_ci	vhost->aborting_passthru = 0;
17448c2ecf20Sopenharmony_ci	dev_info(vhost->dev, "Passthru command cancelled\n");
17458c2ecf20Sopenharmony_ci}
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci/**
17488c2ecf20Sopenharmony_ci * ibmvfc_bsg_timeout - Handle a BSG timeout
17498c2ecf20Sopenharmony_ci * @job:	struct bsg_job that timed out
17508c2ecf20Sopenharmony_ci *
17518c2ecf20Sopenharmony_ci * Returns:
17528c2ecf20Sopenharmony_ci *	0 on success / other on failure
17538c2ecf20Sopenharmony_ci **/
17548c2ecf20Sopenharmony_cistatic int ibmvfc_bsg_timeout(struct bsg_job *job)
17558c2ecf20Sopenharmony_ci{
17568c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
17578c2ecf20Sopenharmony_ci	unsigned long port_id = (unsigned long)job->dd_data;
17588c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
17598c2ecf20Sopenharmony_ci	struct ibmvfc_tmf *tmf;
17608c2ecf20Sopenharmony_ci	unsigned long flags;
17618c2ecf20Sopenharmony_ci	int rc;
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	ENTER;
17648c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
17658c2ecf20Sopenharmony_ci	if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
17668c2ecf20Sopenharmony_ci		__ibmvfc_reset_host(vhost);
17678c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
17688c2ecf20Sopenharmony_ci		return 0;
17698c2ecf20Sopenharmony_ci	}
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci	vhost->aborting_passthru = 1;
17728c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
17738c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_ci	tmf = &evt->iu.tmf;
17768c2ecf20Sopenharmony_ci	memset(tmf, 0, sizeof(*tmf));
17778c2ecf20Sopenharmony_ci	tmf->common.version = cpu_to_be32(1);
17788c2ecf20Sopenharmony_ci	tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
17798c2ecf20Sopenharmony_ci	tmf->common.length = cpu_to_be16(sizeof(*tmf));
17808c2ecf20Sopenharmony_ci	tmf->scsi_id = cpu_to_be64(port_id);
17818c2ecf20Sopenharmony_ci	tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
17828c2ecf20Sopenharmony_ci	tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
17838c2ecf20Sopenharmony_ci	rc = ibmvfc_send_event(evt, vhost, default_timeout);
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ci	if (rc != 0) {
17868c2ecf20Sopenharmony_ci		vhost->aborting_passthru = 0;
17878c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
17888c2ecf20Sopenharmony_ci		rc = -EIO;
17898c2ecf20Sopenharmony_ci	} else
17908c2ecf20Sopenharmony_ci		dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
17918c2ecf20Sopenharmony_ci			 port_id);
17928c2ecf20Sopenharmony_ci
17938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
17948c2ecf20Sopenharmony_ci
17958c2ecf20Sopenharmony_ci	LEAVE;
17968c2ecf20Sopenharmony_ci	return rc;
17978c2ecf20Sopenharmony_ci}
17988c2ecf20Sopenharmony_ci
17998c2ecf20Sopenharmony_ci/**
18008c2ecf20Sopenharmony_ci * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
18018c2ecf20Sopenharmony_ci * @vhost:		struct ibmvfc_host to send command
18028c2ecf20Sopenharmony_ci * @port_id:	port ID to send command
18038c2ecf20Sopenharmony_ci *
18048c2ecf20Sopenharmony_ci * Returns:
18058c2ecf20Sopenharmony_ci *	0 on success / other on failure
18068c2ecf20Sopenharmony_ci **/
18078c2ecf20Sopenharmony_cistatic int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
18088c2ecf20Sopenharmony_ci{
18098c2ecf20Sopenharmony_ci	struct ibmvfc_port_login *plogi;
18108c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
18118c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
18128c2ecf20Sopenharmony_ci	union ibmvfc_iu rsp_iu;
18138c2ecf20Sopenharmony_ci	unsigned long flags;
18148c2ecf20Sopenharmony_ci	int rc = 0, issue_login = 1;
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	ENTER;
18178c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
18188c2ecf20Sopenharmony_ci	list_for_each_entry(tgt, &vhost->targets, queue) {
18198c2ecf20Sopenharmony_ci		if (tgt->scsi_id == port_id) {
18208c2ecf20Sopenharmony_ci			issue_login = 0;
18218c2ecf20Sopenharmony_ci			break;
18228c2ecf20Sopenharmony_ci		}
18238c2ecf20Sopenharmony_ci	}
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_ci	if (!issue_login)
18268c2ecf20Sopenharmony_ci		goto unlock_out;
18278c2ecf20Sopenharmony_ci	if (unlikely((rc = ibmvfc_host_chkready(vhost))))
18288c2ecf20Sopenharmony_ci		goto unlock_out;
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
18318c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
18328c2ecf20Sopenharmony_ci	plogi = &evt->iu.plogi;
18338c2ecf20Sopenharmony_ci	memset(plogi, 0, sizeof(*plogi));
18348c2ecf20Sopenharmony_ci	plogi->common.version = cpu_to_be32(1);
18358c2ecf20Sopenharmony_ci	plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
18368c2ecf20Sopenharmony_ci	plogi->common.length = cpu_to_be16(sizeof(*plogi));
18378c2ecf20Sopenharmony_ci	plogi->scsi_id = cpu_to_be64(port_id);
18388c2ecf20Sopenharmony_ci	evt->sync_iu = &rsp_iu;
18398c2ecf20Sopenharmony_ci	init_completion(&evt->comp);
18408c2ecf20Sopenharmony_ci
18418c2ecf20Sopenharmony_ci	rc = ibmvfc_send_event(evt, vhost, default_timeout);
18428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ci	if (rc)
18458c2ecf20Sopenharmony_ci		return -EIO;
18468c2ecf20Sopenharmony_ci
18478c2ecf20Sopenharmony_ci	wait_for_completion(&evt->comp);
18488c2ecf20Sopenharmony_ci
18498c2ecf20Sopenharmony_ci	if (rsp_iu.plogi.common.status)
18508c2ecf20Sopenharmony_ci		rc = -EIO;
18518c2ecf20Sopenharmony_ci
18528c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
18538c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
18548c2ecf20Sopenharmony_ciunlock_out:
18558c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
18568c2ecf20Sopenharmony_ci	LEAVE;
18578c2ecf20Sopenharmony_ci	return rc;
18588c2ecf20Sopenharmony_ci}
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci/**
18618c2ecf20Sopenharmony_ci * ibmvfc_bsg_request - Handle a BSG request
18628c2ecf20Sopenharmony_ci * @job:	struct bsg_job to be executed
18638c2ecf20Sopenharmony_ci *
18648c2ecf20Sopenharmony_ci * Returns:
18658c2ecf20Sopenharmony_ci *	0 on success / other on failure
18668c2ecf20Sopenharmony_ci **/
18678c2ecf20Sopenharmony_cistatic int ibmvfc_bsg_request(struct bsg_job *job)
18688c2ecf20Sopenharmony_ci{
18698c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
18708c2ecf20Sopenharmony_ci	struct fc_rport *rport = fc_bsg_to_rport(job);
18718c2ecf20Sopenharmony_ci	struct ibmvfc_passthru_mad *mad;
18728c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
18738c2ecf20Sopenharmony_ci	union ibmvfc_iu rsp_iu;
18748c2ecf20Sopenharmony_ci	unsigned long flags, port_id = -1;
18758c2ecf20Sopenharmony_ci	struct fc_bsg_request *bsg_request = job->request;
18768c2ecf20Sopenharmony_ci	struct fc_bsg_reply *bsg_reply = job->reply;
18778c2ecf20Sopenharmony_ci	unsigned int code = bsg_request->msgcode;
18788c2ecf20Sopenharmony_ci	int rc = 0, req_seg, rsp_seg, issue_login = 0;
18798c2ecf20Sopenharmony_ci	u32 fc_flags, rsp_len;
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_ci	ENTER;
18828c2ecf20Sopenharmony_ci	bsg_reply->reply_payload_rcv_len = 0;
18838c2ecf20Sopenharmony_ci	if (rport)
18848c2ecf20Sopenharmony_ci		port_id = rport->port_id;
18858c2ecf20Sopenharmony_ci
18868c2ecf20Sopenharmony_ci	switch (code) {
18878c2ecf20Sopenharmony_ci	case FC_BSG_HST_ELS_NOLOGIN:
18888c2ecf20Sopenharmony_ci		port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
18898c2ecf20Sopenharmony_ci			(bsg_request->rqst_data.h_els.port_id[1] << 8) |
18908c2ecf20Sopenharmony_ci			bsg_request->rqst_data.h_els.port_id[2];
18918c2ecf20Sopenharmony_ci		fallthrough;
18928c2ecf20Sopenharmony_ci	case FC_BSG_RPT_ELS:
18938c2ecf20Sopenharmony_ci		fc_flags = IBMVFC_FC_ELS;
18948c2ecf20Sopenharmony_ci		break;
18958c2ecf20Sopenharmony_ci	case FC_BSG_HST_CT:
18968c2ecf20Sopenharmony_ci		issue_login = 1;
18978c2ecf20Sopenharmony_ci		port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
18988c2ecf20Sopenharmony_ci			(bsg_request->rqst_data.h_ct.port_id[1] << 8) |
18998c2ecf20Sopenharmony_ci			bsg_request->rqst_data.h_ct.port_id[2];
19008c2ecf20Sopenharmony_ci		fallthrough;
19018c2ecf20Sopenharmony_ci	case FC_BSG_RPT_CT:
19028c2ecf20Sopenharmony_ci		fc_flags = IBMVFC_FC_CT_IU;
19038c2ecf20Sopenharmony_ci		break;
19048c2ecf20Sopenharmony_ci	default:
19058c2ecf20Sopenharmony_ci		return -ENOTSUPP;
19068c2ecf20Sopenharmony_ci	}
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_ci	if (port_id == -1)
19098c2ecf20Sopenharmony_ci		return -EINVAL;
19108c2ecf20Sopenharmony_ci	if (!mutex_trylock(&vhost->passthru_mutex))
19118c2ecf20Sopenharmony_ci		return -EBUSY;
19128c2ecf20Sopenharmony_ci
19138c2ecf20Sopenharmony_ci	job->dd_data = (void *)port_id;
19148c2ecf20Sopenharmony_ci	req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
19158c2ecf20Sopenharmony_ci			     job->request_payload.sg_cnt, DMA_TO_DEVICE);
19168c2ecf20Sopenharmony_ci
19178c2ecf20Sopenharmony_ci	if (!req_seg) {
19188c2ecf20Sopenharmony_ci		mutex_unlock(&vhost->passthru_mutex);
19198c2ecf20Sopenharmony_ci		return -ENOMEM;
19208c2ecf20Sopenharmony_ci	}
19218c2ecf20Sopenharmony_ci
19228c2ecf20Sopenharmony_ci	rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
19238c2ecf20Sopenharmony_ci			     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
19248c2ecf20Sopenharmony_ci
19258c2ecf20Sopenharmony_ci	if (!rsp_seg) {
19268c2ecf20Sopenharmony_ci		dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
19278c2ecf20Sopenharmony_ci			     job->request_payload.sg_cnt, DMA_TO_DEVICE);
19288c2ecf20Sopenharmony_ci		mutex_unlock(&vhost->passthru_mutex);
19298c2ecf20Sopenharmony_ci		return -ENOMEM;
19308c2ecf20Sopenharmony_ci	}
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_ci	if (req_seg > 1 || rsp_seg > 1) {
19338c2ecf20Sopenharmony_ci		rc = -EINVAL;
19348c2ecf20Sopenharmony_ci		goto out;
19358c2ecf20Sopenharmony_ci	}
19368c2ecf20Sopenharmony_ci
19378c2ecf20Sopenharmony_ci	if (issue_login)
19388c2ecf20Sopenharmony_ci		rc = ibmvfc_bsg_plogi(vhost, port_id);
19398c2ecf20Sopenharmony_ci
19408c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
19418c2ecf20Sopenharmony_ci
19428c2ecf20Sopenharmony_ci	if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
19438c2ecf20Sopenharmony_ci	    unlikely((rc = ibmvfc_host_chkready(vhost)))) {
19448c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
19458c2ecf20Sopenharmony_ci		goto out;
19468c2ecf20Sopenharmony_ci	}
19478c2ecf20Sopenharmony_ci
19488c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
19498c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
19508c2ecf20Sopenharmony_ci	mad = &evt->iu.passthru;
19518c2ecf20Sopenharmony_ci
19528c2ecf20Sopenharmony_ci	memset(mad, 0, sizeof(*mad));
19538c2ecf20Sopenharmony_ci	mad->common.version = cpu_to_be32(1);
19548c2ecf20Sopenharmony_ci	mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
19558c2ecf20Sopenharmony_ci	mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
19568c2ecf20Sopenharmony_ci
19578c2ecf20Sopenharmony_ci	mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
19588c2ecf20Sopenharmony_ci		offsetof(struct ibmvfc_passthru_mad, iu));
19598c2ecf20Sopenharmony_ci	mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
19608c2ecf20Sopenharmony_ci
19618c2ecf20Sopenharmony_ci	mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
19628c2ecf20Sopenharmony_ci	mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
19638c2ecf20Sopenharmony_ci	mad->iu.flags = cpu_to_be32(fc_flags);
19648c2ecf20Sopenharmony_ci	mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
19658c2ecf20Sopenharmony_ci
19668c2ecf20Sopenharmony_ci	mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
19678c2ecf20Sopenharmony_ci	mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
19688c2ecf20Sopenharmony_ci	mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
19698c2ecf20Sopenharmony_ci	mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
19708c2ecf20Sopenharmony_ci	mad->iu.scsi_id = cpu_to_be64(port_id);
19718c2ecf20Sopenharmony_ci	mad->iu.tag = cpu_to_be64((u64)evt);
19728c2ecf20Sopenharmony_ci	rsp_len = be32_to_cpu(mad->iu.rsp.len);
19738c2ecf20Sopenharmony_ci
19748c2ecf20Sopenharmony_ci	evt->sync_iu = &rsp_iu;
19758c2ecf20Sopenharmony_ci	init_completion(&evt->comp);
19768c2ecf20Sopenharmony_ci	rc = ibmvfc_send_event(evt, vhost, 0);
19778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
19788c2ecf20Sopenharmony_ci
19798c2ecf20Sopenharmony_ci	if (rc) {
19808c2ecf20Sopenharmony_ci		rc = -EIO;
19818c2ecf20Sopenharmony_ci		goto out;
19828c2ecf20Sopenharmony_ci	}
19838c2ecf20Sopenharmony_ci
19848c2ecf20Sopenharmony_ci	wait_for_completion(&evt->comp);
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ci	if (rsp_iu.passthru.common.status)
19878c2ecf20Sopenharmony_ci		rc = -EIO;
19888c2ecf20Sopenharmony_ci	else
19898c2ecf20Sopenharmony_ci		bsg_reply->reply_payload_rcv_len = rsp_len;
19908c2ecf20Sopenharmony_ci
19918c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
19928c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
19938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
19948c2ecf20Sopenharmony_ci	bsg_reply->result = rc;
19958c2ecf20Sopenharmony_ci	bsg_job_done(job, bsg_reply->result,
19968c2ecf20Sopenharmony_ci		       bsg_reply->reply_payload_rcv_len);
19978c2ecf20Sopenharmony_ci	rc = 0;
19988c2ecf20Sopenharmony_ciout:
19998c2ecf20Sopenharmony_ci	dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
20008c2ecf20Sopenharmony_ci		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
20018c2ecf20Sopenharmony_ci	dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
20028c2ecf20Sopenharmony_ci		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
20038c2ecf20Sopenharmony_ci	mutex_unlock(&vhost->passthru_mutex);
20048c2ecf20Sopenharmony_ci	LEAVE;
20058c2ecf20Sopenharmony_ci	return rc;
20068c2ecf20Sopenharmony_ci}
20078c2ecf20Sopenharmony_ci
20088c2ecf20Sopenharmony_ci/**
20098c2ecf20Sopenharmony_ci * ibmvfc_reset_device - Reset the device with the specified reset type
20108c2ecf20Sopenharmony_ci * @sdev:	scsi device to reset
20118c2ecf20Sopenharmony_ci * @type:	reset type
20128c2ecf20Sopenharmony_ci * @desc:	reset type description for log messages
20138c2ecf20Sopenharmony_ci *
20148c2ecf20Sopenharmony_ci * Returns:
20158c2ecf20Sopenharmony_ci *	0 on success / other on failure
20168c2ecf20Sopenharmony_ci **/
20178c2ecf20Sopenharmony_cistatic int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
20188c2ecf20Sopenharmony_ci{
20198c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(sdev->host);
20208c2ecf20Sopenharmony_ci	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
20218c2ecf20Sopenharmony_ci	struct ibmvfc_cmd *tmf;
20228c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt = NULL;
20238c2ecf20Sopenharmony_ci	union ibmvfc_iu rsp_iu;
20248c2ecf20Sopenharmony_ci	struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
20258c2ecf20Sopenharmony_ci	int rsp_rc = -EBUSY;
20268c2ecf20Sopenharmony_ci	unsigned long flags;
20278c2ecf20Sopenharmony_ci	int rsp_code = 0;
20288c2ecf20Sopenharmony_ci
20298c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
20308c2ecf20Sopenharmony_ci	if (vhost->state == IBMVFC_ACTIVE) {
20318c2ecf20Sopenharmony_ci		evt = ibmvfc_get_event(vhost);
20328c2ecf20Sopenharmony_ci		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
20338c2ecf20Sopenharmony_ci
20348c2ecf20Sopenharmony_ci		tmf = &evt->iu.cmd;
20358c2ecf20Sopenharmony_ci		memset(tmf, 0, sizeof(*tmf));
20368c2ecf20Sopenharmony_ci		tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
20378c2ecf20Sopenharmony_ci		tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
20388c2ecf20Sopenharmony_ci		tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
20398c2ecf20Sopenharmony_ci		tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
20408c2ecf20Sopenharmony_ci		tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
20418c2ecf20Sopenharmony_ci		tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
20428c2ecf20Sopenharmony_ci		tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
20438c2ecf20Sopenharmony_ci		int_to_scsilun(sdev->lun, &tmf->iu.lun);
20448c2ecf20Sopenharmony_ci		tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
20458c2ecf20Sopenharmony_ci		tmf->iu.tmf_flags = type;
20468c2ecf20Sopenharmony_ci		evt->sync_iu = &rsp_iu;
20478c2ecf20Sopenharmony_ci
20488c2ecf20Sopenharmony_ci		init_completion(&evt->comp);
20498c2ecf20Sopenharmony_ci		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
20508c2ecf20Sopenharmony_ci	}
20518c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
20528c2ecf20Sopenharmony_ci
20538c2ecf20Sopenharmony_ci	if (rsp_rc != 0) {
20548c2ecf20Sopenharmony_ci		sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
20558c2ecf20Sopenharmony_ci			    desc, rsp_rc);
20568c2ecf20Sopenharmony_ci		return -EIO;
20578c2ecf20Sopenharmony_ci	}
20588c2ecf20Sopenharmony_ci
20598c2ecf20Sopenharmony_ci	sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
20608c2ecf20Sopenharmony_ci	wait_for_completion(&evt->comp);
20618c2ecf20Sopenharmony_ci
20628c2ecf20Sopenharmony_ci	if (rsp_iu.cmd.status)
20638c2ecf20Sopenharmony_ci		rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
20648c2ecf20Sopenharmony_ci
20658c2ecf20Sopenharmony_ci	if (rsp_code) {
20668c2ecf20Sopenharmony_ci		if (fc_rsp->flags & FCP_RSP_LEN_VALID)
20678c2ecf20Sopenharmony_ci			rsp_code = fc_rsp->data.info.rsp_code;
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_ci		sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
20708c2ecf20Sopenharmony_ci			    "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
20718c2ecf20Sopenharmony_ci			    ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
20728c2ecf20Sopenharmony_ci			    be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
20738c2ecf20Sopenharmony_ci			    fc_rsp->scsi_status);
20748c2ecf20Sopenharmony_ci		rsp_rc = -EIO;
20758c2ecf20Sopenharmony_ci	} else
20768c2ecf20Sopenharmony_ci		sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
20778c2ecf20Sopenharmony_ci
20788c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
20798c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
20808c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
20818c2ecf20Sopenharmony_ci	return rsp_rc;
20828c2ecf20Sopenharmony_ci}
20838c2ecf20Sopenharmony_ci
20848c2ecf20Sopenharmony_ci/**
20858c2ecf20Sopenharmony_ci * ibmvfc_match_rport - Match function for specified remote port
20868c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
20878c2ecf20Sopenharmony_ci * @device:	device to match (rport)
20888c2ecf20Sopenharmony_ci *
20898c2ecf20Sopenharmony_ci * Returns:
20908c2ecf20Sopenharmony_ci *	1 if event matches rport / 0 if event does not match rport
20918c2ecf20Sopenharmony_ci **/
20928c2ecf20Sopenharmony_cistatic int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
20938c2ecf20Sopenharmony_ci{
20948c2ecf20Sopenharmony_ci	struct fc_rport *cmd_rport;
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_ci	if (evt->cmnd) {
20978c2ecf20Sopenharmony_ci		cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
20988c2ecf20Sopenharmony_ci		if (cmd_rport == rport)
20998c2ecf20Sopenharmony_ci			return 1;
21008c2ecf20Sopenharmony_ci	}
21018c2ecf20Sopenharmony_ci	return 0;
21028c2ecf20Sopenharmony_ci}
21038c2ecf20Sopenharmony_ci
21048c2ecf20Sopenharmony_ci/**
21058c2ecf20Sopenharmony_ci * ibmvfc_match_target - Match function for specified target
21068c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
21078c2ecf20Sopenharmony_ci * @device:	device to match (starget)
21088c2ecf20Sopenharmony_ci *
21098c2ecf20Sopenharmony_ci * Returns:
21108c2ecf20Sopenharmony_ci *	1 if event matches starget / 0 if event does not match starget
21118c2ecf20Sopenharmony_ci **/
21128c2ecf20Sopenharmony_cistatic int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
21138c2ecf20Sopenharmony_ci{
21148c2ecf20Sopenharmony_ci	if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
21158c2ecf20Sopenharmony_ci		return 1;
21168c2ecf20Sopenharmony_ci	return 0;
21178c2ecf20Sopenharmony_ci}
21188c2ecf20Sopenharmony_ci
21198c2ecf20Sopenharmony_ci/**
21208c2ecf20Sopenharmony_ci * ibmvfc_match_lun - Match function for specified LUN
21218c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
21228c2ecf20Sopenharmony_ci * @device:	device to match (sdev)
21238c2ecf20Sopenharmony_ci *
21248c2ecf20Sopenharmony_ci * Returns:
21258c2ecf20Sopenharmony_ci *	1 if event matches sdev / 0 if event does not match sdev
21268c2ecf20Sopenharmony_ci **/
21278c2ecf20Sopenharmony_cistatic int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
21288c2ecf20Sopenharmony_ci{
21298c2ecf20Sopenharmony_ci	if (evt->cmnd && evt->cmnd->device == device)
21308c2ecf20Sopenharmony_ci		return 1;
21318c2ecf20Sopenharmony_ci	return 0;
21328c2ecf20Sopenharmony_ci}
21338c2ecf20Sopenharmony_ci
21348c2ecf20Sopenharmony_ci/**
21358c2ecf20Sopenharmony_ci * ibmvfc_wait_for_ops - Wait for ops to complete
21368c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
21378c2ecf20Sopenharmony_ci * @device:	device to match (starget or sdev)
21388c2ecf20Sopenharmony_ci * @match:	match function
21398c2ecf20Sopenharmony_ci *
21408c2ecf20Sopenharmony_ci * Returns:
21418c2ecf20Sopenharmony_ci *	SUCCESS / FAILED
21428c2ecf20Sopenharmony_ci **/
21438c2ecf20Sopenharmony_cistatic int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
21448c2ecf20Sopenharmony_ci			       int (*match) (struct ibmvfc_event *, void *))
21458c2ecf20Sopenharmony_ci{
21468c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
21478c2ecf20Sopenharmony_ci	DECLARE_COMPLETION_ONSTACK(comp);
21488c2ecf20Sopenharmony_ci	int wait;
21498c2ecf20Sopenharmony_ci	unsigned long flags;
21508c2ecf20Sopenharmony_ci	signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
21518c2ecf20Sopenharmony_ci
21528c2ecf20Sopenharmony_ci	ENTER;
21538c2ecf20Sopenharmony_ci	do {
21548c2ecf20Sopenharmony_ci		wait = 0;
21558c2ecf20Sopenharmony_ci		spin_lock_irqsave(vhost->host->host_lock, flags);
21568c2ecf20Sopenharmony_ci		list_for_each_entry(evt, &vhost->sent, queue) {
21578c2ecf20Sopenharmony_ci			if (match(evt, device)) {
21588c2ecf20Sopenharmony_ci				evt->eh_comp = &comp;
21598c2ecf20Sopenharmony_ci				wait++;
21608c2ecf20Sopenharmony_ci			}
21618c2ecf20Sopenharmony_ci		}
21628c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
21638c2ecf20Sopenharmony_ci
21648c2ecf20Sopenharmony_ci		if (wait) {
21658c2ecf20Sopenharmony_ci			timeout = wait_for_completion_timeout(&comp, timeout);
21668c2ecf20Sopenharmony_ci
21678c2ecf20Sopenharmony_ci			if (!timeout) {
21688c2ecf20Sopenharmony_ci				wait = 0;
21698c2ecf20Sopenharmony_ci				spin_lock_irqsave(vhost->host->host_lock, flags);
21708c2ecf20Sopenharmony_ci				list_for_each_entry(evt, &vhost->sent, queue) {
21718c2ecf20Sopenharmony_ci					if (match(evt, device)) {
21728c2ecf20Sopenharmony_ci						evt->eh_comp = NULL;
21738c2ecf20Sopenharmony_ci						wait++;
21748c2ecf20Sopenharmony_ci					}
21758c2ecf20Sopenharmony_ci				}
21768c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(vhost->host->host_lock, flags);
21778c2ecf20Sopenharmony_ci				if (wait)
21788c2ecf20Sopenharmony_ci					dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
21798c2ecf20Sopenharmony_ci				LEAVE;
21808c2ecf20Sopenharmony_ci				return wait ? FAILED : SUCCESS;
21818c2ecf20Sopenharmony_ci			}
21828c2ecf20Sopenharmony_ci		}
21838c2ecf20Sopenharmony_ci	} while (wait);
21848c2ecf20Sopenharmony_ci
21858c2ecf20Sopenharmony_ci	LEAVE;
21868c2ecf20Sopenharmony_ci	return SUCCESS;
21878c2ecf20Sopenharmony_ci}
21888c2ecf20Sopenharmony_ci
21898c2ecf20Sopenharmony_ci/**
21908c2ecf20Sopenharmony_ci * ibmvfc_cancel_all - Cancel all outstanding commands to the device
21918c2ecf20Sopenharmony_ci * @sdev:	scsi device to cancel commands
21928c2ecf20Sopenharmony_ci * @type:	type of error recovery being performed
21938c2ecf20Sopenharmony_ci *
21948c2ecf20Sopenharmony_ci * This sends a cancel to the VIOS for the specified device. This does
21958c2ecf20Sopenharmony_ci * NOT send any abort to the actual device. That must be done separately.
21968c2ecf20Sopenharmony_ci *
21978c2ecf20Sopenharmony_ci * Returns:
21988c2ecf20Sopenharmony_ci *	0 on success / other on failure
21998c2ecf20Sopenharmony_ci **/
22008c2ecf20Sopenharmony_cistatic int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
22018c2ecf20Sopenharmony_ci{
22028c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(sdev->host);
22038c2ecf20Sopenharmony_ci	struct scsi_target *starget = scsi_target(sdev);
22048c2ecf20Sopenharmony_ci	struct fc_rport *rport = starget_to_rport(starget);
22058c2ecf20Sopenharmony_ci	struct ibmvfc_tmf *tmf;
22068c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt, *found_evt;
22078c2ecf20Sopenharmony_ci	union ibmvfc_iu rsp;
22088c2ecf20Sopenharmony_ci	int rsp_rc = -EBUSY;
22098c2ecf20Sopenharmony_ci	unsigned long flags;
22108c2ecf20Sopenharmony_ci	u16 status;
22118c2ecf20Sopenharmony_ci
22128c2ecf20Sopenharmony_ci	ENTER;
22138c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
22148c2ecf20Sopenharmony_ci	found_evt = NULL;
22158c2ecf20Sopenharmony_ci	list_for_each_entry(evt, &vhost->sent, queue) {
22168c2ecf20Sopenharmony_ci		if (evt->cmnd && evt->cmnd->device == sdev) {
22178c2ecf20Sopenharmony_ci			found_evt = evt;
22188c2ecf20Sopenharmony_ci			break;
22198c2ecf20Sopenharmony_ci		}
22208c2ecf20Sopenharmony_ci	}
22218c2ecf20Sopenharmony_ci
22228c2ecf20Sopenharmony_ci	if (!found_evt) {
22238c2ecf20Sopenharmony_ci		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
22248c2ecf20Sopenharmony_ci			sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
22258c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
22268c2ecf20Sopenharmony_ci		return 0;
22278c2ecf20Sopenharmony_ci	}
22288c2ecf20Sopenharmony_ci
22298c2ecf20Sopenharmony_ci	if (vhost->logged_in) {
22308c2ecf20Sopenharmony_ci		evt = ibmvfc_get_event(vhost);
22318c2ecf20Sopenharmony_ci		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
22328c2ecf20Sopenharmony_ci
22338c2ecf20Sopenharmony_ci		tmf = &evt->iu.tmf;
22348c2ecf20Sopenharmony_ci		memset(tmf, 0, sizeof(*tmf));
22358c2ecf20Sopenharmony_ci		tmf->common.version = cpu_to_be32(1);
22368c2ecf20Sopenharmony_ci		tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
22378c2ecf20Sopenharmony_ci		tmf->common.length = cpu_to_be16(sizeof(*tmf));
22388c2ecf20Sopenharmony_ci		tmf->scsi_id = cpu_to_be64(rport->port_id);
22398c2ecf20Sopenharmony_ci		int_to_scsilun(sdev->lun, &tmf->lun);
22408c2ecf20Sopenharmony_ci		if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
22418c2ecf20Sopenharmony_ci			type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
22428c2ecf20Sopenharmony_ci		if (vhost->state == IBMVFC_ACTIVE)
22438c2ecf20Sopenharmony_ci			tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
22448c2ecf20Sopenharmony_ci		else
22458c2ecf20Sopenharmony_ci			tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
22468c2ecf20Sopenharmony_ci		tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
22478c2ecf20Sopenharmony_ci		tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
22488c2ecf20Sopenharmony_ci
22498c2ecf20Sopenharmony_ci		evt->sync_iu = &rsp;
22508c2ecf20Sopenharmony_ci		init_completion(&evt->comp);
22518c2ecf20Sopenharmony_ci		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
22528c2ecf20Sopenharmony_ci	}
22538c2ecf20Sopenharmony_ci
22548c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
22558c2ecf20Sopenharmony_ci
22568c2ecf20Sopenharmony_ci	if (rsp_rc != 0) {
22578c2ecf20Sopenharmony_ci		sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
22588c2ecf20Sopenharmony_ci		/* If failure is received, the host adapter is most likely going
22598c2ecf20Sopenharmony_ci		 through reset, return success so the caller will wait for the command
22608c2ecf20Sopenharmony_ci		 being cancelled to get returned */
22618c2ecf20Sopenharmony_ci		return 0;
22628c2ecf20Sopenharmony_ci	}
22638c2ecf20Sopenharmony_ci
22648c2ecf20Sopenharmony_ci	sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
22658c2ecf20Sopenharmony_ci
22668c2ecf20Sopenharmony_ci	wait_for_completion(&evt->comp);
22678c2ecf20Sopenharmony_ci	status = be16_to_cpu(rsp.mad_common.status);
22688c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
22698c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
22708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
22718c2ecf20Sopenharmony_ci
22728c2ecf20Sopenharmony_ci	if (status != IBMVFC_MAD_SUCCESS) {
22738c2ecf20Sopenharmony_ci		sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
22748c2ecf20Sopenharmony_ci		switch (status) {
22758c2ecf20Sopenharmony_ci		case IBMVFC_MAD_DRIVER_FAILED:
22768c2ecf20Sopenharmony_ci		case IBMVFC_MAD_CRQ_ERROR:
22778c2ecf20Sopenharmony_ci			/* Host adapter most likely going through reset, return success to
22788c2ecf20Sopenharmony_ci			 the caller will wait for the command being cancelled to get returned */
22798c2ecf20Sopenharmony_ci			return 0;
22808c2ecf20Sopenharmony_ci		default:
22818c2ecf20Sopenharmony_ci			return -EIO;
22828c2ecf20Sopenharmony_ci		};
22838c2ecf20Sopenharmony_ci	}
22848c2ecf20Sopenharmony_ci
22858c2ecf20Sopenharmony_ci	sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
22868c2ecf20Sopenharmony_ci	return 0;
22878c2ecf20Sopenharmony_ci}
22888c2ecf20Sopenharmony_ci
22898c2ecf20Sopenharmony_ci/**
22908c2ecf20Sopenharmony_ci * ibmvfc_match_key - Match function for specified cancel key
22918c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
22928c2ecf20Sopenharmony_ci * @key:	cancel key to match
22938c2ecf20Sopenharmony_ci *
22948c2ecf20Sopenharmony_ci * Returns:
22958c2ecf20Sopenharmony_ci *	1 if event matches key / 0 if event does not match key
22968c2ecf20Sopenharmony_ci **/
22978c2ecf20Sopenharmony_cistatic int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
22988c2ecf20Sopenharmony_ci{
22998c2ecf20Sopenharmony_ci	unsigned long cancel_key = (unsigned long)key;
23008c2ecf20Sopenharmony_ci
23018c2ecf20Sopenharmony_ci	if (evt->crq.format == IBMVFC_CMD_FORMAT &&
23028c2ecf20Sopenharmony_ci	    be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
23038c2ecf20Sopenharmony_ci		return 1;
23048c2ecf20Sopenharmony_ci	return 0;
23058c2ecf20Sopenharmony_ci}
23068c2ecf20Sopenharmony_ci
23078c2ecf20Sopenharmony_ci/**
23088c2ecf20Sopenharmony_ci * ibmvfc_match_evt - Match function for specified event
23098c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
23108c2ecf20Sopenharmony_ci * @match:	event to match
23118c2ecf20Sopenharmony_ci *
23128c2ecf20Sopenharmony_ci * Returns:
23138c2ecf20Sopenharmony_ci *	1 if event matches key / 0 if event does not match key
23148c2ecf20Sopenharmony_ci **/
23158c2ecf20Sopenharmony_cistatic int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
23168c2ecf20Sopenharmony_ci{
23178c2ecf20Sopenharmony_ci	if (evt == match)
23188c2ecf20Sopenharmony_ci		return 1;
23198c2ecf20Sopenharmony_ci	return 0;
23208c2ecf20Sopenharmony_ci}
23218c2ecf20Sopenharmony_ci
23228c2ecf20Sopenharmony_ci/**
23238c2ecf20Sopenharmony_ci * ibmvfc_abort_task_set - Abort outstanding commands to the device
23248c2ecf20Sopenharmony_ci * @sdev:	scsi device to abort commands
23258c2ecf20Sopenharmony_ci *
23268c2ecf20Sopenharmony_ci * This sends an Abort Task Set to the VIOS for the specified device. This does
23278c2ecf20Sopenharmony_ci * NOT send any cancel to the VIOS. That must be done separately.
23288c2ecf20Sopenharmony_ci *
23298c2ecf20Sopenharmony_ci * Returns:
23308c2ecf20Sopenharmony_ci *	0 on success / other on failure
23318c2ecf20Sopenharmony_ci **/
23328c2ecf20Sopenharmony_cistatic int ibmvfc_abort_task_set(struct scsi_device *sdev)
23338c2ecf20Sopenharmony_ci{
23348c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(sdev->host);
23358c2ecf20Sopenharmony_ci	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
23368c2ecf20Sopenharmony_ci	struct ibmvfc_cmd *tmf;
23378c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt, *found_evt;
23388c2ecf20Sopenharmony_ci	union ibmvfc_iu rsp_iu;
23398c2ecf20Sopenharmony_ci	struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
23408c2ecf20Sopenharmony_ci	int rc, rsp_rc = -EBUSY;
23418c2ecf20Sopenharmony_ci	unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
23428c2ecf20Sopenharmony_ci	int rsp_code = 0;
23438c2ecf20Sopenharmony_ci
23448c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
23458c2ecf20Sopenharmony_ci	found_evt = NULL;
23468c2ecf20Sopenharmony_ci	list_for_each_entry(evt, &vhost->sent, queue) {
23478c2ecf20Sopenharmony_ci		if (evt->cmnd && evt->cmnd->device == sdev) {
23488c2ecf20Sopenharmony_ci			found_evt = evt;
23498c2ecf20Sopenharmony_ci			break;
23508c2ecf20Sopenharmony_ci		}
23518c2ecf20Sopenharmony_ci	}
23528c2ecf20Sopenharmony_ci
23538c2ecf20Sopenharmony_ci	if (!found_evt) {
23548c2ecf20Sopenharmony_ci		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
23558c2ecf20Sopenharmony_ci			sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
23568c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
23578c2ecf20Sopenharmony_ci		return 0;
23588c2ecf20Sopenharmony_ci	}
23598c2ecf20Sopenharmony_ci
23608c2ecf20Sopenharmony_ci	if (vhost->state == IBMVFC_ACTIVE) {
23618c2ecf20Sopenharmony_ci		evt = ibmvfc_get_event(vhost);
23628c2ecf20Sopenharmony_ci		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
23638c2ecf20Sopenharmony_ci
23648c2ecf20Sopenharmony_ci		tmf = &evt->iu.cmd;
23658c2ecf20Sopenharmony_ci		memset(tmf, 0, sizeof(*tmf));
23668c2ecf20Sopenharmony_ci		tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
23678c2ecf20Sopenharmony_ci		tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
23688c2ecf20Sopenharmony_ci		tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
23698c2ecf20Sopenharmony_ci		tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
23708c2ecf20Sopenharmony_ci		tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
23718c2ecf20Sopenharmony_ci		tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
23728c2ecf20Sopenharmony_ci		tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
23738c2ecf20Sopenharmony_ci		int_to_scsilun(sdev->lun, &tmf->iu.lun);
23748c2ecf20Sopenharmony_ci		tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
23758c2ecf20Sopenharmony_ci		tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
23768c2ecf20Sopenharmony_ci		evt->sync_iu = &rsp_iu;
23778c2ecf20Sopenharmony_ci
23788c2ecf20Sopenharmony_ci		init_completion(&evt->comp);
23798c2ecf20Sopenharmony_ci		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
23808c2ecf20Sopenharmony_ci	}
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
23838c2ecf20Sopenharmony_ci
23848c2ecf20Sopenharmony_ci	if (rsp_rc != 0) {
23858c2ecf20Sopenharmony_ci		sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
23868c2ecf20Sopenharmony_ci		return -EIO;
23878c2ecf20Sopenharmony_ci	}
23888c2ecf20Sopenharmony_ci
23898c2ecf20Sopenharmony_ci	sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
23908c2ecf20Sopenharmony_ci	timeout = wait_for_completion_timeout(&evt->comp, timeout);
23918c2ecf20Sopenharmony_ci
23928c2ecf20Sopenharmony_ci	if (!timeout) {
23938c2ecf20Sopenharmony_ci		rc = ibmvfc_cancel_all(sdev, 0);
23948c2ecf20Sopenharmony_ci		if (!rc) {
23958c2ecf20Sopenharmony_ci			rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
23968c2ecf20Sopenharmony_ci			if (rc == SUCCESS)
23978c2ecf20Sopenharmony_ci				rc = 0;
23988c2ecf20Sopenharmony_ci		}
23998c2ecf20Sopenharmony_ci
24008c2ecf20Sopenharmony_ci		if (rc) {
24018c2ecf20Sopenharmony_ci			sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
24028c2ecf20Sopenharmony_ci			ibmvfc_reset_host(vhost);
24038c2ecf20Sopenharmony_ci			rsp_rc = -EIO;
24048c2ecf20Sopenharmony_ci			rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
24058c2ecf20Sopenharmony_ci
24068c2ecf20Sopenharmony_ci			if (rc == SUCCESS)
24078c2ecf20Sopenharmony_ci				rsp_rc = 0;
24088c2ecf20Sopenharmony_ci
24098c2ecf20Sopenharmony_ci			rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
24108c2ecf20Sopenharmony_ci			if (rc != SUCCESS) {
24118c2ecf20Sopenharmony_ci				spin_lock_irqsave(vhost->host->host_lock, flags);
24128c2ecf20Sopenharmony_ci				ibmvfc_hard_reset_host(vhost);
24138c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(vhost->host->host_lock, flags);
24148c2ecf20Sopenharmony_ci				rsp_rc = 0;
24158c2ecf20Sopenharmony_ci			}
24168c2ecf20Sopenharmony_ci
24178c2ecf20Sopenharmony_ci			goto out;
24188c2ecf20Sopenharmony_ci		}
24198c2ecf20Sopenharmony_ci	}
24208c2ecf20Sopenharmony_ci
24218c2ecf20Sopenharmony_ci	if (rsp_iu.cmd.status)
24228c2ecf20Sopenharmony_ci		rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
24238c2ecf20Sopenharmony_ci
24248c2ecf20Sopenharmony_ci	if (rsp_code) {
24258c2ecf20Sopenharmony_ci		if (fc_rsp->flags & FCP_RSP_LEN_VALID)
24268c2ecf20Sopenharmony_ci			rsp_code = fc_rsp->data.info.rsp_code;
24278c2ecf20Sopenharmony_ci
24288c2ecf20Sopenharmony_ci		sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
24298c2ecf20Sopenharmony_ci			    "flags: %x fcp_rsp: %x, scsi_status: %x\n",
24308c2ecf20Sopenharmony_ci			    ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
24318c2ecf20Sopenharmony_ci			    be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
24328c2ecf20Sopenharmony_ci			    fc_rsp->scsi_status);
24338c2ecf20Sopenharmony_ci		rsp_rc = -EIO;
24348c2ecf20Sopenharmony_ci	} else
24358c2ecf20Sopenharmony_ci		sdev_printk(KERN_INFO, sdev, "Abort successful\n");
24368c2ecf20Sopenharmony_ci
24378c2ecf20Sopenharmony_ciout:
24388c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
24398c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
24408c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
24418c2ecf20Sopenharmony_ci	return rsp_rc;
24428c2ecf20Sopenharmony_ci}
24438c2ecf20Sopenharmony_ci
24448c2ecf20Sopenharmony_ci/**
24458c2ecf20Sopenharmony_ci * ibmvfc_eh_abort_handler - Abort a command
24468c2ecf20Sopenharmony_ci * @cmd:	scsi command to abort
24478c2ecf20Sopenharmony_ci *
24488c2ecf20Sopenharmony_ci * Returns:
24498c2ecf20Sopenharmony_ci *	SUCCESS / FAST_IO_FAIL / FAILED
24508c2ecf20Sopenharmony_ci **/
24518c2ecf20Sopenharmony_cistatic int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
24528c2ecf20Sopenharmony_ci{
24538c2ecf20Sopenharmony_ci	struct scsi_device *sdev = cmd->device;
24548c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(sdev->host);
24558c2ecf20Sopenharmony_ci	int cancel_rc, block_rc;
24568c2ecf20Sopenharmony_ci	int rc = FAILED;
24578c2ecf20Sopenharmony_ci
24588c2ecf20Sopenharmony_ci	ENTER;
24598c2ecf20Sopenharmony_ci	block_rc = fc_block_scsi_eh(cmd);
24608c2ecf20Sopenharmony_ci	ibmvfc_wait_while_resetting(vhost);
24618c2ecf20Sopenharmony_ci	if (block_rc != FAST_IO_FAIL) {
24628c2ecf20Sopenharmony_ci		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
24638c2ecf20Sopenharmony_ci		ibmvfc_abort_task_set(sdev);
24648c2ecf20Sopenharmony_ci	} else
24658c2ecf20Sopenharmony_ci		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
24668c2ecf20Sopenharmony_ci
24678c2ecf20Sopenharmony_ci	if (!cancel_rc)
24688c2ecf20Sopenharmony_ci		rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
24698c2ecf20Sopenharmony_ci
24708c2ecf20Sopenharmony_ci	if (block_rc == FAST_IO_FAIL && rc != FAILED)
24718c2ecf20Sopenharmony_ci		rc = FAST_IO_FAIL;
24728c2ecf20Sopenharmony_ci
24738c2ecf20Sopenharmony_ci	LEAVE;
24748c2ecf20Sopenharmony_ci	return rc;
24758c2ecf20Sopenharmony_ci}
24768c2ecf20Sopenharmony_ci
24778c2ecf20Sopenharmony_ci/**
24788c2ecf20Sopenharmony_ci * ibmvfc_eh_device_reset_handler - Reset a single LUN
24798c2ecf20Sopenharmony_ci * @cmd:	scsi command struct
24808c2ecf20Sopenharmony_ci *
24818c2ecf20Sopenharmony_ci * Returns:
24828c2ecf20Sopenharmony_ci *	SUCCESS / FAST_IO_FAIL / FAILED
24838c2ecf20Sopenharmony_ci **/
24848c2ecf20Sopenharmony_cistatic int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
24858c2ecf20Sopenharmony_ci{
24868c2ecf20Sopenharmony_ci	struct scsi_device *sdev = cmd->device;
24878c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(sdev->host);
24888c2ecf20Sopenharmony_ci	int cancel_rc, block_rc, reset_rc = 0;
24898c2ecf20Sopenharmony_ci	int rc = FAILED;
24908c2ecf20Sopenharmony_ci
24918c2ecf20Sopenharmony_ci	ENTER;
24928c2ecf20Sopenharmony_ci	block_rc = fc_block_scsi_eh(cmd);
24938c2ecf20Sopenharmony_ci	ibmvfc_wait_while_resetting(vhost);
24948c2ecf20Sopenharmony_ci	if (block_rc != FAST_IO_FAIL) {
24958c2ecf20Sopenharmony_ci		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
24968c2ecf20Sopenharmony_ci		reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
24978c2ecf20Sopenharmony_ci	} else
24988c2ecf20Sopenharmony_ci		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
24998c2ecf20Sopenharmony_ci
25008c2ecf20Sopenharmony_ci	if (!cancel_rc && !reset_rc)
25018c2ecf20Sopenharmony_ci		rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
25028c2ecf20Sopenharmony_ci
25038c2ecf20Sopenharmony_ci	if (block_rc == FAST_IO_FAIL && rc != FAILED)
25048c2ecf20Sopenharmony_ci		rc = FAST_IO_FAIL;
25058c2ecf20Sopenharmony_ci
25068c2ecf20Sopenharmony_ci	LEAVE;
25078c2ecf20Sopenharmony_ci	return rc;
25088c2ecf20Sopenharmony_ci}
25098c2ecf20Sopenharmony_ci
25108c2ecf20Sopenharmony_ci/**
25118c2ecf20Sopenharmony_ci * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
25128c2ecf20Sopenharmony_ci * @sdev:	scsi device struct
25138c2ecf20Sopenharmony_ci * @data:	return code
25148c2ecf20Sopenharmony_ci *
25158c2ecf20Sopenharmony_ci **/
25168c2ecf20Sopenharmony_cistatic void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
25178c2ecf20Sopenharmony_ci{
25188c2ecf20Sopenharmony_ci	unsigned long *rc = data;
25198c2ecf20Sopenharmony_ci	*rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
25208c2ecf20Sopenharmony_ci}
25218c2ecf20Sopenharmony_ci
25228c2ecf20Sopenharmony_ci/**
25238c2ecf20Sopenharmony_ci * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
25248c2ecf20Sopenharmony_ci * @sdev:	scsi device struct
25258c2ecf20Sopenharmony_ci * @data:	return code
25268c2ecf20Sopenharmony_ci *
25278c2ecf20Sopenharmony_ci **/
25288c2ecf20Sopenharmony_cistatic void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
25298c2ecf20Sopenharmony_ci{
25308c2ecf20Sopenharmony_ci	unsigned long *rc = data;
25318c2ecf20Sopenharmony_ci	*rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
25328c2ecf20Sopenharmony_ci}
25338c2ecf20Sopenharmony_ci
25348c2ecf20Sopenharmony_ci/**
25358c2ecf20Sopenharmony_ci * ibmvfc_eh_target_reset_handler - Reset the target
25368c2ecf20Sopenharmony_ci * @cmd:	scsi command struct
25378c2ecf20Sopenharmony_ci *
25388c2ecf20Sopenharmony_ci * Returns:
25398c2ecf20Sopenharmony_ci *	SUCCESS / FAST_IO_FAIL / FAILED
25408c2ecf20Sopenharmony_ci **/
25418c2ecf20Sopenharmony_cistatic int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
25428c2ecf20Sopenharmony_ci{
25438c2ecf20Sopenharmony_ci	struct scsi_device *sdev = cmd->device;
25448c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(sdev->host);
25458c2ecf20Sopenharmony_ci	struct scsi_target *starget = scsi_target(sdev);
25468c2ecf20Sopenharmony_ci	int block_rc;
25478c2ecf20Sopenharmony_ci	int reset_rc = 0;
25488c2ecf20Sopenharmony_ci	int rc = FAILED;
25498c2ecf20Sopenharmony_ci	unsigned long cancel_rc = 0;
25508c2ecf20Sopenharmony_ci
25518c2ecf20Sopenharmony_ci	ENTER;
25528c2ecf20Sopenharmony_ci	block_rc = fc_block_scsi_eh(cmd);
25538c2ecf20Sopenharmony_ci	ibmvfc_wait_while_resetting(vhost);
25548c2ecf20Sopenharmony_ci	if (block_rc != FAST_IO_FAIL) {
25558c2ecf20Sopenharmony_ci		starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
25568c2ecf20Sopenharmony_ci		reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
25578c2ecf20Sopenharmony_ci	} else
25588c2ecf20Sopenharmony_ci		starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
25598c2ecf20Sopenharmony_ci
25608c2ecf20Sopenharmony_ci	if (!cancel_rc && !reset_rc)
25618c2ecf20Sopenharmony_ci		rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
25628c2ecf20Sopenharmony_ci
25638c2ecf20Sopenharmony_ci	if (block_rc == FAST_IO_FAIL && rc != FAILED)
25648c2ecf20Sopenharmony_ci		rc = FAST_IO_FAIL;
25658c2ecf20Sopenharmony_ci
25668c2ecf20Sopenharmony_ci	LEAVE;
25678c2ecf20Sopenharmony_ci	return rc;
25688c2ecf20Sopenharmony_ci}
25698c2ecf20Sopenharmony_ci
25708c2ecf20Sopenharmony_ci/**
25718c2ecf20Sopenharmony_ci * ibmvfc_eh_host_reset_handler - Reset the connection to the server
25728c2ecf20Sopenharmony_ci * @cmd:	struct scsi_cmnd having problems
25738c2ecf20Sopenharmony_ci *
25748c2ecf20Sopenharmony_ci **/
25758c2ecf20Sopenharmony_cistatic int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
25768c2ecf20Sopenharmony_ci{
25778c2ecf20Sopenharmony_ci	int rc;
25788c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
25798c2ecf20Sopenharmony_ci
25808c2ecf20Sopenharmony_ci	dev_err(vhost->dev, "Resetting connection due to error recovery\n");
25818c2ecf20Sopenharmony_ci	rc = ibmvfc_issue_fc_host_lip(vhost->host);
25828c2ecf20Sopenharmony_ci
25838c2ecf20Sopenharmony_ci	return rc ? FAILED : SUCCESS;
25848c2ecf20Sopenharmony_ci}
25858c2ecf20Sopenharmony_ci
25868c2ecf20Sopenharmony_ci/**
25878c2ecf20Sopenharmony_ci * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
25888c2ecf20Sopenharmony_ci * @rport:		rport struct
25898c2ecf20Sopenharmony_ci *
25908c2ecf20Sopenharmony_ci * Return value:
25918c2ecf20Sopenharmony_ci * 	none
25928c2ecf20Sopenharmony_ci **/
25938c2ecf20Sopenharmony_cistatic void ibmvfc_terminate_rport_io(struct fc_rport *rport)
25948c2ecf20Sopenharmony_ci{
25958c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = rport_to_shost(rport);
25968c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
25978c2ecf20Sopenharmony_ci	struct fc_rport *dev_rport;
25988c2ecf20Sopenharmony_ci	struct scsi_device *sdev;
25998c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
26008c2ecf20Sopenharmony_ci	unsigned long rc, flags;
26018c2ecf20Sopenharmony_ci	unsigned int found;
26028c2ecf20Sopenharmony_ci
26038c2ecf20Sopenharmony_ci	ENTER;
26048c2ecf20Sopenharmony_ci	shost_for_each_device(sdev, shost) {
26058c2ecf20Sopenharmony_ci		dev_rport = starget_to_rport(scsi_target(sdev));
26068c2ecf20Sopenharmony_ci		if (dev_rport != rport)
26078c2ecf20Sopenharmony_ci			continue;
26088c2ecf20Sopenharmony_ci		ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
26098c2ecf20Sopenharmony_ci	}
26108c2ecf20Sopenharmony_ci
26118c2ecf20Sopenharmony_ci	rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
26128c2ecf20Sopenharmony_ci
26138c2ecf20Sopenharmony_ci	if (rc == FAILED)
26148c2ecf20Sopenharmony_ci		ibmvfc_issue_fc_host_lip(shost);
26158c2ecf20Sopenharmony_ci
26168c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
26178c2ecf20Sopenharmony_ci	found = 0;
26188c2ecf20Sopenharmony_ci	list_for_each_entry(tgt, &vhost->targets, queue) {
26198c2ecf20Sopenharmony_ci		if (tgt->scsi_id == rport->port_id) {
26208c2ecf20Sopenharmony_ci			found++;
26218c2ecf20Sopenharmony_ci			break;
26228c2ecf20Sopenharmony_ci		}
26238c2ecf20Sopenharmony_ci	}
26248c2ecf20Sopenharmony_ci
26258c2ecf20Sopenharmony_ci	if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
26268c2ecf20Sopenharmony_ci		/*
26278c2ecf20Sopenharmony_ci		 * If we get here, that means we previously attempted to send
26288c2ecf20Sopenharmony_ci		 * an implicit logout to the target but it failed, most likely
26298c2ecf20Sopenharmony_ci		 * due to I/O being pending, so we need to send it again
26308c2ecf20Sopenharmony_ci		 */
26318c2ecf20Sopenharmony_ci		ibmvfc_del_tgt(tgt);
26328c2ecf20Sopenharmony_ci		ibmvfc_reinit_host(vhost);
26338c2ecf20Sopenharmony_ci	}
26348c2ecf20Sopenharmony_ci
26358c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
26368c2ecf20Sopenharmony_ci	LEAVE;
26378c2ecf20Sopenharmony_ci}
26388c2ecf20Sopenharmony_ci
26398c2ecf20Sopenharmony_cistatic const struct ibmvfc_async_desc ae_desc [] = {
26408c2ecf20Sopenharmony_ci	{ "PLOGI",	IBMVFC_AE_ELS_PLOGI,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
26418c2ecf20Sopenharmony_ci	{ "LOGO",	IBMVFC_AE_ELS_LOGO,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
26428c2ecf20Sopenharmony_ci	{ "PRLO",	IBMVFC_AE_ELS_PRLO,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
26438c2ecf20Sopenharmony_ci	{ "N-Port SCN",	IBMVFC_AE_SCN_NPORT,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
26448c2ecf20Sopenharmony_ci	{ "Group SCN",	IBMVFC_AE_SCN_GROUP,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
26458c2ecf20Sopenharmony_ci	{ "Domain SCN",	IBMVFC_AE_SCN_DOMAIN,	IBMVFC_DEFAULT_LOG_LEVEL },
26468c2ecf20Sopenharmony_ci	{ "Fabric SCN",	IBMVFC_AE_SCN_FABRIC,	IBMVFC_DEFAULT_LOG_LEVEL },
26478c2ecf20Sopenharmony_ci	{ "Link Up",	IBMVFC_AE_LINK_UP,	IBMVFC_DEFAULT_LOG_LEVEL },
26488c2ecf20Sopenharmony_ci	{ "Link Down",	IBMVFC_AE_LINK_DOWN,	IBMVFC_DEFAULT_LOG_LEVEL },
26498c2ecf20Sopenharmony_ci	{ "Link Dead",	IBMVFC_AE_LINK_DEAD,	IBMVFC_DEFAULT_LOG_LEVEL },
26508c2ecf20Sopenharmony_ci	{ "Halt",	IBMVFC_AE_HALT,		IBMVFC_DEFAULT_LOG_LEVEL },
26518c2ecf20Sopenharmony_ci	{ "Resume",	IBMVFC_AE_RESUME,	IBMVFC_DEFAULT_LOG_LEVEL },
26528c2ecf20Sopenharmony_ci	{ "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
26538c2ecf20Sopenharmony_ci};
26548c2ecf20Sopenharmony_ci
26558c2ecf20Sopenharmony_cistatic const struct ibmvfc_async_desc unknown_ae = {
26568c2ecf20Sopenharmony_ci	"Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
26578c2ecf20Sopenharmony_ci};
26588c2ecf20Sopenharmony_ci
26598c2ecf20Sopenharmony_ci/**
26608c2ecf20Sopenharmony_ci * ibmvfc_get_ae_desc - Get text description for async event
26618c2ecf20Sopenharmony_ci * @ae:	async event
26628c2ecf20Sopenharmony_ci *
26638c2ecf20Sopenharmony_ci **/
26648c2ecf20Sopenharmony_cistatic const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
26658c2ecf20Sopenharmony_ci{
26668c2ecf20Sopenharmony_ci	int i;
26678c2ecf20Sopenharmony_ci
26688c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
26698c2ecf20Sopenharmony_ci		if (ae_desc[i].ae == ae)
26708c2ecf20Sopenharmony_ci			return &ae_desc[i];
26718c2ecf20Sopenharmony_ci
26728c2ecf20Sopenharmony_ci	return &unknown_ae;
26738c2ecf20Sopenharmony_ci}
26748c2ecf20Sopenharmony_ci
26758c2ecf20Sopenharmony_cistatic const struct {
26768c2ecf20Sopenharmony_ci	enum ibmvfc_ae_link_state state;
26778c2ecf20Sopenharmony_ci	const char *desc;
26788c2ecf20Sopenharmony_ci} link_desc [] = {
26798c2ecf20Sopenharmony_ci	{ IBMVFC_AE_LS_LINK_UP,		" link up" },
26808c2ecf20Sopenharmony_ci	{ IBMVFC_AE_LS_LINK_BOUNCED,	" link bounced" },
26818c2ecf20Sopenharmony_ci	{ IBMVFC_AE_LS_LINK_DOWN,	" link down" },
26828c2ecf20Sopenharmony_ci	{ IBMVFC_AE_LS_LINK_DEAD,	" link dead" },
26838c2ecf20Sopenharmony_ci};
26848c2ecf20Sopenharmony_ci
26858c2ecf20Sopenharmony_ci/**
26868c2ecf20Sopenharmony_ci * ibmvfc_get_link_state - Get text description for link state
26878c2ecf20Sopenharmony_ci * @state:	link state
26888c2ecf20Sopenharmony_ci *
26898c2ecf20Sopenharmony_ci **/
26908c2ecf20Sopenharmony_cistatic const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
26918c2ecf20Sopenharmony_ci{
26928c2ecf20Sopenharmony_ci	int i;
26938c2ecf20Sopenharmony_ci
26948c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(link_desc); i++)
26958c2ecf20Sopenharmony_ci		if (link_desc[i].state == state)
26968c2ecf20Sopenharmony_ci			return link_desc[i].desc;
26978c2ecf20Sopenharmony_ci
26988c2ecf20Sopenharmony_ci	return "";
26998c2ecf20Sopenharmony_ci}
27008c2ecf20Sopenharmony_ci
27018c2ecf20Sopenharmony_ci/**
27028c2ecf20Sopenharmony_ci * ibmvfc_handle_async - Handle an async event from the adapter
27038c2ecf20Sopenharmony_ci * @crq:	crq to process
27048c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
27058c2ecf20Sopenharmony_ci *
27068c2ecf20Sopenharmony_ci **/
27078c2ecf20Sopenharmony_cistatic void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
27088c2ecf20Sopenharmony_ci				struct ibmvfc_host *vhost)
27098c2ecf20Sopenharmony_ci{
27108c2ecf20Sopenharmony_ci	const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
27118c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
27128c2ecf20Sopenharmony_ci
27138c2ecf20Sopenharmony_ci	ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
27148c2ecf20Sopenharmony_ci		   " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
27158c2ecf20Sopenharmony_ci		   be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
27168c2ecf20Sopenharmony_ci		   ibmvfc_get_link_state(crq->link_state));
27178c2ecf20Sopenharmony_ci
27188c2ecf20Sopenharmony_ci	switch (be64_to_cpu(crq->event)) {
27198c2ecf20Sopenharmony_ci	case IBMVFC_AE_RESUME:
27208c2ecf20Sopenharmony_ci		switch (crq->link_state) {
27218c2ecf20Sopenharmony_ci		case IBMVFC_AE_LS_LINK_DOWN:
27228c2ecf20Sopenharmony_ci			ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
27238c2ecf20Sopenharmony_ci			break;
27248c2ecf20Sopenharmony_ci		case IBMVFC_AE_LS_LINK_DEAD:
27258c2ecf20Sopenharmony_ci			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
27268c2ecf20Sopenharmony_ci			break;
27278c2ecf20Sopenharmony_ci		case IBMVFC_AE_LS_LINK_UP:
27288c2ecf20Sopenharmony_ci		case IBMVFC_AE_LS_LINK_BOUNCED:
27298c2ecf20Sopenharmony_ci		default:
27308c2ecf20Sopenharmony_ci			vhost->events_to_log |= IBMVFC_AE_LINKUP;
27318c2ecf20Sopenharmony_ci			vhost->delay_init = 1;
27328c2ecf20Sopenharmony_ci			__ibmvfc_reset_host(vhost);
27338c2ecf20Sopenharmony_ci			break;
27348c2ecf20Sopenharmony_ci		}
27358c2ecf20Sopenharmony_ci
27368c2ecf20Sopenharmony_ci		break;
27378c2ecf20Sopenharmony_ci	case IBMVFC_AE_LINK_UP:
27388c2ecf20Sopenharmony_ci		vhost->events_to_log |= IBMVFC_AE_LINKUP;
27398c2ecf20Sopenharmony_ci		vhost->delay_init = 1;
27408c2ecf20Sopenharmony_ci		__ibmvfc_reset_host(vhost);
27418c2ecf20Sopenharmony_ci		break;
27428c2ecf20Sopenharmony_ci	case IBMVFC_AE_SCN_FABRIC:
27438c2ecf20Sopenharmony_ci	case IBMVFC_AE_SCN_DOMAIN:
27448c2ecf20Sopenharmony_ci		vhost->events_to_log |= IBMVFC_AE_RSCN;
27458c2ecf20Sopenharmony_ci		if (vhost->state < IBMVFC_HALTED) {
27468c2ecf20Sopenharmony_ci			vhost->delay_init = 1;
27478c2ecf20Sopenharmony_ci			__ibmvfc_reset_host(vhost);
27488c2ecf20Sopenharmony_ci		}
27498c2ecf20Sopenharmony_ci		break;
27508c2ecf20Sopenharmony_ci	case IBMVFC_AE_SCN_NPORT:
27518c2ecf20Sopenharmony_ci	case IBMVFC_AE_SCN_GROUP:
27528c2ecf20Sopenharmony_ci		vhost->events_to_log |= IBMVFC_AE_RSCN;
27538c2ecf20Sopenharmony_ci		ibmvfc_reinit_host(vhost);
27548c2ecf20Sopenharmony_ci		break;
27558c2ecf20Sopenharmony_ci	case IBMVFC_AE_ELS_LOGO:
27568c2ecf20Sopenharmony_ci	case IBMVFC_AE_ELS_PRLO:
27578c2ecf20Sopenharmony_ci	case IBMVFC_AE_ELS_PLOGI:
27588c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue) {
27598c2ecf20Sopenharmony_ci			if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
27608c2ecf20Sopenharmony_ci				break;
27618c2ecf20Sopenharmony_ci			if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
27628c2ecf20Sopenharmony_ci				continue;
27638c2ecf20Sopenharmony_ci			if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
27648c2ecf20Sopenharmony_ci				continue;
27658c2ecf20Sopenharmony_ci			if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
27668c2ecf20Sopenharmony_ci				continue;
27678c2ecf20Sopenharmony_ci			if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
27688c2ecf20Sopenharmony_ci				tgt->logo_rcvd = 1;
27698c2ecf20Sopenharmony_ci			if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
27708c2ecf20Sopenharmony_ci				ibmvfc_del_tgt(tgt);
27718c2ecf20Sopenharmony_ci				ibmvfc_reinit_host(vhost);
27728c2ecf20Sopenharmony_ci			}
27738c2ecf20Sopenharmony_ci		}
27748c2ecf20Sopenharmony_ci		break;
27758c2ecf20Sopenharmony_ci	case IBMVFC_AE_LINK_DOWN:
27768c2ecf20Sopenharmony_ci	case IBMVFC_AE_ADAPTER_FAILED:
27778c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
27788c2ecf20Sopenharmony_ci		break;
27798c2ecf20Sopenharmony_ci	case IBMVFC_AE_LINK_DEAD:
27808c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
27818c2ecf20Sopenharmony_ci		break;
27828c2ecf20Sopenharmony_ci	case IBMVFC_AE_HALT:
27838c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_HALTED);
27848c2ecf20Sopenharmony_ci		break;
27858c2ecf20Sopenharmony_ci	default:
27868c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
27878c2ecf20Sopenharmony_ci		break;
27888c2ecf20Sopenharmony_ci	}
27898c2ecf20Sopenharmony_ci}
27908c2ecf20Sopenharmony_ci
27918c2ecf20Sopenharmony_ci/**
27928c2ecf20Sopenharmony_ci * ibmvfc_handle_crq - Handles and frees received events in the CRQ
27938c2ecf20Sopenharmony_ci * @crq:	Command/Response queue
27948c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
27958c2ecf20Sopenharmony_ci *
27968c2ecf20Sopenharmony_ci **/
27978c2ecf20Sopenharmony_cistatic void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
27988c2ecf20Sopenharmony_ci{
27998c2ecf20Sopenharmony_ci	long rc;
28008c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
28018c2ecf20Sopenharmony_ci
28028c2ecf20Sopenharmony_ci	switch (crq->valid) {
28038c2ecf20Sopenharmony_ci	case IBMVFC_CRQ_INIT_RSP:
28048c2ecf20Sopenharmony_ci		switch (crq->format) {
28058c2ecf20Sopenharmony_ci		case IBMVFC_CRQ_INIT:
28068c2ecf20Sopenharmony_ci			dev_info(vhost->dev, "Partner initialized\n");
28078c2ecf20Sopenharmony_ci			/* Send back a response */
28088c2ecf20Sopenharmony_ci			rc = ibmvfc_send_crq_init_complete(vhost);
28098c2ecf20Sopenharmony_ci			if (rc == 0)
28108c2ecf20Sopenharmony_ci				ibmvfc_init_host(vhost);
28118c2ecf20Sopenharmony_ci			else
28128c2ecf20Sopenharmony_ci				dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
28138c2ecf20Sopenharmony_ci			break;
28148c2ecf20Sopenharmony_ci		case IBMVFC_CRQ_INIT_COMPLETE:
28158c2ecf20Sopenharmony_ci			dev_info(vhost->dev, "Partner initialization complete\n");
28168c2ecf20Sopenharmony_ci			ibmvfc_init_host(vhost);
28178c2ecf20Sopenharmony_ci			break;
28188c2ecf20Sopenharmony_ci		default:
28198c2ecf20Sopenharmony_ci			dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
28208c2ecf20Sopenharmony_ci		}
28218c2ecf20Sopenharmony_ci		return;
28228c2ecf20Sopenharmony_ci	case IBMVFC_CRQ_XPORT_EVENT:
28238c2ecf20Sopenharmony_ci		vhost->state = IBMVFC_NO_CRQ;
28248c2ecf20Sopenharmony_ci		vhost->logged_in = 0;
28258c2ecf20Sopenharmony_ci		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
28268c2ecf20Sopenharmony_ci		if (crq->format == IBMVFC_PARTITION_MIGRATED) {
28278c2ecf20Sopenharmony_ci			/* We need to re-setup the interpartition connection */
28288c2ecf20Sopenharmony_ci			dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
28298c2ecf20Sopenharmony_ci			vhost->client_migrated = 1;
28308c2ecf20Sopenharmony_ci
28318c2ecf20Sopenharmony_ci			scsi_block_requests(vhost->host);
28328c2ecf20Sopenharmony_ci			ibmvfc_purge_requests(vhost, DID_REQUEUE);
28338c2ecf20Sopenharmony_ci			ibmvfc_set_host_state(vhost, IBMVFC_LINK_DOWN);
28348c2ecf20Sopenharmony_ci			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
28358c2ecf20Sopenharmony_ci			wake_up(&vhost->work_wait_q);
28368c2ecf20Sopenharmony_ci		} else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
28378c2ecf20Sopenharmony_ci			dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
28388c2ecf20Sopenharmony_ci			ibmvfc_purge_requests(vhost, DID_ERROR);
28398c2ecf20Sopenharmony_ci			ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
28408c2ecf20Sopenharmony_ci			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
28418c2ecf20Sopenharmony_ci		} else {
28428c2ecf20Sopenharmony_ci			dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
28438c2ecf20Sopenharmony_ci		}
28448c2ecf20Sopenharmony_ci		return;
28458c2ecf20Sopenharmony_ci	case IBMVFC_CRQ_CMD_RSP:
28468c2ecf20Sopenharmony_ci		break;
28478c2ecf20Sopenharmony_ci	default:
28488c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
28498c2ecf20Sopenharmony_ci		return;
28508c2ecf20Sopenharmony_ci	}
28518c2ecf20Sopenharmony_ci
28528c2ecf20Sopenharmony_ci	if (crq->format == IBMVFC_ASYNC_EVENT)
28538c2ecf20Sopenharmony_ci		return;
28548c2ecf20Sopenharmony_ci
28558c2ecf20Sopenharmony_ci	/* The only kind of payload CRQs we should get are responses to
28568c2ecf20Sopenharmony_ci	 * things we send. Make sure this response is to something we
28578c2ecf20Sopenharmony_ci	 * actually sent
28588c2ecf20Sopenharmony_ci	 */
28598c2ecf20Sopenharmony_ci	if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
28608c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
28618c2ecf20Sopenharmony_ci			crq->ioba);
28628c2ecf20Sopenharmony_ci		return;
28638c2ecf20Sopenharmony_ci	}
28648c2ecf20Sopenharmony_ci
28658c2ecf20Sopenharmony_ci	if (unlikely(atomic_read(&evt->free))) {
28668c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
28678c2ecf20Sopenharmony_ci			crq->ioba);
28688c2ecf20Sopenharmony_ci		return;
28698c2ecf20Sopenharmony_ci	}
28708c2ecf20Sopenharmony_ci
28718c2ecf20Sopenharmony_ci	del_timer(&evt->timer);
28728c2ecf20Sopenharmony_ci	list_del(&evt->queue);
28738c2ecf20Sopenharmony_ci	ibmvfc_trc_end(evt);
28748c2ecf20Sopenharmony_ci	evt->done(evt);
28758c2ecf20Sopenharmony_ci}
28768c2ecf20Sopenharmony_ci
28778c2ecf20Sopenharmony_ci/**
28788c2ecf20Sopenharmony_ci * ibmvfc_scan_finished - Check if the device scan is done.
28798c2ecf20Sopenharmony_ci * @shost:	scsi host struct
28808c2ecf20Sopenharmony_ci * @time:	current elapsed time
28818c2ecf20Sopenharmony_ci *
28828c2ecf20Sopenharmony_ci * Returns:
28838c2ecf20Sopenharmony_ci *	0 if scan is not done / 1 if scan is done
28848c2ecf20Sopenharmony_ci **/
28858c2ecf20Sopenharmony_cistatic int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
28868c2ecf20Sopenharmony_ci{
28878c2ecf20Sopenharmony_ci	unsigned long flags;
28888c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
28898c2ecf20Sopenharmony_ci	int done = 0;
28908c2ecf20Sopenharmony_ci
28918c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
28928c2ecf20Sopenharmony_ci	if (time >= (init_timeout * HZ)) {
28938c2ecf20Sopenharmony_ci		dev_info(vhost->dev, "Scan taking longer than %d seconds, "
28948c2ecf20Sopenharmony_ci			 "continuing initialization\n", init_timeout);
28958c2ecf20Sopenharmony_ci		done = 1;
28968c2ecf20Sopenharmony_ci	}
28978c2ecf20Sopenharmony_ci
28988c2ecf20Sopenharmony_ci	if (vhost->scan_complete)
28998c2ecf20Sopenharmony_ci		done = 1;
29008c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
29018c2ecf20Sopenharmony_ci	return done;
29028c2ecf20Sopenharmony_ci}
29038c2ecf20Sopenharmony_ci
29048c2ecf20Sopenharmony_ci/**
29058c2ecf20Sopenharmony_ci * ibmvfc_slave_alloc - Setup the device's task set value
29068c2ecf20Sopenharmony_ci * @sdev:	struct scsi_device device to configure
29078c2ecf20Sopenharmony_ci *
29088c2ecf20Sopenharmony_ci * Set the device's task set value so that error handling works as
29098c2ecf20Sopenharmony_ci * expected.
29108c2ecf20Sopenharmony_ci *
29118c2ecf20Sopenharmony_ci * Returns:
29128c2ecf20Sopenharmony_ci *	0 on success / -ENXIO if device does not exist
29138c2ecf20Sopenharmony_ci **/
29148c2ecf20Sopenharmony_cistatic int ibmvfc_slave_alloc(struct scsi_device *sdev)
29158c2ecf20Sopenharmony_ci{
29168c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = sdev->host;
29178c2ecf20Sopenharmony_ci	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
29188c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
29198c2ecf20Sopenharmony_ci	unsigned long flags = 0;
29208c2ecf20Sopenharmony_ci
29218c2ecf20Sopenharmony_ci	if (!rport || fc_remote_port_chkready(rport))
29228c2ecf20Sopenharmony_ci		return -ENXIO;
29238c2ecf20Sopenharmony_ci
29248c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
29258c2ecf20Sopenharmony_ci	sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
29268c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
29278c2ecf20Sopenharmony_ci	return 0;
29288c2ecf20Sopenharmony_ci}
29298c2ecf20Sopenharmony_ci
29308c2ecf20Sopenharmony_ci/**
29318c2ecf20Sopenharmony_ci * ibmvfc_target_alloc - Setup the target's task set value
29328c2ecf20Sopenharmony_ci * @starget:	struct scsi_target
29338c2ecf20Sopenharmony_ci *
29348c2ecf20Sopenharmony_ci * Set the target's task set value so that error handling works as
29358c2ecf20Sopenharmony_ci * expected.
29368c2ecf20Sopenharmony_ci *
29378c2ecf20Sopenharmony_ci * Returns:
29388c2ecf20Sopenharmony_ci *	0 on success / -ENXIO if device does not exist
29398c2ecf20Sopenharmony_ci **/
29408c2ecf20Sopenharmony_cistatic int ibmvfc_target_alloc(struct scsi_target *starget)
29418c2ecf20Sopenharmony_ci{
29428c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
29438c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
29448c2ecf20Sopenharmony_ci	unsigned long flags = 0;
29458c2ecf20Sopenharmony_ci
29468c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
29478c2ecf20Sopenharmony_ci	starget->hostdata = (void *)(unsigned long)vhost->task_set++;
29488c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
29498c2ecf20Sopenharmony_ci	return 0;
29508c2ecf20Sopenharmony_ci}
29518c2ecf20Sopenharmony_ci
29528c2ecf20Sopenharmony_ci/**
29538c2ecf20Sopenharmony_ci * ibmvfc_slave_configure - Configure the device
29548c2ecf20Sopenharmony_ci * @sdev:	struct scsi_device device to configure
29558c2ecf20Sopenharmony_ci *
29568c2ecf20Sopenharmony_ci * Enable allow_restart for a device if it is a disk. Adjust the
29578c2ecf20Sopenharmony_ci * queue_depth here also.
29588c2ecf20Sopenharmony_ci *
29598c2ecf20Sopenharmony_ci * Returns:
29608c2ecf20Sopenharmony_ci *	0
29618c2ecf20Sopenharmony_ci **/
29628c2ecf20Sopenharmony_cistatic int ibmvfc_slave_configure(struct scsi_device *sdev)
29638c2ecf20Sopenharmony_ci{
29648c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = sdev->host;
29658c2ecf20Sopenharmony_ci	unsigned long flags = 0;
29668c2ecf20Sopenharmony_ci
29678c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
29688c2ecf20Sopenharmony_ci	if (sdev->type == TYPE_DISK) {
29698c2ecf20Sopenharmony_ci		sdev->allow_restart = 1;
29708c2ecf20Sopenharmony_ci		blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
29718c2ecf20Sopenharmony_ci	}
29728c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
29738c2ecf20Sopenharmony_ci	return 0;
29748c2ecf20Sopenharmony_ci}
29758c2ecf20Sopenharmony_ci
29768c2ecf20Sopenharmony_ci/**
29778c2ecf20Sopenharmony_ci * ibmvfc_change_queue_depth - Change the device's queue depth
29788c2ecf20Sopenharmony_ci * @sdev:	scsi device struct
29798c2ecf20Sopenharmony_ci * @qdepth:	depth to set
29808c2ecf20Sopenharmony_ci * @reason:	calling context
29818c2ecf20Sopenharmony_ci *
29828c2ecf20Sopenharmony_ci * Return value:
29838c2ecf20Sopenharmony_ci * 	actual depth set
29848c2ecf20Sopenharmony_ci **/
29858c2ecf20Sopenharmony_cistatic int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
29868c2ecf20Sopenharmony_ci{
29878c2ecf20Sopenharmony_ci	if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
29888c2ecf20Sopenharmony_ci		qdepth = IBMVFC_MAX_CMDS_PER_LUN;
29898c2ecf20Sopenharmony_ci
29908c2ecf20Sopenharmony_ci	return scsi_change_queue_depth(sdev, qdepth);
29918c2ecf20Sopenharmony_ci}
29928c2ecf20Sopenharmony_ci
29938c2ecf20Sopenharmony_cistatic ssize_t ibmvfc_show_host_partition_name(struct device *dev,
29948c2ecf20Sopenharmony_ci						 struct device_attribute *attr, char *buf)
29958c2ecf20Sopenharmony_ci{
29968c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
29978c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
29988c2ecf20Sopenharmony_ci
29998c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%s\n",
30008c2ecf20Sopenharmony_ci			vhost->login_buf->resp.partition_name);
30018c2ecf20Sopenharmony_ci}
30028c2ecf20Sopenharmony_ci
30038c2ecf20Sopenharmony_cistatic ssize_t ibmvfc_show_host_device_name(struct device *dev,
30048c2ecf20Sopenharmony_ci					    struct device_attribute *attr, char *buf)
30058c2ecf20Sopenharmony_ci{
30068c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
30078c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
30088c2ecf20Sopenharmony_ci
30098c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%s\n",
30108c2ecf20Sopenharmony_ci			vhost->login_buf->resp.device_name);
30118c2ecf20Sopenharmony_ci}
30128c2ecf20Sopenharmony_ci
30138c2ecf20Sopenharmony_cistatic ssize_t ibmvfc_show_host_loc_code(struct device *dev,
30148c2ecf20Sopenharmony_ci					 struct device_attribute *attr, char *buf)
30158c2ecf20Sopenharmony_ci{
30168c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
30178c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
30188c2ecf20Sopenharmony_ci
30198c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%s\n",
30208c2ecf20Sopenharmony_ci			vhost->login_buf->resp.port_loc_code);
30218c2ecf20Sopenharmony_ci}
30228c2ecf20Sopenharmony_ci
30238c2ecf20Sopenharmony_cistatic ssize_t ibmvfc_show_host_drc_name(struct device *dev,
30248c2ecf20Sopenharmony_ci					 struct device_attribute *attr, char *buf)
30258c2ecf20Sopenharmony_ci{
30268c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
30278c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
30288c2ecf20Sopenharmony_ci
30298c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%s\n",
30308c2ecf20Sopenharmony_ci			vhost->login_buf->resp.drc_name);
30318c2ecf20Sopenharmony_ci}
30328c2ecf20Sopenharmony_ci
30338c2ecf20Sopenharmony_cistatic ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
30348c2ecf20Sopenharmony_ci					     struct device_attribute *attr, char *buf)
30358c2ecf20Sopenharmony_ci{
30368c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
30378c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
30388c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
30398c2ecf20Sopenharmony_ci}
30408c2ecf20Sopenharmony_ci
30418c2ecf20Sopenharmony_cistatic ssize_t ibmvfc_show_host_capabilities(struct device *dev,
30428c2ecf20Sopenharmony_ci					     struct device_attribute *attr, char *buf)
30438c2ecf20Sopenharmony_ci{
30448c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
30458c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
30468c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
30478c2ecf20Sopenharmony_ci}
30488c2ecf20Sopenharmony_ci
30498c2ecf20Sopenharmony_ci/**
30508c2ecf20Sopenharmony_ci * ibmvfc_show_log_level - Show the adapter's error logging level
30518c2ecf20Sopenharmony_ci * @dev:	class device struct
30528c2ecf20Sopenharmony_ci * @buf:	buffer
30538c2ecf20Sopenharmony_ci *
30548c2ecf20Sopenharmony_ci * Return value:
30558c2ecf20Sopenharmony_ci * 	number of bytes printed to buffer
30568c2ecf20Sopenharmony_ci **/
30578c2ecf20Sopenharmony_cistatic ssize_t ibmvfc_show_log_level(struct device *dev,
30588c2ecf20Sopenharmony_ci				     struct device_attribute *attr, char *buf)
30598c2ecf20Sopenharmony_ci{
30608c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
30618c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
30628c2ecf20Sopenharmony_ci	unsigned long flags = 0;
30638c2ecf20Sopenharmony_ci	int len;
30648c2ecf20Sopenharmony_ci
30658c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
30668c2ecf20Sopenharmony_ci	len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
30678c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
30688c2ecf20Sopenharmony_ci	return len;
30698c2ecf20Sopenharmony_ci}
30708c2ecf20Sopenharmony_ci
30718c2ecf20Sopenharmony_ci/**
30728c2ecf20Sopenharmony_ci * ibmvfc_store_log_level - Change the adapter's error logging level
30738c2ecf20Sopenharmony_ci * @dev:	class device struct
30748c2ecf20Sopenharmony_ci * @buf:	buffer
30758c2ecf20Sopenharmony_ci *
30768c2ecf20Sopenharmony_ci * Return value:
30778c2ecf20Sopenharmony_ci * 	number of bytes printed to buffer
30788c2ecf20Sopenharmony_ci **/
30798c2ecf20Sopenharmony_cistatic ssize_t ibmvfc_store_log_level(struct device *dev,
30808c2ecf20Sopenharmony_ci				      struct device_attribute *attr,
30818c2ecf20Sopenharmony_ci				      const char *buf, size_t count)
30828c2ecf20Sopenharmony_ci{
30838c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
30848c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
30858c2ecf20Sopenharmony_ci	unsigned long flags = 0;
30868c2ecf20Sopenharmony_ci
30878c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
30888c2ecf20Sopenharmony_ci	vhost->log_level = simple_strtoul(buf, NULL, 10);
30898c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
30908c2ecf20Sopenharmony_ci	return strlen(buf);
30918c2ecf20Sopenharmony_ci}
30928c2ecf20Sopenharmony_ci
30938c2ecf20Sopenharmony_cistatic DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
30948c2ecf20Sopenharmony_cistatic DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
30958c2ecf20Sopenharmony_cistatic DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
30968c2ecf20Sopenharmony_cistatic DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
30978c2ecf20Sopenharmony_cistatic DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
30988c2ecf20Sopenharmony_cistatic DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
30998c2ecf20Sopenharmony_cistatic DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
31008c2ecf20Sopenharmony_ci		   ibmvfc_show_log_level, ibmvfc_store_log_level);
31018c2ecf20Sopenharmony_ci
31028c2ecf20Sopenharmony_ci#ifdef CONFIG_SCSI_IBMVFC_TRACE
31038c2ecf20Sopenharmony_ci/**
31048c2ecf20Sopenharmony_ci * ibmvfc_read_trace - Dump the adapter trace
31058c2ecf20Sopenharmony_ci * @filp:		open sysfs file
31068c2ecf20Sopenharmony_ci * @kobj:		kobject struct
31078c2ecf20Sopenharmony_ci * @bin_attr:	bin_attribute struct
31088c2ecf20Sopenharmony_ci * @buf:		buffer
31098c2ecf20Sopenharmony_ci * @off:		offset
31108c2ecf20Sopenharmony_ci * @count:		buffer size
31118c2ecf20Sopenharmony_ci *
31128c2ecf20Sopenharmony_ci * Return value:
31138c2ecf20Sopenharmony_ci *	number of bytes printed to buffer
31148c2ecf20Sopenharmony_ci **/
31158c2ecf20Sopenharmony_cistatic ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
31168c2ecf20Sopenharmony_ci				 struct bin_attribute *bin_attr,
31178c2ecf20Sopenharmony_ci				 char *buf, loff_t off, size_t count)
31188c2ecf20Sopenharmony_ci{
31198c2ecf20Sopenharmony_ci	struct device *dev = container_of(kobj, struct device, kobj);
31208c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
31218c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = shost_priv(shost);
31228c2ecf20Sopenharmony_ci	unsigned long flags = 0;
31238c2ecf20Sopenharmony_ci	int size = IBMVFC_TRACE_SIZE;
31248c2ecf20Sopenharmony_ci	char *src = (char *)vhost->trace;
31258c2ecf20Sopenharmony_ci
31268c2ecf20Sopenharmony_ci	if (off > size)
31278c2ecf20Sopenharmony_ci		return 0;
31288c2ecf20Sopenharmony_ci	if (off + count > size) {
31298c2ecf20Sopenharmony_ci		size -= off;
31308c2ecf20Sopenharmony_ci		count = size;
31318c2ecf20Sopenharmony_ci	}
31328c2ecf20Sopenharmony_ci
31338c2ecf20Sopenharmony_ci	spin_lock_irqsave(shost->host_lock, flags);
31348c2ecf20Sopenharmony_ci	memcpy(buf, &src[off], count);
31358c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(shost->host_lock, flags);
31368c2ecf20Sopenharmony_ci	return count;
31378c2ecf20Sopenharmony_ci}
31388c2ecf20Sopenharmony_ci
31398c2ecf20Sopenharmony_cistatic struct bin_attribute ibmvfc_trace_attr = {
31408c2ecf20Sopenharmony_ci	.attr =	{
31418c2ecf20Sopenharmony_ci		.name = "trace",
31428c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
31438c2ecf20Sopenharmony_ci	},
31448c2ecf20Sopenharmony_ci	.size = 0,
31458c2ecf20Sopenharmony_ci	.read = ibmvfc_read_trace,
31468c2ecf20Sopenharmony_ci};
31478c2ecf20Sopenharmony_ci#endif
31488c2ecf20Sopenharmony_ci
31498c2ecf20Sopenharmony_cistatic struct device_attribute *ibmvfc_attrs[] = {
31508c2ecf20Sopenharmony_ci	&dev_attr_partition_name,
31518c2ecf20Sopenharmony_ci	&dev_attr_device_name,
31528c2ecf20Sopenharmony_ci	&dev_attr_port_loc_code,
31538c2ecf20Sopenharmony_ci	&dev_attr_drc_name,
31548c2ecf20Sopenharmony_ci	&dev_attr_npiv_version,
31558c2ecf20Sopenharmony_ci	&dev_attr_capabilities,
31568c2ecf20Sopenharmony_ci	&dev_attr_log_level,
31578c2ecf20Sopenharmony_ci	NULL
31588c2ecf20Sopenharmony_ci};
31598c2ecf20Sopenharmony_ci
31608c2ecf20Sopenharmony_cistatic struct scsi_host_template driver_template = {
31618c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
31628c2ecf20Sopenharmony_ci	.name = "IBM POWER Virtual FC Adapter",
31638c2ecf20Sopenharmony_ci	.proc_name = IBMVFC_NAME,
31648c2ecf20Sopenharmony_ci	.queuecommand = ibmvfc_queuecommand,
31658c2ecf20Sopenharmony_ci	.eh_timed_out = fc_eh_timed_out,
31668c2ecf20Sopenharmony_ci	.eh_abort_handler = ibmvfc_eh_abort_handler,
31678c2ecf20Sopenharmony_ci	.eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
31688c2ecf20Sopenharmony_ci	.eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
31698c2ecf20Sopenharmony_ci	.eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
31708c2ecf20Sopenharmony_ci	.slave_alloc = ibmvfc_slave_alloc,
31718c2ecf20Sopenharmony_ci	.slave_configure = ibmvfc_slave_configure,
31728c2ecf20Sopenharmony_ci	.target_alloc = ibmvfc_target_alloc,
31738c2ecf20Sopenharmony_ci	.scan_finished = ibmvfc_scan_finished,
31748c2ecf20Sopenharmony_ci	.change_queue_depth = ibmvfc_change_queue_depth,
31758c2ecf20Sopenharmony_ci	.cmd_per_lun = 16,
31768c2ecf20Sopenharmony_ci	.can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
31778c2ecf20Sopenharmony_ci	.this_id = -1,
31788c2ecf20Sopenharmony_ci	.sg_tablesize = SG_ALL,
31798c2ecf20Sopenharmony_ci	.max_sectors = IBMVFC_MAX_SECTORS,
31808c2ecf20Sopenharmony_ci	.shost_attrs = ibmvfc_attrs,
31818c2ecf20Sopenharmony_ci	.track_queue_depth = 1,
31828c2ecf20Sopenharmony_ci};
31838c2ecf20Sopenharmony_ci
31848c2ecf20Sopenharmony_ci/**
31858c2ecf20Sopenharmony_ci * ibmvfc_next_async_crq - Returns the next entry in async queue
31868c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
31878c2ecf20Sopenharmony_ci *
31888c2ecf20Sopenharmony_ci * Returns:
31898c2ecf20Sopenharmony_ci *	Pointer to next entry in queue / NULL if empty
31908c2ecf20Sopenharmony_ci **/
31918c2ecf20Sopenharmony_cistatic struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
31928c2ecf20Sopenharmony_ci{
31938c2ecf20Sopenharmony_ci	struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
31948c2ecf20Sopenharmony_ci	struct ibmvfc_async_crq *crq;
31958c2ecf20Sopenharmony_ci
31968c2ecf20Sopenharmony_ci	crq = &async_crq->msgs[async_crq->cur];
31978c2ecf20Sopenharmony_ci	if (crq->valid & 0x80) {
31988c2ecf20Sopenharmony_ci		if (++async_crq->cur == async_crq->size)
31998c2ecf20Sopenharmony_ci			async_crq->cur = 0;
32008c2ecf20Sopenharmony_ci		rmb();
32018c2ecf20Sopenharmony_ci	} else
32028c2ecf20Sopenharmony_ci		crq = NULL;
32038c2ecf20Sopenharmony_ci
32048c2ecf20Sopenharmony_ci	return crq;
32058c2ecf20Sopenharmony_ci}
32068c2ecf20Sopenharmony_ci
32078c2ecf20Sopenharmony_ci/**
32088c2ecf20Sopenharmony_ci * ibmvfc_next_crq - Returns the next entry in message queue
32098c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
32108c2ecf20Sopenharmony_ci *
32118c2ecf20Sopenharmony_ci * Returns:
32128c2ecf20Sopenharmony_ci *	Pointer to next entry in queue / NULL if empty
32138c2ecf20Sopenharmony_ci **/
32148c2ecf20Sopenharmony_cistatic struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
32158c2ecf20Sopenharmony_ci{
32168c2ecf20Sopenharmony_ci	struct ibmvfc_crq_queue *queue = &vhost->crq;
32178c2ecf20Sopenharmony_ci	struct ibmvfc_crq *crq;
32188c2ecf20Sopenharmony_ci
32198c2ecf20Sopenharmony_ci	crq = &queue->msgs[queue->cur];
32208c2ecf20Sopenharmony_ci	if (crq->valid & 0x80) {
32218c2ecf20Sopenharmony_ci		if (++queue->cur == queue->size)
32228c2ecf20Sopenharmony_ci			queue->cur = 0;
32238c2ecf20Sopenharmony_ci		rmb();
32248c2ecf20Sopenharmony_ci	} else
32258c2ecf20Sopenharmony_ci		crq = NULL;
32268c2ecf20Sopenharmony_ci
32278c2ecf20Sopenharmony_ci	return crq;
32288c2ecf20Sopenharmony_ci}
32298c2ecf20Sopenharmony_ci
32308c2ecf20Sopenharmony_ci/**
32318c2ecf20Sopenharmony_ci * ibmvfc_interrupt - Interrupt handler
32328c2ecf20Sopenharmony_ci * @irq:		number of irq to handle, not used
32338c2ecf20Sopenharmony_ci * @dev_instance: ibmvfc_host that received interrupt
32348c2ecf20Sopenharmony_ci *
32358c2ecf20Sopenharmony_ci * Returns:
32368c2ecf20Sopenharmony_ci *	IRQ_HANDLED
32378c2ecf20Sopenharmony_ci **/
32388c2ecf20Sopenharmony_cistatic irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
32398c2ecf20Sopenharmony_ci{
32408c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
32418c2ecf20Sopenharmony_ci	unsigned long flags;
32428c2ecf20Sopenharmony_ci
32438c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
32448c2ecf20Sopenharmony_ci	vio_disable_interrupts(to_vio_dev(vhost->dev));
32458c2ecf20Sopenharmony_ci	tasklet_schedule(&vhost->tasklet);
32468c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
32478c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
32488c2ecf20Sopenharmony_ci}
32498c2ecf20Sopenharmony_ci
32508c2ecf20Sopenharmony_ci/**
32518c2ecf20Sopenharmony_ci * ibmvfc_tasklet - Interrupt handler tasklet
32528c2ecf20Sopenharmony_ci * @data:		ibmvfc host struct
32538c2ecf20Sopenharmony_ci *
32548c2ecf20Sopenharmony_ci * Returns:
32558c2ecf20Sopenharmony_ci *	Nothing
32568c2ecf20Sopenharmony_ci **/
32578c2ecf20Sopenharmony_cistatic void ibmvfc_tasklet(void *data)
32588c2ecf20Sopenharmony_ci{
32598c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = data;
32608c2ecf20Sopenharmony_ci	struct vio_dev *vdev = to_vio_dev(vhost->dev);
32618c2ecf20Sopenharmony_ci	struct ibmvfc_crq *crq;
32628c2ecf20Sopenharmony_ci	struct ibmvfc_async_crq *async;
32638c2ecf20Sopenharmony_ci	unsigned long flags;
32648c2ecf20Sopenharmony_ci	int done = 0;
32658c2ecf20Sopenharmony_ci
32668c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
32678c2ecf20Sopenharmony_ci	while (!done) {
32688c2ecf20Sopenharmony_ci		/* Pull all the valid messages off the async CRQ */
32698c2ecf20Sopenharmony_ci		while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
32708c2ecf20Sopenharmony_ci			ibmvfc_handle_async(async, vhost);
32718c2ecf20Sopenharmony_ci			async->valid = 0;
32728c2ecf20Sopenharmony_ci			wmb();
32738c2ecf20Sopenharmony_ci		}
32748c2ecf20Sopenharmony_ci
32758c2ecf20Sopenharmony_ci		/* Pull all the valid messages off the CRQ */
32768c2ecf20Sopenharmony_ci		while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
32778c2ecf20Sopenharmony_ci			ibmvfc_handle_crq(crq, vhost);
32788c2ecf20Sopenharmony_ci			crq->valid = 0;
32798c2ecf20Sopenharmony_ci			wmb();
32808c2ecf20Sopenharmony_ci		}
32818c2ecf20Sopenharmony_ci
32828c2ecf20Sopenharmony_ci		vio_enable_interrupts(vdev);
32838c2ecf20Sopenharmony_ci		if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
32848c2ecf20Sopenharmony_ci			vio_disable_interrupts(vdev);
32858c2ecf20Sopenharmony_ci			ibmvfc_handle_async(async, vhost);
32868c2ecf20Sopenharmony_ci			async->valid = 0;
32878c2ecf20Sopenharmony_ci			wmb();
32888c2ecf20Sopenharmony_ci		} else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
32898c2ecf20Sopenharmony_ci			vio_disable_interrupts(vdev);
32908c2ecf20Sopenharmony_ci			ibmvfc_handle_crq(crq, vhost);
32918c2ecf20Sopenharmony_ci			crq->valid = 0;
32928c2ecf20Sopenharmony_ci			wmb();
32938c2ecf20Sopenharmony_ci		} else
32948c2ecf20Sopenharmony_ci			done = 1;
32958c2ecf20Sopenharmony_ci	}
32968c2ecf20Sopenharmony_ci
32978c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
32988c2ecf20Sopenharmony_ci}
32998c2ecf20Sopenharmony_ci
33008c2ecf20Sopenharmony_ci/**
33018c2ecf20Sopenharmony_ci * ibmvfc_init_tgt - Set the next init job step for the target
33028c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
33038c2ecf20Sopenharmony_ci * @job_step:	job step to perform
33048c2ecf20Sopenharmony_ci *
33058c2ecf20Sopenharmony_ci **/
33068c2ecf20Sopenharmony_cistatic void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
33078c2ecf20Sopenharmony_ci			    void (*job_step) (struct ibmvfc_target *))
33088c2ecf20Sopenharmony_ci{
33098c2ecf20Sopenharmony_ci	if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
33108c2ecf20Sopenharmony_ci		tgt->job_step = job_step;
33118c2ecf20Sopenharmony_ci	wake_up(&tgt->vhost->work_wait_q);
33128c2ecf20Sopenharmony_ci}
33138c2ecf20Sopenharmony_ci
33148c2ecf20Sopenharmony_ci/**
33158c2ecf20Sopenharmony_ci * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
33168c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
33178c2ecf20Sopenharmony_ci * @job_step:	initialization job step
33188c2ecf20Sopenharmony_ci *
33198c2ecf20Sopenharmony_ci * Returns: 1 if step will be retried / 0 if not
33208c2ecf20Sopenharmony_ci *
33218c2ecf20Sopenharmony_ci **/
33228c2ecf20Sopenharmony_cistatic int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
33238c2ecf20Sopenharmony_ci				  void (*job_step) (struct ibmvfc_target *))
33248c2ecf20Sopenharmony_ci{
33258c2ecf20Sopenharmony_ci	if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
33268c2ecf20Sopenharmony_ci		ibmvfc_del_tgt(tgt);
33278c2ecf20Sopenharmony_ci		wake_up(&tgt->vhost->work_wait_q);
33288c2ecf20Sopenharmony_ci		return 0;
33298c2ecf20Sopenharmony_ci	} else
33308c2ecf20Sopenharmony_ci		ibmvfc_init_tgt(tgt, job_step);
33318c2ecf20Sopenharmony_ci	return 1;
33328c2ecf20Sopenharmony_ci}
33338c2ecf20Sopenharmony_ci
33348c2ecf20Sopenharmony_ci/* Defined in FC-LS */
33358c2ecf20Sopenharmony_cistatic const struct {
33368c2ecf20Sopenharmony_ci	int code;
33378c2ecf20Sopenharmony_ci	int retry;
33388c2ecf20Sopenharmony_ci	int logged_in;
33398c2ecf20Sopenharmony_ci} prli_rsp [] = {
33408c2ecf20Sopenharmony_ci	{ 0, 1, 0 },
33418c2ecf20Sopenharmony_ci	{ 1, 0, 1 },
33428c2ecf20Sopenharmony_ci	{ 2, 1, 0 },
33438c2ecf20Sopenharmony_ci	{ 3, 1, 0 },
33448c2ecf20Sopenharmony_ci	{ 4, 0, 0 },
33458c2ecf20Sopenharmony_ci	{ 5, 0, 0 },
33468c2ecf20Sopenharmony_ci	{ 6, 0, 1 },
33478c2ecf20Sopenharmony_ci	{ 7, 0, 0 },
33488c2ecf20Sopenharmony_ci	{ 8, 1, 0 },
33498c2ecf20Sopenharmony_ci};
33508c2ecf20Sopenharmony_ci
33518c2ecf20Sopenharmony_ci/**
33528c2ecf20Sopenharmony_ci * ibmvfc_get_prli_rsp - Find PRLI response index
33538c2ecf20Sopenharmony_ci * @flags:	PRLI response flags
33548c2ecf20Sopenharmony_ci *
33558c2ecf20Sopenharmony_ci **/
33568c2ecf20Sopenharmony_cistatic int ibmvfc_get_prli_rsp(u16 flags)
33578c2ecf20Sopenharmony_ci{
33588c2ecf20Sopenharmony_ci	int i;
33598c2ecf20Sopenharmony_ci	int code = (flags & 0x0f00) >> 8;
33608c2ecf20Sopenharmony_ci
33618c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
33628c2ecf20Sopenharmony_ci		if (prli_rsp[i].code == code)
33638c2ecf20Sopenharmony_ci			return i;
33648c2ecf20Sopenharmony_ci
33658c2ecf20Sopenharmony_ci	return 0;
33668c2ecf20Sopenharmony_ci}
33678c2ecf20Sopenharmony_ci
33688c2ecf20Sopenharmony_ci/**
33698c2ecf20Sopenharmony_ci * ibmvfc_tgt_prli_done - Completion handler for Process Login
33708c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
33718c2ecf20Sopenharmony_ci *
33728c2ecf20Sopenharmony_ci **/
33738c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
33748c2ecf20Sopenharmony_ci{
33758c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = evt->tgt;
33768c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
33778c2ecf20Sopenharmony_ci	struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
33788c2ecf20Sopenharmony_ci	struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
33798c2ecf20Sopenharmony_ci	u32 status = be16_to_cpu(rsp->common.status);
33808c2ecf20Sopenharmony_ci	int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
33818c2ecf20Sopenharmony_ci
33828c2ecf20Sopenharmony_ci	vhost->discovery_threads--;
33838c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
33848c2ecf20Sopenharmony_ci	switch (status) {
33858c2ecf20Sopenharmony_ci	case IBMVFC_MAD_SUCCESS:
33868c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
33878c2ecf20Sopenharmony_ci			parms->type, parms->flags, parms->service_parms);
33888c2ecf20Sopenharmony_ci
33898c2ecf20Sopenharmony_ci		if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
33908c2ecf20Sopenharmony_ci			index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
33918c2ecf20Sopenharmony_ci			if (prli_rsp[index].logged_in) {
33928c2ecf20Sopenharmony_ci				if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
33938c2ecf20Sopenharmony_ci					tgt->need_login = 0;
33948c2ecf20Sopenharmony_ci					tgt->ids.roles = 0;
33958c2ecf20Sopenharmony_ci					if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
33968c2ecf20Sopenharmony_ci						tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
33978c2ecf20Sopenharmony_ci					if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
33988c2ecf20Sopenharmony_ci						tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
33998c2ecf20Sopenharmony_ci					tgt->add_rport = 1;
34008c2ecf20Sopenharmony_ci				} else
34018c2ecf20Sopenharmony_ci					ibmvfc_del_tgt(tgt);
34028c2ecf20Sopenharmony_ci			} else if (prli_rsp[index].retry)
34038c2ecf20Sopenharmony_ci				ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
34048c2ecf20Sopenharmony_ci			else
34058c2ecf20Sopenharmony_ci				ibmvfc_del_tgt(tgt);
34068c2ecf20Sopenharmony_ci		} else
34078c2ecf20Sopenharmony_ci			ibmvfc_del_tgt(tgt);
34088c2ecf20Sopenharmony_ci		break;
34098c2ecf20Sopenharmony_ci	case IBMVFC_MAD_DRIVER_FAILED:
34108c2ecf20Sopenharmony_ci		break;
34118c2ecf20Sopenharmony_ci	case IBMVFC_MAD_CRQ_ERROR:
34128c2ecf20Sopenharmony_ci		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
34138c2ecf20Sopenharmony_ci		break;
34148c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FAILED:
34158c2ecf20Sopenharmony_ci	default:
34168c2ecf20Sopenharmony_ci		if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
34178c2ecf20Sopenharmony_ci		     be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
34188c2ecf20Sopenharmony_ci			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
34198c2ecf20Sopenharmony_ci		else if (tgt->logo_rcvd)
34208c2ecf20Sopenharmony_ci			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
34218c2ecf20Sopenharmony_ci		else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
34228c2ecf20Sopenharmony_ci			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
34238c2ecf20Sopenharmony_ci		else
34248c2ecf20Sopenharmony_ci			ibmvfc_del_tgt(tgt);
34258c2ecf20Sopenharmony_ci
34268c2ecf20Sopenharmony_ci		tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
34278c2ecf20Sopenharmony_ci			ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
34288c2ecf20Sopenharmony_ci			be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
34298c2ecf20Sopenharmony_ci		break;
34308c2ecf20Sopenharmony_ci	}
34318c2ecf20Sopenharmony_ci
34328c2ecf20Sopenharmony_ci	kref_put(&tgt->kref, ibmvfc_release_tgt);
34338c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
34348c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
34358c2ecf20Sopenharmony_ci}
34368c2ecf20Sopenharmony_ci
34378c2ecf20Sopenharmony_ci/**
34388c2ecf20Sopenharmony_ci * ibmvfc_tgt_send_prli - Send a process login
34398c2ecf20Sopenharmony_ci * @tgt:	ibmvfc target struct
34408c2ecf20Sopenharmony_ci *
34418c2ecf20Sopenharmony_ci **/
34428c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
34438c2ecf20Sopenharmony_ci{
34448c2ecf20Sopenharmony_ci	struct ibmvfc_process_login *prli;
34458c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
34468c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
34478c2ecf20Sopenharmony_ci
34488c2ecf20Sopenharmony_ci	if (vhost->discovery_threads >= disc_threads)
34498c2ecf20Sopenharmony_ci		return;
34508c2ecf20Sopenharmony_ci
34518c2ecf20Sopenharmony_ci	kref_get(&tgt->kref);
34528c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
34538c2ecf20Sopenharmony_ci	vhost->discovery_threads++;
34548c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
34558c2ecf20Sopenharmony_ci	evt->tgt = tgt;
34568c2ecf20Sopenharmony_ci	prli = &evt->iu.prli;
34578c2ecf20Sopenharmony_ci	memset(prli, 0, sizeof(*prli));
34588c2ecf20Sopenharmony_ci	prli->common.version = cpu_to_be32(1);
34598c2ecf20Sopenharmony_ci	prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
34608c2ecf20Sopenharmony_ci	prli->common.length = cpu_to_be16(sizeof(*prli));
34618c2ecf20Sopenharmony_ci	prli->scsi_id = cpu_to_be64(tgt->scsi_id);
34628c2ecf20Sopenharmony_ci
34638c2ecf20Sopenharmony_ci	prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
34648c2ecf20Sopenharmony_ci	prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
34658c2ecf20Sopenharmony_ci	prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
34668c2ecf20Sopenharmony_ci	prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
34678c2ecf20Sopenharmony_ci
34688c2ecf20Sopenharmony_ci	if (cls3_error)
34698c2ecf20Sopenharmony_ci		prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
34708c2ecf20Sopenharmony_ci
34718c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
34728c2ecf20Sopenharmony_ci	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
34738c2ecf20Sopenharmony_ci		vhost->discovery_threads--;
34748c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
34758c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
34768c2ecf20Sopenharmony_ci	} else
34778c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Sent process login\n");
34788c2ecf20Sopenharmony_ci}
34798c2ecf20Sopenharmony_ci
34808c2ecf20Sopenharmony_ci/**
34818c2ecf20Sopenharmony_ci * ibmvfc_tgt_plogi_done - Completion handler for Port Login
34828c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
34838c2ecf20Sopenharmony_ci *
34848c2ecf20Sopenharmony_ci **/
34858c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
34868c2ecf20Sopenharmony_ci{
34878c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = evt->tgt;
34888c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
34898c2ecf20Sopenharmony_ci	struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
34908c2ecf20Sopenharmony_ci	u32 status = be16_to_cpu(rsp->common.status);
34918c2ecf20Sopenharmony_ci	int level = IBMVFC_DEFAULT_LOG_LEVEL;
34928c2ecf20Sopenharmony_ci
34938c2ecf20Sopenharmony_ci	vhost->discovery_threads--;
34948c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
34958c2ecf20Sopenharmony_ci	switch (status) {
34968c2ecf20Sopenharmony_ci	case IBMVFC_MAD_SUCCESS:
34978c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Port Login succeeded\n");
34988c2ecf20Sopenharmony_ci		if (tgt->ids.port_name &&
34998c2ecf20Sopenharmony_ci		    tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
35008c2ecf20Sopenharmony_ci			vhost->reinit = 1;
35018c2ecf20Sopenharmony_ci			tgt_dbg(tgt, "Port re-init required\n");
35028c2ecf20Sopenharmony_ci			break;
35038c2ecf20Sopenharmony_ci		}
35048c2ecf20Sopenharmony_ci		tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
35058c2ecf20Sopenharmony_ci		tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
35068c2ecf20Sopenharmony_ci		tgt->ids.port_id = tgt->scsi_id;
35078c2ecf20Sopenharmony_ci		memcpy(&tgt->service_parms, &rsp->service_parms,
35088c2ecf20Sopenharmony_ci		       sizeof(tgt->service_parms));
35098c2ecf20Sopenharmony_ci		memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
35108c2ecf20Sopenharmony_ci		       sizeof(tgt->service_parms_change));
35118c2ecf20Sopenharmony_ci		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
35128c2ecf20Sopenharmony_ci		break;
35138c2ecf20Sopenharmony_ci	case IBMVFC_MAD_DRIVER_FAILED:
35148c2ecf20Sopenharmony_ci		break;
35158c2ecf20Sopenharmony_ci	case IBMVFC_MAD_CRQ_ERROR:
35168c2ecf20Sopenharmony_ci		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
35178c2ecf20Sopenharmony_ci		break;
35188c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FAILED:
35198c2ecf20Sopenharmony_ci	default:
35208c2ecf20Sopenharmony_ci		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
35218c2ecf20Sopenharmony_ci			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
35228c2ecf20Sopenharmony_ci		else
35238c2ecf20Sopenharmony_ci			ibmvfc_del_tgt(tgt);
35248c2ecf20Sopenharmony_ci
35258c2ecf20Sopenharmony_ci		tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
35268c2ecf20Sopenharmony_ci			ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
35278c2ecf20Sopenharmony_ci					     be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
35288c2ecf20Sopenharmony_ci			ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
35298c2ecf20Sopenharmony_ci			ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
35308c2ecf20Sopenharmony_ci		break;
35318c2ecf20Sopenharmony_ci	}
35328c2ecf20Sopenharmony_ci
35338c2ecf20Sopenharmony_ci	kref_put(&tgt->kref, ibmvfc_release_tgt);
35348c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
35358c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
35368c2ecf20Sopenharmony_ci}
35378c2ecf20Sopenharmony_ci
35388c2ecf20Sopenharmony_ci/**
35398c2ecf20Sopenharmony_ci * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
35408c2ecf20Sopenharmony_ci * @tgt:	ibmvfc target struct
35418c2ecf20Sopenharmony_ci *
35428c2ecf20Sopenharmony_ci **/
35438c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
35448c2ecf20Sopenharmony_ci{
35458c2ecf20Sopenharmony_ci	struct ibmvfc_port_login *plogi;
35468c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
35478c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
35488c2ecf20Sopenharmony_ci
35498c2ecf20Sopenharmony_ci	if (vhost->discovery_threads >= disc_threads)
35508c2ecf20Sopenharmony_ci		return;
35518c2ecf20Sopenharmony_ci
35528c2ecf20Sopenharmony_ci	kref_get(&tgt->kref);
35538c2ecf20Sopenharmony_ci	tgt->logo_rcvd = 0;
35548c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
35558c2ecf20Sopenharmony_ci	vhost->discovery_threads++;
35568c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
35578c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
35588c2ecf20Sopenharmony_ci	evt->tgt = tgt;
35598c2ecf20Sopenharmony_ci	plogi = &evt->iu.plogi;
35608c2ecf20Sopenharmony_ci	memset(plogi, 0, sizeof(*plogi));
35618c2ecf20Sopenharmony_ci	plogi->common.version = cpu_to_be32(1);
35628c2ecf20Sopenharmony_ci	plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
35638c2ecf20Sopenharmony_ci	plogi->common.length = cpu_to_be16(sizeof(*plogi));
35648c2ecf20Sopenharmony_ci	plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
35658c2ecf20Sopenharmony_ci
35668c2ecf20Sopenharmony_ci	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
35678c2ecf20Sopenharmony_ci		vhost->discovery_threads--;
35688c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
35698c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
35708c2ecf20Sopenharmony_ci	} else
35718c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Sent port login\n");
35728c2ecf20Sopenharmony_ci}
35738c2ecf20Sopenharmony_ci
35748c2ecf20Sopenharmony_ci/**
35758c2ecf20Sopenharmony_ci * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
35768c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
35778c2ecf20Sopenharmony_ci *
35788c2ecf20Sopenharmony_ci **/
35798c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
35808c2ecf20Sopenharmony_ci{
35818c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = evt->tgt;
35828c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
35838c2ecf20Sopenharmony_ci	struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
35848c2ecf20Sopenharmony_ci	u32 status = be16_to_cpu(rsp->common.status);
35858c2ecf20Sopenharmony_ci
35868c2ecf20Sopenharmony_ci	vhost->discovery_threads--;
35878c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
35888c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
35898c2ecf20Sopenharmony_ci
35908c2ecf20Sopenharmony_ci	switch (status) {
35918c2ecf20Sopenharmony_ci	case IBMVFC_MAD_SUCCESS:
35928c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Implicit Logout succeeded\n");
35938c2ecf20Sopenharmony_ci		break;
35948c2ecf20Sopenharmony_ci	case IBMVFC_MAD_DRIVER_FAILED:
35958c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
35968c2ecf20Sopenharmony_ci		wake_up(&vhost->work_wait_q);
35978c2ecf20Sopenharmony_ci		return;
35988c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FAILED:
35998c2ecf20Sopenharmony_ci	default:
36008c2ecf20Sopenharmony_ci		tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
36018c2ecf20Sopenharmony_ci		break;
36028c2ecf20Sopenharmony_ci	}
36038c2ecf20Sopenharmony_ci
36048c2ecf20Sopenharmony_ci	ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
36058c2ecf20Sopenharmony_ci	kref_put(&tgt->kref, ibmvfc_release_tgt);
36068c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
36078c2ecf20Sopenharmony_ci}
36088c2ecf20Sopenharmony_ci
36098c2ecf20Sopenharmony_ci/**
36108c2ecf20Sopenharmony_ci * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
36118c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
36128c2ecf20Sopenharmony_ci *
36138c2ecf20Sopenharmony_ci * Returns:
36148c2ecf20Sopenharmony_ci *	Allocated and initialized ibmvfc_event struct
36158c2ecf20Sopenharmony_ci **/
36168c2ecf20Sopenharmony_cistatic struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
36178c2ecf20Sopenharmony_ci								 void (*done) (struct ibmvfc_event *))
36188c2ecf20Sopenharmony_ci{
36198c2ecf20Sopenharmony_ci	struct ibmvfc_implicit_logout *mad;
36208c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
36218c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
36228c2ecf20Sopenharmony_ci
36238c2ecf20Sopenharmony_ci	kref_get(&tgt->kref);
36248c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
36258c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
36268c2ecf20Sopenharmony_ci	evt->tgt = tgt;
36278c2ecf20Sopenharmony_ci	mad = &evt->iu.implicit_logout;
36288c2ecf20Sopenharmony_ci	memset(mad, 0, sizeof(*mad));
36298c2ecf20Sopenharmony_ci	mad->common.version = cpu_to_be32(1);
36308c2ecf20Sopenharmony_ci	mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
36318c2ecf20Sopenharmony_ci	mad->common.length = cpu_to_be16(sizeof(*mad));
36328c2ecf20Sopenharmony_ci	mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
36338c2ecf20Sopenharmony_ci	return evt;
36348c2ecf20Sopenharmony_ci}
36358c2ecf20Sopenharmony_ci
36368c2ecf20Sopenharmony_ci/**
36378c2ecf20Sopenharmony_ci * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
36388c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
36398c2ecf20Sopenharmony_ci *
36408c2ecf20Sopenharmony_ci **/
36418c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
36428c2ecf20Sopenharmony_ci{
36438c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
36448c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
36458c2ecf20Sopenharmony_ci
36468c2ecf20Sopenharmony_ci	if (vhost->discovery_threads >= disc_threads)
36478c2ecf20Sopenharmony_ci		return;
36488c2ecf20Sopenharmony_ci
36498c2ecf20Sopenharmony_ci	vhost->discovery_threads++;
36508c2ecf20Sopenharmony_ci	evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
36518c2ecf20Sopenharmony_ci						   ibmvfc_tgt_implicit_logout_done);
36528c2ecf20Sopenharmony_ci
36538c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
36548c2ecf20Sopenharmony_ci	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
36558c2ecf20Sopenharmony_ci		vhost->discovery_threads--;
36568c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
36578c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
36588c2ecf20Sopenharmony_ci	} else
36598c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Sent Implicit Logout\n");
36608c2ecf20Sopenharmony_ci}
36618c2ecf20Sopenharmony_ci
36628c2ecf20Sopenharmony_ci/**
36638c2ecf20Sopenharmony_ci * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
36648c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
36658c2ecf20Sopenharmony_ci *
36668c2ecf20Sopenharmony_ci **/
36678c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
36688c2ecf20Sopenharmony_ci{
36698c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = evt->tgt;
36708c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
36718c2ecf20Sopenharmony_ci	struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
36728c2ecf20Sopenharmony_ci	u32 status = be16_to_cpu(mad->common.status);
36738c2ecf20Sopenharmony_ci
36748c2ecf20Sopenharmony_ci	vhost->discovery_threads--;
36758c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
36768c2ecf20Sopenharmony_ci
36778c2ecf20Sopenharmony_ci	/*
36788c2ecf20Sopenharmony_ci	 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
36798c2ecf20Sopenharmony_ci	 * driver in which case we need to free up all the targets. If we are
36808c2ecf20Sopenharmony_ci	 * not unloading, we will still go through a hard reset to get out of
36818c2ecf20Sopenharmony_ci	 * offline state, so there is no need to track the old targets in that
36828c2ecf20Sopenharmony_ci	 * case.
36838c2ecf20Sopenharmony_ci	 */
36848c2ecf20Sopenharmony_ci	if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
36858c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
36868c2ecf20Sopenharmony_ci	else
36878c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
36888c2ecf20Sopenharmony_ci
36898c2ecf20Sopenharmony_ci	tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
36908c2ecf20Sopenharmony_ci	kref_put(&tgt->kref, ibmvfc_release_tgt);
36918c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
36928c2ecf20Sopenharmony_ci}
36938c2ecf20Sopenharmony_ci
36948c2ecf20Sopenharmony_ci/**
36958c2ecf20Sopenharmony_ci * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
36968c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
36978c2ecf20Sopenharmony_ci *
36988c2ecf20Sopenharmony_ci **/
36998c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
37008c2ecf20Sopenharmony_ci{
37018c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
37028c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
37038c2ecf20Sopenharmony_ci
37048c2ecf20Sopenharmony_ci	if (!vhost->logged_in) {
37058c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
37068c2ecf20Sopenharmony_ci		return;
37078c2ecf20Sopenharmony_ci	}
37088c2ecf20Sopenharmony_ci
37098c2ecf20Sopenharmony_ci	if (vhost->discovery_threads >= disc_threads)
37108c2ecf20Sopenharmony_ci		return;
37118c2ecf20Sopenharmony_ci
37128c2ecf20Sopenharmony_ci	vhost->discovery_threads++;
37138c2ecf20Sopenharmony_ci	evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
37148c2ecf20Sopenharmony_ci						   ibmvfc_tgt_implicit_logout_and_del_done);
37158c2ecf20Sopenharmony_ci
37168c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
37178c2ecf20Sopenharmony_ci	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
37188c2ecf20Sopenharmony_ci		vhost->discovery_threads--;
37198c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
37208c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
37218c2ecf20Sopenharmony_ci	} else
37228c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Sent Implicit Logout\n");
37238c2ecf20Sopenharmony_ci}
37248c2ecf20Sopenharmony_ci
37258c2ecf20Sopenharmony_ci/**
37268c2ecf20Sopenharmony_ci * ibmvfc_tgt_move_login_done - Completion handler for Move Login
37278c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
37288c2ecf20Sopenharmony_ci *
37298c2ecf20Sopenharmony_ci **/
37308c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
37318c2ecf20Sopenharmony_ci{
37328c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = evt->tgt;
37338c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
37348c2ecf20Sopenharmony_ci	struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
37358c2ecf20Sopenharmony_ci	u32 status = be16_to_cpu(rsp->common.status);
37368c2ecf20Sopenharmony_ci	int level = IBMVFC_DEFAULT_LOG_LEVEL;
37378c2ecf20Sopenharmony_ci
37388c2ecf20Sopenharmony_ci	vhost->discovery_threads--;
37398c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
37408c2ecf20Sopenharmony_ci	switch (status) {
37418c2ecf20Sopenharmony_ci	case IBMVFC_MAD_SUCCESS:
37428c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Move Login succeeded for old scsi_id: %llX\n", tgt->old_scsi_id);
37438c2ecf20Sopenharmony_ci		tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
37448c2ecf20Sopenharmony_ci		tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
37458c2ecf20Sopenharmony_ci		tgt->ids.port_id = tgt->scsi_id;
37468c2ecf20Sopenharmony_ci		memcpy(&tgt->service_parms, &rsp->service_parms,
37478c2ecf20Sopenharmony_ci		       sizeof(tgt->service_parms));
37488c2ecf20Sopenharmony_ci		memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
37498c2ecf20Sopenharmony_ci		       sizeof(tgt->service_parms_change));
37508c2ecf20Sopenharmony_ci		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
37518c2ecf20Sopenharmony_ci		break;
37528c2ecf20Sopenharmony_ci	case IBMVFC_MAD_DRIVER_FAILED:
37538c2ecf20Sopenharmony_ci		break;
37548c2ecf20Sopenharmony_ci	case IBMVFC_MAD_CRQ_ERROR:
37558c2ecf20Sopenharmony_ci		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
37568c2ecf20Sopenharmony_ci		break;
37578c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FAILED:
37588c2ecf20Sopenharmony_ci	default:
37598c2ecf20Sopenharmony_ci		level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
37608c2ecf20Sopenharmony_ci
37618c2ecf20Sopenharmony_ci		tgt_log(tgt, level,
37628c2ecf20Sopenharmony_ci			"Move Login failed: old scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
37638c2ecf20Sopenharmony_ci			tgt->old_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
37648c2ecf20Sopenharmony_ci			status);
37658c2ecf20Sopenharmony_ci		break;
37668c2ecf20Sopenharmony_ci	}
37678c2ecf20Sopenharmony_ci
37688c2ecf20Sopenharmony_ci	kref_put(&tgt->kref, ibmvfc_release_tgt);
37698c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
37708c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
37718c2ecf20Sopenharmony_ci}
37728c2ecf20Sopenharmony_ci
37738c2ecf20Sopenharmony_ci
37748c2ecf20Sopenharmony_ci/**
37758c2ecf20Sopenharmony_ci * ibmvfc_tgt_move_login - Initiate a move login for specified target
37768c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
37778c2ecf20Sopenharmony_ci *
37788c2ecf20Sopenharmony_ci **/
37798c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
37808c2ecf20Sopenharmony_ci{
37818c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
37828c2ecf20Sopenharmony_ci	struct ibmvfc_move_login *move;
37838c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
37848c2ecf20Sopenharmony_ci
37858c2ecf20Sopenharmony_ci	if (vhost->discovery_threads >= disc_threads)
37868c2ecf20Sopenharmony_ci		return;
37878c2ecf20Sopenharmony_ci
37888c2ecf20Sopenharmony_ci	kref_get(&tgt->kref);
37898c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
37908c2ecf20Sopenharmony_ci	vhost->discovery_threads++;
37918c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
37928c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
37938c2ecf20Sopenharmony_ci	evt->tgt = tgt;
37948c2ecf20Sopenharmony_ci	move = &evt->iu.move_login;
37958c2ecf20Sopenharmony_ci	memset(move, 0, sizeof(*move));
37968c2ecf20Sopenharmony_ci	move->common.version = cpu_to_be32(1);
37978c2ecf20Sopenharmony_ci	move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
37988c2ecf20Sopenharmony_ci	move->common.length = cpu_to_be16(sizeof(*move));
37998c2ecf20Sopenharmony_ci
38008c2ecf20Sopenharmony_ci	move->old_scsi_id = cpu_to_be64(tgt->old_scsi_id);
38018c2ecf20Sopenharmony_ci	move->new_scsi_id = cpu_to_be64(tgt->scsi_id);
38028c2ecf20Sopenharmony_ci	move->wwpn = cpu_to_be64(tgt->wwpn);
38038c2ecf20Sopenharmony_ci	move->node_name = cpu_to_be64(tgt->ids.node_name);
38048c2ecf20Sopenharmony_ci
38058c2ecf20Sopenharmony_ci	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
38068c2ecf20Sopenharmony_ci		vhost->discovery_threads--;
38078c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
38088c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
38098c2ecf20Sopenharmony_ci	} else
38108c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Sent Move Login for old scsi_id: %llX\n", tgt->old_scsi_id);
38118c2ecf20Sopenharmony_ci}
38128c2ecf20Sopenharmony_ci
38138c2ecf20Sopenharmony_ci/**
38148c2ecf20Sopenharmony_ci * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
38158c2ecf20Sopenharmony_ci * @mad:	ibmvfc passthru mad struct
38168c2ecf20Sopenharmony_ci * @tgt:	ibmvfc target struct
38178c2ecf20Sopenharmony_ci *
38188c2ecf20Sopenharmony_ci * Returns:
38198c2ecf20Sopenharmony_ci *	1 if PLOGI needed / 0 if PLOGI not needed
38208c2ecf20Sopenharmony_ci **/
38218c2ecf20Sopenharmony_cistatic int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
38228c2ecf20Sopenharmony_ci				    struct ibmvfc_target *tgt)
38238c2ecf20Sopenharmony_ci{
38248c2ecf20Sopenharmony_ci	if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
38258c2ecf20Sopenharmony_ci		return 1;
38268c2ecf20Sopenharmony_ci	if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
38278c2ecf20Sopenharmony_ci		return 1;
38288c2ecf20Sopenharmony_ci	if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
38298c2ecf20Sopenharmony_ci		return 1;
38308c2ecf20Sopenharmony_ci	return 0;
38318c2ecf20Sopenharmony_ci}
38328c2ecf20Sopenharmony_ci
38338c2ecf20Sopenharmony_ci/**
38348c2ecf20Sopenharmony_ci * ibmvfc_tgt_adisc_done - Completion handler for ADISC
38358c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
38368c2ecf20Sopenharmony_ci *
38378c2ecf20Sopenharmony_ci **/
38388c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
38398c2ecf20Sopenharmony_ci{
38408c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = evt->tgt;
38418c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
38428c2ecf20Sopenharmony_ci	struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
38438c2ecf20Sopenharmony_ci	u32 status = be16_to_cpu(mad->common.status);
38448c2ecf20Sopenharmony_ci	u8 fc_reason, fc_explain;
38458c2ecf20Sopenharmony_ci
38468c2ecf20Sopenharmony_ci	vhost->discovery_threads--;
38478c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
38488c2ecf20Sopenharmony_ci	del_timer(&tgt->timer);
38498c2ecf20Sopenharmony_ci
38508c2ecf20Sopenharmony_ci	switch (status) {
38518c2ecf20Sopenharmony_ci	case IBMVFC_MAD_SUCCESS:
38528c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "ADISC succeeded\n");
38538c2ecf20Sopenharmony_ci		if (ibmvfc_adisc_needs_plogi(mad, tgt))
38548c2ecf20Sopenharmony_ci			ibmvfc_del_tgt(tgt);
38558c2ecf20Sopenharmony_ci		break;
38568c2ecf20Sopenharmony_ci	case IBMVFC_MAD_DRIVER_FAILED:
38578c2ecf20Sopenharmony_ci		break;
38588c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FAILED:
38598c2ecf20Sopenharmony_ci	default:
38608c2ecf20Sopenharmony_ci		ibmvfc_del_tgt(tgt);
38618c2ecf20Sopenharmony_ci		fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
38628c2ecf20Sopenharmony_ci		fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
38638c2ecf20Sopenharmony_ci		tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
38648c2ecf20Sopenharmony_ci			 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
38658c2ecf20Sopenharmony_ci			 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
38668c2ecf20Sopenharmony_ci			 ibmvfc_get_fc_type(fc_reason), fc_reason,
38678c2ecf20Sopenharmony_ci			 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
38688c2ecf20Sopenharmony_ci		break;
38698c2ecf20Sopenharmony_ci	}
38708c2ecf20Sopenharmony_ci
38718c2ecf20Sopenharmony_ci	kref_put(&tgt->kref, ibmvfc_release_tgt);
38728c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
38738c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
38748c2ecf20Sopenharmony_ci}
38758c2ecf20Sopenharmony_ci
38768c2ecf20Sopenharmony_ci/**
38778c2ecf20Sopenharmony_ci * ibmvfc_init_passthru - Initialize an event struct for FC passthru
38788c2ecf20Sopenharmony_ci * @evt:		ibmvfc event struct
38798c2ecf20Sopenharmony_ci *
38808c2ecf20Sopenharmony_ci **/
38818c2ecf20Sopenharmony_cistatic void ibmvfc_init_passthru(struct ibmvfc_event *evt)
38828c2ecf20Sopenharmony_ci{
38838c2ecf20Sopenharmony_ci	struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
38848c2ecf20Sopenharmony_ci
38858c2ecf20Sopenharmony_ci	memset(mad, 0, sizeof(*mad));
38868c2ecf20Sopenharmony_ci	mad->common.version = cpu_to_be32(1);
38878c2ecf20Sopenharmony_ci	mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
38888c2ecf20Sopenharmony_ci	mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
38898c2ecf20Sopenharmony_ci	mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
38908c2ecf20Sopenharmony_ci		offsetof(struct ibmvfc_passthru_mad, iu));
38918c2ecf20Sopenharmony_ci	mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
38928c2ecf20Sopenharmony_ci	mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
38938c2ecf20Sopenharmony_ci	mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
38948c2ecf20Sopenharmony_ci	mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
38958c2ecf20Sopenharmony_ci		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
38968c2ecf20Sopenharmony_ci		offsetof(struct ibmvfc_passthru_fc_iu, payload));
38978c2ecf20Sopenharmony_ci	mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
38988c2ecf20Sopenharmony_ci	mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
38998c2ecf20Sopenharmony_ci		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
39008c2ecf20Sopenharmony_ci		offsetof(struct ibmvfc_passthru_fc_iu, response));
39018c2ecf20Sopenharmony_ci	mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
39028c2ecf20Sopenharmony_ci}
39038c2ecf20Sopenharmony_ci
39048c2ecf20Sopenharmony_ci/**
39058c2ecf20Sopenharmony_ci * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
39068c2ecf20Sopenharmony_ci * @evt:		ibmvfc event struct
39078c2ecf20Sopenharmony_ci *
39088c2ecf20Sopenharmony_ci * Just cleanup this event struct. Everything else is handled by
39098c2ecf20Sopenharmony_ci * the ADISC completion handler. If the ADISC never actually comes
39108c2ecf20Sopenharmony_ci * back, we still have the timer running on the ADISC event struct
39118c2ecf20Sopenharmony_ci * which will fire and cause the CRQ to get reset.
39128c2ecf20Sopenharmony_ci *
39138c2ecf20Sopenharmony_ci **/
39148c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
39158c2ecf20Sopenharmony_ci{
39168c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
39178c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = evt->tgt;
39188c2ecf20Sopenharmony_ci
39198c2ecf20Sopenharmony_ci	tgt_dbg(tgt, "ADISC cancel complete\n");
39208c2ecf20Sopenharmony_ci	vhost->abort_threads--;
39218c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
39228c2ecf20Sopenharmony_ci	kref_put(&tgt->kref, ibmvfc_release_tgt);
39238c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
39248c2ecf20Sopenharmony_ci}
39258c2ecf20Sopenharmony_ci
39268c2ecf20Sopenharmony_ci/**
39278c2ecf20Sopenharmony_ci * ibmvfc_adisc_timeout - Handle an ADISC timeout
39288c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
39298c2ecf20Sopenharmony_ci *
39308c2ecf20Sopenharmony_ci * If an ADISC times out, send a cancel. If the cancel times
39318c2ecf20Sopenharmony_ci * out, reset the CRQ. When the ADISC comes back as cancelled,
39328c2ecf20Sopenharmony_ci * log back into the target.
39338c2ecf20Sopenharmony_ci **/
39348c2ecf20Sopenharmony_cistatic void ibmvfc_adisc_timeout(struct timer_list *t)
39358c2ecf20Sopenharmony_ci{
39368c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
39378c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
39388c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
39398c2ecf20Sopenharmony_ci	struct ibmvfc_tmf *tmf;
39408c2ecf20Sopenharmony_ci	unsigned long flags;
39418c2ecf20Sopenharmony_ci	int rc;
39428c2ecf20Sopenharmony_ci
39438c2ecf20Sopenharmony_ci	tgt_dbg(tgt, "ADISC timeout\n");
39448c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
39458c2ecf20Sopenharmony_ci	if (vhost->abort_threads >= disc_threads ||
39468c2ecf20Sopenharmony_ci	    tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
39478c2ecf20Sopenharmony_ci	    vhost->state != IBMVFC_INITIALIZING ||
39488c2ecf20Sopenharmony_ci	    vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
39498c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
39508c2ecf20Sopenharmony_ci		return;
39518c2ecf20Sopenharmony_ci	}
39528c2ecf20Sopenharmony_ci
39538c2ecf20Sopenharmony_ci	vhost->abort_threads++;
39548c2ecf20Sopenharmony_ci	kref_get(&tgt->kref);
39558c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
39568c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
39578c2ecf20Sopenharmony_ci
39588c2ecf20Sopenharmony_ci	evt->tgt = tgt;
39598c2ecf20Sopenharmony_ci	tmf = &evt->iu.tmf;
39608c2ecf20Sopenharmony_ci	memset(tmf, 0, sizeof(*tmf));
39618c2ecf20Sopenharmony_ci	tmf->common.version = cpu_to_be32(1);
39628c2ecf20Sopenharmony_ci	tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
39638c2ecf20Sopenharmony_ci	tmf->common.length = cpu_to_be16(sizeof(*tmf));
39648c2ecf20Sopenharmony_ci	tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
39658c2ecf20Sopenharmony_ci	tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
39668c2ecf20Sopenharmony_ci
39678c2ecf20Sopenharmony_ci	rc = ibmvfc_send_event(evt, vhost, default_timeout);
39688c2ecf20Sopenharmony_ci
39698c2ecf20Sopenharmony_ci	if (rc) {
39708c2ecf20Sopenharmony_ci		tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
39718c2ecf20Sopenharmony_ci		vhost->abort_threads--;
39728c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
39738c2ecf20Sopenharmony_ci		__ibmvfc_reset_host(vhost);
39748c2ecf20Sopenharmony_ci	} else
39758c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Attempting to cancel ADISC\n");
39768c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
39778c2ecf20Sopenharmony_ci}
39788c2ecf20Sopenharmony_ci
39798c2ecf20Sopenharmony_ci/**
39808c2ecf20Sopenharmony_ci * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
39818c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
39828c2ecf20Sopenharmony_ci *
39838c2ecf20Sopenharmony_ci * When sending an ADISC we end up with two timers running. The
39848c2ecf20Sopenharmony_ci * first timer is the timer in the ibmvfc target struct. If this
39858c2ecf20Sopenharmony_ci * fires, we send a cancel to the target. The second timer is the
39868c2ecf20Sopenharmony_ci * timer on the ibmvfc event for the ADISC, which is longer. If that
39878c2ecf20Sopenharmony_ci * fires, it means the ADISC timed out and our attempt to cancel it
39888c2ecf20Sopenharmony_ci * also failed, so we need to reset the CRQ.
39898c2ecf20Sopenharmony_ci **/
39908c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
39918c2ecf20Sopenharmony_ci{
39928c2ecf20Sopenharmony_ci	struct ibmvfc_passthru_mad *mad;
39938c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
39948c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
39958c2ecf20Sopenharmony_ci
39968c2ecf20Sopenharmony_ci	if (vhost->discovery_threads >= disc_threads)
39978c2ecf20Sopenharmony_ci		return;
39988c2ecf20Sopenharmony_ci
39998c2ecf20Sopenharmony_ci	kref_get(&tgt->kref);
40008c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
40018c2ecf20Sopenharmony_ci	vhost->discovery_threads++;
40028c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
40038c2ecf20Sopenharmony_ci	evt->tgt = tgt;
40048c2ecf20Sopenharmony_ci
40058c2ecf20Sopenharmony_ci	ibmvfc_init_passthru(evt);
40068c2ecf20Sopenharmony_ci	mad = &evt->iu.passthru;
40078c2ecf20Sopenharmony_ci	mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
40088c2ecf20Sopenharmony_ci	mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
40098c2ecf20Sopenharmony_ci	mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
40108c2ecf20Sopenharmony_ci
40118c2ecf20Sopenharmony_ci	mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
40128c2ecf20Sopenharmony_ci	memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
40138c2ecf20Sopenharmony_ci	       sizeof(vhost->login_buf->resp.port_name));
40148c2ecf20Sopenharmony_ci	memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
40158c2ecf20Sopenharmony_ci	       sizeof(vhost->login_buf->resp.node_name));
40168c2ecf20Sopenharmony_ci	mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
40178c2ecf20Sopenharmony_ci
40188c2ecf20Sopenharmony_ci	if (timer_pending(&tgt->timer))
40198c2ecf20Sopenharmony_ci		mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
40208c2ecf20Sopenharmony_ci	else {
40218c2ecf20Sopenharmony_ci		tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
40228c2ecf20Sopenharmony_ci		add_timer(&tgt->timer);
40238c2ecf20Sopenharmony_ci	}
40248c2ecf20Sopenharmony_ci
40258c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
40268c2ecf20Sopenharmony_ci	if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
40278c2ecf20Sopenharmony_ci		vhost->discovery_threads--;
40288c2ecf20Sopenharmony_ci		del_timer(&tgt->timer);
40298c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
40308c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
40318c2ecf20Sopenharmony_ci	} else
40328c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Sent ADISC\n");
40338c2ecf20Sopenharmony_ci}
40348c2ecf20Sopenharmony_ci
40358c2ecf20Sopenharmony_ci/**
40368c2ecf20Sopenharmony_ci * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
40378c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
40388c2ecf20Sopenharmony_ci *
40398c2ecf20Sopenharmony_ci **/
40408c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
40418c2ecf20Sopenharmony_ci{
40428c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt = evt->tgt;
40438c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
40448c2ecf20Sopenharmony_ci	struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
40458c2ecf20Sopenharmony_ci	u32 status = be16_to_cpu(rsp->common.status);
40468c2ecf20Sopenharmony_ci	int level = IBMVFC_DEFAULT_LOG_LEVEL;
40478c2ecf20Sopenharmony_ci
40488c2ecf20Sopenharmony_ci	vhost->discovery_threads--;
40498c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
40508c2ecf20Sopenharmony_ci	switch (status) {
40518c2ecf20Sopenharmony_ci	case IBMVFC_MAD_SUCCESS:
40528c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Query Target succeeded\n");
40538c2ecf20Sopenharmony_ci		if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
40548c2ecf20Sopenharmony_ci			ibmvfc_del_tgt(tgt);
40558c2ecf20Sopenharmony_ci		else
40568c2ecf20Sopenharmony_ci			ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
40578c2ecf20Sopenharmony_ci		break;
40588c2ecf20Sopenharmony_ci	case IBMVFC_MAD_DRIVER_FAILED:
40598c2ecf20Sopenharmony_ci		break;
40608c2ecf20Sopenharmony_ci	case IBMVFC_MAD_CRQ_ERROR:
40618c2ecf20Sopenharmony_ci		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
40628c2ecf20Sopenharmony_ci		break;
40638c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FAILED:
40648c2ecf20Sopenharmony_ci	default:
40658c2ecf20Sopenharmony_ci		if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
40668c2ecf20Sopenharmony_ci		    be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
40678c2ecf20Sopenharmony_ci		    be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
40688c2ecf20Sopenharmony_ci			ibmvfc_del_tgt(tgt);
40698c2ecf20Sopenharmony_ci		else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
40708c2ecf20Sopenharmony_ci			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
40718c2ecf20Sopenharmony_ci		else
40728c2ecf20Sopenharmony_ci			ibmvfc_del_tgt(tgt);
40738c2ecf20Sopenharmony_ci
40748c2ecf20Sopenharmony_ci		tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
40758c2ecf20Sopenharmony_ci			ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
40768c2ecf20Sopenharmony_ci			be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
40778c2ecf20Sopenharmony_ci			ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
40788c2ecf20Sopenharmony_ci			ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
40798c2ecf20Sopenharmony_ci			status);
40808c2ecf20Sopenharmony_ci		break;
40818c2ecf20Sopenharmony_ci	}
40828c2ecf20Sopenharmony_ci
40838c2ecf20Sopenharmony_ci	kref_put(&tgt->kref, ibmvfc_release_tgt);
40848c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
40858c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
40868c2ecf20Sopenharmony_ci}
40878c2ecf20Sopenharmony_ci
40888c2ecf20Sopenharmony_ci/**
40898c2ecf20Sopenharmony_ci * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
40908c2ecf20Sopenharmony_ci * @tgt:	ibmvfc target struct
40918c2ecf20Sopenharmony_ci *
40928c2ecf20Sopenharmony_ci **/
40938c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
40948c2ecf20Sopenharmony_ci{
40958c2ecf20Sopenharmony_ci	struct ibmvfc_query_tgt *query_tgt;
40968c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
40978c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
40988c2ecf20Sopenharmony_ci
40998c2ecf20Sopenharmony_ci	if (vhost->discovery_threads >= disc_threads)
41008c2ecf20Sopenharmony_ci		return;
41018c2ecf20Sopenharmony_ci
41028c2ecf20Sopenharmony_ci	kref_get(&tgt->kref);
41038c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
41048c2ecf20Sopenharmony_ci	vhost->discovery_threads++;
41058c2ecf20Sopenharmony_ci	evt->tgt = tgt;
41068c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
41078c2ecf20Sopenharmony_ci	query_tgt = &evt->iu.query_tgt;
41088c2ecf20Sopenharmony_ci	memset(query_tgt, 0, sizeof(*query_tgt));
41098c2ecf20Sopenharmony_ci	query_tgt->common.version = cpu_to_be32(1);
41108c2ecf20Sopenharmony_ci	query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
41118c2ecf20Sopenharmony_ci	query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
41128c2ecf20Sopenharmony_ci	query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
41138c2ecf20Sopenharmony_ci
41148c2ecf20Sopenharmony_ci	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
41158c2ecf20Sopenharmony_ci	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
41168c2ecf20Sopenharmony_ci		vhost->discovery_threads--;
41178c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
41188c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
41198c2ecf20Sopenharmony_ci	} else
41208c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Sent Query Target\n");
41218c2ecf20Sopenharmony_ci}
41228c2ecf20Sopenharmony_ci
41238c2ecf20Sopenharmony_ci/**
41248c2ecf20Sopenharmony_ci * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
41258c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
41268c2ecf20Sopenharmony_ci * @scsi_id:	SCSI ID to allocate target for
41278c2ecf20Sopenharmony_ci *
41288c2ecf20Sopenharmony_ci * Returns:
41298c2ecf20Sopenharmony_ci *	0 on success / other on failure
41308c2ecf20Sopenharmony_ci **/
41318c2ecf20Sopenharmony_cistatic int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
41328c2ecf20Sopenharmony_ci			       struct ibmvfc_discover_targets_entry *target)
41338c2ecf20Sopenharmony_ci{
41348c2ecf20Sopenharmony_ci	struct ibmvfc_target *stgt = NULL;
41358c2ecf20Sopenharmony_ci	struct ibmvfc_target *wtgt = NULL;
41368c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
41378c2ecf20Sopenharmony_ci	unsigned long flags;
41388c2ecf20Sopenharmony_ci	u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
41398c2ecf20Sopenharmony_ci	u64 wwpn = be64_to_cpu(target->wwpn);
41408c2ecf20Sopenharmony_ci
41418c2ecf20Sopenharmony_ci	/* Look to see if we already have a target allocated for this SCSI ID or WWPN */
41428c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
41438c2ecf20Sopenharmony_ci	list_for_each_entry(tgt, &vhost->targets, queue) {
41448c2ecf20Sopenharmony_ci		if (tgt->wwpn == wwpn) {
41458c2ecf20Sopenharmony_ci			wtgt = tgt;
41468c2ecf20Sopenharmony_ci			break;
41478c2ecf20Sopenharmony_ci		}
41488c2ecf20Sopenharmony_ci	}
41498c2ecf20Sopenharmony_ci
41508c2ecf20Sopenharmony_ci	list_for_each_entry(tgt, &vhost->targets, queue) {
41518c2ecf20Sopenharmony_ci		if (tgt->scsi_id == scsi_id) {
41528c2ecf20Sopenharmony_ci			stgt = tgt;
41538c2ecf20Sopenharmony_ci			break;
41548c2ecf20Sopenharmony_ci		}
41558c2ecf20Sopenharmony_ci	}
41568c2ecf20Sopenharmony_ci
41578c2ecf20Sopenharmony_ci	if (wtgt && !stgt) {
41588c2ecf20Sopenharmony_ci		/*
41598c2ecf20Sopenharmony_ci		 * A WWPN target has moved and we still are tracking the old
41608c2ecf20Sopenharmony_ci		 * SCSI ID.  The only way we should be able to get here is if
41618c2ecf20Sopenharmony_ci		 * we attempted to send an implicit logout for the old SCSI ID
41628c2ecf20Sopenharmony_ci		 * and it failed for some reason, such as there being I/O
41638c2ecf20Sopenharmony_ci		 * pending to the target. In this case, we will have already
41648c2ecf20Sopenharmony_ci		 * deleted the rport from the FC transport so we do a move
41658c2ecf20Sopenharmony_ci		 * login, which works even with I/O pending, as it will cancel
41668c2ecf20Sopenharmony_ci		 * any active commands.
41678c2ecf20Sopenharmony_ci		 */
41688c2ecf20Sopenharmony_ci		if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
41698c2ecf20Sopenharmony_ci			/*
41708c2ecf20Sopenharmony_ci			 * Do a move login here. The old target is no longer
41718c2ecf20Sopenharmony_ci			 * known to the transport layer We don't use the
41728c2ecf20Sopenharmony_ci			 * normal ibmvfc_set_tgt_action to set this, as we
41738c2ecf20Sopenharmony_ci			 * don't normally want to allow this state change.
41748c2ecf20Sopenharmony_ci			 */
41758c2ecf20Sopenharmony_ci			wtgt->old_scsi_id = wtgt->scsi_id;
41768c2ecf20Sopenharmony_ci			wtgt->scsi_id = scsi_id;
41778c2ecf20Sopenharmony_ci			wtgt->action = IBMVFC_TGT_ACTION_INIT;
41788c2ecf20Sopenharmony_ci			ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
41798c2ecf20Sopenharmony_ci			goto unlock_out;
41808c2ecf20Sopenharmony_ci		} else {
41818c2ecf20Sopenharmony_ci			tgt_err(wtgt, "Unexpected target state: %d, %p\n",
41828c2ecf20Sopenharmony_ci				wtgt->action, wtgt->rport);
41838c2ecf20Sopenharmony_ci		}
41848c2ecf20Sopenharmony_ci	} else if (stgt) {
41858c2ecf20Sopenharmony_ci		if (tgt->need_login)
41868c2ecf20Sopenharmony_ci			ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
41878c2ecf20Sopenharmony_ci		goto unlock_out;
41888c2ecf20Sopenharmony_ci	}
41898c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
41908c2ecf20Sopenharmony_ci
41918c2ecf20Sopenharmony_ci	tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
41928c2ecf20Sopenharmony_ci	memset(tgt, 0, sizeof(*tgt));
41938c2ecf20Sopenharmony_ci	tgt->scsi_id = scsi_id;
41948c2ecf20Sopenharmony_ci	tgt->wwpn = wwpn;
41958c2ecf20Sopenharmony_ci	tgt->vhost = vhost;
41968c2ecf20Sopenharmony_ci	tgt->need_login = 1;
41978c2ecf20Sopenharmony_ci	timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
41988c2ecf20Sopenharmony_ci	kref_init(&tgt->kref);
41998c2ecf20Sopenharmony_ci	ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
42008c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
42018c2ecf20Sopenharmony_ci	tgt->cancel_key = vhost->task_set++;
42028c2ecf20Sopenharmony_ci	list_add_tail(&tgt->queue, &vhost->targets);
42038c2ecf20Sopenharmony_ci
42048c2ecf20Sopenharmony_ciunlock_out:
42058c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
42068c2ecf20Sopenharmony_ci	return 0;
42078c2ecf20Sopenharmony_ci}
42088c2ecf20Sopenharmony_ci
42098c2ecf20Sopenharmony_ci/**
42108c2ecf20Sopenharmony_ci * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
42118c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
42128c2ecf20Sopenharmony_ci *
42138c2ecf20Sopenharmony_ci * Returns:
42148c2ecf20Sopenharmony_ci *	0 on success / other on failure
42158c2ecf20Sopenharmony_ci **/
42168c2ecf20Sopenharmony_cistatic int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
42178c2ecf20Sopenharmony_ci{
42188c2ecf20Sopenharmony_ci	int i, rc;
42198c2ecf20Sopenharmony_ci
42208c2ecf20Sopenharmony_ci	for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
42218c2ecf20Sopenharmony_ci		rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
42228c2ecf20Sopenharmony_ci
42238c2ecf20Sopenharmony_ci	return rc;
42248c2ecf20Sopenharmony_ci}
42258c2ecf20Sopenharmony_ci
42268c2ecf20Sopenharmony_ci/**
42278c2ecf20Sopenharmony_ci * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
42288c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
42298c2ecf20Sopenharmony_ci *
42308c2ecf20Sopenharmony_ci **/
42318c2ecf20Sopenharmony_cistatic void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
42328c2ecf20Sopenharmony_ci{
42338c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
42348c2ecf20Sopenharmony_ci	struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
42358c2ecf20Sopenharmony_ci	u32 mad_status = be16_to_cpu(rsp->common.status);
42368c2ecf20Sopenharmony_ci	int level = IBMVFC_DEFAULT_LOG_LEVEL;
42378c2ecf20Sopenharmony_ci
42388c2ecf20Sopenharmony_ci	switch (mad_status) {
42398c2ecf20Sopenharmony_ci	case IBMVFC_MAD_SUCCESS:
42408c2ecf20Sopenharmony_ci		ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
42418c2ecf20Sopenharmony_ci		vhost->num_targets = be32_to_cpu(rsp->num_written);
42428c2ecf20Sopenharmony_ci		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
42438c2ecf20Sopenharmony_ci		break;
42448c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FAILED:
42458c2ecf20Sopenharmony_ci		level += ibmvfc_retry_host_init(vhost);
42468c2ecf20Sopenharmony_ci		ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
42478c2ecf20Sopenharmony_ci			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
42488c2ecf20Sopenharmony_ci			   be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
42498c2ecf20Sopenharmony_ci		break;
42508c2ecf20Sopenharmony_ci	case IBMVFC_MAD_DRIVER_FAILED:
42518c2ecf20Sopenharmony_ci		break;
42528c2ecf20Sopenharmony_ci	default:
42538c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
42548c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
42558c2ecf20Sopenharmony_ci		break;
42568c2ecf20Sopenharmony_ci	}
42578c2ecf20Sopenharmony_ci
42588c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
42598c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
42608c2ecf20Sopenharmony_ci}
42618c2ecf20Sopenharmony_ci
42628c2ecf20Sopenharmony_ci/**
42638c2ecf20Sopenharmony_ci * ibmvfc_discover_targets - Send Discover Targets MAD
42648c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
42658c2ecf20Sopenharmony_ci *
42668c2ecf20Sopenharmony_ci **/
42678c2ecf20Sopenharmony_cistatic void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
42688c2ecf20Sopenharmony_ci{
42698c2ecf20Sopenharmony_ci	struct ibmvfc_discover_targets *mad;
42708c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
42718c2ecf20Sopenharmony_ci
42728c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
42738c2ecf20Sopenharmony_ci	mad = &evt->iu.discover_targets;
42748c2ecf20Sopenharmony_ci	memset(mad, 0, sizeof(*mad));
42758c2ecf20Sopenharmony_ci	mad->common.version = cpu_to_be32(1);
42768c2ecf20Sopenharmony_ci	mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
42778c2ecf20Sopenharmony_ci	mad->common.length = cpu_to_be16(sizeof(*mad));
42788c2ecf20Sopenharmony_ci	mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
42798c2ecf20Sopenharmony_ci	mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
42808c2ecf20Sopenharmony_ci	mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
42818c2ecf20Sopenharmony_ci	mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
42828c2ecf20Sopenharmony_ci	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
42838c2ecf20Sopenharmony_ci
42848c2ecf20Sopenharmony_ci	if (!ibmvfc_send_event(evt, vhost, default_timeout))
42858c2ecf20Sopenharmony_ci		ibmvfc_dbg(vhost, "Sent discover targets\n");
42868c2ecf20Sopenharmony_ci	else
42878c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
42888c2ecf20Sopenharmony_ci}
42898c2ecf20Sopenharmony_ci
42908c2ecf20Sopenharmony_ci/**
42918c2ecf20Sopenharmony_ci * ibmvfc_npiv_login_done - Completion handler for NPIV Login
42928c2ecf20Sopenharmony_ci * @evt:	ibmvfc event struct
42938c2ecf20Sopenharmony_ci *
42948c2ecf20Sopenharmony_ci **/
42958c2ecf20Sopenharmony_cistatic void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
42968c2ecf20Sopenharmony_ci{
42978c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
42988c2ecf20Sopenharmony_ci	u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
42998c2ecf20Sopenharmony_ci	struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
43008c2ecf20Sopenharmony_ci	unsigned int npiv_max_sectors;
43018c2ecf20Sopenharmony_ci	int level = IBMVFC_DEFAULT_LOG_LEVEL;
43028c2ecf20Sopenharmony_ci
43038c2ecf20Sopenharmony_ci	switch (mad_status) {
43048c2ecf20Sopenharmony_ci	case IBMVFC_MAD_SUCCESS:
43058c2ecf20Sopenharmony_ci		ibmvfc_free_event(evt);
43068c2ecf20Sopenharmony_ci		break;
43078c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FAILED:
43088c2ecf20Sopenharmony_ci		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
43098c2ecf20Sopenharmony_ci			level += ibmvfc_retry_host_init(vhost);
43108c2ecf20Sopenharmony_ci		else
43118c2ecf20Sopenharmony_ci			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
43128c2ecf20Sopenharmony_ci		ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
43138c2ecf20Sopenharmony_ci			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
43148c2ecf20Sopenharmony_ci						be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
43158c2ecf20Sopenharmony_ci		ibmvfc_free_event(evt);
43168c2ecf20Sopenharmony_ci		return;
43178c2ecf20Sopenharmony_ci	case IBMVFC_MAD_CRQ_ERROR:
43188c2ecf20Sopenharmony_ci		ibmvfc_retry_host_init(vhost);
43198c2ecf20Sopenharmony_ci		fallthrough;
43208c2ecf20Sopenharmony_ci	case IBMVFC_MAD_DRIVER_FAILED:
43218c2ecf20Sopenharmony_ci		ibmvfc_free_event(evt);
43228c2ecf20Sopenharmony_ci		return;
43238c2ecf20Sopenharmony_ci	default:
43248c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
43258c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
43268c2ecf20Sopenharmony_ci		ibmvfc_free_event(evt);
43278c2ecf20Sopenharmony_ci		return;
43288c2ecf20Sopenharmony_ci	}
43298c2ecf20Sopenharmony_ci
43308c2ecf20Sopenharmony_ci	vhost->client_migrated = 0;
43318c2ecf20Sopenharmony_ci
43328c2ecf20Sopenharmony_ci	if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
43338c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
43348c2ecf20Sopenharmony_ci			rsp->flags);
43358c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
43368c2ecf20Sopenharmony_ci		wake_up(&vhost->work_wait_q);
43378c2ecf20Sopenharmony_ci		return;
43388c2ecf20Sopenharmony_ci	}
43398c2ecf20Sopenharmony_ci
43408c2ecf20Sopenharmony_ci	if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
43418c2ecf20Sopenharmony_ci		dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
43428c2ecf20Sopenharmony_ci			rsp->max_cmds);
43438c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
43448c2ecf20Sopenharmony_ci		wake_up(&vhost->work_wait_q);
43458c2ecf20Sopenharmony_ci		return;
43468c2ecf20Sopenharmony_ci	}
43478c2ecf20Sopenharmony_ci
43488c2ecf20Sopenharmony_ci	vhost->logged_in = 1;
43498c2ecf20Sopenharmony_ci	npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
43508c2ecf20Sopenharmony_ci	dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
43518c2ecf20Sopenharmony_ci		 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
43528c2ecf20Sopenharmony_ci		 rsp->drc_name, npiv_max_sectors);
43538c2ecf20Sopenharmony_ci
43548c2ecf20Sopenharmony_ci	fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
43558c2ecf20Sopenharmony_ci	fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
43568c2ecf20Sopenharmony_ci	fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
43578c2ecf20Sopenharmony_ci	fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
43588c2ecf20Sopenharmony_ci	fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
43598c2ecf20Sopenharmony_ci	fc_host_supported_classes(vhost->host) = 0;
43608c2ecf20Sopenharmony_ci	if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
43618c2ecf20Sopenharmony_ci		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
43628c2ecf20Sopenharmony_ci	if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
43638c2ecf20Sopenharmony_ci		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
43648c2ecf20Sopenharmony_ci	if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
43658c2ecf20Sopenharmony_ci		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
43668c2ecf20Sopenharmony_ci	fc_host_maxframe_size(vhost->host) =
43678c2ecf20Sopenharmony_ci		be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
43688c2ecf20Sopenharmony_ci
43698c2ecf20Sopenharmony_ci	vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
43708c2ecf20Sopenharmony_ci	vhost->host->max_sectors = npiv_max_sectors;
43718c2ecf20Sopenharmony_ci	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
43728c2ecf20Sopenharmony_ci	wake_up(&vhost->work_wait_q);
43738c2ecf20Sopenharmony_ci}
43748c2ecf20Sopenharmony_ci
43758c2ecf20Sopenharmony_ci/**
43768c2ecf20Sopenharmony_ci * ibmvfc_npiv_login - Sends NPIV login
43778c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
43788c2ecf20Sopenharmony_ci *
43798c2ecf20Sopenharmony_ci **/
43808c2ecf20Sopenharmony_cistatic void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
43818c2ecf20Sopenharmony_ci{
43828c2ecf20Sopenharmony_ci	struct ibmvfc_npiv_login_mad *mad;
43838c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
43848c2ecf20Sopenharmony_ci
43858c2ecf20Sopenharmony_ci	ibmvfc_gather_partition_info(vhost);
43868c2ecf20Sopenharmony_ci	ibmvfc_set_login_info(vhost);
43878c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
43888c2ecf20Sopenharmony_ci
43898c2ecf20Sopenharmony_ci	memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
43908c2ecf20Sopenharmony_ci	mad = &evt->iu.npiv_login;
43918c2ecf20Sopenharmony_ci	memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
43928c2ecf20Sopenharmony_ci	mad->common.version = cpu_to_be32(1);
43938c2ecf20Sopenharmony_ci	mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
43948c2ecf20Sopenharmony_ci	mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
43958c2ecf20Sopenharmony_ci	mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
43968c2ecf20Sopenharmony_ci	mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
43978c2ecf20Sopenharmony_ci
43988c2ecf20Sopenharmony_ci	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
43998c2ecf20Sopenharmony_ci
44008c2ecf20Sopenharmony_ci	if (!ibmvfc_send_event(evt, vhost, default_timeout))
44018c2ecf20Sopenharmony_ci		ibmvfc_dbg(vhost, "Sent NPIV login\n");
44028c2ecf20Sopenharmony_ci	else
44038c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
44048c2ecf20Sopenharmony_ci};
44058c2ecf20Sopenharmony_ci
44068c2ecf20Sopenharmony_ci/**
44078c2ecf20Sopenharmony_ci * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
44088c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
44098c2ecf20Sopenharmony_ci *
44108c2ecf20Sopenharmony_ci **/
44118c2ecf20Sopenharmony_cistatic void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
44128c2ecf20Sopenharmony_ci{
44138c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = evt->vhost;
44148c2ecf20Sopenharmony_ci	u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
44158c2ecf20Sopenharmony_ci
44168c2ecf20Sopenharmony_ci	ibmvfc_free_event(evt);
44178c2ecf20Sopenharmony_ci
44188c2ecf20Sopenharmony_ci	switch (mad_status) {
44198c2ecf20Sopenharmony_ci	case IBMVFC_MAD_SUCCESS:
44208c2ecf20Sopenharmony_ci		if (list_empty(&vhost->sent) &&
44218c2ecf20Sopenharmony_ci		    vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
44228c2ecf20Sopenharmony_ci			ibmvfc_init_host(vhost);
44238c2ecf20Sopenharmony_ci			return;
44248c2ecf20Sopenharmony_ci		}
44258c2ecf20Sopenharmony_ci		break;
44268c2ecf20Sopenharmony_ci	case IBMVFC_MAD_FAILED:
44278c2ecf20Sopenharmony_ci	case IBMVFC_MAD_NOT_SUPPORTED:
44288c2ecf20Sopenharmony_ci	case IBMVFC_MAD_CRQ_ERROR:
44298c2ecf20Sopenharmony_ci	case IBMVFC_MAD_DRIVER_FAILED:
44308c2ecf20Sopenharmony_ci	default:
44318c2ecf20Sopenharmony_ci		ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
44328c2ecf20Sopenharmony_ci		break;
44338c2ecf20Sopenharmony_ci	}
44348c2ecf20Sopenharmony_ci
44358c2ecf20Sopenharmony_ci	ibmvfc_hard_reset_host(vhost);
44368c2ecf20Sopenharmony_ci}
44378c2ecf20Sopenharmony_ci
44388c2ecf20Sopenharmony_ci/**
44398c2ecf20Sopenharmony_ci * ibmvfc_npiv_logout - Issue an NPIV Logout
44408c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
44418c2ecf20Sopenharmony_ci *
44428c2ecf20Sopenharmony_ci **/
44438c2ecf20Sopenharmony_cistatic void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
44448c2ecf20Sopenharmony_ci{
44458c2ecf20Sopenharmony_ci	struct ibmvfc_npiv_logout_mad *mad;
44468c2ecf20Sopenharmony_ci	struct ibmvfc_event *evt;
44478c2ecf20Sopenharmony_ci
44488c2ecf20Sopenharmony_ci	evt = ibmvfc_get_event(vhost);
44498c2ecf20Sopenharmony_ci	ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
44508c2ecf20Sopenharmony_ci
44518c2ecf20Sopenharmony_ci	mad = &evt->iu.npiv_logout;
44528c2ecf20Sopenharmony_ci	memset(mad, 0, sizeof(*mad));
44538c2ecf20Sopenharmony_ci	mad->common.version = cpu_to_be32(1);
44548c2ecf20Sopenharmony_ci	mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
44558c2ecf20Sopenharmony_ci	mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
44568c2ecf20Sopenharmony_ci
44578c2ecf20Sopenharmony_ci	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
44588c2ecf20Sopenharmony_ci
44598c2ecf20Sopenharmony_ci	if (!ibmvfc_send_event(evt, vhost, default_timeout))
44608c2ecf20Sopenharmony_ci		ibmvfc_dbg(vhost, "Sent NPIV logout\n");
44618c2ecf20Sopenharmony_ci	else
44628c2ecf20Sopenharmony_ci		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
44638c2ecf20Sopenharmony_ci}
44648c2ecf20Sopenharmony_ci
44658c2ecf20Sopenharmony_ci/**
44668c2ecf20Sopenharmony_ci * ibmvfc_dev_init_to_do - Is there target initialization work to do?
44678c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
44688c2ecf20Sopenharmony_ci *
44698c2ecf20Sopenharmony_ci * Returns:
44708c2ecf20Sopenharmony_ci *	1 if work to do / 0 if not
44718c2ecf20Sopenharmony_ci **/
44728c2ecf20Sopenharmony_cistatic int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
44738c2ecf20Sopenharmony_ci{
44748c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
44758c2ecf20Sopenharmony_ci
44768c2ecf20Sopenharmony_ci	list_for_each_entry(tgt, &vhost->targets, queue) {
44778c2ecf20Sopenharmony_ci		if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
44788c2ecf20Sopenharmony_ci		    tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
44798c2ecf20Sopenharmony_ci			return 1;
44808c2ecf20Sopenharmony_ci	}
44818c2ecf20Sopenharmony_ci
44828c2ecf20Sopenharmony_ci	return 0;
44838c2ecf20Sopenharmony_ci}
44848c2ecf20Sopenharmony_ci
44858c2ecf20Sopenharmony_ci/**
44868c2ecf20Sopenharmony_ci * ibmvfc_dev_logo_to_do - Is there target logout work to do?
44878c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
44888c2ecf20Sopenharmony_ci *
44898c2ecf20Sopenharmony_ci * Returns:
44908c2ecf20Sopenharmony_ci *	1 if work to do / 0 if not
44918c2ecf20Sopenharmony_ci **/
44928c2ecf20Sopenharmony_cistatic int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
44938c2ecf20Sopenharmony_ci{
44948c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
44958c2ecf20Sopenharmony_ci
44968c2ecf20Sopenharmony_ci	list_for_each_entry(tgt, &vhost->targets, queue) {
44978c2ecf20Sopenharmony_ci		if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
44988c2ecf20Sopenharmony_ci		    tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
44998c2ecf20Sopenharmony_ci			return 1;
45008c2ecf20Sopenharmony_ci	}
45018c2ecf20Sopenharmony_ci	return 0;
45028c2ecf20Sopenharmony_ci}
45038c2ecf20Sopenharmony_ci
45048c2ecf20Sopenharmony_ci/**
45058c2ecf20Sopenharmony_ci * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
45068c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
45078c2ecf20Sopenharmony_ci *
45088c2ecf20Sopenharmony_ci * Returns:
45098c2ecf20Sopenharmony_ci *	1 if work to do / 0 if not
45108c2ecf20Sopenharmony_ci **/
45118c2ecf20Sopenharmony_cistatic int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
45128c2ecf20Sopenharmony_ci{
45138c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
45148c2ecf20Sopenharmony_ci
45158c2ecf20Sopenharmony_ci	if (kthread_should_stop())
45168c2ecf20Sopenharmony_ci		return 1;
45178c2ecf20Sopenharmony_ci	switch (vhost->action) {
45188c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_NONE:
45198c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_INIT_WAIT:
45208c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_LOGO_WAIT:
45218c2ecf20Sopenharmony_ci		return 0;
45228c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_TGT_INIT:
45238c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_QUERY_TGTS:
45248c2ecf20Sopenharmony_ci		if (vhost->discovery_threads == disc_threads)
45258c2ecf20Sopenharmony_ci			return 0;
45268c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue)
45278c2ecf20Sopenharmony_ci			if (tgt->action == IBMVFC_TGT_ACTION_INIT)
45288c2ecf20Sopenharmony_ci				return 1;
45298c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue)
45308c2ecf20Sopenharmony_ci			if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
45318c2ecf20Sopenharmony_ci				return 0;
45328c2ecf20Sopenharmony_ci		return 1;
45338c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_TGT_DEL:
45348c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
45358c2ecf20Sopenharmony_ci		if (vhost->discovery_threads == disc_threads)
45368c2ecf20Sopenharmony_ci			return 0;
45378c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue)
45388c2ecf20Sopenharmony_ci			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
45398c2ecf20Sopenharmony_ci				return 1;
45408c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue)
45418c2ecf20Sopenharmony_ci			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
45428c2ecf20Sopenharmony_ci				return 0;
45438c2ecf20Sopenharmony_ci		return 1;
45448c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_LOGO:
45458c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_INIT:
45468c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
45478c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_QUERY:
45488c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_RESET:
45498c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_REENABLE:
45508c2ecf20Sopenharmony_ci	default:
45518c2ecf20Sopenharmony_ci		break;
45528c2ecf20Sopenharmony_ci	}
45538c2ecf20Sopenharmony_ci
45548c2ecf20Sopenharmony_ci	return 1;
45558c2ecf20Sopenharmony_ci}
45568c2ecf20Sopenharmony_ci
45578c2ecf20Sopenharmony_ci/**
45588c2ecf20Sopenharmony_ci * ibmvfc_work_to_do - Is there task level work to do?
45598c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
45608c2ecf20Sopenharmony_ci *
45618c2ecf20Sopenharmony_ci * Returns:
45628c2ecf20Sopenharmony_ci *	1 if work to do / 0 if not
45638c2ecf20Sopenharmony_ci **/
45648c2ecf20Sopenharmony_cistatic int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
45658c2ecf20Sopenharmony_ci{
45668c2ecf20Sopenharmony_ci	unsigned long flags;
45678c2ecf20Sopenharmony_ci	int rc;
45688c2ecf20Sopenharmony_ci
45698c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
45708c2ecf20Sopenharmony_ci	rc = __ibmvfc_work_to_do(vhost);
45718c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
45728c2ecf20Sopenharmony_ci	return rc;
45738c2ecf20Sopenharmony_ci}
45748c2ecf20Sopenharmony_ci
45758c2ecf20Sopenharmony_ci/**
45768c2ecf20Sopenharmony_ci * ibmvfc_log_ae - Log async events if necessary
45778c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
45788c2ecf20Sopenharmony_ci * @events:		events to log
45798c2ecf20Sopenharmony_ci *
45808c2ecf20Sopenharmony_ci **/
45818c2ecf20Sopenharmony_cistatic void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
45828c2ecf20Sopenharmony_ci{
45838c2ecf20Sopenharmony_ci	if (events & IBMVFC_AE_RSCN)
45848c2ecf20Sopenharmony_ci		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
45858c2ecf20Sopenharmony_ci	if ((events & IBMVFC_AE_LINKDOWN) &&
45868c2ecf20Sopenharmony_ci	    vhost->state >= IBMVFC_HALTED)
45878c2ecf20Sopenharmony_ci		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
45888c2ecf20Sopenharmony_ci	if ((events & IBMVFC_AE_LINKUP) &&
45898c2ecf20Sopenharmony_ci	    vhost->state == IBMVFC_INITIALIZING)
45908c2ecf20Sopenharmony_ci		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
45918c2ecf20Sopenharmony_ci}
45928c2ecf20Sopenharmony_ci
45938c2ecf20Sopenharmony_ci/**
45948c2ecf20Sopenharmony_ci * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
45958c2ecf20Sopenharmony_ci * @tgt:		ibmvfc target struct
45968c2ecf20Sopenharmony_ci *
45978c2ecf20Sopenharmony_ci **/
45988c2ecf20Sopenharmony_cistatic void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
45998c2ecf20Sopenharmony_ci{
46008c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = tgt->vhost;
46018c2ecf20Sopenharmony_ci	struct fc_rport *rport;
46028c2ecf20Sopenharmony_ci	unsigned long flags;
46038c2ecf20Sopenharmony_ci
46048c2ecf20Sopenharmony_ci	tgt_dbg(tgt, "Adding rport\n");
46058c2ecf20Sopenharmony_ci	rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
46068c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
46078c2ecf20Sopenharmony_ci
46088c2ecf20Sopenharmony_ci	if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
46098c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Deleting rport\n");
46108c2ecf20Sopenharmony_ci		list_del(&tgt->queue);
46118c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
46128c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
46138c2ecf20Sopenharmony_ci		fc_remote_port_delete(rport);
46148c2ecf20Sopenharmony_ci		del_timer_sync(&tgt->timer);
46158c2ecf20Sopenharmony_ci		kref_put(&tgt->kref, ibmvfc_release_tgt);
46168c2ecf20Sopenharmony_ci		return;
46178c2ecf20Sopenharmony_ci	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
46188c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
46198c2ecf20Sopenharmony_ci		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
46208c2ecf20Sopenharmony_ci		tgt->rport = NULL;
46218c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
46228c2ecf20Sopenharmony_ci		fc_remote_port_delete(rport);
46238c2ecf20Sopenharmony_ci		return;
46248c2ecf20Sopenharmony_ci	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
46258c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
46268c2ecf20Sopenharmony_ci		return;
46278c2ecf20Sopenharmony_ci	}
46288c2ecf20Sopenharmony_ci
46298c2ecf20Sopenharmony_ci	if (rport) {
46308c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "rport add succeeded\n");
46318c2ecf20Sopenharmony_ci		tgt->rport = rport;
46328c2ecf20Sopenharmony_ci		rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
46338c2ecf20Sopenharmony_ci		rport->supported_classes = 0;
46348c2ecf20Sopenharmony_ci		tgt->target_id = rport->scsi_target_id;
46358c2ecf20Sopenharmony_ci		if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
46368c2ecf20Sopenharmony_ci			rport->supported_classes |= FC_COS_CLASS1;
46378c2ecf20Sopenharmony_ci		if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
46388c2ecf20Sopenharmony_ci			rport->supported_classes |= FC_COS_CLASS2;
46398c2ecf20Sopenharmony_ci		if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
46408c2ecf20Sopenharmony_ci			rport->supported_classes |= FC_COS_CLASS3;
46418c2ecf20Sopenharmony_ci		if (rport->rqst_q)
46428c2ecf20Sopenharmony_ci			blk_queue_max_segments(rport->rqst_q, 1);
46438c2ecf20Sopenharmony_ci	} else
46448c2ecf20Sopenharmony_ci		tgt_dbg(tgt, "rport add failed\n");
46458c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
46468c2ecf20Sopenharmony_ci}
46478c2ecf20Sopenharmony_ci
46488c2ecf20Sopenharmony_ci/**
46498c2ecf20Sopenharmony_ci * ibmvfc_do_work - Do task level work
46508c2ecf20Sopenharmony_ci * @vhost:		ibmvfc host struct
46518c2ecf20Sopenharmony_ci *
46528c2ecf20Sopenharmony_ci **/
46538c2ecf20Sopenharmony_cistatic void ibmvfc_do_work(struct ibmvfc_host *vhost)
46548c2ecf20Sopenharmony_ci{
46558c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
46568c2ecf20Sopenharmony_ci	unsigned long flags;
46578c2ecf20Sopenharmony_ci	struct fc_rport *rport;
46588c2ecf20Sopenharmony_ci	int rc;
46598c2ecf20Sopenharmony_ci
46608c2ecf20Sopenharmony_ci	ibmvfc_log_ae(vhost, vhost->events_to_log);
46618c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
46628c2ecf20Sopenharmony_ci	vhost->events_to_log = 0;
46638c2ecf20Sopenharmony_ci	switch (vhost->action) {
46648c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_NONE:
46658c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_LOGO_WAIT:
46668c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_INIT_WAIT:
46678c2ecf20Sopenharmony_ci		break;
46688c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_RESET:
46698c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
46708c2ecf20Sopenharmony_ci		rc = ibmvfc_reset_crq(vhost);
46718c2ecf20Sopenharmony_ci
46728c2ecf20Sopenharmony_ci		spin_lock_irqsave(vhost->host->host_lock, flags);
46738c2ecf20Sopenharmony_ci		if (!rc || rc == H_CLOSED)
46748c2ecf20Sopenharmony_ci			vio_enable_interrupts(to_vio_dev(vhost->dev));
46758c2ecf20Sopenharmony_ci		if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
46768c2ecf20Sopenharmony_ci			/*
46778c2ecf20Sopenharmony_ci			 * The only action we could have changed to would have
46788c2ecf20Sopenharmony_ci			 * been reenable, in which case, we skip the rest of
46798c2ecf20Sopenharmony_ci			 * this path and wait until we've done the re-enable
46808c2ecf20Sopenharmony_ci			 * before sending the crq init.
46818c2ecf20Sopenharmony_ci			 */
46828c2ecf20Sopenharmony_ci			vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
46838c2ecf20Sopenharmony_ci
46848c2ecf20Sopenharmony_ci			if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
46858c2ecf20Sopenharmony_ci			    (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
46868c2ecf20Sopenharmony_ci				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
46878c2ecf20Sopenharmony_ci				dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
46888c2ecf20Sopenharmony_ci			}
46898c2ecf20Sopenharmony_ci		}
46908c2ecf20Sopenharmony_ci		break;
46918c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_REENABLE:
46928c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
46938c2ecf20Sopenharmony_ci		rc = ibmvfc_reenable_crq_queue(vhost);
46948c2ecf20Sopenharmony_ci
46958c2ecf20Sopenharmony_ci		spin_lock_irqsave(vhost->host->host_lock, flags);
46968c2ecf20Sopenharmony_ci		if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
46978c2ecf20Sopenharmony_ci			/*
46988c2ecf20Sopenharmony_ci			 * The only action we could have changed to would have
46998c2ecf20Sopenharmony_ci			 * been reset, in which case, we skip the rest of this
47008c2ecf20Sopenharmony_ci			 * path and wait until we've done the reset before
47018c2ecf20Sopenharmony_ci			 * sending the crq init.
47028c2ecf20Sopenharmony_ci			 */
47038c2ecf20Sopenharmony_ci			vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
47048c2ecf20Sopenharmony_ci			if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
47058c2ecf20Sopenharmony_ci				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
47068c2ecf20Sopenharmony_ci				dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
47078c2ecf20Sopenharmony_ci			}
47088c2ecf20Sopenharmony_ci		}
47098c2ecf20Sopenharmony_ci		break;
47108c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_LOGO:
47118c2ecf20Sopenharmony_ci		vhost->job_step(vhost);
47128c2ecf20Sopenharmony_ci		break;
47138c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_INIT:
47148c2ecf20Sopenharmony_ci		BUG_ON(vhost->state != IBMVFC_INITIALIZING);
47158c2ecf20Sopenharmony_ci		if (vhost->delay_init) {
47168c2ecf20Sopenharmony_ci			vhost->delay_init = 0;
47178c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(vhost->host->host_lock, flags);
47188c2ecf20Sopenharmony_ci			ssleep(15);
47198c2ecf20Sopenharmony_ci			return;
47208c2ecf20Sopenharmony_ci		} else
47218c2ecf20Sopenharmony_ci			vhost->job_step(vhost);
47228c2ecf20Sopenharmony_ci		break;
47238c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_QUERY:
47248c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue)
47258c2ecf20Sopenharmony_ci			ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
47268c2ecf20Sopenharmony_ci		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
47278c2ecf20Sopenharmony_ci		break;
47288c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_QUERY_TGTS:
47298c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue) {
47308c2ecf20Sopenharmony_ci			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
47318c2ecf20Sopenharmony_ci				tgt->job_step(tgt);
47328c2ecf20Sopenharmony_ci				break;
47338c2ecf20Sopenharmony_ci			}
47348c2ecf20Sopenharmony_ci		}
47358c2ecf20Sopenharmony_ci
47368c2ecf20Sopenharmony_ci		if (!ibmvfc_dev_init_to_do(vhost))
47378c2ecf20Sopenharmony_ci			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
47388c2ecf20Sopenharmony_ci		break;
47398c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_TGT_DEL:
47408c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
47418c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue) {
47428c2ecf20Sopenharmony_ci			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
47438c2ecf20Sopenharmony_ci				tgt->job_step(tgt);
47448c2ecf20Sopenharmony_ci				break;
47458c2ecf20Sopenharmony_ci			}
47468c2ecf20Sopenharmony_ci		}
47478c2ecf20Sopenharmony_ci
47488c2ecf20Sopenharmony_ci		if (ibmvfc_dev_logo_to_do(vhost)) {
47498c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(vhost->host->host_lock, flags);
47508c2ecf20Sopenharmony_ci			return;
47518c2ecf20Sopenharmony_ci		}
47528c2ecf20Sopenharmony_ci
47538c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue) {
47548c2ecf20Sopenharmony_ci			if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
47558c2ecf20Sopenharmony_ci				tgt_dbg(tgt, "Deleting rport\n");
47568c2ecf20Sopenharmony_ci				rport = tgt->rport;
47578c2ecf20Sopenharmony_ci				tgt->rport = NULL;
47588c2ecf20Sopenharmony_ci				list_del(&tgt->queue);
47598c2ecf20Sopenharmony_ci				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
47608c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(vhost->host->host_lock, flags);
47618c2ecf20Sopenharmony_ci				if (rport)
47628c2ecf20Sopenharmony_ci					fc_remote_port_delete(rport);
47638c2ecf20Sopenharmony_ci				del_timer_sync(&tgt->timer);
47648c2ecf20Sopenharmony_ci				kref_put(&tgt->kref, ibmvfc_release_tgt);
47658c2ecf20Sopenharmony_ci				return;
47668c2ecf20Sopenharmony_ci			} else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
47678c2ecf20Sopenharmony_ci				tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
47688c2ecf20Sopenharmony_ci				rport = tgt->rport;
47698c2ecf20Sopenharmony_ci				tgt->rport = NULL;
47708c2ecf20Sopenharmony_ci				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
47718c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(vhost->host->host_lock, flags);
47728c2ecf20Sopenharmony_ci				if (rport)
47738c2ecf20Sopenharmony_ci					fc_remote_port_delete(rport);
47748c2ecf20Sopenharmony_ci				return;
47758c2ecf20Sopenharmony_ci			}
47768c2ecf20Sopenharmony_ci		}
47778c2ecf20Sopenharmony_ci
47788c2ecf20Sopenharmony_ci		if (vhost->state == IBMVFC_INITIALIZING) {
47798c2ecf20Sopenharmony_ci			if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
47808c2ecf20Sopenharmony_ci				if (vhost->reinit) {
47818c2ecf20Sopenharmony_ci					vhost->reinit = 0;
47828c2ecf20Sopenharmony_ci					scsi_block_requests(vhost->host);
47838c2ecf20Sopenharmony_ci					ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
47848c2ecf20Sopenharmony_ci					spin_unlock_irqrestore(vhost->host->host_lock, flags);
47858c2ecf20Sopenharmony_ci				} else {
47868c2ecf20Sopenharmony_ci					ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
47878c2ecf20Sopenharmony_ci					ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
47888c2ecf20Sopenharmony_ci					wake_up(&vhost->init_wait_q);
47898c2ecf20Sopenharmony_ci					schedule_work(&vhost->rport_add_work_q);
47908c2ecf20Sopenharmony_ci					vhost->init_retries = 0;
47918c2ecf20Sopenharmony_ci					spin_unlock_irqrestore(vhost->host->host_lock, flags);
47928c2ecf20Sopenharmony_ci					scsi_unblock_requests(vhost->host);
47938c2ecf20Sopenharmony_ci				}
47948c2ecf20Sopenharmony_ci
47958c2ecf20Sopenharmony_ci				return;
47968c2ecf20Sopenharmony_ci			} else {
47978c2ecf20Sopenharmony_ci				ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
47988c2ecf20Sopenharmony_ci				vhost->job_step = ibmvfc_discover_targets;
47998c2ecf20Sopenharmony_ci			}
48008c2ecf20Sopenharmony_ci		} else {
48018c2ecf20Sopenharmony_ci			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
48028c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(vhost->host->host_lock, flags);
48038c2ecf20Sopenharmony_ci			scsi_unblock_requests(vhost->host);
48048c2ecf20Sopenharmony_ci			wake_up(&vhost->init_wait_q);
48058c2ecf20Sopenharmony_ci			return;
48068c2ecf20Sopenharmony_ci		}
48078c2ecf20Sopenharmony_ci		break;
48088c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
48098c2ecf20Sopenharmony_ci		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
48108c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(vhost->host->host_lock, flags);
48118c2ecf20Sopenharmony_ci		ibmvfc_alloc_targets(vhost);
48128c2ecf20Sopenharmony_ci		spin_lock_irqsave(vhost->host->host_lock, flags);
48138c2ecf20Sopenharmony_ci		break;
48148c2ecf20Sopenharmony_ci	case IBMVFC_HOST_ACTION_TGT_INIT:
48158c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue) {
48168c2ecf20Sopenharmony_ci			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
48178c2ecf20Sopenharmony_ci				tgt->job_step(tgt);
48188c2ecf20Sopenharmony_ci				break;
48198c2ecf20Sopenharmony_ci			}
48208c2ecf20Sopenharmony_ci		}
48218c2ecf20Sopenharmony_ci
48228c2ecf20Sopenharmony_ci		if (!ibmvfc_dev_init_to_do(vhost))
48238c2ecf20Sopenharmony_ci			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
48248c2ecf20Sopenharmony_ci		break;
48258c2ecf20Sopenharmony_ci	default:
48268c2ecf20Sopenharmony_ci		break;
48278c2ecf20Sopenharmony_ci	}
48288c2ecf20Sopenharmony_ci
48298c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
48308c2ecf20Sopenharmony_ci}
48318c2ecf20Sopenharmony_ci
48328c2ecf20Sopenharmony_ci/**
48338c2ecf20Sopenharmony_ci * ibmvfc_work - Do task level work
48348c2ecf20Sopenharmony_ci * @data:		ibmvfc host struct
48358c2ecf20Sopenharmony_ci *
48368c2ecf20Sopenharmony_ci * Returns:
48378c2ecf20Sopenharmony_ci *	zero
48388c2ecf20Sopenharmony_ci **/
48398c2ecf20Sopenharmony_cistatic int ibmvfc_work(void *data)
48408c2ecf20Sopenharmony_ci{
48418c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = data;
48428c2ecf20Sopenharmony_ci	int rc;
48438c2ecf20Sopenharmony_ci
48448c2ecf20Sopenharmony_ci	set_user_nice(current, MIN_NICE);
48458c2ecf20Sopenharmony_ci
48468c2ecf20Sopenharmony_ci	while (1) {
48478c2ecf20Sopenharmony_ci		rc = wait_event_interruptible(vhost->work_wait_q,
48488c2ecf20Sopenharmony_ci					      ibmvfc_work_to_do(vhost));
48498c2ecf20Sopenharmony_ci
48508c2ecf20Sopenharmony_ci		BUG_ON(rc);
48518c2ecf20Sopenharmony_ci
48528c2ecf20Sopenharmony_ci		if (kthread_should_stop())
48538c2ecf20Sopenharmony_ci			break;
48548c2ecf20Sopenharmony_ci
48558c2ecf20Sopenharmony_ci		ibmvfc_do_work(vhost);
48568c2ecf20Sopenharmony_ci	}
48578c2ecf20Sopenharmony_ci
48588c2ecf20Sopenharmony_ci	ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
48598c2ecf20Sopenharmony_ci	return 0;
48608c2ecf20Sopenharmony_ci}
48618c2ecf20Sopenharmony_ci
48628c2ecf20Sopenharmony_ci/**
48638c2ecf20Sopenharmony_ci * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
48648c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
48658c2ecf20Sopenharmony_ci *
48668c2ecf20Sopenharmony_ci * Allocates a page for messages, maps it for dma, and registers
48678c2ecf20Sopenharmony_ci * the crq with the hypervisor.
48688c2ecf20Sopenharmony_ci *
48698c2ecf20Sopenharmony_ci * Return value:
48708c2ecf20Sopenharmony_ci *	zero on success / other on failure
48718c2ecf20Sopenharmony_ci **/
48728c2ecf20Sopenharmony_cistatic int ibmvfc_init_crq(struct ibmvfc_host *vhost)
48738c2ecf20Sopenharmony_ci{
48748c2ecf20Sopenharmony_ci	int rc, retrc = -ENOMEM;
48758c2ecf20Sopenharmony_ci	struct device *dev = vhost->dev;
48768c2ecf20Sopenharmony_ci	struct vio_dev *vdev = to_vio_dev(dev);
48778c2ecf20Sopenharmony_ci	struct ibmvfc_crq_queue *crq = &vhost->crq;
48788c2ecf20Sopenharmony_ci
48798c2ecf20Sopenharmony_ci	ENTER;
48808c2ecf20Sopenharmony_ci	crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
48818c2ecf20Sopenharmony_ci
48828c2ecf20Sopenharmony_ci	if (!crq->msgs)
48838c2ecf20Sopenharmony_ci		return -ENOMEM;
48848c2ecf20Sopenharmony_ci
48858c2ecf20Sopenharmony_ci	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
48868c2ecf20Sopenharmony_ci	crq->msg_token = dma_map_single(dev, crq->msgs,
48878c2ecf20Sopenharmony_ci					PAGE_SIZE, DMA_BIDIRECTIONAL);
48888c2ecf20Sopenharmony_ci
48898c2ecf20Sopenharmony_ci	if (dma_mapping_error(dev, crq->msg_token))
48908c2ecf20Sopenharmony_ci		goto map_failed;
48918c2ecf20Sopenharmony_ci
48928c2ecf20Sopenharmony_ci	retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
48938c2ecf20Sopenharmony_ci					crq->msg_token, PAGE_SIZE);
48948c2ecf20Sopenharmony_ci
48958c2ecf20Sopenharmony_ci	if (rc == H_RESOURCE)
48968c2ecf20Sopenharmony_ci		/* maybe kexecing and resource is busy. try a reset */
48978c2ecf20Sopenharmony_ci		retrc = rc = ibmvfc_reset_crq(vhost);
48988c2ecf20Sopenharmony_ci
48998c2ecf20Sopenharmony_ci	if (rc == H_CLOSED)
49008c2ecf20Sopenharmony_ci		dev_warn(dev, "Partner adapter not ready\n");
49018c2ecf20Sopenharmony_ci	else if (rc) {
49028c2ecf20Sopenharmony_ci		dev_warn(dev, "Error %d opening adapter\n", rc);
49038c2ecf20Sopenharmony_ci		goto reg_crq_failed;
49048c2ecf20Sopenharmony_ci	}
49058c2ecf20Sopenharmony_ci
49068c2ecf20Sopenharmony_ci	retrc = 0;
49078c2ecf20Sopenharmony_ci
49088c2ecf20Sopenharmony_ci	tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
49098c2ecf20Sopenharmony_ci
49108c2ecf20Sopenharmony_ci	if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
49118c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
49128c2ecf20Sopenharmony_ci		goto req_irq_failed;
49138c2ecf20Sopenharmony_ci	}
49148c2ecf20Sopenharmony_ci
49158c2ecf20Sopenharmony_ci	if ((rc = vio_enable_interrupts(vdev))) {
49168c2ecf20Sopenharmony_ci		dev_err(dev, "Error %d enabling interrupts\n", rc);
49178c2ecf20Sopenharmony_ci		goto req_irq_failed;
49188c2ecf20Sopenharmony_ci	}
49198c2ecf20Sopenharmony_ci
49208c2ecf20Sopenharmony_ci	crq->cur = 0;
49218c2ecf20Sopenharmony_ci	LEAVE;
49228c2ecf20Sopenharmony_ci	return retrc;
49238c2ecf20Sopenharmony_ci
49248c2ecf20Sopenharmony_cireq_irq_failed:
49258c2ecf20Sopenharmony_ci	tasklet_kill(&vhost->tasklet);
49268c2ecf20Sopenharmony_ci	do {
49278c2ecf20Sopenharmony_ci		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
49288c2ecf20Sopenharmony_ci	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
49298c2ecf20Sopenharmony_cireg_crq_failed:
49308c2ecf20Sopenharmony_ci	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
49318c2ecf20Sopenharmony_cimap_failed:
49328c2ecf20Sopenharmony_ci	free_page((unsigned long)crq->msgs);
49338c2ecf20Sopenharmony_ci	return retrc;
49348c2ecf20Sopenharmony_ci}
49358c2ecf20Sopenharmony_ci
49368c2ecf20Sopenharmony_ci/**
49378c2ecf20Sopenharmony_ci * ibmvfc_free_mem - Free memory for vhost
49388c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
49398c2ecf20Sopenharmony_ci *
49408c2ecf20Sopenharmony_ci * Return value:
49418c2ecf20Sopenharmony_ci * 	none
49428c2ecf20Sopenharmony_ci **/
49438c2ecf20Sopenharmony_cistatic void ibmvfc_free_mem(struct ibmvfc_host *vhost)
49448c2ecf20Sopenharmony_ci{
49458c2ecf20Sopenharmony_ci	struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
49468c2ecf20Sopenharmony_ci
49478c2ecf20Sopenharmony_ci	ENTER;
49488c2ecf20Sopenharmony_ci	mempool_destroy(vhost->tgt_pool);
49498c2ecf20Sopenharmony_ci	kfree(vhost->trace);
49508c2ecf20Sopenharmony_ci	dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
49518c2ecf20Sopenharmony_ci			  vhost->disc_buf_dma);
49528c2ecf20Sopenharmony_ci	dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
49538c2ecf20Sopenharmony_ci			  vhost->login_buf, vhost->login_buf_dma);
49548c2ecf20Sopenharmony_ci	dma_pool_destroy(vhost->sg_pool);
49558c2ecf20Sopenharmony_ci	dma_unmap_single(vhost->dev, async_q->msg_token,
49568c2ecf20Sopenharmony_ci			 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
49578c2ecf20Sopenharmony_ci	free_page((unsigned long)async_q->msgs);
49588c2ecf20Sopenharmony_ci	LEAVE;
49598c2ecf20Sopenharmony_ci}
49608c2ecf20Sopenharmony_ci
49618c2ecf20Sopenharmony_ci/**
49628c2ecf20Sopenharmony_ci * ibmvfc_alloc_mem - Allocate memory for vhost
49638c2ecf20Sopenharmony_ci * @vhost:	ibmvfc host struct
49648c2ecf20Sopenharmony_ci *
49658c2ecf20Sopenharmony_ci * Return value:
49668c2ecf20Sopenharmony_ci * 	0 on success / non-zero on failure
49678c2ecf20Sopenharmony_ci **/
49688c2ecf20Sopenharmony_cistatic int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
49698c2ecf20Sopenharmony_ci{
49708c2ecf20Sopenharmony_ci	struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
49718c2ecf20Sopenharmony_ci	struct device *dev = vhost->dev;
49728c2ecf20Sopenharmony_ci
49738c2ecf20Sopenharmony_ci	ENTER;
49748c2ecf20Sopenharmony_ci	async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
49758c2ecf20Sopenharmony_ci	if (!async_q->msgs) {
49768c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't allocate async queue.\n");
49778c2ecf20Sopenharmony_ci		goto nomem;
49788c2ecf20Sopenharmony_ci	}
49798c2ecf20Sopenharmony_ci
49808c2ecf20Sopenharmony_ci	async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
49818c2ecf20Sopenharmony_ci	async_q->msg_token = dma_map_single(dev, async_q->msgs,
49828c2ecf20Sopenharmony_ci					    async_q->size * sizeof(*async_q->msgs),
49838c2ecf20Sopenharmony_ci					    DMA_BIDIRECTIONAL);
49848c2ecf20Sopenharmony_ci
49858c2ecf20Sopenharmony_ci	if (dma_mapping_error(dev, async_q->msg_token)) {
49868c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to map async queue\n");
49878c2ecf20Sopenharmony_ci		goto free_async_crq;
49888c2ecf20Sopenharmony_ci	}
49898c2ecf20Sopenharmony_ci
49908c2ecf20Sopenharmony_ci	vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
49918c2ecf20Sopenharmony_ci					 SG_ALL * sizeof(struct srp_direct_buf),
49928c2ecf20Sopenharmony_ci					 sizeof(struct srp_direct_buf), 0);
49938c2ecf20Sopenharmony_ci
49948c2ecf20Sopenharmony_ci	if (!vhost->sg_pool) {
49958c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to allocate sg pool\n");
49968c2ecf20Sopenharmony_ci		goto unmap_async_crq;
49978c2ecf20Sopenharmony_ci	}
49988c2ecf20Sopenharmony_ci
49998c2ecf20Sopenharmony_ci	vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
50008c2ecf20Sopenharmony_ci					      &vhost->login_buf_dma, GFP_KERNEL);
50018c2ecf20Sopenharmony_ci
50028c2ecf20Sopenharmony_ci	if (!vhost->login_buf) {
50038c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't allocate NPIV login buffer\n");
50048c2ecf20Sopenharmony_ci		goto free_sg_pool;
50058c2ecf20Sopenharmony_ci	}
50068c2ecf20Sopenharmony_ci
50078c2ecf20Sopenharmony_ci	vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
50088c2ecf20Sopenharmony_ci	vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
50098c2ecf20Sopenharmony_ci					     &vhost->disc_buf_dma, GFP_KERNEL);
50108c2ecf20Sopenharmony_ci
50118c2ecf20Sopenharmony_ci	if (!vhost->disc_buf) {
50128c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
50138c2ecf20Sopenharmony_ci		goto free_login_buffer;
50148c2ecf20Sopenharmony_ci	}
50158c2ecf20Sopenharmony_ci
50168c2ecf20Sopenharmony_ci	vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
50178c2ecf20Sopenharmony_ci			       sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
50188c2ecf20Sopenharmony_ci
50198c2ecf20Sopenharmony_ci	if (!vhost->trace)
50208c2ecf20Sopenharmony_ci		goto free_disc_buffer;
50218c2ecf20Sopenharmony_ci
50228c2ecf20Sopenharmony_ci	vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
50238c2ecf20Sopenharmony_ci						      sizeof(struct ibmvfc_target));
50248c2ecf20Sopenharmony_ci
50258c2ecf20Sopenharmony_ci	if (!vhost->tgt_pool) {
50268c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't allocate target memory pool\n");
50278c2ecf20Sopenharmony_ci		goto free_trace;
50288c2ecf20Sopenharmony_ci	}
50298c2ecf20Sopenharmony_ci
50308c2ecf20Sopenharmony_ci	LEAVE;
50318c2ecf20Sopenharmony_ci	return 0;
50328c2ecf20Sopenharmony_ci
50338c2ecf20Sopenharmony_cifree_trace:
50348c2ecf20Sopenharmony_ci	kfree(vhost->trace);
50358c2ecf20Sopenharmony_cifree_disc_buffer:
50368c2ecf20Sopenharmony_ci	dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
50378c2ecf20Sopenharmony_ci			  vhost->disc_buf_dma);
50388c2ecf20Sopenharmony_cifree_login_buffer:
50398c2ecf20Sopenharmony_ci	dma_free_coherent(dev, sizeof(*vhost->login_buf),
50408c2ecf20Sopenharmony_ci			  vhost->login_buf, vhost->login_buf_dma);
50418c2ecf20Sopenharmony_cifree_sg_pool:
50428c2ecf20Sopenharmony_ci	dma_pool_destroy(vhost->sg_pool);
50438c2ecf20Sopenharmony_ciunmap_async_crq:
50448c2ecf20Sopenharmony_ci	dma_unmap_single(dev, async_q->msg_token,
50458c2ecf20Sopenharmony_ci			 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
50468c2ecf20Sopenharmony_cifree_async_crq:
50478c2ecf20Sopenharmony_ci	free_page((unsigned long)async_q->msgs);
50488c2ecf20Sopenharmony_cinomem:
50498c2ecf20Sopenharmony_ci	LEAVE;
50508c2ecf20Sopenharmony_ci	return -ENOMEM;
50518c2ecf20Sopenharmony_ci}
50528c2ecf20Sopenharmony_ci
50538c2ecf20Sopenharmony_ci/**
50548c2ecf20Sopenharmony_ci * ibmvfc_rport_add_thread - Worker thread for rport adds
50558c2ecf20Sopenharmony_ci * @work:	work struct
50568c2ecf20Sopenharmony_ci *
50578c2ecf20Sopenharmony_ci **/
50588c2ecf20Sopenharmony_cistatic void ibmvfc_rport_add_thread(struct work_struct *work)
50598c2ecf20Sopenharmony_ci{
50608c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
50618c2ecf20Sopenharmony_ci						 rport_add_work_q);
50628c2ecf20Sopenharmony_ci	struct ibmvfc_target *tgt;
50638c2ecf20Sopenharmony_ci	struct fc_rport *rport;
50648c2ecf20Sopenharmony_ci	unsigned long flags;
50658c2ecf20Sopenharmony_ci	int did_work;
50668c2ecf20Sopenharmony_ci
50678c2ecf20Sopenharmony_ci	ENTER;
50688c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
50698c2ecf20Sopenharmony_ci	do {
50708c2ecf20Sopenharmony_ci		did_work = 0;
50718c2ecf20Sopenharmony_ci		if (vhost->state != IBMVFC_ACTIVE)
50728c2ecf20Sopenharmony_ci			break;
50738c2ecf20Sopenharmony_ci
50748c2ecf20Sopenharmony_ci		list_for_each_entry(tgt, &vhost->targets, queue) {
50758c2ecf20Sopenharmony_ci			if (tgt->add_rport) {
50768c2ecf20Sopenharmony_ci				did_work = 1;
50778c2ecf20Sopenharmony_ci				tgt->add_rport = 0;
50788c2ecf20Sopenharmony_ci				kref_get(&tgt->kref);
50798c2ecf20Sopenharmony_ci				rport = tgt->rport;
50808c2ecf20Sopenharmony_ci				if (!rport) {
50818c2ecf20Sopenharmony_ci					spin_unlock_irqrestore(vhost->host->host_lock, flags);
50828c2ecf20Sopenharmony_ci					ibmvfc_tgt_add_rport(tgt);
50838c2ecf20Sopenharmony_ci				} else if (get_device(&rport->dev)) {
50848c2ecf20Sopenharmony_ci					spin_unlock_irqrestore(vhost->host->host_lock, flags);
50858c2ecf20Sopenharmony_ci					tgt_dbg(tgt, "Setting rport roles\n");
50868c2ecf20Sopenharmony_ci					fc_remote_port_rolechg(rport, tgt->ids.roles);
50878c2ecf20Sopenharmony_ci					put_device(&rport->dev);
50888c2ecf20Sopenharmony_ci				} else {
50898c2ecf20Sopenharmony_ci					spin_unlock_irqrestore(vhost->host->host_lock, flags);
50908c2ecf20Sopenharmony_ci				}
50918c2ecf20Sopenharmony_ci
50928c2ecf20Sopenharmony_ci				kref_put(&tgt->kref, ibmvfc_release_tgt);
50938c2ecf20Sopenharmony_ci				spin_lock_irqsave(vhost->host->host_lock, flags);
50948c2ecf20Sopenharmony_ci				break;
50958c2ecf20Sopenharmony_ci			}
50968c2ecf20Sopenharmony_ci		}
50978c2ecf20Sopenharmony_ci	} while(did_work);
50988c2ecf20Sopenharmony_ci
50998c2ecf20Sopenharmony_ci	if (vhost->state == IBMVFC_ACTIVE)
51008c2ecf20Sopenharmony_ci		vhost->scan_complete = 1;
51018c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
51028c2ecf20Sopenharmony_ci	LEAVE;
51038c2ecf20Sopenharmony_ci}
51048c2ecf20Sopenharmony_ci
51058c2ecf20Sopenharmony_ci/**
51068c2ecf20Sopenharmony_ci * ibmvfc_probe - Adapter hot plug add entry point
51078c2ecf20Sopenharmony_ci * @vdev:	vio device struct
51088c2ecf20Sopenharmony_ci * @id:	vio device id struct
51098c2ecf20Sopenharmony_ci *
51108c2ecf20Sopenharmony_ci * Return value:
51118c2ecf20Sopenharmony_ci * 	0 on success / non-zero on failure
51128c2ecf20Sopenharmony_ci **/
51138c2ecf20Sopenharmony_cistatic int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
51148c2ecf20Sopenharmony_ci{
51158c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost;
51168c2ecf20Sopenharmony_ci	struct Scsi_Host *shost;
51178c2ecf20Sopenharmony_ci	struct device *dev = &vdev->dev;
51188c2ecf20Sopenharmony_ci	int rc = -ENOMEM;
51198c2ecf20Sopenharmony_ci
51208c2ecf20Sopenharmony_ci	ENTER;
51218c2ecf20Sopenharmony_ci	shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
51228c2ecf20Sopenharmony_ci	if (!shost) {
51238c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't allocate host data\n");
51248c2ecf20Sopenharmony_ci		goto out;
51258c2ecf20Sopenharmony_ci	}
51268c2ecf20Sopenharmony_ci
51278c2ecf20Sopenharmony_ci	shost->transportt = ibmvfc_transport_template;
51288c2ecf20Sopenharmony_ci	shost->can_queue = max_requests;
51298c2ecf20Sopenharmony_ci	shost->max_lun = max_lun;
51308c2ecf20Sopenharmony_ci	shost->max_id = max_targets;
51318c2ecf20Sopenharmony_ci	shost->max_sectors = IBMVFC_MAX_SECTORS;
51328c2ecf20Sopenharmony_ci	shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
51338c2ecf20Sopenharmony_ci	shost->unique_id = shost->host_no;
51348c2ecf20Sopenharmony_ci
51358c2ecf20Sopenharmony_ci	vhost = shost_priv(shost);
51368c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&vhost->sent);
51378c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&vhost->free);
51388c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&vhost->targets);
51398c2ecf20Sopenharmony_ci	sprintf(vhost->name, IBMVFC_NAME);
51408c2ecf20Sopenharmony_ci	vhost->host = shost;
51418c2ecf20Sopenharmony_ci	vhost->dev = dev;
51428c2ecf20Sopenharmony_ci	vhost->partition_number = -1;
51438c2ecf20Sopenharmony_ci	vhost->log_level = log_level;
51448c2ecf20Sopenharmony_ci	vhost->task_set = 1;
51458c2ecf20Sopenharmony_ci	strcpy(vhost->partition_name, "UNKNOWN");
51468c2ecf20Sopenharmony_ci	init_waitqueue_head(&vhost->work_wait_q);
51478c2ecf20Sopenharmony_ci	init_waitqueue_head(&vhost->init_wait_q);
51488c2ecf20Sopenharmony_ci	INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
51498c2ecf20Sopenharmony_ci	mutex_init(&vhost->passthru_mutex);
51508c2ecf20Sopenharmony_ci
51518c2ecf20Sopenharmony_ci	if ((rc = ibmvfc_alloc_mem(vhost)))
51528c2ecf20Sopenharmony_ci		goto free_scsi_host;
51538c2ecf20Sopenharmony_ci
51548c2ecf20Sopenharmony_ci	vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
51558c2ecf20Sopenharmony_ci					 shost->host_no);
51568c2ecf20Sopenharmony_ci
51578c2ecf20Sopenharmony_ci	if (IS_ERR(vhost->work_thread)) {
51588c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't create kernel thread: %ld\n",
51598c2ecf20Sopenharmony_ci			PTR_ERR(vhost->work_thread));
51608c2ecf20Sopenharmony_ci		rc = PTR_ERR(vhost->work_thread);
51618c2ecf20Sopenharmony_ci		goto free_host_mem;
51628c2ecf20Sopenharmony_ci	}
51638c2ecf20Sopenharmony_ci
51648c2ecf20Sopenharmony_ci	if ((rc = ibmvfc_init_crq(vhost))) {
51658c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
51668c2ecf20Sopenharmony_ci		goto kill_kthread;
51678c2ecf20Sopenharmony_ci	}
51688c2ecf20Sopenharmony_ci
51698c2ecf20Sopenharmony_ci	if ((rc = ibmvfc_init_event_pool(vhost))) {
51708c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
51718c2ecf20Sopenharmony_ci		goto release_crq;
51728c2ecf20Sopenharmony_ci	}
51738c2ecf20Sopenharmony_ci
51748c2ecf20Sopenharmony_ci	if ((rc = scsi_add_host(shost, dev)))
51758c2ecf20Sopenharmony_ci		goto release_event_pool;
51768c2ecf20Sopenharmony_ci
51778c2ecf20Sopenharmony_ci	fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
51788c2ecf20Sopenharmony_ci
51798c2ecf20Sopenharmony_ci	if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
51808c2ecf20Sopenharmony_ci					   &ibmvfc_trace_attr))) {
51818c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
51828c2ecf20Sopenharmony_ci		goto remove_shost;
51838c2ecf20Sopenharmony_ci	}
51848c2ecf20Sopenharmony_ci
51858c2ecf20Sopenharmony_ci	if (shost_to_fc_host(shost)->rqst_q)
51868c2ecf20Sopenharmony_ci		blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
51878c2ecf20Sopenharmony_ci	dev_set_drvdata(dev, vhost);
51888c2ecf20Sopenharmony_ci	spin_lock(&ibmvfc_driver_lock);
51898c2ecf20Sopenharmony_ci	list_add_tail(&vhost->queue, &ibmvfc_head);
51908c2ecf20Sopenharmony_ci	spin_unlock(&ibmvfc_driver_lock);
51918c2ecf20Sopenharmony_ci
51928c2ecf20Sopenharmony_ci	ibmvfc_send_crq_init(vhost);
51938c2ecf20Sopenharmony_ci	scsi_scan_host(shost);
51948c2ecf20Sopenharmony_ci	return 0;
51958c2ecf20Sopenharmony_ci
51968c2ecf20Sopenharmony_ciremove_shost:
51978c2ecf20Sopenharmony_ci	scsi_remove_host(shost);
51988c2ecf20Sopenharmony_cirelease_event_pool:
51998c2ecf20Sopenharmony_ci	ibmvfc_free_event_pool(vhost);
52008c2ecf20Sopenharmony_cirelease_crq:
52018c2ecf20Sopenharmony_ci	ibmvfc_release_crq_queue(vhost);
52028c2ecf20Sopenharmony_cikill_kthread:
52038c2ecf20Sopenharmony_ci	kthread_stop(vhost->work_thread);
52048c2ecf20Sopenharmony_cifree_host_mem:
52058c2ecf20Sopenharmony_ci	ibmvfc_free_mem(vhost);
52068c2ecf20Sopenharmony_cifree_scsi_host:
52078c2ecf20Sopenharmony_ci	scsi_host_put(shost);
52088c2ecf20Sopenharmony_ciout:
52098c2ecf20Sopenharmony_ci	LEAVE;
52108c2ecf20Sopenharmony_ci	return rc;
52118c2ecf20Sopenharmony_ci}
52128c2ecf20Sopenharmony_ci
52138c2ecf20Sopenharmony_ci/**
52148c2ecf20Sopenharmony_ci * ibmvfc_remove - Adapter hot plug remove entry point
52158c2ecf20Sopenharmony_ci * @vdev:	vio device struct
52168c2ecf20Sopenharmony_ci *
52178c2ecf20Sopenharmony_ci * Return value:
52188c2ecf20Sopenharmony_ci * 	0
52198c2ecf20Sopenharmony_ci **/
52208c2ecf20Sopenharmony_cistatic int ibmvfc_remove(struct vio_dev *vdev)
52218c2ecf20Sopenharmony_ci{
52228c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
52238c2ecf20Sopenharmony_ci	unsigned long flags;
52248c2ecf20Sopenharmony_ci
52258c2ecf20Sopenharmony_ci	ENTER;
52268c2ecf20Sopenharmony_ci	ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
52278c2ecf20Sopenharmony_ci
52288c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
52298c2ecf20Sopenharmony_ci	ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
52308c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
52318c2ecf20Sopenharmony_ci
52328c2ecf20Sopenharmony_ci	ibmvfc_wait_while_resetting(vhost);
52338c2ecf20Sopenharmony_ci	ibmvfc_release_crq_queue(vhost);
52348c2ecf20Sopenharmony_ci	kthread_stop(vhost->work_thread);
52358c2ecf20Sopenharmony_ci	fc_remove_host(vhost->host);
52368c2ecf20Sopenharmony_ci	scsi_remove_host(vhost->host);
52378c2ecf20Sopenharmony_ci
52388c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
52398c2ecf20Sopenharmony_ci	ibmvfc_purge_requests(vhost, DID_ERROR);
52408c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
52418c2ecf20Sopenharmony_ci	ibmvfc_free_event_pool(vhost);
52428c2ecf20Sopenharmony_ci
52438c2ecf20Sopenharmony_ci	ibmvfc_free_mem(vhost);
52448c2ecf20Sopenharmony_ci	spin_lock(&ibmvfc_driver_lock);
52458c2ecf20Sopenharmony_ci	list_del(&vhost->queue);
52468c2ecf20Sopenharmony_ci	spin_unlock(&ibmvfc_driver_lock);
52478c2ecf20Sopenharmony_ci	scsi_host_put(vhost->host);
52488c2ecf20Sopenharmony_ci	LEAVE;
52498c2ecf20Sopenharmony_ci	return 0;
52508c2ecf20Sopenharmony_ci}
52518c2ecf20Sopenharmony_ci
52528c2ecf20Sopenharmony_ci/**
52538c2ecf20Sopenharmony_ci * ibmvfc_resume - Resume from suspend
52548c2ecf20Sopenharmony_ci * @dev:	device struct
52558c2ecf20Sopenharmony_ci *
52568c2ecf20Sopenharmony_ci * We may have lost an interrupt across suspend/resume, so kick the
52578c2ecf20Sopenharmony_ci * interrupt handler
52588c2ecf20Sopenharmony_ci *
52598c2ecf20Sopenharmony_ci */
52608c2ecf20Sopenharmony_cistatic int ibmvfc_resume(struct device *dev)
52618c2ecf20Sopenharmony_ci{
52628c2ecf20Sopenharmony_ci	unsigned long flags;
52638c2ecf20Sopenharmony_ci	struct ibmvfc_host *vhost = dev_get_drvdata(dev);
52648c2ecf20Sopenharmony_ci	struct vio_dev *vdev = to_vio_dev(dev);
52658c2ecf20Sopenharmony_ci
52668c2ecf20Sopenharmony_ci	spin_lock_irqsave(vhost->host->host_lock, flags);
52678c2ecf20Sopenharmony_ci	vio_disable_interrupts(vdev);
52688c2ecf20Sopenharmony_ci	tasklet_schedule(&vhost->tasklet);
52698c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(vhost->host->host_lock, flags);
52708c2ecf20Sopenharmony_ci	return 0;
52718c2ecf20Sopenharmony_ci}
52728c2ecf20Sopenharmony_ci
52738c2ecf20Sopenharmony_ci/**
52748c2ecf20Sopenharmony_ci * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
52758c2ecf20Sopenharmony_ci * @vdev:	vio device struct
52768c2ecf20Sopenharmony_ci *
52778c2ecf20Sopenharmony_ci * Return value:
52788c2ecf20Sopenharmony_ci *	Number of bytes the driver will need to DMA map at the same time in
52798c2ecf20Sopenharmony_ci *	order to perform well.
52808c2ecf20Sopenharmony_ci */
52818c2ecf20Sopenharmony_cistatic unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
52828c2ecf20Sopenharmony_ci{
52838c2ecf20Sopenharmony_ci	unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
52848c2ecf20Sopenharmony_ci	return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
52858c2ecf20Sopenharmony_ci}
52868c2ecf20Sopenharmony_ci
52878c2ecf20Sopenharmony_cistatic const struct vio_device_id ibmvfc_device_table[] = {
52888c2ecf20Sopenharmony_ci	{"fcp", "IBM,vfc-client"},
52898c2ecf20Sopenharmony_ci	{ "", "" }
52908c2ecf20Sopenharmony_ci};
52918c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
52928c2ecf20Sopenharmony_ci
52938c2ecf20Sopenharmony_cistatic const struct dev_pm_ops ibmvfc_pm_ops = {
52948c2ecf20Sopenharmony_ci	.resume = ibmvfc_resume
52958c2ecf20Sopenharmony_ci};
52968c2ecf20Sopenharmony_ci
52978c2ecf20Sopenharmony_cistatic struct vio_driver ibmvfc_driver = {
52988c2ecf20Sopenharmony_ci	.id_table = ibmvfc_device_table,
52998c2ecf20Sopenharmony_ci	.probe = ibmvfc_probe,
53008c2ecf20Sopenharmony_ci	.remove = ibmvfc_remove,
53018c2ecf20Sopenharmony_ci	.get_desired_dma = ibmvfc_get_desired_dma,
53028c2ecf20Sopenharmony_ci	.name = IBMVFC_NAME,
53038c2ecf20Sopenharmony_ci	.pm = &ibmvfc_pm_ops,
53048c2ecf20Sopenharmony_ci};
53058c2ecf20Sopenharmony_ci
53068c2ecf20Sopenharmony_cistatic struct fc_function_template ibmvfc_transport_functions = {
53078c2ecf20Sopenharmony_ci	.show_host_fabric_name = 1,
53088c2ecf20Sopenharmony_ci	.show_host_node_name = 1,
53098c2ecf20Sopenharmony_ci	.show_host_port_name = 1,
53108c2ecf20Sopenharmony_ci	.show_host_supported_classes = 1,
53118c2ecf20Sopenharmony_ci	.show_host_port_type = 1,
53128c2ecf20Sopenharmony_ci	.show_host_port_id = 1,
53138c2ecf20Sopenharmony_ci	.show_host_maxframe_size = 1,
53148c2ecf20Sopenharmony_ci
53158c2ecf20Sopenharmony_ci	.get_host_port_state = ibmvfc_get_host_port_state,
53168c2ecf20Sopenharmony_ci	.show_host_port_state = 1,
53178c2ecf20Sopenharmony_ci
53188c2ecf20Sopenharmony_ci	.get_host_speed = ibmvfc_get_host_speed,
53198c2ecf20Sopenharmony_ci	.show_host_speed = 1,
53208c2ecf20Sopenharmony_ci
53218c2ecf20Sopenharmony_ci	.issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
53228c2ecf20Sopenharmony_ci	.terminate_rport_io = ibmvfc_terminate_rport_io,
53238c2ecf20Sopenharmony_ci
53248c2ecf20Sopenharmony_ci	.show_rport_maxframe_size = 1,
53258c2ecf20Sopenharmony_ci	.show_rport_supported_classes = 1,
53268c2ecf20Sopenharmony_ci
53278c2ecf20Sopenharmony_ci	.set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
53288c2ecf20Sopenharmony_ci	.show_rport_dev_loss_tmo = 1,
53298c2ecf20Sopenharmony_ci
53308c2ecf20Sopenharmony_ci	.get_starget_node_name = ibmvfc_get_starget_node_name,
53318c2ecf20Sopenharmony_ci	.show_starget_node_name = 1,
53328c2ecf20Sopenharmony_ci
53338c2ecf20Sopenharmony_ci	.get_starget_port_name = ibmvfc_get_starget_port_name,
53348c2ecf20Sopenharmony_ci	.show_starget_port_name = 1,
53358c2ecf20Sopenharmony_ci
53368c2ecf20Sopenharmony_ci	.get_starget_port_id = ibmvfc_get_starget_port_id,
53378c2ecf20Sopenharmony_ci	.show_starget_port_id = 1,
53388c2ecf20Sopenharmony_ci
53398c2ecf20Sopenharmony_ci	.bsg_request = ibmvfc_bsg_request,
53408c2ecf20Sopenharmony_ci	.bsg_timeout = ibmvfc_bsg_timeout,
53418c2ecf20Sopenharmony_ci};
53428c2ecf20Sopenharmony_ci
53438c2ecf20Sopenharmony_ci/**
53448c2ecf20Sopenharmony_ci * ibmvfc_module_init - Initialize the ibmvfc module
53458c2ecf20Sopenharmony_ci *
53468c2ecf20Sopenharmony_ci * Return value:
53478c2ecf20Sopenharmony_ci * 	0 on success / other on failure
53488c2ecf20Sopenharmony_ci **/
53498c2ecf20Sopenharmony_cistatic int __init ibmvfc_module_init(void)
53508c2ecf20Sopenharmony_ci{
53518c2ecf20Sopenharmony_ci	int rc;
53528c2ecf20Sopenharmony_ci
53538c2ecf20Sopenharmony_ci	if (!firmware_has_feature(FW_FEATURE_VIO))
53548c2ecf20Sopenharmony_ci		return -ENODEV;
53558c2ecf20Sopenharmony_ci
53568c2ecf20Sopenharmony_ci	printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
53578c2ecf20Sopenharmony_ci	       IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
53588c2ecf20Sopenharmony_ci
53598c2ecf20Sopenharmony_ci	ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
53608c2ecf20Sopenharmony_ci	if (!ibmvfc_transport_template)
53618c2ecf20Sopenharmony_ci		return -ENOMEM;
53628c2ecf20Sopenharmony_ci
53638c2ecf20Sopenharmony_ci	rc = vio_register_driver(&ibmvfc_driver);
53648c2ecf20Sopenharmony_ci	if (rc)
53658c2ecf20Sopenharmony_ci		fc_release_transport(ibmvfc_transport_template);
53668c2ecf20Sopenharmony_ci	return rc;
53678c2ecf20Sopenharmony_ci}
53688c2ecf20Sopenharmony_ci
53698c2ecf20Sopenharmony_ci/**
53708c2ecf20Sopenharmony_ci * ibmvfc_module_exit - Teardown the ibmvfc module
53718c2ecf20Sopenharmony_ci *
53728c2ecf20Sopenharmony_ci * Return value:
53738c2ecf20Sopenharmony_ci * 	nothing
53748c2ecf20Sopenharmony_ci **/
53758c2ecf20Sopenharmony_cistatic void __exit ibmvfc_module_exit(void)
53768c2ecf20Sopenharmony_ci{
53778c2ecf20Sopenharmony_ci	vio_unregister_driver(&ibmvfc_driver);
53788c2ecf20Sopenharmony_ci	fc_release_transport(ibmvfc_transport_template);
53798c2ecf20Sopenharmony_ci}
53808c2ecf20Sopenharmony_ci
53818c2ecf20Sopenharmony_cimodule_init(ibmvfc_module_init);
53828c2ecf20Sopenharmony_cimodule_exit(ibmvfc_module_exit);
5383