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