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)
640 scsi_block_requests(vhost->host);
641 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
642 vhost->job_step = ibmvfc_npiv_login;
643 wake_up(&vhost->work_wait_q);
648 * ibmvfc_send_crq - Send a CRQ
649 * @vhost: ibmvfc host struct
650 * @word1: the first 64 bits of the data
651 * @word2: the second 64 bits of the data
654 * 0 on success / other on failure
656 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
658 struct vio_dev *vdev = to_vio_dev(vhost->dev);
659 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
663 * ibmvfc_send_crq_init - Send a CRQ init message
664 * @vhost: ibmvfc host struct
667 * 0 on success / other on failure
669 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
671 ibmvfc_dbg(vhost, "Sending CRQ init\n");
672 return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
676 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
677 * @vhost: ibmvfc host struct
680 * 0 on success / other on failure
682 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
684 ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
685 return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
689 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
690 * @vhost: ibmvfc host struct
692 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
693 * the crq with the hypervisor.
695 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
698 struct vio_dev *vdev = to_vio_dev(vhost->dev);
699 struct ibmvfc_crq_queue *crq = &vhost->crq;
701 ibmvfc_dbg(vhost, "Releasing CRQ\n");
702 free_irq(vdev->irq, vhost);
703 tasklet_kill(&vhost->tasklet);
707 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
708 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
710 vhost->state = IBMVFC_NO_CRQ;
711 vhost->logged_in = 0;
712 dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
713 free_page((unsigned long)crq->msgs);
717 * ibmvfc_reenable_crq_queue - reenables the CRQ
718 * @vhost: ibmvfc host struct
721 * 0 on success / other on failure
723 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
726 struct vio_dev *vdev = to_vio_dev(vhost->dev);
728 /* Re-enable the CRQ */
732 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
733 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
736 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
742 * ibmvfc_reset_crq - resets a crq after a failure
743 * @vhost: ibmvfc host struct
746 * 0 on success / other on failure
748 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
752 struct vio_dev *vdev = to_vio_dev(vhost->dev);
753 struct ibmvfc_crq_queue *crq = &vhost->crq;
759 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
760 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
762 spin_lock_irqsave(vhost->host->host_lock, flags);
763 vhost->state = IBMVFC_NO_CRQ;
764 vhost->logged_in = 0;
766 /* Clean out the queue */
767 memset(crq->msgs, 0, PAGE_SIZE);
770 /* And re-open it again */
771 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
772 crq->msg_token, PAGE_SIZE);
775 /* Adapter is good, but other end is not ready */
776 dev_warn(vhost->dev, "Partner adapter not ready\n");
778 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
779 spin_unlock_irqrestore(vhost->host->host_lock, flags);
785 * ibmvfc_valid_event - Determines if event is valid.
786 * @pool: event_pool that contains the event
787 * @evt: ibmvfc event to be checked for validity
790 * 1 if event is valid / 0 if event is not valid
792 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
793 struct ibmvfc_event *evt)
795 int index = evt - pool->events;
796 if (index < 0 || index >= pool->size) /* outside of bounds */
798 if (evt != pool->events + index) /* unaligned */
804 * ibmvfc_free_event - Free the specified event
805 * @evt: ibmvfc_event to be freed
808 static void ibmvfc_free_event(struct ibmvfc_event *evt)
810 struct ibmvfc_host *vhost = evt->vhost;
811 struct ibmvfc_event_pool *pool = &vhost->pool;
813 BUG_ON(!ibmvfc_valid_event(pool, evt));
814 BUG_ON(atomic_inc_return(&evt->free) != 1);
815 list_add_tail(&evt->queue, &vhost->free);
819 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
820 * @evt: ibmvfc event struct
822 * This function does not setup any error status, that must be done
823 * before this function gets called.
825 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
827 struct scsi_cmnd *cmnd = evt->cmnd;
830 scsi_dma_unmap(cmnd);
831 cmnd->scsi_done(cmnd);
835 complete(evt->eh_comp);
837 ibmvfc_free_event(evt);
841 * ibmvfc_fail_request - Fail request with specified error code
842 * @evt: ibmvfc event struct
843 * @error_code: error code to fail request with
848 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
851 evt->cmnd->result = (error_code << 16);
852 evt->done = ibmvfc_scsi_eh_done;
854 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
856 list_del(&evt->queue);
857 del_timer(&evt->timer);
863 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
864 * @vhost: ibmvfc host struct
865 * @error_code: error code to fail requests with
870 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
872 struct ibmvfc_event *evt, *pos;
874 ibmvfc_dbg(vhost, "Purging all requests\n");
875 list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
876 ibmvfc_fail_request(evt, error_code);
880 * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
881 * @vhost: struct ibmvfc host to reset
883 static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
885 ibmvfc_purge_requests(vhost, DID_ERROR);
886 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
887 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
891 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
892 * @vhost: struct ibmvfc host to reset
894 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
896 if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
897 !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
898 scsi_block_requests(vhost->host);
899 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
900 vhost->job_step = ibmvfc_npiv_logout;
901 wake_up(&vhost->work_wait_q);
903 ibmvfc_hard_reset_host(vhost);
907 * ibmvfc_reset_host - Reset the connection to the server
908 * @vhost: ibmvfc host struct
910 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
914 spin_lock_irqsave(vhost->host->host_lock, flags);
915 __ibmvfc_reset_host(vhost);
916 spin_unlock_irqrestore(vhost->host->host_lock, flags);
920 * ibmvfc_retry_host_init - Retry host initialization if allowed
921 * @vhost: ibmvfc host struct
923 * Returns: 1 if init will be retried / 0 if not
926 static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
930 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
931 vhost->delay_init = 1;
932 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
934 "Host initialization retries exceeded. Taking adapter offline\n");
935 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
936 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
937 __ibmvfc_reset_host(vhost);
939 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
944 wake_up(&vhost->work_wait_q);
949 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
950 * @starget: scsi target struct
953 * ibmvfc_target struct / NULL if not found
955 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
957 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
958 struct ibmvfc_host *vhost = shost_priv(shost);
959 struct ibmvfc_target *tgt;
961 list_for_each_entry(tgt, &vhost->targets, queue)
962 if (tgt->target_id == starget->id) {
963 kref_get(&tgt->kref);
970 * ibmvfc_get_target - Find the specified scsi_target
971 * @starget: scsi target struct
974 * ibmvfc_target struct / NULL if not found
976 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
978 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
979 struct ibmvfc_target *tgt;
982 spin_lock_irqsave(shost->host_lock, flags);
983 tgt = __ibmvfc_get_target(starget);
984 spin_unlock_irqrestore(shost->host_lock, flags);
989 * ibmvfc_get_host_speed - Get host port speed
990 * @shost: scsi host struct
995 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
997 struct ibmvfc_host *vhost = shost_priv(shost);
1000 spin_lock_irqsave(shost->host_lock, flags);
1001 if (vhost->state == IBMVFC_ACTIVE) {
1002 switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
1004 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1007 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1010 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1013 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
1016 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
1019 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
1022 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
1023 be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
1024 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1028 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1029 spin_unlock_irqrestore(shost->host_lock, flags);
1033 * ibmvfc_get_host_port_state - Get host port state
1034 * @shost: scsi host struct
1039 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
1041 struct ibmvfc_host *vhost = shost_priv(shost);
1042 unsigned long flags;
1044 spin_lock_irqsave(shost->host_lock, flags);
1045 switch (vhost->state) {
1046 case IBMVFC_INITIALIZING:
1048 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1050 case IBMVFC_LINK_DOWN:
1051 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1053 case IBMVFC_LINK_DEAD:
1054 case IBMVFC_HOST_OFFLINE:
1055 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1058 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1061 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1064 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
1065 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1068 spin_unlock_irqrestore(shost->host_lock, flags);
1072 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1073 * @rport: rport struct
1074 * @timeout: timeout value
1079 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1082 rport->dev_loss_tmo = timeout;
1084 rport->dev_loss_tmo = 1;
1088 * ibmvfc_release_tgt - Free memory allocated for a target
1089 * @kref: kref struct
1092 static void ibmvfc_release_tgt(struct kref *kref)
1094 struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1099 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1100 * @starget: scsi target struct
1105 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1107 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1108 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1110 kref_put(&tgt->kref, ibmvfc_release_tgt);
1114 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1115 * @starget: scsi target struct
1120 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1122 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1123 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1125 kref_put(&tgt->kref, ibmvfc_release_tgt);
1129 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1130 * @starget: scsi target struct
1135 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1137 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1138 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1140 kref_put(&tgt->kref, ibmvfc_release_tgt);
1144 * ibmvfc_wait_while_resetting - Wait while the host resets
1145 * @vhost: ibmvfc host struct
1148 * 0 on success / other on failure
1150 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1152 long timeout = wait_event_timeout(vhost->init_wait_q,
1153 ((vhost->state == IBMVFC_ACTIVE ||
1154 vhost->state == IBMVFC_HOST_OFFLINE ||
1155 vhost->state == IBMVFC_LINK_DEAD) &&
1156 vhost->action == IBMVFC_HOST_ACTION_NONE),
1157 (init_timeout * HZ));
1159 return timeout ? 0 : -EIO;
1163 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1164 * @shost: scsi host struct
1167 * 0 on success / other on failure
1169 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1171 struct ibmvfc_host *vhost = shost_priv(shost);
1173 dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1174 ibmvfc_reset_host(vhost);
1175 return ibmvfc_wait_while_resetting(vhost);
1179 * ibmvfc_gather_partition_info - Gather info about the LPAR
1184 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1186 struct device_node *rootdn;
1188 const unsigned int *num;
1190 rootdn = of_find_node_by_path("/");
1194 name = of_get_property(rootdn, "ibm,partition-name", NULL);
1196 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1197 num = of_get_property(rootdn, "ibm,partition-no", NULL);
1199 vhost->partition_number = *num;
1200 of_node_put(rootdn);
1204 * ibmvfc_set_login_info - Setup info for NPIV login
1205 * @vhost: ibmvfc host struct
1210 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1212 struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1213 struct device_node *of_node = vhost->dev->of_node;
1214 const char *location;
1216 memset(login_info, 0, sizeof(*login_info));
1218 login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
1219 login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
1220 login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
1221 login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
1222 login_info->partition_num = cpu_to_be32(vhost->partition_number);
1223 login_info->vfc_frame_version = cpu_to_be32(1);
1224 login_info->fcp_version = cpu_to_be16(3);
1225 login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
1226 if (vhost->client_migrated)
1227 login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
1229 login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
1230 login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE);
1231 login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
1232 login_info->async.len = cpu_to_be32(vhost->async_crq.size * sizeof(*vhost->async_crq.msgs));
1233 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1234 strncpy(login_info->device_name,
1235 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
1237 location = of_get_property(of_node, "ibm,loc-code", NULL);
1238 location = location ? location : dev_name(vhost->dev);
1239 strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1243 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1244 * @vhost: ibmvfc host who owns the event pool
1246 * Returns zero on success.
1248 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1251 struct ibmvfc_event_pool *pool = &vhost->pool;
1254 pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1255 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1259 pool->iu_storage = dma_alloc_coherent(vhost->dev,
1260 pool->size * sizeof(*pool->iu_storage),
1261 &pool->iu_token, 0);
1263 if (!pool->iu_storage) {
1264 kfree(pool->events);
1268 for (i = 0; i < pool->size; ++i) {
1269 struct ibmvfc_event *evt = &pool->events[i];
1270 atomic_set(&evt->free, 1);
1271 evt->crq.valid = 0x80;
1272 evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
1273 evt->xfer_iu = pool->iu_storage + i;
1275 evt->ext_list = NULL;
1276 list_add_tail(&evt->queue, &vhost->free);
1284 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1285 * @vhost: ibmvfc host who owns the event pool
1288 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1291 struct ibmvfc_event_pool *pool = &vhost->pool;
1294 for (i = 0; i < pool->size; ++i) {
1295 list_del(&pool->events[i].queue);
1296 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1297 if (pool->events[i].ext_list)
1298 dma_pool_free(vhost->sg_pool,
1299 pool->events[i].ext_list,
1300 pool->events[i].ext_list_token);
1303 kfree(pool->events);
1304 dma_free_coherent(vhost->dev,
1305 pool->size * sizeof(*pool->iu_storage),
1306 pool->iu_storage, pool->iu_token);
1311 * ibmvfc_get_event - Gets the next free event in pool
1312 * @vhost: ibmvfc host struct
1314 * Returns a free event from the pool.
1316 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1318 struct ibmvfc_event *evt;
1320 BUG_ON(list_empty(&vhost->free));
1321 evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1322 atomic_set(&evt->free, 0);
1323 list_del(&evt->queue);
1328 * ibmvfc_init_event - Initialize fields in an event struct that are always
1331 * @done: Routine to call when the event is responded to
1332 * @format: SRP or MAD format
1334 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1335 void (*done) (struct ibmvfc_event *), u8 format)
1338 evt->sync_iu = NULL;
1339 evt->crq.format = format;
1341 evt->eh_comp = NULL;
1345 * ibmvfc_map_sg_list - Initialize scatterlist
1346 * @scmd: scsi command struct
1347 * @nseg: number of scatterlist segments
1348 * @md: memory descriptor list to initialize
1350 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1351 struct srp_direct_buf *md)
1354 struct scatterlist *sg;
1356 scsi_for_each_sg(scmd, sg, nseg, i) {
1357 md[i].va = cpu_to_be64(sg_dma_address(sg));
1358 md[i].len = cpu_to_be32(sg_dma_len(sg));
1364 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
1365 * @scmd: struct scsi_cmnd with the scatterlist
1366 * @evt: ibmvfc event struct
1367 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1368 * @dev: device for which to map dma memory
1371 * 0 on success / non-zero on failure
1373 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1374 struct ibmvfc_event *evt,
1375 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1379 struct srp_direct_buf *data = &vfc_cmd->ioba;
1380 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1383 vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1385 sg_mapped = scsi_dma_map(scmd);
1387 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1389 } else if (unlikely(sg_mapped < 0)) {
1390 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1391 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1395 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1396 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1397 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1399 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1400 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1403 if (sg_mapped == 1) {
1404 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1408 vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1410 if (!evt->ext_list) {
1411 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1412 &evt->ext_list_token);
1414 if (!evt->ext_list) {
1415 scsi_dma_unmap(scmd);
1416 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1417 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1422 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1424 data->va = cpu_to_be64(evt->ext_list_token);
1425 data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1431 * ibmvfc_timeout - Internal command timeout handler
1432 * @evt: struct ibmvfc_event that timed out
1434 * Called when an internally generated command times out
1436 static void ibmvfc_timeout(struct timer_list *t)
1438 struct ibmvfc_event *evt = from_timer(evt, t, timer);
1439 struct ibmvfc_host *vhost = evt->vhost;
1440 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1441 ibmvfc_reset_host(vhost);
1445 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1446 * @evt: event to be sent
1447 * @vhost: ibmvfc host struct
1448 * @timeout: timeout in seconds - 0 means do not time command
1450 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1452 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1453 struct ibmvfc_host *vhost, unsigned long timeout)
1455 __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1458 /* Copy the IU into the transfer area */
1459 *evt->xfer_iu = evt->iu;
1460 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1461 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1462 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1463 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1467 list_add_tail(&evt->queue, &vhost->sent);
1468 timer_setup(&evt->timer, ibmvfc_timeout, 0);
1471 evt->timer.expires = jiffies + (timeout * HZ);
1472 add_timer(&evt->timer);
1477 if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1478 be64_to_cpu(crq_as_u64[1])))) {
1479 list_del(&evt->queue);
1480 del_timer(&evt->timer);
1482 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1483 * Firmware will send a CRQ with a transport event (0xFF) to
1484 * tell this client what has happened to the transport. This
1485 * will be handled in ibmvfc_handle_crq()
1487 if (rc == H_CLOSED) {
1488 if (printk_ratelimit())
1489 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1491 scsi_dma_unmap(evt->cmnd);
1492 ibmvfc_free_event(evt);
1493 return SCSI_MLQUEUE_HOST_BUSY;
1496 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1498 evt->cmnd->result = DID_ERROR << 16;
1499 evt->done = ibmvfc_scsi_eh_done;
1501 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1505 ibmvfc_trc_start(evt);
1511 * ibmvfc_log_error - Log an error for the failed command if appropriate
1512 * @evt: ibmvfc event to log
1515 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1517 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1518 struct ibmvfc_host *vhost = evt->vhost;
1519 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1520 struct scsi_cmnd *cmnd = evt->cmnd;
1521 const char *err = unknown_error;
1522 int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1527 logerr = cmd_status[index].log;
1528 err = cmd_status[index].name;
1531 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1534 if (rsp->flags & FCP_RSP_LEN_VALID)
1535 rsp_code = rsp->data.info.rsp_code;
1537 scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
1538 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1539 cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
1540 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1544 * ibmvfc_relogin - Log back into the specified device
1545 * @sdev: scsi device struct
1548 static void ibmvfc_relogin(struct scsi_device *sdev)
1550 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1551 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1552 struct ibmvfc_target *tgt;
1554 list_for_each_entry(tgt, &vhost->targets, queue) {
1555 if (rport == tgt->rport) {
1556 ibmvfc_del_tgt(tgt);
1561 ibmvfc_reinit_host(vhost);
1565 * ibmvfc_scsi_done - Handle responses from commands
1566 * @evt: ibmvfc event to be handled
1568 * Used as a callback when sending scsi cmds.
1570 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1572 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1573 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1574 struct scsi_cmnd *cmnd = evt->cmnd;
1576 u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1579 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1580 scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1581 else if (rsp->flags & FCP_RESID_UNDER)
1582 scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1584 scsi_set_resid(cmnd, 0);
1586 if (vfc_cmd->status) {
1587 cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1589 if (rsp->flags & FCP_RSP_LEN_VALID)
1590 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1591 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1592 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1593 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1594 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1595 if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1596 (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1597 ibmvfc_relogin(cmnd->device);
1599 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1600 cmnd->result = (DID_ERROR << 16);
1602 ibmvfc_log_error(evt);
1605 if (!cmnd->result &&
1606 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1607 cmnd->result = (DID_ERROR << 16);
1609 scsi_dma_unmap(cmnd);
1610 cmnd->scsi_done(cmnd);
1614 complete(evt->eh_comp);
1616 ibmvfc_free_event(evt);
1620 * ibmvfc_host_chkready - Check if the host can accept commands
1621 * @vhost: struct ibmvfc host
1624 * 1 if host can accept command / 0 if not
1626 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1630 switch (vhost->state) {
1631 case IBMVFC_LINK_DEAD:
1632 case IBMVFC_HOST_OFFLINE:
1633 result = DID_NO_CONNECT << 16;
1636 case IBMVFC_INITIALIZING:
1638 case IBMVFC_LINK_DOWN:
1639 result = DID_REQUEUE << 16;
1650 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1651 * @cmnd: struct scsi_cmnd to be executed
1652 * @done: Callback function to be called when cmnd is completed
1655 * 0 on success / other on failure
1657 static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
1658 void (*done) (struct scsi_cmnd *))
1660 struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1661 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1662 struct ibmvfc_cmd *vfc_cmd;
1663 struct ibmvfc_event *evt;
1666 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1667 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1673 cmnd->result = (DID_OK << 16);
1674 evt = ibmvfc_get_event(vhost);
1675 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1677 cmnd->scsi_done = done;
1678 vfc_cmd = &evt->iu.cmd;
1679 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1680 vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
1681 vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1682 vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1683 vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
1684 vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1685 vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
1686 vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1687 vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1688 int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1689 memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1691 if (cmnd->flags & SCMD_TAGGED) {
1692 vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
1693 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1696 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1697 return ibmvfc_send_event(evt, vhost, 0);
1699 ibmvfc_free_event(evt);
1701 return SCSI_MLQUEUE_HOST_BUSY;
1703 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1704 scmd_printk(KERN_ERR, cmnd,
1705 "Failed to map DMA buffer for command. rc=%d\n", rc);
1707 cmnd->result = DID_ERROR << 16;
1712 static DEF_SCSI_QCMD(ibmvfc_queuecommand)
1715 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1716 * @evt: ibmvfc event struct
1719 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1721 /* copy the response back */
1723 *evt->sync_iu = *evt->xfer_iu;
1725 complete(&evt->comp);
1729 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1730 * @evt: struct ibmvfc_event
1733 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
1735 struct ibmvfc_host *vhost = evt->vhost;
1737 ibmvfc_free_event(evt);
1738 vhost->aborting_passthru = 0;
1739 dev_info(vhost->dev, "Passthru command cancelled\n");
1743 * ibmvfc_bsg_timeout - Handle a BSG timeout
1744 * @job: struct bsg_job that timed out
1747 * 0 on success / other on failure
1749 static int ibmvfc_bsg_timeout(struct bsg_job *job)
1751 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1752 unsigned long port_id = (unsigned long)job->dd_data;
1753 struct ibmvfc_event *evt;
1754 struct ibmvfc_tmf *tmf;
1755 unsigned long flags;
1759 spin_lock_irqsave(vhost->host->host_lock, flags);
1760 if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
1761 __ibmvfc_reset_host(vhost);
1762 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1766 vhost->aborting_passthru = 1;
1767 evt = ibmvfc_get_event(vhost);
1768 ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
1771 memset(tmf, 0, sizeof(*tmf));
1772 tmf->common.version = cpu_to_be32(1);
1773 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
1774 tmf->common.length = cpu_to_be16(sizeof(*tmf));
1775 tmf->scsi_id = cpu_to_be64(port_id);
1776 tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1777 tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
1778 rc = ibmvfc_send_event(evt, vhost, default_timeout);
1781 vhost->aborting_passthru = 0;
1782 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
1785 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
1788 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1795 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
1796 * @vhost: struct ibmvfc_host to send command
1797 * @port_id: port ID to send command
1800 * 0 on success / other on failure
1802 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
1804 struct ibmvfc_port_login *plogi;
1805 struct ibmvfc_target *tgt;
1806 struct ibmvfc_event *evt;
1807 union ibmvfc_iu rsp_iu;
1808 unsigned long flags;
1809 int rc = 0, issue_login = 1;
1812 spin_lock_irqsave(vhost->host->host_lock, flags);
1813 list_for_each_entry(tgt, &vhost->targets, queue) {
1814 if (tgt->scsi_id == port_id) {
1822 if (unlikely((rc = ibmvfc_host_chkready(vhost))))
1825 evt = ibmvfc_get_event(vhost);
1826 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1827 plogi = &evt->iu.plogi;
1828 memset(plogi, 0, sizeof(*plogi));
1829 plogi->common.version = cpu_to_be32(1);
1830 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
1831 plogi->common.length = cpu_to_be16(sizeof(*plogi));
1832 plogi->scsi_id = cpu_to_be64(port_id);
1833 evt->sync_iu = &rsp_iu;
1834 init_completion(&evt->comp);
1836 rc = ibmvfc_send_event(evt, vhost, default_timeout);
1837 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1842 wait_for_completion(&evt->comp);
1844 if (rsp_iu.plogi.common.status)
1847 spin_lock_irqsave(vhost->host->host_lock, flags);
1848 ibmvfc_free_event(evt);
1850 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1856 * ibmvfc_bsg_request - Handle a BSG request
1857 * @job: struct bsg_job to be executed
1860 * 0 on success / other on failure
1862 static int ibmvfc_bsg_request(struct bsg_job *job)
1864 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1865 struct fc_rport *rport = fc_bsg_to_rport(job);
1866 struct ibmvfc_passthru_mad *mad;
1867 struct ibmvfc_event *evt;
1868 union ibmvfc_iu rsp_iu;
1869 unsigned long flags, port_id = -1;
1870 struct fc_bsg_request *bsg_request = job->request;
1871 struct fc_bsg_reply *bsg_reply = job->reply;
1872 unsigned int code = bsg_request->msgcode;
1873 int rc = 0, req_seg, rsp_seg, issue_login = 0;
1874 u32 fc_flags, rsp_len;
1877 bsg_reply->reply_payload_rcv_len = 0;
1879 port_id = rport->port_id;
1882 case FC_BSG_HST_ELS_NOLOGIN:
1883 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
1884 (bsg_request->rqst_data.h_els.port_id[1] << 8) |
1885 bsg_request->rqst_data.h_els.port_id[2];
1887 case FC_BSG_RPT_ELS:
1888 fc_flags = IBMVFC_FC_ELS;
1892 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
1893 (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
1894 bsg_request->rqst_data.h_ct.port_id[2];
1897 fc_flags = IBMVFC_FC_CT_IU;
1905 if (!mutex_trylock(&vhost->passthru_mutex))
1908 job->dd_data = (void *)port_id;
1909 req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
1910 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1913 mutex_unlock(&vhost->passthru_mutex);
1917 rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
1918 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1921 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1922 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1923 mutex_unlock(&vhost->passthru_mutex);
1927 if (req_seg > 1 || rsp_seg > 1) {
1933 rc = ibmvfc_bsg_plogi(vhost, port_id);
1935 spin_lock_irqsave(vhost->host->host_lock, flags);
1937 if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
1938 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1939 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1943 evt = ibmvfc_get_event(vhost);
1944 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1945 mad = &evt->iu.passthru;
1947 memset(mad, 0, sizeof(*mad));
1948 mad->common.version = cpu_to_be32(1);
1949 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
1950 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
1952 mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
1953 offsetof(struct ibmvfc_passthru_mad, iu));
1954 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
1956 mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
1957 mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
1958 mad->iu.flags = cpu_to_be32(fc_flags);
1959 mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1961 mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
1962 mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
1963 mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
1964 mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
1965 mad->iu.scsi_id = cpu_to_be64(port_id);
1966 mad->iu.tag = cpu_to_be64((u64)evt);
1967 rsp_len = be32_to_cpu(mad->iu.rsp.len);
1969 evt->sync_iu = &rsp_iu;
1970 init_completion(&evt->comp);
1971 rc = ibmvfc_send_event(evt, vhost, 0);
1972 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1979 wait_for_completion(&evt->comp);
1981 if (rsp_iu.passthru.common.status)
1984 bsg_reply->reply_payload_rcv_len = rsp_len;
1986 spin_lock_irqsave(vhost->host->host_lock, flags);
1987 ibmvfc_free_event(evt);
1988 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1989 bsg_reply->result = rc;
1990 bsg_job_done(job, bsg_reply->result,
1991 bsg_reply->reply_payload_rcv_len);
1994 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1995 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1996 dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
1997 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1998 mutex_unlock(&vhost->passthru_mutex);
2004 * ibmvfc_reset_device - Reset the device with the specified reset type
2005 * @sdev: scsi device to reset
2007 * @desc: reset type description for log messages
2010 * 0 on success / other on failure
2012 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
2014 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2015 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2016 struct ibmvfc_cmd *tmf;
2017 struct ibmvfc_event *evt = NULL;
2018 union ibmvfc_iu rsp_iu;
2019 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2020 int rsp_rc = -EBUSY;
2021 unsigned long flags;
2024 spin_lock_irqsave(vhost->host->host_lock, flags);
2025 if (vhost->state == IBMVFC_ACTIVE) {
2026 evt = ibmvfc_get_event(vhost);
2027 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2030 memset(tmf, 0, sizeof(*tmf));
2031 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
2032 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
2033 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2034 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
2035 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
2036 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2037 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
2038 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2039 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2040 tmf->iu.tmf_flags = type;
2041 evt->sync_iu = &rsp_iu;
2043 init_completion(&evt->comp);
2044 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2046 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2049 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2054 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2055 wait_for_completion(&evt->comp);
2057 if (rsp_iu.cmd.status)
2058 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2061 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2062 rsp_code = fc_rsp->data.info.rsp_code;
2064 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2065 "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2066 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2067 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2068 fc_rsp->scsi_status);
2071 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2073 spin_lock_irqsave(vhost->host->host_lock, flags);
2074 ibmvfc_free_event(evt);
2075 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2080 * ibmvfc_match_rport - Match function for specified remote port
2081 * @evt: ibmvfc event struct
2082 * @device: device to match (rport)
2085 * 1 if event matches rport / 0 if event does not match rport
2087 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2089 struct fc_rport *cmd_rport;
2092 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2093 if (cmd_rport == rport)
2100 * ibmvfc_match_target - Match function for specified target
2101 * @evt: ibmvfc event struct
2102 * @device: device to match (starget)
2105 * 1 if event matches starget / 0 if event does not match starget
2107 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2109 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2115 * ibmvfc_match_lun - Match function for specified LUN
2116 * @evt: ibmvfc event struct
2117 * @device: device to match (sdev)
2120 * 1 if event matches sdev / 0 if event does not match sdev
2122 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2124 if (evt->cmnd && evt->cmnd->device == device)
2130 * ibmvfc_wait_for_ops - Wait for ops to complete
2131 * @vhost: ibmvfc host struct
2132 * @device: device to match (starget or sdev)
2133 * @match: match function
2138 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2139 int (*match) (struct ibmvfc_event *, void *))
2141 struct ibmvfc_event *evt;
2142 DECLARE_COMPLETION_ONSTACK(comp);
2144 unsigned long flags;
2145 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2150 spin_lock_irqsave(vhost->host->host_lock, flags);
2151 list_for_each_entry(evt, &vhost->sent, queue) {
2152 if (match(evt, device)) {
2153 evt->eh_comp = ∁
2157 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2160 timeout = wait_for_completion_timeout(&comp, timeout);
2164 spin_lock_irqsave(vhost->host->host_lock, flags);
2165 list_for_each_entry(evt, &vhost->sent, queue) {
2166 if (match(evt, device)) {
2167 evt->eh_comp = NULL;
2171 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2173 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2175 return wait ? FAILED : SUCCESS;
2185 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2186 * @sdev: scsi device to cancel commands
2187 * @type: type of error recovery being performed
2189 * This sends a cancel to the VIOS for the specified device. This does
2190 * NOT send any abort to the actual device. That must be done separately.
2193 * 0 on success / other on failure
2195 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2197 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2198 struct scsi_target *starget = scsi_target(sdev);
2199 struct fc_rport *rport = starget_to_rport(starget);
2200 struct ibmvfc_tmf *tmf;
2201 struct ibmvfc_event *evt, *found_evt;
2202 union ibmvfc_iu rsp;
2203 int rsp_rc = -EBUSY;
2204 unsigned long flags;
2208 spin_lock_irqsave(vhost->host->host_lock, flags);
2210 list_for_each_entry(evt, &vhost->sent, queue) {
2211 if (evt->cmnd && evt->cmnd->device == sdev) {
2218 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2219 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2220 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2224 if (vhost->logged_in) {
2225 evt = ibmvfc_get_event(vhost);
2226 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2229 memset(tmf, 0, sizeof(*tmf));
2230 tmf->common.version = cpu_to_be32(1);
2231 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2232 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2233 tmf->scsi_id = cpu_to_be64(rport->port_id);
2234 int_to_scsilun(sdev->lun, &tmf->lun);
2235 if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
2236 type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2237 if (vhost->state == IBMVFC_ACTIVE)
2238 tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2240 tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2241 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2242 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2244 evt->sync_iu = &rsp;
2245 init_completion(&evt->comp);
2246 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2249 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2252 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2253 /* If failure is received, the host adapter is most likely going
2254 through reset, return success so the caller will wait for the command
2255 being cancelled to get returned */
2259 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2261 wait_for_completion(&evt->comp);
2262 status = be16_to_cpu(rsp.mad_common.status);
2263 spin_lock_irqsave(vhost->host->host_lock, flags);
2264 ibmvfc_free_event(evt);
2265 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2267 if (status != IBMVFC_MAD_SUCCESS) {
2268 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2270 case IBMVFC_MAD_DRIVER_FAILED:
2271 case IBMVFC_MAD_CRQ_ERROR:
2272 /* Host adapter most likely going through reset, return success to
2273 the caller will wait for the command being cancelled to get returned */
2280 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2285 * ibmvfc_match_key - Match function for specified cancel key
2286 * @evt: ibmvfc event struct
2287 * @key: cancel key to match
2290 * 1 if event matches key / 0 if event does not match key
2292 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2294 unsigned long cancel_key = (unsigned long)key;
2296 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2297 be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2303 * ibmvfc_match_evt - Match function for specified event
2304 * @evt: ibmvfc event struct
2305 * @match: event to match
2308 * 1 if event matches key / 0 if event does not match key
2310 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2318 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2319 * @sdev: scsi device to abort commands
2321 * This sends an Abort Task Set to the VIOS for the specified device. This does
2322 * NOT send any cancel to the VIOS. That must be done separately.
2325 * 0 on success / other on failure
2327 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2329 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2330 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2331 struct ibmvfc_cmd *tmf;
2332 struct ibmvfc_event *evt, *found_evt;
2333 union ibmvfc_iu rsp_iu;
2334 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2335 int rc, rsp_rc = -EBUSY;
2336 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2339 spin_lock_irqsave(vhost->host->host_lock, flags);
2341 list_for_each_entry(evt, &vhost->sent, queue) {
2342 if (evt->cmnd && evt->cmnd->device == sdev) {
2349 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2350 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2351 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2355 if (vhost->state == IBMVFC_ACTIVE) {
2356 evt = ibmvfc_get_event(vhost);
2357 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2360 memset(tmf, 0, sizeof(*tmf));
2361 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
2362 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
2363 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2364 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
2365 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
2366 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2367 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
2368 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2369 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2370 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
2371 evt->sync_iu = &rsp_iu;
2373 init_completion(&evt->comp);
2374 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2377 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2380 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2384 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2385 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2388 rc = ibmvfc_cancel_all(sdev, 0);
2390 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2396 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2397 ibmvfc_reset_host(vhost);
2399 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2404 rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2405 if (rc != SUCCESS) {
2406 spin_lock_irqsave(vhost->host->host_lock, flags);
2407 ibmvfc_hard_reset_host(vhost);
2408 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2416 if (rsp_iu.cmd.status)
2417 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2420 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2421 rsp_code = fc_rsp->data.info.rsp_code;
2423 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2424 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2425 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2426 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2427 fc_rsp->scsi_status);
2430 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2433 spin_lock_irqsave(vhost->host->host_lock, flags);
2434 ibmvfc_free_event(evt);
2435 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2440 * ibmvfc_eh_abort_handler - Abort a command
2441 * @cmd: scsi command to abort
2444 * SUCCESS / FAST_IO_FAIL / FAILED
2446 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2448 struct scsi_device *sdev = cmd->device;
2449 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2450 int cancel_rc, block_rc;
2454 block_rc = fc_block_scsi_eh(cmd);
2455 ibmvfc_wait_while_resetting(vhost);
2456 if (block_rc != FAST_IO_FAIL) {
2457 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2458 ibmvfc_abort_task_set(sdev);
2460 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2463 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2465 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2473 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2474 * @cmd: scsi command struct
2477 * SUCCESS / FAST_IO_FAIL / FAILED
2479 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2481 struct scsi_device *sdev = cmd->device;
2482 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2483 int cancel_rc, block_rc, reset_rc = 0;
2487 block_rc = fc_block_scsi_eh(cmd);
2488 ibmvfc_wait_while_resetting(vhost);
2489 if (block_rc != FAST_IO_FAIL) {
2490 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2491 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2493 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2495 if (!cancel_rc && !reset_rc)
2496 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2498 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2506 * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2507 * @sdev: scsi device struct
2508 * @data: return code
2511 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2513 unsigned long *rc = data;
2514 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2518 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2519 * @sdev: scsi device struct
2520 * @data: return code
2523 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2525 unsigned long *rc = data;
2526 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2530 * ibmvfc_eh_target_reset_handler - Reset the target
2531 * @cmd: scsi command struct
2534 * SUCCESS / FAST_IO_FAIL / FAILED
2536 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2538 struct scsi_device *sdev = cmd->device;
2539 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2540 struct scsi_target *starget = scsi_target(sdev);
2544 unsigned long cancel_rc = 0;
2547 block_rc = fc_block_scsi_eh(cmd);
2548 ibmvfc_wait_while_resetting(vhost);
2549 if (block_rc != FAST_IO_FAIL) {
2550 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2551 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2553 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2555 if (!cancel_rc && !reset_rc)
2556 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2558 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2566 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2567 * @cmd: struct scsi_cmnd having problems
2570 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2573 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2575 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2576 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2578 return rc ? FAILED : SUCCESS;
2582 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2583 * @rport: rport struct
2588 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2590 struct Scsi_Host *shost = rport_to_shost(rport);
2591 struct ibmvfc_host *vhost = shost_priv(shost);
2592 struct fc_rport *dev_rport;
2593 struct scsi_device *sdev;
2594 struct ibmvfc_target *tgt;
2595 unsigned long rc, flags;
2599 shost_for_each_device(sdev, shost) {
2600 dev_rport = starget_to_rport(scsi_target(sdev));
2601 if (dev_rport != rport)
2603 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2606 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
2609 ibmvfc_issue_fc_host_lip(shost);
2611 spin_lock_irqsave(shost->host_lock, flags);
2613 list_for_each_entry(tgt, &vhost->targets, queue) {
2614 if (tgt->scsi_id == rport->port_id) {
2620 if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
2622 * If we get here, that means we previously attempted to send
2623 * an implicit logout to the target but it failed, most likely
2624 * due to I/O being pending, so we need to send it again
2626 ibmvfc_del_tgt(tgt);
2627 ibmvfc_reinit_host(vhost);
2630 spin_unlock_irqrestore(shost->host_lock, flags);
2634 static const struct ibmvfc_async_desc ae_desc [] = {
2635 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2636 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2637 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2638 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2639 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2640 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
2641 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
2642 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
2643 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
2644 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
2645 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
2646 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
2647 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
2650 static const struct ibmvfc_async_desc unknown_ae = {
2651 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
2655 * ibmvfc_get_ae_desc - Get text description for async event
2659 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
2663 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2664 if (ae_desc[i].ae == ae)
2670 static const struct {
2671 enum ibmvfc_ae_link_state state;
2674 { IBMVFC_AE_LS_LINK_UP, " link up" },
2675 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
2676 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
2677 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
2681 * ibmvfc_get_link_state - Get text description for link state
2682 * @state: link state
2685 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
2689 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
2690 if (link_desc[i].state == state)
2691 return link_desc[i].desc;
2697 * ibmvfc_handle_async - Handle an async event from the adapter
2698 * @crq: crq to process
2699 * @vhost: ibmvfc host struct
2702 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2703 struct ibmvfc_host *vhost)
2705 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
2706 struct ibmvfc_target *tgt;
2708 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
2709 " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
2710 be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
2711 ibmvfc_get_link_state(crq->link_state));
2713 switch (be64_to_cpu(crq->event)) {
2714 case IBMVFC_AE_RESUME:
2715 switch (crq->link_state) {
2716 case IBMVFC_AE_LS_LINK_DOWN:
2717 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2719 case IBMVFC_AE_LS_LINK_DEAD:
2720 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2722 case IBMVFC_AE_LS_LINK_UP:
2723 case IBMVFC_AE_LS_LINK_BOUNCED:
2725 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2726 vhost->delay_init = 1;
2727 __ibmvfc_reset_host(vhost);
2732 case IBMVFC_AE_LINK_UP:
2733 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2734 vhost->delay_init = 1;
2735 __ibmvfc_reset_host(vhost);
2737 case IBMVFC_AE_SCN_FABRIC:
2738 case IBMVFC_AE_SCN_DOMAIN:
2739 vhost->events_to_log |= IBMVFC_AE_RSCN;
2740 if (vhost->state < IBMVFC_HALTED) {
2741 vhost->delay_init = 1;
2742 __ibmvfc_reset_host(vhost);
2745 case IBMVFC_AE_SCN_NPORT:
2746 case IBMVFC_AE_SCN_GROUP:
2747 vhost->events_to_log |= IBMVFC_AE_RSCN;
2748 ibmvfc_reinit_host(vhost);
2750 case IBMVFC_AE_ELS_LOGO:
2751 case IBMVFC_AE_ELS_PRLO:
2752 case IBMVFC_AE_ELS_PLOGI:
2753 list_for_each_entry(tgt, &vhost->targets, queue) {
2754 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
2756 if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
2758 if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
2760 if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
2762 if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
2764 if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
2765 ibmvfc_del_tgt(tgt);
2766 ibmvfc_reinit_host(vhost);
2770 case IBMVFC_AE_LINK_DOWN:
2771 case IBMVFC_AE_ADAPTER_FAILED:
2772 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2774 case IBMVFC_AE_LINK_DEAD:
2775 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2777 case IBMVFC_AE_HALT:
2778 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2781 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
2787 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2788 * @crq: Command/Response queue
2789 * @vhost: ibmvfc host struct
2792 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2795 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
2797 switch (crq->valid) {
2798 case IBMVFC_CRQ_INIT_RSP:
2799 switch (crq->format) {
2800 case IBMVFC_CRQ_INIT:
2801 dev_info(vhost->dev, "Partner initialized\n");
2802 /* Send back a response */
2803 rc = ibmvfc_send_crq_init_complete(vhost);
2805 ibmvfc_init_host(vhost);
2807 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2809 case IBMVFC_CRQ_INIT_COMPLETE:
2810 dev_info(vhost->dev, "Partner initialization complete\n");
2811 ibmvfc_init_host(vhost);
2814 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2817 case IBMVFC_CRQ_XPORT_EVENT:
2818 vhost->state = IBMVFC_NO_CRQ;
2819 vhost->logged_in = 0;
2820 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2821 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2822 /* We need to re-setup the interpartition connection */
2823 dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
2824 vhost->client_migrated = 1;
2825 ibmvfc_purge_requests(vhost, DID_REQUEUE);
2826 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2827 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
2828 } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
2829 dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
2830 ibmvfc_purge_requests(vhost, DID_ERROR);
2831 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2832 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
2834 dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
2837 case IBMVFC_CRQ_CMD_RSP:
2840 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2844 if (crq->format == IBMVFC_ASYNC_EVENT)
2847 /* The only kind of payload CRQs we should get are responses to
2848 * things we send. Make sure this response is to something we
2851 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2852 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
2857 if (unlikely(atomic_read(&evt->free))) {
2858 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
2863 del_timer(&evt->timer);
2864 list_del(&evt->queue);
2865 ibmvfc_trc_end(evt);
2870 * ibmvfc_scan_finished - Check if the device scan is done.
2871 * @shost: scsi host struct
2872 * @time: current elapsed time
2875 * 0 if scan is not done / 1 if scan is done
2877 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2879 unsigned long flags;
2880 struct ibmvfc_host *vhost = shost_priv(shost);
2883 spin_lock_irqsave(shost->host_lock, flags);
2884 if (time >= (init_timeout * HZ)) {
2885 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2886 "continuing initialization\n", init_timeout);
2890 if (vhost->scan_complete)
2892 spin_unlock_irqrestore(shost->host_lock, flags);
2897 * ibmvfc_slave_alloc - Setup the device's task set value
2898 * @sdev: struct scsi_device device to configure
2900 * Set the device's task set value so that error handling works as
2904 * 0 on success / -ENXIO if device does not exist
2906 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2908 struct Scsi_Host *shost = sdev->host;
2909 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2910 struct ibmvfc_host *vhost = shost_priv(shost);
2911 unsigned long flags = 0;
2913 if (!rport || fc_remote_port_chkready(rport))
2916 spin_lock_irqsave(shost->host_lock, flags);
2917 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2918 spin_unlock_irqrestore(shost->host_lock, flags);
2923 * ibmvfc_target_alloc - Setup the target's task set value
2924 * @starget: struct scsi_target
2926 * Set the target's task set value so that error handling works as
2930 * 0 on success / -ENXIO if device does not exist
2932 static int ibmvfc_target_alloc(struct scsi_target *starget)
2934 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2935 struct ibmvfc_host *vhost = shost_priv(shost);
2936 unsigned long flags = 0;
2938 spin_lock_irqsave(shost->host_lock, flags);
2939 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2940 spin_unlock_irqrestore(shost->host_lock, flags);
2945 * ibmvfc_slave_configure - Configure the device
2946 * @sdev: struct scsi_device device to configure
2948 * Enable allow_restart for a device if it is a disk. Adjust the
2949 * queue_depth here also.
2954 static int ibmvfc_slave_configure(struct scsi_device *sdev)
2956 struct Scsi_Host *shost = sdev->host;
2957 unsigned long flags = 0;
2959 spin_lock_irqsave(shost->host_lock, flags);
2960 if (sdev->type == TYPE_DISK) {
2961 sdev->allow_restart = 1;
2962 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
2964 spin_unlock_irqrestore(shost->host_lock, flags);
2969 * ibmvfc_change_queue_depth - Change the device's queue depth
2970 * @sdev: scsi device struct
2971 * @qdepth: depth to set
2972 * @reason: calling context
2977 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2979 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2980 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2982 return scsi_change_queue_depth(sdev, qdepth);
2985 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2986 struct device_attribute *attr, char *buf)
2988 struct Scsi_Host *shost = class_to_shost(dev);
2989 struct ibmvfc_host *vhost = shost_priv(shost);
2991 return snprintf(buf, PAGE_SIZE, "%s\n",
2992 vhost->login_buf->resp.partition_name);
2995 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2996 struct device_attribute *attr, char *buf)
2998 struct Scsi_Host *shost = class_to_shost(dev);
2999 struct ibmvfc_host *vhost = shost_priv(shost);
3001 return snprintf(buf, PAGE_SIZE, "%s\n",
3002 vhost->login_buf->resp.device_name);
3005 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3006 struct device_attribute *attr, char *buf)
3008 struct Scsi_Host *shost = class_to_shost(dev);
3009 struct ibmvfc_host *vhost = shost_priv(shost);
3011 return snprintf(buf, PAGE_SIZE, "%s\n",
3012 vhost->login_buf->resp.port_loc_code);
3015 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3016 struct device_attribute *attr, char *buf)
3018 struct Scsi_Host *shost = class_to_shost(dev);
3019 struct ibmvfc_host *vhost = shost_priv(shost);
3021 return snprintf(buf, PAGE_SIZE, "%s\n",
3022 vhost->login_buf->resp.drc_name);
3025 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3026 struct device_attribute *attr, char *buf)
3028 struct Scsi_Host *shost = class_to_shost(dev);
3029 struct ibmvfc_host *vhost = shost_priv(shost);
3030 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
3033 static ssize_t ibmvfc_show_host_capabilities(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, "%llx\n", vhost->login_buf->resp.capabilities);
3042 * ibmvfc_show_log_level - Show the adapter's error logging level
3043 * @dev: class device struct
3047 * number of bytes printed to buffer
3049 static ssize_t ibmvfc_show_log_level(struct device *dev,
3050 struct device_attribute *attr, char *buf)
3052 struct Scsi_Host *shost = class_to_shost(dev);
3053 struct ibmvfc_host *vhost = shost_priv(shost);
3054 unsigned long flags = 0;
3057 spin_lock_irqsave(shost->host_lock, flags);
3058 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
3059 spin_unlock_irqrestore(shost->host_lock, flags);
3064 * ibmvfc_store_log_level - Change the adapter's error logging level
3065 * @dev: class device struct
3069 * number of bytes printed to buffer
3071 static ssize_t ibmvfc_store_log_level(struct device *dev,
3072 struct device_attribute *attr,
3073 const char *buf, size_t count)
3075 struct Scsi_Host *shost = class_to_shost(dev);
3076 struct ibmvfc_host *vhost = shost_priv(shost);
3077 unsigned long flags = 0;
3079 spin_lock_irqsave(shost->host_lock, flags);
3080 vhost->log_level = simple_strtoul(buf, NULL, 10);
3081 spin_unlock_irqrestore(shost->host_lock, flags);
3085 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3086 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3087 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3088 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3089 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3090 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3091 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3092 ibmvfc_show_log_level, ibmvfc_store_log_level);
3094 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3096 * ibmvfc_read_trace - Dump the adapter trace
3097 * @filp: open sysfs file
3098 * @kobj: kobject struct
3099 * @bin_attr: bin_attribute struct
3102 * @count: buffer size
3105 * number of bytes printed to buffer
3107 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3108 struct bin_attribute *bin_attr,
3109 char *buf, loff_t off, size_t count)
3111 struct device *dev = container_of(kobj, struct device, kobj);
3112 struct Scsi_Host *shost = class_to_shost(dev);
3113 struct ibmvfc_host *vhost = shost_priv(shost);
3114 unsigned long flags = 0;
3115 int size = IBMVFC_TRACE_SIZE;
3116 char *src = (char *)vhost->trace;
3120 if (off + count > size) {
3125 spin_lock_irqsave(shost->host_lock, flags);
3126 memcpy(buf, &src[off], count);
3127 spin_unlock_irqrestore(shost->host_lock, flags);
3131 static struct bin_attribute ibmvfc_trace_attr = {
3137 .read = ibmvfc_read_trace,
3141 static struct device_attribute *ibmvfc_attrs[] = {
3142 &dev_attr_partition_name,
3143 &dev_attr_device_name,
3144 &dev_attr_port_loc_code,
3146 &dev_attr_npiv_version,
3147 &dev_attr_capabilities,
3148 &dev_attr_log_level,
3152 static struct scsi_host_template driver_template = {
3153 .module = THIS_MODULE,
3154 .name = "IBM POWER Virtual FC Adapter",
3155 .proc_name = IBMVFC_NAME,
3156 .queuecommand = ibmvfc_queuecommand,
3157 .eh_timed_out = fc_eh_timed_out,
3158 .eh_abort_handler = ibmvfc_eh_abort_handler,
3159 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3160 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3161 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3162 .slave_alloc = ibmvfc_slave_alloc,
3163 .slave_configure = ibmvfc_slave_configure,
3164 .target_alloc = ibmvfc_target_alloc,
3165 .scan_finished = ibmvfc_scan_finished,
3166 .change_queue_depth = ibmvfc_change_queue_depth,
3168 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3170 .sg_tablesize = SG_ALL,
3171 .max_sectors = IBMVFC_MAX_SECTORS,
3172 .shost_attrs = ibmvfc_attrs,
3173 .track_queue_depth = 1,
3177 * ibmvfc_next_async_crq - Returns the next entry in async queue
3178 * @vhost: ibmvfc host struct
3181 * Pointer to next entry in queue / NULL if empty
3183 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3185 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3186 struct ibmvfc_async_crq *crq;
3188 crq = &async_crq->msgs[async_crq->cur];
3189 if (crq->valid & 0x80) {
3190 if (++async_crq->cur == async_crq->size)
3200 * ibmvfc_next_crq - Returns the next entry in message queue
3201 * @vhost: ibmvfc host struct
3204 * Pointer to next entry in queue / NULL if empty
3206 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3208 struct ibmvfc_crq_queue *queue = &vhost->crq;
3209 struct ibmvfc_crq *crq;
3211 crq = &queue->msgs[queue->cur];
3212 if (crq->valid & 0x80) {
3213 if (++queue->cur == queue->size)
3223 * ibmvfc_interrupt - Interrupt handler
3224 * @irq: number of irq to handle, not used
3225 * @dev_instance: ibmvfc_host that received interrupt
3230 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3232 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3233 unsigned long flags;
3235 spin_lock_irqsave(vhost->host->host_lock, flags);
3236 vio_disable_interrupts(to_vio_dev(vhost->dev));
3237 tasklet_schedule(&vhost->tasklet);
3238 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3243 * ibmvfc_tasklet - Interrupt handler tasklet
3244 * @data: ibmvfc host struct
3249 static void ibmvfc_tasklet(void *data)
3251 struct ibmvfc_host *vhost = data;
3252 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3253 struct ibmvfc_crq *crq;
3254 struct ibmvfc_async_crq *async;
3255 unsigned long flags;
3258 spin_lock_irqsave(vhost->host->host_lock, flags);
3260 /* Pull all the valid messages off the async CRQ */
3261 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3262 ibmvfc_handle_async(async, vhost);
3267 /* Pull all the valid messages off the CRQ */
3268 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3269 ibmvfc_handle_crq(crq, vhost);
3274 vio_enable_interrupts(vdev);
3275 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3276 vio_disable_interrupts(vdev);
3277 ibmvfc_handle_async(async, vhost);
3280 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3281 vio_disable_interrupts(vdev);
3282 ibmvfc_handle_crq(crq, vhost);
3289 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3293 * ibmvfc_init_tgt - Set the next init job step for the target
3294 * @tgt: ibmvfc target struct
3295 * @job_step: job step to perform
3298 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3299 void (*job_step) (struct ibmvfc_target *))
3301 if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3302 tgt->job_step = job_step;
3303 wake_up(&tgt->vhost->work_wait_q);
3307 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3308 * @tgt: ibmvfc target struct
3309 * @job_step: initialization job step
3311 * Returns: 1 if step will be retried / 0 if not
3314 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3315 void (*job_step) (struct ibmvfc_target *))
3317 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3318 ibmvfc_del_tgt(tgt);
3319 wake_up(&tgt->vhost->work_wait_q);
3322 ibmvfc_init_tgt(tgt, job_step);
3326 /* Defined in FC-LS */
3327 static const struct {
3344 * ibmvfc_get_prli_rsp - Find PRLI response index
3345 * @flags: PRLI response flags
3348 static int ibmvfc_get_prli_rsp(u16 flags)
3351 int code = (flags & 0x0f00) >> 8;
3353 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3354 if (prli_rsp[i].code == code)
3361 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3362 * @evt: ibmvfc event struct
3365 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3367 struct ibmvfc_target *tgt = evt->tgt;
3368 struct ibmvfc_host *vhost = evt->vhost;
3369 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3370 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3371 u32 status = be16_to_cpu(rsp->common.status);
3372 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3374 vhost->discovery_threads--;
3375 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3377 case IBMVFC_MAD_SUCCESS:
3378 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3379 parms->type, parms->flags, parms->service_parms);
3381 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3382 index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3383 if (prli_rsp[index].logged_in) {
3384 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3385 tgt->need_login = 0;
3387 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3388 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3389 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3390 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3393 ibmvfc_del_tgt(tgt);
3394 } else if (prli_rsp[index].retry)
3395 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3397 ibmvfc_del_tgt(tgt);
3399 ibmvfc_del_tgt(tgt);
3401 case IBMVFC_MAD_DRIVER_FAILED:
3403 case IBMVFC_MAD_CRQ_ERROR:
3404 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3406 case IBMVFC_MAD_FAILED:
3408 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3409 be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3410 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3411 else if (tgt->logo_rcvd)
3412 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3413 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3414 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3416 ibmvfc_del_tgt(tgt);
3418 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3419 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3420 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
3424 kref_put(&tgt->kref, ibmvfc_release_tgt);
3425 ibmvfc_free_event(evt);
3426 wake_up(&vhost->work_wait_q);
3430 * ibmvfc_tgt_send_prli - Send a process login
3431 * @tgt: ibmvfc target struct
3434 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3436 struct ibmvfc_process_login *prli;
3437 struct ibmvfc_host *vhost = tgt->vhost;
3438 struct ibmvfc_event *evt;
3440 if (vhost->discovery_threads >= disc_threads)
3443 kref_get(&tgt->kref);
3444 evt = ibmvfc_get_event(vhost);
3445 vhost->discovery_threads++;
3446 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3448 prli = &evt->iu.prli;
3449 memset(prli, 0, sizeof(*prli));
3450 prli->common.version = cpu_to_be32(1);
3451 prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
3452 prli->common.length = cpu_to_be16(sizeof(*prli));
3453 prli->scsi_id = cpu_to_be64(tgt->scsi_id);
3455 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3456 prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
3457 prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
3458 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
3461 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
3463 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3464 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3465 vhost->discovery_threads--;
3466 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3467 kref_put(&tgt->kref, ibmvfc_release_tgt);
3469 tgt_dbg(tgt, "Sent process login\n");
3473 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3474 * @evt: ibmvfc event struct
3477 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3479 struct ibmvfc_target *tgt = evt->tgt;
3480 struct ibmvfc_host *vhost = evt->vhost;
3481 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3482 u32 status = be16_to_cpu(rsp->common.status);
3483 int level = IBMVFC_DEFAULT_LOG_LEVEL;
3485 vhost->discovery_threads--;
3486 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3488 case IBMVFC_MAD_SUCCESS:
3489 tgt_dbg(tgt, "Port Login succeeded\n");
3490 if (tgt->ids.port_name &&
3491 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3493 tgt_dbg(tgt, "Port re-init required\n");
3496 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3497 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3498 tgt->ids.port_id = tgt->scsi_id;
3499 memcpy(&tgt->service_parms, &rsp->service_parms,
3500 sizeof(tgt->service_parms));
3501 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3502 sizeof(tgt->service_parms_change));
3503 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3505 case IBMVFC_MAD_DRIVER_FAILED:
3507 case IBMVFC_MAD_CRQ_ERROR:
3508 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3510 case IBMVFC_MAD_FAILED:
3512 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3513 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3515 ibmvfc_del_tgt(tgt);
3517 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3518 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3519 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
3520 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
3521 ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
3525 kref_put(&tgt->kref, ibmvfc_release_tgt);
3526 ibmvfc_free_event(evt);
3527 wake_up(&vhost->work_wait_q);
3531 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3532 * @tgt: ibmvfc target struct
3535 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3537 struct ibmvfc_port_login *plogi;
3538 struct ibmvfc_host *vhost = tgt->vhost;
3539 struct ibmvfc_event *evt;
3541 if (vhost->discovery_threads >= disc_threads)
3544 kref_get(&tgt->kref);
3546 evt = ibmvfc_get_event(vhost);
3547 vhost->discovery_threads++;
3548 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3549 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3551 plogi = &evt->iu.plogi;
3552 memset(plogi, 0, sizeof(*plogi));
3553 plogi->common.version = cpu_to_be32(1);
3554 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
3555 plogi->common.length = cpu_to_be16(sizeof(*plogi));
3556 plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
3558 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3559 vhost->discovery_threads--;
3560 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3561 kref_put(&tgt->kref, ibmvfc_release_tgt);
3563 tgt_dbg(tgt, "Sent port login\n");
3567 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3568 * @evt: ibmvfc event struct
3571 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3573 struct ibmvfc_target *tgt = evt->tgt;
3574 struct ibmvfc_host *vhost = evt->vhost;
3575 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
3576 u32 status = be16_to_cpu(rsp->common.status);
3578 vhost->discovery_threads--;
3579 ibmvfc_free_event(evt);
3580 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3583 case IBMVFC_MAD_SUCCESS:
3584 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3586 case IBMVFC_MAD_DRIVER_FAILED:
3587 kref_put(&tgt->kref, ibmvfc_release_tgt);
3588 wake_up(&vhost->work_wait_q);
3590 case IBMVFC_MAD_FAILED:
3592 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3596 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3597 kref_put(&tgt->kref, ibmvfc_release_tgt);
3598 wake_up(&vhost->work_wait_q);
3602 * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
3603 * @tgt: ibmvfc target struct
3606 * Allocated and initialized ibmvfc_event struct
3608 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
3609 void (*done) (struct ibmvfc_event *))
3611 struct ibmvfc_implicit_logout *mad;
3612 struct ibmvfc_host *vhost = tgt->vhost;
3613 struct ibmvfc_event *evt;
3615 kref_get(&tgt->kref);
3616 evt = ibmvfc_get_event(vhost);
3617 ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
3619 mad = &evt->iu.implicit_logout;
3620 memset(mad, 0, sizeof(*mad));
3621 mad->common.version = cpu_to_be32(1);
3622 mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
3623 mad->common.length = cpu_to_be16(sizeof(*mad));
3624 mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
3629 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3630 * @tgt: ibmvfc target struct
3633 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3635 struct ibmvfc_host *vhost = tgt->vhost;
3636 struct ibmvfc_event *evt;
3638 if (vhost->discovery_threads >= disc_threads)
3641 vhost->discovery_threads++;
3642 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
3643 ibmvfc_tgt_implicit_logout_done);
3645 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3646 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3647 vhost->discovery_threads--;
3648 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3649 kref_put(&tgt->kref, ibmvfc_release_tgt);
3651 tgt_dbg(tgt, "Sent Implicit Logout\n");
3655 * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
3656 * @evt: ibmvfc event struct
3659 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
3661 struct ibmvfc_target *tgt = evt->tgt;
3662 struct ibmvfc_host *vhost = evt->vhost;
3663 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3664 u32 status = be16_to_cpu(mad->common.status);
3666 vhost->discovery_threads--;
3667 ibmvfc_free_event(evt);
3670 * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
3671 * driver in which case we need to free up all the targets. If we are
3672 * not unloading, we will still go through a hard reset to get out of
3673 * offline state, so there is no need to track the old targets in that
3676 if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
3677 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3679 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
3681 tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
3682 kref_put(&tgt->kref, ibmvfc_release_tgt);
3683 wake_up(&vhost->work_wait_q);
3687 * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
3688 * @tgt: ibmvfc target struct
3691 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
3693 struct ibmvfc_host *vhost = tgt->vhost;
3694 struct ibmvfc_event *evt;
3696 if (!vhost->logged_in) {
3697 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3701 if (vhost->discovery_threads >= disc_threads)
3704 vhost->discovery_threads++;
3705 evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
3706 ibmvfc_tgt_implicit_logout_and_del_done);
3708 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
3709 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3710 vhost->discovery_threads--;
3711 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3712 kref_put(&tgt->kref, ibmvfc_release_tgt);
3714 tgt_dbg(tgt, "Sent Implicit Logout\n");
3718 * ibmvfc_tgt_move_login_done - Completion handler for Move Login
3719 * @evt: ibmvfc event struct
3722 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
3724 struct ibmvfc_target *tgt = evt->tgt;
3725 struct ibmvfc_host *vhost = evt->vhost;
3726 struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
3727 u32 status = be16_to_cpu(rsp->common.status);
3728 int level = IBMVFC_DEFAULT_LOG_LEVEL;
3730 vhost->discovery_threads--;
3731 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3733 case IBMVFC_MAD_SUCCESS:
3734 tgt_dbg(tgt, "Move Login succeeded for old scsi_id: %llX\n", tgt->old_scsi_id);
3735 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3736 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3737 tgt->ids.port_id = tgt->scsi_id;
3738 memcpy(&tgt->service_parms, &rsp->service_parms,
3739 sizeof(tgt->service_parms));
3740 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3741 sizeof(tgt->service_parms_change));
3742 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3744 case IBMVFC_MAD_DRIVER_FAILED:
3746 case IBMVFC_MAD_CRQ_ERROR:
3747 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
3749 case IBMVFC_MAD_FAILED:
3751 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
3754 "Move Login failed: old scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
3755 tgt->old_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
3760 kref_put(&tgt->kref, ibmvfc_release_tgt);
3761 ibmvfc_free_event(evt);
3762 wake_up(&vhost->work_wait_q);
3767 * ibmvfc_tgt_move_login - Initiate a move login for specified target
3768 * @tgt: ibmvfc target struct
3771 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
3773 struct ibmvfc_host *vhost = tgt->vhost;
3774 struct ibmvfc_move_login *move;
3775 struct ibmvfc_event *evt;
3777 if (vhost->discovery_threads >= disc_threads)
3780 kref_get(&tgt->kref);
3781 evt = ibmvfc_get_event(vhost);
3782 vhost->discovery_threads++;
3783 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3784 ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
3786 move = &evt->iu.move_login;
3787 memset(move, 0, sizeof(*move));
3788 move->common.version = cpu_to_be32(1);
3789 move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
3790 move->common.length = cpu_to_be16(sizeof(*move));
3792 move->old_scsi_id = cpu_to_be64(tgt->old_scsi_id);
3793 move->new_scsi_id = cpu_to_be64(tgt->scsi_id);
3794 move->wwpn = cpu_to_be64(tgt->wwpn);
3795 move->node_name = cpu_to_be64(tgt->ids.node_name);
3797 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3798 vhost->discovery_threads--;
3799 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3800 kref_put(&tgt->kref, ibmvfc_release_tgt);
3802 tgt_dbg(tgt, "Sent Move Login for old scsi_id: %llX\n", tgt->old_scsi_id);
3806 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3807 * @mad: ibmvfc passthru mad struct
3808 * @tgt: ibmvfc target struct
3811 * 1 if PLOGI needed / 0 if PLOGI not needed
3813 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3814 struct ibmvfc_target *tgt)
3816 if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
3818 if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
3820 if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
3826 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3827 * @evt: ibmvfc event struct
3830 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3832 struct ibmvfc_target *tgt = evt->tgt;
3833 struct ibmvfc_host *vhost = evt->vhost;
3834 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3835 u32 status = be16_to_cpu(mad->common.status);
3836 u8 fc_reason, fc_explain;
3838 vhost->discovery_threads--;
3839 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3840 del_timer(&tgt->timer);
3843 case IBMVFC_MAD_SUCCESS:
3844 tgt_dbg(tgt, "ADISC succeeded\n");
3845 if (ibmvfc_adisc_needs_plogi(mad, tgt))
3846 ibmvfc_del_tgt(tgt);
3848 case IBMVFC_MAD_DRIVER_FAILED:
3850 case IBMVFC_MAD_FAILED:
3852 ibmvfc_del_tgt(tgt);
3853 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
3854 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
3855 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3856 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
3857 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
3858 ibmvfc_get_fc_type(fc_reason), fc_reason,
3859 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3863 kref_put(&tgt->kref, ibmvfc_release_tgt);
3864 ibmvfc_free_event(evt);
3865 wake_up(&vhost->work_wait_q);
3869 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3870 * @evt: ibmvfc event struct
3873 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3875 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3877 memset(mad, 0, sizeof(*mad));
3878 mad->common.version = cpu_to_be32(1);
3879 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
3880 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
3881 mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3882 offsetof(struct ibmvfc_passthru_mad, iu));
3883 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
3884 mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3885 mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
3886 mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3887 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3888 offsetof(struct ibmvfc_passthru_fc_iu, payload));
3889 mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3890 mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3891 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3892 offsetof(struct ibmvfc_passthru_fc_iu, response));
3893 mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
3897 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3898 * @evt: ibmvfc event struct
3900 * Just cleanup this event struct. Everything else is handled by
3901 * the ADISC completion handler. If the ADISC never actually comes
3902 * back, we still have the timer running on the ADISC event struct
3903 * which will fire and cause the CRQ to get reset.
3906 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3908 struct ibmvfc_host *vhost = evt->vhost;
3909 struct ibmvfc_target *tgt = evt->tgt;
3911 tgt_dbg(tgt, "ADISC cancel complete\n");
3912 vhost->abort_threads--;
3913 ibmvfc_free_event(evt);
3914 kref_put(&tgt->kref, ibmvfc_release_tgt);
3915 wake_up(&vhost->work_wait_q);
3919 * ibmvfc_adisc_timeout - Handle an ADISC timeout
3920 * @tgt: ibmvfc target struct
3922 * If an ADISC times out, send a cancel. If the cancel times
3923 * out, reset the CRQ. When the ADISC comes back as cancelled,
3924 * log back into the target.
3926 static void ibmvfc_adisc_timeout(struct timer_list *t)
3928 struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
3929 struct ibmvfc_host *vhost = tgt->vhost;
3930 struct ibmvfc_event *evt;
3931 struct ibmvfc_tmf *tmf;
3932 unsigned long flags;
3935 tgt_dbg(tgt, "ADISC timeout\n");
3936 spin_lock_irqsave(vhost->host->host_lock, flags);
3937 if (vhost->abort_threads >= disc_threads ||
3938 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3939 vhost->state != IBMVFC_INITIALIZING ||
3940 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3941 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3945 vhost->abort_threads++;
3946 kref_get(&tgt->kref);
3947 evt = ibmvfc_get_event(vhost);
3948 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3952 memset(tmf, 0, sizeof(*tmf));
3953 tmf->common.version = cpu_to_be32(1);
3954 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
3955 tmf->common.length = cpu_to_be16(sizeof(*tmf));
3956 tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
3957 tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
3959 rc = ibmvfc_send_event(evt, vhost, default_timeout);
3962 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3963 vhost->abort_threads--;
3964 kref_put(&tgt->kref, ibmvfc_release_tgt);
3965 __ibmvfc_reset_host(vhost);
3967 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3968 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3972 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3973 * @tgt: ibmvfc target struct
3975 * When sending an ADISC we end up with two timers running. The
3976 * first timer is the timer in the ibmvfc target struct. If this
3977 * fires, we send a cancel to the target. The second timer is the
3978 * timer on the ibmvfc event for the ADISC, which is longer. If that
3979 * fires, it means the ADISC timed out and our attempt to cancel it
3980 * also failed, so we need to reset the CRQ.
3982 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3984 struct ibmvfc_passthru_mad *mad;
3985 struct ibmvfc_host *vhost = tgt->vhost;
3986 struct ibmvfc_event *evt;
3988 if (vhost->discovery_threads >= disc_threads)
3991 kref_get(&tgt->kref);
3992 evt = ibmvfc_get_event(vhost);
3993 vhost->discovery_threads++;
3994 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3997 ibmvfc_init_passthru(evt);
3998 mad = &evt->iu.passthru;
3999 mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4000 mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4001 mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4003 mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4004 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4005 sizeof(vhost->login_buf->resp.port_name));
4006 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4007 sizeof(vhost->login_buf->resp.node_name));
4008 mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4010 if (timer_pending(&tgt->timer))
4011 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4013 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4014 add_timer(&tgt->timer);
4017 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4018 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4019 vhost->discovery_threads--;
4020 del_timer(&tgt->timer);
4021 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4022 kref_put(&tgt->kref, ibmvfc_release_tgt);
4024 tgt_dbg(tgt, "Sent ADISC\n");
4028 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4029 * @evt: ibmvfc event struct
4032 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4034 struct ibmvfc_target *tgt = evt->tgt;
4035 struct ibmvfc_host *vhost = evt->vhost;
4036 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4037 u32 status = be16_to_cpu(rsp->common.status);
4038 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4040 vhost->discovery_threads--;
4041 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4043 case IBMVFC_MAD_SUCCESS:
4044 tgt_dbg(tgt, "Query Target succeeded\n");
4045 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4046 ibmvfc_del_tgt(tgt);
4048 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4050 case IBMVFC_MAD_DRIVER_FAILED:
4052 case IBMVFC_MAD_CRQ_ERROR:
4053 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4055 case IBMVFC_MAD_FAILED:
4057 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4058 be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4059 be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4060 ibmvfc_del_tgt(tgt);
4061 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4062 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4064 ibmvfc_del_tgt(tgt);
4066 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4067 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4068 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4069 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4070 ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4075 kref_put(&tgt->kref, ibmvfc_release_tgt);
4076 ibmvfc_free_event(evt);
4077 wake_up(&vhost->work_wait_q);
4081 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4082 * @tgt: ibmvfc target struct
4085 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4087 struct ibmvfc_query_tgt *query_tgt;
4088 struct ibmvfc_host *vhost = tgt->vhost;
4089 struct ibmvfc_event *evt;
4091 if (vhost->discovery_threads >= disc_threads)
4094 kref_get(&tgt->kref);
4095 evt = ibmvfc_get_event(vhost);
4096 vhost->discovery_threads++;
4098 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4099 query_tgt = &evt->iu.query_tgt;
4100 memset(query_tgt, 0, sizeof(*query_tgt));
4101 query_tgt->common.version = cpu_to_be32(1);
4102 query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4103 query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4104 query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4106 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4107 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4108 vhost->discovery_threads--;
4109 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4110 kref_put(&tgt->kref, ibmvfc_release_tgt);
4112 tgt_dbg(tgt, "Sent Query Target\n");
4116 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4117 * @vhost: ibmvfc host struct
4118 * @scsi_id: SCSI ID to allocate target for
4121 * 0 on success / other on failure
4123 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4124 struct ibmvfc_discover_targets_entry *target)
4126 struct ibmvfc_target *stgt = NULL;
4127 struct ibmvfc_target *wtgt = NULL;
4128 struct ibmvfc_target *tgt;
4129 unsigned long flags;
4130 u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4131 u64 wwpn = be64_to_cpu(target->wwpn);
4133 /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4134 spin_lock_irqsave(vhost->host->host_lock, flags);
4135 list_for_each_entry(tgt, &vhost->targets, queue) {
4136 if (tgt->wwpn == wwpn) {
4142 list_for_each_entry(tgt, &vhost->targets, queue) {
4143 if (tgt->scsi_id == scsi_id) {
4149 if (wtgt && !stgt) {
4151 * A WWPN target has moved and we still are tracking the old
4152 * SCSI ID. The only way we should be able to get here is if
4153 * we attempted to send an implicit logout for the old SCSI ID
4154 * and it failed for some reason, such as there being I/O
4155 * pending to the target. In this case, we will have already
4156 * deleted the rport from the FC transport so we do a move
4157 * login, which works even with I/O pending, as it will cancel
4158 * any active commands.
4160 if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4162 * Do a move login here. The old target is no longer
4163 * known to the transport layer We don't use the
4164 * normal ibmvfc_set_tgt_action to set this, as we
4165 * don't normally want to allow this state change.
4167 wtgt->old_scsi_id = wtgt->scsi_id;
4168 wtgt->scsi_id = scsi_id;
4169 wtgt->action = IBMVFC_TGT_ACTION_INIT;
4170 ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4173 tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4174 wtgt->action, wtgt->rport);
4177 if (tgt->need_login)
4178 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4181 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4183 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4184 memset(tgt, 0, sizeof(*tgt));
4185 tgt->scsi_id = scsi_id;
4188 tgt->need_login = 1;
4189 timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4190 kref_init(&tgt->kref);
4191 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4192 spin_lock_irqsave(vhost->host->host_lock, flags);
4193 tgt->cancel_key = vhost->task_set++;
4194 list_add_tail(&tgt->queue, &vhost->targets);
4197 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4202 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4203 * @vhost: ibmvfc host struct
4206 * 0 on success / other on failure
4208 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4212 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4213 rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
4219 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4220 * @evt: ibmvfc event struct
4223 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4225 struct ibmvfc_host *vhost = evt->vhost;
4226 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4227 u32 mad_status = be16_to_cpu(rsp->common.status);
4228 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4230 switch (mad_status) {
4231 case IBMVFC_MAD_SUCCESS:
4232 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4233 vhost->num_targets = be32_to_cpu(rsp->num_written);
4234 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4236 case IBMVFC_MAD_FAILED:
4237 level += ibmvfc_retry_host_init(vhost);
4238 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4239 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4240 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4242 case IBMVFC_MAD_DRIVER_FAILED:
4245 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4246 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4250 ibmvfc_free_event(evt);
4251 wake_up(&vhost->work_wait_q);
4255 * ibmvfc_discover_targets - Send Discover Targets MAD
4256 * @vhost: ibmvfc host struct
4259 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4261 struct ibmvfc_discover_targets *mad;
4262 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4264 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4265 mad = &evt->iu.discover_targets;
4266 memset(mad, 0, sizeof(*mad));
4267 mad->common.version = cpu_to_be32(1);
4268 mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4269 mad->common.length = cpu_to_be16(sizeof(*mad));
4270 mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4271 mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4272 mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4273 mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
4274 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4276 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4277 ibmvfc_dbg(vhost, "Sent discover targets\n");
4279 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4283 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4284 * @evt: ibmvfc event struct
4287 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4289 struct ibmvfc_host *vhost = evt->vhost;
4290 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
4291 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
4292 unsigned int npiv_max_sectors;
4293 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4295 switch (mad_status) {
4296 case IBMVFC_MAD_SUCCESS:
4297 ibmvfc_free_event(evt);
4299 case IBMVFC_MAD_FAILED:
4300 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4301 level += ibmvfc_retry_host_init(vhost);
4303 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4304 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
4305 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4306 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4307 ibmvfc_free_event(evt);
4309 case IBMVFC_MAD_CRQ_ERROR:
4310 ibmvfc_retry_host_init(vhost);
4312 case IBMVFC_MAD_DRIVER_FAILED:
4313 ibmvfc_free_event(evt);
4316 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4317 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4318 ibmvfc_free_event(evt);
4322 vhost->client_migrated = 0;
4324 if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
4325 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4327 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4328 wake_up(&vhost->work_wait_q);
4332 if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
4333 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4335 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4336 wake_up(&vhost->work_wait_q);
4340 vhost->logged_in = 1;
4341 npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
4342 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4343 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4344 rsp->drc_name, npiv_max_sectors);
4346 fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
4347 fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
4348 fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
4349 fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
4350 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4351 fc_host_supported_classes(vhost->host) = 0;
4352 if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
4353 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
4354 if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
4355 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
4356 if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
4357 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4358 fc_host_maxframe_size(vhost->host) =
4359 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
4361 vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
4362 vhost->host->max_sectors = npiv_max_sectors;
4363 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4364 wake_up(&vhost->work_wait_q);
4368 * ibmvfc_npiv_login - Sends NPIV login
4369 * @vhost: ibmvfc host struct
4372 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4374 struct ibmvfc_npiv_login_mad *mad;
4375 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4377 ibmvfc_gather_partition_info(vhost);
4378 ibmvfc_set_login_info(vhost);
4379 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4381 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4382 mad = &evt->iu.npiv_login;
4383 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
4384 mad->common.version = cpu_to_be32(1);
4385 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
4386 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
4387 mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
4388 mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
4390 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4392 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4393 ibmvfc_dbg(vhost, "Sent NPIV login\n");
4395 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4399 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4400 * @vhost: ibmvfc host struct
4403 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4405 struct ibmvfc_host *vhost = evt->vhost;
4406 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
4408 ibmvfc_free_event(evt);
4410 switch (mad_status) {
4411 case IBMVFC_MAD_SUCCESS:
4412 if (list_empty(&vhost->sent) &&
4413 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
4414 ibmvfc_init_host(vhost);
4418 case IBMVFC_MAD_FAILED:
4419 case IBMVFC_MAD_NOT_SUPPORTED:
4420 case IBMVFC_MAD_CRQ_ERROR:
4421 case IBMVFC_MAD_DRIVER_FAILED:
4423 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4427 ibmvfc_hard_reset_host(vhost);
4431 * ibmvfc_npiv_logout - Issue an NPIV Logout
4432 * @vhost: ibmvfc host struct
4435 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4437 struct ibmvfc_npiv_logout_mad *mad;
4438 struct ibmvfc_event *evt;
4440 evt = ibmvfc_get_event(vhost);
4441 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4443 mad = &evt->iu.npiv_logout;
4444 memset(mad, 0, sizeof(*mad));
4445 mad->common.version = cpu_to_be32(1);
4446 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
4447 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
4449 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4451 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4452 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4454 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4458 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4459 * @vhost: ibmvfc host struct
4462 * 1 if work to do / 0 if not
4464 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4466 struct ibmvfc_target *tgt;
4468 list_for_each_entry(tgt, &vhost->targets, queue) {
4469 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4470 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4478 * ibmvfc_dev_logo_to_do - Is there target logout work to do?
4479 * @vhost: ibmvfc host struct
4482 * 1 if work to do / 0 if not
4484 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
4486 struct ibmvfc_target *tgt;
4488 list_for_each_entry(tgt, &vhost->targets, queue) {
4489 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
4490 tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
4497 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4498 * @vhost: ibmvfc host struct
4501 * 1 if work to do / 0 if not
4503 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4505 struct ibmvfc_target *tgt;
4507 if (kthread_should_stop())
4509 switch (vhost->action) {
4510 case IBMVFC_HOST_ACTION_NONE:
4511 case IBMVFC_HOST_ACTION_INIT_WAIT:
4512 case IBMVFC_HOST_ACTION_LOGO_WAIT:
4514 case IBMVFC_HOST_ACTION_TGT_INIT:
4515 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4516 if (vhost->discovery_threads == disc_threads)
4518 list_for_each_entry(tgt, &vhost->targets, queue)
4519 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4521 list_for_each_entry(tgt, &vhost->targets, queue)
4522 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4525 case IBMVFC_HOST_ACTION_TGT_DEL:
4526 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4527 if (vhost->discovery_threads == disc_threads)
4529 list_for_each_entry(tgt, &vhost->targets, queue)
4530 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
4532 list_for_each_entry(tgt, &vhost->targets, queue)
4533 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
4536 case IBMVFC_HOST_ACTION_LOGO:
4537 case IBMVFC_HOST_ACTION_INIT:
4538 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4539 case IBMVFC_HOST_ACTION_QUERY:
4540 case IBMVFC_HOST_ACTION_RESET:
4541 case IBMVFC_HOST_ACTION_REENABLE:
4550 * ibmvfc_work_to_do - Is there task level work to do?
4551 * @vhost: ibmvfc host struct
4554 * 1 if work to do / 0 if not
4556 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4558 unsigned long flags;
4561 spin_lock_irqsave(vhost->host->host_lock, flags);
4562 rc = __ibmvfc_work_to_do(vhost);
4563 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4568 * ibmvfc_log_ae - Log async events if necessary
4569 * @vhost: ibmvfc host struct
4570 * @events: events to log
4573 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4575 if (events & IBMVFC_AE_RSCN)
4576 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4577 if ((events & IBMVFC_AE_LINKDOWN) &&
4578 vhost->state >= IBMVFC_HALTED)
4579 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4580 if ((events & IBMVFC_AE_LINKUP) &&
4581 vhost->state == IBMVFC_INITIALIZING)
4582 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4586 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4587 * @tgt: ibmvfc target struct
4590 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4592 struct ibmvfc_host *vhost = tgt->vhost;
4593 struct fc_rport *rport;
4594 unsigned long flags;
4596 tgt_dbg(tgt, "Adding rport\n");
4597 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4598 spin_lock_irqsave(vhost->host->host_lock, flags);
4600 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4601 tgt_dbg(tgt, "Deleting rport\n");
4602 list_del(&tgt->queue);
4603 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4604 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4605 fc_remote_port_delete(rport);
4606 del_timer_sync(&tgt->timer);
4607 kref_put(&tgt->kref, ibmvfc_release_tgt);
4609 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
4610 tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
4611 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
4613 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4614 fc_remote_port_delete(rport);
4616 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4617 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4622 tgt_dbg(tgt, "rport add succeeded\n");
4624 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
4625 rport->supported_classes = 0;
4626 tgt->target_id = rport->scsi_target_id;
4627 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
4628 rport->supported_classes |= FC_COS_CLASS1;
4629 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
4630 rport->supported_classes |= FC_COS_CLASS2;
4631 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
4632 rport->supported_classes |= FC_COS_CLASS3;
4634 blk_queue_max_segments(rport->rqst_q, 1);
4636 tgt_dbg(tgt, "rport add failed\n");
4637 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4641 * ibmvfc_do_work - Do task level work
4642 * @vhost: ibmvfc host struct
4645 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4647 struct ibmvfc_target *tgt;
4648 unsigned long flags;
4649 struct fc_rport *rport;
4652 ibmvfc_log_ae(vhost, vhost->events_to_log);
4653 spin_lock_irqsave(vhost->host->host_lock, flags);
4654 vhost->events_to_log = 0;
4655 switch (vhost->action) {
4656 case IBMVFC_HOST_ACTION_NONE:
4657 case IBMVFC_HOST_ACTION_LOGO_WAIT:
4658 case IBMVFC_HOST_ACTION_INIT_WAIT:
4660 case IBMVFC_HOST_ACTION_RESET:
4661 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4662 rc = ibmvfc_reset_crq(vhost);
4664 spin_lock_irqsave(vhost->host->host_lock, flags);
4665 if (!rc || rc == H_CLOSED)
4666 vio_enable_interrupts(to_vio_dev(vhost->dev));
4667 if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
4669 * The only action we could have changed to would have
4670 * been reenable, in which case, we skip the rest of
4671 * this path and wait until we've done the re-enable
4672 * before sending the crq init.
4674 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4676 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4677 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
4678 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4679 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4683 case IBMVFC_HOST_ACTION_REENABLE:
4684 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4685 rc = ibmvfc_reenable_crq_queue(vhost);
4687 spin_lock_irqsave(vhost->host->host_lock, flags);
4688 if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
4690 * The only action we could have changed to would have
4691 * been reset, in which case, we skip the rest of this
4692 * path and wait until we've done the reset before
4693 * sending the crq init.
4695 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4696 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4697 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4698 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4702 case IBMVFC_HOST_ACTION_LOGO:
4703 vhost->job_step(vhost);
4705 case IBMVFC_HOST_ACTION_INIT:
4706 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
4707 if (vhost->delay_init) {
4708 vhost->delay_init = 0;
4709 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4713 vhost->job_step(vhost);
4715 case IBMVFC_HOST_ACTION_QUERY:
4716 list_for_each_entry(tgt, &vhost->targets, queue)
4717 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4718 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4720 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4721 list_for_each_entry(tgt, &vhost->targets, queue) {
4722 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4728 if (!ibmvfc_dev_init_to_do(vhost))
4729 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4731 case IBMVFC_HOST_ACTION_TGT_DEL:
4732 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4733 list_for_each_entry(tgt, &vhost->targets, queue) {
4734 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
4740 if (ibmvfc_dev_logo_to_do(vhost)) {
4741 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4745 list_for_each_entry(tgt, &vhost->targets, queue) {
4746 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4747 tgt_dbg(tgt, "Deleting rport\n");
4750 list_del(&tgt->queue);
4751 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4752 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4754 fc_remote_port_delete(rport);
4755 del_timer_sync(&tgt->timer);
4756 kref_put(&tgt->kref, ibmvfc_release_tgt);
4758 } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
4759 tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
4762 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
4763 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4765 fc_remote_port_delete(rport);
4770 if (vhost->state == IBMVFC_INITIALIZING) {
4771 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
4772 if (vhost->reinit) {
4774 scsi_block_requests(vhost->host);
4775 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4776 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4778 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4779 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4780 wake_up(&vhost->init_wait_q);
4781 schedule_work(&vhost->rport_add_work_q);
4782 vhost->init_retries = 0;
4783 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4784 scsi_unblock_requests(vhost->host);
4789 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4790 vhost->job_step = ibmvfc_discover_targets;
4793 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4794 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4795 scsi_unblock_requests(vhost->host);
4796 wake_up(&vhost->init_wait_q);
4800 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4801 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4802 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4803 ibmvfc_alloc_targets(vhost);
4804 spin_lock_irqsave(vhost->host->host_lock, flags);
4806 case IBMVFC_HOST_ACTION_TGT_INIT:
4807 list_for_each_entry(tgt, &vhost->targets, queue) {
4808 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4814 if (!ibmvfc_dev_init_to_do(vhost))
4815 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
4821 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4825 * ibmvfc_work - Do task level work
4826 * @data: ibmvfc host struct
4831 static int ibmvfc_work(void *data)
4833 struct ibmvfc_host *vhost = data;
4836 set_user_nice(current, MIN_NICE);
4839 rc = wait_event_interruptible(vhost->work_wait_q,
4840 ibmvfc_work_to_do(vhost));
4844 if (kthread_should_stop())
4847 ibmvfc_do_work(vhost);
4850 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4855 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4856 * @vhost: ibmvfc host struct
4858 * Allocates a page for messages, maps it for dma, and registers
4859 * the crq with the hypervisor.
4862 * zero on success / other on failure
4864 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4866 int rc, retrc = -ENOMEM;
4867 struct device *dev = vhost->dev;
4868 struct vio_dev *vdev = to_vio_dev(dev);
4869 struct ibmvfc_crq_queue *crq = &vhost->crq;
4872 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4877 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4878 crq->msg_token = dma_map_single(dev, crq->msgs,
4879 PAGE_SIZE, DMA_BIDIRECTIONAL);
4881 if (dma_mapping_error(dev, crq->msg_token))
4884 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4885 crq->msg_token, PAGE_SIZE);
4887 if (rc == H_RESOURCE)
4888 /* maybe kexecing and resource is busy. try a reset */
4889 retrc = rc = ibmvfc_reset_crq(vhost);
4892 dev_warn(dev, "Partner adapter not ready\n");
4894 dev_warn(dev, "Error %d opening adapter\n", rc);
4895 goto reg_crq_failed;
4900 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4902 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4903 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4904 goto req_irq_failed;
4907 if ((rc = vio_enable_interrupts(vdev))) {
4908 dev_err(dev, "Error %d enabling interrupts\n", rc);
4909 goto req_irq_failed;
4917 tasklet_kill(&vhost->tasklet);
4919 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4920 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4922 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4924 free_page((unsigned long)crq->msgs);
4929 * ibmvfc_free_mem - Free memory for vhost
4930 * @vhost: ibmvfc host struct
4935 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4937 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4940 mempool_destroy(vhost->tgt_pool);
4941 kfree(vhost->trace);
4942 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4943 vhost->disc_buf_dma);
4944 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4945 vhost->login_buf, vhost->login_buf_dma);
4946 dma_pool_destroy(vhost->sg_pool);
4947 dma_unmap_single(vhost->dev, async_q->msg_token,
4948 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4949 free_page((unsigned long)async_q->msgs);
4954 * ibmvfc_alloc_mem - Allocate memory for vhost
4955 * @vhost: ibmvfc host struct
4958 * 0 on success / non-zero on failure
4960 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4962 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4963 struct device *dev = vhost->dev;
4966 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4967 if (!async_q->msgs) {
4968 dev_err(dev, "Couldn't allocate async queue.\n");
4972 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4973 async_q->msg_token = dma_map_single(dev, async_q->msgs,
4974 async_q->size * sizeof(*async_q->msgs),
4977 if (dma_mapping_error(dev, async_q->msg_token)) {
4978 dev_err(dev, "Failed to map async queue\n");
4979 goto free_async_crq;
4982 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4983 SG_ALL * sizeof(struct srp_direct_buf),
4984 sizeof(struct srp_direct_buf), 0);
4986 if (!vhost->sg_pool) {
4987 dev_err(dev, "Failed to allocate sg pool\n");
4988 goto unmap_async_crq;
4991 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
4992 &vhost->login_buf_dma, GFP_KERNEL);
4994 if (!vhost->login_buf) {
4995 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
4999 vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
5000 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
5001 &vhost->disc_buf_dma, GFP_KERNEL);
5003 if (!vhost->disc_buf) {
5004 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
5005 goto free_login_buffer;
5008 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
5009 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
5012 goto free_disc_buffer;
5014 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
5015 sizeof(struct ibmvfc_target));
5017 if (!vhost->tgt_pool) {
5018 dev_err(dev, "Couldn't allocate target memory pool\n");
5026 kfree(vhost->trace);
5028 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
5029 vhost->disc_buf_dma);
5031 dma_free_coherent(dev, sizeof(*vhost->login_buf),
5032 vhost->login_buf, vhost->login_buf_dma);
5034 dma_pool_destroy(vhost->sg_pool);
5036 dma_unmap_single(dev, async_q->msg_token,
5037 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
5039 free_page((unsigned long)async_q->msgs);
5046 * ibmvfc_rport_add_thread - Worker thread for rport adds
5047 * @work: work struct
5050 static void ibmvfc_rport_add_thread(struct work_struct *work)
5052 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
5054 struct ibmvfc_target *tgt;
5055 struct fc_rport *rport;
5056 unsigned long flags;
5060 spin_lock_irqsave(vhost->host->host_lock, flags);
5063 if (vhost->state != IBMVFC_ACTIVE)
5066 list_for_each_entry(tgt, &vhost->targets, queue) {
5067 if (tgt->add_rport) {
5070 kref_get(&tgt->kref);
5073 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5074 ibmvfc_tgt_add_rport(tgt);
5075 } else if (get_device(&rport->dev)) {
5076 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5077 tgt_dbg(tgt, "Setting rport roles\n");
5078 fc_remote_port_rolechg(rport, tgt->ids.roles);
5079 put_device(&rport->dev);
5081 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5084 kref_put(&tgt->kref, ibmvfc_release_tgt);
5085 spin_lock_irqsave(vhost->host->host_lock, flags);
5091 if (vhost->state == IBMVFC_ACTIVE)
5092 vhost->scan_complete = 1;
5093 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5098 * ibmvfc_probe - Adapter hot plug add entry point
5099 * @vdev: vio device struct
5100 * @id: vio device id struct
5103 * 0 on success / non-zero on failure
5105 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
5107 struct ibmvfc_host *vhost;
5108 struct Scsi_Host *shost;
5109 struct device *dev = &vdev->dev;
5113 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
5115 dev_err(dev, "Couldn't allocate host data\n");
5119 shost->transportt = ibmvfc_transport_template;
5120 shost->can_queue = max_requests;
5121 shost->max_lun = max_lun;
5122 shost->max_id = max_targets;
5123 shost->max_sectors = IBMVFC_MAX_SECTORS;
5124 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
5125 shost->unique_id = shost->host_no;
5127 vhost = shost_priv(shost);
5128 INIT_LIST_HEAD(&vhost->sent);
5129 INIT_LIST_HEAD(&vhost->free);
5130 INIT_LIST_HEAD(&vhost->targets);
5131 sprintf(vhost->name, IBMVFC_NAME);
5132 vhost->host = shost;
5134 vhost->partition_number = -1;
5135 vhost->log_level = log_level;
5136 vhost->task_set = 1;
5137 strcpy(vhost->partition_name, "UNKNOWN");
5138 init_waitqueue_head(&vhost->work_wait_q);
5139 init_waitqueue_head(&vhost->init_wait_q);
5140 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
5141 mutex_init(&vhost->passthru_mutex);
5143 if ((rc = ibmvfc_alloc_mem(vhost)))
5144 goto free_scsi_host;
5146 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
5149 if (IS_ERR(vhost->work_thread)) {
5150 dev_err(dev, "Couldn't create kernel thread: %ld\n",
5151 PTR_ERR(vhost->work_thread));
5152 rc = PTR_ERR(vhost->work_thread);
5156 if ((rc = ibmvfc_init_crq(vhost))) {
5157 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
5161 if ((rc = ibmvfc_init_event_pool(vhost))) {
5162 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
5166 if ((rc = scsi_add_host(shost, dev)))
5167 goto release_event_pool;
5169 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
5171 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
5172 &ibmvfc_trace_attr))) {
5173 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
5177 if (shost_to_fc_host(shost)->rqst_q)
5178 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
5179 dev_set_drvdata(dev, vhost);
5180 spin_lock(&ibmvfc_driver_lock);
5181 list_add_tail(&vhost->queue, &ibmvfc_head);
5182 spin_unlock(&ibmvfc_driver_lock);
5184 ibmvfc_send_crq_init(vhost);
5185 scsi_scan_host(shost);
5189 scsi_remove_host(shost);
5191 ibmvfc_free_event_pool(vhost);
5193 ibmvfc_release_crq_queue(vhost);
5195 kthread_stop(vhost->work_thread);
5197 ibmvfc_free_mem(vhost);
5199 scsi_host_put(shost);
5206 * ibmvfc_remove - Adapter hot plug remove entry point
5207 * @vdev: vio device struct
5212 static int ibmvfc_remove(struct vio_dev *vdev)
5214 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
5215 unsigned long flags;
5218 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
5220 spin_lock_irqsave(vhost->host->host_lock, flags);
5221 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
5222 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5224 ibmvfc_wait_while_resetting(vhost);
5225 ibmvfc_release_crq_queue(vhost);
5226 kthread_stop(vhost->work_thread);
5227 fc_remove_host(vhost->host);
5228 scsi_remove_host(vhost->host);
5230 spin_lock_irqsave(vhost->host->host_lock, flags);
5231 ibmvfc_purge_requests(vhost, DID_ERROR);
5232 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5233 ibmvfc_free_event_pool(vhost);
5235 ibmvfc_free_mem(vhost);
5236 spin_lock(&ibmvfc_driver_lock);
5237 list_del(&vhost->queue);
5238 spin_unlock(&ibmvfc_driver_lock);
5239 scsi_host_put(vhost->host);
5245 * ibmvfc_resume - Resume from suspend
5246 * @dev: device struct
5248 * We may have lost an interrupt across suspend/resume, so kick the
5252 static int ibmvfc_resume(struct device *dev)
5254 unsigned long flags;
5255 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
5256 struct vio_dev *vdev = to_vio_dev(dev);
5258 spin_lock_irqsave(vhost->host->host_lock, flags);
5259 vio_disable_interrupts(vdev);
5260 tasklet_schedule(&vhost->tasklet);
5261 spin_unlock_irqrestore(vhost->host->host_lock, flags);
5266 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
5267 * @vdev: vio device struct
5270 * Number of bytes the driver will need to DMA map at the same time in
5271 * order to perform well.
5273 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
5275 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
5276 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
5279 static const struct vio_device_id ibmvfc_device_table[] = {
5280 {"fcp", "IBM,vfc-client"},
5283 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
5285 static const struct dev_pm_ops ibmvfc_pm_ops = {
5286 .resume = ibmvfc_resume
5289 static struct vio_driver ibmvfc_driver = {
5290 .id_table = ibmvfc_device_table,
5291 .probe = ibmvfc_probe,
5292 .remove = ibmvfc_remove,
5293 .get_desired_dma = ibmvfc_get_desired_dma,
5294 .name = IBMVFC_NAME,
5295 .pm = &ibmvfc_pm_ops,
5298 static struct fc_function_template ibmvfc_transport_functions = {
5299 .show_host_fabric_name = 1,
5300 .show_host_node_name = 1,
5301 .show_host_port_name = 1,
5302 .show_host_supported_classes = 1,
5303 .show_host_port_type = 1,
5304 .show_host_port_id = 1,
5305 .show_host_maxframe_size = 1,
5307 .get_host_port_state = ibmvfc_get_host_port_state,
5308 .show_host_port_state = 1,
5310 .get_host_speed = ibmvfc_get_host_speed,
5311 .show_host_speed = 1,
5313 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
5314 .terminate_rport_io = ibmvfc_terminate_rport_io,
5316 .show_rport_maxframe_size = 1,
5317 .show_rport_supported_classes = 1,
5319 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
5320 .show_rport_dev_loss_tmo = 1,
5322 .get_starget_node_name = ibmvfc_get_starget_node_name,
5323 .show_starget_node_name = 1,
5325 .get_starget_port_name = ibmvfc_get_starget_port_name,
5326 .show_starget_port_name = 1,
5328 .get_starget_port_id = ibmvfc_get_starget_port_id,
5329 .show_starget_port_id = 1,
5331 .bsg_request = ibmvfc_bsg_request,
5332 .bsg_timeout = ibmvfc_bsg_timeout,
5336 * ibmvfc_module_init - Initialize the ibmvfc module
5339 * 0 on success / other on failure
5341 static int __init ibmvfc_module_init(void)
5345 if (!firmware_has_feature(FW_FEATURE_VIO))
5348 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
5349 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
5351 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
5352 if (!ibmvfc_transport_template)
5355 rc = vio_register_driver(&ibmvfc_driver);
5357 fc_release_transport(ibmvfc_transport_template);
5362 * ibmvfc_module_exit - Teardown the ibmvfc module
5367 static void __exit ibmvfc_module_exit(void)
5369 vio_unregister_driver(&ibmvfc_driver);
5370 fc_release_transport(ibmvfc_transport_template);
5373 module_init(ibmvfc_module_init);
5374 module_exit(ibmvfc_module_exit);