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