• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  **/
ibmvfc_trc_start(struct ibmvfc_event * evt)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  **/
ibmvfc_trc_end(struct ibmvfc_event * evt)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  **/
ibmvfc_get_err_index(u16 status,u16 error)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  **/
ibmvfc_get_cmd_error(u16 status,u16 error)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  **/
ibmvfc_get_err_result(struct ibmvfc_cmd * vfc_cmd)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  **/
ibmvfc_retry_cmd(u16 status,u16 error)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  **/
ibmvfc_get_ls_explain(u16 status)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  **/
ibmvfc_get_gs_explain(u16 status)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  **/
ibmvfc_get_fc_type(u16 status)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  **/
ibmvfc_set_tgt_action(struct ibmvfc_target * tgt,enum ibmvfc_target_action action)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  **/
ibmvfc_set_host_state(struct ibmvfc_host * vhost,enum ibmvfc_host_state state)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  **/
ibmvfc_set_host_action(struct ibmvfc_host * vhost,enum ibmvfc_host_action action)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_REENABLE:
536 	case IBMVFC_HOST_ACTION_RESET:
537 		vhost->action = action;
538 		break;
539 	case IBMVFC_HOST_ACTION_INIT:
540 	case IBMVFC_HOST_ACTION_TGT_DEL:
541 	case IBMVFC_HOST_ACTION_LOGO:
542 	case IBMVFC_HOST_ACTION_QUERY_TGTS:
543 	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
544 	case IBMVFC_HOST_ACTION_NONE:
545 	default:
546 		switch (vhost->action) {
547 		case IBMVFC_HOST_ACTION_RESET:
548 		case IBMVFC_HOST_ACTION_REENABLE:
549 			break;
550 		default:
551 			vhost->action = action;
552 			break;
553 		}
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  **/
ibmvfc_reinit_host(struct ibmvfc_host * vhost)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  **/
ibmvfc_del_tgt(struct ibmvfc_target * tgt)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  **/
ibmvfc_link_down(struct ibmvfc_host * vhost,enum ibmvfc_host_state state)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  **/
ibmvfc_init_host(struct ibmvfc_host * vhost)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  **/
ibmvfc_send_crq(struct ibmvfc_host * vhost,u64 word1,u64 word2)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  **/
ibmvfc_send_crq_init(struct ibmvfc_host * vhost)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  **/
ibmvfc_send_crq_init_complete(struct ibmvfc_host * vhost)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  **/
ibmvfc_release_crq_queue(struct ibmvfc_host * vhost)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  **/
ibmvfc_reenable_crq_queue(struct ibmvfc_host * vhost)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  **/
ibmvfc_reset_crq(struct ibmvfc_host * vhost)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  **/
ibmvfc_valid_event(struct ibmvfc_event_pool * pool,struct ibmvfc_event * evt)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  **/
ibmvfc_free_event(struct ibmvfc_event * evt)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  **/
ibmvfc_scsi_eh_done(struct ibmvfc_event * evt)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  **/
ibmvfc_fail_request(struct ibmvfc_event * evt,int error_code)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  **/
ibmvfc_purge_requests(struct ibmvfc_host * vhost,int error_code)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  **/
ibmvfc_hard_reset_host(struct ibmvfc_host * vhost)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  **/
__ibmvfc_reset_host(struct ibmvfc_host * vhost)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  **/
ibmvfc_reset_host(struct ibmvfc_host * vhost)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  **/
ibmvfc_retry_host_init(struct ibmvfc_host * vhost)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  **/
__ibmvfc_get_target(struct scsi_target * starget)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  **/
ibmvfc_get_target(struct scsi_target * starget)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  **/
ibmvfc_get_host_speed(struct Scsi_Host * shost)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  **/
ibmvfc_get_host_port_state(struct Scsi_Host * shost)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  **/
ibmvfc_set_rport_dev_loss_tmo(struct fc_rport * rport,u32 timeout)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  **/
ibmvfc_release_tgt(struct kref * kref)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  **/
ibmvfc_get_starget_node_name(struct scsi_target * starget)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  **/
ibmvfc_get_starget_port_name(struct scsi_target * starget)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  **/
ibmvfc_get_starget_port_id(struct scsi_target * starget)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  **/
ibmvfc_wait_while_resetting(struct ibmvfc_host * vhost)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  **/
ibmvfc_issue_fc_host_lip(struct Scsi_Host * shost)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  **/
ibmvfc_gather_partition_info(struct ibmvfc_host * vhost)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  **/
ibmvfc_set_login_info(struct ibmvfc_host * vhost)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  **/
ibmvfc_init_event_pool(struct ibmvfc_host * vhost)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  **/
ibmvfc_free_event_pool(struct ibmvfc_host * vhost)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  **/
ibmvfc_get_event(struct ibmvfc_host * vhost)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  **/
ibmvfc_init_event(struct ibmvfc_event * evt,void (* done)(struct ibmvfc_event *),u8 format)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  **/
ibmvfc_map_sg_list(struct scsi_cmnd * scmd,int nseg,struct srp_direct_buf * md)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  **/
ibmvfc_map_sg_data(struct scsi_cmnd * scmd,struct ibmvfc_event * evt,struct ibmvfc_cmd * vfc_cmd,struct device * dev)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  **/
ibmvfc_timeout(struct timer_list * t)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  **/
ibmvfc_send_event(struct ibmvfc_event * evt,struct ibmvfc_host * vhost,unsigned long timeout)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  **/
ibmvfc_log_error(struct ibmvfc_event * evt)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  **/
ibmvfc_relogin(struct scsi_device * sdev)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  **/
ibmvfc_scsi_done(struct ibmvfc_event * evt)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  **/
ibmvfc_host_chkready(struct ibmvfc_host * vhost)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  **/
ibmvfc_queuecommand_lck(struct scsi_cmnd * cmnd,void (* done)(struct scsi_cmnd *))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 
DEF_SCSI_QCMD(ibmvfc_queuecommand)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  **/
ibmvfc_bsg_timeout_done(struct ibmvfc_event * evt)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  **/
ibmvfc_bsg_timeout(struct bsg_job * job)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  **/
ibmvfc_bsg_plogi(struct ibmvfc_host * vhost,unsigned int port_id)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  **/
ibmvfc_bsg_request(struct bsg_job * job)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  **/
ibmvfc_reset_device(struct scsi_device * sdev,int type,char * desc)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  **/
ibmvfc_match_rport(struct ibmvfc_event * evt,void * rport)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  **/
ibmvfc_match_target(struct ibmvfc_event * evt,void * device)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  **/
ibmvfc_match_lun(struct ibmvfc_event * evt,void * device)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  **/
ibmvfc_wait_for_ops(struct ibmvfc_host * vhost,void * device,int (* match)(struct ibmvfc_event *,void *))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  **/
ibmvfc_cancel_all(struct scsi_device * sdev,int type)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  **/
ibmvfc_match_key(struct ibmvfc_event * evt,void * key)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  **/
ibmvfc_match_evt(struct ibmvfc_event * evt,void * match)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  **/
ibmvfc_abort_task_set(struct scsi_device * sdev)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  **/
ibmvfc_eh_abort_handler(struct scsi_cmnd * cmd)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  **/
ibmvfc_eh_device_reset_handler(struct scsi_cmnd * cmd)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  **/
ibmvfc_dev_cancel_all_noreset(struct scsi_device * sdev,void * data)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  **/
ibmvfc_dev_cancel_all_reset(struct scsi_device * sdev,void * data)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  **/
ibmvfc_eh_target_reset_handler(struct scsi_cmnd * cmd)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  **/
ibmvfc_eh_host_reset_handler(struct scsi_cmnd * cmd)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  **/
ibmvfc_terminate_rport_io(struct fc_rport * rport)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  **/
ibmvfc_get_ae_desc(u64 ae)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  **/
ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)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  **/
ibmvfc_handle_async(struct ibmvfc_async_crq * crq,struct ibmvfc_host * vhost)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  **/
ibmvfc_handle_crq(struct ibmvfc_crq * crq,struct ibmvfc_host * vhost)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  **/
ibmvfc_scan_finished(struct Scsi_Host * shost,unsigned long time)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  **/
ibmvfc_slave_alloc(struct scsi_device * sdev)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  **/
ibmvfc_target_alloc(struct scsi_target * starget)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  **/
ibmvfc_slave_configure(struct scsi_device * sdev)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 		blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
2963 	}
2964 	spin_unlock_irqrestore(shost->host_lock, flags);
2965 	return 0;
2966 }
2967 
2968 /**
2969  * ibmvfc_change_queue_depth - Change the device's queue depth
2970  * @sdev:	scsi device struct
2971  * @qdepth:	depth to set
2972  * @reason:	calling context
2973  *
2974  * Return value:
2975  * 	actual depth set
2976  **/
ibmvfc_change_queue_depth(struct scsi_device * sdev,int qdepth)2977 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2978 {
2979 	if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2980 		qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2981 
2982 	return scsi_change_queue_depth(sdev, qdepth);
2983 }
2984 
ibmvfc_show_host_partition_name(struct device * dev,struct device_attribute * attr,char * buf)2985 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2986 						 struct device_attribute *attr, char *buf)
2987 {
2988 	struct Scsi_Host *shost = class_to_shost(dev);
2989 	struct ibmvfc_host *vhost = shost_priv(shost);
2990 
2991 	return snprintf(buf, PAGE_SIZE, "%s\n",
2992 			vhost->login_buf->resp.partition_name);
2993 }
2994 
ibmvfc_show_host_device_name(struct device * dev,struct device_attribute * attr,char * buf)2995 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2996 					    struct device_attribute *attr, char *buf)
2997 {
2998 	struct Scsi_Host *shost = class_to_shost(dev);
2999 	struct ibmvfc_host *vhost = shost_priv(shost);
3000 
3001 	return snprintf(buf, PAGE_SIZE, "%s\n",
3002 			vhost->login_buf->resp.device_name);
3003 }
3004 
ibmvfc_show_host_loc_code(struct device * dev,struct device_attribute * attr,char * buf)3005 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3006 					 struct device_attribute *attr, char *buf)
3007 {
3008 	struct Scsi_Host *shost = class_to_shost(dev);
3009 	struct ibmvfc_host *vhost = shost_priv(shost);
3010 
3011 	return snprintf(buf, PAGE_SIZE, "%s\n",
3012 			vhost->login_buf->resp.port_loc_code);
3013 }
3014 
ibmvfc_show_host_drc_name(struct device * dev,struct device_attribute * attr,char * buf)3015 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3016 					 struct device_attribute *attr, char *buf)
3017 {
3018 	struct Scsi_Host *shost = class_to_shost(dev);
3019 	struct ibmvfc_host *vhost = shost_priv(shost);
3020 
3021 	return snprintf(buf, PAGE_SIZE, "%s\n",
3022 			vhost->login_buf->resp.drc_name);
3023 }
3024 
ibmvfc_show_host_npiv_version(struct device * dev,struct device_attribute * attr,char * buf)3025 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3026 					     struct device_attribute *attr, char *buf)
3027 {
3028 	struct Scsi_Host *shost = class_to_shost(dev);
3029 	struct ibmvfc_host *vhost = shost_priv(shost);
3030 	return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
3031 }
3032 
ibmvfc_show_host_capabilities(struct device * dev,struct device_attribute * attr,char * buf)3033 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3034 					     struct device_attribute *attr, char *buf)
3035 {
3036 	struct Scsi_Host *shost = class_to_shost(dev);
3037 	struct ibmvfc_host *vhost = shost_priv(shost);
3038 	return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
3039 }
3040 
3041 /**
3042  * ibmvfc_show_log_level - Show the adapter's error logging level
3043  * @dev:	class device struct
3044  * @buf:	buffer
3045  *
3046  * Return value:
3047  * 	number of bytes printed to buffer
3048  **/
ibmvfc_show_log_level(struct device * dev,struct device_attribute * attr,char * buf)3049 static ssize_t ibmvfc_show_log_level(struct device *dev,
3050 				     struct device_attribute *attr, char *buf)
3051 {
3052 	struct Scsi_Host *shost = class_to_shost(dev);
3053 	struct ibmvfc_host *vhost = shost_priv(shost);
3054 	unsigned long flags = 0;
3055 	int len;
3056 
3057 	spin_lock_irqsave(shost->host_lock, flags);
3058 	len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
3059 	spin_unlock_irqrestore(shost->host_lock, flags);
3060 	return len;
3061 }
3062 
3063 /**
3064  * ibmvfc_store_log_level - Change the adapter's error logging level
3065  * @dev:	class device struct
3066  * @buf:	buffer
3067  *
3068  * Return value:
3069  * 	number of bytes printed to buffer
3070  **/
ibmvfc_store_log_level(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3071 static ssize_t ibmvfc_store_log_level(struct device *dev,
3072 				      struct device_attribute *attr,
3073 				      const char *buf, size_t count)
3074 {
3075 	struct Scsi_Host *shost = class_to_shost(dev);
3076 	struct ibmvfc_host *vhost = shost_priv(shost);
3077 	unsigned long flags = 0;
3078 
3079 	spin_lock_irqsave(shost->host_lock, flags);
3080 	vhost->log_level = simple_strtoul(buf, NULL, 10);
3081 	spin_unlock_irqrestore(shost->host_lock, flags);
3082 	return strlen(buf);
3083 }
3084 
3085 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3086 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3087 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3088 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3089 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3090 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3091 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3092 		   ibmvfc_show_log_level, ibmvfc_store_log_level);
3093 
3094 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3095 /**
3096  * ibmvfc_read_trace - Dump the adapter trace
3097  * @filp:		open sysfs file
3098  * @kobj:		kobject struct
3099  * @bin_attr:	bin_attribute struct
3100  * @buf:		buffer
3101  * @off:		offset
3102  * @count:		buffer size
3103  *
3104  * Return value:
3105  *	number of bytes printed to buffer
3106  **/
ibmvfc_read_trace(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)3107 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3108 				 struct bin_attribute *bin_attr,
3109 				 char *buf, loff_t off, size_t count)
3110 {
3111 	struct device *dev = container_of(kobj, struct device, kobj);
3112 	struct Scsi_Host *shost = class_to_shost(dev);
3113 	struct ibmvfc_host *vhost = shost_priv(shost);
3114 	unsigned long flags = 0;
3115 	int size = IBMVFC_TRACE_SIZE;
3116 	char *src = (char *)vhost->trace;
3117 
3118 	if (off > size)
3119 		return 0;
3120 	if (off + count > size) {
3121 		size -= off;
3122 		count = size;
3123 	}
3124 
3125 	spin_lock_irqsave(shost->host_lock, flags);
3126 	memcpy(buf, &src[off], count);
3127 	spin_unlock_irqrestore(shost->host_lock, flags);
3128 	return count;
3129 }
3130 
3131 static struct bin_attribute ibmvfc_trace_attr = {
3132 	.attr =	{
3133 		.name = "trace",
3134 		.mode = S_IRUGO,
3135 	},
3136 	.size = 0,
3137 	.read = ibmvfc_read_trace,
3138 };
3139 #endif
3140 
3141 static struct device_attribute *ibmvfc_attrs[] = {
3142 	&dev_attr_partition_name,
3143 	&dev_attr_device_name,
3144 	&dev_attr_port_loc_code,
3145 	&dev_attr_drc_name,
3146 	&dev_attr_npiv_version,
3147 	&dev_attr_capabilities,
3148 	&dev_attr_log_level,
3149 	NULL
3150 };
3151 
3152 static struct scsi_host_template driver_template = {
3153 	.module = THIS_MODULE,
3154 	.name = "IBM POWER Virtual FC Adapter",
3155 	.proc_name = IBMVFC_NAME,
3156 	.queuecommand = ibmvfc_queuecommand,
3157 	.eh_timed_out = fc_eh_timed_out,
3158 	.eh_abort_handler = ibmvfc_eh_abort_handler,
3159 	.eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3160 	.eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3161 	.eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3162 	.slave_alloc = ibmvfc_slave_alloc,
3163 	.slave_configure = ibmvfc_slave_configure,
3164 	.target_alloc = ibmvfc_target_alloc,
3165 	.scan_finished = ibmvfc_scan_finished,
3166 	.change_queue_depth = ibmvfc_change_queue_depth,
3167 	.cmd_per_lun = 16,
3168 	.can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3169 	.this_id = -1,
3170 	.sg_tablesize = SG_ALL,
3171 	.max_sectors = IBMVFC_MAX_SECTORS,
3172 	.shost_attrs = ibmvfc_attrs,
3173 	.track_queue_depth = 1,
3174 };
3175 
3176 /**
3177  * ibmvfc_next_async_crq - Returns the next entry in async queue
3178  * @vhost:	ibmvfc host struct
3179  *
3180  * Returns:
3181  *	Pointer to next entry in queue / NULL if empty
3182  **/
ibmvfc_next_async_crq(struct ibmvfc_host * vhost)3183 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3184 {
3185 	struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3186 	struct ibmvfc_async_crq *crq;
3187 
3188 	crq = &async_crq->msgs[async_crq->cur];
3189 	if (crq->valid & 0x80) {
3190 		if (++async_crq->cur == async_crq->size)
3191 			async_crq->cur = 0;
3192 		rmb();
3193 	} else
3194 		crq = NULL;
3195 
3196 	return crq;
3197 }
3198 
3199 /**
3200  * ibmvfc_next_crq - Returns the next entry in message queue
3201  * @vhost:	ibmvfc host struct
3202  *
3203  * Returns:
3204  *	Pointer to next entry in queue / NULL if empty
3205  **/
ibmvfc_next_crq(struct ibmvfc_host * vhost)3206 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3207 {
3208 	struct ibmvfc_crq_queue *queue = &vhost->crq;
3209 	struct ibmvfc_crq *crq;
3210 
3211 	crq = &queue->msgs[queue->cur];
3212 	if (crq->valid & 0x80) {
3213 		if (++queue->cur == queue->size)
3214 			queue->cur = 0;
3215 		rmb();
3216 	} else
3217 		crq = NULL;
3218 
3219 	return crq;
3220 }
3221 
3222 /**
3223  * ibmvfc_interrupt - Interrupt handler
3224  * @irq:		number of irq to handle, not used
3225  * @dev_instance: ibmvfc_host that received interrupt
3226  *
3227  * Returns:
3228  *	IRQ_HANDLED
3229  **/
ibmvfc_interrupt(int irq,void * dev_instance)3230 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3231 {
3232 	struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3233 	unsigned long flags;
3234 
3235 	spin_lock_irqsave(vhost->host->host_lock, flags);
3236 	vio_disable_interrupts(to_vio_dev(vhost->dev));
3237 	tasklet_schedule(&vhost->tasklet);
3238 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3239 	return IRQ_HANDLED;
3240 }
3241 
3242 /**
3243  * ibmvfc_tasklet - Interrupt handler tasklet
3244  * @data:		ibmvfc host struct
3245  *
3246  * Returns:
3247  *	Nothing
3248  **/
ibmvfc_tasklet(void * data)3249 static void ibmvfc_tasklet(void *data)
3250 {
3251 	struct ibmvfc_host *vhost = data;
3252 	struct vio_dev *vdev = to_vio_dev(vhost->dev);
3253 	struct ibmvfc_crq *crq;
3254 	struct ibmvfc_async_crq *async;
3255 	unsigned long flags;
3256 	int done = 0;
3257 
3258 	spin_lock_irqsave(vhost->host->host_lock, flags);
3259 	while (!done) {
3260 		/* Pull all the valid messages off the async CRQ */
3261 		while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3262 			ibmvfc_handle_async(async, vhost);
3263 			async->valid = 0;
3264 			wmb();
3265 		}
3266 
3267 		/* Pull all the valid messages off the CRQ */
3268 		while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3269 			ibmvfc_handle_crq(crq, vhost);
3270 			crq->valid = 0;
3271 			wmb();
3272 		}
3273 
3274 		vio_enable_interrupts(vdev);
3275 		if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3276 			vio_disable_interrupts(vdev);
3277 			ibmvfc_handle_async(async, vhost);
3278 			async->valid = 0;
3279 			wmb();
3280 		} else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3281 			vio_disable_interrupts(vdev);
3282 			ibmvfc_handle_crq(crq, vhost);
3283 			crq->valid = 0;
3284 			wmb();
3285 		} else
3286 			done = 1;
3287 	}
3288 
3289 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3290 }
3291 
3292 /**
3293  * ibmvfc_init_tgt - Set the next init job step for the target
3294  * @tgt:		ibmvfc target struct
3295  * @job_step:	job step to perform
3296  *
3297  **/
ibmvfc_init_tgt(struct ibmvfc_target * tgt,void (* job_step)(struct ibmvfc_target *))3298 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3299 			    void (*job_step) (struct ibmvfc_target *))
3300 {
3301 	if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3302 		tgt->job_step = job_step;
3303 	wake_up(&tgt->vhost->work_wait_q);
3304 }
3305 
3306 /**
3307  * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3308  * @tgt:		ibmvfc target struct
3309  * @job_step:	initialization job step
3310  *
3311  * Returns: 1 if step will be retried / 0 if not
3312  *
3313  **/
ibmvfc_retry_tgt_init(struct ibmvfc_target * tgt,void (* job_step)(struct ibmvfc_target *))3314 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3315 				  void (*job_step) (struct ibmvfc_target *))
3316 {
3317 	if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3318 		ibmvfc_del_tgt(tgt);
3319 		wake_up(&tgt->vhost->work_wait_q);
3320 		return 0;
3321 	} else
3322 		ibmvfc_init_tgt(tgt, job_step);
3323 	return 1;
3324 }
3325 
3326 /* Defined in FC-LS */
3327 static const struct {
3328 	int code;
3329 	int retry;
3330 	int logged_in;
3331 } prli_rsp [] = {
3332 	{ 0, 1, 0 },
3333 	{ 1, 0, 1 },
3334 	{ 2, 1, 0 },
3335 	{ 3, 1, 0 },
3336 	{ 4, 0, 0 },
3337 	{ 5, 0, 0 },
3338 	{ 6, 0, 1 },
3339 	{ 7, 0, 0 },
3340 	{ 8, 1, 0 },
3341 };
3342 
3343 /**
3344  * ibmvfc_get_prli_rsp - Find PRLI response index
3345  * @flags:	PRLI response flags
3346  *
3347  **/
ibmvfc_get_prli_rsp(u16 flags)3348 static int ibmvfc_get_prli_rsp(u16 flags)
3349 {
3350 	int i;
3351 	int code = (flags & 0x0f00) >> 8;
3352 
3353 	for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3354 		if (prli_rsp[i].code == code)
3355 			return i;
3356 
3357 	return 0;
3358 }
3359 
3360 /**
3361  * ibmvfc_tgt_prli_done - Completion handler for Process Login
3362  * @evt:	ibmvfc event struct
3363  *
3364  **/
ibmvfc_tgt_prli_done(struct ibmvfc_event * evt)3365 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3366 {
3367 	struct ibmvfc_target *tgt = evt->tgt;
3368 	struct ibmvfc_host *vhost = evt->vhost;
3369 	struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3370 	struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3371 	u32 status = be16_to_cpu(rsp->common.status);
3372 	int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3373 
3374 	vhost->discovery_threads--;
3375 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3376 	switch (status) {
3377 	case IBMVFC_MAD_SUCCESS:
3378 		tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3379 			parms->type, parms->flags, parms->service_parms);
3380 
3381 		if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3382 			index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3383 			if (prli_rsp[index].logged_in) {
3384 				if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3385 					tgt->need_login = 0;
3386 					tgt->ids.roles = 0;
3387 					if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3388 						tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3389 					if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3390 						tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3391 					tgt->add_rport = 1;
3392 				} else
3393 					ibmvfc_del_tgt(tgt);
3394 			} else if (prli_rsp[index].retry)
3395 				ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3396 			else
3397 				ibmvfc_del_tgt(tgt);
3398 		} else
3399 			ibmvfc_del_tgt(tgt);
3400 		break;
3401 	case IBMVFC_MAD_DRIVER_FAILED:
3402 		break;
3403 	case IBMVFC_MAD_CRQ_ERROR:
3404 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3405 		break;
3406 	case IBMVFC_MAD_FAILED:
3407 	default:
3408 		if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3409 		     be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3410 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3411 		else if (tgt->logo_rcvd)
3412 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3413 		else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3414 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3415 		else
3416 			ibmvfc_del_tgt(tgt);
3417 
3418 		tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3419 			ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3420 			be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
3421 		break;
3422 	}
3423 
3424 	kref_put(&tgt->kref, ibmvfc_release_tgt);
3425 	ibmvfc_free_event(evt);
3426 	wake_up(&vhost->work_wait_q);
3427 }
3428 
3429 /**
3430  * ibmvfc_tgt_send_prli - Send a process login
3431  * @tgt:	ibmvfc target struct
3432  *
3433  **/
ibmvfc_tgt_send_prli(struct ibmvfc_target * tgt)3434 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3435 {
3436 	struct ibmvfc_process_login *prli;
3437 	struct ibmvfc_host *vhost = tgt->vhost;
3438 	struct ibmvfc_event *evt;
3439 
3440 	if (vhost->discovery_threads >= disc_threads)
3441 		return;
3442 
3443 	kref_get(&tgt->kref);
3444 	evt = ibmvfc_get_event(vhost);
3445 	vhost->discovery_threads++;
3446 	ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3447 	evt->tgt = tgt;
3448 	prli = &evt->iu.prli;
3449 	memset(prli, 0, sizeof(*prli));
3450 	prli->common.version = cpu_to_be32(1);
3451 	prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
3452 	prli->common.length = cpu_to_be16(sizeof(*prli));
3453 	prli->scsi_id = cpu_to_be64(tgt->scsi_id);
3454 
3455 	prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3456 	prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
3457 	prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
3458 	prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
3459 
3460 	if (cls3_error)
3461 		prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
3462 
3463 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3464 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3465 		vhost->discovery_threads--;
3466 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3467 		kref_put(&tgt->kref, ibmvfc_release_tgt);
3468 	} else
3469 		tgt_dbg(tgt, "Sent process login\n");
3470 }
3471 
3472 /**
3473  * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3474  * @evt:	ibmvfc event struct
3475  *
3476  **/
ibmvfc_tgt_plogi_done(struct ibmvfc_event * evt)3477 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3478 {
3479 	struct ibmvfc_target *tgt = evt->tgt;
3480 	struct ibmvfc_host *vhost = evt->vhost;
3481 	struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3482 	u32 status = be16_to_cpu(rsp->common.status);
3483 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
3484 
3485 	vhost->discovery_threads--;
3486 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3487 	switch (status) {
3488 	case IBMVFC_MAD_SUCCESS:
3489 		tgt_dbg(tgt, "Port Login succeeded\n");
3490 		if (tgt->ids.port_name &&
3491 		    tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3492 			vhost->reinit = 1;
3493 			tgt_dbg(tgt, "Port re-init required\n");
3494 			break;
3495 		}
3496 		tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3497 		tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3498 		tgt->ids.port_id = tgt->scsi_id;
3499 		memcpy(&tgt->service_parms, &rsp->service_parms,
3500 		       sizeof(tgt->service_parms));
3501 		memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3502 		       sizeof(tgt->service_parms_change));
3503 		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3504 		break;
3505 	case IBMVFC_MAD_DRIVER_FAILED:
3506 		break;
3507 	case IBMVFC_MAD_CRQ_ERROR:
3508 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3509 		break;
3510 	case IBMVFC_MAD_FAILED:
3511 	default:
3512 		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3513 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3514 		else
3515 			ibmvfc_del_tgt(tgt);
3516 
3517 		tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3518 			ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3519 					     be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
3520 			ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
3521 			ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
3522 		break;
3523 	}
3524 
3525 	kref_put(&tgt->kref, ibmvfc_release_tgt);
3526 	ibmvfc_free_event(evt);
3527 	wake_up(&vhost->work_wait_q);
3528 }
3529 
3530 /**
3531  * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3532  * @tgt:	ibmvfc target struct
3533  *
3534  **/
ibmvfc_tgt_send_plogi(struct ibmvfc_target * tgt)3535 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3536 {
3537 	struct ibmvfc_port_login *plogi;
3538 	struct ibmvfc_host *vhost = tgt->vhost;
3539 	struct ibmvfc_event *evt;
3540 
3541 	if (vhost->discovery_threads >= disc_threads)
3542 		return;
3543 
3544 	kref_get(&tgt->kref);
3545 	tgt->logo_rcvd = 0;
3546 	evt = ibmvfc_get_event(vhost);
3547 	vhost->discovery_threads++;
3548 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3549 	ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3550 	evt->tgt = tgt;
3551 	plogi = &evt->iu.plogi;
3552 	memset(plogi, 0, sizeof(*plogi));
3553 	plogi->common.version = cpu_to_be32(1);
3554 	plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
3555 	plogi->common.length = cpu_to_be16(sizeof(*plogi));
3556 	plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
3557 
3558 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3559 		vhost->discovery_threads--;
3560 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3561 		kref_put(&tgt->kref, ibmvfc_release_tgt);
3562 	} else
3563 		tgt_dbg(tgt, "Sent port login\n");
3564 }
3565 
3566 /**
3567  * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3568  * @evt:	ibmvfc event struct
3569  *
3570  **/
ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event * evt)3571 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3572 {
3573 	struct ibmvfc_target *tgt = evt->tgt;
3574 	struct ibmvfc_host *vhost = evt->vhost;
3575 	struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
3576 	u32 status = be16_to_cpu(rsp->common.status);
3577 
3578 	vhost->discovery_threads--;
3579 	ibmvfc_free_event(evt);
3580 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3581 
3582 	switch (status) {
3583 	case IBMVFC_MAD_SUCCESS:
3584 		tgt_dbg(tgt, "Implicit Logout succeeded\n");
3585 		break;
3586 	case IBMVFC_MAD_DRIVER_FAILED:
3587 		kref_put(&tgt->kref, ibmvfc_release_tgt);
3588 		wake_up(&vhost->work_wait_q);
3589 		return;
3590 	case IBMVFC_MAD_FAILED:
3591 	default:
3592 		tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3593 		break;
3594 	}
3595 
3596 	ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3597 	kref_put(&tgt->kref, ibmvfc_release_tgt);
3598 	wake_up(&vhost->work_wait_q);
3599 }
3600 
3601 /**
3602  * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
3603  * @tgt:		ibmvfc target struct
3604  *
3605  * Returns:
3606  *	Allocated and initialized ibmvfc_event struct
3607  **/
__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target * tgt,void (* done)(struct ibmvfc_event *))3608 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
3609 								 void (*done) (struct ibmvfc_event *))
3610 {
3611 	struct ibmvfc_implicit_logout *mad;
3612 	struct ibmvfc_host *vhost = tgt->vhost;
3613 	struct ibmvfc_event *evt;
3614 
3615 	kref_get(&tgt->kref);
3616 	evt = ibmvfc_get_event(vhost);
3617 	ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
3618 	evt->tgt = tgt;
3619 	mad = &evt->iu.implicit_logout;
3620 	memset(mad, 0, sizeof(*mad));
3621 	mad->common.version = cpu_to_be32(1);
3622 	mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
3623 	mad->common.length = cpu_to_be16(sizeof(*mad));
3624 	mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
3625 	return evt;
3626 }
3627 
3628 /**
3629  * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3630  * @tgt:		ibmvfc target struct
3631  *
3632  **/
ibmvfc_tgt_implicit_logout(struct ibmvfc_target * tgt)3633 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3634 {
3635 	struct ibmvfc_host *vhost = tgt->vhost;
3636 	struct ibmvfc_event *evt;
3637 
3638 	if (vhost->discovery_threads >= disc_threads)
3639 		return;
3640 
3641 	vhost->discovery_threads++;
3642 	evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
3643 						   ibmvfc_tgt_implicit_logout_done);
3644 
3645 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3646 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3647 		vhost->discovery_threads--;
3648 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3649 		kref_put(&tgt->kref, ibmvfc_release_tgt);
3650 	} else
3651 		tgt_dbg(tgt, "Sent Implicit Logout\n");
3652 }
3653 
3654 /**
3655  * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
3656  * @evt:	ibmvfc event struct
3657  *
3658  **/
ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event * evt)3659 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
3660 {
3661 	struct ibmvfc_target *tgt = evt->tgt;
3662 	struct ibmvfc_host *vhost = evt->vhost;
3663 	struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3664 	u32 status = be16_to_cpu(mad->common.status);
3665 
3666 	vhost->discovery_threads--;
3667 	ibmvfc_free_event(evt);
3668 
3669 	/*
3670 	 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
3671 	 * driver in which case we need to free up all the targets. If we are
3672 	 * not unloading, we will still go through a hard reset to get out of
3673 	 * offline state, so there is no need to track the old targets in that
3674 	 * case.
3675 	 */
3676 	if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
3677 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3678 	else
3679 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
3680 
3681 	tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
3682 	kref_put(&tgt->kref, ibmvfc_release_tgt);
3683 	wake_up(&vhost->work_wait_q);
3684 }
3685 
3686 /**
3687  * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
3688  * @tgt:		ibmvfc target struct
3689  *
3690  **/
ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target * tgt)3691 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
3692 {
3693 	struct ibmvfc_host *vhost = tgt->vhost;
3694 	struct ibmvfc_event *evt;
3695 
3696 	if (!vhost->logged_in) {
3697 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3698 		return;
3699 	}
3700 
3701 	if (vhost->discovery_threads >= disc_threads)
3702 		return;
3703 
3704 	vhost->discovery_threads++;
3705 	evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
3706 						   ibmvfc_tgt_implicit_logout_and_del_done);
3707 
3708 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
3709 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3710 		vhost->discovery_threads--;
3711 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3712 		kref_put(&tgt->kref, ibmvfc_release_tgt);
3713 	} else
3714 		tgt_dbg(tgt, "Sent Implicit Logout\n");
3715 }
3716 
3717 /**
3718  * ibmvfc_tgt_move_login_done - Completion handler for Move Login
3719  * @evt:	ibmvfc event struct
3720  *
3721  **/
ibmvfc_tgt_move_login_done(struct ibmvfc_event * evt)3722 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
3723 {
3724 	struct ibmvfc_target *tgt = evt->tgt;
3725 	struct ibmvfc_host *vhost = evt->vhost;
3726 	struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
3727 	u32 status = be16_to_cpu(rsp->common.status);
3728 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
3729 
3730 	vhost->discovery_threads--;
3731 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3732 	switch (status) {
3733 	case IBMVFC_MAD_SUCCESS:
3734 		tgt_dbg(tgt, "Move Login succeeded for old scsi_id: %llX\n", tgt->old_scsi_id);
3735 		tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3736 		tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3737 		tgt->ids.port_id = tgt->scsi_id;
3738 		memcpy(&tgt->service_parms, &rsp->service_parms,
3739 		       sizeof(tgt->service_parms));
3740 		memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3741 		       sizeof(tgt->service_parms_change));
3742 		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3743 		break;
3744 	case IBMVFC_MAD_DRIVER_FAILED:
3745 		break;
3746 	case IBMVFC_MAD_CRQ_ERROR:
3747 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
3748 		break;
3749 	case IBMVFC_MAD_FAILED:
3750 	default:
3751 		level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
3752 
3753 		tgt_log(tgt, level,
3754 			"Move Login failed: old scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
3755 			tgt->old_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
3756 			status);
3757 		break;
3758 	}
3759 
3760 	kref_put(&tgt->kref, ibmvfc_release_tgt);
3761 	ibmvfc_free_event(evt);
3762 	wake_up(&vhost->work_wait_q);
3763 }
3764 
3765 
3766 /**
3767  * ibmvfc_tgt_move_login - Initiate a move login for specified target
3768  * @tgt:		ibmvfc target struct
3769  *
3770  **/
ibmvfc_tgt_move_login(struct ibmvfc_target * tgt)3771 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
3772 {
3773 	struct ibmvfc_host *vhost = tgt->vhost;
3774 	struct ibmvfc_move_login *move;
3775 	struct ibmvfc_event *evt;
3776 
3777 	if (vhost->discovery_threads >= disc_threads)
3778 		return;
3779 
3780 	kref_get(&tgt->kref);
3781 	evt = ibmvfc_get_event(vhost);
3782 	vhost->discovery_threads++;
3783 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3784 	ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
3785 	evt->tgt = tgt;
3786 	move = &evt->iu.move_login;
3787 	memset(move, 0, sizeof(*move));
3788 	move->common.version = cpu_to_be32(1);
3789 	move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
3790 	move->common.length = cpu_to_be16(sizeof(*move));
3791 
3792 	move->old_scsi_id = cpu_to_be64(tgt->old_scsi_id);
3793 	move->new_scsi_id = cpu_to_be64(tgt->scsi_id);
3794 	move->wwpn = cpu_to_be64(tgt->wwpn);
3795 	move->node_name = cpu_to_be64(tgt->ids.node_name);
3796 
3797 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3798 		vhost->discovery_threads--;
3799 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3800 		kref_put(&tgt->kref, ibmvfc_release_tgt);
3801 	} else
3802 		tgt_dbg(tgt, "Sent Move Login for old scsi_id: %llX\n", tgt->old_scsi_id);
3803 }
3804 
3805 /**
3806  * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3807  * @mad:	ibmvfc passthru mad struct
3808  * @tgt:	ibmvfc target struct
3809  *
3810  * Returns:
3811  *	1 if PLOGI needed / 0 if PLOGI not needed
3812  **/
ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad * mad,struct ibmvfc_target * tgt)3813 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3814 				    struct ibmvfc_target *tgt)
3815 {
3816 	if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
3817 		return 1;
3818 	if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
3819 		return 1;
3820 	if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
3821 		return 1;
3822 	return 0;
3823 }
3824 
3825 /**
3826  * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3827  * @evt:	ibmvfc event struct
3828  *
3829  **/
ibmvfc_tgt_adisc_done(struct ibmvfc_event * evt)3830 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3831 {
3832 	struct ibmvfc_target *tgt = evt->tgt;
3833 	struct ibmvfc_host *vhost = evt->vhost;
3834 	struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3835 	u32 status = be16_to_cpu(mad->common.status);
3836 	u8 fc_reason, fc_explain;
3837 
3838 	vhost->discovery_threads--;
3839 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3840 	del_timer(&tgt->timer);
3841 
3842 	switch (status) {
3843 	case IBMVFC_MAD_SUCCESS:
3844 		tgt_dbg(tgt, "ADISC succeeded\n");
3845 		if (ibmvfc_adisc_needs_plogi(mad, tgt))
3846 			ibmvfc_del_tgt(tgt);
3847 		break;
3848 	case IBMVFC_MAD_DRIVER_FAILED:
3849 		break;
3850 	case IBMVFC_MAD_FAILED:
3851 	default:
3852 		ibmvfc_del_tgt(tgt);
3853 		fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
3854 		fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
3855 		tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3856 			 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
3857 			 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
3858 			 ibmvfc_get_fc_type(fc_reason), fc_reason,
3859 			 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3860 		break;
3861 	}
3862 
3863 	kref_put(&tgt->kref, ibmvfc_release_tgt);
3864 	ibmvfc_free_event(evt);
3865 	wake_up(&vhost->work_wait_q);
3866 }
3867 
3868 /**
3869  * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3870  * @evt:		ibmvfc event struct
3871  *
3872  **/
ibmvfc_init_passthru(struct ibmvfc_event * evt)3873 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3874 {
3875 	struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3876 
3877 	memset(mad, 0, sizeof(*mad));
3878 	mad->common.version = cpu_to_be32(1);
3879 	mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
3880 	mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
3881 	mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3882 		offsetof(struct ibmvfc_passthru_mad, iu));
3883 	mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
3884 	mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3885 	mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
3886 	mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3887 		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3888 		offsetof(struct ibmvfc_passthru_fc_iu, payload));
3889 	mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3890 	mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3891 		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3892 		offsetof(struct ibmvfc_passthru_fc_iu, response));
3893 	mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
3894 }
3895 
3896 /**
3897  * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3898  * @evt:		ibmvfc event struct
3899  *
3900  * Just cleanup this event struct. Everything else is handled by
3901  * the ADISC completion handler. If the ADISC never actually comes
3902  * back, we still have the timer running on the ADISC event struct
3903  * which will fire and cause the CRQ to get reset.
3904  *
3905  **/
ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event * evt)3906 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3907 {
3908 	struct ibmvfc_host *vhost = evt->vhost;
3909 	struct ibmvfc_target *tgt = evt->tgt;
3910 
3911 	tgt_dbg(tgt, "ADISC cancel complete\n");
3912 	vhost->abort_threads--;
3913 	ibmvfc_free_event(evt);
3914 	kref_put(&tgt->kref, ibmvfc_release_tgt);
3915 	wake_up(&vhost->work_wait_q);
3916 }
3917 
3918 /**
3919  * ibmvfc_adisc_timeout - Handle an ADISC timeout
3920  * @tgt:		ibmvfc target struct
3921  *
3922  * If an ADISC times out, send a cancel. If the cancel times
3923  * out, reset the CRQ. When the ADISC comes back as cancelled,
3924  * log back into the target.
3925  **/
ibmvfc_adisc_timeout(struct timer_list * t)3926 static void ibmvfc_adisc_timeout(struct timer_list *t)
3927 {
3928 	struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
3929 	struct ibmvfc_host *vhost = tgt->vhost;
3930 	struct ibmvfc_event *evt;
3931 	struct ibmvfc_tmf *tmf;
3932 	unsigned long flags;
3933 	int rc;
3934 
3935 	tgt_dbg(tgt, "ADISC timeout\n");
3936 	spin_lock_irqsave(vhost->host->host_lock, flags);
3937 	if (vhost->abort_threads >= disc_threads ||
3938 	    tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3939 	    vhost->state != IBMVFC_INITIALIZING ||
3940 	    vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3941 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
3942 		return;
3943 	}
3944 
3945 	vhost->abort_threads++;
3946 	kref_get(&tgt->kref);
3947 	evt = ibmvfc_get_event(vhost);
3948 	ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3949 
3950 	evt->tgt = tgt;
3951 	tmf = &evt->iu.tmf;
3952 	memset(tmf, 0, sizeof(*tmf));
3953 	tmf->common.version = cpu_to_be32(1);
3954 	tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
3955 	tmf->common.length = cpu_to_be16(sizeof(*tmf));
3956 	tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
3957 	tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
3958 
3959 	rc = ibmvfc_send_event(evt, vhost, default_timeout);
3960 
3961 	if (rc) {
3962 		tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3963 		vhost->abort_threads--;
3964 		kref_put(&tgt->kref, ibmvfc_release_tgt);
3965 		__ibmvfc_reset_host(vhost);
3966 	} else
3967 		tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3968 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3969 }
3970 
3971 /**
3972  * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3973  * @tgt:		ibmvfc target struct
3974  *
3975  * When sending an ADISC we end up with two timers running. The
3976  * first timer is the timer in the ibmvfc target struct. If this
3977  * fires, we send a cancel to the target. The second timer is the
3978  * timer on the ibmvfc event for the ADISC, which is longer. If that
3979  * fires, it means the ADISC timed out and our attempt to cancel it
3980  * also failed, so we need to reset the CRQ.
3981  **/
ibmvfc_tgt_adisc(struct ibmvfc_target * tgt)3982 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3983 {
3984 	struct ibmvfc_passthru_mad *mad;
3985 	struct ibmvfc_host *vhost = tgt->vhost;
3986 	struct ibmvfc_event *evt;
3987 
3988 	if (vhost->discovery_threads >= disc_threads)
3989 		return;
3990 
3991 	kref_get(&tgt->kref);
3992 	evt = ibmvfc_get_event(vhost);
3993 	vhost->discovery_threads++;
3994 	ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3995 	evt->tgt = tgt;
3996 
3997 	ibmvfc_init_passthru(evt);
3998 	mad = &evt->iu.passthru;
3999 	mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4000 	mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4001 	mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4002 
4003 	mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4004 	memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4005 	       sizeof(vhost->login_buf->resp.port_name));
4006 	memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4007 	       sizeof(vhost->login_buf->resp.node_name));
4008 	mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4009 
4010 	if (timer_pending(&tgt->timer))
4011 		mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4012 	else {
4013 		tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4014 		add_timer(&tgt->timer);
4015 	}
4016 
4017 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4018 	if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4019 		vhost->discovery_threads--;
4020 		del_timer(&tgt->timer);
4021 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4022 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4023 	} else
4024 		tgt_dbg(tgt, "Sent ADISC\n");
4025 }
4026 
4027 /**
4028  * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4029  * @evt:	ibmvfc event struct
4030  *
4031  **/
ibmvfc_tgt_query_target_done(struct ibmvfc_event * evt)4032 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4033 {
4034 	struct ibmvfc_target *tgt = evt->tgt;
4035 	struct ibmvfc_host *vhost = evt->vhost;
4036 	struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4037 	u32 status = be16_to_cpu(rsp->common.status);
4038 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4039 
4040 	vhost->discovery_threads--;
4041 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4042 	switch (status) {
4043 	case IBMVFC_MAD_SUCCESS:
4044 		tgt_dbg(tgt, "Query Target succeeded\n");
4045 		if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4046 			ibmvfc_del_tgt(tgt);
4047 		else
4048 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4049 		break;
4050 	case IBMVFC_MAD_DRIVER_FAILED:
4051 		break;
4052 	case IBMVFC_MAD_CRQ_ERROR:
4053 		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4054 		break;
4055 	case IBMVFC_MAD_FAILED:
4056 	default:
4057 		if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4058 		    be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4059 		    be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4060 			ibmvfc_del_tgt(tgt);
4061 		else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4062 			level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4063 		else
4064 			ibmvfc_del_tgt(tgt);
4065 
4066 		tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4067 			ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4068 			be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4069 			ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4070 			ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4071 			status);
4072 		break;
4073 	}
4074 
4075 	kref_put(&tgt->kref, ibmvfc_release_tgt);
4076 	ibmvfc_free_event(evt);
4077 	wake_up(&vhost->work_wait_q);
4078 }
4079 
4080 /**
4081  * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4082  * @tgt:	ibmvfc target struct
4083  *
4084  **/
ibmvfc_tgt_query_target(struct ibmvfc_target * tgt)4085 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4086 {
4087 	struct ibmvfc_query_tgt *query_tgt;
4088 	struct ibmvfc_host *vhost = tgt->vhost;
4089 	struct ibmvfc_event *evt;
4090 
4091 	if (vhost->discovery_threads >= disc_threads)
4092 		return;
4093 
4094 	kref_get(&tgt->kref);
4095 	evt = ibmvfc_get_event(vhost);
4096 	vhost->discovery_threads++;
4097 	evt->tgt = tgt;
4098 	ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4099 	query_tgt = &evt->iu.query_tgt;
4100 	memset(query_tgt, 0, sizeof(*query_tgt));
4101 	query_tgt->common.version = cpu_to_be32(1);
4102 	query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4103 	query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4104 	query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4105 
4106 	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4107 	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4108 		vhost->discovery_threads--;
4109 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4110 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4111 	} else
4112 		tgt_dbg(tgt, "Sent Query Target\n");
4113 }
4114 
4115 /**
4116  * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4117  * @vhost:		ibmvfc host struct
4118  * @scsi_id:	SCSI ID to allocate target for
4119  *
4120  * Returns:
4121  *	0 on success / other on failure
4122  **/
ibmvfc_alloc_target(struct ibmvfc_host * vhost,struct ibmvfc_discover_targets_entry * target)4123 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4124 			       struct ibmvfc_discover_targets_entry *target)
4125 {
4126 	struct ibmvfc_target *stgt = NULL;
4127 	struct ibmvfc_target *wtgt = NULL;
4128 	struct ibmvfc_target *tgt;
4129 	unsigned long flags;
4130 	u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4131 	u64 wwpn = be64_to_cpu(target->wwpn);
4132 
4133 	/* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4134 	spin_lock_irqsave(vhost->host->host_lock, flags);
4135 	list_for_each_entry(tgt, &vhost->targets, queue) {
4136 		if (tgt->wwpn == wwpn) {
4137 			wtgt = tgt;
4138 			break;
4139 		}
4140 	}
4141 
4142 	list_for_each_entry(tgt, &vhost->targets, queue) {
4143 		if (tgt->scsi_id == scsi_id) {
4144 			stgt = tgt;
4145 			break;
4146 		}
4147 	}
4148 
4149 	if (wtgt && !stgt) {
4150 		/*
4151 		 * A WWPN target has moved and we still are tracking the old
4152 		 * SCSI ID.  The only way we should be able to get here is if
4153 		 * we attempted to send an implicit logout for the old SCSI ID
4154 		 * and it failed for some reason, such as there being I/O
4155 		 * pending to the target. In this case, we will have already
4156 		 * deleted the rport from the FC transport so we do a move
4157 		 * login, which works even with I/O pending, as it will cancel
4158 		 * any active commands.
4159 		 */
4160 		if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4161 			/*
4162 			 * Do a move login here. The old target is no longer
4163 			 * known to the transport layer We don't use the
4164 			 * normal ibmvfc_set_tgt_action to set this, as we
4165 			 * don't normally want to allow this state change.
4166 			 */
4167 			wtgt->old_scsi_id = wtgt->scsi_id;
4168 			wtgt->scsi_id = scsi_id;
4169 			wtgt->action = IBMVFC_TGT_ACTION_INIT;
4170 			ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4171 			goto unlock_out;
4172 		} else {
4173 			tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4174 				wtgt->action, wtgt->rport);
4175 		}
4176 	} else if (stgt) {
4177 		if (tgt->need_login)
4178 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4179 		goto unlock_out;
4180 	}
4181 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4182 
4183 	tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4184 	memset(tgt, 0, sizeof(*tgt));
4185 	tgt->scsi_id = scsi_id;
4186 	tgt->wwpn = wwpn;
4187 	tgt->vhost = vhost;
4188 	tgt->need_login = 1;
4189 	timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4190 	kref_init(&tgt->kref);
4191 	ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4192 	spin_lock_irqsave(vhost->host->host_lock, flags);
4193 	tgt->cancel_key = vhost->task_set++;
4194 	list_add_tail(&tgt->queue, &vhost->targets);
4195 
4196 unlock_out:
4197 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4198 	return 0;
4199 }
4200 
4201 /**
4202  * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4203  * @vhost:		ibmvfc host struct
4204  *
4205  * Returns:
4206  *	0 on success / other on failure
4207  **/
ibmvfc_alloc_targets(struct ibmvfc_host * vhost)4208 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4209 {
4210 	int i, rc;
4211 
4212 	for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4213 		rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
4214 
4215 	return rc;
4216 }
4217 
4218 /**
4219  * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4220  * @evt:	ibmvfc event struct
4221  *
4222  **/
ibmvfc_discover_targets_done(struct ibmvfc_event * evt)4223 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4224 {
4225 	struct ibmvfc_host *vhost = evt->vhost;
4226 	struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4227 	u32 mad_status = be16_to_cpu(rsp->common.status);
4228 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4229 
4230 	switch (mad_status) {
4231 	case IBMVFC_MAD_SUCCESS:
4232 		ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4233 		vhost->num_targets = be32_to_cpu(rsp->num_written);
4234 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4235 		break;
4236 	case IBMVFC_MAD_FAILED:
4237 		level += ibmvfc_retry_host_init(vhost);
4238 		ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4239 			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4240 			   be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4241 		break;
4242 	case IBMVFC_MAD_DRIVER_FAILED:
4243 		break;
4244 	default:
4245 		dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4246 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4247 		break;
4248 	}
4249 
4250 	ibmvfc_free_event(evt);
4251 	wake_up(&vhost->work_wait_q);
4252 }
4253 
4254 /**
4255  * ibmvfc_discover_targets - Send Discover Targets MAD
4256  * @vhost:	ibmvfc host struct
4257  *
4258  **/
ibmvfc_discover_targets(struct ibmvfc_host * vhost)4259 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4260 {
4261 	struct ibmvfc_discover_targets *mad;
4262 	struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4263 
4264 	ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4265 	mad = &evt->iu.discover_targets;
4266 	memset(mad, 0, sizeof(*mad));
4267 	mad->common.version = cpu_to_be32(1);
4268 	mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4269 	mad->common.length = cpu_to_be16(sizeof(*mad));
4270 	mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4271 	mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4272 	mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4273 	mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
4274 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4275 
4276 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
4277 		ibmvfc_dbg(vhost, "Sent discover targets\n");
4278 	else
4279 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4280 }
4281 
4282 /**
4283  * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4284  * @evt:	ibmvfc event struct
4285  *
4286  **/
ibmvfc_npiv_login_done(struct ibmvfc_event * evt)4287 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4288 {
4289 	struct ibmvfc_host *vhost = evt->vhost;
4290 	u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
4291 	struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
4292 	unsigned int npiv_max_sectors;
4293 	int level = IBMVFC_DEFAULT_LOG_LEVEL;
4294 
4295 	switch (mad_status) {
4296 	case IBMVFC_MAD_SUCCESS:
4297 		ibmvfc_free_event(evt);
4298 		break;
4299 	case IBMVFC_MAD_FAILED:
4300 		if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4301 			level += ibmvfc_retry_host_init(vhost);
4302 		else
4303 			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4304 		ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
4305 			   ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4306 						be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4307 		ibmvfc_free_event(evt);
4308 		return;
4309 	case IBMVFC_MAD_CRQ_ERROR:
4310 		ibmvfc_retry_host_init(vhost);
4311 		fallthrough;
4312 	case IBMVFC_MAD_DRIVER_FAILED:
4313 		ibmvfc_free_event(evt);
4314 		return;
4315 	default:
4316 		dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4317 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4318 		ibmvfc_free_event(evt);
4319 		return;
4320 	}
4321 
4322 	vhost->client_migrated = 0;
4323 
4324 	if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
4325 		dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4326 			rsp->flags);
4327 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4328 		wake_up(&vhost->work_wait_q);
4329 		return;
4330 	}
4331 
4332 	if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
4333 		dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4334 			rsp->max_cmds);
4335 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4336 		wake_up(&vhost->work_wait_q);
4337 		return;
4338 	}
4339 
4340 	vhost->logged_in = 1;
4341 	npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
4342 	dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4343 		 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4344 		 rsp->drc_name, npiv_max_sectors);
4345 
4346 	fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
4347 	fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
4348 	fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
4349 	fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
4350 	fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4351 	fc_host_supported_classes(vhost->host) = 0;
4352 	if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
4353 		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
4354 	if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
4355 		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
4356 	if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
4357 		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4358 	fc_host_maxframe_size(vhost->host) =
4359 		be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
4360 
4361 	vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
4362 	vhost->host->max_sectors = npiv_max_sectors;
4363 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4364 	wake_up(&vhost->work_wait_q);
4365 }
4366 
4367 /**
4368  * ibmvfc_npiv_login - Sends NPIV login
4369  * @vhost:	ibmvfc host struct
4370  *
4371  **/
ibmvfc_npiv_login(struct ibmvfc_host * vhost)4372 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4373 {
4374 	struct ibmvfc_npiv_login_mad *mad;
4375 	struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4376 
4377 	ibmvfc_gather_partition_info(vhost);
4378 	ibmvfc_set_login_info(vhost);
4379 	ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4380 
4381 	memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4382 	mad = &evt->iu.npiv_login;
4383 	memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
4384 	mad->common.version = cpu_to_be32(1);
4385 	mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
4386 	mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
4387 	mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
4388 	mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
4389 
4390 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4391 
4392 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
4393 		ibmvfc_dbg(vhost, "Sent NPIV login\n");
4394 	else
4395 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4396 };
4397 
4398 /**
4399  * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4400  * @vhost:		ibmvfc host struct
4401  *
4402  **/
ibmvfc_npiv_logout_done(struct ibmvfc_event * evt)4403 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4404 {
4405 	struct ibmvfc_host *vhost = evt->vhost;
4406 	u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
4407 
4408 	ibmvfc_free_event(evt);
4409 
4410 	switch (mad_status) {
4411 	case IBMVFC_MAD_SUCCESS:
4412 		if (list_empty(&vhost->sent) &&
4413 		    vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
4414 			ibmvfc_init_host(vhost);
4415 			return;
4416 		}
4417 		break;
4418 	case IBMVFC_MAD_FAILED:
4419 	case IBMVFC_MAD_NOT_SUPPORTED:
4420 	case IBMVFC_MAD_CRQ_ERROR:
4421 	case IBMVFC_MAD_DRIVER_FAILED:
4422 	default:
4423 		ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4424 		break;
4425 	}
4426 
4427 	ibmvfc_hard_reset_host(vhost);
4428 }
4429 
4430 /**
4431  * ibmvfc_npiv_logout - Issue an NPIV Logout
4432  * @vhost:		ibmvfc host struct
4433  *
4434  **/
ibmvfc_npiv_logout(struct ibmvfc_host * vhost)4435 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4436 {
4437 	struct ibmvfc_npiv_logout_mad *mad;
4438 	struct ibmvfc_event *evt;
4439 
4440 	evt = ibmvfc_get_event(vhost);
4441 	ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4442 
4443 	mad = &evt->iu.npiv_logout;
4444 	memset(mad, 0, sizeof(*mad));
4445 	mad->common.version = cpu_to_be32(1);
4446 	mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
4447 	mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
4448 
4449 	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4450 
4451 	if (!ibmvfc_send_event(evt, vhost, default_timeout))
4452 		ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4453 	else
4454 		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4455 }
4456 
4457 /**
4458  * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4459  * @vhost:		ibmvfc host struct
4460  *
4461  * Returns:
4462  *	1 if work to do / 0 if not
4463  **/
ibmvfc_dev_init_to_do(struct ibmvfc_host * vhost)4464 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4465 {
4466 	struct ibmvfc_target *tgt;
4467 
4468 	list_for_each_entry(tgt, &vhost->targets, queue) {
4469 		if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4470 		    tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4471 			return 1;
4472 	}
4473 
4474 	return 0;
4475 }
4476 
4477 /**
4478  * ibmvfc_dev_logo_to_do - Is there target logout work to do?
4479  * @vhost:		ibmvfc host struct
4480  *
4481  * Returns:
4482  *	1 if work to do / 0 if not
4483  **/
ibmvfc_dev_logo_to_do(struct ibmvfc_host * vhost)4484 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
4485 {
4486 	struct ibmvfc_target *tgt;
4487 
4488 	list_for_each_entry(tgt, &vhost->targets, queue) {
4489 		if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
4490 		    tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
4491 			return 1;
4492 	}
4493 	return 0;
4494 }
4495 
4496 /**
4497  * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4498  * @vhost:		ibmvfc host struct
4499  *
4500  * Returns:
4501  *	1 if work to do / 0 if not
4502  **/
__ibmvfc_work_to_do(struct ibmvfc_host * vhost)4503 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4504 {
4505 	struct ibmvfc_target *tgt;
4506 
4507 	if (kthread_should_stop())
4508 		return 1;
4509 	switch (vhost->action) {
4510 	case IBMVFC_HOST_ACTION_NONE:
4511 	case IBMVFC_HOST_ACTION_INIT_WAIT:
4512 	case IBMVFC_HOST_ACTION_LOGO_WAIT:
4513 		return 0;
4514 	case IBMVFC_HOST_ACTION_TGT_INIT:
4515 	case IBMVFC_HOST_ACTION_QUERY_TGTS:
4516 		if (vhost->discovery_threads == disc_threads)
4517 			return 0;
4518 		list_for_each_entry(tgt, &vhost->targets, queue)
4519 			if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4520 				return 1;
4521 		list_for_each_entry(tgt, &vhost->targets, queue)
4522 			if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4523 				return 0;
4524 		return 1;
4525 	case IBMVFC_HOST_ACTION_TGT_DEL:
4526 	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4527 		if (vhost->discovery_threads == disc_threads)
4528 			return 0;
4529 		list_for_each_entry(tgt, &vhost->targets, queue)
4530 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
4531 				return 1;
4532 		list_for_each_entry(tgt, &vhost->targets, queue)
4533 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
4534 				return 0;
4535 		return 1;
4536 	case IBMVFC_HOST_ACTION_LOGO:
4537 	case IBMVFC_HOST_ACTION_INIT:
4538 	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4539 	case IBMVFC_HOST_ACTION_QUERY:
4540 	case IBMVFC_HOST_ACTION_RESET:
4541 	case IBMVFC_HOST_ACTION_REENABLE:
4542 	default:
4543 		break;
4544 	}
4545 
4546 	return 1;
4547 }
4548 
4549 /**
4550  * ibmvfc_work_to_do - Is there task level work to do?
4551  * @vhost:		ibmvfc host struct
4552  *
4553  * Returns:
4554  *	1 if work to do / 0 if not
4555  **/
ibmvfc_work_to_do(struct ibmvfc_host * vhost)4556 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4557 {
4558 	unsigned long flags;
4559 	int rc;
4560 
4561 	spin_lock_irqsave(vhost->host->host_lock, flags);
4562 	rc = __ibmvfc_work_to_do(vhost);
4563 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4564 	return rc;
4565 }
4566 
4567 /**
4568  * ibmvfc_log_ae - Log async events if necessary
4569  * @vhost:		ibmvfc host struct
4570  * @events:		events to log
4571  *
4572  **/
ibmvfc_log_ae(struct ibmvfc_host * vhost,int events)4573 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4574 {
4575 	if (events & IBMVFC_AE_RSCN)
4576 		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4577 	if ((events & IBMVFC_AE_LINKDOWN) &&
4578 	    vhost->state >= IBMVFC_HALTED)
4579 		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4580 	if ((events & IBMVFC_AE_LINKUP) &&
4581 	    vhost->state == IBMVFC_INITIALIZING)
4582 		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4583 }
4584 
4585 /**
4586  * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4587  * @tgt:		ibmvfc target struct
4588  *
4589  **/
ibmvfc_tgt_add_rport(struct ibmvfc_target * tgt)4590 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4591 {
4592 	struct ibmvfc_host *vhost = tgt->vhost;
4593 	struct fc_rport *rport;
4594 	unsigned long flags;
4595 
4596 	tgt_dbg(tgt, "Adding rport\n");
4597 	rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4598 	spin_lock_irqsave(vhost->host->host_lock, flags);
4599 
4600 	if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4601 		tgt_dbg(tgt, "Deleting rport\n");
4602 		list_del(&tgt->queue);
4603 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4604 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4605 		fc_remote_port_delete(rport);
4606 		del_timer_sync(&tgt->timer);
4607 		kref_put(&tgt->kref, ibmvfc_release_tgt);
4608 		return;
4609 	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
4610 		tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
4611 		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
4612 		tgt->rport = NULL;
4613 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4614 		fc_remote_port_delete(rport);
4615 		return;
4616 	} else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4617 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4618 		return;
4619 	}
4620 
4621 	if (rport) {
4622 		tgt_dbg(tgt, "rport add succeeded\n");
4623 		tgt->rport = rport;
4624 		rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
4625 		rport->supported_classes = 0;
4626 		tgt->target_id = rport->scsi_target_id;
4627 		if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
4628 			rport->supported_classes |= FC_COS_CLASS1;
4629 		if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
4630 			rport->supported_classes |= FC_COS_CLASS2;
4631 		if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
4632 			rport->supported_classes |= FC_COS_CLASS3;
4633 		if (rport->rqst_q)
4634 			blk_queue_max_segments(rport->rqst_q, 1);
4635 	} else
4636 		tgt_dbg(tgt, "rport add failed\n");
4637 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4638 }
4639 
4640 /**
4641  * ibmvfc_do_work - Do task level work
4642  * @vhost:		ibmvfc host struct
4643  *
4644  **/
ibmvfc_do_work(struct ibmvfc_host * vhost)4645 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4646 {
4647 	struct ibmvfc_target *tgt;
4648 	unsigned long flags;
4649 	struct fc_rport *rport;
4650 	int rc;
4651 
4652 	ibmvfc_log_ae(vhost, vhost->events_to_log);
4653 	spin_lock_irqsave(vhost->host->host_lock, flags);
4654 	vhost->events_to_log = 0;
4655 	switch (vhost->action) {
4656 	case IBMVFC_HOST_ACTION_NONE:
4657 	case IBMVFC_HOST_ACTION_LOGO_WAIT:
4658 	case IBMVFC_HOST_ACTION_INIT_WAIT:
4659 		break;
4660 	case IBMVFC_HOST_ACTION_RESET:
4661 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4662 		rc = ibmvfc_reset_crq(vhost);
4663 
4664 		spin_lock_irqsave(vhost->host->host_lock, flags);
4665 		if (!rc || rc == H_CLOSED)
4666 			vio_enable_interrupts(to_vio_dev(vhost->dev));
4667 		if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
4668 			/*
4669 			 * The only action we could have changed to would have
4670 			 * been reenable, in which case, we skip the rest of
4671 			 * this path and wait until we've done the re-enable
4672 			 * before sending the crq init.
4673 			 */
4674 			vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4675 
4676 			if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4677 			    (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
4678 				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4679 				dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4680 			}
4681 		}
4682 		break;
4683 	case IBMVFC_HOST_ACTION_REENABLE:
4684 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4685 		rc = ibmvfc_reenable_crq_queue(vhost);
4686 
4687 		spin_lock_irqsave(vhost->host->host_lock, flags);
4688 		if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
4689 			/*
4690 			 * The only action we could have changed to would have
4691 			 * been reset, in which case, we skip the rest of this
4692 			 * path and wait until we've done the reset before
4693 			 * sending the crq init.
4694 			 */
4695 			vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4696 			if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4697 				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4698 				dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4699 			}
4700 		}
4701 		break;
4702 	case IBMVFC_HOST_ACTION_LOGO:
4703 		vhost->job_step(vhost);
4704 		break;
4705 	case IBMVFC_HOST_ACTION_INIT:
4706 		BUG_ON(vhost->state != IBMVFC_INITIALIZING);
4707 		if (vhost->delay_init) {
4708 			vhost->delay_init = 0;
4709 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
4710 			ssleep(15);
4711 			return;
4712 		} else
4713 			vhost->job_step(vhost);
4714 		break;
4715 	case IBMVFC_HOST_ACTION_QUERY:
4716 		list_for_each_entry(tgt, &vhost->targets, queue)
4717 			ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4718 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4719 		break;
4720 	case IBMVFC_HOST_ACTION_QUERY_TGTS:
4721 		list_for_each_entry(tgt, &vhost->targets, queue) {
4722 			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4723 				tgt->job_step(tgt);
4724 				break;
4725 			}
4726 		}
4727 
4728 		if (!ibmvfc_dev_init_to_do(vhost))
4729 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4730 		break;
4731 	case IBMVFC_HOST_ACTION_TGT_DEL:
4732 	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4733 		list_for_each_entry(tgt, &vhost->targets, queue) {
4734 			if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
4735 				tgt->job_step(tgt);
4736 				break;
4737 			}
4738 		}
4739 
4740 		if (ibmvfc_dev_logo_to_do(vhost)) {
4741 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
4742 			return;
4743 		}
4744 
4745 		list_for_each_entry(tgt, &vhost->targets, queue) {
4746 			if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4747 				tgt_dbg(tgt, "Deleting rport\n");
4748 				rport = tgt->rport;
4749 				tgt->rport = NULL;
4750 				list_del(&tgt->queue);
4751 				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4752 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
4753 				if (rport)
4754 					fc_remote_port_delete(rport);
4755 				del_timer_sync(&tgt->timer);
4756 				kref_put(&tgt->kref, ibmvfc_release_tgt);
4757 				return;
4758 			} else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
4759 				tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
4760 				rport = tgt->rport;
4761 				tgt->rport = NULL;
4762 				ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
4763 				spin_unlock_irqrestore(vhost->host->host_lock, flags);
4764 				if (rport)
4765 					fc_remote_port_delete(rport);
4766 				return;
4767 			}
4768 		}
4769 
4770 		if (vhost->state == IBMVFC_INITIALIZING) {
4771 			if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
4772 				if (vhost->reinit) {
4773 					vhost->reinit = 0;
4774 					scsi_block_requests(vhost->host);
4775 					ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4776 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
4777 				} else {
4778 					ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4779 					ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4780 					wake_up(&vhost->init_wait_q);
4781 					schedule_work(&vhost->rport_add_work_q);
4782 					vhost->init_retries = 0;
4783 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
4784 					scsi_unblock_requests(vhost->host);
4785 				}
4786 
4787 				return;
4788 			} else {
4789 				ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4790 				vhost->job_step = ibmvfc_discover_targets;
4791 			}
4792 		} else {
4793 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4794 			spin_unlock_irqrestore(vhost->host->host_lock, flags);
4795 			scsi_unblock_requests(vhost->host);
4796 			wake_up(&vhost->init_wait_q);
4797 			return;
4798 		}
4799 		break;
4800 	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4801 		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4802 		spin_unlock_irqrestore(vhost->host->host_lock, flags);
4803 		ibmvfc_alloc_targets(vhost);
4804 		spin_lock_irqsave(vhost->host->host_lock, flags);
4805 		break;
4806 	case IBMVFC_HOST_ACTION_TGT_INIT:
4807 		list_for_each_entry(tgt, &vhost->targets, queue) {
4808 			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4809 				tgt->job_step(tgt);
4810 				break;
4811 			}
4812 		}
4813 
4814 		if (!ibmvfc_dev_init_to_do(vhost))
4815 			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
4816 		break;
4817 	default:
4818 		break;
4819 	}
4820 
4821 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4822 }
4823 
4824 /**
4825  * ibmvfc_work - Do task level work
4826  * @data:		ibmvfc host struct
4827  *
4828  * Returns:
4829  *	zero
4830  **/
ibmvfc_work(void * data)4831 static int ibmvfc_work(void *data)
4832 {
4833 	struct ibmvfc_host *vhost = data;
4834 	int rc;
4835 
4836 	set_user_nice(current, MIN_NICE);
4837 
4838 	while (1) {
4839 		rc = wait_event_interruptible(vhost->work_wait_q,
4840 					      ibmvfc_work_to_do(vhost));
4841 
4842 		BUG_ON(rc);
4843 
4844 		if (kthread_should_stop())
4845 			break;
4846 
4847 		ibmvfc_do_work(vhost);
4848 	}
4849 
4850 	ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4851 	return 0;
4852 }
4853 
4854 /**
4855  * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4856  * @vhost:	ibmvfc host struct
4857  *
4858  * Allocates a page for messages, maps it for dma, and registers
4859  * the crq with the hypervisor.
4860  *
4861  * Return value:
4862  *	zero on success / other on failure
4863  **/
ibmvfc_init_crq(struct ibmvfc_host * vhost)4864 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4865 {
4866 	int rc, retrc = -ENOMEM;
4867 	struct device *dev = vhost->dev;
4868 	struct vio_dev *vdev = to_vio_dev(dev);
4869 	struct ibmvfc_crq_queue *crq = &vhost->crq;
4870 
4871 	ENTER;
4872 	crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4873 
4874 	if (!crq->msgs)
4875 		return -ENOMEM;
4876 
4877 	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4878 	crq->msg_token = dma_map_single(dev, crq->msgs,
4879 					PAGE_SIZE, DMA_BIDIRECTIONAL);
4880 
4881 	if (dma_mapping_error(dev, crq->msg_token))
4882 		goto map_failed;
4883 
4884 	retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4885 					crq->msg_token, PAGE_SIZE);
4886 
4887 	if (rc == H_RESOURCE)
4888 		/* maybe kexecing and resource is busy. try a reset */
4889 		retrc = rc = ibmvfc_reset_crq(vhost);
4890 
4891 	if (rc == H_CLOSED)
4892 		dev_warn(dev, "Partner adapter not ready\n");
4893 	else if (rc) {
4894 		dev_warn(dev, "Error %d opening adapter\n", rc);
4895 		goto reg_crq_failed;
4896 	}
4897 
4898 	retrc = 0;
4899 
4900 	tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4901 
4902 	if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4903 		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4904 		goto req_irq_failed;
4905 	}
4906 
4907 	if ((rc = vio_enable_interrupts(vdev))) {
4908 		dev_err(dev, "Error %d enabling interrupts\n", rc);
4909 		goto req_irq_failed;
4910 	}
4911 
4912 	crq->cur = 0;
4913 	LEAVE;
4914 	return retrc;
4915 
4916 req_irq_failed:
4917 	tasklet_kill(&vhost->tasklet);
4918 	do {
4919 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4920 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4921 reg_crq_failed:
4922 	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4923 map_failed:
4924 	free_page((unsigned long)crq->msgs);
4925 	return retrc;
4926 }
4927 
4928 /**
4929  * ibmvfc_free_mem - Free memory for vhost
4930  * @vhost:	ibmvfc host struct
4931  *
4932  * Return value:
4933  * 	none
4934  **/
ibmvfc_free_mem(struct ibmvfc_host * vhost)4935 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4936 {
4937 	struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4938 
4939 	ENTER;
4940 	mempool_destroy(vhost->tgt_pool);
4941 	kfree(vhost->trace);
4942 	dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4943 			  vhost->disc_buf_dma);
4944 	dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4945 			  vhost->login_buf, vhost->login_buf_dma);
4946 	dma_pool_destroy(vhost->sg_pool);
4947 	dma_unmap_single(vhost->dev, async_q->msg_token,
4948 			 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4949 	free_page((unsigned long)async_q->msgs);
4950 	LEAVE;
4951 }
4952 
4953 /**
4954  * ibmvfc_alloc_mem - Allocate memory for vhost
4955  * @vhost:	ibmvfc host struct
4956  *
4957  * Return value:
4958  * 	0 on success / non-zero on failure
4959  **/
ibmvfc_alloc_mem(struct ibmvfc_host * vhost)4960 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4961 {
4962 	struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4963 	struct device *dev = vhost->dev;
4964 
4965 	ENTER;
4966 	async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4967 	if (!async_q->msgs) {
4968 		dev_err(dev, "Couldn't allocate async queue.\n");
4969 		goto nomem;
4970 	}
4971 
4972 	async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4973 	async_q->msg_token = dma_map_single(dev, async_q->msgs,
4974 					    async_q->size * sizeof(*async_q->msgs),
4975 					    DMA_BIDIRECTIONAL);
4976 
4977 	if (dma_mapping_error(dev, async_q->msg_token)) {
4978 		dev_err(dev, "Failed to map async queue\n");
4979 		goto free_async_crq;
4980 	}
4981 
4982 	vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4983 					 SG_ALL * sizeof(struct srp_direct_buf),
4984 					 sizeof(struct srp_direct_buf), 0);
4985 
4986 	if (!vhost->sg_pool) {
4987 		dev_err(dev, "Failed to allocate sg pool\n");
4988 		goto unmap_async_crq;
4989 	}
4990 
4991 	vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
4992 					      &vhost->login_buf_dma, GFP_KERNEL);
4993 
4994 	if (!vhost->login_buf) {
4995 		dev_err(dev, "Couldn't allocate NPIV login buffer\n");
4996 		goto free_sg_pool;
4997 	}
4998 
4999 	vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
5000 	vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
5001 					     &vhost->disc_buf_dma, GFP_KERNEL);
5002 
5003 	if (!vhost->disc_buf) {
5004 		dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
5005 		goto free_login_buffer;
5006 	}
5007 
5008 	vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
5009 			       sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
5010 
5011 	if (!vhost->trace)
5012 		goto free_disc_buffer;
5013 
5014 	vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
5015 						      sizeof(struct ibmvfc_target));
5016 
5017 	if (!vhost->tgt_pool) {
5018 		dev_err(dev, "Couldn't allocate target memory pool\n");
5019 		goto free_trace;
5020 	}
5021 
5022 	LEAVE;
5023 	return 0;
5024 
5025 free_trace:
5026 	kfree(vhost->trace);
5027 free_disc_buffer:
5028 	dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
5029 			  vhost->disc_buf_dma);
5030 free_login_buffer:
5031 	dma_free_coherent(dev, sizeof(*vhost->login_buf),
5032 			  vhost->login_buf, vhost->login_buf_dma);
5033 free_sg_pool:
5034 	dma_pool_destroy(vhost->sg_pool);
5035 unmap_async_crq:
5036 	dma_unmap_single(dev, async_q->msg_token,
5037 			 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
5038 free_async_crq:
5039 	free_page((unsigned long)async_q->msgs);
5040 nomem:
5041 	LEAVE;
5042 	return -ENOMEM;
5043 }
5044 
5045 /**
5046  * ibmvfc_rport_add_thread - Worker thread for rport adds
5047  * @work:	work struct
5048  *
5049  **/
ibmvfc_rport_add_thread(struct work_struct * work)5050 static void ibmvfc_rport_add_thread(struct work_struct *work)
5051 {
5052 	struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
5053 						 rport_add_work_q);
5054 	struct ibmvfc_target *tgt;
5055 	struct fc_rport *rport;
5056 	unsigned long flags;
5057 	int did_work;
5058 
5059 	ENTER;
5060 	spin_lock_irqsave(vhost->host->host_lock, flags);
5061 	do {
5062 		did_work = 0;
5063 		if (vhost->state != IBMVFC_ACTIVE)
5064 			break;
5065 
5066 		list_for_each_entry(tgt, &vhost->targets, queue) {
5067 			if (tgt->add_rport) {
5068 				did_work = 1;
5069 				tgt->add_rport = 0;
5070 				kref_get(&tgt->kref);
5071 				rport = tgt->rport;
5072 				if (!rport) {
5073 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
5074 					ibmvfc_tgt_add_rport(tgt);
5075 				} else if (get_device(&rport->dev)) {
5076 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
5077 					tgt_dbg(tgt, "Setting rport roles\n");
5078 					fc_remote_port_rolechg(rport, tgt->ids.roles);
5079 					put_device(&rport->dev);
5080 				} else {
5081 					spin_unlock_irqrestore(vhost->host->host_lock, flags);
5082 				}
5083 
5084 				kref_put(&tgt->kref, ibmvfc_release_tgt);
5085 				spin_lock_irqsave(vhost->host->host_lock, flags);
5086 				break;
5087 			}
5088 		}
5089 	} while(did_work);
5090 
5091 	if (vhost->state == IBMVFC_ACTIVE)
5092 		vhost->scan_complete = 1;
5093 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5094 	LEAVE;
5095 }
5096 
5097 /**
5098  * ibmvfc_probe - Adapter hot plug add entry point
5099  * @vdev:	vio device struct
5100  * @id:	vio device id struct
5101  *
5102  * Return value:
5103  * 	0 on success / non-zero on failure
5104  **/
ibmvfc_probe(struct vio_dev * vdev,const struct vio_device_id * id)5105 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
5106 {
5107 	struct ibmvfc_host *vhost;
5108 	struct Scsi_Host *shost;
5109 	struct device *dev = &vdev->dev;
5110 	int rc = -ENOMEM;
5111 
5112 	ENTER;
5113 	shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
5114 	if (!shost) {
5115 		dev_err(dev, "Couldn't allocate host data\n");
5116 		goto out;
5117 	}
5118 
5119 	shost->transportt = ibmvfc_transport_template;
5120 	shost->can_queue = max_requests;
5121 	shost->max_lun = max_lun;
5122 	shost->max_id = max_targets;
5123 	shost->max_sectors = IBMVFC_MAX_SECTORS;
5124 	shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
5125 	shost->unique_id = shost->host_no;
5126 
5127 	vhost = shost_priv(shost);
5128 	INIT_LIST_HEAD(&vhost->sent);
5129 	INIT_LIST_HEAD(&vhost->free);
5130 	INIT_LIST_HEAD(&vhost->targets);
5131 	sprintf(vhost->name, IBMVFC_NAME);
5132 	vhost->host = shost;
5133 	vhost->dev = dev;
5134 	vhost->partition_number = -1;
5135 	vhost->log_level = log_level;
5136 	vhost->task_set = 1;
5137 	strcpy(vhost->partition_name, "UNKNOWN");
5138 	init_waitqueue_head(&vhost->work_wait_q);
5139 	init_waitqueue_head(&vhost->init_wait_q);
5140 	INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
5141 	mutex_init(&vhost->passthru_mutex);
5142 
5143 	if ((rc = ibmvfc_alloc_mem(vhost)))
5144 		goto free_scsi_host;
5145 
5146 	vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
5147 					 shost->host_no);
5148 
5149 	if (IS_ERR(vhost->work_thread)) {
5150 		dev_err(dev, "Couldn't create kernel thread: %ld\n",
5151 			PTR_ERR(vhost->work_thread));
5152 		rc = PTR_ERR(vhost->work_thread);
5153 		goto free_host_mem;
5154 	}
5155 
5156 	if ((rc = ibmvfc_init_crq(vhost))) {
5157 		dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
5158 		goto kill_kthread;
5159 	}
5160 
5161 	if ((rc = ibmvfc_init_event_pool(vhost))) {
5162 		dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
5163 		goto release_crq;
5164 	}
5165 
5166 	if ((rc = scsi_add_host(shost, dev)))
5167 		goto release_event_pool;
5168 
5169 	fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
5170 
5171 	if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
5172 					   &ibmvfc_trace_attr))) {
5173 		dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
5174 		goto remove_shost;
5175 	}
5176 
5177 	if (shost_to_fc_host(shost)->rqst_q)
5178 		blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
5179 	dev_set_drvdata(dev, vhost);
5180 	spin_lock(&ibmvfc_driver_lock);
5181 	list_add_tail(&vhost->queue, &ibmvfc_head);
5182 	spin_unlock(&ibmvfc_driver_lock);
5183 
5184 	ibmvfc_send_crq_init(vhost);
5185 	scsi_scan_host(shost);
5186 	return 0;
5187 
5188 remove_shost:
5189 	scsi_remove_host(shost);
5190 release_event_pool:
5191 	ibmvfc_free_event_pool(vhost);
5192 release_crq:
5193 	ibmvfc_release_crq_queue(vhost);
5194 kill_kthread:
5195 	kthread_stop(vhost->work_thread);
5196 free_host_mem:
5197 	ibmvfc_free_mem(vhost);
5198 free_scsi_host:
5199 	scsi_host_put(shost);
5200 out:
5201 	LEAVE;
5202 	return rc;
5203 }
5204 
5205 /**
5206  * ibmvfc_remove - Adapter hot plug remove entry point
5207  * @vdev:	vio device struct
5208  *
5209  * Return value:
5210  * 	0
5211  **/
ibmvfc_remove(struct vio_dev * vdev)5212 static int ibmvfc_remove(struct vio_dev *vdev)
5213 {
5214 	struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
5215 	unsigned long flags;
5216 
5217 	ENTER;
5218 	ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
5219 
5220 	spin_lock_irqsave(vhost->host->host_lock, flags);
5221 	ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
5222 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5223 
5224 	ibmvfc_wait_while_resetting(vhost);
5225 	ibmvfc_release_crq_queue(vhost);
5226 	kthread_stop(vhost->work_thread);
5227 	fc_remove_host(vhost->host);
5228 	scsi_remove_host(vhost->host);
5229 
5230 	spin_lock_irqsave(vhost->host->host_lock, flags);
5231 	ibmvfc_purge_requests(vhost, DID_ERROR);
5232 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5233 	ibmvfc_free_event_pool(vhost);
5234 
5235 	ibmvfc_free_mem(vhost);
5236 	spin_lock(&ibmvfc_driver_lock);
5237 	list_del(&vhost->queue);
5238 	spin_unlock(&ibmvfc_driver_lock);
5239 	scsi_host_put(vhost->host);
5240 	LEAVE;
5241 	return 0;
5242 }
5243 
5244 /**
5245  * ibmvfc_resume - Resume from suspend
5246  * @dev:	device struct
5247  *
5248  * We may have lost an interrupt across suspend/resume, so kick the
5249  * interrupt handler
5250  *
5251  */
ibmvfc_resume(struct device * dev)5252 static int ibmvfc_resume(struct device *dev)
5253 {
5254 	unsigned long flags;
5255 	struct ibmvfc_host *vhost = dev_get_drvdata(dev);
5256 	struct vio_dev *vdev = to_vio_dev(dev);
5257 
5258 	spin_lock_irqsave(vhost->host->host_lock, flags);
5259 	vio_disable_interrupts(vdev);
5260 	tasklet_schedule(&vhost->tasklet);
5261 	spin_unlock_irqrestore(vhost->host->host_lock, flags);
5262 	return 0;
5263 }
5264 
5265 /**
5266  * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
5267  * @vdev:	vio device struct
5268  *
5269  * Return value:
5270  *	Number of bytes the driver will need to DMA map at the same time in
5271  *	order to perform well.
5272  */
ibmvfc_get_desired_dma(struct vio_dev * vdev)5273 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
5274 {
5275 	unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
5276 	return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
5277 }
5278 
5279 static const struct vio_device_id ibmvfc_device_table[] = {
5280 	{"fcp", "IBM,vfc-client"},
5281 	{ "", "" }
5282 };
5283 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
5284 
5285 static const struct dev_pm_ops ibmvfc_pm_ops = {
5286 	.resume = ibmvfc_resume
5287 };
5288 
5289 static struct vio_driver ibmvfc_driver = {
5290 	.id_table = ibmvfc_device_table,
5291 	.probe = ibmvfc_probe,
5292 	.remove = ibmvfc_remove,
5293 	.get_desired_dma = ibmvfc_get_desired_dma,
5294 	.name = IBMVFC_NAME,
5295 	.pm = &ibmvfc_pm_ops,
5296 };
5297 
5298 static struct fc_function_template ibmvfc_transport_functions = {
5299 	.show_host_fabric_name = 1,
5300 	.show_host_node_name = 1,
5301 	.show_host_port_name = 1,
5302 	.show_host_supported_classes = 1,
5303 	.show_host_port_type = 1,
5304 	.show_host_port_id = 1,
5305 	.show_host_maxframe_size = 1,
5306 
5307 	.get_host_port_state = ibmvfc_get_host_port_state,
5308 	.show_host_port_state = 1,
5309 
5310 	.get_host_speed = ibmvfc_get_host_speed,
5311 	.show_host_speed = 1,
5312 
5313 	.issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
5314 	.terminate_rport_io = ibmvfc_terminate_rport_io,
5315 
5316 	.show_rport_maxframe_size = 1,
5317 	.show_rport_supported_classes = 1,
5318 
5319 	.set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
5320 	.show_rport_dev_loss_tmo = 1,
5321 
5322 	.get_starget_node_name = ibmvfc_get_starget_node_name,
5323 	.show_starget_node_name = 1,
5324 
5325 	.get_starget_port_name = ibmvfc_get_starget_port_name,
5326 	.show_starget_port_name = 1,
5327 
5328 	.get_starget_port_id = ibmvfc_get_starget_port_id,
5329 	.show_starget_port_id = 1,
5330 
5331 	.bsg_request = ibmvfc_bsg_request,
5332 	.bsg_timeout = ibmvfc_bsg_timeout,
5333 };
5334 
5335 /**
5336  * ibmvfc_module_init - Initialize the ibmvfc module
5337  *
5338  * Return value:
5339  * 	0 on success / other on failure
5340  **/
ibmvfc_module_init(void)5341 static int __init ibmvfc_module_init(void)
5342 {
5343 	int rc;
5344 
5345 	if (!firmware_has_feature(FW_FEATURE_VIO))
5346 		return -ENODEV;
5347 
5348 	printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
5349 	       IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
5350 
5351 	ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
5352 	if (!ibmvfc_transport_template)
5353 		return -ENOMEM;
5354 
5355 	rc = vio_register_driver(&ibmvfc_driver);
5356 	if (rc)
5357 		fc_release_transport(ibmvfc_transport_template);
5358 	return rc;
5359 }
5360 
5361 /**
5362  * ibmvfc_module_exit - Teardown the ibmvfc module
5363  *
5364  * Return value:
5365  * 	nothing
5366  **/
ibmvfc_module_exit(void)5367 static void __exit ibmvfc_module_exit(void)
5368 {
5369 	vio_unregister_driver(&ibmvfc_driver);
5370 	fc_release_transport(ibmvfc_transport_template);
5371 }
5372 
5373 module_init(ibmvfc_module_init);
5374 module_exit(ibmvfc_module_exit);
5375