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>
31 #include <linux/slab.h>
34 #include <linux/stringify.h>
35 #include <linux/bsg-lib.h>
36 #include <asm/firmware.h>
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_cmnd.h>
41 #include <scsi/scsi_host.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_tcq.h>
44 #include <scsi/scsi_transport_fc.h>
45 #include <scsi/scsi_bsg_fc.h>
48 static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
49 static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
50 static u64 max_lun = IBMVFC_MAX_LUN;
51 static unsigned int max_targets = IBMVFC_MAX_TARGETS;
52 static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
53 static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
54 static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
55 static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
56 static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
57 static LIST_HEAD(ibmvfc_head);
58 static DEFINE_SPINLOCK(ibmvfc_driver_lock);
59 static struct scsi_transport_template *ibmvfc_transport_template;
61 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
62 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
63 MODULE_LICENSE("GPL");
64 MODULE_VERSION(IBMVFC_DRIVER_VERSION);
66 module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
67 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
68 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
69 module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
70 MODULE_PARM_DESC(default_timeout,
71 "Default timeout in seconds for initialization and EH commands. "
72 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
73 module_param_named(max_requests, max_requests, uint, S_IRUGO);
74 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
75 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
76 module_param_named(max_lun, max_lun, ullong, S_IRUGO);
77 MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
78 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
79 module_param_named(max_targets, max_targets, uint, S_IRUGO);
80 MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
81 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
82 module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
83 MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
84 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
85 module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
86 MODULE_PARM_DESC(debug, "Enable driver debug information. "
87 "[Default=" __stringify(IBMVFC_DEBUG) "]");
88 module_param_named(log_level, log_level, uint, 0);
89 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
90 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
91 module_param_named(cls3_error, cls3_error, uint, 0);
92 MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
93 "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
103 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
104 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
105 { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
106 { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
107 { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
108 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
109 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
110 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
111 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
112 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
113 { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
114 { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
115 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
116 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
118 { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
119 { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
120 { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
121 { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
122 { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
123 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
124 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
125 { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
126 { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
127 { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
129 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
130 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
131 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
132 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
133 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
134 { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
135 { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
136 { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
137 { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
138 { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
139 { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
141 { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
142 { IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." },
145 static void ibmvfc_npiv_login(struct ibmvfc_host *);
146 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
147 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
148 static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
149 static void ibmvfc_npiv_logout(struct ibmvfc_host *);
151 static const char *unknown_error = "unknown error";
153 #ifdef CONFIG_SCSI_IBMVFC_TRACE
155 * ibmvfc_trc_start - Log a start trace entry
156 * @evt: ibmvfc event struct
159 static void ibmvfc_trc_start(struct ibmvfc_event *evt)
161 struct ibmvfc_host *vhost = evt->vhost;
162 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
163 struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
164 struct ibmvfc_trace_entry *entry;
166 entry = &vhost->trace[vhost->trace_index++];
168 entry->time = jiffies;
169 entry->fmt = evt->crq.format;
170 entry->type = IBMVFC_TRC_START;
172 switch (entry->fmt) {
173 case IBMVFC_CMD_FORMAT:
174 entry->op_code = vfc_cmd->iu.cdb[0];
175 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
176 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
177 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
178 entry->u.start.xfer_len = be32_to_cpu(vfc_cmd->iu.xfer_len);
180 case IBMVFC_MAD_FORMAT:
181 entry->op_code = be32_to_cpu(mad->opcode);
189 * ibmvfc_trc_end - Log an end trace entry
190 * @evt: ibmvfc event struct
193 static void ibmvfc_trc_end(struct ibmvfc_event *evt)
195 struct ibmvfc_host *vhost = evt->vhost;
196 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
197 struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
198 struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
201 entry->time = jiffies;
202 entry->fmt = evt->crq.format;
203 entry->type = IBMVFC_TRC_END;
205 switch (entry->fmt) {
206 case IBMVFC_CMD_FORMAT:
207 entry->op_code = vfc_cmd->iu.cdb[0];
208 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
209 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
210 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
211 entry->u.end.status = be16_to_cpu(vfc_cmd->status);
212 entry->u.end.error = be16_to_cpu(vfc_cmd->error);
213 entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
214 entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
215 entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
217 case IBMVFC_MAD_FORMAT:
218 entry->op_code = be32_to_cpu(mad->opcode);
219 entry->u.end.status = be16_to_cpu(mad->status);
228 #define ibmvfc_trc_start(evt) do { } while (0)
229 #define ibmvfc_trc_end(evt) do { } while (0)
233 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
234 * @status: status / error class
238 * index into cmd_status / -EINVAL on failure
240 static int ibmvfc_get_err_index(u16 status, u16 error)
244 for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
245 if ((cmd_status[i].status & status) == cmd_status[i].status &&
246 cmd_status[i].error == error)
253 * ibmvfc_get_cmd_error - Find the error description for the fcp response
254 * @status: status / error class
258 * error description string
260 static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
262 int rc = ibmvfc_get_err_index(status, error);
264 return cmd_status[rc].name;
265 return unknown_error;
269 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
270 * @vfc_cmd: ibmvfc command struct
273 * SCSI result value to return for completed command
275 static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
278 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
279 int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
281 if ((rsp->flags & FCP_RSP_LEN_VALID) &&
282 ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
283 rsp->data.info.rsp_code))
284 return DID_ERROR << 16;
286 err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
288 return rsp->scsi_status | (cmd_status[err].result << 16);
289 return rsp->scsi_status | (DID_ERROR << 16);
293 * ibmvfc_retry_cmd - Determine if error status is retryable
294 * @status: status / error class
298 * 1 if error should be retried / 0 if it should not
300 static int ibmvfc_retry_cmd(u16 status, u16 error)
302 int rc = ibmvfc_get_err_index(status, error);
305 return cmd_status[rc].retry;
309 static const char *unknown_fc_explain = "unknown fc explain";
311 static const struct {
315 { 0x00, "no additional explanation" },
316 { 0x01, "service parameter error - options" },
317 { 0x03, "service parameter error - initiator control" },
318 { 0x05, "service parameter error - recipient control" },
319 { 0x07, "service parameter error - received data field size" },
320 { 0x09, "service parameter error - concurrent seq" },
321 { 0x0B, "service parameter error - credit" },
322 { 0x0D, "invalid N_Port/F_Port_Name" },
323 { 0x0E, "invalid node/Fabric Name" },
324 { 0x0F, "invalid common service parameters" },
325 { 0x11, "invalid association header" },
326 { 0x13, "association header required" },
327 { 0x15, "invalid originator S_ID" },
328 { 0x17, "invalid OX_ID-RX-ID combination" },
329 { 0x19, "command (request) already in progress" },
330 { 0x1E, "N_Port Login requested" },
331 { 0x1F, "Invalid N_Port_ID" },
334 static const struct {
338 { 0x00, "no additional explanation" },
339 { 0x01, "port identifier not registered" },
340 { 0x02, "port name not registered" },
341 { 0x03, "node name not registered" },
342 { 0x04, "class of service not registered" },
343 { 0x06, "initial process associator not registered" },
344 { 0x07, "FC-4 TYPEs not registered" },
345 { 0x08, "symbolic port name not registered" },
346 { 0x09, "symbolic node name not registered" },
347 { 0x0A, "port type not registered" },
348 { 0xF0, "authorization exception" },
349 { 0xF1, "authentication exception" },
350 { 0xF2, "data base full" },
351 { 0xF3, "data base empty" },
352 { 0xF4, "processing request" },
353 { 0xF5, "unable to verify connection" },
354 { 0xF6, "devices not in a common zone" },
358 * ibmvfc_get_ls_explain - Return the FC Explain description text
359 * @status: FC Explain status
364 static const char *ibmvfc_get_ls_explain(u16 status)
368 for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
369 if (ls_explain[i].fc_explain == status)
370 return ls_explain[i].name;
372 return unknown_fc_explain;
376 * ibmvfc_get_gs_explain - Return the FC Explain description text
377 * @status: FC Explain status
382 static const char *ibmvfc_get_gs_explain(u16 status)
386 for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
387 if (gs_explain[i].fc_explain == status)
388 return gs_explain[i].name;
390 return unknown_fc_explain;
393 static const struct {
394 enum ibmvfc_fc_type fc_type;
397 { IBMVFC_FABRIC_REJECT, "fabric reject" },
398 { IBMVFC_PORT_REJECT, "port reject" },
399 { IBMVFC_LS_REJECT, "ELS reject" },
400 { IBMVFC_FABRIC_BUSY, "fabric busy" },
401 { IBMVFC_PORT_BUSY, "port busy" },
402 { IBMVFC_BASIC_REJECT, "basic reject" },
405 static const char *unknown_fc_type = "unknown fc type";
408 * ibmvfc_get_fc_type - Return the FC Type description text
409 * @status: FC Type error status
414 static const char *ibmvfc_get_fc_type(u16 status)
418 for (i = 0; i < ARRAY_SIZE(fc_type); i++)
419 if (fc_type[i].fc_type == status)
420 return fc_type[i].name;
422 return unknown_fc_type;
426 * ibmvfc_set_tgt_action - Set the next init action for the target
427 * @tgt: ibmvfc target struct
428 * @action: action to perform
431 static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
432 enum ibmvfc_target_action action)
434 switch (tgt->action) {
435 case IBMVFC_TGT_ACTION_DEL_RPORT:
436 if (action == IBMVFC_TGT_ACTION_DELETED_RPORT)
437 tgt->action = action;
438 case IBMVFC_TGT_ACTION_DELETED_RPORT:
441 if (action == IBMVFC_TGT_ACTION_DEL_RPORT)
443 tgt->action = action;
449 * ibmvfc_set_host_state - Set the state for the host
450 * @vhost: ibmvfc host struct
451 * @state: state to set host to
454 * 0 if state changed / non-zero if not changed
456 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
457 enum ibmvfc_host_state state)
461 switch (vhost->state) {
462 case IBMVFC_HOST_OFFLINE:
466 vhost->state = state;
474 * ibmvfc_set_host_action - Set the next init action for the host
475 * @vhost: ibmvfc host struct
476 * @action: action to perform
479 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
480 enum ibmvfc_host_action action)
483 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
484 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
485 vhost->action = action;
487 case IBMVFC_HOST_ACTION_LOGO_WAIT:
488 if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
489 vhost->action = action;
491 case IBMVFC_HOST_ACTION_INIT_WAIT:
492 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
493 vhost->action = action;
495 case IBMVFC_HOST_ACTION_QUERY:
496 switch (vhost->action) {
497 case IBMVFC_HOST_ACTION_INIT_WAIT:
498 case IBMVFC_HOST_ACTION_NONE:
499 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
500 vhost->action = action;
506 case IBMVFC_HOST_ACTION_TGT_INIT:
507 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
508 vhost->action = action;
510 case IBMVFC_HOST_ACTION_INIT:
511 case IBMVFC_HOST_ACTION_TGT_DEL:
512 switch (vhost->action) {
513 case IBMVFC_HOST_ACTION_RESET:
514 case IBMVFC_HOST_ACTION_REENABLE:
517 vhost->action = action;
521 case IBMVFC_HOST_ACTION_LOGO:
522 case IBMVFC_HOST_ACTION_QUERY_TGTS:
523 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
524 case IBMVFC_HOST_ACTION_NONE:
525 case IBMVFC_HOST_ACTION_RESET:
526 case IBMVFC_HOST_ACTION_REENABLE:
528 vhost->action = action;
534 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
535 * @vhost: ibmvfc host struct
540 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
542 if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
543 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
544 scsi_block_requests(vhost->host);
545 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
550 wake_up(&vhost->work_wait_q);
554 * ibmvfc_link_down - Handle a link down event from the adapter
555 * @vhost: ibmvfc host struct
556 * @state: ibmvfc host state to enter
559 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
560 enum ibmvfc_host_state state)
562 struct ibmvfc_target *tgt;
565 scsi_block_requests(vhost->host);
566 list_for_each_entry(tgt, &vhost->targets, queue)
567 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
568 ibmvfc_set_host_state(vhost, state);
569 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
570 vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
571 wake_up(&vhost->work_wait_q);
576 * ibmvfc_init_host - Start host initialization
577 * @vhost: ibmvfc host struct
582 static void ibmvfc_init_host(struct ibmvfc_host *vhost)
584 struct ibmvfc_target *tgt;
586 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
587 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
589 "Host initialization retries exceeded. Taking adapter offline\n");
590 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
595 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
596 memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
597 vhost->async_crq.cur = 0;
599 list_for_each_entry(tgt, &vhost->targets, queue)
600 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
601 scsi_block_requests(vhost->host);
602 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
603 vhost->job_step = ibmvfc_npiv_login;
604 wake_up(&vhost->work_wait_q);
609 * ibmvfc_send_crq - Send a CRQ
610 * @vhost: ibmvfc host struct
611 * @word1: the first 64 bits of the data
612 * @word2: the second 64 bits of the data
615 * 0 on success / other on failure
617 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
619 struct vio_dev *vdev = to_vio_dev(vhost->dev);
620 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
624 * ibmvfc_send_crq_init - Send a CRQ init message
625 * @vhost: ibmvfc host struct
628 * 0 on success / other on failure
630 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
632 ibmvfc_dbg(vhost, "Sending CRQ init\n");
633 return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
637 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
638 * @vhost: ibmvfc host struct
641 * 0 on success / other on failure
643 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
645 ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
646 return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
650 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
651 * @vhost: ibmvfc host struct
653 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
654 * the crq with the hypervisor.
656 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
659 struct vio_dev *vdev = to_vio_dev(vhost->dev);
660 struct ibmvfc_crq_queue *crq = &vhost->crq;
662 ibmvfc_dbg(vhost, "Releasing CRQ\n");
663 free_irq(vdev->irq, vhost);
664 tasklet_kill(&vhost->tasklet);
668 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
669 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
671 vhost->state = IBMVFC_NO_CRQ;
672 vhost->logged_in = 0;
673 dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
674 free_page((unsigned long)crq->msgs);
678 * ibmvfc_reenable_crq_queue - reenables the CRQ
679 * @vhost: ibmvfc host struct
682 * 0 on success / other on failure
684 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
687 struct vio_dev *vdev = to_vio_dev(vhost->dev);
689 /* Re-enable the CRQ */
693 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
694 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
697 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
703 * ibmvfc_reset_crq - resets a crq after a failure
704 * @vhost: ibmvfc host struct
707 * 0 on success / other on failure
709 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
713 struct vio_dev *vdev = to_vio_dev(vhost->dev);
714 struct ibmvfc_crq_queue *crq = &vhost->crq;
720 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
721 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
723 spin_lock_irqsave(vhost->host->host_lock, flags);
724 vhost->state = IBMVFC_NO_CRQ;
725 vhost->logged_in = 0;
727 /* Clean out the queue */
728 memset(crq->msgs, 0, PAGE_SIZE);
731 /* And re-open it again */
732 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
733 crq->msg_token, PAGE_SIZE);
736 /* Adapter is good, but other end is not ready */
737 dev_warn(vhost->dev, "Partner adapter not ready\n");
739 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
740 spin_unlock_irqrestore(vhost->host->host_lock, flags);
746 * ibmvfc_valid_event - Determines if event is valid.
747 * @pool: event_pool that contains the event
748 * @evt: ibmvfc event to be checked for validity
751 * 1 if event is valid / 0 if event is not valid
753 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
754 struct ibmvfc_event *evt)
756 int index = evt - pool->events;
757 if (index < 0 || index >= pool->size) /* outside of bounds */
759 if (evt != pool->events + index) /* unaligned */
765 * ibmvfc_free_event - Free the specified event
766 * @evt: ibmvfc_event to be freed
769 static void ibmvfc_free_event(struct ibmvfc_event *evt)
771 struct ibmvfc_host *vhost = evt->vhost;
772 struct ibmvfc_event_pool *pool = &vhost->pool;
774 BUG_ON(!ibmvfc_valid_event(pool, evt));
775 BUG_ON(atomic_inc_return(&evt->free) != 1);
776 list_add_tail(&evt->queue, &vhost->free);
780 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
781 * @evt: ibmvfc event struct
783 * This function does not setup any error status, that must be done
784 * before this function gets called.
786 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
788 struct scsi_cmnd *cmnd = evt->cmnd;
791 scsi_dma_unmap(cmnd);
792 cmnd->scsi_done(cmnd);
796 complete(evt->eh_comp);
798 ibmvfc_free_event(evt);
802 * ibmvfc_fail_request - Fail request with specified error code
803 * @evt: ibmvfc event struct
804 * @error_code: error code to fail request with
809 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
812 evt->cmnd->result = (error_code << 16);
813 evt->done = ibmvfc_scsi_eh_done;
815 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
817 list_del(&evt->queue);
818 del_timer(&evt->timer);
824 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
825 * @vhost: ibmvfc host struct
826 * @error_code: error code to fail requests with
831 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
833 struct ibmvfc_event *evt, *pos;
835 ibmvfc_dbg(vhost, "Purging all requests\n");
836 list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
837 ibmvfc_fail_request(evt, error_code);
841 * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
842 * @vhost: struct ibmvfc host to reset
844 static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
846 ibmvfc_purge_requests(vhost, DID_ERROR);
847 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
848 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
852 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
853 * @vhost: struct ibmvfc host to reset
855 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
857 if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
858 !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
859 scsi_block_requests(vhost->host);
860 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
861 vhost->job_step = ibmvfc_npiv_logout;
862 wake_up(&vhost->work_wait_q);
864 ibmvfc_hard_reset_host(vhost);
868 * ibmvfc_reset_host - Reset the connection to the server
869 * @vhost: ibmvfc host struct
871 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
875 spin_lock_irqsave(vhost->host->host_lock, flags);
876 __ibmvfc_reset_host(vhost);
877 spin_unlock_irqrestore(vhost->host->host_lock, flags);
881 * ibmvfc_retry_host_init - Retry host initialization if allowed
882 * @vhost: ibmvfc host struct
884 * Returns: 1 if init will be retried / 0 if not
887 static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
891 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
892 vhost->delay_init = 1;
893 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
895 "Host initialization retries exceeded. Taking adapter offline\n");
896 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
897 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
898 __ibmvfc_reset_host(vhost);
900 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
905 wake_up(&vhost->work_wait_q);
910 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
911 * @starget: scsi target struct
914 * ibmvfc_target struct / NULL if not found
916 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
918 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
919 struct ibmvfc_host *vhost = shost_priv(shost);
920 struct ibmvfc_target *tgt;
922 list_for_each_entry(tgt, &vhost->targets, queue)
923 if (tgt->target_id == starget->id) {
924 kref_get(&tgt->kref);
931 * ibmvfc_get_target - Find the specified scsi_target
932 * @starget: scsi target struct
935 * ibmvfc_target struct / NULL if not found
937 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
939 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
940 struct ibmvfc_target *tgt;
943 spin_lock_irqsave(shost->host_lock, flags);
944 tgt = __ibmvfc_get_target(starget);
945 spin_unlock_irqrestore(shost->host_lock, flags);
950 * ibmvfc_get_host_speed - Get host port speed
951 * @shost: scsi host struct
956 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
958 struct ibmvfc_host *vhost = shost_priv(shost);
961 spin_lock_irqsave(shost->host_lock, flags);
962 if (vhost->state == IBMVFC_ACTIVE) {
963 switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
965 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
968 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
971 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
974 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
977 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
980 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
983 ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
984 be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
985 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
989 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
990 spin_unlock_irqrestore(shost->host_lock, flags);
994 * ibmvfc_get_host_port_state - Get host port state
995 * @shost: scsi host struct
1000 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
1002 struct ibmvfc_host *vhost = shost_priv(shost);
1003 unsigned long flags;
1005 spin_lock_irqsave(shost->host_lock, flags);
1006 switch (vhost->state) {
1007 case IBMVFC_INITIALIZING:
1009 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1011 case IBMVFC_LINK_DOWN:
1012 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1014 case IBMVFC_LINK_DEAD:
1015 case IBMVFC_HOST_OFFLINE:
1016 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1019 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1022 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1025 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
1026 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1029 spin_unlock_irqrestore(shost->host_lock, flags);
1033 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1034 * @rport: rport struct
1035 * @timeout: timeout value
1040 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1043 rport->dev_loss_tmo = timeout;
1045 rport->dev_loss_tmo = 1;
1049 * ibmvfc_release_tgt - Free memory allocated for a target
1050 * @kref: kref struct
1053 static void ibmvfc_release_tgt(struct kref *kref)
1055 struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1060 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1061 * @starget: scsi target struct
1066 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1068 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1069 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1071 kref_put(&tgt->kref, ibmvfc_release_tgt);
1075 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1076 * @starget: scsi target struct
1081 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1083 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1084 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1086 kref_put(&tgt->kref, ibmvfc_release_tgt);
1090 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1091 * @starget: scsi target struct
1096 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1098 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1099 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1101 kref_put(&tgt->kref, ibmvfc_release_tgt);
1105 * ibmvfc_wait_while_resetting - Wait while the host resets
1106 * @vhost: ibmvfc host struct
1109 * 0 on success / other on failure
1111 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1113 long timeout = wait_event_timeout(vhost->init_wait_q,
1114 ((vhost->state == IBMVFC_ACTIVE ||
1115 vhost->state == IBMVFC_HOST_OFFLINE ||
1116 vhost->state == IBMVFC_LINK_DEAD) &&
1117 vhost->action == IBMVFC_HOST_ACTION_NONE),
1118 (init_timeout * HZ));
1120 return timeout ? 0 : -EIO;
1124 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1125 * @shost: scsi host struct
1128 * 0 on success / other on failure
1130 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1132 struct ibmvfc_host *vhost = shost_priv(shost);
1134 dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1135 ibmvfc_reset_host(vhost);
1136 return ibmvfc_wait_while_resetting(vhost);
1140 * ibmvfc_gather_partition_info - Gather info about the LPAR
1145 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1147 struct device_node *rootdn;
1149 const unsigned int *num;
1151 rootdn = of_find_node_by_path("/");
1155 name = of_get_property(rootdn, "ibm,partition-name", NULL);
1157 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1158 num = of_get_property(rootdn, "ibm,partition-no", NULL);
1160 vhost->partition_number = *num;
1161 of_node_put(rootdn);
1165 * ibmvfc_set_login_info - Setup info for NPIV login
1166 * @vhost: ibmvfc host struct
1171 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1173 struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1174 struct device_node *of_node = vhost->dev->of_node;
1175 const char *location;
1177 memset(login_info, 0, sizeof(*login_info));
1179 login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
1180 login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
1181 login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
1182 login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
1183 login_info->partition_num = cpu_to_be32(vhost->partition_number);
1184 login_info->vfc_frame_version = cpu_to_be32(1);
1185 login_info->fcp_version = cpu_to_be16(3);
1186 login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
1187 if (vhost->client_migrated)
1188 login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
1190 login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
1191 login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE);
1192 login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
1193 login_info->async.len = cpu_to_be32(vhost->async_crq.size * sizeof(*vhost->async_crq.msgs));
1194 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1195 strncpy(login_info->device_name,
1196 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
1198 location = of_get_property(of_node, "ibm,loc-code", NULL);
1199 location = location ? location : dev_name(vhost->dev);
1200 strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1204 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1205 * @vhost: ibmvfc host who owns the event pool
1207 * Returns zero on success.
1209 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1212 struct ibmvfc_event_pool *pool = &vhost->pool;
1215 pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1216 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1220 pool->iu_storage = dma_alloc_coherent(vhost->dev,
1221 pool->size * sizeof(*pool->iu_storage),
1222 &pool->iu_token, 0);
1224 if (!pool->iu_storage) {
1225 kfree(pool->events);
1229 for (i = 0; i < pool->size; ++i) {
1230 struct ibmvfc_event *evt = &pool->events[i];
1231 atomic_set(&evt->free, 1);
1232 evt->crq.valid = 0x80;
1233 evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
1234 evt->xfer_iu = pool->iu_storage + i;
1236 evt->ext_list = NULL;
1237 list_add_tail(&evt->queue, &vhost->free);
1245 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1246 * @vhost: ibmvfc host who owns the event pool
1249 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1252 struct ibmvfc_event_pool *pool = &vhost->pool;
1255 for (i = 0; i < pool->size; ++i) {
1256 list_del(&pool->events[i].queue);
1257 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1258 if (pool->events[i].ext_list)
1259 dma_pool_free(vhost->sg_pool,
1260 pool->events[i].ext_list,
1261 pool->events[i].ext_list_token);
1264 kfree(pool->events);
1265 dma_free_coherent(vhost->dev,
1266 pool->size * sizeof(*pool->iu_storage),
1267 pool->iu_storage, pool->iu_token);
1272 * ibmvfc_get_event - Gets the next free event in pool
1273 * @vhost: ibmvfc host struct
1275 * Returns a free event from the pool.
1277 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1279 struct ibmvfc_event *evt;
1281 BUG_ON(list_empty(&vhost->free));
1282 evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1283 atomic_set(&evt->free, 0);
1284 list_del(&evt->queue);
1289 * ibmvfc_init_event - Initialize fields in an event struct that are always
1292 * @done: Routine to call when the event is responded to
1293 * @format: SRP or MAD format
1295 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1296 void (*done) (struct ibmvfc_event *), u8 format)
1299 evt->sync_iu = NULL;
1300 evt->crq.format = format;
1302 evt->eh_comp = NULL;
1306 * ibmvfc_map_sg_list - Initialize scatterlist
1307 * @scmd: scsi command struct
1308 * @nseg: number of scatterlist segments
1309 * @md: memory descriptor list to initialize
1311 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1312 struct srp_direct_buf *md)
1315 struct scatterlist *sg;
1317 scsi_for_each_sg(scmd, sg, nseg, i) {
1318 md[i].va = cpu_to_be64(sg_dma_address(sg));
1319 md[i].len = cpu_to_be32(sg_dma_len(sg));
1325 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1326 * @scmd: struct scsi_cmnd with the scatterlist
1327 * @evt: ibmvfc event struct
1328 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1329 * @dev: device for which to map dma memory
1332 * 0 on success / non-zero on failure
1334 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1335 struct ibmvfc_event *evt,
1336 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1340 struct srp_direct_buf *data = &vfc_cmd->ioba;
1341 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1344 vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1346 sg_mapped = scsi_dma_map(scmd);
1348 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1350 } else if (unlikely(sg_mapped < 0)) {
1351 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1352 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1356 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1357 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1358 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1360 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1361 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1364 if (sg_mapped == 1) {
1365 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1369 vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1371 if (!evt->ext_list) {
1372 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1373 &evt->ext_list_token);
1375 if (!evt->ext_list) {
1376 scsi_dma_unmap(scmd);
1377 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1378 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1383 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1385 data->va = cpu_to_be64(evt->ext_list_token);
1386 data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1392 * ibmvfc_timeout - Internal command timeout handler
1393 * @evt: struct ibmvfc_event that timed out
1395 * Called when an internally generated command times out
1397 static void ibmvfc_timeout(struct timer_list *t)
1399 struct ibmvfc_event *evt = from_timer(evt, t, timer);
1400 struct ibmvfc_host *vhost = evt->vhost;
1401 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1402 ibmvfc_reset_host(vhost);
1406 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1407 * @evt: event to be sent
1408 * @vhost: ibmvfc host struct
1409 * @timeout: timeout in seconds - 0 means do not time command
1411 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1413 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1414 struct ibmvfc_host *vhost, unsigned long timeout)
1416 __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1419 /* Copy the IU into the transfer area */
1420 *evt->xfer_iu = evt->iu;
1421 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1422 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1423 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1424 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1428 list_add_tail(&evt->queue, &vhost->sent);
1429 timer_setup(&evt->timer, ibmvfc_timeout, 0);
1432 evt->timer.expires = jiffies + (timeout * HZ);
1433 add_timer(&evt->timer);
1438 if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1439 be64_to_cpu(crq_as_u64[1])))) {
1440 list_del(&evt->queue);
1441 del_timer(&evt->timer);
1443 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1444 * Firmware will send a CRQ with a transport event (0xFF) to
1445 * tell this client what has happened to the transport. This
1446 * will be handled in ibmvfc_handle_crq()
1448 if (rc == H_CLOSED) {
1449 if (printk_ratelimit())
1450 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1452 scsi_dma_unmap(evt->cmnd);
1453 ibmvfc_free_event(evt);
1454 return SCSI_MLQUEUE_HOST_BUSY;
1457 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1459 evt->cmnd->result = DID_ERROR << 16;
1460 evt->done = ibmvfc_scsi_eh_done;
1462 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1466 ibmvfc_trc_start(evt);
1472 * ibmvfc_log_error - Log an error for the failed command if appropriate
1473 * @evt: ibmvfc event to log
1476 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1478 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1479 struct ibmvfc_host *vhost = evt->vhost;
1480 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1481 struct scsi_cmnd *cmnd = evt->cmnd;
1482 const char *err = unknown_error;
1483 int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1488 logerr = cmd_status[index].log;
1489 err = cmd_status[index].name;
1492 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1495 if (rsp->flags & FCP_RSP_LEN_VALID)
1496 rsp_code = rsp->data.info.rsp_code;
1498 scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
1499 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1500 cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
1501 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1505 * ibmvfc_relogin - Log back into the specified device
1506 * @sdev: scsi device struct
1509 static void ibmvfc_relogin(struct scsi_device *sdev)
1511 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1512 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1513 struct ibmvfc_target *tgt;
1515 list_for_each_entry(tgt, &vhost->targets, queue) {
1516 if (rport == tgt->rport) {
1517 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
1522 ibmvfc_reinit_host(vhost);
1526 * ibmvfc_scsi_done - Handle responses from commands
1527 * @evt: ibmvfc event to be handled
1529 * Used as a callback when sending scsi cmds.
1531 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1533 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1534 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1535 struct scsi_cmnd *cmnd = evt->cmnd;
1537 u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1540 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1541 scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1542 else if (rsp->flags & FCP_RESID_UNDER)
1543 scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1545 scsi_set_resid(cmnd, 0);
1547 if (vfc_cmd->status) {
1548 cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1550 if (rsp->flags & FCP_RSP_LEN_VALID)
1551 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1552 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1553 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1554 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1555 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1556 if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1557 (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1558 ibmvfc_relogin(cmnd->device);
1560 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1561 cmnd->result = (DID_ERROR << 16);
1563 ibmvfc_log_error(evt);
1566 if (!cmnd->result &&
1567 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1568 cmnd->result = (DID_ERROR << 16);
1570 scsi_dma_unmap(cmnd);
1571 cmnd->scsi_done(cmnd);
1575 complete(evt->eh_comp);
1577 ibmvfc_free_event(evt);
1581 * ibmvfc_host_chkready - Check if the host can accept commands
1582 * @vhost: struct ibmvfc host
1585 * 1 if host can accept command / 0 if not
1587 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1591 switch (vhost->state) {
1592 case IBMVFC_LINK_DEAD:
1593 case IBMVFC_HOST_OFFLINE:
1594 result = DID_NO_CONNECT << 16;
1597 case IBMVFC_INITIALIZING:
1599 case IBMVFC_LINK_DOWN:
1600 result = DID_REQUEUE << 16;
1611 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1612 * @cmnd: struct scsi_cmnd to be executed
1613 * @done: Callback function to be called when cmnd is completed
1616 * 0 on success / other on failure
1618 static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
1619 void (*done) (struct scsi_cmnd *))
1621 struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1622 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1623 struct ibmvfc_cmd *vfc_cmd;
1624 struct ibmvfc_event *evt;
1627 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1628 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1634 cmnd->result = (DID_OK << 16);
1635 evt = ibmvfc_get_event(vhost);
1636 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1638 cmnd->scsi_done = done;
1639 vfc_cmd = &evt->iu.cmd;
1640 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1641 vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
1642 vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1643 vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1644 vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
1645 vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1646 vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
1647 vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1648 vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1649 int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1650 memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1652 if (cmnd->flags & SCMD_TAGGED) {
1653 vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
1654 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1657 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1658 return ibmvfc_send_event(evt, vhost, 0);
1660 ibmvfc_free_event(evt);
1662 return SCSI_MLQUEUE_HOST_BUSY;
1664 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1665 scmd_printk(KERN_ERR, cmnd,
1666 "Failed to map DMA buffer for command. rc=%d\n", rc);
1668 cmnd->result = DID_ERROR << 16;
1673 static DEF_SCSI_QCMD(ibmvfc_queuecommand)
1676 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1677 * @evt: ibmvfc event struct
1680 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1682 /* copy the response back */
1684 *evt->sync_iu = *evt->xfer_iu;
1686 complete(&evt->comp);
1690 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1691 * @evt: struct ibmvfc_event
1694 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
1696 struct ibmvfc_host *vhost = evt->vhost;
1698 ibmvfc_free_event(evt);
1699 vhost->aborting_passthru = 0;
1700 dev_info(vhost->dev, "Passthru command cancelled\n");
1704 * ibmvfc_bsg_timeout - Handle a BSG timeout
1705 * @job: struct bsg_job that timed out
1708 * 0 on success / other on failure
1710 static int ibmvfc_bsg_timeout(struct bsg_job *job)
1712 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1713 unsigned long port_id = (unsigned long)job->dd_data;
1714 struct ibmvfc_event *evt;
1715 struct ibmvfc_tmf *tmf;
1716 unsigned long flags;
1720 spin_lock_irqsave(vhost->host->host_lock, flags);
1721 if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
1722 __ibmvfc_reset_host(vhost);
1723 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1727 vhost->aborting_passthru = 1;
1728 evt = ibmvfc_get_event(vhost);
1729 ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
1732 memset(tmf, 0, sizeof(*tmf));
1733 tmf->common.version = cpu_to_be32(1);
1734 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
1735 tmf->common.length = cpu_to_be16(sizeof(*tmf));
1736 tmf->scsi_id = cpu_to_be64(port_id);
1737 tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1738 tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
1739 rc = ibmvfc_send_event(evt, vhost, default_timeout);
1742 vhost->aborting_passthru = 0;
1743 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
1746 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
1749 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1756 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
1757 * @vhost: struct ibmvfc_host to send command
1758 * @port_id: port ID to send command
1761 * 0 on success / other on failure
1763 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
1765 struct ibmvfc_port_login *plogi;
1766 struct ibmvfc_target *tgt;
1767 struct ibmvfc_event *evt;
1768 union ibmvfc_iu rsp_iu;
1769 unsigned long flags;
1770 int rc = 0, issue_login = 1;
1773 spin_lock_irqsave(vhost->host->host_lock, flags);
1774 list_for_each_entry(tgt, &vhost->targets, queue) {
1775 if (tgt->scsi_id == port_id) {
1783 if (unlikely((rc = ibmvfc_host_chkready(vhost))))
1786 evt = ibmvfc_get_event(vhost);
1787 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1788 plogi = &evt->iu.plogi;
1789 memset(plogi, 0, sizeof(*plogi));
1790 plogi->common.version = cpu_to_be32(1);
1791 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
1792 plogi->common.length = cpu_to_be16(sizeof(*plogi));
1793 plogi->scsi_id = cpu_to_be64(port_id);
1794 evt->sync_iu = &rsp_iu;
1795 init_completion(&evt->comp);
1797 rc = ibmvfc_send_event(evt, vhost, default_timeout);
1798 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1803 wait_for_completion(&evt->comp);
1805 if (rsp_iu.plogi.common.status)
1808 spin_lock_irqsave(vhost->host->host_lock, flags);
1809 ibmvfc_free_event(evt);
1811 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1817 * ibmvfc_bsg_request - Handle a BSG request
1818 * @job: struct bsg_job to be executed
1821 * 0 on success / other on failure
1823 static int ibmvfc_bsg_request(struct bsg_job *job)
1825 struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1826 struct fc_rport *rport = fc_bsg_to_rport(job);
1827 struct ibmvfc_passthru_mad *mad;
1828 struct ibmvfc_event *evt;
1829 union ibmvfc_iu rsp_iu;
1830 unsigned long flags, port_id = -1;
1831 struct fc_bsg_request *bsg_request = job->request;
1832 struct fc_bsg_reply *bsg_reply = job->reply;
1833 unsigned int code = bsg_request->msgcode;
1834 int rc = 0, req_seg, rsp_seg, issue_login = 0;
1835 u32 fc_flags, rsp_len;
1838 bsg_reply->reply_payload_rcv_len = 0;
1840 port_id = rport->port_id;
1843 case FC_BSG_HST_ELS_NOLOGIN:
1844 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
1845 (bsg_request->rqst_data.h_els.port_id[1] << 8) |
1846 bsg_request->rqst_data.h_els.port_id[2];
1847 case FC_BSG_RPT_ELS:
1848 fc_flags = IBMVFC_FC_ELS;
1852 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
1853 (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
1854 bsg_request->rqst_data.h_ct.port_id[2];
1856 fc_flags = IBMVFC_FC_CT_IU;
1864 if (!mutex_trylock(&vhost->passthru_mutex))
1867 job->dd_data = (void *)port_id;
1868 req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
1869 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1872 mutex_unlock(&vhost->passthru_mutex);
1876 rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
1877 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1880 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1881 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1882 mutex_unlock(&vhost->passthru_mutex);
1886 if (req_seg > 1 || rsp_seg > 1) {
1892 rc = ibmvfc_bsg_plogi(vhost, port_id);
1894 spin_lock_irqsave(vhost->host->host_lock, flags);
1896 if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
1897 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1898 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1902 evt = ibmvfc_get_event(vhost);
1903 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1904 mad = &evt->iu.passthru;
1906 memset(mad, 0, sizeof(*mad));
1907 mad->common.version = cpu_to_be32(1);
1908 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
1909 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
1911 mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
1912 offsetof(struct ibmvfc_passthru_mad, iu));
1913 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
1915 mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
1916 mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
1917 mad->iu.flags = cpu_to_be32(fc_flags);
1918 mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1920 mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
1921 mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
1922 mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
1923 mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
1924 mad->iu.scsi_id = cpu_to_be64(port_id);
1925 mad->iu.tag = cpu_to_be64((u64)evt);
1926 rsp_len = be32_to_cpu(mad->iu.rsp.len);
1928 evt->sync_iu = &rsp_iu;
1929 init_completion(&evt->comp);
1930 rc = ibmvfc_send_event(evt, vhost, 0);
1931 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1938 wait_for_completion(&evt->comp);
1940 if (rsp_iu.passthru.common.status)
1943 bsg_reply->reply_payload_rcv_len = rsp_len;
1945 spin_lock_irqsave(vhost->host->host_lock, flags);
1946 ibmvfc_free_event(evt);
1947 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1948 bsg_reply->result = rc;
1949 bsg_job_done(job, bsg_reply->result,
1950 bsg_reply->reply_payload_rcv_len);
1953 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1954 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1955 dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
1956 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1957 mutex_unlock(&vhost->passthru_mutex);
1963 * ibmvfc_reset_device - Reset the device with the specified reset type
1964 * @sdev: scsi device to reset
1966 * @desc: reset type description for log messages
1969 * 0 on success / other on failure
1971 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1973 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1974 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1975 struct ibmvfc_cmd *tmf;
1976 struct ibmvfc_event *evt = NULL;
1977 union ibmvfc_iu rsp_iu;
1978 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1979 int rsp_rc = -EBUSY;
1980 unsigned long flags;
1983 spin_lock_irqsave(vhost->host->host_lock, flags);
1984 if (vhost->state == IBMVFC_ACTIVE) {
1985 evt = ibmvfc_get_event(vhost);
1986 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1989 memset(tmf, 0, sizeof(*tmf));
1990 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
1991 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
1992 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1993 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
1994 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
1995 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
1996 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
1997 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1998 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
1999 tmf->iu.tmf_flags = type;
2000 evt->sync_iu = &rsp_iu;
2002 init_completion(&evt->comp);
2003 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2005 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2008 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2013 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2014 wait_for_completion(&evt->comp);
2016 if (rsp_iu.cmd.status)
2017 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2020 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2021 rsp_code = fc_rsp->data.info.rsp_code;
2023 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2024 "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2025 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2026 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2027 fc_rsp->scsi_status);
2030 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2032 spin_lock_irqsave(vhost->host->host_lock, flags);
2033 ibmvfc_free_event(evt);
2034 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2039 * ibmvfc_match_rport - Match function for specified remote port
2040 * @evt: ibmvfc event struct
2041 * @device: device to match (rport)
2044 * 1 if event matches rport / 0 if event does not match rport
2046 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2048 struct fc_rport *cmd_rport;
2051 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2052 if (cmd_rport == rport)
2059 * ibmvfc_match_target - Match function for specified target
2060 * @evt: ibmvfc event struct
2061 * @device: device to match (starget)
2064 * 1 if event matches starget / 0 if event does not match starget
2066 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2068 if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2074 * ibmvfc_match_lun - Match function for specified LUN
2075 * @evt: ibmvfc event struct
2076 * @device: device to match (sdev)
2079 * 1 if event matches sdev / 0 if event does not match sdev
2081 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2083 if (evt->cmnd && evt->cmnd->device == device)
2089 * ibmvfc_wait_for_ops - Wait for ops to complete
2090 * @vhost: ibmvfc host struct
2091 * @device: device to match (starget or sdev)
2092 * @match: match function
2097 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2098 int (*match) (struct ibmvfc_event *, void *))
2100 struct ibmvfc_event *evt;
2101 DECLARE_COMPLETION_ONSTACK(comp);
2103 unsigned long flags;
2104 signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2109 spin_lock_irqsave(vhost->host->host_lock, flags);
2110 list_for_each_entry(evt, &vhost->sent, queue) {
2111 if (match(evt, device)) {
2112 evt->eh_comp = ∁
2116 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2119 timeout = wait_for_completion_timeout(&comp, timeout);
2123 spin_lock_irqsave(vhost->host->host_lock, flags);
2124 list_for_each_entry(evt, &vhost->sent, queue) {
2125 if (match(evt, device)) {
2126 evt->eh_comp = NULL;
2130 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2132 dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2134 return wait ? FAILED : SUCCESS;
2144 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2145 * @sdev: scsi device to cancel commands
2146 * @type: type of error recovery being performed
2148 * This sends a cancel to the VIOS for the specified device. This does
2149 * NOT send any abort to the actual device. That must be done separately.
2152 * 0 on success / other on failure
2154 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2156 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2157 struct scsi_target *starget = scsi_target(sdev);
2158 struct fc_rport *rport = starget_to_rport(starget);
2159 struct ibmvfc_tmf *tmf;
2160 struct ibmvfc_event *evt, *found_evt;
2161 union ibmvfc_iu rsp;
2162 int rsp_rc = -EBUSY;
2163 unsigned long flags;
2167 spin_lock_irqsave(vhost->host->host_lock, flags);
2169 list_for_each_entry(evt, &vhost->sent, queue) {
2170 if (evt->cmnd && evt->cmnd->device == sdev) {
2177 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2178 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2179 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2183 if (vhost->logged_in) {
2184 evt = ibmvfc_get_event(vhost);
2185 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2188 memset(tmf, 0, sizeof(*tmf));
2189 tmf->common.version = cpu_to_be32(1);
2190 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2191 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2192 tmf->scsi_id = cpu_to_be64(rport->port_id);
2193 int_to_scsilun(sdev->lun, &tmf->lun);
2194 if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
2195 type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2196 if (vhost->state == IBMVFC_ACTIVE)
2197 tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2199 tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2200 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2201 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2203 evt->sync_iu = &rsp;
2204 init_completion(&evt->comp);
2205 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2208 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2211 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2212 /* If failure is received, the host adapter is most likely going
2213 through reset, return success so the caller will wait for the command
2214 being cancelled to get returned */
2218 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2220 wait_for_completion(&evt->comp);
2221 status = be16_to_cpu(rsp.mad_common.status);
2222 spin_lock_irqsave(vhost->host->host_lock, flags);
2223 ibmvfc_free_event(evt);
2224 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2226 if (status != IBMVFC_MAD_SUCCESS) {
2227 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2229 case IBMVFC_MAD_DRIVER_FAILED:
2230 case IBMVFC_MAD_CRQ_ERROR:
2231 /* Host adapter most likely going through reset, return success to
2232 the caller will wait for the command being cancelled to get returned */
2239 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2244 * ibmvfc_match_key - Match function for specified cancel key
2245 * @evt: ibmvfc event struct
2246 * @key: cancel key to match
2249 * 1 if event matches key / 0 if event does not match key
2251 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2253 unsigned long cancel_key = (unsigned long)key;
2255 if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2256 be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2262 * ibmvfc_match_evt - Match function for specified event
2263 * @evt: ibmvfc event struct
2264 * @match: event to match
2267 * 1 if event matches key / 0 if event does not match key
2269 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2277 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2278 * @sdev: scsi device to abort commands
2280 * This sends an Abort Task Set to the VIOS for the specified device. This does
2281 * NOT send any cancel to the VIOS. That must be done separately.
2284 * 0 on success / other on failure
2286 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2288 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2289 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2290 struct ibmvfc_cmd *tmf;
2291 struct ibmvfc_event *evt, *found_evt;
2292 union ibmvfc_iu rsp_iu;
2293 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2294 int rc, rsp_rc = -EBUSY;
2295 unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2298 spin_lock_irqsave(vhost->host->host_lock, flags);
2300 list_for_each_entry(evt, &vhost->sent, queue) {
2301 if (evt->cmnd && evt->cmnd->device == sdev) {
2308 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2309 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2310 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2314 if (vhost->state == IBMVFC_ACTIVE) {
2315 evt = ibmvfc_get_event(vhost);
2316 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2319 memset(tmf, 0, sizeof(*tmf));
2320 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
2321 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
2322 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2323 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
2324 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
2325 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2326 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
2327 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2328 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2329 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
2330 evt->sync_iu = &rsp_iu;
2332 init_completion(&evt->comp);
2333 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2336 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2339 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2343 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2344 timeout = wait_for_completion_timeout(&evt->comp, timeout);
2347 rc = ibmvfc_cancel_all(sdev, 0);
2349 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2355 sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2356 ibmvfc_reset_host(vhost);
2358 rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2363 rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2364 if (rc != SUCCESS) {
2365 spin_lock_irqsave(vhost->host->host_lock, flags);
2366 ibmvfc_hard_reset_host(vhost);
2367 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2375 if (rsp_iu.cmd.status)
2376 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2379 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2380 rsp_code = fc_rsp->data.info.rsp_code;
2382 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2383 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2384 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2385 be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2386 fc_rsp->scsi_status);
2389 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2392 spin_lock_irqsave(vhost->host->host_lock, flags);
2393 ibmvfc_free_event(evt);
2394 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2399 * ibmvfc_eh_abort_handler - Abort a command
2400 * @cmd: scsi command to abort
2403 * SUCCESS / FAST_IO_FAIL / FAILED
2405 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2407 struct scsi_device *sdev = cmd->device;
2408 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2409 int cancel_rc, block_rc;
2413 block_rc = fc_block_scsi_eh(cmd);
2414 ibmvfc_wait_while_resetting(vhost);
2415 if (block_rc != FAST_IO_FAIL) {
2416 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2417 ibmvfc_abort_task_set(sdev);
2419 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2422 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2424 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2432 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2433 * @cmd: scsi command struct
2436 * SUCCESS / FAST_IO_FAIL / FAILED
2438 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2440 struct scsi_device *sdev = cmd->device;
2441 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2442 int cancel_rc, block_rc, reset_rc = 0;
2446 block_rc = fc_block_scsi_eh(cmd);
2447 ibmvfc_wait_while_resetting(vhost);
2448 if (block_rc != FAST_IO_FAIL) {
2449 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2450 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2452 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2454 if (!cancel_rc && !reset_rc)
2455 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2457 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2465 * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2466 * @sdev: scsi device struct
2467 * @data: return code
2470 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2472 unsigned long *rc = data;
2473 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2477 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2478 * @sdev: scsi device struct
2479 * @data: return code
2482 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2484 unsigned long *rc = data;
2485 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2489 * ibmvfc_eh_target_reset_handler - Reset the target
2490 * @cmd: scsi command struct
2493 * SUCCESS / FAST_IO_FAIL / FAILED
2495 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2497 struct scsi_device *sdev = cmd->device;
2498 struct ibmvfc_host *vhost = shost_priv(sdev->host);
2499 struct scsi_target *starget = scsi_target(sdev);
2503 unsigned long cancel_rc = 0;
2506 block_rc = fc_block_scsi_eh(cmd);
2507 ibmvfc_wait_while_resetting(vhost);
2508 if (block_rc != FAST_IO_FAIL) {
2509 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2510 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2512 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2514 if (!cancel_rc && !reset_rc)
2515 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2517 if (block_rc == FAST_IO_FAIL && rc != FAILED)
2525 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2526 * @cmd: struct scsi_cmnd having problems
2529 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2532 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2534 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2535 rc = ibmvfc_issue_fc_host_lip(vhost->host);
2537 return rc ? FAILED : SUCCESS;
2541 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2542 * @rport: rport struct
2547 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2549 struct Scsi_Host *shost = rport_to_shost(rport);
2550 struct ibmvfc_host *vhost = shost_priv(shost);
2551 struct fc_rport *dev_rport;
2552 struct scsi_device *sdev;
2556 shost_for_each_device(sdev, shost) {
2557 dev_rport = starget_to_rport(scsi_target(sdev));
2558 if (dev_rport != rport)
2560 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2563 rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
2566 ibmvfc_issue_fc_host_lip(shost);
2570 static const struct ibmvfc_async_desc ae_desc [] = {
2571 { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2572 { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2573 { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2574 { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2575 { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2576 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
2577 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
2578 { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
2579 { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
2580 { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
2581 { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
2582 { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
2583 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
2586 static const struct ibmvfc_async_desc unknown_ae = {
2587 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
2591 * ibmvfc_get_ae_desc - Get text description for async event
2595 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
2599 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2600 if (ae_desc[i].ae == ae)
2606 static const struct {
2607 enum ibmvfc_ae_link_state state;
2610 { IBMVFC_AE_LS_LINK_UP, " link up" },
2611 { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
2612 { IBMVFC_AE_LS_LINK_DOWN, " link down" },
2613 { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
2617 * ibmvfc_get_link_state - Get text description for link state
2618 * @state: link state
2621 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
2625 for (i = 0; i < ARRAY_SIZE(link_desc); i++)
2626 if (link_desc[i].state == state)
2627 return link_desc[i].desc;
2633 * ibmvfc_handle_async - Handle an async event from the adapter
2634 * @crq: crq to process
2635 * @vhost: ibmvfc host struct
2638 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2639 struct ibmvfc_host *vhost)
2641 const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
2642 struct ibmvfc_target *tgt;
2644 ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
2645 " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
2646 be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
2647 ibmvfc_get_link_state(crq->link_state));
2649 switch (be64_to_cpu(crq->event)) {
2650 case IBMVFC_AE_RESUME:
2651 switch (crq->link_state) {
2652 case IBMVFC_AE_LS_LINK_DOWN:
2653 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2655 case IBMVFC_AE_LS_LINK_DEAD:
2656 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2658 case IBMVFC_AE_LS_LINK_UP:
2659 case IBMVFC_AE_LS_LINK_BOUNCED:
2661 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2662 vhost->delay_init = 1;
2663 __ibmvfc_reset_host(vhost);
2668 case IBMVFC_AE_LINK_UP:
2669 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2670 vhost->delay_init = 1;
2671 __ibmvfc_reset_host(vhost);
2673 case IBMVFC_AE_SCN_FABRIC:
2674 case IBMVFC_AE_SCN_DOMAIN:
2675 vhost->events_to_log |= IBMVFC_AE_RSCN;
2676 if (vhost->state < IBMVFC_HALTED) {
2677 vhost->delay_init = 1;
2678 __ibmvfc_reset_host(vhost);
2681 case IBMVFC_AE_SCN_NPORT:
2682 case IBMVFC_AE_SCN_GROUP:
2683 vhost->events_to_log |= IBMVFC_AE_RSCN;
2684 ibmvfc_reinit_host(vhost);
2686 case IBMVFC_AE_ELS_LOGO:
2687 case IBMVFC_AE_ELS_PRLO:
2688 case IBMVFC_AE_ELS_PLOGI:
2689 list_for_each_entry(tgt, &vhost->targets, queue) {
2690 if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
2692 if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
2694 if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
2696 if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
2698 if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
2700 if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
2701 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2702 ibmvfc_reinit_host(vhost);
2706 case IBMVFC_AE_LINK_DOWN:
2707 case IBMVFC_AE_ADAPTER_FAILED:
2708 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2710 case IBMVFC_AE_LINK_DEAD:
2711 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2713 case IBMVFC_AE_HALT:
2714 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2717 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
2723 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2724 * @crq: Command/Response queue
2725 * @vhost: ibmvfc host struct
2728 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2731 struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
2733 switch (crq->valid) {
2734 case IBMVFC_CRQ_INIT_RSP:
2735 switch (crq->format) {
2736 case IBMVFC_CRQ_INIT:
2737 dev_info(vhost->dev, "Partner initialized\n");
2738 /* Send back a response */
2739 rc = ibmvfc_send_crq_init_complete(vhost);
2741 ibmvfc_init_host(vhost);
2743 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2745 case IBMVFC_CRQ_INIT_COMPLETE:
2746 dev_info(vhost->dev, "Partner initialization complete\n");
2747 ibmvfc_init_host(vhost);
2750 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2753 case IBMVFC_CRQ_XPORT_EVENT:
2754 vhost->state = IBMVFC_NO_CRQ;
2755 vhost->logged_in = 0;
2756 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2757 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2758 /* We need to re-setup the interpartition connection */
2759 dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
2760 vhost->client_migrated = 1;
2761 ibmvfc_purge_requests(vhost, DID_REQUEUE);
2762 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2763 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
2764 } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
2765 dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
2766 ibmvfc_purge_requests(vhost, DID_ERROR);
2767 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2768 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
2770 dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
2773 case IBMVFC_CRQ_CMD_RSP:
2776 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2780 if (crq->format == IBMVFC_ASYNC_EVENT)
2783 /* The only kind of payload CRQs we should get are responses to
2784 * things we send. Make sure this response is to something we
2787 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2788 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
2793 if (unlikely(atomic_read(&evt->free))) {
2794 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
2799 del_timer(&evt->timer);
2800 list_del(&evt->queue);
2801 ibmvfc_trc_end(evt);
2806 * ibmvfc_scan_finished - Check if the device scan is done.
2807 * @shost: scsi host struct
2808 * @time: current elapsed time
2811 * 0 if scan is not done / 1 if scan is done
2813 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2815 unsigned long flags;
2816 struct ibmvfc_host *vhost = shost_priv(shost);
2819 spin_lock_irqsave(shost->host_lock, flags);
2820 if (time >= (init_timeout * HZ)) {
2821 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2822 "continuing initialization\n", init_timeout);
2826 if (vhost->scan_complete)
2828 spin_unlock_irqrestore(shost->host_lock, flags);
2833 * ibmvfc_slave_alloc - Setup the device's task set value
2834 * @sdev: struct scsi_device device to configure
2836 * Set the device's task set value so that error handling works as
2840 * 0 on success / -ENXIO if device does not exist
2842 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2844 struct Scsi_Host *shost = sdev->host;
2845 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2846 struct ibmvfc_host *vhost = shost_priv(shost);
2847 unsigned long flags = 0;
2849 if (!rport || fc_remote_port_chkready(rport))
2852 spin_lock_irqsave(shost->host_lock, flags);
2853 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2854 spin_unlock_irqrestore(shost->host_lock, flags);
2859 * ibmvfc_target_alloc - Setup the target's task set value
2860 * @starget: struct scsi_target
2862 * Set the target's task set value so that error handling works as
2866 * 0 on success / -ENXIO if device does not exist
2868 static int ibmvfc_target_alloc(struct scsi_target *starget)
2870 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2871 struct ibmvfc_host *vhost = shost_priv(shost);
2872 unsigned long flags = 0;
2874 spin_lock_irqsave(shost->host_lock, flags);
2875 starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2876 spin_unlock_irqrestore(shost->host_lock, flags);
2881 * ibmvfc_slave_configure - Configure the device
2882 * @sdev: struct scsi_device device to configure
2884 * Enable allow_restart for a device if it is a disk. Adjust the
2885 * queue_depth here also.
2890 static int ibmvfc_slave_configure(struct scsi_device *sdev)
2892 struct Scsi_Host *shost = sdev->host;
2893 unsigned long flags = 0;
2895 spin_lock_irqsave(shost->host_lock, flags);
2896 if (sdev->type == TYPE_DISK)
2897 sdev->allow_restart = 1;
2898 spin_unlock_irqrestore(shost->host_lock, flags);
2903 * ibmvfc_change_queue_depth - Change the device's queue depth
2904 * @sdev: scsi device struct
2905 * @qdepth: depth to set
2906 * @reason: calling context
2911 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2913 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2914 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2916 return scsi_change_queue_depth(sdev, qdepth);
2919 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2920 struct device_attribute *attr, char *buf)
2922 struct Scsi_Host *shost = class_to_shost(dev);
2923 struct ibmvfc_host *vhost = shost_priv(shost);
2925 return snprintf(buf, PAGE_SIZE, "%s\n",
2926 vhost->login_buf->resp.partition_name);
2929 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2930 struct device_attribute *attr, char *buf)
2932 struct Scsi_Host *shost = class_to_shost(dev);
2933 struct ibmvfc_host *vhost = shost_priv(shost);
2935 return snprintf(buf, PAGE_SIZE, "%s\n",
2936 vhost->login_buf->resp.device_name);
2939 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2940 struct device_attribute *attr, char *buf)
2942 struct Scsi_Host *shost = class_to_shost(dev);
2943 struct ibmvfc_host *vhost = shost_priv(shost);
2945 return snprintf(buf, PAGE_SIZE, "%s\n",
2946 vhost->login_buf->resp.port_loc_code);
2949 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2950 struct device_attribute *attr, char *buf)
2952 struct Scsi_Host *shost = class_to_shost(dev);
2953 struct ibmvfc_host *vhost = shost_priv(shost);
2955 return snprintf(buf, PAGE_SIZE, "%s\n",
2956 vhost->login_buf->resp.drc_name);
2959 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2960 struct device_attribute *attr, char *buf)
2962 struct Scsi_Host *shost = class_to_shost(dev);
2963 struct ibmvfc_host *vhost = shost_priv(shost);
2964 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2967 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
2968 struct device_attribute *attr, char *buf)
2970 struct Scsi_Host *shost = class_to_shost(dev);
2971 struct ibmvfc_host *vhost = shost_priv(shost);
2972 return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
2976 * ibmvfc_show_log_level - Show the adapter's error logging level
2977 * @dev: class device struct
2981 * number of bytes printed to buffer
2983 static ssize_t ibmvfc_show_log_level(struct device *dev,
2984 struct device_attribute *attr, char *buf)
2986 struct Scsi_Host *shost = class_to_shost(dev);
2987 struct ibmvfc_host *vhost = shost_priv(shost);
2988 unsigned long flags = 0;
2991 spin_lock_irqsave(shost->host_lock, flags);
2992 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2993 spin_unlock_irqrestore(shost->host_lock, flags);
2998 * ibmvfc_store_log_level - Change the adapter's error logging level
2999 * @dev: class device struct
3003 * number of bytes printed to buffer
3005 static ssize_t ibmvfc_store_log_level(struct device *dev,
3006 struct device_attribute *attr,
3007 const char *buf, size_t count)
3009 struct Scsi_Host *shost = class_to_shost(dev);
3010 struct ibmvfc_host *vhost = shost_priv(shost);
3011 unsigned long flags = 0;
3013 spin_lock_irqsave(shost->host_lock, flags);
3014 vhost->log_level = simple_strtoul(buf, NULL, 10);
3015 spin_unlock_irqrestore(shost->host_lock, flags);
3019 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3020 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3021 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3022 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3023 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3024 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3025 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3026 ibmvfc_show_log_level, ibmvfc_store_log_level);
3028 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3030 * ibmvfc_read_trace - Dump the adapter trace
3031 * @filp: open sysfs file
3032 * @kobj: kobject struct
3033 * @bin_attr: bin_attribute struct
3036 * @count: buffer size
3039 * number of bytes printed to buffer
3041 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3042 struct bin_attribute *bin_attr,
3043 char *buf, loff_t off, size_t count)
3045 struct device *dev = container_of(kobj, struct device, kobj);
3046 struct Scsi_Host *shost = class_to_shost(dev);
3047 struct ibmvfc_host *vhost = shost_priv(shost);
3048 unsigned long flags = 0;
3049 int size = IBMVFC_TRACE_SIZE;
3050 char *src = (char *)vhost->trace;
3054 if (off + count > size) {
3059 spin_lock_irqsave(shost->host_lock, flags);
3060 memcpy(buf, &src[off], count);
3061 spin_unlock_irqrestore(shost->host_lock, flags);
3065 static struct bin_attribute ibmvfc_trace_attr = {
3071 .read = ibmvfc_read_trace,
3075 static struct device_attribute *ibmvfc_attrs[] = {
3076 &dev_attr_partition_name,
3077 &dev_attr_device_name,
3078 &dev_attr_port_loc_code,
3080 &dev_attr_npiv_version,
3081 &dev_attr_capabilities,
3082 &dev_attr_log_level,
3086 static struct scsi_host_template driver_template = {
3087 .module = THIS_MODULE,
3088 .name = "IBM POWER Virtual FC Adapter",
3089 .proc_name = IBMVFC_NAME,
3090 .queuecommand = ibmvfc_queuecommand,
3091 .eh_timed_out = fc_eh_timed_out,
3092 .eh_abort_handler = ibmvfc_eh_abort_handler,
3093 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3094 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3095 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3096 .slave_alloc = ibmvfc_slave_alloc,
3097 .slave_configure = ibmvfc_slave_configure,
3098 .target_alloc = ibmvfc_target_alloc,
3099 .scan_finished = ibmvfc_scan_finished,
3100 .change_queue_depth = ibmvfc_change_queue_depth,
3102 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3104 .sg_tablesize = SG_ALL,
3105 .max_sectors = IBMVFC_MAX_SECTORS,
3106 .shost_attrs = ibmvfc_attrs,
3107 .track_queue_depth = 1,
3111 * ibmvfc_next_async_crq - Returns the next entry in async queue
3112 * @vhost: ibmvfc host struct
3115 * Pointer to next entry in queue / NULL if empty
3117 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3119 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3120 struct ibmvfc_async_crq *crq;
3122 crq = &async_crq->msgs[async_crq->cur];
3123 if (crq->valid & 0x80) {
3124 if (++async_crq->cur == async_crq->size)
3134 * ibmvfc_next_crq - Returns the next entry in message queue
3135 * @vhost: ibmvfc host struct
3138 * Pointer to next entry in queue / NULL if empty
3140 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3142 struct ibmvfc_crq_queue *queue = &vhost->crq;
3143 struct ibmvfc_crq *crq;
3145 crq = &queue->msgs[queue->cur];
3146 if (crq->valid & 0x80) {
3147 if (++queue->cur == queue->size)
3157 * ibmvfc_interrupt - Interrupt handler
3158 * @irq: number of irq to handle, not used
3159 * @dev_instance: ibmvfc_host that received interrupt
3164 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3166 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3167 unsigned long flags;
3169 spin_lock_irqsave(vhost->host->host_lock, flags);
3170 vio_disable_interrupts(to_vio_dev(vhost->dev));
3171 tasklet_schedule(&vhost->tasklet);
3172 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3177 * ibmvfc_tasklet - Interrupt handler tasklet
3178 * @data: ibmvfc host struct
3183 static void ibmvfc_tasklet(void *data)
3185 struct ibmvfc_host *vhost = data;
3186 struct vio_dev *vdev = to_vio_dev(vhost->dev);
3187 struct ibmvfc_crq *crq;
3188 struct ibmvfc_async_crq *async;
3189 unsigned long flags;
3192 spin_lock_irqsave(vhost->host->host_lock, flags);
3194 /* Pull all the valid messages off the async CRQ */
3195 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3196 ibmvfc_handle_async(async, vhost);
3201 /* Pull all the valid messages off the CRQ */
3202 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3203 ibmvfc_handle_crq(crq, vhost);
3208 vio_enable_interrupts(vdev);
3209 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3210 vio_disable_interrupts(vdev);
3211 ibmvfc_handle_async(async, vhost);
3214 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3215 vio_disable_interrupts(vdev);
3216 ibmvfc_handle_crq(crq, vhost);
3223 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3227 * ibmvfc_init_tgt - Set the next init job step for the target
3228 * @tgt: ibmvfc target struct
3229 * @job_step: job step to perform
3232 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3233 void (*job_step) (struct ibmvfc_target *))
3235 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
3236 tgt->job_step = job_step;
3237 wake_up(&tgt->vhost->work_wait_q);
3241 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3242 * @tgt: ibmvfc target struct
3243 * @job_step: initialization job step
3245 * Returns: 1 if step will be retried / 0 if not
3248 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3249 void (*job_step) (struct ibmvfc_target *))
3251 if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3252 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3253 wake_up(&tgt->vhost->work_wait_q);
3256 ibmvfc_init_tgt(tgt, job_step);
3260 /* Defined in FC-LS */
3261 static const struct {
3278 * ibmvfc_get_prli_rsp - Find PRLI response index
3279 * @flags: PRLI response flags
3282 static int ibmvfc_get_prli_rsp(u16 flags)
3285 int code = (flags & 0x0f00) >> 8;
3287 for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3288 if (prli_rsp[i].code == code)
3295 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3296 * @evt: ibmvfc event struct
3299 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3301 struct ibmvfc_target *tgt = evt->tgt;
3302 struct ibmvfc_host *vhost = evt->vhost;
3303 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3304 struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3305 u32 status = be16_to_cpu(rsp->common.status);
3306 int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3308 vhost->discovery_threads--;
3309 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3311 case IBMVFC_MAD_SUCCESS:
3312 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3313 parms->type, parms->flags, parms->service_parms);
3315 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3316 index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3317 if (prli_rsp[index].logged_in) {
3318 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3319 tgt->need_login = 0;
3321 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3322 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3323 if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3324 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3327 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3328 } else if (prli_rsp[index].retry)
3329 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3331 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3333 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3335 case IBMVFC_MAD_DRIVER_FAILED:
3337 case IBMVFC_MAD_CRQ_ERROR:
3338 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3340 case IBMVFC_MAD_FAILED:
3342 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3343 be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3344 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3345 else if (tgt->logo_rcvd)
3346 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3347 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3348 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3350 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3352 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3353 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3354 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
3358 kref_put(&tgt->kref, ibmvfc_release_tgt);
3359 ibmvfc_free_event(evt);
3360 wake_up(&vhost->work_wait_q);
3364 * ibmvfc_tgt_send_prli - Send a process login
3365 * @tgt: ibmvfc target struct
3368 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3370 struct ibmvfc_process_login *prli;
3371 struct ibmvfc_host *vhost = tgt->vhost;
3372 struct ibmvfc_event *evt;
3374 if (vhost->discovery_threads >= disc_threads)
3377 kref_get(&tgt->kref);
3378 evt = ibmvfc_get_event(vhost);
3379 vhost->discovery_threads++;
3380 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3382 prli = &evt->iu.prli;
3383 memset(prli, 0, sizeof(*prli));
3384 prli->common.version = cpu_to_be32(1);
3385 prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
3386 prli->common.length = cpu_to_be16(sizeof(*prli));
3387 prli->scsi_id = cpu_to_be64(tgt->scsi_id);
3389 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3390 prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
3391 prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
3392 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
3395 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
3397 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3398 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3399 vhost->discovery_threads--;
3400 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3401 kref_put(&tgt->kref, ibmvfc_release_tgt);
3403 tgt_dbg(tgt, "Sent process login\n");
3407 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3408 * @evt: ibmvfc event struct
3411 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3413 struct ibmvfc_target *tgt = evt->tgt;
3414 struct ibmvfc_host *vhost = evt->vhost;
3415 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3416 u32 status = be16_to_cpu(rsp->common.status);
3417 int level = IBMVFC_DEFAULT_LOG_LEVEL;
3419 vhost->discovery_threads--;
3420 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3422 case IBMVFC_MAD_SUCCESS:
3423 tgt_dbg(tgt, "Port Login succeeded\n");
3424 if (tgt->ids.port_name &&
3425 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3427 tgt_dbg(tgt, "Port re-init required\n");
3430 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3431 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3432 tgt->ids.port_id = tgt->scsi_id;
3433 memcpy(&tgt->service_parms, &rsp->service_parms,
3434 sizeof(tgt->service_parms));
3435 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3436 sizeof(tgt->service_parms_change));
3437 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3439 case IBMVFC_MAD_DRIVER_FAILED:
3441 case IBMVFC_MAD_CRQ_ERROR:
3442 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3444 case IBMVFC_MAD_FAILED:
3446 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3447 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3449 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3451 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3452 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3453 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
3454 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
3455 ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
3459 kref_put(&tgt->kref, ibmvfc_release_tgt);
3460 ibmvfc_free_event(evt);
3461 wake_up(&vhost->work_wait_q);
3465 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3466 * @tgt: ibmvfc target struct
3469 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3471 struct ibmvfc_port_login *plogi;
3472 struct ibmvfc_host *vhost = tgt->vhost;
3473 struct ibmvfc_event *evt;
3475 if (vhost->discovery_threads >= disc_threads)
3478 kref_get(&tgt->kref);
3480 evt = ibmvfc_get_event(vhost);
3481 vhost->discovery_threads++;
3482 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3483 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3485 plogi = &evt->iu.plogi;
3486 memset(plogi, 0, sizeof(*plogi));
3487 plogi->common.version = cpu_to_be32(1);
3488 plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
3489 plogi->common.length = cpu_to_be16(sizeof(*plogi));
3490 plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
3492 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3493 vhost->discovery_threads--;
3494 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3495 kref_put(&tgt->kref, ibmvfc_release_tgt);
3497 tgt_dbg(tgt, "Sent port login\n");
3501 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3502 * @evt: ibmvfc event struct
3505 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3507 struct ibmvfc_target *tgt = evt->tgt;
3508 struct ibmvfc_host *vhost = evt->vhost;
3509 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
3510 u32 status = be16_to_cpu(rsp->common.status);
3512 vhost->discovery_threads--;
3513 ibmvfc_free_event(evt);
3514 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3517 case IBMVFC_MAD_SUCCESS:
3518 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3520 case IBMVFC_MAD_DRIVER_FAILED:
3521 kref_put(&tgt->kref, ibmvfc_release_tgt);
3522 wake_up(&vhost->work_wait_q);
3524 case IBMVFC_MAD_FAILED:
3526 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3530 if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
3531 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3532 else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
3533 tgt->scsi_id != tgt->new_scsi_id)
3534 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3535 kref_put(&tgt->kref, ibmvfc_release_tgt);
3536 wake_up(&vhost->work_wait_q);
3540 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3541 * @tgt: ibmvfc target struct
3544 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3546 struct ibmvfc_implicit_logout *mad;
3547 struct ibmvfc_host *vhost = tgt->vhost;
3548 struct ibmvfc_event *evt;
3550 if (vhost->discovery_threads >= disc_threads)
3553 kref_get(&tgt->kref);
3554 evt = ibmvfc_get_event(vhost);
3555 vhost->discovery_threads++;
3556 ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
3558 mad = &evt->iu.implicit_logout;
3559 memset(mad, 0, sizeof(*mad));
3560 mad->common.version = cpu_to_be32(1);
3561 mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
3562 mad->common.length = cpu_to_be16(sizeof(*mad));
3563 mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
3565 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3566 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3567 vhost->discovery_threads--;
3568 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3569 kref_put(&tgt->kref, ibmvfc_release_tgt);
3571 tgt_dbg(tgt, "Sent Implicit Logout\n");
3575 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3576 * @mad: ibmvfc passthru mad struct
3577 * @tgt: ibmvfc target struct
3580 * 1 if PLOGI needed / 0 if PLOGI not needed
3582 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3583 struct ibmvfc_target *tgt)
3585 if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
3587 if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
3589 if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
3595 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3596 * @evt: ibmvfc event struct
3599 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3601 struct ibmvfc_target *tgt = evt->tgt;
3602 struct ibmvfc_host *vhost = evt->vhost;
3603 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3604 u32 status = be16_to_cpu(mad->common.status);
3605 u8 fc_reason, fc_explain;
3607 vhost->discovery_threads--;
3608 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3609 del_timer(&tgt->timer);
3612 case IBMVFC_MAD_SUCCESS:
3613 tgt_dbg(tgt, "ADISC succeeded\n");
3614 if (ibmvfc_adisc_needs_plogi(mad, tgt))
3615 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3617 case IBMVFC_MAD_DRIVER_FAILED:
3619 case IBMVFC_MAD_FAILED:
3621 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3622 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
3623 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
3624 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3625 ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
3626 be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
3627 ibmvfc_get_fc_type(fc_reason), fc_reason,
3628 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3632 kref_put(&tgt->kref, ibmvfc_release_tgt);
3633 ibmvfc_free_event(evt);
3634 wake_up(&vhost->work_wait_q);
3638 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3639 * @evt: ibmvfc event struct
3642 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3644 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3646 memset(mad, 0, sizeof(*mad));
3647 mad->common.version = cpu_to_be32(1);
3648 mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
3649 mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
3650 mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3651 offsetof(struct ibmvfc_passthru_mad, iu));
3652 mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
3653 mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3654 mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
3655 mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3656 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3657 offsetof(struct ibmvfc_passthru_fc_iu, payload));
3658 mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3659 mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3660 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3661 offsetof(struct ibmvfc_passthru_fc_iu, response));
3662 mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
3666 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3667 * @evt: ibmvfc event struct
3669 * Just cleanup this event struct. Everything else is handled by
3670 * the ADISC completion handler. If the ADISC never actually comes
3671 * back, we still have the timer running on the ADISC event struct
3672 * which will fire and cause the CRQ to get reset.
3675 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3677 struct ibmvfc_host *vhost = evt->vhost;
3678 struct ibmvfc_target *tgt = evt->tgt;
3680 tgt_dbg(tgt, "ADISC cancel complete\n");
3681 vhost->abort_threads--;
3682 ibmvfc_free_event(evt);
3683 kref_put(&tgt->kref, ibmvfc_release_tgt);
3684 wake_up(&vhost->work_wait_q);
3688 * ibmvfc_adisc_timeout - Handle an ADISC timeout
3689 * @tgt: ibmvfc target struct
3691 * If an ADISC times out, send a cancel. If the cancel times
3692 * out, reset the CRQ. When the ADISC comes back as cancelled,
3693 * log back into the target.
3695 static void ibmvfc_adisc_timeout(struct timer_list *t)
3697 struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
3698 struct ibmvfc_host *vhost = tgt->vhost;
3699 struct ibmvfc_event *evt;
3700 struct ibmvfc_tmf *tmf;
3701 unsigned long flags;
3704 tgt_dbg(tgt, "ADISC timeout\n");
3705 spin_lock_irqsave(vhost->host->host_lock, flags);
3706 if (vhost->abort_threads >= disc_threads ||
3707 tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3708 vhost->state != IBMVFC_INITIALIZING ||
3709 vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3710 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3714 vhost->abort_threads++;
3715 kref_get(&tgt->kref);
3716 evt = ibmvfc_get_event(vhost);
3717 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3721 memset(tmf, 0, sizeof(*tmf));
3722 tmf->common.version = cpu_to_be32(1);
3723 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
3724 tmf->common.length = cpu_to_be16(sizeof(*tmf));
3725 tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
3726 tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
3728 rc = ibmvfc_send_event(evt, vhost, default_timeout);
3731 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3732 vhost->abort_threads--;
3733 kref_put(&tgt->kref, ibmvfc_release_tgt);
3734 __ibmvfc_reset_host(vhost);
3736 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3737 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3741 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3742 * @tgt: ibmvfc target struct
3744 * When sending an ADISC we end up with two timers running. The
3745 * first timer is the timer in the ibmvfc target struct. If this
3746 * fires, we send a cancel to the target. The second timer is the
3747 * timer on the ibmvfc event for the ADISC, which is longer. If that
3748 * fires, it means the ADISC timed out and our attempt to cancel it
3749 * also failed, so we need to reset the CRQ.
3751 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3753 struct ibmvfc_passthru_mad *mad;
3754 struct ibmvfc_host *vhost = tgt->vhost;
3755 struct ibmvfc_event *evt;
3757 if (vhost->discovery_threads >= disc_threads)
3760 kref_get(&tgt->kref);
3761 evt = ibmvfc_get_event(vhost);
3762 vhost->discovery_threads++;
3763 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3766 ibmvfc_init_passthru(evt);
3767 mad = &evt->iu.passthru;
3768 mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
3769 mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
3770 mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
3772 mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
3773 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3774 sizeof(vhost->login_buf->resp.port_name));
3775 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3776 sizeof(vhost->login_buf->resp.node_name));
3777 mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
3779 if (timer_pending(&tgt->timer))
3780 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
3782 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
3783 add_timer(&tgt->timer);
3786 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3787 if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
3788 vhost->discovery_threads--;
3789 del_timer(&tgt->timer);
3790 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3791 kref_put(&tgt->kref, ibmvfc_release_tgt);
3793 tgt_dbg(tgt, "Sent ADISC\n");
3797 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3798 * @evt: ibmvfc event struct
3801 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3803 struct ibmvfc_target *tgt = evt->tgt;
3804 struct ibmvfc_host *vhost = evt->vhost;
3805 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3806 u32 status = be16_to_cpu(rsp->common.status);
3807 int level = IBMVFC_DEFAULT_LOG_LEVEL;
3809 vhost->discovery_threads--;
3810 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3812 case IBMVFC_MAD_SUCCESS:
3813 tgt_dbg(tgt, "Query Target succeeded\n");
3814 tgt->new_scsi_id = be64_to_cpu(rsp->scsi_id);
3815 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
3816 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3818 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
3820 case IBMVFC_MAD_DRIVER_FAILED:
3822 case IBMVFC_MAD_CRQ_ERROR:
3823 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3825 case IBMVFC_MAD_FAILED:
3827 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3828 be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3829 be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
3830 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3831 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3832 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3834 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3836 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3837 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3838 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
3839 ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
3840 ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
3845 kref_put(&tgt->kref, ibmvfc_release_tgt);
3846 ibmvfc_free_event(evt);
3847 wake_up(&vhost->work_wait_q);
3851 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3852 * @tgt: ibmvfc target struct
3855 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3857 struct ibmvfc_query_tgt *query_tgt;
3858 struct ibmvfc_host *vhost = tgt->vhost;
3859 struct ibmvfc_event *evt;
3861 if (vhost->discovery_threads >= disc_threads)
3864 kref_get(&tgt->kref);
3865 evt = ibmvfc_get_event(vhost);
3866 vhost->discovery_threads++;
3868 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3869 query_tgt = &evt->iu.query_tgt;
3870 memset(query_tgt, 0, sizeof(*query_tgt));
3871 query_tgt->common.version = cpu_to_be32(1);
3872 query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
3873 query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
3874 query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
3876 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3877 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3878 vhost->discovery_threads--;
3879 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3880 kref_put(&tgt->kref, ibmvfc_release_tgt);
3882 tgt_dbg(tgt, "Sent Query Target\n");
3886 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3887 * @vhost: ibmvfc host struct
3888 * @scsi_id: SCSI ID to allocate target for
3891 * 0 on success / other on failure
3893 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3895 struct ibmvfc_target *tgt;
3896 unsigned long flags;
3898 spin_lock_irqsave(vhost->host->host_lock, flags);
3899 list_for_each_entry(tgt, &vhost->targets, queue) {
3900 if (tgt->scsi_id == scsi_id) {
3901 if (tgt->need_login)
3902 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3906 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3908 tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
3909 memset(tgt, 0, sizeof(*tgt));
3910 tgt->scsi_id = scsi_id;
3911 tgt->new_scsi_id = scsi_id;
3913 tgt->need_login = 1;
3914 tgt->cancel_key = vhost->task_set++;
3915 timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
3916 kref_init(&tgt->kref);
3917 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3918 spin_lock_irqsave(vhost->host->host_lock, flags);
3919 list_add_tail(&tgt->queue, &vhost->targets);
3922 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3927 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3928 * @vhost: ibmvfc host struct
3931 * 0 on success / other on failure
3933 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3937 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3938 rc = ibmvfc_alloc_target(vhost,
3939 be32_to_cpu(vhost->disc_buf->scsi_id[i]) &
3940 IBMVFC_DISC_TGT_SCSI_ID_MASK);
3946 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3947 * @evt: ibmvfc event struct
3950 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3952 struct ibmvfc_host *vhost = evt->vhost;
3953 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3954 u32 mad_status = be16_to_cpu(rsp->common.status);
3955 int level = IBMVFC_DEFAULT_LOG_LEVEL;
3957 switch (mad_status) {
3958 case IBMVFC_MAD_SUCCESS:
3959 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3960 vhost->num_targets = be32_to_cpu(rsp->num_written);
3961 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3963 case IBMVFC_MAD_FAILED:
3964 level += ibmvfc_retry_host_init(vhost);
3965 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
3966 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3967 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
3969 case IBMVFC_MAD_DRIVER_FAILED:
3972 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3973 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3977 ibmvfc_free_event(evt);
3978 wake_up(&vhost->work_wait_q);
3982 * ibmvfc_discover_targets - Send Discover Targets MAD
3983 * @vhost: ibmvfc host struct
3986 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3988 struct ibmvfc_discover_targets *mad;
3989 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3991 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3992 mad = &evt->iu.discover_targets;
3993 memset(mad, 0, sizeof(*mad));
3994 mad->common.version = cpu_to_be32(1);
3995 mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
3996 mad->common.length = cpu_to_be16(sizeof(*mad));
3997 mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
3998 mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
3999 mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4000 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4002 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4003 ibmvfc_dbg(vhost, "Sent discover targets\n");
4005 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4009 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4010 * @evt: ibmvfc event struct
4013 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4015 struct ibmvfc_host *vhost = evt->vhost;
4016 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
4017 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
4018 unsigned int npiv_max_sectors;
4019 int level = IBMVFC_DEFAULT_LOG_LEVEL;
4021 switch (mad_status) {
4022 case IBMVFC_MAD_SUCCESS:
4023 ibmvfc_free_event(evt);
4025 case IBMVFC_MAD_FAILED:
4026 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4027 level += ibmvfc_retry_host_init(vhost);
4029 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4030 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
4031 ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4032 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4033 ibmvfc_free_event(evt);
4035 case IBMVFC_MAD_CRQ_ERROR:
4036 ibmvfc_retry_host_init(vhost);
4037 case IBMVFC_MAD_DRIVER_FAILED:
4038 ibmvfc_free_event(evt);
4041 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4042 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4043 ibmvfc_free_event(evt);
4047 vhost->client_migrated = 0;
4049 if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
4050 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4052 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4053 wake_up(&vhost->work_wait_q);
4057 if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
4058 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4060 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4061 wake_up(&vhost->work_wait_q);
4065 vhost->logged_in = 1;
4066 npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
4067 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4068 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4069 rsp->drc_name, npiv_max_sectors);
4071 fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
4072 fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
4073 fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
4074 fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
4075 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4076 fc_host_supported_classes(vhost->host) = 0;
4077 if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
4078 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
4079 if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
4080 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
4081 if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
4082 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4083 fc_host_maxframe_size(vhost->host) =
4084 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
4086 vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
4087 vhost->host->max_sectors = npiv_max_sectors;
4088 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4089 wake_up(&vhost->work_wait_q);
4093 * ibmvfc_npiv_login - Sends NPIV login
4094 * @vhost: ibmvfc host struct
4097 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4099 struct ibmvfc_npiv_login_mad *mad;
4100 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4102 ibmvfc_gather_partition_info(vhost);
4103 ibmvfc_set_login_info(vhost);
4104 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4106 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4107 mad = &evt->iu.npiv_login;
4108 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
4109 mad->common.version = cpu_to_be32(1);
4110 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
4111 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
4112 mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
4113 mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
4115 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4117 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4118 ibmvfc_dbg(vhost, "Sent NPIV login\n");
4120 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4124 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4125 * @vhost: ibmvfc host struct
4128 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4130 struct ibmvfc_host *vhost = evt->vhost;
4131 u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
4133 ibmvfc_free_event(evt);
4135 switch (mad_status) {
4136 case IBMVFC_MAD_SUCCESS:
4137 if (list_empty(&vhost->sent) &&
4138 vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
4139 ibmvfc_init_host(vhost);
4143 case IBMVFC_MAD_FAILED:
4144 case IBMVFC_MAD_NOT_SUPPORTED:
4145 case IBMVFC_MAD_CRQ_ERROR:
4146 case IBMVFC_MAD_DRIVER_FAILED:
4148 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4152 ibmvfc_hard_reset_host(vhost);
4156 * ibmvfc_npiv_logout - Issue an NPIV Logout
4157 * @vhost: ibmvfc host struct
4160 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4162 struct ibmvfc_npiv_logout_mad *mad;
4163 struct ibmvfc_event *evt;
4165 evt = ibmvfc_get_event(vhost);
4166 ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4168 mad = &evt->iu.npiv_logout;
4169 memset(mad, 0, sizeof(*mad));
4170 mad->common.version = cpu_to_be32(1);
4171 mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
4172 mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
4174 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4176 if (!ibmvfc_send_event(evt, vhost, default_timeout))
4177 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4179 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4183 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4184 * @vhost: ibmvfc host struct
4187 * 1 if work to do / 0 if not
4189 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4191 struct ibmvfc_target *tgt;
4193 list_for_each_entry(tgt, &vhost->targets, queue) {
4194 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4195 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4203 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4204 * @vhost: ibmvfc host struct
4207 * 1 if work to do / 0 if not
4209 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4211 struct ibmvfc_target *tgt;
4213 if (kthread_should_stop())
4215 switch (vhost->action) {
4216 case IBMVFC_HOST_ACTION_NONE:
4217 case IBMVFC_HOST_ACTION_INIT_WAIT:
4218 case IBMVFC_HOST_ACTION_LOGO_WAIT:
4220 case IBMVFC_HOST_ACTION_TGT_INIT:
4221 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4222 if (vhost->discovery_threads == disc_threads)
4224 list_for_each_entry(tgt, &vhost->targets, queue)
4225 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4227 list_for_each_entry(tgt, &vhost->targets, queue)
4228 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4231 case IBMVFC_HOST_ACTION_LOGO:
4232 case IBMVFC_HOST_ACTION_INIT:
4233 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4234 case IBMVFC_HOST_ACTION_TGT_DEL:
4235 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4236 case IBMVFC_HOST_ACTION_QUERY:
4237 case IBMVFC_HOST_ACTION_RESET:
4238 case IBMVFC_HOST_ACTION_REENABLE:
4247 * ibmvfc_work_to_do - Is there task level work to do?
4248 * @vhost: ibmvfc host struct
4251 * 1 if work to do / 0 if not
4253 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4255 unsigned long flags;
4258 spin_lock_irqsave(vhost->host->host_lock, flags);
4259 rc = __ibmvfc_work_to_do(vhost);
4260 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4265 * ibmvfc_log_ae - Log async events if necessary
4266 * @vhost: ibmvfc host struct
4267 * @events: events to log
4270 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4272 if (events & IBMVFC_AE_RSCN)
4273 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4274 if ((events & IBMVFC_AE_LINKDOWN) &&
4275 vhost->state >= IBMVFC_HALTED)
4276 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4277 if ((events & IBMVFC_AE_LINKUP) &&
4278 vhost->state == IBMVFC_INITIALIZING)
4279 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4283 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4284 * @tgt: ibmvfc target struct
4287 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4289 struct ibmvfc_host *vhost = tgt->vhost;
4290 struct fc_rport *rport;
4291 unsigned long flags;
4293 tgt_dbg(tgt, "Adding rport\n");
4294 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4295 spin_lock_irqsave(vhost->host->host_lock, flags);
4297 if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4298 tgt_dbg(tgt, "Deleting rport\n");
4299 list_del(&tgt->queue);
4300 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4301 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4302 fc_remote_port_delete(rport);
4303 del_timer_sync(&tgt->timer);
4304 kref_put(&tgt->kref, ibmvfc_release_tgt);
4306 } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4307 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4312 tgt_dbg(tgt, "rport add succeeded\n");
4314 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
4315 rport->supported_classes = 0;
4316 tgt->target_id = rport->scsi_target_id;
4317 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
4318 rport->supported_classes |= FC_COS_CLASS1;
4319 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
4320 rport->supported_classes |= FC_COS_CLASS2;
4321 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
4322 rport->supported_classes |= FC_COS_CLASS3;
4324 blk_queue_max_segments(rport->rqst_q, 1);
4326 tgt_dbg(tgt, "rport add failed\n");
4327 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4331 * ibmvfc_do_work - Do task level work
4332 * @vhost: ibmvfc host struct
4335 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4337 struct ibmvfc_target *tgt;
4338 unsigned long flags;
4339 struct fc_rport *rport;
4342 ibmvfc_log_ae(vhost, vhost->events_to_log);
4343 spin_lock_irqsave(vhost->host->host_lock, flags);
4344 vhost->events_to_log = 0;
4345 switch (vhost->action) {
4346 case IBMVFC_HOST_ACTION_NONE:
4347 case IBMVFC_HOST_ACTION_LOGO_WAIT:
4348 case IBMVFC_HOST_ACTION_INIT_WAIT:
4350 case IBMVFC_HOST_ACTION_RESET:
4351 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4352 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4353 rc = ibmvfc_reset_crq(vhost);
4354 spin_lock_irqsave(vhost->host->host_lock, flags);
4356 vio_enable_interrupts(to_vio_dev(vhost->dev));
4357 if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4358 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
4359 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4360 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4363 case IBMVFC_HOST_ACTION_REENABLE:
4364 vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4365 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4366 rc = ibmvfc_reenable_crq_queue(vhost);
4367 spin_lock_irqsave(vhost->host->host_lock, flags);
4368 if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4369 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4370 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4373 case IBMVFC_HOST_ACTION_LOGO:
4374 vhost->job_step(vhost);
4376 case IBMVFC_HOST_ACTION_INIT:
4377 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
4378 if (vhost->delay_init) {
4379 vhost->delay_init = 0;
4380 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4384 vhost->job_step(vhost);
4386 case IBMVFC_HOST_ACTION_QUERY:
4387 list_for_each_entry(tgt, &vhost->targets, queue)
4388 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4389 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4391 case IBMVFC_HOST_ACTION_QUERY_TGTS:
4392 list_for_each_entry(tgt, &vhost->targets, queue) {
4393 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4399 if (!ibmvfc_dev_init_to_do(vhost))
4400 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4402 case IBMVFC_HOST_ACTION_TGT_DEL:
4403 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4404 list_for_each_entry(tgt, &vhost->targets, queue) {
4405 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4406 tgt_dbg(tgt, "Deleting rport\n");
4409 list_del(&tgt->queue);
4410 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4411 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4413 fc_remote_port_delete(rport);
4414 del_timer_sync(&tgt->timer);
4415 kref_put(&tgt->kref, ibmvfc_release_tgt);
4420 if (vhost->state == IBMVFC_INITIALIZING) {
4421 if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
4422 if (vhost->reinit) {
4424 scsi_block_requests(vhost->host);
4425 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4426 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4428 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4429 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4430 wake_up(&vhost->init_wait_q);
4431 schedule_work(&vhost->rport_add_work_q);
4432 vhost->init_retries = 0;
4433 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4434 scsi_unblock_requests(vhost->host);
4439 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4440 vhost->job_step = ibmvfc_discover_targets;
4443 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4444 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4445 scsi_unblock_requests(vhost->host);
4446 wake_up(&vhost->init_wait_q);
4450 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4451 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4452 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4453 ibmvfc_alloc_targets(vhost);
4454 spin_lock_irqsave(vhost->host->host_lock, flags);
4456 case IBMVFC_HOST_ACTION_TGT_INIT:
4457 list_for_each_entry(tgt, &vhost->targets, queue) {
4458 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4464 if (!ibmvfc_dev_init_to_do(vhost))
4465 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
4471 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4475 * ibmvfc_work - Do task level work
4476 * @data: ibmvfc host struct
4481 static int ibmvfc_work(void *data)
4483 struct ibmvfc_host *vhost = data;
4486 set_user_nice(current, MIN_NICE);
4489 rc = wait_event_interruptible(vhost->work_wait_q,
4490 ibmvfc_work_to_do(vhost));
4494 if (kthread_should_stop())
4497 ibmvfc_do_work(vhost);
4500 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4505 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4506 * @vhost: ibmvfc host struct
4508 * Allocates a page for messages, maps it for dma, and registers
4509 * the crq with the hypervisor.
4512 * zero on success / other on failure
4514 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4516 int rc, retrc = -ENOMEM;
4517 struct device *dev = vhost->dev;
4518 struct vio_dev *vdev = to_vio_dev(dev);
4519 struct ibmvfc_crq_queue *crq = &vhost->crq;
4522 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4527 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4528 crq->msg_token = dma_map_single(dev, crq->msgs,
4529 PAGE_SIZE, DMA_BIDIRECTIONAL);
4531 if (dma_mapping_error(dev, crq->msg_token))
4534 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4535 crq->msg_token, PAGE_SIZE);
4537 if (rc == H_RESOURCE)
4538 /* maybe kexecing and resource is busy. try a reset */
4539 retrc = rc = ibmvfc_reset_crq(vhost);
4542 dev_warn(dev, "Partner adapter not ready\n");
4544 dev_warn(dev, "Error %d opening adapter\n", rc);
4545 goto reg_crq_failed;
4550 tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4552 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4553 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4554 goto req_irq_failed;
4557 if ((rc = vio_enable_interrupts(vdev))) {
4558 dev_err(dev, "Error %d enabling interrupts\n", rc);
4559 goto req_irq_failed;
4567 tasklet_kill(&vhost->tasklet);
4569 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4570 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4572 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4574 free_page((unsigned long)crq->msgs);
4579 * ibmvfc_free_mem - Free memory for vhost
4580 * @vhost: ibmvfc host struct
4585 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4587 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4590 mempool_destroy(vhost->tgt_pool);
4591 kfree(vhost->trace);
4592 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4593 vhost->disc_buf_dma);
4594 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4595 vhost->login_buf, vhost->login_buf_dma);
4596 dma_pool_destroy(vhost->sg_pool);
4597 dma_unmap_single(vhost->dev, async_q->msg_token,
4598 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4599 free_page((unsigned long)async_q->msgs);
4604 * ibmvfc_alloc_mem - Allocate memory for vhost
4605 * @vhost: ibmvfc host struct
4608 * 0 on success / non-zero on failure
4610 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4612 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4613 struct device *dev = vhost->dev;
4616 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4617 if (!async_q->msgs) {
4618 dev_err(dev, "Couldn't allocate async queue.\n");
4622 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4623 async_q->msg_token = dma_map_single(dev, async_q->msgs,
4624 async_q->size * sizeof(*async_q->msgs),
4627 if (dma_mapping_error(dev, async_q->msg_token)) {
4628 dev_err(dev, "Failed to map async queue\n");
4629 goto free_async_crq;
4632 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4633 SG_ALL * sizeof(struct srp_direct_buf),
4634 sizeof(struct srp_direct_buf), 0);
4636 if (!vhost->sg_pool) {
4637 dev_err(dev, "Failed to allocate sg pool\n");
4638 goto unmap_async_crq;
4641 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
4642 &vhost->login_buf_dma, GFP_KERNEL);
4644 if (!vhost->login_buf) {
4645 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
4649 vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
4650 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
4651 &vhost->disc_buf_dma, GFP_KERNEL);
4653 if (!vhost->disc_buf) {
4654 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
4655 goto free_login_buffer;
4658 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
4659 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
4662 goto free_disc_buffer;
4664 vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
4665 sizeof(struct ibmvfc_target));
4667 if (!vhost->tgt_pool) {
4668 dev_err(dev, "Couldn't allocate target memory pool\n");
4676 kfree(vhost->trace);
4678 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
4679 vhost->disc_buf_dma);
4681 dma_free_coherent(dev, sizeof(*vhost->login_buf),
4682 vhost->login_buf, vhost->login_buf_dma);
4684 dma_pool_destroy(vhost->sg_pool);
4686 dma_unmap_single(dev, async_q->msg_token,
4687 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4689 free_page((unsigned long)async_q->msgs);
4696 * ibmvfc_rport_add_thread - Worker thread for rport adds
4697 * @work: work struct
4700 static void ibmvfc_rport_add_thread(struct work_struct *work)
4702 struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
4704 struct ibmvfc_target *tgt;
4705 struct fc_rport *rport;
4706 unsigned long flags;
4710 spin_lock_irqsave(vhost->host->host_lock, flags);
4713 if (vhost->state != IBMVFC_ACTIVE)
4716 list_for_each_entry(tgt, &vhost->targets, queue) {
4717 if (tgt->add_rport) {
4720 kref_get(&tgt->kref);
4723 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4724 ibmvfc_tgt_add_rport(tgt);
4725 } else if (get_device(&rport->dev)) {
4726 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4727 tgt_dbg(tgt, "Setting rport roles\n");
4728 fc_remote_port_rolechg(rport, tgt->ids.roles);
4729 put_device(&rport->dev);
4731 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4734 kref_put(&tgt->kref, ibmvfc_release_tgt);
4735 spin_lock_irqsave(vhost->host->host_lock, flags);
4741 if (vhost->state == IBMVFC_ACTIVE)
4742 vhost->scan_complete = 1;
4743 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4748 * ibmvfc_probe - Adapter hot plug add entry point
4749 * @vdev: vio device struct
4750 * @id: vio device id struct
4753 * 0 on success / non-zero on failure
4755 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
4757 struct ibmvfc_host *vhost;
4758 struct Scsi_Host *shost;
4759 struct device *dev = &vdev->dev;
4763 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
4765 dev_err(dev, "Couldn't allocate host data\n");
4769 shost->transportt = ibmvfc_transport_template;
4770 shost->can_queue = max_requests;
4771 shost->max_lun = max_lun;
4772 shost->max_id = max_targets;
4773 shost->max_sectors = IBMVFC_MAX_SECTORS;
4774 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
4775 shost->unique_id = shost->host_no;
4777 vhost = shost_priv(shost);
4778 INIT_LIST_HEAD(&vhost->sent);
4779 INIT_LIST_HEAD(&vhost->free);
4780 INIT_LIST_HEAD(&vhost->targets);
4781 sprintf(vhost->name, IBMVFC_NAME);
4782 vhost->host = shost;
4784 vhost->partition_number = -1;
4785 vhost->log_level = log_level;
4786 vhost->task_set = 1;
4787 strcpy(vhost->partition_name, "UNKNOWN");
4788 init_waitqueue_head(&vhost->work_wait_q);
4789 init_waitqueue_head(&vhost->init_wait_q);
4790 INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
4791 mutex_init(&vhost->passthru_mutex);
4793 if ((rc = ibmvfc_alloc_mem(vhost)))
4794 goto free_scsi_host;
4796 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
4799 if (IS_ERR(vhost->work_thread)) {
4800 dev_err(dev, "Couldn't create kernel thread: %ld\n",
4801 PTR_ERR(vhost->work_thread));
4805 if ((rc = ibmvfc_init_crq(vhost))) {
4806 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4810 if ((rc = ibmvfc_init_event_pool(vhost))) {
4811 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
4815 if ((rc = scsi_add_host(shost, dev)))
4816 goto release_event_pool;
4818 fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
4820 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
4821 &ibmvfc_trace_attr))) {
4822 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
4826 if (shost_to_fc_host(shost)->rqst_q)
4827 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
4828 dev_set_drvdata(dev, vhost);
4829 spin_lock(&ibmvfc_driver_lock);
4830 list_add_tail(&vhost->queue, &ibmvfc_head);
4831 spin_unlock(&ibmvfc_driver_lock);
4833 ibmvfc_send_crq_init(vhost);
4834 scsi_scan_host(shost);
4838 scsi_remove_host(shost);
4840 ibmvfc_free_event_pool(vhost);
4842 ibmvfc_release_crq_queue(vhost);
4844 kthread_stop(vhost->work_thread);
4846 ibmvfc_free_mem(vhost);
4848 scsi_host_put(shost);
4855 * ibmvfc_remove - Adapter hot plug remove entry point
4856 * @vdev: vio device struct
4861 static int ibmvfc_remove(struct vio_dev *vdev)
4863 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
4864 unsigned long flags;
4867 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
4869 spin_lock_irqsave(vhost->host->host_lock, flags);
4870 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
4871 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4873 ibmvfc_wait_while_resetting(vhost);
4874 ibmvfc_release_crq_queue(vhost);
4875 kthread_stop(vhost->work_thread);
4876 fc_remove_host(vhost->host);
4877 scsi_remove_host(vhost->host);
4879 spin_lock_irqsave(vhost->host->host_lock, flags);
4880 ibmvfc_purge_requests(vhost, DID_ERROR);
4881 ibmvfc_free_event_pool(vhost);
4882 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4884 ibmvfc_free_mem(vhost);
4885 spin_lock(&ibmvfc_driver_lock);
4886 list_del(&vhost->queue);
4887 spin_unlock(&ibmvfc_driver_lock);
4888 scsi_host_put(vhost->host);
4894 * ibmvfc_resume - Resume from suspend
4895 * @dev: device struct
4897 * We may have lost an interrupt across suspend/resume, so kick the
4901 static int ibmvfc_resume(struct device *dev)
4903 unsigned long flags;
4904 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
4905 struct vio_dev *vdev = to_vio_dev(dev);
4907 spin_lock_irqsave(vhost->host->host_lock, flags);
4908 vio_disable_interrupts(vdev);
4909 tasklet_schedule(&vhost->tasklet);
4910 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4915 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4916 * @vdev: vio device struct
4919 * Number of bytes the driver will need to DMA map at the same time in
4920 * order to perform well.
4922 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
4924 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
4925 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
4928 static const struct vio_device_id ibmvfc_device_table[] = {
4929 {"fcp", "IBM,vfc-client"},
4932 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
4934 static const struct dev_pm_ops ibmvfc_pm_ops = {
4935 .resume = ibmvfc_resume
4938 static struct vio_driver ibmvfc_driver = {
4939 .id_table = ibmvfc_device_table,
4940 .probe = ibmvfc_probe,
4941 .remove = ibmvfc_remove,
4942 .get_desired_dma = ibmvfc_get_desired_dma,
4943 .name = IBMVFC_NAME,
4944 .pm = &ibmvfc_pm_ops,
4947 static struct fc_function_template ibmvfc_transport_functions = {
4948 .show_host_fabric_name = 1,
4949 .show_host_node_name = 1,
4950 .show_host_port_name = 1,
4951 .show_host_supported_classes = 1,
4952 .show_host_port_type = 1,
4953 .show_host_port_id = 1,
4954 .show_host_maxframe_size = 1,
4956 .get_host_port_state = ibmvfc_get_host_port_state,
4957 .show_host_port_state = 1,
4959 .get_host_speed = ibmvfc_get_host_speed,
4960 .show_host_speed = 1,
4962 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4963 .terminate_rport_io = ibmvfc_terminate_rport_io,
4965 .show_rport_maxframe_size = 1,
4966 .show_rport_supported_classes = 1,
4968 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4969 .show_rport_dev_loss_tmo = 1,
4971 .get_starget_node_name = ibmvfc_get_starget_node_name,
4972 .show_starget_node_name = 1,
4974 .get_starget_port_name = ibmvfc_get_starget_port_name,
4975 .show_starget_port_name = 1,
4977 .get_starget_port_id = ibmvfc_get_starget_port_id,
4978 .show_starget_port_id = 1,
4980 .bsg_request = ibmvfc_bsg_request,
4981 .bsg_timeout = ibmvfc_bsg_timeout,
4985 * ibmvfc_module_init - Initialize the ibmvfc module
4988 * 0 on success / other on failure
4990 static int __init ibmvfc_module_init(void)
4994 if (!firmware_has_feature(FW_FEATURE_VIO))
4997 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4998 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
5000 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
5001 if (!ibmvfc_transport_template)
5004 rc = vio_register_driver(&ibmvfc_driver);
5006 fc_release_transport(ibmvfc_transport_template);
5011 * ibmvfc_module_exit - Teardown the ibmvfc module
5016 static void __exit ibmvfc_module_exit(void)
5018 vio_unregister_driver(&ibmvfc_driver);
5019 fc_release_transport(ibmvfc_transport_template);
5022 module_init(ibmvfc_module_init);
5023 module_exit(ibmvfc_module_exit);