Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved. |
| 3 | * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>. |
| 4 | * |
| 5 | * This software is available to you under a choice of one of two |
| 6 | * licenses. You may choose to be licensed under the terms of the GNU |
| 7 | * General Public License (GPL) Version 2, available from the file |
| 8 | * COPYING in the main directory of this source tree, or the |
| 9 | * OpenIB.org BSD license below: |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or |
| 12 | * without modification, are permitted provided that the following |
| 13 | * conditions are met: |
| 14 | * |
| 15 | * - Redistributions of source code must retain the above |
| 16 | * copyright notice, this list of conditions and the following |
| 17 | * disclaimer. |
| 18 | * |
| 19 | * - Redistributions in binary form must reproduce the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer in the documentation and/or other materials |
| 22 | * provided with the distribution. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 | * SOFTWARE. |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/err.h> |
| 39 | #include <linux/ctype.h> |
| 40 | #include <linux/kthread.h> |
| 41 | #include <linux/string.h> |
| 42 | #include <linux/delay.h> |
| 43 | #include <linux/atomic.h> |
Bart Van Assche | ba92999 | 2015-05-08 10:11:12 +0200 | [diff] [blame] | 44 | #include <scsi/scsi_proto.h> |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 45 | #include <scsi/scsi_tcq.h> |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 46 | #include <target/target_core_base.h> |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 47 | #include <target/target_core_fabric.h> |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 48 | #include "ib_srpt.h" |
| 49 | |
| 50 | /* Name of this kernel module. */ |
| 51 | #define DRV_NAME "ib_srpt" |
| 52 | #define DRV_VERSION "2.0.0" |
| 53 | #define DRV_RELDATE "2011-02-14" |
| 54 | |
| 55 | #define SRPT_ID_STRING "Linux SRP target" |
| 56 | |
| 57 | #undef pr_fmt |
| 58 | #define pr_fmt(fmt) DRV_NAME " " fmt |
| 59 | |
| 60 | MODULE_AUTHOR("Vu Pham and Bart Van Assche"); |
| 61 | MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target " |
| 62 | "v" DRV_VERSION " (" DRV_RELDATE ")"); |
| 63 | MODULE_LICENSE("Dual BSD/GPL"); |
| 64 | |
| 65 | /* |
| 66 | * Global Variables |
| 67 | */ |
| 68 | |
| 69 | static u64 srpt_service_guid; |
Roland Dreier | 486d8b9 | 2012-02-02 12:55:58 -0800 | [diff] [blame] | 70 | static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */ |
| 71 | static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */ |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 72 | |
| 73 | static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE; |
| 74 | module_param(srp_max_req_size, int, 0444); |
| 75 | MODULE_PARM_DESC(srp_max_req_size, |
| 76 | "Maximum size of SRP request messages in bytes."); |
| 77 | |
| 78 | static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE; |
| 79 | module_param(srpt_srq_size, int, 0444); |
| 80 | MODULE_PARM_DESC(srpt_srq_size, |
| 81 | "Shared receive queue (SRQ) size."); |
| 82 | |
| 83 | static int srpt_get_u64_x(char *buffer, struct kernel_param *kp) |
| 84 | { |
| 85 | return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg); |
| 86 | } |
| 87 | module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid, |
| 88 | 0444); |
| 89 | MODULE_PARM_DESC(srpt_service_guid, |
| 90 | "Using this value for ioc_guid, id_ext, and cm_listen_id" |
| 91 | " instead of using the node_guid of the first HCA."); |
| 92 | |
| 93 | static struct ib_client srpt_client; |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame^] | 94 | static void srpt_release_cmd(struct se_cmd *se_cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 95 | static void srpt_release_channel(struct srpt_rdma_ch *ch); |
| 96 | static int srpt_queue_status(struct se_cmd *cmd); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 97 | static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc); |
| 98 | static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 99 | |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 100 | /* |
| 101 | * The only allowed channel state changes are those that change the channel |
| 102 | * state into a state with a higher numerical value. Hence the new > prev test. |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 103 | */ |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 104 | static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 105 | { |
| 106 | unsigned long flags; |
| 107 | enum rdma_ch_state prev; |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 108 | bool changed = false; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 109 | |
| 110 | spin_lock_irqsave(&ch->spinlock, flags); |
| 111 | prev = ch->state; |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 112 | if (new > prev) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 113 | ch->state = new; |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 114 | changed = true; |
| 115 | } |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 116 | spin_unlock_irqrestore(&ch->spinlock, flags); |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 117 | |
| 118 | return changed; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * srpt_event_handler() - Asynchronous IB event callback function. |
| 123 | * |
| 124 | * Callback function called by the InfiniBand core when an asynchronous IB |
| 125 | * event occurs. This callback may occur in interrupt context. See also |
| 126 | * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand |
| 127 | * Architecture Specification. |
| 128 | */ |
| 129 | static void srpt_event_handler(struct ib_event_handler *handler, |
| 130 | struct ib_event *event) |
| 131 | { |
| 132 | struct srpt_device *sdev; |
| 133 | struct srpt_port *sport; |
| 134 | |
| 135 | sdev = ib_get_client_data(event->device, &srpt_client); |
| 136 | if (!sdev || sdev->device != event->device) |
| 137 | return; |
| 138 | |
| 139 | pr_debug("ASYNC event= %d on device= %s\n", event->event, |
Bart Van Assche | f68cba4e9 | 2016-02-11 11:04:20 -0800 | [diff] [blame] | 140 | sdev->device->name); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 141 | |
| 142 | switch (event->event) { |
| 143 | case IB_EVENT_PORT_ERR: |
| 144 | if (event->element.port_num <= sdev->device->phys_port_cnt) { |
| 145 | sport = &sdev->port[event->element.port_num - 1]; |
| 146 | sport->lid = 0; |
| 147 | sport->sm_lid = 0; |
| 148 | } |
| 149 | break; |
| 150 | case IB_EVENT_PORT_ACTIVE: |
| 151 | case IB_EVENT_LID_CHANGE: |
| 152 | case IB_EVENT_PKEY_CHANGE: |
| 153 | case IB_EVENT_SM_CHANGE: |
| 154 | case IB_EVENT_CLIENT_REREGISTER: |
Doug Ledford | 2aa1cf6 | 2014-08-12 19:20:10 -0400 | [diff] [blame] | 155 | case IB_EVENT_GID_CHANGE: |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 156 | /* Refresh port data asynchronously. */ |
| 157 | if (event->element.port_num <= sdev->device->phys_port_cnt) { |
| 158 | sport = &sdev->port[event->element.port_num - 1]; |
| 159 | if (!sport->lid && !sport->sm_lid) |
| 160 | schedule_work(&sport->work); |
| 161 | } |
| 162 | break; |
| 163 | default: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 164 | pr_err("received unrecognized IB event %d\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 165 | event->event); |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * srpt_srq_event() - SRQ event callback function. |
| 172 | */ |
| 173 | static void srpt_srq_event(struct ib_event *event, void *ctx) |
| 174 | { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 175 | pr_info("SRQ event %d\n", event->event); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
| 179 | * srpt_qp_event() - QP event callback function. |
| 180 | */ |
| 181 | static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch) |
| 182 | { |
| 183 | pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n", |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 184 | event->event, ch->cm_id, ch->sess_name, ch->state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 185 | |
| 186 | switch (event->event) { |
| 187 | case IB_EVENT_COMM_EST: |
| 188 | ib_cm_notify(ch->cm_id, event->event); |
| 189 | break; |
| 190 | case IB_EVENT_QP_LAST_WQE_REACHED: |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 191 | if (srpt_set_ch_state(ch, CH_RELEASING)) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 192 | srpt_release_channel(ch); |
| 193 | else |
| 194 | pr_debug("%s: state %d - ignored LAST_WQE.\n", |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 195 | ch->sess_name, ch->state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 196 | break; |
| 197 | default: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 198 | pr_err("received unrecognized IB QP event %d\n", event->event); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure. |
| 205 | * |
| 206 | * @slot: one-based slot number. |
| 207 | * @value: four-bit value. |
| 208 | * |
| 209 | * Copies the lowest four bits of value in element slot of the array of four |
| 210 | * bit elements called c_list (controller list). The index slot is one-based. |
| 211 | */ |
| 212 | static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value) |
| 213 | { |
| 214 | u16 id; |
| 215 | u8 tmp; |
| 216 | |
| 217 | id = (slot - 1) / 2; |
| 218 | if (slot & 0x1) { |
| 219 | tmp = c_list[id] & 0xf; |
| 220 | c_list[id] = (value << 4) | tmp; |
| 221 | } else { |
| 222 | tmp = c_list[id] & 0xf0; |
| 223 | c_list[id] = (value & 0xf) | tmp; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram. |
| 229 | * |
| 230 | * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture |
| 231 | * Specification. |
| 232 | */ |
| 233 | static void srpt_get_class_port_info(struct ib_dm_mad *mad) |
| 234 | { |
| 235 | struct ib_class_port_info *cif; |
| 236 | |
| 237 | cif = (struct ib_class_port_info *)mad->data; |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 238 | memset(cif, 0, sizeof(*cif)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 239 | cif->base_version = 1; |
| 240 | cif->class_version = 1; |
| 241 | cif->resp_time_value = 20; |
| 242 | |
| 243 | mad->mad_hdr.status = 0; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * srpt_get_iou() - Write IOUnitInfo to a management datagram. |
| 248 | * |
| 249 | * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture |
| 250 | * Specification. See also section B.7, table B.6 in the SRP r16a document. |
| 251 | */ |
| 252 | static void srpt_get_iou(struct ib_dm_mad *mad) |
| 253 | { |
| 254 | struct ib_dm_iou_info *ioui; |
| 255 | u8 slot; |
| 256 | int i; |
| 257 | |
| 258 | ioui = (struct ib_dm_iou_info *)mad->data; |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 259 | ioui->change_id = cpu_to_be16(1); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 260 | ioui->max_controllers = 16; |
| 261 | |
| 262 | /* set present for slot 1 and empty for the rest */ |
| 263 | srpt_set_ioc(ioui->controller_list, 1, 1); |
| 264 | for (i = 1, slot = 2; i < 16; i++, slot++) |
| 265 | srpt_set_ioc(ioui->controller_list, slot, 0); |
| 266 | |
| 267 | mad->mad_hdr.status = 0; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * srpt_get_ioc() - Write IOControllerprofile to a management datagram. |
| 272 | * |
| 273 | * See also section 16.3.3.4 IOControllerProfile in the InfiniBand |
| 274 | * Architecture Specification. See also section B.7, table B.7 in the SRP |
| 275 | * r16a document. |
| 276 | */ |
| 277 | static void srpt_get_ioc(struct srpt_port *sport, u32 slot, |
| 278 | struct ib_dm_mad *mad) |
| 279 | { |
| 280 | struct srpt_device *sdev = sport->sdev; |
| 281 | struct ib_dm_ioc_profile *iocp; |
| 282 | |
| 283 | iocp = (struct ib_dm_ioc_profile *)mad->data; |
| 284 | |
| 285 | if (!slot || slot > 16) { |
| 286 | mad->mad_hdr.status |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 287 | = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | if (slot > 2) { |
| 292 | mad->mad_hdr.status |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 293 | = cpu_to_be16(DM_MAD_STATUS_NO_IOC); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 297 | memset(iocp, 0, sizeof(*iocp)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 298 | strcpy(iocp->id_string, SRPT_ID_STRING); |
| 299 | iocp->guid = cpu_to_be64(srpt_service_guid); |
Or Gerlitz | 4a061b2 | 2015-12-18 10:59:46 +0200 | [diff] [blame] | 300 | iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id); |
| 301 | iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id); |
| 302 | iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver); |
| 303 | iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 304 | iocp->subsys_device_id = 0x0; |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 305 | iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS); |
| 306 | iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS); |
| 307 | iocp->protocol = cpu_to_be16(SRP_PROTOCOL); |
| 308 | iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 309 | iocp->send_queue_depth = cpu_to_be16(sdev->srq_size); |
| 310 | iocp->rdma_read_depth = 4; |
| 311 | iocp->send_size = cpu_to_be32(srp_max_req_size); |
| 312 | iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size, |
| 313 | 1U << 24)); |
| 314 | iocp->num_svc_entries = 1; |
| 315 | iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC | |
| 316 | SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC; |
| 317 | |
| 318 | mad->mad_hdr.status = 0; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * srpt_get_svc_entries() - Write ServiceEntries to a management datagram. |
| 323 | * |
| 324 | * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture |
| 325 | * Specification. See also section B.7, table B.8 in the SRP r16a document. |
| 326 | */ |
| 327 | static void srpt_get_svc_entries(u64 ioc_guid, |
| 328 | u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad) |
| 329 | { |
| 330 | struct ib_dm_svc_entries *svc_entries; |
| 331 | |
| 332 | WARN_ON(!ioc_guid); |
| 333 | |
| 334 | if (!slot || slot > 16) { |
| 335 | mad->mad_hdr.status |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 336 | = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | |
| 340 | if (slot > 2 || lo > hi || hi > 1) { |
| 341 | mad->mad_hdr.status |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 342 | = cpu_to_be16(DM_MAD_STATUS_NO_IOC); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | |
| 346 | svc_entries = (struct ib_dm_svc_entries *)mad->data; |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 347 | memset(svc_entries, 0, sizeof(*svc_entries)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 348 | svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid); |
| 349 | snprintf(svc_entries->service_entries[0].name, |
| 350 | sizeof(svc_entries->service_entries[0].name), |
| 351 | "%s%016llx", |
| 352 | SRP_SERVICE_NAME_PREFIX, |
| 353 | ioc_guid); |
| 354 | |
| 355 | mad->mad_hdr.status = 0; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * srpt_mgmt_method_get() - Process a received management datagram. |
| 360 | * @sp: source port through which the MAD has been received. |
| 361 | * @rq_mad: received MAD. |
| 362 | * @rsp_mad: response MAD. |
| 363 | */ |
| 364 | static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad, |
| 365 | struct ib_dm_mad *rsp_mad) |
| 366 | { |
| 367 | u16 attr_id; |
| 368 | u32 slot; |
| 369 | u8 hi, lo; |
| 370 | |
| 371 | attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id); |
| 372 | switch (attr_id) { |
| 373 | case DM_ATTR_CLASS_PORT_INFO: |
| 374 | srpt_get_class_port_info(rsp_mad); |
| 375 | break; |
| 376 | case DM_ATTR_IOU_INFO: |
| 377 | srpt_get_iou(rsp_mad); |
| 378 | break; |
| 379 | case DM_ATTR_IOC_PROFILE: |
| 380 | slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod); |
| 381 | srpt_get_ioc(sp, slot, rsp_mad); |
| 382 | break; |
| 383 | case DM_ATTR_SVC_ENTRIES: |
| 384 | slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod); |
| 385 | hi = (u8) ((slot >> 8) & 0xff); |
| 386 | lo = (u8) (slot & 0xff); |
| 387 | slot = (u16) ((slot >> 16) & 0xffff); |
| 388 | srpt_get_svc_entries(srpt_service_guid, |
| 389 | slot, hi, lo, rsp_mad); |
| 390 | break; |
| 391 | default: |
| 392 | rsp_mad->mad_hdr.status = |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 393 | cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 394 | break; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * srpt_mad_send_handler() - Post MAD-send callback function. |
| 400 | */ |
| 401 | static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent, |
| 402 | struct ib_mad_send_wc *mad_wc) |
| 403 | { |
| 404 | ib_destroy_ah(mad_wc->send_buf->ah); |
| 405 | ib_free_send_mad(mad_wc->send_buf); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * srpt_mad_recv_handler() - MAD reception callback function. |
| 410 | */ |
| 411 | static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent, |
Christoph Hellwig | ca28126 | 2016-01-04 14:15:58 +0100 | [diff] [blame] | 412 | struct ib_mad_send_buf *send_buf, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 413 | struct ib_mad_recv_wc *mad_wc) |
| 414 | { |
| 415 | struct srpt_port *sport = (struct srpt_port *)mad_agent->context; |
| 416 | struct ib_ah *ah; |
| 417 | struct ib_mad_send_buf *rsp; |
| 418 | struct ib_dm_mad *dm_mad; |
| 419 | |
| 420 | if (!mad_wc || !mad_wc->recv_buf.mad) |
| 421 | return; |
| 422 | |
| 423 | ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc, |
| 424 | mad_wc->recv_buf.grh, mad_agent->port_num); |
| 425 | if (IS_ERR(ah)) |
| 426 | goto err; |
| 427 | |
| 428 | BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR); |
| 429 | |
| 430 | rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp, |
| 431 | mad_wc->wc->pkey_index, 0, |
| 432 | IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA, |
Ira Weiny | da2dfaa | 2015-06-06 14:38:28 -0400 | [diff] [blame] | 433 | GFP_KERNEL, |
| 434 | IB_MGMT_BASE_VERSION); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 435 | if (IS_ERR(rsp)) |
| 436 | goto err_rsp; |
| 437 | |
| 438 | rsp->ah = ah; |
| 439 | |
| 440 | dm_mad = rsp->mad; |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 441 | memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 442 | dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP; |
| 443 | dm_mad->mad_hdr.status = 0; |
| 444 | |
| 445 | switch (mad_wc->recv_buf.mad->mad_hdr.method) { |
| 446 | case IB_MGMT_METHOD_GET: |
| 447 | srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad); |
| 448 | break; |
| 449 | case IB_MGMT_METHOD_SET: |
| 450 | dm_mad->mad_hdr.status = |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 451 | cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 452 | break; |
| 453 | default: |
| 454 | dm_mad->mad_hdr.status = |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 455 | cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 456 | break; |
| 457 | } |
| 458 | |
| 459 | if (!ib_post_send_mad(rsp, NULL)) { |
| 460 | ib_free_recv_mad(mad_wc); |
| 461 | /* will destroy_ah & free_send_mad in send completion */ |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | ib_free_send_mad(rsp); |
| 466 | |
| 467 | err_rsp: |
| 468 | ib_destroy_ah(ah); |
| 469 | err: |
| 470 | ib_free_recv_mad(mad_wc); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * srpt_refresh_port() - Configure a HCA port. |
| 475 | * |
| 476 | * Enable InfiniBand management datagram processing, update the cached sm_lid, |
| 477 | * lid and gid values, and register a callback function for processing MADs |
| 478 | * on the specified port. |
| 479 | * |
| 480 | * Note: It is safe to call this function more than once for the same port. |
| 481 | */ |
| 482 | static int srpt_refresh_port(struct srpt_port *sport) |
| 483 | { |
| 484 | struct ib_mad_reg_req reg_req; |
| 485 | struct ib_port_modify port_modify; |
| 486 | struct ib_port_attr port_attr; |
| 487 | int ret; |
| 488 | |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 489 | memset(&port_modify, 0, sizeof(port_modify)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 490 | port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP; |
| 491 | port_modify.clr_port_cap_mask = 0; |
| 492 | |
| 493 | ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify); |
| 494 | if (ret) |
| 495 | goto err_mod_port; |
| 496 | |
| 497 | ret = ib_query_port(sport->sdev->device, sport->port, &port_attr); |
| 498 | if (ret) |
| 499 | goto err_query_port; |
| 500 | |
| 501 | sport->sm_lid = port_attr.sm_lid; |
| 502 | sport->lid = port_attr.lid; |
| 503 | |
Matan Barak | 55ee3ab | 2015-10-15 18:38:45 +0300 | [diff] [blame] | 504 | ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid, |
| 505 | NULL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 506 | if (ret) |
| 507 | goto err_query_port; |
| 508 | |
| 509 | if (!sport->mad_agent) { |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 510 | memset(®_req, 0, sizeof(reg_req)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 511 | reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT; |
| 512 | reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION; |
| 513 | set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask); |
| 514 | set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask); |
| 515 | |
| 516 | sport->mad_agent = ib_register_mad_agent(sport->sdev->device, |
| 517 | sport->port, |
| 518 | IB_QPT_GSI, |
| 519 | ®_req, 0, |
| 520 | srpt_mad_send_handler, |
| 521 | srpt_mad_recv_handler, |
Ira Weiny | 0f29b46 | 2014-08-08 19:00:55 -0400 | [diff] [blame] | 522 | sport, 0); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 523 | if (IS_ERR(sport->mad_agent)) { |
| 524 | ret = PTR_ERR(sport->mad_agent); |
| 525 | sport->mad_agent = NULL; |
| 526 | goto err_query_port; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | |
| 532 | err_query_port: |
| 533 | |
| 534 | port_modify.set_port_cap_mask = 0; |
| 535 | port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP; |
| 536 | ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify); |
| 537 | |
| 538 | err_mod_port: |
| 539 | |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * srpt_unregister_mad_agent() - Unregister MAD callback functions. |
| 545 | * |
| 546 | * Note: It is safe to call this function more than once for the same device. |
| 547 | */ |
| 548 | static void srpt_unregister_mad_agent(struct srpt_device *sdev) |
| 549 | { |
| 550 | struct ib_port_modify port_modify = { |
| 551 | .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP, |
| 552 | }; |
| 553 | struct srpt_port *sport; |
| 554 | int i; |
| 555 | |
| 556 | for (i = 1; i <= sdev->device->phys_port_cnt; i++) { |
| 557 | sport = &sdev->port[i - 1]; |
| 558 | WARN_ON(sport->port != i); |
| 559 | if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 560 | pr_err("disabling MAD processing failed.\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 561 | if (sport->mad_agent) { |
| 562 | ib_unregister_mad_agent(sport->mad_agent); |
| 563 | sport->mad_agent = NULL; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure. |
| 570 | */ |
| 571 | static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev, |
| 572 | int ioctx_size, int dma_size, |
| 573 | enum dma_data_direction dir) |
| 574 | { |
| 575 | struct srpt_ioctx *ioctx; |
| 576 | |
| 577 | ioctx = kmalloc(ioctx_size, GFP_KERNEL); |
| 578 | if (!ioctx) |
| 579 | goto err; |
| 580 | |
| 581 | ioctx->buf = kmalloc(dma_size, GFP_KERNEL); |
| 582 | if (!ioctx->buf) |
| 583 | goto err_free_ioctx; |
| 584 | |
| 585 | ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir); |
| 586 | if (ib_dma_mapping_error(sdev->device, ioctx->dma)) |
| 587 | goto err_free_buf; |
| 588 | |
| 589 | return ioctx; |
| 590 | |
| 591 | err_free_buf: |
| 592 | kfree(ioctx->buf); |
| 593 | err_free_ioctx: |
| 594 | kfree(ioctx); |
| 595 | err: |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * srpt_free_ioctx() - Free an SRPT I/O context structure. |
| 601 | */ |
| 602 | static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx, |
| 603 | int dma_size, enum dma_data_direction dir) |
| 604 | { |
| 605 | if (!ioctx) |
| 606 | return; |
| 607 | |
| 608 | ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir); |
| 609 | kfree(ioctx->buf); |
| 610 | kfree(ioctx); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures. |
| 615 | * @sdev: Device to allocate the I/O context ring for. |
| 616 | * @ring_size: Number of elements in the I/O context ring. |
| 617 | * @ioctx_size: I/O context size. |
| 618 | * @dma_size: DMA buffer size. |
| 619 | * @dir: DMA data direction. |
| 620 | */ |
| 621 | static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev, |
| 622 | int ring_size, int ioctx_size, |
| 623 | int dma_size, enum dma_data_direction dir) |
| 624 | { |
| 625 | struct srpt_ioctx **ring; |
| 626 | int i; |
| 627 | |
| 628 | WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx) |
| 629 | && ioctx_size != sizeof(struct srpt_send_ioctx)); |
| 630 | |
| 631 | ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL); |
| 632 | if (!ring) |
| 633 | goto out; |
| 634 | for (i = 0; i < ring_size; ++i) { |
| 635 | ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir); |
| 636 | if (!ring[i]) |
| 637 | goto err; |
| 638 | ring[i]->index = i; |
| 639 | } |
| 640 | goto out; |
| 641 | |
| 642 | err: |
| 643 | while (--i >= 0) |
| 644 | srpt_free_ioctx(sdev, ring[i], dma_size, dir); |
| 645 | kfree(ring); |
Jesper Juhl | 715252d | 2012-02-04 23:49:40 +0100 | [diff] [blame] | 646 | ring = NULL; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 647 | out: |
| 648 | return ring; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures. |
| 653 | */ |
| 654 | static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring, |
| 655 | struct srpt_device *sdev, int ring_size, |
| 656 | int dma_size, enum dma_data_direction dir) |
| 657 | { |
| 658 | int i; |
| 659 | |
| 660 | for (i = 0; i < ring_size; ++i) |
| 661 | srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir); |
| 662 | kfree(ioctx_ring); |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * srpt_get_cmd_state() - Get the state of a SCSI command. |
| 667 | */ |
| 668 | static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx) |
| 669 | { |
| 670 | enum srpt_command_state state; |
| 671 | unsigned long flags; |
| 672 | |
| 673 | BUG_ON(!ioctx); |
| 674 | |
| 675 | spin_lock_irqsave(&ioctx->spinlock, flags); |
| 676 | state = ioctx->state; |
| 677 | spin_unlock_irqrestore(&ioctx->spinlock, flags); |
| 678 | return state; |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * srpt_set_cmd_state() - Set the state of a SCSI command. |
| 683 | * |
| 684 | * Does not modify the state of aborted commands. Returns the previous command |
| 685 | * state. |
| 686 | */ |
| 687 | static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx, |
| 688 | enum srpt_command_state new) |
| 689 | { |
| 690 | enum srpt_command_state previous; |
| 691 | unsigned long flags; |
| 692 | |
| 693 | BUG_ON(!ioctx); |
| 694 | |
| 695 | spin_lock_irqsave(&ioctx->spinlock, flags); |
| 696 | previous = ioctx->state; |
| 697 | if (previous != SRPT_STATE_DONE) |
| 698 | ioctx->state = new; |
| 699 | spin_unlock_irqrestore(&ioctx->spinlock, flags); |
| 700 | |
| 701 | return previous; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * srpt_test_and_set_cmd_state() - Test and set the state of a command. |
| 706 | * |
| 707 | * Returns true if and only if the previous command state was equal to 'old'. |
| 708 | */ |
| 709 | static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx, |
| 710 | enum srpt_command_state old, |
| 711 | enum srpt_command_state new) |
| 712 | { |
| 713 | enum srpt_command_state previous; |
| 714 | unsigned long flags; |
| 715 | |
| 716 | WARN_ON(!ioctx); |
| 717 | WARN_ON(old == SRPT_STATE_DONE); |
| 718 | WARN_ON(new == SRPT_STATE_NEW); |
| 719 | |
| 720 | spin_lock_irqsave(&ioctx->spinlock, flags); |
| 721 | previous = ioctx->state; |
| 722 | if (previous == old) |
| 723 | ioctx->state = new; |
| 724 | spin_unlock_irqrestore(&ioctx->spinlock, flags); |
| 725 | return previous == old; |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * srpt_post_recv() - Post an IB receive request. |
| 730 | */ |
| 731 | static int srpt_post_recv(struct srpt_device *sdev, |
| 732 | struct srpt_recv_ioctx *ioctx) |
| 733 | { |
| 734 | struct ib_sge list; |
| 735 | struct ib_recv_wr wr, *bad_wr; |
| 736 | |
| 737 | BUG_ON(!sdev); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 738 | list.addr = ioctx->ioctx.dma; |
| 739 | list.length = srp_max_req_size; |
Jason Gunthorpe | 5a78395 | 2015-07-30 17:22:24 -0600 | [diff] [blame] | 740 | list.lkey = sdev->pd->local_dma_lkey; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 741 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 742 | ioctx->ioctx.cqe.done = srpt_recv_done; |
| 743 | wr.wr_cqe = &ioctx->ioctx.cqe; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 744 | wr.next = NULL; |
| 745 | wr.sg_list = &list; |
| 746 | wr.num_sge = 1; |
| 747 | |
| 748 | return ib_post_srq_recv(sdev->srq, &wr, &bad_wr); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * srpt_post_send() - Post an IB send request. |
| 753 | * |
| 754 | * Returns zero upon success and a non-zero value upon failure. |
| 755 | */ |
| 756 | static int srpt_post_send(struct srpt_rdma_ch *ch, |
| 757 | struct srpt_send_ioctx *ioctx, int len) |
| 758 | { |
| 759 | struct ib_sge list; |
| 760 | struct ib_send_wr wr, *bad_wr; |
| 761 | struct srpt_device *sdev = ch->sport->sdev; |
| 762 | int ret; |
| 763 | |
| 764 | atomic_inc(&ch->req_lim); |
| 765 | |
| 766 | ret = -ENOMEM; |
| 767 | if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 768 | pr_warn("IB send queue full (needed 1)\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 769 | goto out; |
| 770 | } |
| 771 | |
| 772 | ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len, |
| 773 | DMA_TO_DEVICE); |
| 774 | |
| 775 | list.addr = ioctx->ioctx.dma; |
| 776 | list.length = len; |
Jason Gunthorpe | 5a78395 | 2015-07-30 17:22:24 -0600 | [diff] [blame] | 777 | list.lkey = sdev->pd->local_dma_lkey; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 778 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 779 | ioctx->ioctx.cqe.done = srpt_send_done; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 780 | wr.next = NULL; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 781 | wr.wr_cqe = &ioctx->ioctx.cqe; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 782 | wr.sg_list = &list; |
| 783 | wr.num_sge = 1; |
| 784 | wr.opcode = IB_WR_SEND; |
| 785 | wr.send_flags = IB_SEND_SIGNALED; |
| 786 | |
| 787 | ret = ib_post_send(ch->qp, &wr, &bad_wr); |
| 788 | |
| 789 | out: |
| 790 | if (ret < 0) { |
| 791 | atomic_inc(&ch->sq_wr_avail); |
| 792 | atomic_dec(&ch->req_lim); |
| 793 | } |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request. |
| 799 | * @ioctx: Pointer to the I/O context associated with the request. |
| 800 | * @srp_cmd: Pointer to the SRP_CMD request data. |
| 801 | * @dir: Pointer to the variable to which the transfer direction will be |
| 802 | * written. |
| 803 | * @data_len: Pointer to the variable to which the total data length of all |
| 804 | * descriptors in the SRP_CMD request will be written. |
| 805 | * |
| 806 | * This function initializes ioctx->nrbuf and ioctx->r_bufs. |
| 807 | * |
| 808 | * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors; |
| 809 | * -ENOMEM when memory allocation fails and zero upon success. |
| 810 | */ |
| 811 | static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx, |
| 812 | struct srp_cmd *srp_cmd, |
| 813 | enum dma_data_direction *dir, u64 *data_len) |
| 814 | { |
| 815 | struct srp_indirect_buf *idb; |
| 816 | struct srp_direct_buf *db; |
| 817 | unsigned add_cdb_offset; |
| 818 | int ret; |
| 819 | |
| 820 | /* |
| 821 | * The pointer computations below will only be compiled correctly |
| 822 | * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check |
| 823 | * whether srp_cmd::add_data has been declared as a byte pointer. |
| 824 | */ |
| 825 | BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) |
| 826 | && !__same_type(srp_cmd->add_data[0], (u8)0)); |
| 827 | |
| 828 | BUG_ON(!dir); |
| 829 | BUG_ON(!data_len); |
| 830 | |
| 831 | ret = 0; |
| 832 | *data_len = 0; |
| 833 | |
| 834 | /* |
| 835 | * The lower four bits of the buffer format field contain the DATA-IN |
| 836 | * buffer descriptor format, and the highest four bits contain the |
| 837 | * DATA-OUT buffer descriptor format. |
| 838 | */ |
| 839 | *dir = DMA_NONE; |
| 840 | if (srp_cmd->buf_fmt & 0xf) |
| 841 | /* DATA-IN: transfer data from target to initiator (read). */ |
| 842 | *dir = DMA_FROM_DEVICE; |
| 843 | else if (srp_cmd->buf_fmt >> 4) |
| 844 | /* DATA-OUT: transfer data from initiator to target (write). */ |
| 845 | *dir = DMA_TO_DEVICE; |
| 846 | |
| 847 | /* |
| 848 | * According to the SRP spec, the lower two bits of the 'ADDITIONAL |
| 849 | * CDB LENGTH' field are reserved and the size in bytes of this field |
| 850 | * is four times the value specified in bits 3..7. Hence the "& ~3". |
| 851 | */ |
| 852 | add_cdb_offset = srp_cmd->add_cdb_len & ~3; |
| 853 | if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) || |
| 854 | ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) { |
| 855 | ioctx->n_rbuf = 1; |
| 856 | ioctx->rbufs = &ioctx->single_rbuf; |
| 857 | |
| 858 | db = (struct srp_direct_buf *)(srp_cmd->add_data |
| 859 | + add_cdb_offset); |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 860 | memcpy(ioctx->rbufs, db, sizeof(*db)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 861 | *data_len = be32_to_cpu(db->len); |
| 862 | } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) || |
| 863 | ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) { |
| 864 | idb = (struct srp_indirect_buf *)(srp_cmd->add_data |
| 865 | + add_cdb_offset); |
| 866 | |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 867 | ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof(*db); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 868 | |
| 869 | if (ioctx->n_rbuf > |
| 870 | (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 871 | pr_err("received unsupported SRP_CMD request" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 872 | " type (%u out + %u in != %u / %zu)\n", |
| 873 | srp_cmd->data_out_desc_cnt, |
| 874 | srp_cmd->data_in_desc_cnt, |
| 875 | be32_to_cpu(idb->table_desc.len), |
| 876 | sizeof(*db)); |
| 877 | ioctx->n_rbuf = 0; |
| 878 | ret = -EINVAL; |
| 879 | goto out; |
| 880 | } |
| 881 | |
| 882 | if (ioctx->n_rbuf == 1) |
| 883 | ioctx->rbufs = &ioctx->single_rbuf; |
| 884 | else { |
| 885 | ioctx->rbufs = |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 886 | kmalloc(ioctx->n_rbuf * sizeof(*db), GFP_ATOMIC); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 887 | if (!ioctx->rbufs) { |
| 888 | ioctx->n_rbuf = 0; |
| 889 | ret = -ENOMEM; |
| 890 | goto out; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | db = idb->desc_list; |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 895 | memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof(*db)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 896 | *data_len = be32_to_cpu(idb->len); |
| 897 | } |
| 898 | out: |
| 899 | return ret; |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * srpt_init_ch_qp() - Initialize queue pair attributes. |
| 904 | * |
| 905 | * Initialized the attributes of queue pair 'qp' by allowing local write, |
| 906 | * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT. |
| 907 | */ |
| 908 | static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp) |
| 909 | { |
| 910 | struct ib_qp_attr *attr; |
| 911 | int ret; |
| 912 | |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 913 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 914 | if (!attr) |
| 915 | return -ENOMEM; |
| 916 | |
| 917 | attr->qp_state = IB_QPS_INIT; |
| 918 | attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ | |
| 919 | IB_ACCESS_REMOTE_WRITE; |
| 920 | attr->port_num = ch->sport->port; |
| 921 | attr->pkey_index = 0; |
| 922 | |
| 923 | ret = ib_modify_qp(qp, attr, |
| 924 | IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT | |
| 925 | IB_QP_PKEY_INDEX); |
| 926 | |
| 927 | kfree(attr); |
| 928 | return ret; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR). |
| 933 | * @ch: channel of the queue pair. |
| 934 | * @qp: queue pair to change the state of. |
| 935 | * |
| 936 | * Returns zero upon success and a negative value upon failure. |
| 937 | * |
| 938 | * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. |
| 939 | * If this structure ever becomes larger, it might be necessary to allocate |
| 940 | * it dynamically instead of on the stack. |
| 941 | */ |
| 942 | static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp) |
| 943 | { |
| 944 | struct ib_qp_attr qp_attr; |
| 945 | int attr_mask; |
| 946 | int ret; |
| 947 | |
| 948 | qp_attr.qp_state = IB_QPS_RTR; |
| 949 | ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask); |
| 950 | if (ret) |
| 951 | goto out; |
| 952 | |
| 953 | qp_attr.max_dest_rd_atomic = 4; |
| 954 | |
| 955 | ret = ib_modify_qp(qp, &qp_attr, attr_mask); |
| 956 | |
| 957 | out: |
| 958 | return ret; |
| 959 | } |
| 960 | |
| 961 | /** |
| 962 | * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS). |
| 963 | * @ch: channel of the queue pair. |
| 964 | * @qp: queue pair to change the state of. |
| 965 | * |
| 966 | * Returns zero upon success and a negative value upon failure. |
| 967 | * |
| 968 | * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. |
| 969 | * If this structure ever becomes larger, it might be necessary to allocate |
| 970 | * it dynamically instead of on the stack. |
| 971 | */ |
| 972 | static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp) |
| 973 | { |
| 974 | struct ib_qp_attr qp_attr; |
| 975 | int attr_mask; |
| 976 | int ret; |
| 977 | |
| 978 | qp_attr.qp_state = IB_QPS_RTS; |
| 979 | ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask); |
| 980 | if (ret) |
| 981 | goto out; |
| 982 | |
| 983 | qp_attr.max_rd_atomic = 4; |
| 984 | |
| 985 | ret = ib_modify_qp(qp, &qp_attr, attr_mask); |
| 986 | |
| 987 | out: |
| 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * srpt_ch_qp_err() - Set the channel queue pair state to 'error'. |
| 993 | */ |
| 994 | static int srpt_ch_qp_err(struct srpt_rdma_ch *ch) |
| 995 | { |
| 996 | struct ib_qp_attr qp_attr; |
| 997 | |
| 998 | qp_attr.qp_state = IB_QPS_ERR; |
| 999 | return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE); |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list. |
| 1004 | */ |
| 1005 | static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch, |
| 1006 | struct srpt_send_ioctx *ioctx) |
| 1007 | { |
| 1008 | struct scatterlist *sg; |
| 1009 | enum dma_data_direction dir; |
| 1010 | |
| 1011 | BUG_ON(!ch); |
| 1012 | BUG_ON(!ioctx); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1013 | BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1014 | |
| 1015 | while (ioctx->n_rdma) |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1016 | kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1017 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1018 | kfree(ioctx->rdma_wrs); |
| 1019 | ioctx->rdma_wrs = NULL; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1020 | |
| 1021 | if (ioctx->mapped_sg_count) { |
| 1022 | sg = ioctx->sg; |
| 1023 | WARN_ON(!sg); |
| 1024 | dir = ioctx->cmd.data_direction; |
| 1025 | BUG_ON(dir == DMA_NONE); |
| 1026 | ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt, |
Bart Van Assche | 671ec1b | 2016-02-11 11:05:01 -0800 | [diff] [blame] | 1027 | target_reverse_dma_direction(&ioctx->cmd)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1028 | ioctx->mapped_sg_count = 0; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list. |
| 1034 | */ |
| 1035 | static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch, |
| 1036 | struct srpt_send_ioctx *ioctx) |
| 1037 | { |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1038 | struct ib_device *dev = ch->sport->sdev->device; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1039 | struct se_cmd *cmd; |
| 1040 | struct scatterlist *sg, *sg_orig; |
| 1041 | int sg_cnt; |
| 1042 | enum dma_data_direction dir; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1043 | struct ib_rdma_wr *riu; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1044 | struct srp_direct_buf *db; |
| 1045 | dma_addr_t dma_addr; |
| 1046 | struct ib_sge *sge; |
| 1047 | u64 raddr; |
| 1048 | u32 rsize; |
| 1049 | u32 tsize; |
| 1050 | u32 dma_len; |
| 1051 | int count, nrdma; |
| 1052 | int i, j, k; |
| 1053 | |
| 1054 | BUG_ON(!ch); |
| 1055 | BUG_ON(!ioctx); |
| 1056 | cmd = &ioctx->cmd; |
| 1057 | dir = cmd->data_direction; |
| 1058 | BUG_ON(dir == DMA_NONE); |
| 1059 | |
Roland Dreier | 6f9e7f0 | 2012-03-30 11:29:12 -0700 | [diff] [blame] | 1060 | ioctx->sg = sg = sg_orig = cmd->t_data_sg; |
| 1061 | ioctx->sg_cnt = sg_cnt = cmd->t_data_nents; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1062 | |
| 1063 | count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt, |
Bart Van Assche | 671ec1b | 2016-02-11 11:05:01 -0800 | [diff] [blame] | 1064 | target_reverse_dma_direction(cmd)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1065 | if (unlikely(!count)) |
| 1066 | return -EAGAIN; |
| 1067 | |
| 1068 | ioctx->mapped_sg_count = count; |
| 1069 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1070 | if (ioctx->rdma_wrs && ioctx->n_rdma_wrs) |
| 1071 | nrdma = ioctx->n_rdma_wrs; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1072 | else { |
| 1073 | nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE |
| 1074 | + ioctx->n_rbuf; |
| 1075 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1076 | ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs), |
| 1077 | GFP_KERNEL); |
| 1078 | if (!ioctx->rdma_wrs) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1079 | goto free_mem; |
| 1080 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1081 | ioctx->n_rdma_wrs = nrdma; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | db = ioctx->rbufs; |
| 1085 | tsize = cmd->data_length; |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1086 | dma_len = ib_sg_dma_len(dev, &sg[0]); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1087 | riu = ioctx->rdma_wrs; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1088 | |
| 1089 | /* |
| 1090 | * For each remote desc - calculate the #ib_sge. |
| 1091 | * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then |
| 1092 | * each remote desc rdma_iu is required a rdma wr; |
| 1093 | * else |
| 1094 | * we need to allocate extra rdma_iu to carry extra #ib_sge in |
| 1095 | * another rdma wr |
| 1096 | */ |
| 1097 | for (i = 0, j = 0; |
| 1098 | j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) { |
| 1099 | rsize = be32_to_cpu(db->len); |
| 1100 | raddr = be64_to_cpu(db->va); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1101 | riu->remote_addr = raddr; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1102 | riu->rkey = be32_to_cpu(db->key); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1103 | riu->wr.num_sge = 0; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1104 | |
| 1105 | /* calculate how many sge required for this remote_buf */ |
| 1106 | while (rsize > 0 && tsize > 0) { |
| 1107 | |
| 1108 | if (rsize >= dma_len) { |
| 1109 | tsize -= dma_len; |
| 1110 | rsize -= dma_len; |
| 1111 | raddr += dma_len; |
| 1112 | |
| 1113 | if (tsize > 0) { |
| 1114 | ++j; |
| 1115 | if (j < count) { |
| 1116 | sg = sg_next(sg); |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1117 | dma_len = ib_sg_dma_len( |
| 1118 | dev, sg); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | } else { |
| 1122 | tsize -= rsize; |
| 1123 | dma_len -= rsize; |
| 1124 | rsize = 0; |
| 1125 | } |
| 1126 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1127 | ++riu->wr.num_sge; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1128 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1129 | if (rsize > 0 && |
| 1130 | riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1131 | ++ioctx->n_rdma; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1132 | riu->wr.sg_list = kmalloc_array(riu->wr.num_sge, |
| 1133 | sizeof(*riu->wr.sg_list), |
| 1134 | GFP_KERNEL); |
| 1135 | if (!riu->wr.sg_list) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1136 | goto free_mem; |
| 1137 | |
| 1138 | ++riu; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1139 | riu->wr.num_sge = 0; |
| 1140 | riu->remote_addr = raddr; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1141 | riu->rkey = be32_to_cpu(db->key); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | ++ioctx->n_rdma; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1146 | riu->wr.sg_list = kmalloc_array(riu->wr.num_sge, |
| 1147 | sizeof(*riu->wr.sg_list), |
| 1148 | GFP_KERNEL); |
| 1149 | if (!riu->wr.sg_list) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1150 | goto free_mem; |
| 1151 | } |
| 1152 | |
| 1153 | db = ioctx->rbufs; |
| 1154 | tsize = cmd->data_length; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1155 | riu = ioctx->rdma_wrs; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1156 | sg = sg_orig; |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1157 | dma_len = ib_sg_dma_len(dev, &sg[0]); |
| 1158 | dma_addr = ib_sg_dma_address(dev, &sg[0]); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1159 | |
| 1160 | /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */ |
| 1161 | for (i = 0, j = 0; |
| 1162 | j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) { |
| 1163 | rsize = be32_to_cpu(db->len); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1164 | sge = riu->wr.sg_list; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1165 | k = 0; |
| 1166 | |
| 1167 | while (rsize > 0 && tsize > 0) { |
| 1168 | sge->addr = dma_addr; |
Jason Gunthorpe | 5a78395 | 2015-07-30 17:22:24 -0600 | [diff] [blame] | 1169 | sge->lkey = ch->sport->sdev->pd->local_dma_lkey; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1170 | |
| 1171 | if (rsize >= dma_len) { |
| 1172 | sge->length = |
| 1173 | (tsize < dma_len) ? tsize : dma_len; |
| 1174 | tsize -= dma_len; |
| 1175 | rsize -= dma_len; |
| 1176 | |
| 1177 | if (tsize > 0) { |
| 1178 | ++j; |
| 1179 | if (j < count) { |
| 1180 | sg = sg_next(sg); |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1181 | dma_len = ib_sg_dma_len( |
| 1182 | dev, sg); |
| 1183 | dma_addr = ib_sg_dma_address( |
| 1184 | dev, sg); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | } else { |
| 1188 | sge->length = (tsize < rsize) ? tsize : rsize; |
| 1189 | tsize -= rsize; |
| 1190 | dma_len -= rsize; |
| 1191 | dma_addr += rsize; |
| 1192 | rsize = 0; |
| 1193 | } |
| 1194 | |
| 1195 | ++k; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1196 | if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1197 | ++riu; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1198 | sge = riu->wr.sg_list; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1199 | k = 0; |
| 1200 | } else if (rsize > 0 && tsize > 0) |
| 1201 | ++sge; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | return 0; |
| 1206 | |
| 1207 | free_mem: |
| 1208 | srpt_unmap_sg_to_ib_sge(ch, ioctx); |
| 1209 | |
| 1210 | return -ENOMEM; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator. |
| 1215 | */ |
| 1216 | static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch) |
| 1217 | { |
| 1218 | struct srpt_send_ioctx *ioctx; |
| 1219 | unsigned long flags; |
| 1220 | |
| 1221 | BUG_ON(!ch); |
| 1222 | |
| 1223 | ioctx = NULL; |
| 1224 | spin_lock_irqsave(&ch->spinlock, flags); |
| 1225 | if (!list_empty(&ch->free_list)) { |
| 1226 | ioctx = list_first_entry(&ch->free_list, |
| 1227 | struct srpt_send_ioctx, free_list); |
| 1228 | list_del(&ioctx->free_list); |
| 1229 | } |
| 1230 | spin_unlock_irqrestore(&ch->spinlock, flags); |
| 1231 | |
| 1232 | if (!ioctx) |
| 1233 | return ioctx; |
| 1234 | |
| 1235 | BUG_ON(ioctx->ch != ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1236 | spin_lock_init(&ioctx->spinlock); |
| 1237 | ioctx->state = SRPT_STATE_NEW; |
| 1238 | ioctx->n_rbuf = 0; |
| 1239 | ioctx->rbufs = NULL; |
| 1240 | ioctx->n_rdma = 0; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1241 | ioctx->n_rdma_wrs = 0; |
| 1242 | ioctx->rdma_wrs = NULL; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1243 | ioctx->mapped_sg_count = 0; |
| 1244 | init_completion(&ioctx->tx_done); |
| 1245 | ioctx->queue_status_only = false; |
| 1246 | /* |
| 1247 | * transport_init_se_cmd() does not initialize all fields, so do it |
| 1248 | * here. |
| 1249 | */ |
| 1250 | memset(&ioctx->cmd, 0, sizeof(ioctx->cmd)); |
| 1251 | memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data)); |
| 1252 | |
| 1253 | return ioctx; |
| 1254 | } |
| 1255 | |
| 1256 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1257 | * srpt_abort_cmd() - Abort a SCSI command. |
| 1258 | * @ioctx: I/O context associated with the SCSI command. |
| 1259 | * @context: Preferred execution context. |
| 1260 | */ |
| 1261 | static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx) |
| 1262 | { |
| 1263 | enum srpt_command_state state; |
| 1264 | unsigned long flags; |
| 1265 | |
| 1266 | BUG_ON(!ioctx); |
| 1267 | |
| 1268 | /* |
| 1269 | * If the command is in a state where the target core is waiting for |
| 1270 | * the ib_srpt driver, change the state to the next state. Changing |
| 1271 | * the state of the command from SRPT_STATE_NEED_DATA to |
| 1272 | * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this |
| 1273 | * function a second time. |
| 1274 | */ |
| 1275 | |
| 1276 | spin_lock_irqsave(&ioctx->spinlock, flags); |
| 1277 | state = ioctx->state; |
| 1278 | switch (state) { |
| 1279 | case SRPT_STATE_NEED_DATA: |
| 1280 | ioctx->state = SRPT_STATE_DATA_IN; |
| 1281 | break; |
| 1282 | case SRPT_STATE_DATA_IN: |
| 1283 | case SRPT_STATE_CMD_RSP_SENT: |
| 1284 | case SRPT_STATE_MGMT_RSP_SENT: |
| 1285 | ioctx->state = SRPT_STATE_DONE; |
| 1286 | break; |
| 1287 | default: |
| 1288 | break; |
| 1289 | } |
| 1290 | spin_unlock_irqrestore(&ioctx->spinlock, flags); |
| 1291 | |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1292 | if (state == SRPT_STATE_DONE) { |
| 1293 | struct srpt_rdma_ch *ch = ioctx->ch; |
| 1294 | |
| 1295 | BUG_ON(ch->sess == NULL); |
| 1296 | |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 1297 | target_put_sess_cmd(&ioctx->cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1298 | goto out; |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1299 | } |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1300 | |
| 1301 | pr_debug("Aborting cmd with state %d and tag %lld\n", state, |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 1302 | ioctx->cmd.tag); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1303 | |
| 1304 | switch (state) { |
| 1305 | case SRPT_STATE_NEW: |
| 1306 | case SRPT_STATE_DATA_IN: |
| 1307 | case SRPT_STATE_MGMT: |
| 1308 | /* |
| 1309 | * Do nothing - defer abort processing until |
| 1310 | * srpt_queue_response() is invoked. |
| 1311 | */ |
| 1312 | WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false)); |
| 1313 | break; |
| 1314 | case SRPT_STATE_NEED_DATA: |
| 1315 | /* DMA_TO_DEVICE (write) - RDMA read error. */ |
Christoph Hellwig | e672a47 | 2012-07-08 15:58:43 -0400 | [diff] [blame] | 1316 | |
| 1317 | /* XXX(hch): this is a horrible layering violation.. */ |
Christoph Hellwig | 7d680f3b | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 1318 | spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags); |
Christoph Hellwig | e672a47 | 2012-07-08 15:58:43 -0400 | [diff] [blame] | 1319 | ioctx->cmd.transport_state &= ~CMD_T_ACTIVE; |
Christoph Hellwig | 7d680f3b | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 1320 | spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1321 | break; |
| 1322 | case SRPT_STATE_CMD_RSP_SENT: |
| 1323 | /* |
| 1324 | * SRP_RSP sending failed or the SRP_RSP send completion has |
| 1325 | * not been received in time. |
| 1326 | */ |
| 1327 | srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx); |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 1328 | target_put_sess_cmd(&ioctx->cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | case SRPT_STATE_MGMT_RSP_SENT: |
| 1331 | srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 1332 | target_put_sess_cmd(&ioctx->cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1333 | break; |
| 1334 | default: |
Grant Grundler | 532ec6f | 2013-03-26 21:48:28 +0000 | [diff] [blame] | 1335 | WARN(1, "Unexpected command state (%d)", state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1336 | break; |
| 1337 | } |
| 1338 | |
| 1339 | out: |
| 1340 | return state; |
| 1341 | } |
| 1342 | |
| 1343 | /** |
Christoph Hellwig | e672a47 | 2012-07-08 15:58:43 -0400 | [diff] [blame] | 1344 | * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping |
| 1345 | * the data that has been transferred via IB RDMA had to be postponed until the |
Masanari Iida | 142ad5d | 2012-08-10 00:07:58 +0000 | [diff] [blame] | 1346 | * check_stop_free() callback. None of this is necessary anymore and needs to |
Christoph Hellwig | e672a47 | 2012-07-08 15:58:43 -0400 | [diff] [blame] | 1347 | * be cleaned up. |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1348 | */ |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1349 | static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1350 | { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1351 | struct srpt_rdma_ch *ch = cq->cq_context; |
| 1352 | struct srpt_send_ioctx *ioctx = |
Bart Van Assche | 19f5729 | 2015-12-31 09:56:43 +0100 | [diff] [blame] | 1353 | container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1354 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1355 | WARN_ON(ioctx->n_rdma <= 0); |
| 1356 | atomic_add(ioctx->n_rdma, &ch->sq_wr_avail); |
| 1357 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1358 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 1359 | pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n", |
| 1360 | ioctx, wc->status); |
| 1361 | srpt_abort_cmd(ioctx); |
| 1362 | return; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1363 | } |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1364 | |
| 1365 | if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA, |
| 1366 | SRPT_STATE_DATA_IN)) |
| 1367 | target_execute_cmd(&ioctx->cmd); |
| 1368 | else |
| 1369 | pr_err("%s[%d]: wrong state = %d\n", __func__, |
| 1370 | __LINE__, srpt_get_cmd_state(ioctx)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1373 | static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1374 | { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1375 | struct srpt_send_ioctx *ioctx = |
Bart Van Assche | 19f5729 | 2015-12-31 09:56:43 +0100 | [diff] [blame] | 1376 | container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1377 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1378 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 1379 | pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n", |
| 1380 | ioctx, wc->status); |
| 1381 | srpt_abort_cmd(ioctx); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | /** |
| 1386 | * srpt_build_cmd_rsp() - Build an SRP_RSP response. |
| 1387 | * @ch: RDMA channel through which the request has been received. |
| 1388 | * @ioctx: I/O context associated with the SRP_CMD request. The response will |
| 1389 | * be built in the buffer ioctx->buf points at and hence this function will |
| 1390 | * overwrite the request data. |
| 1391 | * @tag: tag of the request for which this response is being generated. |
| 1392 | * @status: value for the STATUS field of the SRP_RSP information unit. |
| 1393 | * |
| 1394 | * Returns the size in bytes of the SRP_RSP response. |
| 1395 | * |
| 1396 | * An SRP_RSP response contains a SCSI status or service response. See also |
| 1397 | * section 6.9 in the SRP r16a document for the format of an SRP_RSP |
| 1398 | * response. See also SPC-2 for more information about sense data. |
| 1399 | */ |
| 1400 | static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch, |
| 1401 | struct srpt_send_ioctx *ioctx, u64 tag, |
| 1402 | int status) |
| 1403 | { |
| 1404 | struct srp_rsp *srp_rsp; |
| 1405 | const u8 *sense_data; |
| 1406 | int sense_data_len, max_sense_len; |
| 1407 | |
| 1408 | /* |
| 1409 | * The lowest bit of all SAM-3 status codes is zero (see also |
| 1410 | * paragraph 5.3 in SAM-3). |
| 1411 | */ |
| 1412 | WARN_ON(status & 1); |
| 1413 | |
| 1414 | srp_rsp = ioctx->ioctx.buf; |
| 1415 | BUG_ON(!srp_rsp); |
| 1416 | |
| 1417 | sense_data = ioctx->sense_data; |
| 1418 | sense_data_len = ioctx->cmd.scsi_sense_length; |
| 1419 | WARN_ON(sense_data_len > sizeof(ioctx->sense_data)); |
| 1420 | |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 1421 | memset(srp_rsp, 0, sizeof(*srp_rsp)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1422 | srp_rsp->opcode = SRP_RSP; |
| 1423 | srp_rsp->req_lim_delta = |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 1424 | cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1425 | srp_rsp->tag = tag; |
| 1426 | srp_rsp->status = status; |
| 1427 | |
| 1428 | if (sense_data_len) { |
| 1429 | BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp)); |
| 1430 | max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp); |
| 1431 | if (sense_data_len > max_sense_len) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1432 | pr_warn("truncated sense data from %d to %d" |
| 1433 | " bytes\n", sense_data_len, max_sense_len); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1434 | sense_data_len = max_sense_len; |
| 1435 | } |
| 1436 | |
| 1437 | srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID; |
| 1438 | srp_rsp->sense_data_len = cpu_to_be32(sense_data_len); |
| 1439 | memcpy(srp_rsp + 1, sense_data, sense_data_len); |
| 1440 | } |
| 1441 | |
| 1442 | return sizeof(*srp_rsp) + sense_data_len; |
| 1443 | } |
| 1444 | |
| 1445 | /** |
| 1446 | * srpt_build_tskmgmt_rsp() - Build a task management response. |
| 1447 | * @ch: RDMA channel through which the request has been received. |
| 1448 | * @ioctx: I/O context in which the SRP_RSP response will be built. |
| 1449 | * @rsp_code: RSP_CODE that will be stored in the response. |
| 1450 | * @tag: Tag of the request for which this response is being generated. |
| 1451 | * |
| 1452 | * Returns the size in bytes of the SRP_RSP response. |
| 1453 | * |
| 1454 | * An SRP_RSP response contains a SCSI status or service response. See also |
| 1455 | * section 6.9 in the SRP r16a document for the format of an SRP_RSP |
| 1456 | * response. |
| 1457 | */ |
| 1458 | static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch, |
| 1459 | struct srpt_send_ioctx *ioctx, |
| 1460 | u8 rsp_code, u64 tag) |
| 1461 | { |
| 1462 | struct srp_rsp *srp_rsp; |
| 1463 | int resp_data_len; |
| 1464 | int resp_len; |
| 1465 | |
Jack Wang | c807f64 | 2013-09-30 10:09:05 +0200 | [diff] [blame] | 1466 | resp_data_len = 4; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1467 | resp_len = sizeof(*srp_rsp) + resp_data_len; |
| 1468 | |
| 1469 | srp_rsp = ioctx->ioctx.buf; |
| 1470 | BUG_ON(!srp_rsp); |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 1471 | memset(srp_rsp, 0, sizeof(*srp_rsp)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1472 | |
| 1473 | srp_rsp->opcode = SRP_RSP; |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 1474 | srp_rsp->req_lim_delta = |
| 1475 | cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1476 | srp_rsp->tag = tag; |
| 1477 | |
Jack Wang | c807f64 | 2013-09-30 10:09:05 +0200 | [diff] [blame] | 1478 | srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID; |
| 1479 | srp_rsp->resp_data_len = cpu_to_be32(resp_data_len); |
| 1480 | srp_rsp->data[3] = rsp_code; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1481 | |
| 1482 | return resp_len; |
| 1483 | } |
| 1484 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1485 | static int srpt_check_stop_free(struct se_cmd *cmd) |
| 1486 | { |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1487 | struct srpt_send_ioctx *ioctx = container_of(cmd, |
| 1488 | struct srpt_send_ioctx, cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1489 | |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 1490 | return target_put_sess_cmd(&ioctx->cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | /** |
| 1494 | * srpt_handle_cmd() - Process SRP_CMD. |
| 1495 | */ |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame^] | 1496 | static void srpt_handle_cmd(struct srpt_rdma_ch *ch, |
| 1497 | struct srpt_recv_ioctx *recv_ioctx, |
| 1498 | struct srpt_send_ioctx *send_ioctx) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1499 | { |
| 1500 | struct se_cmd *cmd; |
| 1501 | struct srp_cmd *srp_cmd; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1502 | u64 data_len; |
| 1503 | enum dma_data_direction dir; |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1504 | int rc; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1505 | |
| 1506 | BUG_ON(!send_ioctx); |
| 1507 | |
| 1508 | srp_cmd = recv_ioctx->ioctx.buf; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1509 | cmd = &send_ioctx->cmd; |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 1510 | cmd->tag = srp_cmd->tag; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1511 | |
| 1512 | switch (srp_cmd->task_attr) { |
| 1513 | case SRP_CMD_SIMPLE_Q: |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1514 | cmd->sam_task_attr = TCM_SIMPLE_TAG; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1515 | break; |
| 1516 | case SRP_CMD_ORDERED_Q: |
| 1517 | default: |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1518 | cmd->sam_task_attr = TCM_ORDERED_TAG; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1519 | break; |
| 1520 | case SRP_CMD_HEAD_OF_Q: |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1521 | cmd->sam_task_attr = TCM_HEAD_TAG; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1522 | break; |
| 1523 | case SRP_CMD_ACA: |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1524 | cmd->sam_task_attr = TCM_ACA_TAG; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1525 | break; |
| 1526 | } |
| 1527 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1528 | if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1529 | pr_err("0x%llx: parsing SRP descriptor table failed.\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1530 | srp_cmd->tag); |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame^] | 1531 | goto release_ioctx; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1534 | rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb, |
Bart Van Assche | e1dd413 | 2016-02-11 11:05:19 -0800 | [diff] [blame] | 1535 | &send_ioctx->sense_data[0], |
| 1536 | scsilun_to_int(&srp_cmd->lun), data_len, |
| 1537 | TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF); |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1538 | if (rc != 0) { |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame^] | 1539 | pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc, |
| 1540 | srp_cmd->tag); |
| 1541 | goto release_ioctx; |
Nicholas Bellinger | 187e70a | 2012-03-17 20:12:36 -0700 | [diff] [blame] | 1542 | } |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame^] | 1543 | return; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1544 | |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame^] | 1545 | release_ioctx: |
| 1546 | send_ioctx->state = SRPT_STATE_DONE; |
| 1547 | srpt_release_cmd(cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1550 | static int srp_tmr_to_tcm(int fn) |
| 1551 | { |
| 1552 | switch (fn) { |
| 1553 | case SRP_TSK_ABORT_TASK: |
| 1554 | return TMR_ABORT_TASK; |
| 1555 | case SRP_TSK_ABORT_TASK_SET: |
| 1556 | return TMR_ABORT_TASK_SET; |
| 1557 | case SRP_TSK_CLEAR_TASK_SET: |
| 1558 | return TMR_CLEAR_TASK_SET; |
| 1559 | case SRP_TSK_LUN_RESET: |
| 1560 | return TMR_LUN_RESET; |
| 1561 | case SRP_TSK_CLEAR_ACA: |
| 1562 | return TMR_CLEAR_ACA; |
| 1563 | default: |
| 1564 | return -1; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | /** |
| 1569 | * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit. |
| 1570 | * |
| 1571 | * Returns 0 if and only if the request will be processed by the target core. |
| 1572 | * |
| 1573 | * For more information about SRP_TSK_MGMT information units, see also section |
| 1574 | * 6.7 in the SRP r16a document. |
| 1575 | */ |
| 1576 | static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch, |
| 1577 | struct srpt_recv_ioctx *recv_ioctx, |
| 1578 | struct srpt_send_ioctx *send_ioctx) |
| 1579 | { |
| 1580 | struct srp_tsk_mgmt *srp_tsk; |
| 1581 | struct se_cmd *cmd; |
Nicholas Bellinger | 3e4f574 | 2012-11-28 01:38:04 -0800 | [diff] [blame] | 1582 | struct se_session *sess = ch->sess; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1583 | int tcm_tmr; |
Nicholas Bellinger | 3e4f574 | 2012-11-28 01:38:04 -0800 | [diff] [blame] | 1584 | int rc; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1585 | |
| 1586 | BUG_ON(!send_ioctx); |
| 1587 | |
| 1588 | srp_tsk = recv_ioctx->ioctx.buf; |
| 1589 | cmd = &send_ioctx->cmd; |
| 1590 | |
| 1591 | pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld" |
| 1592 | " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func, |
| 1593 | srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess); |
| 1594 | |
| 1595 | srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT); |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 1596 | send_ioctx->cmd.tag = srp_tsk->tag; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1597 | tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func); |
Bart Van Assche | e1dd413 | 2016-02-11 11:05:19 -0800 | [diff] [blame] | 1598 | rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, |
| 1599 | scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr, |
| 1600 | GFP_KERNEL, srp_tsk->task_tag, |
| 1601 | TARGET_SCF_ACK_KREF); |
Nicholas Bellinger | 3e4f574 | 2012-11-28 01:38:04 -0800 | [diff] [blame] | 1602 | if (rc != 0) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1603 | send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1604 | goto fail; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1605 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1606 | return; |
| 1607 | fail: |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1608 | transport_send_check_condition_and_sense(cmd, 0, 0); // XXX: |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | /** |
| 1612 | * srpt_handle_new_iu() - Process a newly received information unit. |
| 1613 | * @ch: RDMA channel through which the information unit has been received. |
| 1614 | * @ioctx: SRPT I/O context associated with the information unit. |
| 1615 | */ |
| 1616 | static void srpt_handle_new_iu(struct srpt_rdma_ch *ch, |
| 1617 | struct srpt_recv_ioctx *recv_ioctx, |
| 1618 | struct srpt_send_ioctx *send_ioctx) |
| 1619 | { |
| 1620 | struct srp_cmd *srp_cmd; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1621 | |
| 1622 | BUG_ON(!ch); |
| 1623 | BUG_ON(!recv_ioctx); |
| 1624 | |
| 1625 | ib_dma_sync_single_for_cpu(ch->sport->sdev->device, |
| 1626 | recv_ioctx->ioctx.dma, srp_max_req_size, |
| 1627 | DMA_FROM_DEVICE); |
| 1628 | |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 1629 | if (unlikely(ch->state == CH_CONNECTING)) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1630 | list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list); |
| 1631 | goto out; |
| 1632 | } |
| 1633 | |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 1634 | if (unlikely(ch->state != CH_LIVE)) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1635 | goto out; |
| 1636 | |
| 1637 | srp_cmd = recv_ioctx->ioctx.buf; |
| 1638 | if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) { |
| 1639 | if (!send_ioctx) |
| 1640 | send_ioctx = srpt_get_send_ioctx(ch); |
| 1641 | if (unlikely(!send_ioctx)) { |
| 1642 | list_add_tail(&recv_ioctx->wait_list, |
| 1643 | &ch->cmd_wait_list); |
| 1644 | goto out; |
| 1645 | } |
| 1646 | } |
| 1647 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1648 | switch (srp_cmd->opcode) { |
| 1649 | case SRP_CMD: |
| 1650 | srpt_handle_cmd(ch, recv_ioctx, send_ioctx); |
| 1651 | break; |
| 1652 | case SRP_TSK_MGMT: |
| 1653 | srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx); |
| 1654 | break; |
| 1655 | case SRP_I_LOGOUT: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1656 | pr_err("Not yet implemented: SRP_I_LOGOUT\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1657 | break; |
| 1658 | case SRP_CRED_RSP: |
| 1659 | pr_debug("received SRP_CRED_RSP\n"); |
| 1660 | break; |
| 1661 | case SRP_AER_RSP: |
| 1662 | pr_debug("received SRP_AER_RSP\n"); |
| 1663 | break; |
| 1664 | case SRP_RSP: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1665 | pr_err("Received SRP_RSP\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1666 | break; |
| 1667 | default: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1668 | pr_err("received IU with unknown opcode 0x%x\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1669 | srp_cmd->opcode); |
| 1670 | break; |
| 1671 | } |
| 1672 | |
| 1673 | srpt_post_recv(ch->sport->sdev, recv_ioctx); |
| 1674 | out: |
| 1675 | return; |
| 1676 | } |
| 1677 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1678 | static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1679 | { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1680 | struct srpt_rdma_ch *ch = cq->cq_context; |
| 1681 | struct srpt_recv_ioctx *ioctx = |
| 1682 | container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1683 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1684 | if (wc->status == IB_WC_SUCCESS) { |
| 1685 | int req_lim; |
| 1686 | |
| 1687 | req_lim = atomic_dec_return(&ch->req_lim); |
| 1688 | if (unlikely(req_lim < 0)) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1689 | pr_err("req_lim = %d < 0\n", req_lim); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1690 | srpt_handle_new_iu(ch, ioctx, NULL); |
| 1691 | } else { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1692 | pr_info("receiving failed for ioctx %p with status %d\n", |
| 1693 | ioctx, wc->status); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1698 | * Note: Although this has not yet been observed during tests, at least in |
| 1699 | * theory it is possible that the srpt_get_send_ioctx() call invoked by |
| 1700 | * srpt_handle_new_iu() fails. This is possible because the req_lim_delta |
| 1701 | * value in each response is set to one, and it is possible that this response |
| 1702 | * makes the initiator send a new request before the send completion for that |
| 1703 | * response has been processed. This could e.g. happen if the call to |
| 1704 | * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or |
| 1705 | * if IB retransmission causes generation of the send completion to be |
| 1706 | * delayed. Incoming information units for which srpt_get_send_ioctx() fails |
| 1707 | * are queued on cmd_wait_list. The code below processes these delayed |
| 1708 | * requests one at a time. |
| 1709 | */ |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1710 | static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1711 | { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1712 | struct srpt_rdma_ch *ch = cq->cq_context; |
| 1713 | struct srpt_send_ioctx *ioctx = |
| 1714 | container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe); |
| 1715 | enum srpt_command_state state; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1716 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1717 | state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); |
| 1718 | |
| 1719 | WARN_ON(state != SRPT_STATE_CMD_RSP_SENT && |
| 1720 | state != SRPT_STATE_MGMT_RSP_SENT); |
| 1721 | |
| 1722 | atomic_inc(&ch->sq_wr_avail); |
| 1723 | |
| 1724 | if (wc->status != IB_WC_SUCCESS) { |
| 1725 | pr_info("sending response for ioctx 0x%p failed" |
| 1726 | " with status %d\n", ioctx, wc->status); |
| 1727 | |
| 1728 | atomic_dec(&ch->req_lim); |
| 1729 | srpt_abort_cmd(ioctx); |
| 1730 | goto out; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1733 | if (state != SRPT_STATE_DONE) { |
| 1734 | srpt_unmap_sg_to_ib_sge(ch, ioctx); |
| 1735 | transport_generic_free_cmd(&ioctx->cmd, 0); |
| 1736 | } else { |
| 1737 | pr_err("IB completion has been received too late for" |
| 1738 | " wr_id = %u.\n", ioctx->ioctx.index); |
| 1739 | } |
| 1740 | |
| 1741 | out: |
| 1742 | while (!list_empty(&ch->cmd_wait_list) && |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 1743 | ch->state == CH_LIVE && |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1744 | (ioctx = srpt_get_send_ioctx(ch)) != NULL) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1745 | struct srpt_recv_ioctx *recv_ioctx; |
| 1746 | |
| 1747 | recv_ioctx = list_first_entry(&ch->cmd_wait_list, |
| 1748 | struct srpt_recv_ioctx, |
| 1749 | wait_list); |
| 1750 | list_del(&recv_ioctx->wait_list); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1751 | srpt_handle_new_iu(ch, recv_ioctx, ioctx); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1752 | } |
| 1753 | } |
| 1754 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1755 | /** |
| 1756 | * srpt_create_ch_ib() - Create receive and send completion queues. |
| 1757 | */ |
| 1758 | static int srpt_create_ch_ib(struct srpt_rdma_ch *ch) |
| 1759 | { |
| 1760 | struct ib_qp_init_attr *qp_init; |
| 1761 | struct srpt_port *sport = ch->sport; |
| 1762 | struct srpt_device *sdev = sport->sdev; |
| 1763 | u32 srp_sq_size = sport->port_attrib.srp_sq_size; |
| 1764 | int ret; |
| 1765 | |
| 1766 | WARN_ON(ch->rq_size < 1); |
| 1767 | |
| 1768 | ret = -ENOMEM; |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 1769 | qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1770 | if (!qp_init) |
| 1771 | goto out; |
| 1772 | |
Bart Van Assche | ab477c1 | 2014-10-19 18:05:33 +0300 | [diff] [blame] | 1773 | retry: |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1774 | ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size, |
| 1775 | 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1776 | if (IS_ERR(ch->cq)) { |
| 1777 | ret = PTR_ERR(ch->cq); |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1778 | pr_err("failed to create CQ cqe= %d ret= %d\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1779 | ch->rq_size + srp_sq_size, ret); |
| 1780 | goto out; |
| 1781 | } |
| 1782 | |
| 1783 | qp_init->qp_context = (void *)ch; |
| 1784 | qp_init->event_handler |
| 1785 | = (void(*)(struct ib_event *, void*))srpt_qp_event; |
| 1786 | qp_init->send_cq = ch->cq; |
| 1787 | qp_init->recv_cq = ch->cq; |
| 1788 | qp_init->srq = sdev->srq; |
| 1789 | qp_init->sq_sig_type = IB_SIGNAL_REQ_WR; |
| 1790 | qp_init->qp_type = IB_QPT_RC; |
| 1791 | qp_init->cap.max_send_wr = srp_sq_size; |
| 1792 | qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE; |
| 1793 | |
| 1794 | ch->qp = ib_create_qp(sdev->pd, qp_init); |
| 1795 | if (IS_ERR(ch->qp)) { |
| 1796 | ret = PTR_ERR(ch->qp); |
Bart Van Assche | ab477c1 | 2014-10-19 18:05:33 +0300 | [diff] [blame] | 1797 | if (ret == -ENOMEM) { |
| 1798 | srp_sq_size /= 2; |
| 1799 | if (srp_sq_size >= MIN_SRPT_SQ_SIZE) { |
| 1800 | ib_destroy_cq(ch->cq); |
| 1801 | goto retry; |
| 1802 | } |
| 1803 | } |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1804 | pr_err("failed to create_qp ret= %d\n", ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1805 | goto err_destroy_cq; |
| 1806 | } |
| 1807 | |
| 1808 | atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr); |
| 1809 | |
| 1810 | pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n", |
| 1811 | __func__, ch->cq->cqe, qp_init->cap.max_send_sge, |
| 1812 | qp_init->cap.max_send_wr, ch->cm_id); |
| 1813 | |
| 1814 | ret = srpt_init_ch_qp(ch, ch->qp); |
| 1815 | if (ret) |
| 1816 | goto err_destroy_qp; |
| 1817 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1818 | out: |
| 1819 | kfree(qp_init); |
| 1820 | return ret; |
| 1821 | |
| 1822 | err_destroy_qp: |
| 1823 | ib_destroy_qp(ch->qp); |
| 1824 | err_destroy_cq: |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1825 | ib_free_cq(ch->cq); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1826 | goto out; |
| 1827 | } |
| 1828 | |
| 1829 | static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch) |
| 1830 | { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1831 | ib_destroy_qp(ch->qp); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1832 | ib_free_cq(ch->cq); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * __srpt_close_ch() - Close an RDMA channel by setting the QP error state. |
| 1837 | * |
| 1838 | * Reset the QP and make sure all resources associated with the channel will |
| 1839 | * be deallocated at an appropriate time. |
| 1840 | * |
| 1841 | * Note: The caller must hold ch->sport->sdev->spinlock. |
| 1842 | */ |
| 1843 | static void __srpt_close_ch(struct srpt_rdma_ch *ch) |
| 1844 | { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1845 | enum rdma_ch_state prev_state; |
| 1846 | unsigned long flags; |
| 1847 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1848 | spin_lock_irqsave(&ch->spinlock, flags); |
| 1849 | prev_state = ch->state; |
| 1850 | switch (prev_state) { |
| 1851 | case CH_CONNECTING: |
| 1852 | case CH_LIVE: |
| 1853 | ch->state = CH_DISCONNECTING; |
| 1854 | break; |
| 1855 | default: |
| 1856 | break; |
| 1857 | } |
| 1858 | spin_unlock_irqrestore(&ch->spinlock, flags); |
| 1859 | |
| 1860 | switch (prev_state) { |
| 1861 | case CH_CONNECTING: |
| 1862 | ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0, |
| 1863 | NULL, 0); |
| 1864 | /* fall through */ |
| 1865 | case CH_LIVE: |
| 1866 | if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1867 | pr_err("sending CM DREQ failed.\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1868 | break; |
| 1869 | case CH_DISCONNECTING: |
| 1870 | break; |
| 1871 | case CH_DRAINING: |
| 1872 | case CH_RELEASING: |
| 1873 | break; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | /** |
| 1878 | * srpt_close_ch() - Close an RDMA channel. |
| 1879 | */ |
| 1880 | static void srpt_close_ch(struct srpt_rdma_ch *ch) |
| 1881 | { |
| 1882 | struct srpt_device *sdev; |
| 1883 | |
| 1884 | sdev = ch->sport->sdev; |
| 1885 | spin_lock_irq(&sdev->spinlock); |
| 1886 | __srpt_close_ch(ch); |
| 1887 | spin_unlock_irq(&sdev->spinlock); |
| 1888 | } |
| 1889 | |
| 1890 | /** |
Nicholas Bellinger | 1d19f78 | 2013-05-15 01:30:01 -0700 | [diff] [blame] | 1891 | * srpt_shutdown_session() - Whether or not a session may be shut down. |
| 1892 | */ |
| 1893 | static int srpt_shutdown_session(struct se_session *se_sess) |
| 1894 | { |
Bart Van Assche | 8893625 | 2016-02-11 11:05:58 -0800 | [diff] [blame] | 1895 | return 1; |
Nicholas Bellinger | 1d19f78 | 2013-05-15 01:30:01 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1899 | * srpt_drain_channel() - Drain a channel by resetting the IB queue pair. |
| 1900 | * @cm_id: Pointer to the CM ID of the channel to be drained. |
| 1901 | * |
| 1902 | * Note: Must be called from inside srpt_cm_handler to avoid a race between |
| 1903 | * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one() |
| 1904 | * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one() |
| 1905 | * waits until all target sessions for the associated IB device have been |
| 1906 | * unregistered and target session registration involves a call to |
| 1907 | * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until |
| 1908 | * this function has finished). |
| 1909 | */ |
| 1910 | static void srpt_drain_channel(struct ib_cm_id *cm_id) |
| 1911 | { |
| 1912 | struct srpt_device *sdev; |
| 1913 | struct srpt_rdma_ch *ch; |
| 1914 | int ret; |
| 1915 | bool do_reset = false; |
| 1916 | |
| 1917 | WARN_ON_ONCE(irqs_disabled()); |
| 1918 | |
| 1919 | sdev = cm_id->context; |
| 1920 | BUG_ON(!sdev); |
| 1921 | spin_lock_irq(&sdev->spinlock); |
| 1922 | list_for_each_entry(ch, &sdev->rch_list, list) { |
| 1923 | if (ch->cm_id == cm_id) { |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 1924 | do_reset = srpt_set_ch_state(ch, CH_DRAINING); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1925 | break; |
| 1926 | } |
| 1927 | } |
| 1928 | spin_unlock_irq(&sdev->spinlock); |
| 1929 | |
| 1930 | if (do_reset) { |
Nicholas Bellinger | 1d19f78 | 2013-05-15 01:30:01 -0700 | [diff] [blame] | 1931 | if (ch->sess) |
| 1932 | srpt_shutdown_session(ch->sess); |
| 1933 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1934 | ret = srpt_ch_qp_err(ch); |
| 1935 | if (ret < 0) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1936 | pr_err("Setting queue pair in error state" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1937 | " failed: %d\n", ret); |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | /** |
| 1942 | * srpt_find_channel() - Look up an RDMA channel. |
| 1943 | * @cm_id: Pointer to the CM ID of the channel to be looked up. |
| 1944 | * |
| 1945 | * Return NULL if no matching RDMA channel has been found. |
| 1946 | */ |
| 1947 | static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev, |
| 1948 | struct ib_cm_id *cm_id) |
| 1949 | { |
| 1950 | struct srpt_rdma_ch *ch; |
| 1951 | bool found; |
| 1952 | |
| 1953 | WARN_ON_ONCE(irqs_disabled()); |
| 1954 | BUG_ON(!sdev); |
| 1955 | |
| 1956 | found = false; |
| 1957 | spin_lock_irq(&sdev->spinlock); |
| 1958 | list_for_each_entry(ch, &sdev->rch_list, list) { |
| 1959 | if (ch->cm_id == cm_id) { |
| 1960 | found = true; |
| 1961 | break; |
| 1962 | } |
| 1963 | } |
| 1964 | spin_unlock_irq(&sdev->spinlock); |
| 1965 | |
| 1966 | return found ? ch : NULL; |
| 1967 | } |
| 1968 | |
| 1969 | /** |
| 1970 | * srpt_release_channel() - Release channel resources. |
| 1971 | * |
| 1972 | * Schedules the actual release because: |
| 1973 | * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would |
| 1974 | * trigger a deadlock. |
| 1975 | * - It is not safe to call TCM transport_* functions from interrupt context. |
| 1976 | */ |
| 1977 | static void srpt_release_channel(struct srpt_rdma_ch *ch) |
| 1978 | { |
| 1979 | schedule_work(&ch->release_work); |
| 1980 | } |
| 1981 | |
| 1982 | static void srpt_release_channel_work(struct work_struct *w) |
| 1983 | { |
| 1984 | struct srpt_rdma_ch *ch; |
| 1985 | struct srpt_device *sdev; |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1986 | struct se_session *se_sess; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1987 | |
| 1988 | ch = container_of(w, struct srpt_rdma_ch, release_work); |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 1989 | pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name, |
| 1990 | ch->qp->qp_num, ch->release_done); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1991 | |
| 1992 | sdev = ch->sport->sdev; |
| 1993 | BUG_ON(!sdev); |
| 1994 | |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1995 | se_sess = ch->sess; |
| 1996 | BUG_ON(!se_sess); |
| 1997 | |
Bart Van Assche | 8893625 | 2016-02-11 11:05:58 -0800 | [diff] [blame] | 1998 | target_sess_cmd_list_set_waiting(se_sess); |
Joern Engel | be646c2d | 2013-05-15 00:44:07 -0700 | [diff] [blame] | 1999 | target_wait_for_sess_cmds(se_sess); |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 2000 | |
| 2001 | transport_deregister_session_configfs(se_sess); |
| 2002 | transport_deregister_session(se_sess); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2003 | ch->sess = NULL; |
| 2004 | |
Nicholas Bellinger | 0b41d6c | 2013-09-18 12:48:27 -0700 | [diff] [blame] | 2005 | ib_destroy_cm_id(ch->cm_id); |
| 2006 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2007 | srpt_destroy_ch_ib(ch); |
| 2008 | |
| 2009 | srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, |
| 2010 | ch->sport->sdev, ch->rq_size, |
| 2011 | ch->rsp_size, DMA_TO_DEVICE); |
| 2012 | |
| 2013 | spin_lock_irq(&sdev->spinlock); |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 2014 | list_del_init(&ch->list); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2015 | if (ch->release_done) |
| 2016 | complete(ch->release_done); |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 2017 | spin_unlock_irq(&sdev->spinlock); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2018 | |
| 2019 | wake_up(&sdev->ch_releaseQ); |
| 2020 | |
| 2021 | kfree(ch); |
| 2022 | } |
| 2023 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2024 | /** |
| 2025 | * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED. |
| 2026 | * |
| 2027 | * Ownership of the cm_id is transferred to the target session if this |
| 2028 | * functions returns zero. Otherwise the caller remains the owner of cm_id. |
| 2029 | */ |
| 2030 | static int srpt_cm_req_recv(struct ib_cm_id *cm_id, |
| 2031 | struct ib_cm_req_event_param *param, |
| 2032 | void *private_data) |
| 2033 | { |
| 2034 | struct srpt_device *sdev = cm_id->context; |
| 2035 | struct srpt_port *sport = &sdev->port[param->port - 1]; |
| 2036 | struct srp_login_req *req; |
| 2037 | struct srp_login_rsp *rsp; |
| 2038 | struct srp_login_rej *rej; |
| 2039 | struct ib_cm_rep_param *rep_param; |
| 2040 | struct srpt_rdma_ch *ch, *tmp_ch; |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2041 | struct se_node_acl *se_acl; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2042 | u32 it_iu_len; |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2043 | int i, ret = 0; |
| 2044 | unsigned char *p; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2045 | |
| 2046 | WARN_ON_ONCE(irqs_disabled()); |
| 2047 | |
| 2048 | if (WARN_ON(!sdev || !private_data)) |
| 2049 | return -EINVAL; |
| 2050 | |
| 2051 | req = (struct srp_login_req *)private_data; |
| 2052 | |
| 2053 | it_iu_len = be32_to_cpu(req->req_it_iu_len); |
| 2054 | |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2055 | pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx," |
| 2056 | " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d" |
| 2057 | " (guid=0x%llx:0x%llx)\n", |
| 2058 | be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]), |
| 2059 | be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]), |
| 2060 | be64_to_cpu(*(__be64 *)&req->target_port_id[0]), |
| 2061 | be64_to_cpu(*(__be64 *)&req->target_port_id[8]), |
| 2062 | it_iu_len, |
| 2063 | param->port, |
| 2064 | be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]), |
| 2065 | be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8])); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2066 | |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2067 | rsp = kzalloc(sizeof(*rsp), GFP_KERNEL); |
| 2068 | rej = kzalloc(sizeof(*rej), GFP_KERNEL); |
| 2069 | rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2070 | |
| 2071 | if (!rsp || !rej || !rep_param) { |
| 2072 | ret = -ENOMEM; |
| 2073 | goto out; |
| 2074 | } |
| 2075 | |
| 2076 | if (it_iu_len > srp_max_req_size || it_iu_len < 64) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2077 | rej->reason = cpu_to_be32( |
| 2078 | SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2079 | ret = -EINVAL; |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2080 | pr_err("rejected SRP_LOGIN_REQ because its" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2081 | " length (%d bytes) is out of range (%d .. %d)\n", |
| 2082 | it_iu_len, 64, srp_max_req_size); |
| 2083 | goto reject; |
| 2084 | } |
| 2085 | |
| 2086 | if (!sport->enabled) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2087 | rej->reason = cpu_to_be32( |
| 2088 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2089 | ret = -EINVAL; |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2090 | pr_err("rejected SRP_LOGIN_REQ because the target port" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2091 | " has not yet been enabled\n"); |
| 2092 | goto reject; |
| 2093 | } |
| 2094 | |
| 2095 | if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) { |
| 2096 | rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN; |
| 2097 | |
| 2098 | spin_lock_irq(&sdev->spinlock); |
| 2099 | |
| 2100 | list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) { |
| 2101 | if (!memcmp(ch->i_port_id, req->initiator_port_id, 16) |
| 2102 | && !memcmp(ch->t_port_id, req->target_port_id, 16) |
| 2103 | && param->port == ch->sport->port |
| 2104 | && param->listen_id == ch->sport->sdev->cm_id |
| 2105 | && ch->cm_id) { |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 2106 | if (ch->state != CH_CONNECTING |
| 2107 | && ch->state != CH_LIVE) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2108 | continue; |
| 2109 | |
| 2110 | /* found an existing channel */ |
| 2111 | pr_debug("Found existing channel %s" |
| 2112 | " cm_id= %p state= %d\n", |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 2113 | ch->sess_name, ch->cm_id, ch->state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2114 | |
| 2115 | __srpt_close_ch(ch); |
| 2116 | |
| 2117 | rsp->rsp_flags = |
| 2118 | SRP_LOGIN_RSP_MULTICHAN_TERMINATED; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | spin_unlock_irq(&sdev->spinlock); |
| 2123 | |
| 2124 | } else |
| 2125 | rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED; |
| 2126 | |
| 2127 | if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid) |
| 2128 | || *(__be64 *)(req->target_port_id + 8) != |
| 2129 | cpu_to_be64(srpt_service_guid)) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2130 | rej->reason = cpu_to_be32( |
| 2131 | SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2132 | ret = -ENOMEM; |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2133 | pr_err("rejected SRP_LOGIN_REQ because it" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2134 | " has an invalid target port identifier.\n"); |
| 2135 | goto reject; |
| 2136 | } |
| 2137 | |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2138 | ch = kzalloc(sizeof(*ch), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2139 | if (!ch) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2140 | rej->reason = cpu_to_be32( |
| 2141 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2142 | pr_err("rejected SRP_LOGIN_REQ because no memory.\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2143 | ret = -ENOMEM; |
| 2144 | goto reject; |
| 2145 | } |
| 2146 | |
| 2147 | INIT_WORK(&ch->release_work, srpt_release_channel_work); |
| 2148 | memcpy(ch->i_port_id, req->initiator_port_id, 16); |
| 2149 | memcpy(ch->t_port_id, req->target_port_id, 16); |
| 2150 | ch->sport = &sdev->port[param->port - 1]; |
| 2151 | ch->cm_id = cm_id; |
| 2152 | /* |
| 2153 | * Avoid QUEUE_FULL conditions by limiting the number of buffers used |
| 2154 | * for the SRP protocol to the command queue size. |
| 2155 | */ |
| 2156 | ch->rq_size = SRPT_RQ_SIZE; |
| 2157 | spin_lock_init(&ch->spinlock); |
| 2158 | ch->state = CH_CONNECTING; |
| 2159 | INIT_LIST_HEAD(&ch->cmd_wait_list); |
| 2160 | ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size; |
| 2161 | |
| 2162 | ch->ioctx_ring = (struct srpt_send_ioctx **) |
| 2163 | srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size, |
| 2164 | sizeof(*ch->ioctx_ring[0]), |
| 2165 | ch->rsp_size, DMA_TO_DEVICE); |
| 2166 | if (!ch->ioctx_ring) |
| 2167 | goto free_ch; |
| 2168 | |
| 2169 | INIT_LIST_HEAD(&ch->free_list); |
| 2170 | for (i = 0; i < ch->rq_size; i++) { |
| 2171 | ch->ioctx_ring[i]->ch = ch; |
| 2172 | list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list); |
| 2173 | } |
| 2174 | |
| 2175 | ret = srpt_create_ch_ib(ch); |
| 2176 | if (ret) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2177 | rej->reason = cpu_to_be32( |
| 2178 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2179 | pr_err("rejected SRP_LOGIN_REQ because creating" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2180 | " a new RDMA channel failed.\n"); |
| 2181 | goto free_ring; |
| 2182 | } |
| 2183 | |
| 2184 | ret = srpt_ch_qp_rtr(ch, ch->qp); |
| 2185 | if (ret) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2186 | rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2187 | pr_err("rejected SRP_LOGIN_REQ because enabling" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2188 | " RTR failed (error code = %d)\n", ret); |
| 2189 | goto destroy_ib; |
| 2190 | } |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2191 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2192 | /* |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2193 | * Use the initator port identifier as the session name, when |
| 2194 | * checking against se_node_acl->initiatorname[] this can be |
| 2195 | * with or without preceeding '0x'. |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2196 | */ |
| 2197 | snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx", |
| 2198 | be64_to_cpu(*(__be64 *)ch->i_port_id), |
| 2199 | be64_to_cpu(*(__be64 *)(ch->i_port_id + 8))); |
| 2200 | |
| 2201 | pr_debug("registering session %s\n", ch->sess_name); |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2202 | p = &ch->sess_name[0]; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2203 | |
Nicholas Bellinger | e70beee | 2014-04-02 12:52:38 -0700 | [diff] [blame] | 2204 | ch->sess = transport_init_session(TARGET_PROT_NORMAL); |
Dan Carpenter | 3af3363 | 2011-11-04 21:27:32 +0300 | [diff] [blame] | 2205 | if (IS_ERR(ch->sess)) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2206 | rej->reason = cpu_to_be32( |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2207 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2208 | pr_debug("Failed to create session\n"); |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2209 | goto destroy_ib; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2210 | } |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2211 | |
| 2212 | try_again: |
| 2213 | se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p); |
| 2214 | if (!se_acl) { |
| 2215 | pr_info("Rejected login because no ACL has been" |
| 2216 | " configured yet for initiator %s.\n", ch->sess_name); |
| 2217 | /* |
| 2218 | * XXX: Hack to retry of ch->i_port_id without leading '0x' |
| 2219 | */ |
| 2220 | if (p == &ch->sess_name[0]) { |
| 2221 | p += 2; |
| 2222 | goto try_again; |
| 2223 | } |
| 2224 | rej->reason = cpu_to_be32( |
| 2225 | SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED); |
| 2226 | transport_free_session(ch->sess); |
| 2227 | goto destroy_ib; |
| 2228 | } |
| 2229 | ch->sess->se_node_acl = se_acl; |
| 2230 | |
| 2231 | transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2232 | |
| 2233 | pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess, |
| 2234 | ch->sess_name, ch->cm_id); |
| 2235 | |
| 2236 | /* create srp_login_response */ |
| 2237 | rsp->opcode = SRP_LOGIN_RSP; |
| 2238 | rsp->tag = req->tag; |
| 2239 | rsp->max_it_iu_len = req->req_it_iu_len; |
| 2240 | rsp->max_ti_iu_len = req->req_it_iu_len; |
| 2241 | ch->max_ti_iu_len = it_iu_len; |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2242 | rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
| 2243 | | SRP_BUF_FORMAT_INDIRECT); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2244 | rsp->req_lim_delta = cpu_to_be32(ch->rq_size); |
| 2245 | atomic_set(&ch->req_lim, ch->rq_size); |
| 2246 | atomic_set(&ch->req_lim_delta, 0); |
| 2247 | |
| 2248 | /* create cm reply */ |
| 2249 | rep_param->qp_num = ch->qp->qp_num; |
| 2250 | rep_param->private_data = (void *)rsp; |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2251 | rep_param->private_data_len = sizeof(*rsp); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2252 | rep_param->rnr_retry_count = 7; |
| 2253 | rep_param->flow_control = 1; |
| 2254 | rep_param->failover_accepted = 0; |
| 2255 | rep_param->srq = 1; |
| 2256 | rep_param->responder_resources = 4; |
| 2257 | rep_param->initiator_depth = 4; |
| 2258 | |
| 2259 | ret = ib_send_cm_rep(cm_id, rep_param); |
| 2260 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2261 | pr_err("sending SRP_LOGIN_REQ response failed" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2262 | " (error code = %d)\n", ret); |
| 2263 | goto release_channel; |
| 2264 | } |
| 2265 | |
| 2266 | spin_lock_irq(&sdev->spinlock); |
| 2267 | list_add_tail(&ch->list, &sdev->rch_list); |
| 2268 | spin_unlock_irq(&sdev->spinlock); |
| 2269 | |
| 2270 | goto out; |
| 2271 | |
| 2272 | release_channel: |
| 2273 | srpt_set_ch_state(ch, CH_RELEASING); |
| 2274 | transport_deregister_session_configfs(ch->sess); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2275 | transport_deregister_session(ch->sess); |
| 2276 | ch->sess = NULL; |
| 2277 | |
| 2278 | destroy_ib: |
| 2279 | srpt_destroy_ch_ib(ch); |
| 2280 | |
| 2281 | free_ring: |
| 2282 | srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, |
| 2283 | ch->sport->sdev, ch->rq_size, |
| 2284 | ch->rsp_size, DMA_TO_DEVICE); |
| 2285 | free_ch: |
| 2286 | kfree(ch); |
| 2287 | |
| 2288 | reject: |
| 2289 | rej->opcode = SRP_LOGIN_REJ; |
| 2290 | rej->tag = req->tag; |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2291 | rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
| 2292 | | SRP_BUF_FORMAT_INDIRECT); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2293 | |
| 2294 | ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0, |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2295 | (void *)rej, sizeof(*rej)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2296 | |
| 2297 | out: |
| 2298 | kfree(rep_param); |
| 2299 | kfree(rsp); |
| 2300 | kfree(rej); |
| 2301 | |
| 2302 | return ret; |
| 2303 | } |
| 2304 | |
| 2305 | static void srpt_cm_rej_recv(struct ib_cm_id *cm_id) |
| 2306 | { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2307 | pr_info("Received IB REJ for cm_id %p.\n", cm_id); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2308 | srpt_drain_channel(cm_id); |
| 2309 | } |
| 2310 | |
| 2311 | /** |
| 2312 | * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event. |
| 2313 | * |
| 2314 | * An IB_CM_RTU_RECEIVED message indicates that the connection is established |
| 2315 | * and that the recipient may begin transmitting (RTU = ready to use). |
| 2316 | */ |
| 2317 | static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id) |
| 2318 | { |
| 2319 | struct srpt_rdma_ch *ch; |
| 2320 | int ret; |
| 2321 | |
| 2322 | ch = srpt_find_channel(cm_id->context, cm_id); |
| 2323 | BUG_ON(!ch); |
| 2324 | |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 2325 | if (srpt_set_ch_state(ch, CH_LIVE)) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2326 | struct srpt_recv_ioctx *ioctx, *ioctx_tmp; |
| 2327 | |
| 2328 | ret = srpt_ch_qp_rts(ch, ch->qp); |
| 2329 | |
| 2330 | list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list, |
| 2331 | wait_list) { |
| 2332 | list_del(&ioctx->wait_list); |
| 2333 | srpt_handle_new_iu(ch, ioctx, NULL); |
| 2334 | } |
| 2335 | if (ret) |
| 2336 | srpt_close_ch(ch); |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id) |
| 2341 | { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2342 | pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2343 | srpt_drain_channel(cm_id); |
| 2344 | } |
| 2345 | |
| 2346 | static void srpt_cm_rep_error(struct ib_cm_id *cm_id) |
| 2347 | { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2348 | pr_info("Received IB REP error for cm_id %p.\n", cm_id); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2349 | srpt_drain_channel(cm_id); |
| 2350 | } |
| 2351 | |
| 2352 | /** |
| 2353 | * srpt_cm_dreq_recv() - Process reception of a DREQ message. |
| 2354 | */ |
| 2355 | static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id) |
| 2356 | { |
| 2357 | struct srpt_rdma_ch *ch; |
| 2358 | unsigned long flags; |
| 2359 | bool send_drep = false; |
| 2360 | |
| 2361 | ch = srpt_find_channel(cm_id->context, cm_id); |
| 2362 | BUG_ON(!ch); |
| 2363 | |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 2364 | pr_debug("cm_id= %p ch->state= %d\n", cm_id, ch->state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2365 | |
| 2366 | spin_lock_irqsave(&ch->spinlock, flags); |
| 2367 | switch (ch->state) { |
| 2368 | case CH_CONNECTING: |
| 2369 | case CH_LIVE: |
| 2370 | send_drep = true; |
| 2371 | ch->state = CH_DISCONNECTING; |
| 2372 | break; |
| 2373 | case CH_DISCONNECTING: |
| 2374 | case CH_DRAINING: |
| 2375 | case CH_RELEASING: |
| 2376 | WARN(true, "unexpected channel state %d\n", ch->state); |
| 2377 | break; |
| 2378 | } |
| 2379 | spin_unlock_irqrestore(&ch->spinlock, flags); |
| 2380 | |
| 2381 | if (send_drep) { |
| 2382 | if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2383 | pr_err("Sending IB DREP failed.\n"); |
| 2384 | pr_info("Received DREQ and sent DREP for session %s.\n", |
| 2385 | ch->sess_name); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | /** |
| 2390 | * srpt_cm_drep_recv() - Process reception of a DREP message. |
| 2391 | */ |
| 2392 | static void srpt_cm_drep_recv(struct ib_cm_id *cm_id) |
| 2393 | { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2394 | pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2395 | srpt_drain_channel(cm_id); |
| 2396 | } |
| 2397 | |
| 2398 | /** |
| 2399 | * srpt_cm_handler() - IB connection manager callback function. |
| 2400 | * |
| 2401 | * A non-zero return value will cause the caller destroy the CM ID. |
| 2402 | * |
| 2403 | * Note: srpt_cm_handler() must only return a non-zero value when transferring |
| 2404 | * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning |
| 2405 | * a non-zero value in any other case will trigger a race with the |
| 2406 | * ib_destroy_cm_id() call in srpt_release_channel(). |
| 2407 | */ |
| 2408 | static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) |
| 2409 | { |
| 2410 | int ret; |
| 2411 | |
| 2412 | ret = 0; |
| 2413 | switch (event->event) { |
| 2414 | case IB_CM_REQ_RECEIVED: |
| 2415 | ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd, |
| 2416 | event->private_data); |
| 2417 | break; |
| 2418 | case IB_CM_REJ_RECEIVED: |
| 2419 | srpt_cm_rej_recv(cm_id); |
| 2420 | break; |
| 2421 | case IB_CM_RTU_RECEIVED: |
| 2422 | case IB_CM_USER_ESTABLISHED: |
| 2423 | srpt_cm_rtu_recv(cm_id); |
| 2424 | break; |
| 2425 | case IB_CM_DREQ_RECEIVED: |
| 2426 | srpt_cm_dreq_recv(cm_id); |
| 2427 | break; |
| 2428 | case IB_CM_DREP_RECEIVED: |
| 2429 | srpt_cm_drep_recv(cm_id); |
| 2430 | break; |
| 2431 | case IB_CM_TIMEWAIT_EXIT: |
| 2432 | srpt_cm_timewait_exit(cm_id); |
| 2433 | break; |
| 2434 | case IB_CM_REP_ERROR: |
| 2435 | srpt_cm_rep_error(cm_id); |
| 2436 | break; |
| 2437 | case IB_CM_DREQ_ERROR: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2438 | pr_info("Received IB DREQ ERROR event.\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2439 | break; |
| 2440 | case IB_CM_MRA_RECEIVED: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2441 | pr_info("Received IB MRA event\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2442 | break; |
| 2443 | default: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2444 | pr_err("received unrecognized IB CM event %d\n", event->event); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2445 | break; |
| 2446 | } |
| 2447 | |
| 2448 | return ret; |
| 2449 | } |
| 2450 | |
| 2451 | /** |
| 2452 | * srpt_perform_rdmas() - Perform IB RDMA. |
| 2453 | * |
| 2454 | * Returns zero upon success or a negative number upon failure. |
| 2455 | */ |
| 2456 | static int srpt_perform_rdmas(struct srpt_rdma_ch *ch, |
| 2457 | struct srpt_send_ioctx *ioctx) |
| 2458 | { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2459 | struct ib_send_wr *bad_wr; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2460 | int sq_wr_avail, ret, i; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2461 | enum dma_data_direction dir; |
| 2462 | const int n_rdma = ioctx->n_rdma; |
| 2463 | |
| 2464 | dir = ioctx->cmd.data_direction; |
| 2465 | if (dir == DMA_TO_DEVICE) { |
| 2466 | /* write */ |
| 2467 | ret = -ENOMEM; |
| 2468 | sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail); |
| 2469 | if (sq_wr_avail < 0) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2470 | pr_warn("IB send queue full (needed %d)\n", |
| 2471 | n_rdma); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2472 | goto out; |
| 2473 | } |
| 2474 | } |
| 2475 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2476 | for (i = 0; i < n_rdma; i++) { |
| 2477 | struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2478 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2479 | wr->opcode = (dir == DMA_FROM_DEVICE) ? |
| 2480 | IB_WR_RDMA_WRITE : IB_WR_RDMA_READ; |
| 2481 | |
| 2482 | if (i == n_rdma - 1) { |
| 2483 | /* only get completion event for the last rdma read */ |
| 2484 | if (dir == DMA_TO_DEVICE) { |
| 2485 | wr->send_flags = IB_SEND_SIGNALED; |
| 2486 | ioctx->rdma_cqe.done = srpt_rdma_read_done; |
| 2487 | } else { |
| 2488 | ioctx->rdma_cqe.done = srpt_rdma_write_done; |
| 2489 | } |
| 2490 | wr->wr_cqe = &ioctx->rdma_cqe; |
| 2491 | wr->next = NULL; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2492 | } else { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2493 | wr->wr_cqe = NULL; |
| 2494 | wr->next = &ioctx->rdma_wrs[i + 1].wr; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2495 | } |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2498 | ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2499 | if (ret) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2500 | pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2501 | __func__, __LINE__, ret, i, n_rdma); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2502 | out: |
| 2503 | if (unlikely(dir == DMA_TO_DEVICE && ret < 0)) |
| 2504 | atomic_add(n_rdma, &ch->sq_wr_avail); |
| 2505 | return ret; |
| 2506 | } |
| 2507 | |
| 2508 | /** |
| 2509 | * srpt_xfer_data() - Start data transfer from initiator to target. |
| 2510 | */ |
| 2511 | static int srpt_xfer_data(struct srpt_rdma_ch *ch, |
| 2512 | struct srpt_send_ioctx *ioctx) |
| 2513 | { |
| 2514 | int ret; |
| 2515 | |
| 2516 | ret = srpt_map_sg_to_ib_sge(ch, ioctx); |
| 2517 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2518 | pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2519 | goto out; |
| 2520 | } |
| 2521 | |
| 2522 | ret = srpt_perform_rdmas(ch, ioctx); |
| 2523 | if (ret) { |
| 2524 | if (ret == -EAGAIN || ret == -ENOMEM) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2525 | pr_info("%s[%d] queue full -- ret=%d\n", |
| 2526 | __func__, __LINE__, ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2527 | else |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2528 | pr_err("%s[%d] fatal error -- ret=%d\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2529 | __func__, __LINE__, ret); |
| 2530 | goto out_unmap; |
| 2531 | } |
| 2532 | |
| 2533 | out: |
| 2534 | return ret; |
| 2535 | out_unmap: |
| 2536 | srpt_unmap_sg_to_ib_sge(ch, ioctx); |
| 2537 | goto out; |
| 2538 | } |
| 2539 | |
| 2540 | static int srpt_write_pending_status(struct se_cmd *se_cmd) |
| 2541 | { |
| 2542 | struct srpt_send_ioctx *ioctx; |
| 2543 | |
| 2544 | ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd); |
| 2545 | return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA; |
| 2546 | } |
| 2547 | |
| 2548 | /* |
| 2549 | * srpt_write_pending() - Start data transfer from initiator to target (write). |
| 2550 | */ |
| 2551 | static int srpt_write_pending(struct se_cmd *se_cmd) |
| 2552 | { |
| 2553 | struct srpt_rdma_ch *ch; |
| 2554 | struct srpt_send_ioctx *ioctx; |
| 2555 | enum srpt_command_state new_state; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2556 | int ret; |
| 2557 | |
| 2558 | ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd); |
| 2559 | |
| 2560 | new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA); |
| 2561 | WARN_ON(new_state == SRPT_STATE_DONE); |
| 2562 | |
| 2563 | ch = ioctx->ch; |
| 2564 | BUG_ON(!ch); |
| 2565 | |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 2566 | switch (ch->state) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2567 | case CH_CONNECTING: |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 2568 | WARN(true, "unexpected channel state %d\n", ch->state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2569 | ret = -EINVAL; |
| 2570 | goto out; |
| 2571 | case CH_LIVE: |
| 2572 | break; |
| 2573 | case CH_DISCONNECTING: |
| 2574 | case CH_DRAINING: |
| 2575 | case CH_RELEASING: |
| 2576 | pr_debug("cmd with tag %lld: channel disconnecting\n", |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2577 | ioctx->cmd.tag); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2578 | srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN); |
| 2579 | ret = -EINVAL; |
| 2580 | goto out; |
| 2581 | } |
| 2582 | ret = srpt_xfer_data(ch, ioctx); |
| 2583 | |
| 2584 | out: |
| 2585 | return ret; |
| 2586 | } |
| 2587 | |
| 2588 | static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status) |
| 2589 | { |
| 2590 | switch (tcm_mgmt_status) { |
| 2591 | case TMR_FUNCTION_COMPLETE: |
| 2592 | return SRP_TSK_MGMT_SUCCESS; |
| 2593 | case TMR_FUNCTION_REJECTED: |
| 2594 | return SRP_TSK_MGMT_FUNC_NOT_SUPP; |
| 2595 | } |
| 2596 | return SRP_TSK_MGMT_FAILED; |
| 2597 | } |
| 2598 | |
| 2599 | /** |
| 2600 | * srpt_queue_response() - Transmits the response to a SCSI command. |
| 2601 | * |
| 2602 | * Callback function called by the TCM core. Must not block since it can be |
| 2603 | * invoked on the context of the IB completion handler. |
| 2604 | */ |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2605 | static void srpt_queue_response(struct se_cmd *cmd) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2606 | { |
| 2607 | struct srpt_rdma_ch *ch; |
| 2608 | struct srpt_send_ioctx *ioctx; |
| 2609 | enum srpt_command_state state; |
| 2610 | unsigned long flags; |
| 2611 | int ret; |
| 2612 | enum dma_data_direction dir; |
| 2613 | int resp_len; |
| 2614 | u8 srp_tm_status; |
| 2615 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2616 | ioctx = container_of(cmd, struct srpt_send_ioctx, cmd); |
| 2617 | ch = ioctx->ch; |
| 2618 | BUG_ON(!ch); |
| 2619 | |
| 2620 | spin_lock_irqsave(&ioctx->spinlock, flags); |
| 2621 | state = ioctx->state; |
| 2622 | switch (state) { |
| 2623 | case SRPT_STATE_NEW: |
| 2624 | case SRPT_STATE_DATA_IN: |
| 2625 | ioctx->state = SRPT_STATE_CMD_RSP_SENT; |
| 2626 | break; |
| 2627 | case SRPT_STATE_MGMT: |
| 2628 | ioctx->state = SRPT_STATE_MGMT_RSP_SENT; |
| 2629 | break; |
| 2630 | default: |
| 2631 | WARN(true, "ch %p; cmd %d: unexpected command state %d\n", |
| 2632 | ch, ioctx->ioctx.index, ioctx->state); |
| 2633 | break; |
| 2634 | } |
| 2635 | spin_unlock_irqrestore(&ioctx->spinlock, flags); |
| 2636 | |
| 2637 | if (unlikely(transport_check_aborted_status(&ioctx->cmd, false) |
| 2638 | || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) { |
| 2639 | atomic_inc(&ch->req_lim_delta); |
| 2640 | srpt_abort_cmd(ioctx); |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2641 | return; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | dir = ioctx->cmd.data_direction; |
| 2645 | |
| 2646 | /* For read commands, transfer the data to the initiator. */ |
| 2647 | if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length && |
| 2648 | !ioctx->queue_status_only) { |
| 2649 | ret = srpt_xfer_data(ch, ioctx); |
| 2650 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2651 | pr_err("xfer_data failed for tag %llu\n", |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2652 | ioctx->cmd.tag); |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2653 | return; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | if (state != SRPT_STATE_MGMT) |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2658 | resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2659 | cmd->scsi_status); |
| 2660 | else { |
| 2661 | srp_tm_status |
| 2662 | = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response); |
| 2663 | resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status, |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2664 | ioctx->cmd.tag); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2665 | } |
| 2666 | ret = srpt_post_send(ch, ioctx, resp_len); |
| 2667 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2668 | pr_err("sending cmd response failed for tag %llu\n", |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2669 | ioctx->cmd.tag); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2670 | srpt_unmap_sg_to_ib_sge(ch, ioctx); |
| 2671 | srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 2672 | target_put_sess_cmd(&ioctx->cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2673 | } |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2674 | } |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2675 | |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2676 | static int srpt_queue_data_in(struct se_cmd *cmd) |
| 2677 | { |
| 2678 | srpt_queue_response(cmd); |
| 2679 | return 0; |
| 2680 | } |
| 2681 | |
| 2682 | static void srpt_queue_tm_rsp(struct se_cmd *cmd) |
| 2683 | { |
| 2684 | srpt_queue_response(cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Nicholas Bellinger | 131e6ab | 2014-03-22 14:55:56 -0700 | [diff] [blame] | 2687 | static void srpt_aborted_task(struct se_cmd *cmd) |
| 2688 | { |
| 2689 | struct srpt_send_ioctx *ioctx = container_of(cmd, |
| 2690 | struct srpt_send_ioctx, cmd); |
| 2691 | |
| 2692 | srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx); |
| 2693 | } |
| 2694 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2695 | static int srpt_queue_status(struct se_cmd *cmd) |
| 2696 | { |
| 2697 | struct srpt_send_ioctx *ioctx; |
| 2698 | |
| 2699 | ioctx = container_of(cmd, struct srpt_send_ioctx, cmd); |
| 2700 | BUG_ON(ioctx->sense_data != cmd->sense_buffer); |
| 2701 | if (cmd->se_cmd_flags & |
| 2702 | (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE)) |
| 2703 | WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION); |
| 2704 | ioctx->queue_status_only = true; |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2705 | srpt_queue_response(cmd); |
| 2706 | return 0; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | static void srpt_refresh_port_work(struct work_struct *work) |
| 2710 | { |
| 2711 | struct srpt_port *sport = container_of(work, struct srpt_port, work); |
| 2712 | |
| 2713 | srpt_refresh_port(sport); |
| 2714 | } |
| 2715 | |
| 2716 | static int srpt_ch_list_empty(struct srpt_device *sdev) |
| 2717 | { |
| 2718 | int res; |
| 2719 | |
| 2720 | spin_lock_irq(&sdev->spinlock); |
| 2721 | res = list_empty(&sdev->rch_list); |
| 2722 | spin_unlock_irq(&sdev->spinlock); |
| 2723 | |
| 2724 | return res; |
| 2725 | } |
| 2726 | |
| 2727 | /** |
| 2728 | * srpt_release_sdev() - Free the channel resources associated with a target. |
| 2729 | */ |
| 2730 | static int srpt_release_sdev(struct srpt_device *sdev) |
| 2731 | { |
| 2732 | struct srpt_rdma_ch *ch, *tmp_ch; |
| 2733 | int res; |
| 2734 | |
| 2735 | WARN_ON_ONCE(irqs_disabled()); |
| 2736 | |
| 2737 | BUG_ON(!sdev); |
| 2738 | |
| 2739 | spin_lock_irq(&sdev->spinlock); |
| 2740 | list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) |
| 2741 | __srpt_close_ch(ch); |
| 2742 | spin_unlock_irq(&sdev->spinlock); |
| 2743 | |
| 2744 | res = wait_event_interruptible(sdev->ch_releaseQ, |
| 2745 | srpt_ch_list_empty(sdev)); |
| 2746 | if (res) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2747 | pr_err("%s: interrupted.\n", __func__); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2748 | |
| 2749 | return 0; |
| 2750 | } |
| 2751 | |
| 2752 | static struct srpt_port *__srpt_lookup_port(const char *name) |
| 2753 | { |
| 2754 | struct ib_device *dev; |
| 2755 | struct srpt_device *sdev; |
| 2756 | struct srpt_port *sport; |
| 2757 | int i; |
| 2758 | |
| 2759 | list_for_each_entry(sdev, &srpt_dev_list, list) { |
| 2760 | dev = sdev->device; |
| 2761 | if (!dev) |
| 2762 | continue; |
| 2763 | |
| 2764 | for (i = 0; i < dev->phys_port_cnt; i++) { |
| 2765 | sport = &sdev->port[i]; |
| 2766 | |
| 2767 | if (!strcmp(sport->port_guid, name)) |
| 2768 | return sport; |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | return NULL; |
| 2773 | } |
| 2774 | |
| 2775 | static struct srpt_port *srpt_lookup_port(const char *name) |
| 2776 | { |
| 2777 | struct srpt_port *sport; |
| 2778 | |
| 2779 | spin_lock(&srpt_dev_lock); |
| 2780 | sport = __srpt_lookup_port(name); |
| 2781 | spin_unlock(&srpt_dev_lock); |
| 2782 | |
| 2783 | return sport; |
| 2784 | } |
| 2785 | |
| 2786 | /** |
| 2787 | * srpt_add_one() - Infiniband device addition callback function. |
| 2788 | */ |
| 2789 | static void srpt_add_one(struct ib_device *device) |
| 2790 | { |
| 2791 | struct srpt_device *sdev; |
| 2792 | struct srpt_port *sport; |
| 2793 | struct ib_srq_init_attr srq_attr; |
| 2794 | int i; |
| 2795 | |
| 2796 | pr_debug("device = %p, device->dma_ops = %p\n", device, |
| 2797 | device->dma_ops); |
| 2798 | |
Bart Van Assche | 9d2aa2b4 | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2799 | sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2800 | if (!sdev) |
| 2801 | goto err; |
| 2802 | |
| 2803 | sdev->device = device; |
| 2804 | INIT_LIST_HEAD(&sdev->rch_list); |
| 2805 | init_waitqueue_head(&sdev->ch_releaseQ); |
| 2806 | spin_lock_init(&sdev->spinlock); |
| 2807 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2808 | sdev->pd = ib_alloc_pd(device); |
| 2809 | if (IS_ERR(sdev->pd)) |
| 2810 | goto free_dev; |
| 2811 | |
Or Gerlitz | 4a061b2 | 2015-12-18 10:59:46 +0200 | [diff] [blame] | 2812 | sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2813 | |
| 2814 | srq_attr.event_handler = srpt_srq_event; |
| 2815 | srq_attr.srq_context = (void *)sdev; |
| 2816 | srq_attr.attr.max_wr = sdev->srq_size; |
| 2817 | srq_attr.attr.max_sge = 1; |
| 2818 | srq_attr.attr.srq_limit = 0; |
Roland Dreier | 6f36033 | 2012-04-12 07:51:08 -0700 | [diff] [blame] | 2819 | srq_attr.srq_type = IB_SRQT_BASIC; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2820 | |
| 2821 | sdev->srq = ib_create_srq(sdev->pd, &srq_attr); |
| 2822 | if (IS_ERR(sdev->srq)) |
Jason Gunthorpe | 5a78395 | 2015-07-30 17:22:24 -0600 | [diff] [blame] | 2823 | goto err_pd; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2824 | |
| 2825 | pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n", |
Or Gerlitz | 4a061b2 | 2015-12-18 10:59:46 +0200 | [diff] [blame] | 2826 | __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2827 | device->name); |
| 2828 | |
| 2829 | if (!srpt_service_guid) |
| 2830 | srpt_service_guid = be64_to_cpu(device->node_guid); |
| 2831 | |
| 2832 | sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev); |
| 2833 | if (IS_ERR(sdev->cm_id)) |
| 2834 | goto err_srq; |
| 2835 | |
| 2836 | /* print out target login information */ |
| 2837 | pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx," |
| 2838 | "pkey=ffff,service_id=%016llx\n", srpt_service_guid, |
| 2839 | srpt_service_guid, srpt_service_guid); |
| 2840 | |
| 2841 | /* |
| 2842 | * We do not have a consistent service_id (ie. also id_ext of target_id) |
| 2843 | * to identify this target. We currently use the guid of the first HCA |
| 2844 | * in the system as service_id; therefore, the target_id will change |
| 2845 | * if this HCA is gone bad and replaced by different HCA |
| 2846 | */ |
Haggai Eran | 73fec7f | 2015-07-30 17:50:26 +0300 | [diff] [blame] | 2847 | if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0)) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2848 | goto err_cm; |
| 2849 | |
| 2850 | INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device, |
| 2851 | srpt_event_handler); |
| 2852 | if (ib_register_event_handler(&sdev->event_handler)) |
| 2853 | goto err_cm; |
| 2854 | |
| 2855 | sdev->ioctx_ring = (struct srpt_recv_ioctx **) |
| 2856 | srpt_alloc_ioctx_ring(sdev, sdev->srq_size, |
| 2857 | sizeof(*sdev->ioctx_ring[0]), |
| 2858 | srp_max_req_size, DMA_FROM_DEVICE); |
| 2859 | if (!sdev->ioctx_ring) |
| 2860 | goto err_event; |
| 2861 | |
| 2862 | for (i = 0; i < sdev->srq_size; ++i) |
| 2863 | srpt_post_recv(sdev, sdev->ioctx_ring[i]); |
| 2864 | |
Roland Dreier | f225066 | 2012-02-02 12:55:58 -0800 | [diff] [blame] | 2865 | WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2866 | |
| 2867 | for (i = 1; i <= sdev->device->phys_port_cnt; i++) { |
| 2868 | sport = &sdev->port[i - 1]; |
| 2869 | sport->sdev = sdev; |
| 2870 | sport->port = i; |
| 2871 | sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE; |
| 2872 | sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE; |
| 2873 | sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE; |
| 2874 | INIT_WORK(&sport->work, srpt_refresh_port_work); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2875 | |
| 2876 | if (srpt_refresh_port(sport)) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2877 | pr_err("MAD registration failed for %s-%d.\n", |
Bart Van Assche | f68cba4e9 | 2016-02-11 11:04:20 -0800 | [diff] [blame] | 2878 | sdev->device->name, i); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2879 | goto err_ring; |
| 2880 | } |
| 2881 | snprintf(sport->port_guid, sizeof(sport->port_guid), |
| 2882 | "0x%016llx%016llx", |
| 2883 | be64_to_cpu(sport->gid.global.subnet_prefix), |
| 2884 | be64_to_cpu(sport->gid.global.interface_id)); |
| 2885 | } |
| 2886 | |
| 2887 | spin_lock(&srpt_dev_lock); |
| 2888 | list_add_tail(&sdev->list, &srpt_dev_list); |
| 2889 | spin_unlock(&srpt_dev_lock); |
| 2890 | |
| 2891 | out: |
| 2892 | ib_set_client_data(device, &srpt_client, sdev); |
| 2893 | pr_debug("added %s.\n", device->name); |
| 2894 | return; |
| 2895 | |
| 2896 | err_ring: |
| 2897 | srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev, |
| 2898 | sdev->srq_size, srp_max_req_size, |
| 2899 | DMA_FROM_DEVICE); |
| 2900 | err_event: |
| 2901 | ib_unregister_event_handler(&sdev->event_handler); |
| 2902 | err_cm: |
| 2903 | ib_destroy_cm_id(sdev->cm_id); |
| 2904 | err_srq: |
| 2905 | ib_destroy_srq(sdev->srq); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2906 | err_pd: |
| 2907 | ib_dealloc_pd(sdev->pd); |
| 2908 | free_dev: |
| 2909 | kfree(sdev); |
| 2910 | err: |
| 2911 | sdev = NULL; |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2912 | pr_info("%s(%s) failed.\n", __func__, device->name); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2913 | goto out; |
| 2914 | } |
| 2915 | |
| 2916 | /** |
| 2917 | * srpt_remove_one() - InfiniBand device removal callback function. |
| 2918 | */ |
Haggai Eran | 7c1eb45 | 2015-07-30 17:50:14 +0300 | [diff] [blame] | 2919 | static void srpt_remove_one(struct ib_device *device, void *client_data) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2920 | { |
Haggai Eran | 7c1eb45 | 2015-07-30 17:50:14 +0300 | [diff] [blame] | 2921 | struct srpt_device *sdev = client_data; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2922 | int i; |
| 2923 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2924 | if (!sdev) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2925 | pr_info("%s(%s): nothing to do.\n", __func__, device->name); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2926 | return; |
| 2927 | } |
| 2928 | |
| 2929 | srpt_unregister_mad_agent(sdev); |
| 2930 | |
| 2931 | ib_unregister_event_handler(&sdev->event_handler); |
| 2932 | |
| 2933 | /* Cancel any work queued by the just unregistered IB event handler. */ |
| 2934 | for (i = 0; i < sdev->device->phys_port_cnt; i++) |
| 2935 | cancel_work_sync(&sdev->port[i].work); |
| 2936 | |
| 2937 | ib_destroy_cm_id(sdev->cm_id); |
| 2938 | |
| 2939 | /* |
| 2940 | * Unregistering a target must happen after destroying sdev->cm_id |
| 2941 | * such that no new SRP_LOGIN_REQ information units can arrive while |
| 2942 | * destroying the target. |
| 2943 | */ |
| 2944 | spin_lock(&srpt_dev_lock); |
| 2945 | list_del(&sdev->list); |
| 2946 | spin_unlock(&srpt_dev_lock); |
| 2947 | srpt_release_sdev(sdev); |
| 2948 | |
| 2949 | ib_destroy_srq(sdev->srq); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2950 | ib_dealloc_pd(sdev->pd); |
| 2951 | |
| 2952 | srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev, |
| 2953 | sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE); |
| 2954 | sdev->ioctx_ring = NULL; |
| 2955 | kfree(sdev); |
| 2956 | } |
| 2957 | |
| 2958 | static struct ib_client srpt_client = { |
| 2959 | .name = DRV_NAME, |
| 2960 | .add = srpt_add_one, |
| 2961 | .remove = srpt_remove_one |
| 2962 | }; |
| 2963 | |
| 2964 | static int srpt_check_true(struct se_portal_group *se_tpg) |
| 2965 | { |
| 2966 | return 1; |
| 2967 | } |
| 2968 | |
| 2969 | static int srpt_check_false(struct se_portal_group *se_tpg) |
| 2970 | { |
| 2971 | return 0; |
| 2972 | } |
| 2973 | |
| 2974 | static char *srpt_get_fabric_name(void) |
| 2975 | { |
| 2976 | return "srpt"; |
| 2977 | } |
| 2978 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2979 | static char *srpt_get_fabric_wwn(struct se_portal_group *tpg) |
| 2980 | { |
| 2981 | struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1); |
| 2982 | |
| 2983 | return sport->port_guid; |
| 2984 | } |
| 2985 | |
| 2986 | static u16 srpt_get_tag(struct se_portal_group *tpg) |
| 2987 | { |
| 2988 | return 1; |
| 2989 | } |
| 2990 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2991 | static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg) |
| 2992 | { |
| 2993 | return 1; |
| 2994 | } |
| 2995 | |
| 2996 | static void srpt_release_cmd(struct se_cmd *se_cmd) |
| 2997 | { |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 2998 | struct srpt_send_ioctx *ioctx = container_of(se_cmd, |
| 2999 | struct srpt_send_ioctx, cmd); |
| 3000 | struct srpt_rdma_ch *ch = ioctx->ch; |
| 3001 | unsigned long flags; |
| 3002 | |
| 3003 | WARN_ON(ioctx->state != SRPT_STATE_DONE); |
| 3004 | WARN_ON(ioctx->mapped_sg_count != 0); |
| 3005 | |
| 3006 | if (ioctx->n_rbuf > 1) { |
| 3007 | kfree(ioctx->rbufs); |
| 3008 | ioctx->rbufs = NULL; |
| 3009 | ioctx->n_rbuf = 0; |
| 3010 | } |
| 3011 | |
| 3012 | spin_lock_irqsave(&ch->spinlock, flags); |
| 3013 | list_add(&ioctx->free_list, &ch->free_list); |
| 3014 | spin_unlock_irqrestore(&ch->spinlock, flags); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3018 | * srpt_close_session() - Forcibly close a session. |
| 3019 | * |
| 3020 | * Callback function invoked by the TCM core to clean up sessions associated |
| 3021 | * with a node ACL when the user invokes |
| 3022 | * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id |
| 3023 | */ |
| 3024 | static void srpt_close_session(struct se_session *se_sess) |
| 3025 | { |
| 3026 | DECLARE_COMPLETION_ONSTACK(release_done); |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 3027 | struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr; |
| 3028 | struct srpt_device *sdev = ch->sport->sdev; |
| 3029 | bool wait; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3030 | |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 3031 | pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num, |
| 3032 | ch->state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3033 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3034 | spin_lock_irq(&sdev->spinlock); |
| 3035 | BUG_ON(ch->release_done); |
| 3036 | ch->release_done = &release_done; |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 3037 | wait = !list_empty(&ch->list); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3038 | __srpt_close_ch(ch); |
| 3039 | spin_unlock_irq(&sdev->spinlock); |
| 3040 | |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 3041 | if (!wait) |
| 3042 | return; |
| 3043 | |
| 3044 | while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0) |
| 3045 | pr_info("%s(%s-%d state %d): still waiting ...\n", __func__, |
| 3046 | ch->sess_name, ch->qp->qp_num, ch->state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3050 | * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB). |
| 3051 | * |
| 3052 | * A quote from RFC 4455 (SCSI-MIB) about this MIB object: |
| 3053 | * This object represents an arbitrary integer used to uniquely identify a |
| 3054 | * particular attached remote initiator port to a particular SCSI target port |
| 3055 | * within a particular SCSI target device within a particular SCSI instance. |
| 3056 | */ |
| 3057 | static u32 srpt_sess_get_index(struct se_session *se_sess) |
| 3058 | { |
| 3059 | return 0; |
| 3060 | } |
| 3061 | |
| 3062 | static void srpt_set_default_node_attrs(struct se_node_acl *nacl) |
| 3063 | { |
| 3064 | } |
| 3065 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3066 | /* Note: only used from inside debug printk's by the TCM core. */ |
| 3067 | static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd) |
| 3068 | { |
| 3069 | struct srpt_send_ioctx *ioctx; |
| 3070 | |
| 3071 | ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd); |
| 3072 | return srpt_get_cmd_state(ioctx); |
| 3073 | } |
| 3074 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3075 | /** |
| 3076 | * srpt_parse_i_port_id() - Parse an initiator port ID. |
| 3077 | * @name: ASCII representation of a 128-bit initiator port ID. |
| 3078 | * @i_port_id: Binary 128-bit port ID. |
| 3079 | */ |
| 3080 | static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name) |
| 3081 | { |
| 3082 | const char *p; |
| 3083 | unsigned len, count, leading_zero_bytes; |
| 3084 | int ret, rc; |
| 3085 | |
| 3086 | p = name; |
Rasmus Villemoes | b60459f | 2014-10-13 15:54:46 -0700 | [diff] [blame] | 3087 | if (strncasecmp(p, "0x", 2) == 0) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3088 | p += 2; |
| 3089 | ret = -EINVAL; |
| 3090 | len = strlen(p); |
| 3091 | if (len % 2) |
| 3092 | goto out; |
| 3093 | count = min(len / 2, 16U); |
| 3094 | leading_zero_bytes = 16 - count; |
| 3095 | memset(i_port_id, 0, leading_zero_bytes); |
| 3096 | rc = hex2bin(i_port_id + leading_zero_bytes, p, count); |
| 3097 | if (rc < 0) |
| 3098 | pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc); |
| 3099 | ret = 0; |
| 3100 | out: |
| 3101 | return ret; |
| 3102 | } |
| 3103 | |
| 3104 | /* |
| 3105 | * configfs callback function invoked for |
| 3106 | * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id |
| 3107 | */ |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 3108 | static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3109 | { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3110 | u8 i_port_id[16]; |
| 3111 | |
| 3112 | if (srpt_parse_i_port_id(i_port_id, name) < 0) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3113 | pr_err("invalid initiator port ID %s\n", name); |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 3114 | return -EINVAL; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3115 | } |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 3116 | return 0; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3119 | static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item, |
| 3120 | char *page) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3121 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3122 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3123 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3124 | |
| 3125 | return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size); |
| 3126 | } |
| 3127 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3128 | static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item, |
| 3129 | const char *page, size_t count) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3130 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3131 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3132 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3133 | unsigned long val; |
| 3134 | int ret; |
| 3135 | |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3136 | ret = kstrtoul(page, 0, &val); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3137 | if (ret < 0) { |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3138 | pr_err("kstrtoul() failed with ret: %d\n", ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3139 | return -EINVAL; |
| 3140 | } |
| 3141 | if (val > MAX_SRPT_RDMA_SIZE) { |
| 3142 | pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val, |
| 3143 | MAX_SRPT_RDMA_SIZE); |
| 3144 | return -EINVAL; |
| 3145 | } |
| 3146 | if (val < DEFAULT_MAX_RDMA_SIZE) { |
| 3147 | pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n", |
| 3148 | val, DEFAULT_MAX_RDMA_SIZE); |
| 3149 | return -EINVAL; |
| 3150 | } |
| 3151 | sport->port_attrib.srp_max_rdma_size = val; |
| 3152 | |
| 3153 | return count; |
| 3154 | } |
| 3155 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3156 | static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item, |
| 3157 | char *page) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3158 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3159 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3160 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3161 | |
| 3162 | return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size); |
| 3163 | } |
| 3164 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3165 | static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item, |
| 3166 | const char *page, size_t count) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3167 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3168 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3169 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3170 | unsigned long val; |
| 3171 | int ret; |
| 3172 | |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3173 | ret = kstrtoul(page, 0, &val); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3174 | if (ret < 0) { |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3175 | pr_err("kstrtoul() failed with ret: %d\n", ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3176 | return -EINVAL; |
| 3177 | } |
| 3178 | if (val > MAX_SRPT_RSP_SIZE) { |
| 3179 | pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val, |
| 3180 | MAX_SRPT_RSP_SIZE); |
| 3181 | return -EINVAL; |
| 3182 | } |
| 3183 | if (val < MIN_MAX_RSP_SIZE) { |
| 3184 | pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val, |
| 3185 | MIN_MAX_RSP_SIZE); |
| 3186 | return -EINVAL; |
| 3187 | } |
| 3188 | sport->port_attrib.srp_max_rsp_size = val; |
| 3189 | |
| 3190 | return count; |
| 3191 | } |
| 3192 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3193 | static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item, |
| 3194 | char *page) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3195 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3196 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3197 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3198 | |
| 3199 | return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size); |
| 3200 | } |
| 3201 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3202 | static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item, |
| 3203 | const char *page, size_t count) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3204 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3205 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3206 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3207 | unsigned long val; |
| 3208 | int ret; |
| 3209 | |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3210 | ret = kstrtoul(page, 0, &val); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3211 | if (ret < 0) { |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3212 | pr_err("kstrtoul() failed with ret: %d\n", ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3213 | return -EINVAL; |
| 3214 | } |
| 3215 | if (val > MAX_SRPT_SRQ_SIZE) { |
| 3216 | pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val, |
| 3217 | MAX_SRPT_SRQ_SIZE); |
| 3218 | return -EINVAL; |
| 3219 | } |
| 3220 | if (val < MIN_SRPT_SRQ_SIZE) { |
| 3221 | pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val, |
| 3222 | MIN_SRPT_SRQ_SIZE); |
| 3223 | return -EINVAL; |
| 3224 | } |
| 3225 | sport->port_attrib.srp_sq_size = val; |
| 3226 | |
| 3227 | return count; |
| 3228 | } |
| 3229 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3230 | CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size); |
| 3231 | CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size); |
| 3232 | CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3233 | |
| 3234 | static struct configfs_attribute *srpt_tpg_attrib_attrs[] = { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3235 | &srpt_tpg_attrib_attr_srp_max_rdma_size, |
| 3236 | &srpt_tpg_attrib_attr_srp_max_rsp_size, |
| 3237 | &srpt_tpg_attrib_attr_srp_sq_size, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3238 | NULL, |
| 3239 | }; |
| 3240 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3241 | static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3242 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3243 | struct se_portal_group *se_tpg = to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3244 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3245 | |
| 3246 | return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0); |
| 3247 | } |
| 3248 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3249 | static ssize_t srpt_tpg_enable_store(struct config_item *item, |
| 3250 | const char *page, size_t count) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3251 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3252 | struct se_portal_group *se_tpg = to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3253 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3254 | unsigned long tmp; |
| 3255 | int ret; |
| 3256 | |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3257 | ret = kstrtoul(page, 0, &tmp); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3258 | if (ret < 0) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3259 | pr_err("Unable to extract srpt_tpg_store_enable\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3260 | return -EINVAL; |
| 3261 | } |
| 3262 | |
| 3263 | if ((tmp != 0) && (tmp != 1)) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3264 | pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3265 | return -EINVAL; |
| 3266 | } |
| 3267 | if (tmp == 1) |
| 3268 | sport->enabled = true; |
| 3269 | else |
| 3270 | sport->enabled = false; |
| 3271 | |
| 3272 | return count; |
| 3273 | } |
| 3274 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3275 | CONFIGFS_ATTR(srpt_tpg_, enable); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3276 | |
| 3277 | static struct configfs_attribute *srpt_tpg_attrs[] = { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3278 | &srpt_tpg_attr_enable, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3279 | NULL, |
| 3280 | }; |
| 3281 | |
| 3282 | /** |
| 3283 | * configfs callback invoked for |
| 3284 | * mkdir /sys/kernel/config/target/$driver/$port/$tpg |
| 3285 | */ |
| 3286 | static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn, |
| 3287 | struct config_group *group, |
| 3288 | const char *name) |
| 3289 | { |
| 3290 | struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn); |
| 3291 | int res; |
| 3292 | |
| 3293 | /* Initialize sport->port_wwn and sport->port_tpg_1 */ |
Nicholas Bellinger | bc0c94b | 2015-05-20 21:48:03 -0700 | [diff] [blame] | 3294 | res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3295 | if (res) |
| 3296 | return ERR_PTR(res); |
| 3297 | |
| 3298 | return &sport->port_tpg_1; |
| 3299 | } |
| 3300 | |
| 3301 | /** |
| 3302 | * configfs callback invoked for |
| 3303 | * rmdir /sys/kernel/config/target/$driver/$port/$tpg |
| 3304 | */ |
| 3305 | static void srpt_drop_tpg(struct se_portal_group *tpg) |
| 3306 | { |
| 3307 | struct srpt_port *sport = container_of(tpg, |
| 3308 | struct srpt_port, port_tpg_1); |
| 3309 | |
| 3310 | sport->enabled = false; |
| 3311 | core_tpg_deregister(&sport->port_tpg_1); |
| 3312 | } |
| 3313 | |
| 3314 | /** |
| 3315 | * configfs callback invoked for |
| 3316 | * mkdir /sys/kernel/config/target/$driver/$port |
| 3317 | */ |
| 3318 | static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf, |
| 3319 | struct config_group *group, |
| 3320 | const char *name) |
| 3321 | { |
| 3322 | struct srpt_port *sport; |
| 3323 | int ret; |
| 3324 | |
| 3325 | sport = srpt_lookup_port(name); |
| 3326 | pr_debug("make_tport(%s)\n", name); |
| 3327 | ret = -EINVAL; |
| 3328 | if (!sport) |
| 3329 | goto err; |
| 3330 | |
| 3331 | return &sport->port_wwn; |
| 3332 | |
| 3333 | err: |
| 3334 | return ERR_PTR(ret); |
| 3335 | } |
| 3336 | |
| 3337 | /** |
| 3338 | * configfs callback invoked for |
| 3339 | * rmdir /sys/kernel/config/target/$driver/$port |
| 3340 | */ |
| 3341 | static void srpt_drop_tport(struct se_wwn *wwn) |
| 3342 | { |
| 3343 | struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn); |
| 3344 | |
| 3345 | pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item)); |
| 3346 | } |
| 3347 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3348 | static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3349 | { |
| 3350 | return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION); |
| 3351 | } |
| 3352 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3353 | CONFIGFS_ATTR_RO(srpt_wwn_, version); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3354 | |
| 3355 | static struct configfs_attribute *srpt_wwn_attrs[] = { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3356 | &srpt_wwn_attr_version, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3357 | NULL, |
| 3358 | }; |
| 3359 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3360 | static const struct target_core_fabric_ops srpt_template = { |
| 3361 | .module = THIS_MODULE, |
| 3362 | .name = "srpt", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3363 | .get_fabric_name = srpt_get_fabric_name, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3364 | .tpg_get_wwn = srpt_get_fabric_wwn, |
| 3365 | .tpg_get_tag = srpt_get_tag, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3366 | .tpg_check_demo_mode = srpt_check_false, |
| 3367 | .tpg_check_demo_mode_cache = srpt_check_true, |
| 3368 | .tpg_check_demo_mode_write_protect = srpt_check_true, |
| 3369 | .tpg_check_prod_mode_write_protect = srpt_check_false, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3370 | .tpg_get_inst_index = srpt_tpg_get_inst_index, |
| 3371 | .release_cmd = srpt_release_cmd, |
| 3372 | .check_stop_free = srpt_check_stop_free, |
| 3373 | .shutdown_session = srpt_shutdown_session, |
| 3374 | .close_session = srpt_close_session, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3375 | .sess_get_index = srpt_sess_get_index, |
| 3376 | .sess_get_initiator_sid = NULL, |
| 3377 | .write_pending = srpt_write_pending, |
| 3378 | .write_pending_status = srpt_write_pending_status, |
| 3379 | .set_default_node_attributes = srpt_set_default_node_attrs, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3380 | .get_cmd_state = srpt_get_tcm_cmd_state, |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 3381 | .queue_data_in = srpt_queue_data_in, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3382 | .queue_status = srpt_queue_status, |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 3383 | .queue_tm_rsp = srpt_queue_tm_rsp, |
Nicholas Bellinger | 131e6ab | 2014-03-22 14:55:56 -0700 | [diff] [blame] | 3384 | .aborted_task = srpt_aborted_task, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3385 | /* |
| 3386 | * Setup function pointers for generic logic in |
| 3387 | * target_core_fabric_configfs.c |
| 3388 | */ |
| 3389 | .fabric_make_wwn = srpt_make_tport, |
| 3390 | .fabric_drop_wwn = srpt_drop_tport, |
| 3391 | .fabric_make_tpg = srpt_make_tpg, |
| 3392 | .fabric_drop_tpg = srpt_drop_tpg, |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 3393 | .fabric_init_nodeacl = srpt_init_nodeacl, |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3394 | |
| 3395 | .tfc_wwn_attrs = srpt_wwn_attrs, |
| 3396 | .tfc_tpg_base_attrs = srpt_tpg_attrs, |
| 3397 | .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3398 | }; |
| 3399 | |
| 3400 | /** |
| 3401 | * srpt_init_module() - Kernel module initialization. |
| 3402 | * |
| 3403 | * Note: Since ib_register_client() registers callback functions, and since at |
| 3404 | * least one of these callback functions (srpt_add_one()) calls target core |
| 3405 | * functions, this driver must be registered with the target core before |
| 3406 | * ib_register_client() is called. |
| 3407 | */ |
| 3408 | static int __init srpt_init_module(void) |
| 3409 | { |
| 3410 | int ret; |
| 3411 | |
| 3412 | ret = -EINVAL; |
| 3413 | if (srp_max_req_size < MIN_MAX_REQ_SIZE) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3414 | pr_err("invalid value %d for kernel module parameter" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3415 | " srp_max_req_size -- must be at least %d.\n", |
| 3416 | srp_max_req_size, MIN_MAX_REQ_SIZE); |
| 3417 | goto out; |
| 3418 | } |
| 3419 | |
| 3420 | if (srpt_srq_size < MIN_SRPT_SRQ_SIZE |
| 3421 | || srpt_srq_size > MAX_SRPT_SRQ_SIZE) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3422 | pr_err("invalid value %d for kernel module parameter" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3423 | " srpt_srq_size -- must be in the range [%d..%d].\n", |
| 3424 | srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE); |
| 3425 | goto out; |
| 3426 | } |
| 3427 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3428 | ret = target_register_template(&srpt_template); |
| 3429 | if (ret) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3430 | goto out; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3431 | |
| 3432 | ret = ib_register_client(&srpt_client); |
| 3433 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3434 | pr_err("couldn't register IB client\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3435 | goto out_unregister_target; |
| 3436 | } |
| 3437 | |
| 3438 | return 0; |
| 3439 | |
| 3440 | out_unregister_target: |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3441 | target_unregister_template(&srpt_template); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3442 | out: |
| 3443 | return ret; |
| 3444 | } |
| 3445 | |
| 3446 | static void __exit srpt_cleanup_module(void) |
| 3447 | { |
| 3448 | ib_unregister_client(&srpt_client); |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3449 | target_unregister_template(&srpt_template); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | module_init(srpt_init_module); |
| 3453 | module_exit(srpt_cleanup_module); |