1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
5 * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
7 * Copyright (C) IBM Corporation, 2008
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmapool.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqdomain.h>
17 #include <linux/kthread.h>
18 #include <linux/slab.h>
21 #include <linux/stringify.h>
22 #include <linux/bsg-lib.h>
23 #include <asm/firmware.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_transport_fc.h>
32 #include <scsi/scsi_bsg_fc.h>
35 static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
36 static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
37 static u64 max_lun = IBMVFC_MAX_LUN;
38 static unsigned int max_targets = IBMVFC_MAX_TARGETS;
39 static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
40 static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
41 static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
42 static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
43 static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
44 static unsigned int mq_enabled = IBMVFC_MQ;
45 static unsigned int nr_scsi_hw_queues = IBMVFC_SCSI_HW_QUEUES;
46 static unsigned int nr_scsi_channels = IBMVFC_SCSI_CHANNELS;
47 static unsigned int mig_channels_only = IBMVFC_MIG_NO_SUB_TO_CRQ;
48 static unsigned int mig_no_less_channels = IBMVFC_MIG_NO_N_TO_M;
50 static LIST_HEAD(ibmvfc_head);
51 static DEFINE_SPINLOCK(ibmvfc_driver_lock);
52 static struct scsi_transport_template *ibmvfc_transport_template;
54 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
55 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
56 MODULE_LICENSE("GPL");
57 MODULE_VERSION(IBMVFC_DRIVER_VERSION);
59 module_param_named(mq, mq_enabled, uint, S_IRUGO);
60 MODULE_PARM_DESC(mq, "Enable multiqueue support. "
61 "[Default=" __stringify(IBMVFC_MQ) "]");
62 module_param_named(scsi_host_queues, nr_scsi_hw_queues, uint, S_IRUGO);
63 MODULE_PARM_DESC(scsi_host_queues, "Number of SCSI Host submission queues. "
64 "[Default=" __stringify(IBMVFC_SCSI_HW_QUEUES) "]");
65 module_param_named(scsi_hw_channels, nr_scsi_channels, uint, S_IRUGO);
66 MODULE_PARM_DESC(scsi_hw_channels, "Number of hw scsi channels to request. "
67 "[Default=" __stringify(IBMVFC_SCSI_CHANNELS) "]");
68 module_param_named(mig_channels_only, mig_channels_only, uint, S_IRUGO);
69 MODULE_PARM_DESC(mig_channels_only, "Prevent migration to non-channelized system. "
70 "[Default=" __stringify(IBMVFC_MIG_NO_SUB_TO_CRQ) "]");
71 module_param_named(mig_no_less_channels, mig_no_less_channels, uint, S_IRUGO);
72 MODULE_PARM_DESC(mig_no_less_channels, "Prevent migration to system with less channels. "
73 "[Default=" __stringify(IBMVFC_MIG_NO_N_TO_M) "]");
75 module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
76 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
77 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
78 module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
79 MODULE_PARM_DESC(default_timeout,
80 "Default timeout in seconds for initialization and EH commands. "
81 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
82 module_param_named(max_requests, max_requests, uint, S_IRUGO);
83 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
84 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
85 module_param_named(max_lun, max_lun, ullong, S_IRUGO);
86 MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
87 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
88 module_param_named(max_targets, max_targets, uint, S_IRUGO);
89 MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
90 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
91 module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
92 MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
93 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
94 module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
95 MODULE_PARM_DESC(debug, "Enable driver debug information. "
96 "[Default=" __stringify(IBMVFC_DEBUG) "]");
97 module_param_named(log_level, log_level, uint, 0);
98 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
99 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
100 module_param_named(cls3_error, cls3_error, uint, 0);
101 MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
102 "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
104 static const struct {
112 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
113 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
114 { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
115 { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
116 { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
117 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
118 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
119 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
120 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
121 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
122 { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
123 { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
124 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
125 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
127 { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
128 { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
129 { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
130 { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
131 { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
132 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
133 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
134 { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
135 { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
136 { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
138 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
139 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
140 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
141 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
142 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
143 { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
144 { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
145 { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
146 { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
147 { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
148 { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
150 { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
151 { IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." },
154 static void ibmvfc_npiv_login(struct ibmvfc_host *);
155 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
156 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
157 static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
158 static void ibmvfc_npiv_logout(struct ibmvfc_host *);
159 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *);
160 static void ibmvfc_tgt_move_login(struct ibmvfc_target *);
162 static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *);
163 static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *);
165 static const char *unknown_error = "unknown error";
167 static long h_reg_sub_crq(unsigned long unit_address, unsigned long ioba,
168 unsigned long length, unsigned long *cookie,
171 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
174 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, ioba, length);
181 static int ibmvfc_check_caps(struct ibmvfc_host *vhost, unsigned long cap_flags)
183 u64 host_caps = be64_to_cpu(vhost->login_buf->resp.capabilities);
185 return (host_caps & cap_flags) ? 1 : 0;
188 static struct ibmvfc_fcp_cmd_iu *ibmvfc_get_fcp_iu(struct ibmvfc_host *vhost,
189 struct ibmvfc_cmd *vfc_cmd)
191 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
192 return &vfc_cmd->v2.iu;
194 return &vfc_cmd->v1.iu;
197 static struct ibmvfc_fcp_rsp *ibmvfc_get_fcp_rsp(struct ibmvfc_host *vhost,
198 struct ibmvfc_cmd *vfc_cmd)
200 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
201 return &vfc_cmd->v2.rsp;
203 return &vfc_cmd->v1.rsp;
206 #ifdef CONFIG_SCSI_IBMVFC_TRACE
208 * ibmvfc_trc_start - Log a start trace entry
209 * @evt: ibmvfc event struct
212 static void ibmvfc_trc_start(struct ibmvfc_event *evt)
214 struct ibmvfc_host *vhost = evt->vhost;
215 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
216 struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
217 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
218 struct ibmvfc_trace_entry *entry;
219 int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
221 entry = &vhost->trace[index];
223 entry->time = jiffies;
224 entry->fmt = evt->crq.format;
225 entry->type = IBMVFC_TRC_START;
227 switch (entry->fmt) {
228 case IBMVFC_CMD_FORMAT:
229 entry->op_code = iu->cdb[0];
230 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
231 entry->lun = scsilun_to_int(&iu->lun);
232 entry->tmf_flags = iu->tmf_flags;
233 entry->u.start.xfer_len = be32_to_cpu(iu->xfer_len);
235 case IBMVFC_MAD_FORMAT:
236 entry->op_code = be32_to_cpu(mad->opcode);
244 * ibmvfc_trc_end - Log an end trace entry
245 * @evt: ibmvfc event struct
248 static void ibmvfc_trc_end(struct ibmvfc_event *evt)
250 struct ibmvfc_host *vhost = evt->vhost;
251 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
252 struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
253 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
254 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
255 struct ibmvfc_trace_entry *entry;
256 int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
258 entry = &vhost->trace[index];
260 entry->time = jiffies;
261 entry->fmt = evt->crq.format;
262 entry->type = IBMVFC_TRC_END;
264 switch (entry->fmt) {
265 case IBMVFC_CMD_FORMAT:
266 entry->op_code = iu->cdb[0];
267 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
268 entry->lun = scsilun_to_int(&iu->lun);
269 entry->tmf_flags = iu->tmf_flags;
270 entry->u.end.status = be16_to_cpu(vfc_cmd->status);
271 entry->u.end.error = be16_to_cpu(vfc_cmd->error);
272 entry->u.end.fcp_rsp_flags = rsp->flags;
273 entry->u.end.rsp_code = rsp->data.info.rsp_code;
274 entry->u.end.scsi_status = rsp->scsi_status;
276 case IBMVFC_MAD_FORMAT:
277 entry->op_code = be32_to_cpu(mad->opcode);
278 entry->u.end.status = be16_to_cpu(mad->status);
287 #define ibmvfc_trc_start(evt) do { } while (0)
288 #define ibmvfc_trc_end(evt) do { } while (0)
292 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
293 * @status: status / error class
297 * index into cmd_status / -EINVAL on failure
299 static int ibmvfc_get_err_index(u16 status, u16 error)
303 for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
304 if ((cmd_status[i].status & status) == cmd_status[i].status &&
305 cmd_status[i].error == error)
312 * ibmvfc_get_cmd_error - Find the error description for the fcp response
313 * @status: status / error class
317 * error description string
319 static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
321 int rc = ibmvfc_get_err_index(status, error);
323 return cmd_status[rc].name;
324 return unknown_error;
328 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
329 * @vhost: ibmvfc host struct
330 * @vfc_cmd: ibmvfc command struct
333 * SCSI result value to return for completed command
335 static int ibmvfc_get_err_result(struct ibmvfc_host *vhost, struct ibmvfc_cmd *vfc_cmd)
338 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
339 int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
341 if ((rsp->flags & FCP_RSP_LEN_VALID) &&
342 ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
343 rsp->data.info.rsp_code))
344 return DID_ERROR << 16;
346 err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
348 return rsp->scsi_status | (cmd_status[err].result << 16);
349 return rsp->scsi_status | (DID_ERROR << 16);
353 * ibmvfc_retry_cmd - Determine if error status is retryable
354 * @status: status / error class
358 * 1 if error should be retried / 0 if it should not
360 static int ibmvfc_retry_cmd(u16 status, u16 error)
362 int rc = ibmvfc_get_err_index(status, error);
365 return cmd_status[rc].retry;
369 static const char *unknown_fc_explain = "unknown fc explain";
371 static const struct {
375 { 0x00, "no additional explanation" },
376 { 0x01, "service parameter error - options" },
377 { 0x03, "service parameter error - initiator control" },
378 { 0x05, "service parameter error - recipient control" },
379 { 0x07, "service parameter error - received data field size" },
380 { 0x09, "service parameter error - concurrent seq" },
381 { 0x0B, "service parameter error - credit" },
382 { 0x0D, "invalid N_Port/F_Port_Name" },
383 { 0x0E, "invalid node/Fabric Name" },
384 { 0x0F, "invalid common service parameters" },
385 { 0x11, "invalid association header" },
386 { 0x13, "association header required" },
387 { 0x15, "invalid originator S_ID" },
388 { 0x17, "invalid OX_ID-RX-ID combination" },
389 { 0x19, "command (request) already in progress" },
390 { 0x1E, "N_Port Login requested" },
391 { 0x1F, "Invalid N_Port_ID" },
394 static const struct {
398 { 0x00, "no additional explanation" },
399 { 0x01, "port identifier not registered" },
400 { 0x02, "port name not registered" },
401 { 0x03, "node name not registered" },
402 { 0x04, "class of service not registered" },
403 { 0x06, "initial process associator not registered" },
404 { 0x07, "FC-4 TYPEs not registered" },
405 { 0x08, "symbolic port name not registered" },
406 { 0x09, "symbolic node name not registered" },
407 { 0x0A, "port type not registered" },
408 { 0xF0, "authorization exception" },
409 { 0xF1, "authentication exception" },
410 { 0xF2, "data base full" },
411 { 0xF3, "data base empty" },
412 { 0xF4, "processing request" },
413 { 0xF5, "unable to verify connection" },
414 { 0xF6, "devices not in a common zone" },
418 * ibmvfc_get_ls_explain - Return the FC Explain description text
419 * @status: FC Explain status
424 static const char *ibmvfc_get_ls_explain(u16 status)
428 for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
429 if (ls_explain[i].fc_explain == status)
430 return ls_explain[i].name;
432 return unknown_fc_explain;
436 * ibmvfc_get_gs_explain - Return the FC Explain description text
437 * @status: FC Explain status
442 static const char *ibmvfc_get_gs_explain(u16 status)
446 for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
447 if (gs_explain[i].fc_explain == status)
448 return gs_explain[i].name;
450 return unknown_fc_explain;
453 static const struct {
454 enum ibmvfc_fc_type fc_type;
457 { IBMVFC_FABRIC_REJECT, "fabric reject" },
458 { IBMVFC_PORT_REJECT, "port reject" },
459 { IBMVFC_LS_REJECT, "ELS reject" },
460 { IBMVFC_FABRIC_BUSY, "fabric busy" },
461 { IBMVFC_PORT_BUSY, "port busy" },
462 { IBMVFC_BASIC_REJECT, "basic reject" },
465 static const char *unknown_fc_type = "unknown fc type";
468 * ibmvfc_get_fc_type - Return the FC Type description text
469 * @status: FC Type error status
474 static const char *ibmvfc_get_fc_type(u16 status)
478 for (i = 0; i < ARRAY_SIZE(fc_type); i++)
479 if (fc_type[i].fc_type == status)
480 return fc_type[i].name;
482 return unknown_fc_type;
486 * ibmvfc_set_tgt_action - Set the next init action for the target
487 * @tgt: ibmvfc target struct
488 * @action: action to perform
491 * 0 if action changed / non-zero if not changed
493 static int ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
494 enum ibmvfc_target_action action)
498 switch (tgt->action) {
499 case IBMVFC_TGT_ACTION_LOGOUT_RPORT:
500 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT ||
501 action == IBMVFC_TGT_ACTION_DEL_RPORT) {
502 tgt->action = action;
506 case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT:
507 if (action == IBMVFC_TGT_ACTION_DEL_RPORT ||
508 action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
509 tgt->action = action;
513 case IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT:
514 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
515 tgt->action = action;
519 case IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT:
520 if (action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
521 tgt->action = action;
525 case IBMVFC_TGT_ACTION_DEL_RPORT:
526 if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
527 tgt->action = action;
531 case IBMVFC_TGT_ACTION_DELETED_RPORT:
534 tgt->action = action;
539 if (action >= IBMVFC_TGT_ACTION_LOGOUT_RPORT)
546 * ibmvfc_set_host_state - Set the state for the host
547 * @vhost: ibmvfc host struct
548 * @state: state to set host to
551 * 0 if state changed / non-zero if not changed
553 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
554 enum ibmvfc_host_state state)
558 switch (vhost->state) {
559 case IBMVFC_HOST_OFFLINE:
563 vhost->state = state;
571 * ibmvfc_set_host_action - Set the next init action for the host
572 * @vhost: ibmvfc host struct
573 * @action: action to perform
576 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
577 enum ibmvfc_host_action action)
580 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
581 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
582 vhost->action = action;
584 case IBMVFC_HOST_ACTION_LOGO_WAIT:
585 if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
586 vhost->action = action;
588 case IBMVFC_HOST_ACTION_INIT_WAIT:
589 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
590 vhost->action = action;
592 case IBMVFC_HOST_ACTION_QUERY:
593 switch (vhost->action) {
594 case IBMVFC_HOST_ACTION_INIT_WAIT:
595 case IBMVFC_HOST_ACTION_NONE:
596 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
597 vhost->action = action;
603 case IBMVFC_HOST_ACTION_TGT_INIT:
604 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
605 vhost->action = action;
607 case IBMVFC_HOST_ACTION_REENABLE:
608 case IBMVFC_HOST_ACTION_RESET:
609 vhost->action = action;
611 case IBMVFC_HOST_ACTION_INIT:
612 case IBMVFC_HOST_ACTION_TGT_DEL:
613 case IBMVFC_HOST_ACTION_LOGO:
614 case IBMVFC_HOST_ACTION_QUERY_TGTS:
615 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
616 case IBMVFC_HOST_ACTION_NONE:
618 switch (vhost->action) {
619 case IBMVFC_HOST_ACTION_RESET:
620 case IBMVFC_HOST_ACTION_REENABLE:
623 vhost->action = action;
631 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
632 * @vhost: ibmvfc host struct
637 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
639 if (vhost->action == IBMVFC_HOST_ACTION_NONE &&
640 vhost->state == IBMVFC_ACTIVE) {
641 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
642 scsi_block_requests(vhost->host);
643 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
648 wake_up(&vhost->work_wait_q);
652 * ibmvfc_del_tgt - Schedule cleanup and removal of the target
653 * @tgt: ibmvfc target struct
655 static void ibmvfc_del_tgt(struct ibmvfc_target *tgt)
657 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT)) {
658 tgt->job_step = ibmvfc_tgt_implicit_logout_and_del;
659 tgt->init_retries = 0;
661 wake_up(&tgt->vhost->work_wait_q);
665 * ibmvfc_link_down - Handle a link down event from the adapter
666 * @vhost: ibmvfc host struct
667 * @state: ibmvfc host state to enter
670 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
671 enum ibmvfc_host_state state)
673 struct ibmvfc_target *tgt;
676 scsi_block_requests(vhost->host);
677 list_for_each_entry(tgt, &vhost->targets, queue)
679 ibmvfc_set_host_state(vhost, state);
680 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
681 vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
682 wake_up(&vhost->work_wait_q);
687 * ibmvfc_init_host - Start host initialization
688 * @vhost: ibmvfc host struct
693 static void ibmvfc_init_host(struct ibmvfc_host *vhost)
695 struct ibmvfc_target *tgt;
697 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
698 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
700 "Host initialization retries exceeded. Taking adapter offline\n");
701 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
706 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
707 memset(vhost->async_crq.msgs.async, 0, PAGE_SIZE);
708 vhost->async_crq.cur = 0;
710 list_for_each_entry(tgt, &vhost->targets, queue) {
711 if (vhost->client_migrated)
717 scsi_block_requests(vhost->host);
718 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
719 vhost->job_step = ibmvfc_npiv_login;
720 wake_up(&vhost->work_wait_q);
725 * ibmvfc_send_crq - Send a CRQ
726 * @vhost: ibmvfc host struct
727 * @word1: the first 64 bits of the data
728 * @word2: the second 64 bits of the data
731 * 0 on success / other on failure
733 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
735 struct vio_dev *vdev = to_vio_dev(vhost->dev);
736 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
739 static int ibmvfc_send_sub_crq(struct ibmvfc_host *vhost, u64 cookie, u64 word1,
740 u64 word2, u64 word3, u64 word4)
742 struct vio_dev *vdev = to_vio_dev(vhost->dev);
744 return plpar_hcall_norets(H_SEND_SUB_CRQ, vdev->unit_address, cookie,
745 word1, word2, word3, word4);
749 * ibmvfc_send_crq_init - Send a CRQ init message
750 * @vhost: ibmvfc host struct
753 * 0 on success / other on failure
755 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
757 ibmvfc_dbg(vhost, "Sending CRQ init\n");
758 return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
762 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
763 * @vhost: ibmvfc host struct
766 * 0 on success / other on failure
768 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
770 ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
771 return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
775 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
776 * @vhost: ibmvfc host who owns the event pool
777 * @queue: ibmvfc queue struct
780 * Returns zero on success.
782 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost,
783 struct ibmvfc_queue *queue,
787 struct ibmvfc_event_pool *pool = &queue->evt_pool;
794 pool->events = kcalloc(size, sizeof(*pool->events), GFP_KERNEL);
798 pool->iu_storage = dma_alloc_coherent(vhost->dev,
799 size * sizeof(*pool->iu_storage),
802 if (!pool->iu_storage) {
807 INIT_LIST_HEAD(&queue->sent);
808 INIT_LIST_HEAD(&queue->free);
809 spin_lock_init(&queue->l_lock);
811 for (i = 0; i < size; ++i) {
812 struct ibmvfc_event *evt = &pool->events[i];
817 * 0 = being completed
820 atomic_set(&evt->active, -1);
821 atomic_set(&evt->free, 1);
822 evt->crq.valid = 0x80;
823 evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
824 evt->xfer_iu = pool->iu_storage + i;
827 evt->ext_list = NULL;
828 list_add_tail(&evt->queue_list, &queue->free);
836 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
837 * @vhost: ibmvfc host who owns the event pool
838 * @queue: ibmvfc queue struct
841 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost,
842 struct ibmvfc_queue *queue)
845 struct ibmvfc_event_pool *pool = &queue->evt_pool;
848 for (i = 0; i < pool->size; ++i) {
849 list_del(&pool->events[i].queue_list);
850 BUG_ON(atomic_read(&pool->events[i].free) != 1);
851 if (pool->events[i].ext_list)
852 dma_pool_free(vhost->sg_pool,
853 pool->events[i].ext_list,
854 pool->events[i].ext_list_token);
858 dma_free_coherent(vhost->dev,
859 pool->size * sizeof(*pool->iu_storage),
860 pool->iu_storage, pool->iu_token);
865 * ibmvfc_free_queue - Deallocate queue
866 * @vhost: ibmvfc host struct
867 * @queue: ibmvfc queue struct
869 * Unmaps dma and deallocates page for messages
871 static void ibmvfc_free_queue(struct ibmvfc_host *vhost,
872 struct ibmvfc_queue *queue)
874 struct device *dev = vhost->dev;
876 dma_unmap_single(dev, queue->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
877 free_page((unsigned long)queue->msgs.handle);
878 queue->msgs.handle = NULL;
880 ibmvfc_free_event_pool(vhost, queue);
884 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
885 * @vhost: ibmvfc host struct
887 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
888 * the crq with the hypervisor.
890 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
893 struct vio_dev *vdev = to_vio_dev(vhost->dev);
894 struct ibmvfc_queue *crq = &vhost->crq;
896 ibmvfc_dbg(vhost, "Releasing CRQ\n");
897 free_irq(vdev->irq, vhost);
898 tasklet_kill(&vhost->tasklet);
902 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
903 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
905 vhost->state = IBMVFC_NO_CRQ;
906 vhost->logged_in = 0;
908 ibmvfc_free_queue(vhost, crq);
912 * ibmvfc_reenable_crq_queue - reenables the CRQ
913 * @vhost: ibmvfc host struct
916 * 0 on success / other on failure
918 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
921 struct vio_dev *vdev = to_vio_dev(vhost->dev);
924 ibmvfc_dereg_sub_crqs(vhost);
926 /* Re-enable the CRQ */
930 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
931 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
934 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
936 spin_lock_irqsave(vhost->host->host_lock, flags);
937 spin_lock(vhost->crq.q_lock);
938 vhost->do_enquiry = 1;
939 vhost->using_channels = 0;
940 spin_unlock(vhost->crq.q_lock);
941 spin_unlock_irqrestore(vhost->host->host_lock, flags);
943 ibmvfc_reg_sub_crqs(vhost);
949 * ibmvfc_reset_crq - resets a crq after a failure
950 * @vhost: ibmvfc host struct
953 * 0 on success / other on failure
955 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
959 struct vio_dev *vdev = to_vio_dev(vhost->dev);
960 struct ibmvfc_queue *crq = &vhost->crq;
962 ibmvfc_dereg_sub_crqs(vhost);
968 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
969 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
971 spin_lock_irqsave(vhost->host->host_lock, flags);
972 spin_lock(vhost->crq.q_lock);
973 vhost->state = IBMVFC_NO_CRQ;
974 vhost->logged_in = 0;
975 vhost->do_enquiry = 1;
976 vhost->using_channels = 0;
978 /* Clean out the queue */
979 memset(crq->msgs.crq, 0, PAGE_SIZE);
982 /* And re-open it again */
983 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
984 crq->msg_token, PAGE_SIZE);
987 /* Adapter is good, but other end is not ready */
988 dev_warn(vhost->dev, "Partner adapter not ready\n");
990 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
992 spin_unlock(vhost->crq.q_lock);
993 spin_unlock_irqrestore(vhost->host->host_lock, flags);
995 ibmvfc_reg_sub_crqs(vhost);
1001 * ibmvfc_valid_event - Determines if event is valid.
1002 * @pool: event_pool that contains the event
1003 * @evt: ibmvfc event to be checked for validity
1006 * 1 if event is valid / 0 if event is not valid
1008 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
1009 struct ibmvfc_event *evt)
1011 int index = evt - pool->events;
1012 if (index < 0 || index >= pool->size) /* outside of bounds */
1014 if (evt != pool->events + index) /* unaligned */
1020 * ibmvfc_free_event - Free the specified event
1021 * @evt: ibmvfc_event to be freed
1024 static void ibmvfc_free_event(struct ibmvfc_event *evt)
1026 struct ibmvfc_event_pool *pool = &evt->queue->evt_pool;
1027 unsigned long flags;
1029 BUG_ON(!ibmvfc_valid_event(pool, evt));
1030 BUG_ON(atomic_inc_return(&evt->free) != 1);
1031 BUG_ON(atomic_dec_and_test(&evt->active));
1033 spin_lock_irqsave(&evt->queue->l_lock, flags);
1034 list_add_tail(&evt->queue_list, &evt->queue->free);
1036 complete(evt->eh_comp);
1037 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1041 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
1042 * @evt: ibmvfc event struct
1044 * This function does not setup any error status, that must be done
1045 * before this function gets called.
1047 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
1049 struct scsi_cmnd *cmnd = evt->cmnd;
1052 scsi_dma_unmap(cmnd);
1056 ibmvfc_free_event(evt);
1060 * ibmvfc_complete_purge - Complete failed command list
1061 * @purge_list: list head of failed commands
1063 * This function runs completions on commands to fail as a result of a
1064 * host reset or platform migration.
1066 static void ibmvfc_complete_purge(struct list_head *purge_list)
1068 struct ibmvfc_event *evt, *pos;
1070 list_for_each_entry_safe(evt, pos, purge_list, queue_list) {
1071 list_del(&evt->queue_list);
1072 ibmvfc_trc_end(evt);
1078 * ibmvfc_fail_request - Fail request with specified error code
1079 * @evt: ibmvfc event struct
1080 * @error_code: error code to fail request with
1085 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
1088 * Anything we are failing should still be active. Otherwise, it
1089 * implies we already got a response for the command and are doing
1090 * something bad like double completing it.
1092 BUG_ON(!atomic_dec_and_test(&evt->active));
1094 evt->cmnd->result = (error_code << 16);
1095 evt->done = ibmvfc_scsi_eh_done;
1097 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
1099 del_timer(&evt->timer);
1103 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
1104 * @vhost: ibmvfc host struct
1105 * @error_code: error code to fail requests with
1110 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
1112 struct ibmvfc_event *evt, *pos;
1113 struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
1114 unsigned long flags;
1118 if (vhost->using_channels)
1119 hwqs = vhost->scsi_scrqs.active_queues;
1121 ibmvfc_dbg(vhost, "Purging all requests\n");
1122 spin_lock_irqsave(&vhost->crq.l_lock, flags);
1123 list_for_each_entry_safe(evt, pos, &vhost->crq.sent, queue_list)
1124 ibmvfc_fail_request(evt, error_code);
1125 list_splice_init(&vhost->crq.sent, &vhost->purge);
1126 spin_unlock_irqrestore(&vhost->crq.l_lock, flags);
1128 for (i = 0; i < hwqs; i++) {
1129 spin_lock_irqsave(queues[i].q_lock, flags);
1130 spin_lock(&queues[i].l_lock);
1131 list_for_each_entry_safe(evt, pos, &queues[i].sent, queue_list)
1132 ibmvfc_fail_request(evt, error_code);
1133 list_splice_init(&queues[i].sent, &vhost->purge);
1134 spin_unlock(&queues[i].l_lock);
1135 spin_unlock_irqrestore(queues[i].q_lock, flags);
1140 * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
1141 * @vhost: struct ibmvfc host to reset
1143 static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
1145 ibmvfc_purge_requests(vhost, DID_ERROR);
1146 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
1147 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
1151 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
1152 * @vhost: struct ibmvfc host to reset
1154 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
1156 if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
1157 !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
1158 scsi_block_requests(vhost->host);
1159 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
1160 vhost->job_step = ibmvfc_npiv_logout;
1161 wake_up(&vhost->work_wait_q);
1163 ibmvfc_hard_reset_host(vhost);
1167 * ibmvfc_reset_host - Reset the connection to the server
1168 * @vhost: ibmvfc host struct
1170 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
1172 unsigned long flags;
1174 spin_lock_irqsave(vhost->host->host_lock, flags);
1175 __ibmvfc_reset_host(vhost);
1176 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1180 * ibmvfc_retry_host_init - Retry host initialization if allowed
1181 * @vhost: ibmvfc host struct
1183 * Returns: 1 if init will be retried / 0 if not
1186 static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
1190 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
1191 vhost->delay_init = 1;
1192 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
1194 "Host initialization retries exceeded. Taking adapter offline\n");
1195 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
1196 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
1197 __ibmvfc_reset_host(vhost);
1199 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
1204 wake_up(&vhost->work_wait_q);
1209 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
1210 * @starget: scsi target struct
1213 * ibmvfc_target struct / NULL if not found
1215 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
1217 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1218 struct ibmvfc_host *vhost = shost_priv(shost);
1219 struct ibmvfc_target *tgt;
1221 list_for_each_entry(tgt, &vhost->targets, queue)
1222 if (tgt->target_id == starget->id) {
1223 kref_get(&tgt->kref);
1230 * ibmvfc_get_target - Find the specified scsi_target
1231 * @starget: scsi target struct
1234 * ibmvfc_target struct / NULL if not found
1236 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
1238 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1239 struct ibmvfc_target *tgt;
1240 unsigned long flags;
1242 spin_lock_irqsave(shost->host_lock, flags);
1243 tgt = __ibmvfc_get_target(starget);
1244 spin_unlock_irqrestore(shost->host_lock, flags);
1249 * ibmvfc_get_host_speed - Get host port speed
1250 * @shost: scsi host struct
1255 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
1257 struct ibmvfc_host *vhost = shost_priv(shost);
1258 unsigned long flags;
1260 spin_lock_irqsave(shost->host_lock, flags);
1261 if (vhost->state == IBMVFC_ACTIVE) {
1262 switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
1264 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1267 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1270 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1273 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
1276 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
1279 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
1282 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
1283 be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
1284 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1288 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1289 spin_unlock_irqrestore(shost->host_lock, flags);
1293 * ibmvfc_get_host_port_state - Get host port state
1294 * @shost: scsi host struct
1299 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
1301 struct ibmvfc_host *vhost = shost_priv(shost);
1302 unsigned long flags;
1304 spin_lock_irqsave(shost->host_lock, flags);
1305 switch (vhost->state) {
1306 case IBMVFC_INITIALIZING:
1308 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1310 case IBMVFC_LINK_DOWN:
1311 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1313 case IBMVFC_LINK_DEAD:
1314 case IBMVFC_HOST_OFFLINE:
1315 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1318 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1321 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1324 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
1325 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1328 spin_unlock_irqrestore(shost->host_lock, flags);
1332 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1333 * @rport: rport struct
1334 * @timeout: timeout value
1339 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1342 rport->dev_loss_tmo = timeout;
1344 rport->dev_loss_tmo = 1;
1348 * ibmvfc_release_tgt - Free memory allocated for a target
1349 * @kref: kref struct
1352 static void ibmvfc_release_tgt(struct kref *kref)
1354 struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1359 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1360 * @starget: scsi target struct
1365 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1367 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1368 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1370 kref_put(&tgt->kref, ibmvfc_release_tgt);
1374 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1375 * @starget: scsi target struct
1380 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1382 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1383 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1385 kref_put(&tgt->kref, ibmvfc_release_tgt);
1389 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1390 * @starget: scsi target struct
1395 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1397 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1398 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1400 kref_put(&tgt->kref, ibmvfc_release_tgt);
1404 * ibmvfc_wait_while_resetting - Wait while the host resets
1405 * @vhost: ibmvfc host struct
1408 * 0 on success / other on failure
1410 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1412 long timeout = wait_event_timeout(vhost->init_wait_q,
1413 ((vhost->state == IBMVFC_ACTIVE ||
1414 vhost->state == IBMVFC_HOST_OFFLINE ||
1415 vhost->state == IBMVFC_LINK_DEAD) &&
1416 vhost->action == IBMVFC_HOST_ACTION_NONE),
1417 (init_timeout * HZ));
1419 return timeout ? 0 : -EIO;
1423 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1424 * @shost: scsi host struct
1427 * 0 on success / other on failure
1429 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1431 struct ibmvfc_host *vhost = shost_priv(shost);
1433 dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1434 ibmvfc_reset_host(vhost);
1435 return ibmvfc_wait_while_resetting(vhost);
1439 * ibmvfc_gather_partition_info - Gather info about the LPAR
1440 * @vhost: ibmvfc host struct
1445 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1447 struct device_node *rootdn;
1449 const unsigned int *num;
1451 rootdn = of_find_node_by_path("/");
1455 name = of_get_property(rootdn, "ibm,partition-name", NULL);
1457 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1458 num = of_get_property(rootdn, "ibm,partition-no", NULL);
1460 vhost->partition_number = *num;
1461 of_node_put(rootdn);
1465 * ibmvfc_set_login_info - Setup info for NPIV login
1466 * @vhost: ibmvfc host struct
1471 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1473 struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1474 struct ibmvfc_queue *async_crq = &vhost->async_crq;
1475 struct device_node *of_node = vhost->dev->of_node;
1476 const char *location;
1478 memset(login_info, 0, sizeof(*login_info));
1480 login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
1481 login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
1482 login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
1483 login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
1484 login_info->partition_num = cpu_to_be32(vhost->partition_number);
1485 login_info->vfc_frame_version = cpu_to_be32(1);
1486 login_info->fcp_version = cpu_to_be16(3);
1487 login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
1488 if (vhost->client_migrated)
1489 login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
1491 login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
1492 login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE | IBMVFC_CAN_SEND_VF_WWPN);
1494 if (vhost->mq_enabled || vhost->using_channels)
1495 login_info->capabilities |= cpu_to_be64(IBMVFC_CAN_USE_CHANNELS);
1497 login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
1498 login_info->async.len = cpu_to_be32(async_crq->size *
1499 sizeof(*async_crq->msgs.async));
1500 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1501 strncpy(login_info->device_name,
1502 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
1504 location = of_get_property(of_node, "ibm,loc-code", NULL);
1505 location = location ? location : dev_name(vhost->dev);
1506 strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1510 * ibmvfc_get_event - Gets the next free event in pool
1511 * @queue: ibmvfc queue struct
1513 * Returns a free event from the pool.
1515 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_queue *queue)
1517 struct ibmvfc_event *evt;
1518 unsigned long flags;
1520 spin_lock_irqsave(&queue->l_lock, flags);
1521 if (list_empty(&queue->free)) {
1522 ibmvfc_log(queue->vhost, 4, "empty event pool on queue:%ld\n", queue->hwq_id);
1523 spin_unlock_irqrestore(&queue->l_lock, flags);
1526 evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
1527 atomic_set(&evt->free, 0);
1528 list_del(&evt->queue_list);
1529 spin_unlock_irqrestore(&queue->l_lock, flags);
1534 * ibmvfc_locked_done - Calls evt completion with host_lock held
1535 * @evt: ibmvfc evt to complete
1537 * All non-scsi command completion callbacks have the expectation that the
1538 * host_lock is held. This callback is used by ibmvfc_init_event to wrap a
1539 * MAD evt with the host_lock.
1541 static void ibmvfc_locked_done(struct ibmvfc_event *evt)
1543 unsigned long flags;
1545 spin_lock_irqsave(evt->vhost->host->host_lock, flags);
1547 spin_unlock_irqrestore(evt->vhost->host->host_lock, flags);
1551 * ibmvfc_init_event - Initialize fields in an event struct that are always
1554 * @done: Routine to call when the event is responded to
1555 * @format: SRP or MAD format
1557 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1558 void (*done) (struct ibmvfc_event *), u8 format)
1561 evt->sync_iu = NULL;
1562 evt->eh_comp = NULL;
1563 evt->crq.format = format;
1564 if (format == IBMVFC_CMD_FORMAT)
1568 evt->done = ibmvfc_locked_done;
1574 * ibmvfc_map_sg_list - Initialize scatterlist
1575 * @scmd: scsi command struct
1576 * @nseg: number of scatterlist segments
1577 * @md: memory descriptor list to initialize
1579 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1580 struct srp_direct_buf *md)
1583 struct scatterlist *sg;
1585 scsi_for_each_sg(scmd, sg, nseg, i) {
1586 md[i].va = cpu_to_be64(sg_dma_address(sg));
1587 md[i].len = cpu_to_be32(sg_dma_len(sg));
1593 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
1594 * @scmd: struct scsi_cmnd with the scatterlist
1595 * @evt: ibmvfc event struct
1596 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1597 * @dev: device for which to map dma memory
1600 * 0 on success / non-zero on failure
1602 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1603 struct ibmvfc_event *evt,
1604 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1608 struct srp_direct_buf *data = &vfc_cmd->ioba;
1609 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1610 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(evt->vhost, vfc_cmd);
1613 vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1615 sg_mapped = scsi_dma_map(scmd);
1617 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1619 } else if (unlikely(sg_mapped < 0)) {
1620 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1621 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1625 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1626 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1627 iu->add_cdb_len |= IBMVFC_WRDATA;
1629 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1630 iu->add_cdb_len |= IBMVFC_RDDATA;
1633 if (sg_mapped == 1) {
1634 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1638 vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1640 if (!evt->ext_list) {
1641 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1642 &evt->ext_list_token);
1644 if (!evt->ext_list) {
1645 scsi_dma_unmap(scmd);
1646 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1647 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1652 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1654 data->va = cpu_to_be64(evt->ext_list_token);
1655 data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1661 * ibmvfc_timeout - Internal command timeout handler
1662 * @t: struct ibmvfc_event that timed out
1664 * Called when an internally generated command times out
1666 static void ibmvfc_timeout(struct timer_list *t)
1668 struct ibmvfc_event *evt = from_timer(evt, t, timer);
1669 struct ibmvfc_host *vhost = evt->vhost;
1670 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1671 ibmvfc_reset_host(vhost);
1675 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1676 * @evt: event to be sent
1677 * @vhost: ibmvfc host struct
1678 * @timeout: timeout in seconds - 0 means do not time command
1680 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1682 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1683 struct ibmvfc_host *vhost, unsigned long timeout)
1685 __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1686 unsigned long flags;
1689 /* Copy the IU into the transfer area */
1690 *evt->xfer_iu = evt->iu;
1691 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1692 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1693 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1694 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1698 timer_setup(&evt->timer, ibmvfc_timeout, 0);
1701 evt->timer.expires = jiffies + (timeout * HZ);
1702 add_timer(&evt->timer);
1705 spin_lock_irqsave(&evt->queue->l_lock, flags);
1706 list_add_tail(&evt->queue_list, &evt->queue->sent);
1707 atomic_set(&evt->active, 1);
1711 if (evt->queue->fmt == IBMVFC_SUB_CRQ_FMT)
1712 rc = ibmvfc_send_sub_crq(vhost,
1713 evt->queue->vios_cookie,
1714 be64_to_cpu(crq_as_u64[0]),
1715 be64_to_cpu(crq_as_u64[1]),
1718 rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1719 be64_to_cpu(crq_as_u64[1]));
1722 atomic_set(&evt->active, 0);
1723 list_del(&evt->queue_list);
1724 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1725 del_timer(&evt->timer);
1727 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1728 * Firmware will send a CRQ with a transport event (0xFF) to
1729 * tell this client what has happened to the transport. This
1730 * will be handled in ibmvfc_handle_crq()
1732 if (rc == H_CLOSED) {
1733 if (printk_ratelimit())
1734 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1736 scsi_dma_unmap(evt->cmnd);
1737 ibmvfc_free_event(evt);
1738 return SCSI_MLQUEUE_HOST_BUSY;
1741 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1743 evt->cmnd->result = DID_ERROR << 16;
1744 evt->done = ibmvfc_scsi_eh_done;
1746 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1750 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1751 ibmvfc_trc_start(evt);
1758 * ibmvfc_log_error - Log an error for the failed command if appropriate
1759 * @evt: ibmvfc event to log
1762 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1764 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1765 struct ibmvfc_host *vhost = evt->vhost;
1766 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
1767 struct scsi_cmnd *cmnd = evt->cmnd;
1768 const char *err = unknown_error;
1769 int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1774 logerr = cmd_status[index].log;
1775 err = cmd_status[index].name;
1778 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1781 if (rsp->flags & FCP_RSP_LEN_VALID)
1782 rsp_code = rsp->data.info.rsp_code;
1784 scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
1785 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1786 cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
1787 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1791 * ibmvfc_relogin - Log back into the specified device
1792 * @sdev: scsi device struct
1795 static void ibmvfc_relogin(struct scsi_device *sdev)
1797 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1798 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1799 struct ibmvfc_target *tgt;
1800 unsigned long flags;
1802 spin_lock_irqsave(vhost->host->host_lock, flags);
1803 list_for_each_entry(tgt, &vhost->targets, queue) {
1804 if (rport == tgt->rport) {
1805 ibmvfc_del_tgt(tgt);
1810 ibmvfc_reinit_host(vhost);
1811 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1815 * ibmvfc_scsi_done - Handle responses from commands
1816 * @evt: ibmvfc event to be handled
1818 * Used as a callback when sending scsi cmds.
1820 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1822 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1823 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(evt->vhost, vfc_cmd);
1824 struct scsi_cmnd *cmnd = evt->cmnd;
1826 u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1829 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1830 scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1831 else if (rsp->flags & FCP_RESID_UNDER)
1832 scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1834 scsi_set_resid(cmnd, 0);
1836 if (vfc_cmd->status) {
1837 cmnd->result = ibmvfc_get_err_result(evt->vhost, vfc_cmd);
1839 if (rsp->flags & FCP_RSP_LEN_VALID)
1840 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1841 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1842 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1843 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1844 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1845 if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1846 (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1847 ibmvfc_relogin(cmnd->device);
1849 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1850 cmnd->result = (DID_ERROR << 16);
1852 ibmvfc_log_error(evt);
1855 if (!cmnd->result &&
1856 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1857 cmnd->result = (DID_ERROR << 16);
1859 scsi_dma_unmap(cmnd);
1863 ibmvfc_free_event(evt);
1867 * ibmvfc_host_chkready - Check if the host can accept commands
1868 * @vhost: struct ibmvfc host
1871 * 1 if host can accept command / 0 if not
1873 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1877 switch (vhost->state) {
1878 case IBMVFC_LINK_DEAD:
1879 case IBMVFC_HOST_OFFLINE:
1880 result = DID_NO_CONNECT << 16;
1883 case IBMVFC_INITIALIZING:
1885 case IBMVFC_LINK_DOWN:
1886 result = DID_REQUEUE << 16;
1896 static struct ibmvfc_cmd *ibmvfc_init_vfc_cmd(struct ibmvfc_event *evt, struct scsi_device *sdev)
1898 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1899 struct ibmvfc_host *vhost = evt->vhost;
1900 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
1901 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
1902 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
1905 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1906 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
1907 offset = offsetof(struct ibmvfc_cmd, v2.rsp);
1908 vfc_cmd->target_wwpn = cpu_to_be64(rport->port_name);
1910 offset = offsetof(struct ibmvfc_cmd, v1.rsp);
1911 vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offset);
1912 vfc_cmd->resp.len = cpu_to_be32(sizeof(*rsp));
1913 vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1914 vfc_cmd->payload_len = cpu_to_be32(sizeof(*iu));
1915 vfc_cmd->resp_len = cpu_to_be32(sizeof(*rsp));
1916 vfc_cmd->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
1917 vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1918 int_to_scsilun(sdev->lun, &iu->lun);
1924 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1925 * @shost: scsi host struct
1926 * @cmnd: struct scsi_cmnd to be executed
1929 * 0 on success / other on failure
1931 static int ibmvfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
1933 struct ibmvfc_host *vhost = shost_priv(shost);
1934 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1935 struct ibmvfc_cmd *vfc_cmd;
1936 struct ibmvfc_fcp_cmd_iu *iu;
1937 struct ibmvfc_event *evt;
1938 u32 tag_and_hwq = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
1939 u16 hwq = blk_mq_unique_tag_to_hwq(tag_and_hwq);
1943 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1944 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1950 cmnd->result = (DID_OK << 16);
1951 if (vhost->using_channels) {
1952 scsi_channel = hwq % vhost->scsi_scrqs.active_queues;
1953 evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[scsi_channel]);
1955 return SCSI_MLQUEUE_HOST_BUSY;
1957 evt->hwq = hwq % vhost->scsi_scrqs.active_queues;
1959 evt = ibmvfc_get_event(&vhost->crq);
1961 return SCSI_MLQUEUE_HOST_BUSY;
1964 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1967 vfc_cmd = ibmvfc_init_vfc_cmd(evt, cmnd->device);
1968 iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
1970 iu->xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1971 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
1973 if (cmnd->flags & SCMD_TAGGED) {
1974 vfc_cmd->task_tag = cpu_to_be64(scsi_cmd_to_rq(cmnd)->tag);
1975 iu->pri_task_attr = IBMVFC_SIMPLE_TASK;
1978 vfc_cmd->correlation = cpu_to_be64((u64)evt);
1980 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1981 return ibmvfc_send_event(evt, vhost, 0);
1983 ibmvfc_free_event(evt);
1985 return SCSI_MLQUEUE_HOST_BUSY;
1987 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1988 scmd_printk(KERN_ERR, cmnd,
1989 "Failed to map DMA buffer for command. rc=%d\n", rc);
1991 cmnd->result = DID_ERROR << 16;
1997 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1998 * @evt: ibmvfc event struct
2001 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
2003 /* copy the response back */
2005 *evt->sync_iu = *evt->xfer_iu;
2007 complete(&evt->comp);
2011 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
2012 * @evt: struct ibmvfc_event
2015 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
2017 struct ibmvfc_host *vhost = evt->vhost;
2019 ibmvfc_free_event(evt);
2020 vhost->aborting_passthru = 0;
2021 dev_info(vhost->dev, "Passthru command cancelled\n");
2025 * ibmvfc_bsg_timeout - Handle a BSG timeout
2026 * @job: struct bsg_job that timed out
2029 * 0 on success / other on failure
2031 static int ibmvfc_bsg_timeout(struct bsg_job *job)
2033 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2034 unsigned long port_id = (unsigned long)job->dd_data;
2035 struct ibmvfc_event *evt;
2036 struct ibmvfc_tmf *tmf;
2037 unsigned long flags;
2041 spin_lock_irqsave(vhost->host->host_lock, flags);
2042 if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
2043 __ibmvfc_reset_host(vhost);
2044 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2048 vhost->aborting_passthru = 1;
2049 evt = ibmvfc_get_event(&vhost->crq);
2051 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2055 ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
2058 memset(tmf, 0, sizeof(*tmf));
2059 tmf->common.version = cpu_to_be32(1);
2060 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2061 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2062 tmf->scsi_id = cpu_to_be64(port_id);
2063 tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2064 tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
2065 rc = ibmvfc_send_event(evt, vhost, default_timeout);
2068 vhost->aborting_passthru = 0;
2069 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
2072 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
2075 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2082 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
2083 * @vhost: struct ibmvfc_host to send command
2084 * @port_id: port ID to send command
2087 * 0 on success / other on failure
2089 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
2091 struct ibmvfc_port_login *plogi;
2092 struct ibmvfc_target *tgt;
2093 struct ibmvfc_event *evt;
2094 union ibmvfc_iu rsp_iu;
2095 unsigned long flags;
2096 int rc = 0, issue_login = 1;
2099 spin_lock_irqsave(vhost->host->host_lock, flags);
2100 list_for_each_entry(tgt, &vhost->targets, queue) {
2101 if (tgt->scsi_id == port_id) {
2109 if (unlikely((rc = ibmvfc_host_chkready(vhost))))
2112 evt = ibmvfc_get_event(&vhost->crq);
2117 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2118 plogi = &evt->iu.plogi;
2119 memset(plogi, 0, sizeof(*plogi));
2120 plogi->common.version = cpu_to_be32(1);
2121 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
2122 plogi->common.length = cpu_to_be16(sizeof(*plogi));
2123 plogi->scsi_id = cpu_to_be64(port_id);
2124 evt->sync_iu = &rsp_iu;
2125 init_completion(&evt->comp);
2127 rc = ibmvfc_send_event(evt, vhost, default_timeout);
2128 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2133 wait_for_completion(&evt->comp);
2135 if (rsp_iu.plogi.common.status)
2138 spin_lock_irqsave(vhost->host->host_lock, flags);
2139 ibmvfc_free_event(evt);
2141 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2147 * ibmvfc_bsg_request - Handle a BSG request
2148 * @job: struct bsg_job to be executed
2151 * 0 on success / other on failure
2153 static int ibmvfc_bsg_request(struct bsg_job *job)
2155 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2156 struct fc_rport *rport = fc_bsg_to_rport(job);
2157 struct ibmvfc_passthru_mad *mad;
2158 struct ibmvfc_event *evt;
2159 union ibmvfc_iu rsp_iu;
2160 unsigned long flags, port_id = -1;
2161 struct fc_bsg_request *bsg_request = job->request;
2162 struct fc_bsg_reply *bsg_reply = job->reply;
2163 unsigned int code = bsg_request->msgcode;
2164 int rc = 0, req_seg, rsp_seg, issue_login = 0;
2165 u32 fc_flags, rsp_len;
2168 bsg_reply->reply_payload_rcv_len = 0;
2170 port_id = rport->port_id;
2173 case FC_BSG_HST_ELS_NOLOGIN:
2174 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
2175 (bsg_request->rqst_data.h_els.port_id[1] << 8) |
2176 bsg_request->rqst_data.h_els.port_id[2];
2178 case FC_BSG_RPT_ELS:
2179 fc_flags = IBMVFC_FC_ELS;
2183 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
2184 (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
2185 bsg_request->rqst_data.h_ct.port_id[2];
2188 fc_flags = IBMVFC_FC_CT_IU;
2196 if (!mutex_trylock(&vhost->passthru_mutex))
2199 job->dd_data = (void *)port_id;
2200 req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
2201 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2204 mutex_unlock(&vhost->passthru_mutex);
2208 rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
2209 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2212 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2213 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2214 mutex_unlock(&vhost->passthru_mutex);
2218 if (req_seg > 1 || rsp_seg > 1) {
2224 rc = ibmvfc_bsg_plogi(vhost, port_id);
2226 spin_lock_irqsave(vhost->host->host_lock, flags);
2228 if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
2229 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
2230 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2234 evt = ibmvfc_get_event(&vhost->crq);
2236 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2240 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2241 mad = &evt->iu.passthru;
2243 memset(mad, 0, sizeof(*mad));
2244 mad->common.version = cpu_to_be32(1);
2245 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
2246 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
2248 mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
2249 offsetof(struct ibmvfc_passthru_mad, iu));
2250 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
2252 mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
2253 mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
2254 mad->iu.flags = cpu_to_be32(fc_flags);
2255 mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2257 mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
2258 mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
2259 mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
2260 mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
2261 mad->iu.scsi_id = cpu_to_be64(port_id);
2262 mad->iu.tag = cpu_to_be64((u64)evt);
2263 rsp_len = be32_to_cpu(mad->iu.rsp.len);
2265 evt->sync_iu = &rsp_iu;
2266 init_completion(&evt->comp);
2267 rc = ibmvfc_send_event(evt, vhost, 0);
2268 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2275 wait_for_completion(&evt->comp);
2277 if (rsp_iu.passthru.common.status)
2280 bsg_reply->reply_payload_rcv_len = rsp_len;
2282 spin_lock_irqsave(vhost->host->host_lock, flags);
2283 ibmvfc_free_event(evt);
2284 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2285 bsg_reply->result = rc;
2286 bsg_job_done(job, bsg_reply->result,
2287 bsg_reply->reply_payload_rcv_len);
2290 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2291 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2292 dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
2293 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2294 mutex_unlock(&vhost->passthru_mutex);
2300 * ibmvfc_reset_device - Reset the device with the specified reset type
2301 * @sdev: scsi device to reset
2303 * @desc: reset type description for log messages
2306 * 0 on success / other on failure
2308 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
2310 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2311 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2312 struct ibmvfc_cmd *tmf;
2313 struct ibmvfc_event *evt = NULL;
2314 union ibmvfc_iu rsp_iu;
2315 struct ibmvfc_fcp_cmd_iu *iu;
2316 struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2317 int rsp_rc = -EBUSY;
2318 unsigned long flags;
2321 spin_lock_irqsave(vhost->host->host_lock, flags);
2322 if (vhost->state == IBMVFC_ACTIVE) {
2323 if (vhost->using_channels)
2324 evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[0]);
2326 evt = ibmvfc_get_event(&vhost->crq);
2329 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2333 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2334 tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2335 iu = ibmvfc_get_fcp_iu(vhost, tmf);
2337 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2338 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2339 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2340 iu->tmf_flags = type;
2341 evt->sync_iu = &rsp_iu;
2343 init_completion(&evt->comp);
2344 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2346 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2349 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2354 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2355 wait_for_completion(&evt->comp);
2357 if (rsp_iu.cmd.status)
2358 rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2361 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2362 rsp_code = fc_rsp->data.info.rsp_code;
2364 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2365 "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2366 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2367 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2368 fc_rsp->scsi_status);
2371 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2373 spin_lock_irqsave(vhost->host->host_lock, flags);
2374 ibmvfc_free_event(evt);
2375 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2380 * ibmvfc_match_rport - Match function for specified remote port
2381 * @evt: ibmvfc event struct
2382 * @rport: device to match
2385 * 1 if event matches rport / 0 if event does not match rport
2387 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2389 struct fc_rport *cmd_rport;
2392 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2393 if (cmd_rport == rport)
2400 * ibmvfc_match_target - Match function for specified target
2401 * @evt: ibmvfc event struct
2402 * @device: device to match (starget)
2405 * 1 if event matches starget / 0 if event does not match starget
2407 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2409 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2415 * ibmvfc_match_lun - Match function for specified LUN
2416 * @evt: ibmvfc event struct
2417 * @device: device to match (sdev)
2420 * 1 if event matches sdev / 0 if event does not match sdev
2422 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2424 if (evt->cmnd && evt->cmnd->device == device)
2430 * ibmvfc_event_is_free - Check if event is free or not
2431 * @evt: ibmvfc event struct
2436 static bool ibmvfc_event_is_free(struct ibmvfc_event *evt)
2438 struct ibmvfc_event *loop_evt;
2440 list_for_each_entry(loop_evt, &evt->queue->free, queue_list)
2441 if (loop_evt == evt)
2448 * ibmvfc_wait_for_ops - Wait for ops to complete
2449 * @vhost: ibmvfc host struct
2450 * @device: device to match (starget or sdev)
2451 * @match: match function
2456 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2457 int (*match) (struct ibmvfc_event *, void *))
2459 struct ibmvfc_event *evt;
2460 DECLARE_COMPLETION_ONSTACK(comp);
2461 int wait, i, q_index, q_size;
2462 unsigned long flags;
2463 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2464 struct ibmvfc_queue *queues;
2467 if (vhost->mq_enabled && vhost->using_channels) {
2468 queues = vhost->scsi_scrqs.scrqs;
2469 q_size = vhost->scsi_scrqs.active_queues;
2471 queues = &vhost->crq;
2477 spin_lock_irqsave(vhost->host->host_lock, flags);
2478 for (q_index = 0; q_index < q_size; q_index++) {
2479 spin_lock(&queues[q_index].l_lock);
2480 for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2481 evt = &queues[q_index].evt_pool.events[i];
2482 if (!ibmvfc_event_is_free(evt)) {
2483 if (match(evt, device)) {
2484 evt->eh_comp = ∁
2489 spin_unlock(&queues[q_index].l_lock);
2491 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2494 timeout = wait_for_completion_timeout(&comp, timeout);
2498 spin_lock_irqsave(vhost->host->host_lock, flags);
2499 for (q_index = 0; q_index < q_size; q_index++) {
2500 spin_lock(&queues[q_index].l_lock);
2501 for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2502 evt = &queues[q_index].evt_pool.events[i];
2503 if (!ibmvfc_event_is_free(evt)) {
2504 if (match(evt, device)) {
2505 evt->eh_comp = NULL;
2510 spin_unlock(&queues[q_index].l_lock);
2512 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2514 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2516 return wait ? FAILED : SUCCESS;
2525 static struct ibmvfc_event *ibmvfc_init_tmf(struct ibmvfc_queue *queue,
2526 struct scsi_device *sdev,
2529 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2530 struct scsi_target *starget = scsi_target(sdev);
2531 struct fc_rport *rport = starget_to_rport(starget);
2532 struct ibmvfc_event *evt;
2533 struct ibmvfc_tmf *tmf;
2535 evt = ibmvfc_get_event(queue);
2538 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2541 memset(tmf, 0, sizeof(*tmf));
2542 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
2543 tmf->common.version = cpu_to_be32(2);
2544 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2546 tmf->common.version = cpu_to_be32(1);
2548 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2549 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2550 tmf->scsi_id = cpu_to_be64(rport->port_id);
2551 int_to_scsilun(sdev->lun, &tmf->lun);
2552 if (!ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPRESS_ABTS))
2553 type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2554 if (vhost->state == IBMVFC_ACTIVE)
2555 tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2557 tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2558 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2559 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2561 init_completion(&evt->comp);
2566 static int ibmvfc_cancel_all_mq(struct scsi_device *sdev, int type)
2568 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2569 struct ibmvfc_event *evt, *found_evt, *temp;
2570 struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
2571 unsigned long flags;
2578 spin_lock_irqsave(vhost->host->host_lock, flags);
2579 num_hwq = vhost->scsi_scrqs.active_queues;
2580 for (i = 0; i < num_hwq; i++) {
2581 spin_lock(queues[i].q_lock);
2582 spin_lock(&queues[i].l_lock);
2584 list_for_each_entry(evt, &queues[i].sent, queue_list) {
2585 if (evt->cmnd && evt->cmnd->device == sdev) {
2590 spin_unlock(&queues[i].l_lock);
2592 if (found_evt && vhost->logged_in) {
2593 evt = ibmvfc_init_tmf(&queues[i], sdev, type);
2595 spin_unlock(queues[i].q_lock);
2596 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2599 evt->sync_iu = &queues[i].cancel_rsp;
2600 ibmvfc_send_event(evt, vhost, default_timeout);
2601 list_add_tail(&evt->cancel, &cancelq);
2604 spin_unlock(queues[i].q_lock);
2606 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2608 if (list_empty(&cancelq)) {
2609 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2610 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2614 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2616 list_for_each_entry_safe(evt, temp, &cancelq, cancel) {
2617 wait_for_completion(&evt->comp);
2618 status = be16_to_cpu(evt->queue->cancel_rsp.mad_common.status);
2619 list_del(&evt->cancel);
2620 ibmvfc_free_event(evt);
2622 if (status != IBMVFC_MAD_SUCCESS) {
2623 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2625 case IBMVFC_MAD_DRIVER_FAILED:
2626 case IBMVFC_MAD_CRQ_ERROR:
2627 /* Host adapter most likely going through reset, return success to
2628 * the caller will wait for the command being cancelled to get returned
2641 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2646 static int ibmvfc_cancel_all_sq(struct scsi_device *sdev, int type)
2648 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2649 struct ibmvfc_event *evt, *found_evt;
2650 union ibmvfc_iu rsp;
2651 int rsp_rc = -EBUSY;
2652 unsigned long flags;
2657 spin_lock_irqsave(vhost->host->host_lock, flags);
2658 spin_lock(&vhost->crq.l_lock);
2659 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2660 if (evt->cmnd && evt->cmnd->device == sdev) {
2665 spin_unlock(&vhost->crq.l_lock);
2668 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2669 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2670 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2674 if (vhost->logged_in) {
2675 evt = ibmvfc_init_tmf(&vhost->crq, sdev, type);
2676 evt->sync_iu = &rsp;
2677 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2680 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2683 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2684 /* If failure is received, the host adapter is most likely going
2685 through reset, return success so the caller will wait for the command
2686 being cancelled to get returned */
2690 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2692 wait_for_completion(&evt->comp);
2693 status = be16_to_cpu(rsp.mad_common.status);
2694 spin_lock_irqsave(vhost->host->host_lock, flags);
2695 ibmvfc_free_event(evt);
2696 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2698 if (status != IBMVFC_MAD_SUCCESS) {
2699 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2701 case IBMVFC_MAD_DRIVER_FAILED:
2702 case IBMVFC_MAD_CRQ_ERROR:
2703 /* Host adapter most likely going through reset, return success to
2704 the caller will wait for the command being cancelled to get returned */
2711 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2716 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2717 * @sdev: scsi device to cancel commands
2718 * @type: type of error recovery being performed
2720 * This sends a cancel to the VIOS for the specified device. This does
2721 * NOT send any abort to the actual device. That must be done separately.
2724 * 0 on success / other on failure
2726 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2728 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2730 if (vhost->mq_enabled && vhost->using_channels)
2731 return ibmvfc_cancel_all_mq(sdev, type);
2733 return ibmvfc_cancel_all_sq(sdev, type);
2737 * ibmvfc_match_key - Match function for specified cancel key
2738 * @evt: ibmvfc event struct
2739 * @key: cancel key to match
2742 * 1 if event matches key / 0 if event does not match key
2744 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2746 unsigned long cancel_key = (unsigned long)key;
2748 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2749 be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2755 * ibmvfc_match_evt - Match function for specified event
2756 * @evt: ibmvfc event struct
2757 * @match: event to match
2760 * 1 if event matches key / 0 if event does not match key
2762 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2770 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2771 * @sdev: scsi device to abort commands
2773 * This sends an Abort Task Set to the VIOS for the specified device. This does
2774 * NOT send any cancel to the VIOS. That must be done separately.
2777 * 0 on success / other on failure
2779 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2781 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2782 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2783 struct ibmvfc_cmd *tmf;
2784 struct ibmvfc_event *evt, *found_evt;
2785 union ibmvfc_iu rsp_iu;
2786 struct ibmvfc_fcp_cmd_iu *iu;
2787 struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2788 int rc, rsp_rc = -EBUSY;
2789 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2793 spin_lock_irqsave(vhost->host->host_lock, flags);
2794 spin_lock(&vhost->crq.l_lock);
2795 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2796 if (evt->cmnd && evt->cmnd->device == sdev) {
2801 spin_unlock(&vhost->crq.l_lock);
2804 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2805 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2806 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2810 if (vhost->state == IBMVFC_ACTIVE) {
2811 evt = ibmvfc_get_event(&vhost->crq);
2813 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2816 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2817 tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2818 iu = ibmvfc_get_fcp_iu(vhost, tmf);
2820 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2821 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2822 iu->tmf_flags = IBMVFC_ABORT_TASK_SET;
2823 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2824 evt->sync_iu = &rsp_iu;
2826 tmf->correlation = cpu_to_be64((u64)evt);
2828 init_completion(&evt->comp);
2829 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2832 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2835 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2839 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2840 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2843 rc = ibmvfc_cancel_all(sdev, 0);
2845 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2851 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2852 ibmvfc_reset_host(vhost);
2854 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2859 rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2860 if (rc != SUCCESS) {
2861 spin_lock_irqsave(vhost->host->host_lock, flags);
2862 ibmvfc_hard_reset_host(vhost);
2863 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2871 if (rsp_iu.cmd.status)
2872 rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2875 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2876 rsp_code = fc_rsp->data.info.rsp_code;
2878 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2879 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2880 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2881 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2882 fc_rsp->scsi_status);
2885 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2888 spin_lock_irqsave(vhost->host->host_lock, flags);
2889 ibmvfc_free_event(evt);
2890 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2895 * ibmvfc_eh_abort_handler - Abort a command
2896 * @cmd: scsi command to abort
2899 * SUCCESS / FAST_IO_FAIL / FAILED
2901 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2903 struct scsi_device *sdev = cmd->device;
2904 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2905 int cancel_rc, block_rc;
2909 block_rc = fc_block_scsi_eh(cmd);
2910 ibmvfc_wait_while_resetting(vhost);
2911 if (block_rc != FAST_IO_FAIL) {
2912 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2913 ibmvfc_abort_task_set(sdev);
2915 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2918 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2920 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2928 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2929 * @cmd: scsi command struct
2932 * SUCCESS / FAST_IO_FAIL / FAILED
2934 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2936 struct scsi_device *sdev = cmd->device;
2937 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2938 int cancel_rc, block_rc, reset_rc = 0;
2942 block_rc = fc_block_scsi_eh(cmd);
2943 ibmvfc_wait_while_resetting(vhost);
2944 if (block_rc != FAST_IO_FAIL) {
2945 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2946 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2948 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2950 if (!cancel_rc && !reset_rc)
2951 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2953 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2961 * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2962 * @sdev: scsi device struct
2963 * @data: return code
2966 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2968 unsigned long *rc = data;
2969 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2973 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2974 * @sdev: scsi device struct
2975 * @data: return code
2978 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2980 unsigned long *rc = data;
2981 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2985 * ibmvfc_eh_target_reset_handler - Reset the target
2986 * @cmd: scsi command struct
2989 * SUCCESS / FAST_IO_FAIL / FAILED
2991 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2993 struct scsi_device *sdev = cmd->device;
2994 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2995 struct scsi_target *starget = scsi_target(sdev);
2999 unsigned long cancel_rc = 0;
3002 block_rc = fc_block_scsi_eh(cmd);
3003 ibmvfc_wait_while_resetting(vhost);
3004 if (block_rc != FAST_IO_FAIL) {
3005 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
3006 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
3008 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
3010 if (!cancel_rc && !reset_rc)
3011 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
3013 if (block_rc == FAST_IO_FAIL && rc != FAILED)
3021 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
3022 * @cmd: struct scsi_cmnd having problems
3025 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
3028 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
3030 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
3031 rc = ibmvfc_issue_fc_host_lip(vhost->host);
3033 return rc ? FAILED : SUCCESS;
3037 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
3038 * @rport: rport struct
3043 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
3045 struct Scsi_Host *shost = rport_to_shost(rport);
3046 struct ibmvfc_host *vhost = shost_priv(shost);
3047 struct fc_rport *dev_rport;
3048 struct scsi_device *sdev;
3049 struct ibmvfc_target *tgt;
3050 unsigned long rc, flags;
3054 shost_for_each_device(sdev, shost) {
3055 dev_rport = starget_to_rport(scsi_target(sdev));
3056 if (dev_rport != rport)
3058 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
3061 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
3064 ibmvfc_issue_fc_host_lip(shost);
3066 spin_lock_irqsave(shost->host_lock, flags);
3068 list_for_each_entry(tgt, &vhost->targets, queue) {
3069 if (tgt->scsi_id == rport->port_id) {
3075 if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
3077 * If we get here, that means we previously attempted to send
3078 * an implicit logout to the target but it failed, most likely
3079 * due to I/O being pending, so we need to send it again
3081 ibmvfc_del_tgt(tgt);
3082 ibmvfc_reinit_host(vhost);
3085 spin_unlock_irqrestore(shost->host_lock, flags);
3089 static const struct ibmvfc_async_desc ae_desc [] = {
3090 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3091 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3092 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3093 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3094 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3095 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
3096 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
3097 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
3098 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
3099 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
3100 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
3101 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
3102 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
3105 static const struct ibmvfc_async_desc unknown_ae = {
3106 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
3110 * ibmvfc_get_ae_desc - Get text description for async event
3114 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
3118 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
3119 if (ae_desc[i].ae == ae)
3125 static const struct {
3126 enum ibmvfc_ae_link_state state;
3129 { IBMVFC_AE_LS_LINK_UP, " link up" },
3130 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
3131 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
3132 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
3136 * ibmvfc_get_link_state - Get text description for link state
3137 * @state: link state
3140 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
3144 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
3145 if (link_desc[i].state == state)
3146 return link_desc[i].desc;
3152 * ibmvfc_handle_async - Handle an async event from the adapter
3153 * @crq: crq to process
3154 * @vhost: ibmvfc host struct
3157 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
3158 struct ibmvfc_host *vhost)
3160 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
3161 struct ibmvfc_target *tgt;
3163 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
3164 " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
3165 be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
3166 ibmvfc_get_link_state(crq->link_state));
3168 switch (be64_to_cpu(crq->event)) {
3169 case IBMVFC_AE_RESUME:
3170 switch (crq->link_state) {
3171 case IBMVFC_AE_LS_LINK_DOWN:
3172 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3174 case IBMVFC_AE_LS_LINK_DEAD:
3175 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3177 case IBMVFC_AE_LS_LINK_UP:
3178 case IBMVFC_AE_LS_LINK_BOUNCED:
3180 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3181 vhost->delay_init = 1;
3182 __ibmvfc_reset_host(vhost);
3187 case IBMVFC_AE_LINK_UP:
3188 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3189 vhost->delay_init = 1;
3190 __ibmvfc_reset_host(vhost);
3192 case IBMVFC_AE_SCN_FABRIC:
3193 case IBMVFC_AE_SCN_DOMAIN:
3194 vhost->events_to_log |= IBMVFC_AE_RSCN;
3195 if (vhost->state < IBMVFC_HALTED) {
3196 vhost->delay_init = 1;
3197 __ibmvfc_reset_host(vhost);
3200 case IBMVFC_AE_SCN_NPORT:
3201 case IBMVFC_AE_SCN_GROUP:
3202 vhost->events_to_log |= IBMVFC_AE_RSCN;
3203 ibmvfc_reinit_host(vhost);
3205 case IBMVFC_AE_ELS_LOGO:
3206 case IBMVFC_AE_ELS_PRLO:
3207 case IBMVFC_AE_ELS_PLOGI:
3208 list_for_each_entry(tgt, &vhost->targets, queue) {
3209 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
3211 if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
3213 if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
3215 if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
3217 if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
3219 if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
3220 ibmvfc_del_tgt(tgt);
3221 ibmvfc_reinit_host(vhost);
3225 case IBMVFC_AE_LINK_DOWN:
3226 case IBMVFC_AE_ADAPTER_FAILED:
3227 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3229 case IBMVFC_AE_LINK_DEAD:
3230 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3232 case IBMVFC_AE_HALT:
3233 ibmvfc_link_down(vhost, IBMVFC_HALTED);
3236 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
3242 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
3243 * @crq: Command/Response queue
3244 * @vhost: ibmvfc host struct
3245 * @evt_doneq: Event done queue
3248 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3249 struct list_head *evt_doneq)
3252 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3254 switch (crq->valid) {
3255 case IBMVFC_CRQ_INIT_RSP:
3256 switch (crq->format) {
3257 case IBMVFC_CRQ_INIT:
3258 dev_info(vhost->dev, "Partner initialized\n");
3259 /* Send back a response */
3260 rc = ibmvfc_send_crq_init_complete(vhost);
3262 ibmvfc_init_host(vhost);
3264 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
3266 case IBMVFC_CRQ_INIT_COMPLETE:
3267 dev_info(vhost->dev, "Partner initialization complete\n");
3268 ibmvfc_init_host(vhost);
3271 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
3274 case IBMVFC_CRQ_XPORT_EVENT:
3275 vhost->state = IBMVFC_NO_CRQ;
3276 vhost->logged_in = 0;
3277 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3278 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
3279 /* We need to re-setup the interpartition connection */
3280 dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
3281 vhost->client_migrated = 1;
3283 scsi_block_requests(vhost->host);
3284 ibmvfc_purge_requests(vhost, DID_REQUEUE);
3285 ibmvfc_set_host_state(vhost, IBMVFC_LINK_DOWN);
3286 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
3287 wake_up(&vhost->work_wait_q);
3288 } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
3289 dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
3290 ibmvfc_purge_requests(vhost, DID_ERROR);
3291 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3292 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
3294 dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
3297 case IBMVFC_CRQ_CMD_RSP:
3300 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
3304 if (crq->format == IBMVFC_ASYNC_EVENT)
3307 /* The only kind of payload CRQs we should get are responses to
3308 * things we send. Make sure this response is to something we
3311 if (unlikely(!ibmvfc_valid_event(&vhost->crq.evt_pool, evt))) {
3312 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3317 if (unlikely(atomic_dec_if_positive(&evt->active))) {
3318 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3323 spin_lock(&evt->queue->l_lock);
3324 list_move_tail(&evt->queue_list, evt_doneq);
3325 spin_unlock(&evt->queue->l_lock);
3329 * ibmvfc_scan_finished - Check if the device scan is done.
3330 * @shost: scsi host struct
3331 * @time: current elapsed time
3334 * 0 if scan is not done / 1 if scan is done
3336 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3338 unsigned long flags;
3339 struct ibmvfc_host *vhost = shost_priv(shost);
3342 spin_lock_irqsave(shost->host_lock, flags);
3343 if (!vhost->scan_timeout)
3345 else if (time >= (vhost->scan_timeout * HZ)) {
3346 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
3347 "continuing initialization\n", vhost->scan_timeout);
3351 if (vhost->scan_complete) {
3352 vhost->scan_timeout = init_timeout;
3355 spin_unlock_irqrestore(shost->host_lock, flags);
3360 * ibmvfc_slave_alloc - Setup the device's task set value
3361 * @sdev: struct scsi_device device to configure
3363 * Set the device's task set value so that error handling works as
3367 * 0 on success / -ENXIO if device does not exist
3369 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
3371 struct Scsi_Host *shost = sdev->host;
3372 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3373 struct ibmvfc_host *vhost = shost_priv(shost);
3374 unsigned long flags = 0;
3376 if (!rport || fc_remote_port_chkready(rport))
3379 spin_lock_irqsave(shost->host_lock, flags);
3380 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
3381 spin_unlock_irqrestore(shost->host_lock, flags);
3386 * ibmvfc_target_alloc - Setup the target's task set value
3387 * @starget: struct scsi_target
3389 * Set the target's task set value so that error handling works as
3393 * 0 on success / -ENXIO if device does not exist
3395 static int ibmvfc_target_alloc(struct scsi_target *starget)
3397 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3398 struct ibmvfc_host *vhost = shost_priv(shost);
3399 unsigned long flags = 0;
3401 spin_lock_irqsave(shost->host_lock, flags);
3402 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
3403 spin_unlock_irqrestore(shost->host_lock, flags);
3408 * ibmvfc_slave_configure - Configure the device
3409 * @sdev: struct scsi_device device to configure
3411 * Enable allow_restart for a device if it is a disk. Adjust the
3412 * queue_depth here also.
3417 static int ibmvfc_slave_configure(struct scsi_device *sdev)
3419 struct Scsi_Host *shost = sdev->host;
3420 unsigned long flags = 0;
3422 spin_lock_irqsave(shost->host_lock, flags);
3423 if (sdev->type == TYPE_DISK) {
3424 sdev->allow_restart = 1;
3425 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
3427 spin_unlock_irqrestore(shost->host_lock, flags);
3432 * ibmvfc_change_queue_depth - Change the device's queue depth
3433 * @sdev: scsi device struct
3434 * @qdepth: depth to set
3439 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
3441 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
3442 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
3444 return scsi_change_queue_depth(sdev, qdepth);
3447 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
3448 struct device_attribute *attr, char *buf)
3450 struct Scsi_Host *shost = class_to_shost(dev);
3451 struct ibmvfc_host *vhost = shost_priv(shost);
3453 return snprintf(buf, PAGE_SIZE, "%s\n",
3454 vhost->login_buf->resp.partition_name);
3457 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
3458 struct device_attribute *attr, char *buf)
3460 struct Scsi_Host *shost = class_to_shost(dev);
3461 struct ibmvfc_host *vhost = shost_priv(shost);
3463 return snprintf(buf, PAGE_SIZE, "%s\n",
3464 vhost->login_buf->resp.device_name);
3467 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3468 struct device_attribute *attr, char *buf)
3470 struct Scsi_Host *shost = class_to_shost(dev);
3471 struct ibmvfc_host *vhost = shost_priv(shost);
3473 return snprintf(buf, PAGE_SIZE, "%s\n",
3474 vhost->login_buf->resp.port_loc_code);
3477 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3478 struct device_attribute *attr, char *buf)
3480 struct Scsi_Host *shost = class_to_shost(dev);
3481 struct ibmvfc_host *vhost = shost_priv(shost);
3483 return snprintf(buf, PAGE_SIZE, "%s\n",
3484 vhost->login_buf->resp.drc_name);
3487 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3488 struct device_attribute *attr, char *buf)
3490 struct Scsi_Host *shost = class_to_shost(dev);
3491 struct ibmvfc_host *vhost = shost_priv(shost);
3492 return snprintf(buf, PAGE_SIZE, "%d\n", be32_to_cpu(vhost->login_buf->resp.version));
3495 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3496 struct device_attribute *attr, char *buf)
3498 struct Scsi_Host *shost = class_to_shost(dev);
3499 struct ibmvfc_host *vhost = shost_priv(shost);
3500 return snprintf(buf, PAGE_SIZE, "%llx\n", be64_to_cpu(vhost->login_buf->resp.capabilities));
3504 * ibmvfc_show_log_level - Show the adapter's error logging level
3505 * @dev: class device struct
3510 * number of bytes printed to buffer
3512 static ssize_t ibmvfc_show_log_level(struct device *dev,
3513 struct device_attribute *attr, char *buf)
3515 struct Scsi_Host *shost = class_to_shost(dev);
3516 struct ibmvfc_host *vhost = shost_priv(shost);
3517 unsigned long flags = 0;
3520 spin_lock_irqsave(shost->host_lock, flags);
3521 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
3522 spin_unlock_irqrestore(shost->host_lock, flags);
3527 * ibmvfc_store_log_level - Change the adapter's error logging level
3528 * @dev: class device struct
3531 * @count: buffer size
3534 * number of bytes printed to buffer
3536 static ssize_t ibmvfc_store_log_level(struct device *dev,
3537 struct device_attribute *attr,
3538 const char *buf, size_t count)
3540 struct Scsi_Host *shost = class_to_shost(dev);
3541 struct ibmvfc_host *vhost = shost_priv(shost);
3542 unsigned long flags = 0;
3544 spin_lock_irqsave(shost->host_lock, flags);
3545 vhost->log_level = simple_strtoul(buf, NULL, 10);
3546 spin_unlock_irqrestore(shost->host_lock, flags);
3550 static ssize_t ibmvfc_show_scsi_channels(struct device *dev,
3551 struct device_attribute *attr, char *buf)
3553 struct Scsi_Host *shost = class_to_shost(dev);
3554 struct ibmvfc_host *vhost = shost_priv(shost);
3555 unsigned long flags = 0;
3558 spin_lock_irqsave(shost->host_lock, flags);
3559 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->client_scsi_channels);
3560 spin_unlock_irqrestore(shost->host_lock, flags);
3564 static ssize_t ibmvfc_store_scsi_channels(struct device *dev,
3565 struct device_attribute *attr,
3566 const char *buf, size_t count)
3568 struct Scsi_Host *shost = class_to_shost(dev);
3569 struct ibmvfc_host *vhost = shost_priv(shost);
3570 unsigned long flags = 0;
3571 unsigned int channels;
3573 spin_lock_irqsave(shost->host_lock, flags);
3574 channels = simple_strtoul(buf, NULL, 10);
3575 vhost->client_scsi_channels = min(channels, nr_scsi_hw_queues);
3576 ibmvfc_hard_reset_host(vhost);
3577 spin_unlock_irqrestore(shost->host_lock, flags);
3581 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3582 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3583 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3584 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3585 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3586 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3587 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3588 ibmvfc_show_log_level, ibmvfc_store_log_level);
3589 static DEVICE_ATTR(nr_scsi_channels, S_IRUGO | S_IWUSR,
3590 ibmvfc_show_scsi_channels, ibmvfc_store_scsi_channels);
3592 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3594 * ibmvfc_read_trace - Dump the adapter trace
3595 * @filp: open sysfs file
3596 * @kobj: kobject struct
3597 * @bin_attr: bin_attribute struct
3600 * @count: buffer size
3603 * number of bytes printed to buffer
3605 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3606 struct bin_attribute *bin_attr,
3607 char *buf, loff_t off, size_t count)
3609 struct device *dev = kobj_to_dev(kobj);
3610 struct Scsi_Host *shost = class_to_shost(dev);
3611 struct ibmvfc_host *vhost = shost_priv(shost);
3612 unsigned long flags = 0;
3613 int size = IBMVFC_TRACE_SIZE;
3614 char *src = (char *)vhost->trace;
3618 if (off + count > size) {
3623 spin_lock_irqsave(shost->host_lock, flags);
3624 memcpy(buf, &src[off], count);
3625 spin_unlock_irqrestore(shost->host_lock, flags);
3629 static struct bin_attribute ibmvfc_trace_attr = {
3635 .read = ibmvfc_read_trace,
3639 static struct attribute *ibmvfc_host_attrs[] = {
3640 &dev_attr_partition_name.attr,
3641 &dev_attr_device_name.attr,
3642 &dev_attr_port_loc_code.attr,
3643 &dev_attr_drc_name.attr,
3644 &dev_attr_npiv_version.attr,
3645 &dev_attr_capabilities.attr,
3646 &dev_attr_log_level.attr,
3647 &dev_attr_nr_scsi_channels.attr,
3651 ATTRIBUTE_GROUPS(ibmvfc_host);
3653 static const struct scsi_host_template driver_template = {
3654 .module = THIS_MODULE,
3655 .name = "IBM POWER Virtual FC Adapter",
3656 .proc_name = IBMVFC_NAME,
3657 .queuecommand = ibmvfc_queuecommand,
3658 .eh_timed_out = fc_eh_timed_out,
3659 .eh_abort_handler = ibmvfc_eh_abort_handler,
3660 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3661 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3662 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3663 .slave_alloc = ibmvfc_slave_alloc,
3664 .slave_configure = ibmvfc_slave_configure,
3665 .target_alloc = ibmvfc_target_alloc,
3666 .scan_finished = ibmvfc_scan_finished,
3667 .change_queue_depth = ibmvfc_change_queue_depth,
3669 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3671 .sg_tablesize = SG_ALL,
3672 .max_sectors = IBMVFC_MAX_SECTORS,
3673 .shost_groups = ibmvfc_host_groups,
3674 .track_queue_depth = 1,
3679 * ibmvfc_next_async_crq - Returns the next entry in async queue
3680 * @vhost: ibmvfc host struct
3683 * Pointer to next entry in queue / NULL if empty
3685 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3687 struct ibmvfc_queue *async_crq = &vhost->async_crq;
3688 struct ibmvfc_async_crq *crq;
3690 crq = &async_crq->msgs.async[async_crq->cur];
3691 if (crq->valid & 0x80) {
3692 if (++async_crq->cur == async_crq->size)
3702 * ibmvfc_next_crq - Returns the next entry in message queue
3703 * @vhost: ibmvfc host struct
3706 * Pointer to next entry in queue / NULL if empty
3708 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3710 struct ibmvfc_queue *queue = &vhost->crq;
3711 struct ibmvfc_crq *crq;
3713 crq = &queue->msgs.crq[queue->cur];
3714 if (crq->valid & 0x80) {
3715 if (++queue->cur == queue->size)
3725 * ibmvfc_interrupt - Interrupt handler
3726 * @irq: number of irq to handle, not used
3727 * @dev_instance: ibmvfc_host that received interrupt
3732 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3734 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3735 unsigned long flags;
3737 spin_lock_irqsave(vhost->host->host_lock, flags);
3738 vio_disable_interrupts(to_vio_dev(vhost->dev));
3739 tasklet_schedule(&vhost->tasklet);
3740 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3745 * ibmvfc_tasklet - Interrupt handler tasklet
3746 * @data: ibmvfc host struct
3751 static void ibmvfc_tasklet(void *data)
3753 struct ibmvfc_host *vhost = data;
3754 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3755 struct ibmvfc_crq *crq;
3756 struct ibmvfc_async_crq *async;
3757 struct ibmvfc_event *evt, *temp;
3758 unsigned long flags;
3760 LIST_HEAD(evt_doneq);
3762 spin_lock_irqsave(vhost->host->host_lock, flags);
3763 spin_lock(vhost->crq.q_lock);
3765 /* Pull all the valid messages off the async CRQ */
3766 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3767 ibmvfc_handle_async(async, vhost);
3772 /* Pull all the valid messages off the CRQ */
3773 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3774 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3779 vio_enable_interrupts(vdev);
3780 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3781 vio_disable_interrupts(vdev);
3782 ibmvfc_handle_async(async, vhost);
3785 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3786 vio_disable_interrupts(vdev);
3787 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3794 spin_unlock(vhost->crq.q_lock);
3795 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3797 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3798 del_timer(&evt->timer);
3799 list_del(&evt->queue_list);
3800 ibmvfc_trc_end(evt);
3805 static int ibmvfc_toggle_scrq_irq(struct ibmvfc_queue *scrq, int enable)
3807 struct device *dev = scrq->vhost->dev;
3808 struct vio_dev *vdev = to_vio_dev(dev);
3810 int irq_action = H_ENABLE_VIO_INTERRUPT;
3813 irq_action = H_DISABLE_VIO_INTERRUPT;
3815 rc = plpar_hcall_norets(H_VIOCTL, vdev->unit_address, irq_action,
3816 scrq->hw_irq, 0, 0);
3819 dev_err(dev, "Couldn't %s sub-crq[%lu] irq. rc=%ld\n",
3820 enable ? "enable" : "disable", scrq->hwq_id, rc);
3825 static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3826 struct list_head *evt_doneq)
3828 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3830 switch (crq->valid) {
3831 case IBMVFC_CRQ_CMD_RSP:
3833 case IBMVFC_CRQ_XPORT_EVENT:
3836 dev_err(vhost->dev, "Got and invalid message type 0x%02x\n", crq->valid);
3840 /* The only kind of payload CRQs we should get are responses to
3841 * things we send. Make sure this response is to something we
3844 if (unlikely(!ibmvfc_valid_event(&evt->queue->evt_pool, evt))) {
3845 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3850 if (unlikely(atomic_dec_if_positive(&evt->active))) {
3851 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3856 spin_lock(&evt->queue->l_lock);
3857 list_move_tail(&evt->queue_list, evt_doneq);
3858 spin_unlock(&evt->queue->l_lock);
3861 static struct ibmvfc_crq *ibmvfc_next_scrq(struct ibmvfc_queue *scrq)
3863 struct ibmvfc_crq *crq;
3865 crq = &scrq->msgs.scrq[scrq->cur].crq;
3866 if (crq->valid & 0x80) {
3867 if (++scrq->cur == scrq->size)
3876 static void ibmvfc_drain_sub_crq(struct ibmvfc_queue *scrq)
3878 struct ibmvfc_crq *crq;
3879 struct ibmvfc_event *evt, *temp;
3880 unsigned long flags;
3882 LIST_HEAD(evt_doneq);
3884 spin_lock_irqsave(scrq->q_lock, flags);
3886 while ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3887 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3892 ibmvfc_toggle_scrq_irq(scrq, 1);
3893 if ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3894 ibmvfc_toggle_scrq_irq(scrq, 0);
3895 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3901 spin_unlock_irqrestore(scrq->q_lock, flags);
3903 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3904 del_timer(&evt->timer);
3905 list_del(&evt->queue_list);
3906 ibmvfc_trc_end(evt);
3911 static irqreturn_t ibmvfc_interrupt_scsi(int irq, void *scrq_instance)
3913 struct ibmvfc_queue *scrq = (struct ibmvfc_queue *)scrq_instance;
3915 ibmvfc_toggle_scrq_irq(scrq, 0);
3916 ibmvfc_drain_sub_crq(scrq);
3922 * ibmvfc_init_tgt - Set the next init job step for the target
3923 * @tgt: ibmvfc target struct
3924 * @job_step: job step to perform
3927 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3928 void (*job_step) (struct ibmvfc_target *))
3930 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3931 tgt->job_step = job_step;
3932 wake_up(&tgt->vhost->work_wait_q);
3936 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3937 * @tgt: ibmvfc target struct
3938 * @job_step: initialization job step
3940 * Returns: 1 if step will be retried / 0 if not
3943 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3944 void (*job_step) (struct ibmvfc_target *))
3946 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3947 ibmvfc_del_tgt(tgt);
3948 wake_up(&tgt->vhost->work_wait_q);
3951 ibmvfc_init_tgt(tgt, job_step);
3955 /* Defined in FC-LS */
3956 static const struct {
3973 * ibmvfc_get_prli_rsp - Find PRLI response index
3974 * @flags: PRLI response flags
3977 static int ibmvfc_get_prli_rsp(u16 flags)
3980 int code = (flags & 0x0f00) >> 8;
3982 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3983 if (prli_rsp[i].code == code)
3990 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3991 * @evt: ibmvfc event struct
3994 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3996 struct ibmvfc_target *tgt = evt->tgt;
3997 struct ibmvfc_host *vhost = evt->vhost;
3998 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3999 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
4000 u32 status = be16_to_cpu(rsp->common.status);
4001 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
4003 vhost->discovery_threads--;
4004 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4006 case IBMVFC_MAD_SUCCESS:
4007 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
4008 parms->type, parms->flags, parms->service_parms);
4010 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
4011 index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
4012 if (prli_rsp[index].logged_in) {
4013 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
4014 tgt->need_login = 0;
4016 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
4017 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
4018 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
4019 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4022 ibmvfc_del_tgt(tgt);
4023 } else if (prli_rsp[index].retry)
4024 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4026 ibmvfc_del_tgt(tgt);
4028 ibmvfc_del_tgt(tgt);
4030 case IBMVFC_MAD_DRIVER_FAILED:
4032 case IBMVFC_MAD_CRQ_ERROR:
4033 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4035 case IBMVFC_MAD_FAILED:
4037 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
4038 be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
4039 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4040 else if (tgt->logo_rcvd)
4041 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4042 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4043 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4045 ibmvfc_del_tgt(tgt);
4047 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
4048 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4049 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
4053 kref_put(&tgt->kref, ibmvfc_release_tgt);
4054 ibmvfc_free_event(evt);
4055 wake_up(&vhost->work_wait_q);
4059 * ibmvfc_tgt_send_prli - Send a process login
4060 * @tgt: ibmvfc target struct
4063 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
4065 struct ibmvfc_process_login *prli;
4066 struct ibmvfc_host *vhost = tgt->vhost;
4067 struct ibmvfc_event *evt;
4069 if (vhost->discovery_threads >= disc_threads)
4072 kref_get(&tgt->kref);
4073 evt = ibmvfc_get_event(&vhost->crq);
4075 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4076 kref_put(&tgt->kref, ibmvfc_release_tgt);
4077 __ibmvfc_reset_host(vhost);
4080 vhost->discovery_threads++;
4081 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
4083 prli = &evt->iu.prli;
4084 memset(prli, 0, sizeof(*prli));
4085 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4086 prli->common.version = cpu_to_be32(2);
4087 prli->target_wwpn = cpu_to_be64(tgt->wwpn);
4089 prli->common.version = cpu_to_be32(1);
4091 prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
4092 prli->common.length = cpu_to_be16(sizeof(*prli));
4093 prli->scsi_id = cpu_to_be64(tgt->scsi_id);
4095 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
4096 prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
4097 prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
4098 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
4101 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
4103 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4104 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4105 vhost->discovery_threads--;
4106 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4107 kref_put(&tgt->kref, ibmvfc_release_tgt);
4109 tgt_dbg(tgt, "Sent process login\n");
4113 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
4114 * @evt: ibmvfc event struct
4117 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
4119 struct ibmvfc_target *tgt = evt->tgt;
4120 struct ibmvfc_host *vhost = evt->vhost;
4121 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
4122 u32 status = be16_to_cpu(rsp->common.status);
4123 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4125 vhost->discovery_threads--;
4126 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4128 case IBMVFC_MAD_SUCCESS:
4129 tgt_dbg(tgt, "Port Login succeeded\n");
4130 if (tgt->ids.port_name &&
4131 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
4133 tgt_dbg(tgt, "Port re-init required\n");
4136 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4137 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4138 tgt->ids.port_id = tgt->scsi_id;
4139 memcpy(&tgt->service_parms, &rsp->service_parms,
4140 sizeof(tgt->service_parms));
4141 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4142 sizeof(tgt->service_parms_change));
4143 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4145 case IBMVFC_MAD_DRIVER_FAILED:
4147 case IBMVFC_MAD_CRQ_ERROR:
4148 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4150 case IBMVFC_MAD_FAILED:
4152 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4153 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4155 ibmvfc_del_tgt(tgt);
4157 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4158 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4159 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4160 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4161 ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
4165 kref_put(&tgt->kref, ibmvfc_release_tgt);
4166 ibmvfc_free_event(evt);
4167 wake_up(&vhost->work_wait_q);
4171 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
4172 * @tgt: ibmvfc target struct
4175 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
4177 struct ibmvfc_port_login *plogi;
4178 struct ibmvfc_host *vhost = tgt->vhost;
4179 struct ibmvfc_event *evt;
4181 if (vhost->discovery_threads >= disc_threads)
4184 kref_get(&tgt->kref);
4186 evt = ibmvfc_get_event(&vhost->crq);
4188 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4189 kref_put(&tgt->kref, ibmvfc_release_tgt);
4190 __ibmvfc_reset_host(vhost);
4193 vhost->discovery_threads++;
4194 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4195 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
4197 plogi = &evt->iu.plogi;
4198 memset(plogi, 0, sizeof(*plogi));
4199 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4200 plogi->common.version = cpu_to_be32(2);
4201 plogi->target_wwpn = cpu_to_be64(tgt->wwpn);
4203 plogi->common.version = cpu_to_be32(1);
4205 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
4206 plogi->common.length = cpu_to_be16(sizeof(*plogi));
4207 plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
4209 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4210 vhost->discovery_threads--;
4211 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4212 kref_put(&tgt->kref, ibmvfc_release_tgt);
4214 tgt_dbg(tgt, "Sent port login\n");
4218 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
4219 * @evt: ibmvfc event struct
4222 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
4224 struct ibmvfc_target *tgt = evt->tgt;
4225 struct ibmvfc_host *vhost = evt->vhost;
4226 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
4227 u32 status = be16_to_cpu(rsp->common.status);
4229 vhost->discovery_threads--;
4230 ibmvfc_free_event(evt);
4231 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4234 case IBMVFC_MAD_SUCCESS:
4235 tgt_dbg(tgt, "Implicit Logout succeeded\n");
4237 case IBMVFC_MAD_DRIVER_FAILED:
4238 kref_put(&tgt->kref, ibmvfc_release_tgt);
4239 wake_up(&vhost->work_wait_q);
4241 case IBMVFC_MAD_FAILED:
4243 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
4247 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
4248 kref_put(&tgt->kref, ibmvfc_release_tgt);
4249 wake_up(&vhost->work_wait_q);
4253 * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
4254 * @tgt: ibmvfc target struct
4255 * @done: Routine to call when the event is responded to
4258 * Allocated and initialized ibmvfc_event struct
4260 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
4261 void (*done) (struct ibmvfc_event *))
4263 struct ibmvfc_implicit_logout *mad;
4264 struct ibmvfc_host *vhost = tgt->vhost;
4265 struct ibmvfc_event *evt;
4267 kref_get(&tgt->kref);
4268 evt = ibmvfc_get_event(&vhost->crq);
4271 ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
4273 mad = &evt->iu.implicit_logout;
4274 memset(mad, 0, sizeof(*mad));
4275 mad->common.version = cpu_to_be32(1);
4276 mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
4277 mad->common.length = cpu_to_be16(sizeof(*mad));
4278 mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4283 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
4284 * @tgt: ibmvfc target struct
4287 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
4289 struct ibmvfc_host *vhost = tgt->vhost;
4290 struct ibmvfc_event *evt;
4292 if (vhost->discovery_threads >= disc_threads)
4295 vhost->discovery_threads++;
4296 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4297 ibmvfc_tgt_implicit_logout_done);
4299 vhost->discovery_threads--;
4300 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4301 kref_put(&tgt->kref, ibmvfc_release_tgt);
4302 __ibmvfc_reset_host(vhost);
4306 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4307 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4308 vhost->discovery_threads--;
4309 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4310 kref_put(&tgt->kref, ibmvfc_release_tgt);
4312 tgt_dbg(tgt, "Sent Implicit Logout\n");
4316 * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
4317 * @evt: ibmvfc event struct
4320 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
4322 struct ibmvfc_target *tgt = evt->tgt;
4323 struct ibmvfc_host *vhost = evt->vhost;
4324 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4325 u32 status = be16_to_cpu(mad->common.status);
4327 vhost->discovery_threads--;
4328 ibmvfc_free_event(evt);
4331 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
4332 * driver in which case we need to free up all the targets. If we are
4333 * not unloading, we will still go through a hard reset to get out of
4334 * offline state, so there is no need to track the old targets in that
4337 if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
4338 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4340 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
4342 tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
4343 kref_put(&tgt->kref, ibmvfc_release_tgt);
4344 wake_up(&vhost->work_wait_q);
4348 * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
4349 * @tgt: ibmvfc target struct
4352 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
4354 struct ibmvfc_host *vhost = tgt->vhost;
4355 struct ibmvfc_event *evt;
4357 if (!vhost->logged_in) {
4358 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4362 if (vhost->discovery_threads >= disc_threads)
4365 vhost->discovery_threads++;
4366 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4367 ibmvfc_tgt_implicit_logout_and_del_done);
4369 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
4370 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4371 vhost->discovery_threads--;
4372 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4373 kref_put(&tgt->kref, ibmvfc_release_tgt);
4375 tgt_dbg(tgt, "Sent Implicit Logout\n");
4379 * ibmvfc_tgt_move_login_done - Completion handler for Move Login
4380 * @evt: ibmvfc event struct
4383 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
4385 struct ibmvfc_target *tgt = evt->tgt;
4386 struct ibmvfc_host *vhost = evt->vhost;
4387 struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
4388 u32 status = be16_to_cpu(rsp->common.status);
4389 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4391 vhost->discovery_threads--;
4392 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4394 case IBMVFC_MAD_SUCCESS:
4395 tgt_dbg(tgt, "Move Login succeeded for new scsi_id: %llX\n", tgt->new_scsi_id);
4396 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4397 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4398 tgt->scsi_id = tgt->new_scsi_id;
4399 tgt->ids.port_id = tgt->scsi_id;
4400 memcpy(&tgt->service_parms, &rsp->service_parms,
4401 sizeof(tgt->service_parms));
4402 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4403 sizeof(tgt->service_parms_change));
4404 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4406 case IBMVFC_MAD_DRIVER_FAILED:
4408 case IBMVFC_MAD_CRQ_ERROR:
4409 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4411 case IBMVFC_MAD_FAILED:
4413 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4416 "Move Login failed: new scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
4417 tgt->new_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
4422 kref_put(&tgt->kref, ibmvfc_release_tgt);
4423 ibmvfc_free_event(evt);
4424 wake_up(&vhost->work_wait_q);
4429 * ibmvfc_tgt_move_login - Initiate a move login for specified target
4430 * @tgt: ibmvfc target struct
4433 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
4435 struct ibmvfc_host *vhost = tgt->vhost;
4436 struct ibmvfc_move_login *move;
4437 struct ibmvfc_event *evt;
4439 if (vhost->discovery_threads >= disc_threads)
4442 kref_get(&tgt->kref);
4443 evt = ibmvfc_get_event(&vhost->crq);
4445 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4446 kref_put(&tgt->kref, ibmvfc_release_tgt);
4447 __ibmvfc_reset_host(vhost);
4450 vhost->discovery_threads++;
4451 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4452 ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
4454 move = &evt->iu.move_login;
4455 memset(move, 0, sizeof(*move));
4456 move->common.version = cpu_to_be32(1);
4457 move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
4458 move->common.length = cpu_to_be16(sizeof(*move));
4460 move->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4461 move->new_scsi_id = cpu_to_be64(tgt->new_scsi_id);
4462 move->wwpn = cpu_to_be64(tgt->wwpn);
4463 move->node_name = cpu_to_be64(tgt->ids.node_name);
4465 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4466 vhost->discovery_threads--;
4467 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4468 kref_put(&tgt->kref, ibmvfc_release_tgt);
4470 tgt_dbg(tgt, "Sent Move Login for new scsi_id: %llX\n", tgt->new_scsi_id);
4474 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
4475 * @mad: ibmvfc passthru mad struct
4476 * @tgt: ibmvfc target struct
4479 * 1 if PLOGI needed / 0 if PLOGI not needed
4481 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
4482 struct ibmvfc_target *tgt)
4484 if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
4486 if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
4488 if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
4494 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
4495 * @evt: ibmvfc event struct
4498 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
4500 struct ibmvfc_target *tgt = evt->tgt;
4501 struct ibmvfc_host *vhost = evt->vhost;
4502 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4503 u32 status = be16_to_cpu(mad->common.status);
4504 u8 fc_reason, fc_explain;
4506 vhost->discovery_threads--;
4507 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4508 del_timer(&tgt->timer);
4511 case IBMVFC_MAD_SUCCESS:
4512 tgt_dbg(tgt, "ADISC succeeded\n");
4513 if (ibmvfc_adisc_needs_plogi(mad, tgt))
4514 ibmvfc_del_tgt(tgt);
4516 case IBMVFC_MAD_DRIVER_FAILED:
4518 case IBMVFC_MAD_FAILED:
4520 ibmvfc_del_tgt(tgt);
4521 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
4522 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
4523 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4524 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
4525 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
4526 ibmvfc_get_fc_type(fc_reason), fc_reason,
4527 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
4531 kref_put(&tgt->kref, ibmvfc_release_tgt);
4532 ibmvfc_free_event(evt);
4533 wake_up(&vhost->work_wait_q);
4537 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
4538 * @evt: ibmvfc event struct
4541 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
4543 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
4545 memset(mad, 0, sizeof(*mad));
4546 mad->common.version = cpu_to_be32(1);
4547 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
4548 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
4549 mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4550 offsetof(struct ibmvfc_passthru_mad, iu));
4551 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
4552 mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4553 mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
4554 mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4555 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4556 offsetof(struct ibmvfc_passthru_fc_iu, payload));
4557 mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4558 mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4559 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4560 offsetof(struct ibmvfc_passthru_fc_iu, response));
4561 mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
4565 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
4566 * @evt: ibmvfc event struct
4568 * Just cleanup this event struct. Everything else is handled by
4569 * the ADISC completion handler. If the ADISC never actually comes
4570 * back, we still have the timer running on the ADISC event struct
4571 * which will fire and cause the CRQ to get reset.
4574 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
4576 struct ibmvfc_host *vhost = evt->vhost;
4577 struct ibmvfc_target *tgt = evt->tgt;
4579 tgt_dbg(tgt, "ADISC cancel complete\n");
4580 vhost->abort_threads--;
4581 ibmvfc_free_event(evt);
4582 kref_put(&tgt->kref, ibmvfc_release_tgt);
4583 wake_up(&vhost->work_wait_q);
4587 * ibmvfc_adisc_timeout - Handle an ADISC timeout
4588 * @t: ibmvfc target struct
4590 * If an ADISC times out, send a cancel. If the cancel times
4591 * out, reset the CRQ. When the ADISC comes back as cancelled,
4592 * log back into the target.
4594 static void ibmvfc_adisc_timeout(struct timer_list *t)
4596 struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
4597 struct ibmvfc_host *vhost = tgt->vhost;
4598 struct ibmvfc_event *evt;
4599 struct ibmvfc_tmf *tmf;
4600 unsigned long flags;
4603 tgt_dbg(tgt, "ADISC timeout\n");
4604 spin_lock_irqsave(vhost->host->host_lock, flags);
4605 if (vhost->abort_threads >= disc_threads ||
4606 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
4607 vhost->state != IBMVFC_INITIALIZING ||
4608 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
4609 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4613 vhost->abort_threads++;
4614 kref_get(&tgt->kref);
4615 evt = ibmvfc_get_event(&vhost->crq);
4617 tgt_err(tgt, "Failed to get cancel event for ADISC.\n");
4618 vhost->abort_threads--;
4619 kref_put(&tgt->kref, ibmvfc_release_tgt);
4620 __ibmvfc_reset_host(vhost);
4621 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4624 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
4628 memset(tmf, 0, sizeof(*tmf));
4629 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4630 tmf->common.version = cpu_to_be32(2);
4631 tmf->target_wwpn = cpu_to_be64(tgt->wwpn);
4633 tmf->common.version = cpu_to_be32(1);
4635 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
4636 tmf->common.length = cpu_to_be16(sizeof(*tmf));
4637 tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
4638 tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
4640 rc = ibmvfc_send_event(evt, vhost, default_timeout);
4643 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
4644 vhost->abort_threads--;
4645 kref_put(&tgt->kref, ibmvfc_release_tgt);
4646 __ibmvfc_reset_host(vhost);
4648 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
4649 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4653 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
4654 * @tgt: ibmvfc target struct
4656 * When sending an ADISC we end up with two timers running. The
4657 * first timer is the timer in the ibmvfc target struct. If this
4658 * fires, we send a cancel to the target. The second timer is the
4659 * timer on the ibmvfc event for the ADISC, which is longer. If that
4660 * fires, it means the ADISC timed out and our attempt to cancel it
4661 * also failed, so we need to reset the CRQ.
4663 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
4665 struct ibmvfc_passthru_mad *mad;
4666 struct ibmvfc_host *vhost = tgt->vhost;
4667 struct ibmvfc_event *evt;
4669 if (vhost->discovery_threads >= disc_threads)
4672 kref_get(&tgt->kref);
4673 evt = ibmvfc_get_event(&vhost->crq);
4675 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4676 kref_put(&tgt->kref, ibmvfc_release_tgt);
4677 __ibmvfc_reset_host(vhost);
4680 vhost->discovery_threads++;
4681 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
4684 ibmvfc_init_passthru(evt);
4685 mad = &evt->iu.passthru;
4686 mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4687 mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4688 mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4690 mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4691 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4692 sizeof(vhost->login_buf->resp.port_name));
4693 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4694 sizeof(vhost->login_buf->resp.node_name));
4695 mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4697 if (timer_pending(&tgt->timer))
4698 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4700 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4701 add_timer(&tgt->timer);
4704 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4705 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4706 vhost->discovery_threads--;
4707 del_timer(&tgt->timer);
4708 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4709 kref_put(&tgt->kref, ibmvfc_release_tgt);
4711 tgt_dbg(tgt, "Sent ADISC\n");
4715 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4716 * @evt: ibmvfc event struct
4719 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4721 struct ibmvfc_target *tgt = evt->tgt;
4722 struct ibmvfc_host *vhost = evt->vhost;
4723 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4724 u32 status = be16_to_cpu(rsp->common.status);
4725 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4727 vhost->discovery_threads--;
4728 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4730 case IBMVFC_MAD_SUCCESS:
4731 tgt_dbg(tgt, "Query Target succeeded\n");
4732 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4733 ibmvfc_del_tgt(tgt);
4735 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4737 case IBMVFC_MAD_DRIVER_FAILED:
4739 case IBMVFC_MAD_CRQ_ERROR:
4740 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4742 case IBMVFC_MAD_FAILED:
4744 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4745 be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4746 be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4747 ibmvfc_del_tgt(tgt);
4748 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4749 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4751 ibmvfc_del_tgt(tgt);
4753 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4754 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4755 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4756 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4757 ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4762 kref_put(&tgt->kref, ibmvfc_release_tgt);
4763 ibmvfc_free_event(evt);
4764 wake_up(&vhost->work_wait_q);
4768 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4769 * @tgt: ibmvfc target struct
4772 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4774 struct ibmvfc_query_tgt *query_tgt;
4775 struct ibmvfc_host *vhost = tgt->vhost;
4776 struct ibmvfc_event *evt;
4778 if (vhost->discovery_threads >= disc_threads)
4781 kref_get(&tgt->kref);
4782 evt = ibmvfc_get_event(&vhost->crq);
4784 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4785 kref_put(&tgt->kref, ibmvfc_release_tgt);
4786 __ibmvfc_reset_host(vhost);
4789 vhost->discovery_threads++;
4791 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4792 query_tgt = &evt->iu.query_tgt;
4793 memset(query_tgt, 0, sizeof(*query_tgt));
4794 query_tgt->common.version = cpu_to_be32(1);
4795 query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4796 query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4797 query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4799 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4800 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4801 vhost->discovery_threads--;
4802 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4803 kref_put(&tgt->kref, ibmvfc_release_tgt);
4805 tgt_dbg(tgt, "Sent Query Target\n");
4809 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4810 * @vhost: ibmvfc host struct
4811 * @target: Holds SCSI ID to allocate target forand the WWPN
4814 * 0 on success / other on failure
4816 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4817 struct ibmvfc_discover_targets_entry *target)
4819 struct ibmvfc_target *stgt = NULL;
4820 struct ibmvfc_target *wtgt = NULL;
4821 struct ibmvfc_target *tgt;
4822 unsigned long flags;
4823 u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4824 u64 wwpn = be64_to_cpu(target->wwpn);
4826 /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4827 spin_lock_irqsave(vhost->host->host_lock, flags);
4828 list_for_each_entry(tgt, &vhost->targets, queue) {
4829 if (tgt->wwpn == wwpn) {
4835 list_for_each_entry(tgt, &vhost->targets, queue) {
4836 if (tgt->scsi_id == scsi_id) {
4842 if (wtgt && !stgt) {
4844 * A WWPN target has moved and we still are tracking the old
4845 * SCSI ID. The only way we should be able to get here is if
4846 * we attempted to send an implicit logout for the old SCSI ID
4847 * and it failed for some reason, such as there being I/O
4848 * pending to the target. In this case, we will have already
4849 * deleted the rport from the FC transport so we do a move
4850 * login, which works even with I/O pending, however, if
4851 * there is still I/O pending, it will stay outstanding, so
4852 * we only do this if fast fail is disabled for the rport,
4853 * otherwise we let terminate_rport_io clean up the port
4854 * before we login at the new location.
4856 if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4857 if (wtgt->move_login) {
4859 * Do a move login here. The old target is no longer
4860 * known to the transport layer We don't use the
4861 * normal ibmvfc_set_tgt_action to set this, as we
4862 * don't normally want to allow this state change.
4864 wtgt->new_scsi_id = scsi_id;
4865 wtgt->action = IBMVFC_TGT_ACTION_INIT;
4866 wtgt->init_retries = 0;
4867 ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4871 tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4872 wtgt->action, wtgt->rport);
4875 if (tgt->need_login)
4876 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4879 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4881 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4882 memset(tgt, 0, sizeof(*tgt));
4883 tgt->scsi_id = scsi_id;
4886 tgt->need_login = 1;
4887 timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4888 kref_init(&tgt->kref);
4889 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4890 spin_lock_irqsave(vhost->host->host_lock, flags);
4891 tgt->cancel_key = vhost->task_set++;
4892 list_add_tail(&tgt->queue, &vhost->targets);
4895 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4900 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4901 * @vhost: ibmvfc host struct
4904 * 0 on success / other on failure
4906 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4910 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4911 rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
4917 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4918 * @evt: ibmvfc event struct
4921 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4923 struct ibmvfc_host *vhost = evt->vhost;
4924 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4925 u32 mad_status = be16_to_cpu(rsp->common.status);
4926 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4928 switch (mad_status) {
4929 case IBMVFC_MAD_SUCCESS:
4930 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4931 vhost->num_targets = be32_to_cpu(rsp->num_written);
4932 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4934 case IBMVFC_MAD_FAILED:
4935 level += ibmvfc_retry_host_init(vhost);
4936 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4937 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4938 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4940 case IBMVFC_MAD_DRIVER_FAILED:
4943 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4944 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4948 ibmvfc_free_event(evt);
4949 wake_up(&vhost->work_wait_q);
4953 * ibmvfc_discover_targets - Send Discover Targets MAD
4954 * @vhost: ibmvfc host struct
4957 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4959 struct ibmvfc_discover_targets *mad;
4960 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4961 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4964 ibmvfc_log(vhost, level, "Discover Targets failed: no available events\n");
4965 ibmvfc_hard_reset_host(vhost);
4969 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4970 mad = &evt->iu.discover_targets;
4971 memset(mad, 0, sizeof(*mad));
4972 mad->common.version = cpu_to_be32(1);
4973 mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4974 mad->common.length = cpu_to_be16(sizeof(*mad));
4975 mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4976 mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4977 mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4978 mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
4979 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4981 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4982 ibmvfc_dbg(vhost, "Sent discover targets\n");
4984 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4987 static void ibmvfc_channel_setup_done(struct ibmvfc_event *evt)
4989 struct ibmvfc_host *vhost = evt->vhost;
4990 struct ibmvfc_channel_setup *setup = vhost->channel_setup_buf;
4991 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
4992 u32 mad_status = be16_to_cpu(evt->xfer_iu->channel_setup.common.status);
4993 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4994 int flags, active_queues, i;
4996 ibmvfc_free_event(evt);
4998 switch (mad_status) {
4999 case IBMVFC_MAD_SUCCESS:
5000 ibmvfc_dbg(vhost, "Channel Setup succeeded\n");
5001 flags = be32_to_cpu(setup->flags);
5002 vhost->do_enquiry = 0;
5003 active_queues = be32_to_cpu(setup->num_scsi_subq_channels);
5004 scrqs->active_queues = active_queues;
5006 if (flags & IBMVFC_CHANNELS_CANCELED) {
5007 ibmvfc_dbg(vhost, "Channels Canceled\n");
5008 vhost->using_channels = 0;
5011 vhost->using_channels = 1;
5012 for (i = 0; i < active_queues; i++)
5013 scrqs->scrqs[i].vios_cookie =
5014 be64_to_cpu(setup->channel_handles[i]);
5016 ibmvfc_dbg(vhost, "Using %u channels\n",
5017 vhost->scsi_scrqs.active_queues);
5020 case IBMVFC_MAD_FAILED:
5021 level += ibmvfc_retry_host_init(vhost);
5022 ibmvfc_log(vhost, level, "Channel Setup failed\n");
5024 case IBMVFC_MAD_DRIVER_FAILED:
5027 dev_err(vhost->dev, "Invalid Channel Setup response: 0x%x\n",
5029 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5033 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5034 wake_up(&vhost->work_wait_q);
5037 static void ibmvfc_channel_setup(struct ibmvfc_host *vhost)
5039 struct ibmvfc_channel_setup_mad *mad;
5040 struct ibmvfc_channel_setup *setup_buf = vhost->channel_setup_buf;
5041 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
5042 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
5043 unsigned int num_channels =
5044 min(vhost->client_scsi_channels, vhost->max_vios_scsi_channels);
5045 int level = IBMVFC_DEFAULT_LOG_LEVEL;
5049 ibmvfc_log(vhost, level, "Channel Setup failed: no available events\n");
5050 ibmvfc_hard_reset_host(vhost);
5054 memset(setup_buf, 0, sizeof(*setup_buf));
5055 if (num_channels == 0)
5056 setup_buf->flags = cpu_to_be32(IBMVFC_CANCEL_CHANNELS);
5058 setup_buf->num_scsi_subq_channels = cpu_to_be32(num_channels);
5059 for (i = 0; i < num_channels; i++)
5060 setup_buf->channel_handles[i] = cpu_to_be64(scrqs->scrqs[i].cookie);
5063 ibmvfc_init_event(evt, ibmvfc_channel_setup_done, IBMVFC_MAD_FORMAT);
5064 mad = &evt->iu.channel_setup;
5065 memset(mad, 0, sizeof(*mad));
5066 mad->common.version = cpu_to_be32(1);
5067 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_SETUP);
5068 mad->common.length = cpu_to_be16(sizeof(*mad));
5069 mad->buffer.va = cpu_to_be64(vhost->channel_setup_dma);
5070 mad->buffer.len = cpu_to_be32(sizeof(*vhost->channel_setup_buf));
5072 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5074 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5075 ibmvfc_dbg(vhost, "Sent channel setup\n");
5077 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
5080 static void ibmvfc_channel_enquiry_done(struct ibmvfc_event *evt)
5082 struct ibmvfc_host *vhost = evt->vhost;
5083 struct ibmvfc_channel_enquiry *rsp = &evt->xfer_iu->channel_enquiry;
5084 u32 mad_status = be16_to_cpu(rsp->common.status);
5085 int level = IBMVFC_DEFAULT_LOG_LEVEL;
5087 switch (mad_status) {
5088 case IBMVFC_MAD_SUCCESS:
5089 ibmvfc_dbg(vhost, "Channel Enquiry succeeded\n");
5090 vhost->max_vios_scsi_channels = be32_to_cpu(rsp->num_scsi_subq_channels);
5091 ibmvfc_free_event(evt);
5093 case IBMVFC_MAD_FAILED:
5094 level += ibmvfc_retry_host_init(vhost);
5095 ibmvfc_log(vhost, level, "Channel Enquiry failed\n");
5097 case IBMVFC_MAD_DRIVER_FAILED:
5098 ibmvfc_free_event(evt);
5101 dev_err(vhost->dev, "Invalid Channel Enquiry response: 0x%x\n",
5103 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5104 ibmvfc_free_event(evt);
5108 ibmvfc_channel_setup(vhost);
5111 static void ibmvfc_channel_enquiry(struct ibmvfc_host *vhost)
5113 struct ibmvfc_channel_enquiry *mad;
5114 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
5115 int level = IBMVFC_DEFAULT_LOG_LEVEL;
5118 ibmvfc_log(vhost, level, "Channel Enquiry failed: no available events\n");
5119 ibmvfc_hard_reset_host(vhost);
5123 ibmvfc_init_event(evt, ibmvfc_channel_enquiry_done, IBMVFC_MAD_FORMAT);
5124 mad = &evt->iu.channel_enquiry;
5125 memset(mad, 0, sizeof(*mad));
5126 mad->common.version = cpu_to_be32(1);
5127 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_ENQUIRY);
5128 mad->common.length = cpu_to_be16(sizeof(*mad));
5130 if (mig_channels_only)
5131 mad->flags |= cpu_to_be32(IBMVFC_NO_CHANNELS_TO_CRQ_SUPPORT);
5132 if (mig_no_less_channels)
5133 mad->flags |= cpu_to_be32(IBMVFC_NO_N_TO_M_CHANNELS_SUPPORT);
5135 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5137 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5138 ibmvfc_dbg(vhost, "Send channel enquiry\n");
5140 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5144 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
5145 * @evt: ibmvfc event struct
5148 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
5150 struct ibmvfc_host *vhost = evt->vhost;
5151 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
5152 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
5153 unsigned int npiv_max_sectors;
5154 int level = IBMVFC_DEFAULT_LOG_LEVEL;
5156 switch (mad_status) {
5157 case IBMVFC_MAD_SUCCESS:
5158 ibmvfc_free_event(evt);
5160 case IBMVFC_MAD_FAILED:
5161 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
5162 level += ibmvfc_retry_host_init(vhost);
5164 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5165 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
5166 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
5167 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
5168 ibmvfc_free_event(evt);
5170 case IBMVFC_MAD_CRQ_ERROR:
5171 ibmvfc_retry_host_init(vhost);
5173 case IBMVFC_MAD_DRIVER_FAILED:
5174 ibmvfc_free_event(evt);
5177 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
5178 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5179 ibmvfc_free_event(evt);
5183 vhost->client_migrated = 0;
5185 if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
5186 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
5188 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5189 wake_up(&vhost->work_wait_q);
5193 if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
5194 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
5196 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5197 wake_up(&vhost->work_wait_q);
5201 vhost->logged_in = 1;
5202 npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
5203 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
5204 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
5205 rsp->drc_name, npiv_max_sectors);
5207 fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
5208 fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
5209 fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
5210 fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
5211 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
5212 fc_host_supported_classes(vhost->host) = 0;
5213 if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
5214 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
5215 if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
5216 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
5217 if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
5218 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
5219 fc_host_maxframe_size(vhost->host) =
5220 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
5222 vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
5223 vhost->host->max_sectors = npiv_max_sectors;
5225 if (ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPORT_CHANNELS) && vhost->do_enquiry) {
5226 ibmvfc_channel_enquiry(vhost);
5228 vhost->do_enquiry = 0;
5229 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5230 wake_up(&vhost->work_wait_q);
5235 * ibmvfc_npiv_login - Sends NPIV login
5236 * @vhost: ibmvfc host struct
5239 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
5241 struct ibmvfc_npiv_login_mad *mad;
5242 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
5245 ibmvfc_dbg(vhost, "NPIV Login failed: no available events\n");
5246 ibmvfc_hard_reset_host(vhost);
5250 ibmvfc_gather_partition_info(vhost);
5251 ibmvfc_set_login_info(vhost);
5252 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
5254 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
5255 mad = &evt->iu.npiv_login;
5256 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
5257 mad->common.version = cpu_to_be32(1);
5258 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
5259 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
5260 mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
5261 mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
5263 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5265 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5266 ibmvfc_dbg(vhost, "Sent NPIV login\n");
5268 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5272 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
5273 * @evt: ibmvfc event struct
5276 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
5278 struct ibmvfc_host *vhost = evt->vhost;
5279 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
5281 ibmvfc_free_event(evt);
5283 switch (mad_status) {
5284 case IBMVFC_MAD_SUCCESS:
5285 if (list_empty(&vhost->crq.sent) &&
5286 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
5287 ibmvfc_init_host(vhost);
5291 case IBMVFC_MAD_FAILED:
5292 case IBMVFC_MAD_NOT_SUPPORTED:
5293 case IBMVFC_MAD_CRQ_ERROR:
5294 case IBMVFC_MAD_DRIVER_FAILED:
5296 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
5300 ibmvfc_hard_reset_host(vhost);
5304 * ibmvfc_npiv_logout - Issue an NPIV Logout
5305 * @vhost: ibmvfc host struct
5308 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
5310 struct ibmvfc_npiv_logout_mad *mad;
5311 struct ibmvfc_event *evt;
5313 evt = ibmvfc_get_event(&vhost->crq);
5315 ibmvfc_dbg(vhost, "NPIV Logout failed: no available events\n");
5316 ibmvfc_hard_reset_host(vhost);
5320 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
5322 mad = &evt->iu.npiv_logout;
5323 memset(mad, 0, sizeof(*mad));
5324 mad->common.version = cpu_to_be32(1);
5325 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
5326 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
5328 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
5330 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5331 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
5333 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5337 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
5338 * @vhost: ibmvfc host struct
5341 * 1 if work to do / 0 if not
5343 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
5345 struct ibmvfc_target *tgt;
5347 list_for_each_entry(tgt, &vhost->targets, queue) {
5348 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
5349 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5357 * ibmvfc_dev_logo_to_do - Is there target logout work to do?
5358 * @vhost: ibmvfc host struct
5361 * 1 if work to do / 0 if not
5363 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
5365 struct ibmvfc_target *tgt;
5367 list_for_each_entry(tgt, &vhost->targets, queue) {
5368 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
5369 tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5376 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
5377 * @vhost: ibmvfc host struct
5380 * 1 if work to do / 0 if not
5382 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5384 struct ibmvfc_target *tgt;
5386 if (kthread_should_stop())
5388 switch (vhost->action) {
5389 case IBMVFC_HOST_ACTION_NONE:
5390 case IBMVFC_HOST_ACTION_INIT_WAIT:
5391 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5393 case IBMVFC_HOST_ACTION_TGT_INIT:
5394 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5395 if (vhost->discovery_threads == disc_threads)
5397 list_for_each_entry(tgt, &vhost->targets, queue)
5398 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
5400 list_for_each_entry(tgt, &vhost->targets, queue)
5401 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5404 case IBMVFC_HOST_ACTION_TGT_DEL:
5405 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5406 if (vhost->discovery_threads == disc_threads)
5408 list_for_each_entry(tgt, &vhost->targets, queue)
5409 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
5411 list_for_each_entry(tgt, &vhost->targets, queue)
5412 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5415 case IBMVFC_HOST_ACTION_LOGO:
5416 case IBMVFC_HOST_ACTION_INIT:
5417 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5418 case IBMVFC_HOST_ACTION_QUERY:
5419 case IBMVFC_HOST_ACTION_RESET:
5420 case IBMVFC_HOST_ACTION_REENABLE:
5429 * ibmvfc_work_to_do - Is there task level work to do?
5430 * @vhost: ibmvfc host struct
5433 * 1 if work to do / 0 if not
5435 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5437 unsigned long flags;
5440 spin_lock_irqsave(vhost->host->host_lock, flags);
5441 rc = __ibmvfc_work_to_do(vhost);
5442 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5447 * ibmvfc_log_ae - Log async events if necessary
5448 * @vhost: ibmvfc host struct
5449 * @events: events to log
5452 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
5454 if (events & IBMVFC_AE_RSCN)
5455 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
5456 if ((events & IBMVFC_AE_LINKDOWN) &&
5457 vhost->state >= IBMVFC_HALTED)
5458 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
5459 if ((events & IBMVFC_AE_LINKUP) &&
5460 vhost->state == IBMVFC_INITIALIZING)
5461 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
5465 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
5466 * @tgt: ibmvfc target struct
5469 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
5471 struct ibmvfc_host *vhost = tgt->vhost;
5472 struct fc_rport *rport;
5473 unsigned long flags;
5475 tgt_dbg(tgt, "Adding rport\n");
5476 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
5477 spin_lock_irqsave(vhost->host->host_lock, flags);
5479 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5480 tgt_dbg(tgt, "Deleting rport\n");
5481 list_del(&tgt->queue);
5482 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5483 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5484 fc_remote_port_delete(rport);
5485 del_timer_sync(&tgt->timer);
5486 kref_put(&tgt->kref, ibmvfc_release_tgt);
5488 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5489 tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
5490 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5492 tgt->init_retries = 0;
5493 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5494 fc_remote_port_delete(rport);
5496 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
5497 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5502 tgt_dbg(tgt, "rport add succeeded\n");
5504 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
5505 rport->supported_classes = 0;
5506 tgt->target_id = rport->scsi_target_id;
5507 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
5508 rport->supported_classes |= FC_COS_CLASS1;
5509 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
5510 rport->supported_classes |= FC_COS_CLASS2;
5511 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
5512 rport->supported_classes |= FC_COS_CLASS3;
5514 blk_queue_max_segments(rport->rqst_q, 1);
5516 tgt_dbg(tgt, "rport add failed\n");
5517 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5521 * ibmvfc_do_work - Do task level work
5522 * @vhost: ibmvfc host struct
5525 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
5527 struct ibmvfc_target *tgt;
5528 unsigned long flags;
5529 struct fc_rport *rport;
5533 ibmvfc_log_ae(vhost, vhost->events_to_log);
5534 spin_lock_irqsave(vhost->host->host_lock, flags);
5535 vhost->events_to_log = 0;
5536 switch (vhost->action) {
5537 case IBMVFC_HOST_ACTION_NONE:
5538 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5539 case IBMVFC_HOST_ACTION_INIT_WAIT:
5541 case IBMVFC_HOST_ACTION_RESET:
5542 list_splice_init(&vhost->purge, &purge);
5543 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5544 ibmvfc_complete_purge(&purge);
5545 rc = ibmvfc_reset_crq(vhost);
5547 spin_lock_irqsave(vhost->host->host_lock, flags);
5548 if (!rc || rc == H_CLOSED)
5549 vio_enable_interrupts(to_vio_dev(vhost->dev));
5550 if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
5552 * The only action we could have changed to would have
5553 * been reenable, in which case, we skip the rest of
5554 * this path and wait until we've done the re-enable
5555 * before sending the crq init.
5557 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5559 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
5560 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
5561 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5562 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
5566 case IBMVFC_HOST_ACTION_REENABLE:
5567 list_splice_init(&vhost->purge, &purge);
5568 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5569 ibmvfc_complete_purge(&purge);
5570 rc = ibmvfc_reenable_crq_queue(vhost);
5572 spin_lock_irqsave(vhost->host->host_lock, flags);
5573 if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
5575 * The only action we could have changed to would have
5576 * been reset, in which case, we skip the rest of this
5577 * path and wait until we've done the reset before
5578 * sending the crq init.
5580 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5581 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
5582 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5583 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
5587 case IBMVFC_HOST_ACTION_LOGO:
5588 vhost->job_step(vhost);
5590 case IBMVFC_HOST_ACTION_INIT:
5591 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
5592 if (vhost->delay_init) {
5593 vhost->delay_init = 0;
5594 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5598 vhost->job_step(vhost);
5600 case IBMVFC_HOST_ACTION_QUERY:
5601 list_for_each_entry(tgt, &vhost->targets, queue)
5602 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
5603 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
5605 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5606 list_for_each_entry(tgt, &vhost->targets, queue) {
5607 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5613 if (!ibmvfc_dev_init_to_do(vhost))
5614 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
5616 case IBMVFC_HOST_ACTION_TGT_DEL:
5617 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5618 list_for_each_entry(tgt, &vhost->targets, queue) {
5619 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
5625 if (ibmvfc_dev_logo_to_do(vhost)) {
5626 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5630 list_for_each_entry(tgt, &vhost->targets, queue) {
5631 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5632 tgt_dbg(tgt, "Deleting rport\n");
5635 list_del(&tgt->queue);
5636 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5637 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5639 fc_remote_port_delete(rport);
5640 del_timer_sync(&tgt->timer);
5641 kref_put(&tgt->kref, ibmvfc_release_tgt);
5643 } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5644 tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
5647 tgt->init_retries = 0;
5648 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5651 * If fast fail is enabled, we wait for it to fire and then clean up
5652 * the old port, since we expect the fast fail timer to clean up the
5653 * outstanding I/O faster than waiting for normal command timeouts.
5654 * However, if fast fail is disabled, any I/O outstanding to the
5655 * rport LUNs will stay outstanding indefinitely, since the EH handlers
5656 * won't get invoked for I/O's timing out. If this is a NPIV failover
5657 * scenario, the better alternative is to use the move login.
5659 if (rport && rport->fast_io_fail_tmo == -1)
5660 tgt->move_login = 1;
5661 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5663 fc_remote_port_delete(rport);
5668 if (vhost->state == IBMVFC_INITIALIZING) {
5669 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
5670 if (vhost->reinit) {
5672 scsi_block_requests(vhost->host);
5673 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5674 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5676 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
5677 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5678 wake_up(&vhost->init_wait_q);
5679 schedule_work(&vhost->rport_add_work_q);
5680 vhost->init_retries = 0;
5681 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5682 scsi_unblock_requests(vhost->host);
5687 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
5688 vhost->job_step = ibmvfc_discover_targets;
5691 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5692 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5693 scsi_unblock_requests(vhost->host);
5694 wake_up(&vhost->init_wait_q);
5698 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5699 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
5700 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5701 ibmvfc_alloc_targets(vhost);
5702 spin_lock_irqsave(vhost->host->host_lock, flags);
5704 case IBMVFC_HOST_ACTION_TGT_INIT:
5705 list_for_each_entry(tgt, &vhost->targets, queue) {
5706 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5712 if (!ibmvfc_dev_init_to_do(vhost))
5713 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
5719 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5723 * ibmvfc_work - Do task level work
5724 * @data: ibmvfc host struct
5729 static int ibmvfc_work(void *data)
5731 struct ibmvfc_host *vhost = data;
5734 set_user_nice(current, MIN_NICE);
5737 rc = wait_event_interruptible(vhost->work_wait_q,
5738 ibmvfc_work_to_do(vhost));
5742 if (kthread_should_stop())
5745 ibmvfc_do_work(vhost);
5748 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
5753 * ibmvfc_alloc_queue - Allocate queue
5754 * @vhost: ibmvfc host struct
5755 * @queue: ibmvfc queue to allocate
5756 * @fmt: queue format to allocate
5759 * 0 on success / non-zero on failure
5761 static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
5762 struct ibmvfc_queue *queue,
5763 enum ibmvfc_msg_fmt fmt)
5765 struct device *dev = vhost->dev;
5767 unsigned int pool_size = 0;
5770 spin_lock_init(&queue->_lock);
5771 queue->q_lock = &queue->_lock;
5774 case IBMVFC_CRQ_FMT:
5775 fmt_size = sizeof(*queue->msgs.crq);
5776 pool_size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
5778 case IBMVFC_ASYNC_FMT:
5779 fmt_size = sizeof(*queue->msgs.async);
5781 case IBMVFC_SUB_CRQ_FMT:
5782 fmt_size = sizeof(*queue->msgs.scrq);
5783 /* We need one extra event for Cancel Commands */
5784 pool_size = max_requests + 1;
5787 dev_warn(dev, "Unknown command/response queue message format: %d\n", fmt);
5791 if (ibmvfc_init_event_pool(vhost, queue, pool_size)) {
5792 dev_err(dev, "Couldn't initialize event pool.\n");
5796 queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
5797 if (!queue->msgs.handle)
5800 queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
5803 if (dma_mapping_error(dev, queue->msg_token)) {
5804 free_page((unsigned long)queue->msgs.handle);
5805 queue->msgs.handle = NULL;
5811 queue->size = PAGE_SIZE / fmt_size;
5813 queue->vhost = vhost;
5818 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
5819 * @vhost: ibmvfc host struct
5821 * Allocates a page for messages, maps it for dma, and registers
5822 * the crq with the hypervisor.
5825 * zero on success / other on failure
5827 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
5829 int rc, retrc = -ENOMEM;
5830 struct device *dev = vhost->dev;
5831 struct vio_dev *vdev = to_vio_dev(dev);
5832 struct ibmvfc_queue *crq = &vhost->crq;
5835 if (ibmvfc_alloc_queue(vhost, crq, IBMVFC_CRQ_FMT))
5838 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5839 crq->msg_token, PAGE_SIZE);
5841 if (rc == H_RESOURCE)
5842 /* maybe kexecing and resource is busy. try a reset */
5843 retrc = rc = ibmvfc_reset_crq(vhost);
5846 dev_warn(dev, "Partner adapter not ready\n");
5848 dev_warn(dev, "Error %d opening adapter\n", rc);
5849 goto reg_crq_failed;
5854 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
5856 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
5857 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
5858 goto req_irq_failed;
5861 if ((rc = vio_enable_interrupts(vdev))) {
5862 dev_err(dev, "Error %d enabling interrupts\n", rc);
5863 goto req_irq_failed;
5870 tasklet_kill(&vhost->tasklet);
5872 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5873 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5875 ibmvfc_free_queue(vhost, crq);
5879 static int ibmvfc_register_scsi_channel(struct ibmvfc_host *vhost,
5882 struct device *dev = vhost->dev;
5883 struct vio_dev *vdev = to_vio_dev(dev);
5884 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5889 rc = h_reg_sub_crq(vdev->unit_address, scrq->msg_token, PAGE_SIZE,
5890 &scrq->cookie, &scrq->hw_irq);
5892 /* H_CLOSED indicates successful register, but no CRQ partner */
5893 if (rc && rc != H_CLOSED) {
5894 dev_warn(dev, "Error registering sub-crq: %d\n", rc);
5895 if (rc == H_PARAMETER)
5896 dev_warn_once(dev, "Firmware may not support MQ\n");
5900 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
5904 dev_err(dev, "Error mapping sub-crq[%d] irq\n", index);
5908 snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-scsi%d",
5909 vdev->unit_address, index);
5910 rc = request_irq(scrq->irq, ibmvfc_interrupt_scsi, 0, scrq->name, scrq);
5913 dev_err(dev, "Couldn't register sub-crq[%d] irq\n", index);
5914 irq_dispose_mapping(scrq->irq);
5918 scrq->hwq_id = index;
5925 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address, scrq->cookie);
5926 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5932 static void ibmvfc_deregister_scsi_channel(struct ibmvfc_host *vhost, int index)
5934 struct device *dev = vhost->dev;
5935 struct vio_dev *vdev = to_vio_dev(dev);
5936 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5941 free_irq(scrq->irq, scrq);
5942 irq_dispose_mapping(scrq->irq);
5946 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address,
5948 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5951 dev_err(dev, "Failed to free sub-crq[%d]: rc=%ld\n", index, rc);
5953 /* Clean out the queue */
5954 memset(scrq->msgs.crq, 0, PAGE_SIZE);
5960 static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *vhost)
5965 if (!vhost->mq_enabled || !vhost->scsi_scrqs.scrqs)
5968 for (i = 0; i < nr_scsi_hw_queues; i++) {
5969 if (ibmvfc_register_scsi_channel(vhost, i)) {
5970 for (j = i; j > 0; j--)
5971 ibmvfc_deregister_scsi_channel(vhost, j - 1);
5972 vhost->do_enquiry = 0;
5980 static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *vhost)
5985 if (!vhost->mq_enabled || !vhost->scsi_scrqs.scrqs)
5988 for (i = 0; i < nr_scsi_hw_queues; i++)
5989 ibmvfc_deregister_scsi_channel(vhost, i);
5994 static void ibmvfc_init_sub_crqs(struct ibmvfc_host *vhost)
5996 struct ibmvfc_queue *scrq;
6000 if (!vhost->mq_enabled)
6003 vhost->scsi_scrqs.scrqs = kcalloc(nr_scsi_hw_queues,
6004 sizeof(*vhost->scsi_scrqs.scrqs),
6006 if (!vhost->scsi_scrqs.scrqs) {
6007 vhost->do_enquiry = 0;
6011 for (i = 0; i < nr_scsi_hw_queues; i++) {
6012 scrq = &vhost->scsi_scrqs.scrqs[i];
6013 if (ibmvfc_alloc_queue(vhost, scrq, IBMVFC_SUB_CRQ_FMT)) {
6014 for (j = i; j > 0; j--) {
6015 scrq = &vhost->scsi_scrqs.scrqs[j - 1];
6016 ibmvfc_free_queue(vhost, scrq);
6018 kfree(vhost->scsi_scrqs.scrqs);
6019 vhost->scsi_scrqs.scrqs = NULL;
6020 vhost->scsi_scrqs.active_queues = 0;
6021 vhost->do_enquiry = 0;
6022 vhost->mq_enabled = 0;
6027 ibmvfc_reg_sub_crqs(vhost);
6032 static void ibmvfc_release_sub_crqs(struct ibmvfc_host *vhost)
6034 struct ibmvfc_queue *scrq;
6038 if (!vhost->scsi_scrqs.scrqs)
6041 ibmvfc_dereg_sub_crqs(vhost);
6043 for (i = 0; i < nr_scsi_hw_queues; i++) {
6044 scrq = &vhost->scsi_scrqs.scrqs[i];
6045 ibmvfc_free_queue(vhost, scrq);
6048 kfree(vhost->scsi_scrqs.scrqs);
6049 vhost->scsi_scrqs.scrqs = NULL;
6050 vhost->scsi_scrqs.active_queues = 0;
6055 * ibmvfc_free_mem - Free memory for vhost
6056 * @vhost: ibmvfc host struct
6061 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
6063 struct ibmvfc_queue *async_q = &vhost->async_crq;
6066 mempool_destroy(vhost->tgt_pool);
6067 kfree(vhost->trace);
6068 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
6069 vhost->disc_buf_dma);
6070 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
6071 vhost->login_buf, vhost->login_buf_dma);
6072 dma_free_coherent(vhost->dev, sizeof(*vhost->channel_setup_buf),
6073 vhost->channel_setup_buf, vhost->channel_setup_dma);
6074 dma_pool_destroy(vhost->sg_pool);
6075 ibmvfc_free_queue(vhost, async_q);
6080 * ibmvfc_alloc_mem - Allocate memory for vhost
6081 * @vhost: ibmvfc host struct
6084 * 0 on success / non-zero on failure
6086 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
6088 struct ibmvfc_queue *async_q = &vhost->async_crq;
6089 struct device *dev = vhost->dev;
6092 if (ibmvfc_alloc_queue(vhost, async_q, IBMVFC_ASYNC_FMT)) {
6093 dev_err(dev, "Couldn't allocate/map async queue.\n");
6097 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
6098 SG_ALL * sizeof(struct srp_direct_buf),
6099 sizeof(struct srp_direct_buf), 0);
6101 if (!vhost->sg_pool) {
6102 dev_err(dev, "Failed to allocate sg pool\n");
6103 goto unmap_async_crq;
6106 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
6107 &vhost->login_buf_dma, GFP_KERNEL);
6109 if (!vhost->login_buf) {
6110 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
6114 vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
6115 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
6116 &vhost->disc_buf_dma, GFP_KERNEL);
6118 if (!vhost->disc_buf) {
6119 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
6120 goto free_login_buffer;
6123 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
6124 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
6125 atomic_set(&vhost->trace_index, -1);
6128 goto free_disc_buffer;
6130 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
6131 sizeof(struct ibmvfc_target));
6133 if (!vhost->tgt_pool) {
6134 dev_err(dev, "Couldn't allocate target memory pool\n");
6138 vhost->channel_setup_buf = dma_alloc_coherent(dev, sizeof(*vhost->channel_setup_buf),
6139 &vhost->channel_setup_dma,
6142 if (!vhost->channel_setup_buf) {
6143 dev_err(dev, "Couldn't allocate Channel Setup buffer\n");
6151 mempool_destroy(vhost->tgt_pool);
6153 kfree(vhost->trace);
6155 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
6156 vhost->disc_buf_dma);
6158 dma_free_coherent(dev, sizeof(*vhost->login_buf),
6159 vhost->login_buf, vhost->login_buf_dma);
6161 dma_pool_destroy(vhost->sg_pool);
6163 ibmvfc_free_queue(vhost, async_q);
6170 * ibmvfc_rport_add_thread - Worker thread for rport adds
6171 * @work: work struct
6174 static void ibmvfc_rport_add_thread(struct work_struct *work)
6176 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
6178 struct ibmvfc_target *tgt;
6179 struct fc_rport *rport;
6180 unsigned long flags;
6184 spin_lock_irqsave(vhost->host->host_lock, flags);
6187 if (vhost->state != IBMVFC_ACTIVE)
6190 list_for_each_entry(tgt, &vhost->targets, queue) {
6191 if (tgt->add_rport) {
6194 kref_get(&tgt->kref);
6197 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6198 ibmvfc_tgt_add_rport(tgt);
6199 } else if (get_device(&rport->dev)) {
6200 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6201 tgt_dbg(tgt, "Setting rport roles\n");
6202 fc_remote_port_rolechg(rport, tgt->ids.roles);
6203 put_device(&rport->dev);
6205 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6208 kref_put(&tgt->kref, ibmvfc_release_tgt);
6209 spin_lock_irqsave(vhost->host->host_lock, flags);
6215 if (vhost->state == IBMVFC_ACTIVE)
6216 vhost->scan_complete = 1;
6217 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6222 * ibmvfc_probe - Adapter hot plug add entry point
6223 * @vdev: vio device struct
6224 * @id: vio device id struct
6227 * 0 on success / non-zero on failure
6229 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
6231 struct ibmvfc_host *vhost;
6232 struct Scsi_Host *shost;
6233 struct device *dev = &vdev->dev;
6235 unsigned int max_scsi_queues = IBMVFC_MAX_SCSI_QUEUES;
6238 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
6240 dev_err(dev, "Couldn't allocate host data\n");
6244 shost->transportt = ibmvfc_transport_template;
6245 shost->can_queue = max_requests;
6246 shost->max_lun = max_lun;
6247 shost->max_id = max_targets;
6248 shost->max_sectors = IBMVFC_MAX_SECTORS;
6249 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
6250 shost->unique_id = shost->host_no;
6251 shost->nr_hw_queues = mq_enabled ? min(max_scsi_queues, nr_scsi_hw_queues) : 1;
6253 vhost = shost_priv(shost);
6254 INIT_LIST_HEAD(&vhost->targets);
6255 INIT_LIST_HEAD(&vhost->purge);
6256 sprintf(vhost->name, IBMVFC_NAME);
6257 vhost->host = shost;
6259 vhost->partition_number = -1;
6260 vhost->log_level = log_level;
6261 vhost->task_set = 1;
6263 vhost->mq_enabled = mq_enabled;
6264 vhost->client_scsi_channels = min(shost->nr_hw_queues, nr_scsi_channels);
6265 vhost->using_channels = 0;
6266 vhost->do_enquiry = 1;
6267 vhost->scan_timeout = 0;
6269 strcpy(vhost->partition_name, "UNKNOWN");
6270 init_waitqueue_head(&vhost->work_wait_q);
6271 init_waitqueue_head(&vhost->init_wait_q);
6272 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
6273 mutex_init(&vhost->passthru_mutex);
6275 if ((rc = ibmvfc_alloc_mem(vhost)))
6276 goto free_scsi_host;
6278 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
6281 if (IS_ERR(vhost->work_thread)) {
6282 dev_err(dev, "Couldn't create kernel thread: %ld\n",
6283 PTR_ERR(vhost->work_thread));
6284 rc = PTR_ERR(vhost->work_thread);
6288 if ((rc = ibmvfc_init_crq(vhost))) {
6289 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
6293 if ((rc = scsi_add_host(shost, dev)))
6296 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
6298 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
6299 &ibmvfc_trace_attr))) {
6300 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
6304 ibmvfc_init_sub_crqs(vhost);
6306 if (shost_to_fc_host(shost)->rqst_q)
6307 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
6308 dev_set_drvdata(dev, vhost);
6309 spin_lock(&ibmvfc_driver_lock);
6310 list_add_tail(&vhost->queue, &ibmvfc_head);
6311 spin_unlock(&ibmvfc_driver_lock);
6313 ibmvfc_send_crq_init(vhost);
6314 scsi_scan_host(shost);
6318 scsi_remove_host(shost);
6320 ibmvfc_release_crq_queue(vhost);
6322 kthread_stop(vhost->work_thread);
6324 ibmvfc_free_mem(vhost);
6326 scsi_host_put(shost);
6333 * ibmvfc_remove - Adapter hot plug remove entry point
6334 * @vdev: vio device struct
6339 static void ibmvfc_remove(struct vio_dev *vdev)
6341 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
6343 unsigned long flags;
6346 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
6348 spin_lock_irqsave(vhost->host->host_lock, flags);
6349 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
6350 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6352 ibmvfc_wait_while_resetting(vhost);
6353 kthread_stop(vhost->work_thread);
6354 fc_remove_host(vhost->host);
6355 scsi_remove_host(vhost->host);
6357 spin_lock_irqsave(vhost->host->host_lock, flags);
6358 ibmvfc_purge_requests(vhost, DID_ERROR);
6359 list_splice_init(&vhost->purge, &purge);
6360 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6361 ibmvfc_complete_purge(&purge);
6362 ibmvfc_release_sub_crqs(vhost);
6363 ibmvfc_release_crq_queue(vhost);
6365 ibmvfc_free_mem(vhost);
6366 spin_lock(&ibmvfc_driver_lock);
6367 list_del(&vhost->queue);
6368 spin_unlock(&ibmvfc_driver_lock);
6369 scsi_host_put(vhost->host);
6374 * ibmvfc_resume - Resume from suspend
6375 * @dev: device struct
6377 * We may have lost an interrupt across suspend/resume, so kick the
6381 static int ibmvfc_resume(struct device *dev)
6383 unsigned long flags;
6384 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
6385 struct vio_dev *vdev = to_vio_dev(dev);
6387 spin_lock_irqsave(vhost->host->host_lock, flags);
6388 vio_disable_interrupts(vdev);
6389 tasklet_schedule(&vhost->tasklet);
6390 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6395 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
6396 * @vdev: vio device struct
6399 * Number of bytes the driver will need to DMA map at the same time in
6400 * order to perform well.
6402 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
6404 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
6405 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
6408 static const struct vio_device_id ibmvfc_device_table[] = {
6409 {"fcp", "IBM,vfc-client"},
6412 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
6414 static const struct dev_pm_ops ibmvfc_pm_ops = {
6415 .resume = ibmvfc_resume
6418 static struct vio_driver ibmvfc_driver = {
6419 .id_table = ibmvfc_device_table,
6420 .probe = ibmvfc_probe,
6421 .remove = ibmvfc_remove,
6422 .get_desired_dma = ibmvfc_get_desired_dma,
6423 .name = IBMVFC_NAME,
6424 .pm = &ibmvfc_pm_ops,
6427 static struct fc_function_template ibmvfc_transport_functions = {
6428 .show_host_fabric_name = 1,
6429 .show_host_node_name = 1,
6430 .show_host_port_name = 1,
6431 .show_host_supported_classes = 1,
6432 .show_host_port_type = 1,
6433 .show_host_port_id = 1,
6434 .show_host_maxframe_size = 1,
6436 .get_host_port_state = ibmvfc_get_host_port_state,
6437 .show_host_port_state = 1,
6439 .get_host_speed = ibmvfc_get_host_speed,
6440 .show_host_speed = 1,
6442 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
6443 .terminate_rport_io = ibmvfc_terminate_rport_io,
6445 .show_rport_maxframe_size = 1,
6446 .show_rport_supported_classes = 1,
6448 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
6449 .show_rport_dev_loss_tmo = 1,
6451 .get_starget_node_name = ibmvfc_get_starget_node_name,
6452 .show_starget_node_name = 1,
6454 .get_starget_port_name = ibmvfc_get_starget_port_name,
6455 .show_starget_port_name = 1,
6457 .get_starget_port_id = ibmvfc_get_starget_port_id,
6458 .show_starget_port_id = 1,
6460 .bsg_request = ibmvfc_bsg_request,
6461 .bsg_timeout = ibmvfc_bsg_timeout,
6465 * ibmvfc_module_init - Initialize the ibmvfc module
6468 * 0 on success / other on failure
6470 static int __init ibmvfc_module_init(void)
6474 if (!firmware_has_feature(FW_FEATURE_VIO))
6477 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
6478 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
6480 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
6481 if (!ibmvfc_transport_template)
6484 rc = vio_register_driver(&ibmvfc_driver);
6486 fc_release_transport(ibmvfc_transport_template);
6491 * ibmvfc_module_exit - Teardown the ibmvfc module
6496 static void __exit ibmvfc_module_exit(void)
6498 vio_unregister_driver(&ibmvfc_driver);
6499 fc_release_transport(ibmvfc_transport_template);
6502 module_init(ibmvfc_module_init);
6503 module_exit(ibmvfc_module_exit);