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_event_is_free - Check if event is free or not
2376 * @evt: ibmvfc event struct
2381 static bool ibmvfc_event_is_free(struct ibmvfc_event *evt)
2383 struct ibmvfc_event *loop_evt;
2385 list_for_each_entry(loop_evt, &evt->queue->free, queue_list)
2386 if (loop_evt == evt)
2393 * ibmvfc_wait_for_ops - Wait for ops to complete
2394 * @vhost: ibmvfc host struct
2395 * @device: device to match (starget or sdev)
2396 * @match: match function
2401 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2402 int (*match) (struct ibmvfc_event *, void *))
2404 struct ibmvfc_event *evt;
2405 DECLARE_COMPLETION_ONSTACK(comp);
2406 int wait, i, q_index, q_size;
2407 unsigned long flags;
2408 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2409 struct ibmvfc_queue *queues;
2412 if (vhost->mq_enabled && vhost->using_channels) {
2413 queues = vhost->scsi_scrqs.scrqs;
2414 q_size = vhost->scsi_scrqs.active_queues;
2416 queues = &vhost->crq;
2422 spin_lock_irqsave(vhost->host->host_lock, flags);
2423 for (q_index = 0; q_index < q_size; q_index++) {
2424 spin_lock(&queues[q_index].l_lock);
2425 for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2426 evt = &queues[q_index].evt_pool.events[i];
2427 if (!ibmvfc_event_is_free(evt)) {
2428 if (match(evt, device)) {
2429 evt->eh_comp = ∁
2434 spin_unlock(&queues[q_index].l_lock);
2436 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2439 timeout = wait_for_completion_timeout(&comp, timeout);
2443 spin_lock_irqsave(vhost->host->host_lock, flags);
2444 for (q_index = 0; q_index < q_size; q_index++) {
2445 spin_lock(&queues[q_index].l_lock);
2446 for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2447 evt = &queues[q_index].evt_pool.events[i];
2448 if (!ibmvfc_event_is_free(evt)) {
2449 if (match(evt, device)) {
2450 evt->eh_comp = NULL;
2455 spin_unlock(&queues[q_index].l_lock);
2457 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2459 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2461 return wait ? FAILED : SUCCESS;
2470 static struct ibmvfc_event *ibmvfc_init_tmf(struct ibmvfc_queue *queue,
2471 struct scsi_device *sdev,
2474 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2475 struct scsi_target *starget = scsi_target(sdev);
2476 struct fc_rport *rport = starget_to_rport(starget);
2477 struct ibmvfc_event *evt;
2478 struct ibmvfc_tmf *tmf;
2480 evt = ibmvfc_get_event(queue);
2481 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2484 memset(tmf, 0, sizeof(*tmf));
2485 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
2486 tmf->common.version = cpu_to_be32(2);
2487 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2489 tmf->common.version = cpu_to_be32(1);
2491 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2492 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2493 tmf->scsi_id = cpu_to_be64(rport->port_id);
2494 int_to_scsilun(sdev->lun, &tmf->lun);
2495 if (!ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPRESS_ABTS))
2496 type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2497 if (vhost->state == IBMVFC_ACTIVE)
2498 tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2500 tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2501 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2502 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2504 init_completion(&evt->comp);
2509 static int ibmvfc_cancel_all_mq(struct scsi_device *sdev, int type)
2511 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2512 struct ibmvfc_event *evt, *found_evt, *temp;
2513 struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
2514 unsigned long flags;
2521 spin_lock_irqsave(vhost->host->host_lock, flags);
2522 num_hwq = vhost->scsi_scrqs.active_queues;
2523 for (i = 0; i < num_hwq; i++) {
2524 spin_lock(queues[i].q_lock);
2525 spin_lock(&queues[i].l_lock);
2527 list_for_each_entry(evt, &queues[i].sent, queue_list) {
2528 if (evt->cmnd && evt->cmnd->device == sdev) {
2533 spin_unlock(&queues[i].l_lock);
2535 if (found_evt && vhost->logged_in) {
2536 evt = ibmvfc_init_tmf(&queues[i], sdev, type);
2537 evt->sync_iu = &queues[i].cancel_rsp;
2538 ibmvfc_send_event(evt, vhost, default_timeout);
2539 list_add_tail(&evt->cancel, &cancelq);
2542 spin_unlock(queues[i].q_lock);
2544 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2546 if (list_empty(&cancelq)) {
2547 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2548 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2552 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2554 list_for_each_entry_safe(evt, temp, &cancelq, cancel) {
2555 wait_for_completion(&evt->comp);
2556 status = be16_to_cpu(evt->queue->cancel_rsp.mad_common.status);
2557 list_del(&evt->cancel);
2558 ibmvfc_free_event(evt);
2560 if (status != IBMVFC_MAD_SUCCESS) {
2561 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2563 case IBMVFC_MAD_DRIVER_FAILED:
2564 case IBMVFC_MAD_CRQ_ERROR:
2565 /* Host adapter most likely going through reset, return success to
2566 * the caller will wait for the command being cancelled to get returned
2579 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2584 static int ibmvfc_cancel_all_sq(struct scsi_device *sdev, int type)
2586 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2587 struct ibmvfc_event *evt, *found_evt;
2588 union ibmvfc_iu rsp;
2589 int rsp_rc = -EBUSY;
2590 unsigned long flags;
2595 spin_lock_irqsave(vhost->host->host_lock, flags);
2596 spin_lock(&vhost->crq.l_lock);
2597 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2598 if (evt->cmnd && evt->cmnd->device == sdev) {
2603 spin_unlock(&vhost->crq.l_lock);
2606 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2607 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2608 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2612 if (vhost->logged_in) {
2613 evt = ibmvfc_init_tmf(&vhost->crq, sdev, type);
2614 evt->sync_iu = &rsp;
2615 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2618 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2621 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2622 /* If failure is received, the host adapter is most likely going
2623 through reset, return success so the caller will wait for the command
2624 being cancelled to get returned */
2628 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2630 wait_for_completion(&evt->comp);
2631 status = be16_to_cpu(rsp.mad_common.status);
2632 spin_lock_irqsave(vhost->host->host_lock, flags);
2633 ibmvfc_free_event(evt);
2634 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2636 if (status != IBMVFC_MAD_SUCCESS) {
2637 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2639 case IBMVFC_MAD_DRIVER_FAILED:
2640 case IBMVFC_MAD_CRQ_ERROR:
2641 /* Host adapter most likely going through reset, return success to
2642 the caller will wait for the command being cancelled to get returned */
2649 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2654 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2655 * @sdev: scsi device to cancel commands
2656 * @type: type of error recovery being performed
2658 * This sends a cancel to the VIOS for the specified device. This does
2659 * NOT send any abort to the actual device. That must be done separately.
2662 * 0 on success / other on failure
2664 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2666 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2668 if (vhost->mq_enabled && vhost->using_channels)
2669 return ibmvfc_cancel_all_mq(sdev, type);
2671 return ibmvfc_cancel_all_sq(sdev, type);
2675 * ibmvfc_match_key - Match function for specified cancel key
2676 * @evt: ibmvfc event struct
2677 * @key: cancel key to match
2680 * 1 if event matches key / 0 if event does not match key
2682 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2684 unsigned long cancel_key = (unsigned long)key;
2686 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2687 be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2693 * ibmvfc_match_evt - Match function for specified event
2694 * @evt: ibmvfc event struct
2695 * @match: event to match
2698 * 1 if event matches key / 0 if event does not match key
2700 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2708 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2709 * @sdev: scsi device to abort commands
2711 * This sends an Abort Task Set to the VIOS for the specified device. This does
2712 * NOT send any cancel to the VIOS. That must be done separately.
2715 * 0 on success / other on failure
2717 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2719 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2720 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2721 struct ibmvfc_cmd *tmf;
2722 struct ibmvfc_event *evt, *found_evt;
2723 union ibmvfc_iu rsp_iu;
2724 struct ibmvfc_fcp_cmd_iu *iu;
2725 struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2726 int rc, rsp_rc = -EBUSY;
2727 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2731 spin_lock_irqsave(vhost->host->host_lock, flags);
2732 spin_lock(&vhost->crq.l_lock);
2733 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2734 if (evt->cmnd && evt->cmnd->device == sdev) {
2739 spin_unlock(&vhost->crq.l_lock);
2742 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2743 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2744 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2748 if (vhost->state == IBMVFC_ACTIVE) {
2749 evt = ibmvfc_get_event(&vhost->crq);
2750 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2751 tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2752 iu = ibmvfc_get_fcp_iu(vhost, tmf);
2754 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2755 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2756 iu->tmf_flags = IBMVFC_ABORT_TASK_SET;
2757 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2758 evt->sync_iu = &rsp_iu;
2760 tmf->correlation = cpu_to_be64((u64)evt);
2762 init_completion(&evt->comp);
2763 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2766 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2769 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2773 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2774 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2777 rc = ibmvfc_cancel_all(sdev, 0);
2779 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2785 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2786 ibmvfc_reset_host(vhost);
2788 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2793 rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2794 if (rc != SUCCESS) {
2795 spin_lock_irqsave(vhost->host->host_lock, flags);
2796 ibmvfc_hard_reset_host(vhost);
2797 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2805 if (rsp_iu.cmd.status)
2806 rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2809 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2810 rsp_code = fc_rsp->data.info.rsp_code;
2812 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2813 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2814 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2815 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2816 fc_rsp->scsi_status);
2819 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2822 spin_lock_irqsave(vhost->host->host_lock, flags);
2823 ibmvfc_free_event(evt);
2824 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2829 * ibmvfc_eh_abort_handler - Abort a command
2830 * @cmd: scsi command to abort
2833 * SUCCESS / FAST_IO_FAIL / FAILED
2835 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2837 struct scsi_device *sdev = cmd->device;
2838 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2839 int cancel_rc, block_rc;
2843 block_rc = fc_block_scsi_eh(cmd);
2844 ibmvfc_wait_while_resetting(vhost);
2845 if (block_rc != FAST_IO_FAIL) {
2846 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2847 ibmvfc_abort_task_set(sdev);
2849 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2852 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2854 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2862 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2863 * @cmd: scsi command struct
2866 * SUCCESS / FAST_IO_FAIL / FAILED
2868 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2870 struct scsi_device *sdev = cmd->device;
2871 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2872 int cancel_rc, block_rc, reset_rc = 0;
2876 block_rc = fc_block_scsi_eh(cmd);
2877 ibmvfc_wait_while_resetting(vhost);
2878 if (block_rc != FAST_IO_FAIL) {
2879 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2880 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2882 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2884 if (!cancel_rc && !reset_rc)
2885 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2887 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2895 * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2896 * @sdev: scsi device struct
2897 * @data: return code
2900 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2902 unsigned long *rc = data;
2903 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2907 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2908 * @sdev: scsi device struct
2909 * @data: return code
2912 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2914 unsigned long *rc = data;
2915 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2919 * ibmvfc_eh_target_reset_handler - Reset the target
2920 * @cmd: scsi command struct
2923 * SUCCESS / FAST_IO_FAIL / FAILED
2925 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2927 struct scsi_device *sdev = cmd->device;
2928 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2929 struct scsi_target *starget = scsi_target(sdev);
2933 unsigned long cancel_rc = 0;
2936 block_rc = fc_block_scsi_eh(cmd);
2937 ibmvfc_wait_while_resetting(vhost);
2938 if (block_rc != FAST_IO_FAIL) {
2939 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2940 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2942 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2944 if (!cancel_rc && !reset_rc)
2945 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2947 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2955 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2956 * @cmd: struct scsi_cmnd having problems
2959 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2962 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2964 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2965 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2967 return rc ? FAILED : SUCCESS;
2971 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2972 * @rport: rport struct
2977 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2979 struct Scsi_Host *shost = rport_to_shost(rport);
2980 struct ibmvfc_host *vhost = shost_priv(shost);
2981 struct fc_rport *dev_rport;
2982 struct scsi_device *sdev;
2983 struct ibmvfc_target *tgt;
2984 unsigned long rc, flags;
2988 shost_for_each_device(sdev, shost) {
2989 dev_rport = starget_to_rport(scsi_target(sdev));
2990 if (dev_rport != rport)
2992 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2995 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
2998 ibmvfc_issue_fc_host_lip(shost);
3000 spin_lock_irqsave(shost->host_lock, flags);
3002 list_for_each_entry(tgt, &vhost->targets, queue) {
3003 if (tgt->scsi_id == rport->port_id) {
3009 if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
3011 * If we get here, that means we previously attempted to send
3012 * an implicit logout to the target but it failed, most likely
3013 * due to I/O being pending, so we need to send it again
3015 ibmvfc_del_tgt(tgt);
3016 ibmvfc_reinit_host(vhost);
3019 spin_unlock_irqrestore(shost->host_lock, flags);
3023 static const struct ibmvfc_async_desc ae_desc [] = {
3024 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3025 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3026 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3027 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3028 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3029 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
3030 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
3031 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
3032 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
3033 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
3034 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
3035 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
3036 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
3039 static const struct ibmvfc_async_desc unknown_ae = {
3040 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
3044 * ibmvfc_get_ae_desc - Get text description for async event
3048 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
3052 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
3053 if (ae_desc[i].ae == ae)
3059 static const struct {
3060 enum ibmvfc_ae_link_state state;
3063 { IBMVFC_AE_LS_LINK_UP, " link up" },
3064 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
3065 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
3066 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
3070 * ibmvfc_get_link_state - Get text description for link state
3071 * @state: link state
3074 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
3078 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
3079 if (link_desc[i].state == state)
3080 return link_desc[i].desc;
3086 * ibmvfc_handle_async - Handle an async event from the adapter
3087 * @crq: crq to process
3088 * @vhost: ibmvfc host struct
3091 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
3092 struct ibmvfc_host *vhost)
3094 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
3095 struct ibmvfc_target *tgt;
3097 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
3098 " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
3099 be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
3100 ibmvfc_get_link_state(crq->link_state));
3102 switch (be64_to_cpu(crq->event)) {
3103 case IBMVFC_AE_RESUME:
3104 switch (crq->link_state) {
3105 case IBMVFC_AE_LS_LINK_DOWN:
3106 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3108 case IBMVFC_AE_LS_LINK_DEAD:
3109 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3111 case IBMVFC_AE_LS_LINK_UP:
3112 case IBMVFC_AE_LS_LINK_BOUNCED:
3114 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3115 vhost->delay_init = 1;
3116 __ibmvfc_reset_host(vhost);
3121 case IBMVFC_AE_LINK_UP:
3122 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3123 vhost->delay_init = 1;
3124 __ibmvfc_reset_host(vhost);
3126 case IBMVFC_AE_SCN_FABRIC:
3127 case IBMVFC_AE_SCN_DOMAIN:
3128 vhost->events_to_log |= IBMVFC_AE_RSCN;
3129 if (vhost->state < IBMVFC_HALTED) {
3130 vhost->delay_init = 1;
3131 __ibmvfc_reset_host(vhost);
3134 case IBMVFC_AE_SCN_NPORT:
3135 case IBMVFC_AE_SCN_GROUP:
3136 vhost->events_to_log |= IBMVFC_AE_RSCN;
3137 ibmvfc_reinit_host(vhost);
3139 case IBMVFC_AE_ELS_LOGO:
3140 case IBMVFC_AE_ELS_PRLO:
3141 case IBMVFC_AE_ELS_PLOGI:
3142 list_for_each_entry(tgt, &vhost->targets, queue) {
3143 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
3145 if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
3147 if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
3149 if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
3151 if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
3153 if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
3154 ibmvfc_del_tgt(tgt);
3155 ibmvfc_reinit_host(vhost);
3159 case IBMVFC_AE_LINK_DOWN:
3160 case IBMVFC_AE_ADAPTER_FAILED:
3161 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3163 case IBMVFC_AE_LINK_DEAD:
3164 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3166 case IBMVFC_AE_HALT:
3167 ibmvfc_link_down(vhost, IBMVFC_HALTED);
3170 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
3176 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
3177 * @crq: Command/Response queue
3178 * @vhost: ibmvfc host struct
3181 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3182 struct list_head *evt_doneq)
3185 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3187 switch (crq->valid) {
3188 case IBMVFC_CRQ_INIT_RSP:
3189 switch (crq->format) {
3190 case IBMVFC_CRQ_INIT:
3191 dev_info(vhost->dev, "Partner initialized\n");
3192 /* Send back a response */
3193 rc = ibmvfc_send_crq_init_complete(vhost);
3195 ibmvfc_init_host(vhost);
3197 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
3199 case IBMVFC_CRQ_INIT_COMPLETE:
3200 dev_info(vhost->dev, "Partner initialization complete\n");
3201 ibmvfc_init_host(vhost);
3204 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
3207 case IBMVFC_CRQ_XPORT_EVENT:
3208 vhost->state = IBMVFC_NO_CRQ;
3209 vhost->logged_in = 0;
3210 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3211 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
3212 /* We need to re-setup the interpartition connection */
3213 dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
3214 vhost->client_migrated = 1;
3215 ibmvfc_purge_requests(vhost, DID_REQUEUE);
3216 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3217 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
3218 } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
3219 dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
3220 ibmvfc_purge_requests(vhost, DID_ERROR);
3221 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3222 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
3224 dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
3227 case IBMVFC_CRQ_CMD_RSP:
3230 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
3234 if (crq->format == IBMVFC_ASYNC_EVENT)
3237 /* The only kind of payload CRQs we should get are responses to
3238 * things we send. Make sure this response is to something we
3241 if (unlikely(!ibmvfc_valid_event(&vhost->crq.evt_pool, evt))) {
3242 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3247 if (unlikely(atomic_read(&evt->free))) {
3248 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3253 spin_lock(&evt->queue->l_lock);
3254 list_move_tail(&evt->queue_list, evt_doneq);
3255 spin_unlock(&evt->queue->l_lock);
3259 * ibmvfc_scan_finished - Check if the device scan is done.
3260 * @shost: scsi host struct
3261 * @time: current elapsed time
3264 * 0 if scan is not done / 1 if scan is done
3266 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3268 unsigned long flags;
3269 struct ibmvfc_host *vhost = shost_priv(shost);
3272 spin_lock_irqsave(shost->host_lock, flags);
3273 if (time >= (init_timeout * HZ)) {
3274 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
3275 "continuing initialization\n", init_timeout);
3279 if (vhost->scan_complete)
3281 spin_unlock_irqrestore(shost->host_lock, flags);
3286 * ibmvfc_slave_alloc - Setup the device's task set value
3287 * @sdev: struct scsi_device device to configure
3289 * Set the device's task set value so that error handling works as
3293 * 0 on success / -ENXIO if device does not exist
3295 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
3297 struct Scsi_Host *shost = sdev->host;
3298 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3299 struct ibmvfc_host *vhost = shost_priv(shost);
3300 unsigned long flags = 0;
3302 if (!rport || fc_remote_port_chkready(rport))
3305 spin_lock_irqsave(shost->host_lock, flags);
3306 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
3307 spin_unlock_irqrestore(shost->host_lock, flags);
3312 * ibmvfc_target_alloc - Setup the target's task set value
3313 * @starget: struct scsi_target
3315 * Set the target's task set value so that error handling works as
3319 * 0 on success / -ENXIO if device does not exist
3321 static int ibmvfc_target_alloc(struct scsi_target *starget)
3323 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3324 struct ibmvfc_host *vhost = shost_priv(shost);
3325 unsigned long flags = 0;
3327 spin_lock_irqsave(shost->host_lock, flags);
3328 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
3329 spin_unlock_irqrestore(shost->host_lock, flags);
3334 * ibmvfc_slave_configure - Configure the device
3335 * @sdev: struct scsi_device device to configure
3337 * Enable allow_restart for a device if it is a disk. Adjust the
3338 * queue_depth here also.
3343 static int ibmvfc_slave_configure(struct scsi_device *sdev)
3345 struct Scsi_Host *shost = sdev->host;
3346 unsigned long flags = 0;
3348 spin_lock_irqsave(shost->host_lock, flags);
3349 if (sdev->type == TYPE_DISK) {
3350 sdev->allow_restart = 1;
3351 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
3353 spin_unlock_irqrestore(shost->host_lock, flags);
3358 * ibmvfc_change_queue_depth - Change the device's queue depth
3359 * @sdev: scsi device struct
3360 * @qdepth: depth to set
3361 * @reason: calling context
3366 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
3368 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
3369 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
3371 return scsi_change_queue_depth(sdev, qdepth);
3374 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
3375 struct device_attribute *attr, char *buf)
3377 struct Scsi_Host *shost = class_to_shost(dev);
3378 struct ibmvfc_host *vhost = shost_priv(shost);
3380 return snprintf(buf, PAGE_SIZE, "%s\n",
3381 vhost->login_buf->resp.partition_name);
3384 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
3385 struct device_attribute *attr, char *buf)
3387 struct Scsi_Host *shost = class_to_shost(dev);
3388 struct ibmvfc_host *vhost = shost_priv(shost);
3390 return snprintf(buf, PAGE_SIZE, "%s\n",
3391 vhost->login_buf->resp.device_name);
3394 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3395 struct device_attribute *attr, char *buf)
3397 struct Scsi_Host *shost = class_to_shost(dev);
3398 struct ibmvfc_host *vhost = shost_priv(shost);
3400 return snprintf(buf, PAGE_SIZE, "%s\n",
3401 vhost->login_buf->resp.port_loc_code);
3404 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3405 struct device_attribute *attr, char *buf)
3407 struct Scsi_Host *shost = class_to_shost(dev);
3408 struct ibmvfc_host *vhost = shost_priv(shost);
3410 return snprintf(buf, PAGE_SIZE, "%s\n",
3411 vhost->login_buf->resp.drc_name);
3414 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3415 struct device_attribute *attr, char *buf)
3417 struct Scsi_Host *shost = class_to_shost(dev);
3418 struct ibmvfc_host *vhost = shost_priv(shost);
3419 return snprintf(buf, PAGE_SIZE, "%d\n", be32_to_cpu(vhost->login_buf->resp.version));
3422 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3423 struct device_attribute *attr, char *buf)
3425 struct Scsi_Host *shost = class_to_shost(dev);
3426 struct ibmvfc_host *vhost = shost_priv(shost);
3427 return snprintf(buf, PAGE_SIZE, "%llx\n", be64_to_cpu(vhost->login_buf->resp.capabilities));
3431 * ibmvfc_show_log_level - Show the adapter's error logging level
3432 * @dev: class device struct
3436 * number of bytes printed to buffer
3438 static ssize_t ibmvfc_show_log_level(struct device *dev,
3439 struct device_attribute *attr, char *buf)
3441 struct Scsi_Host *shost = class_to_shost(dev);
3442 struct ibmvfc_host *vhost = shost_priv(shost);
3443 unsigned long flags = 0;
3446 spin_lock_irqsave(shost->host_lock, flags);
3447 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
3448 spin_unlock_irqrestore(shost->host_lock, flags);
3453 * ibmvfc_store_log_level - Change the adapter's error logging level
3454 * @dev: class device struct
3458 * number of bytes printed to buffer
3460 static ssize_t ibmvfc_store_log_level(struct device *dev,
3461 struct device_attribute *attr,
3462 const char *buf, size_t count)
3464 struct Scsi_Host *shost = class_to_shost(dev);
3465 struct ibmvfc_host *vhost = shost_priv(shost);
3466 unsigned long flags = 0;
3468 spin_lock_irqsave(shost->host_lock, flags);
3469 vhost->log_level = simple_strtoul(buf, NULL, 10);
3470 spin_unlock_irqrestore(shost->host_lock, flags);
3474 static ssize_t ibmvfc_show_scsi_channels(struct device *dev,
3475 struct device_attribute *attr, char *buf)
3477 struct Scsi_Host *shost = class_to_shost(dev);
3478 struct ibmvfc_host *vhost = shost_priv(shost);
3479 unsigned long flags = 0;
3482 spin_lock_irqsave(shost->host_lock, flags);
3483 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->client_scsi_channels);
3484 spin_unlock_irqrestore(shost->host_lock, flags);
3488 static ssize_t ibmvfc_store_scsi_channels(struct device *dev,
3489 struct device_attribute *attr,
3490 const char *buf, size_t count)
3492 struct Scsi_Host *shost = class_to_shost(dev);
3493 struct ibmvfc_host *vhost = shost_priv(shost);
3494 unsigned long flags = 0;
3495 unsigned int channels;
3497 spin_lock_irqsave(shost->host_lock, flags);
3498 channels = simple_strtoul(buf, NULL, 10);
3499 vhost->client_scsi_channels = min(channels, nr_scsi_hw_queues);
3500 ibmvfc_hard_reset_host(vhost);
3501 spin_unlock_irqrestore(shost->host_lock, flags);
3505 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3506 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3507 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3508 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3509 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3510 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3511 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3512 ibmvfc_show_log_level, ibmvfc_store_log_level);
3513 static DEVICE_ATTR(nr_scsi_channels, S_IRUGO | S_IWUSR,
3514 ibmvfc_show_scsi_channels, ibmvfc_store_scsi_channels);
3516 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3518 * ibmvfc_read_trace - Dump the adapter trace
3519 * @filp: open sysfs file
3520 * @kobj: kobject struct
3521 * @bin_attr: bin_attribute struct
3524 * @count: buffer size
3527 * number of bytes printed to buffer
3529 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3530 struct bin_attribute *bin_attr,
3531 char *buf, loff_t off, size_t count)
3533 struct device *dev = container_of(kobj, struct device, kobj);
3534 struct Scsi_Host *shost = class_to_shost(dev);
3535 struct ibmvfc_host *vhost = shost_priv(shost);
3536 unsigned long flags = 0;
3537 int size = IBMVFC_TRACE_SIZE;
3538 char *src = (char *)vhost->trace;
3542 if (off + count > size) {
3547 spin_lock_irqsave(shost->host_lock, flags);
3548 memcpy(buf, &src[off], count);
3549 spin_unlock_irqrestore(shost->host_lock, flags);
3553 static struct bin_attribute ibmvfc_trace_attr = {
3559 .read = ibmvfc_read_trace,
3563 static struct device_attribute *ibmvfc_attrs[] = {
3564 &dev_attr_partition_name,
3565 &dev_attr_device_name,
3566 &dev_attr_port_loc_code,
3568 &dev_attr_npiv_version,
3569 &dev_attr_capabilities,
3570 &dev_attr_log_level,
3571 &dev_attr_nr_scsi_channels,
3575 static struct scsi_host_template driver_template = {
3576 .module = THIS_MODULE,
3577 .name = "IBM POWER Virtual FC Adapter",
3578 .proc_name = IBMVFC_NAME,
3579 .queuecommand = ibmvfc_queuecommand,
3580 .eh_timed_out = fc_eh_timed_out,
3581 .eh_abort_handler = ibmvfc_eh_abort_handler,
3582 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3583 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3584 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3585 .slave_alloc = ibmvfc_slave_alloc,
3586 .slave_configure = ibmvfc_slave_configure,
3587 .target_alloc = ibmvfc_target_alloc,
3588 .scan_finished = ibmvfc_scan_finished,
3589 .change_queue_depth = ibmvfc_change_queue_depth,
3591 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3593 .sg_tablesize = SG_ALL,
3594 .max_sectors = IBMVFC_MAX_SECTORS,
3595 .shost_attrs = ibmvfc_attrs,
3596 .track_queue_depth = 1,
3601 * ibmvfc_next_async_crq - Returns the next entry in async queue
3602 * @vhost: ibmvfc host struct
3605 * Pointer to next entry in queue / NULL if empty
3607 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3609 struct ibmvfc_queue *async_crq = &vhost->async_crq;
3610 struct ibmvfc_async_crq *crq;
3612 crq = &async_crq->msgs.async[async_crq->cur];
3613 if (crq->valid & 0x80) {
3614 if (++async_crq->cur == async_crq->size)
3624 * ibmvfc_next_crq - Returns the next entry in message queue
3625 * @vhost: ibmvfc host struct
3628 * Pointer to next entry in queue / NULL if empty
3630 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3632 struct ibmvfc_queue *queue = &vhost->crq;
3633 struct ibmvfc_crq *crq;
3635 crq = &queue->msgs.crq[queue->cur];
3636 if (crq->valid & 0x80) {
3637 if (++queue->cur == queue->size)
3647 * ibmvfc_interrupt - Interrupt handler
3648 * @irq: number of irq to handle, not used
3649 * @dev_instance: ibmvfc_host that received interrupt
3654 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3656 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3657 unsigned long flags;
3659 spin_lock_irqsave(vhost->host->host_lock, flags);
3660 vio_disable_interrupts(to_vio_dev(vhost->dev));
3661 tasklet_schedule(&vhost->tasklet);
3662 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3667 * ibmvfc_tasklet - Interrupt handler tasklet
3668 * @data: ibmvfc host struct
3673 static void ibmvfc_tasklet(void *data)
3675 struct ibmvfc_host *vhost = data;
3676 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3677 struct ibmvfc_crq *crq;
3678 struct ibmvfc_async_crq *async;
3679 struct ibmvfc_event *evt, *temp;
3680 unsigned long flags;
3682 LIST_HEAD(evt_doneq);
3684 spin_lock_irqsave(vhost->host->host_lock, flags);
3685 spin_lock(vhost->crq.q_lock);
3687 /* Pull all the valid messages off the async CRQ */
3688 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3689 ibmvfc_handle_async(async, vhost);
3694 /* Pull all the valid messages off the CRQ */
3695 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3696 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3701 vio_enable_interrupts(vdev);
3702 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3703 vio_disable_interrupts(vdev);
3704 ibmvfc_handle_async(async, vhost);
3707 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3708 vio_disable_interrupts(vdev);
3709 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3716 spin_unlock(vhost->crq.q_lock);
3717 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3719 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3720 del_timer(&evt->timer);
3721 list_del(&evt->queue_list);
3722 ibmvfc_trc_end(evt);
3727 static int ibmvfc_toggle_scrq_irq(struct ibmvfc_queue *scrq, int enable)
3729 struct device *dev = scrq->vhost->dev;
3730 struct vio_dev *vdev = to_vio_dev(dev);
3732 int irq_action = H_ENABLE_VIO_INTERRUPT;
3735 irq_action = H_DISABLE_VIO_INTERRUPT;
3737 rc = plpar_hcall_norets(H_VIOCTL, vdev->unit_address, irq_action,
3738 scrq->hw_irq, 0, 0);
3741 dev_err(dev, "Couldn't %s sub-crq[%lu] irq. rc=%ld\n",
3742 enable ? "enable" : "disable", scrq->hwq_id, rc);
3747 static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3748 struct list_head *evt_doneq)
3750 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3752 switch (crq->valid) {
3753 case IBMVFC_CRQ_CMD_RSP:
3755 case IBMVFC_CRQ_XPORT_EVENT:
3758 dev_err(vhost->dev, "Got and invalid message type 0x%02x\n", crq->valid);
3762 /* The only kind of payload CRQs we should get are responses to
3763 * things we send. Make sure this response is to something we
3766 if (unlikely(!ibmvfc_valid_event(&evt->queue->evt_pool, evt))) {
3767 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3772 if (unlikely(atomic_read(&evt->free))) {
3773 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3778 spin_lock(&evt->queue->l_lock);
3779 list_move_tail(&evt->queue_list, evt_doneq);
3780 spin_unlock(&evt->queue->l_lock);
3783 static struct ibmvfc_crq *ibmvfc_next_scrq(struct ibmvfc_queue *scrq)
3785 struct ibmvfc_crq *crq;
3787 crq = &scrq->msgs.scrq[scrq->cur].crq;
3788 if (crq->valid & 0x80) {
3789 if (++scrq->cur == scrq->size)
3798 static void ibmvfc_drain_sub_crq(struct ibmvfc_queue *scrq)
3800 struct ibmvfc_crq *crq;
3801 struct ibmvfc_event *evt, *temp;
3802 unsigned long flags;
3804 LIST_HEAD(evt_doneq);
3806 spin_lock_irqsave(scrq->q_lock, flags);
3808 while ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3809 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3814 ibmvfc_toggle_scrq_irq(scrq, 1);
3815 if ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3816 ibmvfc_toggle_scrq_irq(scrq, 0);
3817 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3823 spin_unlock_irqrestore(scrq->q_lock, flags);
3825 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3826 del_timer(&evt->timer);
3827 list_del(&evt->queue_list);
3828 ibmvfc_trc_end(evt);
3833 static irqreturn_t ibmvfc_interrupt_scsi(int irq, void *scrq_instance)
3835 struct ibmvfc_queue *scrq = (struct ibmvfc_queue *)scrq_instance;
3837 ibmvfc_toggle_scrq_irq(scrq, 0);
3838 ibmvfc_drain_sub_crq(scrq);
3844 * ibmvfc_init_tgt - Set the next init job step for the target
3845 * @tgt: ibmvfc target struct
3846 * @job_step: job step to perform
3849 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3850 void (*job_step) (struct ibmvfc_target *))
3852 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3853 tgt->job_step = job_step;
3854 wake_up(&tgt->vhost->work_wait_q);
3858 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3859 * @tgt: ibmvfc target struct
3860 * @job_step: initialization job step
3862 * Returns: 1 if step will be retried / 0 if not
3865 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3866 void (*job_step) (struct ibmvfc_target *))
3868 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3869 ibmvfc_del_tgt(tgt);
3870 wake_up(&tgt->vhost->work_wait_q);
3873 ibmvfc_init_tgt(tgt, job_step);
3877 /* Defined in FC-LS */
3878 static const struct {
3895 * ibmvfc_get_prli_rsp - Find PRLI response index
3896 * @flags: PRLI response flags
3899 static int ibmvfc_get_prli_rsp(u16 flags)
3902 int code = (flags & 0x0f00) >> 8;
3904 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3905 if (prli_rsp[i].code == code)
3912 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3913 * @evt: ibmvfc event struct
3916 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3918 struct ibmvfc_target *tgt = evt->tgt;
3919 struct ibmvfc_host *vhost = evt->vhost;
3920 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3921 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3922 u32 status = be16_to_cpu(rsp->common.status);
3923 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3925 vhost->discovery_threads--;
3926 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3928 case IBMVFC_MAD_SUCCESS:
3929 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3930 parms->type, parms->flags, parms->service_parms);
3932 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3933 index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3934 if (prli_rsp[index].logged_in) {
3935 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3936 tgt->need_login = 0;
3938 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3939 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3940 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3941 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3944 ibmvfc_del_tgt(tgt);
3945 } else if (prli_rsp[index].retry)
3946 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3948 ibmvfc_del_tgt(tgt);
3950 ibmvfc_del_tgt(tgt);
3952 case IBMVFC_MAD_DRIVER_FAILED:
3954 case IBMVFC_MAD_CRQ_ERROR:
3955 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3957 case IBMVFC_MAD_FAILED:
3959 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3960 be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3961 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3962 else if (tgt->logo_rcvd)
3963 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3964 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3965 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3967 ibmvfc_del_tgt(tgt);
3969 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3970 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3971 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
3975 kref_put(&tgt->kref, ibmvfc_release_tgt);
3976 ibmvfc_free_event(evt);
3977 wake_up(&vhost->work_wait_q);
3981 * ibmvfc_tgt_send_prli - Send a process login
3982 * @tgt: ibmvfc target struct
3985 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3987 struct ibmvfc_process_login *prli;
3988 struct ibmvfc_host *vhost = tgt->vhost;
3989 struct ibmvfc_event *evt;
3991 if (vhost->discovery_threads >= disc_threads)
3994 kref_get(&tgt->kref);
3995 evt = ibmvfc_get_event(&vhost->crq);
3996 vhost->discovery_threads++;
3997 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3999 prli = &evt->iu.prli;
4000 memset(prli, 0, sizeof(*prli));
4001 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4002 prli->common.version = cpu_to_be32(2);
4003 prli->target_wwpn = cpu_to_be64(tgt->wwpn);
4005 prli->common.version = cpu_to_be32(1);
4007 prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
4008 prli->common.length = cpu_to_be16(sizeof(*prli));
4009 prli->scsi_id = cpu_to_be64(tgt->scsi_id);
4011 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
4012 prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
4013 prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
4014 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
4017 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
4019 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4020 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4021 vhost->discovery_threads--;
4022 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4023 kref_put(&tgt->kref, ibmvfc_release_tgt);
4025 tgt_dbg(tgt, "Sent process login\n");
4029 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
4030 * @evt: ibmvfc event struct
4033 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
4035 struct ibmvfc_target *tgt = evt->tgt;
4036 struct ibmvfc_host *vhost = evt->vhost;
4037 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
4038 u32 status = be16_to_cpu(rsp->common.status);
4039 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4041 vhost->discovery_threads--;
4042 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4044 case IBMVFC_MAD_SUCCESS:
4045 tgt_dbg(tgt, "Port Login succeeded\n");
4046 if (tgt->ids.port_name &&
4047 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
4049 tgt_dbg(tgt, "Port re-init required\n");
4052 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4053 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4054 tgt->ids.port_id = tgt->scsi_id;
4055 memcpy(&tgt->service_parms, &rsp->service_parms,
4056 sizeof(tgt->service_parms));
4057 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4058 sizeof(tgt->service_parms_change));
4059 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4061 case IBMVFC_MAD_DRIVER_FAILED:
4063 case IBMVFC_MAD_CRQ_ERROR:
4064 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4066 case IBMVFC_MAD_FAILED:
4068 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4069 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4071 ibmvfc_del_tgt(tgt);
4073 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4074 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4075 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4076 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4077 ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
4081 kref_put(&tgt->kref, ibmvfc_release_tgt);
4082 ibmvfc_free_event(evt);
4083 wake_up(&vhost->work_wait_q);
4087 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
4088 * @tgt: ibmvfc target struct
4091 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
4093 struct ibmvfc_port_login *plogi;
4094 struct ibmvfc_host *vhost = tgt->vhost;
4095 struct ibmvfc_event *evt;
4097 if (vhost->discovery_threads >= disc_threads)
4100 kref_get(&tgt->kref);
4102 evt = ibmvfc_get_event(&vhost->crq);
4103 vhost->discovery_threads++;
4104 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4105 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
4107 plogi = &evt->iu.plogi;
4108 memset(plogi, 0, sizeof(*plogi));
4109 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4110 plogi->common.version = cpu_to_be32(2);
4111 plogi->target_wwpn = cpu_to_be64(tgt->wwpn);
4113 plogi->common.version = cpu_to_be32(1);
4115 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
4116 plogi->common.length = cpu_to_be16(sizeof(*plogi));
4117 plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
4119 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4120 vhost->discovery_threads--;
4121 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4122 kref_put(&tgt->kref, ibmvfc_release_tgt);
4124 tgt_dbg(tgt, "Sent port login\n");
4128 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
4129 * @evt: ibmvfc event struct
4132 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
4134 struct ibmvfc_target *tgt = evt->tgt;
4135 struct ibmvfc_host *vhost = evt->vhost;
4136 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
4137 u32 status = be16_to_cpu(rsp->common.status);
4139 vhost->discovery_threads--;
4140 ibmvfc_free_event(evt);
4141 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4144 case IBMVFC_MAD_SUCCESS:
4145 tgt_dbg(tgt, "Implicit Logout succeeded\n");
4147 case IBMVFC_MAD_DRIVER_FAILED:
4148 kref_put(&tgt->kref, ibmvfc_release_tgt);
4149 wake_up(&vhost->work_wait_q);
4151 case IBMVFC_MAD_FAILED:
4153 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
4157 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
4158 kref_put(&tgt->kref, ibmvfc_release_tgt);
4159 wake_up(&vhost->work_wait_q);
4163 * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
4164 * @tgt: ibmvfc target struct
4167 * Allocated and initialized ibmvfc_event struct
4169 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
4170 void (*done) (struct ibmvfc_event *))
4172 struct ibmvfc_implicit_logout *mad;
4173 struct ibmvfc_host *vhost = tgt->vhost;
4174 struct ibmvfc_event *evt;
4176 kref_get(&tgt->kref);
4177 evt = ibmvfc_get_event(&vhost->crq);
4178 ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
4180 mad = &evt->iu.implicit_logout;
4181 memset(mad, 0, sizeof(*mad));
4182 mad->common.version = cpu_to_be32(1);
4183 mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
4184 mad->common.length = cpu_to_be16(sizeof(*mad));
4185 mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4190 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
4191 * @tgt: ibmvfc target struct
4194 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
4196 struct ibmvfc_host *vhost = tgt->vhost;
4197 struct ibmvfc_event *evt;
4199 if (vhost->discovery_threads >= disc_threads)
4202 vhost->discovery_threads++;
4203 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4204 ibmvfc_tgt_implicit_logout_done);
4206 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4207 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4208 vhost->discovery_threads--;
4209 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4210 kref_put(&tgt->kref, ibmvfc_release_tgt);
4212 tgt_dbg(tgt, "Sent Implicit Logout\n");
4216 * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
4217 * @evt: ibmvfc event struct
4220 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
4222 struct ibmvfc_target *tgt = evt->tgt;
4223 struct ibmvfc_host *vhost = evt->vhost;
4224 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4225 u32 status = be16_to_cpu(mad->common.status);
4227 vhost->discovery_threads--;
4228 ibmvfc_free_event(evt);
4231 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
4232 * driver in which case we need to free up all the targets. If we are
4233 * not unloading, we will still go through a hard reset to get out of
4234 * offline state, so there is no need to track the old targets in that
4237 if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
4238 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4240 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
4242 tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
4243 kref_put(&tgt->kref, ibmvfc_release_tgt);
4244 wake_up(&vhost->work_wait_q);
4248 * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
4249 * @tgt: ibmvfc target struct
4252 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
4254 struct ibmvfc_host *vhost = tgt->vhost;
4255 struct ibmvfc_event *evt;
4257 if (!vhost->logged_in) {
4258 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4262 if (vhost->discovery_threads >= disc_threads)
4265 vhost->discovery_threads++;
4266 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4267 ibmvfc_tgt_implicit_logout_and_del_done);
4269 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
4270 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4271 vhost->discovery_threads--;
4272 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4273 kref_put(&tgt->kref, ibmvfc_release_tgt);
4275 tgt_dbg(tgt, "Sent Implicit Logout\n");
4279 * ibmvfc_tgt_move_login_done - Completion handler for Move Login
4280 * @evt: ibmvfc event struct
4283 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
4285 struct ibmvfc_target *tgt = evt->tgt;
4286 struct ibmvfc_host *vhost = evt->vhost;
4287 struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
4288 u32 status = be16_to_cpu(rsp->common.status);
4289 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4291 vhost->discovery_threads--;
4292 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4294 case IBMVFC_MAD_SUCCESS:
4295 tgt_dbg(tgt, "Move Login succeeded for old scsi_id: %llX\n", tgt->old_scsi_id);
4296 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4297 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4298 tgt->ids.port_id = tgt->scsi_id;
4299 memcpy(&tgt->service_parms, &rsp->service_parms,
4300 sizeof(tgt->service_parms));
4301 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4302 sizeof(tgt->service_parms_change));
4303 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4305 case IBMVFC_MAD_DRIVER_FAILED:
4307 case IBMVFC_MAD_CRQ_ERROR:
4308 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4310 case IBMVFC_MAD_FAILED:
4312 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4315 "Move Login failed: old scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
4316 tgt->old_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
4321 kref_put(&tgt->kref, ibmvfc_release_tgt);
4322 ibmvfc_free_event(evt);
4323 wake_up(&vhost->work_wait_q);
4328 * ibmvfc_tgt_move_login - Initiate a move login for specified target
4329 * @tgt: ibmvfc target struct
4332 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
4334 struct ibmvfc_host *vhost = tgt->vhost;
4335 struct ibmvfc_move_login *move;
4336 struct ibmvfc_event *evt;
4338 if (vhost->discovery_threads >= disc_threads)
4341 kref_get(&tgt->kref);
4342 evt = ibmvfc_get_event(&vhost->crq);
4343 vhost->discovery_threads++;
4344 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4345 ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
4347 move = &evt->iu.move_login;
4348 memset(move, 0, sizeof(*move));
4349 move->common.version = cpu_to_be32(1);
4350 move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
4351 move->common.length = cpu_to_be16(sizeof(*move));
4353 move->old_scsi_id = cpu_to_be64(tgt->old_scsi_id);
4354 move->new_scsi_id = cpu_to_be64(tgt->scsi_id);
4355 move->wwpn = cpu_to_be64(tgt->wwpn);
4356 move->node_name = cpu_to_be64(tgt->ids.node_name);
4358 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4359 vhost->discovery_threads--;
4360 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4361 kref_put(&tgt->kref, ibmvfc_release_tgt);
4363 tgt_dbg(tgt, "Sent Move Login for old scsi_id: %llX\n", tgt->old_scsi_id);
4367 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
4368 * @mad: ibmvfc passthru mad struct
4369 * @tgt: ibmvfc target struct
4372 * 1 if PLOGI needed / 0 if PLOGI not needed
4374 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
4375 struct ibmvfc_target *tgt)
4377 if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
4379 if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
4381 if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
4387 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
4388 * @evt: ibmvfc event struct
4391 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
4393 struct ibmvfc_target *tgt = evt->tgt;
4394 struct ibmvfc_host *vhost = evt->vhost;
4395 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4396 u32 status = be16_to_cpu(mad->common.status);
4397 u8 fc_reason, fc_explain;
4399 vhost->discovery_threads--;
4400 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4401 del_timer(&tgt->timer);
4404 case IBMVFC_MAD_SUCCESS:
4405 tgt_dbg(tgt, "ADISC succeeded\n");
4406 if (ibmvfc_adisc_needs_plogi(mad, tgt))
4407 ibmvfc_del_tgt(tgt);
4409 case IBMVFC_MAD_DRIVER_FAILED:
4411 case IBMVFC_MAD_FAILED:
4413 ibmvfc_del_tgt(tgt);
4414 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
4415 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
4416 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4417 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
4418 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
4419 ibmvfc_get_fc_type(fc_reason), fc_reason,
4420 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
4424 kref_put(&tgt->kref, ibmvfc_release_tgt);
4425 ibmvfc_free_event(evt);
4426 wake_up(&vhost->work_wait_q);
4430 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
4431 * @evt: ibmvfc event struct
4434 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
4436 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
4438 memset(mad, 0, sizeof(*mad));
4439 mad->common.version = cpu_to_be32(1);
4440 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
4441 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
4442 mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4443 offsetof(struct ibmvfc_passthru_mad, iu));
4444 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
4445 mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4446 mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
4447 mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4448 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4449 offsetof(struct ibmvfc_passthru_fc_iu, payload));
4450 mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4451 mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4452 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4453 offsetof(struct ibmvfc_passthru_fc_iu, response));
4454 mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
4458 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
4459 * @evt: ibmvfc event struct
4461 * Just cleanup this event struct. Everything else is handled by
4462 * the ADISC completion handler. If the ADISC never actually comes
4463 * back, we still have the timer running on the ADISC event struct
4464 * which will fire and cause the CRQ to get reset.
4467 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
4469 struct ibmvfc_host *vhost = evt->vhost;
4470 struct ibmvfc_target *tgt = evt->tgt;
4472 tgt_dbg(tgt, "ADISC cancel complete\n");
4473 vhost->abort_threads--;
4474 ibmvfc_free_event(evt);
4475 kref_put(&tgt->kref, ibmvfc_release_tgt);
4476 wake_up(&vhost->work_wait_q);
4480 * ibmvfc_adisc_timeout - Handle an ADISC timeout
4481 * @tgt: ibmvfc target struct
4483 * If an ADISC times out, send a cancel. If the cancel times
4484 * out, reset the CRQ. When the ADISC comes back as cancelled,
4485 * log back into the target.
4487 static void ibmvfc_adisc_timeout(struct timer_list *t)
4489 struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
4490 struct ibmvfc_host *vhost = tgt->vhost;
4491 struct ibmvfc_event *evt;
4492 struct ibmvfc_tmf *tmf;
4493 unsigned long flags;
4496 tgt_dbg(tgt, "ADISC timeout\n");
4497 spin_lock_irqsave(vhost->host->host_lock, flags);
4498 if (vhost->abort_threads >= disc_threads ||
4499 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
4500 vhost->state != IBMVFC_INITIALIZING ||
4501 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
4502 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4506 vhost->abort_threads++;
4507 kref_get(&tgt->kref);
4508 evt = ibmvfc_get_event(&vhost->crq);
4509 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
4513 memset(tmf, 0, sizeof(*tmf));
4514 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4515 tmf->common.version = cpu_to_be32(2);
4516 tmf->target_wwpn = cpu_to_be64(tgt->wwpn);
4518 tmf->common.version = cpu_to_be32(1);
4520 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
4521 tmf->common.length = cpu_to_be16(sizeof(*tmf));
4522 tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
4523 tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
4525 rc = ibmvfc_send_event(evt, vhost, default_timeout);
4528 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
4529 vhost->abort_threads--;
4530 kref_put(&tgt->kref, ibmvfc_release_tgt);
4531 __ibmvfc_reset_host(vhost);
4533 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
4534 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4538 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
4539 * @tgt: ibmvfc target struct
4541 * When sending an ADISC we end up with two timers running. The
4542 * first timer is the timer in the ibmvfc target struct. If this
4543 * fires, we send a cancel to the target. The second timer is the
4544 * timer on the ibmvfc event for the ADISC, which is longer. If that
4545 * fires, it means the ADISC timed out and our attempt to cancel it
4546 * also failed, so we need to reset the CRQ.
4548 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
4550 struct ibmvfc_passthru_mad *mad;
4551 struct ibmvfc_host *vhost = tgt->vhost;
4552 struct ibmvfc_event *evt;
4554 if (vhost->discovery_threads >= disc_threads)
4557 kref_get(&tgt->kref);
4558 evt = ibmvfc_get_event(&vhost->crq);
4559 vhost->discovery_threads++;
4560 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
4563 ibmvfc_init_passthru(evt);
4564 mad = &evt->iu.passthru;
4565 mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4566 mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4567 mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4569 mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4570 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4571 sizeof(vhost->login_buf->resp.port_name));
4572 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4573 sizeof(vhost->login_buf->resp.node_name));
4574 mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4576 if (timer_pending(&tgt->timer))
4577 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4579 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4580 add_timer(&tgt->timer);
4583 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4584 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4585 vhost->discovery_threads--;
4586 del_timer(&tgt->timer);
4587 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4588 kref_put(&tgt->kref, ibmvfc_release_tgt);
4590 tgt_dbg(tgt, "Sent ADISC\n");
4594 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4595 * @evt: ibmvfc event struct
4598 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4600 struct ibmvfc_target *tgt = evt->tgt;
4601 struct ibmvfc_host *vhost = evt->vhost;
4602 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4603 u32 status = be16_to_cpu(rsp->common.status);
4604 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4606 vhost->discovery_threads--;
4607 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4609 case IBMVFC_MAD_SUCCESS:
4610 tgt_dbg(tgt, "Query Target succeeded\n");
4611 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4612 ibmvfc_del_tgt(tgt);
4614 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4616 case IBMVFC_MAD_DRIVER_FAILED:
4618 case IBMVFC_MAD_CRQ_ERROR:
4619 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4621 case IBMVFC_MAD_FAILED:
4623 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4624 be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4625 be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4626 ibmvfc_del_tgt(tgt);
4627 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4628 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4630 ibmvfc_del_tgt(tgt);
4632 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4633 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4634 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4635 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4636 ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4641 kref_put(&tgt->kref, ibmvfc_release_tgt);
4642 ibmvfc_free_event(evt);
4643 wake_up(&vhost->work_wait_q);
4647 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4648 * @tgt: ibmvfc target struct
4651 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4653 struct ibmvfc_query_tgt *query_tgt;
4654 struct ibmvfc_host *vhost = tgt->vhost;
4655 struct ibmvfc_event *evt;
4657 if (vhost->discovery_threads >= disc_threads)
4660 kref_get(&tgt->kref);
4661 evt = ibmvfc_get_event(&vhost->crq);
4662 vhost->discovery_threads++;
4664 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4665 query_tgt = &evt->iu.query_tgt;
4666 memset(query_tgt, 0, sizeof(*query_tgt));
4667 query_tgt->common.version = cpu_to_be32(1);
4668 query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4669 query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4670 query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4672 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4673 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4674 vhost->discovery_threads--;
4675 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4676 kref_put(&tgt->kref, ibmvfc_release_tgt);
4678 tgt_dbg(tgt, "Sent Query Target\n");
4682 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4683 * @vhost: ibmvfc host struct
4684 * @scsi_id: SCSI ID to allocate target for
4687 * 0 on success / other on failure
4689 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4690 struct ibmvfc_discover_targets_entry *target)
4692 struct ibmvfc_target *stgt = NULL;
4693 struct ibmvfc_target *wtgt = NULL;
4694 struct ibmvfc_target *tgt;
4695 unsigned long flags;
4696 u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4697 u64 wwpn = be64_to_cpu(target->wwpn);
4699 /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4700 spin_lock_irqsave(vhost->host->host_lock, flags);
4701 list_for_each_entry(tgt, &vhost->targets, queue) {
4702 if (tgt->wwpn == wwpn) {
4708 list_for_each_entry(tgt, &vhost->targets, queue) {
4709 if (tgt->scsi_id == scsi_id) {
4715 if (wtgt && !stgt) {
4717 * A WWPN target has moved and we still are tracking the old
4718 * SCSI ID. The only way we should be able to get here is if
4719 * we attempted to send an implicit logout for the old SCSI ID
4720 * and it failed for some reason, such as there being I/O
4721 * pending to the target. In this case, we will have already
4722 * deleted the rport from the FC transport so we do a move
4723 * login, which works even with I/O pending, as it will cancel
4724 * any active commands.
4726 if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4728 * Do a move login here. The old target is no longer
4729 * known to the transport layer We don't use the
4730 * normal ibmvfc_set_tgt_action to set this, as we
4731 * don't normally want to allow this state change.
4733 wtgt->old_scsi_id = wtgt->scsi_id;
4734 wtgt->scsi_id = scsi_id;
4735 wtgt->action = IBMVFC_TGT_ACTION_INIT;
4736 ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4739 tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4740 wtgt->action, wtgt->rport);
4743 if (tgt->need_login)
4744 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4747 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4749 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4750 memset(tgt, 0, sizeof(*tgt));
4751 tgt->scsi_id = scsi_id;
4754 tgt->need_login = 1;
4755 timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4756 kref_init(&tgt->kref);
4757 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4758 spin_lock_irqsave(vhost->host->host_lock, flags);
4759 tgt->cancel_key = vhost->task_set++;
4760 list_add_tail(&tgt->queue, &vhost->targets);
4763 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4768 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4769 * @vhost: ibmvfc host struct
4772 * 0 on success / other on failure
4774 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4778 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4779 rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
4785 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4786 * @evt: ibmvfc event struct
4789 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4791 struct ibmvfc_host *vhost = evt->vhost;
4792 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4793 u32 mad_status = be16_to_cpu(rsp->common.status);
4794 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4796 switch (mad_status) {
4797 case IBMVFC_MAD_SUCCESS:
4798 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4799 vhost->num_targets = be32_to_cpu(rsp->num_written);
4800 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4802 case IBMVFC_MAD_FAILED:
4803 level += ibmvfc_retry_host_init(vhost);
4804 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4805 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4806 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4808 case IBMVFC_MAD_DRIVER_FAILED:
4811 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4812 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4816 ibmvfc_free_event(evt);
4817 wake_up(&vhost->work_wait_q);
4821 * ibmvfc_discover_targets - Send Discover Targets MAD
4822 * @vhost: ibmvfc host struct
4825 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4827 struct ibmvfc_discover_targets *mad;
4828 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4830 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4831 mad = &evt->iu.discover_targets;
4832 memset(mad, 0, sizeof(*mad));
4833 mad->common.version = cpu_to_be32(1);
4834 mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4835 mad->common.length = cpu_to_be16(sizeof(*mad));
4836 mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4837 mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4838 mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4839 mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
4840 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4842 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4843 ibmvfc_dbg(vhost, "Sent discover targets\n");
4845 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4848 static void ibmvfc_channel_setup_done(struct ibmvfc_event *evt)
4850 struct ibmvfc_host *vhost = evt->vhost;
4851 struct ibmvfc_channel_setup *setup = vhost->channel_setup_buf;
4852 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
4853 u32 mad_status = be16_to_cpu(evt->xfer_iu->channel_setup.common.status);
4854 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4855 int flags, active_queues, i;
4857 ibmvfc_free_event(evt);
4859 switch (mad_status) {
4860 case IBMVFC_MAD_SUCCESS:
4861 ibmvfc_dbg(vhost, "Channel Setup succeeded\n");
4862 flags = be32_to_cpu(setup->flags);
4863 vhost->do_enquiry = 0;
4864 active_queues = be32_to_cpu(setup->num_scsi_subq_channels);
4865 scrqs->active_queues = active_queues;
4867 if (flags & IBMVFC_CHANNELS_CANCELED) {
4868 ibmvfc_dbg(vhost, "Channels Canceled\n");
4869 vhost->using_channels = 0;
4872 vhost->using_channels = 1;
4873 for (i = 0; i < active_queues; i++)
4874 scrqs->scrqs[i].vios_cookie =
4875 be64_to_cpu(setup->channel_handles[i]);
4877 ibmvfc_dbg(vhost, "Using %u channels\n",
4878 vhost->scsi_scrqs.active_queues);
4881 case IBMVFC_MAD_FAILED:
4882 level += ibmvfc_retry_host_init(vhost);
4883 ibmvfc_log(vhost, level, "Channel Setup failed\n");
4885 case IBMVFC_MAD_DRIVER_FAILED:
4888 dev_err(vhost->dev, "Invalid Channel Setup response: 0x%x\n",
4890 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4894 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4895 wake_up(&vhost->work_wait_q);
4898 static void ibmvfc_channel_setup(struct ibmvfc_host *vhost)
4900 struct ibmvfc_channel_setup_mad *mad;
4901 struct ibmvfc_channel_setup *setup_buf = vhost->channel_setup_buf;
4902 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4903 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
4904 unsigned int num_channels =
4905 min(vhost->client_scsi_channels, vhost->max_vios_scsi_channels);
4908 memset(setup_buf, 0, sizeof(*setup_buf));
4909 if (num_channels == 0)
4910 setup_buf->flags = cpu_to_be32(IBMVFC_CANCEL_CHANNELS);
4912 setup_buf->num_scsi_subq_channels = cpu_to_be32(num_channels);
4913 for (i = 0; i < num_channels; i++)
4914 setup_buf->channel_handles[i] = cpu_to_be64(scrqs->scrqs[i].cookie);
4917 ibmvfc_init_event(evt, ibmvfc_channel_setup_done, IBMVFC_MAD_FORMAT);
4918 mad = &evt->iu.channel_setup;
4919 memset(mad, 0, sizeof(*mad));
4920 mad->common.version = cpu_to_be32(1);
4921 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_SETUP);
4922 mad->common.length = cpu_to_be16(sizeof(*mad));
4923 mad->buffer.va = cpu_to_be64(vhost->channel_setup_dma);
4924 mad->buffer.len = cpu_to_be32(sizeof(*vhost->channel_setup_buf));
4926 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4928 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4929 ibmvfc_dbg(vhost, "Sent channel setup\n");
4931 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
4934 static void ibmvfc_channel_enquiry_done(struct ibmvfc_event *evt)
4936 struct ibmvfc_host *vhost = evt->vhost;
4937 struct ibmvfc_channel_enquiry *rsp = &evt->xfer_iu->channel_enquiry;
4938 u32 mad_status = be16_to_cpu(rsp->common.status);
4939 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4941 switch (mad_status) {
4942 case IBMVFC_MAD_SUCCESS:
4943 ibmvfc_dbg(vhost, "Channel Enquiry succeeded\n");
4944 vhost->max_vios_scsi_channels = be32_to_cpu(rsp->num_scsi_subq_channels);
4945 ibmvfc_free_event(evt);
4947 case IBMVFC_MAD_FAILED:
4948 level += ibmvfc_retry_host_init(vhost);
4949 ibmvfc_log(vhost, level, "Channel Enquiry failed\n");
4951 case IBMVFC_MAD_DRIVER_FAILED:
4952 ibmvfc_free_event(evt);
4955 dev_err(vhost->dev, "Invalid Channel Enquiry response: 0x%x\n",
4957 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4958 ibmvfc_free_event(evt);
4962 ibmvfc_channel_setup(vhost);
4965 static void ibmvfc_channel_enquiry(struct ibmvfc_host *vhost)
4967 struct ibmvfc_channel_enquiry *mad;
4968 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4970 ibmvfc_init_event(evt, ibmvfc_channel_enquiry_done, IBMVFC_MAD_FORMAT);
4971 mad = &evt->iu.channel_enquiry;
4972 memset(mad, 0, sizeof(*mad));
4973 mad->common.version = cpu_to_be32(1);
4974 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_ENQUIRY);
4975 mad->common.length = cpu_to_be16(sizeof(*mad));
4977 if (mig_channels_only)
4978 mad->flags |= cpu_to_be32(IBMVFC_NO_CHANNELS_TO_CRQ_SUPPORT);
4979 if (mig_no_less_channels)
4980 mad->flags |= cpu_to_be32(IBMVFC_NO_N_TO_M_CHANNELS_SUPPORT);
4982 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4984 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4985 ibmvfc_dbg(vhost, "Send channel enquiry\n");
4987 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4991 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4992 * @evt: ibmvfc event struct
4995 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4997 struct ibmvfc_host *vhost = evt->vhost;
4998 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
4999 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
5000 unsigned int npiv_max_sectors;
5001 int level = IBMVFC_DEFAULT_LOG_LEVEL;
5003 switch (mad_status) {
5004 case IBMVFC_MAD_SUCCESS:
5005 ibmvfc_free_event(evt);
5007 case IBMVFC_MAD_FAILED:
5008 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
5009 level += ibmvfc_retry_host_init(vhost);
5011 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5012 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
5013 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
5014 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
5015 ibmvfc_free_event(evt);
5017 case IBMVFC_MAD_CRQ_ERROR:
5018 ibmvfc_retry_host_init(vhost);
5020 case IBMVFC_MAD_DRIVER_FAILED:
5021 ibmvfc_free_event(evt);
5024 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
5025 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5026 ibmvfc_free_event(evt);
5030 vhost->client_migrated = 0;
5032 if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
5033 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
5035 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5036 wake_up(&vhost->work_wait_q);
5040 if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
5041 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
5043 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5044 wake_up(&vhost->work_wait_q);
5048 vhost->logged_in = 1;
5049 npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
5050 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
5051 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
5052 rsp->drc_name, npiv_max_sectors);
5054 fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
5055 fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
5056 fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
5057 fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
5058 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
5059 fc_host_supported_classes(vhost->host) = 0;
5060 if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
5061 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
5062 if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
5063 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
5064 if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
5065 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
5066 fc_host_maxframe_size(vhost->host) =
5067 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
5069 vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
5070 vhost->host->max_sectors = npiv_max_sectors;
5072 if (ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPORT_CHANNELS) && vhost->do_enquiry) {
5073 ibmvfc_channel_enquiry(vhost);
5075 vhost->do_enquiry = 0;
5076 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5077 wake_up(&vhost->work_wait_q);
5082 * ibmvfc_npiv_login - Sends NPIV login
5083 * @vhost: ibmvfc host struct
5086 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
5088 struct ibmvfc_npiv_login_mad *mad;
5089 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
5091 ibmvfc_gather_partition_info(vhost);
5092 ibmvfc_set_login_info(vhost);
5093 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
5095 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
5096 mad = &evt->iu.npiv_login;
5097 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
5098 mad->common.version = cpu_to_be32(1);
5099 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
5100 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
5101 mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
5102 mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
5104 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5106 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5107 ibmvfc_dbg(vhost, "Sent NPIV login\n");
5109 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5113 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
5114 * @vhost: ibmvfc host struct
5117 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
5119 struct ibmvfc_host *vhost = evt->vhost;
5120 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
5122 ibmvfc_free_event(evt);
5124 switch (mad_status) {
5125 case IBMVFC_MAD_SUCCESS:
5126 if (list_empty(&vhost->crq.sent) &&
5127 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
5128 ibmvfc_init_host(vhost);
5132 case IBMVFC_MAD_FAILED:
5133 case IBMVFC_MAD_NOT_SUPPORTED:
5134 case IBMVFC_MAD_CRQ_ERROR:
5135 case IBMVFC_MAD_DRIVER_FAILED:
5137 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
5141 ibmvfc_hard_reset_host(vhost);
5145 * ibmvfc_npiv_logout - Issue an NPIV Logout
5146 * @vhost: ibmvfc host struct
5149 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
5151 struct ibmvfc_npiv_logout_mad *mad;
5152 struct ibmvfc_event *evt;
5154 evt = ibmvfc_get_event(&vhost->crq);
5155 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
5157 mad = &evt->iu.npiv_logout;
5158 memset(mad, 0, sizeof(*mad));
5159 mad->common.version = cpu_to_be32(1);
5160 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
5161 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
5163 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
5165 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5166 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
5168 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5172 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
5173 * @vhost: ibmvfc host struct
5176 * 1 if work to do / 0 if not
5178 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
5180 struct ibmvfc_target *tgt;
5182 list_for_each_entry(tgt, &vhost->targets, queue) {
5183 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
5184 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5192 * ibmvfc_dev_logo_to_do - Is there target logout work to do?
5193 * @vhost: ibmvfc host struct
5196 * 1 if work to do / 0 if not
5198 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
5200 struct ibmvfc_target *tgt;
5202 list_for_each_entry(tgt, &vhost->targets, queue) {
5203 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
5204 tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5211 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
5212 * @vhost: ibmvfc host struct
5215 * 1 if work to do / 0 if not
5217 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5219 struct ibmvfc_target *tgt;
5221 if (kthread_should_stop())
5223 switch (vhost->action) {
5224 case IBMVFC_HOST_ACTION_NONE:
5225 case IBMVFC_HOST_ACTION_INIT_WAIT:
5226 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5228 case IBMVFC_HOST_ACTION_TGT_INIT:
5229 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5230 if (vhost->discovery_threads == disc_threads)
5232 list_for_each_entry(tgt, &vhost->targets, queue)
5233 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
5235 list_for_each_entry(tgt, &vhost->targets, queue)
5236 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5239 case IBMVFC_HOST_ACTION_TGT_DEL:
5240 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5241 if (vhost->discovery_threads == disc_threads)
5243 list_for_each_entry(tgt, &vhost->targets, queue)
5244 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
5246 list_for_each_entry(tgt, &vhost->targets, queue)
5247 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5250 case IBMVFC_HOST_ACTION_LOGO:
5251 case IBMVFC_HOST_ACTION_INIT:
5252 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5253 case IBMVFC_HOST_ACTION_QUERY:
5254 case IBMVFC_HOST_ACTION_RESET:
5255 case IBMVFC_HOST_ACTION_REENABLE:
5264 * ibmvfc_work_to_do - Is there task level work to do?
5265 * @vhost: ibmvfc host struct
5268 * 1 if work to do / 0 if not
5270 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5272 unsigned long flags;
5275 spin_lock_irqsave(vhost->host->host_lock, flags);
5276 rc = __ibmvfc_work_to_do(vhost);
5277 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5282 * ibmvfc_log_ae - Log async events if necessary
5283 * @vhost: ibmvfc host struct
5284 * @events: events to log
5287 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
5289 if (events & IBMVFC_AE_RSCN)
5290 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
5291 if ((events & IBMVFC_AE_LINKDOWN) &&
5292 vhost->state >= IBMVFC_HALTED)
5293 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
5294 if ((events & IBMVFC_AE_LINKUP) &&
5295 vhost->state == IBMVFC_INITIALIZING)
5296 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
5300 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
5301 * @tgt: ibmvfc target struct
5304 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
5306 struct ibmvfc_host *vhost = tgt->vhost;
5307 struct fc_rport *rport;
5308 unsigned long flags;
5310 tgt_dbg(tgt, "Adding rport\n");
5311 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
5312 spin_lock_irqsave(vhost->host->host_lock, flags);
5314 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5315 tgt_dbg(tgt, "Deleting rport\n");
5316 list_del(&tgt->queue);
5317 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5318 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5319 fc_remote_port_delete(rport);
5320 del_timer_sync(&tgt->timer);
5321 kref_put(&tgt->kref, ibmvfc_release_tgt);
5323 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5324 tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
5325 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5327 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5328 fc_remote_port_delete(rport);
5330 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
5331 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5336 tgt_dbg(tgt, "rport add succeeded\n");
5338 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
5339 rport->supported_classes = 0;
5340 tgt->target_id = rport->scsi_target_id;
5341 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
5342 rport->supported_classes |= FC_COS_CLASS1;
5343 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
5344 rport->supported_classes |= FC_COS_CLASS2;
5345 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
5346 rport->supported_classes |= FC_COS_CLASS3;
5348 blk_queue_max_segments(rport->rqst_q, 1);
5350 tgt_dbg(tgt, "rport add failed\n");
5351 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5355 * ibmvfc_do_work - Do task level work
5356 * @vhost: ibmvfc host struct
5359 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
5361 struct ibmvfc_target *tgt;
5362 unsigned long flags;
5363 struct fc_rport *rport;
5367 ibmvfc_log_ae(vhost, vhost->events_to_log);
5368 spin_lock_irqsave(vhost->host->host_lock, flags);
5369 vhost->events_to_log = 0;
5370 switch (vhost->action) {
5371 case IBMVFC_HOST_ACTION_NONE:
5372 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5373 case IBMVFC_HOST_ACTION_INIT_WAIT:
5375 case IBMVFC_HOST_ACTION_RESET:
5376 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5377 list_splice_init(&vhost->purge, &purge);
5378 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5379 ibmvfc_complete_purge(&purge);
5380 rc = ibmvfc_reset_crq(vhost);
5381 spin_lock_irqsave(vhost->host->host_lock, flags);
5383 vio_enable_interrupts(to_vio_dev(vhost->dev));
5384 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
5385 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
5386 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5387 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
5390 case IBMVFC_HOST_ACTION_REENABLE:
5391 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5392 list_splice_init(&vhost->purge, &purge);
5393 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5394 ibmvfc_complete_purge(&purge);
5395 rc = ibmvfc_reenable_crq_queue(vhost);
5396 spin_lock_irqsave(vhost->host->host_lock, flags);
5397 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
5398 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5399 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
5402 case IBMVFC_HOST_ACTION_LOGO:
5403 vhost->job_step(vhost);
5405 case IBMVFC_HOST_ACTION_INIT:
5406 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
5407 if (vhost->delay_init) {
5408 vhost->delay_init = 0;
5409 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5413 vhost->job_step(vhost);
5415 case IBMVFC_HOST_ACTION_QUERY:
5416 list_for_each_entry(tgt, &vhost->targets, queue)
5417 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
5418 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
5420 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5421 list_for_each_entry(tgt, &vhost->targets, queue) {
5422 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5428 if (!ibmvfc_dev_init_to_do(vhost))
5429 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
5431 case IBMVFC_HOST_ACTION_TGT_DEL:
5432 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5433 list_for_each_entry(tgt, &vhost->targets, queue) {
5434 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
5440 if (ibmvfc_dev_logo_to_do(vhost)) {
5441 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5445 list_for_each_entry(tgt, &vhost->targets, queue) {
5446 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5447 tgt_dbg(tgt, "Deleting rport\n");
5450 list_del(&tgt->queue);
5451 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5452 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5454 fc_remote_port_delete(rport);
5455 del_timer_sync(&tgt->timer);
5456 kref_put(&tgt->kref, ibmvfc_release_tgt);
5458 } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5459 tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
5462 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5463 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5465 fc_remote_port_delete(rport);
5470 if (vhost->state == IBMVFC_INITIALIZING) {
5471 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
5472 if (vhost->reinit) {
5474 scsi_block_requests(vhost->host);
5475 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5476 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5478 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
5479 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5480 wake_up(&vhost->init_wait_q);
5481 schedule_work(&vhost->rport_add_work_q);
5482 vhost->init_retries = 0;
5483 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5484 scsi_unblock_requests(vhost->host);
5489 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
5490 vhost->job_step = ibmvfc_discover_targets;
5493 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5494 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5495 scsi_unblock_requests(vhost->host);
5496 wake_up(&vhost->init_wait_q);
5500 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5501 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
5502 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5503 ibmvfc_alloc_targets(vhost);
5504 spin_lock_irqsave(vhost->host->host_lock, flags);
5506 case IBMVFC_HOST_ACTION_TGT_INIT:
5507 list_for_each_entry(tgt, &vhost->targets, queue) {
5508 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5514 if (!ibmvfc_dev_init_to_do(vhost))
5515 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
5521 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5525 * ibmvfc_work - Do task level work
5526 * @data: ibmvfc host struct
5531 static int ibmvfc_work(void *data)
5533 struct ibmvfc_host *vhost = data;
5536 set_user_nice(current, MIN_NICE);
5539 rc = wait_event_interruptible(vhost->work_wait_q,
5540 ibmvfc_work_to_do(vhost));
5544 if (kthread_should_stop())
5547 ibmvfc_do_work(vhost);
5550 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
5555 * ibmvfc_alloc_queue - Allocate queue
5556 * @vhost: ibmvfc host struct
5557 * @queue: ibmvfc queue to allocate
5558 * @fmt: queue format to allocate
5561 * 0 on success / non-zero on failure
5563 static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
5564 struct ibmvfc_queue *queue,
5565 enum ibmvfc_msg_fmt fmt)
5567 struct device *dev = vhost->dev;
5569 unsigned int pool_size = 0;
5572 spin_lock_init(&queue->_lock);
5573 queue->q_lock = &queue->_lock;
5576 case IBMVFC_CRQ_FMT:
5577 fmt_size = sizeof(*queue->msgs.crq);
5578 pool_size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
5580 case IBMVFC_ASYNC_FMT:
5581 fmt_size = sizeof(*queue->msgs.async);
5583 case IBMVFC_SUB_CRQ_FMT:
5584 fmt_size = sizeof(*queue->msgs.scrq);
5585 /* We need one extra event for Cancel Commands */
5586 pool_size = max_requests + 1;
5589 dev_warn(dev, "Unknown command/response queue message format: %d\n", fmt);
5593 if (ibmvfc_init_event_pool(vhost, queue, pool_size)) {
5594 dev_err(dev, "Couldn't initialize event pool.\n");
5598 queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
5599 if (!queue->msgs.handle)
5602 queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
5605 if (dma_mapping_error(dev, queue->msg_token)) {
5606 free_page((unsigned long)queue->msgs.handle);
5607 queue->msgs.handle = NULL;
5613 queue->size = PAGE_SIZE / fmt_size;
5618 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
5619 * @vhost: ibmvfc host struct
5621 * Allocates a page for messages, maps it for dma, and registers
5622 * the crq with the hypervisor.
5625 * zero on success / other on failure
5627 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
5629 int rc, retrc = -ENOMEM;
5630 struct device *dev = vhost->dev;
5631 struct vio_dev *vdev = to_vio_dev(dev);
5632 struct ibmvfc_queue *crq = &vhost->crq;
5635 if (ibmvfc_alloc_queue(vhost, crq, IBMVFC_CRQ_FMT))
5638 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5639 crq->msg_token, PAGE_SIZE);
5641 if (rc == H_RESOURCE)
5642 /* maybe kexecing and resource is busy. try a reset */
5643 retrc = rc = ibmvfc_reset_crq(vhost);
5646 dev_warn(dev, "Partner adapter not ready\n");
5648 dev_warn(dev, "Error %d opening adapter\n", rc);
5649 goto reg_crq_failed;
5654 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
5656 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
5657 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
5658 goto req_irq_failed;
5661 if ((rc = vio_enable_interrupts(vdev))) {
5662 dev_err(dev, "Error %d enabling interrupts\n", rc);
5663 goto req_irq_failed;
5670 tasklet_kill(&vhost->tasklet);
5672 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5673 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5675 ibmvfc_free_queue(vhost, crq);
5679 static int ibmvfc_register_scsi_channel(struct ibmvfc_host *vhost,
5682 struct device *dev = vhost->dev;
5683 struct vio_dev *vdev = to_vio_dev(dev);
5684 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5689 if (ibmvfc_alloc_queue(vhost, scrq, IBMVFC_SUB_CRQ_FMT))
5692 rc = h_reg_sub_crq(vdev->unit_address, scrq->msg_token, PAGE_SIZE,
5693 &scrq->cookie, &scrq->hw_irq);
5695 /* H_CLOSED indicates successful register, but no CRQ partner */
5696 if (rc && rc != H_CLOSED) {
5697 dev_warn(dev, "Error registering sub-crq: %d\n", rc);
5698 if (rc == H_PARAMETER)
5699 dev_warn_once(dev, "Firmware may not support MQ\n");
5703 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
5707 dev_err(dev, "Error mapping sub-crq[%d] irq\n", index);
5711 snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-scsi%d",
5712 vdev->unit_address, index);
5713 rc = request_irq(scrq->irq, ibmvfc_interrupt_scsi, 0, scrq->name, scrq);
5716 dev_err(dev, "Couldn't register sub-crq[%d] irq\n", index);
5717 irq_dispose_mapping(scrq->irq);
5721 scrq->hwq_id = index;
5722 scrq->vhost = vhost;
5729 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address, scrq->cookie);
5730 } while (rtas_busy_delay(rc));
5732 ibmvfc_free_queue(vhost, scrq);
5737 static void ibmvfc_deregister_scsi_channel(struct ibmvfc_host *vhost, int index)
5739 struct device *dev = vhost->dev;
5740 struct vio_dev *vdev = to_vio_dev(dev);
5741 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5746 free_irq(scrq->irq, scrq);
5747 irq_dispose_mapping(scrq->irq);
5751 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address,
5753 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5756 dev_err(dev, "Failed to free sub-crq[%d]: rc=%ld\n", index, rc);
5758 ibmvfc_free_queue(vhost, scrq);
5762 static void ibmvfc_init_sub_crqs(struct ibmvfc_host *vhost)
5767 if (!vhost->mq_enabled)
5770 vhost->scsi_scrqs.scrqs = kcalloc(nr_scsi_hw_queues,
5771 sizeof(*vhost->scsi_scrqs.scrqs),
5773 if (!vhost->scsi_scrqs.scrqs) {
5774 vhost->do_enquiry = 0;
5778 for (i = 0; i < nr_scsi_hw_queues; i++) {
5779 if (ibmvfc_register_scsi_channel(vhost, i)) {
5780 for (j = i; j > 0; j--)
5781 ibmvfc_deregister_scsi_channel(vhost, j - 1);
5782 kfree(vhost->scsi_scrqs.scrqs);
5783 vhost->scsi_scrqs.scrqs = NULL;
5784 vhost->scsi_scrqs.active_queues = 0;
5785 vhost->do_enquiry = 0;
5793 static void ibmvfc_release_sub_crqs(struct ibmvfc_host *vhost)
5798 if (!vhost->scsi_scrqs.scrqs)
5801 for (i = 0; i < nr_scsi_hw_queues; i++)
5802 ibmvfc_deregister_scsi_channel(vhost, i);
5804 kfree(vhost->scsi_scrqs.scrqs);
5805 vhost->scsi_scrqs.scrqs = NULL;
5806 vhost->scsi_scrqs.active_queues = 0;
5811 * ibmvfc_free_mem - Free memory for vhost
5812 * @vhost: ibmvfc host struct
5817 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
5819 struct ibmvfc_queue *async_q = &vhost->async_crq;
5822 mempool_destroy(vhost->tgt_pool);
5823 kfree(vhost->trace);
5824 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
5825 vhost->disc_buf_dma);
5826 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
5827 vhost->login_buf, vhost->login_buf_dma);
5828 dma_free_coherent(vhost->dev, sizeof(*vhost->channel_setup_buf),
5829 vhost->channel_setup_buf, vhost->channel_setup_dma);
5830 dma_pool_destroy(vhost->sg_pool);
5831 ibmvfc_free_queue(vhost, async_q);
5836 * ibmvfc_alloc_mem - Allocate memory for vhost
5837 * @vhost: ibmvfc host struct
5840 * 0 on success / non-zero on failure
5842 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
5844 struct ibmvfc_queue *async_q = &vhost->async_crq;
5845 struct device *dev = vhost->dev;
5848 if (ibmvfc_alloc_queue(vhost, async_q, IBMVFC_ASYNC_FMT)) {
5849 dev_err(dev, "Couldn't allocate/map async queue.\n");
5853 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
5854 SG_ALL * sizeof(struct srp_direct_buf),
5855 sizeof(struct srp_direct_buf), 0);
5857 if (!vhost->sg_pool) {
5858 dev_err(dev, "Failed to allocate sg pool\n");
5859 goto unmap_async_crq;
5862 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
5863 &vhost->login_buf_dma, GFP_KERNEL);
5865 if (!vhost->login_buf) {
5866 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
5870 vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
5871 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
5872 &vhost->disc_buf_dma, GFP_KERNEL);
5874 if (!vhost->disc_buf) {
5875 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
5876 goto free_login_buffer;
5879 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
5880 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
5881 atomic_set(&vhost->trace_index, -1);
5884 goto free_disc_buffer;
5886 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
5887 sizeof(struct ibmvfc_target));
5889 if (!vhost->tgt_pool) {
5890 dev_err(dev, "Couldn't allocate target memory pool\n");
5894 vhost->channel_setup_buf = dma_alloc_coherent(dev, sizeof(*vhost->channel_setup_buf),
5895 &vhost->channel_setup_dma,
5898 if (!vhost->channel_setup_buf) {
5899 dev_err(dev, "Couldn't allocate Channel Setup buffer\n");
5907 mempool_destroy(vhost->tgt_pool);
5909 kfree(vhost->trace);
5911 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
5912 vhost->disc_buf_dma);
5914 dma_free_coherent(dev, sizeof(*vhost->login_buf),
5915 vhost->login_buf, vhost->login_buf_dma);
5917 dma_pool_destroy(vhost->sg_pool);
5919 ibmvfc_free_queue(vhost, async_q);
5926 * ibmvfc_rport_add_thread - Worker thread for rport adds
5927 * @work: work struct
5930 static void ibmvfc_rport_add_thread(struct work_struct *work)
5932 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
5934 struct ibmvfc_target *tgt;
5935 struct fc_rport *rport;
5936 unsigned long flags;
5940 spin_lock_irqsave(vhost->host->host_lock, flags);
5943 if (vhost->state != IBMVFC_ACTIVE)
5946 list_for_each_entry(tgt, &vhost->targets, queue) {
5947 if (tgt->add_rport) {
5950 kref_get(&tgt->kref);
5953 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5954 ibmvfc_tgt_add_rport(tgt);
5955 } else if (get_device(&rport->dev)) {
5956 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5957 tgt_dbg(tgt, "Setting rport roles\n");
5958 fc_remote_port_rolechg(rport, tgt->ids.roles);
5959 put_device(&rport->dev);
5961 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5964 kref_put(&tgt->kref, ibmvfc_release_tgt);
5965 spin_lock_irqsave(vhost->host->host_lock, flags);
5971 if (vhost->state == IBMVFC_ACTIVE)
5972 vhost->scan_complete = 1;
5973 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5978 * ibmvfc_probe - Adapter hot plug add entry point
5979 * @vdev: vio device struct
5980 * @id: vio device id struct
5983 * 0 on success / non-zero on failure
5985 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
5987 struct ibmvfc_host *vhost;
5988 struct Scsi_Host *shost;
5989 struct device *dev = &vdev->dev;
5991 unsigned int max_scsi_queues = IBMVFC_MAX_SCSI_QUEUES;
5994 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
5996 dev_err(dev, "Couldn't allocate host data\n");
6000 shost->transportt = ibmvfc_transport_template;
6001 shost->can_queue = max_requests;
6002 shost->max_lun = max_lun;
6003 shost->max_id = max_targets;
6004 shost->max_sectors = IBMVFC_MAX_SECTORS;
6005 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
6006 shost->unique_id = shost->host_no;
6007 shost->nr_hw_queues = mq_enabled ? min(max_scsi_queues, nr_scsi_hw_queues) : 1;
6009 vhost = shost_priv(shost);
6010 INIT_LIST_HEAD(&vhost->targets);
6011 INIT_LIST_HEAD(&vhost->purge);
6012 sprintf(vhost->name, IBMVFC_NAME);
6013 vhost->host = shost;
6015 vhost->partition_number = -1;
6016 vhost->log_level = log_level;
6017 vhost->task_set = 1;
6019 vhost->mq_enabled = mq_enabled;
6020 vhost->client_scsi_channels = min(shost->nr_hw_queues, nr_scsi_channels);
6021 vhost->using_channels = 0;
6022 vhost->do_enquiry = 1;
6024 strcpy(vhost->partition_name, "UNKNOWN");
6025 init_waitqueue_head(&vhost->work_wait_q);
6026 init_waitqueue_head(&vhost->init_wait_q);
6027 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
6028 mutex_init(&vhost->passthru_mutex);
6030 if ((rc = ibmvfc_alloc_mem(vhost)))
6031 goto free_scsi_host;
6033 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
6036 if (IS_ERR(vhost->work_thread)) {
6037 dev_err(dev, "Couldn't create kernel thread: %ld\n",
6038 PTR_ERR(vhost->work_thread));
6039 rc = PTR_ERR(vhost->work_thread);
6043 if ((rc = ibmvfc_init_crq(vhost))) {
6044 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
6048 if ((rc = scsi_add_host(shost, dev)))
6051 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
6053 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
6054 &ibmvfc_trace_attr))) {
6055 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
6059 ibmvfc_init_sub_crqs(vhost);
6061 if (shost_to_fc_host(shost)->rqst_q)
6062 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
6063 dev_set_drvdata(dev, vhost);
6064 spin_lock(&ibmvfc_driver_lock);
6065 list_add_tail(&vhost->queue, &ibmvfc_head);
6066 spin_unlock(&ibmvfc_driver_lock);
6068 ibmvfc_send_crq_init(vhost);
6069 scsi_scan_host(shost);
6073 scsi_remove_host(shost);
6075 ibmvfc_release_crq_queue(vhost);
6077 kthread_stop(vhost->work_thread);
6079 ibmvfc_free_mem(vhost);
6081 scsi_host_put(shost);
6088 * ibmvfc_remove - Adapter hot plug remove entry point
6089 * @vdev: vio device struct
6094 static void ibmvfc_remove(struct vio_dev *vdev)
6096 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
6098 unsigned long flags;
6101 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
6103 spin_lock_irqsave(vhost->host->host_lock, flags);
6104 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
6105 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6107 ibmvfc_wait_while_resetting(vhost);
6108 kthread_stop(vhost->work_thread);
6109 fc_remove_host(vhost->host);
6110 scsi_remove_host(vhost->host);
6112 spin_lock_irqsave(vhost->host->host_lock, flags);
6113 ibmvfc_purge_requests(vhost, DID_ERROR);
6114 list_splice_init(&vhost->purge, &purge);
6115 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6116 ibmvfc_complete_purge(&purge);
6117 ibmvfc_release_sub_crqs(vhost);
6118 ibmvfc_release_crq_queue(vhost);
6120 ibmvfc_free_mem(vhost);
6121 spin_lock(&ibmvfc_driver_lock);
6122 list_del(&vhost->queue);
6123 spin_unlock(&ibmvfc_driver_lock);
6124 scsi_host_put(vhost->host);
6129 * ibmvfc_resume - Resume from suspend
6130 * @dev: device struct
6132 * We may have lost an interrupt across suspend/resume, so kick the
6136 static int ibmvfc_resume(struct device *dev)
6138 unsigned long flags;
6139 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
6140 struct vio_dev *vdev = to_vio_dev(dev);
6142 spin_lock_irqsave(vhost->host->host_lock, flags);
6143 vio_disable_interrupts(vdev);
6144 tasklet_schedule(&vhost->tasklet);
6145 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6150 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
6151 * @vdev: vio device struct
6154 * Number of bytes the driver will need to DMA map at the same time in
6155 * order to perform well.
6157 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
6159 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
6160 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
6163 static const struct vio_device_id ibmvfc_device_table[] = {
6164 {"fcp", "IBM,vfc-client"},
6167 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
6169 static const struct dev_pm_ops ibmvfc_pm_ops = {
6170 .resume = ibmvfc_resume
6173 static struct vio_driver ibmvfc_driver = {
6174 .id_table = ibmvfc_device_table,
6175 .probe = ibmvfc_probe,
6176 .remove = ibmvfc_remove,
6177 .get_desired_dma = ibmvfc_get_desired_dma,
6178 .name = IBMVFC_NAME,
6179 .pm = &ibmvfc_pm_ops,
6182 static struct fc_function_template ibmvfc_transport_functions = {
6183 .show_host_fabric_name = 1,
6184 .show_host_node_name = 1,
6185 .show_host_port_name = 1,
6186 .show_host_supported_classes = 1,
6187 .show_host_port_type = 1,
6188 .show_host_port_id = 1,
6189 .show_host_maxframe_size = 1,
6191 .get_host_port_state = ibmvfc_get_host_port_state,
6192 .show_host_port_state = 1,
6194 .get_host_speed = ibmvfc_get_host_speed,
6195 .show_host_speed = 1,
6197 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
6198 .terminate_rport_io = ibmvfc_terminate_rport_io,
6200 .show_rport_maxframe_size = 1,
6201 .show_rport_supported_classes = 1,
6203 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
6204 .show_rport_dev_loss_tmo = 1,
6206 .get_starget_node_name = ibmvfc_get_starget_node_name,
6207 .show_starget_node_name = 1,
6209 .get_starget_port_name = ibmvfc_get_starget_port_name,
6210 .show_starget_port_name = 1,
6212 .get_starget_port_id = ibmvfc_get_starget_port_id,
6213 .show_starget_port_id = 1,
6215 .bsg_request = ibmvfc_bsg_request,
6216 .bsg_timeout = ibmvfc_bsg_timeout,
6220 * ibmvfc_module_init - Initialize the ibmvfc module
6223 * 0 on success / other on failure
6225 static int __init ibmvfc_module_init(void)
6229 if (!firmware_has_feature(FW_FEATURE_VIO))
6232 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
6233 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
6235 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
6236 if (!ibmvfc_transport_template)
6239 rc = vio_register_driver(&ibmvfc_driver);
6241 fc_release_transport(ibmvfc_transport_template);
6246 * ibmvfc_module_exit - Teardown the ibmvfc module
6251 static void __exit ibmvfc_module_exit(void)
6253 vio_unregister_driver(&ibmvfc_driver);
6254 fc_release_transport(ibmvfc_transport_template);
6257 module_init(ibmvfc_module_init);
6258 module_exit(ibmvfc_module_exit);