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