2 * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
4 * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
6 * Copyright (C) IBM Corporation, 2008
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/dmapool.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/kthread.h>
32 #include <linux/stringify.h>
33 #include <asm/firmware.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_tcq.h>
41 #include <scsi/scsi_transport_fc.h>
44 static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
45 static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
46 static unsigned int max_lun = IBMVFC_MAX_LUN;
47 static unsigned int max_targets = IBMVFC_MAX_TARGETS;
48 static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
49 static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
50 static unsigned int dev_loss_tmo = IBMVFC_DEV_LOSS_TMO;
51 static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
52 static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
53 static LIST_HEAD(ibmvfc_head);
54 static DEFINE_SPINLOCK(ibmvfc_driver_lock);
55 static struct scsi_transport_template *ibmvfc_transport_template;
57 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
58 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION(IBMVFC_DRIVER_VERSION);
62 module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
63 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
64 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
65 module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
66 MODULE_PARM_DESC(default_timeout,
67 "Default timeout in seconds for initialization and EH commands. "
68 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
69 module_param_named(max_requests, max_requests, uint, S_IRUGO);
70 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
71 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
72 module_param_named(max_lun, max_lun, uint, S_IRUGO);
73 MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
74 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
75 module_param_named(max_targets, max_targets, uint, S_IRUGO);
76 MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
77 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
78 module_param_named(disc_threads, disc_threads, uint, S_IRUGO | S_IWUSR);
79 MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
80 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
81 module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
82 MODULE_PARM_DESC(debug, "Enable driver debug information. "
83 "[Default=" __stringify(IBMVFC_DEBUG) "]");
84 module_param_named(dev_loss_tmo, dev_loss_tmo, uint, S_IRUGO | S_IWUSR);
85 MODULE_PARM_DESC(dev_loss_tmo, "Maximum number of seconds that the FC "
86 "transport should insulate the loss of a remote port. Once this "
87 "value is exceeded, the scsi target is removed. "
88 "[Default=" __stringify(IBMVFC_DEV_LOSS_TMO) "]");
89 module_param_named(log_level, log_level, uint, 0);
90 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
91 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
101 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
102 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
103 { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
104 { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
105 { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
106 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
107 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
108 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
109 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
110 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
111 { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
112 { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
113 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 0, 0, "link halted" },
114 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
116 { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
117 { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
118 { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
119 { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
120 { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
121 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
122 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
123 { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
124 { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
125 { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
127 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
128 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
129 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
130 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
131 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
132 { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
133 { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
134 { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
135 { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
136 { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
137 { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
139 { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
142 static void ibmvfc_npiv_login(struct ibmvfc_host *);
143 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
144 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
145 static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
147 static const char *unknown_error = "unknown error";
149 #ifdef CONFIG_SCSI_IBMVFC_TRACE
151 * ibmvfc_trc_start - Log a start trace entry
152 * @evt: ibmvfc event struct
155 static void ibmvfc_trc_start(struct ibmvfc_event *evt)
157 struct ibmvfc_host *vhost = evt->vhost;
158 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
159 struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
160 struct ibmvfc_trace_entry *entry;
162 entry = &vhost->trace[vhost->trace_index++];
164 entry->time = jiffies;
165 entry->fmt = evt->crq.format;
166 entry->type = IBMVFC_TRC_START;
168 switch (entry->fmt) {
169 case IBMVFC_CMD_FORMAT:
170 entry->op_code = vfc_cmd->iu.cdb[0];
171 entry->scsi_id = vfc_cmd->tgt_scsi_id;
172 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
173 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
174 entry->u.start.xfer_len = vfc_cmd->iu.xfer_len;
176 case IBMVFC_MAD_FORMAT:
177 entry->op_code = mad->opcode;
185 * ibmvfc_trc_end - Log an end trace entry
186 * @evt: ibmvfc event struct
189 static void ibmvfc_trc_end(struct ibmvfc_event *evt)
191 struct ibmvfc_host *vhost = evt->vhost;
192 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
193 struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
194 struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
197 entry->time = jiffies;
198 entry->fmt = evt->crq.format;
199 entry->type = IBMVFC_TRC_END;
201 switch (entry->fmt) {
202 case IBMVFC_CMD_FORMAT:
203 entry->op_code = vfc_cmd->iu.cdb[0];
204 entry->scsi_id = vfc_cmd->tgt_scsi_id;
205 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
206 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
207 entry->u.end.status = vfc_cmd->status;
208 entry->u.end.error = vfc_cmd->error;
209 entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
210 entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
211 entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
213 case IBMVFC_MAD_FORMAT:
214 entry->op_code = mad->opcode;
215 entry->u.end.status = mad->status;
224 #define ibmvfc_trc_start(evt) do { } while (0)
225 #define ibmvfc_trc_end(evt) do { } while (0)
229 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
230 * @status: status / error class
234 * index into cmd_status / -EINVAL on failure
236 static int ibmvfc_get_err_index(u16 status, u16 error)
240 for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
241 if ((cmd_status[i].status & status) == cmd_status[i].status &&
242 cmd_status[i].error == error)
249 * ibmvfc_get_cmd_error - Find the error description for the fcp response
250 * @status: status / error class
254 * error description string
256 static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
258 int rc = ibmvfc_get_err_index(status, error);
260 return cmd_status[rc].name;
261 return unknown_error;
265 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
266 * @vfc_cmd: ibmvfc command struct
269 * SCSI result value to return for completed command
271 static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
274 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
275 int fc_rsp_len = rsp->fcp_rsp_len;
277 if ((rsp->flags & FCP_RSP_LEN_VALID) &&
278 ((!fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
279 rsp->data.info.rsp_code))
280 return DID_ERROR << 16;
282 err = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
284 return rsp->scsi_status | (cmd_status[err].result << 16);
285 return rsp->scsi_status | (DID_ERROR << 16);
289 * ibmvfc_retry_cmd - Determine if error status is retryable
290 * @status: status / error class
294 * 1 if error should be retried / 0 if it should not
296 static int ibmvfc_retry_cmd(u16 status, u16 error)
298 int rc = ibmvfc_get_err_index(status, error);
301 return cmd_status[rc].retry;
305 static const char *unknown_fc_explain = "unknown fc explain";
307 static const struct {
311 { 0x00, "no additional explanation" },
312 { 0x01, "service parameter error - options" },
313 { 0x03, "service parameter error - initiator control" },
314 { 0x05, "service parameter error - recipient control" },
315 { 0x07, "service parameter error - received data field size" },
316 { 0x09, "service parameter error - concurrent seq" },
317 { 0x0B, "service parameter error - credit" },
318 { 0x0D, "invalid N_Port/F_Port_Name" },
319 { 0x0E, "invalid node/Fabric Name" },
320 { 0x0F, "invalid common service parameters" },
321 { 0x11, "invalid association header" },
322 { 0x13, "association header required" },
323 { 0x15, "invalid originator S_ID" },
324 { 0x17, "invalid OX_ID-RX-ID combination" },
325 { 0x19, "command (request) already in progress" },
326 { 0x1E, "N_Port Login requested" },
327 { 0x1F, "Invalid N_Port_ID" },
330 static const struct {
334 { 0x00, "no additional explanation" },
335 { 0x01, "port identifier not registered" },
336 { 0x02, "port name not registered" },
337 { 0x03, "node name not registered" },
338 { 0x04, "class of service not registered" },
339 { 0x06, "initial process associator not registered" },
340 { 0x07, "FC-4 TYPEs not registered" },
341 { 0x08, "symbolic port name not registered" },
342 { 0x09, "symbolic node name not registered" },
343 { 0x0A, "port type not registered" },
344 { 0xF0, "authorization exception" },
345 { 0xF1, "authentication exception" },
346 { 0xF2, "data base full" },
347 { 0xF3, "data base empty" },
348 { 0xF4, "processing request" },
349 { 0xF5, "unable to verify connection" },
350 { 0xF6, "devices not in a common zone" },
354 * ibmvfc_get_ls_explain - Return the FC Explain description text
355 * @status: FC Explain status
360 static const char *ibmvfc_get_ls_explain(u16 status)
364 for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
365 if (ls_explain[i].fc_explain == status)
366 return ls_explain[i].name;
368 return unknown_fc_explain;
372 * ibmvfc_get_gs_explain - Return the FC Explain description text
373 * @status: FC Explain status
378 static const char *ibmvfc_get_gs_explain(u16 status)
382 for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
383 if (gs_explain[i].fc_explain == status)
384 return gs_explain[i].name;
386 return unknown_fc_explain;
389 static const struct {
390 enum ibmvfc_fc_type fc_type;
393 { IBMVFC_FABRIC_REJECT, "fabric reject" },
394 { IBMVFC_PORT_REJECT, "port reject" },
395 { IBMVFC_LS_REJECT, "ELS reject" },
396 { IBMVFC_FABRIC_BUSY, "fabric busy" },
397 { IBMVFC_PORT_BUSY, "port busy" },
398 { IBMVFC_BASIC_REJECT, "basic reject" },
401 static const char *unknown_fc_type = "unknown fc type";
404 * ibmvfc_get_fc_type - Return the FC Type description text
405 * @status: FC Type error status
410 static const char *ibmvfc_get_fc_type(u16 status)
414 for (i = 0; i < ARRAY_SIZE(fc_type); i++)
415 if (fc_type[i].fc_type == status)
416 return fc_type[i].name;
418 return unknown_fc_type;
422 * ibmvfc_set_tgt_action - Set the next init action for the target
423 * @tgt: ibmvfc target struct
424 * @action: action to perform
427 static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
428 enum ibmvfc_target_action action)
430 switch (tgt->action) {
431 case IBMVFC_TGT_ACTION_DEL_RPORT:
434 tgt->action = action;
440 * ibmvfc_set_host_state - Set the state for the host
441 * @vhost: ibmvfc host struct
442 * @state: state to set host to
445 * 0 if state changed / non-zero if not changed
447 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
448 enum ibmvfc_host_state state)
452 switch (vhost->state) {
453 case IBMVFC_HOST_OFFLINE:
457 vhost->state = state;
465 * ibmvfc_set_host_action - Set the next init action for the host
466 * @vhost: ibmvfc host struct
467 * @action: action to perform
470 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
471 enum ibmvfc_host_action action)
474 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
475 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
476 vhost->action = action;
478 case IBMVFC_HOST_ACTION_INIT_WAIT:
479 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
480 vhost->action = action;
482 case IBMVFC_HOST_ACTION_QUERY:
483 switch (vhost->action) {
484 case IBMVFC_HOST_ACTION_INIT_WAIT:
485 case IBMVFC_HOST_ACTION_NONE:
486 case IBMVFC_HOST_ACTION_TGT_ADD:
487 vhost->action = action;
493 case IBMVFC_HOST_ACTION_TGT_INIT:
494 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
495 vhost->action = action;
497 case IBMVFC_HOST_ACTION_INIT:
498 case IBMVFC_HOST_ACTION_TGT_DEL:
499 case IBMVFC_HOST_ACTION_QUERY_TGTS:
500 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
501 case IBMVFC_HOST_ACTION_TGT_ADD:
502 case IBMVFC_HOST_ACTION_NONE:
504 vhost->action = action;
510 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
511 * @vhost: ibmvfc host struct
516 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
518 if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
519 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
520 scsi_block_requests(vhost->host);
521 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
526 wake_up(&vhost->work_wait_q);
530 * ibmvfc_link_down - Handle a link down event from the adapter
531 * @vhost: ibmvfc host struct
532 * @state: ibmvfc host state to enter
535 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
536 enum ibmvfc_host_state state)
538 struct ibmvfc_target *tgt;
541 scsi_block_requests(vhost->host);
542 list_for_each_entry(tgt, &vhost->targets, queue)
543 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
544 ibmvfc_set_host_state(vhost, state);
545 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
546 vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
547 wake_up(&vhost->work_wait_q);
552 * ibmvfc_init_host - Start host initialization
553 * @vhost: ibmvfc host struct
554 * @relogin: is this a re-login?
559 static void ibmvfc_init_host(struct ibmvfc_host *vhost, int relogin)
561 struct ibmvfc_target *tgt;
563 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
564 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
566 "Host initialization retries exceeded. Taking adapter offline\n");
567 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
572 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
574 memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
575 vhost->async_crq.cur = 0;
578 list_for_each_entry(tgt, &vhost->targets, queue)
580 scsi_block_requests(vhost->host);
581 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
582 vhost->job_step = ibmvfc_npiv_login;
583 wake_up(&vhost->work_wait_q);
588 * ibmvfc_send_crq - Send a CRQ
589 * @vhost: ibmvfc host struct
590 * @word1: the first 64 bits of the data
591 * @word2: the second 64 bits of the data
594 * 0 on success / other on failure
596 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
598 struct vio_dev *vdev = to_vio_dev(vhost->dev);
599 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
603 * ibmvfc_send_crq_init - Send a CRQ init message
604 * @vhost: ibmvfc host struct
607 * 0 on success / other on failure
609 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
611 ibmvfc_dbg(vhost, "Sending CRQ init\n");
612 return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
616 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
617 * @vhost: ibmvfc host struct
620 * 0 on success / other on failure
622 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
624 ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
625 return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
629 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
630 * @vhost: ibmvfc host struct
632 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
633 * the crq with the hypervisor.
635 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
638 struct vio_dev *vdev = to_vio_dev(vhost->dev);
639 struct ibmvfc_crq_queue *crq = &vhost->crq;
641 ibmvfc_dbg(vhost, "Releasing CRQ\n");
642 free_irq(vdev->irq, vhost);
644 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
645 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
647 vhost->state = IBMVFC_NO_CRQ;
648 dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
649 free_page((unsigned long)crq->msgs);
653 * ibmvfc_reenable_crq_queue - reenables the CRQ
654 * @vhost: ibmvfc host struct
657 * 0 on success / other on failure
659 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
662 struct vio_dev *vdev = to_vio_dev(vhost->dev);
664 /* Re-enable the CRQ */
666 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
667 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
670 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
676 * ibmvfc_reset_crq - resets a crq after a failure
677 * @vhost: ibmvfc host struct
680 * 0 on success / other on failure
682 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
685 struct vio_dev *vdev = to_vio_dev(vhost->dev);
686 struct ibmvfc_crq_queue *crq = &vhost->crq;
690 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
691 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
693 vhost->state = IBMVFC_NO_CRQ;
694 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
696 /* Clean out the queue */
697 memset(crq->msgs, 0, PAGE_SIZE);
700 /* And re-open it again */
701 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
702 crq->msg_token, PAGE_SIZE);
705 /* Adapter is good, but other end is not ready */
706 dev_warn(vhost->dev, "Partner adapter not ready\n");
708 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
714 * ibmvfc_valid_event - Determines if event is valid.
715 * @pool: event_pool that contains the event
716 * @evt: ibmvfc event to be checked for validity
719 * 1 if event is valid / 0 if event is not valid
721 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
722 struct ibmvfc_event *evt)
724 int index = evt - pool->events;
725 if (index < 0 || index >= pool->size) /* outside of bounds */
727 if (evt != pool->events + index) /* unaligned */
733 * ibmvfc_free_event - Free the specified event
734 * @evt: ibmvfc_event to be freed
737 static void ibmvfc_free_event(struct ibmvfc_event *evt)
739 struct ibmvfc_host *vhost = evt->vhost;
740 struct ibmvfc_event_pool *pool = &vhost->pool;
742 BUG_ON(!ibmvfc_valid_event(pool, evt));
743 BUG_ON(atomic_inc_return(&evt->free) != 1);
744 list_add_tail(&evt->queue, &vhost->free);
748 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
749 * @evt: ibmvfc event struct
751 * This function does not setup any error status, that must be done
752 * before this function gets called.
754 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
756 struct scsi_cmnd *cmnd = evt->cmnd;
759 scsi_dma_unmap(cmnd);
760 cmnd->scsi_done(cmnd);
764 complete(evt->eh_comp);
766 ibmvfc_free_event(evt);
770 * ibmvfc_fail_request - Fail request with specified error code
771 * @evt: ibmvfc event struct
772 * @error_code: error code to fail request with
777 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
780 evt->cmnd->result = (error_code << 16);
781 evt->done = ibmvfc_scsi_eh_done;
783 evt->xfer_iu->mad_common.status = IBMVFC_MAD_DRIVER_FAILED;
785 list_del(&evt->queue);
786 del_timer(&evt->timer);
792 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
793 * @vhost: ibmvfc host struct
794 * @error_code: error code to fail requests with
799 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
801 struct ibmvfc_event *evt, *pos;
803 ibmvfc_dbg(vhost, "Purging all requests\n");
804 list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
805 ibmvfc_fail_request(evt, error_code);
809 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
810 * @vhost: struct ibmvfc host to reset
812 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
816 scsi_block_requests(vhost->host);
817 ibmvfc_purge_requests(vhost, DID_ERROR);
818 if ((rc = ibmvfc_reset_crq(vhost)) ||
819 (rc = ibmvfc_send_crq_init(vhost)) ||
820 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
821 dev_err(vhost->dev, "Error after reset rc=%d\n", rc);
822 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
824 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
828 * ibmvfc_reset_host - Reset the connection to the server
829 * @vhost: struct ibmvfc host to reset
831 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
835 spin_lock_irqsave(vhost->host->host_lock, flags);
836 __ibmvfc_reset_host(vhost);
837 spin_unlock_irqrestore(vhost->host->host_lock, flags);
841 * ibmvfc_retry_host_init - Retry host initialization if allowed
842 * @vhost: ibmvfc host struct
845 static void ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
847 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
848 vhost->delay_init = 1;
849 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
851 "Host initialization retries exceeded. Taking adapter offline\n");
852 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
853 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
854 __ibmvfc_reset_host(vhost);
856 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
859 wake_up(&vhost->work_wait_q);
863 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
864 * @starget: scsi target struct
867 * ibmvfc_target struct / NULL if not found
869 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
871 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
872 struct ibmvfc_host *vhost = shost_priv(shost);
873 struct ibmvfc_target *tgt;
875 list_for_each_entry(tgt, &vhost->targets, queue)
876 if (tgt->target_id == starget->id) {
877 kref_get(&tgt->kref);
884 * ibmvfc_get_target - Find the specified scsi_target
885 * @starget: scsi target struct
888 * ibmvfc_target struct / NULL if not found
890 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
892 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
893 struct ibmvfc_target *tgt;
896 spin_lock_irqsave(shost->host_lock, flags);
897 tgt = __ibmvfc_get_target(starget);
898 spin_unlock_irqrestore(shost->host_lock, flags);
903 * ibmvfc_get_host_speed - Get host port speed
904 * @shost: scsi host struct
909 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
911 struct ibmvfc_host *vhost = shost_priv(shost);
914 spin_lock_irqsave(shost->host_lock, flags);
915 if (vhost->state == IBMVFC_ACTIVE) {
916 switch (vhost->login_buf->resp.link_speed / 100) {
918 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
921 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
924 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
927 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
930 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
933 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
936 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
937 vhost->login_buf->resp.link_speed / 100);
938 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
942 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
943 spin_unlock_irqrestore(shost->host_lock, flags);
947 * ibmvfc_get_host_port_state - Get host port state
948 * @shost: scsi host struct
953 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
955 struct ibmvfc_host *vhost = shost_priv(shost);
958 spin_lock_irqsave(shost->host_lock, flags);
959 switch (vhost->state) {
960 case IBMVFC_INITIALIZING:
962 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
964 case IBMVFC_LINK_DOWN:
965 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
967 case IBMVFC_LINK_DEAD:
968 case IBMVFC_HOST_OFFLINE:
969 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
972 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
975 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
978 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
979 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
982 spin_unlock_irqrestore(shost->host_lock, flags);
986 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
987 * @rport: rport struct
988 * @timeout: timeout value
993 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
996 rport->dev_loss_tmo = timeout;
998 rport->dev_loss_tmo = 1;
1002 * ibmvfc_release_tgt - Free memory allocated for a target
1003 * @kref: kref struct
1006 static void ibmvfc_release_tgt(struct kref *kref)
1008 struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1013 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1014 * @starget: scsi target struct
1019 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1021 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1022 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1024 kref_put(&tgt->kref, ibmvfc_release_tgt);
1028 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1029 * @starget: scsi target struct
1034 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1036 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1037 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1039 kref_put(&tgt->kref, ibmvfc_release_tgt);
1043 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1044 * @starget: scsi target struct
1049 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1051 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1052 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1054 kref_put(&tgt->kref, ibmvfc_release_tgt);
1058 * ibmvfc_wait_while_resetting - Wait while the host resets
1059 * @vhost: ibmvfc host struct
1062 * 0 on success / other on failure
1064 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1066 long timeout = wait_event_timeout(vhost->init_wait_q,
1067 ((vhost->state == IBMVFC_ACTIVE ||
1068 vhost->state == IBMVFC_HOST_OFFLINE ||
1069 vhost->state == IBMVFC_LINK_DEAD) &&
1070 vhost->action == IBMVFC_HOST_ACTION_NONE),
1071 (init_timeout * HZ));
1073 return timeout ? 0 : -EIO;
1077 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1078 * @shost: scsi host struct
1081 * 0 on success / other on failure
1083 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1085 struct ibmvfc_host *vhost = shost_priv(shost);
1087 dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1088 ibmvfc_reset_host(vhost);
1089 return ibmvfc_wait_while_resetting(vhost);
1093 * ibmvfc_gather_partition_info - Gather info about the LPAR
1098 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1100 struct device_node *rootdn;
1102 const unsigned int *num;
1104 rootdn = of_find_node_by_path("/");
1108 name = of_get_property(rootdn, "ibm,partition-name", NULL);
1110 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1111 num = of_get_property(rootdn, "ibm,partition-no", NULL);
1113 vhost->partition_number = *num;
1114 of_node_put(rootdn);
1118 * ibmvfc_set_login_info - Setup info for NPIV login
1119 * @vhost: ibmvfc host struct
1124 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1126 struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1127 struct device_node *of_node = vhost->dev->archdata.of_node;
1128 const char *location;
1130 memset(login_info, 0, sizeof(*login_info));
1132 login_info->ostype = IBMVFC_OS_LINUX;
1133 login_info->max_dma_len = IBMVFC_MAX_SECTORS << 9;
1134 login_info->max_payload = sizeof(struct ibmvfc_fcp_cmd_iu);
1135 login_info->max_response = sizeof(struct ibmvfc_fcp_rsp);
1136 login_info->partition_num = vhost->partition_number;
1137 login_info->vfc_frame_version = 1;
1138 login_info->fcp_version = 3;
1139 if (vhost->client_migrated)
1140 login_info->flags = IBMVFC_CLIENT_MIGRATED;
1142 login_info->max_cmds = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1143 login_info->capabilities = IBMVFC_CAN_MIGRATE;
1144 login_info->async.va = vhost->async_crq.msg_token;
1145 login_info->async.len = vhost->async_crq.size * sizeof(*vhost->async_crq.msgs);
1146 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1147 strncpy(login_info->device_name,
1148 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
1150 location = of_get_property(of_node, "ibm,loc-code", NULL);
1151 location = location ? location : dev_name(vhost->dev);
1152 strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1156 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1157 * @vhost: ibmvfc host who owns the event pool
1159 * Returns zero on success.
1161 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1164 struct ibmvfc_event_pool *pool = &vhost->pool;
1167 pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1168 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1172 pool->iu_storage = dma_alloc_coherent(vhost->dev,
1173 pool->size * sizeof(*pool->iu_storage),
1174 &pool->iu_token, 0);
1176 if (!pool->iu_storage) {
1177 kfree(pool->events);
1181 for (i = 0; i < pool->size; ++i) {
1182 struct ibmvfc_event *evt = &pool->events[i];
1183 atomic_set(&evt->free, 1);
1184 evt->crq.valid = 0x80;
1185 evt->crq.ioba = pool->iu_token + (sizeof(*evt->xfer_iu) * i);
1186 evt->xfer_iu = pool->iu_storage + i;
1188 evt->ext_list = NULL;
1189 list_add_tail(&evt->queue, &vhost->free);
1197 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1198 * @vhost: ibmvfc host who owns the event pool
1201 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1204 struct ibmvfc_event_pool *pool = &vhost->pool;
1207 for (i = 0; i < pool->size; ++i) {
1208 list_del(&pool->events[i].queue);
1209 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1210 if (pool->events[i].ext_list)
1211 dma_pool_free(vhost->sg_pool,
1212 pool->events[i].ext_list,
1213 pool->events[i].ext_list_token);
1216 kfree(pool->events);
1217 dma_free_coherent(vhost->dev,
1218 pool->size * sizeof(*pool->iu_storage),
1219 pool->iu_storage, pool->iu_token);
1224 * ibmvfc_get_event - Gets the next free event in pool
1225 * @vhost: ibmvfc host struct
1227 * Returns a free event from the pool.
1229 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1231 struct ibmvfc_event *evt;
1233 BUG_ON(list_empty(&vhost->free));
1234 evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1235 atomic_set(&evt->free, 0);
1236 list_del(&evt->queue);
1241 * ibmvfc_init_event - Initialize fields in an event struct that are always
1244 * @done: Routine to call when the event is responded to
1245 * @format: SRP or MAD format
1247 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1248 void (*done) (struct ibmvfc_event *), u8 format)
1251 evt->sync_iu = NULL;
1252 evt->crq.format = format;
1254 evt->eh_comp = NULL;
1258 * ibmvfc_map_sg_list - Initialize scatterlist
1259 * @scmd: scsi command struct
1260 * @nseg: number of scatterlist segments
1261 * @md: memory descriptor list to initialize
1263 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1264 struct srp_direct_buf *md)
1267 struct scatterlist *sg;
1269 scsi_for_each_sg(scmd, sg, nseg, i) {
1270 md[i].va = sg_dma_address(sg);
1271 md[i].len = sg_dma_len(sg);
1277 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1278 * @scmd: Scsi_Cmnd with the scatterlist
1279 * @evt: ibmvfc event struct
1280 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1281 * @dev: device for which to map dma memory
1284 * 0 on success / non-zero on failure
1286 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1287 struct ibmvfc_event *evt,
1288 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1292 struct srp_direct_buf *data = &vfc_cmd->ioba;
1293 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1295 sg_mapped = scsi_dma_map(scmd);
1297 vfc_cmd->flags |= IBMVFC_NO_MEM_DESC;
1299 } else if (unlikely(sg_mapped < 0)) {
1300 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1301 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1305 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1306 vfc_cmd->flags |= IBMVFC_WRITE;
1307 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1309 vfc_cmd->flags |= IBMVFC_READ;
1310 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1313 if (sg_mapped == 1) {
1314 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1318 vfc_cmd->flags |= IBMVFC_SCATTERLIST;
1320 if (!evt->ext_list) {
1321 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1322 &evt->ext_list_token);
1324 if (!evt->ext_list) {
1325 scsi_dma_unmap(scmd);
1326 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1327 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1332 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1334 data->va = evt->ext_list_token;
1335 data->len = sg_mapped * sizeof(struct srp_direct_buf);
1341 * ibmvfc_timeout - Internal command timeout handler
1342 * @evt: struct ibmvfc_event that timed out
1344 * Called when an internally generated command times out
1346 static void ibmvfc_timeout(struct ibmvfc_event *evt)
1348 struct ibmvfc_host *vhost = evt->vhost;
1349 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1350 ibmvfc_reset_host(vhost);
1354 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1355 * @evt: event to be sent
1356 * @vhost: ibmvfc host struct
1357 * @timeout: timeout in seconds - 0 means do not time command
1359 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1361 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1362 struct ibmvfc_host *vhost, unsigned long timeout)
1364 u64 *crq_as_u64 = (u64 *) &evt->crq;
1367 /* Copy the IU into the transfer area */
1368 *evt->xfer_iu = evt->iu;
1369 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1370 evt->xfer_iu->cmd.tag = (u64)evt;
1371 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1372 evt->xfer_iu->mad_common.tag = (u64)evt;
1376 list_add_tail(&evt->queue, &vhost->sent);
1377 init_timer(&evt->timer);
1380 evt->timer.data = (unsigned long) evt;
1381 evt->timer.expires = jiffies + (timeout * HZ);
1382 evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
1383 add_timer(&evt->timer);
1388 if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
1389 list_del(&evt->queue);
1390 del_timer(&evt->timer);
1392 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1393 * Firmware will send a CRQ with a transport event (0xFF) to
1394 * tell this client what has happened to the transport. This
1395 * will be handled in ibmvfc_handle_crq()
1397 if (rc == H_CLOSED) {
1398 if (printk_ratelimit())
1399 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1401 scsi_dma_unmap(evt->cmnd);
1402 ibmvfc_free_event(evt);
1403 return SCSI_MLQUEUE_HOST_BUSY;
1406 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1408 evt->cmnd->result = DID_ERROR << 16;
1409 evt->done = ibmvfc_scsi_eh_done;
1411 evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
1415 ibmvfc_trc_start(evt);
1421 * ibmvfc_log_error - Log an error for the failed command if appropriate
1422 * @evt: ibmvfc event to log
1425 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1427 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1428 struct ibmvfc_host *vhost = evt->vhost;
1429 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1430 struct scsi_cmnd *cmnd = evt->cmnd;
1431 const char *err = unknown_error;
1432 int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
1437 logerr = cmd_status[index].log;
1438 err = cmd_status[index].name;
1441 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1444 if (rsp->flags & FCP_RSP_LEN_VALID)
1445 rsp_code = rsp->data.info.rsp_code;
1447 scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
1448 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1449 cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
1450 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1454 * ibmvfc_scsi_done - Handle responses from commands
1455 * @evt: ibmvfc event to be handled
1457 * Used as a callback when sending scsi cmds.
1459 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1461 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1462 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1463 struct scsi_cmnd *cmnd = evt->cmnd;
1465 u32 sense_len = rsp->fcp_sense_len;
1468 if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
1469 scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
1470 else if (rsp->flags & FCP_RESID_UNDER)
1471 scsi_set_resid(cmnd, rsp->fcp_resid);
1473 scsi_set_resid(cmnd, 0);
1475 if (vfc_cmd->status) {
1476 cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1478 if (rsp->flags & FCP_RSP_LEN_VALID)
1479 rsp_len = rsp->fcp_rsp_len;
1480 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1481 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1482 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1483 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1484 if ((vfc_cmd->status & IBMVFC_VIOS_FAILURE) && (vfc_cmd->error == IBMVFC_PLOGI_REQUIRED))
1485 ibmvfc_reinit_host(evt->vhost);
1487 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1488 cmnd->result = (DID_ERROR << 16);
1490 ibmvfc_log_error(evt);
1493 if (!cmnd->result &&
1494 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1495 cmnd->result = (DID_ERROR << 16);
1497 scsi_dma_unmap(cmnd);
1498 cmnd->scsi_done(cmnd);
1502 complete(evt->eh_comp);
1504 ibmvfc_free_event(evt);
1508 * ibmvfc_host_chkready - Check if the host can accept commands
1509 * @vhost: struct ibmvfc host
1512 * 1 if host can accept command / 0 if not
1514 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1518 switch (vhost->state) {
1519 case IBMVFC_LINK_DEAD:
1520 case IBMVFC_HOST_OFFLINE:
1521 result = DID_NO_CONNECT << 16;
1524 case IBMVFC_INITIALIZING:
1526 case IBMVFC_LINK_DOWN:
1527 result = DID_REQUEUE << 16;
1538 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1539 * @cmnd: struct scsi_cmnd to be executed
1540 * @done: Callback function to be called when cmnd is completed
1543 * 0 on success / other on failure
1545 static int ibmvfc_queuecommand(struct scsi_cmnd *cmnd,
1546 void (*done) (struct scsi_cmnd *))
1548 struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1549 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1550 struct ibmvfc_cmd *vfc_cmd;
1551 struct ibmvfc_event *evt;
1555 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1556 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1562 cmnd->result = (DID_OK << 16);
1563 evt = ibmvfc_get_event(vhost);
1564 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1566 cmnd->scsi_done = done;
1567 vfc_cmd = &evt->iu.cmd;
1568 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1569 vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1570 vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
1571 vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
1572 vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
1573 vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
1574 vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
1575 vfc_cmd->tgt_scsi_id = rport->port_id;
1576 if ((rport->supported_classes & FC_COS_CLASS3) &&
1577 (fc_host_supported_classes(vhost->host) & FC_COS_CLASS3))
1578 vfc_cmd->flags = IBMVFC_CLASS_3_ERR;
1579 vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
1580 int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1581 memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1583 if (scsi_populate_tag_msg(cmnd, tag)) {
1584 vfc_cmd->task_tag = tag[1];
1586 case MSG_SIMPLE_TAG:
1587 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1590 vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
1592 case MSG_ORDERED_TAG:
1593 vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
1598 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1599 return ibmvfc_send_event(evt, vhost, 0);
1601 ibmvfc_free_event(evt);
1603 return SCSI_MLQUEUE_HOST_BUSY;
1605 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1606 scmd_printk(KERN_ERR, cmnd,
1607 "Failed to map DMA buffer for command. rc=%d\n", rc);
1609 cmnd->result = DID_ERROR << 16;
1615 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1616 * @evt: ibmvfc event struct
1619 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1621 /* copy the response back */
1623 *evt->sync_iu = *evt->xfer_iu;
1625 complete(&evt->comp);
1629 * ibmvfc_reset_device - Reset the device with the specified reset type
1630 * @sdev: scsi device to reset
1632 * @desc: reset type description for log messages
1635 * 0 on success / other on failure
1637 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1639 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1640 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1641 struct ibmvfc_cmd *tmf;
1642 struct ibmvfc_event *evt = NULL;
1643 union ibmvfc_iu rsp_iu;
1644 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1645 int rsp_rc = -EBUSY;
1646 unsigned long flags;
1649 spin_lock_irqsave(vhost->host->host_lock, flags);
1650 if (vhost->state == IBMVFC_ACTIVE) {
1651 evt = ibmvfc_get_event(vhost);
1652 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1655 memset(tmf, 0, sizeof(*tmf));
1656 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1657 tmf->resp.len = sizeof(tmf->rsp);
1658 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1659 tmf->payload_len = sizeof(tmf->iu);
1660 tmf->resp_len = sizeof(tmf->rsp);
1661 tmf->cancel_key = (unsigned long)sdev->hostdata;
1662 tmf->tgt_scsi_id = rport->port_id;
1663 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1664 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1665 tmf->iu.tmf_flags = type;
1666 evt->sync_iu = &rsp_iu;
1668 init_completion(&evt->comp);
1669 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1671 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1674 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
1679 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
1680 wait_for_completion(&evt->comp);
1682 if (rsp_iu.cmd.status) {
1683 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1684 rsp_code = fc_rsp->data.info.rsp_code;
1686 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
1687 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1688 desc, ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1689 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1690 fc_rsp->scsi_status);
1693 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
1695 spin_lock_irqsave(vhost->host->host_lock, flags);
1696 ibmvfc_free_event(evt);
1697 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1702 * ibmvfc_abort_task_set - Abort outstanding commands to the device
1703 * @sdev: scsi device to abort commands
1705 * This sends an Abort Task Set to the VIOS for the specified device. This does
1706 * NOT send any cancel to the VIOS. That must be done separately.
1709 * 0 on success / other on failure
1711 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
1713 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1714 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1715 struct ibmvfc_cmd *tmf;
1716 struct ibmvfc_event *evt, *found_evt;
1717 union ibmvfc_iu rsp_iu;
1718 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1719 int rsp_rc = -EBUSY;
1720 unsigned long flags;
1723 spin_lock_irqsave(vhost->host->host_lock, flags);
1725 list_for_each_entry(evt, &vhost->sent, queue) {
1726 if (evt->cmnd && evt->cmnd->device == sdev) {
1733 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1734 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
1735 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1739 if (vhost->state == IBMVFC_ACTIVE) {
1740 evt = ibmvfc_get_event(vhost);
1741 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1744 memset(tmf, 0, sizeof(*tmf));
1745 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1746 tmf->resp.len = sizeof(tmf->rsp);
1747 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1748 tmf->payload_len = sizeof(tmf->iu);
1749 tmf->resp_len = sizeof(tmf->rsp);
1750 tmf->cancel_key = (unsigned long)sdev->hostdata;
1751 tmf->tgt_scsi_id = rport->port_id;
1752 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1753 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1754 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
1755 evt->sync_iu = &rsp_iu;
1757 init_completion(&evt->comp);
1758 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1761 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1764 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
1768 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
1769 wait_for_completion(&evt->comp);
1771 if (rsp_iu.cmd.status) {
1772 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1773 rsp_code = fc_rsp->data.info.rsp_code;
1775 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
1776 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1777 ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1778 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1779 fc_rsp->scsi_status);
1782 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
1784 spin_lock_irqsave(vhost->host->host_lock, flags);
1785 ibmvfc_free_event(evt);
1786 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1791 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
1792 * @sdev: scsi device to cancel commands
1793 * @type: type of error recovery being performed
1795 * This sends a cancel to the VIOS for the specified device. This does
1796 * NOT send any abort to the actual device. That must be done separately.
1799 * 0 on success / other on failure
1801 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
1803 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1804 struct scsi_target *starget = scsi_target(sdev);
1805 struct fc_rport *rport = starget_to_rport(starget);
1806 struct ibmvfc_tmf *tmf;
1807 struct ibmvfc_event *evt, *found_evt;
1808 union ibmvfc_iu rsp;
1809 int rsp_rc = -EBUSY;
1810 unsigned long flags;
1814 spin_lock_irqsave(vhost->host->host_lock, flags);
1816 list_for_each_entry(evt, &vhost->sent, queue) {
1817 if (evt->cmnd && evt->cmnd->device == sdev) {
1824 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1825 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
1826 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1830 if (vhost->state == IBMVFC_ACTIVE) {
1831 evt = ibmvfc_get_event(vhost);
1832 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1835 memset(tmf, 0, sizeof(*tmf));
1836 tmf->common.version = 1;
1837 tmf->common.opcode = IBMVFC_TMF_MAD;
1838 tmf->common.length = sizeof(*tmf);
1839 tmf->scsi_id = rport->port_id;
1840 int_to_scsilun(sdev->lun, &tmf->lun);
1841 tmf->flags = (type | IBMVFC_TMF_LUA_VALID);
1842 tmf->cancel_key = (unsigned long)sdev->hostdata;
1843 tmf->my_cancel_key = (unsigned long)starget->hostdata;
1845 evt->sync_iu = &rsp;
1846 init_completion(&evt->comp);
1847 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1850 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1853 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
1857 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
1859 wait_for_completion(&evt->comp);
1860 status = rsp.mad_common.status;
1861 spin_lock_irqsave(vhost->host->host_lock, flags);
1862 ibmvfc_free_event(evt);
1863 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1865 if (status != IBMVFC_MAD_SUCCESS) {
1866 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
1870 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
1875 * ibmvfc_match_target - Match function for specified target
1876 * @evt: ibmvfc event struct
1877 * @device: device to match (starget)
1880 * 1 if event matches starget / 0 if event does not match starget
1882 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
1884 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
1890 * ibmvfc_match_lun - Match function for specified LUN
1891 * @evt: ibmvfc event struct
1892 * @device: device to match (sdev)
1895 * 1 if event matches sdev / 0 if event does not match sdev
1897 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
1899 if (evt->cmnd && evt->cmnd->device == device)
1905 * ibmvfc_wait_for_ops - Wait for ops to complete
1906 * @vhost: ibmvfc host struct
1907 * @device: device to match (starget or sdev)
1908 * @match: match function
1913 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
1914 int (*match) (struct ibmvfc_event *, void *))
1916 struct ibmvfc_event *evt;
1917 DECLARE_COMPLETION_ONSTACK(comp);
1919 unsigned long flags;
1920 signed long timeout = init_timeout * HZ;
1925 spin_lock_irqsave(vhost->host->host_lock, flags);
1926 list_for_each_entry(evt, &vhost->sent, queue) {
1927 if (match(evt, device)) {
1928 evt->eh_comp = ∁
1932 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1935 timeout = wait_for_completion_timeout(&comp, timeout);
1939 spin_lock_irqsave(vhost->host->host_lock, flags);
1940 list_for_each_entry(evt, &vhost->sent, queue) {
1941 if (match(evt, device)) {
1942 evt->eh_comp = NULL;
1946 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1948 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
1950 return wait ? FAILED : SUCCESS;
1960 * ibmvfc_eh_abort_handler - Abort a command
1961 * @cmd: scsi command to abort
1966 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
1968 struct scsi_device *sdev = cmd->device;
1969 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1970 int cancel_rc, abort_rc;
1974 ibmvfc_wait_while_resetting(vhost);
1975 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
1976 abort_rc = ibmvfc_abort_task_set(sdev);
1978 if (!cancel_rc && !abort_rc)
1979 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
1986 * ibmvfc_eh_device_reset_handler - Reset a single LUN
1987 * @cmd: scsi command struct
1992 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
1994 struct scsi_device *sdev = cmd->device;
1995 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1996 int cancel_rc, reset_rc;
2000 ibmvfc_wait_while_resetting(vhost);
2001 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2002 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2004 if (!cancel_rc && !reset_rc)
2005 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2012 * ibmvfc_dev_cancel_all - Device iterated cancel all function
2013 * @sdev: scsi device struct
2014 * @data: return code
2017 static void ibmvfc_dev_cancel_all(struct scsi_device *sdev, void *data)
2019 unsigned long *rc = data;
2020 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2024 * ibmvfc_dev_abort_all - Device iterated abort task set function
2025 * @sdev: scsi device struct
2026 * @data: return code
2029 static void ibmvfc_dev_abort_all(struct scsi_device *sdev, void *data)
2031 unsigned long *rc = data;
2032 *rc |= ibmvfc_abort_task_set(sdev);
2036 * ibmvfc_eh_target_reset_handler - Reset the target
2037 * @cmd: scsi command struct
2042 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2044 struct scsi_device *sdev = cmd->device;
2045 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2046 struct scsi_target *starget = scsi_target(sdev);
2049 unsigned long cancel_rc = 0;
2052 ibmvfc_wait_while_resetting(vhost);
2053 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
2054 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2056 if (!cancel_rc && !reset_rc)
2057 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2064 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2065 * @cmd: struct scsi_cmnd having problems
2068 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2071 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2073 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2074 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2075 return rc ? FAILED : SUCCESS;
2079 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2080 * @rport: rport struct
2085 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2087 struct scsi_target *starget = to_scsi_target(&rport->dev);
2088 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2089 struct ibmvfc_host *vhost = shost_priv(shost);
2090 unsigned long cancel_rc = 0;
2091 unsigned long abort_rc = 0;
2095 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
2096 starget_for_each_device(starget, &abort_rc, ibmvfc_dev_abort_all);
2098 if (!cancel_rc && !abort_rc)
2099 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2102 ibmvfc_issue_fc_host_lip(shost);
2106 static const struct {
2107 enum ibmvfc_async_event ae;
2110 { IBMVFC_AE_ELS_PLOGI, "PLOGI" },
2111 { IBMVFC_AE_ELS_LOGO, "LOGO" },
2112 { IBMVFC_AE_ELS_PRLO, "PRLO" },
2113 { IBMVFC_AE_SCN_NPORT, "N-Port SCN" },
2114 { IBMVFC_AE_SCN_GROUP, "Group SCN" },
2115 { IBMVFC_AE_SCN_DOMAIN, "Domain SCN" },
2116 { IBMVFC_AE_SCN_FABRIC, "Fabric SCN" },
2117 { IBMVFC_AE_LINK_UP, "Link Up" },
2118 { IBMVFC_AE_LINK_DOWN, "Link Down" },
2119 { IBMVFC_AE_LINK_DEAD, "Link Dead" },
2120 { IBMVFC_AE_HALT, "Halt" },
2121 { IBMVFC_AE_RESUME, "Resume" },
2122 { IBMVFC_AE_ADAPTER_FAILED, "Adapter Failed" },
2125 static const char *unknown_ae = "Unknown async";
2128 * ibmvfc_get_ae_desc - Get text description for async event
2132 static const char *ibmvfc_get_ae_desc(u64 ae)
2136 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2137 if (ae_desc[i].ae == ae)
2138 return ae_desc[i].desc;
2144 * ibmvfc_handle_async - Handle an async event from the adapter
2145 * @crq: crq to process
2146 * @vhost: ibmvfc host struct
2149 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2150 struct ibmvfc_host *vhost)
2152 const char *desc = ibmvfc_get_ae_desc(crq->event);
2154 ibmvfc_log(vhost, 3, "%s event received. scsi_id: %llx, wwpn: %llx,"
2155 " node_name: %llx\n", desc, crq->scsi_id, crq->wwpn, crq->node_name);
2157 switch (crq->event) {
2158 case IBMVFC_AE_LINK_UP:
2159 case IBMVFC_AE_RESUME:
2160 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2161 vhost->delay_init = 1;
2162 __ibmvfc_reset_host(vhost);
2164 case IBMVFC_AE_SCN_FABRIC:
2165 case IBMVFC_AE_SCN_DOMAIN:
2166 vhost->events_to_log |= IBMVFC_AE_RSCN;
2167 vhost->delay_init = 1;
2168 __ibmvfc_reset_host(vhost);
2170 case IBMVFC_AE_SCN_NPORT:
2171 case IBMVFC_AE_SCN_GROUP:
2172 vhost->events_to_log |= IBMVFC_AE_RSCN;
2173 case IBMVFC_AE_ELS_LOGO:
2174 case IBMVFC_AE_ELS_PRLO:
2175 case IBMVFC_AE_ELS_PLOGI:
2176 ibmvfc_reinit_host(vhost);
2178 case IBMVFC_AE_LINK_DOWN:
2179 case IBMVFC_AE_ADAPTER_FAILED:
2180 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2182 case IBMVFC_AE_LINK_DEAD:
2183 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2185 case IBMVFC_AE_HALT:
2186 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2189 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
2195 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2196 * @crq: Command/Response queue
2197 * @vhost: ibmvfc host struct
2200 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2203 struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
2205 switch (crq->valid) {
2206 case IBMVFC_CRQ_INIT_RSP:
2207 switch (crq->format) {
2208 case IBMVFC_CRQ_INIT:
2209 dev_info(vhost->dev, "Partner initialized\n");
2210 /* Send back a response */
2211 rc = ibmvfc_send_crq_init_complete(vhost);
2213 ibmvfc_init_host(vhost, 0);
2215 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2217 case IBMVFC_CRQ_INIT_COMPLETE:
2218 dev_info(vhost->dev, "Partner initialization complete\n");
2219 ibmvfc_init_host(vhost, 0);
2222 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2225 case IBMVFC_CRQ_XPORT_EVENT:
2226 vhost->state = IBMVFC_NO_CRQ;
2227 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2228 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2229 /* We need to re-setup the interpartition connection */
2230 dev_info(vhost->dev, "Re-enabling adapter\n");
2231 vhost->client_migrated = 1;
2232 ibmvfc_purge_requests(vhost, DID_REQUEUE);
2233 if ((rc = ibmvfc_reenable_crq_queue(vhost)) ||
2234 (rc = ibmvfc_send_crq_init(vhost))) {
2235 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2236 dev_err(vhost->dev, "Error after enable (rc=%ld)\n", rc);
2238 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2240 dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
2242 ibmvfc_purge_requests(vhost, DID_ERROR);
2243 if ((rc = ibmvfc_reset_crq(vhost)) ||
2244 (rc = ibmvfc_send_crq_init(vhost))) {
2245 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2246 dev_err(vhost->dev, "Error after reset (rc=%ld)\n", rc);
2248 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2251 case IBMVFC_CRQ_CMD_RSP:
2254 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2258 if (crq->format == IBMVFC_ASYNC_EVENT)
2261 /* The only kind of payload CRQs we should get are responses to
2262 * things we send. Make sure this response is to something we
2265 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2266 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
2271 if (unlikely(atomic_read(&evt->free))) {
2272 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
2277 del_timer(&evt->timer);
2278 list_del(&evt->queue);
2279 ibmvfc_trc_end(evt);
2284 * ibmvfc_scan_finished - Check if the device scan is done.
2285 * @shost: scsi host struct
2286 * @time: current elapsed time
2289 * 0 if scan is not done / 1 if scan is done
2291 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2293 unsigned long flags;
2294 struct ibmvfc_host *vhost = shost_priv(shost);
2297 spin_lock_irqsave(shost->host_lock, flags);
2298 if (time >= (init_timeout * HZ)) {
2299 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2300 "continuing initialization\n", init_timeout);
2304 if (vhost->state != IBMVFC_NO_CRQ && vhost->action == IBMVFC_HOST_ACTION_NONE)
2306 spin_unlock_irqrestore(shost->host_lock, flags);
2311 * ibmvfc_slave_alloc - Setup the device's task set value
2312 * @sdev: struct scsi_device device to configure
2314 * Set the device's task set value so that error handling works as
2318 * 0 on success / -ENXIO if device does not exist
2320 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2322 struct Scsi_Host *shost = sdev->host;
2323 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2324 struct ibmvfc_host *vhost = shost_priv(shost);
2325 unsigned long flags = 0;
2327 if (!rport || fc_remote_port_chkready(rport))
2330 spin_lock_irqsave(shost->host_lock, flags);
2331 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2332 spin_unlock_irqrestore(shost->host_lock, flags);
2337 * ibmvfc_target_alloc - Setup the target's task set value
2338 * @starget: struct scsi_target
2340 * Set the target's task set value so that error handling works as
2344 * 0 on success / -ENXIO if device does not exist
2346 static int ibmvfc_target_alloc(struct scsi_target *starget)
2348 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2349 struct ibmvfc_host *vhost = shost_priv(shost);
2350 unsigned long flags = 0;
2352 spin_lock_irqsave(shost->host_lock, flags);
2353 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2354 spin_unlock_irqrestore(shost->host_lock, flags);
2359 * ibmvfc_slave_configure - Configure the device
2360 * @sdev: struct scsi_device device to configure
2362 * Enable allow_restart for a device if it is a disk. Adjust the
2363 * queue_depth here also.
2368 static int ibmvfc_slave_configure(struct scsi_device *sdev)
2370 struct Scsi_Host *shost = sdev->host;
2371 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
2372 unsigned long flags = 0;
2374 spin_lock_irqsave(shost->host_lock, flags);
2375 if (sdev->type == TYPE_DISK)
2376 sdev->allow_restart = 1;
2378 if (sdev->tagged_supported) {
2379 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2380 scsi_activate_tcq(sdev, sdev->queue_depth);
2382 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2384 rport->dev_loss_tmo = dev_loss_tmo;
2385 spin_unlock_irqrestore(shost->host_lock, flags);
2390 * ibmvfc_change_queue_depth - Change the device's queue depth
2391 * @sdev: scsi device struct
2392 * @qdepth: depth to set
2397 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2399 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2400 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2402 scsi_adjust_queue_depth(sdev, 0, qdepth);
2403 return sdev->queue_depth;
2407 * ibmvfc_change_queue_type - Change the device's queue type
2408 * @sdev: scsi device struct
2409 * @tag_type: type of tags to use
2412 * actual queue type set
2414 static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
2416 if (sdev->tagged_supported) {
2417 scsi_set_tag_type(sdev, tag_type);
2420 scsi_activate_tcq(sdev, sdev->queue_depth);
2422 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2429 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2430 struct device_attribute *attr, char *buf)
2432 struct Scsi_Host *shost = class_to_shost(dev);
2433 struct ibmvfc_host *vhost = shost_priv(shost);
2435 return snprintf(buf, PAGE_SIZE, "%s\n",
2436 vhost->login_buf->resp.partition_name);
2439 static struct device_attribute ibmvfc_host_partition_name = {
2441 .name = "partition_name",
2444 .show = ibmvfc_show_host_partition_name,
2447 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2448 struct device_attribute *attr, char *buf)
2450 struct Scsi_Host *shost = class_to_shost(dev);
2451 struct ibmvfc_host *vhost = shost_priv(shost);
2453 return snprintf(buf, PAGE_SIZE, "%s\n",
2454 vhost->login_buf->resp.device_name);
2457 static struct device_attribute ibmvfc_host_device_name = {
2459 .name = "device_name",
2462 .show = ibmvfc_show_host_device_name,
2465 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2466 struct device_attribute *attr, char *buf)
2468 struct Scsi_Host *shost = class_to_shost(dev);
2469 struct ibmvfc_host *vhost = shost_priv(shost);
2471 return snprintf(buf, PAGE_SIZE, "%s\n",
2472 vhost->login_buf->resp.port_loc_code);
2475 static struct device_attribute ibmvfc_host_loc_code = {
2477 .name = "port_loc_code",
2480 .show = ibmvfc_show_host_loc_code,
2483 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2484 struct device_attribute *attr, char *buf)
2486 struct Scsi_Host *shost = class_to_shost(dev);
2487 struct ibmvfc_host *vhost = shost_priv(shost);
2489 return snprintf(buf, PAGE_SIZE, "%s\n",
2490 vhost->login_buf->resp.drc_name);
2493 static struct device_attribute ibmvfc_host_drc_name = {
2498 .show = ibmvfc_show_host_drc_name,
2501 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2502 struct device_attribute *attr, char *buf)
2504 struct Scsi_Host *shost = class_to_shost(dev);
2505 struct ibmvfc_host *vhost = shost_priv(shost);
2506 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2509 static struct device_attribute ibmvfc_host_npiv_version = {
2511 .name = "npiv_version",
2514 .show = ibmvfc_show_host_npiv_version,
2518 * ibmvfc_show_log_level - Show the adapter's error logging level
2519 * @dev: class device struct
2523 * number of bytes printed to buffer
2525 static ssize_t ibmvfc_show_log_level(struct device *dev,
2526 struct device_attribute *attr, char *buf)
2528 struct Scsi_Host *shost = class_to_shost(dev);
2529 struct ibmvfc_host *vhost = shost_priv(shost);
2530 unsigned long flags = 0;
2533 spin_lock_irqsave(shost->host_lock, flags);
2534 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2535 spin_unlock_irqrestore(shost->host_lock, flags);
2540 * ibmvfc_store_log_level - Change the adapter's error logging level
2541 * @dev: class device struct
2545 * number of bytes printed to buffer
2547 static ssize_t ibmvfc_store_log_level(struct device *dev,
2548 struct device_attribute *attr,
2549 const char *buf, size_t count)
2551 struct Scsi_Host *shost = class_to_shost(dev);
2552 struct ibmvfc_host *vhost = shost_priv(shost);
2553 unsigned long flags = 0;
2555 spin_lock_irqsave(shost->host_lock, flags);
2556 vhost->log_level = simple_strtoul(buf, NULL, 10);
2557 spin_unlock_irqrestore(shost->host_lock, flags);
2561 static struct device_attribute ibmvfc_log_level_attr = {
2563 .name = "log_level",
2564 .mode = S_IRUGO | S_IWUSR,
2566 .show = ibmvfc_show_log_level,
2567 .store = ibmvfc_store_log_level
2570 #ifdef CONFIG_SCSI_IBMVFC_TRACE
2572 * ibmvfc_read_trace - Dump the adapter trace
2573 * @kobj: kobject struct
2574 * @bin_attr: bin_attribute struct
2577 * @count: buffer size
2580 * number of bytes printed to buffer
2582 static ssize_t ibmvfc_read_trace(struct kobject *kobj,
2583 struct bin_attribute *bin_attr,
2584 char *buf, loff_t off, size_t count)
2586 struct device *dev = container_of(kobj, struct device, kobj);
2587 struct Scsi_Host *shost = class_to_shost(dev);
2588 struct ibmvfc_host *vhost = shost_priv(shost);
2589 unsigned long flags = 0;
2590 int size = IBMVFC_TRACE_SIZE;
2591 char *src = (char *)vhost->trace;
2595 if (off + count > size) {
2600 spin_lock_irqsave(shost->host_lock, flags);
2601 memcpy(buf, &src[off], count);
2602 spin_unlock_irqrestore(shost->host_lock, flags);
2606 static struct bin_attribute ibmvfc_trace_attr = {
2612 .read = ibmvfc_read_trace,
2616 static struct device_attribute *ibmvfc_attrs[] = {
2617 &ibmvfc_host_partition_name,
2618 &ibmvfc_host_device_name,
2619 &ibmvfc_host_loc_code,
2620 &ibmvfc_host_drc_name,
2621 &ibmvfc_host_npiv_version,
2622 &ibmvfc_log_level_attr,
2626 static struct scsi_host_template driver_template = {
2627 .module = THIS_MODULE,
2628 .name = "IBM POWER Virtual FC Adapter",
2629 .proc_name = IBMVFC_NAME,
2630 .queuecommand = ibmvfc_queuecommand,
2631 .eh_abort_handler = ibmvfc_eh_abort_handler,
2632 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
2633 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
2634 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
2635 .slave_alloc = ibmvfc_slave_alloc,
2636 .slave_configure = ibmvfc_slave_configure,
2637 .target_alloc = ibmvfc_target_alloc,
2638 .scan_finished = ibmvfc_scan_finished,
2639 .change_queue_depth = ibmvfc_change_queue_depth,
2640 .change_queue_type = ibmvfc_change_queue_type,
2642 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
2644 .sg_tablesize = SG_ALL,
2645 .max_sectors = IBMVFC_MAX_SECTORS,
2646 .use_clustering = ENABLE_CLUSTERING,
2647 .shost_attrs = ibmvfc_attrs,
2651 * ibmvfc_next_async_crq - Returns the next entry in async queue
2652 * @vhost: ibmvfc host struct
2655 * Pointer to next entry in queue / NULL if empty
2657 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
2659 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
2660 struct ibmvfc_async_crq *crq;
2662 crq = &async_crq->msgs[async_crq->cur];
2663 if (crq->valid & 0x80) {
2664 if (++async_crq->cur == async_crq->size)
2673 * ibmvfc_next_crq - Returns the next entry in message queue
2674 * @vhost: ibmvfc host struct
2677 * Pointer to next entry in queue / NULL if empty
2679 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
2681 struct ibmvfc_crq_queue *queue = &vhost->crq;
2682 struct ibmvfc_crq *crq;
2684 crq = &queue->msgs[queue->cur];
2685 if (crq->valid & 0x80) {
2686 if (++queue->cur == queue->size)
2695 * ibmvfc_interrupt - Interrupt handler
2696 * @irq: number of irq to handle, not used
2697 * @dev_instance: ibmvfc_host that received interrupt
2702 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
2704 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
2705 struct vio_dev *vdev = to_vio_dev(vhost->dev);
2706 struct ibmvfc_crq *crq;
2707 struct ibmvfc_async_crq *async;
2708 unsigned long flags;
2711 spin_lock_irqsave(vhost->host->host_lock, flags);
2712 vio_disable_interrupts(to_vio_dev(vhost->dev));
2714 /* Pull all the valid messages off the CRQ */
2715 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2716 ibmvfc_handle_crq(crq, vhost);
2720 /* Pull all the valid messages off the async CRQ */
2721 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2722 ibmvfc_handle_async(async, vhost);
2726 vio_enable_interrupts(vdev);
2727 if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2728 vio_disable_interrupts(vdev);
2729 ibmvfc_handle_crq(crq, vhost);
2731 } else if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2732 vio_disable_interrupts(vdev);
2733 ibmvfc_handle_async(async, vhost);
2739 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2744 * ibmvfc_init_tgt - Set the next init job step for the target
2745 * @tgt: ibmvfc target struct
2746 * @job_step: job step to perform
2749 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
2750 void (*job_step) (struct ibmvfc_target *))
2752 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
2753 tgt->job_step = job_step;
2754 wake_up(&tgt->vhost->work_wait_q);
2758 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
2759 * @tgt: ibmvfc target struct
2760 * @job_step: initialization job step
2763 static void ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
2764 void (*job_step) (struct ibmvfc_target *))
2766 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
2767 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2768 wake_up(&tgt->vhost->work_wait_q);
2770 ibmvfc_init_tgt(tgt, job_step);
2774 * ibmvfc_tgt_prli_done - Completion handler for Process Login
2775 * @evt: ibmvfc event struct
2778 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
2780 struct ibmvfc_target *tgt = evt->tgt;
2781 struct ibmvfc_host *vhost = evt->vhost;
2782 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
2783 u32 status = rsp->common.status;
2785 vhost->discovery_threads--;
2786 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2788 case IBMVFC_MAD_SUCCESS:
2789 tgt_dbg(tgt, "Process Login succeeded\n");
2790 tgt->need_login = 0;
2791 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_ADD_RPORT);
2793 case IBMVFC_MAD_DRIVER_FAILED:
2795 case IBMVFC_MAD_CRQ_ERROR:
2796 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2798 case IBMVFC_MAD_FAILED:
2800 tgt_err(tgt, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
2801 ibmvfc_get_cmd_error(rsp->status, rsp->error),
2802 rsp->status, rsp->error, status);
2803 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2804 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2806 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2810 kref_put(&tgt->kref, ibmvfc_release_tgt);
2811 ibmvfc_free_event(evt);
2812 wake_up(&vhost->work_wait_q);
2816 * ibmvfc_tgt_send_prli - Send a process login
2817 * @tgt: ibmvfc target struct
2820 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
2822 struct ibmvfc_process_login *prli;
2823 struct ibmvfc_host *vhost = tgt->vhost;
2824 struct ibmvfc_event *evt;
2826 if (vhost->discovery_threads >= disc_threads)
2829 kref_get(&tgt->kref);
2830 evt = ibmvfc_get_event(vhost);
2831 vhost->discovery_threads++;
2832 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
2834 prli = &evt->iu.prli;
2835 memset(prli, 0, sizeof(*prli));
2836 prli->common.version = 1;
2837 prli->common.opcode = IBMVFC_PROCESS_LOGIN;
2838 prli->common.length = sizeof(*prli);
2839 prli->scsi_id = tgt->scsi_id;
2841 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
2842 prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
2843 prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
2845 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2846 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2847 vhost->discovery_threads--;
2848 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2849 kref_put(&tgt->kref, ibmvfc_release_tgt);
2851 tgt_dbg(tgt, "Sent process login\n");
2855 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
2856 * @evt: ibmvfc event struct
2859 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
2861 struct ibmvfc_target *tgt = evt->tgt;
2862 struct ibmvfc_host *vhost = evt->vhost;
2863 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
2864 u32 status = rsp->common.status;
2866 vhost->discovery_threads--;
2867 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2869 case IBMVFC_MAD_SUCCESS:
2870 tgt_dbg(tgt, "Port Login succeeded\n");
2871 if (tgt->ids.port_name &&
2872 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
2874 tgt_dbg(tgt, "Port re-init required\n");
2877 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
2878 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
2879 tgt->ids.port_id = tgt->scsi_id;
2880 tgt->ids.roles = FC_PORT_ROLE_FCP_TARGET;
2881 memcpy(&tgt->service_parms, &rsp->service_parms,
2882 sizeof(tgt->service_parms));
2883 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
2884 sizeof(tgt->service_parms_change));
2885 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
2887 case IBMVFC_MAD_DRIVER_FAILED:
2889 case IBMVFC_MAD_CRQ_ERROR:
2890 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2892 case IBMVFC_MAD_FAILED:
2894 tgt_err(tgt, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
2895 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
2896 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
2897 ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
2899 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2900 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2902 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2906 kref_put(&tgt->kref, ibmvfc_release_tgt);
2907 ibmvfc_free_event(evt);
2908 wake_up(&vhost->work_wait_q);
2912 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
2913 * @tgt: ibmvfc target struct
2916 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
2918 struct ibmvfc_port_login *plogi;
2919 struct ibmvfc_host *vhost = tgt->vhost;
2920 struct ibmvfc_event *evt;
2922 if (vhost->discovery_threads >= disc_threads)
2925 kref_get(&tgt->kref);
2926 evt = ibmvfc_get_event(vhost);
2927 vhost->discovery_threads++;
2928 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2929 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
2931 plogi = &evt->iu.plogi;
2932 memset(plogi, 0, sizeof(*plogi));
2933 plogi->common.version = 1;
2934 plogi->common.opcode = IBMVFC_PORT_LOGIN;
2935 plogi->common.length = sizeof(*plogi);
2936 plogi->scsi_id = tgt->scsi_id;
2938 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2939 vhost->discovery_threads--;
2940 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2941 kref_put(&tgt->kref, ibmvfc_release_tgt);
2943 tgt_dbg(tgt, "Sent port login\n");
2947 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
2948 * @evt: ibmvfc event struct
2951 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
2953 struct ibmvfc_target *tgt = evt->tgt;
2954 struct ibmvfc_host *vhost = evt->vhost;
2955 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
2956 u32 status = rsp->common.status;
2958 vhost->discovery_threads--;
2959 ibmvfc_free_event(evt);
2960 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2963 case IBMVFC_MAD_SUCCESS:
2964 tgt_dbg(tgt, "Implicit Logout succeeded\n");
2966 case IBMVFC_MAD_DRIVER_FAILED:
2967 kref_put(&tgt->kref, ibmvfc_release_tgt);
2968 wake_up(&vhost->work_wait_q);
2970 case IBMVFC_MAD_FAILED:
2972 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
2976 if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
2977 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
2978 else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
2979 tgt->scsi_id != tgt->new_scsi_id)
2980 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2981 kref_put(&tgt->kref, ibmvfc_release_tgt);
2982 wake_up(&vhost->work_wait_q);
2986 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
2987 * @tgt: ibmvfc target struct
2990 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
2992 struct ibmvfc_implicit_logout *mad;
2993 struct ibmvfc_host *vhost = tgt->vhost;
2994 struct ibmvfc_event *evt;
2996 if (vhost->discovery_threads >= disc_threads)
2999 kref_get(&tgt->kref);
3000 evt = ibmvfc_get_event(vhost);
3001 vhost->discovery_threads++;
3002 ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
3004 mad = &evt->iu.implicit_logout;
3005 memset(mad, 0, sizeof(*mad));
3006 mad->common.version = 1;
3007 mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
3008 mad->common.length = sizeof(*mad);
3009 mad->old_scsi_id = tgt->scsi_id;
3011 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3012 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3013 vhost->discovery_threads--;
3014 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3015 kref_put(&tgt->kref, ibmvfc_release_tgt);
3017 tgt_dbg(tgt, "Sent Implicit Logout\n");
3021 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3022 * @mad: ibmvfc passthru mad struct
3023 * @tgt: ibmvfc target struct
3026 * 1 if PLOGI needed / 0 if PLOGI not needed
3028 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3029 struct ibmvfc_target *tgt)
3031 if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
3032 sizeof(tgt->ids.port_name)))
3034 if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
3035 sizeof(tgt->ids.node_name)))
3037 if (mad->fc_iu.response[6] != tgt->scsi_id)
3043 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3044 * @evt: ibmvfc event struct
3047 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3049 struct ibmvfc_target *tgt = evt->tgt;
3050 struct ibmvfc_host *vhost = evt->vhost;
3051 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3052 u32 status = mad->common.status;
3053 u8 fc_reason, fc_explain;
3055 vhost->discovery_threads--;
3056 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3059 case IBMVFC_MAD_SUCCESS:
3060 tgt_dbg(tgt, "ADISC succeeded\n");
3061 if (ibmvfc_adisc_needs_plogi(mad, tgt))
3062 tgt->need_login = 1;
3064 case IBMVFC_MAD_DRIVER_FAILED:
3066 case IBMVFC_MAD_FAILED:
3068 tgt->need_login = 1;
3069 fc_reason = (mad->fc_iu.response[1] & 0x00ff0000) >> 16;
3070 fc_explain = (mad->fc_iu.response[1] & 0x0000ff00) >> 8;
3071 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3072 ibmvfc_get_cmd_error(mad->iu.status, mad->iu.error),
3073 mad->iu.status, mad->iu.error,
3074 ibmvfc_get_fc_type(fc_reason), fc_reason,
3075 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3079 kref_put(&tgt->kref, ibmvfc_release_tgt);
3080 ibmvfc_free_event(evt);
3081 wake_up(&vhost->work_wait_q);
3085 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3086 * @evt: ibmvfc event struct
3089 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3091 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3093 memset(mad, 0, sizeof(*mad));
3094 mad->common.version = 1;
3095 mad->common.opcode = IBMVFC_PASSTHRU;
3096 mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
3097 mad->cmd_ioba.va = (u64)evt->crq.ioba +
3098 offsetof(struct ibmvfc_passthru_mad, iu);
3099 mad->cmd_ioba.len = sizeof(mad->iu);
3100 mad->iu.cmd_len = sizeof(mad->fc_iu.payload);
3101 mad->iu.rsp_len = sizeof(mad->fc_iu.response);
3102 mad->iu.cmd.va = (u64)evt->crq.ioba +
3103 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3104 offsetof(struct ibmvfc_passthru_fc_iu, payload);
3105 mad->iu.cmd.len = sizeof(mad->fc_iu.payload);
3106 mad->iu.rsp.va = (u64)evt->crq.ioba +
3107 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3108 offsetof(struct ibmvfc_passthru_fc_iu, response);
3109 mad->iu.rsp.len = sizeof(mad->fc_iu.response);
3113 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3114 * @tgt: ibmvfc target struct
3117 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3119 struct ibmvfc_passthru_mad *mad;
3120 struct ibmvfc_host *vhost = tgt->vhost;
3121 struct ibmvfc_event *evt;
3123 if (vhost->discovery_threads >= disc_threads)
3126 kref_get(&tgt->kref);
3127 evt = ibmvfc_get_event(vhost);
3128 vhost->discovery_threads++;
3129 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3132 ibmvfc_init_passthru(evt);
3133 mad = &evt->iu.passthru;
3134 mad->iu.flags = IBMVFC_FC_ELS;
3135 mad->iu.scsi_id = tgt->scsi_id;
3137 mad->fc_iu.payload[0] = IBMVFC_ADISC;
3138 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3139 sizeof(vhost->login_buf->resp.port_name));
3140 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3141 sizeof(vhost->login_buf->resp.node_name));
3142 mad->fc_iu.payload[6] = vhost->login_buf->resp.scsi_id & 0x00ffffff;
3144 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3145 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3146 vhost->discovery_threads--;
3147 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3148 kref_put(&tgt->kref, ibmvfc_release_tgt);
3150 tgt_dbg(tgt, "Sent ADISC\n");
3154 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3155 * @evt: ibmvfc event struct
3158 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3160 struct ibmvfc_target *tgt = evt->tgt;
3161 struct ibmvfc_host *vhost = evt->vhost;
3162 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3163 u32 status = rsp->common.status;
3165 vhost->discovery_threads--;
3166 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3168 case IBMVFC_MAD_SUCCESS:
3169 tgt_dbg(tgt, "Query Target succeeded\n");
3170 tgt->new_scsi_id = rsp->scsi_id;
3171 if (rsp->scsi_id != tgt->scsi_id)
3172 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3174 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
3176 case IBMVFC_MAD_DRIVER_FAILED:
3178 case IBMVFC_MAD_CRQ_ERROR:
3179 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3181 case IBMVFC_MAD_FAILED:
3183 tgt_err(tgt, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3184 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
3185 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
3186 ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
3188 if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3189 rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3190 rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
3191 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3192 else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3193 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3195 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3199 kref_put(&tgt->kref, ibmvfc_release_tgt);
3200 ibmvfc_free_event(evt);
3201 wake_up(&vhost->work_wait_q);
3205 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3206 * @tgt: ibmvfc target struct
3209 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3211 struct ibmvfc_query_tgt *query_tgt;
3212 struct ibmvfc_host *vhost = tgt->vhost;
3213 struct ibmvfc_event *evt;
3215 if (vhost->discovery_threads >= disc_threads)
3218 kref_get(&tgt->kref);
3219 evt = ibmvfc_get_event(vhost);
3220 vhost->discovery_threads++;
3222 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3223 query_tgt = &evt->iu.query_tgt;
3224 memset(query_tgt, 0, sizeof(*query_tgt));
3225 query_tgt->common.version = 1;
3226 query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
3227 query_tgt->common.length = sizeof(*query_tgt);
3228 query_tgt->wwpn = tgt->ids.port_name;
3230 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3231 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3232 vhost->discovery_threads--;
3233 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3234 kref_put(&tgt->kref, ibmvfc_release_tgt);
3236 tgt_dbg(tgt, "Sent Query Target\n");
3240 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3241 * @vhost: ibmvfc host struct
3242 * @scsi_id: SCSI ID to allocate target for
3245 * 0 on success / other on failure
3247 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3249 struct ibmvfc_target *tgt;
3250 unsigned long flags;
3252 spin_lock_irqsave(vhost->host->host_lock, flags);
3253 list_for_each_entry(tgt, &vhost->targets, queue) {
3254 if (tgt->scsi_id == scsi_id) {
3255 if (tgt->need_login)
3256 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3260 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3262 tgt = mempool_alloc(vhost->tgt_pool, GFP_KERNEL);
3264 dev_err(vhost->dev, "Target allocation failure for scsi id %08llx\n",
3269 tgt->scsi_id = scsi_id;
3270 tgt->new_scsi_id = scsi_id;
3272 tgt->need_login = 1;
3273 kref_init(&tgt->kref);
3274 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3275 spin_lock_irqsave(vhost->host->host_lock, flags);
3276 list_add_tail(&tgt->queue, &vhost->targets);
3279 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3284 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3285 * @vhost: ibmvfc host struct
3288 * 0 on success / other on failure
3290 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3294 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3295 rc = ibmvfc_alloc_target(vhost,
3296 vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
3302 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3303 * @evt: ibmvfc event struct
3306 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3308 struct ibmvfc_host *vhost = evt->vhost;
3309 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3310 u32 mad_status = rsp->common.status;
3312 switch (mad_status) {
3313 case IBMVFC_MAD_SUCCESS:
3314 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3315 vhost->num_targets = rsp->num_written;
3316 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3318 case IBMVFC_MAD_FAILED:
3319 dev_err(vhost->dev, "Discover Targets failed: %s (%x:%x)\n",
3320 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3321 ibmvfc_retry_host_init(vhost);
3323 case IBMVFC_MAD_DRIVER_FAILED:
3326 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3327 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3331 ibmvfc_free_event(evt);
3332 wake_up(&vhost->work_wait_q);
3336 * ibmvfc_discover_targets - Send Discover Targets MAD
3337 * @vhost: ibmvfc host struct
3340 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3342 struct ibmvfc_discover_targets *mad;
3343 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3345 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3346 mad = &evt->iu.discover_targets;
3347 memset(mad, 0, sizeof(*mad));
3348 mad->common.version = 1;
3349 mad->common.opcode = IBMVFC_DISC_TARGETS;
3350 mad->common.length = sizeof(*mad);
3351 mad->bufflen = vhost->disc_buf_sz;
3352 mad->buffer.va = vhost->disc_buf_dma;
3353 mad->buffer.len = vhost->disc_buf_sz;
3354 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3356 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3357 ibmvfc_dbg(vhost, "Sent discover targets\n");
3359 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3363 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3364 * @evt: ibmvfc event struct
3367 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
3369 struct ibmvfc_host *vhost = evt->vhost;
3370 u32 mad_status = evt->xfer_iu->npiv_login.common.status;
3371 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
3372 unsigned int npiv_max_sectors;
3374 switch (mad_status) {
3375 case IBMVFC_MAD_SUCCESS:
3376 ibmvfc_free_event(evt);
3378 case IBMVFC_MAD_FAILED:
3379 dev_err(vhost->dev, "NPIV Login failed: %s (%x:%x)\n",
3380 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3381 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3382 ibmvfc_retry_host_init(vhost);
3384 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3385 ibmvfc_free_event(evt);
3387 case IBMVFC_MAD_CRQ_ERROR:
3388 ibmvfc_retry_host_init(vhost);
3389 case IBMVFC_MAD_DRIVER_FAILED:
3390 ibmvfc_free_event(evt);
3393 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
3394 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3395 ibmvfc_free_event(evt);
3399 vhost->client_migrated = 0;
3401 if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
3402 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
3404 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3405 wake_up(&vhost->work_wait_q);
3409 if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
3410 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
3412 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3413 wake_up(&vhost->work_wait_q);
3417 npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
3418 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
3419 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
3420 rsp->drc_name, npiv_max_sectors);
3422 fc_host_fabric_name(vhost->host) = rsp->node_name;
3423 fc_host_node_name(vhost->host) = rsp->node_name;
3424 fc_host_port_name(vhost->host) = rsp->port_name;
3425 fc_host_port_id(vhost->host) = rsp->scsi_id;
3426 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
3427 fc_host_supported_classes(vhost->host) = 0;
3428 if (rsp->service_parms.class1_parms[0] & 0x80000000)
3429 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
3430 if (rsp->service_parms.class2_parms[0] & 0x80000000)
3431 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
3432 if (rsp->service_parms.class3_parms[0] & 0x80000000)
3433 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
3434 fc_host_maxframe_size(vhost->host) =
3435 rsp->service_parms.common.bb_rcv_sz & 0x0fff;
3437 vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
3438 vhost->host->max_sectors = npiv_max_sectors;
3439 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3440 wake_up(&vhost->work_wait_q);
3444 * ibmvfc_npiv_login - Sends NPIV login
3445 * @vhost: ibmvfc host struct
3448 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
3450 struct ibmvfc_npiv_login_mad *mad;
3451 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3453 ibmvfc_gather_partition_info(vhost);
3454 ibmvfc_set_login_info(vhost);
3455 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
3457 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
3458 mad = &evt->iu.npiv_login;
3459 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
3460 mad->common.version = 1;
3461 mad->common.opcode = IBMVFC_NPIV_LOGIN;
3462 mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
3463 mad->buffer.va = vhost->login_buf_dma;
3464 mad->buffer.len = sizeof(*vhost->login_buf);
3466 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3468 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3469 ibmvfc_dbg(vhost, "Sent NPIV login\n");
3471 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3475 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
3476 * @vhost: ibmvfc host struct
3479 * 1 if work to do / 0 if not
3481 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
3483 struct ibmvfc_target *tgt;
3485 list_for_each_entry(tgt, &vhost->targets, queue) {
3486 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
3487 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3495 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
3496 * @vhost: ibmvfc host struct
3499 * 1 if work to do / 0 if not
3501 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3503 struct ibmvfc_target *tgt;
3505 if (kthread_should_stop())
3507 switch (vhost->action) {
3508 case IBMVFC_HOST_ACTION_NONE:
3509 case IBMVFC_HOST_ACTION_INIT_WAIT:
3511 case IBMVFC_HOST_ACTION_TGT_INIT:
3512 case IBMVFC_HOST_ACTION_QUERY_TGTS:
3513 if (vhost->discovery_threads == disc_threads)
3515 list_for_each_entry(tgt, &vhost->targets, queue)
3516 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
3518 list_for_each_entry(tgt, &vhost->targets, queue)
3519 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3522 case IBMVFC_HOST_ACTION_INIT:
3523 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3524 case IBMVFC_HOST_ACTION_TGT_ADD:
3525 case IBMVFC_HOST_ACTION_TGT_DEL:
3526 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
3527 case IBMVFC_HOST_ACTION_QUERY:
3536 * ibmvfc_work_to_do - Is there task level work to do?
3537 * @vhost: ibmvfc host struct
3540 * 1 if work to do / 0 if not
3542 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3544 unsigned long flags;
3547 spin_lock_irqsave(vhost->host->host_lock, flags);
3548 rc = __ibmvfc_work_to_do(vhost);
3549 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3554 * ibmvfc_log_ae - Log async events if necessary
3555 * @vhost: ibmvfc host struct
3556 * @events: events to log
3559 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
3561 if (events & IBMVFC_AE_RSCN)
3562 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
3563 if ((events & IBMVFC_AE_LINKDOWN) &&
3564 vhost->state >= IBMVFC_HALTED)
3565 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
3566 if ((events & IBMVFC_AE_LINKUP) &&
3567 vhost->state == IBMVFC_INITIALIZING)
3568 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
3572 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
3573 * @tgt: ibmvfc target struct
3576 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
3578 struct ibmvfc_host *vhost = tgt->vhost;
3579 struct fc_rport *rport;
3580 unsigned long flags;
3582 tgt_dbg(tgt, "Adding rport\n");
3583 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
3584 spin_lock_irqsave(vhost->host->host_lock, flags);
3586 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3588 tgt_dbg(tgt, "rport add succeeded\n");
3589 rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
3590 rport->supported_classes = 0;
3591 tgt->target_id = rport->scsi_target_id;
3592 if (tgt->service_parms.class1_parms[0] & 0x80000000)
3593 rport->supported_classes |= FC_COS_CLASS1;
3594 if (tgt->service_parms.class2_parms[0] & 0x80000000)
3595 rport->supported_classes |= FC_COS_CLASS2;
3596 if (tgt->service_parms.class3_parms[0] & 0x80000000)
3597 rport->supported_classes |= FC_COS_CLASS3;
3599 tgt_dbg(tgt, "rport add failed\n");
3600 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3604 * ibmvfc_do_work - Do task level work
3605 * @vhost: ibmvfc host struct
3608 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
3610 struct ibmvfc_target *tgt;
3611 unsigned long flags;
3612 struct fc_rport *rport;
3614 ibmvfc_log_ae(vhost, vhost->events_to_log);
3615 spin_lock_irqsave(vhost->host->host_lock, flags);
3616 vhost->events_to_log = 0;
3617 switch (vhost->action) {
3618 case IBMVFC_HOST_ACTION_NONE:
3619 case IBMVFC_HOST_ACTION_INIT_WAIT:
3621 case IBMVFC_HOST_ACTION_INIT:
3622 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
3623 if (vhost->delay_init) {
3624 vhost->delay_init = 0;
3625 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3629 vhost->job_step(vhost);
3631 case IBMVFC_HOST_ACTION_QUERY:
3632 list_for_each_entry(tgt, &vhost->targets, queue)
3633 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
3634 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
3636 case IBMVFC_HOST_ACTION_QUERY_TGTS:
3637 list_for_each_entry(tgt, &vhost->targets, queue) {
3638 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3644 if (!ibmvfc_dev_init_to_do(vhost))
3645 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
3647 case IBMVFC_HOST_ACTION_TGT_DEL:
3648 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
3649 list_for_each_entry(tgt, &vhost->targets, queue) {
3650 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
3651 tgt_dbg(tgt, "Deleting rport\n");
3654 list_del(&tgt->queue);
3655 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3657 fc_remote_port_delete(rport);
3658 kref_put(&tgt->kref, ibmvfc_release_tgt);
3663 if (vhost->state == IBMVFC_INITIALIZING) {
3664 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
3665 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
3666 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_ADD);
3667 vhost->init_retries = 0;
3668 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3669 scsi_unblock_requests(vhost->host);
3672 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
3673 vhost->job_step = ibmvfc_discover_targets;
3676 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3677 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3678 scsi_unblock_requests(vhost->host);
3679 wake_up(&vhost->init_wait_q);
3683 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3684 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
3685 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3686 ibmvfc_alloc_targets(vhost);
3687 spin_lock_irqsave(vhost->host->host_lock, flags);
3689 case IBMVFC_HOST_ACTION_TGT_INIT:
3690 list_for_each_entry(tgt, &vhost->targets, queue) {
3691 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3697 if (!ibmvfc_dev_init_to_do(vhost))
3698 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
3700 case IBMVFC_HOST_ACTION_TGT_ADD:
3701 list_for_each_entry(tgt, &vhost->targets, queue) {
3702 if (tgt->action == IBMVFC_TGT_ACTION_ADD_RPORT) {
3703 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3704 ibmvfc_tgt_add_rport(tgt);
3709 if (vhost->reinit && !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
3711 scsi_block_requests(vhost->host);
3712 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3714 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3715 wake_up(&vhost->init_wait_q);
3722 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3726 * ibmvfc_work - Do task level work
3727 * @data: ibmvfc host struct
3732 static int ibmvfc_work(void *data)
3734 struct ibmvfc_host *vhost = data;
3737 set_user_nice(current, -20);
3740 rc = wait_event_interruptible(vhost->work_wait_q,
3741 ibmvfc_work_to_do(vhost));
3745 if (kthread_should_stop())
3748 ibmvfc_do_work(vhost);
3751 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
3756 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
3757 * @vhost: ibmvfc host struct
3759 * Allocates a page for messages, maps it for dma, and registers
3760 * the crq with the hypervisor.
3763 * zero on success / other on failure
3765 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
3767 int rc, retrc = -ENOMEM;
3768 struct device *dev = vhost->dev;
3769 struct vio_dev *vdev = to_vio_dev(dev);
3770 struct ibmvfc_crq_queue *crq = &vhost->crq;
3773 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
3778 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3779 crq->msg_token = dma_map_single(dev, crq->msgs,
3780 PAGE_SIZE, DMA_BIDIRECTIONAL);
3782 if (dma_mapping_error(dev, crq->msg_token))
3785 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3786 crq->msg_token, PAGE_SIZE);
3788 if (rc == H_RESOURCE)
3789 /* maybe kexecing and resource is busy. try a reset */
3790 retrc = rc = ibmvfc_reset_crq(vhost);
3793 dev_warn(dev, "Partner adapter not ready\n");
3795 dev_warn(dev, "Error %d opening adapter\n", rc);
3796 goto reg_crq_failed;
3801 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
3802 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
3803 goto req_irq_failed;
3806 if ((rc = vio_enable_interrupts(vdev))) {
3807 dev_err(dev, "Error %d enabling interrupts\n", rc);
3808 goto req_irq_failed;
3817 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3818 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3820 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3822 free_page((unsigned long)crq->msgs);
3827 * ibmvfc_free_mem - Free memory for vhost
3828 * @vhost: ibmvfc host struct
3833 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
3835 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3838 mempool_destroy(vhost->tgt_pool);
3839 kfree(vhost->trace);
3840 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
3841 vhost->disc_buf_dma);
3842 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
3843 vhost->login_buf, vhost->login_buf_dma);
3844 dma_pool_destroy(vhost->sg_pool);
3845 dma_unmap_single(vhost->dev, async_q->msg_token,
3846 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3847 free_page((unsigned long)async_q->msgs);
3852 * ibmvfc_alloc_mem - Allocate memory for vhost
3853 * @vhost: ibmvfc host struct
3856 * 0 on success / non-zero on failure
3858 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
3860 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3861 struct device *dev = vhost->dev;
3864 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
3865 if (!async_q->msgs) {
3866 dev_err(dev, "Couldn't allocate async queue.\n");
3870 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
3871 async_q->msg_token = dma_map_single(dev, async_q->msgs,
3872 async_q->size * sizeof(*async_q->msgs),
3875 if (dma_mapping_error(dev, async_q->msg_token)) {
3876 dev_err(dev, "Failed to map async queue\n");
3877 goto free_async_crq;
3880 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
3881 SG_ALL * sizeof(struct srp_direct_buf),
3882 sizeof(struct srp_direct_buf), 0);
3884 if (!vhost->sg_pool) {
3885 dev_err(dev, "Failed to allocate sg pool\n");
3886 goto unmap_async_crq;
3889 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
3890 &vhost->login_buf_dma, GFP_KERNEL);
3892 if (!vhost->login_buf) {
3893 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
3897 vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
3898 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
3899 &vhost->disc_buf_dma, GFP_KERNEL);
3901 if (!vhost->disc_buf) {
3902 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
3903 goto free_login_buffer;
3906 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
3907 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
3910 goto free_disc_buffer;
3912 vhost->tgt_pool = mempool_create_kzalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
3913 sizeof(struct ibmvfc_target));
3915 if (!vhost->tgt_pool) {
3916 dev_err(dev, "Couldn't allocate target memory pool\n");
3924 kfree(vhost->trace);
3926 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
3927 vhost->disc_buf_dma);
3929 dma_free_coherent(dev, sizeof(*vhost->login_buf),
3930 vhost->login_buf, vhost->login_buf_dma);
3932 dma_pool_destroy(vhost->sg_pool);
3934 dma_unmap_single(dev, async_q->msg_token,
3935 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3937 free_page((unsigned long)async_q->msgs);
3944 * ibmvfc_probe - Adapter hot plug add entry point
3945 * @vdev: vio device struct
3946 * @id: vio device id struct
3949 * 0 on success / non-zero on failure
3951 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
3953 struct ibmvfc_host *vhost;
3954 struct Scsi_Host *shost;
3955 struct device *dev = &vdev->dev;
3959 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
3961 dev_err(dev, "Couldn't allocate host data\n");
3965 shost->transportt = ibmvfc_transport_template;
3966 shost->can_queue = max_requests;
3967 shost->max_lun = max_lun;
3968 shost->max_id = max_targets;
3969 shost->max_sectors = IBMVFC_MAX_SECTORS;
3970 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
3971 shost->unique_id = shost->host_no;
3973 vhost = shost_priv(shost);
3974 INIT_LIST_HEAD(&vhost->sent);
3975 INIT_LIST_HEAD(&vhost->free);
3976 INIT_LIST_HEAD(&vhost->targets);
3977 sprintf(vhost->name, IBMVFC_NAME);
3978 vhost->host = shost;
3980 vhost->partition_number = -1;
3981 vhost->log_level = log_level;
3982 strcpy(vhost->partition_name, "UNKNOWN");
3983 init_waitqueue_head(&vhost->work_wait_q);
3984 init_waitqueue_head(&vhost->init_wait_q);
3986 if ((rc = ibmvfc_alloc_mem(vhost)))
3987 goto free_scsi_host;
3989 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
3992 if (IS_ERR(vhost->work_thread)) {
3993 dev_err(dev, "Couldn't create kernel thread: %ld\n",
3994 PTR_ERR(vhost->work_thread));
3998 if ((rc = ibmvfc_init_crq(vhost))) {
3999 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4003 if ((rc = ibmvfc_init_event_pool(vhost))) {
4004 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
4008 if ((rc = scsi_add_host(shost, dev)))
4009 goto release_event_pool;
4011 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
4012 &ibmvfc_trace_attr))) {
4013 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
4017 dev_set_drvdata(dev, vhost);
4018 spin_lock(&ibmvfc_driver_lock);
4019 list_add_tail(&vhost->queue, &ibmvfc_head);
4020 spin_unlock(&ibmvfc_driver_lock);
4022 ibmvfc_send_crq_init(vhost);
4023 scsi_scan_host(shost);
4027 scsi_remove_host(shost);
4029 ibmvfc_free_event_pool(vhost);
4031 ibmvfc_release_crq_queue(vhost);
4033 kthread_stop(vhost->work_thread);
4035 ibmvfc_free_mem(vhost);
4037 scsi_host_put(shost);
4044 * ibmvfc_remove - Adapter hot plug remove entry point
4045 * @vdev: vio device struct
4050 static int ibmvfc_remove(struct vio_dev *vdev)
4052 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4053 unsigned long flags;
4056 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
4057 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
4058 ibmvfc_wait_while_resetting(vhost);
4059 ibmvfc_release_crq_queue(vhost);
4060 kthread_stop(vhost->work_thread);
4061 fc_remove_host(vhost->host);
4062 scsi_remove_host(vhost->host);
4064 spin_lock_irqsave(vhost->host->host_lock, flags);
4065 ibmvfc_purge_requests(vhost, DID_ERROR);
4066 ibmvfc_free_event_pool(vhost);
4067 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4069 ibmvfc_free_mem(vhost);
4070 spin_lock(&ibmvfc_driver_lock);
4071 list_del(&vhost->queue);
4072 spin_unlock(&ibmvfc_driver_lock);
4073 scsi_host_put(vhost->host);
4079 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4080 * @vdev: vio device struct
4083 * Number of bytes the driver will need to DMA map at the same time in
4084 * order to perform well.
4086 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4088 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4089 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4092 static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
4093 {"fcp", "IBM,vfc-client"},
4096 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4098 static struct vio_driver ibmvfc_driver = {
4099 .id_table = ibmvfc_device_table,
4100 .probe = ibmvfc_probe,
4101 .remove = ibmvfc_remove,
4102 .get_desired_dma = ibmvfc_get_desired_dma,
4104 .name = IBMVFC_NAME,
4105 .owner = THIS_MODULE,
4109 static struct fc_function_template ibmvfc_transport_functions = {
4110 .show_host_fabric_name = 1,
4111 .show_host_node_name = 1,
4112 .show_host_port_name = 1,
4113 .show_host_supported_classes = 1,
4114 .show_host_port_type = 1,
4115 .show_host_port_id = 1,
4117 .get_host_port_state = ibmvfc_get_host_port_state,
4118 .show_host_port_state = 1,
4120 .get_host_speed = ibmvfc_get_host_speed,
4121 .show_host_speed = 1,
4123 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4124 .terminate_rport_io = ibmvfc_terminate_rport_io,
4126 .show_rport_maxframe_size = 1,
4127 .show_rport_supported_classes = 1,
4129 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4130 .show_rport_dev_loss_tmo = 1,
4132 .get_starget_node_name = ibmvfc_get_starget_node_name,
4133 .show_starget_node_name = 1,
4135 .get_starget_port_name = ibmvfc_get_starget_port_name,
4136 .show_starget_port_name = 1,
4138 .get_starget_port_id = ibmvfc_get_starget_port_id,
4139 .show_starget_port_id = 1,
4143 * ibmvfc_module_init - Initialize the ibmvfc module
4146 * 0 on success / other on failure
4148 static int __init ibmvfc_module_init(void)
4152 if (!firmware_has_feature(FW_FEATURE_VIO))
4155 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4156 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
4158 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
4159 if (!ibmvfc_transport_template)
4162 rc = vio_register_driver(&ibmvfc_driver);
4164 fc_release_transport(ibmvfc_transport_template);
4169 * ibmvfc_module_exit - Teardown the ibmvfc module
4174 static void __exit ibmvfc_module_exit(void)
4176 vio_unregister_driver(&ibmvfc_driver);
4177 fc_release_transport(ibmvfc_transport_template);
4180 module_init(ibmvfc_module_init);
4181 module_exit(ibmvfc_module_exit);