1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
5 * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
7 * Copyright (C) IBM Corporation, 2008
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmapool.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqdomain.h>
17 #include <linux/kthread.h>
18 #include <linux/slab.h>
21 #include <linux/stringify.h>
22 #include <linux/bsg-lib.h>
23 #include <asm/firmware.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_tcq.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/scsi_bsg_fc.h>
36 static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
37 static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
38 static u64 max_lun = IBMVFC_MAX_LUN;
39 static unsigned int max_targets = IBMVFC_MAX_TARGETS;
40 static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
41 static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
42 static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
43 static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
44 static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
45 static unsigned int mq_enabled = IBMVFC_MQ;
46 static unsigned int nr_scsi_hw_queues = IBMVFC_SCSI_HW_QUEUES;
47 static unsigned int nr_scsi_channels = IBMVFC_SCSI_CHANNELS;
48 static unsigned int mig_channels_only = IBMVFC_MIG_NO_SUB_TO_CRQ;
49 static unsigned int mig_no_less_channels = IBMVFC_MIG_NO_N_TO_M;
51 static LIST_HEAD(ibmvfc_head);
52 static DEFINE_SPINLOCK(ibmvfc_driver_lock);
53 static struct scsi_transport_template *ibmvfc_transport_template;
55 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
56 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(IBMVFC_DRIVER_VERSION);
60 module_param_named(mq, mq_enabled, uint, S_IRUGO);
61 MODULE_PARM_DESC(mq, "Enable multiqueue support. "
62 "[Default=" __stringify(IBMVFC_MQ) "]");
63 module_param_named(scsi_host_queues, nr_scsi_hw_queues, uint, S_IRUGO);
64 MODULE_PARM_DESC(scsi_host_queues, "Number of SCSI Host submission queues. "
65 "[Default=" __stringify(IBMVFC_SCSI_HW_QUEUES) "]");
66 module_param_named(scsi_hw_channels, nr_scsi_channels, uint, S_IRUGO);
67 MODULE_PARM_DESC(scsi_hw_channels, "Number of hw scsi channels to request. "
68 "[Default=" __stringify(IBMVFC_SCSI_CHANNELS) "]");
69 module_param_named(mig_channels_only, mig_channels_only, uint, S_IRUGO);
70 MODULE_PARM_DESC(mig_channels_only, "Prevent migration to non-channelized system. "
71 "[Default=" __stringify(IBMVFC_MIG_NO_SUB_TO_CRQ) "]");
72 module_param_named(mig_no_less_channels, mig_no_less_channels, uint, S_IRUGO);
73 MODULE_PARM_DESC(mig_no_less_channels, "Prevent migration to system with less channels. "
74 "[Default=" __stringify(IBMVFC_MIG_NO_N_TO_M) "]");
76 module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
77 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
78 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
79 module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(default_timeout,
81 "Default timeout in seconds for initialization and EH commands. "
82 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
83 module_param_named(max_requests, max_requests, uint, S_IRUGO);
84 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
85 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
86 module_param_named(max_lun, max_lun, ullong, S_IRUGO);
87 MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
88 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
89 module_param_named(max_targets, max_targets, uint, S_IRUGO);
90 MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
91 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
92 module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
93 MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
94 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
95 module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
96 MODULE_PARM_DESC(debug, "Enable driver debug information. "
97 "[Default=" __stringify(IBMVFC_DEBUG) "]");
98 module_param_named(log_level, log_level, uint, 0);
99 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
100 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
101 module_param_named(cls3_error, cls3_error, uint, 0);
102 MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
103 "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
105 static const struct {
113 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
114 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
115 { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
116 { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
117 { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
118 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
119 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
120 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
121 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
122 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
123 { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
124 { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
125 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
126 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
128 { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
129 { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
130 { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
131 { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
132 { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
133 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
134 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
135 { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
136 { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
137 { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
139 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
140 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
141 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
142 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
143 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
144 { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
145 { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
146 { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
147 { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
148 { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
149 { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
151 { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
152 { IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." },
155 static void ibmvfc_npiv_login(struct ibmvfc_host *);
156 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
157 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
158 static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
159 static void ibmvfc_npiv_logout(struct ibmvfc_host *);
160 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *);
161 static void ibmvfc_tgt_move_login(struct ibmvfc_target *);
163 static void ibmvfc_release_sub_crqs(struct ibmvfc_host *);
164 static void ibmvfc_init_sub_crqs(struct ibmvfc_host *);
166 static const char *unknown_error = "unknown error";
168 static long h_reg_sub_crq(unsigned long unit_address, unsigned long ioba,
169 unsigned long length, unsigned long *cookie,
172 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
175 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, ioba, length);
182 static int ibmvfc_check_caps(struct ibmvfc_host *vhost, unsigned long cap_flags)
184 u64 host_caps = be64_to_cpu(vhost->login_buf->resp.capabilities);
186 return (host_caps & cap_flags) ? 1 : 0;
189 static struct ibmvfc_fcp_cmd_iu *ibmvfc_get_fcp_iu(struct ibmvfc_host *vhost,
190 struct ibmvfc_cmd *vfc_cmd)
192 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
193 return &vfc_cmd->v2.iu;
195 return &vfc_cmd->v1.iu;
198 static struct ibmvfc_fcp_rsp *ibmvfc_get_fcp_rsp(struct ibmvfc_host *vhost,
199 struct ibmvfc_cmd *vfc_cmd)
201 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
202 return &vfc_cmd->v2.rsp;
204 return &vfc_cmd->v1.rsp;
207 #ifdef CONFIG_SCSI_IBMVFC_TRACE
209 * ibmvfc_trc_start - Log a start trace entry
210 * @evt: ibmvfc event struct
213 static void ibmvfc_trc_start(struct ibmvfc_event *evt)
215 struct ibmvfc_host *vhost = evt->vhost;
216 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
217 struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
218 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
219 struct ibmvfc_trace_entry *entry;
220 int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
222 entry = &vhost->trace[index];
224 entry->time = jiffies;
225 entry->fmt = evt->crq.format;
226 entry->type = IBMVFC_TRC_START;
228 switch (entry->fmt) {
229 case IBMVFC_CMD_FORMAT:
230 entry->op_code = iu->cdb[0];
231 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
232 entry->lun = scsilun_to_int(&iu->lun);
233 entry->tmf_flags = iu->tmf_flags;
234 entry->u.start.xfer_len = be32_to_cpu(iu->xfer_len);
236 case IBMVFC_MAD_FORMAT:
237 entry->op_code = be32_to_cpu(mad->opcode);
245 * ibmvfc_trc_end - Log an end trace entry
246 * @evt: ibmvfc event struct
249 static void ibmvfc_trc_end(struct ibmvfc_event *evt)
251 struct ibmvfc_host *vhost = evt->vhost;
252 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
253 struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
254 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
255 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
256 struct ibmvfc_trace_entry *entry;
257 int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
259 entry = &vhost->trace[index];
261 entry->time = jiffies;
262 entry->fmt = evt->crq.format;
263 entry->type = IBMVFC_TRC_END;
265 switch (entry->fmt) {
266 case IBMVFC_CMD_FORMAT:
267 entry->op_code = iu->cdb[0];
268 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
269 entry->lun = scsilun_to_int(&iu->lun);
270 entry->tmf_flags = iu->tmf_flags;
271 entry->u.end.status = be16_to_cpu(vfc_cmd->status);
272 entry->u.end.error = be16_to_cpu(vfc_cmd->error);
273 entry->u.end.fcp_rsp_flags = rsp->flags;
274 entry->u.end.rsp_code = rsp->data.info.rsp_code;
275 entry->u.end.scsi_status = rsp->scsi_status;
277 case IBMVFC_MAD_FORMAT:
278 entry->op_code = be32_to_cpu(mad->opcode);
279 entry->u.end.status = be16_to_cpu(mad->status);
288 #define ibmvfc_trc_start(evt) do { } while (0)
289 #define ibmvfc_trc_end(evt) do { } while (0)
293 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
294 * @status: status / error class
298 * index into cmd_status / -EINVAL on failure
300 static int ibmvfc_get_err_index(u16 status, u16 error)
304 for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
305 if ((cmd_status[i].status & status) == cmd_status[i].status &&
306 cmd_status[i].error == error)
313 * ibmvfc_get_cmd_error - Find the error description for the fcp response
314 * @status: status / error class
318 * error description string
320 static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
322 int rc = ibmvfc_get_err_index(status, error);
324 return cmd_status[rc].name;
325 return unknown_error;
329 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
330 * @vhost: ibmvfc host struct
331 * @vfc_cmd: ibmvfc command struct
334 * SCSI result value to return for completed command
336 static int ibmvfc_get_err_result(struct ibmvfc_host *vhost, struct ibmvfc_cmd *vfc_cmd)
339 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
340 int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
342 if ((rsp->flags & FCP_RSP_LEN_VALID) &&
343 ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
344 rsp->data.info.rsp_code))
345 return DID_ERROR << 16;
347 err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
349 return rsp->scsi_status | (cmd_status[err].result << 16);
350 return rsp->scsi_status | (DID_ERROR << 16);
354 * ibmvfc_retry_cmd - Determine if error status is retryable
355 * @status: status / error class
359 * 1 if error should be retried / 0 if it should not
361 static int ibmvfc_retry_cmd(u16 status, u16 error)
363 int rc = ibmvfc_get_err_index(status, error);
366 return cmd_status[rc].retry;
370 static const char *unknown_fc_explain = "unknown fc explain";
372 static const struct {
376 { 0x00, "no additional explanation" },
377 { 0x01, "service parameter error - options" },
378 { 0x03, "service parameter error - initiator control" },
379 { 0x05, "service parameter error - recipient control" },
380 { 0x07, "service parameter error - received data field size" },
381 { 0x09, "service parameter error - concurrent seq" },
382 { 0x0B, "service parameter error - credit" },
383 { 0x0D, "invalid N_Port/F_Port_Name" },
384 { 0x0E, "invalid node/Fabric Name" },
385 { 0x0F, "invalid common service parameters" },
386 { 0x11, "invalid association header" },
387 { 0x13, "association header required" },
388 { 0x15, "invalid originator S_ID" },
389 { 0x17, "invalid OX_ID-RX-ID combination" },
390 { 0x19, "command (request) already in progress" },
391 { 0x1E, "N_Port Login requested" },
392 { 0x1F, "Invalid N_Port_ID" },
395 static const struct {
399 { 0x00, "no additional explanation" },
400 { 0x01, "port identifier not registered" },
401 { 0x02, "port name not registered" },
402 { 0x03, "node name not registered" },
403 { 0x04, "class of service not registered" },
404 { 0x06, "initial process associator not registered" },
405 { 0x07, "FC-4 TYPEs not registered" },
406 { 0x08, "symbolic port name not registered" },
407 { 0x09, "symbolic node name not registered" },
408 { 0x0A, "port type not registered" },
409 { 0xF0, "authorization exception" },
410 { 0xF1, "authentication exception" },
411 { 0xF2, "data base full" },
412 { 0xF3, "data base empty" },
413 { 0xF4, "processing request" },
414 { 0xF5, "unable to verify connection" },
415 { 0xF6, "devices not in a common zone" },
419 * ibmvfc_get_ls_explain - Return the FC Explain description text
420 * @status: FC Explain status
425 static const char *ibmvfc_get_ls_explain(u16 status)
429 for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
430 if (ls_explain[i].fc_explain == status)
431 return ls_explain[i].name;
433 return unknown_fc_explain;
437 * ibmvfc_get_gs_explain - Return the FC Explain description text
438 * @status: FC Explain status
443 static const char *ibmvfc_get_gs_explain(u16 status)
447 for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
448 if (gs_explain[i].fc_explain == status)
449 return gs_explain[i].name;
451 return unknown_fc_explain;
454 static const struct {
455 enum ibmvfc_fc_type fc_type;
458 { IBMVFC_FABRIC_REJECT, "fabric reject" },
459 { IBMVFC_PORT_REJECT, "port reject" },
460 { IBMVFC_LS_REJECT, "ELS reject" },
461 { IBMVFC_FABRIC_BUSY, "fabric busy" },
462 { IBMVFC_PORT_BUSY, "port busy" },
463 { IBMVFC_BASIC_REJECT, "basic reject" },
466 static const char *unknown_fc_type = "unknown fc type";
469 * ibmvfc_get_fc_type - Return the FC Type description text
470 * @status: FC Type error status
475 static const char *ibmvfc_get_fc_type(u16 status)
479 for (i = 0; i < ARRAY_SIZE(fc_type); i++)
480 if (fc_type[i].fc_type == status)
481 return fc_type[i].name;
483 return unknown_fc_type;
487 * ibmvfc_set_tgt_action - Set the next init action for the target
488 * @tgt: ibmvfc target struct
489 * @action: action to perform
492 * 0 if action changed / non-zero if not changed
494 static int ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
495 enum ibmvfc_target_action action)
499 switch (tgt->action) {
500 case IBMVFC_TGT_ACTION_LOGOUT_RPORT:
501 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT ||
502 action == IBMVFC_TGT_ACTION_DEL_RPORT) {
503 tgt->action = action;
507 case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT:
508 if (action == IBMVFC_TGT_ACTION_DEL_RPORT ||
509 action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
510 tgt->action = action;
514 case IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT:
515 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
516 tgt->action = action;
520 case IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT:
521 if (action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
522 tgt->action = action;
526 case IBMVFC_TGT_ACTION_DEL_RPORT:
527 if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
528 tgt->action = action;
532 case IBMVFC_TGT_ACTION_DELETED_RPORT:
535 tgt->action = action;
540 if (action >= IBMVFC_TGT_ACTION_LOGOUT_RPORT)
547 * ibmvfc_set_host_state - Set the state for the host
548 * @vhost: ibmvfc host struct
549 * @state: state to set host to
552 * 0 if state changed / non-zero if not changed
554 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
555 enum ibmvfc_host_state state)
559 switch (vhost->state) {
560 case IBMVFC_HOST_OFFLINE:
564 vhost->state = state;
572 * ibmvfc_set_host_action - Set the next init action for the host
573 * @vhost: ibmvfc host struct
574 * @action: action to perform
577 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
578 enum ibmvfc_host_action action)
581 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
582 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
583 vhost->action = action;
585 case IBMVFC_HOST_ACTION_LOGO_WAIT:
586 if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
587 vhost->action = action;
589 case IBMVFC_HOST_ACTION_INIT_WAIT:
590 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
591 vhost->action = action;
593 case IBMVFC_HOST_ACTION_QUERY:
594 switch (vhost->action) {
595 case IBMVFC_HOST_ACTION_INIT_WAIT:
596 case IBMVFC_HOST_ACTION_NONE:
597 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
598 vhost->action = action;
604 case IBMVFC_HOST_ACTION_TGT_INIT:
605 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
606 vhost->action = action;
608 case IBMVFC_HOST_ACTION_REENABLE:
609 case IBMVFC_HOST_ACTION_RESET:
610 vhost->action = action;
612 case IBMVFC_HOST_ACTION_INIT:
613 case IBMVFC_HOST_ACTION_TGT_DEL:
614 case IBMVFC_HOST_ACTION_LOGO:
615 case IBMVFC_HOST_ACTION_QUERY_TGTS:
616 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
617 case IBMVFC_HOST_ACTION_NONE:
619 switch (vhost->action) {
620 case IBMVFC_HOST_ACTION_RESET:
621 case IBMVFC_HOST_ACTION_REENABLE:
624 vhost->action = action;
632 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
633 * @vhost: ibmvfc host struct
638 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
640 if (vhost->action == IBMVFC_HOST_ACTION_NONE &&
641 vhost->state == IBMVFC_ACTIVE) {
642 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
643 scsi_block_requests(vhost->host);
644 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
649 wake_up(&vhost->work_wait_q);
653 * ibmvfc_del_tgt - Schedule cleanup and removal of the target
654 * @tgt: ibmvfc target struct
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 tgt->init_retries = 0;
662 wake_up(&tgt->vhost->work_wait_q);
666 * ibmvfc_link_down - Handle a link down event from the adapter
667 * @vhost: ibmvfc host struct
668 * @state: ibmvfc host state to enter
671 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
672 enum ibmvfc_host_state state)
674 struct ibmvfc_target *tgt;
677 scsi_block_requests(vhost->host);
678 list_for_each_entry(tgt, &vhost->targets, queue)
680 ibmvfc_set_host_state(vhost, state);
681 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
682 vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
683 wake_up(&vhost->work_wait_q);
688 * ibmvfc_init_host - Start host initialization
689 * @vhost: ibmvfc host struct
694 static void ibmvfc_init_host(struct ibmvfc_host *vhost)
696 struct ibmvfc_target *tgt;
698 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
699 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
701 "Host initialization retries exceeded. Taking adapter offline\n");
702 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
707 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
708 memset(vhost->async_crq.msgs.async, 0, PAGE_SIZE);
709 vhost->async_crq.cur = 0;
711 list_for_each_entry(tgt, &vhost->targets, queue)
713 scsi_block_requests(vhost->host);
714 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
715 vhost->job_step = ibmvfc_npiv_login;
716 wake_up(&vhost->work_wait_q);
721 * ibmvfc_send_crq - Send a CRQ
722 * @vhost: ibmvfc host struct
723 * @word1: the first 64 bits of the data
724 * @word2: the second 64 bits of the data
727 * 0 on success / other on failure
729 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
731 struct vio_dev *vdev = to_vio_dev(vhost->dev);
732 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
735 static int ibmvfc_send_sub_crq(struct ibmvfc_host *vhost, u64 cookie, u64 word1,
736 u64 word2, u64 word3, u64 word4)
738 struct vio_dev *vdev = to_vio_dev(vhost->dev);
740 return plpar_hcall_norets(H_SEND_SUB_CRQ, vdev->unit_address, cookie,
741 word1, word2, word3, word4);
745 * ibmvfc_send_crq_init - Send a CRQ init message
746 * @vhost: ibmvfc host struct
749 * 0 on success / other on failure
751 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
753 ibmvfc_dbg(vhost, "Sending CRQ init\n");
754 return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
758 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
759 * @vhost: ibmvfc host struct
762 * 0 on success / other on failure
764 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
766 ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
767 return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
771 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
772 * @vhost: ibmvfc host who owns the event pool
773 * @queue: ibmvfc queue struct
776 * Returns zero on success.
778 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost,
779 struct ibmvfc_queue *queue,
783 struct ibmvfc_event_pool *pool = &queue->evt_pool;
790 pool->events = kcalloc(size, sizeof(*pool->events), GFP_KERNEL);
794 pool->iu_storage = dma_alloc_coherent(vhost->dev,
795 size * sizeof(*pool->iu_storage),
798 if (!pool->iu_storage) {
803 INIT_LIST_HEAD(&queue->sent);
804 INIT_LIST_HEAD(&queue->free);
805 spin_lock_init(&queue->l_lock);
807 for (i = 0; i < size; ++i) {
808 struct ibmvfc_event *evt = &pool->events[i];
810 atomic_set(&evt->free, 1);
811 evt->crq.valid = 0x80;
812 evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
813 evt->xfer_iu = pool->iu_storage + i;
816 evt->ext_list = NULL;
817 list_add_tail(&evt->queue_list, &queue->free);
825 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
826 * @vhost: ibmvfc host who owns the event pool
827 * @queue: ibmvfc queue struct
830 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost,
831 struct ibmvfc_queue *queue)
834 struct ibmvfc_event_pool *pool = &queue->evt_pool;
837 for (i = 0; i < pool->size; ++i) {
838 list_del(&pool->events[i].queue_list);
839 BUG_ON(atomic_read(&pool->events[i].free) != 1);
840 if (pool->events[i].ext_list)
841 dma_pool_free(vhost->sg_pool,
842 pool->events[i].ext_list,
843 pool->events[i].ext_list_token);
847 dma_free_coherent(vhost->dev,
848 pool->size * sizeof(*pool->iu_storage),
849 pool->iu_storage, pool->iu_token);
854 * ibmvfc_free_queue - Deallocate queue
855 * @vhost: ibmvfc host struct
856 * @queue: ibmvfc queue struct
858 * Unmaps dma and deallocates page for messages
860 static void ibmvfc_free_queue(struct ibmvfc_host *vhost,
861 struct ibmvfc_queue *queue)
863 struct device *dev = vhost->dev;
865 dma_unmap_single(dev, queue->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
866 free_page((unsigned long)queue->msgs.handle);
867 queue->msgs.handle = NULL;
869 ibmvfc_free_event_pool(vhost, queue);
873 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
874 * @vhost: ibmvfc host struct
876 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
877 * the crq with the hypervisor.
879 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
882 struct vio_dev *vdev = to_vio_dev(vhost->dev);
883 struct ibmvfc_queue *crq = &vhost->crq;
885 ibmvfc_dbg(vhost, "Releasing CRQ\n");
886 free_irq(vdev->irq, vhost);
887 tasklet_kill(&vhost->tasklet);
891 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
892 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
894 vhost->state = IBMVFC_NO_CRQ;
895 vhost->logged_in = 0;
897 ibmvfc_free_queue(vhost, crq);
901 * ibmvfc_reenable_crq_queue - reenables the CRQ
902 * @vhost: ibmvfc host struct
905 * 0 on success / other on failure
907 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
910 struct vio_dev *vdev = to_vio_dev(vhost->dev);
913 ibmvfc_release_sub_crqs(vhost);
915 /* Re-enable the CRQ */
919 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
920 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
923 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
925 spin_lock_irqsave(vhost->host->host_lock, flags);
926 spin_lock(vhost->crq.q_lock);
927 vhost->do_enquiry = 1;
928 vhost->using_channels = 0;
929 spin_unlock(vhost->crq.q_lock);
930 spin_unlock_irqrestore(vhost->host->host_lock, flags);
932 ibmvfc_init_sub_crqs(vhost);
938 * ibmvfc_reset_crq - resets a crq after a failure
939 * @vhost: ibmvfc host struct
942 * 0 on success / other on failure
944 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
948 struct vio_dev *vdev = to_vio_dev(vhost->dev);
949 struct ibmvfc_queue *crq = &vhost->crq;
951 ibmvfc_release_sub_crqs(vhost);
957 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
958 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
960 spin_lock_irqsave(vhost->host->host_lock, flags);
961 spin_lock(vhost->crq.q_lock);
962 vhost->state = IBMVFC_NO_CRQ;
963 vhost->logged_in = 0;
964 vhost->do_enquiry = 1;
965 vhost->using_channels = 0;
967 /* Clean out the queue */
968 memset(crq->msgs.crq, 0, PAGE_SIZE);
971 /* And re-open it again */
972 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
973 crq->msg_token, PAGE_SIZE);
976 /* Adapter is good, but other end is not ready */
977 dev_warn(vhost->dev, "Partner adapter not ready\n");
979 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
981 spin_unlock(vhost->crq.q_lock);
982 spin_unlock_irqrestore(vhost->host->host_lock, flags);
984 ibmvfc_init_sub_crqs(vhost);
990 * ibmvfc_valid_event - Determines if event is valid.
991 * @pool: event_pool that contains the event
992 * @evt: ibmvfc event to be checked for validity
995 * 1 if event is valid / 0 if event is not valid
997 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
998 struct ibmvfc_event *evt)
1000 int index = evt - pool->events;
1001 if (index < 0 || index >= pool->size) /* outside of bounds */
1003 if (evt != pool->events + index) /* unaligned */
1009 * ibmvfc_free_event - Free the specified event
1010 * @evt: ibmvfc_event to be freed
1013 static void ibmvfc_free_event(struct ibmvfc_event *evt)
1015 struct ibmvfc_event_pool *pool = &evt->queue->evt_pool;
1016 unsigned long flags;
1018 BUG_ON(!ibmvfc_valid_event(pool, evt));
1019 BUG_ON(atomic_inc_return(&evt->free) != 1);
1021 spin_lock_irqsave(&evt->queue->l_lock, flags);
1022 list_add_tail(&evt->queue_list, &evt->queue->free);
1024 complete(evt->eh_comp);
1025 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1029 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
1030 * @evt: ibmvfc event struct
1032 * This function does not setup any error status, that must be done
1033 * before this function gets called.
1035 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
1037 struct scsi_cmnd *cmnd = evt->cmnd;
1040 scsi_dma_unmap(cmnd);
1041 cmnd->scsi_done(cmnd);
1044 ibmvfc_free_event(evt);
1048 * ibmvfc_complete_purge - Complete failed command list
1049 * @purge_list: list head of failed commands
1051 * This function runs completions on commands to fail as a result of a
1052 * host reset or platform migration.
1054 static void ibmvfc_complete_purge(struct list_head *purge_list)
1056 struct ibmvfc_event *evt, *pos;
1058 list_for_each_entry_safe(evt, pos, purge_list, queue_list) {
1059 list_del(&evt->queue_list);
1060 ibmvfc_trc_end(evt);
1066 * ibmvfc_fail_request - Fail request with specified error code
1067 * @evt: ibmvfc event struct
1068 * @error_code: error code to fail request with
1073 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
1076 evt->cmnd->result = (error_code << 16);
1077 evt->done = ibmvfc_scsi_eh_done;
1079 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
1081 del_timer(&evt->timer);
1085 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
1086 * @vhost: ibmvfc host struct
1087 * @error_code: error code to fail requests with
1092 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
1094 struct ibmvfc_event *evt, *pos;
1095 struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
1096 unsigned long flags;
1100 if (vhost->using_channels)
1101 hwqs = vhost->scsi_scrqs.active_queues;
1103 ibmvfc_dbg(vhost, "Purging all requests\n");
1104 spin_lock_irqsave(&vhost->crq.l_lock, flags);
1105 list_for_each_entry_safe(evt, pos, &vhost->crq.sent, queue_list)
1106 ibmvfc_fail_request(evt, error_code);
1107 list_splice_init(&vhost->crq.sent, &vhost->purge);
1108 spin_unlock_irqrestore(&vhost->crq.l_lock, flags);
1110 for (i = 0; i < hwqs; i++) {
1111 spin_lock_irqsave(queues[i].q_lock, flags);
1112 spin_lock(&queues[i].l_lock);
1113 list_for_each_entry_safe(evt, pos, &queues[i].sent, queue_list)
1114 ibmvfc_fail_request(evt, error_code);
1115 list_splice_init(&queues[i].sent, &vhost->purge);
1116 spin_unlock(&queues[i].l_lock);
1117 spin_unlock_irqrestore(queues[i].q_lock, flags);
1122 * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
1123 * @vhost: struct ibmvfc host to reset
1125 static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
1127 ibmvfc_purge_requests(vhost, DID_ERROR);
1128 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
1129 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
1133 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
1134 * @vhost: struct ibmvfc host to reset
1136 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
1138 if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
1139 !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
1140 scsi_block_requests(vhost->host);
1141 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
1142 vhost->job_step = ibmvfc_npiv_logout;
1143 wake_up(&vhost->work_wait_q);
1145 ibmvfc_hard_reset_host(vhost);
1149 * ibmvfc_reset_host - Reset the connection to the server
1150 * @vhost: ibmvfc host struct
1152 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
1154 unsigned long flags;
1156 spin_lock_irqsave(vhost->host->host_lock, flags);
1157 __ibmvfc_reset_host(vhost);
1158 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1162 * ibmvfc_retry_host_init - Retry host initialization if allowed
1163 * @vhost: ibmvfc host struct
1165 * Returns: 1 if init will be retried / 0 if not
1168 static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
1172 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
1173 vhost->delay_init = 1;
1174 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
1176 "Host initialization retries exceeded. Taking adapter offline\n");
1177 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
1178 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
1179 __ibmvfc_reset_host(vhost);
1181 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
1186 wake_up(&vhost->work_wait_q);
1191 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
1192 * @starget: scsi target struct
1195 * ibmvfc_target struct / NULL if not found
1197 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
1199 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1200 struct ibmvfc_host *vhost = shost_priv(shost);
1201 struct ibmvfc_target *tgt;
1203 list_for_each_entry(tgt, &vhost->targets, queue)
1204 if (tgt->target_id == starget->id) {
1205 kref_get(&tgt->kref);
1212 * ibmvfc_get_target - Find the specified scsi_target
1213 * @starget: scsi target struct
1216 * ibmvfc_target struct / NULL if not found
1218 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
1220 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1221 struct ibmvfc_target *tgt;
1222 unsigned long flags;
1224 spin_lock_irqsave(shost->host_lock, flags);
1225 tgt = __ibmvfc_get_target(starget);
1226 spin_unlock_irqrestore(shost->host_lock, flags);
1231 * ibmvfc_get_host_speed - Get host port speed
1232 * @shost: scsi host struct
1237 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
1239 struct ibmvfc_host *vhost = shost_priv(shost);
1240 unsigned long flags;
1242 spin_lock_irqsave(shost->host_lock, flags);
1243 if (vhost->state == IBMVFC_ACTIVE) {
1244 switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
1246 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1249 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1252 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1255 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
1258 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
1261 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
1264 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
1265 be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
1266 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1270 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1271 spin_unlock_irqrestore(shost->host_lock, flags);
1275 * ibmvfc_get_host_port_state - Get host port state
1276 * @shost: scsi host struct
1281 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
1283 struct ibmvfc_host *vhost = shost_priv(shost);
1284 unsigned long flags;
1286 spin_lock_irqsave(shost->host_lock, flags);
1287 switch (vhost->state) {
1288 case IBMVFC_INITIALIZING:
1290 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1292 case IBMVFC_LINK_DOWN:
1293 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1295 case IBMVFC_LINK_DEAD:
1296 case IBMVFC_HOST_OFFLINE:
1297 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1300 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1303 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1306 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
1307 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1310 spin_unlock_irqrestore(shost->host_lock, flags);
1314 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1315 * @rport: rport struct
1316 * @timeout: timeout value
1321 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1324 rport->dev_loss_tmo = timeout;
1326 rport->dev_loss_tmo = 1;
1330 * ibmvfc_release_tgt - Free memory allocated for a target
1331 * @kref: kref struct
1334 static void ibmvfc_release_tgt(struct kref *kref)
1336 struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1341 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1342 * @starget: scsi target struct
1347 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1349 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1350 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1352 kref_put(&tgt->kref, ibmvfc_release_tgt);
1356 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1357 * @starget: scsi target struct
1362 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1364 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1365 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1367 kref_put(&tgt->kref, ibmvfc_release_tgt);
1371 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1372 * @starget: scsi target struct
1377 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1379 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1380 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1382 kref_put(&tgt->kref, ibmvfc_release_tgt);
1386 * ibmvfc_wait_while_resetting - Wait while the host resets
1387 * @vhost: ibmvfc host struct
1390 * 0 on success / other on failure
1392 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1394 long timeout = wait_event_timeout(vhost->init_wait_q,
1395 ((vhost->state == IBMVFC_ACTIVE ||
1396 vhost->state == IBMVFC_HOST_OFFLINE ||
1397 vhost->state == IBMVFC_LINK_DEAD) &&
1398 vhost->action == IBMVFC_HOST_ACTION_NONE),
1399 (init_timeout * HZ));
1401 return timeout ? 0 : -EIO;
1405 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1406 * @shost: scsi host struct
1409 * 0 on success / other on failure
1411 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1413 struct ibmvfc_host *vhost = shost_priv(shost);
1415 dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1416 ibmvfc_reset_host(vhost);
1417 return ibmvfc_wait_while_resetting(vhost);
1421 * ibmvfc_gather_partition_info - Gather info about the LPAR
1422 * @vhost: ibmvfc host struct
1427 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1429 struct device_node *rootdn;
1431 const unsigned int *num;
1433 rootdn = of_find_node_by_path("/");
1437 name = of_get_property(rootdn, "ibm,partition-name", NULL);
1439 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1440 num = of_get_property(rootdn, "ibm,partition-no", NULL);
1442 vhost->partition_number = *num;
1443 of_node_put(rootdn);
1447 * ibmvfc_set_login_info - Setup info for NPIV login
1448 * @vhost: ibmvfc host struct
1453 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1455 struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1456 struct ibmvfc_queue *async_crq = &vhost->async_crq;
1457 struct device_node *of_node = vhost->dev->of_node;
1458 const char *location;
1460 memset(login_info, 0, sizeof(*login_info));
1462 login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
1463 login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
1464 login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
1465 login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
1466 login_info->partition_num = cpu_to_be32(vhost->partition_number);
1467 login_info->vfc_frame_version = cpu_to_be32(1);
1468 login_info->fcp_version = cpu_to_be16(3);
1469 login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
1470 if (vhost->client_migrated)
1471 login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
1473 login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
1474 login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE | IBMVFC_CAN_SEND_VF_WWPN);
1476 if (vhost->mq_enabled || vhost->using_channels)
1477 login_info->capabilities |= cpu_to_be64(IBMVFC_CAN_USE_CHANNELS);
1479 login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
1480 login_info->async.len = cpu_to_be32(async_crq->size *
1481 sizeof(*async_crq->msgs.async));
1482 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1483 strncpy(login_info->device_name,
1484 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
1486 location = of_get_property(of_node, "ibm,loc-code", NULL);
1487 location = location ? location : dev_name(vhost->dev);
1488 strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1492 * ibmvfc_get_event - Gets the next free event in pool
1493 * @queue: ibmvfc queue struct
1495 * Returns a free event from the pool.
1497 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_queue *queue)
1499 struct ibmvfc_event *evt;
1500 unsigned long flags;
1502 spin_lock_irqsave(&queue->l_lock, flags);
1503 BUG_ON(list_empty(&queue->free));
1504 evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
1505 atomic_set(&evt->free, 0);
1506 list_del(&evt->queue_list);
1507 spin_unlock_irqrestore(&queue->l_lock, flags);
1512 * ibmvfc_locked_done - Calls evt completion with host_lock held
1513 * @evt: ibmvfc evt to complete
1515 * All non-scsi command completion callbacks have the expectation that the
1516 * host_lock is held. This callback is used by ibmvfc_init_event to wrap a
1517 * MAD evt with the host_lock.
1519 static void ibmvfc_locked_done(struct ibmvfc_event *evt)
1521 unsigned long flags;
1523 spin_lock_irqsave(evt->vhost->host->host_lock, flags);
1525 spin_unlock_irqrestore(evt->vhost->host->host_lock, flags);
1529 * ibmvfc_init_event - Initialize fields in an event struct that are always
1532 * @done: Routine to call when the event is responded to
1533 * @format: SRP or MAD format
1535 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1536 void (*done) (struct ibmvfc_event *), u8 format)
1539 evt->sync_iu = NULL;
1540 evt->eh_comp = NULL;
1541 evt->crq.format = format;
1542 if (format == IBMVFC_CMD_FORMAT)
1546 evt->done = ibmvfc_locked_done;
1552 * ibmvfc_map_sg_list - Initialize scatterlist
1553 * @scmd: scsi command struct
1554 * @nseg: number of scatterlist segments
1555 * @md: memory descriptor list to initialize
1557 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1558 struct srp_direct_buf *md)
1561 struct scatterlist *sg;
1563 scsi_for_each_sg(scmd, sg, nseg, i) {
1564 md[i].va = cpu_to_be64(sg_dma_address(sg));
1565 md[i].len = cpu_to_be32(sg_dma_len(sg));
1571 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
1572 * @scmd: struct scsi_cmnd with the scatterlist
1573 * @evt: ibmvfc event struct
1574 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1575 * @dev: device for which to map dma memory
1578 * 0 on success / non-zero on failure
1580 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1581 struct ibmvfc_event *evt,
1582 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1586 struct srp_direct_buf *data = &vfc_cmd->ioba;
1587 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1588 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(evt->vhost, vfc_cmd);
1591 vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1593 sg_mapped = scsi_dma_map(scmd);
1595 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1597 } else if (unlikely(sg_mapped < 0)) {
1598 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1599 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1603 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1604 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1605 iu->add_cdb_len |= IBMVFC_WRDATA;
1607 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1608 iu->add_cdb_len |= IBMVFC_RDDATA;
1611 if (sg_mapped == 1) {
1612 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1616 vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1618 if (!evt->ext_list) {
1619 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1620 &evt->ext_list_token);
1622 if (!evt->ext_list) {
1623 scsi_dma_unmap(scmd);
1624 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1625 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1630 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1632 data->va = cpu_to_be64(evt->ext_list_token);
1633 data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1639 * ibmvfc_timeout - Internal command timeout handler
1640 * @t: struct ibmvfc_event that timed out
1642 * Called when an internally generated command times out
1644 static void ibmvfc_timeout(struct timer_list *t)
1646 struct ibmvfc_event *evt = from_timer(evt, t, timer);
1647 struct ibmvfc_host *vhost = evt->vhost;
1648 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1649 ibmvfc_reset_host(vhost);
1653 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1654 * @evt: event to be sent
1655 * @vhost: ibmvfc host struct
1656 * @timeout: timeout in seconds - 0 means do not time command
1658 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1660 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1661 struct ibmvfc_host *vhost, unsigned long timeout)
1663 __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1664 unsigned long flags;
1667 /* Copy the IU into the transfer area */
1668 *evt->xfer_iu = evt->iu;
1669 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1670 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1671 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1672 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1676 timer_setup(&evt->timer, ibmvfc_timeout, 0);
1679 evt->timer.expires = jiffies + (timeout * HZ);
1680 add_timer(&evt->timer);
1683 spin_lock_irqsave(&evt->queue->l_lock, flags);
1684 list_add_tail(&evt->queue_list, &evt->queue->sent);
1688 if (evt->queue->fmt == IBMVFC_SUB_CRQ_FMT)
1689 rc = ibmvfc_send_sub_crq(vhost,
1690 evt->queue->vios_cookie,
1691 be64_to_cpu(crq_as_u64[0]),
1692 be64_to_cpu(crq_as_u64[1]),
1695 rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1696 be64_to_cpu(crq_as_u64[1]));
1699 list_del(&evt->queue_list);
1700 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1701 del_timer(&evt->timer);
1703 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1704 * Firmware will send a CRQ with a transport event (0xFF) to
1705 * tell this client what has happened to the transport. This
1706 * will be handled in ibmvfc_handle_crq()
1708 if (rc == H_CLOSED) {
1709 if (printk_ratelimit())
1710 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1712 scsi_dma_unmap(evt->cmnd);
1713 ibmvfc_free_event(evt);
1714 return SCSI_MLQUEUE_HOST_BUSY;
1717 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1719 evt->cmnd->result = DID_ERROR << 16;
1720 evt->done = ibmvfc_scsi_eh_done;
1722 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1726 spin_unlock_irqrestore(&evt->queue->l_lock, flags);
1727 ibmvfc_trc_start(evt);
1734 * ibmvfc_log_error - Log an error for the failed command if appropriate
1735 * @evt: ibmvfc event to log
1738 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1740 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1741 struct ibmvfc_host *vhost = evt->vhost;
1742 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
1743 struct scsi_cmnd *cmnd = evt->cmnd;
1744 const char *err = unknown_error;
1745 int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1750 logerr = cmd_status[index].log;
1751 err = cmd_status[index].name;
1754 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1757 if (rsp->flags & FCP_RSP_LEN_VALID)
1758 rsp_code = rsp->data.info.rsp_code;
1760 scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
1761 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1762 cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
1763 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1767 * ibmvfc_relogin - Log back into the specified device
1768 * @sdev: scsi device struct
1771 static void ibmvfc_relogin(struct scsi_device *sdev)
1773 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1774 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1775 struct ibmvfc_target *tgt;
1776 unsigned long flags;
1778 spin_lock_irqsave(vhost->host->host_lock, flags);
1779 list_for_each_entry(tgt, &vhost->targets, queue) {
1780 if (rport == tgt->rport) {
1781 ibmvfc_del_tgt(tgt);
1786 ibmvfc_reinit_host(vhost);
1787 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1791 * ibmvfc_scsi_done - Handle responses from commands
1792 * @evt: ibmvfc event to be handled
1794 * Used as a callback when sending scsi cmds.
1796 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1798 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1799 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(evt->vhost, vfc_cmd);
1800 struct scsi_cmnd *cmnd = evt->cmnd;
1802 u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1805 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1806 scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1807 else if (rsp->flags & FCP_RESID_UNDER)
1808 scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1810 scsi_set_resid(cmnd, 0);
1812 if (vfc_cmd->status) {
1813 cmnd->result = ibmvfc_get_err_result(evt->vhost, vfc_cmd);
1815 if (rsp->flags & FCP_RSP_LEN_VALID)
1816 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1817 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1818 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1819 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1820 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1821 if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1822 (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1823 ibmvfc_relogin(cmnd->device);
1825 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1826 cmnd->result = (DID_ERROR << 16);
1828 ibmvfc_log_error(evt);
1831 if (!cmnd->result &&
1832 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1833 cmnd->result = (DID_ERROR << 16);
1835 scsi_dma_unmap(cmnd);
1836 cmnd->scsi_done(cmnd);
1839 ibmvfc_free_event(evt);
1843 * ibmvfc_host_chkready - Check if the host can accept commands
1844 * @vhost: struct ibmvfc host
1847 * 1 if host can accept command / 0 if not
1849 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1853 switch (vhost->state) {
1854 case IBMVFC_LINK_DEAD:
1855 case IBMVFC_HOST_OFFLINE:
1856 result = DID_NO_CONNECT << 16;
1859 case IBMVFC_INITIALIZING:
1861 case IBMVFC_LINK_DOWN:
1862 result = DID_REQUEUE << 16;
1872 static struct ibmvfc_cmd *ibmvfc_init_vfc_cmd(struct ibmvfc_event *evt, struct scsi_device *sdev)
1874 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1875 struct ibmvfc_host *vhost = evt->vhost;
1876 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
1877 struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
1878 struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
1881 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1882 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
1883 offset = offsetof(struct ibmvfc_cmd, v2.rsp);
1884 vfc_cmd->target_wwpn = cpu_to_be64(rport->port_name);
1886 offset = offsetof(struct ibmvfc_cmd, v1.rsp);
1887 vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offset);
1888 vfc_cmd->resp.len = cpu_to_be32(sizeof(*rsp));
1889 vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1890 vfc_cmd->payload_len = cpu_to_be32(sizeof(*iu));
1891 vfc_cmd->resp_len = cpu_to_be32(sizeof(*rsp));
1892 vfc_cmd->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
1893 vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1894 int_to_scsilun(sdev->lun, &iu->lun);
1900 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1901 * @shost: scsi host struct
1902 * @cmnd: struct scsi_cmnd to be executed
1905 * 0 on success / other on failure
1907 static int ibmvfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
1909 struct ibmvfc_host *vhost = shost_priv(shost);
1910 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1911 struct ibmvfc_cmd *vfc_cmd;
1912 struct ibmvfc_fcp_cmd_iu *iu;
1913 struct ibmvfc_event *evt;
1914 u32 tag_and_hwq = blk_mq_unique_tag(cmnd->request);
1915 u16 hwq = blk_mq_unique_tag_to_hwq(tag_and_hwq);
1919 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1920 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1922 cmnd->scsi_done(cmnd);
1926 cmnd->result = (DID_OK << 16);
1927 if (vhost->using_channels) {
1928 scsi_channel = hwq % vhost->scsi_scrqs.active_queues;
1929 evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[scsi_channel]);
1930 evt->hwq = hwq % vhost->scsi_scrqs.active_queues;
1932 evt = ibmvfc_get_event(&vhost->crq);
1934 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1937 vfc_cmd = ibmvfc_init_vfc_cmd(evt, cmnd->device);
1938 iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
1940 iu->xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1941 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
1943 if (cmnd->flags & SCMD_TAGGED) {
1944 vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
1945 iu->pri_task_attr = IBMVFC_SIMPLE_TASK;
1948 vfc_cmd->correlation = cpu_to_be64((u64)evt);
1950 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1951 return ibmvfc_send_event(evt, vhost, 0);
1953 ibmvfc_free_event(evt);
1955 return SCSI_MLQUEUE_HOST_BUSY;
1957 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1958 scmd_printk(KERN_ERR, cmnd,
1959 "Failed to map DMA buffer for command. rc=%d\n", rc);
1961 cmnd->result = DID_ERROR << 16;
1962 cmnd->scsi_done(cmnd);
1967 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1968 * @evt: ibmvfc event struct
1971 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1973 /* copy the response back */
1975 *evt->sync_iu = *evt->xfer_iu;
1977 complete(&evt->comp);
1981 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1982 * @evt: struct ibmvfc_event
1985 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
1987 struct ibmvfc_host *vhost = evt->vhost;
1989 ibmvfc_free_event(evt);
1990 vhost->aborting_passthru = 0;
1991 dev_info(vhost->dev, "Passthru command cancelled\n");
1995 * ibmvfc_bsg_timeout - Handle a BSG timeout
1996 * @job: struct bsg_job that timed out
1999 * 0 on success / other on failure
2001 static int ibmvfc_bsg_timeout(struct bsg_job *job)
2003 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2004 unsigned long port_id = (unsigned long)job->dd_data;
2005 struct ibmvfc_event *evt;
2006 struct ibmvfc_tmf *tmf;
2007 unsigned long flags;
2011 spin_lock_irqsave(vhost->host->host_lock, flags);
2012 if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
2013 __ibmvfc_reset_host(vhost);
2014 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2018 vhost->aborting_passthru = 1;
2019 evt = ibmvfc_get_event(&vhost->crq);
2020 ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
2023 memset(tmf, 0, sizeof(*tmf));
2024 tmf->common.version = cpu_to_be32(1);
2025 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2026 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2027 tmf->scsi_id = cpu_to_be64(port_id);
2028 tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2029 tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
2030 rc = ibmvfc_send_event(evt, vhost, default_timeout);
2033 vhost->aborting_passthru = 0;
2034 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
2037 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
2040 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2047 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
2048 * @vhost: struct ibmvfc_host to send command
2049 * @port_id: port ID to send command
2052 * 0 on success / other on failure
2054 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
2056 struct ibmvfc_port_login *plogi;
2057 struct ibmvfc_target *tgt;
2058 struct ibmvfc_event *evt;
2059 union ibmvfc_iu rsp_iu;
2060 unsigned long flags;
2061 int rc = 0, issue_login = 1;
2064 spin_lock_irqsave(vhost->host->host_lock, flags);
2065 list_for_each_entry(tgt, &vhost->targets, queue) {
2066 if (tgt->scsi_id == port_id) {
2074 if (unlikely((rc = ibmvfc_host_chkready(vhost))))
2077 evt = ibmvfc_get_event(&vhost->crq);
2078 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2079 plogi = &evt->iu.plogi;
2080 memset(plogi, 0, sizeof(*plogi));
2081 plogi->common.version = cpu_to_be32(1);
2082 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
2083 plogi->common.length = cpu_to_be16(sizeof(*plogi));
2084 plogi->scsi_id = cpu_to_be64(port_id);
2085 evt->sync_iu = &rsp_iu;
2086 init_completion(&evt->comp);
2088 rc = ibmvfc_send_event(evt, vhost, default_timeout);
2089 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2094 wait_for_completion(&evt->comp);
2096 if (rsp_iu.plogi.common.status)
2099 spin_lock_irqsave(vhost->host->host_lock, flags);
2100 ibmvfc_free_event(evt);
2102 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2108 * ibmvfc_bsg_request - Handle a BSG request
2109 * @job: struct bsg_job to be executed
2112 * 0 on success / other on failure
2114 static int ibmvfc_bsg_request(struct bsg_job *job)
2116 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
2117 struct fc_rport *rport = fc_bsg_to_rport(job);
2118 struct ibmvfc_passthru_mad *mad;
2119 struct ibmvfc_event *evt;
2120 union ibmvfc_iu rsp_iu;
2121 unsigned long flags, port_id = -1;
2122 struct fc_bsg_request *bsg_request = job->request;
2123 struct fc_bsg_reply *bsg_reply = job->reply;
2124 unsigned int code = bsg_request->msgcode;
2125 int rc = 0, req_seg, rsp_seg, issue_login = 0;
2126 u32 fc_flags, rsp_len;
2129 bsg_reply->reply_payload_rcv_len = 0;
2131 port_id = rport->port_id;
2134 case FC_BSG_HST_ELS_NOLOGIN:
2135 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
2136 (bsg_request->rqst_data.h_els.port_id[1] << 8) |
2137 bsg_request->rqst_data.h_els.port_id[2];
2139 case FC_BSG_RPT_ELS:
2140 fc_flags = IBMVFC_FC_ELS;
2144 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
2145 (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
2146 bsg_request->rqst_data.h_ct.port_id[2];
2149 fc_flags = IBMVFC_FC_CT_IU;
2157 if (!mutex_trylock(&vhost->passthru_mutex))
2160 job->dd_data = (void *)port_id;
2161 req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
2162 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2165 mutex_unlock(&vhost->passthru_mutex);
2169 rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
2170 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2173 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2174 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2175 mutex_unlock(&vhost->passthru_mutex);
2179 if (req_seg > 1 || rsp_seg > 1) {
2185 rc = ibmvfc_bsg_plogi(vhost, port_id);
2187 spin_lock_irqsave(vhost->host->host_lock, flags);
2189 if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
2190 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
2191 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2195 evt = ibmvfc_get_event(&vhost->crq);
2196 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2197 mad = &evt->iu.passthru;
2199 memset(mad, 0, sizeof(*mad));
2200 mad->common.version = cpu_to_be32(1);
2201 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
2202 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
2204 mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
2205 offsetof(struct ibmvfc_passthru_mad, iu));
2206 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
2208 mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
2209 mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
2210 mad->iu.flags = cpu_to_be32(fc_flags);
2211 mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
2213 mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
2214 mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
2215 mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
2216 mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
2217 mad->iu.scsi_id = cpu_to_be64(port_id);
2218 mad->iu.tag = cpu_to_be64((u64)evt);
2219 rsp_len = be32_to_cpu(mad->iu.rsp.len);
2221 evt->sync_iu = &rsp_iu;
2222 init_completion(&evt->comp);
2223 rc = ibmvfc_send_event(evt, vhost, 0);
2224 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2231 wait_for_completion(&evt->comp);
2233 if (rsp_iu.passthru.common.status)
2236 bsg_reply->reply_payload_rcv_len = rsp_len;
2238 spin_lock_irqsave(vhost->host->host_lock, flags);
2239 ibmvfc_free_event(evt);
2240 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2241 bsg_reply->result = rc;
2242 bsg_job_done(job, bsg_reply->result,
2243 bsg_reply->reply_payload_rcv_len);
2246 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2247 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2248 dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
2249 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2250 mutex_unlock(&vhost->passthru_mutex);
2256 * ibmvfc_reset_device - Reset the device with the specified reset type
2257 * @sdev: scsi device to reset
2259 * @desc: reset type description for log messages
2262 * 0 on success / other on failure
2264 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
2266 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2267 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2268 struct ibmvfc_cmd *tmf;
2269 struct ibmvfc_event *evt = NULL;
2270 union ibmvfc_iu rsp_iu;
2271 struct ibmvfc_fcp_cmd_iu *iu;
2272 struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2273 int rsp_rc = -EBUSY;
2274 unsigned long flags;
2277 spin_lock_irqsave(vhost->host->host_lock, flags);
2278 if (vhost->state == IBMVFC_ACTIVE) {
2279 if (vhost->using_channels)
2280 evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[0]);
2282 evt = ibmvfc_get_event(&vhost->crq);
2284 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2285 tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2286 iu = ibmvfc_get_fcp_iu(vhost, tmf);
2288 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2289 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2290 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2291 iu->tmf_flags = type;
2292 evt->sync_iu = &rsp_iu;
2294 init_completion(&evt->comp);
2295 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2297 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2300 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2305 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2306 wait_for_completion(&evt->comp);
2308 if (rsp_iu.cmd.status)
2309 rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2312 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2313 rsp_code = fc_rsp->data.info.rsp_code;
2315 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2316 "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2317 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2318 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2319 fc_rsp->scsi_status);
2322 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2324 spin_lock_irqsave(vhost->host->host_lock, flags);
2325 ibmvfc_free_event(evt);
2326 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2331 * ibmvfc_match_rport - Match function for specified remote port
2332 * @evt: ibmvfc event struct
2333 * @rport: device to match
2336 * 1 if event matches rport / 0 if event does not match rport
2338 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2340 struct fc_rport *cmd_rport;
2343 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2344 if (cmd_rport == rport)
2351 * ibmvfc_match_target - Match function for specified target
2352 * @evt: ibmvfc event struct
2353 * @device: device to match (starget)
2356 * 1 if event matches starget / 0 if event does not match starget
2358 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2360 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2366 * ibmvfc_match_lun - Match function for specified LUN
2367 * @evt: ibmvfc event struct
2368 * @device: device to match (sdev)
2371 * 1 if event matches sdev / 0 if event does not match sdev
2373 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2375 if (evt->cmnd && evt->cmnd->device == device)
2381 * ibmvfc_event_is_free - Check if event is free or not
2382 * @evt: ibmvfc event struct
2387 static bool ibmvfc_event_is_free(struct ibmvfc_event *evt)
2389 struct ibmvfc_event *loop_evt;
2391 list_for_each_entry(loop_evt, &evt->queue->free, queue_list)
2392 if (loop_evt == evt)
2399 * ibmvfc_wait_for_ops - Wait for ops to complete
2400 * @vhost: ibmvfc host struct
2401 * @device: device to match (starget or sdev)
2402 * @match: match function
2407 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2408 int (*match) (struct ibmvfc_event *, void *))
2410 struct ibmvfc_event *evt;
2411 DECLARE_COMPLETION_ONSTACK(comp);
2412 int wait, i, q_index, q_size;
2413 unsigned long flags;
2414 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2415 struct ibmvfc_queue *queues;
2418 if (vhost->mq_enabled && vhost->using_channels) {
2419 queues = vhost->scsi_scrqs.scrqs;
2420 q_size = vhost->scsi_scrqs.active_queues;
2422 queues = &vhost->crq;
2428 spin_lock_irqsave(vhost->host->host_lock, flags);
2429 for (q_index = 0; q_index < q_size; q_index++) {
2430 spin_lock(&queues[q_index].l_lock);
2431 for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2432 evt = &queues[q_index].evt_pool.events[i];
2433 if (!ibmvfc_event_is_free(evt)) {
2434 if (match(evt, device)) {
2435 evt->eh_comp = ∁
2440 spin_unlock(&queues[q_index].l_lock);
2442 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2445 timeout = wait_for_completion_timeout(&comp, timeout);
2449 spin_lock_irqsave(vhost->host->host_lock, flags);
2450 for (q_index = 0; q_index < q_size; q_index++) {
2451 spin_lock(&queues[q_index].l_lock);
2452 for (i = 0; i < queues[q_index].evt_pool.size; i++) {
2453 evt = &queues[q_index].evt_pool.events[i];
2454 if (!ibmvfc_event_is_free(evt)) {
2455 if (match(evt, device)) {
2456 evt->eh_comp = NULL;
2461 spin_unlock(&queues[q_index].l_lock);
2463 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2465 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2467 return wait ? FAILED : SUCCESS;
2476 static struct ibmvfc_event *ibmvfc_init_tmf(struct ibmvfc_queue *queue,
2477 struct scsi_device *sdev,
2480 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2481 struct scsi_target *starget = scsi_target(sdev);
2482 struct fc_rport *rport = starget_to_rport(starget);
2483 struct ibmvfc_event *evt;
2484 struct ibmvfc_tmf *tmf;
2486 evt = ibmvfc_get_event(queue);
2487 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2490 memset(tmf, 0, sizeof(*tmf));
2491 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
2492 tmf->common.version = cpu_to_be32(2);
2493 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2495 tmf->common.version = cpu_to_be32(1);
2497 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2498 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2499 tmf->scsi_id = cpu_to_be64(rport->port_id);
2500 int_to_scsilun(sdev->lun, &tmf->lun);
2501 if (!ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPRESS_ABTS))
2502 type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2503 if (vhost->state == IBMVFC_ACTIVE)
2504 tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2506 tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2507 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2508 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2510 init_completion(&evt->comp);
2515 static int ibmvfc_cancel_all_mq(struct scsi_device *sdev, int type)
2517 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2518 struct ibmvfc_event *evt, *found_evt, *temp;
2519 struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
2520 unsigned long flags;
2527 spin_lock_irqsave(vhost->host->host_lock, flags);
2528 num_hwq = vhost->scsi_scrqs.active_queues;
2529 for (i = 0; i < num_hwq; i++) {
2530 spin_lock(queues[i].q_lock);
2531 spin_lock(&queues[i].l_lock);
2533 list_for_each_entry(evt, &queues[i].sent, queue_list) {
2534 if (evt->cmnd && evt->cmnd->device == sdev) {
2539 spin_unlock(&queues[i].l_lock);
2541 if (found_evt && vhost->logged_in) {
2542 evt = ibmvfc_init_tmf(&queues[i], sdev, type);
2543 evt->sync_iu = &queues[i].cancel_rsp;
2544 ibmvfc_send_event(evt, vhost, default_timeout);
2545 list_add_tail(&evt->cancel, &cancelq);
2548 spin_unlock(queues[i].q_lock);
2550 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2552 if (list_empty(&cancelq)) {
2553 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2554 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2558 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2560 list_for_each_entry_safe(evt, temp, &cancelq, cancel) {
2561 wait_for_completion(&evt->comp);
2562 status = be16_to_cpu(evt->queue->cancel_rsp.mad_common.status);
2563 list_del(&evt->cancel);
2564 ibmvfc_free_event(evt);
2566 if (status != IBMVFC_MAD_SUCCESS) {
2567 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2569 case IBMVFC_MAD_DRIVER_FAILED:
2570 case IBMVFC_MAD_CRQ_ERROR:
2571 /* Host adapter most likely going through reset, return success to
2572 * the caller will wait for the command being cancelled to get returned
2585 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2590 static int ibmvfc_cancel_all_sq(struct scsi_device *sdev, int type)
2592 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2593 struct ibmvfc_event *evt, *found_evt;
2594 union ibmvfc_iu rsp;
2595 int rsp_rc = -EBUSY;
2596 unsigned long flags;
2601 spin_lock_irqsave(vhost->host->host_lock, flags);
2602 spin_lock(&vhost->crq.l_lock);
2603 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2604 if (evt->cmnd && evt->cmnd->device == sdev) {
2609 spin_unlock(&vhost->crq.l_lock);
2612 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2613 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2614 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2618 if (vhost->logged_in) {
2619 evt = ibmvfc_init_tmf(&vhost->crq, sdev, type);
2620 evt->sync_iu = &rsp;
2621 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2624 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2627 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2628 /* If failure is received, the host adapter is most likely going
2629 through reset, return success so the caller will wait for the command
2630 being cancelled to get returned */
2634 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2636 wait_for_completion(&evt->comp);
2637 status = be16_to_cpu(rsp.mad_common.status);
2638 spin_lock_irqsave(vhost->host->host_lock, flags);
2639 ibmvfc_free_event(evt);
2640 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2642 if (status != IBMVFC_MAD_SUCCESS) {
2643 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2645 case IBMVFC_MAD_DRIVER_FAILED:
2646 case IBMVFC_MAD_CRQ_ERROR:
2647 /* Host adapter most likely going through reset, return success to
2648 the caller will wait for the command being cancelled to get returned */
2655 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2660 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2661 * @sdev: scsi device to cancel commands
2662 * @type: type of error recovery being performed
2664 * This sends a cancel to the VIOS for the specified device. This does
2665 * NOT send any abort to the actual device. That must be done separately.
2668 * 0 on success / other on failure
2670 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2672 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2674 if (vhost->mq_enabled && vhost->using_channels)
2675 return ibmvfc_cancel_all_mq(sdev, type);
2677 return ibmvfc_cancel_all_sq(sdev, type);
2681 * ibmvfc_match_key - Match function for specified cancel key
2682 * @evt: ibmvfc event struct
2683 * @key: cancel key to match
2686 * 1 if event matches key / 0 if event does not match key
2688 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2690 unsigned long cancel_key = (unsigned long)key;
2692 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2693 be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2699 * ibmvfc_match_evt - Match function for specified event
2700 * @evt: ibmvfc event struct
2701 * @match: event to match
2704 * 1 if event matches key / 0 if event does not match key
2706 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2714 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2715 * @sdev: scsi device to abort commands
2717 * This sends an Abort Task Set to the VIOS for the specified device. This does
2718 * NOT send any cancel to the VIOS. That must be done separately.
2721 * 0 on success / other on failure
2723 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2725 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2726 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2727 struct ibmvfc_cmd *tmf;
2728 struct ibmvfc_event *evt, *found_evt;
2729 union ibmvfc_iu rsp_iu;
2730 struct ibmvfc_fcp_cmd_iu *iu;
2731 struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
2732 int rc, rsp_rc = -EBUSY;
2733 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2737 spin_lock_irqsave(vhost->host->host_lock, flags);
2738 spin_lock(&vhost->crq.l_lock);
2739 list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
2740 if (evt->cmnd && evt->cmnd->device == sdev) {
2745 spin_unlock(&vhost->crq.l_lock);
2748 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2749 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2750 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2754 if (vhost->state == IBMVFC_ACTIVE) {
2755 evt = ibmvfc_get_event(&vhost->crq);
2756 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2757 tmf = ibmvfc_init_vfc_cmd(evt, sdev);
2758 iu = ibmvfc_get_fcp_iu(vhost, tmf);
2760 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
2761 tmf->target_wwpn = cpu_to_be64(rport->port_name);
2762 iu->tmf_flags = IBMVFC_ABORT_TASK_SET;
2763 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2764 evt->sync_iu = &rsp_iu;
2766 tmf->correlation = cpu_to_be64((u64)evt);
2768 init_completion(&evt->comp);
2769 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2772 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2775 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2779 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2780 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2783 rc = ibmvfc_cancel_all(sdev, 0);
2785 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2791 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2792 ibmvfc_reset_host(vhost);
2794 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2799 rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2800 if (rc != SUCCESS) {
2801 spin_lock_irqsave(vhost->host->host_lock, flags);
2802 ibmvfc_hard_reset_host(vhost);
2803 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2811 if (rsp_iu.cmd.status)
2812 rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
2815 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2816 rsp_code = fc_rsp->data.info.rsp_code;
2818 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2819 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2820 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2821 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2822 fc_rsp->scsi_status);
2825 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2828 spin_lock_irqsave(vhost->host->host_lock, flags);
2829 ibmvfc_free_event(evt);
2830 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2835 * ibmvfc_eh_abort_handler - Abort a command
2836 * @cmd: scsi command to abort
2839 * SUCCESS / FAST_IO_FAIL / FAILED
2841 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2843 struct scsi_device *sdev = cmd->device;
2844 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2845 int cancel_rc, block_rc;
2849 block_rc = fc_block_scsi_eh(cmd);
2850 ibmvfc_wait_while_resetting(vhost);
2851 if (block_rc != FAST_IO_FAIL) {
2852 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2853 ibmvfc_abort_task_set(sdev);
2855 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2858 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2860 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2868 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2869 * @cmd: scsi command struct
2872 * SUCCESS / FAST_IO_FAIL / FAILED
2874 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2876 struct scsi_device *sdev = cmd->device;
2877 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2878 int cancel_rc, block_rc, reset_rc = 0;
2882 block_rc = fc_block_scsi_eh(cmd);
2883 ibmvfc_wait_while_resetting(vhost);
2884 if (block_rc != FAST_IO_FAIL) {
2885 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2886 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2888 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2890 if (!cancel_rc && !reset_rc)
2891 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2893 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2901 * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2902 * @sdev: scsi device struct
2903 * @data: return code
2906 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2908 unsigned long *rc = data;
2909 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2913 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2914 * @sdev: scsi device struct
2915 * @data: return code
2918 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2920 unsigned long *rc = data;
2921 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2925 * ibmvfc_eh_target_reset_handler - Reset the target
2926 * @cmd: scsi command struct
2929 * SUCCESS / FAST_IO_FAIL / FAILED
2931 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2933 struct scsi_device *sdev = cmd->device;
2934 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2935 struct scsi_target *starget = scsi_target(sdev);
2939 unsigned long cancel_rc = 0;
2942 block_rc = fc_block_scsi_eh(cmd);
2943 ibmvfc_wait_while_resetting(vhost);
2944 if (block_rc != FAST_IO_FAIL) {
2945 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2946 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2948 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2950 if (!cancel_rc && !reset_rc)
2951 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2953 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2961 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2962 * @cmd: struct scsi_cmnd having problems
2965 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2968 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2970 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2971 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2973 return rc ? FAILED : SUCCESS;
2977 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2978 * @rport: rport struct
2983 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2985 struct Scsi_Host *shost = rport_to_shost(rport);
2986 struct ibmvfc_host *vhost = shost_priv(shost);
2987 struct fc_rport *dev_rport;
2988 struct scsi_device *sdev;
2989 struct ibmvfc_target *tgt;
2990 unsigned long rc, flags;
2994 shost_for_each_device(sdev, shost) {
2995 dev_rport = starget_to_rport(scsi_target(sdev));
2996 if (dev_rport != rport)
2998 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
3001 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
3004 ibmvfc_issue_fc_host_lip(shost);
3006 spin_lock_irqsave(shost->host_lock, flags);
3008 list_for_each_entry(tgt, &vhost->targets, queue) {
3009 if (tgt->scsi_id == rport->port_id) {
3015 if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
3017 * If we get here, that means we previously attempted to send
3018 * an implicit logout to the target but it failed, most likely
3019 * due to I/O being pending, so we need to send it again
3021 ibmvfc_del_tgt(tgt);
3022 ibmvfc_reinit_host(vhost);
3025 spin_unlock_irqrestore(shost->host_lock, flags);
3029 static const struct ibmvfc_async_desc ae_desc [] = {
3030 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3031 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3032 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3033 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3034 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
3035 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
3036 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
3037 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
3038 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
3039 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
3040 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
3041 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
3042 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
3045 static const struct ibmvfc_async_desc unknown_ae = {
3046 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
3050 * ibmvfc_get_ae_desc - Get text description for async event
3054 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
3058 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
3059 if (ae_desc[i].ae == ae)
3065 static const struct {
3066 enum ibmvfc_ae_link_state state;
3069 { IBMVFC_AE_LS_LINK_UP, " link up" },
3070 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
3071 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
3072 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
3076 * ibmvfc_get_link_state - Get text description for link state
3077 * @state: link state
3080 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
3084 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
3085 if (link_desc[i].state == state)
3086 return link_desc[i].desc;
3092 * ibmvfc_handle_async - Handle an async event from the adapter
3093 * @crq: crq to process
3094 * @vhost: ibmvfc host struct
3097 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
3098 struct ibmvfc_host *vhost)
3100 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
3101 struct ibmvfc_target *tgt;
3103 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
3104 " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
3105 be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
3106 ibmvfc_get_link_state(crq->link_state));
3108 switch (be64_to_cpu(crq->event)) {
3109 case IBMVFC_AE_RESUME:
3110 switch (crq->link_state) {
3111 case IBMVFC_AE_LS_LINK_DOWN:
3112 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3114 case IBMVFC_AE_LS_LINK_DEAD:
3115 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3117 case IBMVFC_AE_LS_LINK_UP:
3118 case IBMVFC_AE_LS_LINK_BOUNCED:
3120 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3121 vhost->delay_init = 1;
3122 __ibmvfc_reset_host(vhost);
3127 case IBMVFC_AE_LINK_UP:
3128 vhost->events_to_log |= IBMVFC_AE_LINKUP;
3129 vhost->delay_init = 1;
3130 __ibmvfc_reset_host(vhost);
3132 case IBMVFC_AE_SCN_FABRIC:
3133 case IBMVFC_AE_SCN_DOMAIN:
3134 vhost->events_to_log |= IBMVFC_AE_RSCN;
3135 if (vhost->state < IBMVFC_HALTED) {
3136 vhost->delay_init = 1;
3137 __ibmvfc_reset_host(vhost);
3140 case IBMVFC_AE_SCN_NPORT:
3141 case IBMVFC_AE_SCN_GROUP:
3142 vhost->events_to_log |= IBMVFC_AE_RSCN;
3143 ibmvfc_reinit_host(vhost);
3145 case IBMVFC_AE_ELS_LOGO:
3146 case IBMVFC_AE_ELS_PRLO:
3147 case IBMVFC_AE_ELS_PLOGI:
3148 list_for_each_entry(tgt, &vhost->targets, queue) {
3149 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
3151 if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
3153 if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
3155 if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
3157 if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
3159 if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
3160 ibmvfc_del_tgt(tgt);
3161 ibmvfc_reinit_host(vhost);
3165 case IBMVFC_AE_LINK_DOWN:
3166 case IBMVFC_AE_ADAPTER_FAILED:
3167 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3169 case IBMVFC_AE_LINK_DEAD:
3170 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3172 case IBMVFC_AE_HALT:
3173 ibmvfc_link_down(vhost, IBMVFC_HALTED);
3176 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
3182 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
3183 * @crq: Command/Response queue
3184 * @vhost: ibmvfc host struct
3185 * @evt_doneq: Event done queue
3188 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3189 struct list_head *evt_doneq)
3192 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3194 switch (crq->valid) {
3195 case IBMVFC_CRQ_INIT_RSP:
3196 switch (crq->format) {
3197 case IBMVFC_CRQ_INIT:
3198 dev_info(vhost->dev, "Partner initialized\n");
3199 /* Send back a response */
3200 rc = ibmvfc_send_crq_init_complete(vhost);
3202 ibmvfc_init_host(vhost);
3204 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
3206 case IBMVFC_CRQ_INIT_COMPLETE:
3207 dev_info(vhost->dev, "Partner initialization complete\n");
3208 ibmvfc_init_host(vhost);
3211 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
3214 case IBMVFC_CRQ_XPORT_EVENT:
3215 vhost->state = IBMVFC_NO_CRQ;
3216 vhost->logged_in = 0;
3217 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3218 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
3219 /* We need to re-setup the interpartition connection */
3220 dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
3221 vhost->client_migrated = 1;
3222 ibmvfc_purge_requests(vhost, DID_REQUEUE);
3223 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3224 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
3225 } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
3226 dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
3227 ibmvfc_purge_requests(vhost, DID_ERROR);
3228 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
3229 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
3231 dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
3234 case IBMVFC_CRQ_CMD_RSP:
3237 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
3241 if (crq->format == IBMVFC_ASYNC_EVENT)
3244 /* The only kind of payload CRQs we should get are responses to
3245 * things we send. Make sure this response is to something we
3248 if (unlikely(!ibmvfc_valid_event(&vhost->crq.evt_pool, evt))) {
3249 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3254 if (unlikely(atomic_read(&evt->free))) {
3255 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3260 spin_lock(&evt->queue->l_lock);
3261 list_move_tail(&evt->queue_list, evt_doneq);
3262 spin_unlock(&evt->queue->l_lock);
3266 * ibmvfc_scan_finished - Check if the device scan is done.
3267 * @shost: scsi host struct
3268 * @time: current elapsed time
3271 * 0 if scan is not done / 1 if scan is done
3273 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3275 unsigned long flags;
3276 struct ibmvfc_host *vhost = shost_priv(shost);
3279 spin_lock_irqsave(shost->host_lock, flags);
3280 if (time >= (init_timeout * HZ)) {
3281 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
3282 "continuing initialization\n", init_timeout);
3286 if (vhost->scan_complete)
3288 spin_unlock_irqrestore(shost->host_lock, flags);
3293 * ibmvfc_slave_alloc - Setup the device's task set value
3294 * @sdev: struct scsi_device device to configure
3296 * Set the device's task set value so that error handling works as
3300 * 0 on success / -ENXIO if device does not exist
3302 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
3304 struct Scsi_Host *shost = sdev->host;
3305 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3306 struct ibmvfc_host *vhost = shost_priv(shost);
3307 unsigned long flags = 0;
3309 if (!rport || fc_remote_port_chkready(rport))
3312 spin_lock_irqsave(shost->host_lock, flags);
3313 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
3314 spin_unlock_irqrestore(shost->host_lock, flags);
3319 * ibmvfc_target_alloc - Setup the target's task set value
3320 * @starget: struct scsi_target
3322 * Set the target's task set value so that error handling works as
3326 * 0 on success / -ENXIO if device does not exist
3328 static int ibmvfc_target_alloc(struct scsi_target *starget)
3330 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3331 struct ibmvfc_host *vhost = shost_priv(shost);
3332 unsigned long flags = 0;
3334 spin_lock_irqsave(shost->host_lock, flags);
3335 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
3336 spin_unlock_irqrestore(shost->host_lock, flags);
3341 * ibmvfc_slave_configure - Configure the device
3342 * @sdev: struct scsi_device device to configure
3344 * Enable allow_restart for a device if it is a disk. Adjust the
3345 * queue_depth here also.
3350 static int ibmvfc_slave_configure(struct scsi_device *sdev)
3352 struct Scsi_Host *shost = sdev->host;
3353 unsigned long flags = 0;
3355 spin_lock_irqsave(shost->host_lock, flags);
3356 if (sdev->type == TYPE_DISK) {
3357 sdev->allow_restart = 1;
3358 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
3360 spin_unlock_irqrestore(shost->host_lock, flags);
3365 * ibmvfc_change_queue_depth - Change the device's queue depth
3366 * @sdev: scsi device struct
3367 * @qdepth: depth to set
3372 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
3374 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
3375 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
3377 return scsi_change_queue_depth(sdev, qdepth);
3380 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
3381 struct device_attribute *attr, char *buf)
3383 struct Scsi_Host *shost = class_to_shost(dev);
3384 struct ibmvfc_host *vhost = shost_priv(shost);
3386 return snprintf(buf, PAGE_SIZE, "%s\n",
3387 vhost->login_buf->resp.partition_name);
3390 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
3391 struct device_attribute *attr, char *buf)
3393 struct Scsi_Host *shost = class_to_shost(dev);
3394 struct ibmvfc_host *vhost = shost_priv(shost);
3396 return snprintf(buf, PAGE_SIZE, "%s\n",
3397 vhost->login_buf->resp.device_name);
3400 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3401 struct device_attribute *attr, char *buf)
3403 struct Scsi_Host *shost = class_to_shost(dev);
3404 struct ibmvfc_host *vhost = shost_priv(shost);
3406 return snprintf(buf, PAGE_SIZE, "%s\n",
3407 vhost->login_buf->resp.port_loc_code);
3410 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3411 struct device_attribute *attr, char *buf)
3413 struct Scsi_Host *shost = class_to_shost(dev);
3414 struct ibmvfc_host *vhost = shost_priv(shost);
3416 return snprintf(buf, PAGE_SIZE, "%s\n",
3417 vhost->login_buf->resp.drc_name);
3420 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3421 struct device_attribute *attr, char *buf)
3423 struct Scsi_Host *shost = class_to_shost(dev);
3424 struct ibmvfc_host *vhost = shost_priv(shost);
3425 return snprintf(buf, PAGE_SIZE, "%d\n", be32_to_cpu(vhost->login_buf->resp.version));
3428 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3429 struct device_attribute *attr, char *buf)
3431 struct Scsi_Host *shost = class_to_shost(dev);
3432 struct ibmvfc_host *vhost = shost_priv(shost);
3433 return snprintf(buf, PAGE_SIZE, "%llx\n", be64_to_cpu(vhost->login_buf->resp.capabilities));
3437 * ibmvfc_show_log_level - Show the adapter's error logging level
3438 * @dev: class device struct
3443 * number of bytes printed to buffer
3445 static ssize_t ibmvfc_show_log_level(struct device *dev,
3446 struct device_attribute *attr, char *buf)
3448 struct Scsi_Host *shost = class_to_shost(dev);
3449 struct ibmvfc_host *vhost = shost_priv(shost);
3450 unsigned long flags = 0;
3453 spin_lock_irqsave(shost->host_lock, flags);
3454 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
3455 spin_unlock_irqrestore(shost->host_lock, flags);
3460 * ibmvfc_store_log_level - Change the adapter's error logging level
3461 * @dev: class device struct
3464 * @count: buffer size
3467 * number of bytes printed to buffer
3469 static ssize_t ibmvfc_store_log_level(struct device *dev,
3470 struct device_attribute *attr,
3471 const char *buf, size_t count)
3473 struct Scsi_Host *shost = class_to_shost(dev);
3474 struct ibmvfc_host *vhost = shost_priv(shost);
3475 unsigned long flags = 0;
3477 spin_lock_irqsave(shost->host_lock, flags);
3478 vhost->log_level = simple_strtoul(buf, NULL, 10);
3479 spin_unlock_irqrestore(shost->host_lock, flags);
3483 static ssize_t ibmvfc_show_scsi_channels(struct device *dev,
3484 struct device_attribute *attr, char *buf)
3486 struct Scsi_Host *shost = class_to_shost(dev);
3487 struct ibmvfc_host *vhost = shost_priv(shost);
3488 unsigned long flags = 0;
3491 spin_lock_irqsave(shost->host_lock, flags);
3492 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->client_scsi_channels);
3493 spin_unlock_irqrestore(shost->host_lock, flags);
3497 static ssize_t ibmvfc_store_scsi_channels(struct device *dev,
3498 struct device_attribute *attr,
3499 const char *buf, size_t count)
3501 struct Scsi_Host *shost = class_to_shost(dev);
3502 struct ibmvfc_host *vhost = shost_priv(shost);
3503 unsigned long flags = 0;
3504 unsigned int channels;
3506 spin_lock_irqsave(shost->host_lock, flags);
3507 channels = simple_strtoul(buf, NULL, 10);
3508 vhost->client_scsi_channels = min(channels, nr_scsi_hw_queues);
3509 ibmvfc_hard_reset_host(vhost);
3510 spin_unlock_irqrestore(shost->host_lock, flags);
3514 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3515 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3516 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3517 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3518 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3519 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3520 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3521 ibmvfc_show_log_level, ibmvfc_store_log_level);
3522 static DEVICE_ATTR(nr_scsi_channels, S_IRUGO | S_IWUSR,
3523 ibmvfc_show_scsi_channels, ibmvfc_store_scsi_channels);
3525 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3527 * ibmvfc_read_trace - Dump the adapter trace
3528 * @filp: open sysfs file
3529 * @kobj: kobject struct
3530 * @bin_attr: bin_attribute struct
3533 * @count: buffer size
3536 * number of bytes printed to buffer
3538 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3539 struct bin_attribute *bin_attr,
3540 char *buf, loff_t off, size_t count)
3542 struct device *dev = kobj_to_dev(kobj);
3543 struct Scsi_Host *shost = class_to_shost(dev);
3544 struct ibmvfc_host *vhost = shost_priv(shost);
3545 unsigned long flags = 0;
3546 int size = IBMVFC_TRACE_SIZE;
3547 char *src = (char *)vhost->trace;
3551 if (off + count > size) {
3556 spin_lock_irqsave(shost->host_lock, flags);
3557 memcpy(buf, &src[off], count);
3558 spin_unlock_irqrestore(shost->host_lock, flags);
3562 static struct bin_attribute ibmvfc_trace_attr = {
3568 .read = ibmvfc_read_trace,
3572 static struct device_attribute *ibmvfc_attrs[] = {
3573 &dev_attr_partition_name,
3574 &dev_attr_device_name,
3575 &dev_attr_port_loc_code,
3577 &dev_attr_npiv_version,
3578 &dev_attr_capabilities,
3579 &dev_attr_log_level,
3580 &dev_attr_nr_scsi_channels,
3584 static struct scsi_host_template driver_template = {
3585 .module = THIS_MODULE,
3586 .name = "IBM POWER Virtual FC Adapter",
3587 .proc_name = IBMVFC_NAME,
3588 .queuecommand = ibmvfc_queuecommand,
3589 .eh_timed_out = fc_eh_timed_out,
3590 .eh_abort_handler = ibmvfc_eh_abort_handler,
3591 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3592 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3593 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3594 .slave_alloc = ibmvfc_slave_alloc,
3595 .slave_configure = ibmvfc_slave_configure,
3596 .target_alloc = ibmvfc_target_alloc,
3597 .scan_finished = ibmvfc_scan_finished,
3598 .change_queue_depth = ibmvfc_change_queue_depth,
3600 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3602 .sg_tablesize = SG_ALL,
3603 .max_sectors = IBMVFC_MAX_SECTORS,
3604 .shost_attrs = ibmvfc_attrs,
3605 .track_queue_depth = 1,
3610 * ibmvfc_next_async_crq - Returns the next entry in async queue
3611 * @vhost: ibmvfc host struct
3614 * Pointer to next entry in queue / NULL if empty
3616 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3618 struct ibmvfc_queue *async_crq = &vhost->async_crq;
3619 struct ibmvfc_async_crq *crq;
3621 crq = &async_crq->msgs.async[async_crq->cur];
3622 if (crq->valid & 0x80) {
3623 if (++async_crq->cur == async_crq->size)
3633 * ibmvfc_next_crq - Returns the next entry in message queue
3634 * @vhost: ibmvfc host struct
3637 * Pointer to next entry in queue / NULL if empty
3639 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3641 struct ibmvfc_queue *queue = &vhost->crq;
3642 struct ibmvfc_crq *crq;
3644 crq = &queue->msgs.crq[queue->cur];
3645 if (crq->valid & 0x80) {
3646 if (++queue->cur == queue->size)
3656 * ibmvfc_interrupt - Interrupt handler
3657 * @irq: number of irq to handle, not used
3658 * @dev_instance: ibmvfc_host that received interrupt
3663 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3665 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3666 unsigned long flags;
3668 spin_lock_irqsave(vhost->host->host_lock, flags);
3669 vio_disable_interrupts(to_vio_dev(vhost->dev));
3670 tasklet_schedule(&vhost->tasklet);
3671 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3676 * ibmvfc_tasklet - Interrupt handler tasklet
3677 * @data: ibmvfc host struct
3682 static void ibmvfc_tasklet(void *data)
3684 struct ibmvfc_host *vhost = data;
3685 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3686 struct ibmvfc_crq *crq;
3687 struct ibmvfc_async_crq *async;
3688 struct ibmvfc_event *evt, *temp;
3689 unsigned long flags;
3691 LIST_HEAD(evt_doneq);
3693 spin_lock_irqsave(vhost->host->host_lock, flags);
3694 spin_lock(vhost->crq.q_lock);
3696 /* Pull all the valid messages off the async CRQ */
3697 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3698 ibmvfc_handle_async(async, vhost);
3703 /* Pull all the valid messages off the CRQ */
3704 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3705 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3710 vio_enable_interrupts(vdev);
3711 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3712 vio_disable_interrupts(vdev);
3713 ibmvfc_handle_async(async, vhost);
3716 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3717 vio_disable_interrupts(vdev);
3718 ibmvfc_handle_crq(crq, vhost, &evt_doneq);
3725 spin_unlock(vhost->crq.q_lock);
3726 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3728 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3729 del_timer(&evt->timer);
3730 list_del(&evt->queue_list);
3731 ibmvfc_trc_end(evt);
3736 static int ibmvfc_toggle_scrq_irq(struct ibmvfc_queue *scrq, int enable)
3738 struct device *dev = scrq->vhost->dev;
3739 struct vio_dev *vdev = to_vio_dev(dev);
3741 int irq_action = H_ENABLE_VIO_INTERRUPT;
3744 irq_action = H_DISABLE_VIO_INTERRUPT;
3746 rc = plpar_hcall_norets(H_VIOCTL, vdev->unit_address, irq_action,
3747 scrq->hw_irq, 0, 0);
3750 dev_err(dev, "Couldn't %s sub-crq[%lu] irq. rc=%ld\n",
3751 enable ? "enable" : "disable", scrq->hwq_id, rc);
3756 static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
3757 struct list_head *evt_doneq)
3759 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
3761 switch (crq->valid) {
3762 case IBMVFC_CRQ_CMD_RSP:
3764 case IBMVFC_CRQ_XPORT_EVENT:
3767 dev_err(vhost->dev, "Got and invalid message type 0x%02x\n", crq->valid);
3771 /* The only kind of payload CRQs we should get are responses to
3772 * things we send. Make sure this response is to something we
3775 if (unlikely(!ibmvfc_valid_event(&evt->queue->evt_pool, evt))) {
3776 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
3781 if (unlikely(atomic_read(&evt->free))) {
3782 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
3787 spin_lock(&evt->queue->l_lock);
3788 list_move_tail(&evt->queue_list, evt_doneq);
3789 spin_unlock(&evt->queue->l_lock);
3792 static struct ibmvfc_crq *ibmvfc_next_scrq(struct ibmvfc_queue *scrq)
3794 struct ibmvfc_crq *crq;
3796 crq = &scrq->msgs.scrq[scrq->cur].crq;
3797 if (crq->valid & 0x80) {
3798 if (++scrq->cur == scrq->size)
3807 static void ibmvfc_drain_sub_crq(struct ibmvfc_queue *scrq)
3809 struct ibmvfc_crq *crq;
3810 struct ibmvfc_event *evt, *temp;
3811 unsigned long flags;
3813 LIST_HEAD(evt_doneq);
3815 spin_lock_irqsave(scrq->q_lock, flags);
3817 while ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3818 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3823 ibmvfc_toggle_scrq_irq(scrq, 1);
3824 if ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
3825 ibmvfc_toggle_scrq_irq(scrq, 0);
3826 ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
3832 spin_unlock_irqrestore(scrq->q_lock, flags);
3834 list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
3835 del_timer(&evt->timer);
3836 list_del(&evt->queue_list);
3837 ibmvfc_trc_end(evt);
3842 static irqreturn_t ibmvfc_interrupt_scsi(int irq, void *scrq_instance)
3844 struct ibmvfc_queue *scrq = (struct ibmvfc_queue *)scrq_instance;
3846 ibmvfc_toggle_scrq_irq(scrq, 0);
3847 ibmvfc_drain_sub_crq(scrq);
3853 * ibmvfc_init_tgt - Set the next init job step for the target
3854 * @tgt: ibmvfc target struct
3855 * @job_step: job step to perform
3858 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3859 void (*job_step) (struct ibmvfc_target *))
3861 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3862 tgt->job_step = job_step;
3863 wake_up(&tgt->vhost->work_wait_q);
3867 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3868 * @tgt: ibmvfc target struct
3869 * @job_step: initialization job step
3871 * Returns: 1 if step will be retried / 0 if not
3874 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3875 void (*job_step) (struct ibmvfc_target *))
3877 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3878 ibmvfc_del_tgt(tgt);
3879 wake_up(&tgt->vhost->work_wait_q);
3882 ibmvfc_init_tgt(tgt, job_step);
3886 /* Defined in FC-LS */
3887 static const struct {
3904 * ibmvfc_get_prli_rsp - Find PRLI response index
3905 * @flags: PRLI response flags
3908 static int ibmvfc_get_prli_rsp(u16 flags)
3911 int code = (flags & 0x0f00) >> 8;
3913 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3914 if (prli_rsp[i].code == code)
3921 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3922 * @evt: ibmvfc event struct
3925 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3927 struct ibmvfc_target *tgt = evt->tgt;
3928 struct ibmvfc_host *vhost = evt->vhost;
3929 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3930 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3931 u32 status = be16_to_cpu(rsp->common.status);
3932 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3934 vhost->discovery_threads--;
3935 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3937 case IBMVFC_MAD_SUCCESS:
3938 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3939 parms->type, parms->flags, parms->service_parms);
3941 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3942 index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3943 if (prli_rsp[index].logged_in) {
3944 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3945 tgt->need_login = 0;
3947 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3948 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3949 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3950 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3953 ibmvfc_del_tgt(tgt);
3954 } else if (prli_rsp[index].retry)
3955 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3957 ibmvfc_del_tgt(tgt);
3959 ibmvfc_del_tgt(tgt);
3961 case IBMVFC_MAD_DRIVER_FAILED:
3963 case IBMVFC_MAD_CRQ_ERROR:
3964 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3966 case IBMVFC_MAD_FAILED:
3968 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3969 be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3970 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3971 else if (tgt->logo_rcvd)
3972 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3973 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3974 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3976 ibmvfc_del_tgt(tgt);
3978 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3979 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3980 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
3984 kref_put(&tgt->kref, ibmvfc_release_tgt);
3985 ibmvfc_free_event(evt);
3986 wake_up(&vhost->work_wait_q);
3990 * ibmvfc_tgt_send_prli - Send a process login
3991 * @tgt: ibmvfc target struct
3994 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3996 struct ibmvfc_process_login *prli;
3997 struct ibmvfc_host *vhost = tgt->vhost;
3998 struct ibmvfc_event *evt;
4000 if (vhost->discovery_threads >= disc_threads)
4003 kref_get(&tgt->kref);
4004 evt = ibmvfc_get_event(&vhost->crq);
4005 vhost->discovery_threads++;
4006 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
4008 prli = &evt->iu.prli;
4009 memset(prli, 0, sizeof(*prli));
4010 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4011 prli->common.version = cpu_to_be32(2);
4012 prli->target_wwpn = cpu_to_be64(tgt->wwpn);
4014 prli->common.version = cpu_to_be32(1);
4016 prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
4017 prli->common.length = cpu_to_be16(sizeof(*prli));
4018 prli->scsi_id = cpu_to_be64(tgt->scsi_id);
4020 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
4021 prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
4022 prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
4023 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
4026 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
4028 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4029 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4030 vhost->discovery_threads--;
4031 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4032 kref_put(&tgt->kref, ibmvfc_release_tgt);
4034 tgt_dbg(tgt, "Sent process login\n");
4038 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
4039 * @evt: ibmvfc event struct
4042 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
4044 struct ibmvfc_target *tgt = evt->tgt;
4045 struct ibmvfc_host *vhost = evt->vhost;
4046 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
4047 u32 status = be16_to_cpu(rsp->common.status);
4048 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4050 vhost->discovery_threads--;
4051 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4053 case IBMVFC_MAD_SUCCESS:
4054 tgt_dbg(tgt, "Port Login succeeded\n");
4055 if (tgt->ids.port_name &&
4056 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
4058 tgt_dbg(tgt, "Port re-init required\n");
4061 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4062 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4063 tgt->ids.port_id = tgt->scsi_id;
4064 memcpy(&tgt->service_parms, &rsp->service_parms,
4065 sizeof(tgt->service_parms));
4066 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4067 sizeof(tgt->service_parms_change));
4068 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4070 case IBMVFC_MAD_DRIVER_FAILED:
4072 case IBMVFC_MAD_CRQ_ERROR:
4073 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4075 case IBMVFC_MAD_FAILED:
4077 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4078 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
4080 ibmvfc_del_tgt(tgt);
4082 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4083 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4084 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4085 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4086 ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
4090 kref_put(&tgt->kref, ibmvfc_release_tgt);
4091 ibmvfc_free_event(evt);
4092 wake_up(&vhost->work_wait_q);
4096 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
4097 * @tgt: ibmvfc target struct
4100 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
4102 struct ibmvfc_port_login *plogi;
4103 struct ibmvfc_host *vhost = tgt->vhost;
4104 struct ibmvfc_event *evt;
4106 if (vhost->discovery_threads >= disc_threads)
4109 kref_get(&tgt->kref);
4111 evt = ibmvfc_get_event(&vhost->crq);
4112 vhost->discovery_threads++;
4113 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4114 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
4116 plogi = &evt->iu.plogi;
4117 memset(plogi, 0, sizeof(*plogi));
4118 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4119 plogi->common.version = cpu_to_be32(2);
4120 plogi->target_wwpn = cpu_to_be64(tgt->wwpn);
4122 plogi->common.version = cpu_to_be32(1);
4124 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
4125 plogi->common.length = cpu_to_be16(sizeof(*plogi));
4126 plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
4128 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4129 vhost->discovery_threads--;
4130 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4131 kref_put(&tgt->kref, ibmvfc_release_tgt);
4133 tgt_dbg(tgt, "Sent port login\n");
4137 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
4138 * @evt: ibmvfc event struct
4141 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
4143 struct ibmvfc_target *tgt = evt->tgt;
4144 struct ibmvfc_host *vhost = evt->vhost;
4145 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
4146 u32 status = be16_to_cpu(rsp->common.status);
4148 vhost->discovery_threads--;
4149 ibmvfc_free_event(evt);
4150 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4153 case IBMVFC_MAD_SUCCESS:
4154 tgt_dbg(tgt, "Implicit Logout succeeded\n");
4156 case IBMVFC_MAD_DRIVER_FAILED:
4157 kref_put(&tgt->kref, ibmvfc_release_tgt);
4158 wake_up(&vhost->work_wait_q);
4160 case IBMVFC_MAD_FAILED:
4162 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
4166 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
4167 kref_put(&tgt->kref, ibmvfc_release_tgt);
4168 wake_up(&vhost->work_wait_q);
4172 * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
4173 * @tgt: ibmvfc target struct
4174 * @done: Routine to call when the event is responded to
4177 * Allocated and initialized ibmvfc_event struct
4179 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
4180 void (*done) (struct ibmvfc_event *))
4182 struct ibmvfc_implicit_logout *mad;
4183 struct ibmvfc_host *vhost = tgt->vhost;
4184 struct ibmvfc_event *evt;
4186 kref_get(&tgt->kref);
4187 evt = ibmvfc_get_event(&vhost->crq);
4188 ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
4190 mad = &evt->iu.implicit_logout;
4191 memset(mad, 0, sizeof(*mad));
4192 mad->common.version = cpu_to_be32(1);
4193 mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
4194 mad->common.length = cpu_to_be16(sizeof(*mad));
4195 mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4200 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
4201 * @tgt: ibmvfc target struct
4204 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
4206 struct ibmvfc_host *vhost = tgt->vhost;
4207 struct ibmvfc_event *evt;
4209 if (vhost->discovery_threads >= disc_threads)
4212 vhost->discovery_threads++;
4213 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4214 ibmvfc_tgt_implicit_logout_done);
4216 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4217 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4218 vhost->discovery_threads--;
4219 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4220 kref_put(&tgt->kref, ibmvfc_release_tgt);
4222 tgt_dbg(tgt, "Sent Implicit Logout\n");
4226 * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
4227 * @evt: ibmvfc event struct
4230 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
4232 struct ibmvfc_target *tgt = evt->tgt;
4233 struct ibmvfc_host *vhost = evt->vhost;
4234 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4235 u32 status = be16_to_cpu(mad->common.status);
4237 vhost->discovery_threads--;
4238 ibmvfc_free_event(evt);
4241 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
4242 * driver in which case we need to free up all the targets. If we are
4243 * not unloading, we will still go through a hard reset to get out of
4244 * offline state, so there is no need to track the old targets in that
4247 if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
4248 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4250 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
4252 tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
4253 kref_put(&tgt->kref, ibmvfc_release_tgt);
4254 wake_up(&vhost->work_wait_q);
4258 * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
4259 * @tgt: ibmvfc target struct
4262 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
4264 struct ibmvfc_host *vhost = tgt->vhost;
4265 struct ibmvfc_event *evt;
4267 if (!vhost->logged_in) {
4268 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4272 if (vhost->discovery_threads >= disc_threads)
4275 vhost->discovery_threads++;
4276 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
4277 ibmvfc_tgt_implicit_logout_and_del_done);
4279 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
4280 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4281 vhost->discovery_threads--;
4282 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4283 kref_put(&tgt->kref, ibmvfc_release_tgt);
4285 tgt_dbg(tgt, "Sent Implicit Logout\n");
4289 * ibmvfc_tgt_move_login_done - Completion handler for Move Login
4290 * @evt: ibmvfc event struct
4293 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
4295 struct ibmvfc_target *tgt = evt->tgt;
4296 struct ibmvfc_host *vhost = evt->vhost;
4297 struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
4298 u32 status = be16_to_cpu(rsp->common.status);
4299 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4301 vhost->discovery_threads--;
4302 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4304 case IBMVFC_MAD_SUCCESS:
4305 tgt_dbg(tgt, "Move Login succeeded for new scsi_id: %llX\n", tgt->new_scsi_id);
4306 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
4307 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
4308 tgt->scsi_id = tgt->new_scsi_id;
4309 tgt->ids.port_id = tgt->scsi_id;
4310 memcpy(&tgt->service_parms, &rsp->service_parms,
4311 sizeof(tgt->service_parms));
4312 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
4313 sizeof(tgt->service_parms_change));
4314 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
4316 case IBMVFC_MAD_DRIVER_FAILED:
4318 case IBMVFC_MAD_CRQ_ERROR:
4319 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4321 case IBMVFC_MAD_FAILED:
4323 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
4326 "Move Login failed: new scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
4327 tgt->new_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
4332 kref_put(&tgt->kref, ibmvfc_release_tgt);
4333 ibmvfc_free_event(evt);
4334 wake_up(&vhost->work_wait_q);
4339 * ibmvfc_tgt_move_login - Initiate a move login for specified target
4340 * @tgt: ibmvfc target struct
4343 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
4345 struct ibmvfc_host *vhost = tgt->vhost;
4346 struct ibmvfc_move_login *move;
4347 struct ibmvfc_event *evt;
4349 if (vhost->discovery_threads >= disc_threads)
4352 kref_get(&tgt->kref);
4353 evt = ibmvfc_get_event(&vhost->crq);
4354 vhost->discovery_threads++;
4355 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4356 ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
4358 move = &evt->iu.move_login;
4359 memset(move, 0, sizeof(*move));
4360 move->common.version = cpu_to_be32(1);
4361 move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
4362 move->common.length = cpu_to_be16(sizeof(*move));
4364 move->old_scsi_id = cpu_to_be64(tgt->scsi_id);
4365 move->new_scsi_id = cpu_to_be64(tgt->new_scsi_id);
4366 move->wwpn = cpu_to_be64(tgt->wwpn);
4367 move->node_name = cpu_to_be64(tgt->ids.node_name);
4369 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4370 vhost->discovery_threads--;
4371 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
4372 kref_put(&tgt->kref, ibmvfc_release_tgt);
4374 tgt_dbg(tgt, "Sent Move Login for new scsi_id: %llX\n", tgt->new_scsi_id);
4378 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
4379 * @mad: ibmvfc passthru mad struct
4380 * @tgt: ibmvfc target struct
4383 * 1 if PLOGI needed / 0 if PLOGI not needed
4385 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
4386 struct ibmvfc_target *tgt)
4388 if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
4390 if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
4392 if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
4398 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
4399 * @evt: ibmvfc event struct
4402 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
4404 struct ibmvfc_target *tgt = evt->tgt;
4405 struct ibmvfc_host *vhost = evt->vhost;
4406 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
4407 u32 status = be16_to_cpu(mad->common.status);
4408 u8 fc_reason, fc_explain;
4410 vhost->discovery_threads--;
4411 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4412 del_timer(&tgt->timer);
4415 case IBMVFC_MAD_SUCCESS:
4416 tgt_dbg(tgt, "ADISC succeeded\n");
4417 if (ibmvfc_adisc_needs_plogi(mad, tgt))
4418 ibmvfc_del_tgt(tgt);
4420 case IBMVFC_MAD_DRIVER_FAILED:
4422 case IBMVFC_MAD_FAILED:
4424 ibmvfc_del_tgt(tgt);
4425 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
4426 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
4427 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4428 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
4429 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
4430 ibmvfc_get_fc_type(fc_reason), fc_reason,
4431 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
4435 kref_put(&tgt->kref, ibmvfc_release_tgt);
4436 ibmvfc_free_event(evt);
4437 wake_up(&vhost->work_wait_q);
4441 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
4442 * @evt: ibmvfc event struct
4445 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
4447 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
4449 memset(mad, 0, sizeof(*mad));
4450 mad->common.version = cpu_to_be32(1);
4451 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
4452 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
4453 mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4454 offsetof(struct ibmvfc_passthru_mad, iu));
4455 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
4456 mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4457 mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
4458 mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4459 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4460 offsetof(struct ibmvfc_passthru_fc_iu, payload));
4461 mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
4462 mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
4463 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
4464 offsetof(struct ibmvfc_passthru_fc_iu, response));
4465 mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
4469 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
4470 * @evt: ibmvfc event struct
4472 * Just cleanup this event struct. Everything else is handled by
4473 * the ADISC completion handler. If the ADISC never actually comes
4474 * back, we still have the timer running on the ADISC event struct
4475 * which will fire and cause the CRQ to get reset.
4478 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
4480 struct ibmvfc_host *vhost = evt->vhost;
4481 struct ibmvfc_target *tgt = evt->tgt;
4483 tgt_dbg(tgt, "ADISC cancel complete\n");
4484 vhost->abort_threads--;
4485 ibmvfc_free_event(evt);
4486 kref_put(&tgt->kref, ibmvfc_release_tgt);
4487 wake_up(&vhost->work_wait_q);
4491 * ibmvfc_adisc_timeout - Handle an ADISC timeout
4492 * @t: ibmvfc target struct
4494 * If an ADISC times out, send a cancel. If the cancel times
4495 * out, reset the CRQ. When the ADISC comes back as cancelled,
4496 * log back into the target.
4498 static void ibmvfc_adisc_timeout(struct timer_list *t)
4500 struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
4501 struct ibmvfc_host *vhost = tgt->vhost;
4502 struct ibmvfc_event *evt;
4503 struct ibmvfc_tmf *tmf;
4504 unsigned long flags;
4507 tgt_dbg(tgt, "ADISC timeout\n");
4508 spin_lock_irqsave(vhost->host->host_lock, flags);
4509 if (vhost->abort_threads >= disc_threads ||
4510 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
4511 vhost->state != IBMVFC_INITIALIZING ||
4512 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
4513 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4517 vhost->abort_threads++;
4518 kref_get(&tgt->kref);
4519 evt = ibmvfc_get_event(&vhost->crq);
4520 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
4524 memset(tmf, 0, sizeof(*tmf));
4525 if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
4526 tmf->common.version = cpu_to_be32(2);
4527 tmf->target_wwpn = cpu_to_be64(tgt->wwpn);
4529 tmf->common.version = cpu_to_be32(1);
4531 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
4532 tmf->common.length = cpu_to_be16(sizeof(*tmf));
4533 tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
4534 tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
4536 rc = ibmvfc_send_event(evt, vhost, default_timeout);
4539 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
4540 vhost->abort_threads--;
4541 kref_put(&tgt->kref, ibmvfc_release_tgt);
4542 __ibmvfc_reset_host(vhost);
4544 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
4545 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4549 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
4550 * @tgt: ibmvfc target struct
4552 * When sending an ADISC we end up with two timers running. The
4553 * first timer is the timer in the ibmvfc target struct. If this
4554 * fires, we send a cancel to the target. The second timer is the
4555 * timer on the ibmvfc event for the ADISC, which is longer. If that
4556 * fires, it means the ADISC timed out and our attempt to cancel it
4557 * also failed, so we need to reset the CRQ.
4559 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
4561 struct ibmvfc_passthru_mad *mad;
4562 struct ibmvfc_host *vhost = tgt->vhost;
4563 struct ibmvfc_event *evt;
4565 if (vhost->discovery_threads >= disc_threads)
4568 kref_get(&tgt->kref);
4569 evt = ibmvfc_get_event(&vhost->crq);
4570 vhost->discovery_threads++;
4571 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
4574 ibmvfc_init_passthru(evt);
4575 mad = &evt->iu.passthru;
4576 mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4577 mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4578 mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4580 mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4581 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4582 sizeof(vhost->login_buf->resp.port_name));
4583 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4584 sizeof(vhost->login_buf->resp.node_name));
4585 mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4587 if (timer_pending(&tgt->timer))
4588 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4590 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4591 add_timer(&tgt->timer);
4594 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4595 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4596 vhost->discovery_threads--;
4597 del_timer(&tgt->timer);
4598 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4599 kref_put(&tgt->kref, ibmvfc_release_tgt);
4601 tgt_dbg(tgt, "Sent ADISC\n");
4605 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4606 * @evt: ibmvfc event struct
4609 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4611 struct ibmvfc_target *tgt = evt->tgt;
4612 struct ibmvfc_host *vhost = evt->vhost;
4613 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4614 u32 status = be16_to_cpu(rsp->common.status);
4615 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4617 vhost->discovery_threads--;
4618 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4620 case IBMVFC_MAD_SUCCESS:
4621 tgt_dbg(tgt, "Query Target succeeded\n");
4622 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4623 ibmvfc_del_tgt(tgt);
4625 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4627 case IBMVFC_MAD_DRIVER_FAILED:
4629 case IBMVFC_MAD_CRQ_ERROR:
4630 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4632 case IBMVFC_MAD_FAILED:
4634 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4635 be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4636 be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4637 ibmvfc_del_tgt(tgt);
4638 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4639 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4641 ibmvfc_del_tgt(tgt);
4643 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4644 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4645 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4646 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4647 ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4652 kref_put(&tgt->kref, ibmvfc_release_tgt);
4653 ibmvfc_free_event(evt);
4654 wake_up(&vhost->work_wait_q);
4658 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4659 * @tgt: ibmvfc target struct
4662 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4664 struct ibmvfc_query_tgt *query_tgt;
4665 struct ibmvfc_host *vhost = tgt->vhost;
4666 struct ibmvfc_event *evt;
4668 if (vhost->discovery_threads >= disc_threads)
4671 kref_get(&tgt->kref);
4672 evt = ibmvfc_get_event(&vhost->crq);
4673 vhost->discovery_threads++;
4675 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4676 query_tgt = &evt->iu.query_tgt;
4677 memset(query_tgt, 0, sizeof(*query_tgt));
4678 query_tgt->common.version = cpu_to_be32(1);
4679 query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4680 query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4681 query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4683 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4684 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4685 vhost->discovery_threads--;
4686 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4687 kref_put(&tgt->kref, ibmvfc_release_tgt);
4689 tgt_dbg(tgt, "Sent Query Target\n");
4693 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4694 * @vhost: ibmvfc host struct
4695 * @target: Holds SCSI ID to allocate target forand the WWPN
4698 * 0 on success / other on failure
4700 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4701 struct ibmvfc_discover_targets_entry *target)
4703 struct ibmvfc_target *stgt = NULL;
4704 struct ibmvfc_target *wtgt = NULL;
4705 struct ibmvfc_target *tgt;
4706 unsigned long flags;
4707 u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4708 u64 wwpn = be64_to_cpu(target->wwpn);
4710 /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4711 spin_lock_irqsave(vhost->host->host_lock, flags);
4712 list_for_each_entry(tgt, &vhost->targets, queue) {
4713 if (tgt->wwpn == wwpn) {
4719 list_for_each_entry(tgt, &vhost->targets, queue) {
4720 if (tgt->scsi_id == scsi_id) {
4726 if (wtgt && !stgt) {
4728 * A WWPN target has moved and we still are tracking the old
4729 * SCSI ID. The only way we should be able to get here is if
4730 * we attempted to send an implicit logout for the old SCSI ID
4731 * and it failed for some reason, such as there being I/O
4732 * pending to the target. In this case, we will have already
4733 * deleted the rport from the FC transport so we do a move
4734 * login, which works even with I/O pending, however, if
4735 * there is still I/O pending, it will stay outstanding, so
4736 * we only do this if fast fail is disabled for the rport,
4737 * otherwise we let terminate_rport_io clean up the port
4738 * before we login at the new location.
4740 if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4741 if (wtgt->move_login) {
4743 * Do a move login here. The old target is no longer
4744 * known to the transport layer We don't use the
4745 * normal ibmvfc_set_tgt_action to set this, as we
4746 * don't normally want to allow this state change.
4748 wtgt->new_scsi_id = scsi_id;
4749 wtgt->action = IBMVFC_TGT_ACTION_INIT;
4750 wtgt->init_retries = 0;
4751 ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4755 tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4756 wtgt->action, wtgt->rport);
4759 if (tgt->need_login)
4760 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4763 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4765 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4766 memset(tgt, 0, sizeof(*tgt));
4767 tgt->scsi_id = scsi_id;
4770 tgt->need_login = 1;
4771 timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4772 kref_init(&tgt->kref);
4773 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4774 spin_lock_irqsave(vhost->host->host_lock, flags);
4775 tgt->cancel_key = vhost->task_set++;
4776 list_add_tail(&tgt->queue, &vhost->targets);
4779 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4784 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4785 * @vhost: ibmvfc host struct
4788 * 0 on success / other on failure
4790 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4794 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4795 rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
4801 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4802 * @evt: ibmvfc event struct
4805 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4807 struct ibmvfc_host *vhost = evt->vhost;
4808 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4809 u32 mad_status = be16_to_cpu(rsp->common.status);
4810 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4812 switch (mad_status) {
4813 case IBMVFC_MAD_SUCCESS:
4814 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4815 vhost->num_targets = be32_to_cpu(rsp->num_written);
4816 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4818 case IBMVFC_MAD_FAILED:
4819 level += ibmvfc_retry_host_init(vhost);
4820 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4821 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4822 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4824 case IBMVFC_MAD_DRIVER_FAILED:
4827 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4828 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4832 ibmvfc_free_event(evt);
4833 wake_up(&vhost->work_wait_q);
4837 * ibmvfc_discover_targets - Send Discover Targets MAD
4838 * @vhost: ibmvfc host struct
4841 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4843 struct ibmvfc_discover_targets *mad;
4844 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4846 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4847 mad = &evt->iu.discover_targets;
4848 memset(mad, 0, sizeof(*mad));
4849 mad->common.version = cpu_to_be32(1);
4850 mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4851 mad->common.length = cpu_to_be16(sizeof(*mad));
4852 mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4853 mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4854 mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4855 mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
4856 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4858 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4859 ibmvfc_dbg(vhost, "Sent discover targets\n");
4861 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4864 static void ibmvfc_channel_setup_done(struct ibmvfc_event *evt)
4866 struct ibmvfc_host *vhost = evt->vhost;
4867 struct ibmvfc_channel_setup *setup = vhost->channel_setup_buf;
4868 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
4869 u32 mad_status = be16_to_cpu(evt->xfer_iu->channel_setup.common.status);
4870 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4871 int flags, active_queues, i;
4873 ibmvfc_free_event(evt);
4875 switch (mad_status) {
4876 case IBMVFC_MAD_SUCCESS:
4877 ibmvfc_dbg(vhost, "Channel Setup succeeded\n");
4878 flags = be32_to_cpu(setup->flags);
4879 vhost->do_enquiry = 0;
4880 active_queues = be32_to_cpu(setup->num_scsi_subq_channels);
4881 scrqs->active_queues = active_queues;
4883 if (flags & IBMVFC_CHANNELS_CANCELED) {
4884 ibmvfc_dbg(vhost, "Channels Canceled\n");
4885 vhost->using_channels = 0;
4888 vhost->using_channels = 1;
4889 for (i = 0; i < active_queues; i++)
4890 scrqs->scrqs[i].vios_cookie =
4891 be64_to_cpu(setup->channel_handles[i]);
4893 ibmvfc_dbg(vhost, "Using %u channels\n",
4894 vhost->scsi_scrqs.active_queues);
4897 case IBMVFC_MAD_FAILED:
4898 level += ibmvfc_retry_host_init(vhost);
4899 ibmvfc_log(vhost, level, "Channel Setup failed\n");
4901 case IBMVFC_MAD_DRIVER_FAILED:
4904 dev_err(vhost->dev, "Invalid Channel Setup response: 0x%x\n",
4906 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4910 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4911 wake_up(&vhost->work_wait_q);
4914 static void ibmvfc_channel_setup(struct ibmvfc_host *vhost)
4916 struct ibmvfc_channel_setup_mad *mad;
4917 struct ibmvfc_channel_setup *setup_buf = vhost->channel_setup_buf;
4918 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4919 struct ibmvfc_scsi_channels *scrqs = &vhost->scsi_scrqs;
4920 unsigned int num_channels =
4921 min(vhost->client_scsi_channels, vhost->max_vios_scsi_channels);
4924 memset(setup_buf, 0, sizeof(*setup_buf));
4925 if (num_channels == 0)
4926 setup_buf->flags = cpu_to_be32(IBMVFC_CANCEL_CHANNELS);
4928 setup_buf->num_scsi_subq_channels = cpu_to_be32(num_channels);
4929 for (i = 0; i < num_channels; i++)
4930 setup_buf->channel_handles[i] = cpu_to_be64(scrqs->scrqs[i].cookie);
4933 ibmvfc_init_event(evt, ibmvfc_channel_setup_done, IBMVFC_MAD_FORMAT);
4934 mad = &evt->iu.channel_setup;
4935 memset(mad, 0, sizeof(*mad));
4936 mad->common.version = cpu_to_be32(1);
4937 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_SETUP);
4938 mad->common.length = cpu_to_be16(sizeof(*mad));
4939 mad->buffer.va = cpu_to_be64(vhost->channel_setup_dma);
4940 mad->buffer.len = cpu_to_be32(sizeof(*vhost->channel_setup_buf));
4942 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4944 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4945 ibmvfc_dbg(vhost, "Sent channel setup\n");
4947 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
4950 static void ibmvfc_channel_enquiry_done(struct ibmvfc_event *evt)
4952 struct ibmvfc_host *vhost = evt->vhost;
4953 struct ibmvfc_channel_enquiry *rsp = &evt->xfer_iu->channel_enquiry;
4954 u32 mad_status = be16_to_cpu(rsp->common.status);
4955 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4957 switch (mad_status) {
4958 case IBMVFC_MAD_SUCCESS:
4959 ibmvfc_dbg(vhost, "Channel Enquiry succeeded\n");
4960 vhost->max_vios_scsi_channels = be32_to_cpu(rsp->num_scsi_subq_channels);
4961 ibmvfc_free_event(evt);
4963 case IBMVFC_MAD_FAILED:
4964 level += ibmvfc_retry_host_init(vhost);
4965 ibmvfc_log(vhost, level, "Channel Enquiry failed\n");
4967 case IBMVFC_MAD_DRIVER_FAILED:
4968 ibmvfc_free_event(evt);
4971 dev_err(vhost->dev, "Invalid Channel Enquiry response: 0x%x\n",
4973 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4974 ibmvfc_free_event(evt);
4978 ibmvfc_channel_setup(vhost);
4981 static void ibmvfc_channel_enquiry(struct ibmvfc_host *vhost)
4983 struct ibmvfc_channel_enquiry *mad;
4984 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
4986 ibmvfc_init_event(evt, ibmvfc_channel_enquiry_done, IBMVFC_MAD_FORMAT);
4987 mad = &evt->iu.channel_enquiry;
4988 memset(mad, 0, sizeof(*mad));
4989 mad->common.version = cpu_to_be32(1);
4990 mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_ENQUIRY);
4991 mad->common.length = cpu_to_be16(sizeof(*mad));
4993 if (mig_channels_only)
4994 mad->flags |= cpu_to_be32(IBMVFC_NO_CHANNELS_TO_CRQ_SUPPORT);
4995 if (mig_no_less_channels)
4996 mad->flags |= cpu_to_be32(IBMVFC_NO_N_TO_M_CHANNELS_SUPPORT);
4998 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5000 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5001 ibmvfc_dbg(vhost, "Send channel enquiry\n");
5003 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5007 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
5008 * @evt: ibmvfc event struct
5011 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
5013 struct ibmvfc_host *vhost = evt->vhost;
5014 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
5015 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
5016 unsigned int npiv_max_sectors;
5017 int level = IBMVFC_DEFAULT_LOG_LEVEL;
5019 switch (mad_status) {
5020 case IBMVFC_MAD_SUCCESS:
5021 ibmvfc_free_event(evt);
5023 case IBMVFC_MAD_FAILED:
5024 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
5025 level += ibmvfc_retry_host_init(vhost);
5027 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5028 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
5029 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
5030 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
5031 ibmvfc_free_event(evt);
5033 case IBMVFC_MAD_CRQ_ERROR:
5034 ibmvfc_retry_host_init(vhost);
5036 case IBMVFC_MAD_DRIVER_FAILED:
5037 ibmvfc_free_event(evt);
5040 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
5041 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5042 ibmvfc_free_event(evt);
5046 vhost->client_migrated = 0;
5048 if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
5049 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
5051 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5052 wake_up(&vhost->work_wait_q);
5056 if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
5057 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
5059 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5060 wake_up(&vhost->work_wait_q);
5064 vhost->logged_in = 1;
5065 npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
5066 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
5067 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
5068 rsp->drc_name, npiv_max_sectors);
5070 fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
5071 fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
5072 fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
5073 fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
5074 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
5075 fc_host_supported_classes(vhost->host) = 0;
5076 if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
5077 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
5078 if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
5079 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
5080 if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
5081 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
5082 fc_host_maxframe_size(vhost->host) =
5083 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
5085 vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
5086 vhost->host->max_sectors = npiv_max_sectors;
5088 if (ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPORT_CHANNELS) && vhost->do_enquiry) {
5089 ibmvfc_channel_enquiry(vhost);
5091 vhost->do_enquiry = 0;
5092 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5093 wake_up(&vhost->work_wait_q);
5098 * ibmvfc_npiv_login - Sends NPIV login
5099 * @vhost: ibmvfc host struct
5102 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
5104 struct ibmvfc_npiv_login_mad *mad;
5105 struct ibmvfc_event *evt = ibmvfc_get_event(&vhost->crq);
5107 ibmvfc_gather_partition_info(vhost);
5108 ibmvfc_set_login_info(vhost);
5109 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
5111 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
5112 mad = &evt->iu.npiv_login;
5113 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
5114 mad->common.version = cpu_to_be32(1);
5115 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
5116 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
5117 mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
5118 mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
5120 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
5122 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5123 ibmvfc_dbg(vhost, "Sent NPIV login\n");
5125 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5129 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
5130 * @evt: ibmvfc event struct
5133 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
5135 struct ibmvfc_host *vhost = evt->vhost;
5136 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
5138 ibmvfc_free_event(evt);
5140 switch (mad_status) {
5141 case IBMVFC_MAD_SUCCESS:
5142 if (list_empty(&vhost->crq.sent) &&
5143 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
5144 ibmvfc_init_host(vhost);
5148 case IBMVFC_MAD_FAILED:
5149 case IBMVFC_MAD_NOT_SUPPORTED:
5150 case IBMVFC_MAD_CRQ_ERROR:
5151 case IBMVFC_MAD_DRIVER_FAILED:
5153 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
5157 ibmvfc_hard_reset_host(vhost);
5161 * ibmvfc_npiv_logout - Issue an NPIV Logout
5162 * @vhost: ibmvfc host struct
5165 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
5167 struct ibmvfc_npiv_logout_mad *mad;
5168 struct ibmvfc_event *evt;
5170 evt = ibmvfc_get_event(&vhost->crq);
5171 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
5173 mad = &evt->iu.npiv_logout;
5174 memset(mad, 0, sizeof(*mad));
5175 mad->common.version = cpu_to_be32(1);
5176 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
5177 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
5179 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
5181 if (!ibmvfc_send_event(evt, vhost, default_timeout))
5182 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
5184 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5188 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
5189 * @vhost: ibmvfc host struct
5192 * 1 if work to do / 0 if not
5194 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
5196 struct ibmvfc_target *tgt;
5198 list_for_each_entry(tgt, &vhost->targets, queue) {
5199 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
5200 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5208 * ibmvfc_dev_logo_to_do - Is there target logout work to do?
5209 * @vhost: ibmvfc host struct
5212 * 1 if work to do / 0 if not
5214 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
5216 struct ibmvfc_target *tgt;
5218 list_for_each_entry(tgt, &vhost->targets, queue) {
5219 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
5220 tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5227 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
5228 * @vhost: ibmvfc host struct
5231 * 1 if work to do / 0 if not
5233 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5235 struct ibmvfc_target *tgt;
5237 if (kthread_should_stop())
5239 switch (vhost->action) {
5240 case IBMVFC_HOST_ACTION_NONE:
5241 case IBMVFC_HOST_ACTION_INIT_WAIT:
5242 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5244 case IBMVFC_HOST_ACTION_TGT_INIT:
5245 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5246 if (vhost->discovery_threads == disc_threads)
5248 list_for_each_entry(tgt, &vhost->targets, queue)
5249 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
5251 list_for_each_entry(tgt, &vhost->targets, queue)
5252 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
5255 case IBMVFC_HOST_ACTION_TGT_DEL:
5256 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5257 if (vhost->discovery_threads == disc_threads)
5259 list_for_each_entry(tgt, &vhost->targets, queue)
5260 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
5262 list_for_each_entry(tgt, &vhost->targets, queue)
5263 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
5266 case IBMVFC_HOST_ACTION_LOGO:
5267 case IBMVFC_HOST_ACTION_INIT:
5268 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5269 case IBMVFC_HOST_ACTION_QUERY:
5270 case IBMVFC_HOST_ACTION_RESET:
5271 case IBMVFC_HOST_ACTION_REENABLE:
5280 * ibmvfc_work_to_do - Is there task level work to do?
5281 * @vhost: ibmvfc host struct
5284 * 1 if work to do / 0 if not
5286 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
5288 unsigned long flags;
5291 spin_lock_irqsave(vhost->host->host_lock, flags);
5292 rc = __ibmvfc_work_to_do(vhost);
5293 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5298 * ibmvfc_log_ae - Log async events if necessary
5299 * @vhost: ibmvfc host struct
5300 * @events: events to log
5303 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
5305 if (events & IBMVFC_AE_RSCN)
5306 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
5307 if ((events & IBMVFC_AE_LINKDOWN) &&
5308 vhost->state >= IBMVFC_HALTED)
5309 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
5310 if ((events & IBMVFC_AE_LINKUP) &&
5311 vhost->state == IBMVFC_INITIALIZING)
5312 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
5316 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
5317 * @tgt: ibmvfc target struct
5320 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
5322 struct ibmvfc_host *vhost = tgt->vhost;
5323 struct fc_rport *rport;
5324 unsigned long flags;
5326 tgt_dbg(tgt, "Adding rport\n");
5327 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
5328 spin_lock_irqsave(vhost->host->host_lock, flags);
5330 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5331 tgt_dbg(tgt, "Deleting rport\n");
5332 list_del(&tgt->queue);
5333 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5334 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5335 fc_remote_port_delete(rport);
5336 del_timer_sync(&tgt->timer);
5337 kref_put(&tgt->kref, ibmvfc_release_tgt);
5339 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5340 tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
5341 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5343 tgt->init_retries = 0;
5344 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5345 fc_remote_port_delete(rport);
5347 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
5348 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5353 tgt_dbg(tgt, "rport add succeeded\n");
5355 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
5356 rport->supported_classes = 0;
5357 tgt->target_id = rport->scsi_target_id;
5358 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
5359 rport->supported_classes |= FC_COS_CLASS1;
5360 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
5361 rport->supported_classes |= FC_COS_CLASS2;
5362 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
5363 rport->supported_classes |= FC_COS_CLASS3;
5365 blk_queue_max_segments(rport->rqst_q, 1);
5367 tgt_dbg(tgt, "rport add failed\n");
5368 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5372 * ibmvfc_do_work - Do task level work
5373 * @vhost: ibmvfc host struct
5376 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
5378 struct ibmvfc_target *tgt;
5379 unsigned long flags;
5380 struct fc_rport *rport;
5384 ibmvfc_log_ae(vhost, vhost->events_to_log);
5385 spin_lock_irqsave(vhost->host->host_lock, flags);
5386 vhost->events_to_log = 0;
5387 switch (vhost->action) {
5388 case IBMVFC_HOST_ACTION_NONE:
5389 case IBMVFC_HOST_ACTION_LOGO_WAIT:
5390 case IBMVFC_HOST_ACTION_INIT_WAIT:
5392 case IBMVFC_HOST_ACTION_RESET:
5393 list_splice_init(&vhost->purge, &purge);
5394 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5395 ibmvfc_complete_purge(&purge);
5396 rc = ibmvfc_reset_crq(vhost);
5398 spin_lock_irqsave(vhost->host->host_lock, flags);
5399 if (!rc || rc == H_CLOSED)
5400 vio_enable_interrupts(to_vio_dev(vhost->dev));
5401 if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
5403 * The only action we could have changed to would have
5404 * been reenable, in which case, we skip the rest of
5405 * this path and wait until we've done the re-enable
5406 * before sending the crq init.
5408 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5410 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
5411 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
5412 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5413 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
5417 case IBMVFC_HOST_ACTION_REENABLE:
5418 list_splice_init(&vhost->purge, &purge);
5419 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5420 ibmvfc_complete_purge(&purge);
5421 rc = ibmvfc_reenable_crq_queue(vhost);
5423 spin_lock_irqsave(vhost->host->host_lock, flags);
5424 if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
5426 * The only action we could have changed to would have
5427 * been reset, in which case, we skip the rest of this
5428 * path and wait until we've done the reset before
5429 * sending the crq init.
5431 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
5432 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
5433 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
5434 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
5438 case IBMVFC_HOST_ACTION_LOGO:
5439 vhost->job_step(vhost);
5441 case IBMVFC_HOST_ACTION_INIT:
5442 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
5443 if (vhost->delay_init) {
5444 vhost->delay_init = 0;
5445 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5449 vhost->job_step(vhost);
5451 case IBMVFC_HOST_ACTION_QUERY:
5452 list_for_each_entry(tgt, &vhost->targets, queue)
5453 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
5454 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
5456 case IBMVFC_HOST_ACTION_QUERY_TGTS:
5457 list_for_each_entry(tgt, &vhost->targets, queue) {
5458 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5464 if (!ibmvfc_dev_init_to_do(vhost))
5465 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
5467 case IBMVFC_HOST_ACTION_TGT_DEL:
5468 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
5469 list_for_each_entry(tgt, &vhost->targets, queue) {
5470 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
5476 if (ibmvfc_dev_logo_to_do(vhost)) {
5477 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5481 list_for_each_entry(tgt, &vhost->targets, queue) {
5482 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
5483 tgt_dbg(tgt, "Deleting rport\n");
5486 list_del(&tgt->queue);
5487 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
5488 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5490 fc_remote_port_delete(rport);
5491 del_timer_sync(&tgt->timer);
5492 kref_put(&tgt->kref, ibmvfc_release_tgt);
5494 } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
5495 tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
5498 tgt->init_retries = 0;
5499 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
5502 * If fast fail is enabled, we wait for it to fire and then clean up
5503 * the old port, since we expect the fast fail timer to clean up the
5504 * outstanding I/O faster than waiting for normal command timeouts.
5505 * However, if fast fail is disabled, any I/O outstanding to the
5506 * rport LUNs will stay outstanding indefinitely, since the EH handlers
5507 * won't get invoked for I/O's timing out. If this is a NPIV failover
5508 * scenario, the better alternative is to use the move login.
5510 if (rport && rport->fast_io_fail_tmo == -1)
5511 tgt->move_login = 1;
5512 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5514 fc_remote_port_delete(rport);
5519 if (vhost->state == IBMVFC_INITIALIZING) {
5520 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
5521 if (vhost->reinit) {
5523 scsi_block_requests(vhost->host);
5524 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
5525 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5527 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
5528 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5529 wake_up(&vhost->init_wait_q);
5530 schedule_work(&vhost->rport_add_work_q);
5531 vhost->init_retries = 0;
5532 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5533 scsi_unblock_requests(vhost->host);
5538 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
5539 vhost->job_step = ibmvfc_discover_targets;
5542 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
5543 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5544 scsi_unblock_requests(vhost->host);
5545 wake_up(&vhost->init_wait_q);
5549 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
5550 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
5551 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5552 ibmvfc_alloc_targets(vhost);
5553 spin_lock_irqsave(vhost->host->host_lock, flags);
5555 case IBMVFC_HOST_ACTION_TGT_INIT:
5556 list_for_each_entry(tgt, &vhost->targets, queue) {
5557 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
5563 if (!ibmvfc_dev_init_to_do(vhost))
5564 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
5570 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5574 * ibmvfc_work - Do task level work
5575 * @data: ibmvfc host struct
5580 static int ibmvfc_work(void *data)
5582 struct ibmvfc_host *vhost = data;
5585 set_user_nice(current, MIN_NICE);
5588 rc = wait_event_interruptible(vhost->work_wait_q,
5589 ibmvfc_work_to_do(vhost));
5593 if (kthread_should_stop())
5596 ibmvfc_do_work(vhost);
5599 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
5604 * ibmvfc_alloc_queue - Allocate queue
5605 * @vhost: ibmvfc host struct
5606 * @queue: ibmvfc queue to allocate
5607 * @fmt: queue format to allocate
5610 * 0 on success / non-zero on failure
5612 static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
5613 struct ibmvfc_queue *queue,
5614 enum ibmvfc_msg_fmt fmt)
5616 struct device *dev = vhost->dev;
5618 unsigned int pool_size = 0;
5621 spin_lock_init(&queue->_lock);
5622 queue->q_lock = &queue->_lock;
5625 case IBMVFC_CRQ_FMT:
5626 fmt_size = sizeof(*queue->msgs.crq);
5627 pool_size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
5629 case IBMVFC_ASYNC_FMT:
5630 fmt_size = sizeof(*queue->msgs.async);
5632 case IBMVFC_SUB_CRQ_FMT:
5633 fmt_size = sizeof(*queue->msgs.scrq);
5634 /* We need one extra event for Cancel Commands */
5635 pool_size = max_requests + 1;
5638 dev_warn(dev, "Unknown command/response queue message format: %d\n", fmt);
5642 if (ibmvfc_init_event_pool(vhost, queue, pool_size)) {
5643 dev_err(dev, "Couldn't initialize event pool.\n");
5647 queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
5648 if (!queue->msgs.handle)
5651 queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
5654 if (dma_mapping_error(dev, queue->msg_token)) {
5655 free_page((unsigned long)queue->msgs.handle);
5656 queue->msgs.handle = NULL;
5662 queue->size = PAGE_SIZE / fmt_size;
5667 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
5668 * @vhost: ibmvfc host struct
5670 * Allocates a page for messages, maps it for dma, and registers
5671 * the crq with the hypervisor.
5674 * zero on success / other on failure
5676 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
5678 int rc, retrc = -ENOMEM;
5679 struct device *dev = vhost->dev;
5680 struct vio_dev *vdev = to_vio_dev(dev);
5681 struct ibmvfc_queue *crq = &vhost->crq;
5684 if (ibmvfc_alloc_queue(vhost, crq, IBMVFC_CRQ_FMT))
5687 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5688 crq->msg_token, PAGE_SIZE);
5690 if (rc == H_RESOURCE)
5691 /* maybe kexecing and resource is busy. try a reset */
5692 retrc = rc = ibmvfc_reset_crq(vhost);
5695 dev_warn(dev, "Partner adapter not ready\n");
5697 dev_warn(dev, "Error %d opening adapter\n", rc);
5698 goto reg_crq_failed;
5703 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
5705 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
5706 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
5707 goto req_irq_failed;
5710 if ((rc = vio_enable_interrupts(vdev))) {
5711 dev_err(dev, "Error %d enabling interrupts\n", rc);
5712 goto req_irq_failed;
5719 tasklet_kill(&vhost->tasklet);
5721 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5722 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5724 ibmvfc_free_queue(vhost, crq);
5728 static int ibmvfc_register_scsi_channel(struct ibmvfc_host *vhost,
5731 struct device *dev = vhost->dev;
5732 struct vio_dev *vdev = to_vio_dev(dev);
5733 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5738 if (ibmvfc_alloc_queue(vhost, scrq, IBMVFC_SUB_CRQ_FMT))
5741 rc = h_reg_sub_crq(vdev->unit_address, scrq->msg_token, PAGE_SIZE,
5742 &scrq->cookie, &scrq->hw_irq);
5744 /* H_CLOSED indicates successful register, but no CRQ partner */
5745 if (rc && rc != H_CLOSED) {
5746 dev_warn(dev, "Error registering sub-crq: %d\n", rc);
5747 if (rc == H_PARAMETER)
5748 dev_warn_once(dev, "Firmware may not support MQ\n");
5752 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
5756 dev_err(dev, "Error mapping sub-crq[%d] irq\n", index);
5760 snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-scsi%d",
5761 vdev->unit_address, index);
5762 rc = request_irq(scrq->irq, ibmvfc_interrupt_scsi, 0, scrq->name, scrq);
5765 dev_err(dev, "Couldn't register sub-crq[%d] irq\n", index);
5766 irq_dispose_mapping(scrq->irq);
5770 scrq->hwq_id = index;
5771 scrq->vhost = vhost;
5778 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address, scrq->cookie);
5779 } while (rtas_busy_delay(rc));
5781 ibmvfc_free_queue(vhost, scrq);
5786 static void ibmvfc_deregister_scsi_channel(struct ibmvfc_host *vhost, int index)
5788 struct device *dev = vhost->dev;
5789 struct vio_dev *vdev = to_vio_dev(dev);
5790 struct ibmvfc_queue *scrq = &vhost->scsi_scrqs.scrqs[index];
5795 free_irq(scrq->irq, scrq);
5796 irq_dispose_mapping(scrq->irq);
5800 rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address,
5802 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5805 dev_err(dev, "Failed to free sub-crq[%d]: rc=%ld\n", index, rc);
5807 ibmvfc_free_queue(vhost, scrq);
5811 static void ibmvfc_init_sub_crqs(struct ibmvfc_host *vhost)
5816 if (!vhost->mq_enabled)
5819 vhost->scsi_scrqs.scrqs = kcalloc(nr_scsi_hw_queues,
5820 sizeof(*vhost->scsi_scrqs.scrqs),
5822 if (!vhost->scsi_scrqs.scrqs) {
5823 vhost->do_enquiry = 0;
5827 for (i = 0; i < nr_scsi_hw_queues; i++) {
5828 if (ibmvfc_register_scsi_channel(vhost, i)) {
5829 for (j = i; j > 0; j--)
5830 ibmvfc_deregister_scsi_channel(vhost, j - 1);
5831 kfree(vhost->scsi_scrqs.scrqs);
5832 vhost->scsi_scrqs.scrqs = NULL;
5833 vhost->scsi_scrqs.active_queues = 0;
5834 vhost->do_enquiry = 0;
5842 static void ibmvfc_release_sub_crqs(struct ibmvfc_host *vhost)
5847 if (!vhost->scsi_scrqs.scrqs)
5850 for (i = 0; i < nr_scsi_hw_queues; i++)
5851 ibmvfc_deregister_scsi_channel(vhost, i);
5853 kfree(vhost->scsi_scrqs.scrqs);
5854 vhost->scsi_scrqs.scrqs = NULL;
5855 vhost->scsi_scrqs.active_queues = 0;
5860 * ibmvfc_free_mem - Free memory for vhost
5861 * @vhost: ibmvfc host struct
5866 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
5868 struct ibmvfc_queue *async_q = &vhost->async_crq;
5871 mempool_destroy(vhost->tgt_pool);
5872 kfree(vhost->trace);
5873 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
5874 vhost->disc_buf_dma);
5875 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
5876 vhost->login_buf, vhost->login_buf_dma);
5877 dma_free_coherent(vhost->dev, sizeof(*vhost->channel_setup_buf),
5878 vhost->channel_setup_buf, vhost->channel_setup_dma);
5879 dma_pool_destroy(vhost->sg_pool);
5880 ibmvfc_free_queue(vhost, async_q);
5885 * ibmvfc_alloc_mem - Allocate memory for vhost
5886 * @vhost: ibmvfc host struct
5889 * 0 on success / non-zero on failure
5891 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
5893 struct ibmvfc_queue *async_q = &vhost->async_crq;
5894 struct device *dev = vhost->dev;
5897 if (ibmvfc_alloc_queue(vhost, async_q, IBMVFC_ASYNC_FMT)) {
5898 dev_err(dev, "Couldn't allocate/map async queue.\n");
5902 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
5903 SG_ALL * sizeof(struct srp_direct_buf),
5904 sizeof(struct srp_direct_buf), 0);
5906 if (!vhost->sg_pool) {
5907 dev_err(dev, "Failed to allocate sg pool\n");
5908 goto unmap_async_crq;
5911 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
5912 &vhost->login_buf_dma, GFP_KERNEL);
5914 if (!vhost->login_buf) {
5915 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
5919 vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
5920 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
5921 &vhost->disc_buf_dma, GFP_KERNEL);
5923 if (!vhost->disc_buf) {
5924 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
5925 goto free_login_buffer;
5928 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
5929 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
5930 atomic_set(&vhost->trace_index, -1);
5933 goto free_disc_buffer;
5935 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
5936 sizeof(struct ibmvfc_target));
5938 if (!vhost->tgt_pool) {
5939 dev_err(dev, "Couldn't allocate target memory pool\n");
5943 vhost->channel_setup_buf = dma_alloc_coherent(dev, sizeof(*vhost->channel_setup_buf),
5944 &vhost->channel_setup_dma,
5947 if (!vhost->channel_setup_buf) {
5948 dev_err(dev, "Couldn't allocate Channel Setup buffer\n");
5956 mempool_destroy(vhost->tgt_pool);
5958 kfree(vhost->trace);
5960 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
5961 vhost->disc_buf_dma);
5963 dma_free_coherent(dev, sizeof(*vhost->login_buf),
5964 vhost->login_buf, vhost->login_buf_dma);
5966 dma_pool_destroy(vhost->sg_pool);
5968 ibmvfc_free_queue(vhost, async_q);
5975 * ibmvfc_rport_add_thread - Worker thread for rport adds
5976 * @work: work struct
5979 static void ibmvfc_rport_add_thread(struct work_struct *work)
5981 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
5983 struct ibmvfc_target *tgt;
5984 struct fc_rport *rport;
5985 unsigned long flags;
5989 spin_lock_irqsave(vhost->host->host_lock, flags);
5992 if (vhost->state != IBMVFC_ACTIVE)
5995 list_for_each_entry(tgt, &vhost->targets, queue) {
5996 if (tgt->add_rport) {
5999 kref_get(&tgt->kref);
6002 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6003 ibmvfc_tgt_add_rport(tgt);
6004 } else if (get_device(&rport->dev)) {
6005 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6006 tgt_dbg(tgt, "Setting rport roles\n");
6007 fc_remote_port_rolechg(rport, tgt->ids.roles);
6008 put_device(&rport->dev);
6010 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6013 kref_put(&tgt->kref, ibmvfc_release_tgt);
6014 spin_lock_irqsave(vhost->host->host_lock, flags);
6020 if (vhost->state == IBMVFC_ACTIVE)
6021 vhost->scan_complete = 1;
6022 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6027 * ibmvfc_probe - Adapter hot plug add entry point
6028 * @vdev: vio device struct
6029 * @id: vio device id struct
6032 * 0 on success / non-zero on failure
6034 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
6036 struct ibmvfc_host *vhost;
6037 struct Scsi_Host *shost;
6038 struct device *dev = &vdev->dev;
6040 unsigned int max_scsi_queues = IBMVFC_MAX_SCSI_QUEUES;
6043 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
6045 dev_err(dev, "Couldn't allocate host data\n");
6049 shost->transportt = ibmvfc_transport_template;
6050 shost->can_queue = max_requests;
6051 shost->max_lun = max_lun;
6052 shost->max_id = max_targets;
6053 shost->max_sectors = IBMVFC_MAX_SECTORS;
6054 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
6055 shost->unique_id = shost->host_no;
6056 shost->nr_hw_queues = mq_enabled ? min(max_scsi_queues, nr_scsi_hw_queues) : 1;
6058 vhost = shost_priv(shost);
6059 INIT_LIST_HEAD(&vhost->targets);
6060 INIT_LIST_HEAD(&vhost->purge);
6061 sprintf(vhost->name, IBMVFC_NAME);
6062 vhost->host = shost;
6064 vhost->partition_number = -1;
6065 vhost->log_level = log_level;
6066 vhost->task_set = 1;
6068 vhost->mq_enabled = mq_enabled;
6069 vhost->client_scsi_channels = min(shost->nr_hw_queues, nr_scsi_channels);
6070 vhost->using_channels = 0;
6071 vhost->do_enquiry = 1;
6073 strcpy(vhost->partition_name, "UNKNOWN");
6074 init_waitqueue_head(&vhost->work_wait_q);
6075 init_waitqueue_head(&vhost->init_wait_q);
6076 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
6077 mutex_init(&vhost->passthru_mutex);
6079 if ((rc = ibmvfc_alloc_mem(vhost)))
6080 goto free_scsi_host;
6082 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
6085 if (IS_ERR(vhost->work_thread)) {
6086 dev_err(dev, "Couldn't create kernel thread: %ld\n",
6087 PTR_ERR(vhost->work_thread));
6088 rc = PTR_ERR(vhost->work_thread);
6092 if ((rc = ibmvfc_init_crq(vhost))) {
6093 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
6097 if ((rc = scsi_add_host(shost, dev)))
6100 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
6102 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
6103 &ibmvfc_trace_attr))) {
6104 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
6108 ibmvfc_init_sub_crqs(vhost);
6110 if (shost_to_fc_host(shost)->rqst_q)
6111 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
6112 dev_set_drvdata(dev, vhost);
6113 spin_lock(&ibmvfc_driver_lock);
6114 list_add_tail(&vhost->queue, &ibmvfc_head);
6115 spin_unlock(&ibmvfc_driver_lock);
6117 ibmvfc_send_crq_init(vhost);
6118 scsi_scan_host(shost);
6122 scsi_remove_host(shost);
6124 ibmvfc_release_crq_queue(vhost);
6126 kthread_stop(vhost->work_thread);
6128 ibmvfc_free_mem(vhost);
6130 scsi_host_put(shost);
6137 * ibmvfc_remove - Adapter hot plug remove entry point
6138 * @vdev: vio device struct
6143 static void ibmvfc_remove(struct vio_dev *vdev)
6145 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
6147 unsigned long flags;
6150 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
6152 spin_lock_irqsave(vhost->host->host_lock, flags);
6153 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
6154 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6156 ibmvfc_wait_while_resetting(vhost);
6157 kthread_stop(vhost->work_thread);
6158 fc_remove_host(vhost->host);
6159 scsi_remove_host(vhost->host);
6161 spin_lock_irqsave(vhost->host->host_lock, flags);
6162 ibmvfc_purge_requests(vhost, DID_ERROR);
6163 list_splice_init(&vhost->purge, &purge);
6164 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6165 ibmvfc_complete_purge(&purge);
6166 ibmvfc_release_sub_crqs(vhost);
6167 ibmvfc_release_crq_queue(vhost);
6169 ibmvfc_free_mem(vhost);
6170 spin_lock(&ibmvfc_driver_lock);
6171 list_del(&vhost->queue);
6172 spin_unlock(&ibmvfc_driver_lock);
6173 scsi_host_put(vhost->host);
6178 * ibmvfc_resume - Resume from suspend
6179 * @dev: device struct
6181 * We may have lost an interrupt across suspend/resume, so kick the
6185 static int ibmvfc_resume(struct device *dev)
6187 unsigned long flags;
6188 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
6189 struct vio_dev *vdev = to_vio_dev(dev);
6191 spin_lock_irqsave(vhost->host->host_lock, flags);
6192 vio_disable_interrupts(vdev);
6193 tasklet_schedule(&vhost->tasklet);
6194 spin_unlock_irqrestore(vhost->host->host_lock, flags);
6199 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
6200 * @vdev: vio device struct
6203 * Number of bytes the driver will need to DMA map at the same time in
6204 * order to perform well.
6206 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
6208 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
6209 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
6212 static const struct vio_device_id ibmvfc_device_table[] = {
6213 {"fcp", "IBM,vfc-client"},
6216 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
6218 static const struct dev_pm_ops ibmvfc_pm_ops = {
6219 .resume = ibmvfc_resume
6222 static struct vio_driver ibmvfc_driver = {
6223 .id_table = ibmvfc_device_table,
6224 .probe = ibmvfc_probe,
6225 .remove = ibmvfc_remove,
6226 .get_desired_dma = ibmvfc_get_desired_dma,
6227 .name = IBMVFC_NAME,
6228 .pm = &ibmvfc_pm_ops,
6231 static struct fc_function_template ibmvfc_transport_functions = {
6232 .show_host_fabric_name = 1,
6233 .show_host_node_name = 1,
6234 .show_host_port_name = 1,
6235 .show_host_supported_classes = 1,
6236 .show_host_port_type = 1,
6237 .show_host_port_id = 1,
6238 .show_host_maxframe_size = 1,
6240 .get_host_port_state = ibmvfc_get_host_port_state,
6241 .show_host_port_state = 1,
6243 .get_host_speed = ibmvfc_get_host_speed,
6244 .show_host_speed = 1,
6246 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
6247 .terminate_rport_io = ibmvfc_terminate_rport_io,
6249 .show_rport_maxframe_size = 1,
6250 .show_rport_supported_classes = 1,
6252 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
6253 .show_rport_dev_loss_tmo = 1,
6255 .get_starget_node_name = ibmvfc_get_starget_node_name,
6256 .show_starget_node_name = 1,
6258 .get_starget_port_name = ibmvfc_get_starget_port_name,
6259 .show_starget_port_name = 1,
6261 .get_starget_port_id = ibmvfc_get_starget_port_id,
6262 .show_starget_port_id = 1,
6264 .bsg_request = ibmvfc_bsg_request,
6265 .bsg_timeout = ibmvfc_bsg_timeout,
6269 * ibmvfc_module_init - Initialize the ibmvfc module
6272 * 0 on success / other on failure
6274 static int __init ibmvfc_module_init(void)
6278 if (!firmware_has_feature(FW_FEATURE_VIO))
6281 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
6282 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
6284 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
6285 if (!ibmvfc_transport_template)
6288 rc = vio_register_driver(&ibmvfc_driver);
6290 fc_release_transport(ibmvfc_transport_template);
6295 * ibmvfc_module_exit - Teardown the ibmvfc module
6300 static void __exit ibmvfc_module_exit(void)
6302 vio_unregister_driver(&ibmvfc_driver);
6303 fc_release_transport(ibmvfc_transport_template);
6306 module_init(ibmvfc_module_init);
6307 module_exit(ibmvfc_module_exit);