Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1 | /* |
| 2 | * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter |
| 3 | * |
| 4 | * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation |
| 5 | * |
| 6 | * Copyright (C) IBM Corporation, 2008 |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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 |
| 21 | * |
| 22 | */ |
| 23 | |
| 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 32 | #include <linux/of.h> |
Brian King | b0f4d4c | 2010-02-21 10:37:58 -0600 | [diff] [blame] | 33 | #include <linux/pm.h> |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 34 | #include <linux/stringify.h> |
Johannes Thumshirn | 75cc8cf | 2016-11-17 10:31:19 +0100 | [diff] [blame] | 35 | #include <linux/bsg-lib.h> |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 36 | #include <asm/firmware.h> |
| 37 | #include <asm/irq.h> |
| 38 | #include <asm/vio.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> |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 45 | #include <scsi/scsi_bsg_fc.h> |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 46 | #include "ibmvfc.h" |
| 47 | |
| 48 | static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT; |
| 49 | static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT; |
Hannes Reinecke | 1abf635 | 2014-06-25 15:27:38 +0200 | [diff] [blame] | 50 | static u64 max_lun = IBMVFC_MAX_LUN; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 54 | static unsigned int ibmvfc_debug = IBMVFC_DEBUG; |
| 55 | static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL; |
Tyrel Datwyler | a6104b1 | 2016-08-03 16:36:53 -0500 | [diff] [blame] | 56 | static unsigned int cls3_error = IBMVFC_CLS3_ERROR; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 57 | static LIST_HEAD(ibmvfc_head); |
| 58 | static DEFINE_SPINLOCK(ibmvfc_driver_lock); |
| 59 | static struct scsi_transport_template *ibmvfc_transport_template; |
| 60 | |
| 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); |
| 65 | |
| 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) "]"); |
Hannes Reinecke | 1abf635 | 2014-06-25 15:27:38 +0200 | [diff] [blame] | 76 | module_param_named(max_lun, max_lun, ullong, S_IRUGO); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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) "]"); |
Brian King | 545ef9a | 2009-03-20 15:44:37 -0500 | [diff] [blame] | 82 | module_param_named(disc_threads, disc_threads, uint, S_IRUGO); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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) "]"); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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) "]"); |
Tyrel Datwyler | a6104b1 | 2016-08-03 16:36:53 -0500 | [diff] [blame] | 91 | module_param_named(cls3_error, cls3_error, uint, 0); |
Wei Yongjun | 6c46301 | 2016-09-08 15:04:41 +0000 | [diff] [blame] | 92 | MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. " |
Tyrel Datwyler | a6104b1 | 2016-08-03 16:36:53 -0500 | [diff] [blame] | 93 | "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]"); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 94 | |
| 95 | static const struct { |
| 96 | u16 status; |
| 97 | u16 error; |
| 98 | u8 result; |
| 99 | u8 retry; |
| 100 | int log; |
| 101 | char *name; |
| 102 | } cmd_status [] = { |
| 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" }, |
Brian King | 752b323 | 2008-12-15 17:09:05 -0600 | [diff] [blame] | 106 | { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" }, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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" }, |
Brian King | 497f9c5 | 2009-05-28 16:17:30 -0500 | [diff] [blame] | 115 | { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" }, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 116 | { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" }, |
| 117 | |
| 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" }, |
Brian King | 752b323 | 2008-12-15 17:09:05 -0600 | [diff] [blame] | 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" }, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 122 | { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" }, |
Brian King | 752b323 | 2008-12-15 17:09:05 -0600 | [diff] [blame] | 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" }, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 125 | { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" }, |
Brian King | 646d385 | 2008-11-14 13:33:53 -0600 | [diff] [blame] | 126 | { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" }, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 127 | { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" }, |
| 128 | |
| 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" }, |
| 140 | |
| 141 | { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" }, |
Tyrel Datwyler | 95237c2 | 2019-03-20 14:56:52 -0500 | [diff] [blame^] | 142 | { IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." }, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 143 | }; |
| 144 | |
| 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 *); |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 149 | static void ibmvfc_npiv_logout(struct ibmvfc_host *); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 150 | |
| 151 | static const char *unknown_error = "unknown error"; |
| 152 | |
| 153 | #ifdef CONFIG_SCSI_IBMVFC_TRACE |
| 154 | /** |
| 155 | * ibmvfc_trc_start - Log a start trace entry |
| 156 | * @evt: ibmvfc event struct |
| 157 | * |
| 158 | **/ |
| 159 | static void ibmvfc_trc_start(struct ibmvfc_event *evt) |
| 160 | { |
| 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; |
| 165 | |
| 166 | entry = &vhost->trace[vhost->trace_index++]; |
| 167 | entry->evt = evt; |
| 168 | entry->time = jiffies; |
| 169 | entry->fmt = evt->crq.format; |
| 170 | entry->type = IBMVFC_TRC_START; |
| 171 | |
| 172 | switch (entry->fmt) { |
| 173 | case IBMVFC_CMD_FORMAT: |
| 174 | entry->op_code = vfc_cmd->iu.cdb[0]; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 175 | entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 176 | entry->lun = scsilun_to_int(&vfc_cmd->iu.lun); |
| 177 | entry->tmf_flags = vfc_cmd->iu.tmf_flags; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 178 | entry->u.start.xfer_len = be32_to_cpu(vfc_cmd->iu.xfer_len); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 179 | break; |
| 180 | case IBMVFC_MAD_FORMAT: |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 181 | entry->op_code = be32_to_cpu(mad->opcode); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 182 | break; |
| 183 | default: |
| 184 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 185 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * ibmvfc_trc_end - Log an end trace entry |
| 190 | * @evt: ibmvfc event struct |
| 191 | * |
| 192 | **/ |
| 193 | static void ibmvfc_trc_end(struct ibmvfc_event *evt) |
| 194 | { |
| 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++]; |
| 199 | |
| 200 | entry->evt = evt; |
| 201 | entry->time = jiffies; |
| 202 | entry->fmt = evt->crq.format; |
| 203 | entry->type = IBMVFC_TRC_END; |
| 204 | |
| 205 | switch (entry->fmt) { |
| 206 | case IBMVFC_CMD_FORMAT: |
| 207 | entry->op_code = vfc_cmd->iu.cdb[0]; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 208 | entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 209 | entry->lun = scsilun_to_int(&vfc_cmd->iu.lun); |
| 210 | entry->tmf_flags = vfc_cmd->iu.tmf_flags; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 211 | entry->u.end.status = be16_to_cpu(vfc_cmd->status); |
| 212 | entry->u.end.error = be16_to_cpu(vfc_cmd->error); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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; |
| 216 | break; |
| 217 | case IBMVFC_MAD_FORMAT: |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 218 | entry->op_code = be32_to_cpu(mad->opcode); |
| 219 | entry->u.end.status = be16_to_cpu(mad->status); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 220 | break; |
| 221 | default: |
| 222 | break; |
| 223 | |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 224 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | #else |
| 228 | #define ibmvfc_trc_start(evt) do { } while (0) |
| 229 | #define ibmvfc_trc_end(evt) do { } while (0) |
| 230 | #endif |
| 231 | |
| 232 | /** |
| 233 | * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response |
| 234 | * @status: status / error class |
| 235 | * @error: error |
| 236 | * |
| 237 | * Return value: |
| 238 | * index into cmd_status / -EINVAL on failure |
| 239 | **/ |
| 240 | static int ibmvfc_get_err_index(u16 status, u16 error) |
| 241 | { |
| 242 | int i; |
| 243 | |
| 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) |
| 247 | return i; |
| 248 | |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * ibmvfc_get_cmd_error - Find the error description for the fcp response |
| 254 | * @status: status / error class |
| 255 | * @error: error |
| 256 | * |
| 257 | * Return value: |
| 258 | * error description string |
| 259 | **/ |
| 260 | static const char *ibmvfc_get_cmd_error(u16 status, u16 error) |
| 261 | { |
| 262 | int rc = ibmvfc_get_err_index(status, error); |
| 263 | if (rc >= 0) |
| 264 | return cmd_status[rc].name; |
| 265 | return unknown_error; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * ibmvfc_get_err_result - Find the scsi status to return for the fcp response |
| 270 | * @vfc_cmd: ibmvfc command struct |
| 271 | * |
| 272 | * Return value: |
| 273 | * SCSI result value to return for completed command |
| 274 | **/ |
| 275 | static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd) |
| 276 | { |
| 277 | int err; |
| 278 | struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 279 | int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 280 | |
| 281 | if ((rsp->flags & FCP_RSP_LEN_VALID) && |
Brian King | 4a2837d | 2009-05-28 16:17:22 -0500 | [diff] [blame] | 282 | ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) || |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 283 | rsp->data.info.rsp_code)) |
| 284 | return DID_ERROR << 16; |
| 285 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 286 | err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 287 | if (err >= 0) |
| 288 | return rsp->scsi_status | (cmd_status[err].result << 16); |
| 289 | return rsp->scsi_status | (DID_ERROR << 16); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * ibmvfc_retry_cmd - Determine if error status is retryable |
| 294 | * @status: status / error class |
| 295 | * @error: error |
| 296 | * |
| 297 | * Return value: |
| 298 | * 1 if error should be retried / 0 if it should not |
| 299 | **/ |
| 300 | static int ibmvfc_retry_cmd(u16 status, u16 error) |
| 301 | { |
| 302 | int rc = ibmvfc_get_err_index(status, error); |
| 303 | |
| 304 | if (rc >= 0) |
| 305 | return cmd_status[rc].retry; |
| 306 | return 1; |
| 307 | } |
| 308 | |
| 309 | static const char *unknown_fc_explain = "unknown fc explain"; |
| 310 | |
| 311 | static const struct { |
| 312 | u16 fc_explain; |
| 313 | char *name; |
| 314 | } ls_explain [] = { |
| 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" }, |
| 332 | }; |
| 333 | |
| 334 | static const struct { |
| 335 | u16 fc_explain; |
| 336 | char *name; |
| 337 | } gs_explain [] = { |
| 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" }, |
| 355 | }; |
| 356 | |
| 357 | /** |
| 358 | * ibmvfc_get_ls_explain - Return the FC Explain description text |
| 359 | * @status: FC Explain status |
| 360 | * |
| 361 | * Returns: |
| 362 | * error string |
| 363 | **/ |
| 364 | static const char *ibmvfc_get_ls_explain(u16 status) |
| 365 | { |
| 366 | int i; |
| 367 | |
| 368 | for (i = 0; i < ARRAY_SIZE(ls_explain); i++) |
| 369 | if (ls_explain[i].fc_explain == status) |
| 370 | return ls_explain[i].name; |
| 371 | |
| 372 | return unknown_fc_explain; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * ibmvfc_get_gs_explain - Return the FC Explain description text |
| 377 | * @status: FC Explain status |
| 378 | * |
| 379 | * Returns: |
| 380 | * error string |
| 381 | **/ |
| 382 | static const char *ibmvfc_get_gs_explain(u16 status) |
| 383 | { |
| 384 | int i; |
| 385 | |
| 386 | for (i = 0; i < ARRAY_SIZE(gs_explain); i++) |
| 387 | if (gs_explain[i].fc_explain == status) |
| 388 | return gs_explain[i].name; |
| 389 | |
| 390 | return unknown_fc_explain; |
| 391 | } |
| 392 | |
| 393 | static const struct { |
| 394 | enum ibmvfc_fc_type fc_type; |
| 395 | char *name; |
| 396 | } 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" }, |
| 403 | }; |
| 404 | |
| 405 | static const char *unknown_fc_type = "unknown fc type"; |
| 406 | |
| 407 | /** |
| 408 | * ibmvfc_get_fc_type - Return the FC Type description text |
| 409 | * @status: FC Type error status |
| 410 | * |
| 411 | * Returns: |
| 412 | * error string |
| 413 | **/ |
| 414 | static const char *ibmvfc_get_fc_type(u16 status) |
| 415 | { |
| 416 | int i; |
| 417 | |
| 418 | for (i = 0; i < ARRAY_SIZE(fc_type); i++) |
| 419 | if (fc_type[i].fc_type == status) |
| 420 | return fc_type[i].name; |
| 421 | |
| 422 | return unknown_fc_type; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * ibmvfc_set_tgt_action - Set the next init action for the target |
| 427 | * @tgt: ibmvfc target struct |
| 428 | * @action: action to perform |
| 429 | * |
| 430 | **/ |
| 431 | static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt, |
| 432 | enum ibmvfc_target_action action) |
| 433 | { |
| 434 | switch (tgt->action) { |
| 435 | case IBMVFC_TGT_ACTION_DEL_RPORT: |
Brian King | d5da304 | 2010-08-05 16:38:31 -0500 | [diff] [blame] | 436 | if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) |
| 437 | tgt->action = action; |
| 438 | case IBMVFC_TGT_ACTION_DELETED_RPORT: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 439 | break; |
| 440 | default: |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 441 | if (action == IBMVFC_TGT_ACTION_DEL_RPORT) |
| 442 | tgt->add_rport = 0; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 443 | tgt->action = action; |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * ibmvfc_set_host_state - Set the state for the host |
| 450 | * @vhost: ibmvfc host struct |
| 451 | * @state: state to set host to |
| 452 | * |
| 453 | * Returns: |
| 454 | * 0 if state changed / non-zero if not changed |
| 455 | **/ |
| 456 | static int ibmvfc_set_host_state(struct ibmvfc_host *vhost, |
| 457 | enum ibmvfc_host_state state) |
| 458 | { |
| 459 | int rc = 0; |
| 460 | |
| 461 | switch (vhost->state) { |
| 462 | case IBMVFC_HOST_OFFLINE: |
| 463 | rc = -EINVAL; |
| 464 | break; |
| 465 | default: |
| 466 | vhost->state = state; |
| 467 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 468 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 469 | |
| 470 | return rc; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * ibmvfc_set_host_action - Set the next init action for the host |
| 475 | * @vhost: ibmvfc host struct |
| 476 | * @action: action to perform |
| 477 | * |
| 478 | **/ |
| 479 | static void ibmvfc_set_host_action(struct ibmvfc_host *vhost, |
| 480 | enum ibmvfc_host_action action) |
| 481 | { |
| 482 | switch (action) { |
| 483 | case IBMVFC_HOST_ACTION_ALLOC_TGTS: |
| 484 | if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) |
| 485 | vhost->action = action; |
| 486 | break; |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 487 | case IBMVFC_HOST_ACTION_LOGO_WAIT: |
| 488 | if (vhost->action == IBMVFC_HOST_ACTION_LOGO) |
| 489 | vhost->action = action; |
| 490 | break; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 491 | case IBMVFC_HOST_ACTION_INIT_WAIT: |
| 492 | if (vhost->action == IBMVFC_HOST_ACTION_INIT) |
| 493 | vhost->action = action; |
| 494 | break; |
| 495 | case IBMVFC_HOST_ACTION_QUERY: |
| 496 | switch (vhost->action) { |
| 497 | case IBMVFC_HOST_ACTION_INIT_WAIT: |
| 498 | case IBMVFC_HOST_ACTION_NONE: |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 499 | case IBMVFC_HOST_ACTION_TGT_DEL_FAILED: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 500 | vhost->action = action; |
| 501 | break; |
| 502 | default: |
| 503 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 504 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 505 | break; |
| 506 | case IBMVFC_HOST_ACTION_TGT_INIT: |
| 507 | if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS) |
| 508 | vhost->action = action; |
| 509 | break; |
| 510 | case IBMVFC_HOST_ACTION_INIT: |
| 511 | case IBMVFC_HOST_ACTION_TGT_DEL: |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 512 | switch (vhost->action) { |
| 513 | case IBMVFC_HOST_ACTION_RESET: |
| 514 | case IBMVFC_HOST_ACTION_REENABLE: |
| 515 | break; |
| 516 | default: |
| 517 | vhost->action = action; |
| 518 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 519 | } |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 520 | break; |
| 521 | case IBMVFC_HOST_ACTION_LOGO: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 522 | case IBMVFC_HOST_ACTION_QUERY_TGTS: |
Brian King | 10e7949 | 2008-10-29 08:46:45 -0500 | [diff] [blame] | 523 | case IBMVFC_HOST_ACTION_TGT_DEL_FAILED: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 524 | case IBMVFC_HOST_ACTION_NONE: |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 525 | case IBMVFC_HOST_ACTION_RESET: |
| 526 | case IBMVFC_HOST_ACTION_REENABLE: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 527 | default: |
| 528 | vhost->action = action; |
| 529 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 530 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /** |
| 534 | * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login) |
| 535 | * @vhost: ibmvfc host struct |
| 536 | * |
| 537 | * Return value: |
| 538 | * nothing |
| 539 | **/ |
| 540 | static void ibmvfc_reinit_host(struct ibmvfc_host *vhost) |
| 541 | { |
| 542 | if (vhost->action == IBMVFC_HOST_ACTION_NONE) { |
Brian King | 2d0da2a | 2008-07-22 08:31:42 -0500 | [diff] [blame] | 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); |
| 546 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 547 | } else |
| 548 | vhost->reinit = 1; |
| 549 | |
| 550 | wake_up(&vhost->work_wait_q); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * ibmvfc_link_down - Handle a link down event from the adapter |
| 555 | * @vhost: ibmvfc host struct |
| 556 | * @state: ibmvfc host state to enter |
| 557 | * |
| 558 | **/ |
| 559 | static void ibmvfc_link_down(struct ibmvfc_host *vhost, |
| 560 | enum ibmvfc_host_state state) |
| 561 | { |
| 562 | struct ibmvfc_target *tgt; |
| 563 | |
| 564 | ENTER; |
| 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); |
| 572 | LEAVE; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * ibmvfc_init_host - Start host initialization |
| 577 | * @vhost: ibmvfc host struct |
| 578 | * |
| 579 | * Return value: |
| 580 | * nothing |
| 581 | **/ |
Brian King | 861890c | 2009-10-19 15:07:49 -0500 | [diff] [blame] | 582 | static void ibmvfc_init_host(struct ibmvfc_host *vhost) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 583 | { |
| 584 | struct ibmvfc_target *tgt; |
| 585 | |
| 586 | if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) { |
Brian King | 1c41fa82 | 2008-12-03 11:02:54 -0600 | [diff] [blame] | 587 | if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 588 | dev_err(vhost->dev, |
| 589 | "Host initialization retries exceeded. Taking adapter offline\n"); |
| 590 | ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE); |
| 591 | return; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) { |
Brian King | 861890c | 2009-10-19 15:07:49 -0500 | [diff] [blame] | 596 | memset(vhost->async_crq.msgs, 0, PAGE_SIZE); |
| 597 | vhost->async_crq.cur = 0; |
Brian King | cf6f10d | 2008-08-15 10:59:23 -0500 | [diff] [blame] | 598 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 599 | list_for_each_entry(tgt, &vhost->targets, queue) |
Brian King | 5e47167 | 2009-05-28 16:17:32 -0500 | [diff] [blame] | 600 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /** |
| 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 |
| 613 | * |
| 614 | * Return value: |
| 615 | * 0 on success / other on failure |
| 616 | **/ |
| 617 | static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2) |
| 618 | { |
| 619 | struct vio_dev *vdev = to_vio_dev(vhost->dev); |
| 620 | return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * ibmvfc_send_crq_init - Send a CRQ init message |
| 625 | * @vhost: ibmvfc host struct |
| 626 | * |
| 627 | * Return value: |
| 628 | * 0 on success / other on failure |
| 629 | **/ |
| 630 | static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost) |
| 631 | { |
| 632 | ibmvfc_dbg(vhost, "Sending CRQ init\n"); |
| 633 | return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * ibmvfc_send_crq_init_complete - Send a CRQ init complete message |
| 638 | * @vhost: ibmvfc host struct |
| 639 | * |
| 640 | * Return value: |
| 641 | * 0 on success / other on failure |
| 642 | **/ |
| 643 | static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost) |
| 644 | { |
| 645 | ibmvfc_dbg(vhost, "Sending CRQ init complete\n"); |
| 646 | return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0); |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ |
| 651 | * @vhost: ibmvfc host struct |
| 652 | * |
| 653 | * Frees irq, deallocates a page for messages, unmaps dma, and unregisters |
| 654 | * the crq with the hypervisor. |
| 655 | **/ |
| 656 | static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost) |
| 657 | { |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 658 | long rc = 0; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 659 | struct vio_dev *vdev = to_vio_dev(vhost->dev); |
| 660 | struct ibmvfc_crq_queue *crq = &vhost->crq; |
| 661 | |
| 662 | ibmvfc_dbg(vhost, "Releasing CRQ\n"); |
| 663 | free_irq(vdev->irq, vhost); |
Brian King | 039a089 | 2009-03-20 15:44:35 -0500 | [diff] [blame] | 664 | tasklet_kill(&vhost->tasklet); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 665 | do { |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 666 | if (rc) |
| 667 | msleep(100); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 668 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); |
| 669 | } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); |
| 670 | |
| 671 | vhost->state = IBMVFC_NO_CRQ; |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 672 | vhost->logged_in = 0; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 673 | dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL); |
| 674 | free_page((unsigned long)crq->msgs); |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * ibmvfc_reenable_crq_queue - reenables the CRQ |
| 679 | * @vhost: ibmvfc host struct |
| 680 | * |
| 681 | * Return value: |
| 682 | * 0 on success / other on failure |
| 683 | **/ |
| 684 | static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost) |
| 685 | { |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 686 | int rc = 0; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 687 | struct vio_dev *vdev = to_vio_dev(vhost->dev); |
| 688 | |
| 689 | /* Re-enable the CRQ */ |
| 690 | do { |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 691 | if (rc) |
| 692 | msleep(100); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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)); |
| 695 | |
| 696 | if (rc) |
| 697 | dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc); |
| 698 | |
| 699 | return rc; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * ibmvfc_reset_crq - resets a crq after a failure |
| 704 | * @vhost: ibmvfc host struct |
| 705 | * |
| 706 | * Return value: |
| 707 | * 0 on success / other on failure |
| 708 | **/ |
| 709 | static int ibmvfc_reset_crq(struct ibmvfc_host *vhost) |
| 710 | { |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 711 | int rc = 0; |
| 712 | unsigned long flags; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 713 | struct vio_dev *vdev = to_vio_dev(vhost->dev); |
| 714 | struct ibmvfc_crq_queue *crq = &vhost->crq; |
| 715 | |
| 716 | /* Close the CRQ */ |
| 717 | do { |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 718 | if (rc) |
| 719 | msleep(100); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 720 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); |
| 721 | } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); |
| 722 | |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 723 | spin_lock_irqsave(vhost->host->host_lock, flags); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 724 | vhost->state = IBMVFC_NO_CRQ; |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 725 | vhost->logged_in = 0; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 726 | |
| 727 | /* Clean out the queue */ |
| 728 | memset(crq->msgs, 0, PAGE_SIZE); |
| 729 | crq->cur = 0; |
| 730 | |
| 731 | /* And re-open it again */ |
| 732 | rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address, |
| 733 | crq->msg_token, PAGE_SIZE); |
| 734 | |
| 735 | if (rc == H_CLOSED) |
| 736 | /* Adapter is good, but other end is not ready */ |
| 737 | dev_warn(vhost->dev, "Partner adapter not ready\n"); |
| 738 | else if (rc != 0) |
| 739 | dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc); |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 740 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 741 | |
| 742 | return rc; |
| 743 | } |
| 744 | |
| 745 | /** |
| 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 |
| 749 | * |
| 750 | * Return value: |
| 751 | * 1 if event is valid / 0 if event is not valid |
| 752 | **/ |
| 753 | static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool, |
| 754 | struct ibmvfc_event *evt) |
| 755 | { |
| 756 | int index = evt - pool->events; |
| 757 | if (index < 0 || index >= pool->size) /* outside of bounds */ |
| 758 | return 0; |
| 759 | if (evt != pool->events + index) /* unaligned */ |
| 760 | return 0; |
| 761 | return 1; |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * ibmvfc_free_event - Free the specified event |
| 766 | * @evt: ibmvfc_event to be freed |
| 767 | * |
| 768 | **/ |
| 769 | static void ibmvfc_free_event(struct ibmvfc_event *evt) |
| 770 | { |
| 771 | struct ibmvfc_host *vhost = evt->vhost; |
| 772 | struct ibmvfc_event_pool *pool = &vhost->pool; |
| 773 | |
| 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); |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * ibmvfc_scsi_eh_done - EH done function for queuecommand commands |
| 781 | * @evt: ibmvfc event struct |
| 782 | * |
| 783 | * This function does not setup any error status, that must be done |
| 784 | * before this function gets called. |
| 785 | **/ |
| 786 | static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt) |
| 787 | { |
| 788 | struct scsi_cmnd *cmnd = evt->cmnd; |
| 789 | |
| 790 | if (cmnd) { |
| 791 | scsi_dma_unmap(cmnd); |
| 792 | cmnd->scsi_done(cmnd); |
| 793 | } |
| 794 | |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 795 | if (evt->eh_comp) |
| 796 | complete(evt->eh_comp); |
| 797 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 798 | ibmvfc_free_event(evt); |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * ibmvfc_fail_request - Fail request with specified error code |
| 803 | * @evt: ibmvfc event struct |
| 804 | * @error_code: error code to fail request with |
| 805 | * |
| 806 | * Return value: |
| 807 | * none |
| 808 | **/ |
| 809 | static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code) |
| 810 | { |
| 811 | if (evt->cmnd) { |
| 812 | evt->cmnd->result = (error_code << 16); |
| 813 | evt->done = ibmvfc_scsi_eh_done; |
| 814 | } else |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 815 | evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 816 | |
| 817 | list_del(&evt->queue); |
| 818 | del_timer(&evt->timer); |
| 819 | ibmvfc_trc_end(evt); |
| 820 | evt->done(evt); |
| 821 | } |
| 822 | |
| 823 | /** |
| 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 |
| 827 | * |
| 828 | * Return value: |
| 829 | * none |
| 830 | **/ |
| 831 | static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code) |
| 832 | { |
| 833 | struct ibmvfc_event *evt, *pos; |
| 834 | |
| 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); |
| 838 | } |
| 839 | |
| 840 | /** |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 841 | * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 842 | * @vhost: struct ibmvfc host to reset |
| 843 | **/ |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 844 | static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 845 | { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 846 | ibmvfc_purge_requests(vhost, DID_ERROR); |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 847 | ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN); |
| 848 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /** |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 852 | * __ibmvfc_reset_host - Reset the connection to the server (no locking) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 853 | * @vhost: struct ibmvfc host to reset |
| 854 | **/ |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 855 | static void __ibmvfc_reset_host(struct ibmvfc_host *vhost) |
| 856 | { |
| 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); |
| 863 | } else |
| 864 | ibmvfc_hard_reset_host(vhost); |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * ibmvfc_reset_host - Reset the connection to the server |
| 869 | * @vhost: ibmvfc host struct |
| 870 | **/ |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 871 | static void ibmvfc_reset_host(struct ibmvfc_host *vhost) |
| 872 | { |
| 873 | unsigned long flags; |
| 874 | |
| 875 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 876 | __ibmvfc_reset_host(vhost); |
| 877 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * ibmvfc_retry_host_init - Retry host initialization if allowed |
| 882 | * @vhost: ibmvfc host struct |
| 883 | * |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 884 | * Returns: 1 if init will be retried / 0 if not |
| 885 | * |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 886 | **/ |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 887 | static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 888 | { |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 889 | int retry = 0; |
| 890 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 891 | if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) { |
Brian King | 1c41fa82 | 2008-12-03 11:02:54 -0600 | [diff] [blame] | 892 | vhost->delay_init = 1; |
| 893 | if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 894 | dev_err(vhost->dev, |
| 895 | "Host initialization retries exceeded. Taking adapter offline\n"); |
| 896 | ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE); |
Brian King | 1c41fa82 | 2008-12-03 11:02:54 -0600 | [diff] [blame] | 897 | } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 898 | __ibmvfc_reset_host(vhost); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 899 | else { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 900 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 901 | retry = 1; |
| 902 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | wake_up(&vhost->work_wait_q); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 906 | return retry; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /** |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 910 | * __ibmvfc_get_target - Find the specified scsi_target (no locking) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 911 | * @starget: scsi target struct |
| 912 | * |
| 913 | * Return value: |
| 914 | * ibmvfc_target struct / NULL if not found |
| 915 | **/ |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 916 | static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 917 | { |
| 918 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 919 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 920 | struct ibmvfc_target *tgt; |
| 921 | |
| 922 | list_for_each_entry(tgt, &vhost->targets, queue) |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 923 | if (tgt->target_id == starget->id) { |
| 924 | kref_get(&tgt->kref); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 925 | return tgt; |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 926 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 927 | return NULL; |
| 928 | } |
| 929 | |
| 930 | /** |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 931 | * ibmvfc_get_target - Find the specified scsi_target |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 932 | * @starget: scsi target struct |
| 933 | * |
| 934 | * Return value: |
| 935 | * ibmvfc_target struct / NULL if not found |
| 936 | **/ |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 937 | static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 938 | { |
| 939 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 940 | struct ibmvfc_target *tgt; |
| 941 | unsigned long flags; |
| 942 | |
| 943 | spin_lock_irqsave(shost->host_lock, flags); |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 944 | tgt = __ibmvfc_get_target(starget); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 945 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 946 | return tgt; |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * ibmvfc_get_host_speed - Get host port speed |
| 951 | * @shost: scsi host struct |
| 952 | * |
| 953 | * Return value: |
| 954 | * none |
| 955 | **/ |
| 956 | static void ibmvfc_get_host_speed(struct Scsi_Host *shost) |
| 957 | { |
| 958 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 959 | unsigned long flags; |
| 960 | |
| 961 | spin_lock_irqsave(shost->host_lock, flags); |
| 962 | if (vhost->state == IBMVFC_ACTIVE) { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 963 | switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 964 | case 1: |
| 965 | fc_host_speed(shost) = FC_PORTSPEED_1GBIT; |
| 966 | break; |
| 967 | case 2: |
| 968 | fc_host_speed(shost) = FC_PORTSPEED_2GBIT; |
| 969 | break; |
| 970 | case 4: |
| 971 | fc_host_speed(shost) = FC_PORTSPEED_4GBIT; |
| 972 | break; |
| 973 | case 8: |
| 974 | fc_host_speed(shost) = FC_PORTSPEED_8GBIT; |
| 975 | break; |
| 976 | case 10: |
| 977 | fc_host_speed(shost) = FC_PORTSPEED_10GBIT; |
| 978 | break; |
| 979 | case 16: |
| 980 | fc_host_speed(shost) = FC_PORTSPEED_16GBIT; |
| 981 | break; |
| 982 | default: |
Stephen Rothwell | 775a42e | 2009-01-06 14:59:00 +0000 | [diff] [blame] | 983 | ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n", |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 984 | be64_to_cpu(vhost->login_buf->resp.link_speed) / 100); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 985 | fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN; |
| 986 | break; |
| 987 | } |
| 988 | } else |
| 989 | fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN; |
| 990 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 991 | } |
| 992 | |
| 993 | /** |
| 994 | * ibmvfc_get_host_port_state - Get host port state |
| 995 | * @shost: scsi host struct |
| 996 | * |
| 997 | * Return value: |
| 998 | * none |
| 999 | **/ |
| 1000 | static void ibmvfc_get_host_port_state(struct Scsi_Host *shost) |
| 1001 | { |
| 1002 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 1003 | unsigned long flags; |
| 1004 | |
| 1005 | spin_lock_irqsave(shost->host_lock, flags); |
| 1006 | switch (vhost->state) { |
| 1007 | case IBMVFC_INITIALIZING: |
| 1008 | case IBMVFC_ACTIVE: |
| 1009 | fc_host_port_state(shost) = FC_PORTSTATE_ONLINE; |
| 1010 | break; |
| 1011 | case IBMVFC_LINK_DOWN: |
| 1012 | fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN; |
| 1013 | break; |
| 1014 | case IBMVFC_LINK_DEAD: |
| 1015 | case IBMVFC_HOST_OFFLINE: |
| 1016 | fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE; |
| 1017 | break; |
| 1018 | case IBMVFC_HALTED: |
| 1019 | fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED; |
| 1020 | break; |
Brian King | 0ae808e | 2008-07-22 08:31:39 -0500 | [diff] [blame] | 1021 | case IBMVFC_NO_CRQ: |
| 1022 | fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN; |
| 1023 | break; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1024 | default: |
| 1025 | ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state); |
| 1026 | fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN; |
| 1027 | break; |
| 1028 | } |
| 1029 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout |
| 1034 | * @rport: rport struct |
| 1035 | * @timeout: timeout value |
| 1036 | * |
| 1037 | * Return value: |
| 1038 | * none |
| 1039 | **/ |
| 1040 | static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout) |
| 1041 | { |
| 1042 | if (timeout) |
| 1043 | rport->dev_loss_tmo = timeout; |
| 1044 | else |
| 1045 | rport->dev_loss_tmo = 1; |
| 1046 | } |
| 1047 | |
| 1048 | /** |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 1049 | * ibmvfc_release_tgt - Free memory allocated for a target |
| 1050 | * @kref: kref struct |
| 1051 | * |
| 1052 | **/ |
| 1053 | static void ibmvfc_release_tgt(struct kref *kref) |
| 1054 | { |
| 1055 | struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref); |
| 1056 | kfree(tgt); |
| 1057 | } |
| 1058 | |
| 1059 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1060 | * ibmvfc_get_starget_node_name - Get SCSI target's node name |
| 1061 | * @starget: scsi target struct |
| 1062 | * |
| 1063 | * Return value: |
| 1064 | * none |
| 1065 | **/ |
| 1066 | static void ibmvfc_get_starget_node_name(struct scsi_target *starget) |
| 1067 | { |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 1068 | struct ibmvfc_target *tgt = ibmvfc_get_target(starget); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1069 | fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0; |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 1070 | if (tgt) |
| 1071 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /** |
| 1075 | * ibmvfc_get_starget_port_name - Get SCSI target's port name |
| 1076 | * @starget: scsi target struct |
| 1077 | * |
| 1078 | * Return value: |
| 1079 | * none |
| 1080 | **/ |
| 1081 | static void ibmvfc_get_starget_port_name(struct scsi_target *starget) |
| 1082 | { |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 1083 | struct ibmvfc_target *tgt = ibmvfc_get_target(starget); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1084 | fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0; |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 1085 | if (tgt) |
| 1086 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /** |
| 1090 | * ibmvfc_get_starget_port_id - Get SCSI target's port ID |
| 1091 | * @starget: scsi target struct |
| 1092 | * |
| 1093 | * Return value: |
| 1094 | * none |
| 1095 | **/ |
| 1096 | static void ibmvfc_get_starget_port_id(struct scsi_target *starget) |
| 1097 | { |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 1098 | struct ibmvfc_target *tgt = ibmvfc_get_target(starget); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1099 | fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1; |
Brian King | b3c10489c | 2008-07-22 08:31:41 -0500 | [diff] [blame] | 1100 | if (tgt) |
| 1101 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * ibmvfc_wait_while_resetting - Wait while the host resets |
| 1106 | * @vhost: ibmvfc host struct |
| 1107 | * |
| 1108 | * Return value: |
| 1109 | * 0 on success / other on failure |
| 1110 | **/ |
| 1111 | static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost) |
| 1112 | { |
| 1113 | long timeout = wait_event_timeout(vhost->init_wait_q, |
Brian King | 3eddc56 | 2008-08-15 10:59:21 -0500 | [diff] [blame] | 1114 | ((vhost->state == IBMVFC_ACTIVE || |
| 1115 | vhost->state == IBMVFC_HOST_OFFLINE || |
| 1116 | vhost->state == IBMVFC_LINK_DEAD) && |
| 1117 | vhost->action == IBMVFC_HOST_ACTION_NONE), |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1118 | (init_timeout * HZ)); |
| 1119 | |
| 1120 | return timeout ? 0 : -EIO; |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * ibmvfc_issue_fc_host_lip - Re-initiate link initialization |
| 1125 | * @shost: scsi host struct |
| 1126 | * |
| 1127 | * Return value: |
| 1128 | * 0 on success / other on failure |
| 1129 | **/ |
| 1130 | static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost) |
| 1131 | { |
| 1132 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 1133 | |
| 1134 | dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n"); |
| 1135 | ibmvfc_reset_host(vhost); |
| 1136 | return ibmvfc_wait_while_resetting(vhost); |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * ibmvfc_gather_partition_info - Gather info about the LPAR |
| 1141 | * |
| 1142 | * Return value: |
| 1143 | * none |
| 1144 | **/ |
| 1145 | static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost) |
| 1146 | { |
| 1147 | struct device_node *rootdn; |
| 1148 | const char *name; |
| 1149 | const unsigned int *num; |
| 1150 | |
| 1151 | rootdn = of_find_node_by_path("/"); |
| 1152 | if (!rootdn) |
| 1153 | return; |
| 1154 | |
| 1155 | name = of_get_property(rootdn, "ibm,partition-name", NULL); |
| 1156 | if (name) |
| 1157 | strncpy(vhost->partition_name, name, sizeof(vhost->partition_name)); |
| 1158 | num = of_get_property(rootdn, "ibm,partition-no", NULL); |
| 1159 | if (num) |
| 1160 | vhost->partition_number = *num; |
| 1161 | of_node_put(rootdn); |
| 1162 | } |
| 1163 | |
| 1164 | /** |
| 1165 | * ibmvfc_set_login_info - Setup info for NPIV login |
| 1166 | * @vhost: ibmvfc host struct |
| 1167 | * |
| 1168 | * Return value: |
| 1169 | * none |
| 1170 | **/ |
| 1171 | static void ibmvfc_set_login_info(struct ibmvfc_host *vhost) |
| 1172 | { |
| 1173 | struct ibmvfc_npiv_login *login_info = &vhost->login_info; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 1174 | struct device_node *of_node = vhost->dev->of_node; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1175 | const char *location; |
| 1176 | |
| 1177 | memset(login_info, 0, sizeof(*login_info)); |
| 1178 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1187 | if (vhost->client_migrated) |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1188 | login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1189 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1194 | strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME); |
| 1195 | strncpy(login_info->device_name, |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 1196 | dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1197 | |
| 1198 | location = of_get_property(of_node, "ibm,loc-code", NULL); |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 1199 | location = location ? location : dev_name(vhost->dev); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1200 | strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME); |
| 1201 | } |
| 1202 | |
| 1203 | /** |
| 1204 | * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host |
| 1205 | * @vhost: ibmvfc host who owns the event pool |
| 1206 | * |
| 1207 | * Returns zero on success. |
| 1208 | **/ |
| 1209 | static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost) |
| 1210 | { |
| 1211 | int i; |
| 1212 | struct ibmvfc_event_pool *pool = &vhost->pool; |
| 1213 | |
| 1214 | ENTER; |
| 1215 | pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ; |
| 1216 | pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL); |
| 1217 | if (!pool->events) |
| 1218 | return -ENOMEM; |
| 1219 | |
| 1220 | pool->iu_storage = dma_alloc_coherent(vhost->dev, |
| 1221 | pool->size * sizeof(*pool->iu_storage), |
| 1222 | &pool->iu_token, 0); |
| 1223 | |
| 1224 | if (!pool->iu_storage) { |
| 1225 | kfree(pool->events); |
| 1226 | return -ENOMEM; |
| 1227 | } |
| 1228 | |
| 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; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1233 | evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1234 | evt->xfer_iu = pool->iu_storage + i; |
| 1235 | evt->vhost = vhost; |
| 1236 | evt->ext_list = NULL; |
| 1237 | list_add_tail(&evt->queue, &vhost->free); |
| 1238 | } |
| 1239 | |
| 1240 | LEAVE; |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | /** |
| 1245 | * ibmvfc_free_event_pool - Frees memory of the event pool of a host |
| 1246 | * @vhost: ibmvfc host who owns the event pool |
| 1247 | * |
| 1248 | **/ |
| 1249 | static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost) |
| 1250 | { |
| 1251 | int i; |
| 1252 | struct ibmvfc_event_pool *pool = &vhost->pool; |
| 1253 | |
| 1254 | ENTER; |
| 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); |
| 1262 | } |
| 1263 | |
| 1264 | kfree(pool->events); |
| 1265 | dma_free_coherent(vhost->dev, |
| 1266 | pool->size * sizeof(*pool->iu_storage), |
| 1267 | pool->iu_storage, pool->iu_token); |
| 1268 | LEAVE; |
| 1269 | } |
| 1270 | |
| 1271 | /** |
| 1272 | * ibmvfc_get_event - Gets the next free event in pool |
| 1273 | * @vhost: ibmvfc host struct |
| 1274 | * |
| 1275 | * Returns a free event from the pool. |
| 1276 | **/ |
| 1277 | static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost) |
| 1278 | { |
| 1279 | struct ibmvfc_event *evt; |
| 1280 | |
| 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); |
| 1285 | return evt; |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * ibmvfc_init_event - Initialize fields in an event struct that are always |
| 1290 | * required. |
| 1291 | * @evt: The event |
| 1292 | * @done: Routine to call when the event is responded to |
| 1293 | * @format: SRP or MAD format |
| 1294 | **/ |
| 1295 | static void ibmvfc_init_event(struct ibmvfc_event *evt, |
| 1296 | void (*done) (struct ibmvfc_event *), u8 format) |
| 1297 | { |
| 1298 | evt->cmnd = NULL; |
| 1299 | evt->sync_iu = NULL; |
| 1300 | evt->crq.format = format; |
| 1301 | evt->done = done; |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 1302 | evt->eh_comp = NULL; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | /** |
| 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 |
| 1310 | **/ |
| 1311 | static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg, |
| 1312 | struct srp_direct_buf *md) |
| 1313 | { |
| 1314 | int i; |
| 1315 | struct scatterlist *sg; |
| 1316 | |
| 1317 | scsi_for_each_sg(scmd, sg, nseg, i) { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1318 | md[i].va = cpu_to_be64(sg_dma_address(sg)); |
| 1319 | md[i].len = cpu_to_be32(sg_dma_len(sg)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1320 | md[i].key = 0; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | /** |
| 1325 | * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields |
Johannes Thumshirn | 91ebc1f | 2018-06-13 09:53:47 +0200 | [diff] [blame] | 1326 | * @scmd: struct scsi_cmnd with the scatterlist |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1327 | * @evt: ibmvfc event struct |
| 1328 | * @vfc_cmd: vfc_cmd that contains the memory descriptor |
| 1329 | * @dev: device for which to map dma memory |
| 1330 | * |
| 1331 | * Returns: |
| 1332 | * 0 on success / non-zero on failure |
| 1333 | **/ |
| 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) |
| 1337 | { |
| 1338 | |
| 1339 | int sg_mapped; |
| 1340 | struct srp_direct_buf *data = &vfc_cmd->ioba; |
| 1341 | struct ibmvfc_host *vhost = dev_get_drvdata(dev); |
| 1342 | |
Tyrel Datwyler | a6104b1 | 2016-08-03 16:36:53 -0500 | [diff] [blame] | 1343 | if (cls3_error) |
| 1344 | vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR); |
| 1345 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1346 | sg_mapped = scsi_dma_map(scmd); |
| 1347 | if (!sg_mapped) { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1348 | vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1349 | return 0; |
| 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"); |
| 1353 | return sg_mapped; |
| 1354 | } |
| 1355 | |
| 1356 | if (scmd->sc_data_direction == DMA_TO_DEVICE) { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1357 | vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1358 | vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA; |
| 1359 | } else { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1360 | vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1361 | vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA; |
| 1362 | } |
| 1363 | |
| 1364 | if (sg_mapped == 1) { |
| 1365 | ibmvfc_map_sg_list(scmd, sg_mapped, data); |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1369 | vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1370 | |
| 1371 | if (!evt->ext_list) { |
| 1372 | evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC, |
| 1373 | &evt->ext_list_token); |
| 1374 | |
| 1375 | if (!evt->ext_list) { |
Brian King | 64b840d | 2009-01-22 15:45:38 -0600 | [diff] [blame] | 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"); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1379 | return -ENOMEM; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list); |
| 1384 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1385 | data->va = cpu_to_be64(evt->ext_list_token); |
| 1386 | data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1387 | data->key = 0; |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
| 1392 | * ibmvfc_timeout - Internal command timeout handler |
| 1393 | * @evt: struct ibmvfc_event that timed out |
| 1394 | * |
| 1395 | * Called when an internally generated command times out |
| 1396 | **/ |
Kees Cook | 9a5d04f | 2017-10-09 20:54:48 -0700 | [diff] [blame] | 1397 | static void ibmvfc_timeout(struct timer_list *t) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1398 | { |
Kees Cook | 9a5d04f | 2017-10-09 20:54:48 -0700 | [diff] [blame] | 1399 | struct ibmvfc_event *evt = from_timer(evt, t, timer); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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); |
| 1403 | } |
| 1404 | |
| 1405 | /** |
| 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 |
| 1410 | * |
| 1411 | * Returns the value returned from ibmvfc_send_crq(). (Zero for success) |
| 1412 | **/ |
| 1413 | static int ibmvfc_send_event(struct ibmvfc_event *evt, |
| 1414 | struct ibmvfc_host *vhost, unsigned long timeout) |
| 1415 | { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1416 | __be64 *crq_as_u64 = (__be64 *) &evt->crq; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1417 | int rc; |
| 1418 | |
| 1419 | /* Copy the IU into the transfer area */ |
| 1420 | *evt->xfer_iu = evt->iu; |
| 1421 | if (evt->crq.format == IBMVFC_CMD_FORMAT) |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1422 | evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1423 | else if (evt->crq.format == IBMVFC_MAD_FORMAT) |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1424 | evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1425 | else |
| 1426 | BUG(); |
| 1427 | |
| 1428 | list_add_tail(&evt->queue, &vhost->sent); |
Kees Cook | 9a5d04f | 2017-10-09 20:54:48 -0700 | [diff] [blame] | 1429 | timer_setup(&evt->timer, ibmvfc_timeout, 0); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1430 | |
| 1431 | if (timeout) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1432 | evt->timer.expires = jiffies + (timeout * HZ); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1433 | add_timer(&evt->timer); |
| 1434 | } |
| 1435 | |
Brian King | a528ab7 | 2008-12-03 11:02:56 -0600 | [diff] [blame] | 1436 | mb(); |
| 1437 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1438 | if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]), |
| 1439 | be64_to_cpu(crq_as_u64[1])))) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1440 | list_del(&evt->queue); |
| 1441 | del_timer(&evt->timer); |
| 1442 | |
| 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() |
| 1447 | */ |
| 1448 | if (rc == H_CLOSED) { |
| 1449 | if (printk_ratelimit()) |
| 1450 | dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n"); |
| 1451 | if (evt->cmnd) |
| 1452 | scsi_dma_unmap(evt->cmnd); |
| 1453 | ibmvfc_free_event(evt); |
| 1454 | return SCSI_MLQUEUE_HOST_BUSY; |
| 1455 | } |
| 1456 | |
| 1457 | dev_err(vhost->dev, "Send error (rc=%d)\n", rc); |
| 1458 | if (evt->cmnd) { |
| 1459 | evt->cmnd->result = DID_ERROR << 16; |
| 1460 | evt->done = ibmvfc_scsi_eh_done; |
| 1461 | } else |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1462 | evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1463 | |
| 1464 | evt->done(evt); |
| 1465 | } else |
| 1466 | ibmvfc_trc_start(evt); |
| 1467 | |
| 1468 | return 0; |
| 1469 | } |
| 1470 | |
| 1471 | /** |
| 1472 | * ibmvfc_log_error - Log an error for the failed command if appropriate |
| 1473 | * @evt: ibmvfc event to log |
| 1474 | * |
| 1475 | **/ |
| 1476 | static void ibmvfc_log_error(struct ibmvfc_event *evt) |
| 1477 | { |
| 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; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1483 | int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1484 | int logerr = 0; |
| 1485 | int rsp_code = 0; |
| 1486 | |
| 1487 | if (index >= 0) { |
| 1488 | logerr = cmd_status[index].log; |
| 1489 | err = cmd_status[index].name; |
| 1490 | } |
| 1491 | |
Brian King | 0ae808e | 2008-07-22 08:31:39 -0500 | [diff] [blame] | 1492 | if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1))) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1493 | return; |
| 1494 | |
| 1495 | if (rsp->flags & FCP_RSP_LEN_VALID) |
| 1496 | rsp_code = rsp->data.info.rsp_code; |
| 1497 | |
Tyrel Datwyler | 6dc6a94 | 2019-03-20 14:56:51 -0500 | [diff] [blame] | 1498 | scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) " |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1499 | "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n", |
| 1500 | cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error, |
| 1501 | rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status); |
| 1502 | } |
| 1503 | |
| 1504 | /** |
Brian King | 6d29cc5 | 2009-05-28 16:17:33 -0500 | [diff] [blame] | 1505 | * ibmvfc_relogin - Log back into the specified device |
| 1506 | * @sdev: scsi device struct |
| 1507 | * |
| 1508 | **/ |
| 1509 | static void ibmvfc_relogin(struct scsi_device *sdev) |
| 1510 | { |
| 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; |
| 1514 | |
| 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); |
| 1518 | break; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | ibmvfc_reinit_host(vhost); |
| 1523 | } |
| 1524 | |
| 1525 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1526 | * ibmvfc_scsi_done - Handle responses from commands |
| 1527 | * @evt: ibmvfc event to be handled |
| 1528 | * |
| 1529 | * Used as a callback when sending scsi cmds. |
| 1530 | **/ |
| 1531 | static void ibmvfc_scsi_done(struct ibmvfc_event *evt) |
| 1532 | { |
| 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; |
Brian King | 2bac406 | 2008-08-15 10:59:26 -0500 | [diff] [blame] | 1536 | u32 rsp_len = 0; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1537 | u32 sense_len = be32_to_cpu(rsp->fcp_sense_len); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1538 | |
| 1539 | if (cmnd) { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1542 | else if (rsp->flags & FCP_RESID_UNDER) |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1543 | scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1544 | else |
| 1545 | scsi_set_resid(cmnd, 0); |
| 1546 | |
| 1547 | if (vfc_cmd->status) { |
| 1548 | cmnd->result = ibmvfc_get_err_result(vfc_cmd); |
| 1549 | |
| 1550 | if (rsp->flags & FCP_RSP_LEN_VALID) |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1551 | rsp_len = be32_to_cpu(rsp->fcp_rsp_len); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1552 | if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE) |
| 1553 | sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len; |
Brian King | 2bac406 | 2008-08-15 10:59:26 -0500 | [diff] [blame] | 1554 | if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1555 | memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1556 | if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) && |
| 1557 | (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED)) |
Brian King | 6d29cc5 | 2009-05-28 16:17:33 -0500 | [diff] [blame] | 1558 | ibmvfc_relogin(cmnd->device); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1559 | |
Brian King | 50119da | 2008-10-29 08:46:36 -0500 | [diff] [blame] | 1560 | if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER))) |
| 1561 | cmnd->result = (DID_ERROR << 16); |
| 1562 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1563 | ibmvfc_log_error(evt); |
| 1564 | } |
| 1565 | |
| 1566 | if (!cmnd->result && |
| 1567 | (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow)) |
| 1568 | cmnd->result = (DID_ERROR << 16); |
| 1569 | |
| 1570 | scsi_dma_unmap(cmnd); |
| 1571 | cmnd->scsi_done(cmnd); |
| 1572 | } |
| 1573 | |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 1574 | if (evt->eh_comp) |
| 1575 | complete(evt->eh_comp); |
| 1576 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1577 | ibmvfc_free_event(evt); |
| 1578 | } |
| 1579 | |
| 1580 | /** |
| 1581 | * ibmvfc_host_chkready - Check if the host can accept commands |
| 1582 | * @vhost: struct ibmvfc host |
| 1583 | * |
| 1584 | * Returns: |
| 1585 | * 1 if host can accept command / 0 if not |
| 1586 | **/ |
| 1587 | static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost) |
| 1588 | { |
| 1589 | int result = 0; |
| 1590 | |
| 1591 | switch (vhost->state) { |
| 1592 | case IBMVFC_LINK_DEAD: |
| 1593 | case IBMVFC_HOST_OFFLINE: |
| 1594 | result = DID_NO_CONNECT << 16; |
| 1595 | break; |
| 1596 | case IBMVFC_NO_CRQ: |
| 1597 | case IBMVFC_INITIALIZING: |
| 1598 | case IBMVFC_HALTED: |
| 1599 | case IBMVFC_LINK_DOWN: |
| 1600 | result = DID_REQUEUE << 16; |
| 1601 | break; |
| 1602 | case IBMVFC_ACTIVE: |
| 1603 | result = 0; |
| 1604 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 1605 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1606 | |
| 1607 | return result; |
| 1608 | } |
| 1609 | |
| 1610 | /** |
| 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 |
| 1614 | * |
| 1615 | * Returns: |
| 1616 | * 0 on success / other on failure |
| 1617 | **/ |
Jeff Garzik | f281233 | 2010-11-16 02:10:29 -0500 | [diff] [blame] | 1618 | static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1619 | void (*done) (struct scsi_cmnd *)) |
| 1620 | { |
| 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; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1625 | int rc; |
| 1626 | |
| 1627 | if (unlikely((rc = fc_remote_port_chkready(rport))) || |
| 1628 | unlikely((rc = ibmvfc_host_chkready(vhost)))) { |
| 1629 | cmnd->result = rc; |
| 1630 | done(cmnd); |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | cmnd->result = (DID_OK << 16); |
| 1635 | evt = ibmvfc_get_event(vhost); |
| 1636 | ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT); |
| 1637 | evt->cmnd = cmnd; |
| 1638 | cmnd->scsi_done = done; |
| 1639 | vfc_cmd = &evt->iu.cmd; |
| 1640 | memset(vfc_cmd, 0, sizeof(*vfc_cmd)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1649 | int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun); |
| 1650 | memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len); |
| 1651 | |
Christoph Hellwig | 5066863 | 2014-10-30 14:30:06 +0100 | [diff] [blame] | 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; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev)))) |
| 1658 | return ibmvfc_send_event(evt, vhost, 0); |
| 1659 | |
| 1660 | ibmvfc_free_event(evt); |
| 1661 | if (rc == -ENOMEM) |
| 1662 | return SCSI_MLQUEUE_HOST_BUSY; |
| 1663 | |
| 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); |
| 1667 | |
| 1668 | cmnd->result = DID_ERROR << 16; |
| 1669 | done(cmnd); |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
Jeff Garzik | f281233 | 2010-11-16 02:10:29 -0500 | [diff] [blame] | 1673 | static DEF_SCSI_QCMD(ibmvfc_queuecommand) |
| 1674 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1675 | /** |
| 1676 | * ibmvfc_sync_completion - Signal that a synchronous command has completed |
| 1677 | * @evt: ibmvfc event struct |
| 1678 | * |
| 1679 | **/ |
| 1680 | static void ibmvfc_sync_completion(struct ibmvfc_event *evt) |
| 1681 | { |
| 1682 | /* copy the response back */ |
| 1683 | if (evt->sync_iu) |
| 1684 | *evt->sync_iu = *evt->xfer_iu; |
| 1685 | |
| 1686 | complete(&evt->comp); |
| 1687 | } |
| 1688 | |
| 1689 | /** |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1690 | * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands |
| 1691 | * @evt: struct ibmvfc_event |
| 1692 | * |
| 1693 | **/ |
| 1694 | static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt) |
| 1695 | { |
| 1696 | struct ibmvfc_host *vhost = evt->vhost; |
| 1697 | |
| 1698 | ibmvfc_free_event(evt); |
| 1699 | vhost->aborting_passthru = 0; |
| 1700 | dev_info(vhost->dev, "Passthru command cancelled\n"); |
| 1701 | } |
| 1702 | |
| 1703 | /** |
| 1704 | * ibmvfc_bsg_timeout - Handle a BSG timeout |
Johannes Thumshirn | 75cc8cf | 2016-11-17 10:31:19 +0100 | [diff] [blame] | 1705 | * @job: struct bsg_job that timed out |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1706 | * |
| 1707 | * Returns: |
| 1708 | * 0 on success / other on failure |
| 1709 | **/ |
Johannes Thumshirn | 75cc8cf | 2016-11-17 10:31:19 +0100 | [diff] [blame] | 1710 | static int ibmvfc_bsg_timeout(struct bsg_job *job) |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1711 | { |
Johannes Thumshirn | cd21c60 | 2016-11-17 10:31:14 +0100 | [diff] [blame] | 1712 | struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job)); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1713 | unsigned long port_id = (unsigned long)job->dd_data; |
| 1714 | struct ibmvfc_event *evt; |
| 1715 | struct ibmvfc_tmf *tmf; |
| 1716 | unsigned long flags; |
| 1717 | int rc; |
| 1718 | |
| 1719 | ENTER; |
| 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); |
| 1724 | return 0; |
| 1725 | } |
| 1726 | |
| 1727 | vhost->aborting_passthru = 1; |
| 1728 | evt = ibmvfc_get_event(vhost); |
| 1729 | ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT); |
| 1730 | |
| 1731 | tmf = &evt->iu.tmf; |
| 1732 | memset(tmf, 0, sizeof(*tmf)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1739 | rc = ibmvfc_send_event(evt, vhost, default_timeout); |
| 1740 | |
| 1741 | if (rc != 0) { |
| 1742 | vhost->aborting_passthru = 0; |
| 1743 | dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc); |
| 1744 | rc = -EIO; |
| 1745 | } else |
| 1746 | dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n", |
| 1747 | port_id); |
| 1748 | |
| 1749 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 1750 | |
| 1751 | LEAVE; |
| 1752 | return rc; |
| 1753 | } |
| 1754 | |
| 1755 | /** |
| 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 |
| 1759 | * |
| 1760 | * Returns: |
| 1761 | * 0 on success / other on failure |
| 1762 | **/ |
| 1763 | static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id) |
| 1764 | { |
| 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; |
| 1771 | |
| 1772 | ENTER; |
| 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) { |
| 1776 | issue_login = 0; |
| 1777 | break; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | if (!issue_login) |
| 1782 | goto unlock_out; |
| 1783 | if (unlikely((rc = ibmvfc_host_chkready(vhost)))) |
| 1784 | goto unlock_out; |
| 1785 | |
| 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)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1794 | evt->sync_iu = &rsp_iu; |
| 1795 | init_completion(&evt->comp); |
| 1796 | |
| 1797 | rc = ibmvfc_send_event(evt, vhost, default_timeout); |
| 1798 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 1799 | |
| 1800 | if (rc) |
| 1801 | return -EIO; |
| 1802 | |
| 1803 | wait_for_completion(&evt->comp); |
| 1804 | |
| 1805 | if (rsp_iu.plogi.common.status) |
| 1806 | rc = -EIO; |
| 1807 | |
| 1808 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 1809 | ibmvfc_free_event(evt); |
| 1810 | unlock_out: |
| 1811 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 1812 | LEAVE; |
| 1813 | return rc; |
| 1814 | } |
| 1815 | |
| 1816 | /** |
| 1817 | * ibmvfc_bsg_request - Handle a BSG request |
Johannes Thumshirn | 75cc8cf | 2016-11-17 10:31:19 +0100 | [diff] [blame] | 1818 | * @job: struct bsg_job to be executed |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1819 | * |
| 1820 | * Returns: |
| 1821 | * 0 on success / other on failure |
| 1822 | **/ |
Johannes Thumshirn | 75cc8cf | 2016-11-17 10:31:19 +0100 | [diff] [blame] | 1823 | static int ibmvfc_bsg_request(struct bsg_job *job) |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1824 | { |
Johannes Thumshirn | cd21c60 | 2016-11-17 10:31:14 +0100 | [diff] [blame] | 1825 | struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job)); |
Johannes Thumshirn | 1d69b12 | 2016-11-17 10:31:15 +0100 | [diff] [blame] | 1826 | struct fc_rport *rport = fc_bsg_to_rport(job); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1827 | struct ibmvfc_passthru_mad *mad; |
| 1828 | struct ibmvfc_event *evt; |
| 1829 | union ibmvfc_iu rsp_iu; |
| 1830 | unsigned long flags, port_id = -1; |
Johannes Thumshirn | 01e0e15 | 2016-11-17 10:31:12 +0100 | [diff] [blame] | 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; |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1834 | int rc = 0, req_seg, rsp_seg, issue_login = 0; |
| 1835 | u32 fc_flags, rsp_len; |
| 1836 | |
| 1837 | ENTER; |
Johannes Thumshirn | 01e0e15 | 2016-11-17 10:31:12 +0100 | [diff] [blame] | 1838 | bsg_reply->reply_payload_rcv_len = 0; |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1839 | if (rport) |
| 1840 | port_id = rport->port_id; |
| 1841 | |
| 1842 | switch (code) { |
| 1843 | case FC_BSG_HST_ELS_NOLOGIN: |
Johannes Thumshirn | 01e0e15 | 2016-11-17 10:31:12 +0100 | [diff] [blame] | 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]; |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1847 | case FC_BSG_RPT_ELS: |
| 1848 | fc_flags = IBMVFC_FC_ELS; |
| 1849 | break; |
| 1850 | case FC_BSG_HST_CT: |
| 1851 | issue_login = 1; |
Johannes Thumshirn | 01e0e15 | 2016-11-17 10:31:12 +0100 | [diff] [blame] | 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]; |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1855 | case FC_BSG_RPT_CT: |
| 1856 | fc_flags = IBMVFC_FC_CT_IU; |
| 1857 | break; |
| 1858 | default: |
| 1859 | return -ENOTSUPP; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 1860 | } |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1861 | |
| 1862 | if (port_id == -1) |
| 1863 | return -EINVAL; |
| 1864 | if (!mutex_trylock(&vhost->passthru_mutex)) |
| 1865 | return -EBUSY; |
| 1866 | |
| 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); |
| 1870 | |
| 1871 | if (!req_seg) { |
| 1872 | mutex_unlock(&vhost->passthru_mutex); |
| 1873 | return -ENOMEM; |
| 1874 | } |
| 1875 | |
| 1876 | rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list, |
| 1877 | job->reply_payload.sg_cnt, DMA_FROM_DEVICE); |
| 1878 | |
| 1879 | if (!rsp_seg) { |
| 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); |
| 1883 | return -ENOMEM; |
| 1884 | } |
| 1885 | |
| 1886 | if (req_seg > 1 || rsp_seg > 1) { |
| 1887 | rc = -EINVAL; |
| 1888 | goto out; |
| 1889 | } |
| 1890 | |
| 1891 | if (issue_login) |
| 1892 | rc = ibmvfc_bsg_plogi(vhost, port_id); |
| 1893 | |
| 1894 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 1895 | |
| 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); |
| 1899 | goto out; |
| 1900 | } |
| 1901 | |
| 1902 | evt = ibmvfc_get_event(vhost); |
| 1903 | ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT); |
| 1904 | mad = &evt->iu.passthru; |
| 1905 | |
| 1906 | memset(mad, 0, sizeof(*mad)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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)); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1910 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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)); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1914 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1919 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1927 | |
| 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); |
| 1932 | |
| 1933 | if (rc) { |
| 1934 | rc = -EIO; |
| 1935 | goto out; |
| 1936 | } |
| 1937 | |
| 1938 | wait_for_completion(&evt->comp); |
| 1939 | |
| 1940 | if (rsp_iu.passthru.common.status) |
| 1941 | rc = -EIO; |
| 1942 | else |
Johannes Thumshirn | 01e0e15 | 2016-11-17 10:31:12 +0100 | [diff] [blame] | 1943 | bsg_reply->reply_payload_rcv_len = rsp_len; |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1944 | |
| 1945 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 1946 | ibmvfc_free_event(evt); |
| 1947 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
Johannes Thumshirn | 01e0e15 | 2016-11-17 10:31:12 +0100 | [diff] [blame] | 1948 | bsg_reply->result = rc; |
Johannes Thumshirn | 0654816 | 2016-11-17 10:31:22 +0100 | [diff] [blame] | 1949 | bsg_job_done(job, bsg_reply->result, |
Johannes Thumshirn | 1abaede | 2016-11-17 10:31:13 +0100 | [diff] [blame] | 1950 | bsg_reply->reply_payload_rcv_len); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 1951 | rc = 0; |
| 1952 | out: |
| 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); |
| 1958 | LEAVE; |
| 1959 | return rc; |
| 1960 | } |
| 1961 | |
| 1962 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1963 | * ibmvfc_reset_device - Reset the device with the specified reset type |
| 1964 | * @sdev: scsi device to reset |
| 1965 | * @type: reset type |
| 1966 | * @desc: reset type description for log messages |
| 1967 | * |
| 1968 | * Returns: |
| 1969 | * 0 on success / other on failure |
| 1970 | **/ |
| 1971 | static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc) |
| 1972 | { |
| 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; |
Brian King | 50ed9a0 | 2008-10-29 08:46:47 -0500 | [diff] [blame] | 1976 | struct ibmvfc_event *evt = NULL; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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; |
| 1981 | int rsp_code = 0; |
| 1982 | |
| 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); |
| 1987 | |
| 1988 | tmf = &evt->iu.cmd; |
| 1989 | memset(tmf, 0, sizeof(*tmf)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1997 | int_to_scsilun(sdev->lun, &tmf->iu.lun); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 1998 | tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 1999 | tmf->iu.tmf_flags = type; |
| 2000 | evt->sync_iu = &rsp_iu; |
| 2001 | |
| 2002 | init_completion(&evt->comp); |
| 2003 | rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout); |
| 2004 | } |
| 2005 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 2006 | |
| 2007 | if (rsp_rc != 0) { |
| 2008 | sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n", |
| 2009 | desc, rsp_rc); |
| 2010 | return -EIO; |
| 2011 | } |
| 2012 | |
| 2013 | sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc); |
| 2014 | wait_for_completion(&evt->comp); |
| 2015 | |
Brian King | 230934a | 2009-10-19 15:07:47 -0500 | [diff] [blame] | 2016 | if (rsp_iu.cmd.status) |
| 2017 | rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd); |
| 2018 | |
| 2019 | if (rsp_code) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2020 | if (fc_rsp->flags & FCP_RSP_LEN_VALID) |
| 2021 | rsp_code = fc_rsp->data.info.rsp_code; |
| 2022 | |
| 2023 | sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) " |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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)), |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2026 | rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code, |
| 2027 | fc_rsp->scsi_status); |
| 2028 | rsp_rc = -EIO; |
| 2029 | } else |
| 2030 | sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc); |
| 2031 | |
| 2032 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 2033 | ibmvfc_free_event(evt); |
| 2034 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 2035 | return rsp_rc; |
| 2036 | } |
| 2037 | |
| 2038 | /** |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2039 | * ibmvfc_match_rport - Match function for specified remote port |
| 2040 | * @evt: ibmvfc event struct |
| 2041 | * @device: device to match (rport) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2042 | * |
| 2043 | * Returns: |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2044 | * 1 if event matches rport / 0 if event does not match rport |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2045 | **/ |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2046 | static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2047 | { |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2048 | struct fc_rport *cmd_rport; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2049 | |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2050 | if (evt->cmnd) { |
| 2051 | cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device)); |
| 2052 | if (cmd_rport == rport) |
| 2053 | return 1; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2054 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2055 | return 0; |
| 2056 | } |
| 2057 | |
| 2058 | /** |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2059 | * ibmvfc_match_target - Match function for specified target |
| 2060 | * @evt: ibmvfc event struct |
| 2061 | * @device: device to match (starget) |
| 2062 | * |
| 2063 | * Returns: |
| 2064 | * 1 if event matches starget / 0 if event does not match starget |
| 2065 | **/ |
| 2066 | static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device) |
| 2067 | { |
| 2068 | if (evt->cmnd && scsi_target(evt->cmnd->device) == device) |
| 2069 | return 1; |
| 2070 | return 0; |
| 2071 | } |
| 2072 | |
| 2073 | /** |
| 2074 | * ibmvfc_match_lun - Match function for specified LUN |
| 2075 | * @evt: ibmvfc event struct |
| 2076 | * @device: device to match (sdev) |
| 2077 | * |
| 2078 | * Returns: |
| 2079 | * 1 if event matches sdev / 0 if event does not match sdev |
| 2080 | **/ |
| 2081 | static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device) |
| 2082 | { |
| 2083 | if (evt->cmnd && evt->cmnd->device == device) |
| 2084 | return 1; |
| 2085 | return 0; |
| 2086 | } |
| 2087 | |
| 2088 | /** |
| 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 |
| 2093 | * |
| 2094 | * Returns: |
| 2095 | * SUCCESS / FAILED |
| 2096 | **/ |
| 2097 | static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device, |
| 2098 | int (*match) (struct ibmvfc_event *, void *)) |
| 2099 | { |
| 2100 | struct ibmvfc_event *evt; |
| 2101 | DECLARE_COMPLETION_ONSTACK(comp); |
| 2102 | int wait; |
| 2103 | unsigned long flags; |
Brian King | daa142d | 2010-04-20 14:21:35 -0500 | [diff] [blame] | 2104 | signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ; |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2105 | |
| 2106 | ENTER; |
| 2107 | do { |
| 2108 | wait = 0; |
| 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 = ∁ |
| 2113 | wait++; |
| 2114 | } |
| 2115 | } |
| 2116 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 2117 | |
| 2118 | if (wait) { |
| 2119 | timeout = wait_for_completion_timeout(&comp, timeout); |
| 2120 | |
| 2121 | if (!timeout) { |
| 2122 | wait = 0; |
| 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; |
| 2127 | wait++; |
| 2128 | } |
| 2129 | } |
| 2130 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 2131 | if (wait) |
| 2132 | dev_err(vhost->dev, "Timed out waiting for aborted commands\n"); |
| 2133 | LEAVE; |
| 2134 | return wait ? FAILED : SUCCESS; |
| 2135 | } |
| 2136 | } |
| 2137 | } while (wait); |
| 2138 | |
| 2139 | LEAVE; |
| 2140 | return SUCCESS; |
| 2141 | } |
| 2142 | |
| 2143 | /** |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 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 |
| 2147 | * |
| 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. |
| 2150 | * |
| 2151 | * Returns: |
| 2152 | * 0 on success / other on failure |
| 2153 | **/ |
| 2154 | static int ibmvfc_cancel_all(struct scsi_device *sdev, int type) |
| 2155 | { |
| 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; |
| 2164 | u16 status; |
| 2165 | |
| 2166 | ENTER; |
| 2167 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 2168 | found_evt = NULL; |
| 2169 | list_for_each_entry(evt, &vhost->sent, queue) { |
| 2170 | if (evt->cmnd && evt->cmnd->device == sdev) { |
| 2171 | found_evt = evt; |
| 2172 | break; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | if (!found_evt) { |
| 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); |
| 2180 | return 0; |
| 2181 | } |
| 2182 | |
Brian King | 55d29bf | 2013-04-12 08:25:17 -0500 | [diff] [blame] | 2183 | if (vhost->logged_in) { |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2184 | evt = ibmvfc_get_event(vhost); |
| 2185 | ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT); |
| 2186 | |
| 2187 | tmf = &evt->iu.tmf; |
| 2188 | memset(tmf, 0, sizeof(*tmf)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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); |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2193 | int_to_scsilun(sdev->lun, &tmf->lun); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2194 | if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS)) |
Brian King | 90f725d | 2013-04-12 08:25:18 -0500 | [diff] [blame] | 2195 | type &= ~IBMVFC_TMF_SUPPRESS_ABTS; |
Brian King | 55d29bf | 2013-04-12 08:25:17 -0500 | [diff] [blame] | 2196 | if (vhost->state == IBMVFC_ACTIVE) |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2197 | tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID)); |
Brian King | 55d29bf | 2013-04-12 08:25:17 -0500 | [diff] [blame] | 2198 | else |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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); |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2202 | |
| 2203 | evt->sync_iu = &rsp; |
| 2204 | init_completion(&evt->comp); |
| 2205 | rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout); |
| 2206 | } |
| 2207 | |
| 2208 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 2209 | |
| 2210 | if (rsp_rc != 0) { |
| 2211 | sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc); |
Brian King | c281e32 | 2013-08-20 11:08:42 -0500 | [diff] [blame] | 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 */ |
| 2215 | return 0; |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n"); |
| 2219 | |
| 2220 | wait_for_completion(&evt->comp); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2221 | status = be16_to_cpu(rsp.mad_common.status); |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2222 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 2223 | ibmvfc_free_event(evt); |
| 2224 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 2225 | |
| 2226 | if (status != IBMVFC_MAD_SUCCESS) { |
| 2227 | sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status); |
Brian King | c281e32 | 2013-08-20 11:08:42 -0500 | [diff] [blame] | 2228 | switch (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 */ |
| 2233 | return 0; |
| 2234 | default: |
| 2235 | return -EIO; |
| 2236 | }; |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n"); |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
| 2243 | /** |
| 2244 | * ibmvfc_match_key - Match function for specified cancel key |
| 2245 | * @evt: ibmvfc event struct |
| 2246 | * @key: cancel key to match |
| 2247 | * |
| 2248 | * Returns: |
| 2249 | * 1 if event matches key / 0 if event does not match key |
| 2250 | **/ |
| 2251 | static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key) |
| 2252 | { |
| 2253 | unsigned long cancel_key = (unsigned long)key; |
| 2254 | |
| 2255 | if (evt->crq.format == IBMVFC_CMD_FORMAT && |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2256 | be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key) |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2257 | return 1; |
| 2258 | return 0; |
| 2259 | } |
| 2260 | |
| 2261 | /** |
Brian King | a077c7f | 2012-08-24 16:50:59 -0500 | [diff] [blame] | 2262 | * ibmvfc_match_evt - Match function for specified event |
| 2263 | * @evt: ibmvfc event struct |
| 2264 | * @match: event to match |
| 2265 | * |
| 2266 | * Returns: |
| 2267 | * 1 if event matches key / 0 if event does not match key |
| 2268 | **/ |
| 2269 | static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match) |
| 2270 | { |
| 2271 | if (evt == match) |
| 2272 | return 1; |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
| 2276 | /** |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2277 | * ibmvfc_abort_task_set - Abort outstanding commands to the device |
| 2278 | * @sdev: scsi device to abort commands |
| 2279 | * |
| 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. |
| 2282 | * |
| 2283 | * Returns: |
| 2284 | * 0 on success / other on failure |
| 2285 | **/ |
| 2286 | static int ibmvfc_abort_task_set(struct scsi_device *sdev) |
| 2287 | { |
| 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; |
| 2296 | int rsp_code = 0; |
| 2297 | |
| 2298 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 2299 | found_evt = NULL; |
| 2300 | list_for_each_entry(evt, &vhost->sent, queue) { |
| 2301 | if (evt->cmnd && evt->cmnd->device == sdev) { |
| 2302 | found_evt = evt; |
| 2303 | break; |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | if (!found_evt) { |
| 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); |
| 2311 | return 0; |
| 2312 | } |
| 2313 | |
| 2314 | if (vhost->state == IBMVFC_ACTIVE) { |
| 2315 | evt = ibmvfc_get_event(vhost); |
| 2316 | ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT); |
| 2317 | |
| 2318 | tmf = &evt->iu.cmd; |
| 2319 | memset(tmf, 0, sizeof(*tmf)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 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); |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2327 | int_to_scsilun(sdev->lun, &tmf->iu.lun); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2328 | tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF)); |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2329 | tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET; |
| 2330 | evt->sync_iu = &rsp_iu; |
| 2331 | |
| 2332 | init_completion(&evt->comp); |
| 2333 | rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout); |
| 2334 | } |
| 2335 | |
| 2336 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 2337 | |
| 2338 | if (rsp_rc != 0) { |
| 2339 | sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc); |
| 2340 | return -EIO; |
| 2341 | } |
| 2342 | |
| 2343 | sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n"); |
| 2344 | timeout = wait_for_completion_timeout(&evt->comp, timeout); |
| 2345 | |
| 2346 | if (!timeout) { |
Brian King | f8804b7 | 2013-04-12 08:25:15 -0500 | [diff] [blame] | 2347 | rc = ibmvfc_cancel_all(sdev, 0); |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2348 | if (!rc) { |
| 2349 | rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key); |
| 2350 | if (rc == SUCCESS) |
| 2351 | rc = 0; |
| 2352 | } |
| 2353 | |
| 2354 | if (rc) { |
| 2355 | sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n"); |
| 2356 | ibmvfc_reset_host(vhost); |
Brian King | a077c7f | 2012-08-24 16:50:59 -0500 | [diff] [blame] | 2357 | rsp_rc = -EIO; |
| 2358 | rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key); |
| 2359 | |
| 2360 | if (rc == SUCCESS) |
| 2361 | rsp_rc = 0; |
| 2362 | |
| 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); |
| 2368 | rsp_rc = 0; |
| 2369 | } |
| 2370 | |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2371 | goto out; |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | if (rsp_iu.cmd.status) |
| 2376 | rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd); |
| 2377 | |
| 2378 | if (rsp_code) { |
| 2379 | if (fc_rsp->flags & FCP_RSP_LEN_VALID) |
| 2380 | rsp_code = fc_rsp->data.info.rsp_code; |
| 2381 | |
| 2382 | sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) " |
| 2383 | "flags: %x fcp_rsp: %x, scsi_status: %x\n", |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2384 | ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)), |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2385 | rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code, |
| 2386 | fc_rsp->scsi_status); |
| 2387 | rsp_rc = -EIO; |
| 2388 | } else |
| 2389 | sdev_printk(KERN_INFO, sdev, "Abort successful\n"); |
| 2390 | |
| 2391 | out: |
| 2392 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 2393 | ibmvfc_free_event(evt); |
| 2394 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 2395 | return rsp_rc; |
| 2396 | } |
| 2397 | |
| 2398 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2399 | * ibmvfc_eh_abort_handler - Abort a command |
| 2400 | * @cmd: scsi command to abort |
| 2401 | * |
| 2402 | * Returns: |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2403 | * SUCCESS / FAST_IO_FAIL / FAILED |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2404 | **/ |
| 2405 | static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd) |
| 2406 | { |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2407 | struct scsi_device *sdev = cmd->device; |
| 2408 | struct ibmvfc_host *vhost = shost_priv(sdev->host); |
Brian King | 55d29bf | 2013-04-12 08:25:17 -0500 | [diff] [blame] | 2409 | int cancel_rc, block_rc; |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2410 | int rc = FAILED; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2411 | |
| 2412 | ENTER; |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2413 | block_rc = fc_block_scsi_eh(cmd); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2414 | ibmvfc_wait_while_resetting(vhost); |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2415 | if (block_rc != FAST_IO_FAIL) { |
| 2416 | cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET); |
Brian King | 55d29bf | 2013-04-12 08:25:17 -0500 | [diff] [blame] | 2417 | ibmvfc_abort_task_set(sdev); |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2418 | } else |
Brian King | 90f725d | 2013-04-12 08:25:18 -0500 | [diff] [blame] | 2419 | cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2420 | |
Brian King | 55d29bf | 2013-04-12 08:25:17 -0500 | [diff] [blame] | 2421 | if (!cancel_rc) |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2422 | rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2423 | |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2424 | if (block_rc == FAST_IO_FAIL && rc != FAILED) |
| 2425 | rc = FAST_IO_FAIL; |
| 2426 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2427 | LEAVE; |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2428 | return rc; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | /** |
| 2432 | * ibmvfc_eh_device_reset_handler - Reset a single LUN |
| 2433 | * @cmd: scsi command struct |
| 2434 | * |
| 2435 | * Returns: |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2436 | * SUCCESS / FAST_IO_FAIL / FAILED |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2437 | **/ |
| 2438 | static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd) |
| 2439 | { |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2440 | struct scsi_device *sdev = cmd->device; |
| 2441 | struct ibmvfc_host *vhost = shost_priv(sdev->host); |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2442 | int cancel_rc, block_rc, reset_rc = 0; |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2443 | int rc = FAILED; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2444 | |
| 2445 | ENTER; |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2446 | block_rc = fc_block_scsi_eh(cmd); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2447 | ibmvfc_wait_while_resetting(vhost); |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 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"); |
| 2451 | } else |
Brian King | 90f725d | 2013-04-12 08:25:18 -0500 | [diff] [blame] | 2452 | cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2453 | |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2454 | if (!cancel_rc && !reset_rc) |
| 2455 | rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2456 | |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2457 | if (block_rc == FAST_IO_FAIL && rc != FAILED) |
| 2458 | rc = FAST_IO_FAIL; |
| 2459 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2460 | LEAVE; |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2461 | return rc; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | /** |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2465 | * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function |
| 2466 | * @sdev: scsi device struct |
| 2467 | * @data: return code |
| 2468 | * |
| 2469 | **/ |
| 2470 | static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data) |
| 2471 | { |
| 2472 | unsigned long *rc = data; |
Brian King | 90f725d | 2013-04-12 08:25:18 -0500 | [diff] [blame] | 2473 | *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS); |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | /** |
Brian King | 4a5c4a5 | 2009-10-19 15:07:53 -0500 | [diff] [blame] | 2477 | * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function |
| 2478 | * @sdev: scsi device struct |
| 2479 | * @data: return code |
| 2480 | * |
| 2481 | **/ |
| 2482 | static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2483 | { |
| 2484 | unsigned long *rc = data; |
| 2485 | *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET); |
| 2486 | } |
| 2487 | |
| 2488 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2489 | * ibmvfc_eh_target_reset_handler - Reset the target |
| 2490 | * @cmd: scsi command struct |
| 2491 | * |
| 2492 | * Returns: |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2493 | * SUCCESS / FAST_IO_FAIL / FAILED |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2494 | **/ |
| 2495 | static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd) |
| 2496 | { |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2497 | struct scsi_device *sdev = cmd->device; |
| 2498 | struct ibmvfc_host *vhost = shost_priv(sdev->host); |
| 2499 | struct scsi_target *starget = scsi_target(sdev); |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2500 | int block_rc; |
| 2501 | int reset_rc = 0; |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2502 | int rc = FAILED; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2503 | unsigned long cancel_rc = 0; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2504 | |
| 2505 | ENTER; |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2506 | block_rc = fc_block_scsi_eh(cmd); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2507 | ibmvfc_wait_while_resetting(vhost); |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 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"); |
| 2511 | } else |
| 2512 | starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2513 | |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2514 | if (!cancel_rc && !reset_rc) |
| 2515 | rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2516 | |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2517 | if (block_rc == FAST_IO_FAIL && rc != FAILED) |
| 2518 | rc = FAST_IO_FAIL; |
| 2519 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2520 | LEAVE; |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2521 | return rc; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | /** |
| 2525 | * ibmvfc_eh_host_reset_handler - Reset the connection to the server |
| 2526 | * @cmd: struct scsi_cmnd having problems |
| 2527 | * |
| 2528 | **/ |
| 2529 | static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 2530 | { |
Hannes Reinecke | 8d8a3f5 | 2017-08-25 13:56:58 +0200 | [diff] [blame] | 2531 | int rc; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2532 | struct ibmvfc_host *vhost = shost_priv(cmd->device->host); |
| 2533 | |
| 2534 | dev_err(vhost->dev, "Resetting connection due to error recovery\n"); |
| 2535 | rc = ibmvfc_issue_fc_host_lip(vhost->host); |
Brian King | 93631b4 | 2013-04-12 08:25:16 -0500 | [diff] [blame] | 2536 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2537 | return rc ? FAILED : SUCCESS; |
| 2538 | } |
| 2539 | |
| 2540 | /** |
| 2541 | * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport. |
| 2542 | * @rport: rport struct |
| 2543 | * |
| 2544 | * Return value: |
| 2545 | * none |
| 2546 | **/ |
| 2547 | static void ibmvfc_terminate_rport_io(struct fc_rport *rport) |
| 2548 | { |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2549 | struct Scsi_Host *shost = rport_to_shost(rport); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2550 | struct ibmvfc_host *vhost = shost_priv(shost); |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2551 | struct fc_rport *dev_rport; |
| 2552 | struct scsi_device *sdev; |
| 2553 | unsigned long rc; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2554 | |
| 2555 | ENTER; |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2556 | shost_for_each_device(sdev, shost) { |
| 2557 | dev_rport = starget_to_rport(scsi_target(sdev)); |
| 2558 | if (dev_rport != rport) |
| 2559 | continue; |
Brian King | 90f725d | 2013-04-12 08:25:18 -0500 | [diff] [blame] | 2560 | ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS); |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2561 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2562 | |
Brian King | d2fab5c | 2010-08-05 16:38:34 -0500 | [diff] [blame] | 2563 | rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport); |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2564 | |
| 2565 | if (rc == FAILED) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2566 | ibmvfc_issue_fc_host_lip(shost); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2567 | LEAVE; |
| 2568 | } |
| 2569 | |
Brian King | d99e5f4 | 2010-09-09 16:20:42 -0500 | [diff] [blame] | 2570 | static const struct ibmvfc_async_desc ae_desc [] = { |
Robert Jennings | 402c6ee | 2010-12-09 14:03:59 -0600 | [diff] [blame] | 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 }, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2584 | }; |
| 2585 | |
Brian King | d99e5f4 | 2010-09-09 16:20:42 -0500 | [diff] [blame] | 2586 | static const struct ibmvfc_async_desc unknown_ae = { |
Robert Jennings | 402c6ee | 2010-12-09 14:03:59 -0600 | [diff] [blame] | 2587 | "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL |
Brian King | d99e5f4 | 2010-09-09 16:20:42 -0500 | [diff] [blame] | 2588 | }; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2589 | |
| 2590 | /** |
| 2591 | * ibmvfc_get_ae_desc - Get text description for async event |
| 2592 | * @ae: async event |
| 2593 | * |
| 2594 | **/ |
Brian King | d99e5f4 | 2010-09-09 16:20:42 -0500 | [diff] [blame] | 2595 | static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2596 | { |
| 2597 | int i; |
| 2598 | |
| 2599 | for (i = 0; i < ARRAY_SIZE(ae_desc); i++) |
| 2600 | if (ae_desc[i].ae == ae) |
Brian King | d99e5f4 | 2010-09-09 16:20:42 -0500 | [diff] [blame] | 2601 | return &ae_desc[i]; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2602 | |
Brian King | d99e5f4 | 2010-09-09 16:20:42 -0500 | [diff] [blame] | 2603 | return &unknown_ae; |
| 2604 | } |
| 2605 | |
| 2606 | static const struct { |
| 2607 | enum ibmvfc_ae_link_state state; |
| 2608 | const char *desc; |
| 2609 | } link_desc [] = { |
| 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" }, |
| 2614 | }; |
| 2615 | |
| 2616 | /** |
| 2617 | * ibmvfc_get_link_state - Get text description for link state |
| 2618 | * @state: link state |
| 2619 | * |
| 2620 | **/ |
| 2621 | static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state) |
| 2622 | { |
| 2623 | int i; |
| 2624 | |
| 2625 | for (i = 0; i < ARRAY_SIZE(link_desc); i++) |
| 2626 | if (link_desc[i].state == state) |
| 2627 | return link_desc[i].desc; |
| 2628 | |
| 2629 | return ""; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | /** |
| 2633 | * ibmvfc_handle_async - Handle an async event from the adapter |
| 2634 | * @crq: crq to process |
| 2635 | * @vhost: ibmvfc host struct |
| 2636 | * |
| 2637 | **/ |
| 2638 | static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq, |
| 2639 | struct ibmvfc_host *vhost) |
| 2640 | { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2641 | const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event)); |
Brian King | 6d29cc5 | 2009-05-28 16:17:33 -0500 | [diff] [blame] | 2642 | struct ibmvfc_target *tgt; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2643 | |
Brian King | d99e5f4 | 2010-09-09 16:20:42 -0500 | [diff] [blame] | 2644 | ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx," |
Tyrel Datwyler | 21367e2 | 2016-02-11 16:24:35 -0600 | [diff] [blame] | 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), |
Brian King | d99e5f4 | 2010-09-09 16:20:42 -0500 | [diff] [blame] | 2647 | ibmvfc_get_link_state(crq->link_state)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2648 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2649 | switch (be64_to_cpu(crq->event)) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2650 | case IBMVFC_AE_RESUME: |
Brian King | 497f9c5 | 2009-05-28 16:17:30 -0500 | [diff] [blame] | 2651 | switch (crq->link_state) { |
| 2652 | case IBMVFC_AE_LS_LINK_DOWN: |
| 2653 | ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN); |
| 2654 | break; |
| 2655 | case IBMVFC_AE_LS_LINK_DEAD: |
| 2656 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 2657 | break; |
| 2658 | case IBMVFC_AE_LS_LINK_UP: |
| 2659 | case IBMVFC_AE_LS_LINK_BOUNCED: |
| 2660 | default: |
| 2661 | vhost->events_to_log |= IBMVFC_AE_LINKUP; |
| 2662 | vhost->delay_init = 1; |
| 2663 | __ibmvfc_reset_host(vhost); |
| 2664 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 2665 | } |
Brian King | 497f9c5 | 2009-05-28 16:17:30 -0500 | [diff] [blame] | 2666 | |
| 2667 | break; |
| 2668 | case IBMVFC_AE_LINK_UP: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2669 | vhost->events_to_log |= IBMVFC_AE_LINKUP; |
Brian King | d2131b3 | 2008-12-18 09:26:51 -0600 | [diff] [blame] | 2670 | vhost->delay_init = 1; |
| 2671 | __ibmvfc_reset_host(vhost); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2672 | break; |
| 2673 | case IBMVFC_AE_SCN_FABRIC: |
Brian King | d2131b3 | 2008-12-18 09:26:51 -0600 | [diff] [blame] | 2674 | case IBMVFC_AE_SCN_DOMAIN: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2675 | vhost->events_to_log |= IBMVFC_AE_RSCN; |
Brian King | 5cdf162 | 2012-08-24 16:51:01 -0500 | [diff] [blame] | 2676 | if (vhost->state < IBMVFC_HALTED) { |
| 2677 | vhost->delay_init = 1; |
| 2678 | __ibmvfc_reset_host(vhost); |
| 2679 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2680 | break; |
| 2681 | case IBMVFC_AE_SCN_NPORT: |
| 2682 | case IBMVFC_AE_SCN_GROUP: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2683 | vhost->events_to_log |= IBMVFC_AE_RSCN; |
Brian King | 6d29cc5 | 2009-05-28 16:17:33 -0500 | [diff] [blame] | 2684 | ibmvfc_reinit_host(vhost); |
| 2685 | break; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2686 | case IBMVFC_AE_ELS_LOGO: |
| 2687 | case IBMVFC_AE_ELS_PRLO: |
| 2688 | case IBMVFC_AE_ELS_PLOGI: |
Brian King | 6d29cc5 | 2009-05-28 16:17:33 -0500 | [diff] [blame] | 2689 | list_for_each_entry(tgt, &vhost->targets, queue) { |
| 2690 | if (!crq->scsi_id && !crq->wwpn && !crq->node_name) |
| 2691 | break; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2692 | if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id) |
Brian King | 6d29cc5 | 2009-05-28 16:17:33 -0500 | [diff] [blame] | 2693 | continue; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2694 | if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn) |
Brian King | 6d29cc5 | 2009-05-28 16:17:33 -0500 | [diff] [blame] | 2695 | continue; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2696 | if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name) |
Brian King | 6d29cc5 | 2009-05-28 16:17:33 -0500 | [diff] [blame] | 2697 | continue; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2698 | if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO) |
Brian King | 017b2ae | 2009-06-18 09:06:55 -0500 | [diff] [blame] | 2699 | tgt->logo_rcvd = 1; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2700 | if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) { |
Brian King | 017b2ae | 2009-06-18 09:06:55 -0500 | [diff] [blame] | 2701 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
| 2702 | ibmvfc_reinit_host(vhost); |
| 2703 | } |
Brian King | 6d29cc5 | 2009-05-28 16:17:33 -0500 | [diff] [blame] | 2704 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2705 | break; |
| 2706 | case IBMVFC_AE_LINK_DOWN: |
| 2707 | case IBMVFC_AE_ADAPTER_FAILED: |
| 2708 | ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN); |
| 2709 | break; |
| 2710 | case IBMVFC_AE_LINK_DEAD: |
| 2711 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 2712 | break; |
| 2713 | case IBMVFC_AE_HALT: |
| 2714 | ibmvfc_link_down(vhost, IBMVFC_HALTED); |
| 2715 | break; |
| 2716 | default: |
Stephen Rothwell | 775a42e | 2009-01-06 14:59:00 +0000 | [diff] [blame] | 2717 | dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2718 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 2719 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | /** |
| 2723 | * ibmvfc_handle_crq - Handles and frees received events in the CRQ |
| 2724 | * @crq: Command/Response queue |
| 2725 | * @vhost: ibmvfc host struct |
| 2726 | * |
| 2727 | **/ |
| 2728 | static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost) |
| 2729 | { |
| 2730 | long rc; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 2731 | struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2732 | |
| 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); |
| 2740 | if (rc == 0) |
Brian King | 861890c | 2009-10-19 15:07:49 -0500 | [diff] [blame] | 2741 | ibmvfc_init_host(vhost); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2742 | else |
| 2743 | dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc); |
| 2744 | break; |
| 2745 | case IBMVFC_CRQ_INIT_COMPLETE: |
| 2746 | dev_info(vhost->dev, "Partner initialization complete\n"); |
Brian King | 861890c | 2009-10-19 15:07:49 -0500 | [diff] [blame] | 2747 | ibmvfc_init_host(vhost); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2748 | break; |
| 2749 | default: |
| 2750 | dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format); |
| 2751 | } |
| 2752 | return; |
| 2753 | case IBMVFC_CRQ_XPORT_EVENT: |
| 2754 | vhost->state = IBMVFC_NO_CRQ; |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 2755 | vhost->logged_in = 0; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 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, "Re-enabling adapter\n"); |
| 2760 | vhost->client_migrated = 1; |
| 2761 | ibmvfc_purge_requests(vhost, DID_REQUEUE); |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 2762 | ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN); |
| 2763 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2764 | } else { |
| 2765 | dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2766 | ibmvfc_purge_requests(vhost, DID_ERROR); |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 2767 | ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN); |
| 2768 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2769 | } |
| 2770 | return; |
| 2771 | case IBMVFC_CRQ_CMD_RSP: |
| 2772 | break; |
| 2773 | default: |
| 2774 | dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid); |
| 2775 | return; |
| 2776 | } |
| 2777 | |
| 2778 | if (crq->format == IBMVFC_ASYNC_EVENT) |
| 2779 | return; |
| 2780 | |
| 2781 | /* The only kind of payload CRQs we should get are responses to |
| 2782 | * things we send. Make sure this response is to something we |
| 2783 | * actually sent |
| 2784 | */ |
| 2785 | if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) { |
Stephen Rothwell | 775a42e | 2009-01-06 14:59:00 +0000 | [diff] [blame] | 2786 | dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n", |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2787 | crq->ioba); |
| 2788 | return; |
| 2789 | } |
| 2790 | |
| 2791 | if (unlikely(atomic_read(&evt->free))) { |
Stephen Rothwell | 775a42e | 2009-01-06 14:59:00 +0000 | [diff] [blame] | 2792 | dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n", |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2793 | crq->ioba); |
| 2794 | return; |
| 2795 | } |
| 2796 | |
| 2797 | del_timer(&evt->timer); |
| 2798 | list_del(&evt->queue); |
| 2799 | ibmvfc_trc_end(evt); |
| 2800 | evt->done(evt); |
| 2801 | } |
| 2802 | |
| 2803 | /** |
| 2804 | * ibmvfc_scan_finished - Check if the device scan is done. |
| 2805 | * @shost: scsi host struct |
| 2806 | * @time: current elapsed time |
| 2807 | * |
| 2808 | * Returns: |
| 2809 | * 0 if scan is not done / 1 if scan is done |
| 2810 | **/ |
| 2811 | static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time) |
| 2812 | { |
| 2813 | unsigned long flags; |
| 2814 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2815 | int done = 0; |
| 2816 | |
| 2817 | spin_lock_irqsave(shost->host_lock, flags); |
| 2818 | if (time >= (init_timeout * HZ)) { |
| 2819 | dev_info(vhost->dev, "Scan taking longer than %d seconds, " |
| 2820 | "continuing initialization\n", init_timeout); |
| 2821 | done = 1; |
| 2822 | } |
| 2823 | |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 2824 | if (vhost->scan_complete) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2825 | done = 1; |
| 2826 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 2827 | return done; |
| 2828 | } |
| 2829 | |
| 2830 | /** |
| 2831 | * ibmvfc_slave_alloc - Setup the device's task set value |
| 2832 | * @sdev: struct scsi_device device to configure |
| 2833 | * |
| 2834 | * Set the device's task set value so that error handling works as |
| 2835 | * expected. |
| 2836 | * |
| 2837 | * Returns: |
| 2838 | * 0 on success / -ENXIO if device does not exist |
| 2839 | **/ |
| 2840 | static int ibmvfc_slave_alloc(struct scsi_device *sdev) |
| 2841 | { |
| 2842 | struct Scsi_Host *shost = sdev->host; |
| 2843 | struct fc_rport *rport = starget_to_rport(scsi_target(sdev)); |
| 2844 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2845 | unsigned long flags = 0; |
| 2846 | |
| 2847 | if (!rport || fc_remote_port_chkready(rport)) |
| 2848 | return -ENXIO; |
| 2849 | |
| 2850 | spin_lock_irqsave(shost->host_lock, flags); |
| 2851 | sdev->hostdata = (void *)(unsigned long)vhost->task_set++; |
| 2852 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 2853 | return 0; |
| 2854 | } |
| 2855 | |
| 2856 | /** |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 2857 | * ibmvfc_target_alloc - Setup the target's task set value |
| 2858 | * @starget: struct scsi_target |
| 2859 | * |
| 2860 | * Set the target's task set value so that error handling works as |
| 2861 | * expected. |
| 2862 | * |
| 2863 | * Returns: |
| 2864 | * 0 on success / -ENXIO if device does not exist |
| 2865 | **/ |
| 2866 | static int ibmvfc_target_alloc(struct scsi_target *starget) |
| 2867 | { |
| 2868 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 2869 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2870 | unsigned long flags = 0; |
| 2871 | |
| 2872 | spin_lock_irqsave(shost->host_lock, flags); |
| 2873 | starget->hostdata = (void *)(unsigned long)vhost->task_set++; |
| 2874 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 2875 | return 0; |
| 2876 | } |
| 2877 | |
| 2878 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2879 | * ibmvfc_slave_configure - Configure the device |
| 2880 | * @sdev: struct scsi_device device to configure |
| 2881 | * |
| 2882 | * Enable allow_restart for a device if it is a disk. Adjust the |
| 2883 | * queue_depth here also. |
| 2884 | * |
| 2885 | * Returns: |
| 2886 | * 0 |
| 2887 | **/ |
| 2888 | static int ibmvfc_slave_configure(struct scsi_device *sdev) |
| 2889 | { |
| 2890 | struct Scsi_Host *shost = sdev->host; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2891 | unsigned long flags = 0; |
| 2892 | |
| 2893 | spin_lock_irqsave(shost->host_lock, flags); |
| 2894 | if (sdev->type == TYPE_DISK) |
| 2895 | sdev->allow_restart = 1; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2896 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 2897 | return 0; |
| 2898 | } |
| 2899 | |
| 2900 | /** |
| 2901 | * ibmvfc_change_queue_depth - Change the device's queue depth |
| 2902 | * @sdev: scsi device struct |
| 2903 | * @qdepth: depth to set |
Mike Christie | e881a17 | 2009-10-15 17:46:39 -0700 | [diff] [blame] | 2904 | * @reason: calling context |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2905 | * |
| 2906 | * Return value: |
| 2907 | * actual depth set |
| 2908 | **/ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 2909 | static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2910 | { |
| 2911 | if (qdepth > IBMVFC_MAX_CMDS_PER_LUN) |
| 2912 | qdepth = IBMVFC_MAX_CMDS_PER_LUN; |
| 2913 | |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 2914 | return scsi_change_queue_depth(sdev, qdepth); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2915 | } |
| 2916 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2917 | static ssize_t ibmvfc_show_host_partition_name(struct device *dev, |
| 2918 | struct device_attribute *attr, char *buf) |
| 2919 | { |
| 2920 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2921 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2922 | |
| 2923 | return snprintf(buf, PAGE_SIZE, "%s\n", |
| 2924 | vhost->login_buf->resp.partition_name); |
| 2925 | } |
| 2926 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2927 | static ssize_t ibmvfc_show_host_device_name(struct device *dev, |
| 2928 | struct device_attribute *attr, char *buf) |
| 2929 | { |
| 2930 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2931 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2932 | |
| 2933 | return snprintf(buf, PAGE_SIZE, "%s\n", |
| 2934 | vhost->login_buf->resp.device_name); |
| 2935 | } |
| 2936 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2937 | static ssize_t ibmvfc_show_host_loc_code(struct device *dev, |
| 2938 | struct device_attribute *attr, char *buf) |
| 2939 | { |
| 2940 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2941 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2942 | |
| 2943 | return snprintf(buf, PAGE_SIZE, "%s\n", |
| 2944 | vhost->login_buf->resp.port_loc_code); |
| 2945 | } |
| 2946 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2947 | static ssize_t ibmvfc_show_host_drc_name(struct device *dev, |
| 2948 | struct device_attribute *attr, char *buf) |
| 2949 | { |
| 2950 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2951 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2952 | |
| 2953 | return snprintf(buf, PAGE_SIZE, "%s\n", |
| 2954 | vhost->login_buf->resp.drc_name); |
| 2955 | } |
| 2956 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2957 | static ssize_t ibmvfc_show_host_npiv_version(struct device *dev, |
| 2958 | struct device_attribute *attr, char *buf) |
| 2959 | { |
| 2960 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2961 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2962 | return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version); |
| 2963 | } |
| 2964 | |
Brian King | 497f9c5 | 2009-05-28 16:17:30 -0500 | [diff] [blame] | 2965 | static ssize_t ibmvfc_show_host_capabilities(struct device *dev, |
| 2966 | struct device_attribute *attr, char *buf) |
| 2967 | { |
| 2968 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2969 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2970 | return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities); |
| 2971 | } |
| 2972 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 2973 | /** |
| 2974 | * ibmvfc_show_log_level - Show the adapter's error logging level |
| 2975 | * @dev: class device struct |
| 2976 | * @buf: buffer |
| 2977 | * |
| 2978 | * Return value: |
| 2979 | * number of bytes printed to buffer |
| 2980 | **/ |
| 2981 | static ssize_t ibmvfc_show_log_level(struct device *dev, |
| 2982 | struct device_attribute *attr, char *buf) |
| 2983 | { |
| 2984 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2985 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 2986 | unsigned long flags = 0; |
| 2987 | int len; |
| 2988 | |
| 2989 | spin_lock_irqsave(shost->host_lock, flags); |
| 2990 | len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level); |
| 2991 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 2992 | return len; |
| 2993 | } |
| 2994 | |
| 2995 | /** |
| 2996 | * ibmvfc_store_log_level - Change the adapter's error logging level |
| 2997 | * @dev: class device struct |
| 2998 | * @buf: buffer |
| 2999 | * |
| 3000 | * Return value: |
| 3001 | * number of bytes printed to buffer |
| 3002 | **/ |
| 3003 | static ssize_t ibmvfc_store_log_level(struct device *dev, |
| 3004 | struct device_attribute *attr, |
| 3005 | const char *buf, size_t count) |
| 3006 | { |
| 3007 | struct Scsi_Host *shost = class_to_shost(dev); |
| 3008 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 3009 | unsigned long flags = 0; |
| 3010 | |
| 3011 | spin_lock_irqsave(shost->host_lock, flags); |
| 3012 | vhost->log_level = simple_strtoul(buf, NULL, 10); |
| 3013 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 3014 | return strlen(buf); |
| 3015 | } |
| 3016 | |
Brian King | 85e2399 | 2009-05-28 16:17:25 -0500 | [diff] [blame] | 3017 | static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL); |
| 3018 | static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL); |
| 3019 | static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL); |
| 3020 | static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL); |
| 3021 | static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL); |
Brian King | 497f9c5 | 2009-05-28 16:17:30 -0500 | [diff] [blame] | 3022 | static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL); |
Brian King | 85e2399 | 2009-05-28 16:17:25 -0500 | [diff] [blame] | 3023 | static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR, |
| 3024 | ibmvfc_show_log_level, ibmvfc_store_log_level); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3025 | |
| 3026 | #ifdef CONFIG_SCSI_IBMVFC_TRACE |
| 3027 | /** |
| 3028 | * ibmvfc_read_trace - Dump the adapter trace |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 3029 | * @filp: open sysfs file |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3030 | * @kobj: kobject struct |
| 3031 | * @bin_attr: bin_attribute struct |
| 3032 | * @buf: buffer |
| 3033 | * @off: offset |
| 3034 | * @count: buffer size |
| 3035 | * |
| 3036 | * Return value: |
| 3037 | * number of bytes printed to buffer |
| 3038 | **/ |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 3039 | static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3040 | struct bin_attribute *bin_attr, |
| 3041 | char *buf, loff_t off, size_t count) |
| 3042 | { |
| 3043 | struct device *dev = container_of(kobj, struct device, kobj); |
| 3044 | struct Scsi_Host *shost = class_to_shost(dev); |
| 3045 | struct ibmvfc_host *vhost = shost_priv(shost); |
| 3046 | unsigned long flags = 0; |
| 3047 | int size = IBMVFC_TRACE_SIZE; |
| 3048 | char *src = (char *)vhost->trace; |
| 3049 | |
| 3050 | if (off > size) |
| 3051 | return 0; |
| 3052 | if (off + count > size) { |
| 3053 | size -= off; |
| 3054 | count = size; |
| 3055 | } |
| 3056 | |
| 3057 | spin_lock_irqsave(shost->host_lock, flags); |
| 3058 | memcpy(buf, &src[off], count); |
| 3059 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 3060 | return count; |
| 3061 | } |
| 3062 | |
| 3063 | static struct bin_attribute ibmvfc_trace_attr = { |
| 3064 | .attr = { |
| 3065 | .name = "trace", |
| 3066 | .mode = S_IRUGO, |
| 3067 | }, |
| 3068 | .size = 0, |
| 3069 | .read = ibmvfc_read_trace, |
| 3070 | }; |
| 3071 | #endif |
| 3072 | |
| 3073 | static struct device_attribute *ibmvfc_attrs[] = { |
Brian King | 85e2399 | 2009-05-28 16:17:25 -0500 | [diff] [blame] | 3074 | &dev_attr_partition_name, |
| 3075 | &dev_attr_device_name, |
| 3076 | &dev_attr_port_loc_code, |
| 3077 | &dev_attr_drc_name, |
| 3078 | &dev_attr_npiv_version, |
Brian King | 497f9c5 | 2009-05-28 16:17:30 -0500 | [diff] [blame] | 3079 | &dev_attr_capabilities, |
Brian King | 85e2399 | 2009-05-28 16:17:25 -0500 | [diff] [blame] | 3080 | &dev_attr_log_level, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3081 | NULL |
| 3082 | }; |
| 3083 | |
| 3084 | static struct scsi_host_template driver_template = { |
| 3085 | .module = THIS_MODULE, |
| 3086 | .name = "IBM POWER Virtual FC Adapter", |
| 3087 | .proc_name = IBMVFC_NAME, |
| 3088 | .queuecommand = ibmvfc_queuecommand, |
Christoph Hellwig | b6a05c8 | 2017-01-30 13:18:58 +0100 | [diff] [blame] | 3089 | .eh_timed_out = fc_eh_timed_out, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3090 | .eh_abort_handler = ibmvfc_eh_abort_handler, |
| 3091 | .eh_device_reset_handler = ibmvfc_eh_device_reset_handler, |
| 3092 | .eh_target_reset_handler = ibmvfc_eh_target_reset_handler, |
| 3093 | .eh_host_reset_handler = ibmvfc_eh_host_reset_handler, |
| 3094 | .slave_alloc = ibmvfc_slave_alloc, |
| 3095 | .slave_configure = ibmvfc_slave_configure, |
Brian King | ad8dcff | 2008-10-29 08:46:41 -0500 | [diff] [blame] | 3096 | .target_alloc = ibmvfc_target_alloc, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3097 | .scan_finished = ibmvfc_scan_finished, |
| 3098 | .change_queue_depth = ibmvfc_change_queue_depth, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3099 | .cmd_per_lun = 16, |
| 3100 | .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT, |
| 3101 | .this_id = -1, |
| 3102 | .sg_tablesize = SG_ALL, |
| 3103 | .max_sectors = IBMVFC_MAX_SECTORS, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3104 | .shost_attrs = ibmvfc_attrs, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 3105 | .track_queue_depth = 1, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3106 | }; |
| 3107 | |
| 3108 | /** |
| 3109 | * ibmvfc_next_async_crq - Returns the next entry in async queue |
| 3110 | * @vhost: ibmvfc host struct |
| 3111 | * |
| 3112 | * Returns: |
| 3113 | * Pointer to next entry in queue / NULL if empty |
| 3114 | **/ |
| 3115 | static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost) |
| 3116 | { |
| 3117 | struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq; |
| 3118 | struct ibmvfc_async_crq *crq; |
| 3119 | |
| 3120 | crq = &async_crq->msgs[async_crq->cur]; |
| 3121 | if (crq->valid & 0x80) { |
| 3122 | if (++async_crq->cur == async_crq->size) |
| 3123 | async_crq->cur = 0; |
Brian King | f5832fa | 2010-04-20 14:21:33 -0500 | [diff] [blame] | 3124 | rmb(); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3125 | } else |
| 3126 | crq = NULL; |
| 3127 | |
| 3128 | return crq; |
| 3129 | } |
| 3130 | |
| 3131 | /** |
| 3132 | * ibmvfc_next_crq - Returns the next entry in message queue |
| 3133 | * @vhost: ibmvfc host struct |
| 3134 | * |
| 3135 | * Returns: |
| 3136 | * Pointer to next entry in queue / NULL if empty |
| 3137 | **/ |
| 3138 | static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost) |
| 3139 | { |
| 3140 | struct ibmvfc_crq_queue *queue = &vhost->crq; |
| 3141 | struct ibmvfc_crq *crq; |
| 3142 | |
| 3143 | crq = &queue->msgs[queue->cur]; |
| 3144 | if (crq->valid & 0x80) { |
| 3145 | if (++queue->cur == queue->size) |
| 3146 | queue->cur = 0; |
Brian King | f5832fa | 2010-04-20 14:21:33 -0500 | [diff] [blame] | 3147 | rmb(); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3148 | } else |
| 3149 | crq = NULL; |
| 3150 | |
| 3151 | return crq; |
| 3152 | } |
| 3153 | |
| 3154 | /** |
| 3155 | * ibmvfc_interrupt - Interrupt handler |
| 3156 | * @irq: number of irq to handle, not used |
| 3157 | * @dev_instance: ibmvfc_host that received interrupt |
| 3158 | * |
| 3159 | * Returns: |
| 3160 | * IRQ_HANDLED |
| 3161 | **/ |
| 3162 | static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance) |
| 3163 | { |
| 3164 | struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance; |
Brian King | 039a089 | 2009-03-20 15:44:35 -0500 | [diff] [blame] | 3165 | unsigned long flags; |
| 3166 | |
| 3167 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 3168 | vio_disable_interrupts(to_vio_dev(vhost->dev)); |
| 3169 | tasklet_schedule(&vhost->tasklet); |
| 3170 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 3171 | return IRQ_HANDLED; |
| 3172 | } |
| 3173 | |
| 3174 | /** |
| 3175 | * ibmvfc_tasklet - Interrupt handler tasklet |
| 3176 | * @data: ibmvfc host struct |
| 3177 | * |
| 3178 | * Returns: |
| 3179 | * Nothing |
| 3180 | **/ |
| 3181 | static void ibmvfc_tasklet(void *data) |
| 3182 | { |
| 3183 | struct ibmvfc_host *vhost = data; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3184 | struct vio_dev *vdev = to_vio_dev(vhost->dev); |
| 3185 | struct ibmvfc_crq *crq; |
| 3186 | struct ibmvfc_async_crq *async; |
| 3187 | unsigned long flags; |
| 3188 | int done = 0; |
| 3189 | |
| 3190 | spin_lock_irqsave(vhost->host->host_lock, flags); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3191 | while (!done) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3192 | /* Pull all the valid messages off the async CRQ */ |
| 3193 | while ((async = ibmvfc_next_async_crq(vhost)) != NULL) { |
| 3194 | ibmvfc_handle_async(async, vhost); |
| 3195 | async->valid = 0; |
Brian King | f5832fa | 2010-04-20 14:21:33 -0500 | [diff] [blame] | 3196 | wmb(); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3197 | } |
| 3198 | |
Brian King | f1d7fb7 | 2009-06-18 09:06:52 -0500 | [diff] [blame] | 3199 | /* Pull all the valid messages off the CRQ */ |
| 3200 | while ((crq = ibmvfc_next_crq(vhost)) != NULL) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3201 | ibmvfc_handle_crq(crq, vhost); |
| 3202 | crq->valid = 0; |
Brian King | f5832fa | 2010-04-20 14:21:33 -0500 | [diff] [blame] | 3203 | wmb(); |
Brian King | f1d7fb7 | 2009-06-18 09:06:52 -0500 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | vio_enable_interrupts(vdev); |
| 3207 | if ((async = ibmvfc_next_async_crq(vhost)) != NULL) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3208 | vio_disable_interrupts(vdev); |
| 3209 | ibmvfc_handle_async(async, vhost); |
Brian King | 4081b77 | 2008-11-14 13:33:50 -0600 | [diff] [blame] | 3210 | async->valid = 0; |
Brian King | f5832fa | 2010-04-20 14:21:33 -0500 | [diff] [blame] | 3211 | wmb(); |
Brian King | f1d7fb7 | 2009-06-18 09:06:52 -0500 | [diff] [blame] | 3212 | } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) { |
| 3213 | vio_disable_interrupts(vdev); |
| 3214 | ibmvfc_handle_crq(crq, vhost); |
| 3215 | crq->valid = 0; |
Brian King | f5832fa | 2010-04-20 14:21:33 -0500 | [diff] [blame] | 3216 | wmb(); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3217 | } else |
| 3218 | done = 1; |
| 3219 | } |
| 3220 | |
| 3221 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | /** |
| 3225 | * ibmvfc_init_tgt - Set the next init job step for the target |
| 3226 | * @tgt: ibmvfc target struct |
| 3227 | * @job_step: job step to perform |
| 3228 | * |
| 3229 | **/ |
| 3230 | static void ibmvfc_init_tgt(struct ibmvfc_target *tgt, |
| 3231 | void (*job_step) (struct ibmvfc_target *)) |
| 3232 | { |
| 3233 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT); |
| 3234 | tgt->job_step = job_step; |
| 3235 | wake_up(&tgt->vhost->work_wait_q); |
| 3236 | } |
| 3237 | |
| 3238 | /** |
| 3239 | * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization |
| 3240 | * @tgt: ibmvfc target struct |
| 3241 | * @job_step: initialization job step |
| 3242 | * |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3243 | * Returns: 1 if step will be retried / 0 if not |
| 3244 | * |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3245 | **/ |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3246 | static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3247 | void (*job_step) (struct ibmvfc_target *)) |
| 3248 | { |
Brian King | 1c41fa82 | 2008-12-03 11:02:54 -0600 | [diff] [blame] | 3249 | if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3250 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
| 3251 | wake_up(&tgt->vhost->work_wait_q); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3252 | return 0; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3253 | } else |
| 3254 | ibmvfc_init_tgt(tgt, job_step); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3255 | return 1; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3256 | } |
| 3257 | |
Brian King | a3b7aea | 2009-02-02 18:39:40 -0600 | [diff] [blame] | 3258 | /* Defined in FC-LS */ |
| 3259 | static const struct { |
| 3260 | int code; |
| 3261 | int retry; |
| 3262 | int logged_in; |
| 3263 | } prli_rsp [] = { |
| 3264 | { 0, 1, 0 }, |
| 3265 | { 1, 0, 1 }, |
| 3266 | { 2, 1, 0 }, |
| 3267 | { 3, 1, 0 }, |
| 3268 | { 4, 0, 0 }, |
| 3269 | { 5, 0, 0 }, |
| 3270 | { 6, 0, 1 }, |
| 3271 | { 7, 0, 0 }, |
| 3272 | { 8, 1, 0 }, |
| 3273 | }; |
| 3274 | |
| 3275 | /** |
| 3276 | * ibmvfc_get_prli_rsp - Find PRLI response index |
| 3277 | * @flags: PRLI response flags |
| 3278 | * |
| 3279 | **/ |
| 3280 | static int ibmvfc_get_prli_rsp(u16 flags) |
| 3281 | { |
| 3282 | int i; |
| 3283 | int code = (flags & 0x0f00) >> 8; |
| 3284 | |
| 3285 | for (i = 0; i < ARRAY_SIZE(prli_rsp); i++) |
| 3286 | if (prli_rsp[i].code == code) |
| 3287 | return i; |
| 3288 | |
| 3289 | return 0; |
| 3290 | } |
| 3291 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3292 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3293 | * ibmvfc_tgt_prli_done - Completion handler for Process Login |
| 3294 | * @evt: ibmvfc event struct |
| 3295 | * |
| 3296 | **/ |
| 3297 | static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt) |
| 3298 | { |
| 3299 | struct ibmvfc_target *tgt = evt->tgt; |
| 3300 | struct ibmvfc_host *vhost = evt->vhost; |
| 3301 | struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli; |
Brian King | a3b7aea | 2009-02-02 18:39:40 -0600 | [diff] [blame] | 3302 | struct ibmvfc_prli_svc_parms *parms = &rsp->parms; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3303 | u32 status = be16_to_cpu(rsp->common.status); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3304 | int index, level = IBMVFC_DEFAULT_LOG_LEVEL; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3305 | |
| 3306 | vhost->discovery_threads--; |
| 3307 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
| 3308 | switch (status) { |
| 3309 | case IBMVFC_MAD_SUCCESS: |
Brian King | a3b7aea | 2009-02-02 18:39:40 -0600 | [diff] [blame] | 3310 | tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n", |
| 3311 | parms->type, parms->flags, parms->service_parms); |
| 3312 | |
| 3313 | if (parms->type == IBMVFC_SCSI_FCP_TYPE) { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3314 | index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags)); |
Brian King | a3b7aea | 2009-02-02 18:39:40 -0600 | [diff] [blame] | 3315 | if (prli_rsp[index].logged_in) { |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3316 | if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) { |
Brian King | a3b7aea | 2009-02-02 18:39:40 -0600 | [diff] [blame] | 3317 | tgt->need_login = 0; |
| 3318 | tgt->ids.roles = 0; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3319 | if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC) |
Brian King | a3b7aea | 2009-02-02 18:39:40 -0600 | [diff] [blame] | 3320 | tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3321 | if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC) |
Brian King | a3b7aea | 2009-02-02 18:39:40 -0600 | [diff] [blame] | 3322 | tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR; |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 3323 | tgt->add_rport = 1; |
Brian King | a3b7aea | 2009-02-02 18:39:40 -0600 | [diff] [blame] | 3324 | } else |
| 3325 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
| 3326 | } else if (prli_rsp[index].retry) |
| 3327 | ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli); |
| 3328 | else |
| 3329 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
| 3330 | } else |
| 3331 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3332 | break; |
| 3333 | case IBMVFC_MAD_DRIVER_FAILED: |
| 3334 | break; |
| 3335 | case IBMVFC_MAD_CRQ_ERROR: |
| 3336 | ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli); |
| 3337 | break; |
| 3338 | case IBMVFC_MAD_FAILED: |
| 3339 | default: |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3340 | if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) && |
| 3341 | be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED) |
Brian King | 017b2ae | 2009-06-18 09:06:55 -0500 | [diff] [blame] | 3342 | level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi); |
| 3343 | else if (tgt->logo_rcvd) |
| 3344 | level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3345 | else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error))) |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3346 | level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli); |
Brian King | 10e7949 | 2008-10-29 08:46:45 -0500 | [diff] [blame] | 3347 | else |
| 3348 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3349 | |
| 3350 | tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n", |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3351 | ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3352 | rsp->status, rsp->error, status); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3353 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 3354 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3355 | |
| 3356 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3357 | ibmvfc_free_event(evt); |
| 3358 | wake_up(&vhost->work_wait_q); |
| 3359 | } |
| 3360 | |
| 3361 | /** |
| 3362 | * ibmvfc_tgt_send_prli - Send a process login |
| 3363 | * @tgt: ibmvfc target struct |
| 3364 | * |
| 3365 | **/ |
| 3366 | static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt) |
| 3367 | { |
| 3368 | struct ibmvfc_process_login *prli; |
| 3369 | struct ibmvfc_host *vhost = tgt->vhost; |
| 3370 | struct ibmvfc_event *evt; |
| 3371 | |
| 3372 | if (vhost->discovery_threads >= disc_threads) |
| 3373 | return; |
| 3374 | |
| 3375 | kref_get(&tgt->kref); |
| 3376 | evt = ibmvfc_get_event(vhost); |
| 3377 | vhost->discovery_threads++; |
| 3378 | ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT); |
| 3379 | evt->tgt = tgt; |
| 3380 | prli = &evt->iu.prli; |
| 3381 | memset(prli, 0, sizeof(*prli)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3382 | prli->common.version = cpu_to_be32(1); |
| 3383 | prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN); |
| 3384 | prli->common.length = cpu_to_be16(sizeof(*prli)); |
| 3385 | prli->scsi_id = cpu_to_be64(tgt->scsi_id); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3386 | |
| 3387 | prli->parms.type = IBMVFC_SCSI_FCP_TYPE; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3388 | prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR); |
| 3389 | prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC); |
Tyrel Datwyler | bb5a505 | 2016-08-03 16:36:52 -0500 | [diff] [blame] | 3390 | prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3391 | |
Tyrel Datwyler | a6104b1 | 2016-08-03 16:36:53 -0500 | [diff] [blame] | 3392 | if (cls3_error) |
| 3393 | prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY); |
| 3394 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3395 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT); |
| 3396 | if (ibmvfc_send_event(evt, vhost, default_timeout)) { |
| 3397 | vhost->discovery_threads--; |
| 3398 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
| 3399 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3400 | } else |
| 3401 | tgt_dbg(tgt, "Sent process login\n"); |
| 3402 | } |
| 3403 | |
| 3404 | /** |
| 3405 | * ibmvfc_tgt_plogi_done - Completion handler for Port Login |
| 3406 | * @evt: ibmvfc event struct |
| 3407 | * |
| 3408 | **/ |
| 3409 | static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt) |
| 3410 | { |
| 3411 | struct ibmvfc_target *tgt = evt->tgt; |
| 3412 | struct ibmvfc_host *vhost = evt->vhost; |
| 3413 | struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3414 | u32 status = be16_to_cpu(rsp->common.status); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3415 | int level = IBMVFC_DEFAULT_LOG_LEVEL; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3416 | |
| 3417 | vhost->discovery_threads--; |
| 3418 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
| 3419 | switch (status) { |
| 3420 | case IBMVFC_MAD_SUCCESS: |
| 3421 | tgt_dbg(tgt, "Port Login succeeded\n"); |
| 3422 | if (tgt->ids.port_name && |
| 3423 | tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) { |
| 3424 | vhost->reinit = 1; |
| 3425 | tgt_dbg(tgt, "Port re-init required\n"); |
| 3426 | break; |
| 3427 | } |
| 3428 | tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name); |
| 3429 | tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name); |
| 3430 | tgt->ids.port_id = tgt->scsi_id; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3431 | memcpy(&tgt->service_parms, &rsp->service_parms, |
| 3432 | sizeof(tgt->service_parms)); |
| 3433 | memcpy(&tgt->service_parms_change, &rsp->service_parms_change, |
| 3434 | sizeof(tgt->service_parms_change)); |
| 3435 | ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli); |
| 3436 | break; |
| 3437 | case IBMVFC_MAD_DRIVER_FAILED: |
| 3438 | break; |
| 3439 | case IBMVFC_MAD_CRQ_ERROR: |
| 3440 | ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi); |
| 3441 | break; |
| 3442 | case IBMVFC_MAD_FAILED: |
| 3443 | default: |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3444 | if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error))) |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3445 | level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi); |
| 3446 | else |
| 3447 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
| 3448 | |
| 3449 | tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n", |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3450 | ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), rsp->status, rsp->error, |
| 3451 | ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), rsp->fc_type, |
| 3452 | ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), rsp->fc_explain, status); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3453 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 3454 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3455 | |
| 3456 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3457 | ibmvfc_free_event(evt); |
| 3458 | wake_up(&vhost->work_wait_q); |
| 3459 | } |
| 3460 | |
| 3461 | /** |
| 3462 | * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target |
| 3463 | * @tgt: ibmvfc target struct |
| 3464 | * |
| 3465 | **/ |
| 3466 | static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt) |
| 3467 | { |
| 3468 | struct ibmvfc_port_login *plogi; |
| 3469 | struct ibmvfc_host *vhost = tgt->vhost; |
| 3470 | struct ibmvfc_event *evt; |
| 3471 | |
| 3472 | if (vhost->discovery_threads >= disc_threads) |
| 3473 | return; |
| 3474 | |
| 3475 | kref_get(&tgt->kref); |
Brian King | 017b2ae | 2009-06-18 09:06:55 -0500 | [diff] [blame] | 3476 | tgt->logo_rcvd = 0; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3477 | evt = ibmvfc_get_event(vhost); |
| 3478 | vhost->discovery_threads++; |
| 3479 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT); |
| 3480 | ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT); |
| 3481 | evt->tgt = tgt; |
| 3482 | plogi = &evt->iu.plogi; |
| 3483 | memset(plogi, 0, sizeof(*plogi)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3484 | plogi->common.version = cpu_to_be32(1); |
| 3485 | plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN); |
| 3486 | plogi->common.length = cpu_to_be16(sizeof(*plogi)); |
| 3487 | plogi->scsi_id = cpu_to_be64(tgt->scsi_id); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3488 | |
| 3489 | if (ibmvfc_send_event(evt, vhost, default_timeout)) { |
| 3490 | vhost->discovery_threads--; |
| 3491 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
| 3492 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3493 | } else |
| 3494 | tgt_dbg(tgt, "Sent port login\n"); |
| 3495 | } |
| 3496 | |
| 3497 | /** |
| 3498 | * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD |
| 3499 | * @evt: ibmvfc event struct |
| 3500 | * |
| 3501 | **/ |
| 3502 | static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt) |
| 3503 | { |
| 3504 | struct ibmvfc_target *tgt = evt->tgt; |
| 3505 | struct ibmvfc_host *vhost = evt->vhost; |
| 3506 | struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3507 | u32 status = be16_to_cpu(rsp->common.status); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3508 | |
| 3509 | vhost->discovery_threads--; |
| 3510 | ibmvfc_free_event(evt); |
| 3511 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
| 3512 | |
| 3513 | switch (status) { |
| 3514 | case IBMVFC_MAD_SUCCESS: |
| 3515 | tgt_dbg(tgt, "Implicit Logout succeeded\n"); |
| 3516 | break; |
| 3517 | case IBMVFC_MAD_DRIVER_FAILED: |
| 3518 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3519 | wake_up(&vhost->work_wait_q); |
| 3520 | return; |
| 3521 | case IBMVFC_MAD_FAILED: |
| 3522 | default: |
| 3523 | tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status); |
| 3524 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 3525 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3526 | |
| 3527 | if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT) |
| 3528 | ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi); |
| 3529 | else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS && |
| 3530 | tgt->scsi_id != tgt->new_scsi_id) |
| 3531 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
| 3532 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3533 | wake_up(&vhost->work_wait_q); |
| 3534 | } |
| 3535 | |
| 3536 | /** |
| 3537 | * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target |
| 3538 | * @tgt: ibmvfc target struct |
| 3539 | * |
| 3540 | **/ |
| 3541 | static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt) |
| 3542 | { |
| 3543 | struct ibmvfc_implicit_logout *mad; |
| 3544 | struct ibmvfc_host *vhost = tgt->vhost; |
| 3545 | struct ibmvfc_event *evt; |
| 3546 | |
| 3547 | if (vhost->discovery_threads >= disc_threads) |
| 3548 | return; |
| 3549 | |
| 3550 | kref_get(&tgt->kref); |
| 3551 | evt = ibmvfc_get_event(vhost); |
| 3552 | vhost->discovery_threads++; |
| 3553 | ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT); |
| 3554 | evt->tgt = tgt; |
| 3555 | mad = &evt->iu.implicit_logout; |
| 3556 | memset(mad, 0, sizeof(*mad)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3557 | mad->common.version = cpu_to_be32(1); |
| 3558 | mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT); |
| 3559 | mad->common.length = cpu_to_be16(sizeof(*mad)); |
| 3560 | mad->old_scsi_id = cpu_to_be64(tgt->scsi_id); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3561 | |
| 3562 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT); |
| 3563 | if (ibmvfc_send_event(evt, vhost, default_timeout)) { |
| 3564 | vhost->discovery_threads--; |
| 3565 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
| 3566 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3567 | } else |
| 3568 | tgt_dbg(tgt, "Sent Implicit Logout\n"); |
| 3569 | } |
| 3570 | |
| 3571 | /** |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3572 | * ibmvfc_adisc_needs_plogi - Does device need PLOGI? |
| 3573 | * @mad: ibmvfc passthru mad struct |
| 3574 | * @tgt: ibmvfc target struct |
| 3575 | * |
| 3576 | * Returns: |
| 3577 | * 1 if PLOGI needed / 0 if PLOGI not needed |
| 3578 | **/ |
| 3579 | static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad, |
| 3580 | struct ibmvfc_target *tgt) |
| 3581 | { |
Brian King | 09dd15e | 2018-03-14 17:13:39 -0500 | [diff] [blame] | 3582 | if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name) |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3583 | return 1; |
Brian King | 09dd15e | 2018-03-14 17:13:39 -0500 | [diff] [blame] | 3584 | if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name) |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3585 | return 1; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3586 | if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id) |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3587 | return 1; |
| 3588 | return 0; |
| 3589 | } |
| 3590 | |
| 3591 | /** |
| 3592 | * ibmvfc_tgt_adisc_done - Completion handler for ADISC |
| 3593 | * @evt: ibmvfc event struct |
| 3594 | * |
| 3595 | **/ |
| 3596 | static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt) |
| 3597 | { |
| 3598 | struct ibmvfc_target *tgt = evt->tgt; |
| 3599 | struct ibmvfc_host *vhost = evt->vhost; |
| 3600 | struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3601 | u32 status = be16_to_cpu(mad->common.status); |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3602 | u8 fc_reason, fc_explain; |
| 3603 | |
| 3604 | vhost->discovery_threads--; |
| 3605 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3606 | del_timer(&tgt->timer); |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3607 | |
| 3608 | switch (status) { |
| 3609 | case IBMVFC_MAD_SUCCESS: |
| 3610 | tgt_dbg(tgt, "ADISC succeeded\n"); |
| 3611 | if (ibmvfc_adisc_needs_plogi(mad, tgt)) |
Brian King | 5e47167 | 2009-05-28 16:17:32 -0500 | [diff] [blame] | 3612 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3613 | break; |
| 3614 | case IBMVFC_MAD_DRIVER_FAILED: |
| 3615 | break; |
| 3616 | case IBMVFC_MAD_FAILED: |
| 3617 | default: |
Brian King | 5e47167 | 2009-05-28 16:17:32 -0500 | [diff] [blame] | 3618 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3619 | fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16; |
| 3620 | fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8; |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3621 | tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n", |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3622 | ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)), |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3623 | mad->iu.status, mad->iu.error, |
| 3624 | ibmvfc_get_fc_type(fc_reason), fc_reason, |
| 3625 | ibmvfc_get_ls_explain(fc_explain), fc_explain, status); |
| 3626 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 3627 | } |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3628 | |
| 3629 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3630 | ibmvfc_free_event(evt); |
| 3631 | wake_up(&vhost->work_wait_q); |
| 3632 | } |
| 3633 | |
| 3634 | /** |
| 3635 | * ibmvfc_init_passthru - Initialize an event struct for FC passthru |
| 3636 | * @evt: ibmvfc event struct |
| 3637 | * |
| 3638 | **/ |
| 3639 | static void ibmvfc_init_passthru(struct ibmvfc_event *evt) |
| 3640 | { |
| 3641 | struct ibmvfc_passthru_mad *mad = &evt->iu.passthru; |
| 3642 | |
| 3643 | memset(mad, 0, sizeof(*mad)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3644 | mad->common.version = cpu_to_be32(1); |
| 3645 | mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU); |
| 3646 | mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu)); |
| 3647 | mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) + |
| 3648 | offsetof(struct ibmvfc_passthru_mad, iu)); |
| 3649 | mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu)); |
| 3650 | mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload)); |
| 3651 | mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response)); |
| 3652 | mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) + |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3653 | offsetof(struct ibmvfc_passthru_mad, fc_iu) + |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3654 | offsetof(struct ibmvfc_passthru_fc_iu, payload)); |
| 3655 | mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload)); |
| 3656 | mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) + |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3657 | offsetof(struct ibmvfc_passthru_mad, fc_iu) + |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3658 | offsetof(struct ibmvfc_passthru_fc_iu, response)); |
| 3659 | mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response)); |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | /** |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3663 | * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC |
| 3664 | * @evt: ibmvfc event struct |
| 3665 | * |
| 3666 | * Just cleanup this event struct. Everything else is handled by |
| 3667 | * the ADISC completion handler. If the ADISC never actually comes |
| 3668 | * back, we still have the timer running on the ADISC event struct |
| 3669 | * which will fire and cause the CRQ to get reset. |
| 3670 | * |
| 3671 | **/ |
| 3672 | static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt) |
| 3673 | { |
| 3674 | struct ibmvfc_host *vhost = evt->vhost; |
| 3675 | struct ibmvfc_target *tgt = evt->tgt; |
| 3676 | |
| 3677 | tgt_dbg(tgt, "ADISC cancel complete\n"); |
| 3678 | vhost->abort_threads--; |
| 3679 | ibmvfc_free_event(evt); |
| 3680 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3681 | wake_up(&vhost->work_wait_q); |
| 3682 | } |
| 3683 | |
| 3684 | /** |
| 3685 | * ibmvfc_adisc_timeout - Handle an ADISC timeout |
| 3686 | * @tgt: ibmvfc target struct |
| 3687 | * |
| 3688 | * If an ADISC times out, send a cancel. If the cancel times |
| 3689 | * out, reset the CRQ. When the ADISC comes back as cancelled, |
| 3690 | * log back into the target. |
| 3691 | **/ |
Kees Cook | 9a5d04f | 2017-10-09 20:54:48 -0700 | [diff] [blame] | 3692 | static void ibmvfc_adisc_timeout(struct timer_list *t) |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3693 | { |
Kees Cook | 9a5d04f | 2017-10-09 20:54:48 -0700 | [diff] [blame] | 3694 | struct ibmvfc_target *tgt = from_timer(tgt, t, timer); |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3695 | struct ibmvfc_host *vhost = tgt->vhost; |
| 3696 | struct ibmvfc_event *evt; |
| 3697 | struct ibmvfc_tmf *tmf; |
| 3698 | unsigned long flags; |
| 3699 | int rc; |
| 3700 | |
| 3701 | tgt_dbg(tgt, "ADISC timeout\n"); |
| 3702 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 3703 | if (vhost->abort_threads >= disc_threads || |
| 3704 | tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT || |
| 3705 | vhost->state != IBMVFC_INITIALIZING || |
| 3706 | vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) { |
| 3707 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 3708 | return; |
| 3709 | } |
| 3710 | |
| 3711 | vhost->abort_threads++; |
| 3712 | kref_get(&tgt->kref); |
| 3713 | evt = ibmvfc_get_event(vhost); |
| 3714 | ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT); |
| 3715 | |
| 3716 | evt->tgt = tgt; |
| 3717 | tmf = &evt->iu.tmf; |
| 3718 | memset(tmf, 0, sizeof(*tmf)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3719 | tmf->common.version = cpu_to_be32(1); |
| 3720 | tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD); |
| 3721 | tmf->common.length = cpu_to_be16(sizeof(*tmf)); |
| 3722 | tmf->scsi_id = cpu_to_be64(tgt->scsi_id); |
| 3723 | tmf->cancel_key = cpu_to_be32(tgt->cancel_key); |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3724 | |
| 3725 | rc = ibmvfc_send_event(evt, vhost, default_timeout); |
| 3726 | |
| 3727 | if (rc) { |
| 3728 | tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc); |
| 3729 | vhost->abort_threads--; |
| 3730 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3731 | __ibmvfc_reset_host(vhost); |
| 3732 | } else |
| 3733 | tgt_dbg(tgt, "Attempting to cancel ADISC\n"); |
| 3734 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 3735 | } |
| 3736 | |
| 3737 | /** |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3738 | * ibmvfc_tgt_adisc - Initiate an ADISC for specified target |
| 3739 | * @tgt: ibmvfc target struct |
| 3740 | * |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3741 | * When sending an ADISC we end up with two timers running. The |
| 3742 | * first timer is the timer in the ibmvfc target struct. If this |
| 3743 | * fires, we send a cancel to the target. The second timer is the |
| 3744 | * timer on the ibmvfc event for the ADISC, which is longer. If that |
| 3745 | * fires, it means the ADISC timed out and our attempt to cancel it |
| 3746 | * also failed, so we need to reset the CRQ. |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3747 | **/ |
| 3748 | static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt) |
| 3749 | { |
| 3750 | struct ibmvfc_passthru_mad *mad; |
| 3751 | struct ibmvfc_host *vhost = tgt->vhost; |
| 3752 | struct ibmvfc_event *evt; |
| 3753 | |
| 3754 | if (vhost->discovery_threads >= disc_threads) |
| 3755 | return; |
| 3756 | |
| 3757 | kref_get(&tgt->kref); |
| 3758 | evt = ibmvfc_get_event(vhost); |
| 3759 | vhost->discovery_threads++; |
| 3760 | ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT); |
| 3761 | evt->tgt = tgt; |
| 3762 | |
| 3763 | ibmvfc_init_passthru(evt); |
| 3764 | mad = &evt->iu.passthru; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3765 | mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS); |
| 3766 | mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id); |
| 3767 | mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key); |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3768 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3769 | mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC); |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3770 | memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name, |
| 3771 | sizeof(vhost->login_buf->resp.port_name)); |
| 3772 | memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name, |
| 3773 | sizeof(vhost->login_buf->resp.node_name)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3774 | mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff); |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3775 | |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3776 | if (timer_pending(&tgt->timer)) |
| 3777 | mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ)); |
| 3778 | else { |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3779 | tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ); |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3780 | add_timer(&tgt->timer); |
| 3781 | } |
| 3782 | |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3783 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT); |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3784 | if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) { |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3785 | vhost->discovery_threads--; |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3786 | del_timer(&tgt->timer); |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3787 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
| 3788 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3789 | } else |
| 3790 | tgt_dbg(tgt, "Sent ADISC\n"); |
| 3791 | } |
| 3792 | |
| 3793 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3794 | * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD |
| 3795 | * @evt: ibmvfc event struct |
| 3796 | * |
| 3797 | **/ |
| 3798 | static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt) |
| 3799 | { |
| 3800 | struct ibmvfc_target *tgt = evt->tgt; |
| 3801 | struct ibmvfc_host *vhost = evt->vhost; |
| 3802 | struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3803 | u32 status = be16_to_cpu(rsp->common.status); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3804 | int level = IBMVFC_DEFAULT_LOG_LEVEL; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3805 | |
| 3806 | vhost->discovery_threads--; |
| 3807 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
| 3808 | switch (status) { |
| 3809 | case IBMVFC_MAD_SUCCESS: |
| 3810 | tgt_dbg(tgt, "Query Target succeeded\n"); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3811 | tgt->new_scsi_id = be64_to_cpu(rsp->scsi_id); |
| 3812 | if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3813 | ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout); |
Brian King | 989b854 | 2008-07-22 08:31:47 -0500 | [diff] [blame] | 3814 | else |
| 3815 | ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3816 | break; |
| 3817 | case IBMVFC_MAD_DRIVER_FAILED: |
| 3818 | break; |
| 3819 | case IBMVFC_MAD_CRQ_ERROR: |
| 3820 | ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target); |
| 3821 | break; |
| 3822 | case IBMVFC_MAD_FAILED: |
| 3823 | default: |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3824 | if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED && |
| 3825 | be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ && |
| 3826 | be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3827 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3828 | else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error))) |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3829 | level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target); |
Brian King | 10e7949 | 2008-10-29 08:46:45 -0500 | [diff] [blame] | 3830 | else |
| 3831 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3832 | |
| 3833 | tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n", |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3834 | ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), |
| 3835 | rsp->status, rsp->error, ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), |
| 3836 | rsp->fc_type, ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), |
| 3837 | rsp->fc_explain, status); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3838 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 3839 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3840 | |
| 3841 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3842 | ibmvfc_free_event(evt); |
| 3843 | wake_up(&vhost->work_wait_q); |
| 3844 | } |
| 3845 | |
| 3846 | /** |
| 3847 | * ibmvfc_tgt_query_target - Initiate a Query Target for specified target |
| 3848 | * @tgt: ibmvfc target struct |
| 3849 | * |
| 3850 | **/ |
| 3851 | static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt) |
| 3852 | { |
| 3853 | struct ibmvfc_query_tgt *query_tgt; |
| 3854 | struct ibmvfc_host *vhost = tgt->vhost; |
| 3855 | struct ibmvfc_event *evt; |
| 3856 | |
| 3857 | if (vhost->discovery_threads >= disc_threads) |
| 3858 | return; |
| 3859 | |
| 3860 | kref_get(&tgt->kref); |
| 3861 | evt = ibmvfc_get_event(vhost); |
| 3862 | vhost->discovery_threads++; |
| 3863 | evt->tgt = tgt; |
| 3864 | ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT); |
| 3865 | query_tgt = &evt->iu.query_tgt; |
| 3866 | memset(query_tgt, 0, sizeof(*query_tgt)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3867 | query_tgt->common.version = cpu_to_be32(1); |
| 3868 | query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET); |
| 3869 | query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt)); |
| 3870 | query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3871 | |
| 3872 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT); |
| 3873 | if (ibmvfc_send_event(evt, vhost, default_timeout)) { |
| 3874 | vhost->discovery_threads--; |
| 3875 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE); |
| 3876 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 3877 | } else |
| 3878 | tgt_dbg(tgt, "Sent Query Target\n"); |
| 3879 | } |
| 3880 | |
| 3881 | /** |
| 3882 | * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target |
| 3883 | * @vhost: ibmvfc host struct |
| 3884 | * @scsi_id: SCSI ID to allocate target for |
| 3885 | * |
| 3886 | * Returns: |
| 3887 | * 0 on success / other on failure |
| 3888 | **/ |
| 3889 | static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id) |
| 3890 | { |
| 3891 | struct ibmvfc_target *tgt; |
| 3892 | unsigned long flags; |
| 3893 | |
| 3894 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 3895 | list_for_each_entry(tgt, &vhost->targets, queue) { |
| 3896 | if (tgt->scsi_id == scsi_id) { |
| 3897 | if (tgt->need_login) |
| 3898 | ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout); |
| 3899 | goto unlock_out; |
| 3900 | } |
| 3901 | } |
| 3902 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 3903 | |
Brian King | 7270b9b | 2009-05-28 16:17:24 -0500 | [diff] [blame] | 3904 | tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO); |
Brian King | 0883e3b | 2009-02-04 16:13:12 -0600 | [diff] [blame] | 3905 | memset(tgt, 0, sizeof(*tgt)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3906 | tgt->scsi_id = scsi_id; |
| 3907 | tgt->new_scsi_id = scsi_id; |
| 3908 | tgt->vhost = vhost; |
| 3909 | tgt->need_login = 1; |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 3910 | tgt->cancel_key = vhost->task_set++; |
Kees Cook | 9a5d04f | 2017-10-09 20:54:48 -0700 | [diff] [blame] | 3911 | timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3912 | kref_init(&tgt->kref); |
| 3913 | ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout); |
| 3914 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 3915 | list_add_tail(&tgt->queue, &vhost->targets); |
| 3916 | |
| 3917 | unlock_out: |
| 3918 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 3919 | return 0; |
| 3920 | } |
| 3921 | |
| 3922 | /** |
| 3923 | * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets |
| 3924 | * @vhost: ibmvfc host struct |
| 3925 | * |
| 3926 | * Returns: |
| 3927 | * 0 on success / other on failure |
| 3928 | **/ |
| 3929 | static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost) |
| 3930 | { |
| 3931 | int i, rc; |
| 3932 | |
| 3933 | for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++) |
| 3934 | rc = ibmvfc_alloc_target(vhost, |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3935 | be32_to_cpu(vhost->disc_buf->scsi_id[i]) & |
| 3936 | IBMVFC_DISC_TGT_SCSI_ID_MASK); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3937 | |
| 3938 | return rc; |
| 3939 | } |
| 3940 | |
| 3941 | /** |
| 3942 | * ibmvfc_discover_targets_done - Completion handler for discover targets MAD |
| 3943 | * @evt: ibmvfc event struct |
| 3944 | * |
| 3945 | **/ |
| 3946 | static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt) |
| 3947 | { |
| 3948 | struct ibmvfc_host *vhost = evt->vhost; |
| 3949 | struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3950 | u32 mad_status = be16_to_cpu(rsp->common.status); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3951 | int level = IBMVFC_DEFAULT_LOG_LEVEL; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3952 | |
| 3953 | switch (mad_status) { |
| 3954 | case IBMVFC_MAD_SUCCESS: |
| 3955 | ibmvfc_dbg(vhost, "Discover Targets succeeded\n"); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3956 | vhost->num_targets = be32_to_cpu(rsp->num_written); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3957 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS); |
| 3958 | break; |
| 3959 | case IBMVFC_MAD_FAILED: |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 3960 | level += ibmvfc_retry_host_init(vhost); |
| 3961 | ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n", |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3962 | ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), |
| 3963 | rsp->status, rsp->error); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3964 | break; |
| 3965 | case IBMVFC_MAD_DRIVER_FAILED: |
| 3966 | break; |
| 3967 | default: |
| 3968 | dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status); |
| 3969 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 3970 | break; |
| 3971 | } |
| 3972 | |
| 3973 | ibmvfc_free_event(evt); |
| 3974 | wake_up(&vhost->work_wait_q); |
| 3975 | } |
| 3976 | |
| 3977 | /** |
| 3978 | * ibmvfc_discover_targets - Send Discover Targets MAD |
| 3979 | * @vhost: ibmvfc host struct |
| 3980 | * |
| 3981 | **/ |
| 3982 | static void ibmvfc_discover_targets(struct ibmvfc_host *vhost) |
| 3983 | { |
| 3984 | struct ibmvfc_discover_targets *mad; |
| 3985 | struct ibmvfc_event *evt = ibmvfc_get_event(vhost); |
| 3986 | |
| 3987 | ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT); |
| 3988 | mad = &evt->iu.discover_targets; |
| 3989 | memset(mad, 0, sizeof(*mad)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 3990 | mad->common.version = cpu_to_be32(1); |
| 3991 | mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS); |
| 3992 | mad->common.length = cpu_to_be16(sizeof(*mad)); |
| 3993 | mad->bufflen = cpu_to_be32(vhost->disc_buf_sz); |
| 3994 | mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma); |
| 3995 | mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 3996 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT); |
| 3997 | |
| 3998 | if (!ibmvfc_send_event(evt, vhost, default_timeout)) |
| 3999 | ibmvfc_dbg(vhost, "Sent discover targets\n"); |
| 4000 | else |
| 4001 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 4002 | } |
| 4003 | |
| 4004 | /** |
| 4005 | * ibmvfc_npiv_login_done - Completion handler for NPIV Login |
| 4006 | * @evt: ibmvfc event struct |
| 4007 | * |
| 4008 | **/ |
| 4009 | static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt) |
| 4010 | { |
| 4011 | struct ibmvfc_host *vhost = evt->vhost; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4012 | u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4013 | struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp; |
| 4014 | unsigned int npiv_max_sectors; |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 4015 | int level = IBMVFC_DEFAULT_LOG_LEVEL; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4016 | |
| 4017 | switch (mad_status) { |
| 4018 | case IBMVFC_MAD_SUCCESS: |
| 4019 | ibmvfc_free_event(evt); |
| 4020 | break; |
| 4021 | case IBMVFC_MAD_FAILED: |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4022 | if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error))) |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 4023 | level += ibmvfc_retry_host_init(vhost); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4024 | else |
| 4025 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
Brian King | 7d0e462 | 2009-05-28 16:17:26 -0500 | [diff] [blame] | 4026 | ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n", |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4027 | ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), |
| 4028 | rsp->status, rsp->error); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4029 | ibmvfc_free_event(evt); |
| 4030 | return; |
| 4031 | case IBMVFC_MAD_CRQ_ERROR: |
| 4032 | ibmvfc_retry_host_init(vhost); |
| 4033 | case IBMVFC_MAD_DRIVER_FAILED: |
| 4034 | ibmvfc_free_event(evt); |
| 4035 | return; |
| 4036 | default: |
| 4037 | dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status); |
| 4038 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 4039 | ibmvfc_free_event(evt); |
| 4040 | return; |
| 4041 | } |
| 4042 | |
| 4043 | vhost->client_migrated = 0; |
| 4044 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4045 | if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4046 | dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n", |
| 4047 | rsp->flags); |
| 4048 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 4049 | wake_up(&vhost->work_wait_q); |
| 4050 | return; |
| 4051 | } |
| 4052 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4053 | if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4054 | dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n", |
| 4055 | rsp->max_cmds); |
| 4056 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 4057 | wake_up(&vhost->work_wait_q); |
| 4058 | return; |
| 4059 | } |
| 4060 | |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 4061 | vhost->logged_in = 1; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4062 | npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4063 | dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n", |
| 4064 | rsp->partition_name, rsp->device_name, rsp->port_loc_code, |
| 4065 | rsp->drc_name, npiv_max_sectors); |
| 4066 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4067 | fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name); |
| 4068 | fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name); |
| 4069 | fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name); |
| 4070 | fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4071 | fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV; |
| 4072 | fc_host_supported_classes(vhost->host) = 0; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4073 | if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4074 | fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4075 | if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4076 | fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4077 | if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4078 | fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3; |
| 4079 | fc_host_maxframe_size(vhost->host) = |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4080 | be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4081 | |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4082 | vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4083 | vhost->host->max_sectors = npiv_max_sectors; |
| 4084 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY); |
| 4085 | wake_up(&vhost->work_wait_q); |
| 4086 | } |
| 4087 | |
| 4088 | /** |
| 4089 | * ibmvfc_npiv_login - Sends NPIV login |
| 4090 | * @vhost: ibmvfc host struct |
| 4091 | * |
| 4092 | **/ |
| 4093 | static void ibmvfc_npiv_login(struct ibmvfc_host *vhost) |
| 4094 | { |
| 4095 | struct ibmvfc_npiv_login_mad *mad; |
| 4096 | struct ibmvfc_event *evt = ibmvfc_get_event(vhost); |
| 4097 | |
| 4098 | ibmvfc_gather_partition_info(vhost); |
| 4099 | ibmvfc_set_login_info(vhost); |
| 4100 | ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT); |
| 4101 | |
| 4102 | memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info)); |
| 4103 | mad = &evt->iu.npiv_login; |
| 4104 | memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4105 | mad->common.version = cpu_to_be32(1); |
| 4106 | mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN); |
| 4107 | mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad)); |
| 4108 | mad->buffer.va = cpu_to_be64(vhost->login_buf_dma); |
| 4109 | mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf)); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4110 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4111 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT); |
| 4112 | |
| 4113 | if (!ibmvfc_send_event(evt, vhost, default_timeout)) |
| 4114 | ibmvfc_dbg(vhost, "Sent NPIV login\n"); |
| 4115 | else |
| 4116 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 4117 | }; |
| 4118 | |
| 4119 | /** |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 4120 | * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout |
| 4121 | * @vhost: ibmvfc host struct |
| 4122 | * |
| 4123 | **/ |
| 4124 | static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt) |
| 4125 | { |
| 4126 | struct ibmvfc_host *vhost = evt->vhost; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4127 | u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status); |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 4128 | |
| 4129 | ibmvfc_free_event(evt); |
| 4130 | |
| 4131 | switch (mad_status) { |
| 4132 | case IBMVFC_MAD_SUCCESS: |
| 4133 | if (list_empty(&vhost->sent) && |
| 4134 | vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) { |
Brian King | 861890c | 2009-10-19 15:07:49 -0500 | [diff] [blame] | 4135 | ibmvfc_init_host(vhost); |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 4136 | return; |
| 4137 | } |
| 4138 | break; |
| 4139 | case IBMVFC_MAD_FAILED: |
| 4140 | case IBMVFC_MAD_NOT_SUPPORTED: |
| 4141 | case IBMVFC_MAD_CRQ_ERROR: |
| 4142 | case IBMVFC_MAD_DRIVER_FAILED: |
| 4143 | default: |
| 4144 | ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status); |
| 4145 | break; |
| 4146 | } |
| 4147 | |
| 4148 | ibmvfc_hard_reset_host(vhost); |
| 4149 | } |
| 4150 | |
| 4151 | /** |
| 4152 | * ibmvfc_npiv_logout - Issue an NPIV Logout |
| 4153 | * @vhost: ibmvfc host struct |
| 4154 | * |
| 4155 | **/ |
| 4156 | static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost) |
| 4157 | { |
| 4158 | struct ibmvfc_npiv_logout_mad *mad; |
| 4159 | struct ibmvfc_event *evt; |
| 4160 | |
| 4161 | evt = ibmvfc_get_event(vhost); |
| 4162 | ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT); |
| 4163 | |
| 4164 | mad = &evt->iu.npiv_logout; |
| 4165 | memset(mad, 0, sizeof(*mad)); |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4166 | mad->common.version = cpu_to_be32(1); |
| 4167 | mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT); |
| 4168 | mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad)); |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 4169 | |
| 4170 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT); |
| 4171 | |
| 4172 | if (!ibmvfc_send_event(evt, vhost, default_timeout)) |
| 4173 | ibmvfc_dbg(vhost, "Sent NPIV logout\n"); |
| 4174 | else |
| 4175 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 4176 | } |
| 4177 | |
| 4178 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4179 | * ibmvfc_dev_init_to_do - Is there target initialization work to do? |
| 4180 | * @vhost: ibmvfc host struct |
| 4181 | * |
| 4182 | * Returns: |
| 4183 | * 1 if work to do / 0 if not |
| 4184 | **/ |
| 4185 | static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost) |
| 4186 | { |
| 4187 | struct ibmvfc_target *tgt; |
| 4188 | |
| 4189 | list_for_each_entry(tgt, &vhost->targets, queue) { |
| 4190 | if (tgt->action == IBMVFC_TGT_ACTION_INIT || |
| 4191 | tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT) |
| 4192 | return 1; |
| 4193 | } |
| 4194 | |
| 4195 | return 0; |
| 4196 | } |
| 4197 | |
| 4198 | /** |
| 4199 | * __ibmvfc_work_to_do - Is there task level work to do? (no locking) |
| 4200 | * @vhost: ibmvfc host struct |
| 4201 | * |
| 4202 | * Returns: |
| 4203 | * 1 if work to do / 0 if not |
| 4204 | **/ |
| 4205 | static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost) |
| 4206 | { |
| 4207 | struct ibmvfc_target *tgt; |
| 4208 | |
| 4209 | if (kthread_should_stop()) |
| 4210 | return 1; |
| 4211 | switch (vhost->action) { |
| 4212 | case IBMVFC_HOST_ACTION_NONE: |
| 4213 | case IBMVFC_HOST_ACTION_INIT_WAIT: |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 4214 | case IBMVFC_HOST_ACTION_LOGO_WAIT: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4215 | return 0; |
| 4216 | case IBMVFC_HOST_ACTION_TGT_INIT: |
| 4217 | case IBMVFC_HOST_ACTION_QUERY_TGTS: |
| 4218 | if (vhost->discovery_threads == disc_threads) |
| 4219 | return 0; |
| 4220 | list_for_each_entry(tgt, &vhost->targets, queue) |
| 4221 | if (tgt->action == IBMVFC_TGT_ACTION_INIT) |
| 4222 | return 1; |
| 4223 | list_for_each_entry(tgt, &vhost->targets, queue) |
| 4224 | if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT) |
| 4225 | return 0; |
| 4226 | return 1; |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 4227 | case IBMVFC_HOST_ACTION_LOGO: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4228 | case IBMVFC_HOST_ACTION_INIT: |
| 4229 | case IBMVFC_HOST_ACTION_ALLOC_TGTS: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4230 | case IBMVFC_HOST_ACTION_TGT_DEL: |
Brian King | 10e7949 | 2008-10-29 08:46:45 -0500 | [diff] [blame] | 4231 | case IBMVFC_HOST_ACTION_TGT_DEL_FAILED: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4232 | case IBMVFC_HOST_ACTION_QUERY: |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 4233 | case IBMVFC_HOST_ACTION_RESET: |
| 4234 | case IBMVFC_HOST_ACTION_REENABLE: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4235 | default: |
| 4236 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 4237 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4238 | |
| 4239 | return 1; |
| 4240 | } |
| 4241 | |
| 4242 | /** |
| 4243 | * ibmvfc_work_to_do - Is there task level work to do? |
| 4244 | * @vhost: ibmvfc host struct |
| 4245 | * |
| 4246 | * Returns: |
| 4247 | * 1 if work to do / 0 if not |
| 4248 | **/ |
| 4249 | static int ibmvfc_work_to_do(struct ibmvfc_host *vhost) |
| 4250 | { |
| 4251 | unsigned long flags; |
| 4252 | int rc; |
| 4253 | |
| 4254 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 4255 | rc = __ibmvfc_work_to_do(vhost); |
| 4256 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4257 | return rc; |
| 4258 | } |
| 4259 | |
| 4260 | /** |
| 4261 | * ibmvfc_log_ae - Log async events if necessary |
| 4262 | * @vhost: ibmvfc host struct |
| 4263 | * @events: events to log |
| 4264 | * |
| 4265 | **/ |
| 4266 | static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events) |
| 4267 | { |
| 4268 | if (events & IBMVFC_AE_RSCN) |
| 4269 | fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0); |
| 4270 | if ((events & IBMVFC_AE_LINKDOWN) && |
| 4271 | vhost->state >= IBMVFC_HALTED) |
| 4272 | fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0); |
| 4273 | if ((events & IBMVFC_AE_LINKUP) && |
| 4274 | vhost->state == IBMVFC_INITIALIZING) |
| 4275 | fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0); |
| 4276 | } |
| 4277 | |
| 4278 | /** |
| 4279 | * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port |
| 4280 | * @tgt: ibmvfc target struct |
| 4281 | * |
| 4282 | **/ |
| 4283 | static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt) |
| 4284 | { |
| 4285 | struct ibmvfc_host *vhost = tgt->vhost; |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 4286 | struct fc_rport *rport; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4287 | unsigned long flags; |
| 4288 | |
| 4289 | tgt_dbg(tgt, "Adding rport\n"); |
| 4290 | rport = fc_remote_port_add(vhost->host, 0, &tgt->ids); |
| 4291 | spin_lock_irqsave(vhost->host->host_lock, flags); |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 4292 | |
| 4293 | if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) { |
| 4294 | tgt_dbg(tgt, "Deleting rport\n"); |
| 4295 | list_del(&tgt->queue); |
Brian King | d5da304 | 2010-08-05 16:38:31 -0500 | [diff] [blame] | 4296 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT); |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 4297 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4298 | fc_remote_port_delete(rport); |
| 4299 | del_timer_sync(&tgt->timer); |
| 4300 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 4301 | return; |
Brian King | d5da304 | 2010-08-05 16:38:31 -0500 | [diff] [blame] | 4302 | } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) { |
| 4303 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4304 | return; |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 4305 | } |
| 4306 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4307 | if (rport) { |
| 4308 | tgt_dbg(tgt, "rport add succeeded\n"); |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 4309 | tgt->rport = rport; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4310 | rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4311 | rport->supported_classes = 0; |
Brian King | 52d7e86 | 2008-07-22 08:31:46 -0500 | [diff] [blame] | 4312 | tgt->target_id = rport->scsi_target_id; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4313 | if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4314 | rport->supported_classes |= FC_COS_CLASS1; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4315 | if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4316 | rport->supported_classes |= FC_COS_CLASS2; |
Tyrel Datwyler | 0aab6c3 | 2014-06-26 19:03:55 -0500 | [diff] [blame] | 4317 | if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4318 | rport->supported_classes |= FC_COS_CLASS3; |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 4319 | if (rport->rqst_q) |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 4320 | blk_queue_max_segments(rport->rqst_q, 1); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4321 | } else |
| 4322 | tgt_dbg(tgt, "rport add failed\n"); |
| 4323 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4324 | } |
| 4325 | |
| 4326 | /** |
| 4327 | * ibmvfc_do_work - Do task level work |
| 4328 | * @vhost: ibmvfc host struct |
| 4329 | * |
| 4330 | **/ |
| 4331 | static void ibmvfc_do_work(struct ibmvfc_host *vhost) |
| 4332 | { |
| 4333 | struct ibmvfc_target *tgt; |
| 4334 | unsigned long flags; |
| 4335 | struct fc_rport *rport; |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 4336 | int rc; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4337 | |
| 4338 | ibmvfc_log_ae(vhost, vhost->events_to_log); |
| 4339 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 4340 | vhost->events_to_log = 0; |
| 4341 | switch (vhost->action) { |
| 4342 | case IBMVFC_HOST_ACTION_NONE: |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 4343 | case IBMVFC_HOST_ACTION_LOGO_WAIT: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4344 | case IBMVFC_HOST_ACTION_INIT_WAIT: |
| 4345 | break; |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 4346 | case IBMVFC_HOST_ACTION_RESET: |
| 4347 | vhost->action = IBMVFC_HOST_ACTION_TGT_DEL; |
| 4348 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4349 | rc = ibmvfc_reset_crq(vhost); |
| 4350 | spin_lock_irqsave(vhost->host->host_lock, flags); |
Brian King | d24099d | 2010-09-21 10:17:11 -0500 | [diff] [blame] | 4351 | if (rc == H_CLOSED) |
| 4352 | vio_enable_interrupts(to_vio_dev(vhost->dev)); |
Brian King | 3855356 | 2011-06-03 08:23:20 -0500 | [diff] [blame] | 4353 | if (rc || (rc = ibmvfc_send_crq_init(vhost)) || |
| 4354 | (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) { |
Brian King | 73ee5d8 | 2010-06-17 13:55:13 -0500 | [diff] [blame] | 4355 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 4356 | dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc); |
| 4357 | } |
| 4358 | break; |
| 4359 | case IBMVFC_HOST_ACTION_REENABLE: |
| 4360 | vhost->action = IBMVFC_HOST_ACTION_TGT_DEL; |
| 4361 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4362 | rc = ibmvfc_reenable_crq_queue(vhost); |
| 4363 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 4364 | if (rc || (rc = ibmvfc_send_crq_init(vhost))) { |
| 4365 | ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD); |
| 4366 | dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc); |
| 4367 | } |
| 4368 | break; |
Brian King | 79111d0 | 2009-05-28 16:17:29 -0500 | [diff] [blame] | 4369 | case IBMVFC_HOST_ACTION_LOGO: |
| 4370 | vhost->job_step(vhost); |
| 4371 | break; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4372 | case IBMVFC_HOST_ACTION_INIT: |
| 4373 | BUG_ON(vhost->state != IBMVFC_INITIALIZING); |
Brian King | 1c41fa82 | 2008-12-03 11:02:54 -0600 | [diff] [blame] | 4374 | if (vhost->delay_init) { |
| 4375 | vhost->delay_init = 0; |
| 4376 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
Brian King | d2131b3 | 2008-12-18 09:26:51 -0600 | [diff] [blame] | 4377 | ssleep(15); |
Brian King | 1c41fa82 | 2008-12-03 11:02:54 -0600 | [diff] [blame] | 4378 | return; |
| 4379 | } else |
| 4380 | vhost->job_step(vhost); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4381 | break; |
| 4382 | case IBMVFC_HOST_ACTION_QUERY: |
| 4383 | list_for_each_entry(tgt, &vhost->targets, queue) |
| 4384 | ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target); |
| 4385 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS); |
| 4386 | break; |
| 4387 | case IBMVFC_HOST_ACTION_QUERY_TGTS: |
| 4388 | list_for_each_entry(tgt, &vhost->targets, queue) { |
| 4389 | if (tgt->action == IBMVFC_TGT_ACTION_INIT) { |
| 4390 | tgt->job_step(tgt); |
| 4391 | break; |
| 4392 | } |
| 4393 | } |
| 4394 | |
| 4395 | if (!ibmvfc_dev_init_to_do(vhost)) |
| 4396 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL); |
| 4397 | break; |
| 4398 | case IBMVFC_HOST_ACTION_TGT_DEL: |
Brian King | 10e7949 | 2008-10-29 08:46:45 -0500 | [diff] [blame] | 4399 | case IBMVFC_HOST_ACTION_TGT_DEL_FAILED: |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4400 | list_for_each_entry(tgt, &vhost->targets, queue) { |
| 4401 | if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) { |
| 4402 | tgt_dbg(tgt, "Deleting rport\n"); |
| 4403 | rport = tgt->rport; |
| 4404 | tgt->rport = NULL; |
| 4405 | list_del(&tgt->queue); |
Brian King | d5da304 | 2010-08-05 16:38:31 -0500 | [diff] [blame] | 4406 | ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4407 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4408 | if (rport) |
| 4409 | fc_remote_port_delete(rport); |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 4410 | del_timer_sync(&tgt->timer); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4411 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 4412 | return; |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | if (vhost->state == IBMVFC_INITIALIZING) { |
Brian King | 10e7949 | 2008-10-29 08:46:45 -0500 | [diff] [blame] | 4417 | if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) { |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 4418 | if (vhost->reinit) { |
| 4419 | vhost->reinit = 0; |
| 4420 | scsi_block_requests(vhost->host); |
| 4421 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY); |
| 4422 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4423 | } else { |
| 4424 | ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE); |
| 4425 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE); |
| 4426 | wake_up(&vhost->init_wait_q); |
| 4427 | schedule_work(&vhost->rport_add_work_q); |
| 4428 | vhost->init_retries = 0; |
| 4429 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4430 | scsi_unblock_requests(vhost->host); |
| 4431 | } |
| 4432 | |
Brian King | 10e7949 | 2008-10-29 08:46:45 -0500 | [diff] [blame] | 4433 | return; |
| 4434 | } else { |
| 4435 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT); |
| 4436 | vhost->job_step = ibmvfc_discover_targets; |
| 4437 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4438 | } else { |
| 4439 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE); |
| 4440 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4441 | scsi_unblock_requests(vhost->host); |
| 4442 | wake_up(&vhost->init_wait_q); |
| 4443 | return; |
| 4444 | } |
| 4445 | break; |
| 4446 | case IBMVFC_HOST_ACTION_ALLOC_TGTS: |
| 4447 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT); |
| 4448 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4449 | ibmvfc_alloc_targets(vhost); |
| 4450 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 4451 | break; |
| 4452 | case IBMVFC_HOST_ACTION_TGT_INIT: |
| 4453 | list_for_each_entry(tgt, &vhost->targets, queue) { |
| 4454 | if (tgt->action == IBMVFC_TGT_ACTION_INIT) { |
| 4455 | tgt->job_step(tgt); |
| 4456 | break; |
| 4457 | } |
| 4458 | } |
| 4459 | |
Brian King | 10e7949 | 2008-10-29 08:46:45 -0500 | [diff] [blame] | 4460 | if (!ibmvfc_dev_init_to_do(vhost)) |
| 4461 | ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4462 | break; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4463 | default: |
| 4464 | break; |
Christopher DÃaz Riveros | f36cfe6 | 2018-01-17 20:38:39 -0500 | [diff] [blame] | 4465 | } |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4466 | |
| 4467 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4468 | } |
| 4469 | |
| 4470 | /** |
| 4471 | * ibmvfc_work - Do task level work |
| 4472 | * @data: ibmvfc host struct |
| 4473 | * |
| 4474 | * Returns: |
| 4475 | * zero |
| 4476 | **/ |
| 4477 | static int ibmvfc_work(void *data) |
| 4478 | { |
| 4479 | struct ibmvfc_host *vhost = data; |
| 4480 | int rc; |
| 4481 | |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 4482 | set_user_nice(current, MIN_NICE); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4483 | |
| 4484 | while (1) { |
| 4485 | rc = wait_event_interruptible(vhost->work_wait_q, |
| 4486 | ibmvfc_work_to_do(vhost)); |
| 4487 | |
| 4488 | BUG_ON(rc); |
| 4489 | |
| 4490 | if (kthread_should_stop()) |
| 4491 | break; |
| 4492 | |
| 4493 | ibmvfc_do_work(vhost); |
| 4494 | } |
| 4495 | |
| 4496 | ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n"); |
| 4497 | return 0; |
| 4498 | } |
| 4499 | |
| 4500 | /** |
| 4501 | * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor |
| 4502 | * @vhost: ibmvfc host struct |
| 4503 | * |
| 4504 | * Allocates a page for messages, maps it for dma, and registers |
| 4505 | * the crq with the hypervisor. |
| 4506 | * |
| 4507 | * Return value: |
| 4508 | * zero on success / other on failure |
| 4509 | **/ |
| 4510 | static int ibmvfc_init_crq(struct ibmvfc_host *vhost) |
| 4511 | { |
| 4512 | int rc, retrc = -ENOMEM; |
| 4513 | struct device *dev = vhost->dev; |
| 4514 | struct vio_dev *vdev = to_vio_dev(dev); |
| 4515 | struct ibmvfc_crq_queue *crq = &vhost->crq; |
| 4516 | |
| 4517 | ENTER; |
| 4518 | crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL); |
| 4519 | |
| 4520 | if (!crq->msgs) |
| 4521 | return -ENOMEM; |
| 4522 | |
| 4523 | crq->size = PAGE_SIZE / sizeof(*crq->msgs); |
| 4524 | crq->msg_token = dma_map_single(dev, crq->msgs, |
| 4525 | PAGE_SIZE, DMA_BIDIRECTIONAL); |
| 4526 | |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 4527 | if (dma_mapping_error(dev, crq->msg_token)) |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4528 | goto map_failed; |
| 4529 | |
| 4530 | retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address, |
| 4531 | crq->msg_token, PAGE_SIZE); |
| 4532 | |
| 4533 | if (rc == H_RESOURCE) |
| 4534 | /* maybe kexecing and resource is busy. try a reset */ |
| 4535 | retrc = rc = ibmvfc_reset_crq(vhost); |
| 4536 | |
| 4537 | if (rc == H_CLOSED) |
| 4538 | dev_warn(dev, "Partner adapter not ready\n"); |
| 4539 | else if (rc) { |
| 4540 | dev_warn(dev, "Error %d opening adapter\n", rc); |
| 4541 | goto reg_crq_failed; |
| 4542 | } |
| 4543 | |
| 4544 | retrc = 0; |
| 4545 | |
Brian King | 039a089 | 2009-03-20 15:44:35 -0500 | [diff] [blame] | 4546 | tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost); |
| 4547 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4548 | if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) { |
| 4549 | dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc); |
| 4550 | goto req_irq_failed; |
| 4551 | } |
| 4552 | |
| 4553 | if ((rc = vio_enable_interrupts(vdev))) { |
| 4554 | dev_err(dev, "Error %d enabling interrupts\n", rc); |
| 4555 | goto req_irq_failed; |
| 4556 | } |
| 4557 | |
| 4558 | crq->cur = 0; |
| 4559 | LEAVE; |
| 4560 | return retrc; |
| 4561 | |
| 4562 | req_irq_failed: |
Brian King | 039a089 | 2009-03-20 15:44:35 -0500 | [diff] [blame] | 4563 | tasklet_kill(&vhost->tasklet); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4564 | do { |
| 4565 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); |
| 4566 | } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); |
| 4567 | reg_crq_failed: |
| 4568 | dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL); |
| 4569 | map_failed: |
| 4570 | free_page((unsigned long)crq->msgs); |
| 4571 | return retrc; |
| 4572 | } |
| 4573 | |
| 4574 | /** |
| 4575 | * ibmvfc_free_mem - Free memory for vhost |
| 4576 | * @vhost: ibmvfc host struct |
| 4577 | * |
| 4578 | * Return value: |
| 4579 | * none |
| 4580 | **/ |
| 4581 | static void ibmvfc_free_mem(struct ibmvfc_host *vhost) |
| 4582 | { |
| 4583 | struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq; |
| 4584 | |
| 4585 | ENTER; |
| 4586 | mempool_destroy(vhost->tgt_pool); |
| 4587 | kfree(vhost->trace); |
| 4588 | dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf, |
| 4589 | vhost->disc_buf_dma); |
| 4590 | dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf), |
| 4591 | vhost->login_buf, vhost->login_buf_dma); |
| 4592 | dma_pool_destroy(vhost->sg_pool); |
| 4593 | dma_unmap_single(vhost->dev, async_q->msg_token, |
| 4594 | async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL); |
| 4595 | free_page((unsigned long)async_q->msgs); |
| 4596 | LEAVE; |
| 4597 | } |
| 4598 | |
| 4599 | /** |
| 4600 | * ibmvfc_alloc_mem - Allocate memory for vhost |
| 4601 | * @vhost: ibmvfc host struct |
| 4602 | * |
| 4603 | * Return value: |
| 4604 | * 0 on success / non-zero on failure |
| 4605 | **/ |
| 4606 | static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost) |
| 4607 | { |
| 4608 | struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq; |
| 4609 | struct device *dev = vhost->dev; |
| 4610 | |
| 4611 | ENTER; |
| 4612 | async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL); |
| 4613 | if (!async_q->msgs) { |
| 4614 | dev_err(dev, "Couldn't allocate async queue.\n"); |
| 4615 | goto nomem; |
| 4616 | } |
| 4617 | |
| 4618 | async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq); |
| 4619 | async_q->msg_token = dma_map_single(dev, async_q->msgs, |
| 4620 | async_q->size * sizeof(*async_q->msgs), |
| 4621 | DMA_BIDIRECTIONAL); |
| 4622 | |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 4623 | if (dma_mapping_error(dev, async_q->msg_token)) { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4624 | dev_err(dev, "Failed to map async queue\n"); |
| 4625 | goto free_async_crq; |
| 4626 | } |
| 4627 | |
| 4628 | vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev, |
| 4629 | SG_ALL * sizeof(struct srp_direct_buf), |
| 4630 | sizeof(struct srp_direct_buf), 0); |
| 4631 | |
| 4632 | if (!vhost->sg_pool) { |
| 4633 | dev_err(dev, "Failed to allocate sg pool\n"); |
| 4634 | goto unmap_async_crq; |
| 4635 | } |
| 4636 | |
| 4637 | vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf), |
| 4638 | &vhost->login_buf_dma, GFP_KERNEL); |
| 4639 | |
| 4640 | if (!vhost->login_buf) { |
| 4641 | dev_err(dev, "Couldn't allocate NPIV login buffer\n"); |
| 4642 | goto free_sg_pool; |
| 4643 | } |
| 4644 | |
| 4645 | vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets; |
| 4646 | vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz, |
| 4647 | &vhost->disc_buf_dma, GFP_KERNEL); |
| 4648 | |
| 4649 | if (!vhost->disc_buf) { |
| 4650 | dev_err(dev, "Couldn't allocate Discover Targets buffer\n"); |
| 4651 | goto free_login_buffer; |
| 4652 | } |
| 4653 | |
| 4654 | vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES, |
| 4655 | sizeof(struct ibmvfc_trace_entry), GFP_KERNEL); |
| 4656 | |
| 4657 | if (!vhost->trace) |
| 4658 | goto free_disc_buffer; |
| 4659 | |
Sage Weil | d688669 | 2009-08-03 13:54:47 -0700 | [diff] [blame] | 4660 | vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4661 | sizeof(struct ibmvfc_target)); |
| 4662 | |
| 4663 | if (!vhost->tgt_pool) { |
| 4664 | dev_err(dev, "Couldn't allocate target memory pool\n"); |
| 4665 | goto free_trace; |
| 4666 | } |
| 4667 | |
| 4668 | LEAVE; |
| 4669 | return 0; |
| 4670 | |
| 4671 | free_trace: |
| 4672 | kfree(vhost->trace); |
| 4673 | free_disc_buffer: |
| 4674 | dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf, |
| 4675 | vhost->disc_buf_dma); |
| 4676 | free_login_buffer: |
| 4677 | dma_free_coherent(dev, sizeof(*vhost->login_buf), |
| 4678 | vhost->login_buf, vhost->login_buf_dma); |
| 4679 | free_sg_pool: |
| 4680 | dma_pool_destroy(vhost->sg_pool); |
| 4681 | unmap_async_crq: |
| 4682 | dma_unmap_single(dev, async_q->msg_token, |
| 4683 | async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL); |
| 4684 | free_async_crq: |
| 4685 | free_page((unsigned long)async_q->msgs); |
| 4686 | nomem: |
| 4687 | LEAVE; |
| 4688 | return -ENOMEM; |
| 4689 | } |
| 4690 | |
| 4691 | /** |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 4692 | * ibmvfc_rport_add_thread - Worker thread for rport adds |
| 4693 | * @work: work struct |
| 4694 | * |
| 4695 | **/ |
| 4696 | static void ibmvfc_rport_add_thread(struct work_struct *work) |
| 4697 | { |
| 4698 | struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host, |
| 4699 | rport_add_work_q); |
| 4700 | struct ibmvfc_target *tgt; |
| 4701 | struct fc_rport *rport; |
| 4702 | unsigned long flags; |
| 4703 | int did_work; |
| 4704 | |
| 4705 | ENTER; |
| 4706 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 4707 | do { |
| 4708 | did_work = 0; |
| 4709 | if (vhost->state != IBMVFC_ACTIVE) |
| 4710 | break; |
| 4711 | |
| 4712 | list_for_each_entry(tgt, &vhost->targets, queue) { |
| 4713 | if (tgt->add_rport) { |
| 4714 | did_work = 1; |
| 4715 | tgt->add_rport = 0; |
| 4716 | kref_get(&tgt->kref); |
| 4717 | rport = tgt->rport; |
| 4718 | if (!rport) { |
| 4719 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4720 | ibmvfc_tgt_add_rport(tgt); |
| 4721 | } else if (get_device(&rport->dev)) { |
| 4722 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4723 | tgt_dbg(tgt, "Setting rport roles\n"); |
| 4724 | fc_remote_port_rolechg(rport, tgt->ids.roles); |
| 4725 | put_device(&rport->dev); |
Dan Carpenter | 0073887 | 2016-07-15 14:18:57 +0300 | [diff] [blame] | 4726 | } else { |
| 4727 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | kref_put(&tgt->kref, ibmvfc_release_tgt); |
| 4731 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 4732 | break; |
| 4733 | } |
| 4734 | } |
| 4735 | } while(did_work); |
| 4736 | |
| 4737 | if (vhost->state == IBMVFC_ACTIVE) |
| 4738 | vhost->scan_complete = 1; |
| 4739 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4740 | LEAVE; |
| 4741 | } |
| 4742 | |
| 4743 | /** |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4744 | * ibmvfc_probe - Adapter hot plug add entry point |
| 4745 | * @vdev: vio device struct |
| 4746 | * @id: vio device id struct |
| 4747 | * |
| 4748 | * Return value: |
| 4749 | * 0 on success / non-zero on failure |
| 4750 | **/ |
| 4751 | static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id) |
| 4752 | { |
| 4753 | struct ibmvfc_host *vhost; |
| 4754 | struct Scsi_Host *shost; |
| 4755 | struct device *dev = &vdev->dev; |
| 4756 | int rc = -ENOMEM; |
| 4757 | |
| 4758 | ENTER; |
| 4759 | shost = scsi_host_alloc(&driver_template, sizeof(*vhost)); |
| 4760 | if (!shost) { |
| 4761 | dev_err(dev, "Couldn't allocate host data\n"); |
| 4762 | goto out; |
| 4763 | } |
| 4764 | |
| 4765 | shost->transportt = ibmvfc_transport_template; |
| 4766 | shost->can_queue = max_requests; |
| 4767 | shost->max_lun = max_lun; |
| 4768 | shost->max_id = max_targets; |
| 4769 | shost->max_sectors = IBMVFC_MAX_SECTORS; |
| 4770 | shost->max_cmd_len = IBMVFC_MAX_CDB_LEN; |
| 4771 | shost->unique_id = shost->host_no; |
| 4772 | |
| 4773 | vhost = shost_priv(shost); |
| 4774 | INIT_LIST_HEAD(&vhost->sent); |
| 4775 | INIT_LIST_HEAD(&vhost->free); |
| 4776 | INIT_LIST_HEAD(&vhost->targets); |
| 4777 | sprintf(vhost->name, IBMVFC_NAME); |
| 4778 | vhost->host = shost; |
| 4779 | vhost->dev = dev; |
| 4780 | vhost->partition_number = -1; |
| 4781 | vhost->log_level = log_level; |
Brian King | 10501e1 | 2009-03-20 15:44:39 -0500 | [diff] [blame] | 4782 | vhost->task_set = 1; |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4783 | strcpy(vhost->partition_name, "UNKNOWN"); |
| 4784 | init_waitqueue_head(&vhost->work_wait_q); |
| 4785 | init_waitqueue_head(&vhost->init_wait_q); |
Brian King | 43c8da9 | 2009-05-28 16:17:28 -0500 | [diff] [blame] | 4786 | INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread); |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 4787 | mutex_init(&vhost->passthru_mutex); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4788 | |
| 4789 | if ((rc = ibmvfc_alloc_mem(vhost))) |
| 4790 | goto free_scsi_host; |
| 4791 | |
| 4792 | vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME, |
| 4793 | shost->host_no); |
| 4794 | |
| 4795 | if (IS_ERR(vhost->work_thread)) { |
| 4796 | dev_err(dev, "Couldn't create kernel thread: %ld\n", |
| 4797 | PTR_ERR(vhost->work_thread)); |
| 4798 | goto free_host_mem; |
| 4799 | } |
| 4800 | |
| 4801 | if ((rc = ibmvfc_init_crq(vhost))) { |
| 4802 | dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc); |
| 4803 | goto kill_kthread; |
| 4804 | } |
| 4805 | |
| 4806 | if ((rc = ibmvfc_init_event_pool(vhost))) { |
| 4807 | dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc); |
| 4808 | goto release_crq; |
| 4809 | } |
| 4810 | |
| 4811 | if ((rc = scsi_add_host(shost, dev))) |
| 4812 | goto release_event_pool; |
| 4813 | |
Mike Christie | a5110f2 | 2010-09-15 16:52:29 -0500 | [diff] [blame] | 4814 | fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO; |
| 4815 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4816 | if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj, |
| 4817 | &ibmvfc_trace_attr))) { |
| 4818 | dev_err(dev, "Failed to create trace file. rc=%d\n", rc); |
| 4819 | goto remove_shost; |
| 4820 | } |
| 4821 | |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 4822 | if (shost_to_fc_host(shost)->rqst_q) |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 4823 | blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4824 | dev_set_drvdata(dev, vhost); |
| 4825 | spin_lock(&ibmvfc_driver_lock); |
| 4826 | list_add_tail(&vhost->queue, &ibmvfc_head); |
| 4827 | spin_unlock(&ibmvfc_driver_lock); |
| 4828 | |
| 4829 | ibmvfc_send_crq_init(vhost); |
| 4830 | scsi_scan_host(shost); |
| 4831 | return 0; |
| 4832 | |
| 4833 | remove_shost: |
| 4834 | scsi_remove_host(shost); |
| 4835 | release_event_pool: |
| 4836 | ibmvfc_free_event_pool(vhost); |
| 4837 | release_crq: |
| 4838 | ibmvfc_release_crq_queue(vhost); |
| 4839 | kill_kthread: |
| 4840 | kthread_stop(vhost->work_thread); |
| 4841 | free_host_mem: |
| 4842 | ibmvfc_free_mem(vhost); |
| 4843 | free_scsi_host: |
| 4844 | scsi_host_put(shost); |
| 4845 | out: |
| 4846 | LEAVE; |
| 4847 | return rc; |
| 4848 | } |
| 4849 | |
| 4850 | /** |
| 4851 | * ibmvfc_remove - Adapter hot plug remove entry point |
| 4852 | * @vdev: vio device struct |
| 4853 | * |
| 4854 | * Return value: |
| 4855 | * 0 |
| 4856 | **/ |
| 4857 | static int ibmvfc_remove(struct vio_dev *vdev) |
| 4858 | { |
| 4859 | struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev); |
| 4860 | unsigned long flags; |
| 4861 | |
| 4862 | ENTER; |
| 4863 | ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr); |
Brian King | 7043110 | 2009-10-19 15:07:48 -0500 | [diff] [blame] | 4864 | |
| 4865 | spin_lock_irqsave(vhost->host->host_lock, flags); |
Brian King | 2d0da2a | 2008-07-22 08:31:42 -0500 | [diff] [blame] | 4866 | ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE); |
Brian King | 7043110 | 2009-10-19 15:07:48 -0500 | [diff] [blame] | 4867 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4868 | |
Brian King | 2d0da2a | 2008-07-22 08:31:42 -0500 | [diff] [blame] | 4869 | ibmvfc_wait_while_resetting(vhost); |
| 4870 | ibmvfc_release_crq_queue(vhost); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4871 | kthread_stop(vhost->work_thread); |
| 4872 | fc_remove_host(vhost->host); |
| 4873 | scsi_remove_host(vhost->host); |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4874 | |
| 4875 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 4876 | ibmvfc_purge_requests(vhost, DID_ERROR); |
| 4877 | ibmvfc_free_event_pool(vhost); |
| 4878 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4879 | |
| 4880 | ibmvfc_free_mem(vhost); |
| 4881 | spin_lock(&ibmvfc_driver_lock); |
| 4882 | list_del(&vhost->queue); |
| 4883 | spin_unlock(&ibmvfc_driver_lock); |
| 4884 | scsi_host_put(vhost->host); |
| 4885 | LEAVE; |
| 4886 | return 0; |
| 4887 | } |
| 4888 | |
Brian King | 39c1ffe | 2008-07-24 04:35:48 +1000 | [diff] [blame] | 4889 | /** |
Brian King | b0f4d4c | 2010-02-21 10:37:58 -0600 | [diff] [blame] | 4890 | * ibmvfc_resume - Resume from suspend |
| 4891 | * @dev: device struct |
| 4892 | * |
| 4893 | * We may have lost an interrupt across suspend/resume, so kick the |
| 4894 | * interrupt handler |
| 4895 | * |
| 4896 | */ |
| 4897 | static int ibmvfc_resume(struct device *dev) |
| 4898 | { |
| 4899 | unsigned long flags; |
| 4900 | struct ibmvfc_host *vhost = dev_get_drvdata(dev); |
| 4901 | struct vio_dev *vdev = to_vio_dev(dev); |
| 4902 | |
| 4903 | spin_lock_irqsave(vhost->host->host_lock, flags); |
| 4904 | vio_disable_interrupts(vdev); |
| 4905 | tasklet_schedule(&vhost->tasklet); |
| 4906 | spin_unlock_irqrestore(vhost->host->host_lock, flags); |
| 4907 | return 0; |
| 4908 | } |
| 4909 | |
| 4910 | /** |
Brian King | 39c1ffe | 2008-07-24 04:35:48 +1000 | [diff] [blame] | 4911 | * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver |
| 4912 | * @vdev: vio device struct |
| 4913 | * |
| 4914 | * Return value: |
| 4915 | * Number of bytes the driver will need to DMA map at the same time in |
| 4916 | * order to perform well. |
| 4917 | */ |
| 4918 | static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev) |
| 4919 | { |
| 4920 | unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu); |
| 4921 | return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun); |
| 4922 | } |
| 4923 | |
Arvind Yadav | e4df3ea | 2017-08-17 19:15:05 +0530 | [diff] [blame] | 4924 | static const struct vio_device_id ibmvfc_device_table[] = { |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4925 | {"fcp", "IBM,vfc-client"}, |
| 4926 | { "", "" } |
| 4927 | }; |
| 4928 | MODULE_DEVICE_TABLE(vio, ibmvfc_device_table); |
| 4929 | |
Arvind Yadav | e4a80c3 | 2017-06-29 13:24:41 +0530 | [diff] [blame] | 4930 | static const struct dev_pm_ops ibmvfc_pm_ops = { |
Brian King | b0f4d4c | 2010-02-21 10:37:58 -0600 | [diff] [blame] | 4931 | .resume = ibmvfc_resume |
| 4932 | }; |
| 4933 | |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4934 | static struct vio_driver ibmvfc_driver = { |
| 4935 | .id_table = ibmvfc_device_table, |
| 4936 | .probe = ibmvfc_probe, |
| 4937 | .remove = ibmvfc_remove, |
Brian King | 39c1ffe | 2008-07-24 04:35:48 +1000 | [diff] [blame] | 4938 | .get_desired_dma = ibmvfc_get_desired_dma, |
Benjamin Herrenschmidt | cb52d89 | 2012-03-26 19:06:30 +0000 | [diff] [blame] | 4939 | .name = IBMVFC_NAME, |
| 4940 | .pm = &ibmvfc_pm_ops, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4941 | }; |
| 4942 | |
| 4943 | static struct fc_function_template ibmvfc_transport_functions = { |
| 4944 | .show_host_fabric_name = 1, |
| 4945 | .show_host_node_name = 1, |
| 4946 | .show_host_port_name = 1, |
| 4947 | .show_host_supported_classes = 1, |
| 4948 | .show_host_port_type = 1, |
| 4949 | .show_host_port_id = 1, |
Brian King | 9ab3610 | 2009-03-20 15:44:38 -0500 | [diff] [blame] | 4950 | .show_host_maxframe_size = 1, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4951 | |
| 4952 | .get_host_port_state = ibmvfc_get_host_port_state, |
| 4953 | .show_host_port_state = 1, |
| 4954 | |
| 4955 | .get_host_speed = ibmvfc_get_host_speed, |
| 4956 | .show_host_speed = 1, |
| 4957 | |
| 4958 | .issue_fc_host_lip = ibmvfc_issue_fc_host_lip, |
| 4959 | .terminate_rport_io = ibmvfc_terminate_rport_io, |
| 4960 | |
| 4961 | .show_rport_maxframe_size = 1, |
| 4962 | .show_rport_supported_classes = 1, |
| 4963 | |
| 4964 | .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo, |
| 4965 | .show_rport_dev_loss_tmo = 1, |
| 4966 | |
| 4967 | .get_starget_node_name = ibmvfc_get_starget_node_name, |
| 4968 | .show_starget_node_name = 1, |
| 4969 | |
| 4970 | .get_starget_port_name = ibmvfc_get_starget_port_name, |
| 4971 | .show_starget_port_name = 1, |
| 4972 | |
| 4973 | .get_starget_port_id = ibmvfc_get_starget_port_id, |
| 4974 | .show_starget_port_id = 1, |
Brian King | d31429e | 2009-10-19 15:07:54 -0500 | [diff] [blame] | 4975 | |
| 4976 | .bsg_request = ibmvfc_bsg_request, |
| 4977 | .bsg_timeout = ibmvfc_bsg_timeout, |
Brian King | 072b91f | 2008-07-01 13:14:30 -0500 | [diff] [blame] | 4978 | }; |
| 4979 | |
| 4980 | /** |
| 4981 | * ibmvfc_module_init - Initialize the ibmvfc module |
| 4982 | * |
| 4983 | * Return value: |
| 4984 | * 0 on success / other on failure |
| 4985 | **/ |
| 4986 | static int __init ibmvfc_module_init(void) |
| 4987 | { |
| 4988 | int rc; |
| 4989 | |
| 4990 | if (!firmware_has_feature(FW_FEATURE_VIO)) |
| 4991 | return -ENODEV; |
| 4992 | |
| 4993 | printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n", |
| 4994 | IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE); |
| 4995 | |
| 4996 | ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions); |
| 4997 | if (!ibmvfc_transport_template) |
| 4998 | return -ENOMEM; |
| 4999 | |
| 5000 | rc = vio_register_driver(&ibmvfc_driver); |
| 5001 | if (rc) |
| 5002 | fc_release_transport(ibmvfc_transport_template); |
| 5003 | return rc; |
| 5004 | } |
| 5005 | |
| 5006 | /** |
| 5007 | * ibmvfc_module_exit - Teardown the ibmvfc module |
| 5008 | * |
| 5009 | * Return value: |
| 5010 | * nothing |
| 5011 | **/ |
| 5012 | static void __exit ibmvfc_module_exit(void) |
| 5013 | { |
| 5014 | vio_unregister_driver(&ibmvfc_driver); |
| 5015 | fc_release_transport(ibmvfc_transport_template); |
| 5016 | } |
| 5017 | |
| 5018 | module_init(ibmvfc_module_init); |
| 5019 | module_exit(ibmvfc_module_exit); |