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