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 BUG_ON(list_empty(&queue->free));
1522 evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
1523 atomic_set(&evt->free, 0);
1524 list_del(&evt->queue_list);
1525 spin_unlock_irqrestore(&queue->l_lock, flags);
1530 * ibmvfc_locked_done - Calls evt completion with host_lock held
1531 * @evt: ibmvfc evt to complete
1533 * All non-scsi command completion callbacks have the expectation that the
1534 * host_lock is held. This callback is used by ibmvfc_init_event to wrap a
1535 * MAD evt with the host_lock.
1537 static void ibmvfc_locked_done(struct ibmvfc_event *evt)
1539 unsigned long flags;
1541 spin_lock_irqsave(evt->vhost->host->host_lock, flags);
1543 spin_unlock_irqrestore(evt->vhost->host->host_lock, flags);
1547 * ibmvfc_init_event - Initialize fields in an event struct that are always
1550 * @done: Routine to call when the event is responded to
1551 * @format: SRP or MAD format
1553 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1554 void (*done) (struct ibmvfc_event *), u8 format)
1557 evt->sync_iu = NULL;
1558 evt->eh_comp = NULL;
1559 evt->crq.format = format;
1560 if (format == IBMVFC_CMD_FORMAT)
1564 evt->done = ibmvfc_locked_done;
1570 * ibmvfc_map_sg_list - Initialize scatterlist
1571 * @scmd: scsi command struct
1572 * @nseg: number of scatterlist segments
1573 * @md: memory descriptor list to initialize
1575 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1576 struct srp_direct_buf *md)
1579 struct scatterlist *sg;
1581 scsi_for_each_sg(scmd, sg, nseg, i) {
1582 md[i].va = cpu_to_be64(sg_dma_address(sg));
1583 md[i].len = cpu_to_be32(sg_dma_len(sg));
1589 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
1590 * @scmd: struct scsi_cmnd with the scatterlist
1591 * @evt: ibmvfc event struct
1592 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1593 * @dev: device for which to map dma memory
1596 * 0 on success / non-zero on failure
1598 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1599 struct ibmvfc_event *evt,
1600 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1604 struct srp_direct_buf *data = &vfc_cmd->ioba;
1605 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1606 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(evt->vhost, vfc_cmd);
1609 vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1611 sg_mapped = scsi_dma_map(scmd);
1613 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1615 } else if (unlikely(sg_mapped < 0)) {
1616 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1617 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1621 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1622 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1623 iu->add_cdb_len |= IBMVFC_WRDATA;
1625 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1626 iu->add_cdb_len |= IBMVFC_RDDATA;
1629 if (sg_mapped == 1) {
1630 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1634 vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1636 if (!evt->ext_list) {
1637 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1638 &evt->ext_list_token);
1640 if (!evt->ext_list) {
1641 scsi_dma_unmap(scmd);
1642 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1643 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1648 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1650 data->va = cpu_to_be64(evt->ext_list_token);
1651 data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1657 * ibmvfc_timeout - Internal command timeout handler
1658 * @t: struct ibmvfc_event that timed out
1660 * Called when an internally generated command times out
1662 static void ibmvfc_timeout(struct timer_list *t)
1664 struct ibmvfc_event *evt = from_timer(evt, t, timer);
1665 struct ibmvfc_host *vhost = evt->vhost;
1666 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1667 ibmvfc_reset_host(vhost);
1671 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1672 * @evt: event to be sent
1673 * @vhost: ibmvfc host struct
1674 * @timeout: timeout in seconds - 0 means do not time command
1676 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1678 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1679 struct ibmvfc_host *vhost, unsigned long timeout)
1681 __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1682 unsigned long flags;
1685 /* Copy the IU into the transfer area */
1686 *evt->xfer_iu = evt->iu;
1687 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1688 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1689 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1690 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1694 timer_setup(&evt->timer, ibmvfc_timeout, 0);
1697 evt->timer.expires = jiffies + (timeout * HZ);
1698 add_timer(&evt->timer);
1701 spin_lock_irqsave(&evt->queue->l_lock, flags);
1702 list_add_tail(&evt->queue_list, &evt->queue->sent);
1703 atomic_set(&evt->active, 1);
1707 if (evt->queue->fmt == IBMVFC_SUB_CRQ_FMT)
1708 rc = ibmvfc_send_sub_crq(vhost,
1709 evt->queue->vios_cookie,
1710 be64_to_cpu(crq_as_u64[0]),
1711 be64_to_cpu(crq_as_u64[1]),
1714 rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1715 be64_to_cpu(crq_as_u64[1]));
1718 atomic_set(&evt->active, 0);
1719 list_del(&evt->queue_list);
1720 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1721 del_timer(&evt->timer);
1723 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1724 * Firmware will send a CRQ with a transport event (0xFF) to
1725 * tell this client what has happened to the transport. This
1726 * will be handled in ibmvfc_handle_crq()
1728 if (rc == H_CLOSED) {
1729 if (printk_ratelimit())
1730 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1732 scsi_dma_unmap(evt->cmnd);
1733 ibmvfc_free_event(evt);
1734 return SCSI_MLQUEUE_HOST_BUSY;
1737 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1739 evt->cmnd->result = DID_ERROR << 16;
1740 evt->done = ibmvfc_scsi_eh_done;
1742 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1746 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1747 ibmvfc_trc_start(evt);
1754 * ibmvfc_log_error - Log an error for the failed command if appropriate
1755 * @evt: ibmvfc event to log
1758 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1760 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1761 struct ibmvfc_host *vhost = evt->vhost;
1762 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
1763 struct scsi_cmnd *cmnd = evt->cmnd;
1764 const char *err = unknown_error;
1765 int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1770 logerr = cmd_status[index].log;
1771 err = cmd_status[index].name;
1774 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1777 if (rsp->flags & FCP_RSP_LEN_VALID)
1778 rsp_code = rsp->data.info.rsp_code;
1780 scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
1781 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1782 cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
1783 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1787 * ibmvfc_relogin - Log back into the specified device
1788 * @sdev: scsi device struct
1791 static void ibmvfc_relogin(struct scsi_device *sdev)
1793 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1794 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1795 struct ibmvfc_target *tgt;
1796 unsigned long flags;
1798 spin_lock_irqsave(vhost->host->host_lock, flags);
1799 list_for_each_entry(tgt, &vhost->targets, queue) {
1800 if (rport == tgt->rport) {
1801 ibmvfc_del_tgt(tgt);
1806 ibmvfc_reinit_host(vhost);
1807 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1811 * ibmvfc_scsi_done - Handle responses from commands
1812 * @evt: ibmvfc event to be handled
1814 * Used as a callback when sending scsi cmds.
1816 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1818 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1819 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(evt->vhost, vfc_cmd);
1820 struct scsi_cmnd *cmnd = evt->cmnd;
1822 u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1825 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1826 scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1827 else if (rsp->flags & FCP_RESID_UNDER)
1828 scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1830 scsi_set_resid(cmnd, 0);
1832 if (vfc_cmd->status) {
1833 cmnd->result = ibmvfc_get_err_result(evt->vhost, vfc_cmd);
1835 if (rsp->flags & FCP_RSP_LEN_VALID)
1836 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1837 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1838 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1839 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1840 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1841 if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1842 (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1843 ibmvfc_relogin(cmnd->device);
1845 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1846 cmnd->result = (DID_ERROR << 16);
1848 ibmvfc_log_error(evt);
1851 if (!cmnd->result &&
1852 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1853 cmnd->result = (DID_ERROR << 16);
1855 scsi_dma_unmap(cmnd);
1859 ibmvfc_free_event(evt);
1863 * ibmvfc_host_chkready - Check if the host can accept commands
1864 * @vhost: struct ibmvfc host
1867 * 1 if host can accept command / 0 if not
1869 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1873 switch (vhost->state) {
1874 case IBMVFC_LINK_DEAD:
1875 case IBMVFC_HOST_OFFLINE:
1876 result = DID_NO_CONNECT << 16;
1879 case IBMVFC_INITIALIZING:
1881 case IBMVFC_LINK_DOWN:
1882 result = DID_REQUEUE << 16;
1892 static struct ibmvfc_cmd *ibmvfc_init_vfc_cmd(struct ibmvfc_event *evt, struct scsi_device *sdev)
1894 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1895 struct ibmvfc_host *vhost = evt->vhost;
1896 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
1897 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
1898 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
1901 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1902 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
1903 offset = offsetof(struct ibmvfc_cmd, v2.rsp);
1904 vfc_cmd->target_wwpn = cpu_to_be64(rport->port_name);
1906 offset = offsetof(struct ibmvfc_cmd, v1.rsp);
1907 vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offset);
1908 vfc_cmd->resp.len = cpu_to_be32(sizeof(*rsp));
1909 vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1910 vfc_cmd->payload_len = cpu_to_be32(sizeof(*iu));
1911 vfc_cmd->resp_len = cpu_to_be32(sizeof(*rsp));
1912 vfc_cmd->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
1913 vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1914 int_to_scsilun(sdev->lun, &iu->lun);
1920 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1921 * @shost: scsi host struct
1922 * @cmnd: struct scsi_cmnd to be executed
1925 * 0 on success / other on failure
1927 static int ibmvfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
1929 struct ibmvfc_host *vhost = shost_priv(shost);
1930 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1931 struct ibmvfc_cmd *vfc_cmd;
1932 struct ibmvfc_fcp_cmd_iu *iu;
1933 struct ibmvfc_event *evt;
1934 u32 tag_and_hwq = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
1935 u16 hwq = blk_mq_unique_tag_to_hwq(tag_and_hwq);
1939 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1940 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1946 cmnd->result = (DID_OK << 16);
1947 if (vhost->using_channels) {
1948 scsi_channel = hwq % vhost->scsi_scrqs.active_queues;
1949 evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[scsi_channel]);
1950 evt->hwq = hwq % vhost->scsi_scrqs.active_queues;
1952 evt = ibmvfc_get_event(&vhost->crq);
1954 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1957 vfc_cmd = ibmvfc_init_vfc_cmd(evt, cmnd->device);
1958 iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
1960 iu->xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1961 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
1963 if (cmnd->flags & SCMD_TAGGED) {
1964 vfc_cmd->task_tag = cpu_to_be64(scsi_cmd_to_rq(cmnd)->tag);
1965 iu->pri_task_attr = IBMVFC_SIMPLE_TASK;
1968 vfc_cmd->correlation = cpu_to_be64((u64)evt);
1970 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1971 return ibmvfc_send_event(evt, vhost, 0);
1973 ibmvfc_free_event(evt);
1975 return SCSI_MLQUEUE_HOST_BUSY;
1977 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1978 scmd_printk(KERN_ERR, cmnd,
1979 "Failed to map DMA buffer for command. rc=%d\n", rc);
1981 cmnd->result = DID_ERROR << 16;
1987 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1988 * @evt: ibmvfc event struct
1991 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1993 /* copy the response back */
1995 *evt->sync_iu = *evt->xfer_iu;
1997 complete(&evt->comp);
2001 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
2002 * @evt: struct ibmvfc_event
2005 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
2007 struct ibmvfc_host *vhost = evt->vhost;
2009 ibmvfc_free_event(evt);
2010 vhost->aborting_passthru = 0;
2011 dev_info(vhost->dev, "Passthru command cancelled\n");
2015 * ibmvfc_bsg_timeout - Handle a BSG timeout
2016 * @job: struct bsg_job that timed out
2019 * 0 on success / other on failure
2021 static int ibmvfc_bsg_timeout(struct bsg_job *job)
2023 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2024 unsigned long port_id = (unsigned long)job->dd_data;
2025 struct ibmvfc_event *evt;
2026 struct ibmvfc_tmf *tmf;
2027 unsigned long flags;
2031 spin_lock_irqsave(vhost->host->host_lock, flags);
2032 if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
2033 __ibmvfc_reset_host(vhost);
2034 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2038 vhost->aborting_passthru = 1;
2039 evt = ibmvfc_get_event(&vhost->crq);
2040 ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
2043 memset(tmf, 0, sizeof(*tmf));
2044 tmf->common.version = cpu_to_be32(1);
2045 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2046 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2047 tmf->scsi_id = cpu_to_be64(port_id);
2048 tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2049 tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
2050 rc = ibmvfc_send_event(evt, vhost, default_timeout);
2053 vhost->aborting_passthru = 0;
2054 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
2057 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
2060 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2067 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
2068 * @vhost: struct ibmvfc_host to send command
2069 * @port_id: port ID to send command
2072 * 0 on success / other on failure
2074 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
2076 struct ibmvfc_port_login *plogi;
2077 struct ibmvfc_target *tgt;
2078 struct ibmvfc_event *evt;
2079 union ibmvfc_iu rsp_iu;
2080 unsigned long flags;
2081 int rc = 0, issue_login = 1;
2084 spin_lock_irqsave(vhost->host->host_lock, flags);
2085 list_for_each_entry(tgt, &vhost->targets, queue) {
2086 if (tgt->scsi_id == port_id) {
2094 if (unlikely((rc = ibmvfc_host_chkready(vhost))))
2097 evt = ibmvfc_get_event(&vhost->crq);
2098 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2099 plogi = &evt->iu.plogi;
2100 memset(plogi, 0, sizeof(*plogi));
2101 plogi->common.version = cpu_to_be32(1);
2102 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
2103 plogi->common.length = cpu_to_be16(sizeof(*plogi));
2104 plogi->scsi_id = cpu_to_be64(port_id);
2105 evt->sync_iu = &rsp_iu;
2106 init_completion(&evt->comp);
2108 rc = ibmvfc_send_event(evt, vhost, default_timeout);
2109 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2114 wait_for_completion(&evt->comp);
2116 if (rsp_iu.plogi.common.status)
2119 spin_lock_irqsave(vhost->host->host_lock, flags);
2120 ibmvfc_free_event(evt);
2122 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2128 * ibmvfc_bsg_request - Handle a BSG request
2129 * @job: struct bsg_job to be executed
2132 * 0 on success / other on failure
2134 static int ibmvfc_bsg_request(struct bsg_job *job)
2136 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2137 struct fc_rport *rport = fc_bsg_to_rport(job);
2138 struct ibmvfc_passthru_mad *mad;
2139 struct ibmvfc_event *evt;
2140 union ibmvfc_iu rsp_iu;
2141 unsigned long flags, port_id = -1;
2142 struct fc_bsg_request *bsg_request = job->request;
2143 struct fc_bsg_reply *bsg_reply = job->reply;
2144 unsigned int code = bsg_request->msgcode;
2145 int rc = 0, req_seg, rsp_seg, issue_login = 0;
2146 u32 fc_flags, rsp_len;
2149 bsg_reply->reply_payload_rcv_len = 0;
2151 port_id = rport->port_id;
2154 case FC_BSG_HST_ELS_NOLOGIN:
2155 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
2156 (bsg_request->rqst_data.h_els.port_id[1] << 8) |
2157 bsg_request->rqst_data.h_els.port_id[2];
2159 case FC_BSG_RPT_ELS:
2160 fc_flags = IBMVFC_FC_ELS;
2164 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
2165 (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
2166 bsg_request->rqst_data.h_ct.port_id[2];
2169 fc_flags = IBMVFC_FC_CT_IU;
2177 if (!mutex_trylock(&vhost->passthru_mutex))
2180 job->dd_data = (void *)port_id;
2181 req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
2182 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2185 mutex_unlock(&vhost->passthru_mutex);
2189 rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
2190 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2193 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2194 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2195 mutex_unlock(&vhost->passthru_mutex);
2199 if (req_seg > 1 || rsp_seg > 1) {
2205 rc = ibmvfc_bsg_plogi(vhost, port_id);
2207 spin_lock_irqsave(vhost->host->host_lock, flags);
2209 if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
2210 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
2211 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2215 evt = ibmvfc_get_event(&vhost->crq);
2216 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2217 mad = &evt->iu.passthru;
2219 memset(mad, 0, sizeof(*mad));
2220 mad->common.version = cpu_to_be32(1);
2221 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
2222 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
2224 mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
2225 offsetof(struct ibmvfc_passthru_mad, iu));
2226 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
2228 mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
2229 mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
2230 mad->iu.flags = cpu_to_be32(fc_flags);
2231 mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2233 mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
2234 mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
2235 mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
2236 mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
2237 mad->iu.scsi_id = cpu_to_be64(port_id);
2238 mad->iu.tag = cpu_to_be64((u64)evt);
2239 rsp_len = be32_to_cpu(mad->iu.rsp.len);
2241 evt->sync_iu = &rsp_iu;
2242 init_completion(&evt->comp);
2243 rc = ibmvfc_send_event(evt, vhost, 0);
2244 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2251 wait_for_completion(&evt->comp);
2253 if (rsp_iu.passthru.common.status)
2256 bsg_reply->reply_payload_rcv_len = rsp_len;
2258 spin_lock_irqsave(vhost->host->host_lock, flags);
2259 ibmvfc_free_event(evt);
2260 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2261 bsg_reply->result = rc;
2262 bsg_job_done(job, bsg_reply->result,
2263 bsg_reply->reply_payload_rcv_len);
2266 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2267 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2268 dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
2269 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2270 mutex_unlock(&vhost->passthru_mutex);
2276 * ibmvfc_reset_device - Reset the device with the specified reset type
2277 * @sdev: scsi device to reset
2279 * @desc: reset type description for log messages
2282 * 0 on success / other on failure
2284 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
2286 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2287 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2288 struct ibmvfc_cmd *tmf;
2289 struct ibmvfc_event *evt = NULL;
2290 union ibmvfc_iu rsp_iu;
2291 struct ibmvfc_fcp_cmd_iu *iu;
2292 struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2293 int rsp_rc = -EBUSY;
2294 unsigned long flags;
2297 spin_lock_irqsave(vhost->host->host_lock, flags);
2298 if (vhost->state == IBMVFC_ACTIVE) {
2299 if (vhost->using_channels)
2300 evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[0]);
2302 evt = ibmvfc_get_event(&vhost->crq);
2304 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2305 tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2306 iu = ibmvfc_get_fcp_iu(vhost, tmf);
2308 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2309 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2310 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2311 iu->tmf_flags = type;
2312 evt->sync_iu = &rsp_iu;
2314 init_completion(&evt->comp);
2315 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2317 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2320 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2325 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2326 wait_for_completion(&evt->comp);
2328 if (rsp_iu.cmd.status)
2329 rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2332 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2333 rsp_code = fc_rsp->data.info.rsp_code;
2335 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2336 "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2337 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2338 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2339 fc_rsp->scsi_status);
2342 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2344 spin_lock_irqsave(vhost->host->host_lock, flags);
2345 ibmvfc_free_event(evt);
2346 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2351 * ibmvfc_match_rport - Match function for specified remote port
2352 * @evt: ibmvfc event struct
2353 * @rport: device to match
2356 * 1 if event matches rport / 0 if event does not match rport
2358 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2360 struct fc_rport *cmd_rport;
2363 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2364 if (cmd_rport == rport)
2371 * ibmvfc_match_target - Match function for specified target
2372 * @evt: ibmvfc event struct
2373 * @device: device to match (starget)
2376 * 1 if event matches starget / 0 if event does not match starget
2378 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2380 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2386 * ibmvfc_match_lun - Match function for specified LUN
2387 * @evt: ibmvfc event struct
2388 * @device: device to match (sdev)
2391 * 1 if event matches sdev / 0 if event does not match sdev
2393 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2395 if (evt->cmnd && evt->cmnd->device == device)
2401 * ibmvfc_event_is_free - Check if event is free or not
2402 * @evt: ibmvfc event struct
2407 static bool ibmvfc_event_is_free(struct ibmvfc_event *evt)
2409 struct ibmvfc_event *loop_evt;
2411 list_for_each_entry(loop_evt, &evt->queue->free, queue_list)
2412 if (loop_evt == evt)
2419 * ibmvfc_wait_for_ops - Wait for ops to complete
2420 * @vhost: ibmvfc host struct
2421 * @device: device to match (starget or sdev)
2422 * @match: match function
2427 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2428 int (*match) (struct ibmvfc_event *, void *))
2430 struct ibmvfc_event *evt;
2431 DECLARE_COMPLETION_ONSTACK(comp);
2432 int wait, i, q_index, q_size;
2433 unsigned long flags;
2434 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2435 struct ibmvfc_queue *queues;
2438 if (vhost->mq_enabled && vhost->using_channels) {
2439 queues = vhost->scsi_scrqs.scrqs;
2440 q_size = vhost->scsi_scrqs.active_queues;
2442 queues = &vhost->crq;
2448 spin_lock_irqsave(vhost->host->host_lock, flags);
2449 for (q_index = 0; q_index < q_size; q_index++) {
2450 spin_lock(&queues[q_index].l_lock);
2451 for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2452 evt = &queues[q_index].evt_pool.events[i];
2453 if (!ibmvfc_event_is_free(evt)) {
2454 if (match(evt, device)) {
2455 evt->eh_comp = ∁
2460 spin_unlock(&queues[q_index].l_lock);
2462 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2465 timeout = wait_for_completion_timeout(&comp, timeout);
2469 spin_lock_irqsave(vhost->host->host_lock, flags);
2470 for (q_index = 0; q_index < q_size; q_index++) {
2471 spin_lock(&queues[q_index].l_lock);
2472 for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2473 evt = &queues[q_index].evt_pool.events[i];
2474 if (!ibmvfc_event_is_free(evt)) {
2475 if (match(evt, device)) {
2476 evt->eh_comp = NULL;
2481 spin_unlock(&queues[q_index].l_lock);
2483 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2485 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2487 return wait ? FAILED : SUCCESS;
2496 static struct ibmvfc_event *ibmvfc_init_tmf(struct ibmvfc_queue *queue,
2497 struct scsi_device *sdev,
2500 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2501 struct scsi_target *starget = scsi_target(sdev);
2502 struct fc_rport *rport = starget_to_rport(starget);
2503 struct ibmvfc_event *evt;
2504 struct ibmvfc_tmf *tmf;
2506 evt = ibmvfc_get_event(queue);
2507 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2510 memset(tmf, 0, sizeof(*tmf));
2511 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
2512 tmf->common.version = cpu_to_be32(2);
2513 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2515 tmf->common.version = cpu_to_be32(1);
2517 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2518 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2519 tmf->scsi_id = cpu_to_be64(rport->port_id);
2520 int_to_scsilun(sdev->lun, &tmf->lun);
2521 if (!ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPRESS_ABTS))
2522 type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2523 if (vhost->state == IBMVFC_ACTIVE)
2524 tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2526 tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2527 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2528 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2530 init_completion(&evt->comp);
2535 static int ibmvfc_cancel_all_mq(struct scsi_device *sdev, int type)
2537 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2538 struct ibmvfc_event *evt, *found_evt, *temp;
2539 struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
2540 unsigned long flags;
2547 spin_lock_irqsave(vhost->host->host_lock, flags);
2548 num_hwq = vhost->scsi_scrqs.active_queues;
2549 for (i = 0; i < num_hwq; i++) {
2550 spin_lock(queues[i].q_lock);
2551 spin_lock(&queues[i].l_lock);
2553 list_for_each_entry(evt, &queues[i].sent, queue_list) {
2554 if (evt->cmnd && evt->cmnd->device == sdev) {
2559 spin_unlock(&queues[i].l_lock);
2561 if (found_evt && vhost->logged_in) {
2562 evt = ibmvfc_init_tmf(&queues[i], sdev, type);
2563 evt->sync_iu = &queues[i].cancel_rsp;
2564 ibmvfc_send_event(evt, vhost, default_timeout);
2565 list_add_tail(&evt->cancel, &cancelq);
2568 spin_unlock(queues[i].q_lock);
2570 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2572 if (list_empty(&cancelq)) {
2573 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2574 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2578 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2580 list_for_each_entry_safe(evt, temp, &cancelq, cancel) {
2581 wait_for_completion(&evt->comp);
2582 status = be16_to_cpu(evt->queue->cancel_rsp.mad_common.status);
2583 list_del(&evt->cancel);
2584 ibmvfc_free_event(evt);
2586 if (status != IBMVFC_MAD_SUCCESS) {
2587 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2589 case IBMVFC_MAD_DRIVER_FAILED:
2590 case IBMVFC_MAD_CRQ_ERROR:
2591 /* Host adapter most likely going through reset, return success to
2592 * the caller will wait for the command being cancelled to get returned
2605 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2610 static int ibmvfc_cancel_all_sq(struct scsi_device *sdev, int type)
2612 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2613 struct ibmvfc_event *evt, *found_evt;
2614 union ibmvfc_iu rsp;
2615 int rsp_rc = -EBUSY;
2616 unsigned long flags;
2621 spin_lock_irqsave(vhost->host->host_lock, flags);
2622 spin_lock(&vhost->crq.l_lock);
2623 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2624 if (evt->cmnd && evt->cmnd->device == sdev) {
2629 spin_unlock(&vhost->crq.l_lock);
2632 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2633 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2634 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2638 if (vhost->logged_in) {
2639 evt = ibmvfc_init_tmf(&vhost->crq, sdev, type);
2640 evt->sync_iu = &rsp;
2641 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2644 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2647 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2648 /* If failure is received, the host adapter is most likely going
2649 through reset, return success so the caller will wait for the command
2650 being cancelled to get returned */
2654 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2656 wait_for_completion(&evt->comp);
2657 status = be16_to_cpu(rsp.mad_common.status);
2658 spin_lock_irqsave(vhost->host->host_lock, flags);
2659 ibmvfc_free_event(evt);
2660 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2662 if (status != IBMVFC_MAD_SUCCESS) {
2663 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2665 case IBMVFC_MAD_DRIVER_FAILED:
2666 case IBMVFC_MAD_CRQ_ERROR:
2667 /* Host adapter most likely going through reset, return success to
2668 the caller will wait for the command being cancelled to get returned */
2675 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2680 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2681 * @sdev: scsi device to cancel commands
2682 * @type: type of error recovery being performed
2684 * This sends a cancel to the VIOS for the specified device. This does
2685 * NOT send any abort to the actual device. That must be done separately.
2688 * 0 on success / other on failure
2690 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2692 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2694 if (vhost->mq_enabled && vhost->using_channels)
2695 return ibmvfc_cancel_all_mq(sdev, type);
2697 return ibmvfc_cancel_all_sq(sdev, type);
2701 * ibmvfc_match_key - Match function for specified cancel key
2702 * @evt: ibmvfc event struct
2703 * @key: cancel key to match
2706 * 1 if event matches key / 0 if event does not match key
2708 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2710 unsigned long cancel_key = (unsigned long)key;
2712 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2713 be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2719 * ibmvfc_match_evt - Match function for specified event
2720 * @evt: ibmvfc event struct
2721 * @match: event to match
2724 * 1 if event matches key / 0 if event does not match key
2726 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2734 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2735 * @sdev: scsi device to abort commands
2737 * This sends an Abort Task Set to the VIOS for the specified device. This does
2738 * NOT send any cancel to the VIOS. That must be done separately.
2741 * 0 on success / other on failure
2743 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2745 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2746 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2747 struct ibmvfc_cmd *tmf;
2748 struct ibmvfc_event *evt, *found_evt;
2749 union ibmvfc_iu rsp_iu;
2750 struct ibmvfc_fcp_cmd_iu *iu;
2751 struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2752 int rc, rsp_rc = -EBUSY;
2753 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2757 spin_lock_irqsave(vhost->host->host_lock, flags);
2758 spin_lock(&vhost->crq.l_lock);
2759 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2760 if (evt->cmnd && evt->cmnd->device == sdev) {
2765 spin_unlock(&vhost->crq.l_lock);
2768 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2769 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2770 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2774 if (vhost->state == IBMVFC_ACTIVE) {
2775 evt = ibmvfc_get_event(&vhost->crq);
2776 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2777 tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2778 iu = ibmvfc_get_fcp_iu(vhost, tmf);
2780 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2781 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2782 iu->tmf_flags = IBMVFC_ABORT_TASK_SET;
2783 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2784 evt->sync_iu = &rsp_iu;
2786 tmf->correlation = cpu_to_be64((u64)evt);
2788 init_completion(&evt->comp);
2789 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2792 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2795 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2799 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2800 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2803 rc = ibmvfc_cancel_all(sdev, 0);
2805 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2811 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2812 ibmvfc_reset_host(vhost);
2814 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2819 rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2820 if (rc != SUCCESS) {
2821 spin_lock_irqsave(vhost->host->host_lock, flags);
2822 ibmvfc_hard_reset_host(vhost);
2823 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2831 if (rsp_iu.cmd.status)
2832 rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2835 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2836 rsp_code = fc_rsp->data.info.rsp_code;
2838 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2839 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2840 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2841 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2842 fc_rsp->scsi_status);
2845 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2848 spin_lock_irqsave(vhost->host->host_lock, flags);
2849 ibmvfc_free_event(evt);
2850 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2855 * ibmvfc_eh_abort_handler - Abort a command
2856 * @cmd: scsi command to abort
2859 * SUCCESS / FAST_IO_FAIL / FAILED
2861 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2863 struct scsi_device *sdev = cmd->device;
2864 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2865 int cancel_rc, block_rc;
2869 block_rc = fc_block_scsi_eh(cmd);
2870 ibmvfc_wait_while_resetting(vhost);
2871 if (block_rc != FAST_IO_FAIL) {
2872 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2873 ibmvfc_abort_task_set(sdev);
2875 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2878 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2880 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2888 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2889 * @cmd: scsi command struct
2892 * SUCCESS / FAST_IO_FAIL / FAILED
2894 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2896 struct scsi_device *sdev = cmd->device;
2897 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2898 int cancel_rc, block_rc, reset_rc = 0;
2902 block_rc = fc_block_scsi_eh(cmd);
2903 ibmvfc_wait_while_resetting(vhost);
2904 if (block_rc != FAST_IO_FAIL) {
2905 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2906 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2908 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2910 if (!cancel_rc && !reset_rc)
2911 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2913 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2921 * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2922 * @sdev: scsi device struct
2923 * @data: return code
2926 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2928 unsigned long *rc = data;
2929 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2933 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2934 * @sdev: scsi device struct
2935 * @data: return code
2938 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2940 unsigned long *rc = data;
2941 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2945 * ibmvfc_eh_target_reset_handler - Reset the target
2946 * @cmd: scsi command struct
2949 * SUCCESS / FAST_IO_FAIL / FAILED
2951 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2953 struct scsi_device *sdev = cmd->device;
2954 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2955 struct scsi_target *starget = scsi_target(sdev);
2959 unsigned long cancel_rc = 0;
2962 block_rc = fc_block_scsi_eh(cmd);
2963 ibmvfc_wait_while_resetting(vhost);
2964 if (block_rc != FAST_IO_FAIL) {
2965 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2966 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2968 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2970 if (!cancel_rc && !reset_rc)
2971 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2973 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2981 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2982 * @cmd: struct scsi_cmnd having problems
2985 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2988 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2990 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2991 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2993 return rc ? FAILED : SUCCESS;
2997 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2998 * @rport: rport struct
3003 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
3005 struct Scsi_Host *shost = rport_to_shost(rport);
3006 struct ibmvfc_host *vhost = shost_priv(shost);
3007 struct fc_rport *dev_rport;
3008 struct scsi_device *sdev;
3009 struct ibmvfc_target *tgt;
3010 unsigned long rc, flags;
3014 shost_for_each_device(sdev, shost) {
3015 dev_rport = starget_to_rport(scsi_target(sdev));
3016 if (dev_rport != rport)
3018 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
3021 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
3024 ibmvfc_issue_fc_host_lip(shost);
3026 spin_lock_irqsave(shost->host_lock, flags);
3028 list_for_each_entry(tgt, &vhost->targets, queue) {
3029 if (tgt->scsi_id == rport->port_id) {
3035 if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
3037 * If we get here, that means we previously attempted to send
3038 * an implicit logout to the target but it failed, most likely
3039 * due to I/O being pending, so we need to send it again
3041 ibmvfc_del_tgt(tgt);
3042 ibmvfc_reinit_host(vhost);
3045 spin_unlock_irqrestore(shost->host_lock, flags);
3049 static const struct ibmvfc_async_desc ae_desc [] = {
3050 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3051 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3052 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3053 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3054 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3055 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
3056 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
3057 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
3058 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
3059 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
3060 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
3061 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
3062 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
3065 static const struct ibmvfc_async_desc unknown_ae = {
3066 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
3070 * ibmvfc_get_ae_desc - Get text description for async event
3074 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
3078 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
3079 if (ae_desc[i].ae == ae)
3085 static const struct {
3086 enum ibmvfc_ae_link_state state;
3089 { IBMVFC_AE_LS_LINK_UP, " link up" },
3090 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
3091 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
3092 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
3096 * ibmvfc_get_link_state - Get text description for link state
3097 * @state: link state
3100 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
3104 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
3105 if (link_desc[i].state == state)
3106 return link_desc[i].desc;
3112 * ibmvfc_handle_async - Handle an async event from the adapter
3113 * @crq: crq to process
3114 * @vhost: ibmvfc host struct
3117 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
3118 struct ibmvfc_host *vhost)
3120 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
3121 struct ibmvfc_target *tgt;
3123 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
3124 " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
3125 be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
3126 ibmvfc_get_link_state(crq->link_state));
3128 switch (be64_to_cpu(crq->event)) {
3129 case IBMVFC_AE_RESUME:
3130 switch (crq->link_state) {
3131 case IBMVFC_AE_LS_LINK_DOWN:
3132 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3134 case IBMVFC_AE_LS_LINK_DEAD:
3135 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3137 case IBMVFC_AE_LS_LINK_UP:
3138 case IBMVFC_AE_LS_LINK_BOUNCED:
3140 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3141 vhost->delay_init = 1;
3142 __ibmvfc_reset_host(vhost);
3147 case IBMVFC_AE_LINK_UP:
3148 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3149 vhost->delay_init = 1;
3150 __ibmvfc_reset_host(vhost);
3152 case IBMVFC_AE_SCN_FABRIC:
3153 case IBMVFC_AE_SCN_DOMAIN:
3154 vhost->events_to_log |= IBMVFC_AE_RSCN;
3155 if (vhost->state < IBMVFC_HALTED) {
3156 vhost->delay_init = 1;
3157 __ibmvfc_reset_host(vhost);
3160 case IBMVFC_AE_SCN_NPORT:
3161 case IBMVFC_AE_SCN_GROUP:
3162 vhost->events_to_log |= IBMVFC_AE_RSCN;
3163 ibmvfc_reinit_host(vhost);
3165 case IBMVFC_AE_ELS_LOGO:
3166 case IBMVFC_AE_ELS_PRLO:
3167 case IBMVFC_AE_ELS_PLOGI:
3168 list_for_each_entry(tgt, &vhost->targets, queue) {
3169 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
3171 if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
3173 if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
3175 if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
3177 if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
3179 if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
3180 ibmvfc_del_tgt(tgt);
3181 ibmvfc_reinit_host(vhost);
3185 case IBMVFC_AE_LINK_DOWN:
3186 case IBMVFC_AE_ADAPTER_FAILED:
3187 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3189 case IBMVFC_AE_LINK_DEAD:
3190 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3192 case IBMVFC_AE_HALT:
3193 ibmvfc_link_down(vhost, IBMVFC_HALTED);
3196 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
3202 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
3203 * @crq: Command/Response queue
3204 * @vhost: ibmvfc host struct
3205 * @evt_doneq: Event done queue
3208 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3209 struct list_head *evt_doneq)
3212 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3214 switch (crq->valid) {
3215 case IBMVFC_CRQ_INIT_RSP:
3216 switch (crq->format) {
3217 case IBMVFC_CRQ_INIT:
3218 dev_info(vhost->dev, "Partner initialized\n");
3219 /* Send back a response */
3220 rc = ibmvfc_send_crq_init_complete(vhost);
3222 ibmvfc_init_host(vhost);
3224 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
3226 case IBMVFC_CRQ_INIT_COMPLETE:
3227 dev_info(vhost->dev, "Partner initialization complete\n");
3228 ibmvfc_init_host(vhost);
3231 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
3234 case IBMVFC_CRQ_XPORT_EVENT:
3235 vhost->state = IBMVFC_NO_CRQ;
3236 vhost->logged_in = 0;
3237 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3238 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
3239 /* We need to re-setup the interpartition connection */
3240 dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
3241 vhost->client_migrated = 1;
3243 scsi_block_requests(vhost->host);
3244 ibmvfc_purge_requests(vhost, DID_REQUEUE);
3245 ibmvfc_set_host_state(vhost, IBMVFC_LINK_DOWN);
3246 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
3247 wake_up(&vhost->work_wait_q);
3248 } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
3249 dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
3250 ibmvfc_purge_requests(vhost, DID_ERROR);
3251 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3252 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
3254 dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
3257 case IBMVFC_CRQ_CMD_RSP:
3260 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
3264 if (crq->format == IBMVFC_ASYNC_EVENT)
3267 /* The only kind of payload CRQs we should get are responses to
3268 * things we send. Make sure this response is to something we
3271 if (unlikely(!ibmvfc_valid_event(&vhost->crq.evt_pool, evt))) {
3272 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3277 if (unlikely(atomic_dec_if_positive(&evt->active))) {
3278 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3283 spin_lock(&evt->queue->l_lock);
3284 list_move_tail(&evt->queue_list, evt_doneq);
3285 spin_unlock(&evt->queue->l_lock);
3289 * ibmvfc_scan_finished - Check if the device scan is done.
3290 * @shost: scsi host struct
3291 * @time: current elapsed time
3294 * 0 if scan is not done / 1 if scan is done
3296 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3298 unsigned long flags;
3299 struct ibmvfc_host *vhost = shost_priv(shost);
3302 spin_lock_irqsave(shost->host_lock, flags);
3303 if (!vhost->scan_timeout)
3305 else if (time >= (vhost->scan_timeout * HZ)) {
3306 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
3307 "continuing initialization\n", vhost->scan_timeout);
3311 if (vhost->scan_complete) {
3312 vhost->scan_timeout = init_timeout;
3315 spin_unlock_irqrestore(shost->host_lock, flags);
3320 * ibmvfc_slave_alloc - Setup the device's task set value
3321 * @sdev: struct scsi_device device to configure
3323 * Set the device's task set value so that error handling works as
3327 * 0 on success / -ENXIO if device does not exist
3329 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
3331 struct Scsi_Host *shost = sdev->host;
3332 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3333 struct ibmvfc_host *vhost = shost_priv(shost);
3334 unsigned long flags = 0;
3336 if (!rport || fc_remote_port_chkready(rport))
3339 spin_lock_irqsave(shost->host_lock, flags);
3340 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
3341 spin_unlock_irqrestore(shost->host_lock, flags);
3346 * ibmvfc_target_alloc - Setup the target's task set value
3347 * @starget: struct scsi_target
3349 * Set the target's task set value so that error handling works as
3353 * 0 on success / -ENXIO if device does not exist
3355 static int ibmvfc_target_alloc(struct scsi_target *starget)
3357 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3358 struct ibmvfc_host *vhost = shost_priv(shost);
3359 unsigned long flags = 0;
3361 spin_lock_irqsave(shost->host_lock, flags);
3362 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
3363 spin_unlock_irqrestore(shost->host_lock, flags);
3368 * ibmvfc_slave_configure - Configure the device
3369 * @sdev: struct scsi_device device to configure
3371 * Enable allow_restart for a device if it is a disk. Adjust the
3372 * queue_depth here also.
3377 static int ibmvfc_slave_configure(struct scsi_device *sdev)
3379 struct Scsi_Host *shost = sdev->host;
3380 unsigned long flags = 0;
3382 spin_lock_irqsave(shost->host_lock, flags);
3383 if (sdev->type == TYPE_DISK) {
3384 sdev->allow_restart = 1;
3385 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
3387 spin_unlock_irqrestore(shost->host_lock, flags);
3392 * ibmvfc_change_queue_depth - Change the device's queue depth
3393 * @sdev: scsi device struct
3394 * @qdepth: depth to set
3399 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
3401 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
3402 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
3404 return scsi_change_queue_depth(sdev, qdepth);
3407 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
3408 struct device_attribute *attr, char *buf)
3410 struct Scsi_Host *shost = class_to_shost(dev);
3411 struct ibmvfc_host *vhost = shost_priv(shost);
3413 return snprintf(buf, PAGE_SIZE, "%s\n",
3414 vhost->login_buf->resp.partition_name);
3417 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
3418 struct device_attribute *attr, char *buf)
3420 struct Scsi_Host *shost = class_to_shost(dev);
3421 struct ibmvfc_host *vhost = shost_priv(shost);
3423 return snprintf(buf, PAGE_SIZE, "%s\n",
3424 vhost->login_buf->resp.device_name);
3427 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3428 struct device_attribute *attr, char *buf)
3430 struct Scsi_Host *shost = class_to_shost(dev);
3431 struct ibmvfc_host *vhost = shost_priv(shost);
3433 return snprintf(buf, PAGE_SIZE, "%s\n",
3434 vhost->login_buf->resp.port_loc_code);
3437 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3438 struct device_attribute *attr, char *buf)
3440 struct Scsi_Host *shost = class_to_shost(dev);
3441 struct ibmvfc_host *vhost = shost_priv(shost);
3443 return snprintf(buf, PAGE_SIZE, "%s\n",
3444 vhost->login_buf->resp.drc_name);
3447 static ssize_t ibmvfc_show_host_npiv_version(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);
3452 return snprintf(buf, PAGE_SIZE, "%d\n", be32_to_cpu(vhost->login_buf->resp.version));
3455 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3456 struct device_attribute *attr, char *buf)
3458 struct Scsi_Host *shost = class_to_shost(dev);
3459 struct ibmvfc_host *vhost = shost_priv(shost);
3460 return snprintf(buf, PAGE_SIZE, "%llx\n", be64_to_cpu(vhost->login_buf->resp.capabilities));
3464 * ibmvfc_show_log_level - Show the adapter's error logging level
3465 * @dev: class device struct
3470 * number of bytes printed to buffer
3472 static ssize_t ibmvfc_show_log_level(struct device *dev,
3473 struct device_attribute *attr, char *buf)
3475 struct Scsi_Host *shost = class_to_shost(dev);
3476 struct ibmvfc_host *vhost = shost_priv(shost);
3477 unsigned long flags = 0;
3480 spin_lock_irqsave(shost->host_lock, flags);
3481 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
3482 spin_unlock_irqrestore(shost->host_lock, flags);
3487 * ibmvfc_store_log_level - Change the adapter's error logging level
3488 * @dev: class device struct
3491 * @count: buffer size
3494 * number of bytes printed to buffer
3496 static ssize_t ibmvfc_store_log_level(struct device *dev,
3497 struct device_attribute *attr,
3498 const char *buf, size_t count)
3500 struct Scsi_Host *shost = class_to_shost(dev);
3501 struct ibmvfc_host *vhost = shost_priv(shost);
3502 unsigned long flags = 0;
3504 spin_lock_irqsave(shost->host_lock, flags);
3505 vhost->log_level = simple_strtoul(buf, NULL, 10);
3506 spin_unlock_irqrestore(shost->host_lock, flags);
3510 static ssize_t ibmvfc_show_scsi_channels(struct device *dev,
3511 struct device_attribute *attr, char *buf)
3513 struct Scsi_Host *shost = class_to_shost(dev);
3514 struct ibmvfc_host *vhost = shost_priv(shost);
3515 unsigned long flags = 0;
3518 spin_lock_irqsave(shost->host_lock, flags);
3519 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->client_scsi_channels);
3520 spin_unlock_irqrestore(shost->host_lock, flags);
3524 static ssize_t ibmvfc_store_scsi_channels(struct device *dev,
3525 struct device_attribute *attr,
3526 const char *buf, size_t count)
3528 struct Scsi_Host *shost = class_to_shost(dev);
3529 struct ibmvfc_host *vhost = shost_priv(shost);
3530 unsigned long flags = 0;
3531 unsigned int channels;
3533 spin_lock_irqsave(shost->host_lock, flags);
3534 channels = simple_strtoul(buf, NULL, 10);
3535 vhost->client_scsi_channels = min(channels, nr_scsi_hw_queues);
3536 ibmvfc_hard_reset_host(vhost);
3537 spin_unlock_irqrestore(shost->host_lock, flags);
3541 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3542 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3543 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3544 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3545 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3546 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3547 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3548 ibmvfc_show_log_level, ibmvfc_store_log_level);
3549 static DEVICE_ATTR(nr_scsi_channels, S_IRUGO | S_IWUSR,
3550 ibmvfc_show_scsi_channels, ibmvfc_store_scsi_channels);
3552 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3554 * ibmvfc_read_trace - Dump the adapter trace
3555 * @filp: open sysfs file
3556 * @kobj: kobject struct
3557 * @bin_attr: bin_attribute struct
3560 * @count: buffer size
3563 * number of bytes printed to buffer
3565 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3566 struct bin_attribute *bin_attr,
3567 char *buf, loff_t off, size_t count)
3569 struct device *dev = kobj_to_dev(kobj);
3570 struct Scsi_Host *shost = class_to_shost(dev);
3571 struct ibmvfc_host *vhost = shost_priv(shost);
3572 unsigned long flags = 0;
3573 int size = IBMVFC_TRACE_SIZE;
3574 char *src = (char *)vhost->trace;
3578 if (off + count > size) {
3583 spin_lock_irqsave(shost->host_lock, flags);
3584 memcpy(buf, &src[off], count);
3585 spin_unlock_irqrestore(shost->host_lock, flags);
3589 static struct bin_attribute ibmvfc_trace_attr = {
3595 .read = ibmvfc_read_trace,
3599 static struct attribute *ibmvfc_host_attrs[] = {
3600 &dev_attr_partition_name.attr,
3601 &dev_attr_device_name.attr,
3602 &dev_attr_port_loc_code.attr,
3603 &dev_attr_drc_name.attr,
3604 &dev_attr_npiv_version.attr,
3605 &dev_attr_capabilities.attr,
3606 &dev_attr_log_level.attr,
3607 &dev_attr_nr_scsi_channels.attr,
3611 ATTRIBUTE_GROUPS(ibmvfc_host);
3613 static const struct scsi_host_template driver_template = {
3614 .module = THIS_MODULE,
3615 .name = "IBM POWER Virtual FC Adapter",
3616 .proc_name = IBMVFC_NAME,
3617 .queuecommand = ibmvfc_queuecommand,
3618 .eh_timed_out = fc_eh_timed_out,
3619 .eh_abort_handler = ibmvfc_eh_abort_handler,
3620 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3621 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3622 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3623 .slave_alloc = ibmvfc_slave_alloc,
3624 .slave_configure = ibmvfc_slave_configure,
3625 .target_alloc = ibmvfc_target_alloc,
3626 .scan_finished = ibmvfc_scan_finished,
3627 .change_queue_depth = ibmvfc_change_queue_depth,
3629 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3631 .sg_tablesize = SG_ALL,
3632 .max_sectors = IBMVFC_MAX_SECTORS,
3633 .shost_groups = ibmvfc_host_groups,
3634 .track_queue_depth = 1,
3639 * ibmvfc_next_async_crq - Returns the next entry in async queue
3640 * @vhost: ibmvfc host struct
3643 * Pointer to next entry in queue / NULL if empty
3645 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3647 struct ibmvfc_queue *async_crq = &vhost->async_crq;
3648 struct ibmvfc_async_crq *crq;
3650 crq = &async_crq->msgs.async[async_crq->cur];
3651 if (crq->valid & 0x80) {
3652 if (++async_crq->cur == async_crq->size)
3662 * ibmvfc_next_crq - Returns the next entry in message queue
3663 * @vhost: ibmvfc host struct
3666 * Pointer to next entry in queue / NULL if empty
3668 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3670 struct ibmvfc_queue *queue = &vhost->crq;
3671 struct ibmvfc_crq *crq;
3673 crq = &queue->msgs.crq[queue->cur];
3674 if (crq->valid & 0x80) {
3675 if (++queue->cur == queue->size)
3685 * ibmvfc_interrupt - Interrupt handler
3686 * @irq: number of irq to handle, not used
3687 * @dev_instance: ibmvfc_host that received interrupt
3692 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3694 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3695 unsigned long flags;
3697 spin_lock_irqsave(vhost->host->host_lock, flags);
3698 vio_disable_interrupts(to_vio_dev(vhost->dev));
3699 tasklet_schedule(&vhost->tasklet);
3700 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3705 * ibmvfc_tasklet - Interrupt handler tasklet
3706 * @data: ibmvfc host struct
3711 static void ibmvfc_tasklet(void *data)
3713 struct ibmvfc_host *vhost = data;
3714 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3715 struct ibmvfc_crq *crq;
3716 struct ibmvfc_async_crq *async;
3717 struct ibmvfc_event *evt, *temp;
3718 unsigned long flags;
3720 LIST_HEAD(evt_doneq);
3722 spin_lock_irqsave(vhost->host->host_lock, flags);
3723 spin_lock(vhost->crq.q_lock);
3725 /* Pull all the valid messages off the async CRQ */
3726 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3727 ibmvfc_handle_async(async, vhost);
3732 /* Pull all the valid messages off the CRQ */
3733 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3734 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3739 vio_enable_interrupts(vdev);
3740 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3741 vio_disable_interrupts(vdev);
3742 ibmvfc_handle_async(async, vhost);
3745 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3746 vio_disable_interrupts(vdev);
3747 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3754 spin_unlock(vhost->crq.q_lock);
3755 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3757 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3758 del_timer(&evt->timer);
3759 list_del(&evt->queue_list);
3760 ibmvfc_trc_end(evt);
3765 static int ibmvfc_toggle_scrq_irq(struct ibmvfc_queue *scrq, int enable)
3767 struct device *dev = scrq->vhost->dev;
3768 struct vio_dev *vdev = to_vio_dev(dev);
3770 int irq_action = H_ENABLE_VIO_INTERRUPT;
3773 irq_action = H_DISABLE_VIO_INTERRUPT;
3775 rc = plpar_hcall_norets(H_VIOCTL, vdev->unit_address, irq_action,
3776 scrq->hw_irq, 0, 0);
3779 dev_err(dev, "Couldn't %s sub-crq[%lu] irq. rc=%ld\n",
3780 enable ? "enable" : "disable", scrq->hwq_id, rc);
3785 static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3786 struct list_head *evt_doneq)
3788 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3790 switch (crq->valid) {
3791 case IBMVFC_CRQ_CMD_RSP:
3793 case IBMVFC_CRQ_XPORT_EVENT:
3796 dev_err(vhost->dev, "Got and invalid message type 0x%02x\n", crq->valid);
3800 /* The only kind of payload CRQs we should get are responses to
3801 * things we send. Make sure this response is to something we
3804 if (unlikely(!ibmvfc_valid_event(&evt->queue->evt_pool, evt))) {
3805 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3810 if (unlikely(atomic_dec_if_positive(&evt->active))) {
3811 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3816 spin_lock(&evt->queue->l_lock);
3817 list_move_tail(&evt->queue_list, evt_doneq);
3818 spin_unlock(&evt->queue->l_lock);
3821 static struct ibmvfc_crq *ibmvfc_next_scrq(struct ibmvfc_queue *scrq)
3823 struct ibmvfc_crq *crq;
3825 crq = &scrq->msgs.scrq[scrq->cur].crq;
3826 if (crq->valid & 0x80) {
3827 if (++scrq->cur == scrq->size)
3836 static void ibmvfc_drain_sub_crq(struct ibmvfc_queue *scrq)
3838 struct ibmvfc_crq *crq;
3839 struct ibmvfc_event *evt, *temp;
3840 unsigned long flags;
3842 LIST_HEAD(evt_doneq);
3844 spin_lock_irqsave(scrq->q_lock, flags);
3846 while ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3847 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3852 ibmvfc_toggle_scrq_irq(scrq, 1);
3853 if ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3854 ibmvfc_toggle_scrq_irq(scrq, 0);
3855 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3861 spin_unlock_irqrestore(scrq->q_lock, flags);
3863 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3864 del_timer(&evt->timer);
3865 list_del(&evt->queue_list);
3866 ibmvfc_trc_end(evt);
3871 static irqreturn_t ibmvfc_interrupt_scsi(int irq, void *scrq_instance)
3873 struct ibmvfc_queue *scrq = (struct ibmvfc_queue *)scrq_instance;
3875 ibmvfc_toggle_scrq_irq(scrq, 0);
3876 ibmvfc_drain_sub_crq(scrq);
3882 * ibmvfc_init_tgt - Set the next init job step for the target
3883 * @tgt: ibmvfc target struct
3884 * @job_step: job step to perform
3887 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3888 void (*job_step) (struct ibmvfc_target *))
3890 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3891 tgt->job_step = job_step;
3892 wake_up(&tgt->vhost->work_wait_q);
3896 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3897 * @tgt: ibmvfc target struct
3898 * @job_step: initialization job step
3900 * Returns: 1 if step will be retried / 0 if not
3903 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3904 void (*job_step) (struct ibmvfc_target *))
3906 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3907 ibmvfc_del_tgt(tgt);
3908 wake_up(&tgt->vhost->work_wait_q);
3911 ibmvfc_init_tgt(tgt, job_step);
3915 /* Defined in FC-LS */
3916 static const struct {
3933 * ibmvfc_get_prli_rsp - Find PRLI response index
3934 * @flags: PRLI response flags
3937 static int ibmvfc_get_prli_rsp(u16 flags)
3940 int code = (flags & 0x0f00) >> 8;
3942 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3943 if (prli_rsp[i].code == code)
3950 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3951 * @evt: ibmvfc event struct
3954 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3956 struct ibmvfc_target *tgt = evt->tgt;
3957 struct ibmvfc_host *vhost = evt->vhost;
3958 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3959 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3960 u32 status = be16_to_cpu(rsp->common.status);
3961 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3963 vhost->discovery_threads--;
3964 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3966 case IBMVFC_MAD_SUCCESS:
3967 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3968 parms->type, parms->flags, parms->service_parms);
3970 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3971 index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3972 if (prli_rsp[index].logged_in) {
3973 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3974 tgt->need_login = 0;
3976 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3977 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3978 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3979 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3982 ibmvfc_del_tgt(tgt);
3983 } else if (prli_rsp[index].retry)
3984 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3986 ibmvfc_del_tgt(tgt);
3988 ibmvfc_del_tgt(tgt);
3990 case IBMVFC_MAD_DRIVER_FAILED:
3992 case IBMVFC_MAD_CRQ_ERROR:
3993 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3995 case IBMVFC_MAD_FAILED:
3997 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3998 be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3999 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4000 else if (tgt->logo_rcvd)
4001 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4002 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4003 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
4005 ibmvfc_del_tgt(tgt);
4007 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
4008 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4009 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
4013 kref_put(&tgt->kref, ibmvfc_release_tgt);
4014 ibmvfc_free_event(evt);
4015 wake_up(&vhost->work_wait_q);
4019 * ibmvfc_tgt_send_prli - Send a process login
4020 * @tgt: ibmvfc target struct
4023 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
4025 struct ibmvfc_process_login *prli;
4026 struct ibmvfc_host *vhost = tgt->vhost;
4027 struct ibmvfc_event *evt;
4029 if (vhost->discovery_threads >= disc_threads)
4032 kref_get(&tgt->kref);
4033 evt = ibmvfc_get_event(&vhost->crq);
4034 vhost->discovery_threads++;
4035 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
4037 prli = &evt->iu.prli;
4038 memset(prli, 0, sizeof(*prli));
4039 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4040 prli->common.version = cpu_to_be32(2);
4041 prli->target_wwpn = cpu_to_be64(tgt->wwpn);
4043 prli->common.version = cpu_to_be32(1);
4045 prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
4046 prli->common.length = cpu_to_be16(sizeof(*prli));
4047 prli->scsi_id = cpu_to_be64(tgt->scsi_id);
4049 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
4050 prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
4051 prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
4052 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
4055 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
4057 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4058 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4059 vhost->discovery_threads--;
4060 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4061 kref_put(&tgt->kref, ibmvfc_release_tgt);
4063 tgt_dbg(tgt, "Sent process login\n");
4067 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
4068 * @evt: ibmvfc event struct
4071 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
4073 struct ibmvfc_target *tgt = evt->tgt;
4074 struct ibmvfc_host *vhost = evt->vhost;
4075 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
4076 u32 status = be16_to_cpu(rsp->common.status);
4077 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4079 vhost->discovery_threads--;
4080 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4082 case IBMVFC_MAD_SUCCESS:
4083 tgt_dbg(tgt, "Port Login succeeded\n");
4084 if (tgt->ids.port_name &&
4085 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
4087 tgt_dbg(tgt, "Port re-init required\n");
4090 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4091 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4092 tgt->ids.port_id = tgt->scsi_id;
4093 memcpy(&tgt->service_parms, &rsp->service_parms,
4094 sizeof(tgt->service_parms));
4095 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4096 sizeof(tgt->service_parms_change));
4097 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4099 case IBMVFC_MAD_DRIVER_FAILED:
4101 case IBMVFC_MAD_CRQ_ERROR:
4102 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4104 case IBMVFC_MAD_FAILED:
4106 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4107 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4109 ibmvfc_del_tgt(tgt);
4111 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4112 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4113 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4114 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4115 ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
4119 kref_put(&tgt->kref, ibmvfc_release_tgt);
4120 ibmvfc_free_event(evt);
4121 wake_up(&vhost->work_wait_q);
4125 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
4126 * @tgt: ibmvfc target struct
4129 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
4131 struct ibmvfc_port_login *plogi;
4132 struct ibmvfc_host *vhost = tgt->vhost;
4133 struct ibmvfc_event *evt;
4135 if (vhost->discovery_threads >= disc_threads)
4138 kref_get(&tgt->kref);
4140 evt = ibmvfc_get_event(&vhost->crq);
4141 vhost->discovery_threads++;
4142 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4143 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
4145 plogi = &evt->iu.plogi;
4146 memset(plogi, 0, sizeof(*plogi));
4147 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4148 plogi->common.version = cpu_to_be32(2);
4149 plogi->target_wwpn = cpu_to_be64(tgt->wwpn);
4151 plogi->common.version = cpu_to_be32(1);
4153 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
4154 plogi->common.length = cpu_to_be16(sizeof(*plogi));
4155 plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
4157 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4158 vhost->discovery_threads--;
4159 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4160 kref_put(&tgt->kref, ibmvfc_release_tgt);
4162 tgt_dbg(tgt, "Sent port login\n");
4166 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
4167 * @evt: ibmvfc event struct
4170 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
4172 struct ibmvfc_target *tgt = evt->tgt;
4173 struct ibmvfc_host *vhost = evt->vhost;
4174 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
4175 u32 status = be16_to_cpu(rsp->common.status);
4177 vhost->discovery_threads--;
4178 ibmvfc_free_event(evt);
4179 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4182 case IBMVFC_MAD_SUCCESS:
4183 tgt_dbg(tgt, "Implicit Logout succeeded\n");
4185 case IBMVFC_MAD_DRIVER_FAILED:
4186 kref_put(&tgt->kref, ibmvfc_release_tgt);
4187 wake_up(&vhost->work_wait_q);
4189 case IBMVFC_MAD_FAILED:
4191 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
4195 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
4196 kref_put(&tgt->kref, ibmvfc_release_tgt);
4197 wake_up(&vhost->work_wait_q);
4201 * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
4202 * @tgt: ibmvfc target struct
4203 * @done: Routine to call when the event is responded to
4206 * Allocated and initialized ibmvfc_event struct
4208 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
4209 void (*done) (struct ibmvfc_event *))
4211 struct ibmvfc_implicit_logout *mad;
4212 struct ibmvfc_host *vhost = tgt->vhost;
4213 struct ibmvfc_event *evt;
4215 kref_get(&tgt->kref);
4216 evt = ibmvfc_get_event(&vhost->crq);
4217 ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
4219 mad = &evt->iu.implicit_logout;
4220 memset(mad, 0, sizeof(*mad));
4221 mad->common.version = cpu_to_be32(1);
4222 mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
4223 mad->common.length = cpu_to_be16(sizeof(*mad));
4224 mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4229 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
4230 * @tgt: ibmvfc target struct
4233 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
4235 struct ibmvfc_host *vhost = tgt->vhost;
4236 struct ibmvfc_event *evt;
4238 if (vhost->discovery_threads >= disc_threads)
4241 vhost->discovery_threads++;
4242 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4243 ibmvfc_tgt_implicit_logout_done);
4245 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4246 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4247 vhost->discovery_threads--;
4248 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4249 kref_put(&tgt->kref, ibmvfc_release_tgt);
4251 tgt_dbg(tgt, "Sent Implicit Logout\n");
4255 * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
4256 * @evt: ibmvfc event struct
4259 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
4261 struct ibmvfc_target *tgt = evt->tgt;
4262 struct ibmvfc_host *vhost = evt->vhost;
4263 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4264 u32 status = be16_to_cpu(mad->common.status);
4266 vhost->discovery_threads--;
4267 ibmvfc_free_event(evt);
4270 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
4271 * driver in which case we need to free up all the targets. If we are
4272 * not unloading, we will still go through a hard reset to get out of
4273 * offline state, so there is no need to track the old targets in that
4276 if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
4277 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4279 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
4281 tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
4282 kref_put(&tgt->kref, ibmvfc_release_tgt);
4283 wake_up(&vhost->work_wait_q);
4287 * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
4288 * @tgt: ibmvfc target struct
4291 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
4293 struct ibmvfc_host *vhost = tgt->vhost;
4294 struct ibmvfc_event *evt;
4296 if (!vhost->logged_in) {
4297 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4301 if (vhost->discovery_threads >= disc_threads)
4304 vhost->discovery_threads++;
4305 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4306 ibmvfc_tgt_implicit_logout_and_del_done);
4308 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
4309 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4310 vhost->discovery_threads--;
4311 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4312 kref_put(&tgt->kref, ibmvfc_release_tgt);
4314 tgt_dbg(tgt, "Sent Implicit Logout\n");
4318 * ibmvfc_tgt_move_login_done - Completion handler for Move Login
4319 * @evt: ibmvfc event struct
4322 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
4324 struct ibmvfc_target *tgt = evt->tgt;
4325 struct ibmvfc_host *vhost = evt->vhost;
4326 struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
4327 u32 status = be16_to_cpu(rsp->common.status);
4328 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4330 vhost->discovery_threads--;
4331 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4333 case IBMVFC_MAD_SUCCESS:
4334 tgt_dbg(tgt, "Move Login succeeded for new scsi_id: %llX\n", tgt->new_scsi_id);
4335 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4336 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4337 tgt->scsi_id = tgt->new_scsi_id;
4338 tgt->ids.port_id = tgt->scsi_id;
4339 memcpy(&tgt->service_parms, &rsp->service_parms,
4340 sizeof(tgt->service_parms));
4341 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4342 sizeof(tgt->service_parms_change));
4343 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4345 case IBMVFC_MAD_DRIVER_FAILED:
4347 case IBMVFC_MAD_CRQ_ERROR:
4348 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4350 case IBMVFC_MAD_FAILED:
4352 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4355 "Move Login failed: new scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
4356 tgt->new_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
4361 kref_put(&tgt->kref, ibmvfc_release_tgt);
4362 ibmvfc_free_event(evt);
4363 wake_up(&vhost->work_wait_q);
4368 * ibmvfc_tgt_move_login - Initiate a move login for specified target
4369 * @tgt: ibmvfc target struct
4372 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
4374 struct ibmvfc_host *vhost = tgt->vhost;
4375 struct ibmvfc_move_login *move;
4376 struct ibmvfc_event *evt;
4378 if (vhost->discovery_threads >= disc_threads)
4381 kref_get(&tgt->kref);
4382 evt = ibmvfc_get_event(&vhost->crq);
4383 vhost->discovery_threads++;
4384 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4385 ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
4387 move = &evt->iu.move_login;
4388 memset(move, 0, sizeof(*move));
4389 move->common.version = cpu_to_be32(1);
4390 move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
4391 move->common.length = cpu_to_be16(sizeof(*move));
4393 move->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4394 move->new_scsi_id = cpu_to_be64(tgt->new_scsi_id);
4395 move->wwpn = cpu_to_be64(tgt->wwpn);
4396 move->node_name = cpu_to_be64(tgt->ids.node_name);
4398 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4399 vhost->discovery_threads--;
4400 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4401 kref_put(&tgt->kref, ibmvfc_release_tgt);
4403 tgt_dbg(tgt, "Sent Move Login for new scsi_id: %llX\n", tgt->new_scsi_id);
4407 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
4408 * @mad: ibmvfc passthru mad struct
4409 * @tgt: ibmvfc target struct
4412 * 1 if PLOGI needed / 0 if PLOGI not needed
4414 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
4415 struct ibmvfc_target *tgt)
4417 if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
4419 if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
4421 if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
4427 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
4428 * @evt: ibmvfc event struct
4431 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
4433 struct ibmvfc_target *tgt = evt->tgt;
4434 struct ibmvfc_host *vhost = evt->vhost;
4435 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4436 u32 status = be16_to_cpu(mad->common.status);
4437 u8 fc_reason, fc_explain;
4439 vhost->discovery_threads--;
4440 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4441 del_timer(&tgt->timer);
4444 case IBMVFC_MAD_SUCCESS:
4445 tgt_dbg(tgt, "ADISC succeeded\n");
4446 if (ibmvfc_adisc_needs_plogi(mad, tgt))
4447 ibmvfc_del_tgt(tgt);
4449 case IBMVFC_MAD_DRIVER_FAILED:
4451 case IBMVFC_MAD_FAILED:
4453 ibmvfc_del_tgt(tgt);
4454 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
4455 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
4456 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4457 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
4458 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
4459 ibmvfc_get_fc_type(fc_reason), fc_reason,
4460 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
4464 kref_put(&tgt->kref, ibmvfc_release_tgt);
4465 ibmvfc_free_event(evt);
4466 wake_up(&vhost->work_wait_q);
4470 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
4471 * @evt: ibmvfc event struct
4474 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
4476 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
4478 memset(mad, 0, sizeof(*mad));
4479 mad->common.version = cpu_to_be32(1);
4480 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
4481 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
4482 mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4483 offsetof(struct ibmvfc_passthru_mad, iu));
4484 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
4485 mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4486 mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
4487 mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4488 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4489 offsetof(struct ibmvfc_passthru_fc_iu, payload));
4490 mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4491 mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4492 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4493 offsetof(struct ibmvfc_passthru_fc_iu, response));
4494 mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
4498 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
4499 * @evt: ibmvfc event struct
4501 * Just cleanup this event struct. Everything else is handled by
4502 * the ADISC completion handler. If the ADISC never actually comes
4503 * back, we still have the timer running on the ADISC event struct
4504 * which will fire and cause the CRQ to get reset.
4507 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
4509 struct ibmvfc_host *vhost = evt->vhost;
4510 struct ibmvfc_target *tgt = evt->tgt;
4512 tgt_dbg(tgt, "ADISC cancel complete\n");
4513 vhost->abort_threads--;
4514 ibmvfc_free_event(evt);
4515 kref_put(&tgt->kref, ibmvfc_release_tgt);
4516 wake_up(&vhost->work_wait_q);
4520 * ibmvfc_adisc_timeout - Handle an ADISC timeout
4521 * @t: ibmvfc target struct
4523 * If an ADISC times out, send a cancel. If the cancel times
4524 * out, reset the CRQ. When the ADISC comes back as cancelled,
4525 * log back into the target.
4527 static void ibmvfc_adisc_timeout(struct timer_list *t)
4529 struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
4530 struct ibmvfc_host *vhost = tgt->vhost;
4531 struct ibmvfc_event *evt;
4532 struct ibmvfc_tmf *tmf;
4533 unsigned long flags;
4536 tgt_dbg(tgt, "ADISC timeout\n");
4537 spin_lock_irqsave(vhost->host->host_lock, flags);
4538 if (vhost->abort_threads >= disc_threads ||
4539 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
4540 vhost->state != IBMVFC_INITIALIZING ||
4541 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
4542 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4546 vhost->abort_threads++;
4547 kref_get(&tgt->kref);
4548 evt = ibmvfc_get_event(&vhost->crq);
4549 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
4553 memset(tmf, 0, sizeof(*tmf));
4554 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4555 tmf->common.version = cpu_to_be32(2);
4556 tmf->target_wwpn = cpu_to_be64(tgt->wwpn);
4558 tmf->common.version = cpu_to_be32(1);
4560 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
4561 tmf->common.length = cpu_to_be16(sizeof(*tmf));
4562 tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
4563 tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
4565 rc = ibmvfc_send_event(evt, vhost, default_timeout);
4568 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
4569 vhost->abort_threads--;
4570 kref_put(&tgt->kref, ibmvfc_release_tgt);
4571 __ibmvfc_reset_host(vhost);
4573 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
4574 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4578 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
4579 * @tgt: ibmvfc target struct
4581 * When sending an ADISC we end up with two timers running. The
4582 * first timer is the timer in the ibmvfc target struct. If this
4583 * fires, we send a cancel to the target. The second timer is the
4584 * timer on the ibmvfc event for the ADISC, which is longer. If that
4585 * fires, it means the ADISC timed out and our attempt to cancel it
4586 * also failed, so we need to reset the CRQ.
4588 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
4590 struct ibmvfc_passthru_mad *mad;
4591 struct ibmvfc_host *vhost = tgt->vhost;
4592 struct ibmvfc_event *evt;
4594 if (vhost->discovery_threads >= disc_threads)
4597 kref_get(&tgt->kref);
4598 evt = ibmvfc_get_event(&vhost->crq);
4599 vhost->discovery_threads++;
4600 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
4603 ibmvfc_init_passthru(evt);
4604 mad = &evt->iu.passthru;
4605 mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4606 mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4607 mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4609 mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4610 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4611 sizeof(vhost->login_buf->resp.port_name));
4612 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4613 sizeof(vhost->login_buf->resp.node_name));
4614 mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4616 if (timer_pending(&tgt->timer))
4617 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4619 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4620 add_timer(&tgt->timer);
4623 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4624 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4625 vhost->discovery_threads--;
4626 del_timer(&tgt->timer);
4627 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4628 kref_put(&tgt->kref, ibmvfc_release_tgt);
4630 tgt_dbg(tgt, "Sent ADISC\n");
4634 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4635 * @evt: ibmvfc event struct
4638 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4640 struct ibmvfc_target *tgt = evt->tgt;
4641 struct ibmvfc_host *vhost = evt->vhost;
4642 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4643 u32 status = be16_to_cpu(rsp->common.status);
4644 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4646 vhost->discovery_threads--;
4647 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4649 case IBMVFC_MAD_SUCCESS:
4650 tgt_dbg(tgt, "Query Target succeeded\n");
4651 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4652 ibmvfc_del_tgt(tgt);
4654 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4656 case IBMVFC_MAD_DRIVER_FAILED:
4658 case IBMVFC_MAD_CRQ_ERROR:
4659 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4661 case IBMVFC_MAD_FAILED:
4663 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4664 be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4665 be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4666 ibmvfc_del_tgt(tgt);
4667 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4668 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4670 ibmvfc_del_tgt(tgt);
4672 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4673 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4674 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4675 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4676 ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4681 kref_put(&tgt->kref, ibmvfc_release_tgt);
4682 ibmvfc_free_event(evt);
4683 wake_up(&vhost->work_wait_q);
4687 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4688 * @tgt: ibmvfc target struct
4691 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4693 struct ibmvfc_query_tgt *query_tgt;
4694 struct ibmvfc_host *vhost = tgt->vhost;
4695 struct ibmvfc_event *evt;
4697 if (vhost->discovery_threads >= disc_threads)
4700 kref_get(&tgt->kref);
4701 evt = ibmvfc_get_event(&vhost->crq);
4702 vhost->discovery_threads++;
4704 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4705 query_tgt = &evt->iu.query_tgt;
4706 memset(query_tgt, 0, sizeof(*query_tgt));
4707 query_tgt->common.version = cpu_to_be32(1);
4708 query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4709 query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4710 query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4712 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4713 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4714 vhost->discovery_threads--;
4715 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4716 kref_put(&tgt->kref, ibmvfc_release_tgt);
4718 tgt_dbg(tgt, "Sent Query Target\n");
4722 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4723 * @vhost: ibmvfc host struct
4724 * @target: Holds SCSI ID to allocate target forand the WWPN
4727 * 0 on success / other on failure
4729 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4730 struct ibmvfc_discover_targets_entry *target)
4732 struct ibmvfc_target *stgt = NULL;
4733 struct ibmvfc_target *wtgt = NULL;
4734 struct ibmvfc_target *tgt;
4735 unsigned long flags;
4736 u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4737 u64 wwpn = be64_to_cpu(target->wwpn);
4739 /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4740 spin_lock_irqsave(vhost->host->host_lock, flags);
4741 list_for_each_entry(tgt, &vhost->targets, queue) {
4742 if (tgt->wwpn == wwpn) {
4748 list_for_each_entry(tgt, &vhost->targets, queue) {
4749 if (tgt->scsi_id == scsi_id) {
4755 if (wtgt && !stgt) {
4757 * A WWPN target has moved and we still are tracking the old
4758 * SCSI ID. The only way we should be able to get here is if
4759 * we attempted to send an implicit logout for the old SCSI ID
4760 * and it failed for some reason, such as there being I/O
4761 * pending to the target. In this case, we will have already
4762 * deleted the rport from the FC transport so we do a move
4763 * login, which works even with I/O pending, however, if
4764 * there is still I/O pending, it will stay outstanding, so
4765 * we only do this if fast fail is disabled for the rport,
4766 * otherwise we let terminate_rport_io clean up the port
4767 * before we login at the new location.
4769 if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4770 if (wtgt->move_login) {
4772 * Do a move login here. The old target is no longer
4773 * known to the transport layer We don't use the
4774 * normal ibmvfc_set_tgt_action to set this, as we
4775 * don't normally want to allow this state change.
4777 wtgt->new_scsi_id = scsi_id;
4778 wtgt->action = IBMVFC_TGT_ACTION_INIT;
4779 wtgt->init_retries = 0;
4780 ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4784 tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4785 wtgt->action, wtgt->rport);
4788 if (tgt->need_login)
4789 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4792 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4794 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4795 memset(tgt, 0, sizeof(*tgt));
4796 tgt->scsi_id = scsi_id;
4799 tgt->need_login = 1;
4800 timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4801 kref_init(&tgt->kref);
4802 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4803 spin_lock_irqsave(vhost->host->host_lock, flags);
4804 tgt->cancel_key = vhost->task_set++;
4805 list_add_tail(&tgt->queue, &vhost->targets);
4808 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4813 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4814 * @vhost: ibmvfc host struct
4817 * 0 on success / other on failure
4819 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4823 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4824 rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
4830 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4831 * @evt: ibmvfc event struct
4834 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4836 struct ibmvfc_host *vhost = evt->vhost;
4837 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4838 u32 mad_status = be16_to_cpu(rsp->common.status);
4839 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4841 switch (mad_status) {
4842 case IBMVFC_MAD_SUCCESS:
4843 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4844 vhost->num_targets = be32_to_cpu(rsp->num_written);
4845 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4847 case IBMVFC_MAD_FAILED:
4848 level += ibmvfc_retry_host_init(vhost);
4849 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4850 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4851 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4853 case IBMVFC_MAD_DRIVER_FAILED:
4856 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4857 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4861 ibmvfc_free_event(evt);
4862 wake_up(&vhost->work_wait_q);
4866 * ibmvfc_discover_targets - Send Discover Targets MAD
4867 * @vhost: ibmvfc host struct
4870 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4872 struct ibmvfc_discover_targets *mad;
4873 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4875 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4876 mad = &evt->iu.discover_targets;
4877 memset(mad, 0, sizeof(*mad));
4878 mad->common.version = cpu_to_be32(1);
4879 mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4880 mad->common.length = cpu_to_be16(sizeof(*mad));
4881 mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4882 mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4883 mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4884 mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
4885 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4887 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4888 ibmvfc_dbg(vhost, "Sent discover targets\n");
4890 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4893 static void ibmvfc_channel_setup_done(struct ibmvfc_event *evt)
4895 struct ibmvfc_host *vhost = evt->vhost;
4896 struct ibmvfc_channel_setup *setup = vhost->channel_setup_buf;
4897 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
4898 u32 mad_status = be16_to_cpu(evt->xfer_iu->channel_setup.common.status);
4899 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4900 int flags, active_queues, i;
4902 ibmvfc_free_event(evt);
4904 switch (mad_status) {
4905 case IBMVFC_MAD_SUCCESS:
4906 ibmvfc_dbg(vhost, "Channel Setup succeeded\n");
4907 flags = be32_to_cpu(setup->flags);
4908 vhost->do_enquiry = 0;
4909 active_queues = be32_to_cpu(setup->num_scsi_subq_channels);
4910 scrqs->active_queues = active_queues;
4912 if (flags & IBMVFC_CHANNELS_CANCELED) {
4913 ibmvfc_dbg(vhost, "Channels Canceled\n");
4914 vhost->using_channels = 0;
4917 vhost->using_channels = 1;
4918 for (i = 0; i < active_queues; i++)
4919 scrqs->scrqs[i].vios_cookie =
4920 be64_to_cpu(setup->channel_handles[i]);
4922 ibmvfc_dbg(vhost, "Using %u channels\n",
4923 vhost->scsi_scrqs.active_queues);
4926 case IBMVFC_MAD_FAILED:
4927 level += ibmvfc_retry_host_init(vhost);
4928 ibmvfc_log(vhost, level, "Channel Setup failed\n");
4930 case IBMVFC_MAD_DRIVER_FAILED:
4933 dev_err(vhost->dev, "Invalid Channel Setup response: 0x%x\n",
4935 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4939 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4940 wake_up(&vhost->work_wait_q);
4943 static void ibmvfc_channel_setup(struct ibmvfc_host *vhost)
4945 struct ibmvfc_channel_setup_mad *mad;
4946 struct ibmvfc_channel_setup *setup_buf = vhost->channel_setup_buf;
4947 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4948 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
4949 unsigned int num_channels =
4950 min(vhost->client_scsi_channels, vhost->max_vios_scsi_channels);
4953 memset(setup_buf, 0, sizeof(*setup_buf));
4954 if (num_channels == 0)
4955 setup_buf->flags = cpu_to_be32(IBMVFC_CANCEL_CHANNELS);
4957 setup_buf->num_scsi_subq_channels = cpu_to_be32(num_channels);
4958 for (i = 0; i < num_channels; i++)
4959 setup_buf->channel_handles[i] = cpu_to_be64(scrqs->scrqs[i].cookie);
4962 ibmvfc_init_event(evt, ibmvfc_channel_setup_done, IBMVFC_MAD_FORMAT);
4963 mad = &evt->iu.channel_setup;
4964 memset(mad, 0, sizeof(*mad));
4965 mad->common.version = cpu_to_be32(1);
4966 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_SETUP);
4967 mad->common.length = cpu_to_be16(sizeof(*mad));
4968 mad->buffer.va = cpu_to_be64(vhost->channel_setup_dma);
4969 mad->buffer.len = cpu_to_be32(sizeof(*vhost->channel_setup_buf));
4971 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4973 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4974 ibmvfc_dbg(vhost, "Sent channel setup\n");
4976 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
4979 static void ibmvfc_channel_enquiry_done(struct ibmvfc_event *evt)
4981 struct ibmvfc_host *vhost = evt->vhost;
4982 struct ibmvfc_channel_enquiry *rsp = &evt->xfer_iu->channel_enquiry;
4983 u32 mad_status = be16_to_cpu(rsp->common.status);
4984 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4986 switch (mad_status) {
4987 case IBMVFC_MAD_SUCCESS:
4988 ibmvfc_dbg(vhost, "Channel Enquiry succeeded\n");
4989 vhost->max_vios_scsi_channels = be32_to_cpu(rsp->num_scsi_subq_channels);
4990 ibmvfc_free_event(evt);
4992 case IBMVFC_MAD_FAILED:
4993 level += ibmvfc_retry_host_init(vhost);
4994 ibmvfc_log(vhost, level, "Channel Enquiry failed\n");
4996 case IBMVFC_MAD_DRIVER_FAILED:
4997 ibmvfc_free_event(evt);
5000 dev_err(vhost->dev, "Invalid Channel Enquiry response: 0x%x\n",
5002 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5003 ibmvfc_free_event(evt);
5007 ibmvfc_channel_setup(vhost);
5010 static void ibmvfc_channel_enquiry(struct ibmvfc_host *vhost)
5012 struct ibmvfc_channel_enquiry *mad;
5013 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
5015 ibmvfc_init_event(evt, ibmvfc_channel_enquiry_done, IBMVFC_MAD_FORMAT);
5016 mad = &evt->iu.channel_enquiry;
5017 memset(mad, 0, sizeof(*mad));
5018 mad->common.version = cpu_to_be32(1);
5019 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_ENQUIRY);
5020 mad->common.length = cpu_to_be16(sizeof(*mad));
5022 if (mig_channels_only)
5023 mad->flags |= cpu_to_be32(IBMVFC_NO_CHANNELS_TO_CRQ_SUPPORT);
5024 if (mig_no_less_channels)
5025 mad->flags |= cpu_to_be32(IBMVFC_NO_N_TO_M_CHANNELS_SUPPORT);
5027 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5029 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5030 ibmvfc_dbg(vhost, "Send channel enquiry\n");
5032 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5036 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
5037 * @evt: ibmvfc event struct
5040 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
5042 struct ibmvfc_host *vhost = evt->vhost;
5043 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
5044 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
5045 unsigned int npiv_max_sectors;
5046 int level = IBMVFC_DEFAULT_LOG_LEVEL;
5048 switch (mad_status) {
5049 case IBMVFC_MAD_SUCCESS:
5050 ibmvfc_free_event(evt);
5052 case IBMVFC_MAD_FAILED:
5053 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
5054 level += ibmvfc_retry_host_init(vhost);
5056 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5057 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
5058 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
5059 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
5060 ibmvfc_free_event(evt);
5062 case IBMVFC_MAD_CRQ_ERROR:
5063 ibmvfc_retry_host_init(vhost);
5065 case IBMVFC_MAD_DRIVER_FAILED:
5066 ibmvfc_free_event(evt);
5069 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
5070 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5071 ibmvfc_free_event(evt);
5075 vhost->client_migrated = 0;
5077 if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
5078 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
5080 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5081 wake_up(&vhost->work_wait_q);
5085 if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
5086 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
5088 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5089 wake_up(&vhost->work_wait_q);
5093 vhost->logged_in = 1;
5094 npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
5095 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
5096 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
5097 rsp->drc_name, npiv_max_sectors);
5099 fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
5100 fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
5101 fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
5102 fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
5103 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
5104 fc_host_supported_classes(vhost->host) = 0;
5105 if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
5106 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
5107 if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
5108 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
5109 if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
5110 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
5111 fc_host_maxframe_size(vhost->host) =
5112 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
5114 vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
5115 vhost->host->max_sectors = npiv_max_sectors;
5117 if (ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPORT_CHANNELS) && vhost->do_enquiry) {
5118 ibmvfc_channel_enquiry(vhost);
5120 vhost->do_enquiry = 0;
5121 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5122 wake_up(&vhost->work_wait_q);
5127 * ibmvfc_npiv_login - Sends NPIV login
5128 * @vhost: ibmvfc host struct
5131 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
5133 struct ibmvfc_npiv_login_mad *mad;
5134 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
5136 ibmvfc_gather_partition_info(vhost);
5137 ibmvfc_set_login_info(vhost);
5138 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
5140 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
5141 mad = &evt->iu.npiv_login;
5142 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
5143 mad->common.version = cpu_to_be32(1);
5144 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
5145 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
5146 mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
5147 mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
5149 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5151 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5152 ibmvfc_dbg(vhost, "Sent NPIV login\n");
5154 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5158 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
5159 * @evt: ibmvfc event struct
5162 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
5164 struct ibmvfc_host *vhost = evt->vhost;
5165 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
5167 ibmvfc_free_event(evt);
5169 switch (mad_status) {
5170 case IBMVFC_MAD_SUCCESS:
5171 if (list_empty(&vhost->crq.sent) &&
5172 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
5173 ibmvfc_init_host(vhost);
5177 case IBMVFC_MAD_FAILED:
5178 case IBMVFC_MAD_NOT_SUPPORTED:
5179 case IBMVFC_MAD_CRQ_ERROR:
5180 case IBMVFC_MAD_DRIVER_FAILED:
5182 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
5186 ibmvfc_hard_reset_host(vhost);
5190 * ibmvfc_npiv_logout - Issue an NPIV Logout
5191 * @vhost: ibmvfc host struct
5194 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
5196 struct ibmvfc_npiv_logout_mad *mad;
5197 struct ibmvfc_event *evt;
5199 evt = ibmvfc_get_event(&vhost->crq);
5200 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
5202 mad = &evt->iu.npiv_logout;
5203 memset(mad, 0, sizeof(*mad));
5204 mad->common.version = cpu_to_be32(1);
5205 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
5206 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
5208 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
5210 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5211 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
5213 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5217 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
5218 * @vhost: ibmvfc host struct
5221 * 1 if work to do / 0 if not
5223 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
5225 struct ibmvfc_target *tgt;
5227 list_for_each_entry(tgt, &vhost->targets, queue) {
5228 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
5229 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5237 * ibmvfc_dev_logo_to_do - Is there target logout work to do?
5238 * @vhost: ibmvfc host struct
5241 * 1 if work to do / 0 if not
5243 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
5245 struct ibmvfc_target *tgt;
5247 list_for_each_entry(tgt, &vhost->targets, queue) {
5248 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
5249 tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5256 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
5257 * @vhost: ibmvfc host struct
5260 * 1 if work to do / 0 if not
5262 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5264 struct ibmvfc_target *tgt;
5266 if (kthread_should_stop())
5268 switch (vhost->action) {
5269 case IBMVFC_HOST_ACTION_NONE:
5270 case IBMVFC_HOST_ACTION_INIT_WAIT:
5271 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5273 case IBMVFC_HOST_ACTION_TGT_INIT:
5274 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5275 if (vhost->discovery_threads == disc_threads)
5277 list_for_each_entry(tgt, &vhost->targets, queue)
5278 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
5280 list_for_each_entry(tgt, &vhost->targets, queue)
5281 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5284 case IBMVFC_HOST_ACTION_TGT_DEL:
5285 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5286 if (vhost->discovery_threads == disc_threads)
5288 list_for_each_entry(tgt, &vhost->targets, queue)
5289 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
5291 list_for_each_entry(tgt, &vhost->targets, queue)
5292 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5295 case IBMVFC_HOST_ACTION_LOGO:
5296 case IBMVFC_HOST_ACTION_INIT:
5297 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5298 case IBMVFC_HOST_ACTION_QUERY:
5299 case IBMVFC_HOST_ACTION_RESET:
5300 case IBMVFC_HOST_ACTION_REENABLE:
5309 * ibmvfc_work_to_do - Is there task level work to do?
5310 * @vhost: ibmvfc host struct
5313 * 1 if work to do / 0 if not
5315 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5317 unsigned long flags;
5320 spin_lock_irqsave(vhost->host->host_lock, flags);
5321 rc = __ibmvfc_work_to_do(vhost);
5322 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5327 * ibmvfc_log_ae - Log async events if necessary
5328 * @vhost: ibmvfc host struct
5329 * @events: events to log
5332 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
5334 if (events & IBMVFC_AE_RSCN)
5335 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
5336 if ((events & IBMVFC_AE_LINKDOWN) &&
5337 vhost->state >= IBMVFC_HALTED)
5338 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
5339 if ((events & IBMVFC_AE_LINKUP) &&
5340 vhost->state == IBMVFC_INITIALIZING)
5341 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
5345 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
5346 * @tgt: ibmvfc target struct
5349 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
5351 struct ibmvfc_host *vhost = tgt->vhost;
5352 struct fc_rport *rport;
5353 unsigned long flags;
5355 tgt_dbg(tgt, "Adding rport\n");
5356 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
5357 spin_lock_irqsave(vhost->host->host_lock, flags);
5359 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5360 tgt_dbg(tgt, "Deleting rport\n");
5361 list_del(&tgt->queue);
5362 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5363 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5364 fc_remote_port_delete(rport);
5365 del_timer_sync(&tgt->timer);
5366 kref_put(&tgt->kref, ibmvfc_release_tgt);
5368 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5369 tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
5370 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5372 tgt->init_retries = 0;
5373 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5374 fc_remote_port_delete(rport);
5376 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
5377 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5382 tgt_dbg(tgt, "rport add succeeded\n");
5384 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
5385 rport->supported_classes = 0;
5386 tgt->target_id = rport->scsi_target_id;
5387 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
5388 rport->supported_classes |= FC_COS_CLASS1;
5389 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
5390 rport->supported_classes |= FC_COS_CLASS2;
5391 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
5392 rport->supported_classes |= FC_COS_CLASS3;
5394 blk_queue_max_segments(rport->rqst_q, 1);
5396 tgt_dbg(tgt, "rport add failed\n");
5397 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5401 * ibmvfc_do_work - Do task level work
5402 * @vhost: ibmvfc host struct
5405 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
5407 struct ibmvfc_target *tgt;
5408 unsigned long flags;
5409 struct fc_rport *rport;
5413 ibmvfc_log_ae(vhost, vhost->events_to_log);
5414 spin_lock_irqsave(vhost->host->host_lock, flags);
5415 vhost->events_to_log = 0;
5416 switch (vhost->action) {
5417 case IBMVFC_HOST_ACTION_NONE:
5418 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5419 case IBMVFC_HOST_ACTION_INIT_WAIT:
5421 case IBMVFC_HOST_ACTION_RESET:
5422 list_splice_init(&vhost->purge, &purge);
5423 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5424 ibmvfc_complete_purge(&purge);
5425 rc = ibmvfc_reset_crq(vhost);
5427 spin_lock_irqsave(vhost->host->host_lock, flags);
5428 if (!rc || rc == H_CLOSED)
5429 vio_enable_interrupts(to_vio_dev(vhost->dev));
5430 if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
5432 * The only action we could have changed to would have
5433 * been reenable, in which case, we skip the rest of
5434 * this path and wait until we've done the re-enable
5435 * before sending the crq init.
5437 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5439 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
5440 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
5441 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5442 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
5446 case IBMVFC_HOST_ACTION_REENABLE:
5447 list_splice_init(&vhost->purge, &purge);
5448 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5449 ibmvfc_complete_purge(&purge);
5450 rc = ibmvfc_reenable_crq_queue(vhost);
5452 spin_lock_irqsave(vhost->host->host_lock, flags);
5453 if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
5455 * The only action we could have changed to would have
5456 * been reset, in which case, we skip the rest of this
5457 * path and wait until we've done the reset before
5458 * sending the crq init.
5460 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5461 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
5462 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5463 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
5467 case IBMVFC_HOST_ACTION_LOGO:
5468 vhost->job_step(vhost);
5470 case IBMVFC_HOST_ACTION_INIT:
5471 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
5472 if (vhost->delay_init) {
5473 vhost->delay_init = 0;
5474 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5478 vhost->job_step(vhost);
5480 case IBMVFC_HOST_ACTION_QUERY:
5481 list_for_each_entry(tgt, &vhost->targets, queue)
5482 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
5483 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
5485 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5486 list_for_each_entry(tgt, &vhost->targets, queue) {
5487 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5493 if (!ibmvfc_dev_init_to_do(vhost))
5494 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
5496 case IBMVFC_HOST_ACTION_TGT_DEL:
5497 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5498 list_for_each_entry(tgt, &vhost->targets, queue) {
5499 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
5505 if (ibmvfc_dev_logo_to_do(vhost)) {
5506 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5510 list_for_each_entry(tgt, &vhost->targets, queue) {
5511 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5512 tgt_dbg(tgt, "Deleting rport\n");
5515 list_del(&tgt->queue);
5516 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5517 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5519 fc_remote_port_delete(rport);
5520 del_timer_sync(&tgt->timer);
5521 kref_put(&tgt->kref, ibmvfc_release_tgt);
5523 } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5524 tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
5527 tgt->init_retries = 0;
5528 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5531 * If fast fail is enabled, we wait for it to fire and then clean up
5532 * the old port, since we expect the fast fail timer to clean up the
5533 * outstanding I/O faster than waiting for normal command timeouts.
5534 * However, if fast fail is disabled, any I/O outstanding to the
5535 * rport LUNs will stay outstanding indefinitely, since the EH handlers
5536 * won't get invoked for I/O's timing out. If this is a NPIV failover
5537 * scenario, the better alternative is to use the move login.
5539 if (rport && rport->fast_io_fail_tmo == -1)
5540 tgt->move_login = 1;
5541 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5543 fc_remote_port_delete(rport);
5548 if (vhost->state == IBMVFC_INITIALIZING) {
5549 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
5550 if (vhost->reinit) {
5552 scsi_block_requests(vhost->host);
5553 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5554 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5556 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
5557 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5558 wake_up(&vhost->init_wait_q);
5559 schedule_work(&vhost->rport_add_work_q);
5560 vhost->init_retries = 0;
5561 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5562 scsi_unblock_requests(vhost->host);
5567 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
5568 vhost->job_step = ibmvfc_discover_targets;
5571 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5572 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5573 scsi_unblock_requests(vhost->host);
5574 wake_up(&vhost->init_wait_q);
5578 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5579 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
5580 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5581 ibmvfc_alloc_targets(vhost);
5582 spin_lock_irqsave(vhost->host->host_lock, flags);
5584 case IBMVFC_HOST_ACTION_TGT_INIT:
5585 list_for_each_entry(tgt, &vhost->targets, queue) {
5586 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5592 if (!ibmvfc_dev_init_to_do(vhost))
5593 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
5599 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5603 * ibmvfc_work - Do task level work
5604 * @data: ibmvfc host struct
5609 static int ibmvfc_work(void *data)
5611 struct ibmvfc_host *vhost = data;
5614 set_user_nice(current, MIN_NICE);
5617 rc = wait_event_interruptible(vhost->work_wait_q,
5618 ibmvfc_work_to_do(vhost));
5622 if (kthread_should_stop())
5625 ibmvfc_do_work(vhost);
5628 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
5633 * ibmvfc_alloc_queue - Allocate queue
5634 * @vhost: ibmvfc host struct
5635 * @queue: ibmvfc queue to allocate
5636 * @fmt: queue format to allocate
5639 * 0 on success / non-zero on failure
5641 static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
5642 struct ibmvfc_queue *queue,
5643 enum ibmvfc_msg_fmt fmt)
5645 struct device *dev = vhost->dev;
5647 unsigned int pool_size = 0;
5650 spin_lock_init(&queue->_lock);
5651 queue->q_lock = &queue->_lock;
5654 case IBMVFC_CRQ_FMT:
5655 fmt_size = sizeof(*queue->msgs.crq);
5656 pool_size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
5658 case IBMVFC_ASYNC_FMT:
5659 fmt_size = sizeof(*queue->msgs.async);
5661 case IBMVFC_SUB_CRQ_FMT:
5662 fmt_size = sizeof(*queue->msgs.scrq);
5663 /* We need one extra event for Cancel Commands */
5664 pool_size = max_requests + 1;
5667 dev_warn(dev, "Unknown command/response queue message format: %d\n", fmt);
5671 if (ibmvfc_init_event_pool(vhost, queue, pool_size)) {
5672 dev_err(dev, "Couldn't initialize event pool.\n");
5676 queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
5677 if (!queue->msgs.handle)
5680 queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
5683 if (dma_mapping_error(dev, queue->msg_token)) {
5684 free_page((unsigned long)queue->msgs.handle);
5685 queue->msgs.handle = NULL;
5691 queue->size = PAGE_SIZE / fmt_size;
5693 queue->vhost = vhost;
5698 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
5699 * @vhost: ibmvfc host struct
5701 * Allocates a page for messages, maps it for dma, and registers
5702 * the crq with the hypervisor.
5705 * zero on success / other on failure
5707 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
5709 int rc, retrc = -ENOMEM;
5710 struct device *dev = vhost->dev;
5711 struct vio_dev *vdev = to_vio_dev(dev);
5712 struct ibmvfc_queue *crq = &vhost->crq;
5715 if (ibmvfc_alloc_queue(vhost, crq, IBMVFC_CRQ_FMT))
5718 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5719 crq->msg_token, PAGE_SIZE);
5721 if (rc == H_RESOURCE)
5722 /* maybe kexecing and resource is busy. try a reset */
5723 retrc = rc = ibmvfc_reset_crq(vhost);
5726 dev_warn(dev, "Partner adapter not ready\n");
5728 dev_warn(dev, "Error %d opening adapter\n", rc);
5729 goto reg_crq_failed;
5734 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
5736 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
5737 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
5738 goto req_irq_failed;
5741 if ((rc = vio_enable_interrupts(vdev))) {
5742 dev_err(dev, "Error %d enabling interrupts\n", rc);
5743 goto req_irq_failed;
5750 tasklet_kill(&vhost->tasklet);
5752 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5753 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5755 ibmvfc_free_queue(vhost, crq);
5759 static int ibmvfc_register_scsi_channel(struct ibmvfc_host *vhost,
5762 struct device *dev = vhost->dev;
5763 struct vio_dev *vdev = to_vio_dev(dev);
5764 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5769 rc = h_reg_sub_crq(vdev->unit_address, scrq->msg_token, PAGE_SIZE,
5770 &scrq->cookie, &scrq->hw_irq);
5772 /* H_CLOSED indicates successful register, but no CRQ partner */
5773 if (rc && rc != H_CLOSED) {
5774 dev_warn(dev, "Error registering sub-crq: %d\n", rc);
5775 if (rc == H_PARAMETER)
5776 dev_warn_once(dev, "Firmware may not support MQ\n");
5780 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
5784 dev_err(dev, "Error mapping sub-crq[%d] irq\n", index);
5788 snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-scsi%d",
5789 vdev->unit_address, index);
5790 rc = request_irq(scrq->irq, ibmvfc_interrupt_scsi, 0, scrq->name, scrq);
5793 dev_err(dev, "Couldn't register sub-crq[%d] irq\n", index);
5794 irq_dispose_mapping(scrq->irq);
5798 scrq->hwq_id = index;
5805 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address, scrq->cookie);
5806 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5812 static void ibmvfc_deregister_scsi_channel(struct ibmvfc_host *vhost, int index)
5814 struct device *dev = vhost->dev;
5815 struct vio_dev *vdev = to_vio_dev(dev);
5816 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5821 free_irq(scrq->irq, scrq);
5822 irq_dispose_mapping(scrq->irq);
5826 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address,
5828 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5831 dev_err(dev, "Failed to free sub-crq[%d]: rc=%ld\n", index, rc);
5833 /* Clean out the queue */
5834 memset(scrq->msgs.crq, 0, PAGE_SIZE);
5840 static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *vhost)
5845 if (!vhost->mq_enabled || !vhost->scsi_scrqs.scrqs)
5848 for (i = 0; i < nr_scsi_hw_queues; i++) {
5849 if (ibmvfc_register_scsi_channel(vhost, i)) {
5850 for (j = i; j > 0; j--)
5851 ibmvfc_deregister_scsi_channel(vhost, j - 1);
5852 vhost->do_enquiry = 0;
5860 static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *vhost)
5865 if (!vhost->mq_enabled || !vhost->scsi_scrqs.scrqs)
5868 for (i = 0; i < nr_scsi_hw_queues; i++)
5869 ibmvfc_deregister_scsi_channel(vhost, i);
5874 static void ibmvfc_init_sub_crqs(struct ibmvfc_host *vhost)
5876 struct ibmvfc_queue *scrq;
5880 if (!vhost->mq_enabled)
5883 vhost->scsi_scrqs.scrqs = kcalloc(nr_scsi_hw_queues,
5884 sizeof(*vhost->scsi_scrqs.scrqs),
5886 if (!vhost->scsi_scrqs.scrqs) {
5887 vhost->do_enquiry = 0;
5891 for (i = 0; i < nr_scsi_hw_queues; i++) {
5892 scrq = &vhost->scsi_scrqs.scrqs[i];
5893 if (ibmvfc_alloc_queue(vhost, scrq, IBMVFC_SUB_CRQ_FMT)) {
5894 for (j = i; j > 0; j--) {
5895 scrq = &vhost->scsi_scrqs.scrqs[j - 1];
5896 ibmvfc_free_queue(vhost, scrq);
5898 kfree(vhost->scsi_scrqs.scrqs);
5899 vhost->scsi_scrqs.scrqs = NULL;
5900 vhost->scsi_scrqs.active_queues = 0;
5901 vhost->do_enquiry = 0;
5902 vhost->mq_enabled = 0;
5907 ibmvfc_reg_sub_crqs(vhost);
5912 static void ibmvfc_release_sub_crqs(struct ibmvfc_host *vhost)
5914 struct ibmvfc_queue *scrq;
5918 if (!vhost->scsi_scrqs.scrqs)
5921 ibmvfc_dereg_sub_crqs(vhost);
5923 for (i = 0; i < nr_scsi_hw_queues; i++) {
5924 scrq = &vhost->scsi_scrqs.scrqs[i];
5925 ibmvfc_free_queue(vhost, scrq);
5928 kfree(vhost->scsi_scrqs.scrqs);
5929 vhost->scsi_scrqs.scrqs = NULL;
5930 vhost->scsi_scrqs.active_queues = 0;
5935 * ibmvfc_free_mem - Free memory for vhost
5936 * @vhost: ibmvfc host struct
5941 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
5943 struct ibmvfc_queue *async_q = &vhost->async_crq;
5946 mempool_destroy(vhost->tgt_pool);
5947 kfree(vhost->trace);
5948 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
5949 vhost->disc_buf_dma);
5950 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
5951 vhost->login_buf, vhost->login_buf_dma);
5952 dma_free_coherent(vhost->dev, sizeof(*vhost->channel_setup_buf),
5953 vhost->channel_setup_buf, vhost->channel_setup_dma);
5954 dma_pool_destroy(vhost->sg_pool);
5955 ibmvfc_free_queue(vhost, async_q);
5960 * ibmvfc_alloc_mem - Allocate memory for vhost
5961 * @vhost: ibmvfc host struct
5964 * 0 on success / non-zero on failure
5966 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
5968 struct ibmvfc_queue *async_q = &vhost->async_crq;
5969 struct device *dev = vhost->dev;
5972 if (ibmvfc_alloc_queue(vhost, async_q, IBMVFC_ASYNC_FMT)) {
5973 dev_err(dev, "Couldn't allocate/map async queue.\n");
5977 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
5978 SG_ALL * sizeof(struct srp_direct_buf),
5979 sizeof(struct srp_direct_buf), 0);
5981 if (!vhost->sg_pool) {
5982 dev_err(dev, "Failed to allocate sg pool\n");
5983 goto unmap_async_crq;
5986 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
5987 &vhost->login_buf_dma, GFP_KERNEL);
5989 if (!vhost->login_buf) {
5990 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
5994 vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
5995 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
5996 &vhost->disc_buf_dma, GFP_KERNEL);
5998 if (!vhost->disc_buf) {
5999 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
6000 goto free_login_buffer;
6003 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
6004 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
6005 atomic_set(&vhost->trace_index, -1);
6008 goto free_disc_buffer;
6010 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
6011 sizeof(struct ibmvfc_target));
6013 if (!vhost->tgt_pool) {
6014 dev_err(dev, "Couldn't allocate target memory pool\n");
6018 vhost->channel_setup_buf = dma_alloc_coherent(dev, sizeof(*vhost->channel_setup_buf),
6019 &vhost->channel_setup_dma,
6022 if (!vhost->channel_setup_buf) {
6023 dev_err(dev, "Couldn't allocate Channel Setup buffer\n");
6031 mempool_destroy(vhost->tgt_pool);
6033 kfree(vhost->trace);
6035 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
6036 vhost->disc_buf_dma);
6038 dma_free_coherent(dev, sizeof(*vhost->login_buf),
6039 vhost->login_buf, vhost->login_buf_dma);
6041 dma_pool_destroy(vhost->sg_pool);
6043 ibmvfc_free_queue(vhost, async_q);
6050 * ibmvfc_rport_add_thread - Worker thread for rport adds
6051 * @work: work struct
6054 static void ibmvfc_rport_add_thread(struct work_struct *work)
6056 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
6058 struct ibmvfc_target *tgt;
6059 struct fc_rport *rport;
6060 unsigned long flags;
6064 spin_lock_irqsave(vhost->host->host_lock, flags);
6067 if (vhost->state != IBMVFC_ACTIVE)
6070 list_for_each_entry(tgt, &vhost->targets, queue) {
6071 if (tgt->add_rport) {
6074 kref_get(&tgt->kref);
6077 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6078 ibmvfc_tgt_add_rport(tgt);
6079 } else if (get_device(&rport->dev)) {
6080 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6081 tgt_dbg(tgt, "Setting rport roles\n");
6082 fc_remote_port_rolechg(rport, tgt->ids.roles);
6083 put_device(&rport->dev);
6085 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6088 kref_put(&tgt->kref, ibmvfc_release_tgt);
6089 spin_lock_irqsave(vhost->host->host_lock, flags);
6095 if (vhost->state == IBMVFC_ACTIVE)
6096 vhost->scan_complete = 1;
6097 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6102 * ibmvfc_probe - Adapter hot plug add entry point
6103 * @vdev: vio device struct
6104 * @id: vio device id struct
6107 * 0 on success / non-zero on failure
6109 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
6111 struct ibmvfc_host *vhost;
6112 struct Scsi_Host *shost;
6113 struct device *dev = &vdev->dev;
6115 unsigned int max_scsi_queues = IBMVFC_MAX_SCSI_QUEUES;
6118 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
6120 dev_err(dev, "Couldn't allocate host data\n");
6124 shost->transportt = ibmvfc_transport_template;
6125 shost->can_queue = max_requests;
6126 shost->max_lun = max_lun;
6127 shost->max_id = max_targets;
6128 shost->max_sectors = IBMVFC_MAX_SECTORS;
6129 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
6130 shost->unique_id = shost->host_no;
6131 shost->nr_hw_queues = mq_enabled ? min(max_scsi_queues, nr_scsi_hw_queues) : 1;
6133 vhost = shost_priv(shost);
6134 INIT_LIST_HEAD(&vhost->targets);
6135 INIT_LIST_HEAD(&vhost->purge);
6136 sprintf(vhost->name, IBMVFC_NAME);
6137 vhost->host = shost;
6139 vhost->partition_number = -1;
6140 vhost->log_level = log_level;
6141 vhost->task_set = 1;
6143 vhost->mq_enabled = mq_enabled;
6144 vhost->client_scsi_channels = min(shost->nr_hw_queues, nr_scsi_channels);
6145 vhost->using_channels = 0;
6146 vhost->do_enquiry = 1;
6147 vhost->scan_timeout = 0;
6149 strcpy(vhost->partition_name, "UNKNOWN");
6150 init_waitqueue_head(&vhost->work_wait_q);
6151 init_waitqueue_head(&vhost->init_wait_q);
6152 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
6153 mutex_init(&vhost->passthru_mutex);
6155 if ((rc = ibmvfc_alloc_mem(vhost)))
6156 goto free_scsi_host;
6158 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
6161 if (IS_ERR(vhost->work_thread)) {
6162 dev_err(dev, "Couldn't create kernel thread: %ld\n",
6163 PTR_ERR(vhost->work_thread));
6164 rc = PTR_ERR(vhost->work_thread);
6168 if ((rc = ibmvfc_init_crq(vhost))) {
6169 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
6173 if ((rc = scsi_add_host(shost, dev)))
6176 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
6178 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
6179 &ibmvfc_trace_attr))) {
6180 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
6184 ibmvfc_init_sub_crqs(vhost);
6186 if (shost_to_fc_host(shost)->rqst_q)
6187 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
6188 dev_set_drvdata(dev, vhost);
6189 spin_lock(&ibmvfc_driver_lock);
6190 list_add_tail(&vhost->queue, &ibmvfc_head);
6191 spin_unlock(&ibmvfc_driver_lock);
6193 ibmvfc_send_crq_init(vhost);
6194 scsi_scan_host(shost);
6198 scsi_remove_host(shost);
6200 ibmvfc_release_crq_queue(vhost);
6202 kthread_stop(vhost->work_thread);
6204 ibmvfc_free_mem(vhost);
6206 scsi_host_put(shost);
6213 * ibmvfc_remove - Adapter hot plug remove entry point
6214 * @vdev: vio device struct
6219 static void ibmvfc_remove(struct vio_dev *vdev)
6221 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
6223 unsigned long flags;
6226 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
6228 spin_lock_irqsave(vhost->host->host_lock, flags);
6229 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
6230 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6232 ibmvfc_wait_while_resetting(vhost);
6233 kthread_stop(vhost->work_thread);
6234 fc_remove_host(vhost->host);
6235 scsi_remove_host(vhost->host);
6237 spin_lock_irqsave(vhost->host->host_lock, flags);
6238 ibmvfc_purge_requests(vhost, DID_ERROR);
6239 list_splice_init(&vhost->purge, &purge);
6240 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6241 ibmvfc_complete_purge(&purge);
6242 ibmvfc_release_sub_crqs(vhost);
6243 ibmvfc_release_crq_queue(vhost);
6245 ibmvfc_free_mem(vhost);
6246 spin_lock(&ibmvfc_driver_lock);
6247 list_del(&vhost->queue);
6248 spin_unlock(&ibmvfc_driver_lock);
6249 scsi_host_put(vhost->host);
6254 * ibmvfc_resume - Resume from suspend
6255 * @dev: device struct
6257 * We may have lost an interrupt across suspend/resume, so kick the
6261 static int ibmvfc_resume(struct device *dev)
6263 unsigned long flags;
6264 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
6265 struct vio_dev *vdev = to_vio_dev(dev);
6267 spin_lock_irqsave(vhost->host->host_lock, flags);
6268 vio_disable_interrupts(vdev);
6269 tasklet_schedule(&vhost->tasklet);
6270 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6275 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
6276 * @vdev: vio device struct
6279 * Number of bytes the driver will need to DMA map at the same time in
6280 * order to perform well.
6282 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
6284 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
6285 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
6288 static const struct vio_device_id ibmvfc_device_table[] = {
6289 {"fcp", "IBM,vfc-client"},
6292 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
6294 static const struct dev_pm_ops ibmvfc_pm_ops = {
6295 .resume = ibmvfc_resume
6298 static struct vio_driver ibmvfc_driver = {
6299 .id_table = ibmvfc_device_table,
6300 .probe = ibmvfc_probe,
6301 .remove = ibmvfc_remove,
6302 .get_desired_dma = ibmvfc_get_desired_dma,
6303 .name = IBMVFC_NAME,
6304 .pm = &ibmvfc_pm_ops,
6307 static struct fc_function_template ibmvfc_transport_functions = {
6308 .show_host_fabric_name = 1,
6309 .show_host_node_name = 1,
6310 .show_host_port_name = 1,
6311 .show_host_supported_classes = 1,
6312 .show_host_port_type = 1,
6313 .show_host_port_id = 1,
6314 .show_host_maxframe_size = 1,
6316 .get_host_port_state = ibmvfc_get_host_port_state,
6317 .show_host_port_state = 1,
6319 .get_host_speed = ibmvfc_get_host_speed,
6320 .show_host_speed = 1,
6322 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
6323 .terminate_rport_io = ibmvfc_terminate_rport_io,
6325 .show_rport_maxframe_size = 1,
6326 .show_rport_supported_classes = 1,
6328 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
6329 .show_rport_dev_loss_tmo = 1,
6331 .get_starget_node_name = ibmvfc_get_starget_node_name,
6332 .show_starget_node_name = 1,
6334 .get_starget_port_name = ibmvfc_get_starget_port_name,
6335 .show_starget_port_name = 1,
6337 .get_starget_port_id = ibmvfc_get_starget_port_id,
6338 .show_starget_port_id = 1,
6340 .bsg_request = ibmvfc_bsg_request,
6341 .bsg_timeout = ibmvfc_bsg_timeout,
6345 * ibmvfc_module_init - Initialize the ibmvfc module
6348 * 0 on success / other on failure
6350 static int __init ibmvfc_module_init(void)
6354 if (!firmware_has_feature(FW_FEATURE_VIO))
6357 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
6358 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
6360 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
6361 if (!ibmvfc_transport_template)
6364 rc = vio_register_driver(&ibmvfc_driver);
6366 fc_release_transport(ibmvfc_transport_template);
6371 * ibmvfc_module_exit - Teardown the ibmvfc module
6376 static void __exit ibmvfc_module_exit(void)
6378 vio_unregister_driver(&ibmvfc_driver);
6379 fc_release_transport(ibmvfc_transport_template);
6382 module_init(ibmvfc_module_init);
6383 module_exit(ibmvfc_module_exit);