1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
5 * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
7 * Copyright (C) IBM Corporation, 2008
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmapool.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/kthread.h>
17 #include <linux/slab.h>
20 #include <linux/stringify.h>
21 #include <linux/bsg-lib.h>
22 #include <asm/firmware.h>
25 #include <scsi/scsi.h>
26 #include <scsi/scsi_cmnd.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_tcq.h>
30 #include <scsi/scsi_transport_fc.h>
31 #include <scsi/scsi_bsg_fc.h>
34 static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
35 static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
36 static u64 max_lun = IBMVFC_MAX_LUN;
37 static unsigned int max_targets = IBMVFC_MAX_TARGETS;
38 static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
39 static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
40 static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
41 static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
42 static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
43 static LIST_HEAD(ibmvfc_head);
44 static DEFINE_SPINLOCK(ibmvfc_driver_lock);
45 static struct scsi_transport_template *ibmvfc_transport_template;
47 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
48 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
49 MODULE_LICENSE("GPL");
50 MODULE_VERSION(IBMVFC_DRIVER_VERSION);
52 module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
54 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
55 module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(default_timeout,
57 "Default timeout in seconds for initialization and EH commands. "
58 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
59 module_param_named(max_requests, max_requests, uint, S_IRUGO);
60 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
61 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
62 module_param_named(max_lun, max_lun, ullong, S_IRUGO);
63 MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
64 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
65 module_param_named(max_targets, max_targets, uint, S_IRUGO);
66 MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
67 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
68 module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
69 MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
70 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
71 module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
72 MODULE_PARM_DESC(debug, "Enable driver debug information. "
73 "[Default=" __stringify(IBMVFC_DEBUG) "]");
74 module_param_named(log_level, log_level, uint, 0);
75 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
76 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
77 module_param_named(cls3_error, cls3_error, uint, 0);
78 MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
79 "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
89 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
90 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
91 { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
92 { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
93 { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
94 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
95 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
96 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
97 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
98 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
99 { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
100 { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
101 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
102 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
104 { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
105 { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
106 { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
107 { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
108 { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
109 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
110 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
111 { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
112 { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
113 { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
115 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
116 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
117 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
118 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
119 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
120 { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
121 { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
122 { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
123 { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
124 { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
125 { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
127 { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
128 { IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." },
131 static void ibmvfc_npiv_login(struct ibmvfc_host *);
132 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
133 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
134 static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
135 static void ibmvfc_npiv_logout(struct ibmvfc_host *);
136 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *);
137 static void ibmvfc_tgt_move_login(struct ibmvfc_target *);
139 static const char *unknown_error = "unknown error";
141 #ifdef CONFIG_SCSI_IBMVFC_TRACE
143 * ibmvfc_trc_start - Log a start trace entry
144 * @evt: ibmvfc event struct
147 static void ibmvfc_trc_start(struct ibmvfc_event *evt)
149 struct ibmvfc_host *vhost = evt->vhost;
150 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
151 struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
152 struct ibmvfc_trace_entry *entry;
154 entry = &vhost->trace[vhost->trace_index++];
156 entry->time = jiffies;
157 entry->fmt = evt->crq.format;
158 entry->type = IBMVFC_TRC_START;
160 switch (entry->fmt) {
161 case IBMVFC_CMD_FORMAT:
162 entry->op_code = vfc_cmd->iu.cdb[0];
163 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
164 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
165 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
166 entry->u.start.xfer_len = be32_to_cpu(vfc_cmd->iu.xfer_len);
168 case IBMVFC_MAD_FORMAT:
169 entry->op_code = be32_to_cpu(mad->opcode);
177 * ibmvfc_trc_end - Log an end trace entry
178 * @evt: ibmvfc event struct
181 static void ibmvfc_trc_end(struct ibmvfc_event *evt)
183 struct ibmvfc_host *vhost = evt->vhost;
184 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
185 struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
186 struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
189 entry->time = jiffies;
190 entry->fmt = evt->crq.format;
191 entry->type = IBMVFC_TRC_END;
193 switch (entry->fmt) {
194 case IBMVFC_CMD_FORMAT:
195 entry->op_code = vfc_cmd->iu.cdb[0];
196 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
197 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
198 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
199 entry->u.end.status = be16_to_cpu(vfc_cmd->status);
200 entry->u.end.error = be16_to_cpu(vfc_cmd->error);
201 entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
202 entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
203 entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
205 case IBMVFC_MAD_FORMAT:
206 entry->op_code = be32_to_cpu(mad->opcode);
207 entry->u.end.status = be16_to_cpu(mad->status);
216 #define ibmvfc_trc_start(evt) do { } while (0)
217 #define ibmvfc_trc_end(evt) do { } while (0)
221 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
222 * @status: status / error class
226 * index into cmd_status / -EINVAL on failure
228 static int ibmvfc_get_err_index(u16 status, u16 error)
232 for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
233 if ((cmd_status[i].status & status) == cmd_status[i].status &&
234 cmd_status[i].error == error)
241 * ibmvfc_get_cmd_error - Find the error description for the fcp response
242 * @status: status / error class
246 * error description string
248 static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
250 int rc = ibmvfc_get_err_index(status, error);
252 return cmd_status[rc].name;
253 return unknown_error;
257 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
258 * @vfc_cmd: ibmvfc command struct
261 * SCSI result value to return for completed command
263 static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
266 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
267 int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
269 if ((rsp->flags & FCP_RSP_LEN_VALID) &&
270 ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
271 rsp->data.info.rsp_code))
272 return DID_ERROR << 16;
274 err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
276 return rsp->scsi_status | (cmd_status[err].result << 16);
277 return rsp->scsi_status | (DID_ERROR << 16);
281 * ibmvfc_retry_cmd - Determine if error status is retryable
282 * @status: status / error class
286 * 1 if error should be retried / 0 if it should not
288 static int ibmvfc_retry_cmd(u16 status, u16 error)
290 int rc = ibmvfc_get_err_index(status, error);
293 return cmd_status[rc].retry;
297 static const char *unknown_fc_explain = "unknown fc explain";
299 static const struct {
303 { 0x00, "no additional explanation" },
304 { 0x01, "service parameter error - options" },
305 { 0x03, "service parameter error - initiator control" },
306 { 0x05, "service parameter error - recipient control" },
307 { 0x07, "service parameter error - received data field size" },
308 { 0x09, "service parameter error - concurrent seq" },
309 { 0x0B, "service parameter error - credit" },
310 { 0x0D, "invalid N_Port/F_Port_Name" },
311 { 0x0E, "invalid node/Fabric Name" },
312 { 0x0F, "invalid common service parameters" },
313 { 0x11, "invalid association header" },
314 { 0x13, "association header required" },
315 { 0x15, "invalid originator S_ID" },
316 { 0x17, "invalid OX_ID-RX-ID combination" },
317 { 0x19, "command (request) already in progress" },
318 { 0x1E, "N_Port Login requested" },
319 { 0x1F, "Invalid N_Port_ID" },
322 static const struct {
326 { 0x00, "no additional explanation" },
327 { 0x01, "port identifier not registered" },
328 { 0x02, "port name not registered" },
329 { 0x03, "node name not registered" },
330 { 0x04, "class of service not registered" },
331 { 0x06, "initial process associator not registered" },
332 { 0x07, "FC-4 TYPEs not registered" },
333 { 0x08, "symbolic port name not registered" },
334 { 0x09, "symbolic node name not registered" },
335 { 0x0A, "port type not registered" },
336 { 0xF0, "authorization exception" },
337 { 0xF1, "authentication exception" },
338 { 0xF2, "data base full" },
339 { 0xF3, "data base empty" },
340 { 0xF4, "processing request" },
341 { 0xF5, "unable to verify connection" },
342 { 0xF6, "devices not in a common zone" },
346 * ibmvfc_get_ls_explain - Return the FC Explain description text
347 * @status: FC Explain status
352 static const char *ibmvfc_get_ls_explain(u16 status)
356 for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
357 if (ls_explain[i].fc_explain == status)
358 return ls_explain[i].name;
360 return unknown_fc_explain;
364 * ibmvfc_get_gs_explain - Return the FC Explain description text
365 * @status: FC Explain status
370 static const char *ibmvfc_get_gs_explain(u16 status)
374 for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
375 if (gs_explain[i].fc_explain == status)
376 return gs_explain[i].name;
378 return unknown_fc_explain;
381 static const struct {
382 enum ibmvfc_fc_type fc_type;
385 { IBMVFC_FABRIC_REJECT, "fabric reject" },
386 { IBMVFC_PORT_REJECT, "port reject" },
387 { IBMVFC_LS_REJECT, "ELS reject" },
388 { IBMVFC_FABRIC_BUSY, "fabric busy" },
389 { IBMVFC_PORT_BUSY, "port busy" },
390 { IBMVFC_BASIC_REJECT, "basic reject" },
393 static const char *unknown_fc_type = "unknown fc type";
396 * ibmvfc_get_fc_type - Return the FC Type description text
397 * @status: FC Type error status
402 static const char *ibmvfc_get_fc_type(u16 status)
406 for (i = 0; i < ARRAY_SIZE(fc_type); i++)
407 if (fc_type[i].fc_type == status)
408 return fc_type[i].name;
410 return unknown_fc_type;
414 * ibmvfc_set_tgt_action - Set the next init action for the target
415 * @tgt: ibmvfc target struct
416 * @action: action to perform
419 * 0 if action changed / non-zero if not changed
421 static int ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
422 enum ibmvfc_target_action action)
426 switch (tgt->action) {
427 case IBMVFC_TGT_ACTION_LOGOUT_RPORT:
428 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT ||
429 action == IBMVFC_TGT_ACTION_DEL_RPORT) {
430 tgt->action = action;
434 case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT:
435 if (action == IBMVFC_TGT_ACTION_DEL_RPORT ||
436 action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
437 tgt->action = action;
441 case IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT:
442 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
443 tgt->action = action;
447 case IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT:
448 if (action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
449 tgt->action = action;
453 case IBMVFC_TGT_ACTION_DEL_RPORT:
454 if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
455 tgt->action = action;
459 case IBMVFC_TGT_ACTION_DELETED_RPORT:
462 tgt->action = action;
467 if (action >= IBMVFC_TGT_ACTION_LOGOUT_RPORT)
474 * ibmvfc_set_host_state - Set the state for the host
475 * @vhost: ibmvfc host struct
476 * @state: state to set host to
479 * 0 if state changed / non-zero if not changed
481 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
482 enum ibmvfc_host_state state)
486 switch (vhost->state) {
487 case IBMVFC_HOST_OFFLINE:
491 vhost->state = state;
499 * ibmvfc_set_host_action - Set the next init action for the host
500 * @vhost: ibmvfc host struct
501 * @action: action to perform
504 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
505 enum ibmvfc_host_action action)
508 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
509 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
510 vhost->action = action;
512 case IBMVFC_HOST_ACTION_LOGO_WAIT:
513 if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
514 vhost->action = action;
516 case IBMVFC_HOST_ACTION_INIT_WAIT:
517 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
518 vhost->action = action;
520 case IBMVFC_HOST_ACTION_QUERY:
521 switch (vhost->action) {
522 case IBMVFC_HOST_ACTION_INIT_WAIT:
523 case IBMVFC_HOST_ACTION_NONE:
524 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
525 vhost->action = action;
531 case IBMVFC_HOST_ACTION_TGT_INIT:
532 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
533 vhost->action = action;
535 case IBMVFC_HOST_ACTION_REENABLE:
536 case IBMVFC_HOST_ACTION_RESET:
537 vhost->action = action;
539 case IBMVFC_HOST_ACTION_INIT:
540 case IBMVFC_HOST_ACTION_TGT_DEL:
541 case IBMVFC_HOST_ACTION_LOGO:
542 case IBMVFC_HOST_ACTION_QUERY_TGTS:
543 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
544 case IBMVFC_HOST_ACTION_NONE:
546 switch (vhost->action) {
547 case IBMVFC_HOST_ACTION_RESET:
548 case IBMVFC_HOST_ACTION_REENABLE:
551 vhost->action = action;
559 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
560 * @vhost: ibmvfc host struct
565 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
567 if (vhost->action == IBMVFC_HOST_ACTION_NONE &&
568 vhost->state == IBMVFC_ACTIVE) {
569 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
570 scsi_block_requests(vhost->host);
571 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
576 wake_up(&vhost->work_wait_q);
580 * ibmvfc_del_tgt - Schedule cleanup and removal of the target
581 * @tgt: ibmvfc target struct
582 * @job_step: job step to perform
585 static void ibmvfc_del_tgt(struct ibmvfc_target *tgt)
587 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT))
588 tgt->job_step = ibmvfc_tgt_implicit_logout_and_del;
589 wake_up(&tgt->vhost->work_wait_q);
593 * ibmvfc_link_down - Handle a link down event from the adapter
594 * @vhost: ibmvfc host struct
595 * @state: ibmvfc host state to enter
598 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
599 enum ibmvfc_host_state state)
601 struct ibmvfc_target *tgt;
604 scsi_block_requests(vhost->host);
605 list_for_each_entry(tgt, &vhost->targets, queue)
607 ibmvfc_set_host_state(vhost, state);
608 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
609 vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
610 wake_up(&vhost->work_wait_q);
615 * ibmvfc_init_host - Start host initialization
616 * @vhost: ibmvfc host struct
621 static void ibmvfc_init_host(struct ibmvfc_host *vhost)
623 struct ibmvfc_target *tgt;
625 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
626 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
628 "Host initialization retries exceeded. Taking adapter offline\n");
629 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
634 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
635 memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
636 vhost->async_crq.cur = 0;
638 list_for_each_entry(tgt, &vhost->targets, queue) {
639 if (vhost->client_migrated)
645 scsi_block_requests(vhost->host);
646 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
647 vhost->job_step = ibmvfc_npiv_login;
648 wake_up(&vhost->work_wait_q);
653 * ibmvfc_send_crq - Send a CRQ
654 * @vhost: ibmvfc host struct
655 * @word1: the first 64 bits of the data
656 * @word2: the second 64 bits of the data
659 * 0 on success / other on failure
661 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
663 struct vio_dev *vdev = to_vio_dev(vhost->dev);
664 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
668 * ibmvfc_send_crq_init - Send a CRQ init message
669 * @vhost: ibmvfc host struct
672 * 0 on success / other on failure
674 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
676 ibmvfc_dbg(vhost, "Sending CRQ init\n");
677 return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
681 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
682 * @vhost: ibmvfc host struct
685 * 0 on success / other on failure
687 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
689 ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
690 return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
694 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
695 * @vhost: ibmvfc host struct
697 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
698 * the crq with the hypervisor.
700 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
703 struct vio_dev *vdev = to_vio_dev(vhost->dev);
704 struct ibmvfc_crq_queue *crq = &vhost->crq;
706 ibmvfc_dbg(vhost, "Releasing CRQ\n");
707 free_irq(vdev->irq, vhost);
708 tasklet_kill(&vhost->tasklet);
712 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
713 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
715 vhost->state = IBMVFC_NO_CRQ;
716 vhost->logged_in = 0;
717 dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
718 free_page((unsigned long)crq->msgs);
722 * ibmvfc_reenable_crq_queue - reenables the CRQ
723 * @vhost: ibmvfc host struct
726 * 0 on success / other on failure
728 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
731 struct vio_dev *vdev = to_vio_dev(vhost->dev);
733 /* Re-enable the CRQ */
737 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
738 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
741 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
747 * ibmvfc_reset_crq - resets a crq after a failure
748 * @vhost: ibmvfc host struct
751 * 0 on success / other on failure
753 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
757 struct vio_dev *vdev = to_vio_dev(vhost->dev);
758 struct ibmvfc_crq_queue *crq = &vhost->crq;
764 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
765 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
767 spin_lock_irqsave(vhost->host->host_lock, flags);
768 vhost->state = IBMVFC_NO_CRQ;
769 vhost->logged_in = 0;
771 /* Clean out the queue */
772 memset(crq->msgs, 0, PAGE_SIZE);
775 /* And re-open it again */
776 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
777 crq->msg_token, PAGE_SIZE);
780 /* Adapter is good, but other end is not ready */
781 dev_warn(vhost->dev, "Partner adapter not ready\n");
783 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
784 spin_unlock_irqrestore(vhost->host->host_lock, flags);
790 * ibmvfc_valid_event - Determines if event is valid.
791 * @pool: event_pool that contains the event
792 * @evt: ibmvfc event to be checked for validity
795 * 1 if event is valid / 0 if event is not valid
797 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
798 struct ibmvfc_event *evt)
800 int index = evt - pool->events;
801 if (index < 0 || index >= pool->size) /* outside of bounds */
803 if (evt != pool->events + index) /* unaligned */
809 * ibmvfc_free_event - Free the specified event
810 * @evt: ibmvfc_event to be freed
813 static void ibmvfc_free_event(struct ibmvfc_event *evt)
815 struct ibmvfc_host *vhost = evt->vhost;
816 struct ibmvfc_event_pool *pool = &vhost->pool;
818 BUG_ON(!ibmvfc_valid_event(pool, evt));
819 BUG_ON(atomic_inc_return(&evt->free) != 1);
820 list_add_tail(&evt->queue, &vhost->free);
824 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
825 * @evt: ibmvfc event struct
827 * This function does not setup any error status, that must be done
828 * before this function gets called.
830 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
832 struct scsi_cmnd *cmnd = evt->cmnd;
835 scsi_dma_unmap(cmnd);
836 cmnd->scsi_done(cmnd);
840 complete(evt->eh_comp);
842 ibmvfc_free_event(evt);
846 * ibmvfc_fail_request - Fail request with specified error code
847 * @evt: ibmvfc event struct
848 * @error_code: error code to fail request with
853 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
856 evt->cmnd->result = (error_code << 16);
857 evt->done = ibmvfc_scsi_eh_done;
859 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
861 list_del(&evt->queue);
862 del_timer(&evt->timer);
868 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
869 * @vhost: ibmvfc host struct
870 * @error_code: error code to fail requests with
875 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
877 struct ibmvfc_event *evt, *pos;
879 ibmvfc_dbg(vhost, "Purging all requests\n");
880 list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
881 ibmvfc_fail_request(evt, error_code);
885 * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
886 * @vhost: struct ibmvfc host to reset
888 static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
890 ibmvfc_purge_requests(vhost, DID_ERROR);
891 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
892 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
896 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
897 * @vhost: struct ibmvfc host to reset
899 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
901 if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
902 !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
903 scsi_block_requests(vhost->host);
904 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
905 vhost->job_step = ibmvfc_npiv_logout;
906 wake_up(&vhost->work_wait_q);
908 ibmvfc_hard_reset_host(vhost);
912 * ibmvfc_reset_host - Reset the connection to the server
913 * @vhost: ibmvfc host struct
915 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
919 spin_lock_irqsave(vhost->host->host_lock, flags);
920 __ibmvfc_reset_host(vhost);
921 spin_unlock_irqrestore(vhost->host->host_lock, flags);
925 * ibmvfc_retry_host_init - Retry host initialization if allowed
926 * @vhost: ibmvfc host struct
928 * Returns: 1 if init will be retried / 0 if not
931 static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
935 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
936 vhost->delay_init = 1;
937 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
939 "Host initialization retries exceeded. Taking adapter offline\n");
940 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
941 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
942 __ibmvfc_reset_host(vhost);
944 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
949 wake_up(&vhost->work_wait_q);
954 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
955 * @starget: scsi target struct
958 * ibmvfc_target struct / NULL if not found
960 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
962 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
963 struct ibmvfc_host *vhost = shost_priv(shost);
964 struct ibmvfc_target *tgt;
966 list_for_each_entry(tgt, &vhost->targets, queue)
967 if (tgt->target_id == starget->id) {
968 kref_get(&tgt->kref);
975 * ibmvfc_get_target - Find the specified scsi_target
976 * @starget: scsi target struct
979 * ibmvfc_target struct / NULL if not found
981 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
983 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
984 struct ibmvfc_target *tgt;
987 spin_lock_irqsave(shost->host_lock, flags);
988 tgt = __ibmvfc_get_target(starget);
989 spin_unlock_irqrestore(shost->host_lock, flags);
994 * ibmvfc_get_host_speed - Get host port speed
995 * @shost: scsi host struct
1000 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
1002 struct ibmvfc_host *vhost = shost_priv(shost);
1003 unsigned long flags;
1005 spin_lock_irqsave(shost->host_lock, flags);
1006 if (vhost->state == IBMVFC_ACTIVE) {
1007 switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
1009 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1012 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1015 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1018 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
1021 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
1024 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
1027 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
1028 be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
1029 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1033 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1034 spin_unlock_irqrestore(shost->host_lock, flags);
1038 * ibmvfc_get_host_port_state - Get host port state
1039 * @shost: scsi host struct
1044 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
1046 struct ibmvfc_host *vhost = shost_priv(shost);
1047 unsigned long flags;
1049 spin_lock_irqsave(shost->host_lock, flags);
1050 switch (vhost->state) {
1051 case IBMVFC_INITIALIZING:
1053 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1055 case IBMVFC_LINK_DOWN:
1056 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1058 case IBMVFC_LINK_DEAD:
1059 case IBMVFC_HOST_OFFLINE:
1060 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1063 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1066 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1069 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
1070 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1073 spin_unlock_irqrestore(shost->host_lock, flags);
1077 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1078 * @rport: rport struct
1079 * @timeout: timeout value
1084 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1087 rport->dev_loss_tmo = timeout;
1089 rport->dev_loss_tmo = 1;
1093 * ibmvfc_release_tgt - Free memory allocated for a target
1094 * @kref: kref struct
1097 static void ibmvfc_release_tgt(struct kref *kref)
1099 struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1104 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1105 * @starget: scsi target struct
1110 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1112 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1113 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1115 kref_put(&tgt->kref, ibmvfc_release_tgt);
1119 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1120 * @starget: scsi target struct
1125 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1127 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1128 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1130 kref_put(&tgt->kref, ibmvfc_release_tgt);
1134 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1135 * @starget: scsi target struct
1140 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1142 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1143 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1145 kref_put(&tgt->kref, ibmvfc_release_tgt);
1149 * ibmvfc_wait_while_resetting - Wait while the host resets
1150 * @vhost: ibmvfc host struct
1153 * 0 on success / other on failure
1155 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1157 long timeout = wait_event_timeout(vhost->init_wait_q,
1158 ((vhost->state == IBMVFC_ACTIVE ||
1159 vhost->state == IBMVFC_HOST_OFFLINE ||
1160 vhost->state == IBMVFC_LINK_DEAD) &&
1161 vhost->action == IBMVFC_HOST_ACTION_NONE),
1162 (init_timeout * HZ));
1164 return timeout ? 0 : -EIO;
1168 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1169 * @shost: scsi host struct
1172 * 0 on success / other on failure
1174 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1176 struct ibmvfc_host *vhost = shost_priv(shost);
1178 dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1179 ibmvfc_reset_host(vhost);
1180 return ibmvfc_wait_while_resetting(vhost);
1184 * ibmvfc_gather_partition_info - Gather info about the LPAR
1189 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1191 struct device_node *rootdn;
1193 const unsigned int *num;
1195 rootdn = of_find_node_by_path("/");
1199 name = of_get_property(rootdn, "ibm,partition-name", NULL);
1201 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1202 num = of_get_property(rootdn, "ibm,partition-no", NULL);
1204 vhost->partition_number = *num;
1205 of_node_put(rootdn);
1209 * ibmvfc_set_login_info - Setup info for NPIV login
1210 * @vhost: ibmvfc host struct
1215 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1217 struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1218 struct device_node *of_node = vhost->dev->of_node;
1219 const char *location;
1221 memset(login_info, 0, sizeof(*login_info));
1223 login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
1224 login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
1225 login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
1226 login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
1227 login_info->partition_num = cpu_to_be32(vhost->partition_number);
1228 login_info->vfc_frame_version = cpu_to_be32(1);
1229 login_info->fcp_version = cpu_to_be16(3);
1230 login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
1231 if (vhost->client_migrated)
1232 login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
1234 login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
1235 login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE);
1236 login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
1237 login_info->async.len = cpu_to_be32(vhost->async_crq.size * sizeof(*vhost->async_crq.msgs));
1238 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1239 strncpy(login_info->device_name,
1240 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
1242 location = of_get_property(of_node, "ibm,loc-code", NULL);
1243 location = location ? location : dev_name(vhost->dev);
1244 strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1248 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1249 * @vhost: ibmvfc host who owns the event pool
1251 * Returns zero on success.
1253 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1256 struct ibmvfc_event_pool *pool = &vhost->pool;
1259 pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1260 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1264 pool->iu_storage = dma_alloc_coherent(vhost->dev,
1265 pool->size * sizeof(*pool->iu_storage),
1266 &pool->iu_token, 0);
1268 if (!pool->iu_storage) {
1269 kfree(pool->events);
1273 for (i = 0; i < pool->size; ++i) {
1274 struct ibmvfc_event *evt = &pool->events[i];
1275 atomic_set(&evt->free, 1);
1276 evt->crq.valid = 0x80;
1277 evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
1278 evt->xfer_iu = pool->iu_storage + i;
1280 evt->ext_list = NULL;
1281 list_add_tail(&evt->queue, &vhost->free);
1289 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1290 * @vhost: ibmvfc host who owns the event pool
1293 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1296 struct ibmvfc_event_pool *pool = &vhost->pool;
1299 for (i = 0; i < pool->size; ++i) {
1300 list_del(&pool->events[i].queue);
1301 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1302 if (pool->events[i].ext_list)
1303 dma_pool_free(vhost->sg_pool,
1304 pool->events[i].ext_list,
1305 pool->events[i].ext_list_token);
1308 kfree(pool->events);
1309 dma_free_coherent(vhost->dev,
1310 pool->size * sizeof(*pool->iu_storage),
1311 pool->iu_storage, pool->iu_token);
1316 * ibmvfc_get_event - Gets the next free event in pool
1317 * @vhost: ibmvfc host struct
1319 * Returns a free event from the pool.
1321 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1323 struct ibmvfc_event *evt;
1325 BUG_ON(list_empty(&vhost->free));
1326 evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1327 atomic_set(&evt->free, 0);
1328 list_del(&evt->queue);
1333 * ibmvfc_init_event - Initialize fields in an event struct that are always
1336 * @done: Routine to call when the event is responded to
1337 * @format: SRP or MAD format
1339 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1340 void (*done) (struct ibmvfc_event *), u8 format)
1343 evt->sync_iu = NULL;
1344 evt->crq.format = format;
1346 evt->eh_comp = NULL;
1350 * ibmvfc_map_sg_list - Initialize scatterlist
1351 * @scmd: scsi command struct
1352 * @nseg: number of scatterlist segments
1353 * @md: memory descriptor list to initialize
1355 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1356 struct srp_direct_buf *md)
1359 struct scatterlist *sg;
1361 scsi_for_each_sg(scmd, sg, nseg, i) {
1362 md[i].va = cpu_to_be64(sg_dma_address(sg));
1363 md[i].len = cpu_to_be32(sg_dma_len(sg));
1369 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
1370 * @scmd: struct scsi_cmnd with the scatterlist
1371 * @evt: ibmvfc event struct
1372 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1373 * @dev: device for which to map dma memory
1376 * 0 on success / non-zero on failure
1378 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1379 struct ibmvfc_event *evt,
1380 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1384 struct srp_direct_buf *data = &vfc_cmd->ioba;
1385 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1388 vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1390 sg_mapped = scsi_dma_map(scmd);
1392 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1394 } else if (unlikely(sg_mapped < 0)) {
1395 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1396 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1400 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1401 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1402 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1404 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1405 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1408 if (sg_mapped == 1) {
1409 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1413 vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1415 if (!evt->ext_list) {
1416 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1417 &evt->ext_list_token);
1419 if (!evt->ext_list) {
1420 scsi_dma_unmap(scmd);
1421 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1422 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1427 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1429 data->va = cpu_to_be64(evt->ext_list_token);
1430 data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1436 * ibmvfc_timeout - Internal command timeout handler
1437 * @evt: struct ibmvfc_event that timed out
1439 * Called when an internally generated command times out
1441 static void ibmvfc_timeout(struct timer_list *t)
1443 struct ibmvfc_event *evt = from_timer(evt, t, timer);
1444 struct ibmvfc_host *vhost = evt->vhost;
1445 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1446 ibmvfc_reset_host(vhost);
1450 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1451 * @evt: event to be sent
1452 * @vhost: ibmvfc host struct
1453 * @timeout: timeout in seconds - 0 means do not time command
1455 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1457 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1458 struct ibmvfc_host *vhost, unsigned long timeout)
1460 __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1463 /* Copy the IU into the transfer area */
1464 *evt->xfer_iu = evt->iu;
1465 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1466 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1467 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1468 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1472 list_add_tail(&evt->queue, &vhost->sent);
1473 timer_setup(&evt->timer, ibmvfc_timeout, 0);
1476 evt->timer.expires = jiffies + (timeout * HZ);
1477 add_timer(&evt->timer);
1482 if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1483 be64_to_cpu(crq_as_u64[1])))) {
1484 list_del(&evt->queue);
1485 del_timer(&evt->timer);
1487 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1488 * Firmware will send a CRQ with a transport event (0xFF) to
1489 * tell this client what has happened to the transport. This
1490 * will be handled in ibmvfc_handle_crq()
1492 if (rc == H_CLOSED) {
1493 if (printk_ratelimit())
1494 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1496 scsi_dma_unmap(evt->cmnd);
1497 ibmvfc_free_event(evt);
1498 return SCSI_MLQUEUE_HOST_BUSY;
1501 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1503 evt->cmnd->result = DID_ERROR << 16;
1504 evt->done = ibmvfc_scsi_eh_done;
1506 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1510 ibmvfc_trc_start(evt);
1516 * ibmvfc_log_error - Log an error for the failed command if appropriate
1517 * @evt: ibmvfc event to log
1520 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1522 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1523 struct ibmvfc_host *vhost = evt->vhost;
1524 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1525 struct scsi_cmnd *cmnd = evt->cmnd;
1526 const char *err = unknown_error;
1527 int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1532 logerr = cmd_status[index].log;
1533 err = cmd_status[index].name;
1536 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1539 if (rsp->flags & FCP_RSP_LEN_VALID)
1540 rsp_code = rsp->data.info.rsp_code;
1542 scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
1543 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1544 cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
1545 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1549 * ibmvfc_relogin - Log back into the specified device
1550 * @sdev: scsi device struct
1553 static void ibmvfc_relogin(struct scsi_device *sdev)
1555 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1556 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1557 struct ibmvfc_target *tgt;
1559 list_for_each_entry(tgt, &vhost->targets, queue) {
1560 if (rport == tgt->rport) {
1561 ibmvfc_del_tgt(tgt);
1566 ibmvfc_reinit_host(vhost);
1570 * ibmvfc_scsi_done - Handle responses from commands
1571 * @evt: ibmvfc event to be handled
1573 * Used as a callback when sending scsi cmds.
1575 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1577 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1578 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1579 struct scsi_cmnd *cmnd = evt->cmnd;
1581 u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1584 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1585 scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1586 else if (rsp->flags & FCP_RESID_UNDER)
1587 scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1589 scsi_set_resid(cmnd, 0);
1591 if (vfc_cmd->status) {
1592 cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1594 if (rsp->flags & FCP_RSP_LEN_VALID)
1595 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1596 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1597 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1598 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1599 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1600 if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1601 (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1602 ibmvfc_relogin(cmnd->device);
1604 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1605 cmnd->result = (DID_ERROR << 16);
1607 ibmvfc_log_error(evt);
1610 if (!cmnd->result &&
1611 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1612 cmnd->result = (DID_ERROR << 16);
1614 scsi_dma_unmap(cmnd);
1615 cmnd->scsi_done(cmnd);
1619 complete(evt->eh_comp);
1621 ibmvfc_free_event(evt);
1625 * ibmvfc_host_chkready - Check if the host can accept commands
1626 * @vhost: struct ibmvfc host
1629 * 1 if host can accept command / 0 if not
1631 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1635 switch (vhost->state) {
1636 case IBMVFC_LINK_DEAD:
1637 case IBMVFC_HOST_OFFLINE:
1638 result = DID_NO_CONNECT << 16;
1641 case IBMVFC_INITIALIZING:
1643 case IBMVFC_LINK_DOWN:
1644 result = DID_REQUEUE << 16;
1655 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1656 * @cmnd: struct scsi_cmnd to be executed
1657 * @done: Callback function to be called when cmnd is completed
1660 * 0 on success / other on failure
1662 static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
1663 void (*done) (struct scsi_cmnd *))
1665 struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1666 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1667 struct ibmvfc_cmd *vfc_cmd;
1668 struct ibmvfc_event *evt;
1671 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1672 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1678 cmnd->result = (DID_OK << 16);
1679 evt = ibmvfc_get_event(vhost);
1680 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1682 cmnd->scsi_done = done;
1683 vfc_cmd = &evt->iu.cmd;
1684 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1685 vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
1686 vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1687 vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1688 vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
1689 vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1690 vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
1691 vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1692 vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1693 int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1694 memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1696 if (cmnd->flags & SCMD_TAGGED) {
1697 vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
1698 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1701 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1702 return ibmvfc_send_event(evt, vhost, 0);
1704 ibmvfc_free_event(evt);
1706 return SCSI_MLQUEUE_HOST_BUSY;
1708 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1709 scmd_printk(KERN_ERR, cmnd,
1710 "Failed to map DMA buffer for command. rc=%d\n", rc);
1712 cmnd->result = DID_ERROR << 16;
1717 static DEF_SCSI_QCMD(ibmvfc_queuecommand)
1720 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1721 * @evt: ibmvfc event struct
1724 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1726 /* copy the response back */
1728 *evt->sync_iu = *evt->xfer_iu;
1730 complete(&evt->comp);
1734 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1735 * @evt: struct ibmvfc_event
1738 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
1740 struct ibmvfc_host *vhost = evt->vhost;
1742 ibmvfc_free_event(evt);
1743 vhost->aborting_passthru = 0;
1744 dev_info(vhost->dev, "Passthru command cancelled\n");
1748 * ibmvfc_bsg_timeout - Handle a BSG timeout
1749 * @job: struct bsg_job that timed out
1752 * 0 on success / other on failure
1754 static int ibmvfc_bsg_timeout(struct bsg_job *job)
1756 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1757 unsigned long port_id = (unsigned long)job->dd_data;
1758 struct ibmvfc_event *evt;
1759 struct ibmvfc_tmf *tmf;
1760 unsigned long flags;
1764 spin_lock_irqsave(vhost->host->host_lock, flags);
1765 if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
1766 __ibmvfc_reset_host(vhost);
1767 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1771 vhost->aborting_passthru = 1;
1772 evt = ibmvfc_get_event(vhost);
1773 ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
1776 memset(tmf, 0, sizeof(*tmf));
1777 tmf->common.version = cpu_to_be32(1);
1778 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
1779 tmf->common.length = cpu_to_be16(sizeof(*tmf));
1780 tmf->scsi_id = cpu_to_be64(port_id);
1781 tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1782 tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
1783 rc = ibmvfc_send_event(evt, vhost, default_timeout);
1786 vhost->aborting_passthru = 0;
1787 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
1790 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
1793 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1800 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
1801 * @vhost: struct ibmvfc_host to send command
1802 * @port_id: port ID to send command
1805 * 0 on success / other on failure
1807 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
1809 struct ibmvfc_port_login *plogi;
1810 struct ibmvfc_target *tgt;
1811 struct ibmvfc_event *evt;
1812 union ibmvfc_iu rsp_iu;
1813 unsigned long flags;
1814 int rc = 0, issue_login = 1;
1817 spin_lock_irqsave(vhost->host->host_lock, flags);
1818 list_for_each_entry(tgt, &vhost->targets, queue) {
1819 if (tgt->scsi_id == port_id) {
1827 if (unlikely((rc = ibmvfc_host_chkready(vhost))))
1830 evt = ibmvfc_get_event(vhost);
1831 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1832 plogi = &evt->iu.plogi;
1833 memset(plogi, 0, sizeof(*plogi));
1834 plogi->common.version = cpu_to_be32(1);
1835 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
1836 plogi->common.length = cpu_to_be16(sizeof(*plogi));
1837 plogi->scsi_id = cpu_to_be64(port_id);
1838 evt->sync_iu = &rsp_iu;
1839 init_completion(&evt->comp);
1841 rc = ibmvfc_send_event(evt, vhost, default_timeout);
1842 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1847 wait_for_completion(&evt->comp);
1849 if (rsp_iu.plogi.common.status)
1852 spin_lock_irqsave(vhost->host->host_lock, flags);
1853 ibmvfc_free_event(evt);
1855 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1861 * ibmvfc_bsg_request - Handle a BSG request
1862 * @job: struct bsg_job to be executed
1865 * 0 on success / other on failure
1867 static int ibmvfc_bsg_request(struct bsg_job *job)
1869 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1870 struct fc_rport *rport = fc_bsg_to_rport(job);
1871 struct ibmvfc_passthru_mad *mad;
1872 struct ibmvfc_event *evt;
1873 union ibmvfc_iu rsp_iu;
1874 unsigned long flags, port_id = -1;
1875 struct fc_bsg_request *bsg_request = job->request;
1876 struct fc_bsg_reply *bsg_reply = job->reply;
1877 unsigned int code = bsg_request->msgcode;
1878 int rc = 0, req_seg, rsp_seg, issue_login = 0;
1879 u32 fc_flags, rsp_len;
1882 bsg_reply->reply_payload_rcv_len = 0;
1884 port_id = rport->port_id;
1887 case FC_BSG_HST_ELS_NOLOGIN:
1888 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
1889 (bsg_request->rqst_data.h_els.port_id[1] << 8) |
1890 bsg_request->rqst_data.h_els.port_id[2];
1892 case FC_BSG_RPT_ELS:
1893 fc_flags = IBMVFC_FC_ELS;
1897 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
1898 (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
1899 bsg_request->rqst_data.h_ct.port_id[2];
1902 fc_flags = IBMVFC_FC_CT_IU;
1910 if (!mutex_trylock(&vhost->passthru_mutex))
1913 job->dd_data = (void *)port_id;
1914 req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
1915 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1918 mutex_unlock(&vhost->passthru_mutex);
1922 rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
1923 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1926 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1927 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1928 mutex_unlock(&vhost->passthru_mutex);
1932 if (req_seg > 1 || rsp_seg > 1) {
1938 rc = ibmvfc_bsg_plogi(vhost, port_id);
1940 spin_lock_irqsave(vhost->host->host_lock, flags);
1942 if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
1943 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1944 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1948 evt = ibmvfc_get_event(vhost);
1949 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1950 mad = &evt->iu.passthru;
1952 memset(mad, 0, sizeof(*mad));
1953 mad->common.version = cpu_to_be32(1);
1954 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
1955 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
1957 mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
1958 offsetof(struct ibmvfc_passthru_mad, iu));
1959 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
1961 mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
1962 mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
1963 mad->iu.flags = cpu_to_be32(fc_flags);
1964 mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1966 mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
1967 mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
1968 mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
1969 mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
1970 mad->iu.scsi_id = cpu_to_be64(port_id);
1971 mad->iu.tag = cpu_to_be64((u64)evt);
1972 rsp_len = be32_to_cpu(mad->iu.rsp.len);
1974 evt->sync_iu = &rsp_iu;
1975 init_completion(&evt->comp);
1976 rc = ibmvfc_send_event(evt, vhost, 0);
1977 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1984 wait_for_completion(&evt->comp);
1986 if (rsp_iu.passthru.common.status)
1989 bsg_reply->reply_payload_rcv_len = rsp_len;
1991 spin_lock_irqsave(vhost->host->host_lock, flags);
1992 ibmvfc_free_event(evt);
1993 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1994 bsg_reply->result = rc;
1995 bsg_job_done(job, bsg_reply->result,
1996 bsg_reply->reply_payload_rcv_len);
1999 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2000 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2001 dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
2002 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2003 mutex_unlock(&vhost->passthru_mutex);
2009 * ibmvfc_reset_device - Reset the device with the specified reset type
2010 * @sdev: scsi device to reset
2012 * @desc: reset type description for log messages
2015 * 0 on success / other on failure
2017 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
2019 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2020 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2021 struct ibmvfc_cmd *tmf;
2022 struct ibmvfc_event *evt = NULL;
2023 union ibmvfc_iu rsp_iu;
2024 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2025 int rsp_rc = -EBUSY;
2026 unsigned long flags;
2029 spin_lock_irqsave(vhost->host->host_lock, flags);
2030 if (vhost->state == IBMVFC_ACTIVE) {
2031 evt = ibmvfc_get_event(vhost);
2032 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2035 memset(tmf, 0, sizeof(*tmf));
2036 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
2037 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
2038 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2039 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
2040 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
2041 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2042 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
2043 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2044 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2045 tmf->iu.tmf_flags = type;
2046 evt->sync_iu = &rsp_iu;
2048 init_completion(&evt->comp);
2049 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2051 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2054 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2059 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2060 wait_for_completion(&evt->comp);
2062 if (rsp_iu.cmd.status)
2063 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2066 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2067 rsp_code = fc_rsp->data.info.rsp_code;
2069 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2070 "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2071 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2072 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2073 fc_rsp->scsi_status);
2076 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2078 spin_lock_irqsave(vhost->host->host_lock, flags);
2079 ibmvfc_free_event(evt);
2080 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2085 * ibmvfc_match_rport - Match function for specified remote port
2086 * @evt: ibmvfc event struct
2087 * @device: device to match (rport)
2090 * 1 if event matches rport / 0 if event does not match rport
2092 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2094 struct fc_rport *cmd_rport;
2097 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2098 if (cmd_rport == rport)
2105 * ibmvfc_match_target - Match function for specified target
2106 * @evt: ibmvfc event struct
2107 * @device: device to match (starget)
2110 * 1 if event matches starget / 0 if event does not match starget
2112 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2114 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2120 * ibmvfc_match_lun - Match function for specified LUN
2121 * @evt: ibmvfc event struct
2122 * @device: device to match (sdev)
2125 * 1 if event matches sdev / 0 if event does not match sdev
2127 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2129 if (evt->cmnd && evt->cmnd->device == device)
2135 * ibmvfc_wait_for_ops - Wait for ops to complete
2136 * @vhost: ibmvfc host struct
2137 * @device: device to match (starget or sdev)
2138 * @match: match function
2143 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2144 int (*match) (struct ibmvfc_event *, void *))
2146 struct ibmvfc_event *evt;
2147 DECLARE_COMPLETION_ONSTACK(comp);
2149 unsigned long flags;
2150 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2155 spin_lock_irqsave(vhost->host->host_lock, flags);
2156 list_for_each_entry(evt, &vhost->sent, queue) {
2157 if (match(evt, device)) {
2158 evt->eh_comp = ∁
2162 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2165 timeout = wait_for_completion_timeout(&comp, timeout);
2169 spin_lock_irqsave(vhost->host->host_lock, flags);
2170 list_for_each_entry(evt, &vhost->sent, queue) {
2171 if (match(evt, device)) {
2172 evt->eh_comp = NULL;
2176 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2178 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2180 return wait ? FAILED : SUCCESS;
2190 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2191 * @sdev: scsi device to cancel commands
2192 * @type: type of error recovery being performed
2194 * This sends a cancel to the VIOS for the specified device. This does
2195 * NOT send any abort to the actual device. That must be done separately.
2198 * 0 on success / other on failure
2200 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2202 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2203 struct scsi_target *starget = scsi_target(sdev);
2204 struct fc_rport *rport = starget_to_rport(starget);
2205 struct ibmvfc_tmf *tmf;
2206 struct ibmvfc_event *evt, *found_evt;
2207 union ibmvfc_iu rsp;
2208 int rsp_rc = -EBUSY;
2209 unsigned long flags;
2213 spin_lock_irqsave(vhost->host->host_lock, flags);
2215 list_for_each_entry(evt, &vhost->sent, queue) {
2216 if (evt->cmnd && evt->cmnd->device == sdev) {
2223 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2224 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2225 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2229 if (vhost->logged_in) {
2230 evt = ibmvfc_get_event(vhost);
2231 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2234 memset(tmf, 0, sizeof(*tmf));
2235 tmf->common.version = cpu_to_be32(1);
2236 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2237 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2238 tmf->scsi_id = cpu_to_be64(rport->port_id);
2239 int_to_scsilun(sdev->lun, &tmf->lun);
2240 if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
2241 type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2242 if (vhost->state == IBMVFC_ACTIVE)
2243 tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2245 tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2246 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2247 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2249 evt->sync_iu = &rsp;
2250 init_completion(&evt->comp);
2251 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2254 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2257 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2258 /* If failure is received, the host adapter is most likely going
2259 through reset, return success so the caller will wait for the command
2260 being cancelled to get returned */
2264 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2266 wait_for_completion(&evt->comp);
2267 status = be16_to_cpu(rsp.mad_common.status);
2268 spin_lock_irqsave(vhost->host->host_lock, flags);
2269 ibmvfc_free_event(evt);
2270 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2272 if (status != IBMVFC_MAD_SUCCESS) {
2273 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2275 case IBMVFC_MAD_DRIVER_FAILED:
2276 case IBMVFC_MAD_CRQ_ERROR:
2277 /* Host adapter most likely going through reset, return success to
2278 the caller will wait for the command being cancelled to get returned */
2285 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2290 * ibmvfc_match_key - Match function for specified cancel key
2291 * @evt: ibmvfc event struct
2292 * @key: cancel key to match
2295 * 1 if event matches key / 0 if event does not match key
2297 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2299 unsigned long cancel_key = (unsigned long)key;
2301 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2302 be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2308 * ibmvfc_match_evt - Match function for specified event
2309 * @evt: ibmvfc event struct
2310 * @match: event to match
2313 * 1 if event matches key / 0 if event does not match key
2315 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2323 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2324 * @sdev: scsi device to abort commands
2326 * This sends an Abort Task Set to the VIOS for the specified device. This does
2327 * NOT send any cancel to the VIOS. That must be done separately.
2330 * 0 on success / other on failure
2332 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2334 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2335 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2336 struct ibmvfc_cmd *tmf;
2337 struct ibmvfc_event *evt, *found_evt;
2338 union ibmvfc_iu rsp_iu;
2339 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2340 int rc, rsp_rc = -EBUSY;
2341 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2344 spin_lock_irqsave(vhost->host->host_lock, flags);
2346 list_for_each_entry(evt, &vhost->sent, queue) {
2347 if (evt->cmnd && evt->cmnd->device == sdev) {
2354 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2355 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2356 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2360 if (vhost->state == IBMVFC_ACTIVE) {
2361 evt = ibmvfc_get_event(vhost);
2362 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2365 memset(tmf, 0, sizeof(*tmf));
2366 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
2367 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
2368 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2369 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
2370 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
2371 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2372 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
2373 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2374 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2375 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
2376 evt->sync_iu = &rsp_iu;
2378 init_completion(&evt->comp);
2379 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2382 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2385 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2389 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2390 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2393 rc = ibmvfc_cancel_all(sdev, 0);
2395 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2401 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2402 ibmvfc_reset_host(vhost);
2404 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2409 rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2410 if (rc != SUCCESS) {
2411 spin_lock_irqsave(vhost->host->host_lock, flags);
2412 ibmvfc_hard_reset_host(vhost);
2413 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2421 if (rsp_iu.cmd.status)
2422 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2425 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2426 rsp_code = fc_rsp->data.info.rsp_code;
2428 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2429 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2430 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2431 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2432 fc_rsp->scsi_status);
2435 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2438 spin_lock_irqsave(vhost->host->host_lock, flags);
2439 ibmvfc_free_event(evt);
2440 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2445 * ibmvfc_eh_abort_handler - Abort a command
2446 * @cmd: scsi command to abort
2449 * SUCCESS / FAST_IO_FAIL / FAILED
2451 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2453 struct scsi_device *sdev = cmd->device;
2454 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2455 int cancel_rc, block_rc;
2459 block_rc = fc_block_scsi_eh(cmd);
2460 ibmvfc_wait_while_resetting(vhost);
2461 if (block_rc != FAST_IO_FAIL) {
2462 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2463 ibmvfc_abort_task_set(sdev);
2465 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2468 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2470 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2478 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2479 * @cmd: scsi command struct
2482 * SUCCESS / FAST_IO_FAIL / FAILED
2484 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2486 struct scsi_device *sdev = cmd->device;
2487 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2488 int cancel_rc, block_rc, reset_rc = 0;
2492 block_rc = fc_block_scsi_eh(cmd);
2493 ibmvfc_wait_while_resetting(vhost);
2494 if (block_rc != FAST_IO_FAIL) {
2495 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2496 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2498 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2500 if (!cancel_rc && !reset_rc)
2501 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2503 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2511 * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2512 * @sdev: scsi device struct
2513 * @data: return code
2516 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2518 unsigned long *rc = data;
2519 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2523 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2524 * @sdev: scsi device struct
2525 * @data: return code
2528 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2530 unsigned long *rc = data;
2531 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2535 * ibmvfc_eh_target_reset_handler - Reset the target
2536 * @cmd: scsi command struct
2539 * SUCCESS / FAST_IO_FAIL / FAILED
2541 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2543 struct scsi_device *sdev = cmd->device;
2544 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2545 struct scsi_target *starget = scsi_target(sdev);
2549 unsigned long cancel_rc = 0;
2552 block_rc = fc_block_scsi_eh(cmd);
2553 ibmvfc_wait_while_resetting(vhost);
2554 if (block_rc != FAST_IO_FAIL) {
2555 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2556 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2558 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2560 if (!cancel_rc && !reset_rc)
2561 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2563 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2571 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2572 * @cmd: struct scsi_cmnd having problems
2575 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2578 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2580 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2581 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2583 return rc ? FAILED : SUCCESS;
2587 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2588 * @rport: rport struct
2593 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2595 struct Scsi_Host *shost = rport_to_shost(rport);
2596 struct ibmvfc_host *vhost = shost_priv(shost);
2597 struct fc_rport *dev_rport;
2598 struct scsi_device *sdev;
2599 struct ibmvfc_target *tgt;
2600 unsigned long rc, flags;
2604 shost_for_each_device(sdev, shost) {
2605 dev_rport = starget_to_rport(scsi_target(sdev));
2606 if (dev_rport != rport)
2608 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2611 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
2614 ibmvfc_issue_fc_host_lip(shost);
2616 spin_lock_irqsave(shost->host_lock, flags);
2618 list_for_each_entry(tgt, &vhost->targets, queue) {
2619 if (tgt->scsi_id == rport->port_id) {
2625 if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
2627 * If we get here, that means we previously attempted to send
2628 * an implicit logout to the target but it failed, most likely
2629 * due to I/O being pending, so we need to send it again
2631 ibmvfc_del_tgt(tgt);
2632 ibmvfc_reinit_host(vhost);
2635 spin_unlock_irqrestore(shost->host_lock, flags);
2639 static const struct ibmvfc_async_desc ae_desc [] = {
2640 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2641 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2642 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2643 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2644 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2645 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
2646 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
2647 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
2648 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
2649 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
2650 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
2651 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
2652 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
2655 static const struct ibmvfc_async_desc unknown_ae = {
2656 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
2660 * ibmvfc_get_ae_desc - Get text description for async event
2664 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
2668 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2669 if (ae_desc[i].ae == ae)
2675 static const struct {
2676 enum ibmvfc_ae_link_state state;
2679 { IBMVFC_AE_LS_LINK_UP, " link up" },
2680 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
2681 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
2682 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
2686 * ibmvfc_get_link_state - Get text description for link state
2687 * @state: link state
2690 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
2694 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
2695 if (link_desc[i].state == state)
2696 return link_desc[i].desc;
2702 * ibmvfc_handle_async - Handle an async event from the adapter
2703 * @crq: crq to process
2704 * @vhost: ibmvfc host struct
2707 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2708 struct ibmvfc_host *vhost)
2710 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
2711 struct ibmvfc_target *tgt;
2713 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
2714 " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
2715 be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
2716 ibmvfc_get_link_state(crq->link_state));
2718 switch (be64_to_cpu(crq->event)) {
2719 case IBMVFC_AE_RESUME:
2720 switch (crq->link_state) {
2721 case IBMVFC_AE_LS_LINK_DOWN:
2722 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2724 case IBMVFC_AE_LS_LINK_DEAD:
2725 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2727 case IBMVFC_AE_LS_LINK_UP:
2728 case IBMVFC_AE_LS_LINK_BOUNCED:
2730 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2731 vhost->delay_init = 1;
2732 __ibmvfc_reset_host(vhost);
2737 case IBMVFC_AE_LINK_UP:
2738 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2739 vhost->delay_init = 1;
2740 __ibmvfc_reset_host(vhost);
2742 case IBMVFC_AE_SCN_FABRIC:
2743 case IBMVFC_AE_SCN_DOMAIN:
2744 vhost->events_to_log |= IBMVFC_AE_RSCN;
2745 if (vhost->state < IBMVFC_HALTED) {
2746 vhost->delay_init = 1;
2747 __ibmvfc_reset_host(vhost);
2750 case IBMVFC_AE_SCN_NPORT:
2751 case IBMVFC_AE_SCN_GROUP:
2752 vhost->events_to_log |= IBMVFC_AE_RSCN;
2753 ibmvfc_reinit_host(vhost);
2755 case IBMVFC_AE_ELS_LOGO:
2756 case IBMVFC_AE_ELS_PRLO:
2757 case IBMVFC_AE_ELS_PLOGI:
2758 list_for_each_entry(tgt, &vhost->targets, queue) {
2759 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
2761 if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
2763 if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
2765 if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
2767 if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
2769 if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
2770 ibmvfc_del_tgt(tgt);
2771 ibmvfc_reinit_host(vhost);
2775 case IBMVFC_AE_LINK_DOWN:
2776 case IBMVFC_AE_ADAPTER_FAILED:
2777 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2779 case IBMVFC_AE_LINK_DEAD:
2780 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2782 case IBMVFC_AE_HALT:
2783 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2786 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
2792 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2793 * @crq: Command/Response queue
2794 * @vhost: ibmvfc host struct
2797 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2800 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
2802 switch (crq->valid) {
2803 case IBMVFC_CRQ_INIT_RSP:
2804 switch (crq->format) {
2805 case IBMVFC_CRQ_INIT:
2806 dev_info(vhost->dev, "Partner initialized\n");
2807 /* Send back a response */
2808 rc = ibmvfc_send_crq_init_complete(vhost);
2810 ibmvfc_init_host(vhost);
2812 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2814 case IBMVFC_CRQ_INIT_COMPLETE:
2815 dev_info(vhost->dev, "Partner initialization complete\n");
2816 ibmvfc_init_host(vhost);
2819 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2822 case IBMVFC_CRQ_XPORT_EVENT:
2823 vhost->state = IBMVFC_NO_CRQ;
2824 vhost->logged_in = 0;
2825 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2826 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2827 /* We need to re-setup the interpartition connection */
2828 dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
2829 vhost->client_migrated = 1;
2831 scsi_block_requests(vhost->host);
2832 ibmvfc_purge_requests(vhost, DID_REQUEUE);
2833 ibmvfc_set_host_state(vhost, IBMVFC_LINK_DOWN);
2834 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
2835 wake_up(&vhost->work_wait_q);
2836 } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
2837 dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
2838 ibmvfc_purge_requests(vhost, DID_ERROR);
2839 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2840 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
2842 dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
2845 case IBMVFC_CRQ_CMD_RSP:
2848 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2852 if (crq->format == IBMVFC_ASYNC_EVENT)
2855 /* The only kind of payload CRQs we should get are responses to
2856 * things we send. Make sure this response is to something we
2859 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2860 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
2865 if (unlikely(atomic_read(&evt->free))) {
2866 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
2871 del_timer(&evt->timer);
2872 list_del(&evt->queue);
2873 ibmvfc_trc_end(evt);
2878 * ibmvfc_scan_finished - Check if the device scan is done.
2879 * @shost: scsi host struct
2880 * @time: current elapsed time
2883 * 0 if scan is not done / 1 if scan is done
2885 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2887 unsigned long flags;
2888 struct ibmvfc_host *vhost = shost_priv(shost);
2891 spin_lock_irqsave(shost->host_lock, flags);
2892 if (time >= (init_timeout * HZ)) {
2893 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2894 "continuing initialization\n", init_timeout);
2898 if (vhost->scan_complete)
2900 spin_unlock_irqrestore(shost->host_lock, flags);
2905 * ibmvfc_slave_alloc - Setup the device's task set value
2906 * @sdev: struct scsi_device device to configure
2908 * Set the device's task set value so that error handling works as
2912 * 0 on success / -ENXIO if device does not exist
2914 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2916 struct Scsi_Host *shost = sdev->host;
2917 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2918 struct ibmvfc_host *vhost = shost_priv(shost);
2919 unsigned long flags = 0;
2921 if (!rport || fc_remote_port_chkready(rport))
2924 spin_lock_irqsave(shost->host_lock, flags);
2925 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2926 spin_unlock_irqrestore(shost->host_lock, flags);
2931 * ibmvfc_target_alloc - Setup the target's task set value
2932 * @starget: struct scsi_target
2934 * Set the target's task set value so that error handling works as
2938 * 0 on success / -ENXIO if device does not exist
2940 static int ibmvfc_target_alloc(struct scsi_target *starget)
2942 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2943 struct ibmvfc_host *vhost = shost_priv(shost);
2944 unsigned long flags = 0;
2946 spin_lock_irqsave(shost->host_lock, flags);
2947 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2948 spin_unlock_irqrestore(shost->host_lock, flags);
2953 * ibmvfc_slave_configure - Configure the device
2954 * @sdev: struct scsi_device device to configure
2956 * Enable allow_restart for a device if it is a disk. Adjust the
2957 * queue_depth here also.
2962 static int ibmvfc_slave_configure(struct scsi_device *sdev)
2964 struct Scsi_Host *shost = sdev->host;
2965 unsigned long flags = 0;
2967 spin_lock_irqsave(shost->host_lock, flags);
2968 if (sdev->type == TYPE_DISK) {
2969 sdev->allow_restart = 1;
2970 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
2972 spin_unlock_irqrestore(shost->host_lock, flags);
2977 * ibmvfc_change_queue_depth - Change the device's queue depth
2978 * @sdev: scsi device struct
2979 * @qdepth: depth to set
2980 * @reason: calling context
2985 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2987 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2988 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2990 return scsi_change_queue_depth(sdev, qdepth);
2993 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2994 struct device_attribute *attr, char *buf)
2996 struct Scsi_Host *shost = class_to_shost(dev);
2997 struct ibmvfc_host *vhost = shost_priv(shost);
2999 return snprintf(buf, PAGE_SIZE, "%s\n",
3000 vhost->login_buf->resp.partition_name);
3003 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
3004 struct device_attribute *attr, char *buf)
3006 struct Scsi_Host *shost = class_to_shost(dev);
3007 struct ibmvfc_host *vhost = shost_priv(shost);
3009 return snprintf(buf, PAGE_SIZE, "%s\n",
3010 vhost->login_buf->resp.device_name);
3013 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3014 struct device_attribute *attr, char *buf)
3016 struct Scsi_Host *shost = class_to_shost(dev);
3017 struct ibmvfc_host *vhost = shost_priv(shost);
3019 return snprintf(buf, PAGE_SIZE, "%s\n",
3020 vhost->login_buf->resp.port_loc_code);
3023 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3024 struct device_attribute *attr, char *buf)
3026 struct Scsi_Host *shost = class_to_shost(dev);
3027 struct ibmvfc_host *vhost = shost_priv(shost);
3029 return snprintf(buf, PAGE_SIZE, "%s\n",
3030 vhost->login_buf->resp.drc_name);
3033 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3034 struct device_attribute *attr, char *buf)
3036 struct Scsi_Host *shost = class_to_shost(dev);
3037 struct ibmvfc_host *vhost = shost_priv(shost);
3038 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
3041 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3042 struct device_attribute *attr, char *buf)
3044 struct Scsi_Host *shost = class_to_shost(dev);
3045 struct ibmvfc_host *vhost = shost_priv(shost);
3046 return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
3050 * ibmvfc_show_log_level - Show the adapter's error logging level
3051 * @dev: class device struct
3055 * number of bytes printed to buffer
3057 static ssize_t ibmvfc_show_log_level(struct device *dev,
3058 struct device_attribute *attr, char *buf)
3060 struct Scsi_Host *shost = class_to_shost(dev);
3061 struct ibmvfc_host *vhost = shost_priv(shost);
3062 unsigned long flags = 0;
3065 spin_lock_irqsave(shost->host_lock, flags);
3066 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
3067 spin_unlock_irqrestore(shost->host_lock, flags);
3072 * ibmvfc_store_log_level - Change the adapter's error logging level
3073 * @dev: class device struct
3077 * number of bytes printed to buffer
3079 static ssize_t ibmvfc_store_log_level(struct device *dev,
3080 struct device_attribute *attr,
3081 const char *buf, size_t count)
3083 struct Scsi_Host *shost = class_to_shost(dev);
3084 struct ibmvfc_host *vhost = shost_priv(shost);
3085 unsigned long flags = 0;
3087 spin_lock_irqsave(shost->host_lock, flags);
3088 vhost->log_level = simple_strtoul(buf, NULL, 10);
3089 spin_unlock_irqrestore(shost->host_lock, flags);
3093 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3094 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3095 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3096 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3097 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3098 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3099 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3100 ibmvfc_show_log_level, ibmvfc_store_log_level);
3102 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3104 * ibmvfc_read_trace - Dump the adapter trace
3105 * @filp: open sysfs file
3106 * @kobj: kobject struct
3107 * @bin_attr: bin_attribute struct
3110 * @count: buffer size
3113 * number of bytes printed to buffer
3115 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3116 struct bin_attribute *bin_attr,
3117 char *buf, loff_t off, size_t count)
3119 struct device *dev = container_of(kobj, struct device, kobj);
3120 struct Scsi_Host *shost = class_to_shost(dev);
3121 struct ibmvfc_host *vhost = shost_priv(shost);
3122 unsigned long flags = 0;
3123 int size = IBMVFC_TRACE_SIZE;
3124 char *src = (char *)vhost->trace;
3128 if (off + count > size) {
3133 spin_lock_irqsave(shost->host_lock, flags);
3134 memcpy(buf, &src[off], count);
3135 spin_unlock_irqrestore(shost->host_lock, flags);
3139 static struct bin_attribute ibmvfc_trace_attr = {
3145 .read = ibmvfc_read_trace,
3149 static struct device_attribute *ibmvfc_attrs[] = {
3150 &dev_attr_partition_name,
3151 &dev_attr_device_name,
3152 &dev_attr_port_loc_code,
3154 &dev_attr_npiv_version,
3155 &dev_attr_capabilities,
3156 &dev_attr_log_level,
3160 static struct scsi_host_template driver_template = {
3161 .module = THIS_MODULE,
3162 .name = "IBM POWER Virtual FC Adapter",
3163 .proc_name = IBMVFC_NAME,
3164 .queuecommand = ibmvfc_queuecommand,
3165 .eh_timed_out = fc_eh_timed_out,
3166 .eh_abort_handler = ibmvfc_eh_abort_handler,
3167 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3168 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3169 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3170 .slave_alloc = ibmvfc_slave_alloc,
3171 .slave_configure = ibmvfc_slave_configure,
3172 .target_alloc = ibmvfc_target_alloc,
3173 .scan_finished = ibmvfc_scan_finished,
3174 .change_queue_depth = ibmvfc_change_queue_depth,
3176 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3178 .sg_tablesize = SG_ALL,
3179 .max_sectors = IBMVFC_MAX_SECTORS,
3180 .shost_attrs = ibmvfc_attrs,
3181 .track_queue_depth = 1,
3185 * ibmvfc_next_async_crq - Returns the next entry in async queue
3186 * @vhost: ibmvfc host struct
3189 * Pointer to next entry in queue / NULL if empty
3191 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3193 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3194 struct ibmvfc_async_crq *crq;
3196 crq = &async_crq->msgs[async_crq->cur];
3197 if (crq->valid & 0x80) {
3198 if (++async_crq->cur == async_crq->size)
3208 * ibmvfc_next_crq - Returns the next entry in message queue
3209 * @vhost: ibmvfc host struct
3212 * Pointer to next entry in queue / NULL if empty
3214 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3216 struct ibmvfc_crq_queue *queue = &vhost->crq;
3217 struct ibmvfc_crq *crq;
3219 crq = &queue->msgs[queue->cur];
3220 if (crq->valid & 0x80) {
3221 if (++queue->cur == queue->size)
3231 * ibmvfc_interrupt - Interrupt handler
3232 * @irq: number of irq to handle, not used
3233 * @dev_instance: ibmvfc_host that received interrupt
3238 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3240 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3241 unsigned long flags;
3243 spin_lock_irqsave(vhost->host->host_lock, flags);
3244 vio_disable_interrupts(to_vio_dev(vhost->dev));
3245 tasklet_schedule(&vhost->tasklet);
3246 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3251 * ibmvfc_tasklet - Interrupt handler tasklet
3252 * @data: ibmvfc host struct
3257 static void ibmvfc_tasklet(void *data)
3259 struct ibmvfc_host *vhost = data;
3260 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3261 struct ibmvfc_crq *crq;
3262 struct ibmvfc_async_crq *async;
3263 unsigned long flags;
3266 spin_lock_irqsave(vhost->host->host_lock, flags);
3268 /* Pull all the valid messages off the async CRQ */
3269 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3270 ibmvfc_handle_async(async, vhost);
3275 /* Pull all the valid messages off the CRQ */
3276 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3277 ibmvfc_handle_crq(crq, vhost);
3282 vio_enable_interrupts(vdev);
3283 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3284 vio_disable_interrupts(vdev);
3285 ibmvfc_handle_async(async, vhost);
3288 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3289 vio_disable_interrupts(vdev);
3290 ibmvfc_handle_crq(crq, vhost);
3297 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3301 * ibmvfc_init_tgt - Set the next init job step for the target
3302 * @tgt: ibmvfc target struct
3303 * @job_step: job step to perform
3306 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3307 void (*job_step) (struct ibmvfc_target *))
3309 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3310 tgt->job_step = job_step;
3311 wake_up(&tgt->vhost->work_wait_q);
3315 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3316 * @tgt: ibmvfc target struct
3317 * @job_step: initialization job step
3319 * Returns: 1 if step will be retried / 0 if not
3322 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3323 void (*job_step) (struct ibmvfc_target *))
3325 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3326 ibmvfc_del_tgt(tgt);
3327 wake_up(&tgt->vhost->work_wait_q);
3330 ibmvfc_init_tgt(tgt, job_step);
3334 /* Defined in FC-LS */
3335 static const struct {
3352 * ibmvfc_get_prli_rsp - Find PRLI response index
3353 * @flags: PRLI response flags
3356 static int ibmvfc_get_prli_rsp(u16 flags)
3359 int code = (flags & 0x0f00) >> 8;
3361 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3362 if (prli_rsp[i].code == code)
3369 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3370 * @evt: ibmvfc event struct
3373 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3375 struct ibmvfc_target *tgt = evt->tgt;
3376 struct ibmvfc_host *vhost = evt->vhost;
3377 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3378 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3379 u32 status = be16_to_cpu(rsp->common.status);
3380 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3382 vhost->discovery_threads--;
3383 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3385 case IBMVFC_MAD_SUCCESS:
3386 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3387 parms->type, parms->flags, parms->service_parms);
3389 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3390 index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3391 if (prli_rsp[index].logged_in) {
3392 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3393 tgt->need_login = 0;
3395 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3396 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3397 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3398 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3401 ibmvfc_del_tgt(tgt);
3402 } else if (prli_rsp[index].retry)
3403 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3405 ibmvfc_del_tgt(tgt);
3407 ibmvfc_del_tgt(tgt);
3409 case IBMVFC_MAD_DRIVER_FAILED:
3411 case IBMVFC_MAD_CRQ_ERROR:
3412 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3414 case IBMVFC_MAD_FAILED:
3416 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3417 be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3418 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3419 else if (tgt->logo_rcvd)
3420 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3421 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3422 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3424 ibmvfc_del_tgt(tgt);
3426 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3427 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3428 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
3432 kref_put(&tgt->kref, ibmvfc_release_tgt);
3433 ibmvfc_free_event(evt);
3434 wake_up(&vhost->work_wait_q);
3438 * ibmvfc_tgt_send_prli - Send a process login
3439 * @tgt: ibmvfc target struct
3442 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3444 struct ibmvfc_process_login *prli;
3445 struct ibmvfc_host *vhost = tgt->vhost;
3446 struct ibmvfc_event *evt;
3448 if (vhost->discovery_threads >= disc_threads)
3451 kref_get(&tgt->kref);
3452 evt = ibmvfc_get_event(vhost);
3453 vhost->discovery_threads++;
3454 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3456 prli = &evt->iu.prli;
3457 memset(prli, 0, sizeof(*prli));
3458 prli->common.version = cpu_to_be32(1);
3459 prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
3460 prli->common.length = cpu_to_be16(sizeof(*prli));
3461 prli->scsi_id = cpu_to_be64(tgt->scsi_id);
3463 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3464 prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
3465 prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
3466 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
3469 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
3471 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3472 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3473 vhost->discovery_threads--;
3474 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3475 kref_put(&tgt->kref, ibmvfc_release_tgt);
3477 tgt_dbg(tgt, "Sent process login\n");
3481 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3482 * @evt: ibmvfc event struct
3485 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3487 struct ibmvfc_target *tgt = evt->tgt;
3488 struct ibmvfc_host *vhost = evt->vhost;
3489 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3490 u32 status = be16_to_cpu(rsp->common.status);
3491 int level = IBMVFC_DEFAULT_LOG_LEVEL;
3493 vhost->discovery_threads--;
3494 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3496 case IBMVFC_MAD_SUCCESS:
3497 tgt_dbg(tgt, "Port Login succeeded\n");
3498 if (tgt->ids.port_name &&
3499 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3501 tgt_dbg(tgt, "Port re-init required\n");
3504 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3505 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3506 tgt->ids.port_id = tgt->scsi_id;
3507 memcpy(&tgt->service_parms, &rsp->service_parms,
3508 sizeof(tgt->service_parms));
3509 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3510 sizeof(tgt->service_parms_change));
3511 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3513 case IBMVFC_MAD_DRIVER_FAILED:
3515 case IBMVFC_MAD_CRQ_ERROR:
3516 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3518 case IBMVFC_MAD_FAILED:
3520 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3521 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3523 ibmvfc_del_tgt(tgt);
3525 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3526 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3527 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
3528 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
3529 ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
3533 kref_put(&tgt->kref, ibmvfc_release_tgt);
3534 ibmvfc_free_event(evt);
3535 wake_up(&vhost->work_wait_q);
3539 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3540 * @tgt: ibmvfc target struct
3543 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3545 struct ibmvfc_port_login *plogi;
3546 struct ibmvfc_host *vhost = tgt->vhost;
3547 struct ibmvfc_event *evt;
3549 if (vhost->discovery_threads >= disc_threads)
3552 kref_get(&tgt->kref);
3554 evt = ibmvfc_get_event(vhost);
3555 vhost->discovery_threads++;
3556 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3557 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3559 plogi = &evt->iu.plogi;
3560 memset(plogi, 0, sizeof(*plogi));
3561 plogi->common.version = cpu_to_be32(1);
3562 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
3563 plogi->common.length = cpu_to_be16(sizeof(*plogi));
3564 plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
3566 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3567 vhost->discovery_threads--;
3568 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3569 kref_put(&tgt->kref, ibmvfc_release_tgt);
3571 tgt_dbg(tgt, "Sent port login\n");
3575 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3576 * @evt: ibmvfc event struct
3579 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3581 struct ibmvfc_target *tgt = evt->tgt;
3582 struct ibmvfc_host *vhost = evt->vhost;
3583 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
3584 u32 status = be16_to_cpu(rsp->common.status);
3586 vhost->discovery_threads--;
3587 ibmvfc_free_event(evt);
3588 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3591 case IBMVFC_MAD_SUCCESS:
3592 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3594 case IBMVFC_MAD_DRIVER_FAILED:
3595 kref_put(&tgt->kref, ibmvfc_release_tgt);
3596 wake_up(&vhost->work_wait_q);
3598 case IBMVFC_MAD_FAILED:
3600 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3604 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3605 kref_put(&tgt->kref, ibmvfc_release_tgt);
3606 wake_up(&vhost->work_wait_q);
3610 * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
3611 * @tgt: ibmvfc target struct
3614 * Allocated and initialized ibmvfc_event struct
3616 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
3617 void (*done) (struct ibmvfc_event *))
3619 struct ibmvfc_implicit_logout *mad;
3620 struct ibmvfc_host *vhost = tgt->vhost;
3621 struct ibmvfc_event *evt;
3623 kref_get(&tgt->kref);
3624 evt = ibmvfc_get_event(vhost);
3625 ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
3627 mad = &evt->iu.implicit_logout;
3628 memset(mad, 0, sizeof(*mad));
3629 mad->common.version = cpu_to_be32(1);
3630 mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
3631 mad->common.length = cpu_to_be16(sizeof(*mad));
3632 mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
3637 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3638 * @tgt: ibmvfc target struct
3641 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3643 struct ibmvfc_host *vhost = tgt->vhost;
3644 struct ibmvfc_event *evt;
3646 if (vhost->discovery_threads >= disc_threads)
3649 vhost->discovery_threads++;
3650 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
3651 ibmvfc_tgt_implicit_logout_done);
3653 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3654 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3655 vhost->discovery_threads--;
3656 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3657 kref_put(&tgt->kref, ibmvfc_release_tgt);
3659 tgt_dbg(tgt, "Sent Implicit Logout\n");
3663 * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
3664 * @evt: ibmvfc event struct
3667 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
3669 struct ibmvfc_target *tgt = evt->tgt;
3670 struct ibmvfc_host *vhost = evt->vhost;
3671 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3672 u32 status = be16_to_cpu(mad->common.status);
3674 vhost->discovery_threads--;
3675 ibmvfc_free_event(evt);
3678 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
3679 * driver in which case we need to free up all the targets. If we are
3680 * not unloading, we will still go through a hard reset to get out of
3681 * offline state, so there is no need to track the old targets in that
3684 if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
3685 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3687 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
3689 tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
3690 kref_put(&tgt->kref, ibmvfc_release_tgt);
3691 wake_up(&vhost->work_wait_q);
3695 * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
3696 * @tgt: ibmvfc target struct
3699 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
3701 struct ibmvfc_host *vhost = tgt->vhost;
3702 struct ibmvfc_event *evt;
3704 if (!vhost->logged_in) {
3705 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3709 if (vhost->discovery_threads >= disc_threads)
3712 vhost->discovery_threads++;
3713 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
3714 ibmvfc_tgt_implicit_logout_and_del_done);
3716 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
3717 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3718 vhost->discovery_threads--;
3719 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3720 kref_put(&tgt->kref, ibmvfc_release_tgt);
3722 tgt_dbg(tgt, "Sent Implicit Logout\n");
3726 * ibmvfc_tgt_move_login_done - Completion handler for Move Login
3727 * @evt: ibmvfc event struct
3730 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
3732 struct ibmvfc_target *tgt = evt->tgt;
3733 struct ibmvfc_host *vhost = evt->vhost;
3734 struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
3735 u32 status = be16_to_cpu(rsp->common.status);
3736 int level = IBMVFC_DEFAULT_LOG_LEVEL;
3738 vhost->discovery_threads--;
3739 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3741 case IBMVFC_MAD_SUCCESS:
3742 tgt_dbg(tgt, "Move Login succeeded for old scsi_id: %llX\n", tgt->old_scsi_id);
3743 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3744 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3745 tgt->ids.port_id = tgt->scsi_id;
3746 memcpy(&tgt->service_parms, &rsp->service_parms,
3747 sizeof(tgt->service_parms));
3748 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3749 sizeof(tgt->service_parms_change));
3750 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3752 case IBMVFC_MAD_DRIVER_FAILED:
3754 case IBMVFC_MAD_CRQ_ERROR:
3755 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
3757 case IBMVFC_MAD_FAILED:
3759 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
3762 "Move Login failed: old scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
3763 tgt->old_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
3768 kref_put(&tgt->kref, ibmvfc_release_tgt);
3769 ibmvfc_free_event(evt);
3770 wake_up(&vhost->work_wait_q);
3775 * ibmvfc_tgt_move_login - Initiate a move login for specified target
3776 * @tgt: ibmvfc target struct
3779 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
3781 struct ibmvfc_host *vhost = tgt->vhost;
3782 struct ibmvfc_move_login *move;
3783 struct ibmvfc_event *evt;
3785 if (vhost->discovery_threads >= disc_threads)
3788 kref_get(&tgt->kref);
3789 evt = ibmvfc_get_event(vhost);
3790 vhost->discovery_threads++;
3791 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3792 ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
3794 move = &evt->iu.move_login;
3795 memset(move, 0, sizeof(*move));
3796 move->common.version = cpu_to_be32(1);
3797 move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
3798 move->common.length = cpu_to_be16(sizeof(*move));
3800 move->old_scsi_id = cpu_to_be64(tgt->old_scsi_id);
3801 move->new_scsi_id = cpu_to_be64(tgt->scsi_id);
3802 move->wwpn = cpu_to_be64(tgt->wwpn);
3803 move->node_name = cpu_to_be64(tgt->ids.node_name);
3805 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3806 vhost->discovery_threads--;
3807 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3808 kref_put(&tgt->kref, ibmvfc_release_tgt);
3810 tgt_dbg(tgt, "Sent Move Login for old scsi_id: %llX\n", tgt->old_scsi_id);
3814 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3815 * @mad: ibmvfc passthru mad struct
3816 * @tgt: ibmvfc target struct
3819 * 1 if PLOGI needed / 0 if PLOGI not needed
3821 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3822 struct ibmvfc_target *tgt)
3824 if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
3826 if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
3828 if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
3834 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3835 * @evt: ibmvfc event struct
3838 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3840 struct ibmvfc_target *tgt = evt->tgt;
3841 struct ibmvfc_host *vhost = evt->vhost;
3842 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3843 u32 status = be16_to_cpu(mad->common.status);
3844 u8 fc_reason, fc_explain;
3846 vhost->discovery_threads--;
3847 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3848 del_timer(&tgt->timer);
3851 case IBMVFC_MAD_SUCCESS:
3852 tgt_dbg(tgt, "ADISC succeeded\n");
3853 if (ibmvfc_adisc_needs_plogi(mad, tgt))
3854 ibmvfc_del_tgt(tgt);
3856 case IBMVFC_MAD_DRIVER_FAILED:
3858 case IBMVFC_MAD_FAILED:
3860 ibmvfc_del_tgt(tgt);
3861 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
3862 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
3863 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3864 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
3865 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
3866 ibmvfc_get_fc_type(fc_reason), fc_reason,
3867 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3871 kref_put(&tgt->kref, ibmvfc_release_tgt);
3872 ibmvfc_free_event(evt);
3873 wake_up(&vhost->work_wait_q);
3877 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3878 * @evt: ibmvfc event struct
3881 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3883 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3885 memset(mad, 0, sizeof(*mad));
3886 mad->common.version = cpu_to_be32(1);
3887 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
3888 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
3889 mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3890 offsetof(struct ibmvfc_passthru_mad, iu));
3891 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
3892 mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3893 mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
3894 mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3895 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3896 offsetof(struct ibmvfc_passthru_fc_iu, payload));
3897 mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3898 mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3899 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3900 offsetof(struct ibmvfc_passthru_fc_iu, response));
3901 mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
3905 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3906 * @evt: ibmvfc event struct
3908 * Just cleanup this event struct. Everything else is handled by
3909 * the ADISC completion handler. If the ADISC never actually comes
3910 * back, we still have the timer running on the ADISC event struct
3911 * which will fire and cause the CRQ to get reset.
3914 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3916 struct ibmvfc_host *vhost = evt->vhost;
3917 struct ibmvfc_target *tgt = evt->tgt;
3919 tgt_dbg(tgt, "ADISC cancel complete\n");
3920 vhost->abort_threads--;
3921 ibmvfc_free_event(evt);
3922 kref_put(&tgt->kref, ibmvfc_release_tgt);
3923 wake_up(&vhost->work_wait_q);
3927 * ibmvfc_adisc_timeout - Handle an ADISC timeout
3928 * @tgt: ibmvfc target struct
3930 * If an ADISC times out, send a cancel. If the cancel times
3931 * out, reset the CRQ. When the ADISC comes back as cancelled,
3932 * log back into the target.
3934 static void ibmvfc_adisc_timeout(struct timer_list *t)
3936 struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
3937 struct ibmvfc_host *vhost = tgt->vhost;
3938 struct ibmvfc_event *evt;
3939 struct ibmvfc_tmf *tmf;
3940 unsigned long flags;
3943 tgt_dbg(tgt, "ADISC timeout\n");
3944 spin_lock_irqsave(vhost->host->host_lock, flags);
3945 if (vhost->abort_threads >= disc_threads ||
3946 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3947 vhost->state != IBMVFC_INITIALIZING ||
3948 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3949 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3953 vhost->abort_threads++;
3954 kref_get(&tgt->kref);
3955 evt = ibmvfc_get_event(vhost);
3956 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3960 memset(tmf, 0, sizeof(*tmf));
3961 tmf->common.version = cpu_to_be32(1);
3962 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
3963 tmf->common.length = cpu_to_be16(sizeof(*tmf));
3964 tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
3965 tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
3967 rc = ibmvfc_send_event(evt, vhost, default_timeout);
3970 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3971 vhost->abort_threads--;
3972 kref_put(&tgt->kref, ibmvfc_release_tgt);
3973 __ibmvfc_reset_host(vhost);
3975 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3976 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3980 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3981 * @tgt: ibmvfc target struct
3983 * When sending an ADISC we end up with two timers running. The
3984 * first timer is the timer in the ibmvfc target struct. If this
3985 * fires, we send a cancel to the target. The second timer is the
3986 * timer on the ibmvfc event for the ADISC, which is longer. If that
3987 * fires, it means the ADISC timed out and our attempt to cancel it
3988 * also failed, so we need to reset the CRQ.
3990 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3992 struct ibmvfc_passthru_mad *mad;
3993 struct ibmvfc_host *vhost = tgt->vhost;
3994 struct ibmvfc_event *evt;
3996 if (vhost->discovery_threads >= disc_threads)
3999 kref_get(&tgt->kref);
4000 evt = ibmvfc_get_event(vhost);
4001 vhost->discovery_threads++;
4002 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
4005 ibmvfc_init_passthru(evt);
4006 mad = &evt->iu.passthru;
4007 mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4008 mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4009 mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4011 mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4012 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4013 sizeof(vhost->login_buf->resp.port_name));
4014 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4015 sizeof(vhost->login_buf->resp.node_name));
4016 mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4018 if (timer_pending(&tgt->timer))
4019 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4021 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4022 add_timer(&tgt->timer);
4025 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4026 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4027 vhost->discovery_threads--;
4028 del_timer(&tgt->timer);
4029 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4030 kref_put(&tgt->kref, ibmvfc_release_tgt);
4032 tgt_dbg(tgt, "Sent ADISC\n");
4036 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4037 * @evt: ibmvfc event struct
4040 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4042 struct ibmvfc_target *tgt = evt->tgt;
4043 struct ibmvfc_host *vhost = evt->vhost;
4044 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4045 u32 status = be16_to_cpu(rsp->common.status);
4046 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4048 vhost->discovery_threads--;
4049 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4051 case IBMVFC_MAD_SUCCESS:
4052 tgt_dbg(tgt, "Query Target succeeded\n");
4053 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4054 ibmvfc_del_tgt(tgt);
4056 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4058 case IBMVFC_MAD_DRIVER_FAILED:
4060 case IBMVFC_MAD_CRQ_ERROR:
4061 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4063 case IBMVFC_MAD_FAILED:
4065 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4066 be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4067 be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4068 ibmvfc_del_tgt(tgt);
4069 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4070 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4072 ibmvfc_del_tgt(tgt);
4074 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4075 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4076 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4077 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4078 ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4083 kref_put(&tgt->kref, ibmvfc_release_tgt);
4084 ibmvfc_free_event(evt);
4085 wake_up(&vhost->work_wait_q);
4089 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4090 * @tgt: ibmvfc target struct
4093 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4095 struct ibmvfc_query_tgt *query_tgt;
4096 struct ibmvfc_host *vhost = tgt->vhost;
4097 struct ibmvfc_event *evt;
4099 if (vhost->discovery_threads >= disc_threads)
4102 kref_get(&tgt->kref);
4103 evt = ibmvfc_get_event(vhost);
4104 vhost->discovery_threads++;
4106 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4107 query_tgt = &evt->iu.query_tgt;
4108 memset(query_tgt, 0, sizeof(*query_tgt));
4109 query_tgt->common.version = cpu_to_be32(1);
4110 query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4111 query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4112 query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4114 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4115 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4116 vhost->discovery_threads--;
4117 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4118 kref_put(&tgt->kref, ibmvfc_release_tgt);
4120 tgt_dbg(tgt, "Sent Query Target\n");
4124 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4125 * @vhost: ibmvfc host struct
4126 * @scsi_id: SCSI ID to allocate target for
4129 * 0 on success / other on failure
4131 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4132 struct ibmvfc_discover_targets_entry *target)
4134 struct ibmvfc_target *stgt = NULL;
4135 struct ibmvfc_target *wtgt = NULL;
4136 struct ibmvfc_target *tgt;
4137 unsigned long flags;
4138 u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4139 u64 wwpn = be64_to_cpu(target->wwpn);
4141 /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4142 spin_lock_irqsave(vhost->host->host_lock, flags);
4143 list_for_each_entry(tgt, &vhost->targets, queue) {
4144 if (tgt->wwpn == wwpn) {
4150 list_for_each_entry(tgt, &vhost->targets, queue) {
4151 if (tgt->scsi_id == scsi_id) {
4157 if (wtgt && !stgt) {
4159 * A WWPN target has moved and we still are tracking the old
4160 * SCSI ID. The only way we should be able to get here is if
4161 * we attempted to send an implicit logout for the old SCSI ID
4162 * and it failed for some reason, such as there being I/O
4163 * pending to the target. In this case, we will have already
4164 * deleted the rport from the FC transport so we do a move
4165 * login, which works even with I/O pending, as it will cancel
4166 * any active commands.
4168 if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4170 * Do a move login here. The old target is no longer
4171 * known to the transport layer We don't use the
4172 * normal ibmvfc_set_tgt_action to set this, as we
4173 * don't normally want to allow this state change.
4175 wtgt->old_scsi_id = wtgt->scsi_id;
4176 wtgt->scsi_id = scsi_id;
4177 wtgt->action = IBMVFC_TGT_ACTION_INIT;
4178 ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4181 tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4182 wtgt->action, wtgt->rport);
4185 if (tgt->need_login)
4186 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4189 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4191 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4192 memset(tgt, 0, sizeof(*tgt));
4193 tgt->scsi_id = scsi_id;
4196 tgt->need_login = 1;
4197 timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4198 kref_init(&tgt->kref);
4199 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4200 spin_lock_irqsave(vhost->host->host_lock, flags);
4201 tgt->cancel_key = vhost->task_set++;
4202 list_add_tail(&tgt->queue, &vhost->targets);
4205 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4210 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4211 * @vhost: ibmvfc host struct
4214 * 0 on success / other on failure
4216 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4220 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4221 rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
4227 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4228 * @evt: ibmvfc event struct
4231 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4233 struct ibmvfc_host *vhost = evt->vhost;
4234 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4235 u32 mad_status = be16_to_cpu(rsp->common.status);
4236 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4238 switch (mad_status) {
4239 case IBMVFC_MAD_SUCCESS:
4240 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4241 vhost->num_targets = be32_to_cpu(rsp->num_written);
4242 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4244 case IBMVFC_MAD_FAILED:
4245 level += ibmvfc_retry_host_init(vhost);
4246 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4247 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4248 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4250 case IBMVFC_MAD_DRIVER_FAILED:
4253 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4254 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4258 ibmvfc_free_event(evt);
4259 wake_up(&vhost->work_wait_q);
4263 * ibmvfc_discover_targets - Send Discover Targets MAD
4264 * @vhost: ibmvfc host struct
4267 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4269 struct ibmvfc_discover_targets *mad;
4270 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4272 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4273 mad = &evt->iu.discover_targets;
4274 memset(mad, 0, sizeof(*mad));
4275 mad->common.version = cpu_to_be32(1);
4276 mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4277 mad->common.length = cpu_to_be16(sizeof(*mad));
4278 mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4279 mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4280 mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4281 mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
4282 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4284 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4285 ibmvfc_dbg(vhost, "Sent discover targets\n");
4287 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4291 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4292 * @evt: ibmvfc event struct
4295 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4297 struct ibmvfc_host *vhost = evt->vhost;
4298 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
4299 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
4300 unsigned int npiv_max_sectors;
4301 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4303 switch (mad_status) {
4304 case IBMVFC_MAD_SUCCESS:
4305 ibmvfc_free_event(evt);
4307 case IBMVFC_MAD_FAILED:
4308 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4309 level += ibmvfc_retry_host_init(vhost);
4311 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4312 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
4313 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4314 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4315 ibmvfc_free_event(evt);
4317 case IBMVFC_MAD_CRQ_ERROR:
4318 ibmvfc_retry_host_init(vhost);
4320 case IBMVFC_MAD_DRIVER_FAILED:
4321 ibmvfc_free_event(evt);
4324 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4325 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4326 ibmvfc_free_event(evt);
4330 vhost->client_migrated = 0;
4332 if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
4333 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4335 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4336 wake_up(&vhost->work_wait_q);
4340 if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
4341 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4343 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4344 wake_up(&vhost->work_wait_q);
4348 vhost->logged_in = 1;
4349 npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
4350 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4351 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4352 rsp->drc_name, npiv_max_sectors);
4354 fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
4355 fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
4356 fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
4357 fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
4358 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4359 fc_host_supported_classes(vhost->host) = 0;
4360 if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
4361 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
4362 if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
4363 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
4364 if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
4365 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4366 fc_host_maxframe_size(vhost->host) =
4367 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
4369 vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
4370 vhost->host->max_sectors = npiv_max_sectors;
4371 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4372 wake_up(&vhost->work_wait_q);
4376 * ibmvfc_npiv_login - Sends NPIV login
4377 * @vhost: ibmvfc host struct
4380 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4382 struct ibmvfc_npiv_login_mad *mad;
4383 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4385 ibmvfc_gather_partition_info(vhost);
4386 ibmvfc_set_login_info(vhost);
4387 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4389 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4390 mad = &evt->iu.npiv_login;
4391 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
4392 mad->common.version = cpu_to_be32(1);
4393 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
4394 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
4395 mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
4396 mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
4398 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4400 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4401 ibmvfc_dbg(vhost, "Sent NPIV login\n");
4403 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4407 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4408 * @vhost: ibmvfc host struct
4411 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4413 struct ibmvfc_host *vhost = evt->vhost;
4414 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
4416 ibmvfc_free_event(evt);
4418 switch (mad_status) {
4419 case IBMVFC_MAD_SUCCESS:
4420 if (list_empty(&vhost->sent) &&
4421 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
4422 ibmvfc_init_host(vhost);
4426 case IBMVFC_MAD_FAILED:
4427 case IBMVFC_MAD_NOT_SUPPORTED:
4428 case IBMVFC_MAD_CRQ_ERROR:
4429 case IBMVFC_MAD_DRIVER_FAILED:
4431 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4435 ibmvfc_hard_reset_host(vhost);
4439 * ibmvfc_npiv_logout - Issue an NPIV Logout
4440 * @vhost: ibmvfc host struct
4443 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4445 struct ibmvfc_npiv_logout_mad *mad;
4446 struct ibmvfc_event *evt;
4448 evt = ibmvfc_get_event(vhost);
4449 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4451 mad = &evt->iu.npiv_logout;
4452 memset(mad, 0, sizeof(*mad));
4453 mad->common.version = cpu_to_be32(1);
4454 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
4455 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
4457 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4459 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4460 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4462 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4466 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4467 * @vhost: ibmvfc host struct
4470 * 1 if work to do / 0 if not
4472 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4474 struct ibmvfc_target *tgt;
4476 list_for_each_entry(tgt, &vhost->targets, queue) {
4477 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4478 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4486 * ibmvfc_dev_logo_to_do - Is there target logout work to do?
4487 * @vhost: ibmvfc host struct
4490 * 1 if work to do / 0 if not
4492 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
4494 struct ibmvfc_target *tgt;
4496 list_for_each_entry(tgt, &vhost->targets, queue) {
4497 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
4498 tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
4505 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4506 * @vhost: ibmvfc host struct
4509 * 1 if work to do / 0 if not
4511 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4513 struct ibmvfc_target *tgt;
4515 if (kthread_should_stop())
4517 switch (vhost->action) {
4518 case IBMVFC_HOST_ACTION_NONE:
4519 case IBMVFC_HOST_ACTION_INIT_WAIT:
4520 case IBMVFC_HOST_ACTION_LOGO_WAIT:
4522 case IBMVFC_HOST_ACTION_TGT_INIT:
4523 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4524 if (vhost->discovery_threads == disc_threads)
4526 list_for_each_entry(tgt, &vhost->targets, queue)
4527 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4529 list_for_each_entry(tgt, &vhost->targets, queue)
4530 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4533 case IBMVFC_HOST_ACTION_TGT_DEL:
4534 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4535 if (vhost->discovery_threads == disc_threads)
4537 list_for_each_entry(tgt, &vhost->targets, queue)
4538 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
4540 list_for_each_entry(tgt, &vhost->targets, queue)
4541 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
4544 case IBMVFC_HOST_ACTION_LOGO:
4545 case IBMVFC_HOST_ACTION_INIT:
4546 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4547 case IBMVFC_HOST_ACTION_QUERY:
4548 case IBMVFC_HOST_ACTION_RESET:
4549 case IBMVFC_HOST_ACTION_REENABLE:
4558 * ibmvfc_work_to_do - Is there task level work to do?
4559 * @vhost: ibmvfc host struct
4562 * 1 if work to do / 0 if not
4564 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4566 unsigned long flags;
4569 spin_lock_irqsave(vhost->host->host_lock, flags);
4570 rc = __ibmvfc_work_to_do(vhost);
4571 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4576 * ibmvfc_log_ae - Log async events if necessary
4577 * @vhost: ibmvfc host struct
4578 * @events: events to log
4581 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4583 if (events & IBMVFC_AE_RSCN)
4584 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4585 if ((events & IBMVFC_AE_LINKDOWN) &&
4586 vhost->state >= IBMVFC_HALTED)
4587 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4588 if ((events & IBMVFC_AE_LINKUP) &&
4589 vhost->state == IBMVFC_INITIALIZING)
4590 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4594 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4595 * @tgt: ibmvfc target struct
4598 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4600 struct ibmvfc_host *vhost = tgt->vhost;
4601 struct fc_rport *rport;
4602 unsigned long flags;
4604 tgt_dbg(tgt, "Adding rport\n");
4605 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4606 spin_lock_irqsave(vhost->host->host_lock, flags);
4608 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4609 tgt_dbg(tgt, "Deleting rport\n");
4610 list_del(&tgt->queue);
4611 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4612 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4613 fc_remote_port_delete(rport);
4614 del_timer_sync(&tgt->timer);
4615 kref_put(&tgt->kref, ibmvfc_release_tgt);
4617 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
4618 tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
4619 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
4621 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4622 fc_remote_port_delete(rport);
4624 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4625 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4630 tgt_dbg(tgt, "rport add succeeded\n");
4632 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
4633 rport->supported_classes = 0;
4634 tgt->target_id = rport->scsi_target_id;
4635 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
4636 rport->supported_classes |= FC_COS_CLASS1;
4637 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
4638 rport->supported_classes |= FC_COS_CLASS2;
4639 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
4640 rport->supported_classes |= FC_COS_CLASS3;
4642 blk_queue_max_segments(rport->rqst_q, 1);
4644 tgt_dbg(tgt, "rport add failed\n");
4645 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4649 * ibmvfc_do_work - Do task level work
4650 * @vhost: ibmvfc host struct
4653 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4655 struct ibmvfc_target *tgt;
4656 unsigned long flags;
4657 struct fc_rport *rport;
4660 ibmvfc_log_ae(vhost, vhost->events_to_log);
4661 spin_lock_irqsave(vhost->host->host_lock, flags);
4662 vhost->events_to_log = 0;
4663 switch (vhost->action) {
4664 case IBMVFC_HOST_ACTION_NONE:
4665 case IBMVFC_HOST_ACTION_LOGO_WAIT:
4666 case IBMVFC_HOST_ACTION_INIT_WAIT:
4668 case IBMVFC_HOST_ACTION_RESET:
4669 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4670 rc = ibmvfc_reset_crq(vhost);
4672 spin_lock_irqsave(vhost->host->host_lock, flags);
4673 if (!rc || rc == H_CLOSED)
4674 vio_enable_interrupts(to_vio_dev(vhost->dev));
4675 if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
4677 * The only action we could have changed to would have
4678 * been reenable, in which case, we skip the rest of
4679 * this path and wait until we've done the re-enable
4680 * before sending the crq init.
4682 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4684 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4685 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
4686 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4687 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4691 case IBMVFC_HOST_ACTION_REENABLE:
4692 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4693 rc = ibmvfc_reenable_crq_queue(vhost);
4695 spin_lock_irqsave(vhost->host->host_lock, flags);
4696 if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
4698 * The only action we could have changed to would have
4699 * been reset, in which case, we skip the rest of this
4700 * path and wait until we've done the reset before
4701 * sending the crq init.
4703 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4704 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4705 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4706 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4710 case IBMVFC_HOST_ACTION_LOGO:
4711 vhost->job_step(vhost);
4713 case IBMVFC_HOST_ACTION_INIT:
4714 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
4715 if (vhost->delay_init) {
4716 vhost->delay_init = 0;
4717 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4721 vhost->job_step(vhost);
4723 case IBMVFC_HOST_ACTION_QUERY:
4724 list_for_each_entry(tgt, &vhost->targets, queue)
4725 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4726 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4728 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4729 list_for_each_entry(tgt, &vhost->targets, queue) {
4730 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4736 if (!ibmvfc_dev_init_to_do(vhost))
4737 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4739 case IBMVFC_HOST_ACTION_TGT_DEL:
4740 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4741 list_for_each_entry(tgt, &vhost->targets, queue) {
4742 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
4748 if (ibmvfc_dev_logo_to_do(vhost)) {
4749 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4753 list_for_each_entry(tgt, &vhost->targets, queue) {
4754 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4755 tgt_dbg(tgt, "Deleting rport\n");
4758 list_del(&tgt->queue);
4759 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4760 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4762 fc_remote_port_delete(rport);
4763 del_timer_sync(&tgt->timer);
4764 kref_put(&tgt->kref, ibmvfc_release_tgt);
4766 } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
4767 tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
4770 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
4771 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4773 fc_remote_port_delete(rport);
4778 if (vhost->state == IBMVFC_INITIALIZING) {
4779 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
4780 if (vhost->reinit) {
4782 scsi_block_requests(vhost->host);
4783 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4784 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4786 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4787 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4788 wake_up(&vhost->init_wait_q);
4789 schedule_work(&vhost->rport_add_work_q);
4790 vhost->init_retries = 0;
4791 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4792 scsi_unblock_requests(vhost->host);
4797 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4798 vhost->job_step = ibmvfc_discover_targets;
4801 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4802 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4803 scsi_unblock_requests(vhost->host);
4804 wake_up(&vhost->init_wait_q);
4808 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4809 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4810 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4811 ibmvfc_alloc_targets(vhost);
4812 spin_lock_irqsave(vhost->host->host_lock, flags);
4814 case IBMVFC_HOST_ACTION_TGT_INIT:
4815 list_for_each_entry(tgt, &vhost->targets, queue) {
4816 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4822 if (!ibmvfc_dev_init_to_do(vhost))
4823 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
4829 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4833 * ibmvfc_work - Do task level work
4834 * @data: ibmvfc host struct
4839 static int ibmvfc_work(void *data)
4841 struct ibmvfc_host *vhost = data;
4844 set_user_nice(current, MIN_NICE);
4847 rc = wait_event_interruptible(vhost->work_wait_q,
4848 ibmvfc_work_to_do(vhost));
4852 if (kthread_should_stop())
4855 ibmvfc_do_work(vhost);
4858 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4863 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4864 * @vhost: ibmvfc host struct
4866 * Allocates a page for messages, maps it for dma, and registers
4867 * the crq with the hypervisor.
4870 * zero on success / other on failure
4872 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4874 int rc, retrc = -ENOMEM;
4875 struct device *dev = vhost->dev;
4876 struct vio_dev *vdev = to_vio_dev(dev);
4877 struct ibmvfc_crq_queue *crq = &vhost->crq;
4880 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4885 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4886 crq->msg_token = dma_map_single(dev, crq->msgs,
4887 PAGE_SIZE, DMA_BIDIRECTIONAL);
4889 if (dma_mapping_error(dev, crq->msg_token))
4892 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4893 crq->msg_token, PAGE_SIZE);
4895 if (rc == H_RESOURCE)
4896 /* maybe kexecing and resource is busy. try a reset */
4897 retrc = rc = ibmvfc_reset_crq(vhost);
4900 dev_warn(dev, "Partner adapter not ready\n");
4902 dev_warn(dev, "Error %d opening adapter\n", rc);
4903 goto reg_crq_failed;
4908 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4910 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4911 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4912 goto req_irq_failed;
4915 if ((rc = vio_enable_interrupts(vdev))) {
4916 dev_err(dev, "Error %d enabling interrupts\n", rc);
4917 goto req_irq_failed;
4925 tasklet_kill(&vhost->tasklet);
4927 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4928 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4930 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4932 free_page((unsigned long)crq->msgs);
4937 * ibmvfc_free_mem - Free memory for vhost
4938 * @vhost: ibmvfc host struct
4943 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4945 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4948 mempool_destroy(vhost->tgt_pool);
4949 kfree(vhost->trace);
4950 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4951 vhost->disc_buf_dma);
4952 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4953 vhost->login_buf, vhost->login_buf_dma);
4954 dma_pool_destroy(vhost->sg_pool);
4955 dma_unmap_single(vhost->dev, async_q->msg_token,
4956 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4957 free_page((unsigned long)async_q->msgs);
4962 * ibmvfc_alloc_mem - Allocate memory for vhost
4963 * @vhost: ibmvfc host struct
4966 * 0 on success / non-zero on failure
4968 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4970 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4971 struct device *dev = vhost->dev;
4974 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4975 if (!async_q->msgs) {
4976 dev_err(dev, "Couldn't allocate async queue.\n");
4980 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4981 async_q->msg_token = dma_map_single(dev, async_q->msgs,
4982 async_q->size * sizeof(*async_q->msgs),
4985 if (dma_mapping_error(dev, async_q->msg_token)) {
4986 dev_err(dev, "Failed to map async queue\n");
4987 goto free_async_crq;
4990 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4991 SG_ALL * sizeof(struct srp_direct_buf),
4992 sizeof(struct srp_direct_buf), 0);
4994 if (!vhost->sg_pool) {
4995 dev_err(dev, "Failed to allocate sg pool\n");
4996 goto unmap_async_crq;
4999 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
5000 &vhost->login_buf_dma, GFP_KERNEL);
5002 if (!vhost->login_buf) {
5003 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
5007 vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
5008 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
5009 &vhost->disc_buf_dma, GFP_KERNEL);
5011 if (!vhost->disc_buf) {
5012 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
5013 goto free_login_buffer;
5016 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
5017 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
5020 goto free_disc_buffer;
5022 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
5023 sizeof(struct ibmvfc_target));
5025 if (!vhost->tgt_pool) {
5026 dev_err(dev, "Couldn't allocate target memory pool\n");
5034 kfree(vhost->trace);
5036 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
5037 vhost->disc_buf_dma);
5039 dma_free_coherent(dev, sizeof(*vhost->login_buf),
5040 vhost->login_buf, vhost->login_buf_dma);
5042 dma_pool_destroy(vhost->sg_pool);
5044 dma_unmap_single(dev, async_q->msg_token,
5045 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
5047 free_page((unsigned long)async_q->msgs);
5054 * ibmvfc_rport_add_thread - Worker thread for rport adds
5055 * @work: work struct
5058 static void ibmvfc_rport_add_thread(struct work_struct *work)
5060 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
5062 struct ibmvfc_target *tgt;
5063 struct fc_rport *rport;
5064 unsigned long flags;
5068 spin_lock_irqsave(vhost->host->host_lock, flags);
5071 if (vhost->state != IBMVFC_ACTIVE)
5074 list_for_each_entry(tgt, &vhost->targets, queue) {
5075 if (tgt->add_rport) {
5078 kref_get(&tgt->kref);
5081 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5082 ibmvfc_tgt_add_rport(tgt);
5083 } else if (get_device(&rport->dev)) {
5084 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5085 tgt_dbg(tgt, "Setting rport roles\n");
5086 fc_remote_port_rolechg(rport, tgt->ids.roles);
5087 put_device(&rport->dev);
5089 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5092 kref_put(&tgt->kref, ibmvfc_release_tgt);
5093 spin_lock_irqsave(vhost->host->host_lock, flags);
5099 if (vhost->state == IBMVFC_ACTIVE)
5100 vhost->scan_complete = 1;
5101 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5106 * ibmvfc_probe - Adapter hot plug add entry point
5107 * @vdev: vio device struct
5108 * @id: vio device id struct
5111 * 0 on success / non-zero on failure
5113 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
5115 struct ibmvfc_host *vhost;
5116 struct Scsi_Host *shost;
5117 struct device *dev = &vdev->dev;
5121 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
5123 dev_err(dev, "Couldn't allocate host data\n");
5127 shost->transportt = ibmvfc_transport_template;
5128 shost->can_queue = max_requests;
5129 shost->max_lun = max_lun;
5130 shost->max_id = max_targets;
5131 shost->max_sectors = IBMVFC_MAX_SECTORS;
5132 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
5133 shost->unique_id = shost->host_no;
5135 vhost = shost_priv(shost);
5136 INIT_LIST_HEAD(&vhost->sent);
5137 INIT_LIST_HEAD(&vhost->free);
5138 INIT_LIST_HEAD(&vhost->targets);
5139 sprintf(vhost->name, IBMVFC_NAME);
5140 vhost->host = shost;
5142 vhost->partition_number = -1;
5143 vhost->log_level = log_level;
5144 vhost->task_set = 1;
5145 strcpy(vhost->partition_name, "UNKNOWN");
5146 init_waitqueue_head(&vhost->work_wait_q);
5147 init_waitqueue_head(&vhost->init_wait_q);
5148 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
5149 mutex_init(&vhost->passthru_mutex);
5151 if ((rc = ibmvfc_alloc_mem(vhost)))
5152 goto free_scsi_host;
5154 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
5157 if (IS_ERR(vhost->work_thread)) {
5158 dev_err(dev, "Couldn't create kernel thread: %ld\n",
5159 PTR_ERR(vhost->work_thread));
5160 rc = PTR_ERR(vhost->work_thread);
5164 if ((rc = ibmvfc_init_crq(vhost))) {
5165 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
5169 if ((rc = ibmvfc_init_event_pool(vhost))) {
5170 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
5174 if ((rc = scsi_add_host(shost, dev)))
5175 goto release_event_pool;
5177 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
5179 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
5180 &ibmvfc_trace_attr))) {
5181 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
5185 if (shost_to_fc_host(shost)->rqst_q)
5186 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
5187 dev_set_drvdata(dev, vhost);
5188 spin_lock(&ibmvfc_driver_lock);
5189 list_add_tail(&vhost->queue, &ibmvfc_head);
5190 spin_unlock(&ibmvfc_driver_lock);
5192 ibmvfc_send_crq_init(vhost);
5193 scsi_scan_host(shost);
5197 scsi_remove_host(shost);
5199 ibmvfc_free_event_pool(vhost);
5201 ibmvfc_release_crq_queue(vhost);
5203 kthread_stop(vhost->work_thread);
5205 ibmvfc_free_mem(vhost);
5207 scsi_host_put(shost);
5214 * ibmvfc_remove - Adapter hot plug remove entry point
5215 * @vdev: vio device struct
5220 static int ibmvfc_remove(struct vio_dev *vdev)
5222 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
5223 unsigned long flags;
5226 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
5228 spin_lock_irqsave(vhost->host->host_lock, flags);
5229 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
5230 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5232 ibmvfc_wait_while_resetting(vhost);
5233 ibmvfc_release_crq_queue(vhost);
5234 kthread_stop(vhost->work_thread);
5235 fc_remove_host(vhost->host);
5236 scsi_remove_host(vhost->host);
5238 spin_lock_irqsave(vhost->host->host_lock, flags);
5239 ibmvfc_purge_requests(vhost, DID_ERROR);
5240 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5241 ibmvfc_free_event_pool(vhost);
5243 ibmvfc_free_mem(vhost);
5244 spin_lock(&ibmvfc_driver_lock);
5245 list_del(&vhost->queue);
5246 spin_unlock(&ibmvfc_driver_lock);
5247 scsi_host_put(vhost->host);
5253 * ibmvfc_resume - Resume from suspend
5254 * @dev: device struct
5256 * We may have lost an interrupt across suspend/resume, so kick the
5260 static int ibmvfc_resume(struct device *dev)
5262 unsigned long flags;
5263 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
5264 struct vio_dev *vdev = to_vio_dev(dev);
5266 spin_lock_irqsave(vhost->host->host_lock, flags);
5267 vio_disable_interrupts(vdev);
5268 tasklet_schedule(&vhost->tasklet);
5269 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5274 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
5275 * @vdev: vio device struct
5278 * Number of bytes the driver will need to DMA map at the same time in
5279 * order to perform well.
5281 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
5283 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
5284 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
5287 static const struct vio_device_id ibmvfc_device_table[] = {
5288 {"fcp", "IBM,vfc-client"},
5291 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
5293 static const struct dev_pm_ops ibmvfc_pm_ops = {
5294 .resume = ibmvfc_resume
5297 static struct vio_driver ibmvfc_driver = {
5298 .id_table = ibmvfc_device_table,
5299 .probe = ibmvfc_probe,
5300 .remove = ibmvfc_remove,
5301 .get_desired_dma = ibmvfc_get_desired_dma,
5302 .name = IBMVFC_NAME,
5303 .pm = &ibmvfc_pm_ops,
5306 static struct fc_function_template ibmvfc_transport_functions = {
5307 .show_host_fabric_name = 1,
5308 .show_host_node_name = 1,
5309 .show_host_port_name = 1,
5310 .show_host_supported_classes = 1,
5311 .show_host_port_type = 1,
5312 .show_host_port_id = 1,
5313 .show_host_maxframe_size = 1,
5315 .get_host_port_state = ibmvfc_get_host_port_state,
5316 .show_host_port_state = 1,
5318 .get_host_speed = ibmvfc_get_host_speed,
5319 .show_host_speed = 1,
5321 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
5322 .terminate_rport_io = ibmvfc_terminate_rport_io,
5324 .show_rport_maxframe_size = 1,
5325 .show_rport_supported_classes = 1,
5327 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
5328 .show_rport_dev_loss_tmo = 1,
5330 .get_starget_node_name = ibmvfc_get_starget_node_name,
5331 .show_starget_node_name = 1,
5333 .get_starget_port_name = ibmvfc_get_starget_port_name,
5334 .show_starget_port_name = 1,
5336 .get_starget_port_id = ibmvfc_get_starget_port_id,
5337 .show_starget_port_id = 1,
5339 .bsg_request = ibmvfc_bsg_request,
5340 .bsg_timeout = ibmvfc_bsg_timeout,
5344 * ibmvfc_module_init - Initialize the ibmvfc module
5347 * 0 on success / other on failure
5349 static int __init ibmvfc_module_init(void)
5353 if (!firmware_has_feature(FW_FEATURE_VIO))
5356 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
5357 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
5359 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
5360 if (!ibmvfc_transport_template)
5363 rc = vio_register_driver(&ibmvfc_driver);
5365 fc_release_transport(ibmvfc_transport_template);
5370 * ibmvfc_module_exit - Teardown the ibmvfc module
5375 static void __exit ibmvfc_module_exit(void)
5377 vio_unregister_driver(&ibmvfc_driver);
5378 fc_release_transport(ibmvfc_transport_template);
5381 module_init(ibmvfc_module_init);
5382 module_exit(ibmvfc_module_exit);