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