ibmvfc.c revision 10e794996ed00a0f09ff0745f87e3a3dd5de4606
1/*
2 * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
3 *
4 * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
5 *
6 * Copyright (C) IBM Corporation, 2008
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/dma-mapping.h>
27#include <linux/dmapool.h>
28#include <linux/delay.h>
29#include <linux/interrupt.h>
30#include <linux/kthread.h>
31#include <linux/of.h>
32#include <linux/stringify.h>
33#include <asm/firmware.h>
34#include <asm/irq.h>
35#include <asm/vio.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_cmnd.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_tcq.h>
41#include <scsi/scsi_transport_fc.h>
42#include "ibmvfc.h"
43
44static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
45static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
46static unsigned int max_lun = IBMVFC_MAX_LUN;
47static unsigned int max_targets = IBMVFC_MAX_TARGETS;
48static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
49static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
50static unsigned int dev_loss_tmo = IBMVFC_DEV_LOSS_TMO;
51static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
52static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
53static LIST_HEAD(ibmvfc_head);
54static DEFINE_SPINLOCK(ibmvfc_driver_lock);
55static struct scsi_transport_template *ibmvfc_transport_template;
56
57MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
58MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
59MODULE_LICENSE("GPL");
60MODULE_VERSION(IBMVFC_DRIVER_VERSION);
61
62module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
63MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
64		 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
65module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
66MODULE_PARM_DESC(default_timeout,
67		 "Default timeout in seconds for initialization and EH commands. "
68		 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
69module_param_named(max_requests, max_requests, uint, S_IRUGO);
70MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
71		 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
72module_param_named(max_lun, max_lun, uint, S_IRUGO);
73MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
74		 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
75module_param_named(max_targets, max_targets, uint, S_IRUGO);
76MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
77		 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
78module_param_named(disc_threads, disc_threads, uint, S_IRUGO | S_IWUSR);
79MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
80		 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
81module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
82MODULE_PARM_DESC(debug, "Enable driver debug information. "
83		 "[Default=" __stringify(IBMVFC_DEBUG) "]");
84module_param_named(dev_loss_tmo, dev_loss_tmo, uint, S_IRUGO | S_IWUSR);
85MODULE_PARM_DESC(dev_loss_tmo, "Maximum number of seconds that the FC "
86		 "transport should insulate the loss of a remote port. Once this "
87		 "value is exceeded, the scsi target is removed. "
88		 "[Default=" __stringify(IBMVFC_DEV_LOSS_TMO) "]");
89module_param_named(log_level, log_level, uint, 0);
90MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
91		 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
92
93static const struct {
94	u16 status;
95	u16 error;
96	u8 result;
97	u8 retry;
98	int log;
99	char *name;
100} cmd_status [] = {
101	{ IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
102	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
103	{ IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
104	{ IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_NO_CONNECT, 1, 1, "network down" },
105	{ IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
106	{ IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
107	{ IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
108	{ IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
109	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
110	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
111	{ IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
112	{ IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
113	{ IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 0, 0, "link halted" },
114	{ IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
115
116	{ IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
117	{ IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
118	{ IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ABORT, 0, 1, "invalid parameter" },
119	{ IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ABORT, 0, 1, "missing parameter" },
120	{ IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
121	{ IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ABORT, 0, 1, "transaction cancelled" },
122	{ IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ABORT, 0, 1, "transaction cancelled implicit" },
123	{ IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
124	{ IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
125
126	{ IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
127	{ IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
128	{ IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
129	{ IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
130	{ IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
131	{ IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
132	{ IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
133	{ IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
134	{ IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
135	{ IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
136	{ IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
137
138	{ IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
139};
140
141static void ibmvfc_npiv_login(struct ibmvfc_host *);
142static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
143static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
144static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
145
146static const char *unknown_error = "unknown error";
147
148#ifdef CONFIG_SCSI_IBMVFC_TRACE
149/**
150 * ibmvfc_trc_start - Log a start trace entry
151 * @evt:		ibmvfc event struct
152 *
153 **/
154static void ibmvfc_trc_start(struct ibmvfc_event *evt)
155{
156	struct ibmvfc_host *vhost = evt->vhost;
157	struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
158	struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
159	struct ibmvfc_trace_entry *entry;
160
161	entry = &vhost->trace[vhost->trace_index++];
162	entry->evt = evt;
163	entry->time = jiffies;
164	entry->fmt = evt->crq.format;
165	entry->type = IBMVFC_TRC_START;
166
167	switch (entry->fmt) {
168	case IBMVFC_CMD_FORMAT:
169		entry->op_code = vfc_cmd->iu.cdb[0];
170		entry->scsi_id = vfc_cmd->tgt_scsi_id;
171		entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
172		entry->tmf_flags = vfc_cmd->iu.tmf_flags;
173		entry->u.start.xfer_len = vfc_cmd->iu.xfer_len;
174		break;
175	case IBMVFC_MAD_FORMAT:
176		entry->op_code = mad->opcode;
177		break;
178	default:
179		break;
180	};
181}
182
183/**
184 * ibmvfc_trc_end - Log an end trace entry
185 * @evt:		ibmvfc event struct
186 *
187 **/
188static void ibmvfc_trc_end(struct ibmvfc_event *evt)
189{
190	struct ibmvfc_host *vhost = evt->vhost;
191	struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
192	struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
193	struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
194
195	entry->evt = evt;
196	entry->time = jiffies;
197	entry->fmt = evt->crq.format;
198	entry->type = IBMVFC_TRC_END;
199
200	switch (entry->fmt) {
201	case IBMVFC_CMD_FORMAT:
202		entry->op_code = vfc_cmd->iu.cdb[0];
203		entry->scsi_id = vfc_cmd->tgt_scsi_id;
204		entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
205		entry->tmf_flags = vfc_cmd->iu.tmf_flags;
206		entry->u.end.status = vfc_cmd->status;
207		entry->u.end.error = vfc_cmd->error;
208		entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
209		entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
210		entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
211		break;
212	case IBMVFC_MAD_FORMAT:
213		entry->op_code = mad->opcode;
214		entry->u.end.status = mad->status;
215		break;
216	default:
217		break;
218
219	};
220}
221
222#else
223#define ibmvfc_trc_start(evt) do { } while (0)
224#define ibmvfc_trc_end(evt) do { } while (0)
225#endif
226
227/**
228 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
229 * @status:		status / error class
230 * @error:		error
231 *
232 * Return value:
233 *	index into cmd_status / -EINVAL on failure
234 **/
235static int ibmvfc_get_err_index(u16 status, u16 error)
236{
237	int i;
238
239	for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
240		if ((cmd_status[i].status & status) == cmd_status[i].status &&
241		    cmd_status[i].error == error)
242			return i;
243
244	return -EINVAL;
245}
246
247/**
248 * ibmvfc_get_cmd_error - Find the error description for the fcp response
249 * @status:		status / error class
250 * @error:		error
251 *
252 * Return value:
253 *	error description string
254 **/
255static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
256{
257	int rc = ibmvfc_get_err_index(status, error);
258	if (rc >= 0)
259		return cmd_status[rc].name;
260	return unknown_error;
261}
262
263/**
264 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
265 * @vfc_cmd:	ibmvfc command struct
266 *
267 * Return value:
268 *	SCSI result value to return for completed command
269 **/
270static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
271{
272	int err;
273	struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
274	int fc_rsp_len = rsp->fcp_rsp_len;
275
276	if ((rsp->flags & FCP_RSP_LEN_VALID) &&
277	    ((!fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
278	     rsp->data.info.rsp_code))
279		return DID_ERROR << 16;
280
281	err = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
282	if (err >= 0)
283		return rsp->scsi_status | (cmd_status[err].result << 16);
284	return rsp->scsi_status | (DID_ERROR << 16);
285}
286
287/**
288 * ibmvfc_retry_cmd - Determine if error status is retryable
289 * @status:		status / error class
290 * @error:		error
291 *
292 * Return value:
293 *	1 if error should be retried / 0 if it should not
294 **/
295static int ibmvfc_retry_cmd(u16 status, u16 error)
296{
297	int rc = ibmvfc_get_err_index(status, error);
298
299	if (rc >= 0)
300		return cmd_status[rc].retry;
301	return 1;
302}
303
304static const char *unknown_fc_explain = "unknown fc explain";
305
306static const struct {
307	u16 fc_explain;
308	char *name;
309} ls_explain [] = {
310	{ 0x00, "no additional explanation" },
311	{ 0x01, "service parameter error - options" },
312	{ 0x03, "service parameter error - initiator control" },
313	{ 0x05, "service parameter error - recipient control" },
314	{ 0x07, "service parameter error - received data field size" },
315	{ 0x09, "service parameter error - concurrent seq" },
316	{ 0x0B, "service parameter error - credit" },
317	{ 0x0D, "invalid N_Port/F_Port_Name" },
318	{ 0x0E, "invalid node/Fabric Name" },
319	{ 0x0F, "invalid common service parameters" },
320	{ 0x11, "invalid association header" },
321	{ 0x13, "association header required" },
322	{ 0x15, "invalid originator S_ID" },
323	{ 0x17, "invalid OX_ID-RX-ID combination" },
324	{ 0x19, "command (request) already in progress" },
325	{ 0x1E, "N_Port Login requested" },
326	{ 0x1F, "Invalid N_Port_ID" },
327};
328
329static const struct {
330	u16 fc_explain;
331	char *name;
332} gs_explain [] = {
333	{ 0x00, "no additional explanation" },
334	{ 0x01, "port identifier not registered" },
335	{ 0x02, "port name not registered" },
336	{ 0x03, "node name not registered" },
337	{ 0x04, "class of service not registered" },
338	{ 0x06, "initial process associator not registered" },
339	{ 0x07, "FC-4 TYPEs not registered" },
340	{ 0x08, "symbolic port name not registered" },
341	{ 0x09, "symbolic node name not registered" },
342	{ 0x0A, "port type not registered" },
343	{ 0xF0, "authorization exception" },
344	{ 0xF1, "authentication exception" },
345	{ 0xF2, "data base full" },
346	{ 0xF3, "data base empty" },
347	{ 0xF4, "processing request" },
348	{ 0xF5, "unable to verify connection" },
349	{ 0xF6, "devices not in a common zone" },
350};
351
352/**
353 * ibmvfc_get_ls_explain - Return the FC Explain description text
354 * @status:	FC Explain status
355 *
356 * Returns:
357 *	error string
358 **/
359static const char *ibmvfc_get_ls_explain(u16 status)
360{
361	int i;
362
363	for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
364		if (ls_explain[i].fc_explain == status)
365			return ls_explain[i].name;
366
367	return unknown_fc_explain;
368}
369
370/**
371 * ibmvfc_get_gs_explain - Return the FC Explain description text
372 * @status:	FC Explain status
373 *
374 * Returns:
375 *	error string
376 **/
377static const char *ibmvfc_get_gs_explain(u16 status)
378{
379	int i;
380
381	for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
382		if (gs_explain[i].fc_explain == status)
383			return gs_explain[i].name;
384
385	return unknown_fc_explain;
386}
387
388static const struct {
389	enum ibmvfc_fc_type fc_type;
390	char *name;
391} fc_type [] = {
392	{ IBMVFC_FABRIC_REJECT, "fabric reject" },
393	{ IBMVFC_PORT_REJECT, "port reject" },
394	{ IBMVFC_LS_REJECT, "ELS reject" },
395	{ IBMVFC_FABRIC_BUSY, "fabric busy" },
396	{ IBMVFC_PORT_BUSY, "port busy" },
397	{ IBMVFC_BASIC_REJECT, "basic reject" },
398};
399
400static const char *unknown_fc_type = "unknown fc type";
401
402/**
403 * ibmvfc_get_fc_type - Return the FC Type description text
404 * @status:	FC Type error status
405 *
406 * Returns:
407 *	error string
408 **/
409static const char *ibmvfc_get_fc_type(u16 status)
410{
411	int i;
412
413	for (i = 0; i < ARRAY_SIZE(fc_type); i++)
414		if (fc_type[i].fc_type == status)
415			return fc_type[i].name;
416
417	return unknown_fc_type;
418}
419
420/**
421 * ibmvfc_set_tgt_action - Set the next init action for the target
422 * @tgt:		ibmvfc target struct
423 * @action:		action to perform
424 *
425 **/
426static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
427				  enum ibmvfc_target_action action)
428{
429	switch (tgt->action) {
430	case IBMVFC_TGT_ACTION_DEL_RPORT:
431		break;
432	default:
433		tgt->action = action;
434		break;
435	}
436}
437
438/**
439 * ibmvfc_set_host_state - Set the state for the host
440 * @vhost:		ibmvfc host struct
441 * @state:		state to set host to
442 *
443 * Returns:
444 *	0 if state changed / non-zero if not changed
445 **/
446static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
447				  enum ibmvfc_host_state state)
448{
449	int rc = 0;
450
451	switch (vhost->state) {
452	case IBMVFC_HOST_OFFLINE:
453		rc = -EINVAL;
454		break;
455	default:
456		vhost->state = state;
457		break;
458	};
459
460	return rc;
461}
462
463/**
464 * ibmvfc_set_host_action - Set the next init action for the host
465 * @vhost:		ibmvfc host struct
466 * @action:		action to perform
467 *
468 **/
469static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
470				   enum ibmvfc_host_action action)
471{
472	switch (action) {
473	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
474		if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
475			vhost->action = action;
476		break;
477	case IBMVFC_HOST_ACTION_INIT_WAIT:
478		if (vhost->action == IBMVFC_HOST_ACTION_INIT)
479			vhost->action = action;
480		break;
481	case IBMVFC_HOST_ACTION_QUERY:
482		switch (vhost->action) {
483		case IBMVFC_HOST_ACTION_INIT_WAIT:
484		case IBMVFC_HOST_ACTION_NONE:
485		case IBMVFC_HOST_ACTION_TGT_ADD:
486			vhost->action = action;
487			break;
488		default:
489			break;
490		};
491		break;
492	case IBMVFC_HOST_ACTION_TGT_INIT:
493		if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
494			vhost->action = action;
495		break;
496	case IBMVFC_HOST_ACTION_INIT:
497	case IBMVFC_HOST_ACTION_TGT_DEL:
498	case IBMVFC_HOST_ACTION_QUERY_TGTS:
499	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
500	case IBMVFC_HOST_ACTION_TGT_ADD:
501	case IBMVFC_HOST_ACTION_NONE:
502	default:
503		vhost->action = action;
504		break;
505	};
506}
507
508/**
509 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
510 * @vhost:		ibmvfc host struct
511 *
512 * Return value:
513 *	nothing
514 **/
515static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
516{
517	if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
518		if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
519			scsi_block_requests(vhost->host);
520			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
521		}
522	} else
523		vhost->reinit = 1;
524
525	wake_up(&vhost->work_wait_q);
526}
527
528/**
529 * ibmvfc_link_down - Handle a link down event from the adapter
530 * @vhost:	ibmvfc host struct
531 * @state:	ibmvfc host state to enter
532 *
533 **/
534static void ibmvfc_link_down(struct ibmvfc_host *vhost,
535			     enum ibmvfc_host_state state)
536{
537	struct ibmvfc_target *tgt;
538
539	ENTER;
540	scsi_block_requests(vhost->host);
541	list_for_each_entry(tgt, &vhost->targets, queue)
542		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
543	ibmvfc_set_host_state(vhost, state);
544	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
545	vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
546	wake_up(&vhost->work_wait_q);
547	LEAVE;
548}
549
550/**
551 * ibmvfc_init_host - Start host initialization
552 * @vhost:		ibmvfc host struct
553 * @relogin:	is this a re-login?
554 *
555 * Return value:
556 *	nothing
557 **/
558static void ibmvfc_init_host(struct ibmvfc_host *vhost, int relogin)
559{
560	struct ibmvfc_target *tgt;
561
562	if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
563		if (++vhost->init_retries > IBMVFC_MAX_INIT_RETRIES) {
564			dev_err(vhost->dev,
565				"Host initialization retries exceeded. Taking adapter offline\n");
566			ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
567			return;
568		}
569	}
570
571	if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
572		if (!relogin) {
573			memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
574			vhost->async_crq.cur = 0;
575		}
576
577		list_for_each_entry(tgt, &vhost->targets, queue)
578			tgt->need_login = 1;
579		scsi_block_requests(vhost->host);
580		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
581		vhost->job_step = ibmvfc_npiv_login;
582		wake_up(&vhost->work_wait_q);
583	}
584}
585
586/**
587 * ibmvfc_send_crq - Send a CRQ
588 * @vhost:	ibmvfc host struct
589 * @word1:	the first 64 bits of the data
590 * @word2:	the second 64 bits of the data
591 *
592 * Return value:
593 *	0 on success / other on failure
594 **/
595static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
596{
597	struct vio_dev *vdev = to_vio_dev(vhost->dev);
598	return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
599}
600
601/**
602 * ibmvfc_send_crq_init - Send a CRQ init message
603 * @vhost:	ibmvfc host struct
604 *
605 * Return value:
606 *	0 on success / other on failure
607 **/
608static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
609{
610	ibmvfc_dbg(vhost, "Sending CRQ init\n");
611	return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
612}
613
614/**
615 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
616 * @vhost:	ibmvfc host struct
617 *
618 * Return value:
619 *	0 on success / other on failure
620 **/
621static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
622{
623	ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
624	return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
625}
626
627/**
628 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
629 * @vhost:	ibmvfc host struct
630 *
631 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
632 * the crq with the hypervisor.
633 **/
634static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
635{
636	long rc;
637	struct vio_dev *vdev = to_vio_dev(vhost->dev);
638	struct ibmvfc_crq_queue *crq = &vhost->crq;
639
640	ibmvfc_dbg(vhost, "Releasing CRQ\n");
641	free_irq(vdev->irq, vhost);
642	do {
643		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
644	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
645
646	vhost->state = IBMVFC_NO_CRQ;
647	dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
648	free_page((unsigned long)crq->msgs);
649}
650
651/**
652 * ibmvfc_reenable_crq_queue - reenables the CRQ
653 * @vhost:	ibmvfc host struct
654 *
655 * Return value:
656 *	0 on success / other on failure
657 **/
658static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
659{
660	int rc;
661	struct vio_dev *vdev = to_vio_dev(vhost->dev);
662
663	/* Re-enable the CRQ */
664	do {
665		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
666	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
667
668	if (rc)
669		dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
670
671	return rc;
672}
673
674/**
675 * ibmvfc_reset_crq - resets a crq after a failure
676 * @vhost:	ibmvfc host struct
677 *
678 * Return value:
679 *	0 on success / other on failure
680 **/
681static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
682{
683	int rc;
684	struct vio_dev *vdev = to_vio_dev(vhost->dev);
685	struct ibmvfc_crq_queue *crq = &vhost->crq;
686
687	/* Close the CRQ */
688	do {
689		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
690	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
691
692	vhost->state = IBMVFC_NO_CRQ;
693	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
694
695	/* Clean out the queue */
696	memset(crq->msgs, 0, PAGE_SIZE);
697	crq->cur = 0;
698
699	/* And re-open it again */
700	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
701				crq->msg_token, PAGE_SIZE);
702
703	if (rc == H_CLOSED)
704		/* Adapter is good, but other end is not ready */
705		dev_warn(vhost->dev, "Partner adapter not ready\n");
706	else if (rc != 0)
707		dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
708
709	return rc;
710}
711
712/**
713 * ibmvfc_valid_event - Determines if event is valid.
714 * @pool:	event_pool that contains the event
715 * @evt:	ibmvfc event to be checked for validity
716 *
717 * Return value:
718 *	1 if event is valid / 0 if event is not valid
719 **/
720static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
721			      struct ibmvfc_event *evt)
722{
723	int index = evt - pool->events;
724	if (index < 0 || index >= pool->size)	/* outside of bounds */
725		return 0;
726	if (evt != pool->events + index)	/* unaligned */
727		return 0;
728	return 1;
729}
730
731/**
732 * ibmvfc_free_event - Free the specified event
733 * @evt:	ibmvfc_event to be freed
734 *
735 **/
736static void ibmvfc_free_event(struct ibmvfc_event *evt)
737{
738	struct ibmvfc_host *vhost = evt->vhost;
739	struct ibmvfc_event_pool *pool = &vhost->pool;
740
741	BUG_ON(!ibmvfc_valid_event(pool, evt));
742	BUG_ON(atomic_inc_return(&evt->free) != 1);
743	list_add_tail(&evt->queue, &vhost->free);
744}
745
746/**
747 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
748 * @evt:	ibmvfc event struct
749 *
750 * This function does not setup any error status, that must be done
751 * before this function gets called.
752 **/
753static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
754{
755	struct scsi_cmnd *cmnd = evt->cmnd;
756
757	if (cmnd) {
758		scsi_dma_unmap(cmnd);
759		cmnd->scsi_done(cmnd);
760	}
761
762	if (evt->eh_comp)
763		complete(evt->eh_comp);
764
765	ibmvfc_free_event(evt);
766}
767
768/**
769 * ibmvfc_fail_request - Fail request with specified error code
770 * @evt:		ibmvfc event struct
771 * @error_code:	error code to fail request with
772 *
773 * Return value:
774 *	none
775 **/
776static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
777{
778	if (evt->cmnd) {
779		evt->cmnd->result = (error_code << 16);
780		evt->done = ibmvfc_scsi_eh_done;
781	} else
782		evt->xfer_iu->mad_common.status = IBMVFC_MAD_DRIVER_FAILED;
783
784	list_del(&evt->queue);
785	del_timer(&evt->timer);
786	ibmvfc_trc_end(evt);
787	evt->done(evt);
788}
789
790/**
791 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
792 * @vhost:		ibmvfc host struct
793 * @error_code:	error code to fail requests with
794 *
795 * Return value:
796 *	none
797 **/
798static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
799{
800	struct ibmvfc_event *evt, *pos;
801
802	ibmvfc_dbg(vhost, "Purging all requests\n");
803	list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
804		ibmvfc_fail_request(evt, error_code);
805}
806
807/**
808 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
809 * @vhost:	struct ibmvfc host to reset
810 **/
811static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
812{
813	int rc;
814
815	scsi_block_requests(vhost->host);
816	ibmvfc_purge_requests(vhost, DID_ERROR);
817	if ((rc = ibmvfc_reset_crq(vhost)) ||
818	    (rc = ibmvfc_send_crq_init(vhost)) ||
819	    (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
820		dev_err(vhost->dev, "Error after reset rc=%d\n", rc);
821		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
822	} else
823		ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
824}
825
826/**
827 * ibmvfc_reset_host - Reset the connection to the server
828 * @vhost:	struct ibmvfc host to reset
829 **/
830static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
831{
832	unsigned long flags;
833
834	spin_lock_irqsave(vhost->host->host_lock, flags);
835	__ibmvfc_reset_host(vhost);
836	spin_unlock_irqrestore(vhost->host->host_lock, flags);
837}
838
839/**
840 * ibmvfc_retry_host_init - Retry host initialization if allowed
841 * @vhost:	ibmvfc host struct
842 *
843 **/
844static void ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
845{
846	if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
847		if (++vhost->init_retries > IBMVFC_MAX_INIT_RETRIES) {
848			dev_err(vhost->dev,
849				"Host initialization retries exceeded. Taking adapter offline\n");
850			ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
851		} else if (vhost->init_retries == IBMVFC_MAX_INIT_RETRIES)
852			__ibmvfc_reset_host(vhost);
853		else
854			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
855	}
856
857	wake_up(&vhost->work_wait_q);
858}
859
860/**
861 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
862 * @starget:	scsi target struct
863 *
864 * Return value:
865 *	ibmvfc_target struct / NULL if not found
866 **/
867static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
868{
869	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
870	struct ibmvfc_host *vhost = shost_priv(shost);
871	struct ibmvfc_target *tgt;
872
873	list_for_each_entry(tgt, &vhost->targets, queue)
874		if (tgt->target_id == starget->id) {
875			kref_get(&tgt->kref);
876			return tgt;
877		}
878	return NULL;
879}
880
881/**
882 * ibmvfc_get_target - Find the specified scsi_target
883 * @starget:	scsi target struct
884 *
885 * Return value:
886 *	ibmvfc_target struct / NULL if not found
887 **/
888static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
889{
890	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
891	struct ibmvfc_target *tgt;
892	unsigned long flags;
893
894	spin_lock_irqsave(shost->host_lock, flags);
895	tgt = __ibmvfc_get_target(starget);
896	spin_unlock_irqrestore(shost->host_lock, flags);
897	return tgt;
898}
899
900/**
901 * ibmvfc_get_host_speed - Get host port speed
902 * @shost:		scsi host struct
903 *
904 * Return value:
905 * 	none
906 **/
907static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
908{
909	struct ibmvfc_host *vhost = shost_priv(shost);
910	unsigned long flags;
911
912	spin_lock_irqsave(shost->host_lock, flags);
913	if (vhost->state == IBMVFC_ACTIVE) {
914		switch (vhost->login_buf->resp.link_speed / 100) {
915		case 1:
916			fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
917			break;
918		case 2:
919			fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
920			break;
921		case 4:
922			fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
923			break;
924		case 8:
925			fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
926			break;
927		case 10:
928			fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
929			break;
930		case 16:
931			fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
932			break;
933		default:
934			ibmvfc_log(vhost, 3, "Unknown port speed: %ld Gbit\n",
935				   vhost->login_buf->resp.link_speed / 100);
936			fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
937			break;
938		}
939	} else
940		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
941	spin_unlock_irqrestore(shost->host_lock, flags);
942}
943
944/**
945 * ibmvfc_get_host_port_state - Get host port state
946 * @shost:		scsi host struct
947 *
948 * Return value:
949 * 	none
950 **/
951static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
952{
953	struct ibmvfc_host *vhost = shost_priv(shost);
954	unsigned long flags;
955
956	spin_lock_irqsave(shost->host_lock, flags);
957	switch (vhost->state) {
958	case IBMVFC_INITIALIZING:
959	case IBMVFC_ACTIVE:
960		fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
961		break;
962	case IBMVFC_LINK_DOWN:
963		fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
964		break;
965	case IBMVFC_LINK_DEAD:
966	case IBMVFC_HOST_OFFLINE:
967		fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
968		break;
969	case IBMVFC_HALTED:
970		fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
971		break;
972	case IBMVFC_NO_CRQ:
973		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
974		break;
975	default:
976		ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
977		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
978		break;
979	}
980	spin_unlock_irqrestore(shost->host_lock, flags);
981}
982
983/**
984 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
985 * @rport:		rport struct
986 * @timeout:	timeout value
987 *
988 * Return value:
989 * 	none
990 **/
991static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
992{
993	if (timeout)
994		rport->dev_loss_tmo = timeout;
995	else
996		rport->dev_loss_tmo = 1;
997}
998
999/**
1000 * ibmvfc_release_tgt - Free memory allocated for a target
1001 * @kref:		kref struct
1002 *
1003 **/
1004static void ibmvfc_release_tgt(struct kref *kref)
1005{
1006	struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1007	kfree(tgt);
1008}
1009
1010/**
1011 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1012 * @starget:	scsi target struct
1013 *
1014 * Return value:
1015 * 	none
1016 **/
1017static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1018{
1019	struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1020	fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1021	if (tgt)
1022		kref_put(&tgt->kref, ibmvfc_release_tgt);
1023}
1024
1025/**
1026 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1027 * @starget:	scsi target struct
1028 *
1029 * Return value:
1030 * 	none
1031 **/
1032static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1033{
1034	struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1035	fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1036	if (tgt)
1037		kref_put(&tgt->kref, ibmvfc_release_tgt);
1038}
1039
1040/**
1041 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1042 * @starget:	scsi target struct
1043 *
1044 * Return value:
1045 * 	none
1046 **/
1047static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1048{
1049	struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1050	fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1051	if (tgt)
1052		kref_put(&tgt->kref, ibmvfc_release_tgt);
1053}
1054
1055/**
1056 * ibmvfc_wait_while_resetting - Wait while the host resets
1057 * @vhost:		ibmvfc host struct
1058 *
1059 * Return value:
1060 * 	0 on success / other on failure
1061 **/
1062static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1063{
1064	long timeout = wait_event_timeout(vhost->init_wait_q,
1065					  ((vhost->state == IBMVFC_ACTIVE ||
1066					    vhost->state == IBMVFC_HOST_OFFLINE ||
1067					    vhost->state == IBMVFC_LINK_DEAD) &&
1068					   vhost->action == IBMVFC_HOST_ACTION_NONE),
1069					  (init_timeout * HZ));
1070
1071	return timeout ? 0 : -EIO;
1072}
1073
1074/**
1075 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1076 * @shost:		scsi host struct
1077 *
1078 * Return value:
1079 * 	0 on success / other on failure
1080 **/
1081static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1082{
1083	struct ibmvfc_host *vhost = shost_priv(shost);
1084
1085	dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1086	ibmvfc_reset_host(vhost);
1087	return ibmvfc_wait_while_resetting(vhost);
1088}
1089
1090/**
1091 * ibmvfc_gather_partition_info - Gather info about the LPAR
1092 *
1093 * Return value:
1094 *	none
1095 **/
1096static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1097{
1098	struct device_node *rootdn;
1099	const char *name;
1100	const unsigned int *num;
1101
1102	rootdn = of_find_node_by_path("/");
1103	if (!rootdn)
1104		return;
1105
1106	name = of_get_property(rootdn, "ibm,partition-name", NULL);
1107	if (name)
1108		strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1109	num = of_get_property(rootdn, "ibm,partition-no", NULL);
1110	if (num)
1111		vhost->partition_number = *num;
1112	of_node_put(rootdn);
1113}
1114
1115/**
1116 * ibmvfc_set_login_info - Setup info for NPIV login
1117 * @vhost:	ibmvfc host struct
1118 *
1119 * Return value:
1120 *	none
1121 **/
1122static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1123{
1124	struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1125	struct device_node *of_node = vhost->dev->archdata.of_node;
1126	const char *location;
1127
1128	memset(login_info, 0, sizeof(*login_info));
1129
1130	login_info->ostype = IBMVFC_OS_LINUX;
1131	login_info->max_dma_len = IBMVFC_MAX_SECTORS << 9;
1132	login_info->max_payload = sizeof(struct ibmvfc_fcp_cmd_iu);
1133	login_info->max_response = sizeof(struct ibmvfc_fcp_rsp);
1134	login_info->partition_num = vhost->partition_number;
1135	login_info->vfc_frame_version = 1;
1136	login_info->fcp_version = 3;
1137	if (vhost->client_migrated)
1138		login_info->flags = IBMVFC_CLIENT_MIGRATED;
1139
1140	login_info->max_cmds = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1141	login_info->capabilities = IBMVFC_CAN_MIGRATE;
1142	login_info->async.va = vhost->async_crq.msg_token;
1143	login_info->async.len = vhost->async_crq.size * sizeof(*vhost->async_crq.msgs);
1144	strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1145	strncpy(login_info->device_name,
1146		vhost->host->shost_gendev.bus_id, IBMVFC_MAX_NAME);
1147
1148	location = of_get_property(of_node, "ibm,loc-code", NULL);
1149	location = location ? location : vhost->dev->bus_id;
1150	strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1151}
1152
1153/**
1154 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1155 * @vhost:	ibmvfc host who owns the event pool
1156 *
1157 * Returns zero on success.
1158 **/
1159static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1160{
1161	int i;
1162	struct ibmvfc_event_pool *pool = &vhost->pool;
1163
1164	ENTER;
1165	pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1166	pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1167	if (!pool->events)
1168		return -ENOMEM;
1169
1170	pool->iu_storage = dma_alloc_coherent(vhost->dev,
1171					      pool->size * sizeof(*pool->iu_storage),
1172					      &pool->iu_token, 0);
1173
1174	if (!pool->iu_storage) {
1175		kfree(pool->events);
1176		return -ENOMEM;
1177	}
1178
1179	for (i = 0; i < pool->size; ++i) {
1180		struct ibmvfc_event *evt = &pool->events[i];
1181		atomic_set(&evt->free, 1);
1182		evt->crq.valid = 0x80;
1183		evt->crq.ioba = pool->iu_token + (sizeof(*evt->xfer_iu) * i);
1184		evt->xfer_iu = pool->iu_storage + i;
1185		evt->vhost = vhost;
1186		evt->ext_list = NULL;
1187		list_add_tail(&evt->queue, &vhost->free);
1188	}
1189
1190	LEAVE;
1191	return 0;
1192}
1193
1194/**
1195 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1196 * @vhost:	ibmvfc host who owns the event pool
1197 *
1198 **/
1199static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1200{
1201	int i;
1202	struct ibmvfc_event_pool *pool = &vhost->pool;
1203
1204	ENTER;
1205	for (i = 0; i < pool->size; ++i) {
1206		list_del(&pool->events[i].queue);
1207		BUG_ON(atomic_read(&pool->events[i].free) != 1);
1208		if (pool->events[i].ext_list)
1209			dma_pool_free(vhost->sg_pool,
1210				      pool->events[i].ext_list,
1211				      pool->events[i].ext_list_token);
1212	}
1213
1214	kfree(pool->events);
1215	dma_free_coherent(vhost->dev,
1216			  pool->size * sizeof(*pool->iu_storage),
1217			  pool->iu_storage, pool->iu_token);
1218	LEAVE;
1219}
1220
1221/**
1222 * ibmvfc_get_event - Gets the next free event in pool
1223 * @vhost:	ibmvfc host struct
1224 *
1225 * Returns a free event from the pool.
1226 **/
1227static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1228{
1229	struct ibmvfc_event *evt;
1230
1231	BUG_ON(list_empty(&vhost->free));
1232	evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1233	atomic_set(&evt->free, 0);
1234	list_del(&evt->queue);
1235	return evt;
1236}
1237
1238/**
1239 * ibmvfc_init_event - Initialize fields in an event struct that are always
1240 *				required.
1241 * @evt:	The event
1242 * @done:	Routine to call when the event is responded to
1243 * @format:	SRP or MAD format
1244 **/
1245static void ibmvfc_init_event(struct ibmvfc_event *evt,
1246			      void (*done) (struct ibmvfc_event *), u8 format)
1247{
1248	evt->cmnd = NULL;
1249	evt->sync_iu = NULL;
1250	evt->crq.format = format;
1251	evt->done = done;
1252	evt->eh_comp = NULL;
1253}
1254
1255/**
1256 * ibmvfc_map_sg_list - Initialize scatterlist
1257 * @scmd:	scsi command struct
1258 * @nseg:	number of scatterlist segments
1259 * @md:	memory descriptor list to initialize
1260 **/
1261static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1262			       struct srp_direct_buf *md)
1263{
1264	int i;
1265	struct scatterlist *sg;
1266
1267	scsi_for_each_sg(scmd, sg, nseg, i) {
1268		md[i].va = sg_dma_address(sg);
1269		md[i].len = sg_dma_len(sg);
1270		md[i].key = 0;
1271	}
1272}
1273
1274/**
1275 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1276 * @scmd:		Scsi_Cmnd with the scatterlist
1277 * @evt:		ibmvfc event struct
1278 * @vfc_cmd:	vfc_cmd that contains the memory descriptor
1279 * @dev:		device for which to map dma memory
1280 *
1281 * Returns:
1282 *	0 on success / non-zero on failure
1283 **/
1284static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1285			      struct ibmvfc_event *evt,
1286			      struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1287{
1288
1289	int sg_mapped;
1290	struct srp_direct_buf *data = &vfc_cmd->ioba;
1291	struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1292
1293	sg_mapped = scsi_dma_map(scmd);
1294	if (!sg_mapped) {
1295		vfc_cmd->flags |= IBMVFC_NO_MEM_DESC;
1296		return 0;
1297	} else if (unlikely(sg_mapped < 0)) {
1298		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1299			scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1300		return sg_mapped;
1301	}
1302
1303	if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1304		vfc_cmd->flags |= IBMVFC_WRITE;
1305		vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1306	} else {
1307		vfc_cmd->flags |= IBMVFC_READ;
1308		vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1309	}
1310
1311	if (sg_mapped == 1) {
1312		ibmvfc_map_sg_list(scmd, sg_mapped, data);
1313		return 0;
1314	}
1315
1316	vfc_cmd->flags |= IBMVFC_SCATTERLIST;
1317
1318	if (!evt->ext_list) {
1319		evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1320					       &evt->ext_list_token);
1321
1322		if (!evt->ext_list) {
1323			scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1324			return -ENOMEM;
1325		}
1326	}
1327
1328	ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1329
1330	data->va = evt->ext_list_token;
1331	data->len = sg_mapped * sizeof(struct srp_direct_buf);
1332	data->key = 0;
1333	return 0;
1334}
1335
1336/**
1337 * ibmvfc_timeout - Internal command timeout handler
1338 * @evt:	struct ibmvfc_event that timed out
1339 *
1340 * Called when an internally generated command times out
1341 **/
1342static void ibmvfc_timeout(struct ibmvfc_event *evt)
1343{
1344	struct ibmvfc_host *vhost = evt->vhost;
1345	dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1346	ibmvfc_reset_host(vhost);
1347}
1348
1349/**
1350 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1351 * @evt:		event to be sent
1352 * @vhost:		ibmvfc host struct
1353 * @timeout:	timeout in seconds - 0 means do not time command
1354 *
1355 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1356 **/
1357static int ibmvfc_send_event(struct ibmvfc_event *evt,
1358			     struct ibmvfc_host *vhost, unsigned long timeout)
1359{
1360	u64 *crq_as_u64 = (u64 *) &evt->crq;
1361	int rc;
1362
1363	/* Copy the IU into the transfer area */
1364	*evt->xfer_iu = evt->iu;
1365	if (evt->crq.format == IBMVFC_CMD_FORMAT)
1366		evt->xfer_iu->cmd.tag = (u64)evt;
1367	else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1368		evt->xfer_iu->mad_common.tag = (u64)evt;
1369	else
1370		BUG();
1371
1372	list_add_tail(&evt->queue, &vhost->sent);
1373	init_timer(&evt->timer);
1374
1375	if (timeout) {
1376		evt->timer.data = (unsigned long) evt;
1377		evt->timer.expires = jiffies + (timeout * HZ);
1378		evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
1379		add_timer(&evt->timer);
1380	}
1381
1382	if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
1383		list_del(&evt->queue);
1384		del_timer(&evt->timer);
1385
1386		/* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1387		 * Firmware will send a CRQ with a transport event (0xFF) to
1388		 * tell this client what has happened to the transport. This
1389		 * will be handled in ibmvfc_handle_crq()
1390		 */
1391		if (rc == H_CLOSED) {
1392			if (printk_ratelimit())
1393				dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1394			if (evt->cmnd)
1395				scsi_dma_unmap(evt->cmnd);
1396			ibmvfc_free_event(evt);
1397			return SCSI_MLQUEUE_HOST_BUSY;
1398		}
1399
1400		dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1401		if (evt->cmnd) {
1402			evt->cmnd->result = DID_ERROR << 16;
1403			evt->done = ibmvfc_scsi_eh_done;
1404		} else
1405			evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
1406
1407		evt->done(evt);
1408	} else
1409		ibmvfc_trc_start(evt);
1410
1411	return 0;
1412}
1413
1414/**
1415 * ibmvfc_log_error - Log an error for the failed command if appropriate
1416 * @evt:	ibmvfc event to log
1417 *
1418 **/
1419static void ibmvfc_log_error(struct ibmvfc_event *evt)
1420{
1421	struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1422	struct ibmvfc_host *vhost = evt->vhost;
1423	struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1424	struct scsi_cmnd *cmnd = evt->cmnd;
1425	const char *err = unknown_error;
1426	int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
1427	int logerr = 0;
1428	int rsp_code = 0;
1429
1430	if (index >= 0) {
1431		logerr = cmd_status[index].log;
1432		err = cmd_status[index].name;
1433	}
1434
1435	if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1436		return;
1437
1438	if (rsp->flags & FCP_RSP_LEN_VALID)
1439		rsp_code = rsp->data.info.rsp_code;
1440
1441	scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
1442		    "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1443		    cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
1444		    rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1445}
1446
1447/**
1448 * ibmvfc_scsi_done - Handle responses from commands
1449 * @evt:	ibmvfc event to be handled
1450 *
1451 * Used as a callback when sending scsi cmds.
1452 **/
1453static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1454{
1455	struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1456	struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1457	struct scsi_cmnd *cmnd = evt->cmnd;
1458	u32 rsp_len = 0;
1459	u32 sense_len = rsp->fcp_sense_len;
1460
1461	if (cmnd) {
1462		if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
1463			scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
1464		else if (rsp->flags & FCP_RESID_UNDER)
1465			scsi_set_resid(cmnd, rsp->fcp_resid);
1466		else
1467			scsi_set_resid(cmnd, 0);
1468
1469		if (vfc_cmd->status) {
1470			cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1471
1472			if (rsp->flags & FCP_RSP_LEN_VALID)
1473				rsp_len = rsp->fcp_rsp_len;
1474			if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1475				sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1476			if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1477				memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1478
1479			if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1480				cmnd->result = (DID_ERROR << 16);
1481
1482			ibmvfc_log_error(evt);
1483		}
1484
1485		if (!cmnd->result &&
1486		    (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1487			cmnd->result = (DID_ERROR << 16);
1488
1489		scsi_dma_unmap(cmnd);
1490		cmnd->scsi_done(cmnd);
1491	}
1492
1493	if (evt->eh_comp)
1494		complete(evt->eh_comp);
1495
1496	ibmvfc_free_event(evt);
1497}
1498
1499/**
1500 * ibmvfc_host_chkready - Check if the host can accept commands
1501 * @vhost:	 struct ibmvfc host
1502 *
1503 * Returns:
1504 *	1 if host can accept command / 0 if not
1505 **/
1506static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1507{
1508	int result = 0;
1509
1510	switch (vhost->state) {
1511	case IBMVFC_LINK_DEAD:
1512	case IBMVFC_HOST_OFFLINE:
1513		result = DID_NO_CONNECT << 16;
1514		break;
1515	case IBMVFC_NO_CRQ:
1516	case IBMVFC_INITIALIZING:
1517	case IBMVFC_HALTED:
1518	case IBMVFC_LINK_DOWN:
1519		result = DID_REQUEUE << 16;
1520		break;
1521	case IBMVFC_ACTIVE:
1522		result = 0;
1523		break;
1524	};
1525
1526	return result;
1527}
1528
1529/**
1530 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1531 * @cmnd:	struct scsi_cmnd to be executed
1532 * @done:	Callback function to be called when cmnd is completed
1533 *
1534 * Returns:
1535 *	0 on success / other on failure
1536 **/
1537static int ibmvfc_queuecommand(struct scsi_cmnd *cmnd,
1538			       void (*done) (struct scsi_cmnd *))
1539{
1540	struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1541	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1542	struct ibmvfc_cmd *vfc_cmd;
1543	struct ibmvfc_event *evt;
1544	u8 tag[2];
1545	int rc;
1546
1547	if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1548	    unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1549		cmnd->result = rc;
1550		done(cmnd);
1551		return 0;
1552	}
1553
1554	cmnd->result = (DID_OK << 16);
1555	evt = ibmvfc_get_event(vhost);
1556	ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1557	evt->cmnd = cmnd;
1558	cmnd->scsi_done = done;
1559	vfc_cmd = &evt->iu.cmd;
1560	memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1561	vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1562	vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
1563	vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
1564	vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
1565	vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
1566	vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
1567	vfc_cmd->tgt_scsi_id = rport->port_id;
1568	if ((rport->supported_classes & FC_COS_CLASS3) &&
1569	    (fc_host_supported_classes(vhost->host) & FC_COS_CLASS3))
1570		vfc_cmd->flags = IBMVFC_CLASS_3_ERR;
1571	vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
1572	int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1573	memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1574
1575	if (scsi_populate_tag_msg(cmnd, tag)) {
1576		vfc_cmd->task_tag = tag[1];
1577		switch (tag[0]) {
1578		case MSG_SIMPLE_TAG:
1579			vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1580			break;
1581		case MSG_HEAD_TAG:
1582			vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
1583			break;
1584		case MSG_ORDERED_TAG:
1585			vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
1586			break;
1587		};
1588	}
1589
1590	if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1591		return ibmvfc_send_event(evt, vhost, 0);
1592
1593	ibmvfc_free_event(evt);
1594	if (rc == -ENOMEM)
1595		return SCSI_MLQUEUE_HOST_BUSY;
1596
1597	if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1598		scmd_printk(KERN_ERR, cmnd,
1599			    "Failed to map DMA buffer for command. rc=%d\n", rc);
1600
1601	cmnd->result = DID_ERROR << 16;
1602	done(cmnd);
1603	return 0;
1604}
1605
1606/**
1607 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1608 * @evt:	ibmvfc event struct
1609 *
1610 **/
1611static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1612{
1613	/* copy the response back */
1614	if (evt->sync_iu)
1615		*evt->sync_iu = *evt->xfer_iu;
1616
1617	complete(&evt->comp);
1618}
1619
1620/**
1621 * ibmvfc_reset_device - Reset the device with the specified reset type
1622 * @sdev:	scsi device to reset
1623 * @type:	reset type
1624 * @desc:	reset type description for log messages
1625 *
1626 * Returns:
1627 *	0 on success / other on failure
1628 **/
1629static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1630{
1631	struct ibmvfc_host *vhost = shost_priv(sdev->host);
1632	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1633	struct ibmvfc_cmd *tmf;
1634	struct ibmvfc_event *evt;
1635	union ibmvfc_iu rsp_iu;
1636	struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1637	int rsp_rc = -EBUSY;
1638	unsigned long flags;
1639	int rsp_code = 0;
1640
1641	spin_lock_irqsave(vhost->host->host_lock, flags);
1642	if (vhost->state == IBMVFC_ACTIVE) {
1643		evt = ibmvfc_get_event(vhost);
1644		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1645
1646		tmf = &evt->iu.cmd;
1647		memset(tmf, 0, sizeof(*tmf));
1648		tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1649		tmf->resp.len = sizeof(tmf->rsp);
1650		tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1651		tmf->payload_len = sizeof(tmf->iu);
1652		tmf->resp_len = sizeof(tmf->rsp);
1653		tmf->cancel_key = (unsigned long)sdev->hostdata;
1654		tmf->tgt_scsi_id = rport->port_id;
1655		int_to_scsilun(sdev->lun, &tmf->iu.lun);
1656		tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1657		tmf->iu.tmf_flags = type;
1658		evt->sync_iu = &rsp_iu;
1659
1660		init_completion(&evt->comp);
1661		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1662	}
1663	spin_unlock_irqrestore(vhost->host->host_lock, flags);
1664
1665	if (rsp_rc != 0) {
1666		sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
1667			    desc, rsp_rc);
1668		return -EIO;
1669	}
1670
1671	sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
1672	wait_for_completion(&evt->comp);
1673
1674	if (rsp_iu.cmd.status) {
1675		if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1676			rsp_code = fc_rsp->data.info.rsp_code;
1677
1678		sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
1679			    "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1680			    desc, ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1681			    rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1682			    fc_rsp->scsi_status);
1683		rsp_rc = -EIO;
1684	} else
1685		sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
1686
1687	spin_lock_irqsave(vhost->host->host_lock, flags);
1688	ibmvfc_free_event(evt);
1689	spin_unlock_irqrestore(vhost->host->host_lock, flags);
1690	return rsp_rc;
1691}
1692
1693/**
1694 * ibmvfc_abort_task_set - Abort outstanding commands to the device
1695 * @sdev:	scsi device to abort commands
1696 *
1697 * This sends an Abort Task Set to the VIOS for the specified device. This does
1698 * NOT send any cancel to the VIOS. That must be done separately.
1699 *
1700 * Returns:
1701 *	0 on success / other on failure
1702 **/
1703static int ibmvfc_abort_task_set(struct scsi_device *sdev)
1704{
1705	struct ibmvfc_host *vhost = shost_priv(sdev->host);
1706	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1707	struct ibmvfc_cmd *tmf;
1708	struct ibmvfc_event *evt, *found_evt;
1709	union ibmvfc_iu rsp_iu;
1710	struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1711	int rsp_rc = -EBUSY;
1712	unsigned long flags;
1713	int rsp_code = 0;
1714
1715	spin_lock_irqsave(vhost->host->host_lock, flags);
1716	found_evt = NULL;
1717	list_for_each_entry(evt, &vhost->sent, queue) {
1718		if (evt->cmnd && evt->cmnd->device == sdev) {
1719			found_evt = evt;
1720			break;
1721		}
1722	}
1723
1724	if (!found_evt) {
1725		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1726			sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
1727		spin_unlock_irqrestore(vhost->host->host_lock, flags);
1728		return 0;
1729	}
1730
1731	if (vhost->state == IBMVFC_ACTIVE) {
1732		evt = ibmvfc_get_event(vhost);
1733		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1734
1735		tmf = &evt->iu.cmd;
1736		memset(tmf, 0, sizeof(*tmf));
1737		tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1738		tmf->resp.len = sizeof(tmf->rsp);
1739		tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1740		tmf->payload_len = sizeof(tmf->iu);
1741		tmf->resp_len = sizeof(tmf->rsp);
1742		tmf->cancel_key = (unsigned long)sdev->hostdata;
1743		tmf->tgt_scsi_id = rport->port_id;
1744		int_to_scsilun(sdev->lun, &tmf->iu.lun);
1745		tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1746		tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
1747		evt->sync_iu = &rsp_iu;
1748
1749		init_completion(&evt->comp);
1750		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1751	}
1752
1753	spin_unlock_irqrestore(vhost->host->host_lock, flags);
1754
1755	if (rsp_rc != 0) {
1756		sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
1757		return -EIO;
1758	}
1759
1760	sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
1761	wait_for_completion(&evt->comp);
1762
1763	if (rsp_iu.cmd.status) {
1764		if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1765			rsp_code = fc_rsp->data.info.rsp_code;
1766
1767		sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
1768			    "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1769			    ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1770			    rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1771			    fc_rsp->scsi_status);
1772		rsp_rc = -EIO;
1773	} else
1774		sdev_printk(KERN_INFO, sdev, "Abort successful\n");
1775
1776	spin_lock_irqsave(vhost->host->host_lock, flags);
1777	ibmvfc_free_event(evt);
1778	spin_unlock_irqrestore(vhost->host->host_lock, flags);
1779	return rsp_rc;
1780}
1781
1782/**
1783 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
1784 * @sdev:	scsi device to cancel commands
1785 * @type:	type of error recovery being performed
1786 *
1787 * This sends a cancel to the VIOS for the specified device. This does
1788 * NOT send any abort to the actual device. That must be done separately.
1789 *
1790 * Returns:
1791 *	0 on success / other on failure
1792 **/
1793static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
1794{
1795	struct ibmvfc_host *vhost = shost_priv(sdev->host);
1796	struct scsi_target *starget = scsi_target(sdev);
1797	struct fc_rport *rport = starget_to_rport(starget);
1798	struct ibmvfc_tmf *tmf;
1799	struct ibmvfc_event *evt, *found_evt;
1800	union ibmvfc_iu rsp;
1801	int rsp_rc = -EBUSY;
1802	unsigned long flags;
1803	u16 status;
1804
1805	ENTER;
1806	spin_lock_irqsave(vhost->host->host_lock, flags);
1807	found_evt = NULL;
1808	list_for_each_entry(evt, &vhost->sent, queue) {
1809		if (evt->cmnd && evt->cmnd->device == sdev) {
1810			found_evt = evt;
1811			break;
1812		}
1813	}
1814
1815	if (!found_evt) {
1816		if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1817			sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
1818		spin_unlock_irqrestore(vhost->host->host_lock, flags);
1819		return 0;
1820	}
1821
1822	if (vhost->state == IBMVFC_ACTIVE) {
1823		evt = ibmvfc_get_event(vhost);
1824		ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1825
1826		tmf = &evt->iu.tmf;
1827		memset(tmf, 0, sizeof(*tmf));
1828		tmf->common.version = 1;
1829		tmf->common.opcode = IBMVFC_TMF_MAD;
1830		tmf->common.length = sizeof(*tmf);
1831		tmf->scsi_id = rport->port_id;
1832		int_to_scsilun(sdev->lun, &tmf->lun);
1833		tmf->flags = (type | IBMVFC_TMF_LUA_VALID);
1834		tmf->cancel_key = (unsigned long)sdev->hostdata;
1835		tmf->my_cancel_key = (unsigned long)starget->hostdata;
1836
1837		evt->sync_iu = &rsp;
1838		init_completion(&evt->comp);
1839		rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1840	}
1841
1842	spin_unlock_irqrestore(vhost->host->host_lock, flags);
1843
1844	if (rsp_rc != 0) {
1845		sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
1846		return -EIO;
1847	}
1848
1849	sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
1850
1851	wait_for_completion(&evt->comp);
1852	status = rsp.mad_common.status;
1853	spin_lock_irqsave(vhost->host->host_lock, flags);
1854	ibmvfc_free_event(evt);
1855	spin_unlock_irqrestore(vhost->host->host_lock, flags);
1856
1857	if (status != IBMVFC_MAD_SUCCESS) {
1858		sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
1859		return -EIO;
1860	}
1861
1862	sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
1863	return 0;
1864}
1865
1866/**
1867 * ibmvfc_match_target - Match function for specified target
1868 * @evt:	ibmvfc event struct
1869 * @device:	device to match (starget)
1870 *
1871 * Returns:
1872 *	1 if event matches starget / 0 if event does not match starget
1873 **/
1874static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
1875{
1876	if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
1877		return 1;
1878	return 0;
1879}
1880
1881/**
1882 * ibmvfc_match_lun - Match function for specified LUN
1883 * @evt:	ibmvfc event struct
1884 * @device:	device to match (sdev)
1885 *
1886 * Returns:
1887 *	1 if event matches sdev / 0 if event does not match sdev
1888 **/
1889static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
1890{
1891	if (evt->cmnd && evt->cmnd->device == device)
1892		return 1;
1893	return 0;
1894}
1895
1896/**
1897 * ibmvfc_wait_for_ops - Wait for ops to complete
1898 * @vhost:	ibmvfc host struct
1899 * @device:	device to match (starget or sdev)
1900 * @match:	match function
1901 *
1902 * Returns:
1903 *	SUCCESS / FAILED
1904 **/
1905static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
1906			       int (*match) (struct ibmvfc_event *, void *))
1907{
1908	struct ibmvfc_event *evt;
1909	DECLARE_COMPLETION_ONSTACK(comp);
1910	int wait;
1911	unsigned long flags;
1912	signed long timeout = init_timeout * HZ;
1913
1914	ENTER;
1915	do {
1916		wait = 0;
1917		spin_lock_irqsave(vhost->host->host_lock, flags);
1918		list_for_each_entry(evt, &vhost->sent, queue) {
1919			if (match(evt, device)) {
1920				evt->eh_comp = &comp;
1921				wait++;
1922			}
1923		}
1924		spin_unlock_irqrestore(vhost->host->host_lock, flags);
1925
1926		if (wait) {
1927			timeout = wait_for_completion_timeout(&comp, timeout);
1928
1929			if (!timeout) {
1930				wait = 0;
1931				spin_lock_irqsave(vhost->host->host_lock, flags);
1932				list_for_each_entry(evt, &vhost->sent, queue) {
1933					if (match(evt, device)) {
1934						evt->eh_comp = NULL;
1935						wait++;
1936					}
1937				}
1938				spin_unlock_irqrestore(vhost->host->host_lock, flags);
1939				if (wait)
1940					dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
1941				LEAVE;
1942				return wait ? FAILED : SUCCESS;
1943			}
1944		}
1945	} while (wait);
1946
1947	LEAVE;
1948	return SUCCESS;
1949}
1950
1951/**
1952 * ibmvfc_eh_abort_handler - Abort a command
1953 * @cmd:	scsi command to abort
1954 *
1955 * Returns:
1956 *	SUCCESS / FAILED
1957 **/
1958static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
1959{
1960	struct scsi_device *sdev = cmd->device;
1961	struct ibmvfc_host *vhost = shost_priv(sdev->host);
1962	int cancel_rc, abort_rc;
1963	int rc = FAILED;
1964
1965	ENTER;
1966	ibmvfc_wait_while_resetting(vhost);
1967	cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
1968	abort_rc = ibmvfc_abort_task_set(sdev);
1969
1970	if (!cancel_rc && !abort_rc)
1971		rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
1972
1973	LEAVE;
1974	return rc;
1975}
1976
1977/**
1978 * ibmvfc_eh_device_reset_handler - Reset a single LUN
1979 * @cmd:	scsi command struct
1980 *
1981 * Returns:
1982 *	SUCCESS / FAILED
1983 **/
1984static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
1985{
1986	struct scsi_device *sdev = cmd->device;
1987	struct ibmvfc_host *vhost = shost_priv(sdev->host);
1988	int cancel_rc, reset_rc;
1989	int rc = FAILED;
1990
1991	ENTER;
1992	ibmvfc_wait_while_resetting(vhost);
1993	cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
1994	reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
1995
1996	if (!cancel_rc && !reset_rc)
1997		rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
1998
1999	LEAVE;
2000	return rc;
2001}
2002
2003/**
2004 * ibmvfc_dev_cancel_all - Device iterated cancel all function
2005 * @sdev:	scsi device struct
2006 * @data:	return code
2007 *
2008 **/
2009static void ibmvfc_dev_cancel_all(struct scsi_device *sdev, void *data)
2010{
2011	unsigned long *rc = data;
2012	*rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2013}
2014
2015/**
2016 * ibmvfc_dev_abort_all - Device iterated abort task set function
2017 * @sdev:	scsi device struct
2018 * @data:	return code
2019 *
2020 **/
2021static void ibmvfc_dev_abort_all(struct scsi_device *sdev, void *data)
2022{
2023	unsigned long *rc = data;
2024	*rc |= ibmvfc_abort_task_set(sdev);
2025}
2026
2027/**
2028 * ibmvfc_eh_target_reset_handler - Reset the target
2029 * @cmd:	scsi command struct
2030 *
2031 * Returns:
2032 *	SUCCESS / FAILED
2033 **/
2034static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2035{
2036	struct scsi_device *sdev = cmd->device;
2037	struct ibmvfc_host *vhost = shost_priv(sdev->host);
2038	struct scsi_target *starget = scsi_target(sdev);
2039	int reset_rc;
2040	int rc = FAILED;
2041	unsigned long cancel_rc = 0;
2042
2043	ENTER;
2044	ibmvfc_wait_while_resetting(vhost);
2045	starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
2046	reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2047
2048	if (!cancel_rc && !reset_rc)
2049		rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2050
2051	LEAVE;
2052	return rc;
2053}
2054
2055/**
2056 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2057 * @cmd:	struct scsi_cmnd having problems
2058 *
2059 **/
2060static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2061{
2062	int rc;
2063	struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2064
2065	dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2066	rc = ibmvfc_issue_fc_host_lip(vhost->host);
2067	return rc ? FAILED : SUCCESS;
2068}
2069
2070/**
2071 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2072 * @rport:		rport struct
2073 *
2074 * Return value:
2075 * 	none
2076 **/
2077static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2078{
2079	struct scsi_target *starget = to_scsi_target(&rport->dev);
2080	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2081	struct ibmvfc_host *vhost = shost_priv(shost);
2082	unsigned long cancel_rc = 0;
2083	unsigned long abort_rc = 0;
2084	int rc = FAILED;
2085
2086	ENTER;
2087	starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
2088	starget_for_each_device(starget, &abort_rc, ibmvfc_dev_abort_all);
2089
2090	if (!cancel_rc && !abort_rc)
2091		rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2092
2093	if (rc == FAILED)
2094		ibmvfc_issue_fc_host_lip(shost);
2095	LEAVE;
2096}
2097
2098static const struct {
2099	enum ibmvfc_async_event ae;
2100	const char *desc;
2101} ae_desc [] = {
2102	{ IBMVFC_AE_ELS_PLOGI,		"PLOGI" },
2103	{ IBMVFC_AE_ELS_LOGO,		"LOGO" },
2104	{ IBMVFC_AE_ELS_PRLO,		"PRLO" },
2105	{ IBMVFC_AE_SCN_NPORT,		"N-Port SCN" },
2106	{ IBMVFC_AE_SCN_GROUP,		"Group SCN" },
2107	{ IBMVFC_AE_SCN_DOMAIN,		"Domain SCN" },
2108	{ IBMVFC_AE_SCN_FABRIC,		"Fabric SCN" },
2109	{ IBMVFC_AE_LINK_UP,		"Link Up" },
2110	{ IBMVFC_AE_LINK_DOWN,		"Link Down" },
2111	{ IBMVFC_AE_LINK_DEAD,		"Link Dead" },
2112	{ IBMVFC_AE_HALT,			"Halt" },
2113	{ IBMVFC_AE_RESUME,		"Resume" },
2114	{ IBMVFC_AE_ADAPTER_FAILED,	"Adapter Failed" },
2115};
2116
2117static const char *unknown_ae = "Unknown async";
2118
2119/**
2120 * ibmvfc_get_ae_desc - Get text description for async event
2121 * @ae:	async event
2122 *
2123 **/
2124static const char *ibmvfc_get_ae_desc(u64 ae)
2125{
2126	int i;
2127
2128	for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2129		if (ae_desc[i].ae == ae)
2130			return ae_desc[i].desc;
2131
2132	return unknown_ae;
2133}
2134
2135/**
2136 * ibmvfc_handle_async - Handle an async event from the adapter
2137 * @crq:	crq to process
2138 * @vhost:	ibmvfc host struct
2139 *
2140 **/
2141static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2142				struct ibmvfc_host *vhost)
2143{
2144	const char *desc = ibmvfc_get_ae_desc(crq->event);
2145
2146	ibmvfc_log(vhost, 3, "%s event received. scsi_id: %lx, wwpn: %lx,"
2147		   " node_name: %lx\n", desc, crq->scsi_id, crq->wwpn, crq->node_name);
2148
2149	switch (crq->event) {
2150	case IBMVFC_AE_LINK_UP:
2151	case IBMVFC_AE_RESUME:
2152		vhost->events_to_log |= IBMVFC_AE_LINKUP;
2153		ibmvfc_init_host(vhost, 1);
2154		break;
2155	case IBMVFC_AE_SCN_FABRIC:
2156		vhost->events_to_log |= IBMVFC_AE_RSCN;
2157		ibmvfc_init_host(vhost, 1);
2158		break;
2159	case IBMVFC_AE_SCN_NPORT:
2160	case IBMVFC_AE_SCN_GROUP:
2161	case IBMVFC_AE_SCN_DOMAIN:
2162		vhost->events_to_log |= IBMVFC_AE_RSCN;
2163	case IBMVFC_AE_ELS_LOGO:
2164	case IBMVFC_AE_ELS_PRLO:
2165	case IBMVFC_AE_ELS_PLOGI:
2166		ibmvfc_reinit_host(vhost);
2167		break;
2168	case IBMVFC_AE_LINK_DOWN:
2169	case IBMVFC_AE_ADAPTER_FAILED:
2170		ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2171		break;
2172	case IBMVFC_AE_LINK_DEAD:
2173		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2174		break;
2175	case IBMVFC_AE_HALT:
2176		ibmvfc_link_down(vhost, IBMVFC_HALTED);
2177		break;
2178	default:
2179		dev_err(vhost->dev, "Unknown async event received: %ld\n", crq->event);
2180		break;
2181	};
2182}
2183
2184/**
2185 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2186 * @crq:	Command/Response queue
2187 * @vhost:	ibmvfc host struct
2188 *
2189 **/
2190static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2191{
2192	long rc;
2193	struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
2194
2195	switch (crq->valid) {
2196	case IBMVFC_CRQ_INIT_RSP:
2197		switch (crq->format) {
2198		case IBMVFC_CRQ_INIT:
2199			dev_info(vhost->dev, "Partner initialized\n");
2200			/* Send back a response */
2201			rc = ibmvfc_send_crq_init_complete(vhost);
2202			if (rc == 0)
2203				ibmvfc_init_host(vhost, 0);
2204			else
2205				dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2206			break;
2207		case IBMVFC_CRQ_INIT_COMPLETE:
2208			dev_info(vhost->dev, "Partner initialization complete\n");
2209			ibmvfc_init_host(vhost, 0);
2210			break;
2211		default:
2212			dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2213		}
2214		return;
2215	case IBMVFC_CRQ_XPORT_EVENT:
2216		vhost->state = IBMVFC_NO_CRQ;
2217		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2218		if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2219			/* We need to re-setup the interpartition connection */
2220			dev_info(vhost->dev, "Re-enabling adapter\n");
2221			vhost->client_migrated = 1;
2222			ibmvfc_purge_requests(vhost, DID_REQUEUE);
2223			if ((rc = ibmvfc_reenable_crq_queue(vhost)) ||
2224			    (rc = ibmvfc_send_crq_init(vhost))) {
2225				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2226				dev_err(vhost->dev, "Error after enable (rc=%ld)\n", rc);
2227			} else
2228				ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2229		} else {
2230			dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
2231
2232			ibmvfc_purge_requests(vhost, DID_ERROR);
2233			if ((rc = ibmvfc_reset_crq(vhost)) ||
2234			    (rc = ibmvfc_send_crq_init(vhost))) {
2235				ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2236				dev_err(vhost->dev, "Error after reset (rc=%ld)\n", rc);
2237			} else
2238				ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2239		}
2240		return;
2241	case IBMVFC_CRQ_CMD_RSP:
2242		break;
2243	default:
2244		dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2245		return;
2246	}
2247
2248	if (crq->format == IBMVFC_ASYNC_EVENT)
2249		return;
2250
2251	/* The only kind of payload CRQs we should get are responses to
2252	 * things we send. Make sure this response is to something we
2253	 * actually sent
2254	 */
2255	if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2256		dev_err(vhost->dev, "Returned correlation_token 0x%08lx is invalid!\n",
2257			crq->ioba);
2258		return;
2259	}
2260
2261	if (unlikely(atomic_read(&evt->free))) {
2262		dev_err(vhost->dev, "Received duplicate correlation_token 0x%08lx!\n",
2263			crq->ioba);
2264		return;
2265	}
2266
2267	del_timer(&evt->timer);
2268	list_del(&evt->queue);
2269	ibmvfc_trc_end(evt);
2270	evt->done(evt);
2271}
2272
2273/**
2274 * ibmvfc_scan_finished - Check if the device scan is done.
2275 * @shost:	scsi host struct
2276 * @time:	current elapsed time
2277 *
2278 * Returns:
2279 *	0 if scan is not done / 1 if scan is done
2280 **/
2281static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2282{
2283	unsigned long flags;
2284	struct ibmvfc_host *vhost = shost_priv(shost);
2285	int done = 0;
2286
2287	spin_lock_irqsave(shost->host_lock, flags);
2288	if (time >= (init_timeout * HZ)) {
2289		dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2290			 "continuing initialization\n", init_timeout);
2291		done = 1;
2292	}
2293
2294	if (vhost->state != IBMVFC_NO_CRQ && vhost->action == IBMVFC_HOST_ACTION_NONE)
2295		done = 1;
2296	spin_unlock_irqrestore(shost->host_lock, flags);
2297	return done;
2298}
2299
2300/**
2301 * ibmvfc_slave_alloc - Setup the device's task set value
2302 * @sdev:	struct scsi_device device to configure
2303 *
2304 * Set the device's task set value so that error handling works as
2305 * expected.
2306 *
2307 * Returns:
2308 *	0 on success / -ENXIO if device does not exist
2309 **/
2310static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2311{
2312	struct Scsi_Host *shost = sdev->host;
2313	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2314	struct ibmvfc_host *vhost = shost_priv(shost);
2315	unsigned long flags = 0;
2316
2317	if (!rport || fc_remote_port_chkready(rport))
2318		return -ENXIO;
2319
2320	spin_lock_irqsave(shost->host_lock, flags);
2321	sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2322	spin_unlock_irqrestore(shost->host_lock, flags);
2323	return 0;
2324}
2325
2326/**
2327 * ibmvfc_target_alloc - Setup the target's task set value
2328 * @starget:	struct scsi_target
2329 *
2330 * Set the target's task set value so that error handling works as
2331 * expected.
2332 *
2333 * Returns:
2334 *	0 on success / -ENXIO if device does not exist
2335 **/
2336static int ibmvfc_target_alloc(struct scsi_target *starget)
2337{
2338	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2339	struct ibmvfc_host *vhost = shost_priv(shost);
2340	unsigned long flags = 0;
2341
2342	spin_lock_irqsave(shost->host_lock, flags);
2343	starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2344	spin_unlock_irqrestore(shost->host_lock, flags);
2345	return 0;
2346}
2347
2348/**
2349 * ibmvfc_slave_configure - Configure the device
2350 * @sdev:	struct scsi_device device to configure
2351 *
2352 * Enable allow_restart for a device if it is a disk. Adjust the
2353 * queue_depth here also.
2354 *
2355 * Returns:
2356 *	0
2357 **/
2358static int ibmvfc_slave_configure(struct scsi_device *sdev)
2359{
2360	struct Scsi_Host *shost = sdev->host;
2361	struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
2362	unsigned long flags = 0;
2363
2364	spin_lock_irqsave(shost->host_lock, flags);
2365	if (sdev->type == TYPE_DISK)
2366		sdev->allow_restart = 1;
2367
2368	if (sdev->tagged_supported) {
2369		scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2370		scsi_activate_tcq(sdev, sdev->queue_depth);
2371	} else
2372		scsi_deactivate_tcq(sdev, sdev->queue_depth);
2373
2374	rport->dev_loss_tmo = dev_loss_tmo;
2375	spin_unlock_irqrestore(shost->host_lock, flags);
2376	return 0;
2377}
2378
2379/**
2380 * ibmvfc_change_queue_depth - Change the device's queue depth
2381 * @sdev:	scsi device struct
2382 * @qdepth:	depth to set
2383 *
2384 * Return value:
2385 * 	actual depth set
2386 **/
2387static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2388{
2389	if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2390		qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2391
2392	scsi_adjust_queue_depth(sdev, 0, qdepth);
2393	return sdev->queue_depth;
2394}
2395
2396/**
2397 * ibmvfc_change_queue_type - Change the device's queue type
2398 * @sdev:		scsi device struct
2399 * @tag_type:	type of tags to use
2400 *
2401 * Return value:
2402 * 	actual queue type set
2403 **/
2404static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
2405{
2406	if (sdev->tagged_supported) {
2407		scsi_set_tag_type(sdev, tag_type);
2408
2409		if (tag_type)
2410			scsi_activate_tcq(sdev, sdev->queue_depth);
2411		else
2412			scsi_deactivate_tcq(sdev, sdev->queue_depth);
2413	} else
2414		tag_type = 0;
2415
2416	return tag_type;
2417}
2418
2419static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2420						 struct device_attribute *attr, char *buf)
2421{
2422	struct Scsi_Host *shost = class_to_shost(dev);
2423	struct ibmvfc_host *vhost = shost_priv(shost);
2424
2425	return snprintf(buf, PAGE_SIZE, "%s\n",
2426			vhost->login_buf->resp.partition_name);
2427}
2428
2429static struct device_attribute ibmvfc_host_partition_name = {
2430	.attr = {
2431		.name = "partition_name",
2432		.mode = S_IRUGO,
2433	},
2434	.show = ibmvfc_show_host_partition_name,
2435};
2436
2437static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2438					    struct device_attribute *attr, char *buf)
2439{
2440	struct Scsi_Host *shost = class_to_shost(dev);
2441	struct ibmvfc_host *vhost = shost_priv(shost);
2442
2443	return snprintf(buf, PAGE_SIZE, "%s\n",
2444			vhost->login_buf->resp.device_name);
2445}
2446
2447static struct device_attribute ibmvfc_host_device_name = {
2448	.attr = {
2449		.name = "device_name",
2450		.mode = S_IRUGO,
2451	},
2452	.show = ibmvfc_show_host_device_name,
2453};
2454
2455static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2456					 struct device_attribute *attr, char *buf)
2457{
2458	struct Scsi_Host *shost = class_to_shost(dev);
2459	struct ibmvfc_host *vhost = shost_priv(shost);
2460
2461	return snprintf(buf, PAGE_SIZE, "%s\n",
2462			vhost->login_buf->resp.port_loc_code);
2463}
2464
2465static struct device_attribute ibmvfc_host_loc_code = {
2466	.attr = {
2467		.name = "port_loc_code",
2468		.mode = S_IRUGO,
2469	},
2470	.show = ibmvfc_show_host_loc_code,
2471};
2472
2473static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2474					 struct device_attribute *attr, char *buf)
2475{
2476	struct Scsi_Host *shost = class_to_shost(dev);
2477	struct ibmvfc_host *vhost = shost_priv(shost);
2478
2479	return snprintf(buf, PAGE_SIZE, "%s\n",
2480			vhost->login_buf->resp.drc_name);
2481}
2482
2483static struct device_attribute ibmvfc_host_drc_name = {
2484	.attr = {
2485		.name = "drc_name",
2486		.mode = S_IRUGO,
2487	},
2488	.show = ibmvfc_show_host_drc_name,
2489};
2490
2491static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2492					     struct device_attribute *attr, char *buf)
2493{
2494	struct Scsi_Host *shost = class_to_shost(dev);
2495	struct ibmvfc_host *vhost = shost_priv(shost);
2496	return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2497}
2498
2499static struct device_attribute ibmvfc_host_npiv_version = {
2500	.attr = {
2501		.name = "npiv_version",
2502		.mode = S_IRUGO,
2503	},
2504	.show = ibmvfc_show_host_npiv_version,
2505};
2506
2507/**
2508 * ibmvfc_show_log_level - Show the adapter's error logging level
2509 * @dev:	class device struct
2510 * @buf:	buffer
2511 *
2512 * Return value:
2513 * 	number of bytes printed to buffer
2514 **/
2515static ssize_t ibmvfc_show_log_level(struct device *dev,
2516				     struct device_attribute *attr, char *buf)
2517{
2518	struct Scsi_Host *shost = class_to_shost(dev);
2519	struct ibmvfc_host *vhost = shost_priv(shost);
2520	unsigned long flags = 0;
2521	int len;
2522
2523	spin_lock_irqsave(shost->host_lock, flags);
2524	len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2525	spin_unlock_irqrestore(shost->host_lock, flags);
2526	return len;
2527}
2528
2529/**
2530 * ibmvfc_store_log_level - Change the adapter's error logging level
2531 * @dev:	class device struct
2532 * @buf:	buffer
2533 *
2534 * Return value:
2535 * 	number of bytes printed to buffer
2536 **/
2537static ssize_t ibmvfc_store_log_level(struct device *dev,
2538				      struct device_attribute *attr,
2539				      const char *buf, size_t count)
2540{
2541	struct Scsi_Host *shost = class_to_shost(dev);
2542	struct ibmvfc_host *vhost = shost_priv(shost);
2543	unsigned long flags = 0;
2544
2545	spin_lock_irqsave(shost->host_lock, flags);
2546	vhost->log_level = simple_strtoul(buf, NULL, 10);
2547	spin_unlock_irqrestore(shost->host_lock, flags);
2548	return strlen(buf);
2549}
2550
2551static struct device_attribute ibmvfc_log_level_attr = {
2552	.attr = {
2553		.name =		"log_level",
2554		.mode =		S_IRUGO | S_IWUSR,
2555	},
2556	.show = ibmvfc_show_log_level,
2557	.store = ibmvfc_store_log_level
2558};
2559
2560#ifdef CONFIG_SCSI_IBMVFC_TRACE
2561/**
2562 * ibmvfc_read_trace - Dump the adapter trace
2563 * @kobj:		kobject struct
2564 * @bin_attr:	bin_attribute struct
2565 * @buf:		buffer
2566 * @off:		offset
2567 * @count:		buffer size
2568 *
2569 * Return value:
2570 *	number of bytes printed to buffer
2571 **/
2572static ssize_t ibmvfc_read_trace(struct kobject *kobj,
2573				 struct bin_attribute *bin_attr,
2574				 char *buf, loff_t off, size_t count)
2575{
2576	struct device *dev = container_of(kobj, struct device, kobj);
2577	struct Scsi_Host *shost = class_to_shost(dev);
2578	struct ibmvfc_host *vhost = shost_priv(shost);
2579	unsigned long flags = 0;
2580	int size = IBMVFC_TRACE_SIZE;
2581	char *src = (char *)vhost->trace;
2582
2583	if (off > size)
2584		return 0;
2585	if (off + count > size) {
2586		size -= off;
2587		count = size;
2588	}
2589
2590	spin_lock_irqsave(shost->host_lock, flags);
2591	memcpy(buf, &src[off], count);
2592	spin_unlock_irqrestore(shost->host_lock, flags);
2593	return count;
2594}
2595
2596static struct bin_attribute ibmvfc_trace_attr = {
2597	.attr =	{
2598		.name = "trace",
2599		.mode = S_IRUGO,
2600	},
2601	.size = 0,
2602	.read = ibmvfc_read_trace,
2603};
2604#endif
2605
2606static struct device_attribute *ibmvfc_attrs[] = {
2607	&ibmvfc_host_partition_name,
2608	&ibmvfc_host_device_name,
2609	&ibmvfc_host_loc_code,
2610	&ibmvfc_host_drc_name,
2611	&ibmvfc_host_npiv_version,
2612	&ibmvfc_log_level_attr,
2613	NULL
2614};
2615
2616static struct scsi_host_template driver_template = {
2617	.module = THIS_MODULE,
2618	.name = "IBM POWER Virtual FC Adapter",
2619	.proc_name = IBMVFC_NAME,
2620	.queuecommand = ibmvfc_queuecommand,
2621	.eh_abort_handler = ibmvfc_eh_abort_handler,
2622	.eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
2623	.eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
2624	.eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
2625	.slave_alloc = ibmvfc_slave_alloc,
2626	.slave_configure = ibmvfc_slave_configure,
2627	.target_alloc = ibmvfc_target_alloc,
2628	.scan_finished = ibmvfc_scan_finished,
2629	.change_queue_depth = ibmvfc_change_queue_depth,
2630	.change_queue_type = ibmvfc_change_queue_type,
2631	.cmd_per_lun = 16,
2632	.can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
2633	.this_id = -1,
2634	.sg_tablesize = SG_ALL,
2635	.max_sectors = IBMVFC_MAX_SECTORS,
2636	.use_clustering = ENABLE_CLUSTERING,
2637	.shost_attrs = ibmvfc_attrs,
2638};
2639
2640/**
2641 * ibmvfc_next_async_crq - Returns the next entry in async queue
2642 * @vhost:	ibmvfc host struct
2643 *
2644 * Returns:
2645 *	Pointer to next entry in queue / NULL if empty
2646 **/
2647static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
2648{
2649	struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
2650	struct ibmvfc_async_crq *crq;
2651
2652	crq = &async_crq->msgs[async_crq->cur];
2653	if (crq->valid & 0x80) {
2654		if (++async_crq->cur == async_crq->size)
2655			async_crq->cur = 0;
2656	} else
2657		crq = NULL;
2658
2659	return crq;
2660}
2661
2662/**
2663 * ibmvfc_next_crq - Returns the next entry in message queue
2664 * @vhost:	ibmvfc host struct
2665 *
2666 * Returns:
2667 *	Pointer to next entry in queue / NULL if empty
2668 **/
2669static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
2670{
2671	struct ibmvfc_crq_queue *queue = &vhost->crq;
2672	struct ibmvfc_crq *crq;
2673
2674	crq = &queue->msgs[queue->cur];
2675	if (crq->valid & 0x80) {
2676		if (++queue->cur == queue->size)
2677			queue->cur = 0;
2678	} else
2679		crq = NULL;
2680
2681	return crq;
2682}
2683
2684/**
2685 * ibmvfc_interrupt - Interrupt handler
2686 * @irq:		number of irq to handle, not used
2687 * @dev_instance: ibmvfc_host that received interrupt
2688 *
2689 * Returns:
2690 *	IRQ_HANDLED
2691 **/
2692static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
2693{
2694	struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
2695	struct vio_dev *vdev = to_vio_dev(vhost->dev);
2696	struct ibmvfc_crq *crq;
2697	struct ibmvfc_async_crq *async;
2698	unsigned long flags;
2699	int done = 0;
2700
2701	spin_lock_irqsave(vhost->host->host_lock, flags);
2702	vio_disable_interrupts(to_vio_dev(vhost->dev));
2703	while (!done) {
2704		/* Pull all the valid messages off the CRQ */
2705		while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2706			ibmvfc_handle_crq(crq, vhost);
2707			crq->valid = 0;
2708		}
2709
2710		/* Pull all the valid messages off the async CRQ */
2711		while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2712			ibmvfc_handle_async(async, vhost);
2713			async->valid = 0;
2714		}
2715
2716		vio_enable_interrupts(vdev);
2717		if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2718			vio_disable_interrupts(vdev);
2719			ibmvfc_handle_crq(crq, vhost);
2720			crq->valid = 0;
2721		} else if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2722			vio_disable_interrupts(vdev);
2723			ibmvfc_handle_async(async, vhost);
2724			crq->valid = 0;
2725		} else
2726			done = 1;
2727	}
2728
2729	spin_unlock_irqrestore(vhost->host->host_lock, flags);
2730	return IRQ_HANDLED;
2731}
2732
2733/**
2734 * ibmvfc_init_tgt - Set the next init job step for the target
2735 * @tgt:		ibmvfc target struct
2736 * @job_step:	job step to perform
2737 *
2738 **/
2739static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
2740			    void (*job_step) (struct ibmvfc_target *))
2741{
2742	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
2743	tgt->job_step = job_step;
2744	wake_up(&tgt->vhost->work_wait_q);
2745}
2746
2747/**
2748 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
2749 * @tgt:		ibmvfc target struct
2750 * @job_step:	initialization job step
2751 *
2752 **/
2753static void ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
2754				  void (*job_step) (struct ibmvfc_target *))
2755{
2756	if (++tgt->init_retries > IBMVFC_MAX_INIT_RETRIES) {
2757		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2758		wake_up(&tgt->vhost->work_wait_q);
2759	} else
2760		ibmvfc_init_tgt(tgt, job_step);
2761}
2762
2763/**
2764 * ibmvfc_tgt_prli_done - Completion handler for Process Login
2765 * @evt:	ibmvfc event struct
2766 *
2767 **/
2768static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
2769{
2770	struct ibmvfc_target *tgt = evt->tgt;
2771	struct ibmvfc_host *vhost = evt->vhost;
2772	struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
2773	u32 status = rsp->common.status;
2774
2775	vhost->discovery_threads--;
2776	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2777	switch (status) {
2778	case IBMVFC_MAD_SUCCESS:
2779		tgt_dbg(tgt, "Process Login succeeded\n");
2780		tgt->need_login = 0;
2781		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_ADD_RPORT);
2782		break;
2783	case IBMVFC_MAD_DRIVER_FAILED:
2784		break;
2785	case IBMVFC_MAD_CRQ_ERROR:
2786		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2787		break;
2788	case IBMVFC_MAD_FAILED:
2789	default:
2790		tgt_err(tgt, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
2791			ibmvfc_get_cmd_error(rsp->status, rsp->error),
2792			rsp->status, rsp->error, status);
2793		if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2794			ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2795		else
2796			ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2797		break;
2798	};
2799
2800	kref_put(&tgt->kref, ibmvfc_release_tgt);
2801	ibmvfc_free_event(evt);
2802	wake_up(&vhost->work_wait_q);
2803}
2804
2805/**
2806 * ibmvfc_tgt_send_prli - Send a process login
2807 * @tgt:	ibmvfc target struct
2808 *
2809 **/
2810static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
2811{
2812	struct ibmvfc_process_login *prli;
2813	struct ibmvfc_host *vhost = tgt->vhost;
2814	struct ibmvfc_event *evt;
2815
2816	if (vhost->discovery_threads >= disc_threads)
2817		return;
2818
2819	kref_get(&tgt->kref);
2820	evt = ibmvfc_get_event(vhost);
2821	vhost->discovery_threads++;
2822	ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
2823	evt->tgt = tgt;
2824	prli = &evt->iu.prli;
2825	memset(prli, 0, sizeof(*prli));
2826	prli->common.version = 1;
2827	prli->common.opcode = IBMVFC_PROCESS_LOGIN;
2828	prli->common.length = sizeof(*prli);
2829	prli->scsi_id = tgt->scsi_id;
2830
2831	prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
2832	prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
2833	prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
2834
2835	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2836	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2837		vhost->discovery_threads--;
2838		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2839		kref_put(&tgt->kref, ibmvfc_release_tgt);
2840	} else
2841		tgt_dbg(tgt, "Sent process login\n");
2842}
2843
2844/**
2845 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
2846 * @evt:	ibmvfc event struct
2847 *
2848 **/
2849static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
2850{
2851	struct ibmvfc_target *tgt = evt->tgt;
2852	struct ibmvfc_host *vhost = evt->vhost;
2853	struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
2854	u32 status = rsp->common.status;
2855
2856	vhost->discovery_threads--;
2857	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2858	switch (status) {
2859	case IBMVFC_MAD_SUCCESS:
2860		tgt_dbg(tgt, "Port Login succeeded\n");
2861		if (tgt->ids.port_name &&
2862		    tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
2863			vhost->reinit = 1;
2864			tgt_dbg(tgt, "Port re-init required\n");
2865			break;
2866		}
2867		tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
2868		tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
2869		tgt->ids.port_id = tgt->scsi_id;
2870		tgt->ids.roles = FC_PORT_ROLE_FCP_TARGET;
2871		memcpy(&tgt->service_parms, &rsp->service_parms,
2872		       sizeof(tgt->service_parms));
2873		memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
2874		       sizeof(tgt->service_parms_change));
2875		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
2876		break;
2877	case IBMVFC_MAD_DRIVER_FAILED:
2878		break;
2879	case IBMVFC_MAD_CRQ_ERROR:
2880		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2881		break;
2882	case IBMVFC_MAD_FAILED:
2883	default:
2884		tgt_err(tgt, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
2885			ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
2886			ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
2887			ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
2888
2889		if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2890			ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2891		else
2892			ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2893		break;
2894	};
2895
2896	kref_put(&tgt->kref, ibmvfc_release_tgt);
2897	ibmvfc_free_event(evt);
2898	wake_up(&vhost->work_wait_q);
2899}
2900
2901/**
2902 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
2903 * @tgt:	ibmvfc target struct
2904 *
2905 **/
2906static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
2907{
2908	struct ibmvfc_port_login *plogi;
2909	struct ibmvfc_host *vhost = tgt->vhost;
2910	struct ibmvfc_event *evt;
2911
2912	if (vhost->discovery_threads >= disc_threads)
2913		return;
2914
2915	kref_get(&tgt->kref);
2916	evt = ibmvfc_get_event(vhost);
2917	vhost->discovery_threads++;
2918	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2919	ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
2920	evt->tgt = tgt;
2921	plogi = &evt->iu.plogi;
2922	memset(plogi, 0, sizeof(*plogi));
2923	plogi->common.version = 1;
2924	plogi->common.opcode = IBMVFC_PORT_LOGIN;
2925	plogi->common.length = sizeof(*plogi);
2926	plogi->scsi_id = tgt->scsi_id;
2927
2928	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2929		vhost->discovery_threads--;
2930		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2931		kref_put(&tgt->kref, ibmvfc_release_tgt);
2932	} else
2933		tgt_dbg(tgt, "Sent port login\n");
2934}
2935
2936/**
2937 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
2938 * @evt:	ibmvfc event struct
2939 *
2940 **/
2941static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
2942{
2943	struct ibmvfc_target *tgt = evt->tgt;
2944	struct ibmvfc_host *vhost = evt->vhost;
2945	struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
2946	u32 status = rsp->common.status;
2947
2948	vhost->discovery_threads--;
2949	ibmvfc_free_event(evt);
2950	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2951
2952	switch (status) {
2953	case IBMVFC_MAD_SUCCESS:
2954		tgt_dbg(tgt, "Implicit Logout succeeded\n");
2955		break;
2956	case IBMVFC_MAD_DRIVER_FAILED:
2957		kref_put(&tgt->kref, ibmvfc_release_tgt);
2958		wake_up(&vhost->work_wait_q);
2959		return;
2960	case IBMVFC_MAD_FAILED:
2961	default:
2962		tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
2963		break;
2964	};
2965
2966	if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
2967		ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
2968	else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
2969		 tgt->scsi_id != tgt->new_scsi_id)
2970		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2971	kref_put(&tgt->kref, ibmvfc_release_tgt);
2972	wake_up(&vhost->work_wait_q);
2973}
2974
2975/**
2976 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
2977 * @tgt:		ibmvfc target struct
2978 *
2979 **/
2980static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
2981{
2982	struct ibmvfc_implicit_logout *mad;
2983	struct ibmvfc_host *vhost = tgt->vhost;
2984	struct ibmvfc_event *evt;
2985
2986	if (vhost->discovery_threads >= disc_threads)
2987		return;
2988
2989	kref_get(&tgt->kref);
2990	evt = ibmvfc_get_event(vhost);
2991	vhost->discovery_threads++;
2992	ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
2993	evt->tgt = tgt;
2994	mad = &evt->iu.implicit_logout;
2995	memset(mad, 0, sizeof(*mad));
2996	mad->common.version = 1;
2997	mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
2998	mad->common.length = sizeof(*mad);
2999	mad->old_scsi_id = tgt->scsi_id;
3000
3001	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3002	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3003		vhost->discovery_threads--;
3004		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3005		kref_put(&tgt->kref, ibmvfc_release_tgt);
3006	} else
3007		tgt_dbg(tgt, "Sent Implicit Logout\n");
3008}
3009
3010/**
3011 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3012 * @mad:	ibmvfc passthru mad struct
3013 * @tgt:	ibmvfc target struct
3014 *
3015 * Returns:
3016 *	1 if PLOGI needed / 0 if PLOGI not needed
3017 **/
3018static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3019				    struct ibmvfc_target *tgt)
3020{
3021	if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
3022		   sizeof(tgt->ids.port_name)))
3023		return 1;
3024	if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
3025		   sizeof(tgt->ids.node_name)))
3026		return 1;
3027	if (mad->fc_iu.response[6] != tgt->scsi_id)
3028		return 1;
3029	return 0;
3030}
3031
3032/**
3033 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3034 * @evt:	ibmvfc event struct
3035 *
3036 **/
3037static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3038{
3039	struct ibmvfc_target *tgt = evt->tgt;
3040	struct ibmvfc_host *vhost = evt->vhost;
3041	struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3042	u32 status = mad->common.status;
3043	u8 fc_reason, fc_explain;
3044
3045	vhost->discovery_threads--;
3046	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3047
3048	switch (status) {
3049	case IBMVFC_MAD_SUCCESS:
3050		tgt_dbg(tgt, "ADISC succeeded\n");
3051		if (ibmvfc_adisc_needs_plogi(mad, tgt))
3052			tgt->need_login = 1;
3053		break;
3054	case IBMVFC_MAD_DRIVER_FAILED:
3055		break;
3056	case IBMVFC_MAD_FAILED:
3057	default:
3058		tgt->need_login = 1;
3059		fc_reason = (mad->fc_iu.response[1] & 0x00ff0000) >> 16;
3060		fc_explain = (mad->fc_iu.response[1] & 0x0000ff00) >> 8;
3061		tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3062			 ibmvfc_get_cmd_error(mad->iu.status, mad->iu.error),
3063			 mad->iu.status, mad->iu.error,
3064			 ibmvfc_get_fc_type(fc_reason), fc_reason,
3065			 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3066		break;
3067	};
3068
3069	kref_put(&tgt->kref, ibmvfc_release_tgt);
3070	ibmvfc_free_event(evt);
3071	wake_up(&vhost->work_wait_q);
3072}
3073
3074/**
3075 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3076 * @evt:		ibmvfc event struct
3077 *
3078 **/
3079static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3080{
3081	struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3082
3083	memset(mad, 0, sizeof(*mad));
3084	mad->common.version = 1;
3085	mad->common.opcode = IBMVFC_PASSTHRU;
3086	mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
3087	mad->cmd_ioba.va = (u64)evt->crq.ioba +
3088		offsetof(struct ibmvfc_passthru_mad, iu);
3089	mad->cmd_ioba.len = sizeof(mad->iu);
3090	mad->iu.cmd_len = sizeof(mad->fc_iu.payload);
3091	mad->iu.rsp_len = sizeof(mad->fc_iu.response);
3092	mad->iu.cmd.va = (u64)evt->crq.ioba +
3093		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3094		offsetof(struct ibmvfc_passthru_fc_iu, payload);
3095	mad->iu.cmd.len = sizeof(mad->fc_iu.payload);
3096	mad->iu.rsp.va = (u64)evt->crq.ioba +
3097		offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3098		offsetof(struct ibmvfc_passthru_fc_iu, response);
3099	mad->iu.rsp.len = sizeof(mad->fc_iu.response);
3100}
3101
3102/**
3103 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3104 * @tgt:		ibmvfc target struct
3105 *
3106 **/
3107static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3108{
3109	struct ibmvfc_passthru_mad *mad;
3110	struct ibmvfc_host *vhost = tgt->vhost;
3111	struct ibmvfc_event *evt;
3112
3113	if (vhost->discovery_threads >= disc_threads)
3114		return;
3115
3116	kref_get(&tgt->kref);
3117	evt = ibmvfc_get_event(vhost);
3118	vhost->discovery_threads++;
3119	ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3120	evt->tgt = tgt;
3121
3122	ibmvfc_init_passthru(evt);
3123	mad = &evt->iu.passthru;
3124	mad->iu.flags = IBMVFC_FC_ELS;
3125	mad->iu.scsi_id = tgt->scsi_id;
3126
3127	mad->fc_iu.payload[0] = IBMVFC_ADISC;
3128	memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3129	       sizeof(vhost->login_buf->resp.port_name));
3130	memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3131	       sizeof(vhost->login_buf->resp.node_name));
3132	mad->fc_iu.payload[6] = vhost->login_buf->resp.scsi_id & 0x00ffffff;
3133
3134	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3135	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3136		vhost->discovery_threads--;
3137		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3138		kref_put(&tgt->kref, ibmvfc_release_tgt);
3139	} else
3140		tgt_dbg(tgt, "Sent ADISC\n");
3141}
3142
3143/**
3144 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3145 * @evt:	ibmvfc event struct
3146 *
3147 **/
3148static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3149{
3150	struct ibmvfc_target *tgt = evt->tgt;
3151	struct ibmvfc_host *vhost = evt->vhost;
3152	struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3153	u32 status = rsp->common.status;
3154
3155	vhost->discovery_threads--;
3156	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3157	switch (status) {
3158	case IBMVFC_MAD_SUCCESS:
3159		tgt_dbg(tgt, "Query Target succeeded\n");
3160		tgt->new_scsi_id = rsp->scsi_id;
3161		if (rsp->scsi_id != tgt->scsi_id)
3162			ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3163		else
3164			ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
3165		break;
3166	case IBMVFC_MAD_DRIVER_FAILED:
3167		break;
3168	case IBMVFC_MAD_CRQ_ERROR:
3169		ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3170		break;
3171	case IBMVFC_MAD_FAILED:
3172	default:
3173		tgt_err(tgt, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3174			ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
3175			ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
3176			ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
3177
3178		if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3179		    rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3180		    rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
3181			ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3182		else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3183			ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3184		else
3185			ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3186		break;
3187	};
3188
3189	kref_put(&tgt->kref, ibmvfc_release_tgt);
3190	ibmvfc_free_event(evt);
3191	wake_up(&vhost->work_wait_q);
3192}
3193
3194/**
3195 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3196 * @tgt:	ibmvfc target struct
3197 *
3198 **/
3199static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3200{
3201	struct ibmvfc_query_tgt *query_tgt;
3202	struct ibmvfc_host *vhost = tgt->vhost;
3203	struct ibmvfc_event *evt;
3204
3205	if (vhost->discovery_threads >= disc_threads)
3206		return;
3207
3208	kref_get(&tgt->kref);
3209	evt = ibmvfc_get_event(vhost);
3210	vhost->discovery_threads++;
3211	evt->tgt = tgt;
3212	ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3213	query_tgt = &evt->iu.query_tgt;
3214	memset(query_tgt, 0, sizeof(*query_tgt));
3215	query_tgt->common.version = 1;
3216	query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
3217	query_tgt->common.length = sizeof(*query_tgt);
3218	query_tgt->wwpn = tgt->ids.port_name;
3219
3220	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3221	if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3222		vhost->discovery_threads--;
3223		ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3224		kref_put(&tgt->kref, ibmvfc_release_tgt);
3225	} else
3226		tgt_dbg(tgt, "Sent Query Target\n");
3227}
3228
3229/**
3230 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3231 * @vhost:		ibmvfc host struct
3232 * @scsi_id:	SCSI ID to allocate target for
3233 *
3234 * Returns:
3235 *	0 on success / other on failure
3236 **/
3237static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3238{
3239	struct ibmvfc_target *tgt;
3240	unsigned long flags;
3241
3242	spin_lock_irqsave(vhost->host->host_lock, flags);
3243	list_for_each_entry(tgt, &vhost->targets, queue) {
3244		if (tgt->scsi_id == scsi_id) {
3245			if (tgt->need_login)
3246				ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3247			goto unlock_out;
3248		}
3249	}
3250	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3251
3252	tgt = mempool_alloc(vhost->tgt_pool, GFP_KERNEL);
3253	if (!tgt) {
3254		dev_err(vhost->dev, "Target allocation failure for scsi id %08lx\n",
3255			scsi_id);
3256		return -ENOMEM;
3257	}
3258
3259	tgt->scsi_id = scsi_id;
3260	tgt->new_scsi_id = scsi_id;
3261	tgt->vhost = vhost;
3262	tgt->need_login = 1;
3263	kref_init(&tgt->kref);
3264	ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3265	spin_lock_irqsave(vhost->host->host_lock, flags);
3266	list_add_tail(&tgt->queue, &vhost->targets);
3267
3268unlock_out:
3269	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3270	return 0;
3271}
3272
3273/**
3274 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3275 * @vhost:		ibmvfc host struct
3276 *
3277 * Returns:
3278 *	0 on success / other on failure
3279 **/
3280static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3281{
3282	int i, rc;
3283
3284	for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3285		rc = ibmvfc_alloc_target(vhost,
3286					 vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
3287
3288	return rc;
3289}
3290
3291/**
3292 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3293 * @evt:	ibmvfc event struct
3294 *
3295 **/
3296static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3297{
3298	struct ibmvfc_host *vhost = evt->vhost;
3299	struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3300	u32 mad_status = rsp->common.status;
3301
3302	switch (mad_status) {
3303	case IBMVFC_MAD_SUCCESS:
3304		ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3305		vhost->num_targets = rsp->num_written;
3306		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3307		break;
3308	case IBMVFC_MAD_FAILED:
3309		dev_err(vhost->dev, "Discover Targets failed: %s (%x:%x)\n",
3310			ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3311		ibmvfc_retry_host_init(vhost);
3312		break;
3313	case IBMVFC_MAD_DRIVER_FAILED:
3314		break;
3315	default:
3316		dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3317		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3318		break;
3319	}
3320
3321	ibmvfc_free_event(evt);
3322	wake_up(&vhost->work_wait_q);
3323}
3324
3325/**
3326 * ibmvfc_discover_targets - Send Discover Targets MAD
3327 * @vhost:	ibmvfc host struct
3328 *
3329 **/
3330static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3331{
3332	struct ibmvfc_discover_targets *mad;
3333	struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3334
3335	ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3336	mad = &evt->iu.discover_targets;
3337	memset(mad, 0, sizeof(*mad));
3338	mad->common.version = 1;
3339	mad->common.opcode = IBMVFC_DISC_TARGETS;
3340	mad->common.length = sizeof(*mad);
3341	mad->bufflen = vhost->disc_buf_sz;
3342	mad->buffer.va = vhost->disc_buf_dma;
3343	mad->buffer.len = vhost->disc_buf_sz;
3344	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3345
3346	if (!ibmvfc_send_event(evt, vhost, default_timeout))
3347		ibmvfc_dbg(vhost, "Sent discover targets\n");
3348	else
3349		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3350}
3351
3352/**
3353 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3354 * @evt:	ibmvfc event struct
3355 *
3356 **/
3357static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
3358{
3359	struct ibmvfc_host *vhost = evt->vhost;
3360	u32 mad_status = evt->xfer_iu->npiv_login.common.status;
3361	struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
3362	unsigned int npiv_max_sectors;
3363
3364	switch (mad_status) {
3365	case IBMVFC_MAD_SUCCESS:
3366		ibmvfc_free_event(evt);
3367		break;
3368	case IBMVFC_MAD_FAILED:
3369		dev_err(vhost->dev, "NPIV Login failed: %s (%x:%x)\n",
3370			ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3371		if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3372			ibmvfc_retry_host_init(vhost);
3373		else
3374			ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3375		ibmvfc_free_event(evt);
3376		return;
3377	case IBMVFC_MAD_CRQ_ERROR:
3378		ibmvfc_retry_host_init(vhost);
3379	case IBMVFC_MAD_DRIVER_FAILED:
3380		ibmvfc_free_event(evt);
3381		return;
3382	default:
3383		dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
3384		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3385		ibmvfc_free_event(evt);
3386		return;
3387	}
3388
3389	vhost->client_migrated = 0;
3390
3391	if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
3392		dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
3393			rsp->flags);
3394		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3395		wake_up(&vhost->work_wait_q);
3396		return;
3397	}
3398
3399	if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
3400		dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
3401			rsp->max_cmds);
3402		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3403		wake_up(&vhost->work_wait_q);
3404		return;
3405	}
3406
3407	npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
3408	dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
3409		 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
3410		 rsp->drc_name, npiv_max_sectors);
3411
3412	fc_host_fabric_name(vhost->host) = rsp->node_name;
3413	fc_host_node_name(vhost->host) = rsp->node_name;
3414	fc_host_port_name(vhost->host) = rsp->port_name;
3415	fc_host_port_id(vhost->host) = rsp->scsi_id;
3416	fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
3417	fc_host_supported_classes(vhost->host) = 0;
3418	if (rsp->service_parms.class1_parms[0] & 0x80000000)
3419		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
3420	if (rsp->service_parms.class2_parms[0] & 0x80000000)
3421		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
3422	if (rsp->service_parms.class3_parms[0] & 0x80000000)
3423		fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
3424	fc_host_maxframe_size(vhost->host) =
3425		rsp->service_parms.common.bb_rcv_sz & 0x0fff;
3426
3427	vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
3428	vhost->host->max_sectors = npiv_max_sectors;
3429	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3430	wake_up(&vhost->work_wait_q);
3431}
3432
3433/**
3434 * ibmvfc_npiv_login - Sends NPIV login
3435 * @vhost:	ibmvfc host struct
3436 *
3437 **/
3438static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
3439{
3440	struct ibmvfc_npiv_login_mad *mad;
3441	struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3442
3443	ibmvfc_gather_partition_info(vhost);
3444	ibmvfc_set_login_info(vhost);
3445	ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
3446
3447	memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
3448	mad = &evt->iu.npiv_login;
3449	memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
3450	mad->common.version = 1;
3451	mad->common.opcode = IBMVFC_NPIV_LOGIN;
3452	mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
3453	mad->buffer.va = vhost->login_buf_dma;
3454	mad->buffer.len = sizeof(*vhost->login_buf);
3455
3456	ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3457
3458	if (!ibmvfc_send_event(evt, vhost, default_timeout))
3459		ibmvfc_dbg(vhost, "Sent NPIV login\n");
3460	else
3461		ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3462};
3463
3464/**
3465 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
3466 * @vhost:		ibmvfc host struct
3467 *
3468 * Returns:
3469 *	1 if work to do / 0 if not
3470 **/
3471static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
3472{
3473	struct ibmvfc_target *tgt;
3474
3475	list_for_each_entry(tgt, &vhost->targets, queue) {
3476		if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
3477		    tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3478			return 1;
3479	}
3480
3481	return 0;
3482}
3483
3484/**
3485 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
3486 * @vhost:		ibmvfc host struct
3487 *
3488 * Returns:
3489 *	1 if work to do / 0 if not
3490 **/
3491static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3492{
3493	struct ibmvfc_target *tgt;
3494
3495	if (kthread_should_stop())
3496		return 1;
3497	switch (vhost->action) {
3498	case IBMVFC_HOST_ACTION_NONE:
3499	case IBMVFC_HOST_ACTION_INIT_WAIT:
3500		return 0;
3501	case IBMVFC_HOST_ACTION_TGT_INIT:
3502	case IBMVFC_HOST_ACTION_QUERY_TGTS:
3503		if (vhost->discovery_threads == disc_threads)
3504			return 0;
3505		list_for_each_entry(tgt, &vhost->targets, queue)
3506			if (tgt->action == IBMVFC_TGT_ACTION_INIT)
3507				return 1;
3508		list_for_each_entry(tgt, &vhost->targets, queue)
3509			if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3510				return 0;
3511		return 1;
3512	case IBMVFC_HOST_ACTION_INIT:
3513	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3514	case IBMVFC_HOST_ACTION_TGT_ADD:
3515	case IBMVFC_HOST_ACTION_TGT_DEL:
3516	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
3517	case IBMVFC_HOST_ACTION_QUERY:
3518	default:
3519		break;
3520	};
3521
3522	return 1;
3523}
3524
3525/**
3526 * ibmvfc_work_to_do - Is there task level work to do?
3527 * @vhost:		ibmvfc host struct
3528 *
3529 * Returns:
3530 *	1 if work to do / 0 if not
3531 **/
3532static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3533{
3534	unsigned long flags;
3535	int rc;
3536
3537	spin_lock_irqsave(vhost->host->host_lock, flags);
3538	rc = __ibmvfc_work_to_do(vhost);
3539	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3540	return rc;
3541}
3542
3543/**
3544 * ibmvfc_log_ae - Log async events if necessary
3545 * @vhost:		ibmvfc host struct
3546 * @events:		events to log
3547 *
3548 **/
3549static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
3550{
3551	if (events & IBMVFC_AE_RSCN)
3552		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
3553	if ((events & IBMVFC_AE_LINKDOWN) &&
3554	    vhost->state >= IBMVFC_HALTED)
3555		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
3556	if ((events & IBMVFC_AE_LINKUP) &&
3557	    vhost->state == IBMVFC_INITIALIZING)
3558		fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
3559}
3560
3561/**
3562 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
3563 * @tgt:		ibmvfc target struct
3564 *
3565 **/
3566static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
3567{
3568	struct ibmvfc_host *vhost = tgt->vhost;
3569	struct fc_rport *rport;
3570	unsigned long flags;
3571
3572	tgt_dbg(tgt, "Adding rport\n");
3573	rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
3574	spin_lock_irqsave(vhost->host->host_lock, flags);
3575	tgt->rport = rport;
3576	ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3577	if (rport) {
3578		tgt_dbg(tgt, "rport add succeeded\n");
3579		rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
3580		rport->supported_classes = 0;
3581		tgt->target_id = rport->scsi_target_id;
3582		if (tgt->service_parms.class1_parms[0] & 0x80000000)
3583			rport->supported_classes |= FC_COS_CLASS1;
3584		if (tgt->service_parms.class2_parms[0] & 0x80000000)
3585			rport->supported_classes |= FC_COS_CLASS2;
3586		if (tgt->service_parms.class3_parms[0] & 0x80000000)
3587			rport->supported_classes |= FC_COS_CLASS3;
3588	} else
3589		tgt_dbg(tgt, "rport add failed\n");
3590	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3591}
3592
3593/**
3594 * ibmvfc_do_work - Do task level work
3595 * @vhost:		ibmvfc host struct
3596 *
3597 **/
3598static void ibmvfc_do_work(struct ibmvfc_host *vhost)
3599{
3600	struct ibmvfc_target *tgt;
3601	unsigned long flags;
3602	struct fc_rport *rport;
3603
3604	ibmvfc_log_ae(vhost, vhost->events_to_log);
3605	spin_lock_irqsave(vhost->host->host_lock, flags);
3606	vhost->events_to_log = 0;
3607	switch (vhost->action) {
3608	case IBMVFC_HOST_ACTION_NONE:
3609	case IBMVFC_HOST_ACTION_INIT_WAIT:
3610		break;
3611	case IBMVFC_HOST_ACTION_INIT:
3612		BUG_ON(vhost->state != IBMVFC_INITIALIZING);
3613		vhost->job_step(vhost);
3614		break;
3615	case IBMVFC_HOST_ACTION_QUERY:
3616		list_for_each_entry(tgt, &vhost->targets, queue)
3617			ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
3618		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
3619		break;
3620	case IBMVFC_HOST_ACTION_QUERY_TGTS:
3621		list_for_each_entry(tgt, &vhost->targets, queue) {
3622			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3623				tgt->job_step(tgt);
3624				break;
3625			}
3626		}
3627
3628		if (!ibmvfc_dev_init_to_do(vhost))
3629			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
3630		break;
3631	case IBMVFC_HOST_ACTION_TGT_DEL:
3632	case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
3633		list_for_each_entry(tgt, &vhost->targets, queue) {
3634			if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
3635				tgt_dbg(tgt, "Deleting rport\n");
3636				rport = tgt->rport;
3637				tgt->rport = NULL;
3638				list_del(&tgt->queue);
3639				spin_unlock_irqrestore(vhost->host->host_lock, flags);
3640				if (rport)
3641					fc_remote_port_delete(rport);
3642				kref_put(&tgt->kref, ibmvfc_release_tgt);
3643				return;
3644			}
3645		}
3646
3647		if (vhost->state == IBMVFC_INITIALIZING) {
3648			if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
3649				ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
3650				ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_ADD);
3651				vhost->init_retries = 0;
3652				spin_unlock_irqrestore(vhost->host->host_lock, flags);
3653				scsi_unblock_requests(vhost->host);
3654				return;
3655			} else {
3656				ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
3657				vhost->job_step = ibmvfc_discover_targets;
3658			}
3659		} else {
3660			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3661			spin_unlock_irqrestore(vhost->host->host_lock, flags);
3662			scsi_unblock_requests(vhost->host);
3663			wake_up(&vhost->init_wait_q);
3664			return;
3665		}
3666		break;
3667	case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3668		ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
3669		spin_unlock_irqrestore(vhost->host->host_lock, flags);
3670		ibmvfc_alloc_targets(vhost);
3671		spin_lock_irqsave(vhost->host->host_lock, flags);
3672		break;
3673	case IBMVFC_HOST_ACTION_TGT_INIT:
3674		list_for_each_entry(tgt, &vhost->targets, queue) {
3675			if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3676				tgt->job_step(tgt);
3677				break;
3678			}
3679		}
3680
3681		if (!ibmvfc_dev_init_to_do(vhost))
3682			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
3683		break;
3684	case IBMVFC_HOST_ACTION_TGT_ADD:
3685		list_for_each_entry(tgt, &vhost->targets, queue) {
3686			if (tgt->action == IBMVFC_TGT_ACTION_ADD_RPORT) {
3687				spin_unlock_irqrestore(vhost->host->host_lock, flags);
3688				ibmvfc_tgt_add_rport(tgt);
3689				return;
3690			}
3691		}
3692
3693		if (vhost->reinit && !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
3694			vhost->reinit = 0;
3695			scsi_block_requests(vhost->host);
3696			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3697		} else {
3698			ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3699			wake_up(&vhost->init_wait_q);
3700		}
3701		break;
3702	default:
3703		break;
3704	};
3705
3706	spin_unlock_irqrestore(vhost->host->host_lock, flags);
3707}
3708
3709/**
3710 * ibmvfc_work - Do task level work
3711 * @data:		ibmvfc host struct
3712 *
3713 * Returns:
3714 *	zero
3715 **/
3716static int ibmvfc_work(void *data)
3717{
3718	struct ibmvfc_host *vhost = data;
3719	int rc;
3720
3721	set_user_nice(current, -20);
3722
3723	while (1) {
3724		rc = wait_event_interruptible(vhost->work_wait_q,
3725					      ibmvfc_work_to_do(vhost));
3726
3727		BUG_ON(rc);
3728
3729		if (kthread_should_stop())
3730			break;
3731
3732		ibmvfc_do_work(vhost);
3733	}
3734
3735	ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
3736	return 0;
3737}
3738
3739/**
3740 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
3741 * @vhost:	ibmvfc host struct
3742 *
3743 * Allocates a page for messages, maps it for dma, and registers
3744 * the crq with the hypervisor.
3745 *
3746 * Return value:
3747 *	zero on success / other on failure
3748 **/
3749static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
3750{
3751	int rc, retrc = -ENOMEM;
3752	struct device *dev = vhost->dev;
3753	struct vio_dev *vdev = to_vio_dev(dev);
3754	struct ibmvfc_crq_queue *crq = &vhost->crq;
3755
3756	ENTER;
3757	crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
3758
3759	if (!crq->msgs)
3760		return -ENOMEM;
3761
3762	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3763	crq->msg_token = dma_map_single(dev, crq->msgs,
3764					PAGE_SIZE, DMA_BIDIRECTIONAL);
3765
3766	if (dma_mapping_error(dev, crq->msg_token))
3767		goto map_failed;
3768
3769	retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3770					crq->msg_token, PAGE_SIZE);
3771
3772	if (rc == H_RESOURCE)
3773		/* maybe kexecing and resource is busy. try a reset */
3774		retrc = rc = ibmvfc_reset_crq(vhost);
3775
3776	if (rc == H_CLOSED)
3777		dev_warn(dev, "Partner adapter not ready\n");
3778	else if (rc) {
3779		dev_warn(dev, "Error %d opening adapter\n", rc);
3780		goto reg_crq_failed;
3781	}
3782
3783	retrc = 0;
3784
3785	if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
3786		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
3787		goto req_irq_failed;
3788	}
3789
3790	if ((rc = vio_enable_interrupts(vdev))) {
3791		dev_err(dev, "Error %d enabling interrupts\n", rc);
3792		goto req_irq_failed;
3793	}
3794
3795	crq->cur = 0;
3796	LEAVE;
3797	return retrc;
3798
3799req_irq_failed:
3800	do {
3801		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3802	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3803reg_crq_failed:
3804	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3805map_failed:
3806	free_page((unsigned long)crq->msgs);
3807	return retrc;
3808}
3809
3810/**
3811 * ibmvfc_free_mem - Free memory for vhost
3812 * @vhost:	ibmvfc host struct
3813 *
3814 * Return value:
3815 * 	none
3816 **/
3817static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
3818{
3819	struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3820
3821	ENTER;
3822	mempool_destroy(vhost->tgt_pool);
3823	kfree(vhost->trace);
3824	dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
3825			  vhost->disc_buf_dma);
3826	dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
3827			  vhost->login_buf, vhost->login_buf_dma);
3828	dma_pool_destroy(vhost->sg_pool);
3829	dma_unmap_single(vhost->dev, async_q->msg_token,
3830			 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3831	free_page((unsigned long)async_q->msgs);
3832	LEAVE;
3833}
3834
3835/**
3836 * ibmvfc_alloc_mem - Allocate memory for vhost
3837 * @vhost:	ibmvfc host struct
3838 *
3839 * Return value:
3840 * 	0 on success / non-zero on failure
3841 **/
3842static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
3843{
3844	struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3845	struct device *dev = vhost->dev;
3846
3847	ENTER;
3848	async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
3849	if (!async_q->msgs) {
3850		dev_err(dev, "Couldn't allocate async queue.\n");
3851		goto nomem;
3852	}
3853
3854	async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
3855	async_q->msg_token = dma_map_single(dev, async_q->msgs,
3856					    async_q->size * sizeof(*async_q->msgs),
3857					    DMA_BIDIRECTIONAL);
3858
3859	if (dma_mapping_error(dev, async_q->msg_token)) {
3860		dev_err(dev, "Failed to map async queue\n");
3861		goto free_async_crq;
3862	}
3863
3864	vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
3865					 SG_ALL * sizeof(struct srp_direct_buf),
3866					 sizeof(struct srp_direct_buf), 0);
3867
3868	if (!vhost->sg_pool) {
3869		dev_err(dev, "Failed to allocate sg pool\n");
3870		goto unmap_async_crq;
3871	}
3872
3873	vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
3874					      &vhost->login_buf_dma, GFP_KERNEL);
3875
3876	if (!vhost->login_buf) {
3877		dev_err(dev, "Couldn't allocate NPIV login buffer\n");
3878		goto free_sg_pool;
3879	}
3880
3881	vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
3882	vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
3883					     &vhost->disc_buf_dma, GFP_KERNEL);
3884
3885	if (!vhost->disc_buf) {
3886		dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
3887		goto free_login_buffer;
3888	}
3889
3890	vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
3891			       sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
3892
3893	if (!vhost->trace)
3894		goto free_disc_buffer;
3895
3896	vhost->tgt_pool = mempool_create_kzalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
3897						      sizeof(struct ibmvfc_target));
3898
3899	if (!vhost->tgt_pool) {
3900		dev_err(dev, "Couldn't allocate target memory pool\n");
3901		goto free_trace;
3902	}
3903
3904	LEAVE;
3905	return 0;
3906
3907free_trace:
3908	kfree(vhost->trace);
3909free_disc_buffer:
3910	dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
3911			  vhost->disc_buf_dma);
3912free_login_buffer:
3913	dma_free_coherent(dev, sizeof(*vhost->login_buf),
3914			  vhost->login_buf, vhost->login_buf_dma);
3915free_sg_pool:
3916	dma_pool_destroy(vhost->sg_pool);
3917unmap_async_crq:
3918	dma_unmap_single(dev, async_q->msg_token,
3919			 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3920free_async_crq:
3921	free_page((unsigned long)async_q->msgs);
3922nomem:
3923	LEAVE;
3924	return -ENOMEM;
3925}
3926
3927/**
3928 * ibmvfc_probe - Adapter hot plug add entry point
3929 * @vdev:	vio device struct
3930 * @id:	vio device id struct
3931 *
3932 * Return value:
3933 * 	0 on success / non-zero on failure
3934 **/
3935static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
3936{
3937	struct ibmvfc_host *vhost;
3938	struct Scsi_Host *shost;
3939	struct device *dev = &vdev->dev;
3940	int rc = -ENOMEM;
3941
3942	ENTER;
3943	shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
3944	if (!shost) {
3945		dev_err(dev, "Couldn't allocate host data\n");
3946		goto out;
3947	}
3948
3949	shost->transportt = ibmvfc_transport_template;
3950	shost->can_queue = max_requests;
3951	shost->max_lun = max_lun;
3952	shost->max_id = max_targets;
3953	shost->max_sectors = IBMVFC_MAX_SECTORS;
3954	shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
3955	shost->unique_id = shost->host_no;
3956
3957	vhost = shost_priv(shost);
3958	INIT_LIST_HEAD(&vhost->sent);
3959	INIT_LIST_HEAD(&vhost->free);
3960	INIT_LIST_HEAD(&vhost->targets);
3961	sprintf(vhost->name, IBMVFC_NAME);
3962	vhost->host = shost;
3963	vhost->dev = dev;
3964	vhost->partition_number = -1;
3965	vhost->log_level = log_level;
3966	strcpy(vhost->partition_name, "UNKNOWN");
3967	init_waitqueue_head(&vhost->work_wait_q);
3968	init_waitqueue_head(&vhost->init_wait_q);
3969
3970	if ((rc = ibmvfc_alloc_mem(vhost)))
3971		goto free_scsi_host;
3972
3973	vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
3974					 shost->host_no);
3975
3976	if (IS_ERR(vhost->work_thread)) {
3977		dev_err(dev, "Couldn't create kernel thread: %ld\n",
3978			PTR_ERR(vhost->work_thread));
3979		goto free_host_mem;
3980	}
3981
3982	if ((rc = ibmvfc_init_crq(vhost))) {
3983		dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3984		goto kill_kthread;
3985	}
3986
3987	if ((rc = ibmvfc_init_event_pool(vhost))) {
3988		dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
3989		goto release_crq;
3990	}
3991
3992	if ((rc = scsi_add_host(shost, dev)))
3993		goto release_event_pool;
3994
3995	if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
3996					   &ibmvfc_trace_attr))) {
3997		dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
3998		goto remove_shost;
3999	}
4000
4001	dev_set_drvdata(dev, vhost);
4002	spin_lock(&ibmvfc_driver_lock);
4003	list_add_tail(&vhost->queue, &ibmvfc_head);
4004	spin_unlock(&ibmvfc_driver_lock);
4005
4006	ibmvfc_send_crq_init(vhost);
4007	scsi_scan_host(shost);
4008	return 0;
4009
4010remove_shost:
4011	scsi_remove_host(shost);
4012release_event_pool:
4013	ibmvfc_free_event_pool(vhost);
4014release_crq:
4015	ibmvfc_release_crq_queue(vhost);
4016kill_kthread:
4017	kthread_stop(vhost->work_thread);
4018free_host_mem:
4019	ibmvfc_free_mem(vhost);
4020free_scsi_host:
4021	scsi_host_put(shost);
4022out:
4023	LEAVE;
4024	return rc;
4025}
4026
4027/**
4028 * ibmvfc_remove - Adapter hot plug remove entry point
4029 * @vdev:	vio device struct
4030 *
4031 * Return value:
4032 * 	0
4033 **/
4034static int ibmvfc_remove(struct vio_dev *vdev)
4035{
4036	struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4037	unsigned long flags;
4038
4039	ENTER;
4040	ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
4041	ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
4042	ibmvfc_wait_while_resetting(vhost);
4043	ibmvfc_release_crq_queue(vhost);
4044	kthread_stop(vhost->work_thread);
4045	fc_remove_host(vhost->host);
4046	scsi_remove_host(vhost->host);
4047
4048	spin_lock_irqsave(vhost->host->host_lock, flags);
4049	ibmvfc_purge_requests(vhost, DID_ERROR);
4050	ibmvfc_free_event_pool(vhost);
4051	spin_unlock_irqrestore(vhost->host->host_lock, flags);
4052
4053	ibmvfc_free_mem(vhost);
4054	spin_lock(&ibmvfc_driver_lock);
4055	list_del(&vhost->queue);
4056	spin_unlock(&ibmvfc_driver_lock);
4057	scsi_host_put(vhost->host);
4058	LEAVE;
4059	return 0;
4060}
4061
4062/**
4063 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4064 * @vdev:	vio device struct
4065 *
4066 * Return value:
4067 *	Number of bytes the driver will need to DMA map at the same time in
4068 *	order to perform well.
4069 */
4070static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4071{
4072	unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4073	return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4074}
4075
4076static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
4077	{"fcp", "IBM,vfc-client"},
4078	{ "", "" }
4079};
4080MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4081
4082static struct vio_driver ibmvfc_driver = {
4083	.id_table = ibmvfc_device_table,
4084	.probe = ibmvfc_probe,
4085	.remove = ibmvfc_remove,
4086	.get_desired_dma = ibmvfc_get_desired_dma,
4087	.driver = {
4088		.name = IBMVFC_NAME,
4089		.owner = THIS_MODULE,
4090	}
4091};
4092
4093static struct fc_function_template ibmvfc_transport_functions = {
4094	.show_host_fabric_name = 1,
4095	.show_host_node_name = 1,
4096	.show_host_port_name = 1,
4097	.show_host_supported_classes = 1,
4098	.show_host_port_type = 1,
4099	.show_host_port_id = 1,
4100
4101	.get_host_port_state = ibmvfc_get_host_port_state,
4102	.show_host_port_state = 1,
4103
4104	.get_host_speed = ibmvfc_get_host_speed,
4105	.show_host_speed = 1,
4106
4107	.issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4108	.terminate_rport_io = ibmvfc_terminate_rport_io,
4109
4110	.show_rport_maxframe_size = 1,
4111	.show_rport_supported_classes = 1,
4112
4113	.set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4114	.show_rport_dev_loss_tmo = 1,
4115
4116	.get_starget_node_name = ibmvfc_get_starget_node_name,
4117	.show_starget_node_name = 1,
4118
4119	.get_starget_port_name = ibmvfc_get_starget_port_name,
4120	.show_starget_port_name = 1,
4121
4122	.get_starget_port_id = ibmvfc_get_starget_port_id,
4123	.show_starget_port_id = 1,
4124};
4125
4126/**
4127 * ibmvfc_module_init - Initialize the ibmvfc module
4128 *
4129 * Return value:
4130 * 	0 on success / other on failure
4131 **/
4132static int __init ibmvfc_module_init(void)
4133{
4134	int rc;
4135
4136	if (!firmware_has_feature(FW_FEATURE_VIO))
4137		return -ENODEV;
4138
4139	printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4140	       IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
4141
4142	ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
4143	if (!ibmvfc_transport_template)
4144		return -ENOMEM;
4145
4146	rc = vio_register_driver(&ibmvfc_driver);
4147	if (rc)
4148		fc_release_transport(ibmvfc_transport_template);
4149	return rc;
4150}
4151
4152/**
4153 * ibmvfc_module_exit - Teardown the ibmvfc module
4154 *
4155 * Return value:
4156 * 	nothing
4157 **/
4158static void __exit ibmvfc_module_exit(void)
4159{
4160	vio_unregister_driver(&ibmvfc_driver);
4161	fc_release_transport(ibmvfc_transport_template);
4162}
4163
4164module_init(ibmvfc_module_init);
4165module_exit(ibmvfc_module_exit);
4166