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/kthread.h>
17 #include <linux/slab.h>
20 #include <linux/stringify.h>
21 #include <linux/bsg-lib.h>
22 #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_release_sub_crqs(struct ibmvfc_host *);
163 static void ibmvfc_init_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 * @vfc_cmd: ibmvfc command struct
332 * SCSI result value to return for completed command
334 static int ibmvfc_get_err_result(struct ibmvfc_host *vhost, struct ibmvfc_cmd *vfc_cmd)
337 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
338 int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
340 if ((rsp->flags & FCP_RSP_LEN_VALID) &&
341 ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
342 rsp->data.info.rsp_code))
343 return DID_ERROR << 16;
345 err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
347 return rsp->scsi_status | (cmd_status[err].result << 16);
348 return rsp->scsi_status | (DID_ERROR << 16);
352 * ibmvfc_retry_cmd - Determine if error status is retryable
353 * @status: status / error class
357 * 1 if error should be retried / 0 if it should not
359 static int ibmvfc_retry_cmd(u16 status, u16 error)
361 int rc = ibmvfc_get_err_index(status, error);
364 return cmd_status[rc].retry;
368 static const char *unknown_fc_explain = "unknown fc explain";
370 static const struct {
374 { 0x00, "no additional explanation" },
375 { 0x01, "service parameter error - options" },
376 { 0x03, "service parameter error - initiator control" },
377 { 0x05, "service parameter error - recipient control" },
378 { 0x07, "service parameter error - received data field size" },
379 { 0x09, "service parameter error - concurrent seq" },
380 { 0x0B, "service parameter error - credit" },
381 { 0x0D, "invalid N_Port/F_Port_Name" },
382 { 0x0E, "invalid node/Fabric Name" },
383 { 0x0F, "invalid common service parameters" },
384 { 0x11, "invalid association header" },
385 { 0x13, "association header required" },
386 { 0x15, "invalid originator S_ID" },
387 { 0x17, "invalid OX_ID-RX-ID combination" },
388 { 0x19, "command (request) already in progress" },
389 { 0x1E, "N_Port Login requested" },
390 { 0x1F, "Invalid N_Port_ID" },
393 static const struct {
397 { 0x00, "no additional explanation" },
398 { 0x01, "port identifier not registered" },
399 { 0x02, "port name not registered" },
400 { 0x03, "node name not registered" },
401 { 0x04, "class of service not registered" },
402 { 0x06, "initial process associator not registered" },
403 { 0x07, "FC-4 TYPEs not registered" },
404 { 0x08, "symbolic port name not registered" },
405 { 0x09, "symbolic node name not registered" },
406 { 0x0A, "port type not registered" },
407 { 0xF0, "authorization exception" },
408 { 0xF1, "authentication exception" },
409 { 0xF2, "data base full" },
410 { 0xF3, "data base empty" },
411 { 0xF4, "processing request" },
412 { 0xF5, "unable to verify connection" },
413 { 0xF6, "devices not in a common zone" },
417 * ibmvfc_get_ls_explain - Return the FC Explain description text
418 * @status: FC Explain status
423 static const char *ibmvfc_get_ls_explain(u16 status)
427 for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
428 if (ls_explain[i].fc_explain == status)
429 return ls_explain[i].name;
431 return unknown_fc_explain;
435 * ibmvfc_get_gs_explain - Return the FC Explain description text
436 * @status: FC Explain status
441 static const char *ibmvfc_get_gs_explain(u16 status)
445 for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
446 if (gs_explain[i].fc_explain == status)
447 return gs_explain[i].name;
449 return unknown_fc_explain;
452 static const struct {
453 enum ibmvfc_fc_type fc_type;
456 { IBMVFC_FABRIC_REJECT, "fabric reject" },
457 { IBMVFC_PORT_REJECT, "port reject" },
458 { IBMVFC_LS_REJECT, "ELS reject" },
459 { IBMVFC_FABRIC_BUSY, "fabric busy" },
460 { IBMVFC_PORT_BUSY, "port busy" },
461 { IBMVFC_BASIC_REJECT, "basic reject" },
464 static const char *unknown_fc_type = "unknown fc type";
467 * ibmvfc_get_fc_type - Return the FC Type description text
468 * @status: FC Type error status
473 static const char *ibmvfc_get_fc_type(u16 status)
477 for (i = 0; i < ARRAY_SIZE(fc_type); i++)
478 if (fc_type[i].fc_type == status)
479 return fc_type[i].name;
481 return unknown_fc_type;
485 * ibmvfc_set_tgt_action - Set the next init action for the target
486 * @tgt: ibmvfc target struct
487 * @action: action to perform
490 * 0 if action changed / non-zero if not changed
492 static int ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
493 enum ibmvfc_target_action action)
497 switch (tgt->action) {
498 case IBMVFC_TGT_ACTION_LOGOUT_RPORT:
499 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT ||
500 action == IBMVFC_TGT_ACTION_DEL_RPORT) {
501 tgt->action = action;
505 case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT:
506 if (action == IBMVFC_TGT_ACTION_DEL_RPORT ||
507 action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
508 tgt->action = action;
512 case IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT:
513 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
514 tgt->action = action;
518 case IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT:
519 if (action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
520 tgt->action = action;
524 case IBMVFC_TGT_ACTION_DEL_RPORT:
525 if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
526 tgt->action = action;
530 case IBMVFC_TGT_ACTION_DELETED_RPORT:
533 tgt->action = action;
538 if (action >= IBMVFC_TGT_ACTION_LOGOUT_RPORT)
545 * ibmvfc_set_host_state - Set the state for the host
546 * @vhost: ibmvfc host struct
547 * @state: state to set host to
550 * 0 if state changed / non-zero if not changed
552 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
553 enum ibmvfc_host_state state)
557 switch (vhost->state) {
558 case IBMVFC_HOST_OFFLINE:
562 vhost->state = state;
570 * ibmvfc_set_host_action - Set the next init action for the host
571 * @vhost: ibmvfc host struct
572 * @action: action to perform
575 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
576 enum ibmvfc_host_action action)
579 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
580 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
581 vhost->action = action;
583 case IBMVFC_HOST_ACTION_LOGO_WAIT:
584 if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
585 vhost->action = action;
587 case IBMVFC_HOST_ACTION_INIT_WAIT:
588 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
589 vhost->action = action;
591 case IBMVFC_HOST_ACTION_QUERY:
592 switch (vhost->action) {
593 case IBMVFC_HOST_ACTION_INIT_WAIT:
594 case IBMVFC_HOST_ACTION_NONE:
595 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
596 vhost->action = action;
602 case IBMVFC_HOST_ACTION_TGT_INIT:
603 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
604 vhost->action = action;
606 case IBMVFC_HOST_ACTION_INIT:
607 case IBMVFC_HOST_ACTION_TGT_DEL:
608 switch (vhost->action) {
609 case IBMVFC_HOST_ACTION_RESET:
610 case IBMVFC_HOST_ACTION_REENABLE:
613 vhost->action = action;
617 case IBMVFC_HOST_ACTION_LOGO:
618 case IBMVFC_HOST_ACTION_QUERY_TGTS:
619 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
620 case IBMVFC_HOST_ACTION_NONE:
621 case IBMVFC_HOST_ACTION_RESET:
622 case IBMVFC_HOST_ACTION_REENABLE:
624 vhost->action = action;
630 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
631 * @vhost: ibmvfc host struct
636 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
638 if (vhost->action == IBMVFC_HOST_ACTION_NONE &&
639 vhost->state == IBMVFC_ACTIVE) {
640 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
641 scsi_block_requests(vhost->host);
642 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
647 wake_up(&vhost->work_wait_q);
651 * ibmvfc_del_tgt - Schedule cleanup and removal of the target
652 * @tgt: ibmvfc target struct
653 * @job_step: job step to perform
656 static void ibmvfc_del_tgt(struct ibmvfc_target *tgt)
658 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT))
659 tgt->job_step = ibmvfc_tgt_implicit_logout_and_del;
660 wake_up(&tgt->vhost->work_wait_q);
664 * ibmvfc_link_down - Handle a link down event from the adapter
665 * @vhost: ibmvfc host struct
666 * @state: ibmvfc host state to enter
669 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
670 enum ibmvfc_host_state state)
672 struct ibmvfc_target *tgt;
675 scsi_block_requests(vhost->host);
676 list_for_each_entry(tgt, &vhost->targets, queue)
678 ibmvfc_set_host_state(vhost, state);
679 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
680 vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
681 wake_up(&vhost->work_wait_q);
686 * ibmvfc_init_host - Start host initialization
687 * @vhost: ibmvfc host struct
692 static void ibmvfc_init_host(struct ibmvfc_host *vhost)
694 struct ibmvfc_target *tgt;
696 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
697 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
699 "Host initialization retries exceeded. Taking adapter offline\n");
700 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
705 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
706 memset(vhost->async_crq.msgs.async, 0, PAGE_SIZE);
707 vhost->async_crq.cur = 0;
709 list_for_each_entry(tgt, &vhost->targets, queue)
711 scsi_block_requests(vhost->host);
712 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
713 vhost->job_step = ibmvfc_npiv_login;
714 wake_up(&vhost->work_wait_q);
719 * ibmvfc_send_crq - Send a CRQ
720 * @vhost: ibmvfc host struct
721 * @word1: the first 64 bits of the data
722 * @word2: the second 64 bits of the data
725 * 0 on success / other on failure
727 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
729 struct vio_dev *vdev = to_vio_dev(vhost->dev);
730 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
733 static int ibmvfc_send_sub_crq(struct ibmvfc_host *vhost, u64 cookie, u64 word1,
734 u64 word2, u64 word3, u64 word4)
736 struct vio_dev *vdev = to_vio_dev(vhost->dev);
738 return plpar_hcall_norets(H_SEND_SUB_CRQ, vdev->unit_address, cookie,
739 word1, word2, word3, word4);
743 * ibmvfc_send_crq_init - Send a CRQ init message
744 * @vhost: ibmvfc host struct
747 * 0 on success / other on failure
749 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
751 ibmvfc_dbg(vhost, "Sending CRQ init\n");
752 return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
756 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
757 * @vhost: ibmvfc host struct
760 * 0 on success / other on failure
762 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
764 ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
765 return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
769 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
770 * @vhost: ibmvfc host who owns the event pool
772 * Returns zero on success.
774 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost,
775 struct ibmvfc_queue *queue,
779 struct ibmvfc_event_pool *pool = &queue->evt_pool;
786 pool->events = kcalloc(size, sizeof(*pool->events), GFP_KERNEL);
790 pool->iu_storage = dma_alloc_coherent(vhost->dev,
791 size * sizeof(*pool->iu_storage),
794 if (!pool->iu_storage) {
799 INIT_LIST_HEAD(&queue->sent);
800 INIT_LIST_HEAD(&queue->free);
801 spin_lock_init(&queue->l_lock);
803 for (i = 0; i < size; ++i) {
804 struct ibmvfc_event *evt = &pool->events[i];
806 atomic_set(&evt->free, 1);
807 evt->crq.valid = 0x80;
808 evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
809 evt->xfer_iu = pool->iu_storage + i;
812 evt->ext_list = NULL;
813 list_add_tail(&evt->queue_list, &queue->free);
821 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
822 * @vhost: ibmvfc host who owns the event pool
825 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost,
826 struct ibmvfc_queue *queue)
829 struct ibmvfc_event_pool *pool = &queue->evt_pool;
832 for (i = 0; i < pool->size; ++i) {
833 list_del(&pool->events[i].queue_list);
834 BUG_ON(atomic_read(&pool->events[i].free) != 1);
835 if (pool->events[i].ext_list)
836 dma_pool_free(vhost->sg_pool,
837 pool->events[i].ext_list,
838 pool->events[i].ext_list_token);
842 dma_free_coherent(vhost->dev,
843 pool->size * sizeof(*pool->iu_storage),
844 pool->iu_storage, pool->iu_token);
849 * ibmvfc_free_queue - Deallocate queue
850 * @vhost: ibmvfc host struct
851 * @queue: ibmvfc queue struct
853 * Unmaps dma and deallocates page for messages
855 static void ibmvfc_free_queue(struct ibmvfc_host *vhost,
856 struct ibmvfc_queue *queue)
858 struct device *dev = vhost->dev;
860 dma_unmap_single(dev, queue->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
861 free_page((unsigned long)queue->msgs.handle);
862 queue->msgs.handle = NULL;
864 ibmvfc_free_event_pool(vhost, queue);
868 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
869 * @vhost: ibmvfc host struct
871 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
872 * the crq with the hypervisor.
874 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
877 struct vio_dev *vdev = to_vio_dev(vhost->dev);
878 struct ibmvfc_queue *crq = &vhost->crq;
880 ibmvfc_dbg(vhost, "Releasing CRQ\n");
881 free_irq(vdev->irq, vhost);
882 tasklet_kill(&vhost->tasklet);
886 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
887 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
889 vhost->state = IBMVFC_NO_CRQ;
890 vhost->logged_in = 0;
892 ibmvfc_free_queue(vhost, crq);
896 * ibmvfc_reenable_crq_queue - reenables the CRQ
897 * @vhost: ibmvfc host struct
900 * 0 on success / other on failure
902 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
905 struct vio_dev *vdev = to_vio_dev(vhost->dev);
908 ibmvfc_release_sub_crqs(vhost);
910 /* Re-enable the CRQ */
914 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
915 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
918 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
920 spin_lock_irqsave(vhost->host->host_lock, flags);
921 spin_lock(vhost->crq.q_lock);
922 vhost->do_enquiry = 1;
923 vhost->using_channels = 0;
924 spin_unlock(vhost->crq.q_lock);
925 spin_unlock_irqrestore(vhost->host->host_lock, flags);
927 ibmvfc_init_sub_crqs(vhost);
933 * ibmvfc_reset_crq - resets a crq after a failure
934 * @vhost: ibmvfc host struct
937 * 0 on success / other on failure
939 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
943 struct vio_dev *vdev = to_vio_dev(vhost->dev);
944 struct ibmvfc_queue *crq = &vhost->crq;
946 ibmvfc_release_sub_crqs(vhost);
952 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
953 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
955 spin_lock_irqsave(vhost->host->host_lock, flags);
956 spin_lock(vhost->crq.q_lock);
957 vhost->state = IBMVFC_NO_CRQ;
958 vhost->logged_in = 0;
959 vhost->do_enquiry = 1;
960 vhost->using_channels = 0;
962 /* Clean out the queue */
963 memset(crq->msgs.crq, 0, PAGE_SIZE);
966 /* And re-open it again */
967 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
968 crq->msg_token, PAGE_SIZE);
971 /* Adapter is good, but other end is not ready */
972 dev_warn(vhost->dev, "Partner adapter not ready\n");
974 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
976 spin_unlock(vhost->crq.q_lock);
977 spin_unlock_irqrestore(vhost->host->host_lock, flags);
979 ibmvfc_init_sub_crqs(vhost);
985 * ibmvfc_valid_event - Determines if event is valid.
986 * @pool: event_pool that contains the event
987 * @evt: ibmvfc event to be checked for validity
990 * 1 if event is valid / 0 if event is not valid
992 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
993 struct ibmvfc_event *evt)
995 int index = evt - pool->events;
996 if (index < 0 || index >= pool->size) /* outside of bounds */
998 if (evt != pool->events + index) /* unaligned */
1004 * ibmvfc_free_event - Free the specified event
1005 * @evt: ibmvfc_event to be freed
1008 static void ibmvfc_free_event(struct ibmvfc_event *evt)
1010 struct ibmvfc_event_pool *pool = &evt->queue->evt_pool;
1011 unsigned long flags;
1013 BUG_ON(!ibmvfc_valid_event(pool, evt));
1014 BUG_ON(atomic_inc_return(&evt->free) != 1);
1016 spin_lock_irqsave(&evt->queue->l_lock, flags);
1017 list_add_tail(&evt->queue_list, &evt->queue->free);
1019 complete(evt->eh_comp);
1020 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1024 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
1025 * @evt: ibmvfc event struct
1027 * This function does not setup any error status, that must be done
1028 * before this function gets called.
1030 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
1032 struct scsi_cmnd *cmnd = evt->cmnd;
1035 scsi_dma_unmap(cmnd);
1036 cmnd->scsi_done(cmnd);
1039 ibmvfc_free_event(evt);
1043 * ibmvfc_complete_purge - Complete failed command list
1044 * @purge_list: list head of failed commands
1046 * This function runs completions on commands to fail as a result of a
1047 * host reset or platform migration.
1049 static void ibmvfc_complete_purge(struct list_head *purge_list)
1051 struct ibmvfc_event *evt, *pos;
1053 list_for_each_entry_safe(evt, pos, purge_list, queue_list) {
1054 list_del(&evt->queue_list);
1055 ibmvfc_trc_end(evt);
1061 * ibmvfc_fail_request - Fail request with specified error code
1062 * @evt: ibmvfc event struct
1063 * @error_code: error code to fail request with
1068 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
1071 evt->cmnd->result = (error_code << 16);
1072 evt->done = ibmvfc_scsi_eh_done;
1074 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
1076 del_timer(&evt->timer);
1080 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
1081 * @vhost: ibmvfc host struct
1082 * @error_code: error code to fail requests with
1087 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
1089 struct ibmvfc_event *evt, *pos;
1090 struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
1091 unsigned long flags;
1095 if (vhost->using_channels)
1096 hwqs = vhost->scsi_scrqs.active_queues;
1098 ibmvfc_dbg(vhost, "Purging all requests\n");
1099 spin_lock_irqsave(&vhost->crq.l_lock, flags);
1100 list_for_each_entry_safe(evt, pos, &vhost->crq.sent, queue_list)
1101 ibmvfc_fail_request(evt, error_code);
1102 list_splice_init(&vhost->crq.sent, &vhost->purge);
1103 spin_unlock_irqrestore(&vhost->crq.l_lock, flags);
1105 for (i = 0; i < hwqs; i++) {
1106 spin_lock_irqsave(queues[i].q_lock, flags);
1107 spin_lock(&queues[i].l_lock);
1108 list_for_each_entry_safe(evt, pos, &queues[i].sent, queue_list)
1109 ibmvfc_fail_request(evt, error_code);
1110 list_splice_init(&queues[i].sent, &vhost->purge);
1111 spin_unlock(&queues[i].l_lock);
1112 spin_unlock_irqrestore(queues[i].q_lock, flags);
1117 * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
1118 * @vhost: struct ibmvfc host to reset
1120 static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
1122 ibmvfc_purge_requests(vhost, DID_ERROR);
1123 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
1124 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
1128 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
1129 * @vhost: struct ibmvfc host to reset
1131 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
1133 if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
1134 !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
1135 scsi_block_requests(vhost->host);
1136 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
1137 vhost->job_step = ibmvfc_npiv_logout;
1138 wake_up(&vhost->work_wait_q);
1140 ibmvfc_hard_reset_host(vhost);
1144 * ibmvfc_reset_host - Reset the connection to the server
1145 * @vhost: ibmvfc host struct
1147 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
1149 unsigned long flags;
1151 spin_lock_irqsave(vhost->host->host_lock, flags);
1152 __ibmvfc_reset_host(vhost);
1153 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1157 * ibmvfc_retry_host_init - Retry host initialization if allowed
1158 * @vhost: ibmvfc host struct
1160 * Returns: 1 if init will be retried / 0 if not
1163 static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
1167 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
1168 vhost->delay_init = 1;
1169 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
1171 "Host initialization retries exceeded. Taking adapter offline\n");
1172 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
1173 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
1174 __ibmvfc_reset_host(vhost);
1176 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
1181 wake_up(&vhost->work_wait_q);
1186 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
1187 * @starget: scsi target struct
1190 * ibmvfc_target struct / NULL if not found
1192 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
1194 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1195 struct ibmvfc_host *vhost = shost_priv(shost);
1196 struct ibmvfc_target *tgt;
1198 list_for_each_entry(tgt, &vhost->targets, queue)
1199 if (tgt->target_id == starget->id) {
1200 kref_get(&tgt->kref);
1207 * ibmvfc_get_target - Find the specified scsi_target
1208 * @starget: scsi target struct
1211 * ibmvfc_target struct / NULL if not found
1213 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
1215 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1216 struct ibmvfc_target *tgt;
1217 unsigned long flags;
1219 spin_lock_irqsave(shost->host_lock, flags);
1220 tgt = __ibmvfc_get_target(starget);
1221 spin_unlock_irqrestore(shost->host_lock, flags);
1226 * ibmvfc_get_host_speed - Get host port speed
1227 * @shost: scsi host struct
1232 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
1234 struct ibmvfc_host *vhost = shost_priv(shost);
1235 unsigned long flags;
1237 spin_lock_irqsave(shost->host_lock, flags);
1238 if (vhost->state == IBMVFC_ACTIVE) {
1239 switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
1241 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1244 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1247 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1250 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
1253 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
1256 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
1259 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
1260 be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
1261 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1265 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1266 spin_unlock_irqrestore(shost->host_lock, flags);
1270 * ibmvfc_get_host_port_state - Get host port state
1271 * @shost: scsi host struct
1276 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
1278 struct ibmvfc_host *vhost = shost_priv(shost);
1279 unsigned long flags;
1281 spin_lock_irqsave(shost->host_lock, flags);
1282 switch (vhost->state) {
1283 case IBMVFC_INITIALIZING:
1285 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1287 case IBMVFC_LINK_DOWN:
1288 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1290 case IBMVFC_LINK_DEAD:
1291 case IBMVFC_HOST_OFFLINE:
1292 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1295 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1298 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1301 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
1302 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1305 spin_unlock_irqrestore(shost->host_lock, flags);
1309 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1310 * @rport: rport struct
1311 * @timeout: timeout value
1316 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1319 rport->dev_loss_tmo = timeout;
1321 rport->dev_loss_tmo = 1;
1325 * ibmvfc_release_tgt - Free memory allocated for a target
1326 * @kref: kref struct
1329 static void ibmvfc_release_tgt(struct kref *kref)
1331 struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1336 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1337 * @starget: scsi target struct
1342 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1344 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1345 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1347 kref_put(&tgt->kref, ibmvfc_release_tgt);
1351 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1352 * @starget: scsi target struct
1357 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1359 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1360 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1362 kref_put(&tgt->kref, ibmvfc_release_tgt);
1366 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1367 * @starget: scsi target struct
1372 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1374 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1375 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1377 kref_put(&tgt->kref, ibmvfc_release_tgt);
1381 * ibmvfc_wait_while_resetting - Wait while the host resets
1382 * @vhost: ibmvfc host struct
1385 * 0 on success / other on failure
1387 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1389 long timeout = wait_event_timeout(vhost->init_wait_q,
1390 ((vhost->state == IBMVFC_ACTIVE ||
1391 vhost->state == IBMVFC_HOST_OFFLINE ||
1392 vhost->state == IBMVFC_LINK_DEAD) &&
1393 vhost->action == IBMVFC_HOST_ACTION_NONE),
1394 (init_timeout * HZ));
1396 return timeout ? 0 : -EIO;
1400 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1401 * @shost: scsi host struct
1404 * 0 on success / other on failure
1406 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1408 struct ibmvfc_host *vhost = shost_priv(shost);
1410 dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1411 ibmvfc_reset_host(vhost);
1412 return ibmvfc_wait_while_resetting(vhost);
1416 * ibmvfc_gather_partition_info - Gather info about the LPAR
1421 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1423 struct device_node *rootdn;
1425 const unsigned int *num;
1427 rootdn = of_find_node_by_path("/");
1431 name = of_get_property(rootdn, "ibm,partition-name", NULL);
1433 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1434 num = of_get_property(rootdn, "ibm,partition-no", NULL);
1436 vhost->partition_number = *num;
1437 of_node_put(rootdn);
1441 * ibmvfc_set_login_info - Setup info for NPIV login
1442 * @vhost: ibmvfc host struct
1447 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1449 struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1450 struct ibmvfc_queue *async_crq = &vhost->async_crq;
1451 struct device_node *of_node = vhost->dev->of_node;
1452 const char *location;
1454 memset(login_info, 0, sizeof(*login_info));
1456 login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
1457 login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
1458 login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
1459 login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
1460 login_info->partition_num = cpu_to_be32(vhost->partition_number);
1461 login_info->vfc_frame_version = cpu_to_be32(1);
1462 login_info->fcp_version = cpu_to_be16(3);
1463 login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
1464 if (vhost->client_migrated)
1465 login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
1467 login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
1468 login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE | IBMVFC_CAN_SEND_VF_WWPN);
1470 if (vhost->mq_enabled || vhost->using_channels)
1471 login_info->capabilities |= cpu_to_be64(IBMVFC_CAN_USE_CHANNELS);
1473 login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
1474 login_info->async.len = cpu_to_be32(async_crq->size *
1475 sizeof(*async_crq->msgs.async));
1476 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1477 strncpy(login_info->device_name,
1478 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
1480 location = of_get_property(of_node, "ibm,loc-code", NULL);
1481 location = location ? location : dev_name(vhost->dev);
1482 strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1486 * ibmvfc_get_event - Gets the next free event in pool
1487 * @vhost: ibmvfc host struct
1489 * Returns a free event from the pool.
1491 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_queue *queue)
1493 struct ibmvfc_event *evt;
1494 unsigned long flags;
1496 spin_lock_irqsave(&queue->l_lock, flags);
1497 BUG_ON(list_empty(&queue->free));
1498 evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
1499 atomic_set(&evt->free, 0);
1500 list_del(&evt->queue_list);
1501 spin_unlock_irqrestore(&queue->l_lock, flags);
1506 * ibmvfc_locked_done - Calls evt completion with host_lock held
1507 * @evt: ibmvfc evt to complete
1509 * All non-scsi command completion callbacks have the expectation that the
1510 * host_lock is held. This callback is used by ibmvfc_init_event to wrap a
1511 * MAD evt with the host_lock.
1513 static void ibmvfc_locked_done(struct ibmvfc_event *evt)
1515 unsigned long flags;
1517 spin_lock_irqsave(evt->vhost->host->host_lock, flags);
1519 spin_unlock_irqrestore(evt->vhost->host->host_lock, flags);
1523 * ibmvfc_init_event - Initialize fields in an event struct that are always
1526 * @done: Routine to call when the event is responded to
1527 * @format: SRP or MAD format
1529 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1530 void (*done) (struct ibmvfc_event *), u8 format)
1533 evt->sync_iu = NULL;
1534 evt->eh_comp = NULL;
1535 evt->crq.format = format;
1536 if (format == IBMVFC_CMD_FORMAT)
1540 evt->done = ibmvfc_locked_done;
1546 * ibmvfc_map_sg_list - Initialize scatterlist
1547 * @scmd: scsi command struct
1548 * @nseg: number of scatterlist segments
1549 * @md: memory descriptor list to initialize
1551 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1552 struct srp_direct_buf *md)
1555 struct scatterlist *sg;
1557 scsi_for_each_sg(scmd, sg, nseg, i) {
1558 md[i].va = cpu_to_be64(sg_dma_address(sg));
1559 md[i].len = cpu_to_be32(sg_dma_len(sg));
1565 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
1566 * @scmd: struct scsi_cmnd with the scatterlist
1567 * @evt: ibmvfc event struct
1568 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1569 * @dev: device for which to map dma memory
1572 * 0 on success / non-zero on failure
1574 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1575 struct ibmvfc_event *evt,
1576 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1580 struct srp_direct_buf *data = &vfc_cmd->ioba;
1581 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1582 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(evt->vhost, vfc_cmd);
1585 vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1587 sg_mapped = scsi_dma_map(scmd);
1589 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1591 } else if (unlikely(sg_mapped < 0)) {
1592 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1593 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1597 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1598 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1599 iu->add_cdb_len |= IBMVFC_WRDATA;
1601 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1602 iu->add_cdb_len |= IBMVFC_RDDATA;
1605 if (sg_mapped == 1) {
1606 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1610 vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1612 if (!evt->ext_list) {
1613 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1614 &evt->ext_list_token);
1616 if (!evt->ext_list) {
1617 scsi_dma_unmap(scmd);
1618 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1619 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1624 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1626 data->va = cpu_to_be64(evt->ext_list_token);
1627 data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1633 * ibmvfc_timeout - Internal command timeout handler
1634 * @evt: struct ibmvfc_event that timed out
1636 * Called when an internally generated command times out
1638 static void ibmvfc_timeout(struct timer_list *t)
1640 struct ibmvfc_event *evt = from_timer(evt, t, timer);
1641 struct ibmvfc_host *vhost = evt->vhost;
1642 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1643 ibmvfc_reset_host(vhost);
1647 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1648 * @evt: event to be sent
1649 * @vhost: ibmvfc host struct
1650 * @timeout: timeout in seconds - 0 means do not time command
1652 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1654 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1655 struct ibmvfc_host *vhost, unsigned long timeout)
1657 __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1658 unsigned long flags;
1661 /* Copy the IU into the transfer area */
1662 *evt->xfer_iu = evt->iu;
1663 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1664 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1665 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1666 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1670 timer_setup(&evt->timer, ibmvfc_timeout, 0);
1673 evt->timer.expires = jiffies + (timeout * HZ);
1674 add_timer(&evt->timer);
1677 spin_lock_irqsave(&evt->queue->l_lock, flags);
1678 list_add_tail(&evt->queue_list, &evt->queue->sent);
1682 if (evt->queue->fmt == IBMVFC_SUB_CRQ_FMT)
1683 rc = ibmvfc_send_sub_crq(vhost,
1684 evt->queue->vios_cookie,
1685 be64_to_cpu(crq_as_u64[0]),
1686 be64_to_cpu(crq_as_u64[1]),
1689 rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1690 be64_to_cpu(crq_as_u64[1]));
1693 list_del(&evt->queue_list);
1694 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1695 del_timer(&evt->timer);
1697 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1698 * Firmware will send a CRQ with a transport event (0xFF) to
1699 * tell this client what has happened to the transport. This
1700 * will be handled in ibmvfc_handle_crq()
1702 if (rc == H_CLOSED) {
1703 if (printk_ratelimit())
1704 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1706 scsi_dma_unmap(evt->cmnd);
1707 ibmvfc_free_event(evt);
1708 return SCSI_MLQUEUE_HOST_BUSY;
1711 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1713 evt->cmnd->result = DID_ERROR << 16;
1714 evt->done = ibmvfc_scsi_eh_done;
1716 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1720 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1721 ibmvfc_trc_start(evt);
1728 * ibmvfc_log_error - Log an error for the failed command if appropriate
1729 * @evt: ibmvfc event to log
1732 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1734 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1735 struct ibmvfc_host *vhost = evt->vhost;
1736 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
1737 struct scsi_cmnd *cmnd = evt->cmnd;
1738 const char *err = unknown_error;
1739 int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1744 logerr = cmd_status[index].log;
1745 err = cmd_status[index].name;
1748 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1751 if (rsp->flags & FCP_RSP_LEN_VALID)
1752 rsp_code = rsp->data.info.rsp_code;
1754 scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
1755 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1756 cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
1757 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1761 * ibmvfc_relogin - Log back into the specified device
1762 * @sdev: scsi device struct
1765 static void ibmvfc_relogin(struct scsi_device *sdev)
1767 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1768 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1769 struct ibmvfc_target *tgt;
1770 unsigned long flags;
1772 spin_lock_irqsave(vhost->host->host_lock, flags);
1773 list_for_each_entry(tgt, &vhost->targets, queue) {
1774 if (rport == tgt->rport) {
1775 ibmvfc_del_tgt(tgt);
1780 ibmvfc_reinit_host(vhost);
1781 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1785 * ibmvfc_scsi_done - Handle responses from commands
1786 * @evt: ibmvfc event to be handled
1788 * Used as a callback when sending scsi cmds.
1790 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1792 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1793 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(evt->vhost, vfc_cmd);
1794 struct scsi_cmnd *cmnd = evt->cmnd;
1796 u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1799 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1800 scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1801 else if (rsp->flags & FCP_RESID_UNDER)
1802 scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1804 scsi_set_resid(cmnd, 0);
1806 if (vfc_cmd->status) {
1807 cmnd->result = ibmvfc_get_err_result(evt->vhost, vfc_cmd);
1809 if (rsp->flags & FCP_RSP_LEN_VALID)
1810 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1811 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1812 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1813 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1814 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1815 if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1816 (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1817 ibmvfc_relogin(cmnd->device);
1819 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1820 cmnd->result = (DID_ERROR << 16);
1822 ibmvfc_log_error(evt);
1825 if (!cmnd->result &&
1826 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1827 cmnd->result = (DID_ERROR << 16);
1829 scsi_dma_unmap(cmnd);
1830 cmnd->scsi_done(cmnd);
1833 ibmvfc_free_event(evt);
1837 * ibmvfc_host_chkready - Check if the host can accept commands
1838 * @vhost: struct ibmvfc host
1841 * 1 if host can accept command / 0 if not
1843 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1847 switch (vhost->state) {
1848 case IBMVFC_LINK_DEAD:
1849 case IBMVFC_HOST_OFFLINE:
1850 result = DID_NO_CONNECT << 16;
1853 case IBMVFC_INITIALIZING:
1855 case IBMVFC_LINK_DOWN:
1856 result = DID_REQUEUE << 16;
1866 static struct ibmvfc_cmd *ibmvfc_init_vfc_cmd(struct ibmvfc_event *evt, struct scsi_device *sdev)
1868 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1869 struct ibmvfc_host *vhost = evt->vhost;
1870 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
1871 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
1872 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
1875 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1876 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
1877 offset = offsetof(struct ibmvfc_cmd, v2.rsp);
1878 vfc_cmd->target_wwpn = cpu_to_be64(rport->port_name);
1880 offset = offsetof(struct ibmvfc_cmd, v1.rsp);
1881 vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offset);
1882 vfc_cmd->resp.len = cpu_to_be32(sizeof(*rsp));
1883 vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1884 vfc_cmd->payload_len = cpu_to_be32(sizeof(*iu));
1885 vfc_cmd->resp_len = cpu_to_be32(sizeof(*rsp));
1886 vfc_cmd->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
1887 vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1888 int_to_scsilun(sdev->lun, &iu->lun);
1894 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1895 * @cmnd: struct scsi_cmnd to be executed
1896 * @done: Callback function to be called when cmnd is completed
1899 * 0 on success / other on failure
1901 static int ibmvfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
1903 struct ibmvfc_host *vhost = shost_priv(shost);
1904 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1905 struct ibmvfc_cmd *vfc_cmd;
1906 struct ibmvfc_fcp_cmd_iu *iu;
1907 struct ibmvfc_event *evt;
1908 u32 tag_and_hwq = blk_mq_unique_tag(cmnd->request);
1909 u16 hwq = blk_mq_unique_tag_to_hwq(tag_and_hwq);
1913 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1914 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1916 cmnd->scsi_done(cmnd);
1920 cmnd->result = (DID_OK << 16);
1921 if (vhost->using_channels) {
1922 scsi_channel = hwq % vhost->scsi_scrqs.active_queues;
1923 evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[scsi_channel]);
1924 evt->hwq = hwq % vhost->scsi_scrqs.active_queues;
1926 evt = ibmvfc_get_event(&vhost->crq);
1928 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1931 vfc_cmd = ibmvfc_init_vfc_cmd(evt, cmnd->device);
1932 iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
1934 iu->xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1935 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
1937 if (cmnd->flags & SCMD_TAGGED) {
1938 vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
1939 iu->pri_task_attr = IBMVFC_SIMPLE_TASK;
1942 vfc_cmd->correlation = cpu_to_be64((u64)evt);
1944 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1945 return ibmvfc_send_event(evt, vhost, 0);
1947 ibmvfc_free_event(evt);
1949 return SCSI_MLQUEUE_HOST_BUSY;
1951 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1952 scmd_printk(KERN_ERR, cmnd,
1953 "Failed to map DMA buffer for command. rc=%d\n", rc);
1955 cmnd->result = DID_ERROR << 16;
1956 cmnd->scsi_done(cmnd);
1961 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1962 * @evt: ibmvfc event struct
1965 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1967 /* copy the response back */
1969 *evt->sync_iu = *evt->xfer_iu;
1971 complete(&evt->comp);
1975 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1976 * @evt: struct ibmvfc_event
1979 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
1981 struct ibmvfc_host *vhost = evt->vhost;
1983 ibmvfc_free_event(evt);
1984 vhost->aborting_passthru = 0;
1985 dev_info(vhost->dev, "Passthru command cancelled\n");
1989 * ibmvfc_bsg_timeout - Handle a BSG timeout
1990 * @job: struct bsg_job that timed out
1993 * 0 on success / other on failure
1995 static int ibmvfc_bsg_timeout(struct bsg_job *job)
1997 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1998 unsigned long port_id = (unsigned long)job->dd_data;
1999 struct ibmvfc_event *evt;
2000 struct ibmvfc_tmf *tmf;
2001 unsigned long flags;
2005 spin_lock_irqsave(vhost->host->host_lock, flags);
2006 if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
2007 __ibmvfc_reset_host(vhost);
2008 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2012 vhost->aborting_passthru = 1;
2013 evt = ibmvfc_get_event(&vhost->crq);
2014 ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
2017 memset(tmf, 0, sizeof(*tmf));
2018 tmf->common.version = cpu_to_be32(1);
2019 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2020 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2021 tmf->scsi_id = cpu_to_be64(port_id);
2022 tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2023 tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
2024 rc = ibmvfc_send_event(evt, vhost, default_timeout);
2027 vhost->aborting_passthru = 0;
2028 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
2031 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
2034 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2041 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
2042 * @vhost: struct ibmvfc_host to send command
2043 * @port_id: port ID to send command
2046 * 0 on success / other on failure
2048 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
2050 struct ibmvfc_port_login *plogi;
2051 struct ibmvfc_target *tgt;
2052 struct ibmvfc_event *evt;
2053 union ibmvfc_iu rsp_iu;
2054 unsigned long flags;
2055 int rc = 0, issue_login = 1;
2058 spin_lock_irqsave(vhost->host->host_lock, flags);
2059 list_for_each_entry(tgt, &vhost->targets, queue) {
2060 if (tgt->scsi_id == port_id) {
2068 if (unlikely((rc = ibmvfc_host_chkready(vhost))))
2071 evt = ibmvfc_get_event(&vhost->crq);
2072 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2073 plogi = &evt->iu.plogi;
2074 memset(plogi, 0, sizeof(*plogi));
2075 plogi->common.version = cpu_to_be32(1);
2076 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
2077 plogi->common.length = cpu_to_be16(sizeof(*plogi));
2078 plogi->scsi_id = cpu_to_be64(port_id);
2079 evt->sync_iu = &rsp_iu;
2080 init_completion(&evt->comp);
2082 rc = ibmvfc_send_event(evt, vhost, default_timeout);
2083 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2088 wait_for_completion(&evt->comp);
2090 if (rsp_iu.plogi.common.status)
2093 spin_lock_irqsave(vhost->host->host_lock, flags);
2094 ibmvfc_free_event(evt);
2096 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2102 * ibmvfc_bsg_request - Handle a BSG request
2103 * @job: struct bsg_job to be executed
2106 * 0 on success / other on failure
2108 static int ibmvfc_bsg_request(struct bsg_job *job)
2110 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2111 struct fc_rport *rport = fc_bsg_to_rport(job);
2112 struct ibmvfc_passthru_mad *mad;
2113 struct ibmvfc_event *evt;
2114 union ibmvfc_iu rsp_iu;
2115 unsigned long flags, port_id = -1;
2116 struct fc_bsg_request *bsg_request = job->request;
2117 struct fc_bsg_reply *bsg_reply = job->reply;
2118 unsigned int code = bsg_request->msgcode;
2119 int rc = 0, req_seg, rsp_seg, issue_login = 0;
2120 u32 fc_flags, rsp_len;
2123 bsg_reply->reply_payload_rcv_len = 0;
2125 port_id = rport->port_id;
2128 case FC_BSG_HST_ELS_NOLOGIN:
2129 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
2130 (bsg_request->rqst_data.h_els.port_id[1] << 8) |
2131 bsg_request->rqst_data.h_els.port_id[2];
2133 case FC_BSG_RPT_ELS:
2134 fc_flags = IBMVFC_FC_ELS;
2138 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
2139 (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
2140 bsg_request->rqst_data.h_ct.port_id[2];
2143 fc_flags = IBMVFC_FC_CT_IU;
2151 if (!mutex_trylock(&vhost->passthru_mutex))
2154 job->dd_data = (void *)port_id;
2155 req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
2156 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2159 mutex_unlock(&vhost->passthru_mutex);
2163 rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
2164 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2167 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2168 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2169 mutex_unlock(&vhost->passthru_mutex);
2173 if (req_seg > 1 || rsp_seg > 1) {
2179 rc = ibmvfc_bsg_plogi(vhost, port_id);
2181 spin_lock_irqsave(vhost->host->host_lock, flags);
2183 if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
2184 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
2185 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2189 evt = ibmvfc_get_event(&vhost->crq);
2190 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2191 mad = &evt->iu.passthru;
2193 memset(mad, 0, sizeof(*mad));
2194 mad->common.version = cpu_to_be32(1);
2195 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
2196 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
2198 mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
2199 offsetof(struct ibmvfc_passthru_mad, iu));
2200 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
2202 mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
2203 mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
2204 mad->iu.flags = cpu_to_be32(fc_flags);
2205 mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2207 mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
2208 mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
2209 mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
2210 mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
2211 mad->iu.scsi_id = cpu_to_be64(port_id);
2212 mad->iu.tag = cpu_to_be64((u64)evt);
2213 rsp_len = be32_to_cpu(mad->iu.rsp.len);
2215 evt->sync_iu = &rsp_iu;
2216 init_completion(&evt->comp);
2217 rc = ibmvfc_send_event(evt, vhost, 0);
2218 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2225 wait_for_completion(&evt->comp);
2227 if (rsp_iu.passthru.common.status)
2230 bsg_reply->reply_payload_rcv_len = rsp_len;
2232 spin_lock_irqsave(vhost->host->host_lock, flags);
2233 ibmvfc_free_event(evt);
2234 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2235 bsg_reply->result = rc;
2236 bsg_job_done(job, bsg_reply->result,
2237 bsg_reply->reply_payload_rcv_len);
2240 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2241 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2242 dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
2243 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2244 mutex_unlock(&vhost->passthru_mutex);
2250 * ibmvfc_reset_device - Reset the device with the specified reset type
2251 * @sdev: scsi device to reset
2253 * @desc: reset type description for log messages
2256 * 0 on success / other on failure
2258 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
2260 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2261 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2262 struct ibmvfc_cmd *tmf;
2263 struct ibmvfc_event *evt = NULL;
2264 union ibmvfc_iu rsp_iu;
2265 struct ibmvfc_fcp_cmd_iu *iu;
2266 struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2267 int rsp_rc = -EBUSY;
2268 unsigned long flags;
2271 spin_lock_irqsave(vhost->host->host_lock, flags);
2272 if (vhost->state == IBMVFC_ACTIVE) {
2273 if (vhost->using_channels)
2274 evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[0]);
2276 evt = ibmvfc_get_event(&vhost->crq);
2278 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2279 tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2280 iu = ibmvfc_get_fcp_iu(vhost, tmf);
2282 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2283 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2284 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2285 iu->tmf_flags = type;
2286 evt->sync_iu = &rsp_iu;
2288 init_completion(&evt->comp);
2289 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2291 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2294 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2299 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2300 wait_for_completion(&evt->comp);
2302 if (rsp_iu.cmd.status)
2303 rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2306 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2307 rsp_code = fc_rsp->data.info.rsp_code;
2309 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2310 "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2311 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2312 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2313 fc_rsp->scsi_status);
2316 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2318 spin_lock_irqsave(vhost->host->host_lock, flags);
2319 ibmvfc_free_event(evt);
2320 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2325 * ibmvfc_match_rport - Match function for specified remote port
2326 * @evt: ibmvfc event struct
2327 * @device: device to match (rport)
2330 * 1 if event matches rport / 0 if event does not match rport
2332 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2334 struct fc_rport *cmd_rport;
2337 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2338 if (cmd_rport == rport)
2345 * ibmvfc_match_target - Match function for specified target
2346 * @evt: ibmvfc event struct
2347 * @device: device to match (starget)
2350 * 1 if event matches starget / 0 if event does not match starget
2352 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2354 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2360 * ibmvfc_match_lun - Match function for specified LUN
2361 * @evt: ibmvfc event struct
2362 * @device: device to match (sdev)
2365 * 1 if event matches sdev / 0 if event does not match sdev
2367 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2369 if (evt->cmnd && evt->cmnd->device == device)
2375 * ibmvfc_wait_for_ops - Wait for ops to complete
2376 * @vhost: ibmvfc host struct
2377 * @device: device to match (starget or sdev)
2378 * @match: match function
2383 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2384 int (*match) (struct ibmvfc_event *, void *))
2386 struct ibmvfc_event *evt;
2387 DECLARE_COMPLETION_ONSTACK(comp);
2389 unsigned long flags;
2390 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2395 spin_lock_irqsave(&vhost->crq.l_lock, flags);
2396 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2397 if (match(evt, device)) {
2398 evt->eh_comp = ∁
2402 spin_unlock_irqrestore(&vhost->crq.l_lock, flags);
2405 timeout = wait_for_completion_timeout(&comp, timeout);
2409 spin_lock_irqsave(&vhost->crq.l_lock, flags);
2410 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2411 if (match(evt, device)) {
2412 evt->eh_comp = NULL;
2416 spin_unlock_irqrestore(&vhost->crq.l_lock, flags);
2418 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2420 return wait ? FAILED : SUCCESS;
2429 static struct ibmvfc_event *ibmvfc_init_tmf(struct ibmvfc_queue *queue,
2430 struct scsi_device *sdev,
2433 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2434 struct scsi_target *starget = scsi_target(sdev);
2435 struct fc_rport *rport = starget_to_rport(starget);
2436 struct ibmvfc_event *evt;
2437 struct ibmvfc_tmf *tmf;
2439 evt = ibmvfc_get_event(queue);
2440 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2443 memset(tmf, 0, sizeof(*tmf));
2444 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
2445 tmf->common.version = cpu_to_be32(2);
2446 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2448 tmf->common.version = cpu_to_be32(1);
2450 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2451 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2452 tmf->scsi_id = cpu_to_be64(rport->port_id);
2453 int_to_scsilun(sdev->lun, &tmf->lun);
2454 if (!ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPRESS_ABTS))
2455 type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2456 if (vhost->state == IBMVFC_ACTIVE)
2457 tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2459 tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2460 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2461 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2463 init_completion(&evt->comp);
2468 static int ibmvfc_cancel_all_mq(struct scsi_device *sdev, int type)
2470 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2471 struct ibmvfc_event *evt, *found_evt, *temp;
2472 struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
2473 unsigned long flags;
2480 spin_lock_irqsave(vhost->host->host_lock, flags);
2481 num_hwq = vhost->scsi_scrqs.active_queues;
2482 for (i = 0; i < num_hwq; i++) {
2483 spin_lock(queues[i].q_lock);
2484 spin_lock(&queues[i].l_lock);
2486 list_for_each_entry(evt, &queues[i].sent, queue_list) {
2487 if (evt->cmnd && evt->cmnd->device == sdev) {
2492 spin_unlock(&queues[i].l_lock);
2494 if (found_evt && vhost->logged_in) {
2495 evt = ibmvfc_init_tmf(&queues[i], sdev, type);
2496 evt->sync_iu = &queues[i].cancel_rsp;
2497 ibmvfc_send_event(evt, vhost, default_timeout);
2498 list_add_tail(&evt->cancel, &cancelq);
2501 spin_unlock(queues[i].q_lock);
2503 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2505 if (list_empty(&cancelq)) {
2506 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2507 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2511 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2513 list_for_each_entry_safe(evt, temp, &cancelq, cancel) {
2514 wait_for_completion(&evt->comp);
2515 status = be16_to_cpu(evt->queue->cancel_rsp.mad_common.status);
2516 list_del(&evt->cancel);
2517 ibmvfc_free_event(evt);
2519 if (status != IBMVFC_MAD_SUCCESS) {
2520 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2522 case IBMVFC_MAD_DRIVER_FAILED:
2523 case IBMVFC_MAD_CRQ_ERROR:
2524 /* Host adapter most likely going through reset, return success to
2525 * the caller will wait for the command being cancelled to get returned
2538 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2543 static int ibmvfc_cancel_all_sq(struct scsi_device *sdev, int type)
2545 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2546 struct ibmvfc_event *evt, *found_evt;
2547 union ibmvfc_iu rsp;
2548 int rsp_rc = -EBUSY;
2549 unsigned long flags;
2554 spin_lock_irqsave(vhost->host->host_lock, flags);
2555 spin_lock(&vhost->crq.l_lock);
2556 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2557 if (evt->cmnd && evt->cmnd->device == sdev) {
2562 spin_unlock(&vhost->crq.l_lock);
2565 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2566 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2567 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2571 if (vhost->logged_in) {
2572 evt = ibmvfc_init_tmf(&vhost->crq, sdev, type);
2573 evt->sync_iu = &rsp;
2574 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2577 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2580 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2581 /* If failure is received, the host adapter is most likely going
2582 through reset, return success so the caller will wait for the command
2583 being cancelled to get returned */
2587 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2589 wait_for_completion(&evt->comp);
2590 status = be16_to_cpu(rsp.mad_common.status);
2591 spin_lock_irqsave(vhost->host->host_lock, flags);
2592 ibmvfc_free_event(evt);
2593 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2595 if (status != IBMVFC_MAD_SUCCESS) {
2596 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2598 case IBMVFC_MAD_DRIVER_FAILED:
2599 case IBMVFC_MAD_CRQ_ERROR:
2600 /* Host adapter most likely going through reset, return success to
2601 the caller will wait for the command being cancelled to get returned */
2608 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2613 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2614 * @sdev: scsi device to cancel commands
2615 * @type: type of error recovery being performed
2617 * This sends a cancel to the VIOS for the specified device. This does
2618 * NOT send any abort to the actual device. That must be done separately.
2621 * 0 on success / other on failure
2623 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2625 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2627 if (vhost->mq_enabled && vhost->using_channels)
2628 return ibmvfc_cancel_all_mq(sdev, type);
2630 return ibmvfc_cancel_all_sq(sdev, type);
2634 * ibmvfc_match_key - Match function for specified cancel key
2635 * @evt: ibmvfc event struct
2636 * @key: cancel key to match
2639 * 1 if event matches key / 0 if event does not match key
2641 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2643 unsigned long cancel_key = (unsigned long)key;
2645 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2646 be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2652 * ibmvfc_match_evt - Match function for specified event
2653 * @evt: ibmvfc event struct
2654 * @match: event to match
2657 * 1 if event matches key / 0 if event does not match key
2659 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2667 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2668 * @sdev: scsi device to abort commands
2670 * This sends an Abort Task Set to the VIOS for the specified device. This does
2671 * NOT send any cancel to the VIOS. That must be done separately.
2674 * 0 on success / other on failure
2676 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2678 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2679 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2680 struct ibmvfc_cmd *tmf;
2681 struct ibmvfc_event *evt, *found_evt;
2682 union ibmvfc_iu rsp_iu;
2683 struct ibmvfc_fcp_cmd_iu *iu;
2684 struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2685 int rc, rsp_rc = -EBUSY;
2686 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2690 spin_lock_irqsave(vhost->host->host_lock, flags);
2691 spin_lock(&vhost->crq.l_lock);
2692 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2693 if (evt->cmnd && evt->cmnd->device == sdev) {
2698 spin_unlock(&vhost->crq.l_lock);
2701 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2702 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2703 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2707 if (vhost->state == IBMVFC_ACTIVE) {
2708 evt = ibmvfc_get_event(&vhost->crq);
2709 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2710 tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2711 iu = ibmvfc_get_fcp_iu(vhost, tmf);
2713 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2714 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2715 iu->tmf_flags = IBMVFC_ABORT_TASK_SET;
2716 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2717 evt->sync_iu = &rsp_iu;
2719 tmf->correlation = cpu_to_be64((u64)evt);
2721 init_completion(&evt->comp);
2722 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2725 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2728 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2732 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2733 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2736 rc = ibmvfc_cancel_all(sdev, 0);
2738 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2744 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2745 ibmvfc_reset_host(vhost);
2747 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2752 rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2753 if (rc != SUCCESS) {
2754 spin_lock_irqsave(vhost->host->host_lock, flags);
2755 ibmvfc_hard_reset_host(vhost);
2756 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2764 if (rsp_iu.cmd.status)
2765 rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2768 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2769 rsp_code = fc_rsp->data.info.rsp_code;
2771 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2772 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2773 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2774 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2775 fc_rsp->scsi_status);
2778 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2781 spin_lock_irqsave(vhost->host->host_lock, flags);
2782 ibmvfc_free_event(evt);
2783 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2788 * ibmvfc_eh_abort_handler - Abort a command
2789 * @cmd: scsi command to abort
2792 * SUCCESS / FAST_IO_FAIL / FAILED
2794 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2796 struct scsi_device *sdev = cmd->device;
2797 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2798 int cancel_rc, block_rc;
2802 block_rc = fc_block_scsi_eh(cmd);
2803 ibmvfc_wait_while_resetting(vhost);
2804 if (block_rc != FAST_IO_FAIL) {
2805 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2806 ibmvfc_abort_task_set(sdev);
2808 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2811 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2813 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2821 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2822 * @cmd: scsi command struct
2825 * SUCCESS / FAST_IO_FAIL / FAILED
2827 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2829 struct scsi_device *sdev = cmd->device;
2830 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2831 int cancel_rc, block_rc, reset_rc = 0;
2835 block_rc = fc_block_scsi_eh(cmd);
2836 ibmvfc_wait_while_resetting(vhost);
2837 if (block_rc != FAST_IO_FAIL) {
2838 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2839 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2841 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2843 if (!cancel_rc && !reset_rc)
2844 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2846 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2854 * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2855 * @sdev: scsi device struct
2856 * @data: return code
2859 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2861 unsigned long *rc = data;
2862 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2866 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2867 * @sdev: scsi device struct
2868 * @data: return code
2871 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2873 unsigned long *rc = data;
2874 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2878 * ibmvfc_eh_target_reset_handler - Reset the target
2879 * @cmd: scsi command struct
2882 * SUCCESS / FAST_IO_FAIL / FAILED
2884 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2886 struct scsi_device *sdev = cmd->device;
2887 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2888 struct scsi_target *starget = scsi_target(sdev);
2892 unsigned long cancel_rc = 0;
2895 block_rc = fc_block_scsi_eh(cmd);
2896 ibmvfc_wait_while_resetting(vhost);
2897 if (block_rc != FAST_IO_FAIL) {
2898 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2899 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2901 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2903 if (!cancel_rc && !reset_rc)
2904 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2906 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2914 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2915 * @cmd: struct scsi_cmnd having problems
2918 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2921 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2923 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2924 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2926 return rc ? FAILED : SUCCESS;
2930 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2931 * @rport: rport struct
2936 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2938 struct Scsi_Host *shost = rport_to_shost(rport);
2939 struct ibmvfc_host *vhost = shost_priv(shost);
2940 struct fc_rport *dev_rport;
2941 struct scsi_device *sdev;
2942 struct ibmvfc_target *tgt;
2943 unsigned long rc, flags;
2947 shost_for_each_device(sdev, shost) {
2948 dev_rport = starget_to_rport(scsi_target(sdev));
2949 if (dev_rport != rport)
2951 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2954 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
2957 ibmvfc_issue_fc_host_lip(shost);
2959 spin_lock_irqsave(shost->host_lock, flags);
2961 list_for_each_entry(tgt, &vhost->targets, queue) {
2962 if (tgt->scsi_id == rport->port_id) {
2968 if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
2970 * If we get here, that means we previously attempted to send
2971 * an implicit logout to the target but it failed, most likely
2972 * due to I/O being pending, so we need to send it again
2974 ibmvfc_del_tgt(tgt);
2975 ibmvfc_reinit_host(vhost);
2978 spin_unlock_irqrestore(shost->host_lock, flags);
2982 static const struct ibmvfc_async_desc ae_desc [] = {
2983 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2984 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2985 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2986 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2987 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2988 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
2989 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
2990 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
2991 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
2992 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
2993 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
2994 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
2995 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
2998 static const struct ibmvfc_async_desc unknown_ae = {
2999 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
3003 * ibmvfc_get_ae_desc - Get text description for async event
3007 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
3011 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
3012 if (ae_desc[i].ae == ae)
3018 static const struct {
3019 enum ibmvfc_ae_link_state state;
3022 { IBMVFC_AE_LS_LINK_UP, " link up" },
3023 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
3024 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
3025 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
3029 * ibmvfc_get_link_state - Get text description for link state
3030 * @state: link state
3033 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
3037 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
3038 if (link_desc[i].state == state)
3039 return link_desc[i].desc;
3045 * ibmvfc_handle_async - Handle an async event from the adapter
3046 * @crq: crq to process
3047 * @vhost: ibmvfc host struct
3050 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
3051 struct ibmvfc_host *vhost)
3053 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
3054 struct ibmvfc_target *tgt;
3056 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
3057 " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
3058 be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
3059 ibmvfc_get_link_state(crq->link_state));
3061 switch (be64_to_cpu(crq->event)) {
3062 case IBMVFC_AE_RESUME:
3063 switch (crq->link_state) {
3064 case IBMVFC_AE_LS_LINK_DOWN:
3065 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3067 case IBMVFC_AE_LS_LINK_DEAD:
3068 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3070 case IBMVFC_AE_LS_LINK_UP:
3071 case IBMVFC_AE_LS_LINK_BOUNCED:
3073 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3074 vhost->delay_init = 1;
3075 __ibmvfc_reset_host(vhost);
3080 case IBMVFC_AE_LINK_UP:
3081 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3082 vhost->delay_init = 1;
3083 __ibmvfc_reset_host(vhost);
3085 case IBMVFC_AE_SCN_FABRIC:
3086 case IBMVFC_AE_SCN_DOMAIN:
3087 vhost->events_to_log |= IBMVFC_AE_RSCN;
3088 if (vhost->state < IBMVFC_HALTED) {
3089 vhost->delay_init = 1;
3090 __ibmvfc_reset_host(vhost);
3093 case IBMVFC_AE_SCN_NPORT:
3094 case IBMVFC_AE_SCN_GROUP:
3095 vhost->events_to_log |= IBMVFC_AE_RSCN;
3096 ibmvfc_reinit_host(vhost);
3098 case IBMVFC_AE_ELS_LOGO:
3099 case IBMVFC_AE_ELS_PRLO:
3100 case IBMVFC_AE_ELS_PLOGI:
3101 list_for_each_entry(tgt, &vhost->targets, queue) {
3102 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
3104 if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
3106 if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
3108 if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
3110 if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
3112 if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
3113 ibmvfc_del_tgt(tgt);
3114 ibmvfc_reinit_host(vhost);
3118 case IBMVFC_AE_LINK_DOWN:
3119 case IBMVFC_AE_ADAPTER_FAILED:
3120 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3122 case IBMVFC_AE_LINK_DEAD:
3123 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3125 case IBMVFC_AE_HALT:
3126 ibmvfc_link_down(vhost, IBMVFC_HALTED);
3129 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
3135 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
3136 * @crq: Command/Response queue
3137 * @vhost: ibmvfc host struct
3140 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3141 struct list_head *evt_doneq)
3144 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3146 switch (crq->valid) {
3147 case IBMVFC_CRQ_INIT_RSP:
3148 switch (crq->format) {
3149 case IBMVFC_CRQ_INIT:
3150 dev_info(vhost->dev, "Partner initialized\n");
3151 /* Send back a response */
3152 rc = ibmvfc_send_crq_init_complete(vhost);
3154 ibmvfc_init_host(vhost);
3156 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
3158 case IBMVFC_CRQ_INIT_COMPLETE:
3159 dev_info(vhost->dev, "Partner initialization complete\n");
3160 ibmvfc_init_host(vhost);
3163 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
3166 case IBMVFC_CRQ_XPORT_EVENT:
3167 vhost->state = IBMVFC_NO_CRQ;
3168 vhost->logged_in = 0;
3169 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3170 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
3171 /* We need to re-setup the interpartition connection */
3172 dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
3173 vhost->client_migrated = 1;
3174 ibmvfc_purge_requests(vhost, DID_REQUEUE);
3175 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3176 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
3177 } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
3178 dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
3179 ibmvfc_purge_requests(vhost, DID_ERROR);
3180 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3181 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
3183 dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
3186 case IBMVFC_CRQ_CMD_RSP:
3189 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
3193 if (crq->format == IBMVFC_ASYNC_EVENT)
3196 /* The only kind of payload CRQs we should get are responses to
3197 * things we send. Make sure this response is to something we
3200 if (unlikely(!ibmvfc_valid_event(&vhost->crq.evt_pool, evt))) {
3201 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3206 if (unlikely(atomic_read(&evt->free))) {
3207 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3212 spin_lock(&evt->queue->l_lock);
3213 list_move_tail(&evt->queue_list, evt_doneq);
3214 spin_unlock(&evt->queue->l_lock);
3218 * ibmvfc_scan_finished - Check if the device scan is done.
3219 * @shost: scsi host struct
3220 * @time: current elapsed time
3223 * 0 if scan is not done / 1 if scan is done
3225 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3227 unsigned long flags;
3228 struct ibmvfc_host *vhost = shost_priv(shost);
3231 spin_lock_irqsave(shost->host_lock, flags);
3232 if (time >= (init_timeout * HZ)) {
3233 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
3234 "continuing initialization\n", init_timeout);
3238 if (vhost->scan_complete)
3240 spin_unlock_irqrestore(shost->host_lock, flags);
3245 * ibmvfc_slave_alloc - Setup the device's task set value
3246 * @sdev: struct scsi_device device to configure
3248 * Set the device's task set value so that error handling works as
3252 * 0 on success / -ENXIO if device does not exist
3254 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
3256 struct Scsi_Host *shost = sdev->host;
3257 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3258 struct ibmvfc_host *vhost = shost_priv(shost);
3259 unsigned long flags = 0;
3261 if (!rport || fc_remote_port_chkready(rport))
3264 spin_lock_irqsave(shost->host_lock, flags);
3265 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
3266 spin_unlock_irqrestore(shost->host_lock, flags);
3271 * ibmvfc_target_alloc - Setup the target's task set value
3272 * @starget: struct scsi_target
3274 * Set the target's task set value so that error handling works as
3278 * 0 on success / -ENXIO if device does not exist
3280 static int ibmvfc_target_alloc(struct scsi_target *starget)
3282 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3283 struct ibmvfc_host *vhost = shost_priv(shost);
3284 unsigned long flags = 0;
3286 spin_lock_irqsave(shost->host_lock, flags);
3287 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
3288 spin_unlock_irqrestore(shost->host_lock, flags);
3293 * ibmvfc_slave_configure - Configure the device
3294 * @sdev: struct scsi_device device to configure
3296 * Enable allow_restart for a device if it is a disk. Adjust the
3297 * queue_depth here also.
3302 static int ibmvfc_slave_configure(struct scsi_device *sdev)
3304 struct Scsi_Host *shost = sdev->host;
3305 unsigned long flags = 0;
3307 spin_lock_irqsave(shost->host_lock, flags);
3308 if (sdev->type == TYPE_DISK) {
3309 sdev->allow_restart = 1;
3310 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
3312 spin_unlock_irqrestore(shost->host_lock, flags);
3317 * ibmvfc_change_queue_depth - Change the device's queue depth
3318 * @sdev: scsi device struct
3319 * @qdepth: depth to set
3320 * @reason: calling context
3325 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
3327 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
3328 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
3330 return scsi_change_queue_depth(sdev, qdepth);
3333 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
3334 struct device_attribute *attr, char *buf)
3336 struct Scsi_Host *shost = class_to_shost(dev);
3337 struct ibmvfc_host *vhost = shost_priv(shost);
3339 return snprintf(buf, PAGE_SIZE, "%s\n",
3340 vhost->login_buf->resp.partition_name);
3343 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
3344 struct device_attribute *attr, char *buf)
3346 struct Scsi_Host *shost = class_to_shost(dev);
3347 struct ibmvfc_host *vhost = shost_priv(shost);
3349 return snprintf(buf, PAGE_SIZE, "%s\n",
3350 vhost->login_buf->resp.device_name);
3353 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3354 struct device_attribute *attr, char *buf)
3356 struct Scsi_Host *shost = class_to_shost(dev);
3357 struct ibmvfc_host *vhost = shost_priv(shost);
3359 return snprintf(buf, PAGE_SIZE, "%s\n",
3360 vhost->login_buf->resp.port_loc_code);
3363 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3364 struct device_attribute *attr, char *buf)
3366 struct Scsi_Host *shost = class_to_shost(dev);
3367 struct ibmvfc_host *vhost = shost_priv(shost);
3369 return snprintf(buf, PAGE_SIZE, "%s\n",
3370 vhost->login_buf->resp.drc_name);
3373 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3374 struct device_attribute *attr, char *buf)
3376 struct Scsi_Host *shost = class_to_shost(dev);
3377 struct ibmvfc_host *vhost = shost_priv(shost);
3378 return snprintf(buf, PAGE_SIZE, "%d\n", be32_to_cpu(vhost->login_buf->resp.version));
3381 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3382 struct device_attribute *attr, char *buf)
3384 struct Scsi_Host *shost = class_to_shost(dev);
3385 struct ibmvfc_host *vhost = shost_priv(shost);
3386 return snprintf(buf, PAGE_SIZE, "%llx\n", be64_to_cpu(vhost->login_buf->resp.capabilities));
3390 * ibmvfc_show_log_level - Show the adapter's error logging level
3391 * @dev: class device struct
3395 * number of bytes printed to buffer
3397 static ssize_t ibmvfc_show_log_level(struct device *dev,
3398 struct device_attribute *attr, char *buf)
3400 struct Scsi_Host *shost = class_to_shost(dev);
3401 struct ibmvfc_host *vhost = shost_priv(shost);
3402 unsigned long flags = 0;
3405 spin_lock_irqsave(shost->host_lock, flags);
3406 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
3407 spin_unlock_irqrestore(shost->host_lock, flags);
3412 * ibmvfc_store_log_level - Change the adapter's error logging level
3413 * @dev: class device struct
3417 * number of bytes printed to buffer
3419 static ssize_t ibmvfc_store_log_level(struct device *dev,
3420 struct device_attribute *attr,
3421 const char *buf, size_t count)
3423 struct Scsi_Host *shost = class_to_shost(dev);
3424 struct ibmvfc_host *vhost = shost_priv(shost);
3425 unsigned long flags = 0;
3427 spin_lock_irqsave(shost->host_lock, flags);
3428 vhost->log_level = simple_strtoul(buf, NULL, 10);
3429 spin_unlock_irqrestore(shost->host_lock, flags);
3433 static ssize_t ibmvfc_show_scsi_channels(struct device *dev,
3434 struct device_attribute *attr, char *buf)
3436 struct Scsi_Host *shost = class_to_shost(dev);
3437 struct ibmvfc_host *vhost = shost_priv(shost);
3438 unsigned long flags = 0;
3441 spin_lock_irqsave(shost->host_lock, flags);
3442 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->client_scsi_channels);
3443 spin_unlock_irqrestore(shost->host_lock, flags);
3447 static ssize_t ibmvfc_store_scsi_channels(struct device *dev,
3448 struct device_attribute *attr,
3449 const char *buf, size_t count)
3451 struct Scsi_Host *shost = class_to_shost(dev);
3452 struct ibmvfc_host *vhost = shost_priv(shost);
3453 unsigned long flags = 0;
3454 unsigned int channels;
3456 spin_lock_irqsave(shost->host_lock, flags);
3457 channels = simple_strtoul(buf, NULL, 10);
3458 vhost->client_scsi_channels = min(channels, nr_scsi_hw_queues);
3459 ibmvfc_hard_reset_host(vhost);
3460 spin_unlock_irqrestore(shost->host_lock, flags);
3464 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3465 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3466 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3467 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3468 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3469 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3470 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3471 ibmvfc_show_log_level, ibmvfc_store_log_level);
3472 static DEVICE_ATTR(nr_scsi_channels, S_IRUGO | S_IWUSR,
3473 ibmvfc_show_scsi_channels, ibmvfc_store_scsi_channels);
3475 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3477 * ibmvfc_read_trace - Dump the adapter trace
3478 * @filp: open sysfs file
3479 * @kobj: kobject struct
3480 * @bin_attr: bin_attribute struct
3483 * @count: buffer size
3486 * number of bytes printed to buffer
3488 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3489 struct bin_attribute *bin_attr,
3490 char *buf, loff_t off, size_t count)
3492 struct device *dev = container_of(kobj, struct device, kobj);
3493 struct Scsi_Host *shost = class_to_shost(dev);
3494 struct ibmvfc_host *vhost = shost_priv(shost);
3495 unsigned long flags = 0;
3496 int size = IBMVFC_TRACE_SIZE;
3497 char *src = (char *)vhost->trace;
3501 if (off + count > size) {
3506 spin_lock_irqsave(shost->host_lock, flags);
3507 memcpy(buf, &src[off], count);
3508 spin_unlock_irqrestore(shost->host_lock, flags);
3512 static struct bin_attribute ibmvfc_trace_attr = {
3518 .read = ibmvfc_read_trace,
3522 static struct device_attribute *ibmvfc_attrs[] = {
3523 &dev_attr_partition_name,
3524 &dev_attr_device_name,
3525 &dev_attr_port_loc_code,
3527 &dev_attr_npiv_version,
3528 &dev_attr_capabilities,
3529 &dev_attr_log_level,
3530 &dev_attr_nr_scsi_channels,
3534 static struct scsi_host_template driver_template = {
3535 .module = THIS_MODULE,
3536 .name = "IBM POWER Virtual FC Adapter",
3537 .proc_name = IBMVFC_NAME,
3538 .queuecommand = ibmvfc_queuecommand,
3539 .eh_timed_out = fc_eh_timed_out,
3540 .eh_abort_handler = ibmvfc_eh_abort_handler,
3541 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3542 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3543 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3544 .slave_alloc = ibmvfc_slave_alloc,
3545 .slave_configure = ibmvfc_slave_configure,
3546 .target_alloc = ibmvfc_target_alloc,
3547 .scan_finished = ibmvfc_scan_finished,
3548 .change_queue_depth = ibmvfc_change_queue_depth,
3550 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3552 .sg_tablesize = SG_ALL,
3553 .max_sectors = IBMVFC_MAX_SECTORS,
3554 .shost_attrs = ibmvfc_attrs,
3555 .track_queue_depth = 1,
3560 * ibmvfc_next_async_crq - Returns the next entry in async queue
3561 * @vhost: ibmvfc host struct
3564 * Pointer to next entry in queue / NULL if empty
3566 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3568 struct ibmvfc_queue *async_crq = &vhost->async_crq;
3569 struct ibmvfc_async_crq *crq;
3571 crq = &async_crq->msgs.async[async_crq->cur];
3572 if (crq->valid & 0x80) {
3573 if (++async_crq->cur == async_crq->size)
3583 * ibmvfc_next_crq - Returns the next entry in message queue
3584 * @vhost: ibmvfc host struct
3587 * Pointer to next entry in queue / NULL if empty
3589 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3591 struct ibmvfc_queue *queue = &vhost->crq;
3592 struct ibmvfc_crq *crq;
3594 crq = &queue->msgs.crq[queue->cur];
3595 if (crq->valid & 0x80) {
3596 if (++queue->cur == queue->size)
3606 * ibmvfc_interrupt - Interrupt handler
3607 * @irq: number of irq to handle, not used
3608 * @dev_instance: ibmvfc_host that received interrupt
3613 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3615 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3616 unsigned long flags;
3618 spin_lock_irqsave(vhost->host->host_lock, flags);
3619 vio_disable_interrupts(to_vio_dev(vhost->dev));
3620 tasklet_schedule(&vhost->tasklet);
3621 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3626 * ibmvfc_tasklet - Interrupt handler tasklet
3627 * @data: ibmvfc host struct
3632 static void ibmvfc_tasklet(void *data)
3634 struct ibmvfc_host *vhost = data;
3635 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3636 struct ibmvfc_crq *crq;
3637 struct ibmvfc_async_crq *async;
3638 struct ibmvfc_event *evt, *temp;
3639 unsigned long flags;
3641 LIST_HEAD(evt_doneq);
3643 spin_lock_irqsave(vhost->host->host_lock, flags);
3644 spin_lock(vhost->crq.q_lock);
3646 /* Pull all the valid messages off the async CRQ */
3647 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3648 ibmvfc_handle_async(async, vhost);
3653 /* Pull all the valid messages off the CRQ */
3654 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3655 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3660 vio_enable_interrupts(vdev);
3661 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3662 vio_disable_interrupts(vdev);
3663 ibmvfc_handle_async(async, vhost);
3666 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3667 vio_disable_interrupts(vdev);
3668 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3675 spin_unlock(vhost->crq.q_lock);
3676 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3678 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3679 del_timer(&evt->timer);
3680 list_del(&evt->queue_list);
3681 ibmvfc_trc_end(evt);
3686 static int ibmvfc_toggle_scrq_irq(struct ibmvfc_queue *scrq, int enable)
3688 struct device *dev = scrq->vhost->dev;
3689 struct vio_dev *vdev = to_vio_dev(dev);
3691 int irq_action = H_ENABLE_VIO_INTERRUPT;
3694 irq_action = H_DISABLE_VIO_INTERRUPT;
3696 rc = plpar_hcall_norets(H_VIOCTL, vdev->unit_address, irq_action,
3697 scrq->hw_irq, 0, 0);
3700 dev_err(dev, "Couldn't %s sub-crq[%lu] irq. rc=%ld\n",
3701 enable ? "enable" : "disable", scrq->hwq_id, rc);
3706 static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3707 struct list_head *evt_doneq)
3709 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3711 switch (crq->valid) {
3712 case IBMVFC_CRQ_CMD_RSP:
3714 case IBMVFC_CRQ_XPORT_EVENT:
3717 dev_err(vhost->dev, "Got and invalid message type 0x%02x\n", crq->valid);
3721 /* The only kind of payload CRQs we should get are responses to
3722 * things we send. Make sure this response is to something we
3725 if (unlikely(!ibmvfc_valid_event(&evt->queue->evt_pool, evt))) {
3726 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3731 if (unlikely(atomic_read(&evt->free))) {
3732 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3737 spin_lock(&evt->queue->l_lock);
3738 list_move_tail(&evt->queue_list, evt_doneq);
3739 spin_unlock(&evt->queue->l_lock);
3742 static struct ibmvfc_crq *ibmvfc_next_scrq(struct ibmvfc_queue *scrq)
3744 struct ibmvfc_crq *crq;
3746 crq = &scrq->msgs.scrq[scrq->cur].crq;
3747 if (crq->valid & 0x80) {
3748 if (++scrq->cur == scrq->size)
3757 static void ibmvfc_drain_sub_crq(struct ibmvfc_queue *scrq)
3759 struct ibmvfc_crq *crq;
3760 struct ibmvfc_event *evt, *temp;
3761 unsigned long flags;
3763 LIST_HEAD(evt_doneq);
3765 spin_lock_irqsave(scrq->q_lock, flags);
3767 while ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3768 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3773 ibmvfc_toggle_scrq_irq(scrq, 1);
3774 if ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3775 ibmvfc_toggle_scrq_irq(scrq, 0);
3776 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3782 spin_unlock_irqrestore(scrq->q_lock, flags);
3784 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3785 del_timer(&evt->timer);
3786 list_del(&evt->queue_list);
3787 ibmvfc_trc_end(evt);
3792 static irqreturn_t ibmvfc_interrupt_scsi(int irq, void *scrq_instance)
3794 struct ibmvfc_queue *scrq = (struct ibmvfc_queue *)scrq_instance;
3796 ibmvfc_toggle_scrq_irq(scrq, 0);
3797 ibmvfc_drain_sub_crq(scrq);
3803 * ibmvfc_init_tgt - Set the next init job step for the target
3804 * @tgt: ibmvfc target struct
3805 * @job_step: job step to perform
3808 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3809 void (*job_step) (struct ibmvfc_target *))
3811 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3812 tgt->job_step = job_step;
3813 wake_up(&tgt->vhost->work_wait_q);
3817 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3818 * @tgt: ibmvfc target struct
3819 * @job_step: initialization job step
3821 * Returns: 1 if step will be retried / 0 if not
3824 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3825 void (*job_step) (struct ibmvfc_target *))
3827 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3828 ibmvfc_del_tgt(tgt);
3829 wake_up(&tgt->vhost->work_wait_q);
3832 ibmvfc_init_tgt(tgt, job_step);
3836 /* Defined in FC-LS */
3837 static const struct {
3854 * ibmvfc_get_prli_rsp - Find PRLI response index
3855 * @flags: PRLI response flags
3858 static int ibmvfc_get_prli_rsp(u16 flags)
3861 int code = (flags & 0x0f00) >> 8;
3863 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3864 if (prli_rsp[i].code == code)
3871 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3872 * @evt: ibmvfc event struct
3875 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3877 struct ibmvfc_target *tgt = evt->tgt;
3878 struct ibmvfc_host *vhost = evt->vhost;
3879 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3880 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3881 u32 status = be16_to_cpu(rsp->common.status);
3882 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3884 vhost->discovery_threads--;
3885 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3887 case IBMVFC_MAD_SUCCESS:
3888 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3889 parms->type, parms->flags, parms->service_parms);
3891 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3892 index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3893 if (prli_rsp[index].logged_in) {
3894 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3895 tgt->need_login = 0;
3897 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3898 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3899 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3900 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3903 ibmvfc_del_tgt(tgt);
3904 } else if (prli_rsp[index].retry)
3905 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3907 ibmvfc_del_tgt(tgt);
3909 ibmvfc_del_tgt(tgt);
3911 case IBMVFC_MAD_DRIVER_FAILED:
3913 case IBMVFC_MAD_CRQ_ERROR:
3914 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3916 case IBMVFC_MAD_FAILED:
3918 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3919 be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3920 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3921 else if (tgt->logo_rcvd)
3922 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3923 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3924 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3926 ibmvfc_del_tgt(tgt);
3928 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3929 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3930 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
3934 kref_put(&tgt->kref, ibmvfc_release_tgt);
3935 ibmvfc_free_event(evt);
3936 wake_up(&vhost->work_wait_q);
3940 * ibmvfc_tgt_send_prli - Send a process login
3941 * @tgt: ibmvfc target struct
3944 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3946 struct ibmvfc_process_login *prli;
3947 struct ibmvfc_host *vhost = tgt->vhost;
3948 struct ibmvfc_event *evt;
3950 if (vhost->discovery_threads >= disc_threads)
3953 kref_get(&tgt->kref);
3954 evt = ibmvfc_get_event(&vhost->crq);
3955 vhost->discovery_threads++;
3956 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3958 prli = &evt->iu.prli;
3959 memset(prli, 0, sizeof(*prli));
3960 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
3961 prli->common.version = cpu_to_be32(2);
3962 prli->target_wwpn = cpu_to_be64(tgt->wwpn);
3964 prli->common.version = cpu_to_be32(1);
3966 prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
3967 prli->common.length = cpu_to_be16(sizeof(*prli));
3968 prli->scsi_id = cpu_to_be64(tgt->scsi_id);
3970 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3971 prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
3972 prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
3973 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
3976 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
3978 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3979 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3980 vhost->discovery_threads--;
3981 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3982 kref_put(&tgt->kref, ibmvfc_release_tgt);
3984 tgt_dbg(tgt, "Sent process login\n");
3988 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3989 * @evt: ibmvfc event struct
3992 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3994 struct ibmvfc_target *tgt = evt->tgt;
3995 struct ibmvfc_host *vhost = evt->vhost;
3996 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3997 u32 status = be16_to_cpu(rsp->common.status);
3998 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4000 vhost->discovery_threads--;
4001 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4003 case IBMVFC_MAD_SUCCESS:
4004 tgt_dbg(tgt, "Port Login succeeded\n");
4005 if (tgt->ids.port_name &&
4006 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
4008 tgt_dbg(tgt, "Port re-init required\n");
4011 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4012 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4013 tgt->ids.port_id = tgt->scsi_id;
4014 memcpy(&tgt->service_parms, &rsp->service_parms,
4015 sizeof(tgt->service_parms));
4016 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4017 sizeof(tgt->service_parms_change));
4018 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4020 case IBMVFC_MAD_DRIVER_FAILED:
4022 case IBMVFC_MAD_CRQ_ERROR:
4023 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4025 case IBMVFC_MAD_FAILED:
4027 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4028 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4030 ibmvfc_del_tgt(tgt);
4032 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4033 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4034 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4035 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4036 ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
4040 kref_put(&tgt->kref, ibmvfc_release_tgt);
4041 ibmvfc_free_event(evt);
4042 wake_up(&vhost->work_wait_q);
4046 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
4047 * @tgt: ibmvfc target struct
4050 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
4052 struct ibmvfc_port_login *plogi;
4053 struct ibmvfc_host *vhost = tgt->vhost;
4054 struct ibmvfc_event *evt;
4056 if (vhost->discovery_threads >= disc_threads)
4059 kref_get(&tgt->kref);
4061 evt = ibmvfc_get_event(&vhost->crq);
4062 vhost->discovery_threads++;
4063 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4064 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
4066 plogi = &evt->iu.plogi;
4067 memset(plogi, 0, sizeof(*plogi));
4068 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4069 plogi->common.version = cpu_to_be32(2);
4070 plogi->target_wwpn = cpu_to_be64(tgt->wwpn);
4072 plogi->common.version = cpu_to_be32(1);
4074 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
4075 plogi->common.length = cpu_to_be16(sizeof(*plogi));
4076 plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
4078 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4079 vhost->discovery_threads--;
4080 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4081 kref_put(&tgt->kref, ibmvfc_release_tgt);
4083 tgt_dbg(tgt, "Sent port login\n");
4087 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
4088 * @evt: ibmvfc event struct
4091 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
4093 struct ibmvfc_target *tgt = evt->tgt;
4094 struct ibmvfc_host *vhost = evt->vhost;
4095 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
4096 u32 status = be16_to_cpu(rsp->common.status);
4098 vhost->discovery_threads--;
4099 ibmvfc_free_event(evt);
4100 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4103 case IBMVFC_MAD_SUCCESS:
4104 tgt_dbg(tgt, "Implicit Logout succeeded\n");
4106 case IBMVFC_MAD_DRIVER_FAILED:
4107 kref_put(&tgt->kref, ibmvfc_release_tgt);
4108 wake_up(&vhost->work_wait_q);
4110 case IBMVFC_MAD_FAILED:
4112 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
4116 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
4117 kref_put(&tgt->kref, ibmvfc_release_tgt);
4118 wake_up(&vhost->work_wait_q);
4122 * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
4123 * @tgt: ibmvfc target struct
4126 * Allocated and initialized ibmvfc_event struct
4128 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
4129 void (*done) (struct ibmvfc_event *))
4131 struct ibmvfc_implicit_logout *mad;
4132 struct ibmvfc_host *vhost = tgt->vhost;
4133 struct ibmvfc_event *evt;
4135 kref_get(&tgt->kref);
4136 evt = ibmvfc_get_event(&vhost->crq);
4137 ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
4139 mad = &evt->iu.implicit_logout;
4140 memset(mad, 0, sizeof(*mad));
4141 mad->common.version = cpu_to_be32(1);
4142 mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
4143 mad->common.length = cpu_to_be16(sizeof(*mad));
4144 mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4149 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
4150 * @tgt: ibmvfc target struct
4153 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
4155 struct ibmvfc_host *vhost = tgt->vhost;
4156 struct ibmvfc_event *evt;
4158 if (vhost->discovery_threads >= disc_threads)
4161 vhost->discovery_threads++;
4162 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4163 ibmvfc_tgt_implicit_logout_done);
4165 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4166 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4167 vhost->discovery_threads--;
4168 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4169 kref_put(&tgt->kref, ibmvfc_release_tgt);
4171 tgt_dbg(tgt, "Sent Implicit Logout\n");
4175 * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
4176 * @evt: ibmvfc event struct
4179 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
4181 struct ibmvfc_target *tgt = evt->tgt;
4182 struct ibmvfc_host *vhost = evt->vhost;
4183 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4184 u32 status = be16_to_cpu(mad->common.status);
4186 vhost->discovery_threads--;
4187 ibmvfc_free_event(evt);
4190 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
4191 * driver in which case we need to free up all the targets. If we are
4192 * not unloading, we will still go through a hard reset to get out of
4193 * offline state, so there is no need to track the old targets in that
4196 if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
4197 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4199 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
4201 tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
4202 kref_put(&tgt->kref, ibmvfc_release_tgt);
4203 wake_up(&vhost->work_wait_q);
4207 * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
4208 * @tgt: ibmvfc target struct
4211 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
4213 struct ibmvfc_host *vhost = tgt->vhost;
4214 struct ibmvfc_event *evt;
4216 if (!vhost->logged_in) {
4217 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4221 if (vhost->discovery_threads >= disc_threads)
4224 vhost->discovery_threads++;
4225 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4226 ibmvfc_tgt_implicit_logout_and_del_done);
4228 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
4229 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4230 vhost->discovery_threads--;
4231 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4232 kref_put(&tgt->kref, ibmvfc_release_tgt);
4234 tgt_dbg(tgt, "Sent Implicit Logout\n");
4238 * ibmvfc_tgt_move_login_done - Completion handler for Move Login
4239 * @evt: ibmvfc event struct
4242 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
4244 struct ibmvfc_target *tgt = evt->tgt;
4245 struct ibmvfc_host *vhost = evt->vhost;
4246 struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
4247 u32 status = be16_to_cpu(rsp->common.status);
4248 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4250 vhost->discovery_threads--;
4251 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4253 case IBMVFC_MAD_SUCCESS:
4254 tgt_dbg(tgt, "Move Login succeeded for old scsi_id: %llX\n", tgt->old_scsi_id);
4255 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4256 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4257 tgt->ids.port_id = tgt->scsi_id;
4258 memcpy(&tgt->service_parms, &rsp->service_parms,
4259 sizeof(tgt->service_parms));
4260 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4261 sizeof(tgt->service_parms_change));
4262 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4264 case IBMVFC_MAD_DRIVER_FAILED:
4266 case IBMVFC_MAD_CRQ_ERROR:
4267 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4269 case IBMVFC_MAD_FAILED:
4271 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4274 "Move Login failed: old scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
4275 tgt->old_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
4280 kref_put(&tgt->kref, ibmvfc_release_tgt);
4281 ibmvfc_free_event(evt);
4282 wake_up(&vhost->work_wait_q);
4287 * ibmvfc_tgt_move_login - Initiate a move login for specified target
4288 * @tgt: ibmvfc target struct
4291 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
4293 struct ibmvfc_host *vhost = tgt->vhost;
4294 struct ibmvfc_move_login *move;
4295 struct ibmvfc_event *evt;
4297 if (vhost->discovery_threads >= disc_threads)
4300 kref_get(&tgt->kref);
4301 evt = ibmvfc_get_event(&vhost->crq);
4302 vhost->discovery_threads++;
4303 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4304 ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
4306 move = &evt->iu.move_login;
4307 memset(move, 0, sizeof(*move));
4308 move->common.version = cpu_to_be32(1);
4309 move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
4310 move->common.length = cpu_to_be16(sizeof(*move));
4312 move->old_scsi_id = cpu_to_be64(tgt->old_scsi_id);
4313 move->new_scsi_id = cpu_to_be64(tgt->scsi_id);
4314 move->wwpn = cpu_to_be64(tgt->wwpn);
4315 move->node_name = cpu_to_be64(tgt->ids.node_name);
4317 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4318 vhost->discovery_threads--;
4319 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4320 kref_put(&tgt->kref, ibmvfc_release_tgt);
4322 tgt_dbg(tgt, "Sent Move Login for old scsi_id: %llX\n", tgt->old_scsi_id);
4326 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
4327 * @mad: ibmvfc passthru mad struct
4328 * @tgt: ibmvfc target struct
4331 * 1 if PLOGI needed / 0 if PLOGI not needed
4333 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
4334 struct ibmvfc_target *tgt)
4336 if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
4338 if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
4340 if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
4346 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
4347 * @evt: ibmvfc event struct
4350 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
4352 struct ibmvfc_target *tgt = evt->tgt;
4353 struct ibmvfc_host *vhost = evt->vhost;
4354 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4355 u32 status = be16_to_cpu(mad->common.status);
4356 u8 fc_reason, fc_explain;
4358 vhost->discovery_threads--;
4359 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4360 del_timer(&tgt->timer);
4363 case IBMVFC_MAD_SUCCESS:
4364 tgt_dbg(tgt, "ADISC succeeded\n");
4365 if (ibmvfc_adisc_needs_plogi(mad, tgt))
4366 ibmvfc_del_tgt(tgt);
4368 case IBMVFC_MAD_DRIVER_FAILED:
4370 case IBMVFC_MAD_FAILED:
4372 ibmvfc_del_tgt(tgt);
4373 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
4374 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
4375 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4376 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
4377 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
4378 ibmvfc_get_fc_type(fc_reason), fc_reason,
4379 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
4383 kref_put(&tgt->kref, ibmvfc_release_tgt);
4384 ibmvfc_free_event(evt);
4385 wake_up(&vhost->work_wait_q);
4389 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
4390 * @evt: ibmvfc event struct
4393 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
4395 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
4397 memset(mad, 0, sizeof(*mad));
4398 mad->common.version = cpu_to_be32(1);
4399 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
4400 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
4401 mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4402 offsetof(struct ibmvfc_passthru_mad, iu));
4403 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
4404 mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4405 mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
4406 mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4407 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4408 offsetof(struct ibmvfc_passthru_fc_iu, payload));
4409 mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4410 mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4411 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4412 offsetof(struct ibmvfc_passthru_fc_iu, response));
4413 mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
4417 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
4418 * @evt: ibmvfc event struct
4420 * Just cleanup this event struct. Everything else is handled by
4421 * the ADISC completion handler. If the ADISC never actually comes
4422 * back, we still have the timer running on the ADISC event struct
4423 * which will fire and cause the CRQ to get reset.
4426 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
4428 struct ibmvfc_host *vhost = evt->vhost;
4429 struct ibmvfc_target *tgt = evt->tgt;
4431 tgt_dbg(tgt, "ADISC cancel complete\n");
4432 vhost->abort_threads--;
4433 ibmvfc_free_event(evt);
4434 kref_put(&tgt->kref, ibmvfc_release_tgt);
4435 wake_up(&vhost->work_wait_q);
4439 * ibmvfc_adisc_timeout - Handle an ADISC timeout
4440 * @tgt: ibmvfc target struct
4442 * If an ADISC times out, send a cancel. If the cancel times
4443 * out, reset the CRQ. When the ADISC comes back as cancelled,
4444 * log back into the target.
4446 static void ibmvfc_adisc_timeout(struct timer_list *t)
4448 struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
4449 struct ibmvfc_host *vhost = tgt->vhost;
4450 struct ibmvfc_event *evt;
4451 struct ibmvfc_tmf *tmf;
4452 unsigned long flags;
4455 tgt_dbg(tgt, "ADISC timeout\n");
4456 spin_lock_irqsave(vhost->host->host_lock, flags);
4457 if (vhost->abort_threads >= disc_threads ||
4458 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
4459 vhost->state != IBMVFC_INITIALIZING ||
4460 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
4461 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4465 vhost->abort_threads++;
4466 kref_get(&tgt->kref);
4467 evt = ibmvfc_get_event(&vhost->crq);
4468 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
4472 memset(tmf, 0, sizeof(*tmf));
4473 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4474 tmf->common.version = cpu_to_be32(2);
4475 tmf->target_wwpn = cpu_to_be64(tgt->wwpn);
4477 tmf->common.version = cpu_to_be32(1);
4479 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
4480 tmf->common.length = cpu_to_be16(sizeof(*tmf));
4481 tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
4482 tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
4484 rc = ibmvfc_send_event(evt, vhost, default_timeout);
4487 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
4488 vhost->abort_threads--;
4489 kref_put(&tgt->kref, ibmvfc_release_tgt);
4490 __ibmvfc_reset_host(vhost);
4492 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
4493 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4497 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
4498 * @tgt: ibmvfc target struct
4500 * When sending an ADISC we end up with two timers running. The
4501 * first timer is the timer in the ibmvfc target struct. If this
4502 * fires, we send a cancel to the target. The second timer is the
4503 * timer on the ibmvfc event for the ADISC, which is longer. If that
4504 * fires, it means the ADISC timed out and our attempt to cancel it
4505 * also failed, so we need to reset the CRQ.
4507 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
4509 struct ibmvfc_passthru_mad *mad;
4510 struct ibmvfc_host *vhost = tgt->vhost;
4511 struct ibmvfc_event *evt;
4513 if (vhost->discovery_threads >= disc_threads)
4516 kref_get(&tgt->kref);
4517 evt = ibmvfc_get_event(&vhost->crq);
4518 vhost->discovery_threads++;
4519 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
4522 ibmvfc_init_passthru(evt);
4523 mad = &evt->iu.passthru;
4524 mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4525 mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4526 mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4528 mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4529 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4530 sizeof(vhost->login_buf->resp.port_name));
4531 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4532 sizeof(vhost->login_buf->resp.node_name));
4533 mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4535 if (timer_pending(&tgt->timer))
4536 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4538 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4539 add_timer(&tgt->timer);
4542 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4543 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4544 vhost->discovery_threads--;
4545 del_timer(&tgt->timer);
4546 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4547 kref_put(&tgt->kref, ibmvfc_release_tgt);
4549 tgt_dbg(tgt, "Sent ADISC\n");
4553 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4554 * @evt: ibmvfc event struct
4557 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4559 struct ibmvfc_target *tgt = evt->tgt;
4560 struct ibmvfc_host *vhost = evt->vhost;
4561 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4562 u32 status = be16_to_cpu(rsp->common.status);
4563 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4565 vhost->discovery_threads--;
4566 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4568 case IBMVFC_MAD_SUCCESS:
4569 tgt_dbg(tgt, "Query Target succeeded\n");
4570 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4571 ibmvfc_del_tgt(tgt);
4573 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4575 case IBMVFC_MAD_DRIVER_FAILED:
4577 case IBMVFC_MAD_CRQ_ERROR:
4578 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4580 case IBMVFC_MAD_FAILED:
4582 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4583 be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4584 be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4585 ibmvfc_del_tgt(tgt);
4586 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4587 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4589 ibmvfc_del_tgt(tgt);
4591 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4592 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4593 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4594 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4595 ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4600 kref_put(&tgt->kref, ibmvfc_release_tgt);
4601 ibmvfc_free_event(evt);
4602 wake_up(&vhost->work_wait_q);
4606 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4607 * @tgt: ibmvfc target struct
4610 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4612 struct ibmvfc_query_tgt *query_tgt;
4613 struct ibmvfc_host *vhost = tgt->vhost;
4614 struct ibmvfc_event *evt;
4616 if (vhost->discovery_threads >= disc_threads)
4619 kref_get(&tgt->kref);
4620 evt = ibmvfc_get_event(&vhost->crq);
4621 vhost->discovery_threads++;
4623 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4624 query_tgt = &evt->iu.query_tgt;
4625 memset(query_tgt, 0, sizeof(*query_tgt));
4626 query_tgt->common.version = cpu_to_be32(1);
4627 query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4628 query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4629 query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4631 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4632 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4633 vhost->discovery_threads--;
4634 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4635 kref_put(&tgt->kref, ibmvfc_release_tgt);
4637 tgt_dbg(tgt, "Sent Query Target\n");
4641 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4642 * @vhost: ibmvfc host struct
4643 * @scsi_id: SCSI ID to allocate target for
4646 * 0 on success / other on failure
4648 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4649 struct ibmvfc_discover_targets_entry *target)
4651 struct ibmvfc_target *stgt = NULL;
4652 struct ibmvfc_target *wtgt = NULL;
4653 struct ibmvfc_target *tgt;
4654 unsigned long flags;
4655 u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4656 u64 wwpn = be64_to_cpu(target->wwpn);
4658 /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4659 spin_lock_irqsave(vhost->host->host_lock, flags);
4660 list_for_each_entry(tgt, &vhost->targets, queue) {
4661 if (tgt->wwpn == wwpn) {
4667 list_for_each_entry(tgt, &vhost->targets, queue) {
4668 if (tgt->scsi_id == scsi_id) {
4674 if (wtgt && !stgt) {
4676 * A WWPN target has moved and we still are tracking the old
4677 * SCSI ID. The only way we should be able to get here is if
4678 * we attempted to send an implicit logout for the old SCSI ID
4679 * and it failed for some reason, such as there being I/O
4680 * pending to the target. In this case, we will have already
4681 * deleted the rport from the FC transport so we do a move
4682 * login, which works even with I/O pending, as it will cancel
4683 * any active commands.
4685 if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4687 * Do a move login here. The old target is no longer
4688 * known to the transport layer We don't use the
4689 * normal ibmvfc_set_tgt_action to set this, as we
4690 * don't normally want to allow this state change.
4692 wtgt->old_scsi_id = wtgt->scsi_id;
4693 wtgt->scsi_id = scsi_id;
4694 wtgt->action = IBMVFC_TGT_ACTION_INIT;
4695 ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4698 tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4699 wtgt->action, wtgt->rport);
4702 if (tgt->need_login)
4703 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4706 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4708 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4709 memset(tgt, 0, sizeof(*tgt));
4710 tgt->scsi_id = scsi_id;
4713 tgt->need_login = 1;
4714 timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4715 kref_init(&tgt->kref);
4716 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4717 spin_lock_irqsave(vhost->host->host_lock, flags);
4718 tgt->cancel_key = vhost->task_set++;
4719 list_add_tail(&tgt->queue, &vhost->targets);
4722 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4727 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4728 * @vhost: ibmvfc host struct
4731 * 0 on success / other on failure
4733 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4737 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4738 rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
4744 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4745 * @evt: ibmvfc event struct
4748 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4750 struct ibmvfc_host *vhost = evt->vhost;
4751 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4752 u32 mad_status = be16_to_cpu(rsp->common.status);
4753 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4755 switch (mad_status) {
4756 case IBMVFC_MAD_SUCCESS:
4757 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4758 vhost->num_targets = be32_to_cpu(rsp->num_written);
4759 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4761 case IBMVFC_MAD_FAILED:
4762 level += ibmvfc_retry_host_init(vhost);
4763 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4764 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4765 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4767 case IBMVFC_MAD_DRIVER_FAILED:
4770 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4771 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4775 ibmvfc_free_event(evt);
4776 wake_up(&vhost->work_wait_q);
4780 * ibmvfc_discover_targets - Send Discover Targets MAD
4781 * @vhost: ibmvfc host struct
4784 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4786 struct ibmvfc_discover_targets *mad;
4787 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4789 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4790 mad = &evt->iu.discover_targets;
4791 memset(mad, 0, sizeof(*mad));
4792 mad->common.version = cpu_to_be32(1);
4793 mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4794 mad->common.length = cpu_to_be16(sizeof(*mad));
4795 mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4796 mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4797 mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4798 mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
4799 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4801 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4802 ibmvfc_dbg(vhost, "Sent discover targets\n");
4804 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4807 static void ibmvfc_channel_setup_done(struct ibmvfc_event *evt)
4809 struct ibmvfc_host *vhost = evt->vhost;
4810 struct ibmvfc_channel_setup *setup = vhost->channel_setup_buf;
4811 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
4812 u32 mad_status = be16_to_cpu(evt->xfer_iu->channel_setup.common.status);
4813 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4814 int flags, active_queues, i;
4816 ibmvfc_free_event(evt);
4818 switch (mad_status) {
4819 case IBMVFC_MAD_SUCCESS:
4820 ibmvfc_dbg(vhost, "Channel Setup succeeded\n");
4821 flags = be32_to_cpu(setup->flags);
4822 vhost->do_enquiry = 0;
4823 active_queues = be32_to_cpu(setup->num_scsi_subq_channels);
4824 scrqs->active_queues = active_queues;
4826 if (flags & IBMVFC_CHANNELS_CANCELED) {
4827 ibmvfc_dbg(vhost, "Channels Canceled\n");
4828 vhost->using_channels = 0;
4831 vhost->using_channels = 1;
4832 for (i = 0; i < active_queues; i++)
4833 scrqs->scrqs[i].vios_cookie =
4834 be64_to_cpu(setup->channel_handles[i]);
4836 ibmvfc_dbg(vhost, "Using %u channels\n",
4837 vhost->scsi_scrqs.active_queues);
4840 case IBMVFC_MAD_FAILED:
4841 level += ibmvfc_retry_host_init(vhost);
4842 ibmvfc_log(vhost, level, "Channel Setup failed\n");
4844 case IBMVFC_MAD_DRIVER_FAILED:
4847 dev_err(vhost->dev, "Invalid Channel Setup response: 0x%x\n",
4849 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4853 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4854 wake_up(&vhost->work_wait_q);
4857 static void ibmvfc_channel_setup(struct ibmvfc_host *vhost)
4859 struct ibmvfc_channel_setup_mad *mad;
4860 struct ibmvfc_channel_setup *setup_buf = vhost->channel_setup_buf;
4861 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4862 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
4863 unsigned int num_channels =
4864 min(vhost->client_scsi_channels, vhost->max_vios_scsi_channels);
4867 memset(setup_buf, 0, sizeof(*setup_buf));
4868 if (num_channels == 0)
4869 setup_buf->flags = cpu_to_be32(IBMVFC_CANCEL_CHANNELS);
4871 setup_buf->num_scsi_subq_channels = cpu_to_be32(num_channels);
4872 for (i = 0; i < num_channels; i++)
4873 setup_buf->channel_handles[i] = cpu_to_be64(scrqs->scrqs[i].cookie);
4876 ibmvfc_init_event(evt, ibmvfc_channel_setup_done, IBMVFC_MAD_FORMAT);
4877 mad = &evt->iu.channel_setup;
4878 memset(mad, 0, sizeof(*mad));
4879 mad->common.version = cpu_to_be32(1);
4880 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_SETUP);
4881 mad->common.length = cpu_to_be16(sizeof(*mad));
4882 mad->buffer.va = cpu_to_be64(vhost->channel_setup_dma);
4883 mad->buffer.len = cpu_to_be32(sizeof(*vhost->channel_setup_buf));
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 channel setup\n");
4890 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
4893 static void ibmvfc_channel_enquiry_done(struct ibmvfc_event *evt)
4895 struct ibmvfc_host *vhost = evt->vhost;
4896 struct ibmvfc_channel_enquiry *rsp = &evt->xfer_iu->channel_enquiry;
4897 u32 mad_status = be16_to_cpu(rsp->common.status);
4898 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4900 switch (mad_status) {
4901 case IBMVFC_MAD_SUCCESS:
4902 ibmvfc_dbg(vhost, "Channel Enquiry succeeded\n");
4903 vhost->max_vios_scsi_channels = be32_to_cpu(rsp->num_scsi_subq_channels);
4904 ibmvfc_free_event(evt);
4906 case IBMVFC_MAD_FAILED:
4907 level += ibmvfc_retry_host_init(vhost);
4908 ibmvfc_log(vhost, level, "Channel Enquiry failed\n");
4910 case IBMVFC_MAD_DRIVER_FAILED:
4911 ibmvfc_free_event(evt);
4914 dev_err(vhost->dev, "Invalid Channel Enquiry response: 0x%x\n",
4916 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4917 ibmvfc_free_event(evt);
4921 ibmvfc_channel_setup(vhost);
4924 static void ibmvfc_channel_enquiry(struct ibmvfc_host *vhost)
4926 struct ibmvfc_channel_enquiry *mad;
4927 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4929 ibmvfc_init_event(evt, ibmvfc_channel_enquiry_done, IBMVFC_MAD_FORMAT);
4930 mad = &evt->iu.channel_enquiry;
4931 memset(mad, 0, sizeof(*mad));
4932 mad->common.version = cpu_to_be32(1);
4933 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_ENQUIRY);
4934 mad->common.length = cpu_to_be16(sizeof(*mad));
4936 if (mig_channels_only)
4937 mad->flags |= cpu_to_be32(IBMVFC_NO_CHANNELS_TO_CRQ_SUPPORT);
4938 if (mig_no_less_channels)
4939 mad->flags |= cpu_to_be32(IBMVFC_NO_N_TO_M_CHANNELS_SUPPORT);
4941 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4943 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4944 ibmvfc_dbg(vhost, "Send channel enquiry\n");
4946 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4950 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4951 * @evt: ibmvfc event struct
4954 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4956 struct ibmvfc_host *vhost = evt->vhost;
4957 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
4958 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
4959 unsigned int npiv_max_sectors;
4960 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4962 switch (mad_status) {
4963 case IBMVFC_MAD_SUCCESS:
4964 ibmvfc_free_event(evt);
4966 case IBMVFC_MAD_FAILED:
4967 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4968 level += ibmvfc_retry_host_init(vhost);
4970 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4971 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
4972 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4973 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4974 ibmvfc_free_event(evt);
4976 case IBMVFC_MAD_CRQ_ERROR:
4977 ibmvfc_retry_host_init(vhost);
4979 case IBMVFC_MAD_DRIVER_FAILED:
4980 ibmvfc_free_event(evt);
4983 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4984 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4985 ibmvfc_free_event(evt);
4989 vhost->client_migrated = 0;
4991 if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
4992 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4994 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4995 wake_up(&vhost->work_wait_q);
4999 if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
5000 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
5002 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5003 wake_up(&vhost->work_wait_q);
5007 vhost->logged_in = 1;
5008 npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
5009 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
5010 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
5011 rsp->drc_name, npiv_max_sectors);
5013 fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
5014 fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
5015 fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
5016 fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
5017 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
5018 fc_host_supported_classes(vhost->host) = 0;
5019 if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
5020 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
5021 if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
5022 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
5023 if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
5024 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
5025 fc_host_maxframe_size(vhost->host) =
5026 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
5028 vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
5029 vhost->host->max_sectors = npiv_max_sectors;
5031 if (ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPORT_CHANNELS) && vhost->do_enquiry) {
5032 ibmvfc_channel_enquiry(vhost);
5034 vhost->do_enquiry = 0;
5035 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5036 wake_up(&vhost->work_wait_q);
5041 * ibmvfc_npiv_login - Sends NPIV login
5042 * @vhost: ibmvfc host struct
5045 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
5047 struct ibmvfc_npiv_login_mad *mad;
5048 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
5050 ibmvfc_gather_partition_info(vhost);
5051 ibmvfc_set_login_info(vhost);
5052 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
5054 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
5055 mad = &evt->iu.npiv_login;
5056 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
5057 mad->common.version = cpu_to_be32(1);
5058 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
5059 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
5060 mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
5061 mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
5063 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5065 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5066 ibmvfc_dbg(vhost, "Sent NPIV login\n");
5068 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5072 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
5073 * @vhost: ibmvfc host struct
5076 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
5078 struct ibmvfc_host *vhost = evt->vhost;
5079 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
5081 ibmvfc_free_event(evt);
5083 switch (mad_status) {
5084 case IBMVFC_MAD_SUCCESS:
5085 if (list_empty(&vhost->crq.sent) &&
5086 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
5087 ibmvfc_init_host(vhost);
5091 case IBMVFC_MAD_FAILED:
5092 case IBMVFC_MAD_NOT_SUPPORTED:
5093 case IBMVFC_MAD_CRQ_ERROR:
5094 case IBMVFC_MAD_DRIVER_FAILED:
5096 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
5100 ibmvfc_hard_reset_host(vhost);
5104 * ibmvfc_npiv_logout - Issue an NPIV Logout
5105 * @vhost: ibmvfc host struct
5108 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
5110 struct ibmvfc_npiv_logout_mad *mad;
5111 struct ibmvfc_event *evt;
5113 evt = ibmvfc_get_event(&vhost->crq);
5114 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
5116 mad = &evt->iu.npiv_logout;
5117 memset(mad, 0, sizeof(*mad));
5118 mad->common.version = cpu_to_be32(1);
5119 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
5120 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
5122 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
5124 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5125 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
5127 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5131 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
5132 * @vhost: ibmvfc host struct
5135 * 1 if work to do / 0 if not
5137 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
5139 struct ibmvfc_target *tgt;
5141 list_for_each_entry(tgt, &vhost->targets, queue) {
5142 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
5143 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5151 * ibmvfc_dev_logo_to_do - Is there target logout work to do?
5152 * @vhost: ibmvfc host struct
5155 * 1 if work to do / 0 if not
5157 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
5159 struct ibmvfc_target *tgt;
5161 list_for_each_entry(tgt, &vhost->targets, queue) {
5162 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
5163 tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5170 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
5171 * @vhost: ibmvfc host struct
5174 * 1 if work to do / 0 if not
5176 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5178 struct ibmvfc_target *tgt;
5180 if (kthread_should_stop())
5182 switch (vhost->action) {
5183 case IBMVFC_HOST_ACTION_NONE:
5184 case IBMVFC_HOST_ACTION_INIT_WAIT:
5185 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5187 case IBMVFC_HOST_ACTION_TGT_INIT:
5188 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5189 if (vhost->discovery_threads == disc_threads)
5191 list_for_each_entry(tgt, &vhost->targets, queue)
5192 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
5194 list_for_each_entry(tgt, &vhost->targets, queue)
5195 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5198 case IBMVFC_HOST_ACTION_TGT_DEL:
5199 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5200 if (vhost->discovery_threads == disc_threads)
5202 list_for_each_entry(tgt, &vhost->targets, queue)
5203 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
5205 list_for_each_entry(tgt, &vhost->targets, queue)
5206 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5209 case IBMVFC_HOST_ACTION_LOGO:
5210 case IBMVFC_HOST_ACTION_INIT:
5211 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5212 case IBMVFC_HOST_ACTION_QUERY:
5213 case IBMVFC_HOST_ACTION_RESET:
5214 case IBMVFC_HOST_ACTION_REENABLE:
5223 * ibmvfc_work_to_do - Is there task level work to do?
5224 * @vhost: ibmvfc host struct
5227 * 1 if work to do / 0 if not
5229 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5231 unsigned long flags;
5234 spin_lock_irqsave(vhost->host->host_lock, flags);
5235 rc = __ibmvfc_work_to_do(vhost);
5236 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5241 * ibmvfc_log_ae - Log async events if necessary
5242 * @vhost: ibmvfc host struct
5243 * @events: events to log
5246 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
5248 if (events & IBMVFC_AE_RSCN)
5249 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
5250 if ((events & IBMVFC_AE_LINKDOWN) &&
5251 vhost->state >= IBMVFC_HALTED)
5252 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
5253 if ((events & IBMVFC_AE_LINKUP) &&
5254 vhost->state == IBMVFC_INITIALIZING)
5255 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
5259 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
5260 * @tgt: ibmvfc target struct
5263 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
5265 struct ibmvfc_host *vhost = tgt->vhost;
5266 struct fc_rport *rport;
5267 unsigned long flags;
5269 tgt_dbg(tgt, "Adding rport\n");
5270 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
5271 spin_lock_irqsave(vhost->host->host_lock, flags);
5273 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5274 tgt_dbg(tgt, "Deleting rport\n");
5275 list_del(&tgt->queue);
5276 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5277 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5278 fc_remote_port_delete(rport);
5279 del_timer_sync(&tgt->timer);
5280 kref_put(&tgt->kref, ibmvfc_release_tgt);
5282 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5283 tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
5284 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5286 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5287 fc_remote_port_delete(rport);
5289 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
5290 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5295 tgt_dbg(tgt, "rport add succeeded\n");
5297 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
5298 rport->supported_classes = 0;
5299 tgt->target_id = rport->scsi_target_id;
5300 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
5301 rport->supported_classes |= FC_COS_CLASS1;
5302 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
5303 rport->supported_classes |= FC_COS_CLASS2;
5304 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
5305 rport->supported_classes |= FC_COS_CLASS3;
5307 blk_queue_max_segments(rport->rqst_q, 1);
5309 tgt_dbg(tgt, "rport add failed\n");
5310 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5314 * ibmvfc_do_work - Do task level work
5315 * @vhost: ibmvfc host struct
5318 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
5320 struct ibmvfc_target *tgt;
5321 unsigned long flags;
5322 struct fc_rport *rport;
5326 ibmvfc_log_ae(vhost, vhost->events_to_log);
5327 spin_lock_irqsave(vhost->host->host_lock, flags);
5328 vhost->events_to_log = 0;
5329 switch (vhost->action) {
5330 case IBMVFC_HOST_ACTION_NONE:
5331 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5332 case IBMVFC_HOST_ACTION_INIT_WAIT:
5334 case IBMVFC_HOST_ACTION_RESET:
5335 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5336 list_splice_init(&vhost->purge, &purge);
5337 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5338 ibmvfc_complete_purge(&purge);
5339 rc = ibmvfc_reset_crq(vhost);
5340 spin_lock_irqsave(vhost->host->host_lock, flags);
5342 vio_enable_interrupts(to_vio_dev(vhost->dev));
5343 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
5344 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
5345 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5346 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
5349 case IBMVFC_HOST_ACTION_REENABLE:
5350 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5351 list_splice_init(&vhost->purge, &purge);
5352 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5353 ibmvfc_complete_purge(&purge);
5354 rc = ibmvfc_reenable_crq_queue(vhost);
5355 spin_lock_irqsave(vhost->host->host_lock, flags);
5356 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
5357 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5358 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
5361 case IBMVFC_HOST_ACTION_LOGO:
5362 vhost->job_step(vhost);
5364 case IBMVFC_HOST_ACTION_INIT:
5365 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
5366 if (vhost->delay_init) {
5367 vhost->delay_init = 0;
5368 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5372 vhost->job_step(vhost);
5374 case IBMVFC_HOST_ACTION_QUERY:
5375 list_for_each_entry(tgt, &vhost->targets, queue)
5376 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
5377 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
5379 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5380 list_for_each_entry(tgt, &vhost->targets, queue) {
5381 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5387 if (!ibmvfc_dev_init_to_do(vhost))
5388 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
5390 case IBMVFC_HOST_ACTION_TGT_DEL:
5391 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5392 list_for_each_entry(tgt, &vhost->targets, queue) {
5393 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
5399 if (ibmvfc_dev_logo_to_do(vhost)) {
5400 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5404 list_for_each_entry(tgt, &vhost->targets, queue) {
5405 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5406 tgt_dbg(tgt, "Deleting rport\n");
5409 list_del(&tgt->queue);
5410 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5411 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5413 fc_remote_port_delete(rport);
5414 del_timer_sync(&tgt->timer);
5415 kref_put(&tgt->kref, ibmvfc_release_tgt);
5417 } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5418 tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
5421 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5422 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5424 fc_remote_port_delete(rport);
5429 if (vhost->state == IBMVFC_INITIALIZING) {
5430 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
5431 if (vhost->reinit) {
5433 scsi_block_requests(vhost->host);
5434 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5435 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5437 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
5438 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5439 wake_up(&vhost->init_wait_q);
5440 schedule_work(&vhost->rport_add_work_q);
5441 vhost->init_retries = 0;
5442 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5443 scsi_unblock_requests(vhost->host);
5448 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
5449 vhost->job_step = ibmvfc_discover_targets;
5452 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5453 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5454 scsi_unblock_requests(vhost->host);
5455 wake_up(&vhost->init_wait_q);
5459 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5460 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
5461 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5462 ibmvfc_alloc_targets(vhost);
5463 spin_lock_irqsave(vhost->host->host_lock, flags);
5465 case IBMVFC_HOST_ACTION_TGT_INIT:
5466 list_for_each_entry(tgt, &vhost->targets, queue) {
5467 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5473 if (!ibmvfc_dev_init_to_do(vhost))
5474 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
5480 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5484 * ibmvfc_work - Do task level work
5485 * @data: ibmvfc host struct
5490 static int ibmvfc_work(void *data)
5492 struct ibmvfc_host *vhost = data;
5495 set_user_nice(current, MIN_NICE);
5498 rc = wait_event_interruptible(vhost->work_wait_q,
5499 ibmvfc_work_to_do(vhost));
5503 if (kthread_should_stop())
5506 ibmvfc_do_work(vhost);
5509 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
5514 * ibmvfc_alloc_queue - Allocate queue
5515 * @vhost: ibmvfc host struct
5516 * @queue: ibmvfc queue to allocate
5517 * @fmt: queue format to allocate
5520 * 0 on success / non-zero on failure
5522 static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
5523 struct ibmvfc_queue *queue,
5524 enum ibmvfc_msg_fmt fmt)
5526 struct device *dev = vhost->dev;
5528 unsigned int pool_size = 0;
5531 spin_lock_init(&queue->_lock);
5532 queue->q_lock = &queue->_lock;
5535 case IBMVFC_CRQ_FMT:
5536 fmt_size = sizeof(*queue->msgs.crq);
5537 pool_size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
5539 case IBMVFC_ASYNC_FMT:
5540 fmt_size = sizeof(*queue->msgs.async);
5542 case IBMVFC_SUB_CRQ_FMT:
5543 fmt_size = sizeof(*queue->msgs.scrq);
5544 /* We need one extra event for Cancel Commands */
5545 pool_size = max_requests + 1;
5548 dev_warn(dev, "Unknown command/response queue message format: %d\n", fmt);
5552 if (ibmvfc_init_event_pool(vhost, queue, pool_size)) {
5553 dev_err(dev, "Couldn't initialize event pool.\n");
5557 queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
5558 if (!queue->msgs.handle)
5561 queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
5564 if (dma_mapping_error(dev, queue->msg_token)) {
5565 free_page((unsigned long)queue->msgs.handle);
5566 queue->msgs.handle = NULL;
5572 queue->size = PAGE_SIZE / fmt_size;
5577 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
5578 * @vhost: ibmvfc host struct
5580 * Allocates a page for messages, maps it for dma, and registers
5581 * the crq with the hypervisor.
5584 * zero on success / other on failure
5586 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
5588 int rc, retrc = -ENOMEM;
5589 struct device *dev = vhost->dev;
5590 struct vio_dev *vdev = to_vio_dev(dev);
5591 struct ibmvfc_queue *crq = &vhost->crq;
5594 if (ibmvfc_alloc_queue(vhost, crq, IBMVFC_CRQ_FMT))
5597 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5598 crq->msg_token, PAGE_SIZE);
5600 if (rc == H_RESOURCE)
5601 /* maybe kexecing and resource is busy. try a reset */
5602 retrc = rc = ibmvfc_reset_crq(vhost);
5605 dev_warn(dev, "Partner adapter not ready\n");
5607 dev_warn(dev, "Error %d opening adapter\n", rc);
5608 goto reg_crq_failed;
5613 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
5615 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
5616 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
5617 goto req_irq_failed;
5620 if ((rc = vio_enable_interrupts(vdev))) {
5621 dev_err(dev, "Error %d enabling interrupts\n", rc);
5622 goto req_irq_failed;
5629 tasklet_kill(&vhost->tasklet);
5631 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5632 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5634 ibmvfc_free_queue(vhost, crq);
5638 static int ibmvfc_register_scsi_channel(struct ibmvfc_host *vhost,
5641 struct device *dev = vhost->dev;
5642 struct vio_dev *vdev = to_vio_dev(dev);
5643 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5648 if (ibmvfc_alloc_queue(vhost, scrq, IBMVFC_SUB_CRQ_FMT))
5651 rc = h_reg_sub_crq(vdev->unit_address, scrq->msg_token, PAGE_SIZE,
5652 &scrq->cookie, &scrq->hw_irq);
5654 /* H_CLOSED indicates successful register, but no CRQ partner */
5655 if (rc && rc != H_CLOSED) {
5656 dev_warn(dev, "Error registering sub-crq: %d\n", rc);
5657 if (rc == H_PARAMETER)
5658 dev_warn_once(dev, "Firmware may not support MQ\n");
5662 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
5666 dev_err(dev, "Error mapping sub-crq[%d] irq\n", index);
5670 snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-scsi%d",
5671 vdev->unit_address, index);
5672 rc = request_irq(scrq->irq, ibmvfc_interrupt_scsi, 0, scrq->name, scrq);
5675 dev_err(dev, "Couldn't register sub-crq[%d] irq\n", index);
5676 irq_dispose_mapping(scrq->irq);
5680 scrq->hwq_id = index;
5681 scrq->vhost = vhost;
5688 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address, scrq->cookie);
5689 } while (rtas_busy_delay(rc));
5691 ibmvfc_free_queue(vhost, scrq);
5696 static void ibmvfc_deregister_scsi_channel(struct ibmvfc_host *vhost, int index)
5698 struct device *dev = vhost->dev;
5699 struct vio_dev *vdev = to_vio_dev(dev);
5700 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5705 free_irq(scrq->irq, scrq);
5706 irq_dispose_mapping(scrq->irq);
5710 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address,
5712 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5715 dev_err(dev, "Failed to free sub-crq[%d]: rc=%ld\n", index, rc);
5717 ibmvfc_free_queue(vhost, scrq);
5721 static void ibmvfc_init_sub_crqs(struct ibmvfc_host *vhost)
5726 if (!vhost->mq_enabled)
5729 vhost->scsi_scrqs.scrqs = kcalloc(nr_scsi_hw_queues,
5730 sizeof(*vhost->scsi_scrqs.scrqs),
5732 if (!vhost->scsi_scrqs.scrqs) {
5733 vhost->do_enquiry = 0;
5737 for (i = 0; i < nr_scsi_hw_queues; i++) {
5738 if (ibmvfc_register_scsi_channel(vhost, i)) {
5739 for (j = i; j > 0; j--)
5740 ibmvfc_deregister_scsi_channel(vhost, j - 1);
5741 kfree(vhost->scsi_scrqs.scrqs);
5742 vhost->scsi_scrqs.scrqs = NULL;
5743 vhost->scsi_scrqs.active_queues = 0;
5744 vhost->do_enquiry = 0;
5752 static void ibmvfc_release_sub_crqs(struct ibmvfc_host *vhost)
5757 if (!vhost->scsi_scrqs.scrqs)
5760 for (i = 0; i < nr_scsi_hw_queues; i++)
5761 ibmvfc_deregister_scsi_channel(vhost, i);
5763 kfree(vhost->scsi_scrqs.scrqs);
5764 vhost->scsi_scrqs.scrqs = NULL;
5765 vhost->scsi_scrqs.active_queues = 0;
5770 * ibmvfc_free_mem - Free memory for vhost
5771 * @vhost: ibmvfc host struct
5776 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
5778 struct ibmvfc_queue *async_q = &vhost->async_crq;
5781 mempool_destroy(vhost->tgt_pool);
5782 kfree(vhost->trace);
5783 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
5784 vhost->disc_buf_dma);
5785 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
5786 vhost->login_buf, vhost->login_buf_dma);
5787 dma_pool_destroy(vhost->sg_pool);
5788 ibmvfc_free_queue(vhost, async_q);
5793 * ibmvfc_alloc_mem - Allocate memory for vhost
5794 * @vhost: ibmvfc host struct
5797 * 0 on success / non-zero on failure
5799 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
5801 struct ibmvfc_queue *async_q = &vhost->async_crq;
5802 struct device *dev = vhost->dev;
5805 if (ibmvfc_alloc_queue(vhost, async_q, IBMVFC_ASYNC_FMT)) {
5806 dev_err(dev, "Couldn't allocate/map async queue.\n");
5810 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
5811 SG_ALL * sizeof(struct srp_direct_buf),
5812 sizeof(struct srp_direct_buf), 0);
5814 if (!vhost->sg_pool) {
5815 dev_err(dev, "Failed to allocate sg pool\n");
5816 goto unmap_async_crq;
5819 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
5820 &vhost->login_buf_dma, GFP_KERNEL);
5822 if (!vhost->login_buf) {
5823 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
5827 vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
5828 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
5829 &vhost->disc_buf_dma, GFP_KERNEL);
5831 if (!vhost->disc_buf) {
5832 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
5833 goto free_login_buffer;
5836 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
5837 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
5838 atomic_set(&vhost->trace_index, -1);
5841 goto free_disc_buffer;
5843 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
5844 sizeof(struct ibmvfc_target));
5846 if (!vhost->tgt_pool) {
5847 dev_err(dev, "Couldn't allocate target memory pool\n");
5851 vhost->channel_setup_buf = dma_alloc_coherent(dev, sizeof(*vhost->channel_setup_buf),
5852 &vhost->channel_setup_dma,
5855 if (!vhost->channel_setup_buf) {
5856 dev_err(dev, "Couldn't allocate Channel Setup buffer\n");
5864 mempool_destroy(vhost->tgt_pool);
5866 kfree(vhost->trace);
5868 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
5869 vhost->disc_buf_dma);
5871 dma_free_coherent(dev, sizeof(*vhost->login_buf),
5872 vhost->login_buf, vhost->login_buf_dma);
5874 dma_pool_destroy(vhost->sg_pool);
5876 ibmvfc_free_queue(vhost, async_q);
5883 * ibmvfc_rport_add_thread - Worker thread for rport adds
5884 * @work: work struct
5887 static void ibmvfc_rport_add_thread(struct work_struct *work)
5889 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
5891 struct ibmvfc_target *tgt;
5892 struct fc_rport *rport;
5893 unsigned long flags;
5897 spin_lock_irqsave(vhost->host->host_lock, flags);
5900 if (vhost->state != IBMVFC_ACTIVE)
5903 list_for_each_entry(tgt, &vhost->targets, queue) {
5904 if (tgt->add_rport) {
5907 kref_get(&tgt->kref);
5910 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5911 ibmvfc_tgt_add_rport(tgt);
5912 } else if (get_device(&rport->dev)) {
5913 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5914 tgt_dbg(tgt, "Setting rport roles\n");
5915 fc_remote_port_rolechg(rport, tgt->ids.roles);
5916 put_device(&rport->dev);
5918 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5921 kref_put(&tgt->kref, ibmvfc_release_tgt);
5922 spin_lock_irqsave(vhost->host->host_lock, flags);
5928 if (vhost->state == IBMVFC_ACTIVE)
5929 vhost->scan_complete = 1;
5930 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5935 * ibmvfc_probe - Adapter hot plug add entry point
5936 * @vdev: vio device struct
5937 * @id: vio device id struct
5940 * 0 on success / non-zero on failure
5942 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
5944 struct ibmvfc_host *vhost;
5945 struct Scsi_Host *shost;
5946 struct device *dev = &vdev->dev;
5948 unsigned int max_scsi_queues = IBMVFC_MAX_SCSI_QUEUES;
5951 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
5953 dev_err(dev, "Couldn't allocate host data\n");
5957 shost->transportt = ibmvfc_transport_template;
5958 shost->can_queue = max_requests;
5959 shost->max_lun = max_lun;
5960 shost->max_id = max_targets;
5961 shost->max_sectors = IBMVFC_MAX_SECTORS;
5962 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
5963 shost->unique_id = shost->host_no;
5964 shost->nr_hw_queues = mq_enabled ? min(max_scsi_queues, nr_scsi_hw_queues) : 1;
5966 vhost = shost_priv(shost);
5967 INIT_LIST_HEAD(&vhost->targets);
5968 INIT_LIST_HEAD(&vhost->purge);
5969 sprintf(vhost->name, IBMVFC_NAME);
5970 vhost->host = shost;
5972 vhost->partition_number = -1;
5973 vhost->log_level = log_level;
5974 vhost->task_set = 1;
5976 vhost->mq_enabled = mq_enabled;
5977 vhost->client_scsi_channels = min(shost->nr_hw_queues, nr_scsi_channels);
5978 vhost->using_channels = 0;
5979 vhost->do_enquiry = 1;
5981 strcpy(vhost->partition_name, "UNKNOWN");
5982 init_waitqueue_head(&vhost->work_wait_q);
5983 init_waitqueue_head(&vhost->init_wait_q);
5984 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
5985 mutex_init(&vhost->passthru_mutex);
5987 if ((rc = ibmvfc_alloc_mem(vhost)))
5988 goto free_scsi_host;
5990 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
5993 if (IS_ERR(vhost->work_thread)) {
5994 dev_err(dev, "Couldn't create kernel thread: %ld\n",
5995 PTR_ERR(vhost->work_thread));
5996 rc = PTR_ERR(vhost->work_thread);
6000 if ((rc = ibmvfc_init_crq(vhost))) {
6001 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
6005 if ((rc = scsi_add_host(shost, dev)))
6008 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
6010 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
6011 &ibmvfc_trace_attr))) {
6012 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
6016 ibmvfc_init_sub_crqs(vhost);
6018 if (shost_to_fc_host(shost)->rqst_q)
6019 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
6020 dev_set_drvdata(dev, vhost);
6021 spin_lock(&ibmvfc_driver_lock);
6022 list_add_tail(&vhost->queue, &ibmvfc_head);
6023 spin_unlock(&ibmvfc_driver_lock);
6025 ibmvfc_send_crq_init(vhost);
6026 scsi_scan_host(shost);
6030 scsi_remove_host(shost);
6032 ibmvfc_release_crq_queue(vhost);
6034 kthread_stop(vhost->work_thread);
6036 ibmvfc_free_mem(vhost);
6038 scsi_host_put(shost);
6045 * ibmvfc_remove - Adapter hot plug remove entry point
6046 * @vdev: vio device struct
6051 static void ibmvfc_remove(struct vio_dev *vdev)
6053 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
6055 unsigned long flags;
6058 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
6060 spin_lock_irqsave(vhost->host->host_lock, flags);
6061 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
6062 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6064 ibmvfc_wait_while_resetting(vhost);
6065 kthread_stop(vhost->work_thread);
6066 fc_remove_host(vhost->host);
6067 scsi_remove_host(vhost->host);
6069 spin_lock_irqsave(vhost->host->host_lock, flags);
6070 ibmvfc_purge_requests(vhost, DID_ERROR);
6071 list_splice_init(&vhost->purge, &purge);
6072 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6073 ibmvfc_complete_purge(&purge);
6074 ibmvfc_release_sub_crqs(vhost);
6075 ibmvfc_release_crq_queue(vhost);
6077 ibmvfc_free_mem(vhost);
6078 spin_lock(&ibmvfc_driver_lock);
6079 list_del(&vhost->queue);
6080 spin_unlock(&ibmvfc_driver_lock);
6081 scsi_host_put(vhost->host);
6086 * ibmvfc_resume - Resume from suspend
6087 * @dev: device struct
6089 * We may have lost an interrupt across suspend/resume, so kick the
6093 static int ibmvfc_resume(struct device *dev)
6095 unsigned long flags;
6096 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
6097 struct vio_dev *vdev = to_vio_dev(dev);
6099 spin_lock_irqsave(vhost->host->host_lock, flags);
6100 vio_disable_interrupts(vdev);
6101 tasklet_schedule(&vhost->tasklet);
6102 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6107 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
6108 * @vdev: vio device struct
6111 * Number of bytes the driver will need to DMA map at the same time in
6112 * order to perform well.
6114 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
6116 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
6117 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
6120 static const struct vio_device_id ibmvfc_device_table[] = {
6121 {"fcp", "IBM,vfc-client"},
6124 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
6126 static const struct dev_pm_ops ibmvfc_pm_ops = {
6127 .resume = ibmvfc_resume
6130 static struct vio_driver ibmvfc_driver = {
6131 .id_table = ibmvfc_device_table,
6132 .probe = ibmvfc_probe,
6133 .remove = ibmvfc_remove,
6134 .get_desired_dma = ibmvfc_get_desired_dma,
6135 .name = IBMVFC_NAME,
6136 .pm = &ibmvfc_pm_ops,
6139 static struct fc_function_template ibmvfc_transport_functions = {
6140 .show_host_fabric_name = 1,
6141 .show_host_node_name = 1,
6142 .show_host_port_name = 1,
6143 .show_host_supported_classes = 1,
6144 .show_host_port_type = 1,
6145 .show_host_port_id = 1,
6146 .show_host_maxframe_size = 1,
6148 .get_host_port_state = ibmvfc_get_host_port_state,
6149 .show_host_port_state = 1,
6151 .get_host_speed = ibmvfc_get_host_speed,
6152 .show_host_speed = 1,
6154 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
6155 .terminate_rport_io = ibmvfc_terminate_rport_io,
6157 .show_rport_maxframe_size = 1,
6158 .show_rport_supported_classes = 1,
6160 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
6161 .show_rport_dev_loss_tmo = 1,
6163 .get_starget_node_name = ibmvfc_get_starget_node_name,
6164 .show_starget_node_name = 1,
6166 .get_starget_port_name = ibmvfc_get_starget_port_name,
6167 .show_starget_port_name = 1,
6169 .get_starget_port_id = ibmvfc_get_starget_port_id,
6170 .show_starget_port_id = 1,
6172 .bsg_request = ibmvfc_bsg_request,
6173 .bsg_timeout = ibmvfc_bsg_timeout,
6177 * ibmvfc_module_init - Initialize the ibmvfc module
6180 * 0 on success / other on failure
6182 static int __init ibmvfc_module_init(void)
6186 if (!firmware_has_feature(FW_FEATURE_VIO))
6189 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
6190 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
6192 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
6193 if (!ibmvfc_transport_template)
6196 rc = vio_register_driver(&ibmvfc_driver);
6198 fc_release_transport(ibmvfc_transport_template);
6203 * ibmvfc_module_exit - Teardown the ibmvfc module
6208 static void __exit ibmvfc_module_exit(void)
6210 vio_unregister_driver(&ibmvfc_driver);
6211 fc_release_transport(ibmvfc_transport_template);
6214 module_init(ibmvfc_module_init);
6215 module_exit(ibmvfc_module_exit);