xref: /linux/drivers/scsi/ibmvscsi/ibmvfc-core.c (revision 08dbfad3f5040f5bdb6c529da20d6d4e81fefd72)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /*
4  * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
5  *
6  * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
7  *
8  * Copyright (C) IBM Corporation, 2008-2026
9  */
10 
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/dmapool.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/irqdomain.h>
18 #include <linux/kthread.h>
19 #include <linux/slab.h>
20 #include <linux/of.h>
21 #include <linux/pm.h>
22 #include <linux/stringify.h>
23 #include <linux/bsg-lib.h>
24 #include <asm/firmware.h>
25 #include <asm/irq.h>
26 #include <asm/vio.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_tcq.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/scsi_bsg_fc.h>
34 #include "ibmvfc.h"
35 
36 static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
37 static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
38 static u64 max_lun = IBMVFC_MAX_LUN;
39 static unsigned int max_targets = IBMVFC_MAX_TARGETS;
40 static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
41 static u16 max_sectors = IBMVFC_MAX_SECTORS;
42 static u16 scsi_qdepth = IBMVFC_SCSI_QDEPTH;
43 static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
44 static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
45 static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
46 static unsigned int mq_enabled = IBMVFC_MQ;
47 static unsigned int nr_scsi_hw_queues = IBMVFC_SCSI_HW_QUEUES;
48 static unsigned int nr_scsi_channels = IBMVFC_SCSI_CHANNELS;
49 static unsigned int nvme_enabled = IBMVFC_NVME;
50 static unsigned int nr_nvme_hw_queues = IBMVFC_NVME_HW_QUEUES;
51 static unsigned int nr_nvme_channels = IBMVFC_NVME_CHANNELS;
52 static unsigned int mig_channels_only = IBMVFC_MIG_NO_SUB_TO_CRQ;
53 static unsigned int mig_no_less_channels = IBMVFC_MIG_NO_N_TO_M;
54 
55 unsigned int ibmvfc_debug = IBMVFC_DEBUG;
56 
57 static LIST_HEAD(ibmvfc_head);
58 static DEFINE_SPINLOCK(ibmvfc_driver_lock);
59 static struct scsi_transport_template *ibmvfc_transport_template;
60 
61 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
62 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
63 MODULE_LICENSE("GPL");
64 MODULE_VERSION(IBMVFC_DRIVER_VERSION);
65 
66 module_param_named(mq, mq_enabled, uint, S_IRUGO);
67 MODULE_PARM_DESC(mq, "Enable multiqueue support. "
68 		 "[Default=" __stringify(IBMVFC_MQ) "]");
69 module_param_named(scsi_host_queues, nr_scsi_hw_queues, uint, S_IRUGO);
70 MODULE_PARM_DESC(scsi_host_queues, "Number of SCSI Host submission queues. "
71 		 "[Default=" __stringify(IBMVFC_SCSI_HW_QUEUES) "]");
72 module_param_named(scsi_hw_channels, nr_scsi_channels, uint, S_IRUGO);
73 MODULE_PARM_DESC(scsi_hw_channels, "Number of hw scsi channels to request. "
74 		 "[Default=" __stringify(IBMVFC_SCSI_CHANNELS) "]");
75 module_param_named(mig_channels_only, mig_channels_only, uint, S_IRUGO);
76 MODULE_PARM_DESC(mig_channels_only, "Prevent migration to non-channelized system. "
77 		 "[Default=" __stringify(IBMVFC_MIG_NO_SUB_TO_CRQ) "]");
78 module_param_named(mig_no_less_channels, mig_no_less_channels, uint, S_IRUGO);
79 MODULE_PARM_DESC(mig_no_less_channels, "Prevent migration to system with less channels. "
80 		 "[Default=" __stringify(IBMVFC_MIG_NO_N_TO_M) "]");
81 
82 module_param_named(nvme, nvme_enabled, uint, S_IRUGO);
83 MODULE_PARM_DESC(nvme, "Enable NVMe over FC support. "
84 		 "[Default=" __stringify(IBMVFC_NVME) "]");
85 module_param_named(nvme_host_queues, nr_nvme_hw_queues, uint, S_IRUGO);
86 MODULE_PARM_DESC(nvme_host_queues, "Number of NVMeoF Host submission queues. "
87 		 "[Default=" __stringify(IBMVFC_NVME_HW_QUEUES) "]");
88 module_param_named(nvme_hw_channels, nr_nvme_channels, uint, S_IRUGO);
89 MODULE_PARM_DESC(nvme_hw_channels, "Number of hw NVMeoF channels to request. "
90 		 "[Default=" __stringify(IBMVFC_NVME_CHANNELS) "]");
91 
92 module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
93 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
94 		 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
95 module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
96 MODULE_PARM_DESC(default_timeout,
97 		 "Default timeout in seconds for initialization and EH commands. "
98 		 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
99 module_param_named(max_requests, max_requests, uint, S_IRUGO);
100 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
101 		 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
102 module_param_named(max_sectors, max_sectors, ushort, S_IRUGO);
103 MODULE_PARM_DESC(max_sectors, "Maximum sectors for this adapter. "
104 		 "[Default=" __stringify(IBMVFC_MAX_SECTORS) "]");
105 module_param_named(scsi_qdepth, scsi_qdepth, ushort, S_IRUGO);
106 MODULE_PARM_DESC(scsi_qdepth, "Maximum scsi command depth per adapter queue. "
107 		 "[Default=" __stringify(IBMVFC_SCSI_QDEPTH) "]");
108 module_param_named(max_lun, max_lun, ullong, S_IRUGO);
109 MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
110 		 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
111 module_param_named(max_targets, max_targets, uint, S_IRUGO);
112 MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
113 		 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
114 module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
115 MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
116 		 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
117 module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
118 MODULE_PARM_DESC(debug, "Enable driver debug information. "
119 		 "[Default=" __stringify(IBMVFC_DEBUG) "]");
120 module_param_named(log_level, log_level, uint, 0);
121 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
122 		 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
123 module_param_named(cls3_error, cls3_error, uint, 0);
124 MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
125 		 "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
126 
127 static const struct {
128 	u16 status;
129 	u16 error;
130 	u8 result;
131 	u8 retry;
132 	int log;
133 	char *name;
134 } cmd_status [] = {
135 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
136 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
137 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
138 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
139 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
140 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
141 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
142 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
143 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
144 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
145 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
146 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
147 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
148 	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
149 
150 	{ IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
151 	{ IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
152 	{ IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
153 	{ IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
154 	{ IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
155 	{ IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
156 	{ IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
157 	{ IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
158 	{ IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
159 	{ IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
160 
161 	{ IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
162 	{ IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
163 	{ IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
164 	{ IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
165 	{ IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
166 	{ IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
167 	{ IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
168 	{ IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
169 	{ IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
170 	{ IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
171 	{ IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
172 
173 	{ IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
174 	{ IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." },
175 };
176 
177 static const char *proto_type[] = {
178 	"SCSI",
179 	"NVMe",
180 };
181 
182 static void ibmvfc_npiv_login(struct ibmvfc_host *);
183 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
184 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
185 static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
186 static void ibmvfc_npiv_logout(struct ibmvfc_host *);
187 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *);
188 static void ibmvfc_tgt_move_login(struct ibmvfc_target *);
189 
190 static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *, struct ibmvfc_channels *);
191 static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *, struct ibmvfc_channels *);
192 
193 static const char *unknown_error = "unknown error";
194 
h_reg_sub_crq(unsigned long unit_address,unsigned long ioba,unsigned long length,unsigned long * cookie,unsigned long * irq)195 static long h_reg_sub_crq(unsigned long unit_address, unsigned long ioba,
196 			  unsigned long length, unsigned long *cookie,
197 			  unsigned long *irq)
198 {
199 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
200 	long rc;
201 
202 	rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, ioba, length);
203 	*cookie = retbuf[0];
204 	*irq = retbuf[1];
205 
206 	return rc;
207 }
208 
ibmvfc_nvme_active(struct ibmvfc_host * vhost)209 static int ibmvfc_nvme_active(struct ibmvfc_host *vhost)
210 {
211 	return (ibmvfc_check_caps(vhost, IBMVFC_SUPPORT_NVMEOF) &&
212 		vhost->nvme_scrqs.active_queues);
213 }
214 
ibmvfc_get_fcp_iu(struct ibmvfc_host * vhost,struct ibmvfc_cmd * vfc_cmd)215 static struct ibmvfc_fcp_cmd_iu *ibmvfc_get_fcp_iu(struct ibmvfc_host *vhost,
216 						   struct ibmvfc_cmd *vfc_cmd)
217 {
218 	if (ibmvfc_check_caps(vhost, IBMVFC_SUPPORT_NVMEOF))
219 		return &vfc_cmd->v3scsi.iu;
220 	else if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
221 		return &vfc_cmd->v2.iu;
222 	else
223 		return &vfc_cmd->v1.iu;
224 }
225 
ibmvfc_get_fcp_rsp(struct ibmvfc_host * vhost,struct ibmvfc_cmd * vfc_cmd)226 static struct ibmvfc_fcp_rsp *ibmvfc_get_fcp_rsp(struct ibmvfc_host *vhost,
227 						 struct ibmvfc_cmd *vfc_cmd)
228 {
229 	if (ibmvfc_check_caps(vhost, IBMVFC_SUPPORT_NVMEOF))
230 		return &vfc_cmd->v3scsi.rsp;
231 	else if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
232 		return &vfc_cmd->v2.rsp;
233 	else
234 		return &vfc_cmd->v1.rsp;
235 }
236 
237 #ifdef CONFIG_SCSI_IBMVFC_TRACE
238 /**
239  * ibmvfc_trc_start - Log a start trace entry
240  * @evt:		ibmvfc event struct
241  *
242  **/
ibmvfc_trc_start(struct ibmvfc_event * evt)243 static void ibmvfc_trc_start(struct ibmvfc_event *evt)
244 {
245 	struct ibmvfc_host *vhost = evt->vhost;
246 	struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
247 	struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
248 	struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
249 	struct ibmvfc_trace_entry *entry;
250 	int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
251 
252 	entry = &vhost->trace[index];
253 	entry->evt = evt;
254 	entry->time = jiffies;
255 	entry->fmt = evt->crq.format;
256 	entry->type = IBMVFC_TRC_START;
257 
258 	switch (entry->fmt) {
259 	case IBMVFC_CMD_FORMAT:
260 		entry->op_code = iu->cdb[0];
261 		entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
262 		entry->lun = scsilun_to_int(&iu->lun);
263 		entry->tmf_flags = iu->tmf_flags;
264 		entry->u.start.xfer_len = be32_to_cpu(iu->xfer_len);
265 		break;
266 	case IBMVFC_MAD_FORMAT:
267 		entry->op_code = be32_to_cpu(mad->opcode);
268 		break;
269 	default:
270 		break;
271 	}
272 }
273 
274 /**
275  * ibmvfc_trc_end - Log an end trace entry
276  * @evt:		ibmvfc event struct
277  *
278  **/
ibmvfc_trc_end(struct ibmvfc_event * evt)279 static void ibmvfc_trc_end(struct ibmvfc_event *evt)
280 {
281 	struct ibmvfc_host *vhost = evt->vhost;
282 	struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
283 	struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
284 	struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
285 	struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
286 	struct ibmvfc_trace_entry *entry;
287 	int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
288 
289 	entry = &vhost->trace[index];
290 	entry->evt = evt;
291 	entry->time = jiffies;
292 	entry->fmt = evt->crq.format;
293 	entry->type = IBMVFC_TRC_END;
294 
295 	switch (entry->fmt) {
296 	case IBMVFC_CMD_FORMAT:
297 		entry->op_code = iu->cdb[0];
298 		entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
299 		entry->lun = scsilun_to_int(&iu->lun);
300 		entry->tmf_flags = iu->tmf_flags;
301 		entry->u.end.status = be16_to_cpu(vfc_cmd->status);
302 		entry->u.end.error = be16_to_cpu(vfc_cmd->error);
303 		entry->u.end.fcp_rsp_flags = rsp->flags;
304 		entry->u.end.rsp_code = rsp->data.info.rsp_code;
305 		entry->u.end.scsi_status = rsp->scsi_status;
306 		break;
307 	case IBMVFC_MAD_FORMAT:
308 		entry->op_code = be32_to_cpu(mad->opcode);
309 		entry->u.end.status = be16_to_cpu(mad->status);
310 		break;
311 	default:
312 		break;
313 
314 	}
315 }
316 
317 #else
318 #define ibmvfc_trc_start(evt) do { } while (0)
319 #define ibmvfc_trc_end(evt) do { } while (0)
320 #endif
321 
322 /**
323  * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
324  * @status:		status / error class
325  * @error:		error
326  *
327  * Return value:
328  *	index into cmd_status / -EINVAL on failure
329  **/
ibmvfc_get_err_index(u16 status,u16 error)330 static int ibmvfc_get_err_index(u16 status, u16 error)
331 {
332 	int i;
333 
334 	for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
335 		if ((cmd_status[i].status & status) == cmd_status[i].status &&
336 		    cmd_status[i].error == error)
337 			return i;
338 
339 	return -EINVAL;
340 }
341 
342 /**
343  * ibmvfc_get_cmd_error - Find the error description for the fcp response
344  * @status:		status / error class
345  * @error:		error
346  *
347  * Return value:
348  *	error description string
349  **/
ibmvfc_get_cmd_error(u16 status,u16 error)350 const char *ibmvfc_get_cmd_error(u16 status, u16 error)
351 {
352 	int rc = ibmvfc_get_err_index(status, error);
353 	if (rc >= 0)
354 		return cmd_status[rc].name;
355 	return unknown_error;
356 }
357 
358 /**
359  * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
360  * @vhost:      ibmvfc host struct
361  * @vfc_cmd:	ibmvfc command struct
362  *
363  * Return value:
364  *	SCSI result value to return for completed command
365  **/
ibmvfc_get_err_result(struct ibmvfc_host * vhost,struct ibmvfc_cmd * vfc_cmd)366 static int ibmvfc_get_err_result(struct ibmvfc_host *vhost, struct ibmvfc_cmd *vfc_cmd)
367 {
368 	int err;
369 	struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
370 	int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
371 
372 	if ((rsp->flags & FCP_RSP_LEN_VALID) &&
373 	    ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
374 	     rsp->data.info.rsp_code))
375 		return DID_ERROR << 16;
376 
377 	err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
378 	if (err >= 0)
379 		return rsp->scsi_status | (cmd_status[err].result << 16);
380 	return rsp->scsi_status | (DID_ERROR << 16);
381 }
382 
383 /**
384  * ibmvfc_retry_cmd - Determine if error status is retryable
385  * @status:		status / error class
386  * @error:		error
387  *
388  * Return value:
389  *	1 if error should be retried / 0 if it should not
390  **/
ibmvfc_retry_cmd(u16 status,u16 error)391 static int ibmvfc_retry_cmd(u16 status, u16 error)
392 {
393 	int rc = ibmvfc_get_err_index(status, error);
394 
395 	if (rc >= 0)
396 		return cmd_status[rc].retry;
397 	return 1;
398 }
399 
400 static const char *unknown_fc_explain = "unknown fc explain";
401 
402 static const struct {
403 	u16 fc_explain;
404 	char *name;
405 } ls_explain [] = {
406 	{ 0x00, "no additional explanation" },
407 	{ 0x01, "service parameter error - options" },
408 	{ 0x03, "service parameter error - initiator control" },
409 	{ 0x05, "service parameter error - recipient control" },
410 	{ 0x07, "service parameter error - received data field size" },
411 	{ 0x09, "service parameter error - concurrent seq" },
412 	{ 0x0B, "service parameter error - credit" },
413 	{ 0x0D, "invalid N_Port/F_Port_Name" },
414 	{ 0x0E, "invalid node/Fabric Name" },
415 	{ 0x0F, "invalid common service parameters" },
416 	{ 0x11, "invalid association header" },
417 	{ 0x13, "association header required" },
418 	{ 0x15, "invalid originator S_ID" },
419 	{ 0x17, "invalid OX_ID-RX-ID combination" },
420 	{ 0x19, "command (request) already in progress" },
421 	{ 0x1E, "N_Port Login requested" },
422 	{ 0x1F, "Invalid N_Port_ID" },
423 };
424 
425 static const struct {
426 	u16 fc_explain;
427 	char *name;
428 } gs_explain [] = {
429 	{ 0x00, "no additional explanation" },
430 	{ 0x01, "port identifier not registered" },
431 	{ 0x02, "port name not registered" },
432 	{ 0x03, "node name not registered" },
433 	{ 0x04, "class of service not registered" },
434 	{ 0x06, "initial process associator not registered" },
435 	{ 0x07, "FC-4 TYPEs not registered" },
436 	{ 0x08, "symbolic port name not registered" },
437 	{ 0x09, "symbolic node name not registered" },
438 	{ 0x0A, "port type not registered" },
439 	{ 0xF0, "authorization exception" },
440 	{ 0xF1, "authentication exception" },
441 	{ 0xF2, "data base full" },
442 	{ 0xF3, "data base empty" },
443 	{ 0xF4, "processing request" },
444 	{ 0xF5, "unable to verify connection" },
445 	{ 0xF6, "devices not in a common zone" },
446 };
447 
448 /**
449  * ibmvfc_get_ls_explain - Return the FC Explain description text
450  * @status:	FC Explain status
451  *
452  * Returns:
453  *	error string
454  **/
ibmvfc_get_ls_explain(u16 status)455 static const char *ibmvfc_get_ls_explain(u16 status)
456 {
457 	int i;
458 
459 	for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
460 		if (ls_explain[i].fc_explain == status)
461 			return ls_explain[i].name;
462 
463 	return unknown_fc_explain;
464 }
465 
466 /**
467  * ibmvfc_get_gs_explain - Return the FC Explain description text
468  * @status:	FC Explain status
469  *
470  * Returns:
471  *	error string
472  **/
ibmvfc_get_gs_explain(u16 status)473 static const char *ibmvfc_get_gs_explain(u16 status)
474 {
475 	int i;
476 
477 	for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
478 		if (gs_explain[i].fc_explain == status)
479 			return gs_explain[i].name;
480 
481 	return unknown_fc_explain;
482 }
483 
484 static const struct {
485 	enum ibmvfc_fc_type fc_type;
486 	char *name;
487 } fc_type [] = {
488 	{ IBMVFC_FABRIC_REJECT, "fabric reject" },
489 	{ IBMVFC_PORT_REJECT, "port reject" },
490 	{ IBMVFC_LS_REJECT, "ELS reject" },
491 	{ IBMVFC_FABRIC_BUSY, "fabric busy" },
492 	{ IBMVFC_PORT_BUSY, "port busy" },
493 	{ IBMVFC_BASIC_REJECT, "basic reject" },
494 	{ IBMVFC_FC4_LS_REJECT, "fc4 ls reject" },
495 };
496 
497 static const char *unknown_fc_type = "unknown fc type";
498 
499 /**
500  * ibmvfc_get_fc_type - Return the FC Type description text
501  * @status:	FC Type error status
502  *
503  * Returns:
504  *	error string
505  **/
ibmvfc_get_fc_type(u16 status)506 static const char *ibmvfc_get_fc_type(u16 status)
507 {
508 	int i;
509 
510 	for (i = 0; i < ARRAY_SIZE(fc_type); i++)
511 		if (fc_type[i].fc_type == status)
512 			return fc_type[i].name;
513 
514 	return unknown_fc_type;
515 }
516 
517 /**
518  * ibmvfc_set_tgt_action - Set the next init action for the target
519  * @tgt:		ibmvfc target struct
520  * @action:		action to perform
521  *
522  * Returns:
523  *	0 if action changed / non-zero if not changed
524  **/
ibmvfc_set_tgt_action(struct ibmvfc_target * tgt,enum ibmvfc_target_action action)525 static int ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
526 				  enum ibmvfc_target_action action)
527 {
528 	int rc = -EINVAL;
529 
530 	switch (tgt->action) {
531 	case IBMVFC_TGT_ACTION_LOGOUT_RPORT:
532 		if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT ||
533 		    action == IBMVFC_TGT_ACTION_DEL_RPORT) {
534 			tgt->action = action;
535 			rc = 0;
536 		}
537 		break;
538 	case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT:
539 		if (action == IBMVFC_TGT_ACTION_DEL_RPORT ||
540 		    action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
541 			tgt->action = action;
542 			rc = 0;
543 		}
544 		break;
545 	case IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT:
546 		if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
547 			tgt->action = action;
548 			rc = 0;
549 		}
550 		break;
551 	case IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT:
552 		if (action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
553 			tgt->action = action;
554 			rc = 0;
555 		}
556 		break;
557 	case IBMVFC_TGT_ACTION_DEL_RPORT:
558 		if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
559 			tgt->action = action;
560 			rc = 0;
561 		}
562 		break;
563 	case IBMVFC_TGT_ACTION_DELETED_RPORT:
564 		break;
565 	default:
566 		tgt->action = action;
567 		rc = 0;
568 		break;
569 	}
570 
571 	if (action >= IBMVFC_TGT_ACTION_LOGOUT_RPORT)
572 		tgt->add_rport = 0;
573 
574 	return rc;
575 }
576 
577 /**
578  * ibmvfc_set_host_state - Set the state for the host
579  * @vhost:		ibmvfc host struct
580  * @state:		state to set host to
581  *
582  * Returns:
583  *	0 if state changed / non-zero if not changed
584  **/
ibmvfc_set_host_state(struct ibmvfc_host * vhost,enum ibmvfc_host_state state)585 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
586 				  enum ibmvfc_host_state state)
587 {
588 	int rc = 0;
589 
590 	switch (vhost->state) {
591 	case IBMVFC_HOST_OFFLINE:
592 		rc = -EINVAL;
593 		break;
594 	default:
595 		vhost->state = state;
596 		break;
597 	}
598 
599 	return rc;
600 }
601 
602 /**
603  * ibmvfc_set_host_action - Set the next init action for the host
604  * @vhost:		ibmvfc host struct
605  * @action:		action to perform
606  *
607  **/
ibmvfc_set_host_action(struct ibmvfc_host * vhost,enum ibmvfc_host_action action)608 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
609 				   enum ibmvfc_host_action action)
610 {
611 	switch (action) {
612 	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
613 		if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
614 			vhost->action = action;
615 		break;
616 	case IBMVFC_HOST_ACTION_LOGO_WAIT:
617 		if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
618 			vhost->action = action;
619 		break;
620 	case IBMVFC_HOST_ACTION_INIT_WAIT:
621 		if (vhost->action == IBMVFC_HOST_ACTION_INIT)
622 			vhost->action = action;
623 		break;
624 	case IBMVFC_HOST_ACTION_QUERY:
625 		switch (vhost->action) {
626 		case IBMVFC_HOST_ACTION_INIT_WAIT:
627 		case IBMVFC_HOST_ACTION_NONE:
628 		case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
629 			vhost->action = action;
630 			break;
631 		default:
632 			break;
633 		}
634 		break;
635 	case IBMVFC_HOST_ACTION_TGT_INIT:
636 		if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
637 			vhost->action = action;
638 		break;
639 	case IBMVFC_HOST_ACTION_REENABLE:
640 	case IBMVFC_HOST_ACTION_RESET:
641 		vhost->action = action;
642 		break;
643 	case IBMVFC_HOST_ACTION_INIT:
644 	case IBMVFC_HOST_ACTION_TGT_DEL:
645 	case IBMVFC_HOST_ACTION_LOGO:
646 	case IBMVFC_HOST_ACTION_QUERY_TGTS:
647 	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
648 	case IBMVFC_HOST_ACTION_NONE:
649 	default:
650 		switch (vhost->action) {
651 		case IBMVFC_HOST_ACTION_RESET:
652 		case IBMVFC_HOST_ACTION_REENABLE:
653 			break;
654 		default:
655 			vhost->action = action;
656 			break;
657 		}
658 		break;
659 	}
660 }
661 
662 /**
663  * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
664  * @vhost:		ibmvfc host struct
665  *
666  * Return value:
667  *	nothing
668  **/
ibmvfc_reinit_host(struct ibmvfc_host * vhost)669 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
670 {
671 	if (vhost->action == IBMVFC_HOST_ACTION_NONE &&
672 	    vhost->state == IBMVFC_ACTIVE) {
673 		if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
674 			scsi_block_requests(vhost->host);
675 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
676 		}
677 	} else
678 		vhost->reinit = 1;
679 
680 	wake_up(&vhost->work_wait_q);
681 }
682 
683 /**
684  * ibmvfc_del_tgt - Schedule cleanup and removal of the target
685  * @tgt:		ibmvfc target struct
686  **/
ibmvfc_del_tgt(struct ibmvfc_target * tgt)687 static void ibmvfc_del_tgt(struct ibmvfc_target *tgt)
688 {
689 	if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT)) {
690 		tgt->job_step = ibmvfc_tgt_implicit_logout_and_del;
691 		tgt->init_retries = 0;
692 	}
693 	wake_up(&tgt->vhost->work_wait_q);
694 }
695 
696 /**
697  * ibmvfc_link_down - Handle a link down event from the adapter
698  * @vhost:	ibmvfc host struct
699  * @state:	ibmvfc host state to enter
700  *
701  **/
ibmvfc_link_down(struct ibmvfc_host * vhost,enum ibmvfc_host_state state)702 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
703 			     enum ibmvfc_host_state state)
704 {
705 	struct ibmvfc_target *tgt;
706 
707 	ENTER;
708 	scsi_block_requests(vhost->host);
709 	list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue)
710 		ibmvfc_del_tgt(tgt);
711 	list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue)
712 		ibmvfc_del_tgt(tgt);
713 	ibmvfc_set_host_state(vhost, state);
714 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
715 	vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
716 	wake_up(&vhost->work_wait_q);
717 	LEAVE;
718 }
719 
720 /**
721  * ibmvfc_init_host - Start host initialization
722  * @vhost:		ibmvfc host struct
723  *
724  * Return value:
725  *	nothing
726  **/
ibmvfc_init_host(struct ibmvfc_host * vhost)727 static void ibmvfc_init_host(struct ibmvfc_host *vhost)
728 {
729 	struct ibmvfc_target *tgt;
730 
731 	if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
732 		if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
733 			dev_err(vhost->dev,
734 				"Host initialization retries exceeded. Taking adapter offline\n");
735 			ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
736 			return;
737 		}
738 	}
739 
740 	if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
741 		memset(vhost->async_crq.msgs.async, 0, PAGE_SIZE);
742 		vhost->async_crq.cur = 0;
743 
744 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
745 			if (vhost->client_migrated)
746 				tgt->need_login = 1;
747 			else
748 				ibmvfc_del_tgt(tgt);
749 		}
750 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue) {
751 			if (vhost->client_migrated)
752 				tgt->need_login = 1;
753 			else
754 				ibmvfc_del_tgt(tgt);
755 		}
756 
757 		scsi_block_requests(vhost->host);
758 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
759 		vhost->job_step = ibmvfc_npiv_login;
760 		wake_up(&vhost->work_wait_q);
761 	}
762 }
763 
764 /**
765  * ibmvfc_send_crq - Send a CRQ
766  * @vhost:	ibmvfc host struct
767  * @word1:	the first 64 bits of the data
768  * @word2:	the second 64 bits of the data
769  *
770  * Return value:
771  *	0 on success / other on failure
772  **/
ibmvfc_send_crq(struct ibmvfc_host * vhost,u64 word1,u64 word2)773 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
774 {
775 	struct vio_dev *vdev = to_vio_dev(vhost->dev);
776 	return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
777 }
778 
ibmvfc_send_sub_crq(struct ibmvfc_host * vhost,u64 cookie,u64 word1,u64 word2,u64 word3,u64 word4)779 static int ibmvfc_send_sub_crq(struct ibmvfc_host *vhost, u64 cookie, u64 word1,
780 			       u64 word2, u64 word3, u64 word4)
781 {
782 	struct vio_dev *vdev = to_vio_dev(vhost->dev);
783 
784 	return plpar_hcall_norets(H_SEND_SUB_CRQ, vdev->unit_address, cookie,
785 				  word1, word2, word3, word4);
786 }
787 
788 /**
789  * ibmvfc_send_crq_init - Send a CRQ init message
790  * @vhost:	ibmvfc host struct
791  *
792  * Return value:
793  *	0 on success / other on failure
794  **/
ibmvfc_send_crq_init(struct ibmvfc_host * vhost)795 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
796 {
797 	ibmvfc_dbg(vhost, "Sending CRQ init\n");
798 	return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
799 }
800 
801 /**
802  * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
803  * @vhost:	ibmvfc host struct
804  *
805  * Return value:
806  *	0 on success / other on failure
807  **/
ibmvfc_send_crq_init_complete(struct ibmvfc_host * vhost)808 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
809 {
810 	ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
811 	return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
812 }
813 
814 /**
815  * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
816  * @vhost:	ibmvfc host who owns the event pool
817  * @queue:      ibmvfc queue struct
818  *
819  * Returns zero on success.
820  **/
ibmvfc_init_event_pool(struct ibmvfc_host * vhost,struct ibmvfc_queue * queue)821 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost,
822 				  struct ibmvfc_queue *queue)
823 {
824 	int i;
825 	struct ibmvfc_event_pool *pool = &queue->evt_pool;
826 
827 	ENTER;
828 	if (!queue->total_depth)
829 		return 0;
830 
831 	pool->size = queue->total_depth;
832 	pool->events = kzalloc_objs(*pool->events, pool->size);
833 	if (!pool->events)
834 		return -ENOMEM;
835 
836 	pool->iu_storage = dma_alloc_coherent(vhost->dev,
837 					      pool->size * sizeof(*pool->iu_storage),
838 					      &pool->iu_token, 0);
839 
840 	if (!pool->iu_storage) {
841 		kfree(pool->events);
842 		return -ENOMEM;
843 	}
844 
845 	INIT_LIST_HEAD(&queue->sent);
846 	INIT_LIST_HEAD(&queue->free);
847 	queue->evt_free = queue->evt_depth;
848 	queue->reserved_free = queue->reserved_depth;
849 	spin_lock_init(&queue->l_lock);
850 
851 	for (i = 0; i < pool->size; ++i) {
852 		struct ibmvfc_event *evt = &pool->events[i];
853 
854 		/*
855 		 * evt->active states
856 		 *  1 = in flight
857 		 *  0 = being completed
858 		 * -1 = free/freed
859 		 */
860 		atomic_set(&evt->active, -1);
861 		atomic_set(&evt->free, 1);
862 		evt->crq.valid = 0x80;
863 		evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
864 		evt->xfer_iu = pool->iu_storage + i;
865 		evt->vhost = vhost;
866 		evt->queue = queue;
867 		evt->ext_list = NULL;
868 		list_add_tail(&evt->queue_list, &queue->free);
869 	}
870 
871 	LEAVE;
872 	return 0;
873 }
874 
875 /**
876  * ibmvfc_free_event_pool - Frees memory of the event pool of a host
877  * @vhost:	ibmvfc host who owns the event pool
878  * @queue:      ibmvfc queue struct
879  *
880  **/
ibmvfc_free_event_pool(struct ibmvfc_host * vhost,struct ibmvfc_queue * queue)881 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost,
882 				   struct ibmvfc_queue *queue)
883 {
884 	int i;
885 	struct ibmvfc_event_pool *pool = &queue->evt_pool;
886 
887 	ENTER;
888 	for (i = 0; i < pool->size; ++i) {
889 		list_del(&pool->events[i].queue_list);
890 		BUG_ON(atomic_read(&pool->events[i].free) != 1);
891 		if (pool->events[i].ext_list)
892 			dma_pool_free(vhost->sg_pool,
893 				      pool->events[i].ext_list,
894 				      pool->events[i].ext_list_token);
895 	}
896 
897 	kfree(pool->events);
898 	dma_free_coherent(vhost->dev,
899 			  pool->size * sizeof(*pool->iu_storage),
900 			  pool->iu_storage, pool->iu_token);
901 	LEAVE;
902 }
903 
904 /**
905  * ibmvfc_free_queue - Deallocate queue
906  * @vhost:	ibmvfc host struct
907  * @queue:	ibmvfc queue struct
908  *
909  * Unmaps dma and deallocates page for messages
910  **/
ibmvfc_free_queue(struct ibmvfc_host * vhost,struct ibmvfc_queue * queue)911 static void ibmvfc_free_queue(struct ibmvfc_host *vhost,
912 			      struct ibmvfc_queue *queue)
913 {
914 	struct device *dev = vhost->dev;
915 
916 	dma_unmap_single(dev, queue->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
917 	free_page((unsigned long)queue->msgs.handle);
918 	queue->msgs.handle = NULL;
919 
920 	ibmvfc_free_event_pool(vhost, queue);
921 }
922 
923 /**
924  * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
925  * @vhost:	ibmvfc host struct
926  *
927  * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
928  * the crq with the hypervisor.
929  **/
ibmvfc_release_crq_queue(struct ibmvfc_host * vhost)930 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
931 {
932 	long rc = 0;
933 	struct vio_dev *vdev = to_vio_dev(vhost->dev);
934 	struct ibmvfc_queue *crq = &vhost->crq;
935 
936 	ibmvfc_dbg(vhost, "Releasing CRQ\n");
937 	free_irq(vdev->irq, vhost);
938 	tasklet_kill(&vhost->tasklet);
939 	do {
940 		if (rc)
941 			msleep(100);
942 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
943 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
944 
945 	vhost->state = IBMVFC_NO_CRQ;
946 	vhost->logged_in = 0;
947 
948 	ibmvfc_free_queue(vhost, crq);
949 }
950 
951 /**
952  * ibmvfc_reenable_crq_queue - reenables the CRQ
953  * @vhost:	ibmvfc host struct
954  *
955  * Return value:
956  *	0 on success / other on failure
957  **/
ibmvfc_reenable_crq_queue(struct ibmvfc_host * vhost)958 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
959 {
960 	int rc = 0;
961 	struct vio_dev *vdev = to_vio_dev(vhost->dev);
962 	unsigned long flags;
963 
964 	ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
965 	ibmvfc_dereg_sub_crqs(vhost, &vhost->nvme_scrqs);
966 
967 	/* Re-enable the CRQ */
968 	do {
969 		if (rc)
970 			msleep(100);
971 		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
972 	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
973 
974 	if (rc)
975 		dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
976 
977 	spin_lock_irqsave(vhost->host->host_lock, flags);
978 	spin_lock(vhost->crq.q_lock);
979 	vhost->do_enquiry = 1;
980 	vhost->using_channels = 0;
981 	vhost->do_scsi_login = 0;
982 	vhost->do_nvme_login = 0;
983 	spin_unlock(vhost->crq.q_lock);
984 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
985 
986 	ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
987 	ibmvfc_reg_sub_crqs(vhost, &vhost->nvme_scrqs);
988 
989 	return rc;
990 }
991 
992 /**
993  * ibmvfc_reset_crq - resets a crq after a failure
994  * @vhost:	ibmvfc host struct
995  *
996  * Return value:
997  *	0 on success / other on failure
998  **/
ibmvfc_reset_crq(struct ibmvfc_host * vhost)999 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
1000 {
1001 	int rc = 0;
1002 	unsigned long flags;
1003 	struct vio_dev *vdev = to_vio_dev(vhost->dev);
1004 	struct ibmvfc_queue *crq = &vhost->crq;
1005 
1006 	ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
1007 	ibmvfc_dereg_sub_crqs(vhost, &vhost->nvme_scrqs);
1008 
1009 	/* Close the CRQ */
1010 	do {
1011 		if (rc)
1012 			msleep(100);
1013 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
1014 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1015 
1016 	spin_lock_irqsave(vhost->host->host_lock, flags);
1017 	spin_lock(vhost->crq.q_lock);
1018 	vhost->state = IBMVFC_NO_CRQ;
1019 	vhost->logged_in = 0;
1020 	vhost->do_enquiry = 1;
1021 	vhost->using_channels = 0;
1022 	vhost->do_scsi_login = 0;
1023 	vhost->do_nvme_login = 0;
1024 
1025 	/* Clean out the queue */
1026 	memset(crq->msgs.crq, 0, PAGE_SIZE);
1027 	crq->cur = 0;
1028 
1029 	/* And re-open it again */
1030 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
1031 				crq->msg_token, PAGE_SIZE);
1032 
1033 	if (rc == H_CLOSED)
1034 		/* Adapter is good, but other end is not ready */
1035 		dev_warn(vhost->dev, "Partner adapter not ready\n");
1036 	else if (rc != 0)
1037 		dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
1038 
1039 	spin_unlock(vhost->crq.q_lock);
1040 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
1041 
1042 	ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
1043 	ibmvfc_reg_sub_crqs(vhost, &vhost->nvme_scrqs);
1044 
1045 	return rc;
1046 }
1047 
1048 /**
1049  * ibmvfc_valid_event - Determines if event is valid.
1050  * @pool:	event_pool that contains the event
1051  * @evt:	ibmvfc event to be checked for validity
1052  *
1053  * Return value:
1054  *	1 if event is valid / 0 if event is not valid
1055  **/
ibmvfc_valid_event(struct ibmvfc_event_pool * pool,struct ibmvfc_event * evt)1056 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
1057 			      struct ibmvfc_event *evt)
1058 {
1059 	int index = evt - pool->events;
1060 	if (index < 0 || index >= pool->size)	/* outside of bounds */
1061 		return 0;
1062 	if (evt != pool->events + index)	/* unaligned */
1063 		return 0;
1064 	return 1;
1065 }
1066 
1067 /**
1068  * ibmvfc_free_event - Free the specified event
1069  * @evt:	ibmvfc_event to be freed
1070  *
1071  **/
ibmvfc_free_event(struct ibmvfc_event * evt)1072 void ibmvfc_free_event(struct ibmvfc_event *evt)
1073 {
1074 	struct ibmvfc_event_pool *pool = &evt->queue->evt_pool;
1075 	unsigned long flags;
1076 
1077 	BUG_ON(!ibmvfc_valid_event(pool, evt));
1078 	BUG_ON(atomic_inc_return(&evt->free) != 1);
1079 	BUG_ON(atomic_dec_and_test(&evt->active));
1080 
1081 	spin_lock_irqsave(&evt->queue->l_lock, flags);
1082 	list_add_tail(&evt->queue_list, &evt->queue->free);
1083 	if (evt->reserved) {
1084 		evt->reserved = 0;
1085 		evt->queue->reserved_free++;
1086 	} else {
1087 		evt->queue->evt_free++;
1088 	}
1089 	if (evt->eh_comp)
1090 		complete(evt->eh_comp);
1091 	spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1092 }
1093 
1094 /**
1095  * ibmvfc_vfc_eh_done - EH done function for queued IO
1096  * @evt:	ibmvfc event struct
1097  *
1098  * This function does not setup any error status for scsi commands, that must be
1099  * done before this function gets called.
1100  **/
ibmvfc_vfc_eh_done(struct ibmvfc_event * evt)1101 static void ibmvfc_vfc_eh_done(struct ibmvfc_event *evt)
1102 {
1103 	struct scsi_cmnd *cmnd = evt->cmnd;
1104 	struct nvmefc_ls_req *ls_req = evt->ls_req;
1105 	struct nvmefc_fcp_req *fcp_req = evt->fcp_req;
1106 
1107 	if (cmnd) {
1108 		scsi_dma_unmap(cmnd);
1109 		scsi_done(cmnd);
1110 	} else if (fcp_req) {
1111 		fcp_req->rcv_rsplen = 0;
1112 		fcp_req->transferred_length = 0;
1113 		fcp_req->status = NVME_SC_INTERNAL;
1114 		fcp_req->done(fcp_req);
1115 	} else if (ls_req) {
1116 		ls_req->done(ls_req, -EIO);
1117 		kref_put(&evt->tgt->kref, ibmvfc_release_tgt);
1118 	}
1119 
1120 	ibmvfc_free_event(evt);
1121 }
1122 
1123 /**
1124  * ibmvfc_complete_purge - Complete failed command list
1125  * @purge_list:		list head of failed commands
1126  *
1127  * This function runs completions on commands to fail as a result of a
1128  * host reset or platform migration.
1129  **/
ibmvfc_complete_purge(struct list_head * purge_list)1130 static void ibmvfc_complete_purge(struct list_head *purge_list)
1131 {
1132 	struct ibmvfc_event *evt, *pos;
1133 
1134 	list_for_each_entry_safe(evt, pos, purge_list, queue_list) {
1135 		list_del(&evt->queue_list);
1136 		ibmvfc_trc_end(evt);
1137 		evt->done(evt);
1138 	}
1139 }
1140 
1141 /**
1142  * ibmvfc_fail_request - Fail request with specified error code
1143  * @evt:		ibmvfc event struct
1144  * @error_code:	error code to fail request with
1145  *
1146  * Return value:
1147  *	none
1148  **/
ibmvfc_fail_request(struct ibmvfc_event * evt,int error_code)1149 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
1150 {
1151 	/*
1152 	 * Anything we are failing should still be active. Otherwise, it
1153 	 * implies we already got a response for the command and are doing
1154 	 * something bad like double completing it.
1155 	 */
1156 	BUG_ON(!atomic_dec_and_test(&evt->active));
1157 	if (evt->cmnd) {
1158 		evt->cmnd->result = (error_code << 16);
1159 		evt->done = ibmvfc_vfc_eh_done;
1160 	} else if (evt->fcp_req || evt->ls_req)
1161 		evt->done = ibmvfc_vfc_eh_done;
1162 	else
1163 		evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
1164 
1165 	timer_delete(&evt->timer);
1166 }
1167 
1168 /**
1169  * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
1170  * @vhost:		ibmvfc host struct
1171  * @error_code:	error code to fail requests with
1172  *
1173  * Return value:
1174  *	none
1175  **/
ibmvfc_purge_requests(struct ibmvfc_host * vhost,int error_code)1176 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
1177 {
1178 	struct ibmvfc_event *evt, *pos;
1179 	struct ibmvfc_queue *scsi_q = vhost->scsi_scrqs.scrqs;
1180 	struct ibmvfc_queue *nvme_q = vhost->nvme_scrqs.scrqs;
1181 	unsigned long flags;
1182 	int shwqs, nhwqs = 0;
1183 	int i;
1184 
1185 	if (vhost->using_channels) {
1186 		shwqs = vhost->scsi_scrqs.active_queues;
1187 		nhwqs = vhost->nvme_scrqs.active_queues;
1188 	}
1189 
1190 	ibmvfc_dbg(vhost, "Purging all requests\n");
1191 	spin_lock_irqsave(&vhost->crq.l_lock, flags);
1192 	list_for_each_entry_safe(evt, pos, &vhost->crq.sent, queue_list)
1193 		ibmvfc_fail_request(evt, error_code);
1194 	list_splice_init(&vhost->crq.sent, &vhost->purge);
1195 	spin_unlock_irqrestore(&vhost->crq.l_lock, flags);
1196 
1197 	for (i = 0; i < shwqs; i++) {
1198 		spin_lock_irqsave(scsi_q[i].q_lock, flags);
1199 		spin_lock(&scsi_q[i].l_lock);
1200 		list_for_each_entry_safe(evt, pos, &scsi_q[i].sent, queue_list)
1201 			ibmvfc_fail_request(evt, error_code);
1202 		list_splice_init(&scsi_q[i].sent, &vhost->purge);
1203 		spin_unlock(&scsi_q[i].l_lock);
1204 		spin_unlock_irqrestore(scsi_q[i].q_lock, flags);
1205 	}
1206 
1207 	for (i = 0; i < nhwqs; i++) {
1208 		spin_lock_irqsave(nvme_q[i].q_lock, flags);
1209 		spin_lock(&nvme_q[i].l_lock);
1210 		list_for_each_entry_safe(evt, pos, &nvme_q[i].sent, queue_list)
1211 			ibmvfc_fail_request(evt, error_code);
1212 		list_splice_init(&nvme_q[i].sent, &vhost->purge);
1213 		spin_unlock(&nvme_q[i].l_lock);
1214 		spin_unlock_irqrestore(nvme_q[i].q_lock, flags);
1215 	}
1216 }
1217 
1218 /**
1219  * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
1220  * @vhost:	struct ibmvfc host to reset
1221  **/
ibmvfc_hard_reset_host(struct ibmvfc_host * vhost)1222 static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
1223 {
1224 	ibmvfc_purge_requests(vhost, DID_ERROR);
1225 	ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
1226 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
1227 }
1228 
1229 /**
1230  * __ibmvfc_reset_host - Reset the connection to the server (no locking)
1231  * @vhost:	struct ibmvfc host to reset
1232  **/
__ibmvfc_reset_host(struct ibmvfc_host * vhost)1233 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
1234 {
1235 	if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
1236 	    !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
1237 		scsi_block_requests(vhost->host);
1238 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
1239 		vhost->job_step = ibmvfc_npiv_logout;
1240 		wake_up(&vhost->work_wait_q);
1241 	} else
1242 		ibmvfc_hard_reset_host(vhost);
1243 }
1244 
1245 /**
1246  * ibmvfc_reset_host - Reset the connection to the server
1247  * @vhost:	ibmvfc host struct
1248  **/
ibmvfc_reset_host(struct ibmvfc_host * vhost)1249 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
1250 {
1251 	unsigned long flags;
1252 
1253 	spin_lock_irqsave(vhost->host->host_lock, flags);
1254 	__ibmvfc_reset_host(vhost);
1255 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
1256 }
1257 
1258 /**
1259  * ibmvfc_retry_host_init - Retry host initialization if allowed
1260  * @vhost:	ibmvfc host struct
1261  *
1262  * Returns: 1 if init will be retried / 0 if not
1263  *
1264  **/
ibmvfc_retry_host_init(struct ibmvfc_host * vhost)1265 static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
1266 {
1267 	int retry = 0;
1268 
1269 	if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
1270 		vhost->delay_init = 1;
1271 		if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
1272 			dev_err(vhost->dev,
1273 				"Host initialization retries exceeded. Taking adapter offline\n");
1274 			ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
1275 		} else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
1276 			__ibmvfc_reset_host(vhost);
1277 		else {
1278 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
1279 			retry = 1;
1280 		}
1281 	}
1282 
1283 	wake_up(&vhost->work_wait_q);
1284 	return retry;
1285 }
1286 
1287 /**
1288  * __ibmvfc_get_target - Find the specified scsi_target (no locking)
1289  * @starget:	scsi target struct
1290  *
1291  * Return value:
1292  *	ibmvfc_target struct / NULL if not found
1293  **/
__ibmvfc_get_target(struct scsi_target * starget)1294 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
1295 {
1296 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1297 	struct ibmvfc_host *vhost = shost_priv(shost);
1298 	struct ibmvfc_target *tgt;
1299 
1300 	list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue)
1301 		if (tgt->target_id == starget->id) {
1302 			kref_get(&tgt->kref);
1303 			return tgt;
1304 		}
1305 	return NULL;
1306 }
1307 
1308 /**
1309  * ibmvfc_get_target - Find the specified scsi_target
1310  * @starget:	scsi target struct
1311  *
1312  * Return value:
1313  *	ibmvfc_target struct / NULL if not found
1314  **/
ibmvfc_get_target(struct scsi_target * starget)1315 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
1316 {
1317 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1318 	struct ibmvfc_target *tgt;
1319 	unsigned long flags;
1320 
1321 	spin_lock_irqsave(shost->host_lock, flags);
1322 	tgt = __ibmvfc_get_target(starget);
1323 	spin_unlock_irqrestore(shost->host_lock, flags);
1324 	return tgt;
1325 }
1326 
1327 /**
1328  * ibmvfc_get_host_speed - Get host port speed
1329  * @shost:		scsi host struct
1330  *
1331  * Return value:
1332  * 	none
1333  **/
ibmvfc_get_host_speed(struct Scsi_Host * shost)1334 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
1335 {
1336 	struct ibmvfc_host *vhost = shost_priv(shost);
1337 	unsigned long flags;
1338 
1339 	spin_lock_irqsave(shost->host_lock, flags);
1340 	if (vhost->state == IBMVFC_ACTIVE) {
1341 		switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
1342 		case 1:
1343 			fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1344 			break;
1345 		case 2:
1346 			fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1347 			break;
1348 		case 4:
1349 			fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1350 			break;
1351 		case 8:
1352 			fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
1353 			break;
1354 		case 10:
1355 			fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
1356 			break;
1357 		case 16:
1358 			fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
1359 			break;
1360 		default:
1361 			ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
1362 				   be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
1363 			fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1364 			break;
1365 		}
1366 	} else
1367 		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1368 	spin_unlock_irqrestore(shost->host_lock, flags);
1369 }
1370 
1371 /**
1372  * ibmvfc_get_host_port_state - Get host port state
1373  * @shost:		scsi host struct
1374  *
1375  * Return value:
1376  * 	none
1377  **/
ibmvfc_get_host_port_state(struct Scsi_Host * shost)1378 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
1379 {
1380 	struct ibmvfc_host *vhost = shost_priv(shost);
1381 	unsigned long flags;
1382 
1383 	spin_lock_irqsave(shost->host_lock, flags);
1384 	switch (vhost->state) {
1385 	case IBMVFC_INITIALIZING:
1386 	case IBMVFC_ACTIVE:
1387 		fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1388 		break;
1389 	case IBMVFC_LINK_DOWN:
1390 		fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1391 		break;
1392 	case IBMVFC_LINK_DEAD:
1393 	case IBMVFC_HOST_OFFLINE:
1394 		fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1395 		break;
1396 	case IBMVFC_HALTED:
1397 		fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1398 		break;
1399 	case IBMVFC_NO_CRQ:
1400 		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1401 		break;
1402 	default:
1403 		ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
1404 		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1405 		break;
1406 	}
1407 	spin_unlock_irqrestore(shost->host_lock, flags);
1408 }
1409 
1410 /**
1411  * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1412  * @rport:		rport struct
1413  * @timeout:	timeout value
1414  *
1415  * Return value:
1416  * 	none
1417  **/
ibmvfc_set_rport_dev_loss_tmo(struct fc_rport * rport,u32 timeout)1418 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1419 {
1420 	if (timeout)
1421 		rport->dev_loss_tmo = timeout;
1422 	else
1423 		rport->dev_loss_tmo = 1;
1424 }
1425 
1426 /**
1427  * ibmvfc_release_tgt - Free memory allocated for a target
1428  * @kref:		kref struct
1429  *
1430  **/
ibmvfc_release_tgt(struct kref * kref)1431 void ibmvfc_release_tgt(struct kref *kref)
1432 {
1433 	struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1434 	mempool_free(tgt, tgt->vhost->tgt_pool);
1435 }
1436 
1437 /**
1438  * ibmvfc_get_starget_node_name - Get SCSI target's node name
1439  * @starget:	scsi target struct
1440  *
1441  * Return value:
1442  * 	none
1443  **/
ibmvfc_get_starget_node_name(struct scsi_target * starget)1444 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1445 {
1446 	struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1447 	fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1448 	if (tgt)
1449 		kref_put(&tgt->kref, ibmvfc_release_tgt);
1450 }
1451 
1452 /**
1453  * ibmvfc_get_starget_port_name - Get SCSI target's port name
1454  * @starget:	scsi target struct
1455  *
1456  * Return value:
1457  * 	none
1458  **/
ibmvfc_get_starget_port_name(struct scsi_target * starget)1459 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1460 {
1461 	struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1462 	fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1463 	if (tgt)
1464 		kref_put(&tgt->kref, ibmvfc_release_tgt);
1465 }
1466 
1467 /**
1468  * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1469  * @starget:	scsi target struct
1470  *
1471  * Return value:
1472  * 	none
1473  **/
ibmvfc_get_starget_port_id(struct scsi_target * starget)1474 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1475 {
1476 	struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1477 	fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1478 	if (tgt)
1479 		kref_put(&tgt->kref, ibmvfc_release_tgt);
1480 }
1481 
1482 /**
1483  * ibmvfc_wait_while_resetting - Wait while the host resets
1484  * @vhost:		ibmvfc host struct
1485  *
1486  * Return value:
1487  * 	0 on success / other on failure
1488  **/
ibmvfc_wait_while_resetting(struct ibmvfc_host * vhost)1489 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1490 {
1491 	long timeout = wait_event_timeout(vhost->init_wait_q,
1492 					  ((vhost->state == IBMVFC_ACTIVE ||
1493 					    vhost->state == IBMVFC_HOST_OFFLINE ||
1494 					    vhost->state == IBMVFC_LINK_DEAD) &&
1495 					   vhost->action == IBMVFC_HOST_ACTION_NONE),
1496 					  (init_timeout * HZ));
1497 
1498 	return timeout ? 0 : -EIO;
1499 }
1500 
1501 /**
1502  * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1503  * @shost:		scsi host struct
1504  *
1505  * Return value:
1506  * 	0 on success / other on failure
1507  **/
ibmvfc_issue_fc_host_lip(struct Scsi_Host * shost)1508 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1509 {
1510 	struct ibmvfc_host *vhost = shost_priv(shost);
1511 
1512 	dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1513 	ibmvfc_reset_host(vhost);
1514 	return ibmvfc_wait_while_resetting(vhost);
1515 }
1516 
1517 /**
1518  * ibmvfc_gather_partition_info - Gather info about the LPAR
1519  * @vhost:      ibmvfc host struct
1520  *
1521  * Return value:
1522  *	none
1523  **/
ibmvfc_gather_partition_info(struct ibmvfc_host * vhost)1524 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1525 {
1526 	struct device_node *rootdn;
1527 	const char *name;
1528 	const unsigned int *num;
1529 
1530 	rootdn = of_find_node_by_path("/");
1531 	if (!rootdn)
1532 		return;
1533 
1534 	name = of_get_property(rootdn, "ibm,partition-name", NULL);
1535 	if (name)
1536 		strscpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1537 	num = of_get_property(rootdn, "ibm,partition-no", NULL);
1538 	if (num)
1539 		vhost->partition_number = *num;
1540 	of_node_put(rootdn);
1541 }
1542 
1543 /**
1544  * ibmvfc_set_login_info - Setup info for NPIV login
1545  * @vhost:	ibmvfc host struct
1546  *
1547  * Return value:
1548  *	none
1549  **/
ibmvfc_set_login_info(struct ibmvfc_host * vhost)1550 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1551 {
1552 	struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1553 	struct ibmvfc_queue *async_crq = &vhost->async_crq;
1554 	struct device_node *of_node = vhost->dev->of_node;
1555 	const char *location;
1556 	u16 max_cmds;
1557 
1558 	max_cmds = scsi_qdepth + IBMVFC_NUM_INTERNAL_REQ;
1559 	if (mq_enabled)
1560 		max_cmds += (scsi_qdepth + IBMVFC_NUM_INTERNAL_SUBQ_REQ) *
1561 			(vhost->scsi_scrqs.desired_queues + vhost->nvme_scrqs.desired_queues);
1562 
1563 	memset(login_info, 0, sizeof(*login_info));
1564 
1565 	login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
1566 	login_info->max_dma_len = cpu_to_be64(max_sectors << 9);
1567 	login_info->max_payload = cpu_to_be32(sizeof(struct nvme_fc_cmd_iu));
1568 	login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
1569 	login_info->partition_num = cpu_to_be32(vhost->partition_number);
1570 	login_info->vfc_frame_version = cpu_to_be32(1);
1571 	login_info->fcp_version = cpu_to_be16(3);
1572 	login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
1573 	if (vhost->client_migrated)
1574 		login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
1575 
1576 	login_info->max_cmds = cpu_to_be32(max_cmds);
1577 	login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE | IBMVFC_CAN_SEND_VF_WWPN);
1578 
1579 	if (vhost->mq_enabled || vhost->using_channels) {
1580 		login_info->capabilities |= cpu_to_be64(IBMVFC_CAN_USE_CHANNELS);
1581 		if (vhost->nvme_enabled) {
1582 			login_info->capabilities |= cpu_to_be64(IBMVFC_YES_NVMEOF);
1583 			login_info->capabilities |= cpu_to_be64(IBMVFC_YES_SCSI);
1584 			login_info->capabilities |= cpu_to_be64(IBMVFC_CAN_USE_WWPN_ALL);
1585 		}
1586 	}
1587 
1588 	login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
1589 	login_info->async.len = cpu_to_be32(async_crq->size *
1590 					    sizeof(*async_crq->msgs.async));
1591 	strscpy(login_info->partition_name, vhost->partition_name,
1592 		sizeof(login_info->partition_name));
1593 
1594 	strscpy(login_info->device_name,
1595 		dev_name(&vhost->host->shost_gendev), sizeof(login_info->device_name));
1596 
1597 	location = of_get_property(of_node, "ibm,loc-code", NULL);
1598 	location = location ? location : dev_name(vhost->dev);
1599 	strscpy(login_info->drc_name, location, sizeof(login_info->drc_name));
1600 }
1601 
1602 /**
1603  * __ibmvfc_get_event - Gets the next free event in pool
1604  * @queue:      ibmvfc queue struct
1605  * @reserved:	event is for a reserved management command
1606  *
1607  * Returns a free event from the pool.
1608  **/
__ibmvfc_get_event(struct ibmvfc_queue * queue,int reserved)1609 struct ibmvfc_event *__ibmvfc_get_event(struct ibmvfc_queue *queue, int reserved)
1610 {
1611 	struct ibmvfc_event *evt = NULL;
1612 	unsigned long flags;
1613 
1614 	spin_lock_irqsave(&queue->l_lock, flags);
1615 	if (reserved && queue->reserved_free) {
1616 		evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
1617 		evt->reserved = 1;
1618 		queue->reserved_free--;
1619 	} else if (queue->evt_free) {
1620 		evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
1621 		queue->evt_free--;
1622 	} else {
1623 		goto out;
1624 	}
1625 
1626 	atomic_set(&evt->free, 0);
1627 	list_del(&evt->queue_list);
1628 out:
1629 	spin_unlock_irqrestore(&queue->l_lock, flags);
1630 	return evt;
1631 }
1632 
1633 /**
1634  * ibmvfc_locked_done - Calls evt completion with host_lock held
1635  * @evt:	ibmvfc evt to complete
1636  *
1637  * All non-scsi command completion callbacks have the expectation that the
1638  * host_lock is held. This callback is used by ibmvfc_init_event to wrap a
1639  * MAD evt with the host_lock.
1640  **/
ibmvfc_locked_done(struct ibmvfc_event * evt)1641 static void ibmvfc_locked_done(struct ibmvfc_event *evt)
1642 {
1643 	unsigned long flags;
1644 
1645 	spin_lock_irqsave(evt->vhost->host->host_lock, flags);
1646 	evt->_done(evt);
1647 	spin_unlock_irqrestore(evt->vhost->host->host_lock, flags);
1648 }
1649 
1650 /**
1651  * ibmvfc_init_event - Initialize fields in an event struct that are always
1652  *				required.
1653  * @evt:	The event
1654  * @done:	Routine to call when the event is responded to
1655  * @format:	SRP or MAD format
1656  **/
ibmvfc_init_event(struct ibmvfc_event * evt,void (* done)(struct ibmvfc_event *),u8 format)1657 void ibmvfc_init_event(struct ibmvfc_event *evt,
1658 			      void (*done) (struct ibmvfc_event *), u8 format)
1659 {
1660 	evt->cmnd = NULL;
1661 	evt->fcp_req = NULL;
1662 	evt->ls_req = NULL;
1663 	evt->sync_iu = NULL;
1664 	evt->eh_comp = NULL;
1665 	evt->crq.format = format;
1666 	if (format == IBMVFC_CMD_FORMAT)
1667 		evt->done = done;
1668 	else {
1669 		evt->_done = done;
1670 		evt->done = ibmvfc_locked_done;
1671 	}
1672 	evt->hwq = 0;
1673 }
1674 
1675 /**
1676  * ibmvfc_map_sg_list - Initialize scatterlist
1677  * @scmd:	scsi command struct
1678  * @nseg:	number of scatterlist segments
1679  * @md:	memory descriptor list to initialize
1680  **/
ibmvfc_map_sg_list(struct scsi_cmnd * scmd,int nseg,struct srp_direct_buf * md)1681 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1682 			       struct srp_direct_buf *md)
1683 {
1684 	int i;
1685 	struct scatterlist *sg;
1686 
1687 	scsi_for_each_sg(scmd, sg, nseg, i) {
1688 		md[i].va = cpu_to_be64(sg_dma_address(sg));
1689 		md[i].len = cpu_to_be32(sg_dma_len(sg));
1690 		md[i].key = 0;
1691 	}
1692 }
1693 
1694 /**
1695  * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
1696  * @scmd:		struct scsi_cmnd with the scatterlist
1697  * @evt:		ibmvfc event struct
1698  * @vfc_cmd:	vfc_cmd that contains the memory descriptor
1699  * @dev:		device for which to map dma memory
1700  *
1701  * Returns:
1702  *	0 on success / non-zero on failure
1703  **/
ibmvfc_map_sg_data(struct scsi_cmnd * scmd,struct ibmvfc_event * evt,struct ibmvfc_cmd * vfc_cmd,struct device * dev)1704 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1705 			      struct ibmvfc_event *evt,
1706 			      struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1707 {
1708 
1709 	int sg_mapped;
1710 	struct srp_direct_buf *data = &vfc_cmd->ioba;
1711 	struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1712 	struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(evt->vhost, vfc_cmd);
1713 
1714 	if (cls3_error)
1715 		vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1716 
1717 	sg_mapped = scsi_dma_map(scmd);
1718 	if (!sg_mapped) {
1719 		vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1720 		return 0;
1721 	} else if (unlikely(sg_mapped < 0)) {
1722 		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1723 			scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1724 		return sg_mapped;
1725 	}
1726 
1727 	if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1728 		vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1729 		iu->add_cdb_len |= IBMVFC_WRDATA;
1730 	} else {
1731 		vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1732 		iu->add_cdb_len |= IBMVFC_RDDATA;
1733 	}
1734 
1735 	if (sg_mapped == 1) {
1736 		ibmvfc_map_sg_list(scmd, sg_mapped, data);
1737 		return 0;
1738 	}
1739 
1740 	vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1741 
1742 	if (!evt->ext_list) {
1743 		evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1744 					       &evt->ext_list_token);
1745 
1746 		if (!evt->ext_list) {
1747 			scsi_dma_unmap(scmd);
1748 			if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1749 				scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1750 			return -ENOMEM;
1751 		}
1752 	}
1753 
1754 	ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1755 
1756 	data->va = cpu_to_be64(evt->ext_list_token);
1757 	data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1758 	data->key = 0;
1759 	return 0;
1760 }
1761 
1762 /**
1763  * ibmvfc_timeout - Internal command timeout handler
1764  * @t:	struct ibmvfc_event that timed out
1765  *
1766  * Called when an internally generated command times out
1767  **/
ibmvfc_timeout(struct timer_list * t)1768 static void ibmvfc_timeout(struct timer_list *t)
1769 {
1770 	struct ibmvfc_event *evt = timer_container_of(evt, t, timer);
1771 	struct ibmvfc_host *vhost = evt->vhost;
1772 	dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1773 	ibmvfc_reset_host(vhost);
1774 }
1775 
1776 /**
1777  * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1778  * @evt:		event to be sent
1779  * @vhost:		ibmvfc host struct
1780  * @timeout:	timeout in seconds - 0 means do not time command
1781  *
1782  * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1783  **/
ibmvfc_send_event(struct ibmvfc_event * evt,struct ibmvfc_host * vhost,unsigned long timeout)1784 int ibmvfc_send_event(struct ibmvfc_event *evt,
1785 			     struct ibmvfc_host *vhost, unsigned long timeout)
1786 {
1787 	__be64 *crq_as_u64 = (__be64 *) &evt->crq;
1788 	unsigned long flags;
1789 	int rc;
1790 
1791 	/* Copy the IU into the transfer area */
1792 	*evt->xfer_iu = evt->iu;
1793 	if (evt->crq.format == IBMVFC_CMD_FORMAT)
1794 		evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1795 	else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1796 		evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1797 	else
1798 		BUG();
1799 
1800 	timer_setup(&evt->timer, ibmvfc_timeout, 0);
1801 
1802 	if (timeout) {
1803 		evt->timer.expires = jiffies + (timeout * HZ);
1804 		add_timer(&evt->timer);
1805 	}
1806 
1807 	spin_lock_irqsave(&evt->queue->l_lock, flags);
1808 	list_add_tail(&evt->queue_list, &evt->queue->sent);
1809 	atomic_set(&evt->active, 1);
1810 
1811 	mb();
1812 
1813 	if (evt->queue->fmt == IBMVFC_SUB_CRQ_FMT)
1814 		rc = ibmvfc_send_sub_crq(vhost,
1815 					 evt->queue->vios_cookie,
1816 					 be64_to_cpu(crq_as_u64[0]),
1817 					 be64_to_cpu(crq_as_u64[1]),
1818 					 0, 0);
1819 	else
1820 		rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1821 				     be64_to_cpu(crq_as_u64[1]));
1822 
1823 	if (rc) {
1824 		atomic_set(&evt->active, 0);
1825 		list_del(&evt->queue_list);
1826 		spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1827 		timer_delete(&evt->timer);
1828 
1829 		/* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1830 		 * Firmware will send a CRQ with a transport event (0xFF) to
1831 		 * tell this client what has happened to the transport. This
1832 		 * will be handled in ibmvfc_handle_crq()
1833 		 */
1834 		if (rc == H_CLOSED) {
1835 			if (printk_ratelimit())
1836 				dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1837 			if (evt->cmnd)
1838 				scsi_dma_unmap(evt->cmnd);
1839 			ibmvfc_free_event(evt);
1840 			return SCSI_MLQUEUE_HOST_BUSY;
1841 		}
1842 
1843 		dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1844 		if (evt->cmnd) {
1845 			evt->cmnd->result = DID_ERROR << 16;
1846 			evt->done = ibmvfc_vfc_eh_done;
1847 		} else if (evt->fcp_req || evt->ls_req) {
1848 			evt->done = ibmvfc_vfc_eh_done;
1849 		} else {
1850 			evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1851 			evt->done = evt->_done;
1852 		}
1853 
1854 		evt->done(evt);
1855 	} else {
1856 		spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1857 		ibmvfc_trc_start(evt);
1858 	}
1859 
1860 	return 0;
1861 }
1862 
1863 /**
1864  * ibmvfc_log_error - Log an error for the failed command if appropriate
1865  * @evt:	ibmvfc event to log
1866  *
1867  **/
ibmvfc_log_error(struct ibmvfc_event * evt)1868 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1869 {
1870 	struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1871 	struct ibmvfc_host *vhost = evt->vhost;
1872 	struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
1873 	struct scsi_cmnd *cmnd = evt->cmnd;
1874 	const char *err = unknown_error;
1875 	int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1876 	int logerr = 0;
1877 	int rsp_code = 0;
1878 
1879 	if (index >= 0) {
1880 		logerr = cmd_status[index].log;
1881 		err = cmd_status[index].name;
1882 	}
1883 
1884 	if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1885 		return;
1886 
1887 	if (rsp->flags & FCP_RSP_LEN_VALID)
1888 		rsp_code = rsp->data.info.rsp_code;
1889 
1890 	scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
1891 		    "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1892 		    cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
1893 		    rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1894 }
1895 
1896 /**
1897  * ibmvfc_relogin - Log back into the specified device
1898  * @sdev:	scsi device struct
1899  *
1900  **/
ibmvfc_scsi_relogin(struct scsi_device * sdev)1901 static void ibmvfc_scsi_relogin(struct scsi_device *sdev)
1902 {
1903 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
1904 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1905 	struct ibmvfc_target *tgt;
1906 	unsigned long flags;
1907 
1908 	spin_lock_irqsave(vhost->host->host_lock, flags);
1909 	list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
1910 		if (rport == tgt->rport) {
1911 			ibmvfc_del_tgt(tgt);
1912 			break;
1913 		}
1914 	}
1915 
1916 	ibmvfc_reinit_host(vhost);
1917 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
1918 }
1919 
1920 /**
1921  * ibmvfc_scsi_done - Handle responses from commands
1922  * @evt:	ibmvfc event to be handled
1923  *
1924  * Used as a callback when sending scsi cmds.
1925  **/
ibmvfc_scsi_done(struct ibmvfc_event * evt)1926 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1927 {
1928 	struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1929 	struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(evt->vhost, vfc_cmd);
1930 	struct scsi_cmnd *cmnd = evt->cmnd;
1931 	u32 rsp_len = 0;
1932 	u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1933 
1934 	if (cmnd) {
1935 		if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1936 			scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1937 		else if (rsp->flags & FCP_RESID_UNDER)
1938 			scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1939 		else
1940 			scsi_set_resid(cmnd, 0);
1941 
1942 		if (vfc_cmd->status) {
1943 			cmnd->result = ibmvfc_get_err_result(evt->vhost, vfc_cmd);
1944 
1945 			if (rsp->flags & FCP_RSP_LEN_VALID)
1946 				rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1947 			if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1948 				sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1949 			if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1950 				memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1951 			if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1952 			    (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1953 				ibmvfc_scsi_relogin(cmnd->device);
1954 
1955 			if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1956 				cmnd->result = (DID_ERROR << 16);
1957 
1958 			ibmvfc_log_error(evt);
1959 		}
1960 
1961 		if (!cmnd->result &&
1962 		    (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1963 			cmnd->result = (DID_ERROR << 16);
1964 
1965 		scsi_dma_unmap(cmnd);
1966 		scsi_done(cmnd);
1967 	}
1968 
1969 	ibmvfc_free_event(evt);
1970 }
1971 
1972 /**
1973  * ibmvfc_host_chkready - Check if the host can accept commands
1974  * @vhost:	 struct ibmvfc host
1975  *
1976  * Returns:
1977  *	1 if host can accept command / 0 if not
1978  **/
ibmvfc_host_chkready(struct ibmvfc_host * vhost)1979 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1980 {
1981 	int result = 0;
1982 
1983 	switch (vhost->state) {
1984 	case IBMVFC_LINK_DEAD:
1985 	case IBMVFC_HOST_OFFLINE:
1986 		result = DID_NO_CONNECT << 16;
1987 		break;
1988 	case IBMVFC_NO_CRQ:
1989 	case IBMVFC_INITIALIZING:
1990 	case IBMVFC_HALTED:
1991 	case IBMVFC_LINK_DOWN:
1992 		result = DID_REQUEUE << 16;
1993 		break;
1994 	case IBMVFC_ACTIVE:
1995 		result = 0;
1996 		break;
1997 	}
1998 
1999 	return result;
2000 }
2001 
ibmvfc_init_vfc_cmd(struct ibmvfc_event * evt,struct scsi_device * sdev)2002 static struct ibmvfc_cmd *ibmvfc_init_vfc_cmd(struct ibmvfc_event *evt, struct scsi_device *sdev)
2003 {
2004 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2005 	struct ibmvfc_host *vhost = evt->vhost;
2006 	struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
2007 	struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
2008 	struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
2009 	size_t offset;
2010 
2011 	memset(vfc_cmd, 0, sizeof(*vfc_cmd));
2012 	if (ibmvfc_check_caps(vhost, IBMVFC_SUPPORT_NVMEOF))
2013 		offset = offsetof(struct ibmvfc_cmd, v3scsi.rsp);
2014 	else if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2015 		offset = offsetof(struct ibmvfc_cmd, v2.rsp);
2016 	else
2017 		offset = offsetof(struct ibmvfc_cmd, v1.rsp);
2018 	vfc_cmd->target_wwpn = cpu_to_be64(rport->port_name);
2019 	vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offset);
2020 	vfc_cmd->resp.len = cpu_to_be32(sizeof(*rsp));
2021 	vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2022 	vfc_cmd->payload_len = cpu_to_be32(sizeof(*iu));
2023 	vfc_cmd->resp_len = cpu_to_be32(sizeof(*rsp));
2024 	vfc_cmd->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2025 	vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
2026 	int_to_scsilun(sdev->lun, &iu->lun);
2027 
2028 	return vfc_cmd;
2029 }
2030 
2031 /**
2032  * ibmvfc_queuecommand - The queuecommand function of the scsi template
2033  * @shost:	scsi host struct
2034  * @cmnd:	struct scsi_cmnd to be executed
2035  *
2036  * Returns:
2037  *	0 on success / other on failure
2038  **/
ibmvfc_queuecommand(struct Scsi_Host * shost,struct scsi_cmnd * cmnd)2039 static enum scsi_qc_status ibmvfc_queuecommand(struct Scsi_Host *shost,
2040 					       struct scsi_cmnd *cmnd)
2041 {
2042 	struct ibmvfc_host *vhost = shost_priv(shost);
2043 	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
2044 	struct ibmvfc_cmd *vfc_cmd;
2045 	struct ibmvfc_fcp_cmd_iu *iu;
2046 	struct ibmvfc_event *evt;
2047 	u32 tag_and_hwq = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
2048 	u16 hwq = blk_mq_unique_tag_to_hwq(tag_and_hwq);
2049 	u16 scsi_channel;
2050 	int rc;
2051 
2052 	if (unlikely((rc = fc_remote_port_chkready(rport))) ||
2053 	    unlikely((rc = ibmvfc_host_chkready(vhost)))) {
2054 		cmnd->result = rc;
2055 		scsi_done(cmnd);
2056 		return 0;
2057 	}
2058 
2059 	cmnd->result = (DID_OK << 16);
2060 	if (vhost->using_channels) {
2061 		scsi_channel = hwq % vhost->scsi_scrqs.active_queues;
2062 		evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[scsi_channel]);
2063 		if (!evt)
2064 			return SCSI_MLQUEUE_HOST_BUSY;
2065 
2066 		evt->hwq = hwq % vhost->scsi_scrqs.active_queues;
2067 	} else {
2068 		evt = ibmvfc_get_event(&vhost->crq);
2069 		if (!evt)
2070 			return SCSI_MLQUEUE_HOST_BUSY;
2071 	}
2072 
2073 	ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
2074 	evt->cmnd = cmnd;
2075 
2076 	vfc_cmd = ibmvfc_init_vfc_cmd(evt, cmnd->device);
2077 	iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
2078 
2079 	iu->xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
2080 	memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
2081 
2082 	if (cmnd->flags & SCMD_TAGGED) {
2083 		vfc_cmd->task_tag = cpu_to_be64(scsi_cmd_to_rq(cmnd)->tag);
2084 		iu->pri_task_attr = IBMVFC_SIMPLE_TASK;
2085 	}
2086 
2087 	vfc_cmd->correlation = cpu_to_be64((u64)evt);
2088 
2089 	if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
2090 		return ibmvfc_send_event(evt, vhost, 0);
2091 
2092 	ibmvfc_free_event(evt);
2093 	if (rc == -ENOMEM)
2094 		return SCSI_MLQUEUE_HOST_BUSY;
2095 
2096 	if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2097 		scmd_printk(KERN_ERR, cmnd,
2098 			    "Failed to map DMA buffer for command. rc=%d\n", rc);
2099 
2100 	cmnd->result = DID_ERROR << 16;
2101 	scsi_done(cmnd);
2102 	return 0;
2103 }
2104 
2105 /**
2106  * ibmvfc_sync_completion - Signal that a synchronous command has completed
2107  * @evt:	ibmvfc event struct
2108  *
2109  **/
ibmvfc_sync_completion(struct ibmvfc_event * evt)2110 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
2111 {
2112 	/* copy the response back */
2113 	if (evt->sync_iu)
2114 		*evt->sync_iu = *evt->xfer_iu;
2115 
2116 	complete(&evt->comp);
2117 }
2118 
2119 /**
2120  * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
2121  * @evt:	struct ibmvfc_event
2122  *
2123  **/
ibmvfc_bsg_timeout_done(struct ibmvfc_event * evt)2124 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
2125 {
2126 	struct ibmvfc_host *vhost = evt->vhost;
2127 
2128 	ibmvfc_free_event(evt);
2129 	vhost->aborting_passthru = 0;
2130 	dev_info(vhost->dev, "Passthru command cancelled\n");
2131 }
2132 
2133 /**
2134  * ibmvfc_bsg_timeout - Handle a BSG timeout
2135  * @job:	struct bsg_job that timed out
2136  *
2137  * Returns:
2138  *	0 on success / other on failure
2139  **/
ibmvfc_bsg_timeout(struct bsg_job * job)2140 static int ibmvfc_bsg_timeout(struct bsg_job *job)
2141 {
2142 	struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2143 	unsigned long port_id = (unsigned long)job->dd_data;
2144 	struct ibmvfc_event *evt;
2145 	struct ibmvfc_tmf *tmf;
2146 	unsigned long flags;
2147 	int rc;
2148 
2149 	ENTER;
2150 	spin_lock_irqsave(vhost->host->host_lock, flags);
2151 	if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
2152 		__ibmvfc_reset_host(vhost);
2153 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2154 		return 0;
2155 	}
2156 
2157 	vhost->aborting_passthru = 1;
2158 	evt = ibmvfc_get_reserved_event(&vhost->crq);
2159 	if (!evt) {
2160 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2161 		return -ENOMEM;
2162 	}
2163 
2164 	ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
2165 
2166 	tmf = &evt->iu.tmf;
2167 	memset(tmf, 0, sizeof(*tmf));
2168 	tmf->common.version = cpu_to_be32(1);
2169 	tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2170 	tmf->common.length = cpu_to_be16(sizeof(*tmf));
2171 	tmf->scsi_id = cpu_to_be64(port_id);
2172 	tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2173 	tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
2174 	rc = ibmvfc_send_event(evt, vhost, default_timeout);
2175 
2176 	if (rc != 0) {
2177 		vhost->aborting_passthru = 0;
2178 		dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
2179 		rc = -EIO;
2180 	} else
2181 		dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
2182 			 port_id);
2183 
2184 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2185 
2186 	LEAVE;
2187 	return rc;
2188 }
2189 
2190 /**
2191  * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
2192  * @vhost:		struct ibmvfc_host to send command
2193  * @port_id:	port ID to send command
2194  *
2195  * Returns:
2196  *	0 on success / other on failure
2197  **/
ibmvfc_bsg_plogi(struct ibmvfc_host * vhost,unsigned int port_id)2198 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
2199 {
2200 	struct ibmvfc_port_login *plogi;
2201 	struct ibmvfc_target *tgt;
2202 	struct ibmvfc_event *evt;
2203 	union ibmvfc_iu rsp_iu;
2204 	unsigned long flags;
2205 	int rc = 0, issue_login = 1;
2206 
2207 	ENTER;
2208 	spin_lock_irqsave(vhost->host->host_lock, flags);
2209 	list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
2210 		if (tgt->scsi_id == port_id) {
2211 			issue_login = 0;
2212 			break;
2213 		}
2214 	}
2215 
2216 	if (!issue_login)
2217 		goto unlock_out;
2218 	if (unlikely((rc = ibmvfc_host_chkready(vhost))))
2219 		goto unlock_out;
2220 
2221 	evt = ibmvfc_get_reserved_event(&vhost->crq);
2222 	if (!evt) {
2223 		rc = -ENOMEM;
2224 		goto unlock_out;
2225 	}
2226 	ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2227 	plogi = &evt->iu.plogi;
2228 	memset(plogi, 0, sizeof(*plogi));
2229 	plogi->common.version = cpu_to_be32(1);
2230 	plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
2231 	plogi->common.length = cpu_to_be16(sizeof(*plogi));
2232 	plogi->scsi_id = cpu_to_be64(port_id);
2233 	evt->sync_iu = &rsp_iu;
2234 	init_completion(&evt->comp);
2235 
2236 	rc = ibmvfc_send_event(evt, vhost, default_timeout);
2237 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2238 
2239 	if (rc)
2240 		return -EIO;
2241 
2242 	wait_for_completion(&evt->comp);
2243 
2244 	if (rsp_iu.plogi.common.status)
2245 		rc = -EIO;
2246 
2247 	spin_lock_irqsave(vhost->host->host_lock, flags);
2248 	ibmvfc_free_event(evt);
2249 unlock_out:
2250 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2251 	LEAVE;
2252 	return rc;
2253 }
2254 
2255 /**
2256  * ibmvfc_bsg_request - Handle a BSG request
2257  * @job:	struct bsg_job to be executed
2258  *
2259  * Returns:
2260  *	0 on success / other on failure
2261  **/
ibmvfc_bsg_request(struct bsg_job * job)2262 static int ibmvfc_bsg_request(struct bsg_job *job)
2263 {
2264 	struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2265 	struct fc_rport *rport = fc_bsg_to_rport(job);
2266 	struct ibmvfc_passthru_mad *mad;
2267 	struct ibmvfc_event *evt;
2268 	union ibmvfc_iu rsp_iu;
2269 	unsigned long flags, port_id = -1;
2270 	struct fc_bsg_request *bsg_request = job->request;
2271 	struct fc_bsg_reply *bsg_reply = job->reply;
2272 	unsigned int code = bsg_request->msgcode;
2273 	int rc = 0, req_seg, rsp_seg, issue_login = 0;
2274 	u32 fc_flags, rsp_len;
2275 
2276 	ENTER;
2277 	bsg_reply->reply_payload_rcv_len = 0;
2278 	if (rport)
2279 		port_id = rport->port_id;
2280 
2281 	switch (code) {
2282 	case FC_BSG_HST_ELS_NOLOGIN:
2283 		port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
2284 			(bsg_request->rqst_data.h_els.port_id[1] << 8) |
2285 			bsg_request->rqst_data.h_els.port_id[2];
2286 		fallthrough;
2287 	case FC_BSG_RPT_ELS:
2288 		fc_flags = IBMVFC_FC_ELS;
2289 		break;
2290 	case FC_BSG_HST_CT:
2291 		issue_login = 1;
2292 		port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
2293 			(bsg_request->rqst_data.h_ct.port_id[1] << 8) |
2294 			bsg_request->rqst_data.h_ct.port_id[2];
2295 		fallthrough;
2296 	case FC_BSG_RPT_CT:
2297 		fc_flags = IBMVFC_FC_CT_IU;
2298 		break;
2299 	default:
2300 		return -ENOTSUPP;
2301 	}
2302 
2303 	if (port_id == -1)
2304 		return -EINVAL;
2305 	if (!mutex_trylock(&vhost->passthru_mutex))
2306 		return -EBUSY;
2307 
2308 	job->dd_data = (void *)port_id;
2309 	req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
2310 			     job->request_payload.sg_cnt, DMA_TO_DEVICE);
2311 
2312 	if (!req_seg) {
2313 		mutex_unlock(&vhost->passthru_mutex);
2314 		return -ENOMEM;
2315 	}
2316 
2317 	rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
2318 			     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2319 
2320 	if (!rsp_seg) {
2321 		dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2322 			     job->request_payload.sg_cnt, DMA_TO_DEVICE);
2323 		mutex_unlock(&vhost->passthru_mutex);
2324 		return -ENOMEM;
2325 	}
2326 
2327 	if (req_seg > 1 || rsp_seg > 1) {
2328 		rc = -EINVAL;
2329 		goto out;
2330 	}
2331 
2332 	if (issue_login)
2333 		rc = ibmvfc_bsg_plogi(vhost, port_id);
2334 
2335 	spin_lock_irqsave(vhost->host->host_lock, flags);
2336 
2337 	if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
2338 	    unlikely((rc = ibmvfc_host_chkready(vhost)))) {
2339 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2340 		goto out;
2341 	}
2342 
2343 	evt = ibmvfc_get_reserved_event(&vhost->crq);
2344 	if (!evt) {
2345 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2346 		rc = -ENOMEM;
2347 		goto out;
2348 	}
2349 	ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2350 	mad = &evt->iu.passthru;
2351 
2352 	memset(mad, 0, sizeof(*mad));
2353 	mad->common.version = cpu_to_be32(1);
2354 	mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
2355 	mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
2356 
2357 	mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
2358 		offsetof(struct ibmvfc_passthru_mad, iu));
2359 	mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
2360 
2361 	mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
2362 	mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
2363 	mad->iu.flags = cpu_to_be32(fc_flags);
2364 	mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2365 
2366 	mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
2367 	mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
2368 	mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
2369 	mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
2370 	mad->iu.scsi_id = cpu_to_be64(port_id);
2371 	mad->iu.tag = cpu_to_be64((u64)evt);
2372 	rsp_len = be32_to_cpu(mad->iu.rsp.len);
2373 
2374 	evt->sync_iu = &rsp_iu;
2375 	init_completion(&evt->comp);
2376 	rc = ibmvfc_send_event(evt, vhost, 0);
2377 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2378 
2379 	if (rc) {
2380 		rc = -EIO;
2381 		goto out;
2382 	}
2383 
2384 	wait_for_completion(&evt->comp);
2385 
2386 	if (rsp_iu.passthru.common.status)
2387 		rc = -EIO;
2388 	else
2389 		bsg_reply->reply_payload_rcv_len = rsp_len;
2390 
2391 	spin_lock_irqsave(vhost->host->host_lock, flags);
2392 	ibmvfc_free_event(evt);
2393 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2394 	bsg_reply->result = rc;
2395 	bsg_job_done(job, bsg_reply->result,
2396 		       bsg_reply->reply_payload_rcv_len);
2397 	rc = 0;
2398 out:
2399 	dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2400 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
2401 	dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
2402 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2403 	mutex_unlock(&vhost->passthru_mutex);
2404 	LEAVE;
2405 	return rc;
2406 }
2407 
2408 /**
2409  * ibmvfc_reset_device - Reset the device with the specified reset type
2410  * @sdev:	scsi device to reset
2411  * @type:	reset type
2412  * @desc:	reset type description for log messages
2413  *
2414  * Returns:
2415  *	0 on success / other on failure
2416  **/
ibmvfc_reset_device(struct scsi_device * sdev,int type,char * desc)2417 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
2418 {
2419 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2420 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2421 	struct ibmvfc_cmd *tmf;
2422 	struct ibmvfc_event *evt = NULL;
2423 	union ibmvfc_iu rsp_iu;
2424 	struct ibmvfc_fcp_cmd_iu *iu;
2425 	struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2426 	int rsp_rc = -EBUSY;
2427 	unsigned long flags;
2428 	int rsp_code = 0;
2429 
2430 	spin_lock_irqsave(vhost->host->host_lock, flags);
2431 	if (vhost->state == IBMVFC_ACTIVE) {
2432 		if (vhost->using_channels)
2433 			evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[0]);
2434 		else
2435 			evt = ibmvfc_get_event(&vhost->crq);
2436 
2437 		if (!evt) {
2438 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
2439 			return -ENOMEM;
2440 		}
2441 
2442 		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2443 		tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2444 		iu = ibmvfc_get_fcp_iu(vhost, tmf);
2445 
2446 		tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2447 		if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2448 			tmf->target_wwpn = cpu_to_be64(rport->port_name);
2449 		iu->tmf_flags = type;
2450 		evt->sync_iu = &rsp_iu;
2451 
2452 		init_completion(&evt->comp);
2453 		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2454 	}
2455 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2456 
2457 	if (rsp_rc != 0) {
2458 		sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2459 			    desc, rsp_rc);
2460 		return -EIO;
2461 	}
2462 
2463 	sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2464 	wait_for_completion(&evt->comp);
2465 
2466 	if (rsp_iu.cmd.status)
2467 		rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2468 
2469 	if (rsp_code) {
2470 		if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2471 			rsp_code = fc_rsp->data.info.rsp_code;
2472 
2473 		sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2474 			    "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2475 			    ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2476 			    be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2477 			    fc_rsp->scsi_status);
2478 		rsp_rc = -EIO;
2479 	} else
2480 		sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2481 
2482 	spin_lock_irqsave(vhost->host->host_lock, flags);
2483 	ibmvfc_free_event(evt);
2484 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2485 	return rsp_rc;
2486 }
2487 
2488 /**
2489  * ibmvfc_match_rport - Match function for specified remote port
2490  * @evt:	ibmvfc event struct
2491  * @rport:	device to match
2492  *
2493  * Returns:
2494  *	1 if event matches rport / 0 if event does not match rport
2495  **/
ibmvfc_match_rport(struct ibmvfc_event * evt,void * rport)2496 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2497 {
2498 	struct fc_rport *cmd_rport;
2499 
2500 	if (evt->cmnd) {
2501 		cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2502 		if (cmd_rport == rport)
2503 			return 1;
2504 	}
2505 	return 0;
2506 }
2507 
2508 /**
2509  * ibmvfc_match_target - Match function for specified target
2510  * @evt:	ibmvfc event struct
2511  * @device:	device to match (starget)
2512  *
2513  * Returns:
2514  *	1 if event matches starget / 0 if event does not match starget
2515  **/
ibmvfc_match_target(struct ibmvfc_event * evt,void * device)2516 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2517 {
2518 	if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2519 		return 1;
2520 	return 0;
2521 }
2522 
2523 /**
2524  * ibmvfc_match_lun - Match function for specified LUN
2525  * @evt:	ibmvfc event struct
2526  * @device:	device to match (sdev)
2527  *
2528  * Returns:
2529  *	1 if event matches sdev / 0 if event does not match sdev
2530  **/
ibmvfc_match_lun(struct ibmvfc_event * evt,void * device)2531 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2532 {
2533 	if (evt->cmnd && evt->cmnd->device == device)
2534 		return 1;
2535 	return 0;
2536 }
2537 
2538 /**
2539  * ibmvfc_event_is_free - Check if event is free or not
2540  * @evt:	ibmvfc event struct
2541  *
2542  * Returns:
2543  *	true / false
2544  **/
ibmvfc_event_is_free(struct ibmvfc_event * evt)2545 static bool ibmvfc_event_is_free(struct ibmvfc_event *evt)
2546 {
2547 	struct ibmvfc_event *loop_evt;
2548 
2549 	list_for_each_entry(loop_evt, &evt->queue->free, queue_list)
2550 		if (loop_evt == evt)
2551 			return true;
2552 
2553 	return false;
2554 }
2555 
2556 /**
2557  * ibmvfc_wait_for_ops - Wait for ops to complete
2558  * @vhost:	ibmvfc host struct
2559  * @device:	device to match (starget or sdev)
2560  * @match:	match function
2561  *
2562  * Returns:
2563  *	SUCCESS / FAILED
2564  **/
ibmvfc_wait_for_ops(struct ibmvfc_host * vhost,void * device,int (* match)(struct ibmvfc_event *,void *))2565 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2566 			       int (*match) (struct ibmvfc_event *, void *))
2567 {
2568 	struct ibmvfc_event *evt;
2569 	DECLARE_COMPLETION_ONSTACK(comp);
2570 	int wait, i, q_index, q_size;
2571 	unsigned long flags;
2572 	signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2573 	struct ibmvfc_queue *queues;
2574 
2575 	ENTER;
2576 	if (vhost->mq_enabled && vhost->using_channels) {
2577 		queues = vhost->scsi_scrqs.scrqs;
2578 		q_size = vhost->scsi_scrqs.active_queues;
2579 	} else {
2580 		queues = &vhost->crq;
2581 		q_size = 1;
2582 	}
2583 
2584 	do {
2585 		wait = 0;
2586 		spin_lock_irqsave(vhost->host->host_lock, flags);
2587 		for (q_index = 0; q_index < q_size; q_index++) {
2588 			spin_lock(&queues[q_index].l_lock);
2589 			for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2590 				evt = &queues[q_index].evt_pool.events[i];
2591 				if (!ibmvfc_event_is_free(evt)) {
2592 					if (match(evt, device)) {
2593 						evt->eh_comp = &comp;
2594 						wait++;
2595 					}
2596 				}
2597 			}
2598 			spin_unlock(&queues[q_index].l_lock);
2599 		}
2600 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2601 
2602 		if (wait) {
2603 			timeout = wait_for_completion_timeout(&comp, timeout);
2604 
2605 			if (!timeout) {
2606 				wait = 0;
2607 				spin_lock_irqsave(vhost->host->host_lock, flags);
2608 				for (q_index = 0; q_index < q_size; q_index++) {
2609 					spin_lock(&queues[q_index].l_lock);
2610 					for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2611 						evt = &queues[q_index].evt_pool.events[i];
2612 						if (!ibmvfc_event_is_free(evt)) {
2613 							if (match(evt, device)) {
2614 								evt->eh_comp = NULL;
2615 								wait++;
2616 							}
2617 						}
2618 					}
2619 					spin_unlock(&queues[q_index].l_lock);
2620 				}
2621 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
2622 				if (wait)
2623 					dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2624 				LEAVE;
2625 				return wait ? FAILED : SUCCESS;
2626 			}
2627 		}
2628 	} while (wait);
2629 
2630 	LEAVE;
2631 	return SUCCESS;
2632 }
2633 
ibmvfc_init_tmf(struct ibmvfc_queue * queue,struct scsi_device * sdev,int type)2634 static struct ibmvfc_event *ibmvfc_init_tmf(struct ibmvfc_queue *queue,
2635 					    struct scsi_device *sdev,
2636 					    int type)
2637 {
2638 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2639 	struct scsi_target *starget = scsi_target(sdev);
2640 	struct fc_rport *rport = starget_to_rport(starget);
2641 	struct ibmvfc_event *evt;
2642 	struct ibmvfc_tmf *tmf;
2643 
2644 	evt = ibmvfc_get_reserved_event(queue);
2645 	if (!evt)
2646 		return NULL;
2647 	ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2648 
2649 	tmf = &evt->iu.tmf;
2650 	memset(tmf, 0, sizeof(*tmf));
2651 	if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
2652 		tmf->common.version = cpu_to_be32(2);
2653 		tmf->target_wwpn = cpu_to_be64(rport->port_name);
2654 	} else {
2655 		tmf->common.version = cpu_to_be32(1);
2656 	}
2657 	tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2658 	tmf->common.length = cpu_to_be16(sizeof(*tmf));
2659 	tmf->scsi_id = cpu_to_be64(rport->port_id);
2660 	int_to_scsilun(sdev->lun, &tmf->lun);
2661 	if (!ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPRESS_ABTS))
2662 		type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2663 	if (vhost->state == IBMVFC_ACTIVE)
2664 		tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2665 	else
2666 		tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2667 	tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2668 	tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2669 
2670 	init_completion(&evt->comp);
2671 
2672 	return evt;
2673 }
2674 
ibmvfc_cancel_all_mq(struct scsi_device * sdev,int type)2675 static int ibmvfc_cancel_all_mq(struct scsi_device *sdev, int type)
2676 {
2677 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2678 	struct ibmvfc_event *evt, *found_evt, *temp;
2679 	struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
2680 	unsigned long flags;
2681 	int num_hwq, i;
2682 	int fail = 0;
2683 	LIST_HEAD(cancelq);
2684 	u16 status;
2685 
2686 	ENTER;
2687 	spin_lock_irqsave(vhost->host->host_lock, flags);
2688 	num_hwq = vhost->scsi_scrqs.active_queues;
2689 	for (i = 0; i < num_hwq; i++) {
2690 		spin_lock(queues[i].q_lock);
2691 		spin_lock(&queues[i].l_lock);
2692 		found_evt = NULL;
2693 		list_for_each_entry(evt, &queues[i].sent, queue_list) {
2694 			if (evt->cmnd && evt->cmnd->device == sdev) {
2695 				found_evt = evt;
2696 				break;
2697 			}
2698 		}
2699 		spin_unlock(&queues[i].l_lock);
2700 
2701 		if (found_evt && vhost->logged_in) {
2702 			evt = ibmvfc_init_tmf(&queues[i], sdev, type);
2703 			if (!evt) {
2704 				spin_unlock(queues[i].q_lock);
2705 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
2706 				return -ENOMEM;
2707 			}
2708 			evt->sync_iu = &queues[i].cancel_rsp;
2709 			ibmvfc_send_event(evt, vhost, default_timeout);
2710 			list_add_tail(&evt->cancel, &cancelq);
2711 		}
2712 
2713 		spin_unlock(queues[i].q_lock);
2714 	}
2715 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2716 
2717 	if (list_empty(&cancelq)) {
2718 		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2719 			sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2720 		return 0;
2721 	}
2722 
2723 	sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2724 
2725 	list_for_each_entry_safe(evt, temp, &cancelq, cancel) {
2726 		wait_for_completion(&evt->comp);
2727 		status = be16_to_cpu(evt->queue->cancel_rsp.mad_common.status);
2728 		list_del(&evt->cancel);
2729 		ibmvfc_free_event(evt);
2730 
2731 		if (status != IBMVFC_MAD_SUCCESS) {
2732 			sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2733 			switch (status) {
2734 			case IBMVFC_MAD_DRIVER_FAILED:
2735 			case IBMVFC_MAD_CRQ_ERROR:
2736 			/* Host adapter most likely going through reset, return success to
2737 			 * the caller will wait for the command being cancelled to get returned
2738 			 */
2739 				break;
2740 			default:
2741 				fail = 1;
2742 				break;
2743 			}
2744 		}
2745 	}
2746 
2747 	if (fail)
2748 		return -EIO;
2749 
2750 	sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2751 	LEAVE;
2752 	return 0;
2753 }
2754 
ibmvfc_cancel_all_sq(struct scsi_device * sdev,int type)2755 static int ibmvfc_cancel_all_sq(struct scsi_device *sdev, int type)
2756 {
2757 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2758 	struct ibmvfc_event *evt, *found_evt;
2759 	union ibmvfc_iu rsp;
2760 	int rsp_rc = -EBUSY;
2761 	unsigned long flags;
2762 	u16 status;
2763 
2764 	ENTER;
2765 	found_evt = NULL;
2766 	spin_lock_irqsave(vhost->host->host_lock, flags);
2767 	spin_lock(&vhost->crq.l_lock);
2768 	list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2769 		if (evt->cmnd && evt->cmnd->device == sdev) {
2770 			found_evt = evt;
2771 			break;
2772 		}
2773 	}
2774 	spin_unlock(&vhost->crq.l_lock);
2775 
2776 	if (!found_evt) {
2777 		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2778 			sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2779 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2780 		return 0;
2781 	}
2782 
2783 	if (vhost->logged_in) {
2784 		evt = ibmvfc_init_tmf(&vhost->crq, sdev, type);
2785 		evt->sync_iu = &rsp;
2786 		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2787 	}
2788 
2789 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2790 
2791 	if (rsp_rc != 0) {
2792 		sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2793 		/* If failure is received, the host adapter is most likely going
2794 		 through reset, return success so the caller will wait for the command
2795 		 being cancelled to get returned */
2796 		return 0;
2797 	}
2798 
2799 	sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2800 
2801 	wait_for_completion(&evt->comp);
2802 	status = be16_to_cpu(rsp.mad_common.status);
2803 	spin_lock_irqsave(vhost->host->host_lock, flags);
2804 	ibmvfc_free_event(evt);
2805 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2806 
2807 	if (status != IBMVFC_MAD_SUCCESS) {
2808 		sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2809 		switch (status) {
2810 		case IBMVFC_MAD_DRIVER_FAILED:
2811 		case IBMVFC_MAD_CRQ_ERROR:
2812 			/* Host adapter most likely going through reset, return success to
2813 			 the caller will wait for the command being cancelled to get returned */
2814 			return 0;
2815 		default:
2816 			return -EIO;
2817 		};
2818 	}
2819 
2820 	sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2821 	return 0;
2822 }
2823 
2824 /**
2825  * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2826  * @sdev:	scsi device to cancel commands
2827  * @type:	type of error recovery being performed
2828  *
2829  * This sends a cancel to the VIOS for the specified device. This does
2830  * NOT send any abort to the actual device. That must be done separately.
2831  *
2832  * Returns:
2833  *	0 on success / other on failure
2834  **/
ibmvfc_cancel_all(struct scsi_device * sdev,int type)2835 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2836 {
2837 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2838 
2839 	if (vhost->mq_enabled && vhost->using_channels)
2840 		return ibmvfc_cancel_all_mq(sdev, type);
2841 	else
2842 		return ibmvfc_cancel_all_sq(sdev, type);
2843 }
2844 
2845 /**
2846  * ibmvfc_match_key - Match function for specified cancel key
2847  * @evt:	ibmvfc event struct
2848  * @key:	cancel key to match
2849  *
2850  * Returns:
2851  *	1 if event matches key / 0 if event does not match key
2852  **/
ibmvfc_match_key(struct ibmvfc_event * evt,void * key)2853 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2854 {
2855 	unsigned long cancel_key = (unsigned long)key;
2856 
2857 	if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2858 	    be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2859 		return 1;
2860 	return 0;
2861 }
2862 
2863 /**
2864  * ibmvfc_match_evt - Match function for specified event
2865  * @evt:	ibmvfc event struct
2866  * @match:	event to match
2867  *
2868  * Returns:
2869  *	1 if event matches key / 0 if event does not match key
2870  **/
ibmvfc_match_evt(struct ibmvfc_event * evt,void * match)2871 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2872 {
2873 	if (evt == match)
2874 		return 1;
2875 	return 0;
2876 }
2877 
2878 /**
2879  * ibmvfc_abort_task_set - Abort outstanding commands to the device
2880  * @sdev:	scsi device to abort commands
2881  *
2882  * This sends an Abort Task Set to the VIOS for the specified device. This does
2883  * NOT send any cancel to the VIOS. That must be done separately.
2884  *
2885  * Returns:
2886  *	0 on success / other on failure
2887  **/
ibmvfc_abort_task_set(struct scsi_device * sdev)2888 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2889 {
2890 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2891 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2892 	struct ibmvfc_cmd *tmf;
2893 	struct ibmvfc_event *evt, *found_evt;
2894 	union ibmvfc_iu rsp_iu;
2895 	struct ibmvfc_fcp_cmd_iu *iu;
2896 	struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2897 	int rc, rsp_rc = -EBUSY;
2898 	unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2899 	int rsp_code = 0;
2900 
2901 	found_evt = NULL;
2902 	spin_lock_irqsave(vhost->host->host_lock, flags);
2903 	spin_lock(&vhost->crq.l_lock);
2904 	list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2905 		if (evt->cmnd && evt->cmnd->device == sdev) {
2906 			found_evt = evt;
2907 			break;
2908 		}
2909 	}
2910 	spin_unlock(&vhost->crq.l_lock);
2911 
2912 	if (!found_evt) {
2913 		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2914 			sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2915 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2916 		return 0;
2917 	}
2918 
2919 	if (vhost->state == IBMVFC_ACTIVE) {
2920 		evt = ibmvfc_get_event(&vhost->crq);
2921 		if (!evt) {
2922 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
2923 			return -ENOMEM;
2924 		}
2925 		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2926 		tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2927 		iu = ibmvfc_get_fcp_iu(vhost, tmf);
2928 
2929 		if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2930 			tmf->target_wwpn = cpu_to_be64(rport->port_name);
2931 		iu->tmf_flags = IBMVFC_ABORT_TASK_SET;
2932 		tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2933 		evt->sync_iu = &rsp_iu;
2934 
2935 		tmf->correlation = cpu_to_be64((u64)evt);
2936 
2937 		init_completion(&evt->comp);
2938 		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2939 	}
2940 
2941 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2942 
2943 	if (rsp_rc != 0) {
2944 		sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2945 		return -EIO;
2946 	}
2947 
2948 	sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2949 	timeout = wait_for_completion_timeout(&evt->comp, timeout);
2950 
2951 	if (!timeout) {
2952 		rc = ibmvfc_cancel_all(sdev, 0);
2953 		if (!rc) {
2954 			rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2955 			if (rc == SUCCESS)
2956 				rc = 0;
2957 		}
2958 
2959 		if (rc) {
2960 			sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2961 			ibmvfc_reset_host(vhost);
2962 			rsp_rc = -EIO;
2963 			rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2964 
2965 			if (rc == SUCCESS)
2966 				rsp_rc = 0;
2967 
2968 			rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2969 			if (rc != SUCCESS) {
2970 				spin_lock_irqsave(vhost->host->host_lock, flags);
2971 				ibmvfc_hard_reset_host(vhost);
2972 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
2973 				rsp_rc = 0;
2974 			}
2975 
2976 			goto out;
2977 		}
2978 	}
2979 
2980 	if (rsp_iu.cmd.status)
2981 		rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2982 
2983 	if (rsp_code) {
2984 		if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2985 			rsp_code = fc_rsp->data.info.rsp_code;
2986 
2987 		sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2988 			    "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2989 			    ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2990 			    be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2991 			    fc_rsp->scsi_status);
2992 		rsp_rc = -EIO;
2993 	} else
2994 		sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2995 
2996 out:
2997 	spin_lock_irqsave(vhost->host->host_lock, flags);
2998 	ibmvfc_free_event(evt);
2999 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3000 	return rsp_rc;
3001 }
3002 
3003 /**
3004  * ibmvfc_eh_abort_handler - Abort a command
3005  * @cmd:	scsi command to abort
3006  *
3007  * Returns:
3008  *	SUCCESS / FAST_IO_FAIL / FAILED
3009  **/
ibmvfc_eh_abort_handler(struct scsi_cmnd * cmd)3010 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
3011 {
3012 	struct scsi_device *sdev = cmd->device;
3013 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
3014 	int cancel_rc, block_rc;
3015 	int rc = FAILED;
3016 
3017 	ENTER;
3018 	block_rc = fc_block_scsi_eh(cmd);
3019 	ibmvfc_wait_while_resetting(vhost);
3020 	if (block_rc != FAST_IO_FAIL) {
3021 		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
3022 		ibmvfc_abort_task_set(sdev);
3023 	} else
3024 		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
3025 
3026 	if (!cancel_rc)
3027 		rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
3028 
3029 	if (block_rc == FAST_IO_FAIL && rc != FAILED)
3030 		rc = FAST_IO_FAIL;
3031 
3032 	LEAVE;
3033 	return rc;
3034 }
3035 
3036 /**
3037  * ibmvfc_eh_device_reset_handler - Reset a single LUN
3038  * @cmd:	scsi command struct
3039  *
3040  * Returns:
3041  *	SUCCESS / FAST_IO_FAIL / FAILED
3042  **/
ibmvfc_eh_device_reset_handler(struct scsi_cmnd * cmd)3043 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
3044 {
3045 	struct scsi_device *sdev = cmd->device;
3046 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
3047 	int cancel_rc, block_rc, reset_rc = 0;
3048 	int rc = FAILED;
3049 
3050 	ENTER;
3051 	block_rc = fc_block_scsi_eh(cmd);
3052 	ibmvfc_wait_while_resetting(vhost);
3053 	if (block_rc != FAST_IO_FAIL) {
3054 		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
3055 		reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
3056 	} else
3057 		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
3058 
3059 	if (!cancel_rc && !reset_rc)
3060 		rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
3061 
3062 	if (block_rc == FAST_IO_FAIL && rc != FAILED)
3063 		rc = FAST_IO_FAIL;
3064 
3065 	LEAVE;
3066 	return rc;
3067 }
3068 
3069 /**
3070  * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
3071  * @sdev:	scsi device struct
3072  * @data:	return code
3073  *
3074  **/
ibmvfc_dev_cancel_all_noreset(struct scsi_device * sdev,void * data)3075 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
3076 {
3077 	unsigned long *rc = data;
3078 	*rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
3079 }
3080 
3081 /**
3082  * ibmvfc_eh_target_reset_handler - Reset the target
3083  * @cmd:	scsi command struct
3084  *
3085  * Returns:
3086  *	SUCCESS / FAST_IO_FAIL / FAILED
3087  **/
ibmvfc_eh_target_reset_handler(struct scsi_cmnd * cmd)3088 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
3089 {
3090 	struct scsi_target *starget = scsi_target(cmd->device);
3091 	struct fc_rport *rport = starget_to_rport(starget);
3092 	struct Scsi_Host *shost = rport_to_shost(rport);
3093 	struct ibmvfc_host *vhost = shost_priv(shost);
3094 	int block_rc;
3095 	int reset_rc = 0;
3096 	int rc = FAILED;
3097 	unsigned long cancel_rc = 0;
3098 	bool tgt_reset = false;
3099 
3100 	ENTER;
3101 	block_rc = fc_block_rport(rport);
3102 	ibmvfc_wait_while_resetting(vhost);
3103 	if (block_rc != FAST_IO_FAIL) {
3104 		struct scsi_device *sdev;
3105 
3106 		shost_for_each_device(sdev, shost) {
3107 			if ((sdev->channel != starget->channel) ||
3108 			    (sdev->id != starget->id))
3109 				continue;
3110 
3111 			cancel_rc |= ibmvfc_cancel_all(sdev,
3112 						       IBMVFC_TMF_TGT_RESET);
3113 			if (!tgt_reset) {
3114 				reset_rc = ibmvfc_reset_device(sdev,
3115 					IBMVFC_TARGET_RESET, "target");
3116 				tgt_reset = true;
3117 			}
3118 		}
3119 	} else
3120 		starget_for_each_device(starget, &cancel_rc,
3121 					ibmvfc_dev_cancel_all_noreset);
3122 
3123 	if (!cancel_rc && !reset_rc)
3124 		rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
3125 
3126 	if (block_rc == FAST_IO_FAIL && rc != FAILED)
3127 		rc = FAST_IO_FAIL;
3128 
3129 	LEAVE;
3130 	return rc;
3131 }
3132 
3133 /**
3134  * ibmvfc_eh_host_reset_handler - Reset the connection to the server
3135  * @cmd:	struct scsi_cmnd having problems
3136  *
3137  **/
ibmvfc_eh_host_reset_handler(struct scsi_cmnd * cmd)3138 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
3139 {
3140 	int rc;
3141 	struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
3142 
3143 	dev_err(vhost->dev, "Resetting connection due to error recovery\n");
3144 	rc = ibmvfc_issue_fc_host_lip(vhost->host);
3145 
3146 	return rc ? FAILED : SUCCESS;
3147 }
3148 
3149 /**
3150  * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
3151  * @rport:		rport struct
3152  *
3153  * Return value:
3154  * 	none
3155  **/
ibmvfc_terminate_rport_io(struct fc_rport * rport)3156 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
3157 {
3158 	struct Scsi_Host *shost = rport_to_shost(rport);
3159 	struct ibmvfc_host *vhost = shost_priv(shost);
3160 	struct fc_rport *dev_rport;
3161 	struct scsi_device *sdev;
3162 	struct ibmvfc_target *tgt;
3163 	unsigned long rc, flags;
3164 	unsigned int found;
3165 
3166 	ENTER;
3167 	shost_for_each_device(sdev, shost) {
3168 		dev_rport = starget_to_rport(scsi_target(sdev));
3169 		if (dev_rport != rport)
3170 			continue;
3171 		ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
3172 	}
3173 
3174 	rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
3175 
3176 	if (rc == FAILED)
3177 		ibmvfc_issue_fc_host_lip(shost);
3178 
3179 	spin_lock_irqsave(shost->host_lock, flags);
3180 	found = 0;
3181 	list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
3182 		if (tgt->scsi_id == rport->port_id) {
3183 			found++;
3184 			break;
3185 		}
3186 	}
3187 
3188 	if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
3189 		/*
3190 		 * If we get here, that means we previously attempted to send
3191 		 * an implicit logout to the target but it failed, most likely
3192 		 * due to I/O being pending, so we need to send it again
3193 		 */
3194 		ibmvfc_del_tgt(tgt);
3195 		ibmvfc_reinit_host(vhost);
3196 	}
3197 
3198 	spin_unlock_irqrestore(shost->host_lock, flags);
3199 	LEAVE;
3200 }
3201 
3202 static const struct ibmvfc_async_desc ae_desc [] = {
3203 	{ "PLOGI",	IBMVFC_AE_ELS_PLOGI,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3204 	{ "LOGO",	IBMVFC_AE_ELS_LOGO,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3205 	{ "PRLO",	IBMVFC_AE_ELS_PRLO,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3206 	{ "N-Port SCN",	IBMVFC_AE_SCN_NPORT,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3207 	{ "Group SCN",	IBMVFC_AE_SCN_GROUP,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3208 	{ "Domain SCN",	IBMVFC_AE_SCN_DOMAIN,	IBMVFC_DEFAULT_LOG_LEVEL },
3209 	{ "Fabric SCN",	IBMVFC_AE_SCN_FABRIC,	IBMVFC_DEFAULT_LOG_LEVEL },
3210 	{ "Link Up",	IBMVFC_AE_LINK_UP,	IBMVFC_DEFAULT_LOG_LEVEL },
3211 	{ "Link Down",	IBMVFC_AE_LINK_DOWN,	IBMVFC_DEFAULT_LOG_LEVEL },
3212 	{ "Link Dead",	IBMVFC_AE_LINK_DEAD,	IBMVFC_DEFAULT_LOG_LEVEL },
3213 	{ "Halt",	IBMVFC_AE_HALT,		IBMVFC_DEFAULT_LOG_LEVEL },
3214 	{ "Resume",	IBMVFC_AE_RESUME,	IBMVFC_DEFAULT_LOG_LEVEL },
3215 	{ "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
3216 };
3217 
3218 static const struct ibmvfc_async_desc unknown_ae = {
3219 	"Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
3220 };
3221 
3222 /**
3223  * ibmvfc_get_ae_desc - Get text description for async event
3224  * @ae:	async event
3225  *
3226  **/
ibmvfc_get_ae_desc(u64 ae)3227 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
3228 {
3229 	int i;
3230 
3231 	for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
3232 		if (ae_desc[i].ae == ae)
3233 			return &ae_desc[i];
3234 
3235 	return &unknown_ae;
3236 }
3237 
3238 static const struct {
3239 	enum ibmvfc_ae_link_state state;
3240 	const char *desc;
3241 } link_desc [] = {
3242 	{ IBMVFC_AE_LS_LINK_UP,		" link up" },
3243 	{ IBMVFC_AE_LS_LINK_BOUNCED,	" link bounced" },
3244 	{ IBMVFC_AE_LS_LINK_DOWN,	" link down" },
3245 	{ IBMVFC_AE_LS_LINK_DEAD,	" link dead" },
3246 };
3247 
3248 /**
3249  * ibmvfc_get_link_state - Get text description for link state
3250  * @state:	link state
3251  *
3252  **/
ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)3253 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
3254 {
3255 	int i;
3256 
3257 	for (i = 0; i < ARRAY_SIZE(link_desc); i++)
3258 		if (link_desc[i].state == state)
3259 			return link_desc[i].desc;
3260 
3261 	return "";
3262 }
3263 
3264 /**
3265  * ibmvfc_handle_async - Handle an async event from the adapter
3266  * @crq:	crq to process
3267  * @vhost:	ibmvfc host struct
3268  *
3269  **/
ibmvfc_handle_async(struct ibmvfc_async_crq * crq,struct ibmvfc_host * vhost)3270 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
3271 				struct ibmvfc_host *vhost)
3272 {
3273 	const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
3274 	struct ibmvfc_target *tgt;
3275 
3276 	ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
3277 		   " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
3278 		   be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
3279 		   ibmvfc_get_link_state(crq->link_state));
3280 
3281 	switch (be64_to_cpu(crq->event)) {
3282 	case IBMVFC_AE_RESUME:
3283 		switch (crq->link_state) {
3284 		case IBMVFC_AE_LS_LINK_DOWN:
3285 			ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3286 			break;
3287 		case IBMVFC_AE_LS_LINK_DEAD:
3288 			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3289 			break;
3290 		case IBMVFC_AE_LS_LINK_UP:
3291 		case IBMVFC_AE_LS_LINK_BOUNCED:
3292 		default:
3293 			vhost->events_to_log |= IBMVFC_AE_LINKUP;
3294 			vhost->delay_init = 1;
3295 			__ibmvfc_reset_host(vhost);
3296 			break;
3297 		}
3298 
3299 		break;
3300 	case IBMVFC_AE_LINK_UP:
3301 		vhost->events_to_log |= IBMVFC_AE_LINKUP;
3302 		vhost->delay_init = 1;
3303 		__ibmvfc_reset_host(vhost);
3304 		break;
3305 	case IBMVFC_AE_SCN_FABRIC:
3306 	case IBMVFC_AE_SCN_DOMAIN:
3307 		vhost->events_to_log |= IBMVFC_AE_RSCN;
3308 		if (vhost->state < IBMVFC_HALTED) {
3309 			vhost->delay_init = 1;
3310 			__ibmvfc_reset_host(vhost);
3311 		}
3312 		break;
3313 	case IBMVFC_AE_SCN_NPORT:
3314 	case IBMVFC_AE_SCN_GROUP:
3315 		vhost->events_to_log |= IBMVFC_AE_RSCN;
3316 		ibmvfc_reinit_host(vhost);
3317 		break;
3318 	case IBMVFC_AE_ELS_LOGO:
3319 	case IBMVFC_AE_ELS_PRLO:
3320 	case IBMVFC_AE_ELS_PLOGI:
3321 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
3322 			if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
3323 				break;
3324 			if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
3325 				continue;
3326 			if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
3327 				continue;
3328 			if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
3329 				continue;
3330 			if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
3331 				tgt->logo_rcvd = 1;
3332 			if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
3333 				ibmvfc_del_tgt(tgt);
3334 				ibmvfc_reinit_host(vhost);
3335 			}
3336 		}
3337 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue) {
3338 			if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
3339 				break;
3340 			if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
3341 				continue;
3342 			if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
3343 				continue;
3344 			if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
3345 				continue;
3346 			if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
3347 				tgt->logo_rcvd = 1;
3348 			if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
3349 				ibmvfc_del_tgt(tgt);
3350 				ibmvfc_reinit_host(vhost);
3351 			}
3352 		}
3353 		break;
3354 	case IBMVFC_AE_LINK_DOWN:
3355 	case IBMVFC_AE_ADAPTER_FAILED:
3356 		ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3357 		break;
3358 	case IBMVFC_AE_LINK_DEAD:
3359 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3360 		break;
3361 	case IBMVFC_AE_HALT:
3362 		ibmvfc_link_down(vhost, IBMVFC_HALTED);
3363 		break;
3364 	default:
3365 		dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
3366 		break;
3367 	}
3368 }
3369 
3370 /**
3371  * ibmvfc_handle_crq - Handles and frees received events in the CRQ
3372  * @crq:	Command/Response queue
3373  * @vhost:	ibmvfc host struct
3374  * @evt_doneq:	Event done queue
3375  *
3376 **/
ibmvfc_handle_crq(struct ibmvfc_crq * crq,struct ibmvfc_host * vhost,struct list_head * evt_doneq)3377 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3378 			      struct list_head *evt_doneq)
3379 {
3380 	long rc;
3381 	struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3382 
3383 	switch (crq->valid) {
3384 	case IBMVFC_CRQ_INIT_RSP:
3385 		switch (crq->format) {
3386 		case IBMVFC_CRQ_INIT:
3387 			dev_info(vhost->dev, "Partner initialized\n");
3388 			/* Send back a response */
3389 			rc = ibmvfc_send_crq_init_complete(vhost);
3390 			if (rc == 0)
3391 				ibmvfc_init_host(vhost);
3392 			else
3393 				dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
3394 			break;
3395 		case IBMVFC_CRQ_INIT_COMPLETE:
3396 			dev_info(vhost->dev, "Partner initialization complete\n");
3397 			ibmvfc_init_host(vhost);
3398 			break;
3399 		default:
3400 			dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
3401 		}
3402 		return;
3403 	case IBMVFC_CRQ_XPORT_EVENT:
3404 		vhost->state = IBMVFC_NO_CRQ;
3405 		vhost->logged_in = 0;
3406 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3407 		if (crq->format == IBMVFC_PARTITION_MIGRATED) {
3408 			/* We need to re-setup the interpartition connection */
3409 			dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
3410 			vhost->client_migrated = 1;
3411 
3412 			scsi_block_requests(vhost->host);
3413 			ibmvfc_purge_requests(vhost, DID_REQUEUE);
3414 			ibmvfc_set_host_state(vhost, IBMVFC_LINK_DOWN);
3415 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
3416 			wake_up(&vhost->work_wait_q);
3417 		} else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
3418 			dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
3419 			ibmvfc_purge_requests(vhost, DID_ERROR);
3420 			ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3421 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
3422 		} else {
3423 			dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
3424 		}
3425 		return;
3426 	case IBMVFC_CRQ_CMD_RSP:
3427 		break;
3428 	default:
3429 		dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
3430 		return;
3431 	}
3432 
3433 	if (crq->format == IBMVFC_ASYNC_EVENT)
3434 		return;
3435 
3436 	/* The only kind of payload CRQs we should get are responses to
3437 	 * things we send. Make sure this response is to something we
3438 	 * actually sent
3439 	 */
3440 	if (unlikely(!ibmvfc_valid_event(&vhost->crq.evt_pool, evt))) {
3441 		dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3442 			crq->ioba);
3443 		return;
3444 	}
3445 
3446 	if (unlikely(atomic_dec_if_positive(&evt->active))) {
3447 		dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3448 			crq->ioba);
3449 		return;
3450 	}
3451 
3452 	spin_lock(&evt->queue->l_lock);
3453 	list_move_tail(&evt->queue_list, evt_doneq);
3454 	spin_unlock(&evt->queue->l_lock);
3455 }
3456 
3457 /**
3458  * ibmvfc_scan_finished - Check if the device scan is done.
3459  * @shost:	scsi host struct
3460  * @time:	current elapsed time
3461  *
3462  * Returns:
3463  *	0 if scan is not done / 1 if scan is done
3464  **/
ibmvfc_scan_finished(struct Scsi_Host * shost,unsigned long time)3465 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3466 {
3467 	unsigned long flags;
3468 	struct ibmvfc_host *vhost = shost_priv(shost);
3469 	int done = 0;
3470 
3471 	spin_lock_irqsave(shost->host_lock, flags);
3472 	if (!vhost->scan_timeout)
3473 		done = 1;
3474 	else if (time >= (vhost->scan_timeout * HZ)) {
3475 		dev_info(vhost->dev, "Scan taking longer than %d seconds, "
3476 			 "continuing initialization\n", vhost->scan_timeout);
3477 		done = 1;
3478 	}
3479 
3480 	if (vhost->scan_complete) {
3481 		vhost->scan_timeout = init_timeout;
3482 		done = 1;
3483 	}
3484 	spin_unlock_irqrestore(shost->host_lock, flags);
3485 	return done;
3486 }
3487 
3488 /**
3489  * ibmvfc_sdev_init - Setup the device's task set value
3490  * @sdev:	struct scsi_device device to configure
3491  *
3492  * Set the device's task set value so that error handling works as
3493  * expected.
3494  *
3495  * Returns:
3496  *	0 on success / -ENXIO if device does not exist
3497  **/
ibmvfc_sdev_init(struct scsi_device * sdev)3498 static int ibmvfc_sdev_init(struct scsi_device *sdev)
3499 {
3500 	struct Scsi_Host *shost = sdev->host;
3501 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3502 	struct ibmvfc_host *vhost = shost_priv(shost);
3503 	unsigned long flags = 0;
3504 
3505 	if (!rport || fc_remote_port_chkready(rport))
3506 		return -ENXIO;
3507 
3508 	spin_lock_irqsave(shost->host_lock, flags);
3509 	sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
3510 	spin_unlock_irqrestore(shost->host_lock, flags);
3511 	return 0;
3512 }
3513 
3514 /**
3515  * ibmvfc_target_alloc - Setup the target's task set value
3516  * @starget:	struct scsi_target
3517  *
3518  * Set the target's task set value so that error handling works as
3519  * expected.
3520  *
3521  * Returns:
3522  *	0 on success / -ENXIO if device does not exist
3523  **/
ibmvfc_target_alloc(struct scsi_target * starget)3524 static int ibmvfc_target_alloc(struct scsi_target *starget)
3525 {
3526 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3527 	struct ibmvfc_host *vhost = shost_priv(shost);
3528 	unsigned long flags = 0;
3529 
3530 	spin_lock_irqsave(shost->host_lock, flags);
3531 	starget->hostdata = (void *)(unsigned long)vhost->task_set++;
3532 	spin_unlock_irqrestore(shost->host_lock, flags);
3533 	return 0;
3534 }
3535 
3536 /**
3537  * ibmvfc_sdev_configure - Configure the device
3538  * @sdev:	struct scsi_device device to configure
3539  * @lim:	Request queue limits
3540  *
3541  * Enable allow_restart for a device if it is a disk. Adjust the
3542  * queue_depth here also.
3543  *
3544  * Returns:
3545  *	0
3546  **/
ibmvfc_sdev_configure(struct scsi_device * sdev,struct queue_limits * lim)3547 static int ibmvfc_sdev_configure(struct scsi_device *sdev,
3548 				 struct queue_limits *lim)
3549 {
3550 	struct Scsi_Host *shost = sdev->host;
3551 	unsigned long flags = 0;
3552 
3553 	spin_lock_irqsave(shost->host_lock, flags);
3554 	if (sdev->type == TYPE_DISK) {
3555 		sdev->allow_restart = 1;
3556 		blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
3557 	}
3558 	spin_unlock_irqrestore(shost->host_lock, flags);
3559 	return 0;
3560 }
3561 
3562 /**
3563  * ibmvfc_change_queue_depth - Change the device's queue depth
3564  * @sdev:	scsi device struct
3565  * @qdepth:	depth to set
3566  *
3567  * Return value:
3568  * 	actual depth set
3569  **/
ibmvfc_change_queue_depth(struct scsi_device * sdev,int qdepth)3570 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
3571 {
3572 	if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
3573 		qdepth = IBMVFC_MAX_CMDS_PER_LUN;
3574 
3575 	return scsi_change_queue_depth(sdev, qdepth);
3576 }
3577 
ibmvfc_show_host_partition_name(struct device * dev,struct device_attribute * attr,char * buf)3578 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
3579 						 struct device_attribute *attr, char *buf)
3580 {
3581 	struct Scsi_Host *shost = class_to_shost(dev);
3582 	struct ibmvfc_host *vhost = shost_priv(shost);
3583 
3584 	return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.partition_name);
3585 }
3586 
ibmvfc_show_host_device_name(struct device * dev,struct device_attribute * attr,char * buf)3587 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
3588 					    struct device_attribute *attr, char *buf)
3589 {
3590 	struct Scsi_Host *shost = class_to_shost(dev);
3591 	struct ibmvfc_host *vhost = shost_priv(shost);
3592 
3593 	return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.device_name);
3594 }
3595 
ibmvfc_show_host_loc_code(struct device * dev,struct device_attribute * attr,char * buf)3596 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3597 					 struct device_attribute *attr, char *buf)
3598 {
3599 	struct Scsi_Host *shost = class_to_shost(dev);
3600 	struct ibmvfc_host *vhost = shost_priv(shost);
3601 
3602 	return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.port_loc_code);
3603 }
3604 
ibmvfc_show_host_drc_name(struct device * dev,struct device_attribute * attr,char * buf)3605 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3606 					 struct device_attribute *attr, char *buf)
3607 {
3608 	struct Scsi_Host *shost = class_to_shost(dev);
3609 	struct ibmvfc_host *vhost = shost_priv(shost);
3610 
3611 	return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.drc_name);
3612 }
3613 
ibmvfc_show_host_npiv_version(struct device * dev,struct device_attribute * attr,char * buf)3614 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3615 					     struct device_attribute *attr, char *buf)
3616 {
3617 	struct Scsi_Host *shost = class_to_shost(dev);
3618 	struct ibmvfc_host *vhost = shost_priv(shost);
3619 	return sysfs_emit(buf, "%d\n",
3620 			  be32_to_cpu(vhost->login_buf->resp.version));
3621 }
3622 
ibmvfc_show_host_capabilities(struct device * dev,struct device_attribute * attr,char * buf)3623 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3624 					     struct device_attribute *attr, char *buf)
3625 {
3626 	struct Scsi_Host *shost = class_to_shost(dev);
3627 	struct ibmvfc_host *vhost = shost_priv(shost);
3628 	return sysfs_emit(buf, "%llx\n",
3629 			  be64_to_cpu(vhost->login_buf->resp.capabilities));
3630 }
3631 
3632 /**
3633  * ibmvfc_show_log_level - Show the adapter's error logging level
3634  * @dev:	class device struct
3635  * @attr:	unused
3636  * @buf:	buffer
3637  *
3638  * Return value:
3639  * 	number of bytes printed to buffer
3640  **/
ibmvfc_show_log_level(struct device * dev,struct device_attribute * attr,char * buf)3641 static ssize_t ibmvfc_show_log_level(struct device *dev,
3642 				     struct device_attribute *attr, char *buf)
3643 {
3644 	struct Scsi_Host *shost = class_to_shost(dev);
3645 	struct ibmvfc_host *vhost = shost_priv(shost);
3646 	unsigned long flags = 0;
3647 	int len;
3648 
3649 	spin_lock_irqsave(shost->host_lock, flags);
3650 	len = sysfs_emit(buf, "%d\n", vhost->log_level);
3651 	spin_unlock_irqrestore(shost->host_lock, flags);
3652 	return len;
3653 }
3654 
3655 /**
3656  * ibmvfc_store_log_level - Change the adapter's error logging level
3657  * @dev:	class device struct
3658  * @attr:	unused
3659  * @buf:	buffer
3660  * @count:      buffer size
3661  *
3662  * Return value:
3663  * 	number of bytes printed to buffer
3664  **/
ibmvfc_store_log_level(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3665 static ssize_t ibmvfc_store_log_level(struct device *dev,
3666 				      struct device_attribute *attr,
3667 				      const char *buf, size_t count)
3668 {
3669 	struct Scsi_Host *shost = class_to_shost(dev);
3670 	struct ibmvfc_host *vhost = shost_priv(shost);
3671 	unsigned long flags = 0;
3672 
3673 	spin_lock_irqsave(shost->host_lock, flags);
3674 	vhost->log_level = simple_strtoul(buf, NULL, 10);
3675 	spin_unlock_irqrestore(shost->host_lock, flags);
3676 	return strlen(buf);
3677 }
3678 
ibmvfc_show_scsi_channels(struct device * dev,struct device_attribute * attr,char * buf)3679 static ssize_t ibmvfc_show_scsi_channels(struct device *dev,
3680 					 struct device_attribute *attr, char *buf)
3681 {
3682 	struct Scsi_Host *shost = class_to_shost(dev);
3683 	struct ibmvfc_host *vhost = shost_priv(shost);
3684 	struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
3685 	unsigned long flags = 0;
3686 	int len;
3687 
3688 	spin_lock_irqsave(shost->host_lock, flags);
3689 	len = sysfs_emit(buf, "%d\n", scsi->desired_queues);
3690 	spin_unlock_irqrestore(shost->host_lock, flags);
3691 	return len;
3692 }
3693 
ibmvfc_store_scsi_channels(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3694 static ssize_t ibmvfc_store_scsi_channels(struct device *dev,
3695 					 struct device_attribute *attr,
3696 					 const char *buf, size_t count)
3697 {
3698 	struct Scsi_Host *shost = class_to_shost(dev);
3699 	struct ibmvfc_host *vhost = shost_priv(shost);
3700 	struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
3701 	unsigned long flags = 0;
3702 	unsigned int channels;
3703 
3704 	spin_lock_irqsave(shost->host_lock, flags);
3705 	channels = simple_strtoul(buf, NULL, 10);
3706 	scsi->desired_queues = min(channels, shost->nr_hw_queues);
3707 	ibmvfc_hard_reset_host(vhost);
3708 	spin_unlock_irqrestore(shost->host_lock, flags);
3709 	return strlen(buf);
3710 }
3711 
3712 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3713 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3714 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3715 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3716 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3717 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3718 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3719 		   ibmvfc_show_log_level, ibmvfc_store_log_level);
3720 static DEVICE_ATTR(nr_scsi_channels, S_IRUGO | S_IWUSR,
3721 		   ibmvfc_show_scsi_channels, ibmvfc_store_scsi_channels);
3722 
3723 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3724 /**
3725  * ibmvfc_read_trace - Dump the adapter trace
3726  * @filp:		open sysfs file
3727  * @kobj:		kobject struct
3728  * @bin_attr:	bin_attribute struct
3729  * @buf:		buffer
3730  * @off:		offset
3731  * @count:		buffer size
3732  *
3733  * Return value:
3734  *	number of bytes printed to buffer
3735  **/
ibmvfc_read_trace(struct file * filp,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)3736 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3737 				 const struct bin_attribute *bin_attr,
3738 				 char *buf, loff_t off, size_t count)
3739 {
3740 	struct device *dev = kobj_to_dev(kobj);
3741 	struct Scsi_Host *shost = class_to_shost(dev);
3742 	struct ibmvfc_host *vhost = shost_priv(shost);
3743 	unsigned long flags = 0;
3744 	int size = IBMVFC_TRACE_SIZE;
3745 	char *src = (char *)vhost->trace;
3746 
3747 	if (off > size)
3748 		return 0;
3749 	if (off + count > size) {
3750 		size -= off;
3751 		count = size;
3752 	}
3753 
3754 	spin_lock_irqsave(shost->host_lock, flags);
3755 	memcpy(buf, &src[off], count);
3756 	spin_unlock_irqrestore(shost->host_lock, flags);
3757 	return count;
3758 }
3759 
3760 static const struct bin_attribute ibmvfc_trace_attr = {
3761 	.attr =	{
3762 		.name = "trace",
3763 		.mode = S_IRUGO,
3764 	},
3765 	.size = 0,
3766 	.read = ibmvfc_read_trace,
3767 };
3768 #endif
3769 
3770 static struct attribute *ibmvfc_host_attrs[] = {
3771 	&dev_attr_partition_name.attr,
3772 	&dev_attr_device_name.attr,
3773 	&dev_attr_port_loc_code.attr,
3774 	&dev_attr_drc_name.attr,
3775 	&dev_attr_npiv_version.attr,
3776 	&dev_attr_capabilities.attr,
3777 	&dev_attr_log_level.attr,
3778 	&dev_attr_nr_scsi_channels.attr,
3779 	NULL
3780 };
3781 
3782 ATTRIBUTE_GROUPS(ibmvfc_host);
3783 
3784 static const struct scsi_host_template driver_template = {
3785 	.module = THIS_MODULE,
3786 	.name = "IBM POWER Virtual FC Adapter",
3787 	.proc_name = IBMVFC_NAME,
3788 	.queuecommand = ibmvfc_queuecommand,
3789 	.eh_timed_out = fc_eh_timed_out,
3790 	.eh_abort_handler = ibmvfc_eh_abort_handler,
3791 	.eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3792 	.eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3793 	.eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3794 	.sdev_init = ibmvfc_sdev_init,
3795 	.sdev_configure = ibmvfc_sdev_configure,
3796 	.target_alloc = ibmvfc_target_alloc,
3797 	.scan_finished = ibmvfc_scan_finished,
3798 	.change_queue_depth = ibmvfc_change_queue_depth,
3799 	.cmd_per_lun = 16,
3800 	.can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3801 	.this_id = -1,
3802 	.sg_tablesize = SG_ALL,
3803 	.max_sectors = IBMVFC_MAX_SECTORS,
3804 	.shost_groups = ibmvfc_host_groups,
3805 	.track_queue_depth = 1,
3806 };
3807 
3808 /**
3809  * ibmvfc_next_async_crq - Returns the next entry in async queue
3810  * @vhost:	ibmvfc host struct
3811  *
3812  * Returns:
3813  *	Pointer to next entry in queue / NULL if empty
3814  **/
ibmvfc_next_async_crq(struct ibmvfc_host * vhost)3815 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3816 {
3817 	struct ibmvfc_queue *async_crq = &vhost->async_crq;
3818 	struct ibmvfc_async_crq *crq;
3819 
3820 	crq = &async_crq->msgs.async[async_crq->cur];
3821 	if (crq->valid & 0x80) {
3822 		if (++async_crq->cur == async_crq->size)
3823 			async_crq->cur = 0;
3824 		rmb();
3825 	} else
3826 		crq = NULL;
3827 
3828 	return crq;
3829 }
3830 
3831 /**
3832  * ibmvfc_next_crq - Returns the next entry in message queue
3833  * @vhost:	ibmvfc host struct
3834  *
3835  * Returns:
3836  *	Pointer to next entry in queue / NULL if empty
3837  **/
ibmvfc_next_crq(struct ibmvfc_host * vhost)3838 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3839 {
3840 	struct ibmvfc_queue *queue = &vhost->crq;
3841 	struct ibmvfc_crq *crq;
3842 
3843 	crq = &queue->msgs.crq[queue->cur];
3844 	if (crq->valid & 0x80) {
3845 		if (++queue->cur == queue->size)
3846 			queue->cur = 0;
3847 		rmb();
3848 	} else
3849 		crq = NULL;
3850 
3851 	return crq;
3852 }
3853 
3854 /**
3855  * ibmvfc_interrupt - Interrupt handler
3856  * @irq:		number of irq to handle, not used
3857  * @dev_instance: ibmvfc_host that received interrupt
3858  *
3859  * Returns:
3860  *	IRQ_HANDLED
3861  **/
ibmvfc_interrupt(int irq,void * dev_instance)3862 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3863 {
3864 	struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3865 	unsigned long flags;
3866 
3867 	spin_lock_irqsave(vhost->host->host_lock, flags);
3868 	vio_disable_interrupts(to_vio_dev(vhost->dev));
3869 	tasklet_schedule(&vhost->tasklet);
3870 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3871 	return IRQ_HANDLED;
3872 }
3873 
3874 /**
3875  * ibmvfc_tasklet - Interrupt handler tasklet
3876  * @data:		ibmvfc host struct
3877  *
3878  * Returns:
3879  *	Nothing
3880  **/
ibmvfc_tasklet(void * data)3881 static void ibmvfc_tasklet(void *data)
3882 {
3883 	struct ibmvfc_host *vhost = data;
3884 	struct vio_dev *vdev = to_vio_dev(vhost->dev);
3885 	struct ibmvfc_crq *crq;
3886 	struct ibmvfc_async_crq *async;
3887 	struct ibmvfc_event *evt, *temp;
3888 	unsigned long flags;
3889 	int done = 0;
3890 	LIST_HEAD(evt_doneq);
3891 
3892 	spin_lock_irqsave(vhost->host->host_lock, flags);
3893 	spin_lock(vhost->crq.q_lock);
3894 	while (!done) {
3895 		/* Pull all the valid messages off the async CRQ */
3896 		while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3897 			ibmvfc_handle_async(async, vhost);
3898 			async->valid = 0;
3899 			wmb();
3900 		}
3901 
3902 		/* Pull all the valid messages off the CRQ */
3903 		while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3904 			ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3905 			crq->valid = 0;
3906 			wmb();
3907 		}
3908 
3909 		vio_enable_interrupts(vdev);
3910 		if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3911 			vio_disable_interrupts(vdev);
3912 			ibmvfc_handle_async(async, vhost);
3913 			async->valid = 0;
3914 			wmb();
3915 		} else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3916 			vio_disable_interrupts(vdev);
3917 			ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3918 			crq->valid = 0;
3919 			wmb();
3920 		} else
3921 			done = 1;
3922 	}
3923 
3924 	spin_unlock(vhost->crq.q_lock);
3925 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3926 
3927 	list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3928 		timer_delete(&evt->timer);
3929 		list_del(&evt->queue_list);
3930 		ibmvfc_trc_end(evt);
3931 		evt->done(evt);
3932 	}
3933 }
3934 
ibmvfc_toggle_scrq_irq(struct ibmvfc_queue * scrq,int enable)3935 static int ibmvfc_toggle_scrq_irq(struct ibmvfc_queue *scrq, int enable)
3936 {
3937 	struct device *dev = scrq->vhost->dev;
3938 	struct vio_dev *vdev = to_vio_dev(dev);
3939 	unsigned long rc;
3940 	int irq_action = H_ENABLE_VIO_INTERRUPT;
3941 
3942 	if (!enable)
3943 		irq_action = H_DISABLE_VIO_INTERRUPT;
3944 
3945 	rc = plpar_hcall_norets(H_VIOCTL, vdev->unit_address, irq_action,
3946 				scrq->hw_irq, 0, 0);
3947 
3948 	if (rc)
3949 		dev_err(dev, "Couldn't %s sub-crq[%lu] irq. rc=%ld\n",
3950 			enable ? "enable" : "disable", scrq->hwq_id, rc);
3951 
3952 	return rc;
3953 }
3954 
ibmvfc_handle_scrq(struct ibmvfc_crq * crq,struct ibmvfc_host * vhost,struct list_head * evt_doneq)3955 static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3956 			       struct list_head *evt_doneq)
3957 {
3958 	struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3959 
3960 	switch (crq->valid) {
3961 	case IBMVFC_CRQ_CMD_RSP:
3962 		break;
3963 	case IBMVFC_CRQ_XPORT_EVENT:
3964 		return;
3965 	default:
3966 		dev_err(vhost->dev, "Got and invalid message type 0x%02x\n", crq->valid);
3967 		return;
3968 	}
3969 
3970 	/* The only kind of payload CRQs we should get are responses to
3971 	 * things we send. Make sure this response is to something we
3972 	 * actually sent
3973 	 */
3974 	if (unlikely(!ibmvfc_valid_event(&evt->queue->evt_pool, evt))) {
3975 		dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3976 			crq->ioba);
3977 		return;
3978 	}
3979 
3980 	if (unlikely(atomic_dec_if_positive(&evt->active))) {
3981 		dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3982 			crq->ioba);
3983 		return;
3984 	}
3985 
3986 	spin_lock(&evt->queue->l_lock);
3987 	list_move_tail(&evt->queue_list, evt_doneq);
3988 	spin_unlock(&evt->queue->l_lock);
3989 }
3990 
ibmvfc_next_scrq(struct ibmvfc_queue * scrq)3991 static struct ibmvfc_crq *ibmvfc_next_scrq(struct ibmvfc_queue *scrq)
3992 {
3993 	struct ibmvfc_crq *crq;
3994 
3995 	crq = &scrq->msgs.scrq[scrq->cur].crq;
3996 	if (crq->valid & 0x80) {
3997 		if (++scrq->cur == scrq->size)
3998 			scrq->cur = 0;
3999 		rmb();
4000 	} else
4001 		crq = NULL;
4002 
4003 	return crq;
4004 }
4005 
ibmvfc_drain_sub_crq(struct ibmvfc_queue * scrq)4006 static void ibmvfc_drain_sub_crq(struct ibmvfc_queue *scrq)
4007 {
4008 	struct ibmvfc_crq *crq;
4009 	struct ibmvfc_event *evt, *temp;
4010 	unsigned long flags;
4011 	int done = 0;
4012 	LIST_HEAD(evt_doneq);
4013 
4014 	spin_lock_irqsave(scrq->q_lock, flags);
4015 	while (!done) {
4016 		while ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
4017 			ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
4018 			crq->valid = 0;
4019 			wmb();
4020 		}
4021 
4022 		ibmvfc_toggle_scrq_irq(scrq, 1);
4023 		if ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
4024 			ibmvfc_toggle_scrq_irq(scrq, 0);
4025 			ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
4026 			crq->valid = 0;
4027 			wmb();
4028 		} else
4029 			done = 1;
4030 	}
4031 	spin_unlock_irqrestore(scrq->q_lock, flags);
4032 
4033 	list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
4034 		timer_delete(&evt->timer);
4035 		list_del(&evt->queue_list);
4036 		ibmvfc_trc_end(evt);
4037 		evt->done(evt);
4038 	}
4039 }
4040 
ibmvfc_interrupt_mq(int irq,void * scrq_instance)4041 static irqreturn_t ibmvfc_interrupt_mq(int irq, void *scrq_instance)
4042 {
4043 	struct ibmvfc_queue *scrq = (struct ibmvfc_queue *)scrq_instance;
4044 
4045 	ibmvfc_toggle_scrq_irq(scrq, 0);
4046 	ibmvfc_drain_sub_crq(scrq);
4047 
4048 	return IRQ_HANDLED;
4049 }
4050 
4051 /**
4052  * ibmvfc_init_tgt - Set the next init job step for the target
4053  * @tgt:		ibmvfc target struct
4054  * @job_step:	job step to perform
4055  *
4056  **/
ibmvfc_init_tgt(struct ibmvfc_target * tgt,void (* job_step)(struct ibmvfc_target *))4057 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
4058 			    void (*job_step) (struct ibmvfc_target *))
4059 {
4060 	if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
4061 		tgt->job_step = job_step;
4062 	wake_up(&tgt->vhost->work_wait_q);
4063 }
4064 
4065 /**
4066  * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
4067  * @tgt:		ibmvfc target struct
4068  * @job_step:	initialization job step
4069  *
4070  * Returns: 1 if step will be retried / 0 if not
4071  *
4072  **/
ibmvfc_retry_tgt_init(struct ibmvfc_target * tgt,void (* job_step)(struct ibmvfc_target *))4073 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
4074 				  void (*job_step) (struct ibmvfc_target *))
4075 {
4076 	if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
4077 		ibmvfc_del_tgt(tgt);
4078 		wake_up(&tgt->vhost->work_wait_q);
4079 		return 0;
4080 	} else
4081 		ibmvfc_init_tgt(tgt, job_step);
4082 	return 1;
4083 }
4084 
4085 /* Defined in FC-LS */
4086 static const struct {
4087 	int code;
4088 	int retry;
4089 	int logged_in;
4090 } prli_rsp [] = {
4091 	{ 0, 1, 0 },
4092 	{ 1, 0, 1 },
4093 	{ 2, 1, 0 },
4094 	{ 3, 1, 0 },
4095 	{ 4, 0, 0 },
4096 	{ 5, 0, 0 },
4097 	{ 6, 0, 1 },
4098 	{ 7, 0, 0 },
4099 	{ 8, 1, 0 },
4100 };
4101 
4102 /**
4103  * ibmvfc_get_prli_rsp - Find PRLI response index
4104  * @flags:	PRLI response flags
4105  *
4106  **/
ibmvfc_get_prli_rsp(u16 flags)4107 static int ibmvfc_get_prli_rsp(u16 flags)
4108 {
4109 	int i;
4110 	int code = (flags & 0x0f00) >> 8;
4111 
4112 	for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
4113 		if (prli_rsp[i].code == code)
4114 			return i;
4115 
4116 	return 0;
4117 }
4118 
4119 /**
4120  * ibmvfc_tgt_prli_done - Completion handler for Process Login
4121  * @evt:	ibmvfc event struct
4122  *
4123  **/
ibmvfc_tgt_prli_done(struct ibmvfc_event * evt)4124 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
4125 {
4126 	struct ibmvfc_target *tgt = evt->tgt;
4127 	struct ibmvfc_host *vhost = evt->vhost;
4128 	struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
4129 	struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
4130 	u32 status = be16_to_cpu(rsp->common.status);
4131 	int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
4132 
4133 	vhost->discovery_threads--;
4134 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4135 	switch (status) {
4136 	case IBMVFC_MAD_SUCCESS:
4137 		tgt_dbg(tgt, "%s Process Login succeeded: %X %02X %04X\n",
4138 			proto_type[tgt->protocol], parms->type, parms->flags,
4139 			parms->service_parms);
4140 
4141 		index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
4142 		if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
4143 			if (prli_rsp[index].logged_in) {
4144 				if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
4145 					tgt->need_login = 0;
4146 					tgt->ids.roles = 0;
4147 					if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
4148 						tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
4149 					if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
4150 						tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4151 					tgt->add_rport = 1;
4152 				} else
4153 					ibmvfc_del_tgt(tgt);
4154 			} else if (prli_rsp[index].retry)
4155 				ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4156 			else
4157 				ibmvfc_del_tgt(tgt);
4158 		} else if (parms->type == IBMVFC_NVME_FCP_TYPE) {
4159 			if (prli_rsp[index].logged_in) {
4160 				/* For FC-NVMe PRLI the Image Pair field is always set to zero (see 6.3.3) */
4161 				tgt->need_login = 0;
4162 				tgt->ids.roles = 0;
4163 				if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_NVME_TARGET)
4164 					tgt->ids.roles |= FC_PORT_ROLE_NVME_TARGET;
4165 				if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_NVME_INITIATOR)
4166 					tgt->ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
4167 				if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_NVME_DISCOVERY)
4168 					tgt->ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
4169 				tgt->add_rport = 1;
4170 			} else if (prli_rsp[index].retry)
4171 				ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4172 			else
4173 				ibmvfc_del_tgt(tgt);
4174 		} else
4175 			ibmvfc_del_tgt(tgt);
4176 		break;
4177 	case IBMVFC_MAD_DRIVER_FAILED:
4178 		break;
4179 	case IBMVFC_MAD_CRQ_ERROR:
4180 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4181 		break;
4182 	case IBMVFC_MAD_FAILED:
4183 	default:
4184 		if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
4185 		     be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
4186 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4187 		else if (tgt->logo_rcvd)
4188 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4189 		else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4190 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4191 		else
4192 			ibmvfc_del_tgt(tgt);
4193 
4194 		tgt_log(tgt, level, "%s Process Login failed: %s (%x:%x) rc=0x%02X\n",
4195 			proto_type[tgt->protocol], ibmvfc_get_cmd_error(be16_to_cpu(rsp->status),
4196 			be16_to_cpu(rsp->error)), be16_to_cpu(rsp->status),
4197 			be16_to_cpu(rsp->error), status);
4198 		break;
4199 	}
4200 
4201 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4202 	ibmvfc_free_event(evt);
4203 	wake_up(&vhost->work_wait_q);
4204 }
4205 
4206 /**
4207  * ibmvfc_tgt_send_prli - Send a process login
4208  * @tgt:	ibmvfc target struct
4209  *
4210  **/
ibmvfc_tgt_send_prli(struct ibmvfc_target * tgt)4211 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
4212 {
4213 	struct ibmvfc_process_login *prli;
4214 	struct ibmvfc_host *vhost = tgt->vhost;
4215 	struct ibmvfc_event *evt;
4216 
4217 	if (vhost->discovery_threads >= disc_threads)
4218 		return;
4219 
4220 	kref_get(&tgt->kref);
4221 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4222 	if (!evt) {
4223 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4224 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4225 		__ibmvfc_reset_host(vhost);
4226 		return;
4227 	}
4228 	vhost->discovery_threads++;
4229 	ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
4230 	evt->tgt = tgt;
4231 	prli = &evt->iu.prli;
4232 	memset(prli, 0, sizeof(*prli));
4233 	if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4234 		prli->common.version = cpu_to_be32(2);
4235 		prli->target_wwpn = cpu_to_be64(tgt->wwpn);
4236 	} else {
4237 		prli->common.version = cpu_to_be32(1);
4238 	}
4239 	if (tgt->protocol == IBMVFC_PROTO_SCSI) {
4240 		prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
4241 		prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
4242 		prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
4243 		prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
4244 		prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
4245 
4246 		if (cls3_error)
4247 			prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
4248 	} else {
4249 		prli->common.opcode = cpu_to_be32(IBMVFC_NVMF_PROCESS_LOGIN);
4250 		prli->parms.type = IBMVFC_NVME_FCP_TYPE;
4251 		prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_NVME_INITIATOR);
4252 	}
4253 	prli->common.length = cpu_to_be16(sizeof(*prli));
4254 	prli->scsi_id = cpu_to_be64(tgt->scsi_id);
4255 
4256 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4257 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4258 		vhost->discovery_threads--;
4259 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4260 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4261 	} else
4262 		tgt_dbg(tgt, "%s Sent process login\n", proto_type[tgt->protocol]);
4263 }
4264 
4265 /**
4266  * ibmvfc_tgt_plogi_done - Completion handler for Port Login
4267  * @evt:	ibmvfc event struct
4268  *
4269  **/
ibmvfc_tgt_plogi_done(struct ibmvfc_event * evt)4270 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
4271 {
4272 	struct ibmvfc_target *tgt = evt->tgt;
4273 	struct ibmvfc_host *vhost = evt->vhost;
4274 	struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
4275 	u32 status = be16_to_cpu(rsp->common.status);
4276 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4277 
4278 	vhost->discovery_threads--;
4279 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4280 	switch (status) {
4281 	case IBMVFC_MAD_SUCCESS:
4282 		tgt_dbg(tgt, "%s Port Login succeeded\n", proto_type[tgt->protocol]);
4283 		if (tgt->ids.port_name &&
4284 		    tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
4285 			vhost->reinit = 1;
4286 			tgt_dbg(tgt, "Port re-init required\n");
4287 			break;
4288 		}
4289 		tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4290 		tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4291 		tgt->ids.port_id = tgt->scsi_id;
4292 		memcpy(&tgt->service_parms, &rsp->service_parms,
4293 		       sizeof(tgt->service_parms));
4294 		memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4295 		       sizeof(tgt->service_parms_change));
4296 		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4297 		break;
4298 	case IBMVFC_MAD_DRIVER_FAILED:
4299 		break;
4300 	case IBMVFC_MAD_CRQ_ERROR:
4301 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4302 		break;
4303 	case IBMVFC_MAD_FAILED:
4304 	default:
4305 		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4306 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4307 		else
4308 			ibmvfc_del_tgt(tgt);
4309 
4310 		tgt_log(tgt, level, "%s Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4311 			proto_type[tgt->protocol], ibmvfc_get_cmd_error(be16_to_cpu(rsp->status),
4312 			be16_to_cpu(rsp->error)), be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4313 			ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4314 			ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
4315 		break;
4316 	}
4317 
4318 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4319 	ibmvfc_free_event(evt);
4320 	wake_up(&vhost->work_wait_q);
4321 }
4322 
4323 /**
4324  * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
4325  * @tgt:	ibmvfc target struct
4326  *
4327  **/
ibmvfc_tgt_send_plogi(struct ibmvfc_target * tgt)4328 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
4329 {
4330 	struct ibmvfc_port_login *plogi;
4331 	struct ibmvfc_host *vhost = tgt->vhost;
4332 	struct ibmvfc_event *evt;
4333 
4334 	if (vhost->discovery_threads >= disc_threads)
4335 		return;
4336 
4337 	kref_get(&tgt->kref);
4338 	tgt->logo_rcvd = 0;
4339 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4340 	if (!evt) {
4341 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4342 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4343 		__ibmvfc_reset_host(vhost);
4344 		return;
4345 	}
4346 	vhost->discovery_threads++;
4347 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4348 	ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
4349 	evt->tgt = tgt;
4350 	plogi = &evt->iu.plogi;
4351 	memset(plogi, 0, sizeof(*plogi));
4352 	if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4353 		plogi->common.version = cpu_to_be32(2);
4354 		plogi->target_wwpn = cpu_to_be64(tgt->wwpn);
4355 	} else {
4356 		plogi->common.version = cpu_to_be32(1);
4357 	}
4358 	if (tgt->protocol == IBMVFC_PROTO_SCSI)
4359 		plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
4360 	else
4361 		plogi->common.opcode = cpu_to_be32(IBMVFC_NVMF_PORT_LOGIN);
4362 	plogi->common.length = cpu_to_be16(sizeof(*plogi));
4363 	plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
4364 
4365 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4366 		vhost->discovery_threads--;
4367 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4368 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4369 	} else
4370 		tgt_dbg(tgt, "Sent %s port login\n", proto_type[tgt->protocol]);
4371 }
4372 
4373 /**
4374  * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
4375  * @evt:	ibmvfc event struct
4376  *
4377  **/
ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event * evt)4378 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
4379 {
4380 	struct ibmvfc_target *tgt = evt->tgt;
4381 	struct ibmvfc_host *vhost = evt->vhost;
4382 	struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
4383 	u32 status = be16_to_cpu(rsp->common.status);
4384 
4385 	vhost->discovery_threads--;
4386 	ibmvfc_free_event(evt);
4387 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4388 
4389 	switch (status) {
4390 	case IBMVFC_MAD_SUCCESS:
4391 		tgt_dbg(tgt, "%s Implicit Logout succeeded\n", proto_type[tgt->protocol]);
4392 		break;
4393 	case IBMVFC_MAD_DRIVER_FAILED:
4394 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4395 		wake_up(&vhost->work_wait_q);
4396 		return;
4397 	case IBMVFC_MAD_FAILED:
4398 	default:
4399 		tgt_err(tgt, "%s Implicit Logout failed: rc=0x%02X\n",
4400 			proto_type[tgt->protocol], status);
4401 		break;
4402 	}
4403 
4404 	ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
4405 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4406 	wake_up(&vhost->work_wait_q);
4407 }
4408 
4409 /**
4410  * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
4411  * @tgt:		ibmvfc target struct
4412  * @done:		Routine to call when the event is responded to
4413  *
4414  * Returns:
4415  *	Allocated and initialized ibmvfc_event struct
4416  **/
__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target * tgt,void (* done)(struct ibmvfc_event *))4417 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
4418 								 void (*done) (struct ibmvfc_event *))
4419 {
4420 	struct ibmvfc_implicit_logout *mad;
4421 	struct ibmvfc_host *vhost = tgt->vhost;
4422 	struct ibmvfc_event *evt;
4423 
4424 	kref_get(&tgt->kref);
4425 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4426 	if (!evt)
4427 		return NULL;
4428 	ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
4429 	evt->tgt = tgt;
4430 	mad = &evt->iu.implicit_logout;
4431 	memset(mad, 0, sizeof(*mad));
4432 	mad->common.version = cpu_to_be32(1);
4433 	if (tgt->protocol == IBMVFC_PROTO_SCSI)
4434 		mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
4435 	else
4436 		mad->common.opcode = cpu_to_be32(IBMVFC_NVMF_IMPLICIT_LOGOUT);
4437 	mad->common.length = cpu_to_be16(sizeof(*mad));
4438 	mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4439 	return evt;
4440 }
4441 
4442 /**
4443  * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
4444  * @tgt:		ibmvfc target struct
4445  *
4446  **/
ibmvfc_tgt_implicit_logout(struct ibmvfc_target * tgt)4447 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
4448 {
4449 	struct ibmvfc_host *vhost = tgt->vhost;
4450 	struct ibmvfc_event *evt;
4451 
4452 	if (vhost->discovery_threads >= disc_threads)
4453 		return;
4454 
4455 	vhost->discovery_threads++;
4456 	evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4457 						   ibmvfc_tgt_implicit_logout_done);
4458 	if (!evt) {
4459 		vhost->discovery_threads--;
4460 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4461 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4462 		__ibmvfc_reset_host(vhost);
4463 		return;
4464 	}
4465 
4466 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4467 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4468 		vhost->discovery_threads--;
4469 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4470 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4471 	} else
4472 		tgt_dbg(tgt, "%s Sent Implicit Logout\n", proto_type[tgt->protocol]);
4473 }
4474 
4475 /**
4476  * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
4477  * @evt:	ibmvfc event struct
4478  *
4479  **/
ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event * evt)4480 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
4481 {
4482 	struct ibmvfc_target *tgt = evt->tgt;
4483 	struct ibmvfc_host *vhost = evt->vhost;
4484 	struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4485 	u32 status = be16_to_cpu(mad->common.status);
4486 
4487 	vhost->discovery_threads--;
4488 	ibmvfc_free_event(evt);
4489 
4490 	/*
4491 	 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
4492 	 * driver in which case we need to free up all the targets. If we are
4493 	 * not unloading, we will still go through a hard reset to get out of
4494 	 * offline state, so there is no need to track the old targets in that
4495 	 * case.
4496 	 */
4497 	if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
4498 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4499 	else
4500 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
4501 
4502 	tgt_dbg(tgt, "%s Implicit Logout %s\n", proto_type[tgt->protocol],
4503 		(status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
4504 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4505 	wake_up(&vhost->work_wait_q);
4506 }
4507 
4508 /**
4509  * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
4510  * @tgt:		ibmvfc target struct
4511  *
4512  **/
ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target * tgt)4513 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
4514 {
4515 	struct ibmvfc_host *vhost = tgt->vhost;
4516 	struct ibmvfc_event *evt;
4517 
4518 	if (!vhost->logged_in) {
4519 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4520 		return;
4521 	}
4522 
4523 	if (vhost->discovery_threads >= disc_threads)
4524 		return;
4525 
4526 	vhost->discovery_threads++;
4527 	evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4528 						   ibmvfc_tgt_implicit_logout_and_del_done);
4529 
4530 	if (!evt) {
4531 		vhost->discovery_threads--;
4532 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4533 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4534 		__ibmvfc_reset_host(vhost);
4535 		return;
4536 	}
4537 
4538 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
4539 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4540 		vhost->discovery_threads--;
4541 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4542 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4543 	} else
4544 		tgt_dbg(tgt, "%s Sent Implicit Logout\n", proto_type[tgt->protocol]);
4545 }
4546 
4547 /**
4548  * ibmvfc_tgt_move_login_done - Completion handler for Move Login
4549  * @evt:	ibmvfc event struct
4550  *
4551  **/
ibmvfc_tgt_move_login_done(struct ibmvfc_event * evt)4552 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
4553 {
4554 	struct ibmvfc_target *tgt = evt->tgt;
4555 	struct ibmvfc_host *vhost = evt->vhost;
4556 	struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
4557 	u32 status = be16_to_cpu(rsp->common.status);
4558 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4559 
4560 	vhost->discovery_threads--;
4561 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4562 	switch (status) {
4563 	case IBMVFC_MAD_SUCCESS:
4564 		tgt_dbg(tgt, "%s Move Login succeeded for new scsi_id: %llX\n",
4565 			proto_type[tgt->protocol], tgt->new_scsi_id);
4566 		tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4567 		tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4568 		tgt->scsi_id = tgt->new_scsi_id;
4569 		tgt->ids.port_id = tgt->scsi_id;
4570 		memcpy(&tgt->service_parms, &rsp->service_parms,
4571 		       sizeof(tgt->service_parms));
4572 		memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4573 		       sizeof(tgt->service_parms_change));
4574 		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4575 		break;
4576 	case IBMVFC_MAD_DRIVER_FAILED:
4577 		break;
4578 	case IBMVFC_MAD_CRQ_ERROR:
4579 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4580 		break;
4581 	case IBMVFC_MAD_FAILED:
4582 	default:
4583 		level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4584 
4585 		tgt_log(tgt, level,
4586 			"%s Move Login failed: new scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
4587 			proto_type[tgt->protocol], tgt->new_scsi_id,
4588 			be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
4589 			status);
4590 		break;
4591 	}
4592 
4593 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4594 	ibmvfc_free_event(evt);
4595 	wake_up(&vhost->work_wait_q);
4596 }
4597 
4598 
4599 /**
4600  * ibmvfc_tgt_move_login - Initiate a move login for specified target
4601  * @tgt:		ibmvfc target struct
4602  *
4603  **/
ibmvfc_tgt_move_login(struct ibmvfc_target * tgt)4604 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
4605 {
4606 	struct ibmvfc_host *vhost = tgt->vhost;
4607 	struct ibmvfc_move_login *move;
4608 	struct ibmvfc_event *evt;
4609 
4610 	if (vhost->discovery_threads >= disc_threads)
4611 		return;
4612 
4613 	kref_get(&tgt->kref);
4614 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4615 	if (!evt) {
4616 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4617 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4618 		__ibmvfc_reset_host(vhost);
4619 		return;
4620 	}
4621 	vhost->discovery_threads++;
4622 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4623 	ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
4624 	evt->tgt = tgt;
4625 	move = &evt->iu.move_login;
4626 	memset(move, 0, sizeof(*move));
4627 	move->common.version = cpu_to_be32(1);
4628 	if (tgt->protocol == IBMVFC_PROTO_SCSI)
4629 		move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
4630 	else
4631 		move->common.opcode = cpu_to_be32(IBMVFC_NVMF_MOVE_LOGIN);
4632 	move->common.length = cpu_to_be16(sizeof(*move));
4633 
4634 	move->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4635 	move->new_scsi_id = cpu_to_be64(tgt->new_scsi_id);
4636 	move->wwpn = cpu_to_be64(tgt->wwpn);
4637 	move->node_name = cpu_to_be64(tgt->ids.node_name);
4638 
4639 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4640 		vhost->discovery_threads--;
4641 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4642 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4643 	} else
4644 		tgt_dbg(tgt, "Sent %s Move Login for new scsi_id: %llX\n",
4645 			proto_type[tgt->protocol], tgt->new_scsi_id);
4646 }
4647 
4648 /**
4649  * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
4650  * @mad:	ibmvfc passthru mad struct
4651  * @tgt:	ibmvfc target struct
4652  *
4653  * Returns:
4654  *	1 if PLOGI needed / 0 if PLOGI not needed
4655  **/
ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad * mad,struct ibmvfc_target * tgt)4656 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
4657 				    struct ibmvfc_target *tgt)
4658 {
4659 	if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
4660 		return 1;
4661 	if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
4662 		return 1;
4663 	if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
4664 		return 1;
4665 	return 0;
4666 }
4667 
4668 /**
4669  * ibmvfc_tgt_adisc_done - Completion handler for ADISC
4670  * @evt:	ibmvfc event struct
4671  *
4672  **/
ibmvfc_tgt_adisc_done(struct ibmvfc_event * evt)4673 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
4674 {
4675 	struct ibmvfc_target *tgt = evt->tgt;
4676 	struct ibmvfc_host *vhost = evt->vhost;
4677 	struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4678 	u32 status = be16_to_cpu(mad->common.status);
4679 	u8 fc_reason, fc_explain;
4680 
4681 	vhost->discovery_threads--;
4682 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4683 	timer_delete(&tgt->timer);
4684 
4685 	switch (status) {
4686 	case IBMVFC_MAD_SUCCESS:
4687 		tgt_dbg(tgt, "ADISC succeeded\n");
4688 		if (ibmvfc_adisc_needs_plogi(mad, tgt))
4689 			ibmvfc_del_tgt(tgt);
4690 		break;
4691 	case IBMVFC_MAD_DRIVER_FAILED:
4692 		break;
4693 	case IBMVFC_MAD_FAILED:
4694 	default:
4695 		ibmvfc_del_tgt(tgt);
4696 		fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
4697 		fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
4698 		tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4699 			 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
4700 			 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
4701 			 ibmvfc_get_fc_type(fc_reason), fc_reason,
4702 			 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
4703 		break;
4704 	}
4705 
4706 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4707 	ibmvfc_free_event(evt);
4708 	wake_up(&vhost->work_wait_q);
4709 }
4710 
4711 /**
4712  * ibmvfc_init_passthru - Initialize an event struct for FC passthru
4713  * @evt:		ibmvfc event struct
4714  *
4715  **/
ibmvfc_init_passthru(struct ibmvfc_event * evt)4716 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
4717 {
4718 	struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
4719 
4720 	memset(mad, 0, sizeof(*mad));
4721 	mad->common.version = cpu_to_be32(1);
4722 	mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
4723 	mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
4724 	mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4725 		offsetof(struct ibmvfc_passthru_mad, iu));
4726 	mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
4727 	mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4728 	mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
4729 	mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4730 		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4731 		offsetof(struct ibmvfc_passthru_fc_iu, payload));
4732 	mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4733 	mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4734 		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4735 		offsetof(struct ibmvfc_passthru_fc_iu, response));
4736 	mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
4737 }
4738 
4739 /**
4740  * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
4741  * @evt:		ibmvfc event struct
4742  *
4743  * Just cleanup this event struct. Everything else is handled by
4744  * the ADISC completion handler. If the ADISC never actually comes
4745  * back, we still have the timer running on the ADISC event struct
4746  * which will fire and cause the CRQ to get reset.
4747  *
4748  **/
ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event * evt)4749 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
4750 {
4751 	struct ibmvfc_host *vhost = evt->vhost;
4752 	struct ibmvfc_target *tgt = evt->tgt;
4753 
4754 	tgt_dbg(tgt, "ADISC cancel complete\n");
4755 	vhost->abort_threads--;
4756 	ibmvfc_free_event(evt);
4757 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4758 	wake_up(&vhost->work_wait_q);
4759 }
4760 
4761 /**
4762  * ibmvfc_adisc_timeout - Handle an ADISC timeout
4763  * @t:		ibmvfc target struct
4764  *
4765  * If an ADISC times out, send a cancel. If the cancel times
4766  * out, reset the CRQ. When the ADISC comes back as cancelled,
4767  * log back into the target.
4768  **/
ibmvfc_adisc_timeout(struct timer_list * t)4769 static void ibmvfc_adisc_timeout(struct timer_list *t)
4770 {
4771 	struct ibmvfc_target *tgt = timer_container_of(tgt, t, timer);
4772 	struct ibmvfc_host *vhost = tgt->vhost;
4773 	struct ibmvfc_event *evt;
4774 	struct ibmvfc_tmf *tmf;
4775 	unsigned long flags;
4776 	int rc;
4777 
4778 	tgt_dbg(tgt, "ADISC timeout\n");
4779 	spin_lock_irqsave(vhost->host->host_lock, flags);
4780 	if (vhost->abort_threads >= disc_threads ||
4781 	    tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
4782 	    vhost->state != IBMVFC_INITIALIZING ||
4783 	    vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
4784 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4785 		return;
4786 	}
4787 
4788 	vhost->abort_threads++;
4789 	kref_get(&tgt->kref);
4790 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4791 	if (!evt) {
4792 		tgt_err(tgt, "Failed to get cancel event for ADISC.\n");
4793 		vhost->abort_threads--;
4794 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4795 		__ibmvfc_reset_host(vhost);
4796 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4797 		return;
4798 	}
4799 	ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
4800 
4801 	evt->tgt = tgt;
4802 	tmf = &evt->iu.tmf;
4803 	memset(tmf, 0, sizeof(*tmf));
4804 	if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4805 		tmf->common.version = cpu_to_be32(2);
4806 		tmf->target_wwpn = cpu_to_be64(tgt->wwpn);
4807 	} else {
4808 		tmf->common.version = cpu_to_be32(1);
4809 	}
4810 	tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
4811 	tmf->common.length = cpu_to_be16(sizeof(*tmf));
4812 	tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
4813 	tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
4814 
4815 	rc = ibmvfc_send_event(evt, vhost, default_timeout);
4816 
4817 	if (rc) {
4818 		tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
4819 		vhost->abort_threads--;
4820 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4821 		__ibmvfc_reset_host(vhost);
4822 	} else
4823 		tgt_dbg(tgt, "Attempting to cancel ADISC\n");
4824 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4825 }
4826 
4827 /**
4828  * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
4829  * @tgt:		ibmvfc target struct
4830  *
4831  * When sending an ADISC we end up with two timers running. The
4832  * first timer is the timer in the ibmvfc target struct. If this
4833  * fires, we send a cancel to the target. The second timer is the
4834  * timer on the ibmvfc event for the ADISC, which is longer. If that
4835  * fires, it means the ADISC timed out and our attempt to cancel it
4836  * also failed, so we need to reset the CRQ.
4837  **/
ibmvfc_tgt_adisc(struct ibmvfc_target * tgt)4838 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
4839 {
4840 	struct ibmvfc_passthru_mad *mad;
4841 	struct ibmvfc_host *vhost = tgt->vhost;
4842 	struct ibmvfc_event *evt;
4843 
4844 	if (vhost->discovery_threads >= disc_threads)
4845 		return;
4846 
4847 	kref_get(&tgt->kref);
4848 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4849 	if (!evt) {
4850 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4851 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4852 		__ibmvfc_reset_host(vhost);
4853 		return;
4854 	}
4855 	vhost->discovery_threads++;
4856 	ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
4857 	evt->tgt = tgt;
4858 
4859 	ibmvfc_init_passthru(evt);
4860 	mad = &evt->iu.passthru;
4861 	mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4862 	mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4863 	mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4864 
4865 	mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4866 	memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4867 	       sizeof(vhost->login_buf->resp.port_name));
4868 	memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4869 	       sizeof(vhost->login_buf->resp.node_name));
4870 	mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4871 
4872 	if (timer_pending(&tgt->timer))
4873 		mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4874 	else {
4875 		tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4876 		add_timer(&tgt->timer);
4877 	}
4878 
4879 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4880 	if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4881 		vhost->discovery_threads--;
4882 		timer_delete(&tgt->timer);
4883 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4884 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4885 	} else
4886 		tgt_dbg(tgt, "Sent ADISC\n");
4887 }
4888 
4889 /**
4890  * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4891  * @evt:	ibmvfc event struct
4892  *
4893  **/
ibmvfc_tgt_query_target_done(struct ibmvfc_event * evt)4894 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4895 {
4896 	struct ibmvfc_target *tgt = evt->tgt;
4897 	struct ibmvfc_host *vhost = evt->vhost;
4898 	struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4899 	u32 status = be16_to_cpu(rsp->common.status);
4900 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4901 
4902 	vhost->discovery_threads--;
4903 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4904 	switch (status) {
4905 	case IBMVFC_MAD_SUCCESS:
4906 		tgt_dbg(tgt, "%s Query Target succeeded\n", proto_type[tgt->protocol]);
4907 		if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4908 			ibmvfc_del_tgt(tgt);
4909 		else
4910 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4911 		break;
4912 	case IBMVFC_MAD_DRIVER_FAILED:
4913 		break;
4914 	case IBMVFC_MAD_CRQ_ERROR:
4915 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4916 		break;
4917 	case IBMVFC_MAD_FAILED:
4918 	default:
4919 		if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4920 		    be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4921 		    be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4922 			ibmvfc_del_tgt(tgt);
4923 		else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4924 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4925 		else
4926 			ibmvfc_del_tgt(tgt);
4927 
4928 		tgt_log(tgt, level, "%s Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4929 			proto_type[tgt->protocol], ibmvfc_get_cmd_error(be16_to_cpu(rsp->status),
4930 			be16_to_cpu(rsp->error)), be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4931 			ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4932 			ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4933 			status);
4934 		break;
4935 	}
4936 
4937 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4938 	ibmvfc_free_event(evt);
4939 	wake_up(&vhost->work_wait_q);
4940 }
4941 
4942 /**
4943  * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4944  * @tgt:	ibmvfc target struct
4945  *
4946  **/
ibmvfc_tgt_query_target(struct ibmvfc_target * tgt)4947 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4948 {
4949 	struct ibmvfc_query_tgt *query_tgt;
4950 	struct ibmvfc_host *vhost = tgt->vhost;
4951 	struct ibmvfc_event *evt;
4952 
4953 	if (vhost->discovery_threads >= disc_threads)
4954 		return;
4955 
4956 	kref_get(&tgt->kref);
4957 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4958 	if (!evt) {
4959 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4960 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4961 		__ibmvfc_reset_host(vhost);
4962 		return;
4963 	}
4964 	vhost->discovery_threads++;
4965 	evt->tgt = tgt;
4966 	ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4967 	query_tgt = &evt->iu.query_tgt;
4968 	memset(query_tgt, 0, sizeof(*query_tgt));
4969 	query_tgt->common.version = cpu_to_be32(1);
4970 	if (tgt->protocol == IBMVFC_PROTO_SCSI)
4971 		query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4972 	else
4973 		query_tgt->common.opcode = cpu_to_be32(IBMVFC_NVMF_QUERY_TARGET);
4974 	query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4975 	query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4976 
4977 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4978 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4979 		vhost->discovery_threads--;
4980 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4981 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4982 	} else
4983 		tgt_dbg(tgt, "Sent %s Query Target\n", proto_type[tgt->protocol]);
4984 }
4985 
4986 /**
4987  * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4988  * @vhost:		ibmvfc host struct
4989  * @target:		Holds SCSI ID to allocate target forand the WWPN
4990  *
4991  * Returns:
4992  *	0 on success / other on failure
4993  **/
ibmvfc_alloc_target(struct ibmvfc_host * vhost,struct ibmvfc_discover_targets_entry * target,enum ibmvfc_protocol protocol)4994 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4995 			       struct ibmvfc_discover_targets_entry *target,
4996 			       enum ibmvfc_protocol protocol)
4997 {
4998 	struct ibmvfc_target *stgt = NULL;
4999 	struct ibmvfc_target *wtgt = NULL;
5000 	struct ibmvfc_target *tgt;
5001 	struct ibmvfc_channels *channels;
5002 	unsigned long flags;
5003 	u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
5004 	u64 wwpn = be64_to_cpu(target->wwpn);
5005 
5006 	if (protocol == IBMVFC_PROTO_SCSI)
5007 		channels = &vhost->scsi_scrqs;
5008 	else
5009 		channels = &vhost->nvme_scrqs;
5010 
5011 	/* Look to see if we already have a target allocated for this SCSI ID or WWPN */
5012 	spin_lock_irqsave(vhost->host->host_lock, flags);
5013 	list_for_each_entry(tgt, &channels->targets, queue) {
5014 		if (tgt->wwpn == wwpn) {
5015 			wtgt = tgt;
5016 			break;
5017 		}
5018 	}
5019 
5020 	list_for_each_entry(tgt, &channels->targets, queue) {
5021 		if (tgt->scsi_id == scsi_id) {
5022 			stgt = tgt;
5023 			break;
5024 		}
5025 	}
5026 
5027 	if (wtgt && !stgt) {
5028 		/*
5029 		 * A WWPN target has moved and we still are tracking the old
5030 		 * SCSI ID.  The only way we should be able to get here is if
5031 		 * we attempted to send an implicit logout for the old SCSI ID
5032 		 * and it failed for some reason, such as there being I/O
5033 		 * pending to the target. In this case, we will have already
5034 		 * deleted the rport from the FC transport so we do a move
5035 		 * login, which works even with I/O pending, however, if
5036 		 * there is still I/O pending, it will stay outstanding, so
5037 		 * we only do this if fast fail is disabled for the rport,
5038 		 * otherwise we let terminate_rport_io clean up the port
5039 		 * before we login at the new location.
5040 		 */
5041 		if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
5042 			if (wtgt->move_login) {
5043 				/*
5044 				 * Do a move login here. The old target is no longer
5045 				 * known to the transport layer We don't use the
5046 				 * normal ibmvfc_set_tgt_action to set this, as we
5047 				 * don't normally want to allow this state change.
5048 				 */
5049 				wtgt->new_scsi_id = scsi_id;
5050 				wtgt->action = IBMVFC_TGT_ACTION_INIT;
5051 				wtgt->init_retries = 0;
5052 				ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
5053 			}
5054 			goto unlock_out;
5055 		} else {
5056 			tgt_err(wtgt, "Unexpected target state: %d, %p\n",
5057 				wtgt->action, wtgt->rport);
5058 		}
5059 	} else if (stgt) {
5060 		if (tgt->need_login)
5061 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
5062 		goto unlock_out;
5063 	}
5064 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5065 
5066 	tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
5067 	memset(tgt, 0, sizeof(*tgt));
5068 	tgt->protocol = protocol;
5069 	tgt->scsi_id = scsi_id;
5070 	tgt->wwpn = wwpn;
5071 	tgt->vhost = vhost;
5072 	tgt->need_login = 1;
5073 	timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
5074 	kref_init(&tgt->kref);
5075 	ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
5076 	spin_lock_irqsave(vhost->host->host_lock, flags);
5077 	tgt->cancel_key = vhost->task_set++;
5078 	list_add_tail(&tgt->queue, &channels->targets);
5079 
5080 unlock_out:
5081 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5082 	return 0;
5083 }
5084 
5085 /**
5086  * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
5087  * @vhost:		ibmvfc host struct
5088  *
5089  * Returns:
5090  *	0 on success / other on failure
5091  **/
ibmvfc_alloc_targets(struct ibmvfc_host * vhost)5092 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
5093 {
5094 	int i, rc;
5095 
5096 	for (i = 0, rc = 0; !rc && i < vhost->scsi_scrqs.num_targets; i++)
5097 		rc = ibmvfc_alloc_target(vhost, &vhost->scsi_scrqs.disc_buf[i],
5098 					 vhost->scsi_scrqs.protocol);
5099 	for (i = 0; !rc && i < vhost->nvme_scrqs.num_targets; i++)
5100 		rc = ibmvfc_alloc_target(vhost, &vhost->nvme_scrqs.disc_buf[i],
5101 					 vhost->nvme_scrqs.protocol);
5102 
5103 	return rc;
5104 }
5105 
5106 /**
5107  * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
5108  * @evt:	ibmvfc event struct
5109  *
5110  **/
ibmvfc_discover_targets_done(struct ibmvfc_event * evt)5111 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
5112 {
5113 	struct ibmvfc_host *vhost = evt->vhost;
5114 	struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
5115 	struct ibmvfc_channels *channels;
5116 	u32 mad_status = be16_to_cpu(rsp->common.status);
5117 	u32 opcode = be32_to_cpu(rsp->common.opcode);
5118 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5119 
5120 	if (opcode == IBMVFC_DISC_TARGETS)
5121 		channels = &vhost->scsi_scrqs;
5122 	else
5123 		channels = &vhost->nvme_scrqs;
5124 
5125 	switch (mad_status) {
5126 	case IBMVFC_MAD_SUCCESS:
5127 		ibmvfc_dbg(vhost, "Discover %s Targets succeeded\n",
5128 			   proto_type[channels->protocol]);
5129 		channels->num_targets = min_t(u32, be32_to_cpu(rsp->num_written),
5130 					      max_targets);
5131 		ibmvfc_dbg(vhost, "%d %s targets found\n", channels->num_targets,
5132 			   proto_type[channels->protocol]);
5133 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
5134 		break;
5135 	case IBMVFC_MAD_FAILED:
5136 		level += ibmvfc_retry_host_init(vhost);
5137 		ibmvfc_log(vhost, level, "Discover %s Targets failed: %s (%x:%x)\n",
5138 			   proto_type[channels->protocol],
5139 			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
5140 			   be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
5141 		break;
5142 	case IBMVFC_MAD_DRIVER_FAILED:
5143 		break;
5144 	default:
5145 		dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
5146 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5147 		break;
5148 	}
5149 
5150 	ibmvfc_free_event(evt);
5151 	wake_up(&vhost->work_wait_q);
5152 }
5153 
ibmvfc_get_disc_event(struct ibmvfc_channels * channels)5154 static struct ibmvfc_event *ibmvfc_get_disc_event(struct ibmvfc_channels *channels)
5155 {
5156 	struct ibmvfc_discover_targets *mad;
5157 	struct ibmvfc_host *vhost = ibmvfc_channels_to_vhost(channels);
5158 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
5159 
5160 	if (!evt)
5161 		return NULL;
5162 
5163 	ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
5164 	mad = &evt->iu.discover_targets;
5165 	memset(mad, 0, sizeof(*mad));
5166 	mad->common.version = cpu_to_be32(1);
5167 	if (channels->protocol == IBMVFC_PROTO_SCSI)
5168 		mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
5169 	else
5170 		mad->common.opcode = cpu_to_be32(IBMVFC_DISC_NVMF_TARGETS);
5171 	mad->common.length = cpu_to_be16(sizeof(*mad));
5172 	mad->bufflen = cpu_to_be32(channels->disc_buf_sz);
5173 	mad->buffer.va = cpu_to_be64(channels->disc_buf_dma);
5174 	mad->buffer.len = cpu_to_be32(channels->disc_buf_sz);
5175 	mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
5176 
5177 	return evt;
5178 }
5179 
5180 /**
5181  * ibmvfc_discover_targets - Send Discover Targets MAD
5182  * @vhost:	ibmvfc host struct
5183  *
5184  **/
ibmvfc_discover_targets(struct ibmvfc_host * vhost)5185 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
5186 {
5187 	struct ibmvfc_event *evt = ibmvfc_get_disc_event(&vhost->scsi_scrqs);
5188 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5189 
5190 	if (!evt) {
5191 		ibmvfc_log(vhost, level, "Discover SCSI Targets failed: no available events\n");
5192 		ibmvfc_hard_reset_host(vhost);
5193 		return;
5194 	}
5195 
5196 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5197 
5198 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5199 		ibmvfc_dbg(vhost, "Sent discover SCSI targets\n");
5200 	else
5201 		goto link_down;
5202 
5203 	if (!ibmvfc_nvme_active(vhost))
5204 		return;
5205 
5206 	evt = ibmvfc_get_disc_event(&vhost->nvme_scrqs);
5207 	if (!evt) {
5208 		ibmvfc_log(vhost, level, "Discover NVMe Targets failed: no available events\n");
5209 		ibmvfc_hard_reset_host(vhost);
5210 		return;
5211 	}
5212 
5213 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5214 		ibmvfc_dbg(vhost, "Sent discover NVMe targets\n");
5215 	else
5216 		goto link_down;
5217 
5218 	return;
5219 
5220 link_down:
5221 	ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5222 }
5223 
ibmvfc_fabric_login_nvme_done(struct ibmvfc_event * evt)5224 static void ibmvfc_fabric_login_nvme_done(struct ibmvfc_event *evt)
5225 {
5226 	struct ibmvfc_host *vhost = evt->vhost;
5227 	struct ibmvfc_fabric_login_mad *rsp = &evt->xfer_iu->fabric_login;
5228 	u32 mad_status = be16_to_cpu(rsp->common.status);
5229 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5230 
5231 	switch (mad_status) {
5232 	case IBMVFC_MAD_SUCCESS:
5233 		fc_host_port_id(vhost->host) = be64_to_cpu(rsp->nport_id);
5234 		ibmvfc_nvme_register(vhost);
5235 		ibmvfc_dbg(vhost, "NVMe fabric login succeeded\n");
5236 		break;
5237 	case IBMVFC_MAD_FAILED:
5238 		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
5239 			level += ibmvfc_retry_host_init(vhost);
5240 		else
5241 			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5242 		ibmvfc_log(vhost, level, "NVMe fabric login failed: %s (%x:%x)\n",
5243 			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
5244 			   be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
5245 		ibmvfc_free_event(evt);
5246 		return;
5247 	case IBMVFC_MAD_DRIVER_FAILED:
5248 		ibmvfc_free_event(evt);
5249 		return;
5250 	default:
5251 		dev_err(vhost->dev, "Invalid NVMe fabric login response: 0x%x\n", mad_status);
5252 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5253 		break;
5254 	}
5255 
5256 	ibmvfc_free_event(evt);
5257 
5258 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5259 	wake_up(&vhost->work_wait_q);
5260 }
5261 
ibmvfc_fabric_login_nvme(struct ibmvfc_host * vhost)5262 static void ibmvfc_fabric_login_nvme(struct ibmvfc_host *vhost)
5263 {
5264 	struct ibmvfc_fabric_login_mad *mad;
5265 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
5266 
5267 	if (!evt) {
5268 		ibmvfc_retry_host_init(vhost);
5269 		return;
5270 	}
5271 
5272 	ibmvfc_init_event(evt, ibmvfc_fabric_login_nvme_done, IBMVFC_MAD_FORMAT);
5273 	mad = &evt->iu.fabric_login;
5274 	memset(mad, 0, sizeof(*mad));
5275 	mad->common.version = cpu_to_be32(1);
5276 	mad->common.opcode = cpu_to_be32(IBMVFC_NVMF_FABRIC_LOGIN);
5277 	mad->common.length = cpu_to_be16(sizeof(*mad));
5278 
5279 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5280 
5281 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5282 		ibmvfc_dbg(vhost, "Send NVMe fabric login\n");
5283 	else
5284 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5285 }
5286 
ibmvfc_fabric_login_scsi_done(struct ibmvfc_event * evt)5287 static void ibmvfc_fabric_login_scsi_done(struct ibmvfc_event *evt)
5288 {
5289 	struct ibmvfc_host *vhost = evt->vhost;
5290 	struct ibmvfc_fabric_login_mad *rsp = &evt->xfer_iu->fabric_login;
5291 	u32 mad_status = be16_to_cpu(rsp->common.status);
5292 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5293 
5294 	switch (mad_status) {
5295 	case IBMVFC_MAD_SUCCESS:
5296 		fc_host_port_id(vhost->host) = be64_to_cpu(rsp->nport_id);
5297 		ibmvfc_dbg(vhost, "SCSI fabric login succeeded\n");
5298 		break;
5299 	case IBMVFC_MAD_FAILED:
5300 		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
5301 			level += ibmvfc_retry_host_init(vhost);
5302 		else
5303 			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5304 		ibmvfc_log(vhost, level, "SCSI fabric login failed: %s (%x:%x)\n",
5305 			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
5306 			   be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
5307 		ibmvfc_free_event(evt);
5308 		return;
5309 	case IBMVFC_MAD_DRIVER_FAILED:
5310 		ibmvfc_free_event(evt);
5311 		return;
5312 	default:
5313 		dev_err(vhost->dev, "Invalid SCSI fabric login response: 0x%x\n", mad_status);
5314 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5315 		break;
5316 	}
5317 
5318 	ibmvfc_free_event(evt);
5319 
5320 	if (vhost->do_nvme_login) {
5321 		ibmvfc_fabric_login_nvme(vhost);
5322 	} else {
5323 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5324 		wake_up(&vhost->work_wait_q);
5325 	}
5326 }
5327 
ibmvfc_fabric_login_scsi(struct ibmvfc_host * vhost)5328 static void ibmvfc_fabric_login_scsi(struct ibmvfc_host *vhost)
5329 {
5330 	struct ibmvfc_fabric_login_mad *mad;
5331 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
5332 
5333 	if (!evt) {
5334 		ibmvfc_retry_host_init(vhost);
5335 		return;
5336 	}
5337 
5338 	ibmvfc_init_event(evt, ibmvfc_fabric_login_scsi_done, IBMVFC_MAD_FORMAT);
5339 	mad = &evt->iu.fabric_login;
5340 	memset(mad, 0, sizeof(*mad));
5341 	mad->common.version = cpu_to_be32(1);
5342 	mad->common.opcode = cpu_to_be32(IBMVFC_FABRIC_LOGIN);
5343 	mad->common.length = cpu_to_be16(sizeof(*mad));
5344 
5345 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5346 
5347 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5348 		ibmvfc_dbg(vhost, "Send SCSI fabric login\n");
5349 	else
5350 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5351 }
5352 
ibmvfc_channel_setup_done(struct ibmvfc_event * evt)5353 static void ibmvfc_channel_setup_done(struct ibmvfc_event *evt)
5354 {
5355 	struct ibmvfc_host *vhost = evt->vhost;
5356 	struct ibmvfc_channel_setup *setup = vhost->channel_setup_buf;
5357 	struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
5358 	struct ibmvfc_channels *nvme = &vhost->nvme_scrqs;
5359 	u32 mad_status = be16_to_cpu(evt->xfer_iu->channel_setup.common.status);
5360 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5361 	int flags, i;
5362 
5363 	ibmvfc_free_event(evt);
5364 
5365 	switch (mad_status) {
5366 	case IBMVFC_MAD_SUCCESS:
5367 		ibmvfc_dbg(vhost, "Channel Setup succeeded\n");
5368 		flags = be32_to_cpu(setup->flags);
5369 		vhost->do_enquiry = 0;
5370 		scsi->active_queues = be32_to_cpu(setup->num_scsi_subq_channels);
5371 		nvme->active_queues = be32_to_cpu(setup->num_nvme_subq_channels);
5372 
5373 		if (flags & IBMVFC_CHANNELS_CANCELED) {
5374 			ibmvfc_dbg(vhost, "Channels Canceled\n");
5375 			vhost->using_channels = 0;
5376 			break;
5377 		}
5378 
5379 		if (scsi->active_queues || nvme->active_queues)
5380 			vhost->using_channels = 1;
5381 		for (i = 0; i < scsi->active_queues; i++)
5382 			scsi->scrqs[i].vios_cookie =
5383 				be64_to_cpu(setup->channel_handles[i]);
5384 		for (i = 0; i < nvme->active_queues; i++)
5385 			nvme->scrqs[i].vios_cookie =
5386 				be64_to_cpu(setup->channel_handles[scsi->active_queues + i]);
5387 
5388 		ibmvfc_dbg(vhost, "Using %u SCSI channels\n",
5389 			   scsi->active_queues);
5390 		ibmvfc_dbg(vhost, "Using %u NVMe channels\n",
5391 			   nvme->active_queues);
5392 		break;
5393 	case IBMVFC_MAD_FAILED:
5394 		level += ibmvfc_retry_host_init(vhost);
5395 		ibmvfc_log(vhost, level, "Channel Setup failed\n");
5396 		fallthrough;
5397 	case IBMVFC_MAD_DRIVER_FAILED:
5398 		return;
5399 	default:
5400 		dev_err(vhost->dev, "Invalid Channel Setup response: 0x%x\n",
5401 			mad_status);
5402 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5403 		return;
5404 	}
5405 
5406 	if (vhost->do_scsi_login) {
5407 		ibmvfc_fabric_login_scsi(vhost);
5408 	} else {
5409 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5410 		wake_up(&vhost->work_wait_q);
5411 	}
5412 }
5413 
ibmvfc_channel_setup(struct ibmvfc_host * vhost)5414 static void ibmvfc_channel_setup(struct ibmvfc_host *vhost)
5415 {
5416 	struct ibmvfc_channel_setup_mad *mad;
5417 	struct ibmvfc_channel_setup *setup_buf = vhost->channel_setup_buf;
5418 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
5419 	struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
5420 	struct ibmvfc_channels *nvme = &vhost->nvme_scrqs;
5421 	unsigned int scsi_channels =
5422 		min(scsi->desired_queues, vhost->max_vios_scsi_channels);
5423 	unsigned int nvme_channels =
5424 		min(nvme->desired_queues, vhost->max_vios_nvme_channels);
5425 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5426 	int i;
5427 
5428 	if (!evt) {
5429 		ibmvfc_log(vhost, level, "Channel Setup failed: no available events\n");
5430 		ibmvfc_hard_reset_host(vhost);
5431 		return;
5432 	}
5433 
5434 	memset(setup_buf, 0, sizeof(*setup_buf));
5435 	if (!scsi_channels && !nvme_channels)
5436 		setup_buf->flags = cpu_to_be32(IBMVFC_CANCEL_CHANNELS);
5437 	else {
5438 		setup_buf->num_scsi_subq_channels = cpu_to_be32(scsi_channels);
5439 		setup_buf->num_nvme_subq_channels = cpu_to_be32(nvme_channels);
5440 		for (i = 0; i < scsi_channels; i++)
5441 			setup_buf->channel_handles[i] =
5442 				cpu_to_be64(scsi->scrqs[i].cookie);
5443 		for (i = 0; i < nvme_channels; i++)
5444 			setup_buf->channel_handles[scsi_channels + i] =
5445 				cpu_to_be64(nvme->scrqs[i].cookie);
5446 	}
5447 
5448 	ibmvfc_init_event(evt, ibmvfc_channel_setup_done, IBMVFC_MAD_FORMAT);
5449 	mad = &evt->iu.channel_setup;
5450 	memset(mad, 0, sizeof(*mad));
5451 	mad->common.version = cpu_to_be32(1);
5452 	mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_SETUP);
5453 	mad->common.length = cpu_to_be16(sizeof(*mad));
5454 	mad->buffer.va = cpu_to_be64(vhost->channel_setup_dma);
5455 	mad->buffer.len = cpu_to_be32(sizeof(*vhost->channel_setup_buf));
5456 
5457 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5458 
5459 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5460 		ibmvfc_dbg(vhost, "Sent channel setup\n");
5461 	else
5462 		ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
5463 }
5464 
ibmvfc_channel_enquiry_done(struct ibmvfc_event * evt)5465 static void ibmvfc_channel_enquiry_done(struct ibmvfc_event *evt)
5466 {
5467 	struct ibmvfc_host *vhost = evt->vhost;
5468 	struct ibmvfc_channel_enquiry *rsp = &evt->xfer_iu->channel_enquiry;
5469 	u32 mad_status = be16_to_cpu(rsp->common.status);
5470 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5471 
5472 	switch (mad_status) {
5473 	case IBMVFC_MAD_SUCCESS:
5474 		ibmvfc_dbg(vhost, "Channel Enquiry succeeded\n");
5475 		vhost->max_vios_scsi_channels = be32_to_cpu(rsp->num_scsi_subq_channels);
5476 		vhost->max_vios_nvme_channels = be32_to_cpu(rsp->num_nvme_subq_channels);
5477 		ibmvfc_free_event(evt);
5478 		break;
5479 	case IBMVFC_MAD_FAILED:
5480 		level += ibmvfc_retry_host_init(vhost);
5481 		ibmvfc_log(vhost, level, "Channel Enquiry failed\n");
5482 		fallthrough;
5483 	case IBMVFC_MAD_DRIVER_FAILED:
5484 		ibmvfc_free_event(evt);
5485 		return;
5486 	default:
5487 		dev_err(vhost->dev, "Invalid Channel Enquiry response: 0x%x\n",
5488 			mad_status);
5489 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5490 		ibmvfc_free_event(evt);
5491 		return;
5492 	}
5493 
5494 	ibmvfc_channel_setup(vhost);
5495 }
5496 
ibmvfc_channel_enquiry(struct ibmvfc_host * vhost)5497 static void ibmvfc_channel_enquiry(struct ibmvfc_host *vhost)
5498 {
5499 	struct ibmvfc_channel_enquiry *mad;
5500 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
5501 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5502 
5503 	if (!evt) {
5504 		ibmvfc_log(vhost, level, "Channel Enquiry failed: no available events\n");
5505 		ibmvfc_hard_reset_host(vhost);
5506 		return;
5507 	}
5508 
5509 	ibmvfc_init_event(evt, ibmvfc_channel_enquiry_done, IBMVFC_MAD_FORMAT);
5510 	mad = &evt->iu.channel_enquiry;
5511 	memset(mad, 0, sizeof(*mad));
5512 	mad->common.version = cpu_to_be32(1);
5513 	mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_ENQUIRY);
5514 	mad->common.length = cpu_to_be16(sizeof(*mad));
5515 
5516 	if (mig_channels_only)
5517 		mad->flags |= cpu_to_be32(IBMVFC_NO_CHANNELS_TO_CRQ_SUPPORT);
5518 	if (mig_no_less_channels)
5519 		mad->flags |= cpu_to_be32(IBMVFC_NO_N_TO_M_CHANNELS_SUPPORT);
5520 
5521 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5522 
5523 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5524 		ibmvfc_dbg(vhost, "Send channel enquiry\n");
5525 	else
5526 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5527 }
5528 
5529 /**
5530  * ibmvfc_npiv_login_done - Completion handler for NPIV Login
5531  * @evt:	ibmvfc event struct
5532  *
5533  **/
ibmvfc_npiv_login_done(struct ibmvfc_event * evt)5534 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
5535 {
5536 	struct ibmvfc_host *vhost = evt->vhost;
5537 	u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
5538 	struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
5539 	unsigned int npiv_max_sectors;
5540 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5541 
5542 	switch (mad_status) {
5543 	case IBMVFC_MAD_SUCCESS:
5544 		ibmvfc_free_event(evt);
5545 		break;
5546 	case IBMVFC_MAD_FAILED:
5547 		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
5548 			level += ibmvfc_retry_host_init(vhost);
5549 		else
5550 			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5551 		ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
5552 			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
5553 						be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
5554 		ibmvfc_free_event(evt);
5555 		return;
5556 	case IBMVFC_MAD_CRQ_ERROR:
5557 		ibmvfc_retry_host_init(vhost);
5558 		fallthrough;
5559 	case IBMVFC_MAD_DRIVER_FAILED:
5560 		ibmvfc_free_event(evt);
5561 		return;
5562 	default:
5563 		dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
5564 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5565 		ibmvfc_free_event(evt);
5566 		return;
5567 	}
5568 
5569 	vhost->client_migrated = 0;
5570 
5571 	if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
5572 		dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
5573 			rsp->flags);
5574 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5575 		wake_up(&vhost->work_wait_q);
5576 		return;
5577 	}
5578 
5579 	if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
5580 		dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
5581 			rsp->max_cmds);
5582 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5583 		wake_up(&vhost->work_wait_q);
5584 		return;
5585 	}
5586 
5587 	vhost->logged_in = 1;
5588 	npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), max_sectors);
5589 	dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
5590 		 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
5591 		 rsp->drc_name, npiv_max_sectors);
5592 
5593 	fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
5594 	fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
5595 	fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
5596 	fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
5597 	fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
5598 	fc_host_supported_classes(vhost->host) = 0;
5599 	if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
5600 		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
5601 	if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
5602 		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
5603 	if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
5604 		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
5605 	fc_host_maxframe_size(vhost->host) =
5606 		be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
5607 
5608 	vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
5609 	vhost->host->max_sectors = npiv_max_sectors;
5610 
5611 
5612 	if (ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPORT_CHANNELS)) {
5613 		if (ibmvfc_check_caps(vhost, IBMVFC_SUPPORT_SCSI))
5614 			vhost->do_scsi_login = 1;
5615 		if (ibmvfc_check_caps(vhost, IBMVFC_SUPPORT_NVMEOF))
5616 			vhost->do_nvme_login = 1;
5617 		if (vhost->do_enquiry)
5618 			ibmvfc_channel_enquiry(vhost);
5619 	} else {
5620 		vhost->do_enquiry = 0;
5621 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5622 		wake_up(&vhost->work_wait_q);
5623 	}
5624 }
5625 
5626 /**
5627  * ibmvfc_npiv_login - Sends NPIV login
5628  * @vhost:	ibmvfc host struct
5629  *
5630  **/
ibmvfc_npiv_login(struct ibmvfc_host * vhost)5631 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
5632 {
5633 	struct ibmvfc_npiv_login_mad *mad;
5634 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
5635 
5636 	if (!evt) {
5637 		ibmvfc_dbg(vhost, "NPIV Login failed: no available events\n");
5638 		ibmvfc_hard_reset_host(vhost);
5639 		return;
5640 	}
5641 
5642 	ibmvfc_gather_partition_info(vhost);
5643 	ibmvfc_set_login_info(vhost);
5644 	ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
5645 
5646 	memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
5647 	mad = &evt->iu.npiv_login;
5648 	memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
5649 	mad->common.version = cpu_to_be32(1);
5650 	mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
5651 	mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
5652 	mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
5653 	mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
5654 
5655 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5656 
5657 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5658 		ibmvfc_dbg(vhost, "Sent NPIV login\n");
5659 	else
5660 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5661 }
5662 
5663 /**
5664  * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
5665  * @evt:		ibmvfc event struct
5666  *
5667  **/
ibmvfc_npiv_logout_done(struct ibmvfc_event * evt)5668 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
5669 {
5670 	struct ibmvfc_host *vhost = evt->vhost;
5671 	u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
5672 
5673 	ibmvfc_free_event(evt);
5674 
5675 	switch (mad_status) {
5676 	case IBMVFC_MAD_SUCCESS:
5677 		if (list_empty(&vhost->crq.sent) &&
5678 		    vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
5679 			ibmvfc_nvme_unregister(vhost);
5680 			ibmvfc_init_host(vhost);
5681 			return;
5682 		}
5683 		break;
5684 	case IBMVFC_MAD_FAILED:
5685 	case IBMVFC_MAD_NOT_SUPPORTED:
5686 	case IBMVFC_MAD_CRQ_ERROR:
5687 	case IBMVFC_MAD_DRIVER_FAILED:
5688 	default:
5689 		ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
5690 		break;
5691 	}
5692 
5693 	ibmvfc_hard_reset_host(vhost);
5694 }
5695 
5696 /**
5697  * ibmvfc_npiv_logout - Issue an NPIV Logout
5698  * @vhost:		ibmvfc host struct
5699  *
5700  **/
ibmvfc_npiv_logout(struct ibmvfc_host * vhost)5701 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
5702 {
5703 	struct ibmvfc_npiv_logout_mad *mad;
5704 	struct ibmvfc_event *evt;
5705 
5706 	evt = ibmvfc_get_reserved_event(&vhost->crq);
5707 	if (!evt) {
5708 		ibmvfc_dbg(vhost, "NPIV Logout failed: no available events\n");
5709 		ibmvfc_hard_reset_host(vhost);
5710 		return;
5711 	}
5712 
5713 	ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
5714 
5715 	mad = &evt->iu.npiv_logout;
5716 	memset(mad, 0, sizeof(*mad));
5717 	mad->common.version = cpu_to_be32(1);
5718 	mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
5719 	mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
5720 
5721 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
5722 
5723 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5724 		ibmvfc_dbg(vhost, "Sent NPIV logout\n");
5725 	else
5726 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5727 }
5728 
5729 /**
5730  * ibmvfc_dev_init_to_do - Is there target initialization work to do?
5731  * @vhost:		ibmvfc host struct
5732  *
5733  * Returns:
5734  *	1 if work to do / 0 if not
5735  **/
ibmvfc_dev_init_to_do(struct ibmvfc_host * vhost)5736 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
5737 {
5738 	struct ibmvfc_target *tgt;
5739 
5740 	list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
5741 		if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
5742 		    tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5743 			return 1;
5744 	}
5745 	list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue) {
5746 		if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
5747 		    tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5748 			return 1;
5749 	}
5750 
5751 	return 0;
5752 }
5753 
5754 /**
5755  * ibmvfc_dev_logo_to_do - Is there target logout work to do?
5756  * @vhost:		ibmvfc host struct
5757  *
5758  * Returns:
5759  *	1 if work to do / 0 if not
5760  **/
ibmvfc_dev_logo_to_do(struct ibmvfc_host * vhost)5761 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
5762 {
5763 	struct ibmvfc_target *tgt;
5764 
5765 	list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
5766 		if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
5767 		    tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5768 			return 1;
5769 	}
5770 	list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue) {
5771 		if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
5772 		    tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5773 			return 1;
5774 	}
5775 	return 0;
5776 }
5777 
5778 /**
5779  * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
5780  * @vhost:		ibmvfc host struct
5781  *
5782  * Returns:
5783  *	1 if work to do / 0 if not
5784  **/
__ibmvfc_work_to_do(struct ibmvfc_host * vhost)5785 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5786 {
5787 	struct ibmvfc_target *tgt;
5788 
5789 	if (kthread_should_stop())
5790 		return 1;
5791 	switch (vhost->action) {
5792 	case IBMVFC_HOST_ACTION_NONE:
5793 	case IBMVFC_HOST_ACTION_INIT_WAIT:
5794 	case IBMVFC_HOST_ACTION_LOGO_WAIT:
5795 		return 0;
5796 	case IBMVFC_HOST_ACTION_TGT_INIT:
5797 	case IBMVFC_HOST_ACTION_QUERY_TGTS:
5798 		if (vhost->discovery_threads == disc_threads)
5799 			return 0;
5800 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue)
5801 			if (tgt->action == IBMVFC_TGT_ACTION_INIT)
5802 				return 1;
5803 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue)
5804 			if (tgt->action == IBMVFC_TGT_ACTION_INIT)
5805 				return 1;
5806 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue)
5807 			if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5808 				return 0;
5809 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue)
5810 			if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5811 				return 0;
5812 		return 1;
5813 	case IBMVFC_HOST_ACTION_TGT_DEL:
5814 	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5815 		if (vhost->discovery_threads == disc_threads)
5816 			return 0;
5817 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue)
5818 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
5819 				return 1;
5820 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue)
5821 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
5822 				return 1;
5823 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue)
5824 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5825 				return 0;
5826 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue)
5827 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5828 				return 0;
5829 		return 1;
5830 	case IBMVFC_HOST_ACTION_LOGO:
5831 	case IBMVFC_HOST_ACTION_INIT:
5832 	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5833 	case IBMVFC_HOST_ACTION_QUERY:
5834 	case IBMVFC_HOST_ACTION_RESET:
5835 	case IBMVFC_HOST_ACTION_REENABLE:
5836 	default:
5837 		break;
5838 	}
5839 
5840 	return 1;
5841 }
5842 
5843 /**
5844  * ibmvfc_work_to_do - Is there task level work to do?
5845  * @vhost:		ibmvfc host struct
5846  *
5847  * Returns:
5848  *	1 if work to do / 0 if not
5849  **/
ibmvfc_work_to_do(struct ibmvfc_host * vhost)5850 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5851 {
5852 	unsigned long flags;
5853 	int rc;
5854 
5855 	spin_lock_irqsave(vhost->host->host_lock, flags);
5856 	rc = __ibmvfc_work_to_do(vhost);
5857 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5858 	return rc;
5859 }
5860 
5861 /**
5862  * ibmvfc_log_ae - Log async events if necessary
5863  * @vhost:		ibmvfc host struct
5864  * @events:		events to log
5865  *
5866  **/
ibmvfc_log_ae(struct ibmvfc_host * vhost,int events)5867 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
5868 {
5869 	if (events & IBMVFC_AE_RSCN)
5870 		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
5871 	if ((events & IBMVFC_AE_LINKDOWN) &&
5872 	    vhost->state >= IBMVFC_HALTED)
5873 		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
5874 	if ((events & IBMVFC_AE_LINKUP) &&
5875 	    vhost->state == IBMVFC_INITIALIZING)
5876 		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
5877 }
5878 
5879 /**
5880  * ibmvfc_tgt_add_nvme_rport - Tell the FC transport about a new remote port
5881  * @tgt:		ibmvfc target struct
5882  *
5883  **/
ibmvfc_tgt_add_nvme_rport(struct ibmvfc_target * tgt)5884 static void ibmvfc_tgt_add_nvme_rport(struct ibmvfc_target *tgt)
5885 {
5886 	struct ibmvfc_host *vhost = tgt->vhost;
5887 	struct nvme_fc_remote_port *rport;
5888 	unsigned long flags;
5889 
5890 	tgt_dbg(tgt, "Adding NVMe rport\n");
5891 	ibmvfc_nvme_register_remoteport(tgt);
5892 	spin_lock_irqsave(vhost->host->host_lock, flags);
5893 	rport = tgt->nvme_remote_port;
5894 
5895 	if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5896 		tgt_dbg(tgt, "Deleting NVMe rport\n");
5897 		list_del(&tgt->queue);
5898 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5899 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5900 		ibmvfc_nvme_unregister_remoteport(tgt);
5901 		timer_delete_sync(&tgt->timer);
5902 		kref_put(&tgt->kref, ibmvfc_release_tgt);
5903 		return;
5904 	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5905 		tgt_dbg(tgt, "Deleting NVMe rport with outstanding I/O\n");
5906 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5907 		tgt->init_retries = 0;
5908 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5909 		ibmvfc_nvme_unregister_remoteport(tgt);
5910 		return;
5911 	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
5912 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5913 		return;
5914 	}
5915 
5916 	if (rport) {
5917 		tgt_dbg(tgt, "NVMe rport add succeeded\n");
5918 		tgt->target_id = tgt->nvme_remote_port->port_id;
5919 	} else
5920 		tgt_dbg(tgt, "NVMe rport add failed\n");
5921 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5922 }
5923 
5924 /**
5925  * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
5926  * @tgt:		ibmvfc target struct
5927  *
5928  **/
ibmvfc_tgt_add_rport(struct ibmvfc_target * tgt)5929 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
5930 {
5931 	struct ibmvfc_host *vhost = tgt->vhost;
5932 	struct fc_rport *rport;
5933 	unsigned long flags;
5934 
5935 	tgt_dbg(tgt, "Adding rport\n");
5936 	rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
5937 	spin_lock_irqsave(vhost->host->host_lock, flags);
5938 
5939 	if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5940 		tgt_dbg(tgt, "Deleting rport\n");
5941 		list_del(&tgt->queue);
5942 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5943 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5944 		fc_remote_port_delete(rport);
5945 		timer_delete_sync(&tgt->timer);
5946 		kref_put(&tgt->kref, ibmvfc_release_tgt);
5947 		return;
5948 	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5949 		tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
5950 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5951 		tgt->rport = NULL;
5952 		tgt->init_retries = 0;
5953 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5954 		fc_remote_port_delete(rport);
5955 		return;
5956 	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
5957 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5958 		return;
5959 	}
5960 
5961 	if (rport) {
5962 		tgt_dbg(tgt, "rport add succeeded\n");
5963 		tgt->rport = rport;
5964 		rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
5965 		rport->supported_classes = 0;
5966 		tgt->target_id = rport->scsi_target_id;
5967 		if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
5968 			rport->supported_classes |= FC_COS_CLASS1;
5969 		if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
5970 			rport->supported_classes |= FC_COS_CLASS2;
5971 		if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
5972 			rport->supported_classes |= FC_COS_CLASS3;
5973 	} else
5974 		tgt_dbg(tgt, "rport add failed\n");
5975 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5976 }
5977 
5978 /**
5979  * ibmvfc_do_work - Do task level work
5980  * @vhost:		ibmvfc host struct
5981  *
5982  **/
ibmvfc_do_work(struct ibmvfc_host * vhost)5983 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
5984 {
5985 	struct ibmvfc_target *tgt;
5986 	unsigned long flags;
5987 	struct fc_rport *rport;
5988 	struct nvme_fc_remote_port *nvme_rport;
5989 	LIST_HEAD(purge);
5990 	int rc;
5991 
5992 	ibmvfc_log_ae(vhost, vhost->events_to_log);
5993 	spin_lock_irqsave(vhost->host->host_lock, flags);
5994 	vhost->events_to_log = 0;
5995 	switch (vhost->action) {
5996 	case IBMVFC_HOST_ACTION_NONE:
5997 	case IBMVFC_HOST_ACTION_LOGO_WAIT:
5998 	case IBMVFC_HOST_ACTION_INIT_WAIT:
5999 		break;
6000 	case IBMVFC_HOST_ACTION_RESET:
6001 		list_splice_init(&vhost->purge, &purge);
6002 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
6003 		ibmvfc_complete_purge(&purge);
6004 		ibmvfc_nvme_unregister(vhost);
6005 		rc = ibmvfc_reset_crq(vhost);
6006 
6007 		spin_lock_irqsave(vhost->host->host_lock, flags);
6008 		if (!rc || rc == H_CLOSED)
6009 			vio_enable_interrupts(to_vio_dev(vhost->dev));
6010 		if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
6011 			/*
6012 			 * The only action we could have changed to would have
6013 			 * been reenable, in which case, we skip the rest of
6014 			 * this path and wait until we've done the re-enable
6015 			 * before sending the crq init.
6016 			 */
6017 			vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
6018 
6019 			if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
6020 			    (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
6021 				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
6022 				dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
6023 			}
6024 		}
6025 		break;
6026 	case IBMVFC_HOST_ACTION_REENABLE:
6027 		list_splice_init(&vhost->purge, &purge);
6028 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
6029 		ibmvfc_complete_purge(&purge);
6030 		rc = ibmvfc_reenable_crq_queue(vhost);
6031 
6032 		spin_lock_irqsave(vhost->host->host_lock, flags);
6033 		if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
6034 			/*
6035 			 * The only action we could have changed to would have
6036 			 * been reset, in which case, we skip the rest of this
6037 			 * path and wait until we've done the reset before
6038 			 * sending the crq init.
6039 			 */
6040 			vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
6041 			if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
6042 				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
6043 				dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
6044 			}
6045 		}
6046 		break;
6047 	case IBMVFC_HOST_ACTION_LOGO:
6048 		vhost->job_step(vhost);
6049 		break;
6050 	case IBMVFC_HOST_ACTION_INIT:
6051 		BUG_ON(vhost->state != IBMVFC_INITIALIZING);
6052 		if (vhost->delay_init) {
6053 			vhost->delay_init = 0;
6054 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
6055 			ssleep(15);
6056 			return;
6057 		} else
6058 			vhost->job_step(vhost);
6059 		break;
6060 	case IBMVFC_HOST_ACTION_QUERY:
6061 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue)
6062 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
6063 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue)
6064 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
6065 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
6066 		break;
6067 	case IBMVFC_HOST_ACTION_QUERY_TGTS:
6068 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
6069 			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
6070 				tgt->job_step(tgt);
6071 				break;
6072 			}
6073 		}
6074 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue) {
6075 			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
6076 				tgt->job_step(tgt);
6077 				break;
6078 			}
6079 		}
6080 
6081 		if (!ibmvfc_dev_init_to_do(vhost))
6082 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
6083 		break;
6084 	case IBMVFC_HOST_ACTION_TGT_DEL:
6085 	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
6086 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
6087 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
6088 				tgt->job_step(tgt);
6089 				break;
6090 			}
6091 		}
6092 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue) {
6093 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
6094 				tgt->job_step(tgt);
6095 				break;
6096 			}
6097 		}
6098 
6099 		if (ibmvfc_dev_logo_to_do(vhost)) {
6100 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
6101 			return;
6102 		}
6103 
6104 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
6105 			if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
6106 				tgt_dbg(tgt, "Deleting rport\n");
6107 				rport = tgt->rport;
6108 				tgt->rport = NULL;
6109 				list_del(&tgt->queue);
6110 				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
6111 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
6112 				if (rport)
6113 					fc_remote_port_delete(rport);
6114 				timer_delete_sync(&tgt->timer);
6115 				kref_put(&tgt->kref, ibmvfc_release_tgt);
6116 				return;
6117 			} else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
6118 				tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
6119 				rport = tgt->rport;
6120 				tgt->rport = NULL;
6121 				tgt->init_retries = 0;
6122 				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
6123 
6124 				/*
6125 				 * If fast fail is enabled, we wait for it to fire and then clean up
6126 				 * the old port, since we expect the fast fail timer to clean up the
6127 				 * outstanding I/O faster than waiting for normal command timeouts.
6128 				 * However, if fast fail is disabled, any I/O outstanding to the
6129 				 * rport LUNs will stay outstanding indefinitely, since the EH handlers
6130 				 * won't get invoked for I/O's timing out. If this is a NPIV failover
6131 				 * scenario, the better alternative is to use the move login.
6132 				 */
6133 				if (rport && rport->fast_io_fail_tmo == -1)
6134 					tgt->move_login = 1;
6135 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
6136 				if (rport)
6137 					fc_remote_port_delete(rport);
6138 				return;
6139 			}
6140 		}
6141 
6142 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue) {
6143 			if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
6144 				tgt_dbg(tgt, "Deleting NVMe rport\n");
6145 				nvme_rport = tgt->nvme_remote_port;
6146 				list_del(&tgt->queue);
6147 				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
6148 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
6149 				if (nvme_rport)
6150 					ibmvfc_nvme_unregister_remoteport(tgt);
6151 				timer_delete_sync(&tgt->timer);
6152 				kref_put(&tgt->kref, ibmvfc_release_tgt);
6153 				return;
6154 			} else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
6155 				tgt_dbg(tgt, "Deleting NVMe rport with outstanding I/O\n");
6156 				nvme_rport = tgt->nvme_remote_port;
6157 				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
6158 				tgt->init_retries = 0;
6159 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
6160 				if (nvme_rport)
6161 					ibmvfc_nvme_unregister_remoteport(tgt);
6162 				return;
6163 			}
6164 		}
6165 
6166 		if (vhost->state == IBMVFC_INITIALIZING) {
6167 			if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
6168 				if (vhost->reinit) {
6169 					vhost->reinit = 0;
6170 					scsi_block_requests(vhost->host);
6171 					ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
6172 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6173 				} else {
6174 					ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
6175 					ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
6176 					wake_up(&vhost->init_wait_q);
6177 					schedule_work(&vhost->rport_add_work_q);
6178 					vhost->init_retries = 0;
6179 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6180 					scsi_unblock_requests(vhost->host);
6181 				}
6182 
6183 				return;
6184 			} else {
6185 				ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
6186 				vhost->job_step = ibmvfc_discover_targets;
6187 			}
6188 		} else {
6189 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
6190 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
6191 			scsi_unblock_requests(vhost->host);
6192 			wake_up(&vhost->init_wait_q);
6193 			return;
6194 		}
6195 		break;
6196 	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
6197 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
6198 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
6199 		ibmvfc_alloc_targets(vhost);
6200 		spin_lock_irqsave(vhost->host->host_lock, flags);
6201 		break;
6202 	case IBMVFC_HOST_ACTION_TGT_INIT:
6203 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
6204 			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
6205 				tgt->job_step(tgt);
6206 				break;
6207 			}
6208 		}
6209 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue) {
6210 			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
6211 				tgt->job_step(tgt);
6212 				break;
6213 			}
6214 		}
6215 
6216 		if (!ibmvfc_dev_init_to_do(vhost))
6217 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
6218 		break;
6219 	default:
6220 		break;
6221 	}
6222 
6223 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
6224 }
6225 
6226 /**
6227  * ibmvfc_work - Do task level work
6228  * @data:		ibmvfc host struct
6229  *
6230  * Returns:
6231  *	zero
6232  **/
ibmvfc_work(void * data)6233 static int ibmvfc_work(void *data)
6234 {
6235 	struct ibmvfc_host *vhost = data;
6236 	int rc;
6237 
6238 	set_user_nice(current, MIN_NICE);
6239 
6240 	while (1) {
6241 		rc = wait_event_interruptible(vhost->work_wait_q,
6242 					      ibmvfc_work_to_do(vhost));
6243 
6244 		BUG_ON(rc);
6245 
6246 		if (kthread_should_stop())
6247 			break;
6248 
6249 		ibmvfc_do_work(vhost);
6250 	}
6251 
6252 	ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
6253 	return 0;
6254 }
6255 
6256 /**
6257  * ibmvfc_alloc_queue - Allocate queue
6258  * @vhost:	ibmvfc host struct
6259  * @queue:	ibmvfc queue to allocate
6260  * @fmt:	queue format to allocate
6261  *
6262  * Returns:
6263  *	0 on success / non-zero on failure
6264  **/
ibmvfc_alloc_queue(struct ibmvfc_host * vhost,struct ibmvfc_queue * queue,enum ibmvfc_msg_fmt fmt)6265 static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
6266 			      struct ibmvfc_queue *queue,
6267 			      enum ibmvfc_msg_fmt fmt)
6268 {
6269 	struct device *dev = vhost->dev;
6270 	size_t fmt_size;
6271 
6272 	ENTER;
6273 	spin_lock_init(&queue->_lock);
6274 	queue->q_lock = &queue->_lock;
6275 
6276 	switch (fmt) {
6277 	case IBMVFC_CRQ_FMT:
6278 		fmt_size = sizeof(*queue->msgs.crq);
6279 		queue->total_depth = scsi_qdepth + IBMVFC_NUM_INTERNAL_REQ;
6280 		queue->evt_depth = scsi_qdepth;
6281 		queue->reserved_depth = IBMVFC_NUM_INTERNAL_REQ;
6282 		break;
6283 	case IBMVFC_ASYNC_FMT:
6284 		fmt_size = sizeof(*queue->msgs.async);
6285 		break;
6286 	case IBMVFC_SUB_CRQ_FMT:
6287 		fmt_size = sizeof(*queue->msgs.scrq);
6288 		/* We need one extra event for Cancel Commands */
6289 		queue->total_depth = scsi_qdepth + IBMVFC_NUM_INTERNAL_SUBQ_REQ;
6290 		queue->evt_depth = scsi_qdepth;
6291 		queue->reserved_depth = IBMVFC_NUM_INTERNAL_SUBQ_REQ;
6292 		break;
6293 	default:
6294 		dev_warn(dev, "Unknown command/response queue message format: %d\n", fmt);
6295 		return -EINVAL;
6296 	}
6297 
6298 	queue->fmt = fmt;
6299 	if (ibmvfc_init_event_pool(vhost, queue)) {
6300 		dev_err(dev, "Couldn't initialize event pool.\n");
6301 		return -ENOMEM;
6302 	}
6303 
6304 	queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
6305 	if (!queue->msgs.handle)
6306 		return -ENOMEM;
6307 
6308 	queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
6309 					  DMA_BIDIRECTIONAL);
6310 
6311 	if (dma_mapping_error(dev, queue->msg_token)) {
6312 		free_page((unsigned long)queue->msgs.handle);
6313 		queue->msgs.handle = NULL;
6314 		return -ENOMEM;
6315 	}
6316 
6317 	queue->cur = 0;
6318 	queue->size = PAGE_SIZE / fmt_size;
6319 
6320 	queue->vhost = vhost;
6321 	return 0;
6322 }
6323 
6324 /**
6325  * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
6326  * @vhost:	ibmvfc host struct
6327  *
6328  * Allocates a page for messages, maps it for dma, and registers
6329  * the crq with the hypervisor.
6330  *
6331  * Return value:
6332  *	zero on success / other on failure
6333  **/
ibmvfc_init_crq(struct ibmvfc_host * vhost)6334 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
6335 {
6336 	int rc, retrc = -ENOMEM;
6337 	struct device *dev = vhost->dev;
6338 	struct vio_dev *vdev = to_vio_dev(dev);
6339 	struct ibmvfc_queue *crq = &vhost->crq;
6340 
6341 	ENTER;
6342 	if (ibmvfc_alloc_queue(vhost, crq, IBMVFC_CRQ_FMT))
6343 		return -ENOMEM;
6344 
6345 	retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
6346 					crq->msg_token, PAGE_SIZE);
6347 
6348 	if (rc == H_RESOURCE)
6349 		/* maybe kexecing and resource is busy. try a reset */
6350 		retrc = rc = ibmvfc_reset_crq(vhost);
6351 
6352 	if (rc == H_CLOSED)
6353 		dev_warn(dev, "Partner adapter not ready\n");
6354 	else if (rc) {
6355 		dev_warn(dev, "Error %d opening adapter\n", rc);
6356 		goto reg_crq_failed;
6357 	}
6358 
6359 	retrc = 0;
6360 
6361 	tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
6362 
6363 	if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
6364 		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
6365 		goto req_irq_failed;
6366 	}
6367 
6368 	if ((rc = vio_enable_interrupts(vdev))) {
6369 		dev_err(dev, "Error %d enabling interrupts\n", rc);
6370 		goto req_irq_failed;
6371 	}
6372 
6373 	LEAVE;
6374 	return retrc;
6375 
6376 req_irq_failed:
6377 	tasklet_kill(&vhost->tasklet);
6378 	do {
6379 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
6380 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6381 reg_crq_failed:
6382 	ibmvfc_free_queue(vhost, crq);
6383 	return retrc;
6384 }
6385 
ibmvfc_register_channel(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels,int index)6386 static int ibmvfc_register_channel(struct ibmvfc_host *vhost,
6387 				   struct ibmvfc_channels *channels,
6388 				   int index)
6389 {
6390 	struct device *dev = vhost->dev;
6391 	struct vio_dev *vdev = to_vio_dev(dev);
6392 	struct ibmvfc_queue *scrq = &channels->scrqs[index];
6393 	int rc = -ENOMEM;
6394 
6395 	ENTER;
6396 
6397 	rc = h_reg_sub_crq(vdev->unit_address, scrq->msg_token, PAGE_SIZE,
6398 			   &scrq->cookie, &scrq->hw_irq);
6399 
6400 	/* H_CLOSED indicates successful register, but no CRQ partner */
6401 	if (rc && rc != H_CLOSED) {
6402 		dev_warn(dev, "Error registering sub-crq: %d\n", rc);
6403 		if (rc == H_PARAMETER)
6404 			dev_warn_once(dev, "Firmware may not support MQ\n");
6405 		goto reg_failed;
6406 	}
6407 
6408 	scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
6409 
6410 	if (!scrq->irq) {
6411 		rc = -EINVAL;
6412 		dev_err(dev, "Error mapping sub-crq[%d] irq\n", index);
6413 		goto irq_failed;
6414 	}
6415 
6416 	switch (channels->protocol) {
6417 	case IBMVFC_PROTO_SCSI:
6418 		snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-scsi%d",
6419 			 vdev->unit_address, index);
6420 		scrq->handler = ibmvfc_interrupt_mq;
6421 		break;
6422 	case IBMVFC_PROTO_NVME:
6423 		snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-nvmf%d",
6424 			 vdev->unit_address, index);
6425 		scrq->handler = ibmvfc_interrupt_mq;
6426 		break;
6427 	default:
6428 		dev_err(dev, "Unknown channel protocol (%d)\n",
6429 			channels->protocol);
6430 		goto irq_failed;
6431 	}
6432 
6433 	rc = request_irq(scrq->irq, scrq->handler, 0, scrq->name, scrq);
6434 
6435 	if (rc) {
6436 		dev_err(dev, "Couldn't register sub-crq[%d] irq\n", index);
6437 		irq_dispose_mapping(scrq->irq);
6438 		goto irq_failed;
6439 	}
6440 
6441 	scrq->hwq_id = index;
6442 
6443 	LEAVE;
6444 	return 0;
6445 
6446 irq_failed:
6447 	do {
6448 		rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address, scrq->cookie);
6449 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6450 reg_failed:
6451 	LEAVE;
6452 	return rc;
6453 }
6454 
ibmvfc_deregister_channel(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels,int index)6455 static void ibmvfc_deregister_channel(struct ibmvfc_host *vhost,
6456 				      struct ibmvfc_channels *channels,
6457 				      int index)
6458 {
6459 	struct device *dev = vhost->dev;
6460 	struct vio_dev *vdev = to_vio_dev(dev);
6461 	struct ibmvfc_queue *scrq = &channels->scrqs[index];
6462 	long rc;
6463 
6464 	ENTER;
6465 
6466 	free_irq(scrq->irq, scrq);
6467 	irq_dispose_mapping(scrq->irq);
6468 	scrq->irq = 0;
6469 
6470 	do {
6471 		rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address,
6472 					scrq->cookie);
6473 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6474 
6475 	if (rc)
6476 		dev_err(dev, "Failed to free sub-crq[%d]: rc=%ld\n", index, rc);
6477 
6478 	/* Clean out the queue */
6479 	memset(scrq->msgs.crq, 0, PAGE_SIZE);
6480 	scrq->cur = 0;
6481 
6482 	LEAVE;
6483 }
6484 
ibmvfc_reg_sub_crqs(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels)6485 static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *vhost,
6486 				struct ibmvfc_channels *channels)
6487 {
6488 	int i, j;
6489 
6490 	ENTER;
6491 	if (!vhost->mq_enabled || !channels->scrqs)
6492 		return;
6493 
6494 	for (i = 0; i < channels->max_queues; i++) {
6495 		if (ibmvfc_register_channel(vhost, channels, i)) {
6496 			for (j = i; j > 0; j--)
6497 				ibmvfc_deregister_channel(vhost, channels, j - 1);
6498 			vhost->do_enquiry = 0;
6499 			return;
6500 		}
6501 	}
6502 
6503 	LEAVE;
6504 }
6505 
ibmvfc_dereg_sub_crqs(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels)6506 static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *vhost,
6507 				  struct ibmvfc_channels *channels)
6508 {
6509 	int i;
6510 
6511 	ENTER;
6512 	if (!vhost->mq_enabled || !channels->scrqs)
6513 		return;
6514 
6515 	for (i = 0; i < channels->max_queues; i++)
6516 		ibmvfc_deregister_channel(vhost, channels, i);
6517 
6518 	LEAVE;
6519 }
6520 
ibmvfc_alloc_channels(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels)6521 static int ibmvfc_alloc_channels(struct ibmvfc_host *vhost,
6522 				 struct ibmvfc_channels *channels)
6523 {
6524 	struct ibmvfc_queue *scrq;
6525 	int i, j;
6526 	int rc = 0;
6527 
6528 	channels->scrqs = kzalloc_objs(*channels->scrqs, channels->max_queues);
6529 	if (!channels->scrqs)
6530 		return -ENOMEM;
6531 
6532 	for (i = 0; i < channels->max_queues; i++) {
6533 		scrq = &channels->scrqs[i];
6534 		rc = ibmvfc_alloc_queue(vhost, scrq, IBMVFC_SUB_CRQ_FMT);
6535 		if (rc) {
6536 			for (j = i; j > 0; j--) {
6537 				scrq = &channels->scrqs[j - 1];
6538 				ibmvfc_free_queue(vhost, scrq);
6539 			}
6540 			kfree(channels->scrqs);
6541 			channels->scrqs = NULL;
6542 			channels->active_queues = 0;
6543 			return rc;
6544 		}
6545 	}
6546 
6547 	return rc;
6548 }
6549 
ibmvfc_init_sub_crqs(struct ibmvfc_host * vhost)6550 static void ibmvfc_init_sub_crqs(struct ibmvfc_host *vhost)
6551 {
6552 	ENTER;
6553 	if (!vhost->mq_enabled)
6554 		return;
6555 
6556 	if (ibmvfc_alloc_channels(vhost, &vhost->scsi_scrqs)) {
6557 		vhost->do_enquiry = 0;
6558 		vhost->mq_enabled = 0;
6559 		return;
6560 	}
6561 
6562 	ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
6563 
6564 	if (vhost->nvme_enabled) {
6565 		if (ibmvfc_alloc_channels(vhost, &vhost->nvme_scrqs))
6566 			vhost->nvme_enabled = 0;
6567 		else
6568 			ibmvfc_reg_sub_crqs(vhost, &vhost->nvme_scrqs);
6569 	}
6570 
6571 	LEAVE;
6572 }
6573 
ibmvfc_release_channels(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels)6574 static void ibmvfc_release_channels(struct ibmvfc_host *vhost,
6575 				    struct ibmvfc_channels *channels)
6576 {
6577 	struct ibmvfc_queue *scrq;
6578 	int i;
6579 
6580 	if (channels->scrqs) {
6581 		for (i = 0; i < channels->max_queues; i++) {
6582 			scrq = &channels->scrqs[i];
6583 			ibmvfc_free_queue(vhost, scrq);
6584 		}
6585 
6586 		kfree(channels->scrqs);
6587 		channels->scrqs = NULL;
6588 		channels->active_queues = 0;
6589 	}
6590 }
6591 
ibmvfc_release_sub_crqs(struct ibmvfc_host * vhost)6592 static void ibmvfc_release_sub_crqs(struct ibmvfc_host *vhost)
6593 {
6594 	ENTER;
6595 	if (!vhost->scsi_scrqs.scrqs)
6596 		return;
6597 
6598 	ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
6599 	ibmvfc_release_channels(vhost, &vhost->scsi_scrqs);
6600 
6601 	ibmvfc_dereg_sub_crqs(vhost, &vhost->nvme_scrqs);
6602 	ibmvfc_release_channels(vhost, &vhost->nvme_scrqs);
6603 	LEAVE;
6604 }
6605 
ibmvfc_free_disc_buf(struct device * dev,struct ibmvfc_channels * channels)6606 static void ibmvfc_free_disc_buf(struct device *dev, struct ibmvfc_channels *channels)
6607 {
6608 	dma_free_coherent(dev, channels->disc_buf_sz, channels->disc_buf,
6609 			  channels->disc_buf_dma);
6610 }
6611 
6612 /**
6613  * ibmvfc_free_mem - Free memory for vhost
6614  * @vhost:	ibmvfc host struct
6615  *
6616  * Return value:
6617  * 	none
6618  **/
ibmvfc_free_mem(struct ibmvfc_host * vhost)6619 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
6620 {
6621 	struct ibmvfc_queue *async_q = &vhost->async_crq;
6622 
6623 	ENTER;
6624 	mempool_destroy(vhost->tgt_pool);
6625 	kfree(vhost->trace);
6626 	ibmvfc_free_disc_buf(vhost->dev, &vhost->scsi_scrqs);
6627 	ibmvfc_free_disc_buf(vhost->dev, &vhost->nvme_scrqs);
6628 	dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
6629 			  vhost->login_buf, vhost->login_buf_dma);
6630 	dma_free_coherent(vhost->dev, sizeof(*vhost->channel_setup_buf),
6631 			  vhost->channel_setup_buf, vhost->channel_setup_dma);
6632 	dma_pool_destroy(vhost->sg_pool);
6633 	ibmvfc_free_queue(vhost, async_q);
6634 	LEAVE;
6635 }
6636 
ibmvfc_alloc_disc_buf(struct device * dev,struct ibmvfc_channels * channels)6637 static int ibmvfc_alloc_disc_buf(struct device *dev, struct ibmvfc_channels *channels)
6638 {
6639 	channels->disc_buf_sz = sizeof(*channels->disc_buf) * max_targets;
6640 	channels->disc_buf = dma_alloc_coherent(dev, channels->disc_buf_sz,
6641 					     &channels->disc_buf_dma, GFP_KERNEL);
6642 
6643 	if (!channels->disc_buf) {
6644 		dev_err(dev, "Couldn't allocate %s Discover Targets buffer\n",
6645 			(channels->protocol == IBMVFC_PROTO_SCSI) ? "SCSI" : "NVMe");
6646 		return -ENOMEM;
6647 	}
6648 
6649 	return 0;
6650 }
6651 
6652 /**
6653  * ibmvfc_alloc_mem - Allocate memory for vhost
6654  * @vhost:	ibmvfc host struct
6655  *
6656  * Return value:
6657  * 	0 on success / non-zero on failure
6658  **/
ibmvfc_alloc_mem(struct ibmvfc_host * vhost)6659 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
6660 {
6661 	struct ibmvfc_queue *async_q = &vhost->async_crq;
6662 	struct device *dev = vhost->dev;
6663 
6664 	ENTER;
6665 	if (ibmvfc_alloc_queue(vhost, async_q, IBMVFC_ASYNC_FMT)) {
6666 		dev_err(dev, "Couldn't allocate/map async queue.\n");
6667 		goto nomem;
6668 	}
6669 
6670 	vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
6671 					 SG_ALL * sizeof(struct srp_direct_buf),
6672 					 sizeof(struct srp_direct_buf), 0);
6673 
6674 	if (!vhost->sg_pool) {
6675 		dev_err(dev, "Failed to allocate sg pool\n");
6676 		goto unmap_async_crq;
6677 	}
6678 
6679 	vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
6680 					      &vhost->login_buf_dma, GFP_KERNEL);
6681 
6682 	if (!vhost->login_buf) {
6683 		dev_err(dev, "Couldn't allocate NPIV login buffer\n");
6684 		goto free_sg_pool;
6685 	}
6686 
6687 	if (ibmvfc_alloc_disc_buf(dev, &vhost->scsi_scrqs))
6688 		goto free_login_buffer;
6689 
6690 	if (ibmvfc_alloc_disc_buf(dev, &vhost->nvme_scrqs))
6691 		goto free_scsi_disc_buffer;
6692 
6693 	vhost->trace = kzalloc_objs(struct ibmvfc_trace_entry,
6694 				    IBMVFC_NUM_TRACE_ENTRIES);
6695 	atomic_set(&vhost->trace_index, -1);
6696 
6697 	if (!vhost->trace)
6698 		goto free_nvme_disc_buffer;
6699 
6700 	vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
6701 						      sizeof(struct ibmvfc_target));
6702 
6703 	if (!vhost->tgt_pool) {
6704 		dev_err(dev, "Couldn't allocate target memory pool\n");
6705 		goto free_trace;
6706 	}
6707 
6708 	vhost->channel_setup_buf = dma_alloc_coherent(dev, sizeof(*vhost->channel_setup_buf),
6709 						      &vhost->channel_setup_dma,
6710 						      GFP_KERNEL);
6711 
6712 	if (!vhost->channel_setup_buf) {
6713 		dev_err(dev, "Couldn't allocate Channel Setup buffer\n");
6714 		goto free_tgt_pool;
6715 	}
6716 
6717 	LEAVE;
6718 	return 0;
6719 
6720 free_tgt_pool:
6721 	mempool_destroy(vhost->tgt_pool);
6722 free_trace:
6723 	kfree(vhost->trace);
6724 free_nvme_disc_buffer:
6725 	ibmvfc_free_disc_buf(dev, &vhost->nvme_scrqs);
6726 free_scsi_disc_buffer:
6727 	ibmvfc_free_disc_buf(dev, &vhost->scsi_scrqs);
6728 free_login_buffer:
6729 	dma_free_coherent(dev, sizeof(*vhost->login_buf),
6730 			  vhost->login_buf, vhost->login_buf_dma);
6731 free_sg_pool:
6732 	dma_pool_destroy(vhost->sg_pool);
6733 unmap_async_crq:
6734 	ibmvfc_free_queue(vhost, async_q);
6735 nomem:
6736 	LEAVE;
6737 	return -ENOMEM;
6738 }
6739 
6740 /**
6741  * ibmvfc_rport_add_thread - Worker thread for rport adds
6742  * @work:	work struct
6743  *
6744  **/
ibmvfc_rport_add_thread(struct work_struct * work)6745 static void ibmvfc_rport_add_thread(struct work_struct *work)
6746 {
6747 	struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
6748 						 rport_add_work_q);
6749 	struct ibmvfc_target *tgt;
6750 	struct fc_rport *rport;
6751 	struct nvme_fc_remote_port *nvme_rport;
6752 	unsigned long flags;
6753 	int did_work;
6754 
6755 	ENTER;
6756 	spin_lock_irqsave(vhost->host->host_lock, flags);
6757 	do {
6758 		did_work = 0;
6759 		if (vhost->state != IBMVFC_ACTIVE)
6760 			break;
6761 
6762 		list_for_each_entry(tgt, &vhost->scsi_scrqs.targets, queue) {
6763 			if (tgt->add_rport) {
6764 				did_work = 1;
6765 				tgt->add_rport = 0;
6766 				kref_get(&tgt->kref);
6767 				rport = tgt->rport;
6768 				if (!rport) {
6769 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6770 					ibmvfc_tgt_add_rport(tgt);
6771 				} else if (get_device(&rport->dev)) {
6772 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6773 					tgt_dbg(tgt, "Setting rport roles\n");
6774 					fc_remote_port_rolechg(rport, tgt->ids.roles);
6775 					put_device(&rport->dev);
6776 				} else {
6777 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6778 				}
6779 
6780 				kref_put(&tgt->kref, ibmvfc_release_tgt);
6781 				spin_lock_irqsave(vhost->host->host_lock, flags);
6782 				break;
6783 			}
6784 		}
6785 
6786 		list_for_each_entry(tgt, &vhost->nvme_scrqs.targets, queue) {
6787 			if (tgt->add_rport) {
6788 				did_work = 1;
6789 				tgt->add_rport = 0;
6790 				kref_get(&tgt->kref);
6791 				nvme_rport = tgt->nvme_remote_port;
6792 				if (!nvme_rport) {
6793 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6794 					ibmvfc_tgt_add_nvme_rport(tgt);
6795 				} else {
6796 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6797 					if (IS_ENABLED(CONFIG_NVME_FC))
6798 						nvme_fc_rescan_remoteport(nvme_rport);
6799 				}
6800 
6801 				kref_put(&tgt->kref, ibmvfc_release_tgt);
6802 				spin_lock_irqsave(vhost->host->host_lock, flags);
6803 				break;
6804 			}
6805 		}
6806 	} while (did_work);
6807 
6808 	if (vhost->state == IBMVFC_ACTIVE)
6809 		vhost->scan_complete = 1;
6810 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
6811 	LEAVE;
6812 }
6813 
6814 /**
6815  * ibmvfc_probe - Adapter hot plug add entry point
6816  * @vdev:	vio device struct
6817  * @id:	vio device id struct
6818  *
6819  * Return value:
6820  * 	0 on success / non-zero on failure
6821  **/
ibmvfc_probe(struct vio_dev * vdev,const struct vio_device_id * id)6822 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
6823 {
6824 	struct ibmvfc_host *vhost;
6825 	struct Scsi_Host *shost;
6826 	struct device *dev = &vdev->dev;
6827 	int rc = -ENOMEM;
6828 	unsigned int online_cpus = num_online_cpus();
6829 	unsigned int max_scsi_queues = min_t(unsigned int, IBMVFC_MAX_SCSI_QUEUES, online_cpus);
6830 	unsigned int max_nvme_queues = min_t(unsigned int, IBMVFC_MAX_NVME_QUEUES, online_cpus);
6831 
6832 	ENTER;
6833 	shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
6834 	if (!shost) {
6835 		dev_err(dev, "Couldn't allocate host data\n");
6836 		goto out;
6837 	}
6838 
6839 	shost->transportt = ibmvfc_transport_template;
6840 	shost->can_queue = scsi_qdepth;
6841 	shost->max_lun = max_lun;
6842 	shost->max_id = max_targets;
6843 	shost->max_sectors = max_sectors;
6844 	shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
6845 	shost->unique_id = shost->host_no;
6846 	shost->nr_hw_queues = mq_enabled ? min(max_scsi_queues, nr_scsi_hw_queues) : 1;
6847 
6848 	vhost = shost_priv(shost);
6849 	INIT_LIST_HEAD(&vhost->scsi_scrqs.targets);
6850 	INIT_LIST_HEAD(&vhost->nvme_scrqs.targets);
6851 	INIT_LIST_HEAD(&vhost->purge);
6852 	sprintf(vhost->name, IBMVFC_NAME);
6853 	vhost->host = shost;
6854 	vhost->dev = dev;
6855 	vhost->partition_number = -1;
6856 	vhost->log_level = log_level;
6857 	vhost->task_set = 1;
6858 
6859 	vhost->mq_enabled = mq_enabled;
6860 	vhost->scsi_scrqs.desired_queues = min(shost->nr_hw_queues, nr_scsi_channels);
6861 	vhost->scsi_scrqs.max_queues = shost->nr_hw_queues;
6862 	vhost->scsi_scrqs.protocol = IBMVFC_PROTO_SCSI;
6863 	vhost->using_channels = 0;
6864 	vhost->do_enquiry = 1;
6865 	vhost->nvme_enabled = mq_enabled ? nvme_enabled : 0;
6866 	vhost->nvme_scrqs.desired_queues = min(max_nvme_queues, nr_nvme_channels);
6867 	vhost->nvme_scrqs.max_queues = max_nvme_queues;
6868 	vhost->nvme_scrqs.protocol = IBMVFC_PROTO_NVME;
6869 	vhost->scan_timeout = 0;
6870 
6871 	strcpy(vhost->partition_name, "UNKNOWN");
6872 	init_waitqueue_head(&vhost->work_wait_q);
6873 	init_waitqueue_head(&vhost->init_wait_q);
6874 	INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
6875 	mutex_init(&vhost->passthru_mutex);
6876 
6877 	if ((rc = ibmvfc_alloc_mem(vhost)))
6878 		goto free_scsi_host;
6879 
6880 	vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
6881 					 shost->host_no);
6882 
6883 	if (IS_ERR(vhost->work_thread)) {
6884 		dev_err(dev, "Couldn't create kernel thread: %ld\n",
6885 			PTR_ERR(vhost->work_thread));
6886 		rc = PTR_ERR(vhost->work_thread);
6887 		goto free_host_mem;
6888 	}
6889 
6890 	if ((rc = ibmvfc_init_crq(vhost))) {
6891 		dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
6892 		goto kill_kthread;
6893 	}
6894 
6895 	if ((rc = scsi_add_host(shost, dev)))
6896 		goto release_crq;
6897 
6898 	fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
6899 
6900 	if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
6901 					   &ibmvfc_trace_attr))) {
6902 		dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
6903 		goto remove_shost;
6904 	}
6905 
6906 	ibmvfc_init_sub_crqs(vhost);
6907 
6908 	dev_set_drvdata(dev, vhost);
6909 	spin_lock(&ibmvfc_driver_lock);
6910 	list_add_tail(&vhost->queue, &ibmvfc_head);
6911 	spin_unlock(&ibmvfc_driver_lock);
6912 
6913 	ibmvfc_send_crq_init(vhost);
6914 	scsi_scan_host(shost);
6915 	return 0;
6916 
6917 remove_shost:
6918 	scsi_remove_host(shost);
6919 release_crq:
6920 	ibmvfc_release_crq_queue(vhost);
6921 kill_kthread:
6922 	kthread_stop(vhost->work_thread);
6923 free_host_mem:
6924 	ibmvfc_free_mem(vhost);
6925 free_scsi_host:
6926 	scsi_host_put(shost);
6927 out:
6928 	LEAVE;
6929 	return rc;
6930 }
6931 
6932 /**
6933  * ibmvfc_remove - Adapter hot plug remove entry point
6934  * @vdev:	vio device struct
6935  *
6936  * Return value:
6937  * 	0
6938  **/
ibmvfc_remove(struct vio_dev * vdev)6939 static void ibmvfc_remove(struct vio_dev *vdev)
6940 {
6941 	struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
6942 	LIST_HEAD(purge);
6943 	unsigned long flags;
6944 
6945 	ENTER;
6946 	ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
6947 
6948 	spin_lock_irqsave(vhost->host->host_lock, flags);
6949 	ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
6950 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
6951 
6952 	ibmvfc_wait_while_resetting(vhost);
6953 	kthread_stop(vhost->work_thread);
6954 	flush_work(&vhost->rport_add_work_q);
6955 	fc_remove_host(vhost->host);
6956 	scsi_remove_host(vhost->host);
6957 
6958 	spin_lock_irqsave(vhost->host->host_lock, flags);
6959 	ibmvfc_purge_requests(vhost, DID_ERROR);
6960 	list_splice_init(&vhost->purge, &purge);
6961 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
6962 	ibmvfc_complete_purge(&purge);
6963 	ibmvfc_release_sub_crqs(vhost);
6964 	ibmvfc_release_crq_queue(vhost);
6965 
6966 	ibmvfc_free_mem(vhost);
6967 	spin_lock(&ibmvfc_driver_lock);
6968 	list_del(&vhost->queue);
6969 	spin_unlock(&ibmvfc_driver_lock);
6970 	scsi_host_put(vhost->host);
6971 	LEAVE;
6972 }
6973 
6974 /**
6975  * ibmvfc_resume - Resume from suspend
6976  * @dev:	device struct
6977  *
6978  * We may have lost an interrupt across suspend/resume, so kick the
6979  * interrupt handler
6980  *
6981  */
ibmvfc_resume(struct device * dev)6982 static int ibmvfc_resume(struct device *dev)
6983 {
6984 	unsigned long flags;
6985 	struct ibmvfc_host *vhost = dev_get_drvdata(dev);
6986 	struct vio_dev *vdev = to_vio_dev(dev);
6987 
6988 	spin_lock_irqsave(vhost->host->host_lock, flags);
6989 	vio_disable_interrupts(vdev);
6990 	tasklet_schedule(&vhost->tasklet);
6991 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
6992 	return 0;
6993 }
6994 
6995 /**
6996  * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
6997  * @vdev:	vio device struct
6998  *
6999  * Return value:
7000  *	Number of bytes the driver will need to DMA map at the same time in
7001  *	order to perform well.
7002  */
ibmvfc_get_desired_dma(struct vio_dev * vdev)7003 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
7004 {
7005 	unsigned long pool_dma;
7006 
7007 	pool_dma = (IBMVFC_MAX_SCSI_QUEUES * scsi_qdepth) * sizeof(union ibmvfc_iu);
7008 	return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
7009 }
7010 
7011 static const struct vio_device_id ibmvfc_device_table[] = {
7012 	{"fcp", "IBM,vfc-client"},
7013 	{ "", "" }
7014 };
7015 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
7016 
7017 static const struct dev_pm_ops ibmvfc_pm_ops = {
7018 	.resume = ibmvfc_resume
7019 };
7020 
7021 static struct vio_driver ibmvfc_driver = {
7022 	.id_table = ibmvfc_device_table,
7023 	.probe = ibmvfc_probe,
7024 	.remove = ibmvfc_remove,
7025 	.get_desired_dma = ibmvfc_get_desired_dma,
7026 	.name = IBMVFC_NAME,
7027 	.pm = &ibmvfc_pm_ops,
7028 };
7029 
7030 static struct fc_function_template ibmvfc_transport_functions = {
7031 	.show_host_fabric_name = 1,
7032 	.show_host_node_name = 1,
7033 	.show_host_port_name = 1,
7034 	.show_host_supported_classes = 1,
7035 	.show_host_port_type = 1,
7036 	.show_host_port_id = 1,
7037 	.show_host_maxframe_size = 1,
7038 
7039 	.get_host_port_state = ibmvfc_get_host_port_state,
7040 	.show_host_port_state = 1,
7041 
7042 	.get_host_speed = ibmvfc_get_host_speed,
7043 	.show_host_speed = 1,
7044 
7045 	.issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
7046 	.terminate_rport_io = ibmvfc_terminate_rport_io,
7047 
7048 	.show_rport_maxframe_size = 1,
7049 	.show_rport_supported_classes = 1,
7050 
7051 	.set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
7052 	.show_rport_dev_loss_tmo = 1,
7053 
7054 	.get_starget_node_name = ibmvfc_get_starget_node_name,
7055 	.show_starget_node_name = 1,
7056 
7057 	.get_starget_port_name = ibmvfc_get_starget_port_name,
7058 	.show_starget_port_name = 1,
7059 
7060 	.get_starget_port_id = ibmvfc_get_starget_port_id,
7061 	.show_starget_port_id = 1,
7062 
7063 	.max_bsg_segments = 1,
7064 	.bsg_request = ibmvfc_bsg_request,
7065 	.bsg_timeout = ibmvfc_bsg_timeout,
7066 };
7067 
7068 /**
7069  * ibmvfc_module_init - Initialize the ibmvfc module
7070  *
7071  * Return value:
7072  * 	0 on success / other on failure
7073  **/
ibmvfc_module_init(void)7074 static int __init ibmvfc_module_init(void)
7075 {
7076 	int min_max_sectors = PAGE_SIZE >> 9;
7077 	int rc;
7078 
7079 	if (!firmware_has_feature(FW_FEATURE_VIO))
7080 		return -ENODEV;
7081 
7082 	printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
7083 	       IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
7084 
7085 	/*
7086 	 * Range check the max_sectors module parameter. The upper bounds is
7087 	 * implicity checked since the parameter is a ushort.
7088 	 */
7089 	if (max_sectors < min_max_sectors) {
7090 		printk(KERN_ERR IBMVFC_NAME ": max_sectors must be at least %d.\n",
7091 			min_max_sectors);
7092 		max_sectors = min_max_sectors;
7093 	}
7094 
7095 	ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
7096 	if (!ibmvfc_transport_template)
7097 		return -ENOMEM;
7098 
7099 	rc = vio_register_driver(&ibmvfc_driver);
7100 	if (rc)
7101 		fc_release_transport(ibmvfc_transport_template);
7102 	return rc;
7103 }
7104 
7105 /**
7106  * ibmvfc_module_exit - Teardown the ibmvfc module
7107  *
7108  * Return value:
7109  * 	nothing
7110  **/
ibmvfc_module_exit(void)7111 static void __exit ibmvfc_module_exit(void)
7112 {
7113 	vio_unregister_driver(&ibmvfc_driver);
7114 	fc_release_transport(ibmvfc_transport_template);
7115 }
7116 
7117 module_init(ibmvfc_module_init);
7118 module_exit(ibmvfc_module_exit);
7119