xref: /linux/drivers/scsi/ibmvscsi/ibmvfc.c (revision 3ed7df085225ea8736b80d1e1a247a40d91281c8)
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 = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
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 	del_timer(&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 = from_timer(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 		del_timer(&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 int ibmvfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
1964 {
1965 	struct ibmvfc_host *vhost = shost_priv(shost);
1966 	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1967 	struct ibmvfc_cmd *vfc_cmd;
1968 	struct ibmvfc_fcp_cmd_iu *iu;
1969 	struct ibmvfc_event *evt;
1970 	u32 tag_and_hwq = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
1971 	u16 hwq = blk_mq_unique_tag_to_hwq(tag_and_hwq);
1972 	u16 scsi_channel;
1973 	int rc;
1974 
1975 	if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1976 	    unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1977 		cmnd->result = rc;
1978 		scsi_done(cmnd);
1979 		return 0;
1980 	}
1981 
1982 	cmnd->result = (DID_OK << 16);
1983 	if (vhost->using_channels) {
1984 		scsi_channel = hwq % vhost->scsi_scrqs.active_queues;
1985 		evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[scsi_channel]);
1986 		if (!evt)
1987 			return SCSI_MLQUEUE_HOST_BUSY;
1988 
1989 		evt->hwq = hwq % vhost->scsi_scrqs.active_queues;
1990 	} else {
1991 		evt = ibmvfc_get_event(&vhost->crq);
1992 		if (!evt)
1993 			return SCSI_MLQUEUE_HOST_BUSY;
1994 	}
1995 
1996 	ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1997 	evt->cmnd = cmnd;
1998 
1999 	vfc_cmd = ibmvfc_init_vfc_cmd(evt, cmnd->device);
2000 	iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
2001 
2002 	iu->xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
2003 	memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
2004 
2005 	if (cmnd->flags & SCMD_TAGGED) {
2006 		vfc_cmd->task_tag = cpu_to_be64(scsi_cmd_to_rq(cmnd)->tag);
2007 		iu->pri_task_attr = IBMVFC_SIMPLE_TASK;
2008 	}
2009 
2010 	vfc_cmd->correlation = cpu_to_be64((u64)evt);
2011 
2012 	if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
2013 		return ibmvfc_send_event(evt, vhost, 0);
2014 
2015 	ibmvfc_free_event(evt);
2016 	if (rc == -ENOMEM)
2017 		return SCSI_MLQUEUE_HOST_BUSY;
2018 
2019 	if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2020 		scmd_printk(KERN_ERR, cmnd,
2021 			    "Failed to map DMA buffer for command. rc=%d\n", rc);
2022 
2023 	cmnd->result = DID_ERROR << 16;
2024 	scsi_done(cmnd);
2025 	return 0;
2026 }
2027 
2028 /**
2029  * ibmvfc_sync_completion - Signal that a synchronous command has completed
2030  * @evt:	ibmvfc event struct
2031  *
2032  **/
ibmvfc_sync_completion(struct ibmvfc_event * evt)2033 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
2034 {
2035 	/* copy the response back */
2036 	if (evt->sync_iu)
2037 		*evt->sync_iu = *evt->xfer_iu;
2038 
2039 	complete(&evt->comp);
2040 }
2041 
2042 /**
2043  * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
2044  * @evt:	struct ibmvfc_event
2045  *
2046  **/
ibmvfc_bsg_timeout_done(struct ibmvfc_event * evt)2047 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
2048 {
2049 	struct ibmvfc_host *vhost = evt->vhost;
2050 
2051 	ibmvfc_free_event(evt);
2052 	vhost->aborting_passthru = 0;
2053 	dev_info(vhost->dev, "Passthru command cancelled\n");
2054 }
2055 
2056 /**
2057  * ibmvfc_bsg_timeout - Handle a BSG timeout
2058  * @job:	struct bsg_job that timed out
2059  *
2060  * Returns:
2061  *	0 on success / other on failure
2062  **/
ibmvfc_bsg_timeout(struct bsg_job * job)2063 static int ibmvfc_bsg_timeout(struct bsg_job *job)
2064 {
2065 	struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2066 	unsigned long port_id = (unsigned long)job->dd_data;
2067 	struct ibmvfc_event *evt;
2068 	struct ibmvfc_tmf *tmf;
2069 	unsigned long flags;
2070 	int rc;
2071 
2072 	ENTER;
2073 	spin_lock_irqsave(vhost->host->host_lock, flags);
2074 	if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
2075 		__ibmvfc_reset_host(vhost);
2076 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2077 		return 0;
2078 	}
2079 
2080 	vhost->aborting_passthru = 1;
2081 	evt = ibmvfc_get_reserved_event(&vhost->crq);
2082 	if (!evt) {
2083 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2084 		return -ENOMEM;
2085 	}
2086 
2087 	ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
2088 
2089 	tmf = &evt->iu.tmf;
2090 	memset(tmf, 0, sizeof(*tmf));
2091 	tmf->common.version = cpu_to_be32(1);
2092 	tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2093 	tmf->common.length = cpu_to_be16(sizeof(*tmf));
2094 	tmf->scsi_id = cpu_to_be64(port_id);
2095 	tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2096 	tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
2097 	rc = ibmvfc_send_event(evt, vhost, default_timeout);
2098 
2099 	if (rc != 0) {
2100 		vhost->aborting_passthru = 0;
2101 		dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
2102 		rc = -EIO;
2103 	} else
2104 		dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
2105 			 port_id);
2106 
2107 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2108 
2109 	LEAVE;
2110 	return rc;
2111 }
2112 
2113 /**
2114  * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
2115  * @vhost:		struct ibmvfc_host to send command
2116  * @port_id:	port ID to send command
2117  *
2118  * Returns:
2119  *	0 on success / other on failure
2120  **/
ibmvfc_bsg_plogi(struct ibmvfc_host * vhost,unsigned int port_id)2121 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
2122 {
2123 	struct ibmvfc_port_login *plogi;
2124 	struct ibmvfc_target *tgt;
2125 	struct ibmvfc_event *evt;
2126 	union ibmvfc_iu rsp_iu;
2127 	unsigned long flags;
2128 	int rc = 0, issue_login = 1;
2129 
2130 	ENTER;
2131 	spin_lock_irqsave(vhost->host->host_lock, flags);
2132 	list_for_each_entry(tgt, &vhost->targets, queue) {
2133 		if (tgt->scsi_id == port_id) {
2134 			issue_login = 0;
2135 			break;
2136 		}
2137 	}
2138 
2139 	if (!issue_login)
2140 		goto unlock_out;
2141 	if (unlikely((rc = ibmvfc_host_chkready(vhost))))
2142 		goto unlock_out;
2143 
2144 	evt = ibmvfc_get_reserved_event(&vhost->crq);
2145 	if (!evt) {
2146 		rc = -ENOMEM;
2147 		goto unlock_out;
2148 	}
2149 	ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2150 	plogi = &evt->iu.plogi;
2151 	memset(plogi, 0, sizeof(*plogi));
2152 	plogi->common.version = cpu_to_be32(1);
2153 	plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
2154 	plogi->common.length = cpu_to_be16(sizeof(*plogi));
2155 	plogi->scsi_id = cpu_to_be64(port_id);
2156 	evt->sync_iu = &rsp_iu;
2157 	init_completion(&evt->comp);
2158 
2159 	rc = ibmvfc_send_event(evt, vhost, default_timeout);
2160 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2161 
2162 	if (rc)
2163 		return -EIO;
2164 
2165 	wait_for_completion(&evt->comp);
2166 
2167 	if (rsp_iu.plogi.common.status)
2168 		rc = -EIO;
2169 
2170 	spin_lock_irqsave(vhost->host->host_lock, flags);
2171 	ibmvfc_free_event(evt);
2172 unlock_out:
2173 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2174 	LEAVE;
2175 	return rc;
2176 }
2177 
2178 /**
2179  * ibmvfc_bsg_request - Handle a BSG request
2180  * @job:	struct bsg_job to be executed
2181  *
2182  * Returns:
2183  *	0 on success / other on failure
2184  **/
ibmvfc_bsg_request(struct bsg_job * job)2185 static int ibmvfc_bsg_request(struct bsg_job *job)
2186 {
2187 	struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2188 	struct fc_rport *rport = fc_bsg_to_rport(job);
2189 	struct ibmvfc_passthru_mad *mad;
2190 	struct ibmvfc_event *evt;
2191 	union ibmvfc_iu rsp_iu;
2192 	unsigned long flags, port_id = -1;
2193 	struct fc_bsg_request *bsg_request = job->request;
2194 	struct fc_bsg_reply *bsg_reply = job->reply;
2195 	unsigned int code = bsg_request->msgcode;
2196 	int rc = 0, req_seg, rsp_seg, issue_login = 0;
2197 	u32 fc_flags, rsp_len;
2198 
2199 	ENTER;
2200 	bsg_reply->reply_payload_rcv_len = 0;
2201 	if (rport)
2202 		port_id = rport->port_id;
2203 
2204 	switch (code) {
2205 	case FC_BSG_HST_ELS_NOLOGIN:
2206 		port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
2207 			(bsg_request->rqst_data.h_els.port_id[1] << 8) |
2208 			bsg_request->rqst_data.h_els.port_id[2];
2209 		fallthrough;
2210 	case FC_BSG_RPT_ELS:
2211 		fc_flags = IBMVFC_FC_ELS;
2212 		break;
2213 	case FC_BSG_HST_CT:
2214 		issue_login = 1;
2215 		port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
2216 			(bsg_request->rqst_data.h_ct.port_id[1] << 8) |
2217 			bsg_request->rqst_data.h_ct.port_id[2];
2218 		fallthrough;
2219 	case FC_BSG_RPT_CT:
2220 		fc_flags = IBMVFC_FC_CT_IU;
2221 		break;
2222 	default:
2223 		return -ENOTSUPP;
2224 	}
2225 
2226 	if (port_id == -1)
2227 		return -EINVAL;
2228 	if (!mutex_trylock(&vhost->passthru_mutex))
2229 		return -EBUSY;
2230 
2231 	job->dd_data = (void *)port_id;
2232 	req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
2233 			     job->request_payload.sg_cnt, DMA_TO_DEVICE);
2234 
2235 	if (!req_seg) {
2236 		mutex_unlock(&vhost->passthru_mutex);
2237 		return -ENOMEM;
2238 	}
2239 
2240 	rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
2241 			     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2242 
2243 	if (!rsp_seg) {
2244 		dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2245 			     job->request_payload.sg_cnt, DMA_TO_DEVICE);
2246 		mutex_unlock(&vhost->passthru_mutex);
2247 		return -ENOMEM;
2248 	}
2249 
2250 	if (req_seg > 1 || rsp_seg > 1) {
2251 		rc = -EINVAL;
2252 		goto out;
2253 	}
2254 
2255 	if (issue_login)
2256 		rc = ibmvfc_bsg_plogi(vhost, port_id);
2257 
2258 	spin_lock_irqsave(vhost->host->host_lock, flags);
2259 
2260 	if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
2261 	    unlikely((rc = ibmvfc_host_chkready(vhost)))) {
2262 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2263 		goto out;
2264 	}
2265 
2266 	evt = ibmvfc_get_reserved_event(&vhost->crq);
2267 	if (!evt) {
2268 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2269 		rc = -ENOMEM;
2270 		goto out;
2271 	}
2272 	ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2273 	mad = &evt->iu.passthru;
2274 
2275 	memset(mad, 0, sizeof(*mad));
2276 	mad->common.version = cpu_to_be32(1);
2277 	mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
2278 	mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
2279 
2280 	mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
2281 		offsetof(struct ibmvfc_passthru_mad, iu));
2282 	mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
2283 
2284 	mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
2285 	mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
2286 	mad->iu.flags = cpu_to_be32(fc_flags);
2287 	mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2288 
2289 	mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
2290 	mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
2291 	mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
2292 	mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
2293 	mad->iu.scsi_id = cpu_to_be64(port_id);
2294 	mad->iu.tag = cpu_to_be64((u64)evt);
2295 	rsp_len = be32_to_cpu(mad->iu.rsp.len);
2296 
2297 	evt->sync_iu = &rsp_iu;
2298 	init_completion(&evt->comp);
2299 	rc = ibmvfc_send_event(evt, vhost, 0);
2300 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2301 
2302 	if (rc) {
2303 		rc = -EIO;
2304 		goto out;
2305 	}
2306 
2307 	wait_for_completion(&evt->comp);
2308 
2309 	if (rsp_iu.passthru.common.status)
2310 		rc = -EIO;
2311 	else
2312 		bsg_reply->reply_payload_rcv_len = rsp_len;
2313 
2314 	spin_lock_irqsave(vhost->host->host_lock, flags);
2315 	ibmvfc_free_event(evt);
2316 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2317 	bsg_reply->result = rc;
2318 	bsg_job_done(job, bsg_reply->result,
2319 		       bsg_reply->reply_payload_rcv_len);
2320 	rc = 0;
2321 out:
2322 	dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2323 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
2324 	dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
2325 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2326 	mutex_unlock(&vhost->passthru_mutex);
2327 	LEAVE;
2328 	return rc;
2329 }
2330 
2331 /**
2332  * ibmvfc_reset_device - Reset the device with the specified reset type
2333  * @sdev:	scsi device to reset
2334  * @type:	reset type
2335  * @desc:	reset type description for log messages
2336  *
2337  * Returns:
2338  *	0 on success / other on failure
2339  **/
ibmvfc_reset_device(struct scsi_device * sdev,int type,char * desc)2340 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
2341 {
2342 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2343 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2344 	struct ibmvfc_cmd *tmf;
2345 	struct ibmvfc_event *evt = NULL;
2346 	union ibmvfc_iu rsp_iu;
2347 	struct ibmvfc_fcp_cmd_iu *iu;
2348 	struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2349 	int rsp_rc = -EBUSY;
2350 	unsigned long flags;
2351 	int rsp_code = 0;
2352 
2353 	spin_lock_irqsave(vhost->host->host_lock, flags);
2354 	if (vhost->state == IBMVFC_ACTIVE) {
2355 		if (vhost->using_channels)
2356 			evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[0]);
2357 		else
2358 			evt = ibmvfc_get_event(&vhost->crq);
2359 
2360 		if (!evt) {
2361 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
2362 			return -ENOMEM;
2363 		}
2364 
2365 		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2366 		tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2367 		iu = ibmvfc_get_fcp_iu(vhost, tmf);
2368 
2369 		tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2370 		if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2371 			tmf->target_wwpn = cpu_to_be64(rport->port_name);
2372 		iu->tmf_flags = type;
2373 		evt->sync_iu = &rsp_iu;
2374 
2375 		init_completion(&evt->comp);
2376 		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2377 	}
2378 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2379 
2380 	if (rsp_rc != 0) {
2381 		sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2382 			    desc, rsp_rc);
2383 		return -EIO;
2384 	}
2385 
2386 	sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2387 	wait_for_completion(&evt->comp);
2388 
2389 	if (rsp_iu.cmd.status)
2390 		rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2391 
2392 	if (rsp_code) {
2393 		if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2394 			rsp_code = fc_rsp->data.info.rsp_code;
2395 
2396 		sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2397 			    "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2398 			    ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2399 			    be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2400 			    fc_rsp->scsi_status);
2401 		rsp_rc = -EIO;
2402 	} else
2403 		sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2404 
2405 	spin_lock_irqsave(vhost->host->host_lock, flags);
2406 	ibmvfc_free_event(evt);
2407 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2408 	return rsp_rc;
2409 }
2410 
2411 /**
2412  * ibmvfc_match_rport - Match function for specified remote port
2413  * @evt:	ibmvfc event struct
2414  * @rport:	device to match
2415  *
2416  * Returns:
2417  *	1 if event matches rport / 0 if event does not match rport
2418  **/
ibmvfc_match_rport(struct ibmvfc_event * evt,void * rport)2419 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2420 {
2421 	struct fc_rport *cmd_rport;
2422 
2423 	if (evt->cmnd) {
2424 		cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2425 		if (cmd_rport == rport)
2426 			return 1;
2427 	}
2428 	return 0;
2429 }
2430 
2431 /**
2432  * ibmvfc_match_target - Match function for specified target
2433  * @evt:	ibmvfc event struct
2434  * @device:	device to match (starget)
2435  *
2436  * Returns:
2437  *	1 if event matches starget / 0 if event does not match starget
2438  **/
ibmvfc_match_target(struct ibmvfc_event * evt,void * device)2439 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2440 {
2441 	if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2442 		return 1;
2443 	return 0;
2444 }
2445 
2446 /**
2447  * ibmvfc_match_lun - Match function for specified LUN
2448  * @evt:	ibmvfc event struct
2449  * @device:	device to match (sdev)
2450  *
2451  * Returns:
2452  *	1 if event matches sdev / 0 if event does not match sdev
2453  **/
ibmvfc_match_lun(struct ibmvfc_event * evt,void * device)2454 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2455 {
2456 	if (evt->cmnd && evt->cmnd->device == device)
2457 		return 1;
2458 	return 0;
2459 }
2460 
2461 /**
2462  * ibmvfc_event_is_free - Check if event is free or not
2463  * @evt:	ibmvfc event struct
2464  *
2465  * Returns:
2466  *	true / false
2467  **/
ibmvfc_event_is_free(struct ibmvfc_event * evt)2468 static bool ibmvfc_event_is_free(struct ibmvfc_event *evt)
2469 {
2470 	struct ibmvfc_event *loop_evt;
2471 
2472 	list_for_each_entry(loop_evt, &evt->queue->free, queue_list)
2473 		if (loop_evt == evt)
2474 			return true;
2475 
2476 	return false;
2477 }
2478 
2479 /**
2480  * ibmvfc_wait_for_ops - Wait for ops to complete
2481  * @vhost:	ibmvfc host struct
2482  * @device:	device to match (starget or sdev)
2483  * @match:	match function
2484  *
2485  * Returns:
2486  *	SUCCESS / FAILED
2487  **/
ibmvfc_wait_for_ops(struct ibmvfc_host * vhost,void * device,int (* match)(struct ibmvfc_event *,void *))2488 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2489 			       int (*match) (struct ibmvfc_event *, void *))
2490 {
2491 	struct ibmvfc_event *evt;
2492 	DECLARE_COMPLETION_ONSTACK(comp);
2493 	int wait, i, q_index, q_size;
2494 	unsigned long flags;
2495 	signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2496 	struct ibmvfc_queue *queues;
2497 
2498 	ENTER;
2499 	if (vhost->mq_enabled && vhost->using_channels) {
2500 		queues = vhost->scsi_scrqs.scrqs;
2501 		q_size = vhost->scsi_scrqs.active_queues;
2502 	} else {
2503 		queues = &vhost->crq;
2504 		q_size = 1;
2505 	}
2506 
2507 	do {
2508 		wait = 0;
2509 		spin_lock_irqsave(vhost->host->host_lock, flags);
2510 		for (q_index = 0; q_index < q_size; q_index++) {
2511 			spin_lock(&queues[q_index].l_lock);
2512 			for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2513 				evt = &queues[q_index].evt_pool.events[i];
2514 				if (!ibmvfc_event_is_free(evt)) {
2515 					if (match(evt, device)) {
2516 						evt->eh_comp = &comp;
2517 						wait++;
2518 					}
2519 				}
2520 			}
2521 			spin_unlock(&queues[q_index].l_lock);
2522 		}
2523 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2524 
2525 		if (wait) {
2526 			timeout = wait_for_completion_timeout(&comp, timeout);
2527 
2528 			if (!timeout) {
2529 				wait = 0;
2530 				spin_lock_irqsave(vhost->host->host_lock, flags);
2531 				for (q_index = 0; q_index < q_size; q_index++) {
2532 					spin_lock(&queues[q_index].l_lock);
2533 					for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2534 						evt = &queues[q_index].evt_pool.events[i];
2535 						if (!ibmvfc_event_is_free(evt)) {
2536 							if (match(evt, device)) {
2537 								evt->eh_comp = NULL;
2538 								wait++;
2539 							}
2540 						}
2541 					}
2542 					spin_unlock(&queues[q_index].l_lock);
2543 				}
2544 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
2545 				if (wait)
2546 					dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2547 				LEAVE;
2548 				return wait ? FAILED : SUCCESS;
2549 			}
2550 		}
2551 	} while (wait);
2552 
2553 	LEAVE;
2554 	return SUCCESS;
2555 }
2556 
ibmvfc_init_tmf(struct ibmvfc_queue * queue,struct scsi_device * sdev,int type)2557 static struct ibmvfc_event *ibmvfc_init_tmf(struct ibmvfc_queue *queue,
2558 					    struct scsi_device *sdev,
2559 					    int type)
2560 {
2561 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2562 	struct scsi_target *starget = scsi_target(sdev);
2563 	struct fc_rport *rport = starget_to_rport(starget);
2564 	struct ibmvfc_event *evt;
2565 	struct ibmvfc_tmf *tmf;
2566 
2567 	evt = ibmvfc_get_reserved_event(queue);
2568 	if (!evt)
2569 		return NULL;
2570 	ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2571 
2572 	tmf = &evt->iu.tmf;
2573 	memset(tmf, 0, sizeof(*tmf));
2574 	if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
2575 		tmf->common.version = cpu_to_be32(2);
2576 		tmf->target_wwpn = cpu_to_be64(rport->port_name);
2577 	} else {
2578 		tmf->common.version = cpu_to_be32(1);
2579 	}
2580 	tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2581 	tmf->common.length = cpu_to_be16(sizeof(*tmf));
2582 	tmf->scsi_id = cpu_to_be64(rport->port_id);
2583 	int_to_scsilun(sdev->lun, &tmf->lun);
2584 	if (!ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPRESS_ABTS))
2585 		type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2586 	if (vhost->state == IBMVFC_ACTIVE)
2587 		tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2588 	else
2589 		tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2590 	tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2591 	tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2592 
2593 	init_completion(&evt->comp);
2594 
2595 	return evt;
2596 }
2597 
ibmvfc_cancel_all_mq(struct scsi_device * sdev,int type)2598 static int ibmvfc_cancel_all_mq(struct scsi_device *sdev, int type)
2599 {
2600 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2601 	struct ibmvfc_event *evt, *found_evt, *temp;
2602 	struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
2603 	unsigned long flags;
2604 	int num_hwq, i;
2605 	int fail = 0;
2606 	LIST_HEAD(cancelq);
2607 	u16 status;
2608 
2609 	ENTER;
2610 	spin_lock_irqsave(vhost->host->host_lock, flags);
2611 	num_hwq = vhost->scsi_scrqs.active_queues;
2612 	for (i = 0; i < num_hwq; i++) {
2613 		spin_lock(queues[i].q_lock);
2614 		spin_lock(&queues[i].l_lock);
2615 		found_evt = NULL;
2616 		list_for_each_entry(evt, &queues[i].sent, queue_list) {
2617 			if (evt->cmnd && evt->cmnd->device == sdev) {
2618 				found_evt = evt;
2619 				break;
2620 			}
2621 		}
2622 		spin_unlock(&queues[i].l_lock);
2623 
2624 		if (found_evt && vhost->logged_in) {
2625 			evt = ibmvfc_init_tmf(&queues[i], sdev, type);
2626 			if (!evt) {
2627 				spin_unlock(queues[i].q_lock);
2628 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
2629 				return -ENOMEM;
2630 			}
2631 			evt->sync_iu = &queues[i].cancel_rsp;
2632 			ibmvfc_send_event(evt, vhost, default_timeout);
2633 			list_add_tail(&evt->cancel, &cancelq);
2634 		}
2635 
2636 		spin_unlock(queues[i].q_lock);
2637 	}
2638 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2639 
2640 	if (list_empty(&cancelq)) {
2641 		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2642 			sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2643 		return 0;
2644 	}
2645 
2646 	sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2647 
2648 	list_for_each_entry_safe(evt, temp, &cancelq, cancel) {
2649 		wait_for_completion(&evt->comp);
2650 		status = be16_to_cpu(evt->queue->cancel_rsp.mad_common.status);
2651 		list_del(&evt->cancel);
2652 		ibmvfc_free_event(evt);
2653 
2654 		if (status != IBMVFC_MAD_SUCCESS) {
2655 			sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2656 			switch (status) {
2657 			case IBMVFC_MAD_DRIVER_FAILED:
2658 			case IBMVFC_MAD_CRQ_ERROR:
2659 			/* Host adapter most likely going through reset, return success to
2660 			 * the caller will wait for the command being cancelled to get returned
2661 			 */
2662 				break;
2663 			default:
2664 				fail = 1;
2665 				break;
2666 			}
2667 		}
2668 	}
2669 
2670 	if (fail)
2671 		return -EIO;
2672 
2673 	sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2674 	LEAVE;
2675 	return 0;
2676 }
2677 
ibmvfc_cancel_all_sq(struct scsi_device * sdev,int type)2678 static int ibmvfc_cancel_all_sq(struct scsi_device *sdev, int type)
2679 {
2680 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2681 	struct ibmvfc_event *evt, *found_evt;
2682 	union ibmvfc_iu rsp;
2683 	int rsp_rc = -EBUSY;
2684 	unsigned long flags;
2685 	u16 status;
2686 
2687 	ENTER;
2688 	found_evt = NULL;
2689 	spin_lock_irqsave(vhost->host->host_lock, flags);
2690 	spin_lock(&vhost->crq.l_lock);
2691 	list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2692 		if (evt->cmnd && evt->cmnd->device == sdev) {
2693 			found_evt = evt;
2694 			break;
2695 		}
2696 	}
2697 	spin_unlock(&vhost->crq.l_lock);
2698 
2699 	if (!found_evt) {
2700 		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2701 			sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2702 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2703 		return 0;
2704 	}
2705 
2706 	if (vhost->logged_in) {
2707 		evt = ibmvfc_init_tmf(&vhost->crq, sdev, type);
2708 		evt->sync_iu = &rsp;
2709 		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2710 	}
2711 
2712 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2713 
2714 	if (rsp_rc != 0) {
2715 		sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2716 		/* If failure is received, the host adapter is most likely going
2717 		 through reset, return success so the caller will wait for the command
2718 		 being cancelled to get returned */
2719 		return 0;
2720 	}
2721 
2722 	sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2723 
2724 	wait_for_completion(&evt->comp);
2725 	status = be16_to_cpu(rsp.mad_common.status);
2726 	spin_lock_irqsave(vhost->host->host_lock, flags);
2727 	ibmvfc_free_event(evt);
2728 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2729 
2730 	if (status != IBMVFC_MAD_SUCCESS) {
2731 		sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2732 		switch (status) {
2733 		case IBMVFC_MAD_DRIVER_FAILED:
2734 		case IBMVFC_MAD_CRQ_ERROR:
2735 			/* Host adapter most likely going through reset, return success to
2736 			 the caller will wait for the command being cancelled to get returned */
2737 			return 0;
2738 		default:
2739 			return -EIO;
2740 		};
2741 	}
2742 
2743 	sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2744 	return 0;
2745 }
2746 
2747 /**
2748  * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2749  * @sdev:	scsi device to cancel commands
2750  * @type:	type of error recovery being performed
2751  *
2752  * This sends a cancel to the VIOS for the specified device. This does
2753  * NOT send any abort to the actual device. That must be done separately.
2754  *
2755  * Returns:
2756  *	0 on success / other on failure
2757  **/
ibmvfc_cancel_all(struct scsi_device * sdev,int type)2758 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2759 {
2760 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2761 
2762 	if (vhost->mq_enabled && vhost->using_channels)
2763 		return ibmvfc_cancel_all_mq(sdev, type);
2764 	else
2765 		return ibmvfc_cancel_all_sq(sdev, type);
2766 }
2767 
2768 /**
2769  * ibmvfc_match_key - Match function for specified cancel key
2770  * @evt:	ibmvfc event struct
2771  * @key:	cancel key to match
2772  *
2773  * Returns:
2774  *	1 if event matches key / 0 if event does not match key
2775  **/
ibmvfc_match_key(struct ibmvfc_event * evt,void * key)2776 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2777 {
2778 	unsigned long cancel_key = (unsigned long)key;
2779 
2780 	if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2781 	    be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2782 		return 1;
2783 	return 0;
2784 }
2785 
2786 /**
2787  * ibmvfc_match_evt - Match function for specified event
2788  * @evt:	ibmvfc event struct
2789  * @match:	event to match
2790  *
2791  * Returns:
2792  *	1 if event matches key / 0 if event does not match key
2793  **/
ibmvfc_match_evt(struct ibmvfc_event * evt,void * match)2794 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2795 {
2796 	if (evt == match)
2797 		return 1;
2798 	return 0;
2799 }
2800 
2801 /**
2802  * ibmvfc_abort_task_set - Abort outstanding commands to the device
2803  * @sdev:	scsi device to abort commands
2804  *
2805  * This sends an Abort Task Set to the VIOS for the specified device. This does
2806  * NOT send any cancel to the VIOS. That must be done separately.
2807  *
2808  * Returns:
2809  *	0 on success / other on failure
2810  **/
ibmvfc_abort_task_set(struct scsi_device * sdev)2811 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2812 {
2813 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2814 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2815 	struct ibmvfc_cmd *tmf;
2816 	struct ibmvfc_event *evt, *found_evt;
2817 	union ibmvfc_iu rsp_iu;
2818 	struct ibmvfc_fcp_cmd_iu *iu;
2819 	struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2820 	int rc, rsp_rc = -EBUSY;
2821 	unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2822 	int rsp_code = 0;
2823 
2824 	found_evt = NULL;
2825 	spin_lock_irqsave(vhost->host->host_lock, flags);
2826 	spin_lock(&vhost->crq.l_lock);
2827 	list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2828 		if (evt->cmnd && evt->cmnd->device == sdev) {
2829 			found_evt = evt;
2830 			break;
2831 		}
2832 	}
2833 	spin_unlock(&vhost->crq.l_lock);
2834 
2835 	if (!found_evt) {
2836 		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2837 			sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2838 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
2839 		return 0;
2840 	}
2841 
2842 	if (vhost->state == IBMVFC_ACTIVE) {
2843 		evt = ibmvfc_get_event(&vhost->crq);
2844 		if (!evt) {
2845 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
2846 			return -ENOMEM;
2847 		}
2848 		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2849 		tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2850 		iu = ibmvfc_get_fcp_iu(vhost, tmf);
2851 
2852 		if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2853 			tmf->target_wwpn = cpu_to_be64(rport->port_name);
2854 		iu->tmf_flags = IBMVFC_ABORT_TASK_SET;
2855 		tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2856 		evt->sync_iu = &rsp_iu;
2857 
2858 		tmf->correlation = cpu_to_be64((u64)evt);
2859 
2860 		init_completion(&evt->comp);
2861 		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2862 	}
2863 
2864 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2865 
2866 	if (rsp_rc != 0) {
2867 		sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2868 		return -EIO;
2869 	}
2870 
2871 	sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2872 	timeout = wait_for_completion_timeout(&evt->comp, timeout);
2873 
2874 	if (!timeout) {
2875 		rc = ibmvfc_cancel_all(sdev, 0);
2876 		if (!rc) {
2877 			rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2878 			if (rc == SUCCESS)
2879 				rc = 0;
2880 		}
2881 
2882 		if (rc) {
2883 			sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2884 			ibmvfc_reset_host(vhost);
2885 			rsp_rc = -EIO;
2886 			rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2887 
2888 			if (rc == SUCCESS)
2889 				rsp_rc = 0;
2890 
2891 			rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2892 			if (rc != SUCCESS) {
2893 				spin_lock_irqsave(vhost->host->host_lock, flags);
2894 				ibmvfc_hard_reset_host(vhost);
2895 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
2896 				rsp_rc = 0;
2897 			}
2898 
2899 			goto out;
2900 		}
2901 	}
2902 
2903 	if (rsp_iu.cmd.status)
2904 		rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2905 
2906 	if (rsp_code) {
2907 		if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2908 			rsp_code = fc_rsp->data.info.rsp_code;
2909 
2910 		sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2911 			    "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2912 			    ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2913 			    be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2914 			    fc_rsp->scsi_status);
2915 		rsp_rc = -EIO;
2916 	} else
2917 		sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2918 
2919 out:
2920 	spin_lock_irqsave(vhost->host->host_lock, flags);
2921 	ibmvfc_free_event(evt);
2922 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2923 	return rsp_rc;
2924 }
2925 
2926 /**
2927  * ibmvfc_eh_abort_handler - Abort a command
2928  * @cmd:	scsi command to abort
2929  *
2930  * Returns:
2931  *	SUCCESS / FAST_IO_FAIL / FAILED
2932  **/
ibmvfc_eh_abort_handler(struct scsi_cmnd * cmd)2933 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2934 {
2935 	struct scsi_device *sdev = cmd->device;
2936 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2937 	int cancel_rc, block_rc;
2938 	int rc = FAILED;
2939 
2940 	ENTER;
2941 	block_rc = fc_block_scsi_eh(cmd);
2942 	ibmvfc_wait_while_resetting(vhost);
2943 	if (block_rc != FAST_IO_FAIL) {
2944 		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2945 		ibmvfc_abort_task_set(sdev);
2946 	} else
2947 		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2948 
2949 	if (!cancel_rc)
2950 		rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2951 
2952 	if (block_rc == FAST_IO_FAIL && rc != FAILED)
2953 		rc = FAST_IO_FAIL;
2954 
2955 	LEAVE;
2956 	return rc;
2957 }
2958 
2959 /**
2960  * ibmvfc_eh_device_reset_handler - Reset a single LUN
2961  * @cmd:	scsi command struct
2962  *
2963  * Returns:
2964  *	SUCCESS / FAST_IO_FAIL / FAILED
2965  **/
ibmvfc_eh_device_reset_handler(struct scsi_cmnd * cmd)2966 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2967 {
2968 	struct scsi_device *sdev = cmd->device;
2969 	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2970 	int cancel_rc, block_rc, reset_rc = 0;
2971 	int rc = FAILED;
2972 
2973 	ENTER;
2974 	block_rc = fc_block_scsi_eh(cmd);
2975 	ibmvfc_wait_while_resetting(vhost);
2976 	if (block_rc != FAST_IO_FAIL) {
2977 		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2978 		reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2979 	} else
2980 		cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2981 
2982 	if (!cancel_rc && !reset_rc)
2983 		rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2984 
2985 	if (block_rc == FAST_IO_FAIL && rc != FAILED)
2986 		rc = FAST_IO_FAIL;
2987 
2988 	LEAVE;
2989 	return rc;
2990 }
2991 
2992 /**
2993  * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2994  * @sdev:	scsi device struct
2995  * @data:	return code
2996  *
2997  **/
ibmvfc_dev_cancel_all_noreset(struct scsi_device * sdev,void * data)2998 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2999 {
3000 	unsigned long *rc = data;
3001 	*rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
3002 }
3003 
3004 /**
3005  * ibmvfc_eh_target_reset_handler - Reset the target
3006  * @cmd:	scsi command struct
3007  *
3008  * Returns:
3009  *	SUCCESS / FAST_IO_FAIL / FAILED
3010  **/
ibmvfc_eh_target_reset_handler(struct scsi_cmnd * cmd)3011 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
3012 {
3013 	struct scsi_target *starget = scsi_target(cmd->device);
3014 	struct fc_rport *rport = starget_to_rport(starget);
3015 	struct Scsi_Host *shost = rport_to_shost(rport);
3016 	struct ibmvfc_host *vhost = shost_priv(shost);
3017 	int block_rc;
3018 	int reset_rc = 0;
3019 	int rc = FAILED;
3020 	unsigned long cancel_rc = 0;
3021 	bool tgt_reset = false;
3022 
3023 	ENTER;
3024 	block_rc = fc_block_rport(rport);
3025 	ibmvfc_wait_while_resetting(vhost);
3026 	if (block_rc != FAST_IO_FAIL) {
3027 		struct scsi_device *sdev;
3028 
3029 		shost_for_each_device(sdev, shost) {
3030 			if ((sdev->channel != starget->channel) ||
3031 			    (sdev->id != starget->id))
3032 				continue;
3033 
3034 			cancel_rc |= ibmvfc_cancel_all(sdev,
3035 						       IBMVFC_TMF_TGT_RESET);
3036 			if (!tgt_reset) {
3037 				reset_rc = ibmvfc_reset_device(sdev,
3038 					IBMVFC_TARGET_RESET, "target");
3039 				tgt_reset = true;
3040 			}
3041 		}
3042 	} else
3043 		starget_for_each_device(starget, &cancel_rc,
3044 					ibmvfc_dev_cancel_all_noreset);
3045 
3046 	if (!cancel_rc && !reset_rc)
3047 		rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
3048 
3049 	if (block_rc == FAST_IO_FAIL && rc != FAILED)
3050 		rc = FAST_IO_FAIL;
3051 
3052 	LEAVE;
3053 	return rc;
3054 }
3055 
3056 /**
3057  * ibmvfc_eh_host_reset_handler - Reset the connection to the server
3058  * @cmd:	struct scsi_cmnd having problems
3059  *
3060  **/
ibmvfc_eh_host_reset_handler(struct scsi_cmnd * cmd)3061 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
3062 {
3063 	int rc;
3064 	struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
3065 
3066 	dev_err(vhost->dev, "Resetting connection due to error recovery\n");
3067 	rc = ibmvfc_issue_fc_host_lip(vhost->host);
3068 
3069 	return rc ? FAILED : SUCCESS;
3070 }
3071 
3072 /**
3073  * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
3074  * @rport:		rport struct
3075  *
3076  * Return value:
3077  * 	none
3078  **/
ibmvfc_terminate_rport_io(struct fc_rport * rport)3079 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
3080 {
3081 	struct Scsi_Host *shost = rport_to_shost(rport);
3082 	struct ibmvfc_host *vhost = shost_priv(shost);
3083 	struct fc_rport *dev_rport;
3084 	struct scsi_device *sdev;
3085 	struct ibmvfc_target *tgt;
3086 	unsigned long rc, flags;
3087 	unsigned int found;
3088 
3089 	ENTER;
3090 	shost_for_each_device(sdev, shost) {
3091 		dev_rport = starget_to_rport(scsi_target(sdev));
3092 		if (dev_rport != rport)
3093 			continue;
3094 		ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
3095 	}
3096 
3097 	rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
3098 
3099 	if (rc == FAILED)
3100 		ibmvfc_issue_fc_host_lip(shost);
3101 
3102 	spin_lock_irqsave(shost->host_lock, flags);
3103 	found = 0;
3104 	list_for_each_entry(tgt, &vhost->targets, queue) {
3105 		if (tgt->scsi_id == rport->port_id) {
3106 			found++;
3107 			break;
3108 		}
3109 	}
3110 
3111 	if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
3112 		/*
3113 		 * If we get here, that means we previously attempted to send
3114 		 * an implicit logout to the target but it failed, most likely
3115 		 * due to I/O being pending, so we need to send it again
3116 		 */
3117 		ibmvfc_del_tgt(tgt);
3118 		ibmvfc_reinit_host(vhost);
3119 	}
3120 
3121 	spin_unlock_irqrestore(shost->host_lock, flags);
3122 	LEAVE;
3123 }
3124 
3125 static const struct ibmvfc_async_desc ae_desc [] = {
3126 	{ "PLOGI",	IBMVFC_AE_ELS_PLOGI,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3127 	{ "LOGO",	IBMVFC_AE_ELS_LOGO,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3128 	{ "PRLO",	IBMVFC_AE_ELS_PRLO,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3129 	{ "N-Port SCN",	IBMVFC_AE_SCN_NPORT,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3130 	{ "Group SCN",	IBMVFC_AE_SCN_GROUP,	IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3131 	{ "Domain SCN",	IBMVFC_AE_SCN_DOMAIN,	IBMVFC_DEFAULT_LOG_LEVEL },
3132 	{ "Fabric SCN",	IBMVFC_AE_SCN_FABRIC,	IBMVFC_DEFAULT_LOG_LEVEL },
3133 	{ "Link Up",	IBMVFC_AE_LINK_UP,	IBMVFC_DEFAULT_LOG_LEVEL },
3134 	{ "Link Down",	IBMVFC_AE_LINK_DOWN,	IBMVFC_DEFAULT_LOG_LEVEL },
3135 	{ "Link Dead",	IBMVFC_AE_LINK_DEAD,	IBMVFC_DEFAULT_LOG_LEVEL },
3136 	{ "Halt",	IBMVFC_AE_HALT,		IBMVFC_DEFAULT_LOG_LEVEL },
3137 	{ "Resume",	IBMVFC_AE_RESUME,	IBMVFC_DEFAULT_LOG_LEVEL },
3138 	{ "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
3139 };
3140 
3141 static const struct ibmvfc_async_desc unknown_ae = {
3142 	"Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
3143 };
3144 
3145 /**
3146  * ibmvfc_get_ae_desc - Get text description for async event
3147  * @ae:	async event
3148  *
3149  **/
ibmvfc_get_ae_desc(u64 ae)3150 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
3151 {
3152 	int i;
3153 
3154 	for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
3155 		if (ae_desc[i].ae == ae)
3156 			return &ae_desc[i];
3157 
3158 	return &unknown_ae;
3159 }
3160 
3161 static const struct {
3162 	enum ibmvfc_ae_link_state state;
3163 	const char *desc;
3164 } link_desc [] = {
3165 	{ IBMVFC_AE_LS_LINK_UP,		" link up" },
3166 	{ IBMVFC_AE_LS_LINK_BOUNCED,	" link bounced" },
3167 	{ IBMVFC_AE_LS_LINK_DOWN,	" link down" },
3168 	{ IBMVFC_AE_LS_LINK_DEAD,	" link dead" },
3169 };
3170 
3171 /**
3172  * ibmvfc_get_link_state - Get text description for link state
3173  * @state:	link state
3174  *
3175  **/
ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)3176 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
3177 {
3178 	int i;
3179 
3180 	for (i = 0; i < ARRAY_SIZE(link_desc); i++)
3181 		if (link_desc[i].state == state)
3182 			return link_desc[i].desc;
3183 
3184 	return "";
3185 }
3186 
3187 /**
3188  * ibmvfc_handle_async - Handle an async event from the adapter
3189  * @crq:	crq to process
3190  * @vhost:	ibmvfc host struct
3191  *
3192  **/
ibmvfc_handle_async(struct ibmvfc_async_crq * crq,struct ibmvfc_host * vhost)3193 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
3194 				struct ibmvfc_host *vhost)
3195 {
3196 	const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
3197 	struct ibmvfc_target *tgt;
3198 
3199 	ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
3200 		   " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
3201 		   be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
3202 		   ibmvfc_get_link_state(crq->link_state));
3203 
3204 	switch (be64_to_cpu(crq->event)) {
3205 	case IBMVFC_AE_RESUME:
3206 		switch (crq->link_state) {
3207 		case IBMVFC_AE_LS_LINK_DOWN:
3208 			ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3209 			break;
3210 		case IBMVFC_AE_LS_LINK_DEAD:
3211 			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3212 			break;
3213 		case IBMVFC_AE_LS_LINK_UP:
3214 		case IBMVFC_AE_LS_LINK_BOUNCED:
3215 		default:
3216 			vhost->events_to_log |= IBMVFC_AE_LINKUP;
3217 			vhost->delay_init = 1;
3218 			__ibmvfc_reset_host(vhost);
3219 			break;
3220 		}
3221 
3222 		break;
3223 	case IBMVFC_AE_LINK_UP:
3224 		vhost->events_to_log |= IBMVFC_AE_LINKUP;
3225 		vhost->delay_init = 1;
3226 		__ibmvfc_reset_host(vhost);
3227 		break;
3228 	case IBMVFC_AE_SCN_FABRIC:
3229 	case IBMVFC_AE_SCN_DOMAIN:
3230 		vhost->events_to_log |= IBMVFC_AE_RSCN;
3231 		if (vhost->state < IBMVFC_HALTED) {
3232 			vhost->delay_init = 1;
3233 			__ibmvfc_reset_host(vhost);
3234 		}
3235 		break;
3236 	case IBMVFC_AE_SCN_NPORT:
3237 	case IBMVFC_AE_SCN_GROUP:
3238 		vhost->events_to_log |= IBMVFC_AE_RSCN;
3239 		ibmvfc_reinit_host(vhost);
3240 		break;
3241 	case IBMVFC_AE_ELS_LOGO:
3242 	case IBMVFC_AE_ELS_PRLO:
3243 	case IBMVFC_AE_ELS_PLOGI:
3244 		list_for_each_entry(tgt, &vhost->targets, queue) {
3245 			if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
3246 				break;
3247 			if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
3248 				continue;
3249 			if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
3250 				continue;
3251 			if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
3252 				continue;
3253 			if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
3254 				tgt->logo_rcvd = 1;
3255 			if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
3256 				ibmvfc_del_tgt(tgt);
3257 				ibmvfc_reinit_host(vhost);
3258 			}
3259 		}
3260 		break;
3261 	case IBMVFC_AE_LINK_DOWN:
3262 	case IBMVFC_AE_ADAPTER_FAILED:
3263 		ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3264 		break;
3265 	case IBMVFC_AE_LINK_DEAD:
3266 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3267 		break;
3268 	case IBMVFC_AE_HALT:
3269 		ibmvfc_link_down(vhost, IBMVFC_HALTED);
3270 		break;
3271 	default:
3272 		dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
3273 		break;
3274 	}
3275 }
3276 
3277 /**
3278  * ibmvfc_handle_crq - Handles and frees received events in the CRQ
3279  * @crq:	Command/Response queue
3280  * @vhost:	ibmvfc host struct
3281  * @evt_doneq:	Event done queue
3282  *
3283 **/
ibmvfc_handle_crq(struct ibmvfc_crq * crq,struct ibmvfc_host * vhost,struct list_head * evt_doneq)3284 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3285 			      struct list_head *evt_doneq)
3286 {
3287 	long rc;
3288 	struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3289 
3290 	switch (crq->valid) {
3291 	case IBMVFC_CRQ_INIT_RSP:
3292 		switch (crq->format) {
3293 		case IBMVFC_CRQ_INIT:
3294 			dev_info(vhost->dev, "Partner initialized\n");
3295 			/* Send back a response */
3296 			rc = ibmvfc_send_crq_init_complete(vhost);
3297 			if (rc == 0)
3298 				ibmvfc_init_host(vhost);
3299 			else
3300 				dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
3301 			break;
3302 		case IBMVFC_CRQ_INIT_COMPLETE:
3303 			dev_info(vhost->dev, "Partner initialization complete\n");
3304 			ibmvfc_init_host(vhost);
3305 			break;
3306 		default:
3307 			dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
3308 		}
3309 		return;
3310 	case IBMVFC_CRQ_XPORT_EVENT:
3311 		vhost->state = IBMVFC_NO_CRQ;
3312 		vhost->logged_in = 0;
3313 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3314 		if (crq->format == IBMVFC_PARTITION_MIGRATED) {
3315 			/* We need to re-setup the interpartition connection */
3316 			dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
3317 			vhost->client_migrated = 1;
3318 
3319 			scsi_block_requests(vhost->host);
3320 			ibmvfc_purge_requests(vhost, DID_REQUEUE);
3321 			ibmvfc_set_host_state(vhost, IBMVFC_LINK_DOWN);
3322 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
3323 			wake_up(&vhost->work_wait_q);
3324 		} else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
3325 			dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
3326 			ibmvfc_purge_requests(vhost, DID_ERROR);
3327 			ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3328 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
3329 		} else {
3330 			dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
3331 		}
3332 		return;
3333 	case IBMVFC_CRQ_CMD_RSP:
3334 		break;
3335 	default:
3336 		dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
3337 		return;
3338 	}
3339 
3340 	if (crq->format == IBMVFC_ASYNC_EVENT)
3341 		return;
3342 
3343 	/* The only kind of payload CRQs we should get are responses to
3344 	 * things we send. Make sure this response is to something we
3345 	 * actually sent
3346 	 */
3347 	if (unlikely(!ibmvfc_valid_event(&vhost->crq.evt_pool, evt))) {
3348 		dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3349 			crq->ioba);
3350 		return;
3351 	}
3352 
3353 	if (unlikely(atomic_dec_if_positive(&evt->active))) {
3354 		dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3355 			crq->ioba);
3356 		return;
3357 	}
3358 
3359 	spin_lock(&evt->queue->l_lock);
3360 	list_move_tail(&evt->queue_list, evt_doneq);
3361 	spin_unlock(&evt->queue->l_lock);
3362 }
3363 
3364 /**
3365  * ibmvfc_scan_finished - Check if the device scan is done.
3366  * @shost:	scsi host struct
3367  * @time:	current elapsed time
3368  *
3369  * Returns:
3370  *	0 if scan is not done / 1 if scan is done
3371  **/
ibmvfc_scan_finished(struct Scsi_Host * shost,unsigned long time)3372 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3373 {
3374 	unsigned long flags;
3375 	struct ibmvfc_host *vhost = shost_priv(shost);
3376 	int done = 0;
3377 
3378 	spin_lock_irqsave(shost->host_lock, flags);
3379 	if (!vhost->scan_timeout)
3380 		done = 1;
3381 	else if (time >= (vhost->scan_timeout * HZ)) {
3382 		dev_info(vhost->dev, "Scan taking longer than %d seconds, "
3383 			 "continuing initialization\n", vhost->scan_timeout);
3384 		done = 1;
3385 	}
3386 
3387 	if (vhost->scan_complete) {
3388 		vhost->scan_timeout = init_timeout;
3389 		done = 1;
3390 	}
3391 	spin_unlock_irqrestore(shost->host_lock, flags);
3392 	return done;
3393 }
3394 
3395 /**
3396  * ibmvfc_slave_alloc - Setup the device's task set value
3397  * @sdev:	struct scsi_device device to configure
3398  *
3399  * Set the device's task set value so that error handling works as
3400  * expected.
3401  *
3402  * Returns:
3403  *	0 on success / -ENXIO if device does not exist
3404  **/
ibmvfc_slave_alloc(struct scsi_device * sdev)3405 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
3406 {
3407 	struct Scsi_Host *shost = sdev->host;
3408 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3409 	struct ibmvfc_host *vhost = shost_priv(shost);
3410 	unsigned long flags = 0;
3411 
3412 	if (!rport || fc_remote_port_chkready(rport))
3413 		return -ENXIO;
3414 
3415 	spin_lock_irqsave(shost->host_lock, flags);
3416 	sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
3417 	spin_unlock_irqrestore(shost->host_lock, flags);
3418 	return 0;
3419 }
3420 
3421 /**
3422  * ibmvfc_target_alloc - Setup the target's task set value
3423  * @starget:	struct scsi_target
3424  *
3425  * Set the target's task set value so that error handling works as
3426  * expected.
3427  *
3428  * Returns:
3429  *	0 on success / -ENXIO if device does not exist
3430  **/
ibmvfc_target_alloc(struct scsi_target * starget)3431 static int ibmvfc_target_alloc(struct scsi_target *starget)
3432 {
3433 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3434 	struct ibmvfc_host *vhost = shost_priv(shost);
3435 	unsigned long flags = 0;
3436 
3437 	spin_lock_irqsave(shost->host_lock, flags);
3438 	starget->hostdata = (void *)(unsigned long)vhost->task_set++;
3439 	spin_unlock_irqrestore(shost->host_lock, flags);
3440 	return 0;
3441 }
3442 
3443 /**
3444  * ibmvfc_slave_configure - Configure the device
3445  * @sdev:	struct scsi_device device to configure
3446  *
3447  * Enable allow_restart for a device if it is a disk. Adjust the
3448  * queue_depth here also.
3449  *
3450  * Returns:
3451  *	0
3452  **/
ibmvfc_slave_configure(struct scsi_device * sdev)3453 static int ibmvfc_slave_configure(struct scsi_device *sdev)
3454 {
3455 	struct Scsi_Host *shost = sdev->host;
3456 	unsigned long flags = 0;
3457 
3458 	spin_lock_irqsave(shost->host_lock, flags);
3459 	if (sdev->type == TYPE_DISK) {
3460 		sdev->allow_restart = 1;
3461 		blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
3462 	}
3463 	spin_unlock_irqrestore(shost->host_lock, flags);
3464 	return 0;
3465 }
3466 
3467 /**
3468  * ibmvfc_change_queue_depth - Change the device's queue depth
3469  * @sdev:	scsi device struct
3470  * @qdepth:	depth to set
3471  *
3472  * Return value:
3473  * 	actual depth set
3474  **/
ibmvfc_change_queue_depth(struct scsi_device * sdev,int qdepth)3475 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
3476 {
3477 	if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
3478 		qdepth = IBMVFC_MAX_CMDS_PER_LUN;
3479 
3480 	return scsi_change_queue_depth(sdev, qdepth);
3481 }
3482 
ibmvfc_show_host_partition_name(struct device * dev,struct device_attribute * attr,char * buf)3483 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
3484 						 struct device_attribute *attr, char *buf)
3485 {
3486 	struct Scsi_Host *shost = class_to_shost(dev);
3487 	struct ibmvfc_host *vhost = shost_priv(shost);
3488 
3489 	return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.partition_name);
3490 }
3491 
ibmvfc_show_host_device_name(struct device * dev,struct device_attribute * attr,char * buf)3492 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
3493 					    struct device_attribute *attr, char *buf)
3494 {
3495 	struct Scsi_Host *shost = class_to_shost(dev);
3496 	struct ibmvfc_host *vhost = shost_priv(shost);
3497 
3498 	return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.device_name);
3499 }
3500 
ibmvfc_show_host_loc_code(struct device * dev,struct device_attribute * attr,char * buf)3501 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3502 					 struct device_attribute *attr, char *buf)
3503 {
3504 	struct Scsi_Host *shost = class_to_shost(dev);
3505 	struct ibmvfc_host *vhost = shost_priv(shost);
3506 
3507 	return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.port_loc_code);
3508 }
3509 
ibmvfc_show_host_drc_name(struct device * dev,struct device_attribute * attr,char * buf)3510 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3511 					 struct device_attribute *attr, char *buf)
3512 {
3513 	struct Scsi_Host *shost = class_to_shost(dev);
3514 	struct ibmvfc_host *vhost = shost_priv(shost);
3515 
3516 	return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.drc_name);
3517 }
3518 
ibmvfc_show_host_npiv_version(struct device * dev,struct device_attribute * attr,char * buf)3519 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3520 					     struct device_attribute *attr, char *buf)
3521 {
3522 	struct Scsi_Host *shost = class_to_shost(dev);
3523 	struct ibmvfc_host *vhost = shost_priv(shost);
3524 	return sysfs_emit(buf, "%d\n",
3525 			  be32_to_cpu(vhost->login_buf->resp.version));
3526 }
3527 
ibmvfc_show_host_capabilities(struct device * dev,struct device_attribute * attr,char * buf)3528 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3529 					     struct device_attribute *attr, char *buf)
3530 {
3531 	struct Scsi_Host *shost = class_to_shost(dev);
3532 	struct ibmvfc_host *vhost = shost_priv(shost);
3533 	return sysfs_emit(buf, "%llx\n",
3534 			  be64_to_cpu(vhost->login_buf->resp.capabilities));
3535 }
3536 
3537 /**
3538  * ibmvfc_show_log_level - Show the adapter's error logging level
3539  * @dev:	class device struct
3540  * @attr:	unused
3541  * @buf:	buffer
3542  *
3543  * Return value:
3544  * 	number of bytes printed to buffer
3545  **/
ibmvfc_show_log_level(struct device * dev,struct device_attribute * attr,char * buf)3546 static ssize_t ibmvfc_show_log_level(struct device *dev,
3547 				     struct device_attribute *attr, char *buf)
3548 {
3549 	struct Scsi_Host *shost = class_to_shost(dev);
3550 	struct ibmvfc_host *vhost = shost_priv(shost);
3551 	unsigned long flags = 0;
3552 	int len;
3553 
3554 	spin_lock_irqsave(shost->host_lock, flags);
3555 	len = sysfs_emit(buf, "%d\n", vhost->log_level);
3556 	spin_unlock_irqrestore(shost->host_lock, flags);
3557 	return len;
3558 }
3559 
3560 /**
3561  * ibmvfc_store_log_level - Change the adapter's error logging level
3562  * @dev:	class device struct
3563  * @attr:	unused
3564  * @buf:	buffer
3565  * @count:      buffer size
3566  *
3567  * Return value:
3568  * 	number of bytes printed to buffer
3569  **/
ibmvfc_store_log_level(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3570 static ssize_t ibmvfc_store_log_level(struct device *dev,
3571 				      struct device_attribute *attr,
3572 				      const char *buf, size_t count)
3573 {
3574 	struct Scsi_Host *shost = class_to_shost(dev);
3575 	struct ibmvfc_host *vhost = shost_priv(shost);
3576 	unsigned long flags = 0;
3577 
3578 	spin_lock_irqsave(shost->host_lock, flags);
3579 	vhost->log_level = simple_strtoul(buf, NULL, 10);
3580 	spin_unlock_irqrestore(shost->host_lock, flags);
3581 	return strlen(buf);
3582 }
3583 
ibmvfc_show_scsi_channels(struct device * dev,struct device_attribute * attr,char * buf)3584 static ssize_t ibmvfc_show_scsi_channels(struct device *dev,
3585 					 struct device_attribute *attr, char *buf)
3586 {
3587 	struct Scsi_Host *shost = class_to_shost(dev);
3588 	struct ibmvfc_host *vhost = shost_priv(shost);
3589 	struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
3590 	unsigned long flags = 0;
3591 	int len;
3592 
3593 	spin_lock_irqsave(shost->host_lock, flags);
3594 	len = sysfs_emit(buf, "%d\n", scsi->desired_queues);
3595 	spin_unlock_irqrestore(shost->host_lock, flags);
3596 	return len;
3597 }
3598 
ibmvfc_store_scsi_channels(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3599 static ssize_t ibmvfc_store_scsi_channels(struct device *dev,
3600 					 struct device_attribute *attr,
3601 					 const char *buf, size_t count)
3602 {
3603 	struct Scsi_Host *shost = class_to_shost(dev);
3604 	struct ibmvfc_host *vhost = shost_priv(shost);
3605 	struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
3606 	unsigned long flags = 0;
3607 	unsigned int channels;
3608 
3609 	spin_lock_irqsave(shost->host_lock, flags);
3610 	channels = simple_strtoul(buf, NULL, 10);
3611 	scsi->desired_queues = min(channels, shost->nr_hw_queues);
3612 	ibmvfc_hard_reset_host(vhost);
3613 	spin_unlock_irqrestore(shost->host_lock, flags);
3614 	return strlen(buf);
3615 }
3616 
3617 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3618 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3619 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3620 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3621 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3622 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3623 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3624 		   ibmvfc_show_log_level, ibmvfc_store_log_level);
3625 static DEVICE_ATTR(nr_scsi_channels, S_IRUGO | S_IWUSR,
3626 		   ibmvfc_show_scsi_channels, ibmvfc_store_scsi_channels);
3627 
3628 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3629 /**
3630  * ibmvfc_read_trace - Dump the adapter trace
3631  * @filp:		open sysfs file
3632  * @kobj:		kobject struct
3633  * @bin_attr:	bin_attribute struct
3634  * @buf:		buffer
3635  * @off:		offset
3636  * @count:		buffer size
3637  *
3638  * Return value:
3639  *	number of bytes printed to buffer
3640  **/
ibmvfc_read_trace(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)3641 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3642 				 struct bin_attribute *bin_attr,
3643 				 char *buf, loff_t off, size_t count)
3644 {
3645 	struct device *dev = kobj_to_dev(kobj);
3646 	struct Scsi_Host *shost = class_to_shost(dev);
3647 	struct ibmvfc_host *vhost = shost_priv(shost);
3648 	unsigned long flags = 0;
3649 	int size = IBMVFC_TRACE_SIZE;
3650 	char *src = (char *)vhost->trace;
3651 
3652 	if (off > size)
3653 		return 0;
3654 	if (off + count > size) {
3655 		size -= off;
3656 		count = size;
3657 	}
3658 
3659 	spin_lock_irqsave(shost->host_lock, flags);
3660 	memcpy(buf, &src[off], count);
3661 	spin_unlock_irqrestore(shost->host_lock, flags);
3662 	return count;
3663 }
3664 
3665 static struct bin_attribute ibmvfc_trace_attr = {
3666 	.attr =	{
3667 		.name = "trace",
3668 		.mode = S_IRUGO,
3669 	},
3670 	.size = 0,
3671 	.read = ibmvfc_read_trace,
3672 };
3673 #endif
3674 
3675 static struct attribute *ibmvfc_host_attrs[] = {
3676 	&dev_attr_partition_name.attr,
3677 	&dev_attr_device_name.attr,
3678 	&dev_attr_port_loc_code.attr,
3679 	&dev_attr_drc_name.attr,
3680 	&dev_attr_npiv_version.attr,
3681 	&dev_attr_capabilities.attr,
3682 	&dev_attr_log_level.attr,
3683 	&dev_attr_nr_scsi_channels.attr,
3684 	NULL
3685 };
3686 
3687 ATTRIBUTE_GROUPS(ibmvfc_host);
3688 
3689 static const struct scsi_host_template driver_template = {
3690 	.module = THIS_MODULE,
3691 	.name = "IBM POWER Virtual FC Adapter",
3692 	.proc_name = IBMVFC_NAME,
3693 	.queuecommand = ibmvfc_queuecommand,
3694 	.eh_timed_out = fc_eh_timed_out,
3695 	.eh_abort_handler = ibmvfc_eh_abort_handler,
3696 	.eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3697 	.eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3698 	.eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3699 	.slave_alloc = ibmvfc_slave_alloc,
3700 	.slave_configure = ibmvfc_slave_configure,
3701 	.target_alloc = ibmvfc_target_alloc,
3702 	.scan_finished = ibmvfc_scan_finished,
3703 	.change_queue_depth = ibmvfc_change_queue_depth,
3704 	.cmd_per_lun = 16,
3705 	.can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3706 	.this_id = -1,
3707 	.sg_tablesize = SG_ALL,
3708 	.max_sectors = IBMVFC_MAX_SECTORS,
3709 	.shost_groups = ibmvfc_host_groups,
3710 	.track_queue_depth = 1,
3711 };
3712 
3713 /**
3714  * ibmvfc_next_async_crq - Returns the next entry in async queue
3715  * @vhost:	ibmvfc host struct
3716  *
3717  * Returns:
3718  *	Pointer to next entry in queue / NULL if empty
3719  **/
ibmvfc_next_async_crq(struct ibmvfc_host * vhost)3720 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3721 {
3722 	struct ibmvfc_queue *async_crq = &vhost->async_crq;
3723 	struct ibmvfc_async_crq *crq;
3724 
3725 	crq = &async_crq->msgs.async[async_crq->cur];
3726 	if (crq->valid & 0x80) {
3727 		if (++async_crq->cur == async_crq->size)
3728 			async_crq->cur = 0;
3729 		rmb();
3730 	} else
3731 		crq = NULL;
3732 
3733 	return crq;
3734 }
3735 
3736 /**
3737  * ibmvfc_next_crq - Returns the next entry in message queue
3738  * @vhost:	ibmvfc host struct
3739  *
3740  * Returns:
3741  *	Pointer to next entry in queue / NULL if empty
3742  **/
ibmvfc_next_crq(struct ibmvfc_host * vhost)3743 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3744 {
3745 	struct ibmvfc_queue *queue = &vhost->crq;
3746 	struct ibmvfc_crq *crq;
3747 
3748 	crq = &queue->msgs.crq[queue->cur];
3749 	if (crq->valid & 0x80) {
3750 		if (++queue->cur == queue->size)
3751 			queue->cur = 0;
3752 		rmb();
3753 	} else
3754 		crq = NULL;
3755 
3756 	return crq;
3757 }
3758 
3759 /**
3760  * ibmvfc_interrupt - Interrupt handler
3761  * @irq:		number of irq to handle, not used
3762  * @dev_instance: ibmvfc_host that received interrupt
3763  *
3764  * Returns:
3765  *	IRQ_HANDLED
3766  **/
ibmvfc_interrupt(int irq,void * dev_instance)3767 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3768 {
3769 	struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3770 	unsigned long flags;
3771 
3772 	spin_lock_irqsave(vhost->host->host_lock, flags);
3773 	vio_disable_interrupts(to_vio_dev(vhost->dev));
3774 	tasklet_schedule(&vhost->tasklet);
3775 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3776 	return IRQ_HANDLED;
3777 }
3778 
3779 /**
3780  * ibmvfc_tasklet - Interrupt handler tasklet
3781  * @data:		ibmvfc host struct
3782  *
3783  * Returns:
3784  *	Nothing
3785  **/
ibmvfc_tasklet(void * data)3786 static void ibmvfc_tasklet(void *data)
3787 {
3788 	struct ibmvfc_host *vhost = data;
3789 	struct vio_dev *vdev = to_vio_dev(vhost->dev);
3790 	struct ibmvfc_crq *crq;
3791 	struct ibmvfc_async_crq *async;
3792 	struct ibmvfc_event *evt, *temp;
3793 	unsigned long flags;
3794 	int done = 0;
3795 	LIST_HEAD(evt_doneq);
3796 
3797 	spin_lock_irqsave(vhost->host->host_lock, flags);
3798 	spin_lock(vhost->crq.q_lock);
3799 	while (!done) {
3800 		/* Pull all the valid messages off the async CRQ */
3801 		while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3802 			ibmvfc_handle_async(async, vhost);
3803 			async->valid = 0;
3804 			wmb();
3805 		}
3806 
3807 		/* Pull all the valid messages off the CRQ */
3808 		while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3809 			ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3810 			crq->valid = 0;
3811 			wmb();
3812 		}
3813 
3814 		vio_enable_interrupts(vdev);
3815 		if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3816 			vio_disable_interrupts(vdev);
3817 			ibmvfc_handle_async(async, vhost);
3818 			async->valid = 0;
3819 			wmb();
3820 		} else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3821 			vio_disable_interrupts(vdev);
3822 			ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3823 			crq->valid = 0;
3824 			wmb();
3825 		} else
3826 			done = 1;
3827 	}
3828 
3829 	spin_unlock(vhost->crq.q_lock);
3830 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3831 
3832 	list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3833 		del_timer(&evt->timer);
3834 		list_del(&evt->queue_list);
3835 		ibmvfc_trc_end(evt);
3836 		evt->done(evt);
3837 	}
3838 }
3839 
ibmvfc_toggle_scrq_irq(struct ibmvfc_queue * scrq,int enable)3840 static int ibmvfc_toggle_scrq_irq(struct ibmvfc_queue *scrq, int enable)
3841 {
3842 	struct device *dev = scrq->vhost->dev;
3843 	struct vio_dev *vdev = to_vio_dev(dev);
3844 	unsigned long rc;
3845 	int irq_action = H_ENABLE_VIO_INTERRUPT;
3846 
3847 	if (!enable)
3848 		irq_action = H_DISABLE_VIO_INTERRUPT;
3849 
3850 	rc = plpar_hcall_norets(H_VIOCTL, vdev->unit_address, irq_action,
3851 				scrq->hw_irq, 0, 0);
3852 
3853 	if (rc)
3854 		dev_err(dev, "Couldn't %s sub-crq[%lu] irq. rc=%ld\n",
3855 			enable ? "enable" : "disable", scrq->hwq_id, rc);
3856 
3857 	return rc;
3858 }
3859 
ibmvfc_handle_scrq(struct ibmvfc_crq * crq,struct ibmvfc_host * vhost,struct list_head * evt_doneq)3860 static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3861 			       struct list_head *evt_doneq)
3862 {
3863 	struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3864 
3865 	switch (crq->valid) {
3866 	case IBMVFC_CRQ_CMD_RSP:
3867 		break;
3868 	case IBMVFC_CRQ_XPORT_EVENT:
3869 		return;
3870 	default:
3871 		dev_err(vhost->dev, "Got and invalid message type 0x%02x\n", crq->valid);
3872 		return;
3873 	}
3874 
3875 	/* The only kind of payload CRQs we should get are responses to
3876 	 * things we send. Make sure this response is to something we
3877 	 * actually sent
3878 	 */
3879 	if (unlikely(!ibmvfc_valid_event(&evt->queue->evt_pool, evt))) {
3880 		dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3881 			crq->ioba);
3882 		return;
3883 	}
3884 
3885 	if (unlikely(atomic_dec_if_positive(&evt->active))) {
3886 		dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3887 			crq->ioba);
3888 		return;
3889 	}
3890 
3891 	spin_lock(&evt->queue->l_lock);
3892 	list_move_tail(&evt->queue_list, evt_doneq);
3893 	spin_unlock(&evt->queue->l_lock);
3894 }
3895 
ibmvfc_next_scrq(struct ibmvfc_queue * scrq)3896 static struct ibmvfc_crq *ibmvfc_next_scrq(struct ibmvfc_queue *scrq)
3897 {
3898 	struct ibmvfc_crq *crq;
3899 
3900 	crq = &scrq->msgs.scrq[scrq->cur].crq;
3901 	if (crq->valid & 0x80) {
3902 		if (++scrq->cur == scrq->size)
3903 			scrq->cur = 0;
3904 		rmb();
3905 	} else
3906 		crq = NULL;
3907 
3908 	return crq;
3909 }
3910 
ibmvfc_drain_sub_crq(struct ibmvfc_queue * scrq)3911 static void ibmvfc_drain_sub_crq(struct ibmvfc_queue *scrq)
3912 {
3913 	struct ibmvfc_crq *crq;
3914 	struct ibmvfc_event *evt, *temp;
3915 	unsigned long flags;
3916 	int done = 0;
3917 	LIST_HEAD(evt_doneq);
3918 
3919 	spin_lock_irqsave(scrq->q_lock, flags);
3920 	while (!done) {
3921 		while ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3922 			ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3923 			crq->valid = 0;
3924 			wmb();
3925 		}
3926 
3927 		ibmvfc_toggle_scrq_irq(scrq, 1);
3928 		if ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3929 			ibmvfc_toggle_scrq_irq(scrq, 0);
3930 			ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3931 			crq->valid = 0;
3932 			wmb();
3933 		} else
3934 			done = 1;
3935 	}
3936 	spin_unlock_irqrestore(scrq->q_lock, flags);
3937 
3938 	list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3939 		del_timer(&evt->timer);
3940 		list_del(&evt->queue_list);
3941 		ibmvfc_trc_end(evt);
3942 		evt->done(evt);
3943 	}
3944 }
3945 
ibmvfc_interrupt_mq(int irq,void * scrq_instance)3946 static irqreturn_t ibmvfc_interrupt_mq(int irq, void *scrq_instance)
3947 {
3948 	struct ibmvfc_queue *scrq = (struct ibmvfc_queue *)scrq_instance;
3949 
3950 	ibmvfc_toggle_scrq_irq(scrq, 0);
3951 	ibmvfc_drain_sub_crq(scrq);
3952 
3953 	return IRQ_HANDLED;
3954 }
3955 
3956 /**
3957  * ibmvfc_init_tgt - Set the next init job step for the target
3958  * @tgt:		ibmvfc target struct
3959  * @job_step:	job step to perform
3960  *
3961  **/
ibmvfc_init_tgt(struct ibmvfc_target * tgt,void (* job_step)(struct ibmvfc_target *))3962 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3963 			    void (*job_step) (struct ibmvfc_target *))
3964 {
3965 	if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3966 		tgt->job_step = job_step;
3967 	wake_up(&tgt->vhost->work_wait_q);
3968 }
3969 
3970 /**
3971  * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3972  * @tgt:		ibmvfc target struct
3973  * @job_step:	initialization job step
3974  *
3975  * Returns: 1 if step will be retried / 0 if not
3976  *
3977  **/
ibmvfc_retry_tgt_init(struct ibmvfc_target * tgt,void (* job_step)(struct ibmvfc_target *))3978 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3979 				  void (*job_step) (struct ibmvfc_target *))
3980 {
3981 	if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3982 		ibmvfc_del_tgt(tgt);
3983 		wake_up(&tgt->vhost->work_wait_q);
3984 		return 0;
3985 	} else
3986 		ibmvfc_init_tgt(tgt, job_step);
3987 	return 1;
3988 }
3989 
3990 /* Defined in FC-LS */
3991 static const struct {
3992 	int code;
3993 	int retry;
3994 	int logged_in;
3995 } prli_rsp [] = {
3996 	{ 0, 1, 0 },
3997 	{ 1, 0, 1 },
3998 	{ 2, 1, 0 },
3999 	{ 3, 1, 0 },
4000 	{ 4, 0, 0 },
4001 	{ 5, 0, 0 },
4002 	{ 6, 0, 1 },
4003 	{ 7, 0, 0 },
4004 	{ 8, 1, 0 },
4005 };
4006 
4007 /**
4008  * ibmvfc_get_prli_rsp - Find PRLI response index
4009  * @flags:	PRLI response flags
4010  *
4011  **/
ibmvfc_get_prli_rsp(u16 flags)4012 static int ibmvfc_get_prli_rsp(u16 flags)
4013 {
4014 	int i;
4015 	int code = (flags & 0x0f00) >> 8;
4016 
4017 	for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
4018 		if (prli_rsp[i].code == code)
4019 			return i;
4020 
4021 	return 0;
4022 }
4023 
4024 /**
4025  * ibmvfc_tgt_prli_done - Completion handler for Process Login
4026  * @evt:	ibmvfc event struct
4027  *
4028  **/
ibmvfc_tgt_prli_done(struct ibmvfc_event * evt)4029 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
4030 {
4031 	struct ibmvfc_target *tgt = evt->tgt;
4032 	struct ibmvfc_host *vhost = evt->vhost;
4033 	struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
4034 	struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
4035 	u32 status = be16_to_cpu(rsp->common.status);
4036 	int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
4037 
4038 	vhost->discovery_threads--;
4039 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4040 	switch (status) {
4041 	case IBMVFC_MAD_SUCCESS:
4042 		tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
4043 			parms->type, parms->flags, parms->service_parms);
4044 
4045 		if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
4046 			index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
4047 			if (prli_rsp[index].logged_in) {
4048 				if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
4049 					tgt->need_login = 0;
4050 					tgt->ids.roles = 0;
4051 					if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
4052 						tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
4053 					if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
4054 						tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4055 					tgt->add_rport = 1;
4056 				} else
4057 					ibmvfc_del_tgt(tgt);
4058 			} else if (prli_rsp[index].retry)
4059 				ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4060 			else
4061 				ibmvfc_del_tgt(tgt);
4062 		} else
4063 			ibmvfc_del_tgt(tgt);
4064 		break;
4065 	case IBMVFC_MAD_DRIVER_FAILED:
4066 		break;
4067 	case IBMVFC_MAD_CRQ_ERROR:
4068 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4069 		break;
4070 	case IBMVFC_MAD_FAILED:
4071 	default:
4072 		if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
4073 		     be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
4074 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4075 		else if (tgt->logo_rcvd)
4076 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4077 		else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4078 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4079 		else
4080 			ibmvfc_del_tgt(tgt);
4081 
4082 		tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
4083 			ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4084 			be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
4085 		break;
4086 	}
4087 
4088 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4089 	ibmvfc_free_event(evt);
4090 	wake_up(&vhost->work_wait_q);
4091 }
4092 
4093 /**
4094  * ibmvfc_tgt_send_prli - Send a process login
4095  * @tgt:	ibmvfc target struct
4096  *
4097  **/
ibmvfc_tgt_send_prli(struct ibmvfc_target * tgt)4098 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
4099 {
4100 	struct ibmvfc_process_login *prli;
4101 	struct ibmvfc_host *vhost = tgt->vhost;
4102 	struct ibmvfc_event *evt;
4103 
4104 	if (vhost->discovery_threads >= disc_threads)
4105 		return;
4106 
4107 	kref_get(&tgt->kref);
4108 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4109 	if (!evt) {
4110 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4111 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4112 		__ibmvfc_reset_host(vhost);
4113 		return;
4114 	}
4115 	vhost->discovery_threads++;
4116 	ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
4117 	evt->tgt = tgt;
4118 	prli = &evt->iu.prli;
4119 	memset(prli, 0, sizeof(*prli));
4120 	if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4121 		prli->common.version = cpu_to_be32(2);
4122 		prli->target_wwpn = cpu_to_be64(tgt->wwpn);
4123 	} else {
4124 		prli->common.version = cpu_to_be32(1);
4125 	}
4126 	prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
4127 	prli->common.length = cpu_to_be16(sizeof(*prli));
4128 	prli->scsi_id = cpu_to_be64(tgt->scsi_id);
4129 
4130 	prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
4131 	prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
4132 	prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
4133 	prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
4134 
4135 	if (cls3_error)
4136 		prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
4137 
4138 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4139 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4140 		vhost->discovery_threads--;
4141 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4142 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4143 	} else
4144 		tgt_dbg(tgt, "Sent process login\n");
4145 }
4146 
4147 /**
4148  * ibmvfc_tgt_plogi_done - Completion handler for Port Login
4149  * @evt:	ibmvfc event struct
4150  *
4151  **/
ibmvfc_tgt_plogi_done(struct ibmvfc_event * evt)4152 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
4153 {
4154 	struct ibmvfc_target *tgt = evt->tgt;
4155 	struct ibmvfc_host *vhost = evt->vhost;
4156 	struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
4157 	u32 status = be16_to_cpu(rsp->common.status);
4158 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4159 
4160 	vhost->discovery_threads--;
4161 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4162 	switch (status) {
4163 	case IBMVFC_MAD_SUCCESS:
4164 		tgt_dbg(tgt, "Port Login succeeded\n");
4165 		if (tgt->ids.port_name &&
4166 		    tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
4167 			vhost->reinit = 1;
4168 			tgt_dbg(tgt, "Port re-init required\n");
4169 			break;
4170 		}
4171 		tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4172 		tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4173 		tgt->ids.port_id = tgt->scsi_id;
4174 		memcpy(&tgt->service_parms, &rsp->service_parms,
4175 		       sizeof(tgt->service_parms));
4176 		memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4177 		       sizeof(tgt->service_parms_change));
4178 		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4179 		break;
4180 	case IBMVFC_MAD_DRIVER_FAILED:
4181 		break;
4182 	case IBMVFC_MAD_CRQ_ERROR:
4183 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4184 		break;
4185 	case IBMVFC_MAD_FAILED:
4186 	default:
4187 		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4188 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4189 		else
4190 			ibmvfc_del_tgt(tgt);
4191 
4192 		tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4193 			ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4194 					     be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4195 			ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4196 			ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
4197 		break;
4198 	}
4199 
4200 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4201 	ibmvfc_free_event(evt);
4202 	wake_up(&vhost->work_wait_q);
4203 }
4204 
4205 /**
4206  * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
4207  * @tgt:	ibmvfc target struct
4208  *
4209  **/
ibmvfc_tgt_send_plogi(struct ibmvfc_target * tgt)4210 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
4211 {
4212 	struct ibmvfc_port_login *plogi;
4213 	struct ibmvfc_host *vhost = tgt->vhost;
4214 	struct ibmvfc_event *evt;
4215 
4216 	if (vhost->discovery_threads >= disc_threads)
4217 		return;
4218 
4219 	kref_get(&tgt->kref);
4220 	tgt->logo_rcvd = 0;
4221 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4222 	if (!evt) {
4223 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4224 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4225 		__ibmvfc_reset_host(vhost);
4226 		return;
4227 	}
4228 	vhost->discovery_threads++;
4229 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4230 	ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
4231 	evt->tgt = tgt;
4232 	plogi = &evt->iu.plogi;
4233 	memset(plogi, 0, sizeof(*plogi));
4234 	if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4235 		plogi->common.version = cpu_to_be32(2);
4236 		plogi->target_wwpn = cpu_to_be64(tgt->wwpn);
4237 	} else {
4238 		plogi->common.version = cpu_to_be32(1);
4239 	}
4240 	plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
4241 	plogi->common.length = cpu_to_be16(sizeof(*plogi));
4242 	plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
4243 
4244 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4245 		vhost->discovery_threads--;
4246 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4247 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4248 	} else
4249 		tgt_dbg(tgt, "Sent port login\n");
4250 }
4251 
4252 /**
4253  * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
4254  * @evt:	ibmvfc event struct
4255  *
4256  **/
ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event * evt)4257 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
4258 {
4259 	struct ibmvfc_target *tgt = evt->tgt;
4260 	struct ibmvfc_host *vhost = evt->vhost;
4261 	struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
4262 	u32 status = be16_to_cpu(rsp->common.status);
4263 
4264 	vhost->discovery_threads--;
4265 	ibmvfc_free_event(evt);
4266 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4267 
4268 	switch (status) {
4269 	case IBMVFC_MAD_SUCCESS:
4270 		tgt_dbg(tgt, "Implicit Logout succeeded\n");
4271 		break;
4272 	case IBMVFC_MAD_DRIVER_FAILED:
4273 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4274 		wake_up(&vhost->work_wait_q);
4275 		return;
4276 	case IBMVFC_MAD_FAILED:
4277 	default:
4278 		tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
4279 		break;
4280 	}
4281 
4282 	ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
4283 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4284 	wake_up(&vhost->work_wait_q);
4285 }
4286 
4287 /**
4288  * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
4289  * @tgt:		ibmvfc target struct
4290  * @done:		Routine to call when the event is responded to
4291  *
4292  * Returns:
4293  *	Allocated and initialized ibmvfc_event struct
4294  **/
__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target * tgt,void (* done)(struct ibmvfc_event *))4295 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
4296 								 void (*done) (struct ibmvfc_event *))
4297 {
4298 	struct ibmvfc_implicit_logout *mad;
4299 	struct ibmvfc_host *vhost = tgt->vhost;
4300 	struct ibmvfc_event *evt;
4301 
4302 	kref_get(&tgt->kref);
4303 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4304 	if (!evt)
4305 		return NULL;
4306 	ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
4307 	evt->tgt = tgt;
4308 	mad = &evt->iu.implicit_logout;
4309 	memset(mad, 0, sizeof(*mad));
4310 	mad->common.version = cpu_to_be32(1);
4311 	mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
4312 	mad->common.length = cpu_to_be16(sizeof(*mad));
4313 	mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4314 	return evt;
4315 }
4316 
4317 /**
4318  * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
4319  * @tgt:		ibmvfc target struct
4320  *
4321  **/
ibmvfc_tgt_implicit_logout(struct ibmvfc_target * tgt)4322 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
4323 {
4324 	struct ibmvfc_host *vhost = tgt->vhost;
4325 	struct ibmvfc_event *evt;
4326 
4327 	if (vhost->discovery_threads >= disc_threads)
4328 		return;
4329 
4330 	vhost->discovery_threads++;
4331 	evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4332 						   ibmvfc_tgt_implicit_logout_done);
4333 	if (!evt) {
4334 		vhost->discovery_threads--;
4335 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4336 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4337 		__ibmvfc_reset_host(vhost);
4338 		return;
4339 	}
4340 
4341 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4342 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4343 		vhost->discovery_threads--;
4344 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4345 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4346 	} else
4347 		tgt_dbg(tgt, "Sent Implicit Logout\n");
4348 }
4349 
4350 /**
4351  * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
4352  * @evt:	ibmvfc event struct
4353  *
4354  **/
ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event * evt)4355 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
4356 {
4357 	struct ibmvfc_target *tgt = evt->tgt;
4358 	struct ibmvfc_host *vhost = evt->vhost;
4359 	struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4360 	u32 status = be16_to_cpu(mad->common.status);
4361 
4362 	vhost->discovery_threads--;
4363 	ibmvfc_free_event(evt);
4364 
4365 	/*
4366 	 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
4367 	 * driver in which case we need to free up all the targets. If we are
4368 	 * not unloading, we will still go through a hard reset to get out of
4369 	 * offline state, so there is no need to track the old targets in that
4370 	 * case.
4371 	 */
4372 	if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
4373 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4374 	else
4375 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
4376 
4377 	tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
4378 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4379 	wake_up(&vhost->work_wait_q);
4380 }
4381 
4382 /**
4383  * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
4384  * @tgt:		ibmvfc target struct
4385  *
4386  **/
ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target * tgt)4387 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
4388 {
4389 	struct ibmvfc_host *vhost = tgt->vhost;
4390 	struct ibmvfc_event *evt;
4391 
4392 	if (!vhost->logged_in) {
4393 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4394 		return;
4395 	}
4396 
4397 	if (vhost->discovery_threads >= disc_threads)
4398 		return;
4399 
4400 	vhost->discovery_threads++;
4401 	evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4402 						   ibmvfc_tgt_implicit_logout_and_del_done);
4403 
4404 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
4405 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4406 		vhost->discovery_threads--;
4407 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4408 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4409 	} else
4410 		tgt_dbg(tgt, "Sent Implicit Logout\n");
4411 }
4412 
4413 /**
4414  * ibmvfc_tgt_move_login_done - Completion handler for Move Login
4415  * @evt:	ibmvfc event struct
4416  *
4417  **/
ibmvfc_tgt_move_login_done(struct ibmvfc_event * evt)4418 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
4419 {
4420 	struct ibmvfc_target *tgt = evt->tgt;
4421 	struct ibmvfc_host *vhost = evt->vhost;
4422 	struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
4423 	u32 status = be16_to_cpu(rsp->common.status);
4424 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4425 
4426 	vhost->discovery_threads--;
4427 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4428 	switch (status) {
4429 	case IBMVFC_MAD_SUCCESS:
4430 		tgt_dbg(tgt, "Move Login succeeded for new scsi_id: %llX\n", tgt->new_scsi_id);
4431 		tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4432 		tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4433 		tgt->scsi_id = tgt->new_scsi_id;
4434 		tgt->ids.port_id = tgt->scsi_id;
4435 		memcpy(&tgt->service_parms, &rsp->service_parms,
4436 		       sizeof(tgt->service_parms));
4437 		memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4438 		       sizeof(tgt->service_parms_change));
4439 		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4440 		break;
4441 	case IBMVFC_MAD_DRIVER_FAILED:
4442 		break;
4443 	case IBMVFC_MAD_CRQ_ERROR:
4444 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4445 		break;
4446 	case IBMVFC_MAD_FAILED:
4447 	default:
4448 		level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4449 
4450 		tgt_log(tgt, level,
4451 			"Move Login failed: new scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
4452 			tgt->new_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
4453 			status);
4454 		break;
4455 	}
4456 
4457 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4458 	ibmvfc_free_event(evt);
4459 	wake_up(&vhost->work_wait_q);
4460 }
4461 
4462 
4463 /**
4464  * ibmvfc_tgt_move_login - Initiate a move login for specified target
4465  * @tgt:		ibmvfc target struct
4466  *
4467  **/
ibmvfc_tgt_move_login(struct ibmvfc_target * tgt)4468 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
4469 {
4470 	struct ibmvfc_host *vhost = tgt->vhost;
4471 	struct ibmvfc_move_login *move;
4472 	struct ibmvfc_event *evt;
4473 
4474 	if (vhost->discovery_threads >= disc_threads)
4475 		return;
4476 
4477 	kref_get(&tgt->kref);
4478 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4479 	if (!evt) {
4480 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4481 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4482 		__ibmvfc_reset_host(vhost);
4483 		return;
4484 	}
4485 	vhost->discovery_threads++;
4486 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4487 	ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
4488 	evt->tgt = tgt;
4489 	move = &evt->iu.move_login;
4490 	memset(move, 0, sizeof(*move));
4491 	move->common.version = cpu_to_be32(1);
4492 	move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
4493 	move->common.length = cpu_to_be16(sizeof(*move));
4494 
4495 	move->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4496 	move->new_scsi_id = cpu_to_be64(tgt->new_scsi_id);
4497 	move->wwpn = cpu_to_be64(tgt->wwpn);
4498 	move->node_name = cpu_to_be64(tgt->ids.node_name);
4499 
4500 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4501 		vhost->discovery_threads--;
4502 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4503 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4504 	} else
4505 		tgt_dbg(tgt, "Sent Move Login for new scsi_id: %llX\n", tgt->new_scsi_id);
4506 }
4507 
4508 /**
4509  * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
4510  * @mad:	ibmvfc passthru mad struct
4511  * @tgt:	ibmvfc target struct
4512  *
4513  * Returns:
4514  *	1 if PLOGI needed / 0 if PLOGI not needed
4515  **/
ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad * mad,struct ibmvfc_target * tgt)4516 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
4517 				    struct ibmvfc_target *tgt)
4518 {
4519 	if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
4520 		return 1;
4521 	if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
4522 		return 1;
4523 	if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
4524 		return 1;
4525 	return 0;
4526 }
4527 
4528 /**
4529  * ibmvfc_tgt_adisc_done - Completion handler for ADISC
4530  * @evt:	ibmvfc event struct
4531  *
4532  **/
ibmvfc_tgt_adisc_done(struct ibmvfc_event * evt)4533 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
4534 {
4535 	struct ibmvfc_target *tgt = evt->tgt;
4536 	struct ibmvfc_host *vhost = evt->vhost;
4537 	struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4538 	u32 status = be16_to_cpu(mad->common.status);
4539 	u8 fc_reason, fc_explain;
4540 
4541 	vhost->discovery_threads--;
4542 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4543 	del_timer(&tgt->timer);
4544 
4545 	switch (status) {
4546 	case IBMVFC_MAD_SUCCESS:
4547 		tgt_dbg(tgt, "ADISC succeeded\n");
4548 		if (ibmvfc_adisc_needs_plogi(mad, tgt))
4549 			ibmvfc_del_tgt(tgt);
4550 		break;
4551 	case IBMVFC_MAD_DRIVER_FAILED:
4552 		break;
4553 	case IBMVFC_MAD_FAILED:
4554 	default:
4555 		ibmvfc_del_tgt(tgt);
4556 		fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
4557 		fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
4558 		tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4559 			 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
4560 			 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
4561 			 ibmvfc_get_fc_type(fc_reason), fc_reason,
4562 			 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
4563 		break;
4564 	}
4565 
4566 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4567 	ibmvfc_free_event(evt);
4568 	wake_up(&vhost->work_wait_q);
4569 }
4570 
4571 /**
4572  * ibmvfc_init_passthru - Initialize an event struct for FC passthru
4573  * @evt:		ibmvfc event struct
4574  *
4575  **/
ibmvfc_init_passthru(struct ibmvfc_event * evt)4576 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
4577 {
4578 	struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
4579 
4580 	memset(mad, 0, sizeof(*mad));
4581 	mad->common.version = cpu_to_be32(1);
4582 	mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
4583 	mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
4584 	mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4585 		offsetof(struct ibmvfc_passthru_mad, iu));
4586 	mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
4587 	mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4588 	mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
4589 	mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4590 		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4591 		offsetof(struct ibmvfc_passthru_fc_iu, payload));
4592 	mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4593 	mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4594 		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4595 		offsetof(struct ibmvfc_passthru_fc_iu, response));
4596 	mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
4597 }
4598 
4599 /**
4600  * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
4601  * @evt:		ibmvfc event struct
4602  *
4603  * Just cleanup this event struct. Everything else is handled by
4604  * the ADISC completion handler. If the ADISC never actually comes
4605  * back, we still have the timer running on the ADISC event struct
4606  * which will fire and cause the CRQ to get reset.
4607  *
4608  **/
ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event * evt)4609 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
4610 {
4611 	struct ibmvfc_host *vhost = evt->vhost;
4612 	struct ibmvfc_target *tgt = evt->tgt;
4613 
4614 	tgt_dbg(tgt, "ADISC cancel complete\n");
4615 	vhost->abort_threads--;
4616 	ibmvfc_free_event(evt);
4617 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4618 	wake_up(&vhost->work_wait_q);
4619 }
4620 
4621 /**
4622  * ibmvfc_adisc_timeout - Handle an ADISC timeout
4623  * @t:		ibmvfc target struct
4624  *
4625  * If an ADISC times out, send a cancel. If the cancel times
4626  * out, reset the CRQ. When the ADISC comes back as cancelled,
4627  * log back into the target.
4628  **/
ibmvfc_adisc_timeout(struct timer_list * t)4629 static void ibmvfc_adisc_timeout(struct timer_list *t)
4630 {
4631 	struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
4632 	struct ibmvfc_host *vhost = tgt->vhost;
4633 	struct ibmvfc_event *evt;
4634 	struct ibmvfc_tmf *tmf;
4635 	unsigned long flags;
4636 	int rc;
4637 
4638 	tgt_dbg(tgt, "ADISC timeout\n");
4639 	spin_lock_irqsave(vhost->host->host_lock, flags);
4640 	if (vhost->abort_threads >= disc_threads ||
4641 	    tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
4642 	    vhost->state != IBMVFC_INITIALIZING ||
4643 	    vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
4644 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4645 		return;
4646 	}
4647 
4648 	vhost->abort_threads++;
4649 	kref_get(&tgt->kref);
4650 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4651 	if (!evt) {
4652 		tgt_err(tgt, "Failed to get cancel event for ADISC.\n");
4653 		vhost->abort_threads--;
4654 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4655 		__ibmvfc_reset_host(vhost);
4656 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4657 		return;
4658 	}
4659 	ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
4660 
4661 	evt->tgt = tgt;
4662 	tmf = &evt->iu.tmf;
4663 	memset(tmf, 0, sizeof(*tmf));
4664 	if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4665 		tmf->common.version = cpu_to_be32(2);
4666 		tmf->target_wwpn = cpu_to_be64(tgt->wwpn);
4667 	} else {
4668 		tmf->common.version = cpu_to_be32(1);
4669 	}
4670 	tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
4671 	tmf->common.length = cpu_to_be16(sizeof(*tmf));
4672 	tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
4673 	tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
4674 
4675 	rc = ibmvfc_send_event(evt, vhost, default_timeout);
4676 
4677 	if (rc) {
4678 		tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
4679 		vhost->abort_threads--;
4680 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4681 		__ibmvfc_reset_host(vhost);
4682 	} else
4683 		tgt_dbg(tgt, "Attempting to cancel ADISC\n");
4684 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4685 }
4686 
4687 /**
4688  * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
4689  * @tgt:		ibmvfc target struct
4690  *
4691  * When sending an ADISC we end up with two timers running. The
4692  * first timer is the timer in the ibmvfc target struct. If this
4693  * fires, we send a cancel to the target. The second timer is the
4694  * timer on the ibmvfc event for the ADISC, which is longer. If that
4695  * fires, it means the ADISC timed out and our attempt to cancel it
4696  * also failed, so we need to reset the CRQ.
4697  **/
ibmvfc_tgt_adisc(struct ibmvfc_target * tgt)4698 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
4699 {
4700 	struct ibmvfc_passthru_mad *mad;
4701 	struct ibmvfc_host *vhost = tgt->vhost;
4702 	struct ibmvfc_event *evt;
4703 
4704 	if (vhost->discovery_threads >= disc_threads)
4705 		return;
4706 
4707 	kref_get(&tgt->kref);
4708 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4709 	if (!evt) {
4710 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4711 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4712 		__ibmvfc_reset_host(vhost);
4713 		return;
4714 	}
4715 	vhost->discovery_threads++;
4716 	ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
4717 	evt->tgt = tgt;
4718 
4719 	ibmvfc_init_passthru(evt);
4720 	mad = &evt->iu.passthru;
4721 	mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4722 	mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4723 	mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4724 
4725 	mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4726 	memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4727 	       sizeof(vhost->login_buf->resp.port_name));
4728 	memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4729 	       sizeof(vhost->login_buf->resp.node_name));
4730 	mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4731 
4732 	if (timer_pending(&tgt->timer))
4733 		mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4734 	else {
4735 		tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4736 		add_timer(&tgt->timer);
4737 	}
4738 
4739 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4740 	if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4741 		vhost->discovery_threads--;
4742 		del_timer(&tgt->timer);
4743 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4744 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4745 	} else
4746 		tgt_dbg(tgt, "Sent ADISC\n");
4747 }
4748 
4749 /**
4750  * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4751  * @evt:	ibmvfc event struct
4752  *
4753  **/
ibmvfc_tgt_query_target_done(struct ibmvfc_event * evt)4754 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4755 {
4756 	struct ibmvfc_target *tgt = evt->tgt;
4757 	struct ibmvfc_host *vhost = evt->vhost;
4758 	struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4759 	u32 status = be16_to_cpu(rsp->common.status);
4760 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4761 
4762 	vhost->discovery_threads--;
4763 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4764 	switch (status) {
4765 	case IBMVFC_MAD_SUCCESS:
4766 		tgt_dbg(tgt, "Query Target succeeded\n");
4767 		if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4768 			ibmvfc_del_tgt(tgt);
4769 		else
4770 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4771 		break;
4772 	case IBMVFC_MAD_DRIVER_FAILED:
4773 		break;
4774 	case IBMVFC_MAD_CRQ_ERROR:
4775 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4776 		break;
4777 	case IBMVFC_MAD_FAILED:
4778 	default:
4779 		if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4780 		    be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4781 		    be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4782 			ibmvfc_del_tgt(tgt);
4783 		else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4784 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4785 		else
4786 			ibmvfc_del_tgt(tgt);
4787 
4788 		tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4789 			ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4790 			be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4791 			ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4792 			ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4793 			status);
4794 		break;
4795 	}
4796 
4797 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4798 	ibmvfc_free_event(evt);
4799 	wake_up(&vhost->work_wait_q);
4800 }
4801 
4802 /**
4803  * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4804  * @tgt:	ibmvfc target struct
4805  *
4806  **/
ibmvfc_tgt_query_target(struct ibmvfc_target * tgt)4807 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4808 {
4809 	struct ibmvfc_query_tgt *query_tgt;
4810 	struct ibmvfc_host *vhost = tgt->vhost;
4811 	struct ibmvfc_event *evt;
4812 
4813 	if (vhost->discovery_threads >= disc_threads)
4814 		return;
4815 
4816 	kref_get(&tgt->kref);
4817 	evt = ibmvfc_get_reserved_event(&vhost->crq);
4818 	if (!evt) {
4819 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4820 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4821 		__ibmvfc_reset_host(vhost);
4822 		return;
4823 	}
4824 	vhost->discovery_threads++;
4825 	evt->tgt = tgt;
4826 	ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4827 	query_tgt = &evt->iu.query_tgt;
4828 	memset(query_tgt, 0, sizeof(*query_tgt));
4829 	query_tgt->common.version = cpu_to_be32(1);
4830 	query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4831 	query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4832 	query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4833 
4834 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4835 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4836 		vhost->discovery_threads--;
4837 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4838 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4839 	} else
4840 		tgt_dbg(tgt, "Sent Query Target\n");
4841 }
4842 
4843 /**
4844  * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4845  * @vhost:		ibmvfc host struct
4846  * @target:		Holds SCSI ID to allocate target forand the WWPN
4847  *
4848  * Returns:
4849  *	0 on success / other on failure
4850  **/
ibmvfc_alloc_target(struct ibmvfc_host * vhost,struct ibmvfc_discover_targets_entry * target)4851 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4852 			       struct ibmvfc_discover_targets_entry *target)
4853 {
4854 	struct ibmvfc_target *stgt = NULL;
4855 	struct ibmvfc_target *wtgt = NULL;
4856 	struct ibmvfc_target *tgt;
4857 	unsigned long flags;
4858 	u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4859 	u64 wwpn = be64_to_cpu(target->wwpn);
4860 
4861 	/* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4862 	spin_lock_irqsave(vhost->host->host_lock, flags);
4863 	list_for_each_entry(tgt, &vhost->targets, queue) {
4864 		if (tgt->wwpn == wwpn) {
4865 			wtgt = tgt;
4866 			break;
4867 		}
4868 	}
4869 
4870 	list_for_each_entry(tgt, &vhost->targets, queue) {
4871 		if (tgt->scsi_id == scsi_id) {
4872 			stgt = tgt;
4873 			break;
4874 		}
4875 	}
4876 
4877 	if (wtgt && !stgt) {
4878 		/*
4879 		 * A WWPN target has moved and we still are tracking the old
4880 		 * SCSI ID.  The only way we should be able to get here is if
4881 		 * we attempted to send an implicit logout for the old SCSI ID
4882 		 * and it failed for some reason, such as there being I/O
4883 		 * pending to the target. In this case, we will have already
4884 		 * deleted the rport from the FC transport so we do a move
4885 		 * login, which works even with I/O pending, however, if
4886 		 * there is still I/O pending, it will stay outstanding, so
4887 		 * we only do this if fast fail is disabled for the rport,
4888 		 * otherwise we let terminate_rport_io clean up the port
4889 		 * before we login at the new location.
4890 		 */
4891 		if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4892 			if (wtgt->move_login) {
4893 				/*
4894 				 * Do a move login here. The old target is no longer
4895 				 * known to the transport layer We don't use the
4896 				 * normal ibmvfc_set_tgt_action to set this, as we
4897 				 * don't normally want to allow this state change.
4898 				 */
4899 				wtgt->new_scsi_id = scsi_id;
4900 				wtgt->action = IBMVFC_TGT_ACTION_INIT;
4901 				wtgt->init_retries = 0;
4902 				ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4903 			}
4904 			goto unlock_out;
4905 		} else {
4906 			tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4907 				wtgt->action, wtgt->rport);
4908 		}
4909 	} else if (stgt) {
4910 		if (tgt->need_login)
4911 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4912 		goto unlock_out;
4913 	}
4914 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4915 
4916 	tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4917 	memset(tgt, 0, sizeof(*tgt));
4918 	tgt->scsi_id = scsi_id;
4919 	tgt->wwpn = wwpn;
4920 	tgt->vhost = vhost;
4921 	tgt->need_login = 1;
4922 	timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4923 	kref_init(&tgt->kref);
4924 	ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4925 	spin_lock_irqsave(vhost->host->host_lock, flags);
4926 	tgt->cancel_key = vhost->task_set++;
4927 	list_add_tail(&tgt->queue, &vhost->targets);
4928 
4929 unlock_out:
4930 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4931 	return 0;
4932 }
4933 
4934 /**
4935  * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4936  * @vhost:		ibmvfc host struct
4937  *
4938  * Returns:
4939  *	0 on success / other on failure
4940  **/
ibmvfc_alloc_targets(struct ibmvfc_host * vhost)4941 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4942 {
4943 	int i, rc;
4944 
4945 	for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4946 		rc = ibmvfc_alloc_target(vhost, &vhost->scsi_scrqs.disc_buf[i]);
4947 
4948 	return rc;
4949 }
4950 
4951 /**
4952  * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4953  * @evt:	ibmvfc event struct
4954  *
4955  **/
ibmvfc_discover_targets_done(struct ibmvfc_event * evt)4956 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4957 {
4958 	struct ibmvfc_host *vhost = evt->vhost;
4959 	struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4960 	u32 mad_status = be16_to_cpu(rsp->common.status);
4961 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4962 
4963 	switch (mad_status) {
4964 	case IBMVFC_MAD_SUCCESS:
4965 		ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4966 		vhost->num_targets = be32_to_cpu(rsp->num_written);
4967 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4968 		break;
4969 	case IBMVFC_MAD_FAILED:
4970 		level += ibmvfc_retry_host_init(vhost);
4971 		ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4972 			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4973 			   be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4974 		break;
4975 	case IBMVFC_MAD_DRIVER_FAILED:
4976 		break;
4977 	default:
4978 		dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4979 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4980 		break;
4981 	}
4982 
4983 	ibmvfc_free_event(evt);
4984 	wake_up(&vhost->work_wait_q);
4985 }
4986 
4987 /**
4988  * ibmvfc_discover_targets - Send Discover Targets MAD
4989  * @vhost:	ibmvfc host struct
4990  *
4991  **/
ibmvfc_discover_targets(struct ibmvfc_host * vhost)4992 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4993 {
4994 	struct ibmvfc_discover_targets *mad;
4995 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
4996 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4997 
4998 	if (!evt) {
4999 		ibmvfc_log(vhost, level, "Discover Targets failed: no available events\n");
5000 		ibmvfc_hard_reset_host(vhost);
5001 		return;
5002 	}
5003 
5004 	ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
5005 	mad = &evt->iu.discover_targets;
5006 	memset(mad, 0, sizeof(*mad));
5007 	mad->common.version = cpu_to_be32(1);
5008 	mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
5009 	mad->common.length = cpu_to_be16(sizeof(*mad));
5010 	mad->bufflen = cpu_to_be32(vhost->scsi_scrqs.disc_buf_sz);
5011 	mad->buffer.va = cpu_to_be64(vhost->scsi_scrqs.disc_buf_dma);
5012 	mad->buffer.len = cpu_to_be32(vhost->scsi_scrqs.disc_buf_sz);
5013 	mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
5014 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5015 
5016 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5017 		ibmvfc_dbg(vhost, "Sent discover targets\n");
5018 	else
5019 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5020 }
5021 
ibmvfc_channel_setup_done(struct ibmvfc_event * evt)5022 static void ibmvfc_channel_setup_done(struct ibmvfc_event *evt)
5023 {
5024 	struct ibmvfc_host *vhost = evt->vhost;
5025 	struct ibmvfc_channel_setup *setup = vhost->channel_setup_buf;
5026 	struct ibmvfc_channels *scrqs = &vhost->scsi_scrqs;
5027 	u32 mad_status = be16_to_cpu(evt->xfer_iu->channel_setup.common.status);
5028 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5029 	int flags, active_queues, i;
5030 
5031 	ibmvfc_free_event(evt);
5032 
5033 	switch (mad_status) {
5034 	case IBMVFC_MAD_SUCCESS:
5035 		ibmvfc_dbg(vhost, "Channel Setup succeeded\n");
5036 		flags = be32_to_cpu(setup->flags);
5037 		vhost->do_enquiry = 0;
5038 		active_queues = be32_to_cpu(setup->num_scsi_subq_channels);
5039 		scrqs->active_queues = active_queues;
5040 
5041 		if (flags & IBMVFC_CHANNELS_CANCELED) {
5042 			ibmvfc_dbg(vhost, "Channels Canceled\n");
5043 			vhost->using_channels = 0;
5044 		} else {
5045 			if (active_queues)
5046 				vhost->using_channels = 1;
5047 			for (i = 0; i < active_queues; i++)
5048 				scrqs->scrqs[i].vios_cookie =
5049 					be64_to_cpu(setup->channel_handles[i]);
5050 
5051 			ibmvfc_dbg(vhost, "Using %u channels\n",
5052 				   vhost->scsi_scrqs.active_queues);
5053 		}
5054 		break;
5055 	case IBMVFC_MAD_FAILED:
5056 		level += ibmvfc_retry_host_init(vhost);
5057 		ibmvfc_log(vhost, level, "Channel Setup failed\n");
5058 		fallthrough;
5059 	case IBMVFC_MAD_DRIVER_FAILED:
5060 		return;
5061 	default:
5062 		dev_err(vhost->dev, "Invalid Channel Setup response: 0x%x\n",
5063 			mad_status);
5064 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5065 		return;
5066 	}
5067 
5068 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5069 	wake_up(&vhost->work_wait_q);
5070 }
5071 
ibmvfc_channel_setup(struct ibmvfc_host * vhost)5072 static void ibmvfc_channel_setup(struct ibmvfc_host *vhost)
5073 {
5074 	struct ibmvfc_channel_setup_mad *mad;
5075 	struct ibmvfc_channel_setup *setup_buf = vhost->channel_setup_buf;
5076 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
5077 	struct ibmvfc_channels *scrqs = &vhost->scsi_scrqs;
5078 	unsigned int num_channels =
5079 		min(scrqs->desired_queues, vhost->max_vios_scsi_channels);
5080 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5081 	int i;
5082 
5083 	if (!evt) {
5084 		ibmvfc_log(vhost, level, "Channel Setup failed: no available events\n");
5085 		ibmvfc_hard_reset_host(vhost);
5086 		return;
5087 	}
5088 
5089 	memset(setup_buf, 0, sizeof(*setup_buf));
5090 	if (num_channels == 0)
5091 		setup_buf->flags = cpu_to_be32(IBMVFC_CANCEL_CHANNELS);
5092 	else {
5093 		setup_buf->num_scsi_subq_channels = cpu_to_be32(num_channels);
5094 		for (i = 0; i < num_channels; i++)
5095 			setup_buf->channel_handles[i] = cpu_to_be64(scrqs->scrqs[i].cookie);
5096 	}
5097 
5098 	ibmvfc_init_event(evt, ibmvfc_channel_setup_done, IBMVFC_MAD_FORMAT);
5099 	mad = &evt->iu.channel_setup;
5100 	memset(mad, 0, sizeof(*mad));
5101 	mad->common.version = cpu_to_be32(1);
5102 	mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_SETUP);
5103 	mad->common.length = cpu_to_be16(sizeof(*mad));
5104 	mad->buffer.va = cpu_to_be64(vhost->channel_setup_dma);
5105 	mad->buffer.len = cpu_to_be32(sizeof(*vhost->channel_setup_buf));
5106 
5107 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5108 
5109 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5110 		ibmvfc_dbg(vhost, "Sent channel setup\n");
5111 	else
5112 		ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
5113 }
5114 
ibmvfc_channel_enquiry_done(struct ibmvfc_event * evt)5115 static void ibmvfc_channel_enquiry_done(struct ibmvfc_event *evt)
5116 {
5117 	struct ibmvfc_host *vhost = evt->vhost;
5118 	struct ibmvfc_channel_enquiry *rsp = &evt->xfer_iu->channel_enquiry;
5119 	u32 mad_status = be16_to_cpu(rsp->common.status);
5120 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5121 
5122 	switch (mad_status) {
5123 	case IBMVFC_MAD_SUCCESS:
5124 		ibmvfc_dbg(vhost, "Channel Enquiry succeeded\n");
5125 		vhost->max_vios_scsi_channels = be32_to_cpu(rsp->num_scsi_subq_channels);
5126 		ibmvfc_free_event(evt);
5127 		break;
5128 	case IBMVFC_MAD_FAILED:
5129 		level += ibmvfc_retry_host_init(vhost);
5130 		ibmvfc_log(vhost, level, "Channel Enquiry failed\n");
5131 		fallthrough;
5132 	case IBMVFC_MAD_DRIVER_FAILED:
5133 		ibmvfc_free_event(evt);
5134 		return;
5135 	default:
5136 		dev_err(vhost->dev, "Invalid Channel Enquiry response: 0x%x\n",
5137 			mad_status);
5138 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5139 		ibmvfc_free_event(evt);
5140 		return;
5141 	}
5142 
5143 	ibmvfc_channel_setup(vhost);
5144 }
5145 
ibmvfc_channel_enquiry(struct ibmvfc_host * vhost)5146 static void ibmvfc_channel_enquiry(struct ibmvfc_host *vhost)
5147 {
5148 	struct ibmvfc_channel_enquiry *mad;
5149 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
5150 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5151 
5152 	if (!evt) {
5153 		ibmvfc_log(vhost, level, "Channel Enquiry failed: no available events\n");
5154 		ibmvfc_hard_reset_host(vhost);
5155 		return;
5156 	}
5157 
5158 	ibmvfc_init_event(evt, ibmvfc_channel_enquiry_done, IBMVFC_MAD_FORMAT);
5159 	mad = &evt->iu.channel_enquiry;
5160 	memset(mad, 0, sizeof(*mad));
5161 	mad->common.version = cpu_to_be32(1);
5162 	mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_ENQUIRY);
5163 	mad->common.length = cpu_to_be16(sizeof(*mad));
5164 
5165 	if (mig_channels_only)
5166 		mad->flags |= cpu_to_be32(IBMVFC_NO_CHANNELS_TO_CRQ_SUPPORT);
5167 	if (mig_no_less_channels)
5168 		mad->flags |= cpu_to_be32(IBMVFC_NO_N_TO_M_CHANNELS_SUPPORT);
5169 
5170 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5171 
5172 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5173 		ibmvfc_dbg(vhost, "Send channel enquiry\n");
5174 	else
5175 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5176 }
5177 
5178 /**
5179  * ibmvfc_npiv_login_done - Completion handler for NPIV Login
5180  * @evt:	ibmvfc event struct
5181  *
5182  **/
ibmvfc_npiv_login_done(struct ibmvfc_event * evt)5183 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
5184 {
5185 	struct ibmvfc_host *vhost = evt->vhost;
5186 	u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
5187 	struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
5188 	unsigned int npiv_max_sectors;
5189 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
5190 
5191 	switch (mad_status) {
5192 	case IBMVFC_MAD_SUCCESS:
5193 		ibmvfc_free_event(evt);
5194 		break;
5195 	case IBMVFC_MAD_FAILED:
5196 		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
5197 			level += ibmvfc_retry_host_init(vhost);
5198 		else
5199 			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5200 		ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
5201 			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
5202 						be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
5203 		ibmvfc_free_event(evt);
5204 		return;
5205 	case IBMVFC_MAD_CRQ_ERROR:
5206 		ibmvfc_retry_host_init(vhost);
5207 		fallthrough;
5208 	case IBMVFC_MAD_DRIVER_FAILED:
5209 		ibmvfc_free_event(evt);
5210 		return;
5211 	default:
5212 		dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
5213 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5214 		ibmvfc_free_event(evt);
5215 		return;
5216 	}
5217 
5218 	vhost->client_migrated = 0;
5219 
5220 	if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
5221 		dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
5222 			rsp->flags);
5223 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5224 		wake_up(&vhost->work_wait_q);
5225 		return;
5226 	}
5227 
5228 	if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
5229 		dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
5230 			rsp->max_cmds);
5231 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5232 		wake_up(&vhost->work_wait_q);
5233 		return;
5234 	}
5235 
5236 	vhost->logged_in = 1;
5237 	npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), max_sectors);
5238 	dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
5239 		 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
5240 		 rsp->drc_name, npiv_max_sectors);
5241 
5242 	fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
5243 	fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
5244 	fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
5245 	fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
5246 	fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
5247 	fc_host_supported_classes(vhost->host) = 0;
5248 	if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
5249 		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
5250 	if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
5251 		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
5252 	if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
5253 		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
5254 	fc_host_maxframe_size(vhost->host) =
5255 		be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
5256 
5257 	vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
5258 	vhost->host->max_sectors = npiv_max_sectors;
5259 
5260 	if (ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPORT_CHANNELS) && vhost->do_enquiry) {
5261 		ibmvfc_channel_enquiry(vhost);
5262 	} else {
5263 		vhost->do_enquiry = 0;
5264 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5265 		wake_up(&vhost->work_wait_q);
5266 	}
5267 }
5268 
5269 /**
5270  * ibmvfc_npiv_login - Sends NPIV login
5271  * @vhost:	ibmvfc host struct
5272  *
5273  **/
ibmvfc_npiv_login(struct ibmvfc_host * vhost)5274 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
5275 {
5276 	struct ibmvfc_npiv_login_mad *mad;
5277 	struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
5278 
5279 	if (!evt) {
5280 		ibmvfc_dbg(vhost, "NPIV Login failed: no available events\n");
5281 		ibmvfc_hard_reset_host(vhost);
5282 		return;
5283 	}
5284 
5285 	ibmvfc_gather_partition_info(vhost);
5286 	ibmvfc_set_login_info(vhost);
5287 	ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
5288 
5289 	memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
5290 	mad = &evt->iu.npiv_login;
5291 	memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
5292 	mad->common.version = cpu_to_be32(1);
5293 	mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
5294 	mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
5295 	mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
5296 	mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
5297 
5298 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5299 
5300 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5301 		ibmvfc_dbg(vhost, "Sent NPIV login\n");
5302 	else
5303 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5304 }
5305 
5306 /**
5307  * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
5308  * @evt:		ibmvfc event struct
5309  *
5310  **/
ibmvfc_npiv_logout_done(struct ibmvfc_event * evt)5311 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
5312 {
5313 	struct ibmvfc_host *vhost = evt->vhost;
5314 	u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
5315 
5316 	ibmvfc_free_event(evt);
5317 
5318 	switch (mad_status) {
5319 	case IBMVFC_MAD_SUCCESS:
5320 		if (list_empty(&vhost->crq.sent) &&
5321 		    vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
5322 			ibmvfc_init_host(vhost);
5323 			return;
5324 		}
5325 		break;
5326 	case IBMVFC_MAD_FAILED:
5327 	case IBMVFC_MAD_NOT_SUPPORTED:
5328 	case IBMVFC_MAD_CRQ_ERROR:
5329 	case IBMVFC_MAD_DRIVER_FAILED:
5330 	default:
5331 		ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
5332 		break;
5333 	}
5334 
5335 	ibmvfc_hard_reset_host(vhost);
5336 }
5337 
5338 /**
5339  * ibmvfc_npiv_logout - Issue an NPIV Logout
5340  * @vhost:		ibmvfc host struct
5341  *
5342  **/
ibmvfc_npiv_logout(struct ibmvfc_host * vhost)5343 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
5344 {
5345 	struct ibmvfc_npiv_logout_mad *mad;
5346 	struct ibmvfc_event *evt;
5347 
5348 	evt = ibmvfc_get_reserved_event(&vhost->crq);
5349 	if (!evt) {
5350 		ibmvfc_dbg(vhost, "NPIV Logout failed: no available events\n");
5351 		ibmvfc_hard_reset_host(vhost);
5352 		return;
5353 	}
5354 
5355 	ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
5356 
5357 	mad = &evt->iu.npiv_logout;
5358 	memset(mad, 0, sizeof(*mad));
5359 	mad->common.version = cpu_to_be32(1);
5360 	mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
5361 	mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
5362 
5363 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
5364 
5365 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
5366 		ibmvfc_dbg(vhost, "Sent NPIV logout\n");
5367 	else
5368 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5369 }
5370 
5371 /**
5372  * ibmvfc_dev_init_to_do - Is there target initialization work to do?
5373  * @vhost:		ibmvfc host struct
5374  *
5375  * Returns:
5376  *	1 if work to do / 0 if not
5377  **/
ibmvfc_dev_init_to_do(struct ibmvfc_host * vhost)5378 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
5379 {
5380 	struct ibmvfc_target *tgt;
5381 
5382 	list_for_each_entry(tgt, &vhost->targets, queue) {
5383 		if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
5384 		    tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5385 			return 1;
5386 	}
5387 
5388 	return 0;
5389 }
5390 
5391 /**
5392  * ibmvfc_dev_logo_to_do - Is there target logout work to do?
5393  * @vhost:		ibmvfc host struct
5394  *
5395  * Returns:
5396  *	1 if work to do / 0 if not
5397  **/
ibmvfc_dev_logo_to_do(struct ibmvfc_host * vhost)5398 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
5399 {
5400 	struct ibmvfc_target *tgt;
5401 
5402 	list_for_each_entry(tgt, &vhost->targets, queue) {
5403 		if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
5404 		    tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5405 			return 1;
5406 	}
5407 	return 0;
5408 }
5409 
5410 /**
5411  * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
5412  * @vhost:		ibmvfc host struct
5413  *
5414  * Returns:
5415  *	1 if work to do / 0 if not
5416  **/
__ibmvfc_work_to_do(struct ibmvfc_host * vhost)5417 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5418 {
5419 	struct ibmvfc_target *tgt;
5420 
5421 	if (kthread_should_stop())
5422 		return 1;
5423 	switch (vhost->action) {
5424 	case IBMVFC_HOST_ACTION_NONE:
5425 	case IBMVFC_HOST_ACTION_INIT_WAIT:
5426 	case IBMVFC_HOST_ACTION_LOGO_WAIT:
5427 		return 0;
5428 	case IBMVFC_HOST_ACTION_TGT_INIT:
5429 	case IBMVFC_HOST_ACTION_QUERY_TGTS:
5430 		if (vhost->discovery_threads == disc_threads)
5431 			return 0;
5432 		list_for_each_entry(tgt, &vhost->targets, queue)
5433 			if (tgt->action == IBMVFC_TGT_ACTION_INIT)
5434 				return 1;
5435 		list_for_each_entry(tgt, &vhost->targets, queue)
5436 			if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5437 				return 0;
5438 		return 1;
5439 	case IBMVFC_HOST_ACTION_TGT_DEL:
5440 	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5441 		if (vhost->discovery_threads == disc_threads)
5442 			return 0;
5443 		list_for_each_entry(tgt, &vhost->targets, queue)
5444 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
5445 				return 1;
5446 		list_for_each_entry(tgt, &vhost->targets, queue)
5447 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5448 				return 0;
5449 		return 1;
5450 	case IBMVFC_HOST_ACTION_LOGO:
5451 	case IBMVFC_HOST_ACTION_INIT:
5452 	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5453 	case IBMVFC_HOST_ACTION_QUERY:
5454 	case IBMVFC_HOST_ACTION_RESET:
5455 	case IBMVFC_HOST_ACTION_REENABLE:
5456 	default:
5457 		break;
5458 	}
5459 
5460 	return 1;
5461 }
5462 
5463 /**
5464  * ibmvfc_work_to_do - Is there task level work to do?
5465  * @vhost:		ibmvfc host struct
5466  *
5467  * Returns:
5468  *	1 if work to do / 0 if not
5469  **/
ibmvfc_work_to_do(struct ibmvfc_host * vhost)5470 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5471 {
5472 	unsigned long flags;
5473 	int rc;
5474 
5475 	spin_lock_irqsave(vhost->host->host_lock, flags);
5476 	rc = __ibmvfc_work_to_do(vhost);
5477 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5478 	return rc;
5479 }
5480 
5481 /**
5482  * ibmvfc_log_ae - Log async events if necessary
5483  * @vhost:		ibmvfc host struct
5484  * @events:		events to log
5485  *
5486  **/
ibmvfc_log_ae(struct ibmvfc_host * vhost,int events)5487 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
5488 {
5489 	if (events & IBMVFC_AE_RSCN)
5490 		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
5491 	if ((events & IBMVFC_AE_LINKDOWN) &&
5492 	    vhost->state >= IBMVFC_HALTED)
5493 		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
5494 	if ((events & IBMVFC_AE_LINKUP) &&
5495 	    vhost->state == IBMVFC_INITIALIZING)
5496 		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
5497 }
5498 
5499 /**
5500  * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
5501  * @tgt:		ibmvfc target struct
5502  *
5503  **/
ibmvfc_tgt_add_rport(struct ibmvfc_target * tgt)5504 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
5505 {
5506 	struct ibmvfc_host *vhost = tgt->vhost;
5507 	struct fc_rport *rport;
5508 	unsigned long flags;
5509 
5510 	tgt_dbg(tgt, "Adding rport\n");
5511 	rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
5512 	spin_lock_irqsave(vhost->host->host_lock, flags);
5513 
5514 	if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5515 		tgt_dbg(tgt, "Deleting rport\n");
5516 		list_del(&tgt->queue);
5517 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5518 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5519 		fc_remote_port_delete(rport);
5520 		del_timer_sync(&tgt->timer);
5521 		kref_put(&tgt->kref, ibmvfc_release_tgt);
5522 		return;
5523 	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5524 		tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
5525 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5526 		tgt->rport = NULL;
5527 		tgt->init_retries = 0;
5528 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5529 		fc_remote_port_delete(rport);
5530 		return;
5531 	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
5532 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5533 		return;
5534 	}
5535 
5536 	if (rport) {
5537 		tgt_dbg(tgt, "rport add succeeded\n");
5538 		tgt->rport = rport;
5539 		rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
5540 		rport->supported_classes = 0;
5541 		tgt->target_id = rport->scsi_target_id;
5542 		if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
5543 			rport->supported_classes |= FC_COS_CLASS1;
5544 		if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
5545 			rport->supported_classes |= FC_COS_CLASS2;
5546 		if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
5547 			rport->supported_classes |= FC_COS_CLASS3;
5548 	} else
5549 		tgt_dbg(tgt, "rport add failed\n");
5550 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5551 }
5552 
5553 /**
5554  * ibmvfc_do_work - Do task level work
5555  * @vhost:		ibmvfc host struct
5556  *
5557  **/
ibmvfc_do_work(struct ibmvfc_host * vhost)5558 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
5559 {
5560 	struct ibmvfc_target *tgt;
5561 	unsigned long flags;
5562 	struct fc_rport *rport;
5563 	LIST_HEAD(purge);
5564 	int rc;
5565 
5566 	ibmvfc_log_ae(vhost, vhost->events_to_log);
5567 	spin_lock_irqsave(vhost->host->host_lock, flags);
5568 	vhost->events_to_log = 0;
5569 	switch (vhost->action) {
5570 	case IBMVFC_HOST_ACTION_NONE:
5571 	case IBMVFC_HOST_ACTION_LOGO_WAIT:
5572 	case IBMVFC_HOST_ACTION_INIT_WAIT:
5573 		break;
5574 	case IBMVFC_HOST_ACTION_RESET:
5575 		list_splice_init(&vhost->purge, &purge);
5576 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5577 		ibmvfc_complete_purge(&purge);
5578 		rc = ibmvfc_reset_crq(vhost);
5579 
5580 		spin_lock_irqsave(vhost->host->host_lock, flags);
5581 		if (!rc || rc == H_CLOSED)
5582 			vio_enable_interrupts(to_vio_dev(vhost->dev));
5583 		if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
5584 			/*
5585 			 * The only action we could have changed to would have
5586 			 * been reenable, in which case, we skip the rest of
5587 			 * this path and wait until we've done the re-enable
5588 			 * before sending the crq init.
5589 			 */
5590 			vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5591 
5592 			if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
5593 			    (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
5594 				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5595 				dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
5596 			}
5597 		}
5598 		break;
5599 	case IBMVFC_HOST_ACTION_REENABLE:
5600 		list_splice_init(&vhost->purge, &purge);
5601 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5602 		ibmvfc_complete_purge(&purge);
5603 		rc = ibmvfc_reenable_crq_queue(vhost);
5604 
5605 		spin_lock_irqsave(vhost->host->host_lock, flags);
5606 		if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
5607 			/*
5608 			 * The only action we could have changed to would have
5609 			 * been reset, in which case, we skip the rest of this
5610 			 * path and wait until we've done the reset before
5611 			 * sending the crq init.
5612 			 */
5613 			vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5614 			if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
5615 				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5616 				dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
5617 			}
5618 		}
5619 		break;
5620 	case IBMVFC_HOST_ACTION_LOGO:
5621 		vhost->job_step(vhost);
5622 		break;
5623 	case IBMVFC_HOST_ACTION_INIT:
5624 		BUG_ON(vhost->state != IBMVFC_INITIALIZING);
5625 		if (vhost->delay_init) {
5626 			vhost->delay_init = 0;
5627 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
5628 			ssleep(15);
5629 			return;
5630 		} else
5631 			vhost->job_step(vhost);
5632 		break;
5633 	case IBMVFC_HOST_ACTION_QUERY:
5634 		list_for_each_entry(tgt, &vhost->targets, queue)
5635 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
5636 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
5637 		break;
5638 	case IBMVFC_HOST_ACTION_QUERY_TGTS:
5639 		list_for_each_entry(tgt, &vhost->targets, queue) {
5640 			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5641 				tgt->job_step(tgt);
5642 				break;
5643 			}
5644 		}
5645 
5646 		if (!ibmvfc_dev_init_to_do(vhost))
5647 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
5648 		break;
5649 	case IBMVFC_HOST_ACTION_TGT_DEL:
5650 	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5651 		list_for_each_entry(tgt, &vhost->targets, queue) {
5652 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
5653 				tgt->job_step(tgt);
5654 				break;
5655 			}
5656 		}
5657 
5658 		if (ibmvfc_dev_logo_to_do(vhost)) {
5659 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
5660 			return;
5661 		}
5662 
5663 		list_for_each_entry(tgt, &vhost->targets, queue) {
5664 			if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5665 				tgt_dbg(tgt, "Deleting rport\n");
5666 				rport = tgt->rport;
5667 				tgt->rport = NULL;
5668 				list_del(&tgt->queue);
5669 				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5670 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
5671 				if (rport)
5672 					fc_remote_port_delete(rport);
5673 				del_timer_sync(&tgt->timer);
5674 				kref_put(&tgt->kref, ibmvfc_release_tgt);
5675 				return;
5676 			} else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5677 				tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
5678 				rport = tgt->rport;
5679 				tgt->rport = NULL;
5680 				tgt->init_retries = 0;
5681 				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5682 
5683 				/*
5684 				 * If fast fail is enabled, we wait for it to fire and then clean up
5685 				 * the old port, since we expect the fast fail timer to clean up the
5686 				 * outstanding I/O faster than waiting for normal command timeouts.
5687 				 * However, if fast fail is disabled, any I/O outstanding to the
5688 				 * rport LUNs will stay outstanding indefinitely, since the EH handlers
5689 				 * won't get invoked for I/O's timing out. If this is a NPIV failover
5690 				 * scenario, the better alternative is to use the move login.
5691 				 */
5692 				if (rport && rport->fast_io_fail_tmo == -1)
5693 					tgt->move_login = 1;
5694 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
5695 				if (rport)
5696 					fc_remote_port_delete(rport);
5697 				return;
5698 			}
5699 		}
5700 
5701 		if (vhost->state == IBMVFC_INITIALIZING) {
5702 			if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
5703 				if (vhost->reinit) {
5704 					vhost->reinit = 0;
5705 					scsi_block_requests(vhost->host);
5706 					ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5707 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
5708 				} else {
5709 					ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
5710 					ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5711 					wake_up(&vhost->init_wait_q);
5712 					schedule_work(&vhost->rport_add_work_q);
5713 					vhost->init_retries = 0;
5714 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
5715 					scsi_unblock_requests(vhost->host);
5716 				}
5717 
5718 				return;
5719 			} else {
5720 				ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
5721 				vhost->job_step = ibmvfc_discover_targets;
5722 			}
5723 		} else {
5724 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5725 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
5726 			scsi_unblock_requests(vhost->host);
5727 			wake_up(&vhost->init_wait_q);
5728 			return;
5729 		}
5730 		break;
5731 	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5732 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
5733 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
5734 		ibmvfc_alloc_targets(vhost);
5735 		spin_lock_irqsave(vhost->host->host_lock, flags);
5736 		break;
5737 	case IBMVFC_HOST_ACTION_TGT_INIT:
5738 		list_for_each_entry(tgt, &vhost->targets, queue) {
5739 			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5740 				tgt->job_step(tgt);
5741 				break;
5742 			}
5743 		}
5744 
5745 		if (!ibmvfc_dev_init_to_do(vhost))
5746 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
5747 		break;
5748 	default:
5749 		break;
5750 	}
5751 
5752 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5753 }
5754 
5755 /**
5756  * ibmvfc_work - Do task level work
5757  * @data:		ibmvfc host struct
5758  *
5759  * Returns:
5760  *	zero
5761  **/
ibmvfc_work(void * data)5762 static int ibmvfc_work(void *data)
5763 {
5764 	struct ibmvfc_host *vhost = data;
5765 	int rc;
5766 
5767 	set_user_nice(current, MIN_NICE);
5768 
5769 	while (1) {
5770 		rc = wait_event_interruptible(vhost->work_wait_q,
5771 					      ibmvfc_work_to_do(vhost));
5772 
5773 		BUG_ON(rc);
5774 
5775 		if (kthread_should_stop())
5776 			break;
5777 
5778 		ibmvfc_do_work(vhost);
5779 	}
5780 
5781 	ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
5782 	return 0;
5783 }
5784 
5785 /**
5786  * ibmvfc_alloc_queue - Allocate queue
5787  * @vhost:	ibmvfc host struct
5788  * @queue:	ibmvfc queue to allocate
5789  * @fmt:	queue format to allocate
5790  *
5791  * Returns:
5792  *	0 on success / non-zero on failure
5793  **/
ibmvfc_alloc_queue(struct ibmvfc_host * vhost,struct ibmvfc_queue * queue,enum ibmvfc_msg_fmt fmt)5794 static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
5795 			      struct ibmvfc_queue *queue,
5796 			      enum ibmvfc_msg_fmt fmt)
5797 {
5798 	struct device *dev = vhost->dev;
5799 	size_t fmt_size;
5800 
5801 	ENTER;
5802 	spin_lock_init(&queue->_lock);
5803 	queue->q_lock = &queue->_lock;
5804 
5805 	switch (fmt) {
5806 	case IBMVFC_CRQ_FMT:
5807 		fmt_size = sizeof(*queue->msgs.crq);
5808 		queue->total_depth = scsi_qdepth + IBMVFC_NUM_INTERNAL_REQ;
5809 		queue->evt_depth = scsi_qdepth;
5810 		queue->reserved_depth = IBMVFC_NUM_INTERNAL_REQ;
5811 		break;
5812 	case IBMVFC_ASYNC_FMT:
5813 		fmt_size = sizeof(*queue->msgs.async);
5814 		break;
5815 	case IBMVFC_SUB_CRQ_FMT:
5816 		fmt_size = sizeof(*queue->msgs.scrq);
5817 		/* We need one extra event for Cancel Commands */
5818 		queue->total_depth = scsi_qdepth + IBMVFC_NUM_INTERNAL_SUBQ_REQ;
5819 		queue->evt_depth = scsi_qdepth;
5820 		queue->reserved_depth = IBMVFC_NUM_INTERNAL_SUBQ_REQ;
5821 		break;
5822 	default:
5823 		dev_warn(dev, "Unknown command/response queue message format: %d\n", fmt);
5824 		return -EINVAL;
5825 	}
5826 
5827 	queue->fmt = fmt;
5828 	if (ibmvfc_init_event_pool(vhost, queue)) {
5829 		dev_err(dev, "Couldn't initialize event pool.\n");
5830 		return -ENOMEM;
5831 	}
5832 
5833 	queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
5834 	if (!queue->msgs.handle)
5835 		return -ENOMEM;
5836 
5837 	queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
5838 					  DMA_BIDIRECTIONAL);
5839 
5840 	if (dma_mapping_error(dev, queue->msg_token)) {
5841 		free_page((unsigned long)queue->msgs.handle);
5842 		queue->msgs.handle = NULL;
5843 		return -ENOMEM;
5844 	}
5845 
5846 	queue->cur = 0;
5847 	queue->size = PAGE_SIZE / fmt_size;
5848 
5849 	queue->vhost = vhost;
5850 	return 0;
5851 }
5852 
5853 /**
5854  * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
5855  * @vhost:	ibmvfc host struct
5856  *
5857  * Allocates a page for messages, maps it for dma, and registers
5858  * the crq with the hypervisor.
5859  *
5860  * Return value:
5861  *	zero on success / other on failure
5862  **/
ibmvfc_init_crq(struct ibmvfc_host * vhost)5863 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
5864 {
5865 	int rc, retrc = -ENOMEM;
5866 	struct device *dev = vhost->dev;
5867 	struct vio_dev *vdev = to_vio_dev(dev);
5868 	struct ibmvfc_queue *crq = &vhost->crq;
5869 
5870 	ENTER;
5871 	if (ibmvfc_alloc_queue(vhost, crq, IBMVFC_CRQ_FMT))
5872 		return -ENOMEM;
5873 
5874 	retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5875 					crq->msg_token, PAGE_SIZE);
5876 
5877 	if (rc == H_RESOURCE)
5878 		/* maybe kexecing and resource is busy. try a reset */
5879 		retrc = rc = ibmvfc_reset_crq(vhost);
5880 
5881 	if (rc == H_CLOSED)
5882 		dev_warn(dev, "Partner adapter not ready\n");
5883 	else if (rc) {
5884 		dev_warn(dev, "Error %d opening adapter\n", rc);
5885 		goto reg_crq_failed;
5886 	}
5887 
5888 	retrc = 0;
5889 
5890 	tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
5891 
5892 	if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
5893 		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
5894 		goto req_irq_failed;
5895 	}
5896 
5897 	if ((rc = vio_enable_interrupts(vdev))) {
5898 		dev_err(dev, "Error %d enabling interrupts\n", rc);
5899 		goto req_irq_failed;
5900 	}
5901 
5902 	LEAVE;
5903 	return retrc;
5904 
5905 req_irq_failed:
5906 	tasklet_kill(&vhost->tasklet);
5907 	do {
5908 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5909 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5910 reg_crq_failed:
5911 	ibmvfc_free_queue(vhost, crq);
5912 	return retrc;
5913 }
5914 
ibmvfc_register_channel(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels,int index)5915 static int ibmvfc_register_channel(struct ibmvfc_host *vhost,
5916 				   struct ibmvfc_channels *channels,
5917 				   int index)
5918 {
5919 	struct device *dev = vhost->dev;
5920 	struct vio_dev *vdev = to_vio_dev(dev);
5921 	struct ibmvfc_queue *scrq = &channels->scrqs[index];
5922 	int rc = -ENOMEM;
5923 
5924 	ENTER;
5925 
5926 	rc = h_reg_sub_crq(vdev->unit_address, scrq->msg_token, PAGE_SIZE,
5927 			   &scrq->cookie, &scrq->hw_irq);
5928 
5929 	/* H_CLOSED indicates successful register, but no CRQ partner */
5930 	if (rc && rc != H_CLOSED) {
5931 		dev_warn(dev, "Error registering sub-crq: %d\n", rc);
5932 		if (rc == H_PARAMETER)
5933 			dev_warn_once(dev, "Firmware may not support MQ\n");
5934 		goto reg_failed;
5935 	}
5936 
5937 	scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
5938 
5939 	if (!scrq->irq) {
5940 		rc = -EINVAL;
5941 		dev_err(dev, "Error mapping sub-crq[%d] irq\n", index);
5942 		goto irq_failed;
5943 	}
5944 
5945 	switch (channels->protocol) {
5946 	case IBMVFC_PROTO_SCSI:
5947 		snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-scsi%d",
5948 			 vdev->unit_address, index);
5949 		scrq->handler = ibmvfc_interrupt_mq;
5950 		break;
5951 	case IBMVFC_PROTO_NVME:
5952 		snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-nvmf%d",
5953 			 vdev->unit_address, index);
5954 		scrq->handler = ibmvfc_interrupt_mq;
5955 		break;
5956 	default:
5957 		dev_err(dev, "Unknown channel protocol (%d)\n",
5958 			channels->protocol);
5959 		goto irq_failed;
5960 	}
5961 
5962 	rc = request_irq(scrq->irq, scrq->handler, 0, scrq->name, scrq);
5963 
5964 	if (rc) {
5965 		dev_err(dev, "Couldn't register sub-crq[%d] irq\n", index);
5966 		irq_dispose_mapping(scrq->irq);
5967 		goto irq_failed;
5968 	}
5969 
5970 	scrq->hwq_id = index;
5971 
5972 	LEAVE;
5973 	return 0;
5974 
5975 irq_failed:
5976 	do {
5977 		rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address, scrq->cookie);
5978 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5979 reg_failed:
5980 	LEAVE;
5981 	return rc;
5982 }
5983 
ibmvfc_deregister_channel(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels,int index)5984 static void ibmvfc_deregister_channel(struct ibmvfc_host *vhost,
5985 				      struct ibmvfc_channels *channels,
5986 				      int index)
5987 {
5988 	struct device *dev = vhost->dev;
5989 	struct vio_dev *vdev = to_vio_dev(dev);
5990 	struct ibmvfc_queue *scrq = &channels->scrqs[index];
5991 	long rc;
5992 
5993 	ENTER;
5994 
5995 	free_irq(scrq->irq, scrq);
5996 	irq_dispose_mapping(scrq->irq);
5997 	scrq->irq = 0;
5998 
5999 	do {
6000 		rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address,
6001 					scrq->cookie);
6002 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6003 
6004 	if (rc)
6005 		dev_err(dev, "Failed to free sub-crq[%d]: rc=%ld\n", index, rc);
6006 
6007 	/* Clean out the queue */
6008 	memset(scrq->msgs.crq, 0, PAGE_SIZE);
6009 	scrq->cur = 0;
6010 
6011 	LEAVE;
6012 }
6013 
ibmvfc_reg_sub_crqs(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels)6014 static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *vhost,
6015 				struct ibmvfc_channels *channels)
6016 {
6017 	int i, j;
6018 
6019 	ENTER;
6020 	if (!vhost->mq_enabled || !channels->scrqs)
6021 		return;
6022 
6023 	for (i = 0; i < channels->max_queues; i++) {
6024 		if (ibmvfc_register_channel(vhost, channels, i)) {
6025 			for (j = i; j > 0; j--)
6026 				ibmvfc_deregister_channel(vhost, channels, j - 1);
6027 			vhost->do_enquiry = 0;
6028 			return;
6029 		}
6030 	}
6031 
6032 	LEAVE;
6033 }
6034 
ibmvfc_dereg_sub_crqs(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels)6035 static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *vhost,
6036 				  struct ibmvfc_channels *channels)
6037 {
6038 	int i;
6039 
6040 	ENTER;
6041 	if (!vhost->mq_enabled || !channels->scrqs)
6042 		return;
6043 
6044 	for (i = 0; i < channels->max_queues; i++)
6045 		ibmvfc_deregister_channel(vhost, channels, i);
6046 
6047 	LEAVE;
6048 }
6049 
ibmvfc_alloc_channels(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels)6050 static int ibmvfc_alloc_channels(struct ibmvfc_host *vhost,
6051 				 struct ibmvfc_channels *channels)
6052 {
6053 	struct ibmvfc_queue *scrq;
6054 	int i, j;
6055 	int rc = 0;
6056 
6057 	channels->scrqs = kcalloc(channels->max_queues,
6058 				  sizeof(*channels->scrqs),
6059 				  GFP_KERNEL);
6060 	if (!channels->scrqs)
6061 		return -ENOMEM;
6062 
6063 	for (i = 0; i < channels->max_queues; i++) {
6064 		scrq = &channels->scrqs[i];
6065 		rc = ibmvfc_alloc_queue(vhost, scrq, IBMVFC_SUB_CRQ_FMT);
6066 		if (rc) {
6067 			for (j = i; j > 0; j--) {
6068 				scrq = &channels->scrqs[j - 1];
6069 				ibmvfc_free_queue(vhost, scrq);
6070 			}
6071 			kfree(channels->scrqs);
6072 			channels->scrqs = NULL;
6073 			channels->active_queues = 0;
6074 			return rc;
6075 		}
6076 	}
6077 
6078 	return rc;
6079 }
6080 
ibmvfc_init_sub_crqs(struct ibmvfc_host * vhost)6081 static void ibmvfc_init_sub_crqs(struct ibmvfc_host *vhost)
6082 {
6083 	ENTER;
6084 	if (!vhost->mq_enabled)
6085 		return;
6086 
6087 	if (ibmvfc_alloc_channels(vhost, &vhost->scsi_scrqs)) {
6088 		vhost->do_enquiry = 0;
6089 		vhost->mq_enabled = 0;
6090 		return;
6091 	}
6092 
6093 	ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
6094 
6095 	LEAVE;
6096 }
6097 
ibmvfc_release_channels(struct ibmvfc_host * vhost,struct ibmvfc_channels * channels)6098 static void ibmvfc_release_channels(struct ibmvfc_host *vhost,
6099 				    struct ibmvfc_channels *channels)
6100 {
6101 	struct ibmvfc_queue *scrq;
6102 	int i;
6103 
6104 	if (channels->scrqs) {
6105 		for (i = 0; i < channels->max_queues; i++) {
6106 			scrq = &channels->scrqs[i];
6107 			ibmvfc_free_queue(vhost, scrq);
6108 		}
6109 
6110 		kfree(channels->scrqs);
6111 		channels->scrqs = NULL;
6112 		channels->active_queues = 0;
6113 	}
6114 }
6115 
ibmvfc_release_sub_crqs(struct ibmvfc_host * vhost)6116 static void ibmvfc_release_sub_crqs(struct ibmvfc_host *vhost)
6117 {
6118 	ENTER;
6119 	if (!vhost->scsi_scrqs.scrqs)
6120 		return;
6121 
6122 	ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
6123 
6124 	ibmvfc_release_channels(vhost, &vhost->scsi_scrqs);
6125 	LEAVE;
6126 }
6127 
ibmvfc_free_disc_buf(struct device * dev,struct ibmvfc_channels * channels)6128 static void ibmvfc_free_disc_buf(struct device *dev, struct ibmvfc_channels *channels)
6129 {
6130 	dma_free_coherent(dev, channels->disc_buf_sz, channels->disc_buf,
6131 			  channels->disc_buf_dma);
6132 }
6133 
6134 /**
6135  * ibmvfc_free_mem - Free memory for vhost
6136  * @vhost:	ibmvfc host struct
6137  *
6138  * Return value:
6139  * 	none
6140  **/
ibmvfc_free_mem(struct ibmvfc_host * vhost)6141 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
6142 {
6143 	struct ibmvfc_queue *async_q = &vhost->async_crq;
6144 
6145 	ENTER;
6146 	mempool_destroy(vhost->tgt_pool);
6147 	kfree(vhost->trace);
6148 	ibmvfc_free_disc_buf(vhost->dev, &vhost->scsi_scrqs);
6149 	dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
6150 			  vhost->login_buf, vhost->login_buf_dma);
6151 	dma_free_coherent(vhost->dev, sizeof(*vhost->channel_setup_buf),
6152 			  vhost->channel_setup_buf, vhost->channel_setup_dma);
6153 	dma_pool_destroy(vhost->sg_pool);
6154 	ibmvfc_free_queue(vhost, async_q);
6155 	LEAVE;
6156 }
6157 
ibmvfc_alloc_disc_buf(struct device * dev,struct ibmvfc_channels * channels)6158 static int ibmvfc_alloc_disc_buf(struct device *dev, struct ibmvfc_channels *channels)
6159 {
6160 	channels->disc_buf_sz = sizeof(*channels->disc_buf) * max_targets;
6161 	channels->disc_buf = dma_alloc_coherent(dev, channels->disc_buf_sz,
6162 					     &channels->disc_buf_dma, GFP_KERNEL);
6163 
6164 	if (!channels->disc_buf) {
6165 		dev_err(dev, "Couldn't allocate %s Discover Targets buffer\n",
6166 			(channels->protocol == IBMVFC_PROTO_SCSI) ? "SCSI" : "NVMe");
6167 		return -ENOMEM;
6168 	}
6169 
6170 	return 0;
6171 }
6172 
6173 /**
6174  * ibmvfc_alloc_mem - Allocate memory for vhost
6175  * @vhost:	ibmvfc host struct
6176  *
6177  * Return value:
6178  * 	0 on success / non-zero on failure
6179  **/
ibmvfc_alloc_mem(struct ibmvfc_host * vhost)6180 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
6181 {
6182 	struct ibmvfc_queue *async_q = &vhost->async_crq;
6183 	struct device *dev = vhost->dev;
6184 
6185 	ENTER;
6186 	if (ibmvfc_alloc_queue(vhost, async_q, IBMVFC_ASYNC_FMT)) {
6187 		dev_err(dev, "Couldn't allocate/map async queue.\n");
6188 		goto nomem;
6189 	}
6190 
6191 	vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
6192 					 SG_ALL * sizeof(struct srp_direct_buf),
6193 					 sizeof(struct srp_direct_buf), 0);
6194 
6195 	if (!vhost->sg_pool) {
6196 		dev_err(dev, "Failed to allocate sg pool\n");
6197 		goto unmap_async_crq;
6198 	}
6199 
6200 	vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
6201 					      &vhost->login_buf_dma, GFP_KERNEL);
6202 
6203 	if (!vhost->login_buf) {
6204 		dev_err(dev, "Couldn't allocate NPIV login buffer\n");
6205 		goto free_sg_pool;
6206 	}
6207 
6208 	if (ibmvfc_alloc_disc_buf(dev, &vhost->scsi_scrqs))
6209 		goto free_login_buffer;
6210 
6211 	vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
6212 			       sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
6213 	atomic_set(&vhost->trace_index, -1);
6214 
6215 	if (!vhost->trace)
6216 		goto free_scsi_disc_buffer;
6217 
6218 	vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
6219 						      sizeof(struct ibmvfc_target));
6220 
6221 	if (!vhost->tgt_pool) {
6222 		dev_err(dev, "Couldn't allocate target memory pool\n");
6223 		goto free_trace;
6224 	}
6225 
6226 	vhost->channel_setup_buf = dma_alloc_coherent(dev, sizeof(*vhost->channel_setup_buf),
6227 						      &vhost->channel_setup_dma,
6228 						      GFP_KERNEL);
6229 
6230 	if (!vhost->channel_setup_buf) {
6231 		dev_err(dev, "Couldn't allocate Channel Setup buffer\n");
6232 		goto free_tgt_pool;
6233 	}
6234 
6235 	LEAVE;
6236 	return 0;
6237 
6238 free_tgt_pool:
6239 	mempool_destroy(vhost->tgt_pool);
6240 free_trace:
6241 	kfree(vhost->trace);
6242 free_scsi_disc_buffer:
6243 	ibmvfc_free_disc_buf(dev, &vhost->scsi_scrqs);
6244 free_login_buffer:
6245 	dma_free_coherent(dev, sizeof(*vhost->login_buf),
6246 			  vhost->login_buf, vhost->login_buf_dma);
6247 free_sg_pool:
6248 	dma_pool_destroy(vhost->sg_pool);
6249 unmap_async_crq:
6250 	ibmvfc_free_queue(vhost, async_q);
6251 nomem:
6252 	LEAVE;
6253 	return -ENOMEM;
6254 }
6255 
6256 /**
6257  * ibmvfc_rport_add_thread - Worker thread for rport adds
6258  * @work:	work struct
6259  *
6260  **/
ibmvfc_rport_add_thread(struct work_struct * work)6261 static void ibmvfc_rport_add_thread(struct work_struct *work)
6262 {
6263 	struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
6264 						 rport_add_work_q);
6265 	struct ibmvfc_target *tgt;
6266 	struct fc_rport *rport;
6267 	unsigned long flags;
6268 	int did_work;
6269 
6270 	ENTER;
6271 	spin_lock_irqsave(vhost->host->host_lock, flags);
6272 	do {
6273 		did_work = 0;
6274 		if (vhost->state != IBMVFC_ACTIVE)
6275 			break;
6276 
6277 		list_for_each_entry(tgt, &vhost->targets, queue) {
6278 			if (tgt->add_rport) {
6279 				did_work = 1;
6280 				tgt->add_rport = 0;
6281 				kref_get(&tgt->kref);
6282 				rport = tgt->rport;
6283 				if (!rport) {
6284 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6285 					ibmvfc_tgt_add_rport(tgt);
6286 				} else if (get_device(&rport->dev)) {
6287 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6288 					tgt_dbg(tgt, "Setting rport roles\n");
6289 					fc_remote_port_rolechg(rport, tgt->ids.roles);
6290 					put_device(&rport->dev);
6291 				} else {
6292 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
6293 				}
6294 
6295 				kref_put(&tgt->kref, ibmvfc_release_tgt);
6296 				spin_lock_irqsave(vhost->host->host_lock, flags);
6297 				break;
6298 			}
6299 		}
6300 	} while(did_work);
6301 
6302 	if (vhost->state == IBMVFC_ACTIVE)
6303 		vhost->scan_complete = 1;
6304 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
6305 	LEAVE;
6306 }
6307 
6308 /**
6309  * ibmvfc_probe - Adapter hot plug add entry point
6310  * @vdev:	vio device struct
6311  * @id:	vio device id struct
6312  *
6313  * Return value:
6314  * 	0 on success / non-zero on failure
6315  **/
ibmvfc_probe(struct vio_dev * vdev,const struct vio_device_id * id)6316 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
6317 {
6318 	struct ibmvfc_host *vhost;
6319 	struct Scsi_Host *shost;
6320 	struct device *dev = &vdev->dev;
6321 	int rc = -ENOMEM;
6322 	unsigned int online_cpus = num_online_cpus();
6323 	unsigned int max_scsi_queues = min((unsigned int)IBMVFC_MAX_SCSI_QUEUES, online_cpus);
6324 
6325 	ENTER;
6326 	shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
6327 	if (!shost) {
6328 		dev_err(dev, "Couldn't allocate host data\n");
6329 		goto out;
6330 	}
6331 
6332 	shost->transportt = ibmvfc_transport_template;
6333 	shost->can_queue = scsi_qdepth;
6334 	shost->max_lun = max_lun;
6335 	shost->max_id = max_targets;
6336 	shost->max_sectors = max_sectors;
6337 	shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
6338 	shost->unique_id = shost->host_no;
6339 	shost->nr_hw_queues = mq_enabled ? min(max_scsi_queues, nr_scsi_hw_queues) : 1;
6340 
6341 	vhost = shost_priv(shost);
6342 	INIT_LIST_HEAD(&vhost->targets);
6343 	INIT_LIST_HEAD(&vhost->purge);
6344 	sprintf(vhost->name, IBMVFC_NAME);
6345 	vhost->host = shost;
6346 	vhost->dev = dev;
6347 	vhost->partition_number = -1;
6348 	vhost->log_level = log_level;
6349 	vhost->task_set = 1;
6350 
6351 	vhost->mq_enabled = mq_enabled;
6352 	vhost->scsi_scrqs.desired_queues = min(shost->nr_hw_queues, nr_scsi_channels);
6353 	vhost->scsi_scrqs.max_queues = shost->nr_hw_queues;
6354 	vhost->scsi_scrqs.protocol = IBMVFC_PROTO_SCSI;
6355 	vhost->using_channels = 0;
6356 	vhost->do_enquiry = 1;
6357 	vhost->scan_timeout = 0;
6358 
6359 	strcpy(vhost->partition_name, "UNKNOWN");
6360 	init_waitqueue_head(&vhost->work_wait_q);
6361 	init_waitqueue_head(&vhost->init_wait_q);
6362 	INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
6363 	mutex_init(&vhost->passthru_mutex);
6364 
6365 	if ((rc = ibmvfc_alloc_mem(vhost)))
6366 		goto free_scsi_host;
6367 
6368 	vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
6369 					 shost->host_no);
6370 
6371 	if (IS_ERR(vhost->work_thread)) {
6372 		dev_err(dev, "Couldn't create kernel thread: %ld\n",
6373 			PTR_ERR(vhost->work_thread));
6374 		rc = PTR_ERR(vhost->work_thread);
6375 		goto free_host_mem;
6376 	}
6377 
6378 	if ((rc = ibmvfc_init_crq(vhost))) {
6379 		dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
6380 		goto kill_kthread;
6381 	}
6382 
6383 	if ((rc = scsi_add_host(shost, dev)))
6384 		goto release_crq;
6385 
6386 	fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
6387 
6388 	if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
6389 					   &ibmvfc_trace_attr))) {
6390 		dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
6391 		goto remove_shost;
6392 	}
6393 
6394 	ibmvfc_init_sub_crqs(vhost);
6395 
6396 	dev_set_drvdata(dev, vhost);
6397 	spin_lock(&ibmvfc_driver_lock);
6398 	list_add_tail(&vhost->queue, &ibmvfc_head);
6399 	spin_unlock(&ibmvfc_driver_lock);
6400 
6401 	ibmvfc_send_crq_init(vhost);
6402 	scsi_scan_host(shost);
6403 	return 0;
6404 
6405 remove_shost:
6406 	scsi_remove_host(shost);
6407 release_crq:
6408 	ibmvfc_release_crq_queue(vhost);
6409 kill_kthread:
6410 	kthread_stop(vhost->work_thread);
6411 free_host_mem:
6412 	ibmvfc_free_mem(vhost);
6413 free_scsi_host:
6414 	scsi_host_put(shost);
6415 out:
6416 	LEAVE;
6417 	return rc;
6418 }
6419 
6420 /**
6421  * ibmvfc_remove - Adapter hot plug remove entry point
6422  * @vdev:	vio device struct
6423  *
6424  * Return value:
6425  * 	0
6426  **/
ibmvfc_remove(struct vio_dev * vdev)6427 static void ibmvfc_remove(struct vio_dev *vdev)
6428 {
6429 	struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
6430 	LIST_HEAD(purge);
6431 	unsigned long flags;
6432 
6433 	ENTER;
6434 	ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
6435 
6436 	spin_lock_irqsave(vhost->host->host_lock, flags);
6437 	ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
6438 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
6439 
6440 	ibmvfc_wait_while_resetting(vhost);
6441 	kthread_stop(vhost->work_thread);
6442 	fc_remove_host(vhost->host);
6443 	scsi_remove_host(vhost->host);
6444 
6445 	spin_lock_irqsave(vhost->host->host_lock, flags);
6446 	ibmvfc_purge_requests(vhost, DID_ERROR);
6447 	list_splice_init(&vhost->purge, &purge);
6448 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
6449 	ibmvfc_complete_purge(&purge);
6450 	ibmvfc_release_sub_crqs(vhost);
6451 	ibmvfc_release_crq_queue(vhost);
6452 
6453 	ibmvfc_free_mem(vhost);
6454 	spin_lock(&ibmvfc_driver_lock);
6455 	list_del(&vhost->queue);
6456 	spin_unlock(&ibmvfc_driver_lock);
6457 	scsi_host_put(vhost->host);
6458 	LEAVE;
6459 }
6460 
6461 /**
6462  * ibmvfc_resume - Resume from suspend
6463  * @dev:	device struct
6464  *
6465  * We may have lost an interrupt across suspend/resume, so kick the
6466  * interrupt handler
6467  *
6468  */
ibmvfc_resume(struct device * dev)6469 static int ibmvfc_resume(struct device *dev)
6470 {
6471 	unsigned long flags;
6472 	struct ibmvfc_host *vhost = dev_get_drvdata(dev);
6473 	struct vio_dev *vdev = to_vio_dev(dev);
6474 
6475 	spin_lock_irqsave(vhost->host->host_lock, flags);
6476 	vio_disable_interrupts(vdev);
6477 	tasklet_schedule(&vhost->tasklet);
6478 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
6479 	return 0;
6480 }
6481 
6482 /**
6483  * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
6484  * @vdev:	vio device struct
6485  *
6486  * Return value:
6487  *	Number of bytes the driver will need to DMA map at the same time in
6488  *	order to perform well.
6489  */
ibmvfc_get_desired_dma(struct vio_dev * vdev)6490 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
6491 {
6492 	unsigned long pool_dma;
6493 
6494 	pool_dma = (IBMVFC_MAX_SCSI_QUEUES * scsi_qdepth) * sizeof(union ibmvfc_iu);
6495 	return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
6496 }
6497 
6498 static const struct vio_device_id ibmvfc_device_table[] = {
6499 	{"fcp", "IBM,vfc-client"},
6500 	{ "", "" }
6501 };
6502 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
6503 
6504 static const struct dev_pm_ops ibmvfc_pm_ops = {
6505 	.resume = ibmvfc_resume
6506 };
6507 
6508 static struct vio_driver ibmvfc_driver = {
6509 	.id_table = ibmvfc_device_table,
6510 	.probe = ibmvfc_probe,
6511 	.remove = ibmvfc_remove,
6512 	.get_desired_dma = ibmvfc_get_desired_dma,
6513 	.name = IBMVFC_NAME,
6514 	.pm = &ibmvfc_pm_ops,
6515 };
6516 
6517 static struct fc_function_template ibmvfc_transport_functions = {
6518 	.show_host_fabric_name = 1,
6519 	.show_host_node_name = 1,
6520 	.show_host_port_name = 1,
6521 	.show_host_supported_classes = 1,
6522 	.show_host_port_type = 1,
6523 	.show_host_port_id = 1,
6524 	.show_host_maxframe_size = 1,
6525 
6526 	.get_host_port_state = ibmvfc_get_host_port_state,
6527 	.show_host_port_state = 1,
6528 
6529 	.get_host_speed = ibmvfc_get_host_speed,
6530 	.show_host_speed = 1,
6531 
6532 	.issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
6533 	.terminate_rport_io = ibmvfc_terminate_rport_io,
6534 
6535 	.show_rport_maxframe_size = 1,
6536 	.show_rport_supported_classes = 1,
6537 
6538 	.set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
6539 	.show_rport_dev_loss_tmo = 1,
6540 
6541 	.get_starget_node_name = ibmvfc_get_starget_node_name,
6542 	.show_starget_node_name = 1,
6543 
6544 	.get_starget_port_name = ibmvfc_get_starget_port_name,
6545 	.show_starget_port_name = 1,
6546 
6547 	.get_starget_port_id = ibmvfc_get_starget_port_id,
6548 	.show_starget_port_id = 1,
6549 
6550 	.max_bsg_segments = 1,
6551 	.bsg_request = ibmvfc_bsg_request,
6552 	.bsg_timeout = ibmvfc_bsg_timeout,
6553 };
6554 
6555 /**
6556  * ibmvfc_module_init - Initialize the ibmvfc module
6557  *
6558  * Return value:
6559  * 	0 on success / other on failure
6560  **/
ibmvfc_module_init(void)6561 static int __init ibmvfc_module_init(void)
6562 {
6563 	int min_max_sectors = PAGE_SIZE >> 9;
6564 	int rc;
6565 
6566 	if (!firmware_has_feature(FW_FEATURE_VIO))
6567 		return -ENODEV;
6568 
6569 	printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
6570 	       IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
6571 
6572 	/*
6573 	 * Range check the max_sectors module parameter. The upper bounds is
6574 	 * implicity checked since the parameter is a ushort.
6575 	 */
6576 	if (max_sectors < min_max_sectors) {
6577 		printk(KERN_ERR IBMVFC_NAME ": max_sectors must be at least %d.\n",
6578 			min_max_sectors);
6579 		max_sectors = min_max_sectors;
6580 	}
6581 
6582 	ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
6583 	if (!ibmvfc_transport_template)
6584 		return -ENOMEM;
6585 
6586 	rc = vio_register_driver(&ibmvfc_driver);
6587 	if (rc)
6588 		fc_release_transport(ibmvfc_transport_template);
6589 	return rc;
6590 }
6591 
6592 /**
6593  * ibmvfc_module_exit - Teardown the ibmvfc module
6594  *
6595  * Return value:
6596  * 	nothing
6597  **/
ibmvfc_module_exit(void)6598 static void __exit ibmvfc_module_exit(void)
6599 {
6600 	vio_unregister_driver(&ibmvfc_driver);
6601 	fc_release_transport(ibmvfc_transport_template);
6602 }
6603 
6604 module_init(ibmvfc_module_init);
6605 module_exit(ibmvfc_module_exit);
6606