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 | 387add4 | 2016-02-11 11:10:09 -0800 | [diff] [blame^] | 99 | static void srpt_process_wait_list(struct srpt_rdma_ch *ch); |
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 | 9d2aa2b | 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 | 9d2aa2b | 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 | 9d2aa2b | 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 | 9d2aa2b | 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 | 9d2aa2b | 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 | 9d2aa2b | 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 | |
Bart Van Assche | 387add4 | 2016-02-11 11:10:09 -0800 | [diff] [blame^] | 836 | if (wc->status == IB_WC_SUCCESS) { |
| 837 | srpt_process_wait_list(ch); |
| 838 | } else { |
| 839 | if (srpt_set_ch_state(ch, CH_DISCONNECTED)) |
| 840 | schedule_work(&ch->release_work); |
| 841 | else |
| 842 | WARN_ONCE("%s-%d\n", ch->sess_name, ch->qp->qp_num); |
| 843 | } |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 847 | * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request. |
| 848 | * @ioctx: Pointer to the I/O context associated with the request. |
| 849 | * @srp_cmd: Pointer to the SRP_CMD request data. |
| 850 | * @dir: Pointer to the variable to which the transfer direction will be |
| 851 | * written. |
| 852 | * @data_len: Pointer to the variable to which the total data length of all |
| 853 | * descriptors in the SRP_CMD request will be written. |
| 854 | * |
| 855 | * This function initializes ioctx->nrbuf and ioctx->r_bufs. |
| 856 | * |
| 857 | * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors; |
| 858 | * -ENOMEM when memory allocation fails and zero upon success. |
| 859 | */ |
| 860 | static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx, |
| 861 | struct srp_cmd *srp_cmd, |
| 862 | enum dma_data_direction *dir, u64 *data_len) |
| 863 | { |
| 864 | struct srp_indirect_buf *idb; |
| 865 | struct srp_direct_buf *db; |
| 866 | unsigned add_cdb_offset; |
| 867 | int ret; |
| 868 | |
| 869 | /* |
| 870 | * The pointer computations below will only be compiled correctly |
| 871 | * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check |
| 872 | * whether srp_cmd::add_data has been declared as a byte pointer. |
| 873 | */ |
| 874 | BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) |
| 875 | && !__same_type(srp_cmd->add_data[0], (u8)0)); |
| 876 | |
| 877 | BUG_ON(!dir); |
| 878 | BUG_ON(!data_len); |
| 879 | |
| 880 | ret = 0; |
| 881 | *data_len = 0; |
| 882 | |
| 883 | /* |
| 884 | * The lower four bits of the buffer format field contain the DATA-IN |
| 885 | * buffer descriptor format, and the highest four bits contain the |
| 886 | * DATA-OUT buffer descriptor format. |
| 887 | */ |
| 888 | *dir = DMA_NONE; |
| 889 | if (srp_cmd->buf_fmt & 0xf) |
| 890 | /* DATA-IN: transfer data from target to initiator (read). */ |
| 891 | *dir = DMA_FROM_DEVICE; |
| 892 | else if (srp_cmd->buf_fmt >> 4) |
| 893 | /* DATA-OUT: transfer data from initiator to target (write). */ |
| 894 | *dir = DMA_TO_DEVICE; |
| 895 | |
| 896 | /* |
| 897 | * According to the SRP spec, the lower two bits of the 'ADDITIONAL |
| 898 | * CDB LENGTH' field are reserved and the size in bytes of this field |
| 899 | * is four times the value specified in bits 3..7. Hence the "& ~3". |
| 900 | */ |
| 901 | add_cdb_offset = srp_cmd->add_cdb_len & ~3; |
| 902 | if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) || |
| 903 | ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) { |
| 904 | ioctx->n_rbuf = 1; |
| 905 | ioctx->rbufs = &ioctx->single_rbuf; |
| 906 | |
| 907 | db = (struct srp_direct_buf *)(srp_cmd->add_data |
| 908 | + add_cdb_offset); |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 909 | memcpy(ioctx->rbufs, db, sizeof(*db)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 910 | *data_len = be32_to_cpu(db->len); |
| 911 | } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) || |
| 912 | ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) { |
| 913 | idb = (struct srp_indirect_buf *)(srp_cmd->add_data |
| 914 | + add_cdb_offset); |
| 915 | |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 916 | 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] | 917 | |
| 918 | if (ioctx->n_rbuf > |
| 919 | (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 920 | pr_err("received unsupported SRP_CMD request" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 921 | " type (%u out + %u in != %u / %zu)\n", |
| 922 | srp_cmd->data_out_desc_cnt, |
| 923 | srp_cmd->data_in_desc_cnt, |
| 924 | be32_to_cpu(idb->table_desc.len), |
| 925 | sizeof(*db)); |
| 926 | ioctx->n_rbuf = 0; |
| 927 | ret = -EINVAL; |
| 928 | goto out; |
| 929 | } |
| 930 | |
| 931 | if (ioctx->n_rbuf == 1) |
| 932 | ioctx->rbufs = &ioctx->single_rbuf; |
| 933 | else { |
| 934 | ioctx->rbufs = |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 935 | kmalloc(ioctx->n_rbuf * sizeof(*db), GFP_ATOMIC); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 936 | if (!ioctx->rbufs) { |
| 937 | ioctx->n_rbuf = 0; |
| 938 | ret = -ENOMEM; |
| 939 | goto out; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | db = idb->desc_list; |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 944 | memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof(*db)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 945 | *data_len = be32_to_cpu(idb->len); |
| 946 | } |
| 947 | out: |
| 948 | return ret; |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * srpt_init_ch_qp() - Initialize queue pair attributes. |
| 953 | * |
| 954 | * Initialized the attributes of queue pair 'qp' by allowing local write, |
| 955 | * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT. |
| 956 | */ |
| 957 | static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp) |
| 958 | { |
| 959 | struct ib_qp_attr *attr; |
| 960 | int ret; |
| 961 | |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 962 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 963 | if (!attr) |
| 964 | return -ENOMEM; |
| 965 | |
| 966 | attr->qp_state = IB_QPS_INIT; |
| 967 | attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ | |
| 968 | IB_ACCESS_REMOTE_WRITE; |
| 969 | attr->port_num = ch->sport->port; |
| 970 | attr->pkey_index = 0; |
| 971 | |
| 972 | ret = ib_modify_qp(qp, attr, |
| 973 | IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT | |
| 974 | IB_QP_PKEY_INDEX); |
| 975 | |
| 976 | kfree(attr); |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR). |
| 982 | * @ch: channel of the queue pair. |
| 983 | * @qp: queue pair to change the state of. |
| 984 | * |
| 985 | * Returns zero upon success and a negative value upon failure. |
| 986 | * |
| 987 | * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. |
| 988 | * If this structure ever becomes larger, it might be necessary to allocate |
| 989 | * it dynamically instead of on the stack. |
| 990 | */ |
| 991 | static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp) |
| 992 | { |
| 993 | struct ib_qp_attr qp_attr; |
| 994 | int attr_mask; |
| 995 | int ret; |
| 996 | |
| 997 | qp_attr.qp_state = IB_QPS_RTR; |
| 998 | ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask); |
| 999 | if (ret) |
| 1000 | goto out; |
| 1001 | |
| 1002 | qp_attr.max_dest_rd_atomic = 4; |
| 1003 | |
| 1004 | ret = ib_modify_qp(qp, &qp_attr, attr_mask); |
| 1005 | |
| 1006 | out: |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS). |
| 1012 | * @ch: channel of the queue pair. |
| 1013 | * @qp: queue pair to change the state of. |
| 1014 | * |
| 1015 | * Returns zero upon success and a negative value upon failure. |
| 1016 | * |
| 1017 | * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. |
| 1018 | * If this structure ever becomes larger, it might be necessary to allocate |
| 1019 | * it dynamically instead of on the stack. |
| 1020 | */ |
| 1021 | static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp) |
| 1022 | { |
| 1023 | struct ib_qp_attr qp_attr; |
| 1024 | int attr_mask; |
| 1025 | int ret; |
| 1026 | |
| 1027 | qp_attr.qp_state = IB_QPS_RTS; |
| 1028 | ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask); |
| 1029 | if (ret) |
| 1030 | goto out; |
| 1031 | |
| 1032 | qp_attr.max_rd_atomic = 4; |
| 1033 | |
| 1034 | ret = ib_modify_qp(qp, &qp_attr, attr_mask); |
| 1035 | |
| 1036 | out: |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * srpt_ch_qp_err() - Set the channel queue pair state to 'error'. |
| 1042 | */ |
| 1043 | static int srpt_ch_qp_err(struct srpt_rdma_ch *ch) |
| 1044 | { |
| 1045 | struct ib_qp_attr qp_attr; |
| 1046 | |
| 1047 | qp_attr.qp_state = IB_QPS_ERR; |
| 1048 | return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE); |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list. |
| 1053 | */ |
| 1054 | static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch, |
| 1055 | struct srpt_send_ioctx *ioctx) |
| 1056 | { |
| 1057 | struct scatterlist *sg; |
| 1058 | enum dma_data_direction dir; |
| 1059 | |
| 1060 | BUG_ON(!ch); |
| 1061 | BUG_ON(!ioctx); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1062 | BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1063 | |
| 1064 | while (ioctx->n_rdma) |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1065 | kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1066 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1067 | kfree(ioctx->rdma_wrs); |
| 1068 | ioctx->rdma_wrs = NULL; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (ioctx->mapped_sg_count) { |
| 1071 | sg = ioctx->sg; |
| 1072 | WARN_ON(!sg); |
| 1073 | dir = ioctx->cmd.data_direction; |
| 1074 | BUG_ON(dir == DMA_NONE); |
| 1075 | 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] | 1076 | target_reverse_dma_direction(&ioctx->cmd)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1077 | ioctx->mapped_sg_count = 0; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list. |
| 1083 | */ |
| 1084 | static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch, |
| 1085 | struct srpt_send_ioctx *ioctx) |
| 1086 | { |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1087 | struct ib_device *dev = ch->sport->sdev->device; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1088 | struct se_cmd *cmd; |
| 1089 | struct scatterlist *sg, *sg_orig; |
| 1090 | int sg_cnt; |
| 1091 | enum dma_data_direction dir; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1092 | struct ib_rdma_wr *riu; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1093 | struct srp_direct_buf *db; |
| 1094 | dma_addr_t dma_addr; |
| 1095 | struct ib_sge *sge; |
| 1096 | u64 raddr; |
| 1097 | u32 rsize; |
| 1098 | u32 tsize; |
| 1099 | u32 dma_len; |
| 1100 | int count, nrdma; |
| 1101 | int i, j, k; |
| 1102 | |
| 1103 | BUG_ON(!ch); |
| 1104 | BUG_ON(!ioctx); |
| 1105 | cmd = &ioctx->cmd; |
| 1106 | dir = cmd->data_direction; |
| 1107 | BUG_ON(dir == DMA_NONE); |
| 1108 | |
Roland Dreier | 6f9e7f0 | 2012-03-30 11:29:12 -0700 | [diff] [blame] | 1109 | ioctx->sg = sg = sg_orig = cmd->t_data_sg; |
| 1110 | ioctx->sg_cnt = sg_cnt = cmd->t_data_nents; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1111 | |
| 1112 | 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] | 1113 | target_reverse_dma_direction(cmd)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1114 | if (unlikely(!count)) |
| 1115 | return -EAGAIN; |
| 1116 | |
| 1117 | ioctx->mapped_sg_count = count; |
| 1118 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1119 | if (ioctx->rdma_wrs && ioctx->n_rdma_wrs) |
| 1120 | nrdma = ioctx->n_rdma_wrs; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1121 | else { |
| 1122 | nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE |
| 1123 | + ioctx->n_rbuf; |
| 1124 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1125 | ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs), |
| 1126 | GFP_KERNEL); |
| 1127 | if (!ioctx->rdma_wrs) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1128 | goto free_mem; |
| 1129 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1130 | ioctx->n_rdma_wrs = nrdma; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | db = ioctx->rbufs; |
| 1134 | tsize = cmd->data_length; |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1135 | dma_len = ib_sg_dma_len(dev, &sg[0]); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1136 | riu = ioctx->rdma_wrs; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1137 | |
| 1138 | /* |
| 1139 | * For each remote desc - calculate the #ib_sge. |
| 1140 | * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then |
| 1141 | * each remote desc rdma_iu is required a rdma wr; |
| 1142 | * else |
| 1143 | * we need to allocate extra rdma_iu to carry extra #ib_sge in |
| 1144 | * another rdma wr |
| 1145 | */ |
| 1146 | for (i = 0, j = 0; |
| 1147 | j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) { |
| 1148 | rsize = be32_to_cpu(db->len); |
| 1149 | raddr = be64_to_cpu(db->va); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1150 | riu->remote_addr = raddr; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1151 | riu->rkey = be32_to_cpu(db->key); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1152 | riu->wr.num_sge = 0; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1153 | |
| 1154 | /* calculate how many sge required for this remote_buf */ |
| 1155 | while (rsize > 0 && tsize > 0) { |
| 1156 | |
| 1157 | if (rsize >= dma_len) { |
| 1158 | tsize -= dma_len; |
| 1159 | rsize -= dma_len; |
| 1160 | raddr += dma_len; |
| 1161 | |
| 1162 | if (tsize > 0) { |
| 1163 | ++j; |
| 1164 | if (j < count) { |
| 1165 | sg = sg_next(sg); |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1166 | dma_len = ib_sg_dma_len( |
| 1167 | dev, sg); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | } else { |
| 1171 | tsize -= rsize; |
| 1172 | dma_len -= rsize; |
| 1173 | rsize = 0; |
| 1174 | } |
| 1175 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1176 | ++riu->wr.num_sge; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1177 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1178 | if (rsize > 0 && |
| 1179 | riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1180 | ++ioctx->n_rdma; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1181 | riu->wr.sg_list = kmalloc_array(riu->wr.num_sge, |
| 1182 | sizeof(*riu->wr.sg_list), |
| 1183 | GFP_KERNEL); |
| 1184 | if (!riu->wr.sg_list) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1185 | goto free_mem; |
| 1186 | |
| 1187 | ++riu; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1188 | riu->wr.num_sge = 0; |
| 1189 | riu->remote_addr = raddr; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1190 | riu->rkey = be32_to_cpu(db->key); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | ++ioctx->n_rdma; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1195 | riu->wr.sg_list = kmalloc_array(riu->wr.num_sge, |
| 1196 | sizeof(*riu->wr.sg_list), |
| 1197 | GFP_KERNEL); |
| 1198 | if (!riu->wr.sg_list) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1199 | goto free_mem; |
| 1200 | } |
| 1201 | |
| 1202 | db = ioctx->rbufs; |
| 1203 | tsize = cmd->data_length; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1204 | riu = ioctx->rdma_wrs; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1205 | sg = sg_orig; |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1206 | dma_len = ib_sg_dma_len(dev, &sg[0]); |
| 1207 | dma_addr = ib_sg_dma_address(dev, &sg[0]); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1208 | |
| 1209 | /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */ |
| 1210 | for (i = 0, j = 0; |
| 1211 | j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) { |
| 1212 | rsize = be32_to_cpu(db->len); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1213 | sge = riu->wr.sg_list; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1214 | k = 0; |
| 1215 | |
| 1216 | while (rsize > 0 && tsize > 0) { |
| 1217 | sge->addr = dma_addr; |
Jason Gunthorpe | 5a78395 | 2015-07-30 17:22:24 -0600 | [diff] [blame] | 1218 | sge->lkey = ch->sport->sdev->pd->local_dma_lkey; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1219 | |
| 1220 | if (rsize >= dma_len) { |
| 1221 | sge->length = |
| 1222 | (tsize < dma_len) ? tsize : dma_len; |
| 1223 | tsize -= dma_len; |
| 1224 | rsize -= dma_len; |
| 1225 | |
| 1226 | if (tsize > 0) { |
| 1227 | ++j; |
| 1228 | if (j < count) { |
| 1229 | sg = sg_next(sg); |
Mike Marciniszyn | b076808 | 2014-04-07 13:58:35 -0400 | [diff] [blame] | 1230 | dma_len = ib_sg_dma_len( |
| 1231 | dev, sg); |
| 1232 | dma_addr = ib_sg_dma_address( |
| 1233 | dev, sg); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | } else { |
| 1237 | sge->length = (tsize < rsize) ? tsize : rsize; |
| 1238 | tsize -= rsize; |
| 1239 | dma_len -= rsize; |
| 1240 | dma_addr += rsize; |
| 1241 | rsize = 0; |
| 1242 | } |
| 1243 | |
| 1244 | ++k; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1245 | if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1246 | ++riu; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1247 | sge = riu->wr.sg_list; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1248 | k = 0; |
| 1249 | } else if (rsize > 0 && tsize > 0) |
| 1250 | ++sge; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | return 0; |
| 1255 | |
| 1256 | free_mem: |
| 1257 | srpt_unmap_sg_to_ib_sge(ch, ioctx); |
| 1258 | |
| 1259 | return -ENOMEM; |
| 1260 | } |
| 1261 | |
| 1262 | /** |
| 1263 | * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator. |
| 1264 | */ |
| 1265 | static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch) |
| 1266 | { |
| 1267 | struct srpt_send_ioctx *ioctx; |
| 1268 | unsigned long flags; |
| 1269 | |
| 1270 | BUG_ON(!ch); |
| 1271 | |
| 1272 | ioctx = NULL; |
| 1273 | spin_lock_irqsave(&ch->spinlock, flags); |
| 1274 | if (!list_empty(&ch->free_list)) { |
| 1275 | ioctx = list_first_entry(&ch->free_list, |
| 1276 | struct srpt_send_ioctx, free_list); |
| 1277 | list_del(&ioctx->free_list); |
| 1278 | } |
| 1279 | spin_unlock_irqrestore(&ch->spinlock, flags); |
| 1280 | |
| 1281 | if (!ioctx) |
| 1282 | return ioctx; |
| 1283 | |
| 1284 | BUG_ON(ioctx->ch != ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1285 | spin_lock_init(&ioctx->spinlock); |
| 1286 | ioctx->state = SRPT_STATE_NEW; |
| 1287 | ioctx->n_rbuf = 0; |
| 1288 | ioctx->rbufs = NULL; |
| 1289 | ioctx->n_rdma = 0; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1290 | ioctx->n_rdma_wrs = 0; |
| 1291 | ioctx->rdma_wrs = NULL; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1292 | ioctx->mapped_sg_count = 0; |
| 1293 | init_completion(&ioctx->tx_done); |
| 1294 | ioctx->queue_status_only = false; |
| 1295 | /* |
| 1296 | * transport_init_se_cmd() does not initialize all fields, so do it |
| 1297 | * here. |
| 1298 | */ |
| 1299 | memset(&ioctx->cmd, 0, sizeof(ioctx->cmd)); |
| 1300 | memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data)); |
| 1301 | |
| 1302 | return ioctx; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1306 | * srpt_abort_cmd() - Abort a SCSI command. |
| 1307 | * @ioctx: I/O context associated with the SCSI command. |
| 1308 | * @context: Preferred execution context. |
| 1309 | */ |
| 1310 | static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx) |
| 1311 | { |
| 1312 | enum srpt_command_state state; |
| 1313 | unsigned long flags; |
| 1314 | |
| 1315 | BUG_ON(!ioctx); |
| 1316 | |
| 1317 | /* |
| 1318 | * 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] | 1319 | * the ib_srpt driver, change the state to the next state. |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1320 | */ |
| 1321 | |
| 1322 | spin_lock_irqsave(&ioctx->spinlock, flags); |
| 1323 | state = ioctx->state; |
| 1324 | switch (state) { |
| 1325 | case SRPT_STATE_NEED_DATA: |
| 1326 | ioctx->state = SRPT_STATE_DATA_IN; |
| 1327 | break; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1328 | case SRPT_STATE_CMD_RSP_SENT: |
| 1329 | case SRPT_STATE_MGMT_RSP_SENT: |
| 1330 | ioctx->state = SRPT_STATE_DONE; |
| 1331 | break; |
| 1332 | default: |
Bart Van Assche | 49f4016 | 2016-02-11 11:07:11 -0800 | [diff] [blame] | 1333 | WARN_ONCE(true, "%s: unexpected I/O context state %d\n", |
| 1334 | __func__, state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1335 | break; |
| 1336 | } |
| 1337 | spin_unlock_irqrestore(&ioctx->spinlock, flags); |
| 1338 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1339 | 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] | 1340 | ioctx->cmd.tag); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1341 | |
| 1342 | switch (state) { |
| 1343 | case SRPT_STATE_NEW: |
| 1344 | case SRPT_STATE_DATA_IN: |
| 1345 | case SRPT_STATE_MGMT: |
Bart Van Assche | 49f4016 | 2016-02-11 11:07:11 -0800 | [diff] [blame] | 1346 | case SRPT_STATE_DONE: |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1347 | /* |
| 1348 | * Do nothing - defer abort processing until |
| 1349 | * srpt_queue_response() is invoked. |
| 1350 | */ |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1351 | break; |
| 1352 | case SRPT_STATE_NEED_DATA: |
Bart Van Assche | 49f4016 | 2016-02-11 11:07:11 -0800 | [diff] [blame] | 1353 | pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag); |
| 1354 | transport_generic_request_failure(&ioctx->cmd, |
| 1355 | TCM_CHECK_CONDITION_ABORT_CMD); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1356 | break; |
| 1357 | case SRPT_STATE_CMD_RSP_SENT: |
| 1358 | /* |
| 1359 | * SRP_RSP sending failed or the SRP_RSP send completion has |
| 1360 | * not been received in time. |
| 1361 | */ |
| 1362 | srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx); |
Bart Van Assche | 49f4016 | 2016-02-11 11:07:11 -0800 | [diff] [blame] | 1363 | transport_generic_free_cmd(&ioctx->cmd, 0); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1364 | break; |
| 1365 | case SRPT_STATE_MGMT_RSP_SENT: |
Bart Van Assche | 49f4016 | 2016-02-11 11:07:11 -0800 | [diff] [blame] | 1366 | transport_generic_free_cmd(&ioctx->cmd, 0); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1367 | break; |
| 1368 | default: |
Grant Grundler | 532ec6f | 2013-03-26 21:48:28 +0000 | [diff] [blame] | 1369 | WARN(1, "Unexpected command state (%d)", state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | } |
| 1372 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1373 | return state; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
Christoph Hellwig | e672a47 | 2012-07-08 15:58:43 -0400 | [diff] [blame] | 1377 | * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping |
| 1378 | * 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] | 1379 | * 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] | 1380 | * be cleaned up. |
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 | 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] | 1383 | { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1384 | struct srpt_rdma_ch *ch = cq->cq_context; |
| 1385 | struct srpt_send_ioctx *ioctx = |
Bart Van Assche | 19f5729 | 2015-12-31 09:56:43 +0100 | [diff] [blame] | 1386 | container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1387 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1388 | WARN_ON(ioctx->n_rdma <= 0); |
| 1389 | atomic_add(ioctx->n_rdma, &ch->sq_wr_avail); |
| 1390 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1391 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 1392 | pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n", |
| 1393 | ioctx, wc->status); |
| 1394 | srpt_abort_cmd(ioctx); |
| 1395 | return; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1396 | } |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1397 | |
| 1398 | if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA, |
| 1399 | SRPT_STATE_DATA_IN)) |
| 1400 | target_execute_cmd(&ioctx->cmd); |
| 1401 | else |
| 1402 | pr_err("%s[%d]: wrong state = %d\n", __func__, |
| 1403 | __LINE__, srpt_get_cmd_state(ioctx)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1406 | 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] | 1407 | { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1408 | struct srpt_send_ioctx *ioctx = |
Bart Van Assche | 19f5729 | 2015-12-31 09:56:43 +0100 | [diff] [blame] | 1409 | container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1410 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1411 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
Bart Van Assche | 49f4016 | 2016-02-11 11:07:11 -0800 | [diff] [blame] | 1412 | /* |
| 1413 | * Note: if an RDMA write error completion is received that |
| 1414 | * means that a SEND also has been posted. Defer further |
| 1415 | * processing of the associated command until the send error |
| 1416 | * completion has been received. |
| 1417 | */ |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1418 | pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n", |
| 1419 | ioctx, wc->status); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | /** |
| 1424 | * srpt_build_cmd_rsp() - Build an SRP_RSP response. |
| 1425 | * @ch: RDMA channel through which the request has been received. |
| 1426 | * @ioctx: I/O context associated with the SRP_CMD request. The response will |
| 1427 | * be built in the buffer ioctx->buf points at and hence this function will |
| 1428 | * overwrite the request data. |
| 1429 | * @tag: tag of the request for which this response is being generated. |
| 1430 | * @status: value for the STATUS field of the SRP_RSP information unit. |
| 1431 | * |
| 1432 | * Returns the size in bytes of the SRP_RSP response. |
| 1433 | * |
| 1434 | * An SRP_RSP response contains a SCSI status or service response. See also |
| 1435 | * section 6.9 in the SRP r16a document for the format of an SRP_RSP |
| 1436 | * response. See also SPC-2 for more information about sense data. |
| 1437 | */ |
| 1438 | static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch, |
| 1439 | struct srpt_send_ioctx *ioctx, u64 tag, |
| 1440 | int status) |
| 1441 | { |
| 1442 | struct srp_rsp *srp_rsp; |
| 1443 | const u8 *sense_data; |
| 1444 | int sense_data_len, max_sense_len; |
| 1445 | |
| 1446 | /* |
| 1447 | * The lowest bit of all SAM-3 status codes is zero (see also |
| 1448 | * paragraph 5.3 in SAM-3). |
| 1449 | */ |
| 1450 | WARN_ON(status & 1); |
| 1451 | |
| 1452 | srp_rsp = ioctx->ioctx.buf; |
| 1453 | BUG_ON(!srp_rsp); |
| 1454 | |
| 1455 | sense_data = ioctx->sense_data; |
| 1456 | sense_data_len = ioctx->cmd.scsi_sense_length; |
| 1457 | WARN_ON(sense_data_len > sizeof(ioctx->sense_data)); |
| 1458 | |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 1459 | memset(srp_rsp, 0, sizeof(*srp_rsp)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1460 | srp_rsp->opcode = SRP_RSP; |
| 1461 | srp_rsp->req_lim_delta = |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 1462 | cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1463 | srp_rsp->tag = tag; |
| 1464 | srp_rsp->status = status; |
| 1465 | |
| 1466 | if (sense_data_len) { |
| 1467 | BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp)); |
| 1468 | max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp); |
| 1469 | if (sense_data_len > max_sense_len) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1470 | pr_warn("truncated sense data from %d to %d" |
| 1471 | " bytes\n", sense_data_len, max_sense_len); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1472 | sense_data_len = max_sense_len; |
| 1473 | } |
| 1474 | |
| 1475 | srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID; |
| 1476 | srp_rsp->sense_data_len = cpu_to_be32(sense_data_len); |
| 1477 | memcpy(srp_rsp + 1, sense_data, sense_data_len); |
| 1478 | } |
| 1479 | |
| 1480 | return sizeof(*srp_rsp) + sense_data_len; |
| 1481 | } |
| 1482 | |
| 1483 | /** |
| 1484 | * srpt_build_tskmgmt_rsp() - Build a task management response. |
| 1485 | * @ch: RDMA channel through which the request has been received. |
| 1486 | * @ioctx: I/O context in which the SRP_RSP response will be built. |
| 1487 | * @rsp_code: RSP_CODE that will be stored in the response. |
| 1488 | * @tag: Tag of the request for which this response is being generated. |
| 1489 | * |
| 1490 | * Returns the size in bytes of the SRP_RSP response. |
| 1491 | * |
| 1492 | * An SRP_RSP response contains a SCSI status or service response. See also |
| 1493 | * section 6.9 in the SRP r16a document for the format of an SRP_RSP |
| 1494 | * response. |
| 1495 | */ |
| 1496 | static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch, |
| 1497 | struct srpt_send_ioctx *ioctx, |
| 1498 | u8 rsp_code, u64 tag) |
| 1499 | { |
| 1500 | struct srp_rsp *srp_rsp; |
| 1501 | int resp_data_len; |
| 1502 | int resp_len; |
| 1503 | |
Jack Wang | c807f64 | 2013-09-30 10:09:05 +0200 | [diff] [blame] | 1504 | resp_data_len = 4; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1505 | resp_len = sizeof(*srp_rsp) + resp_data_len; |
| 1506 | |
| 1507 | srp_rsp = ioctx->ioctx.buf; |
| 1508 | BUG_ON(!srp_rsp); |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 1509 | memset(srp_rsp, 0, sizeof(*srp_rsp)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1510 | |
| 1511 | srp_rsp->opcode = SRP_RSP; |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 1512 | srp_rsp->req_lim_delta = |
| 1513 | cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1514 | srp_rsp->tag = tag; |
| 1515 | |
Jack Wang | c807f64 | 2013-09-30 10:09:05 +0200 | [diff] [blame] | 1516 | srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID; |
| 1517 | srp_rsp->resp_data_len = cpu_to_be32(resp_data_len); |
| 1518 | srp_rsp->data[3] = rsp_code; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1519 | |
| 1520 | return resp_len; |
| 1521 | } |
| 1522 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1523 | static int srpt_check_stop_free(struct se_cmd *cmd) |
| 1524 | { |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1525 | struct srpt_send_ioctx *ioctx = container_of(cmd, |
| 1526 | struct srpt_send_ioctx, cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1527 | |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 1528 | return target_put_sess_cmd(&ioctx->cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | /** |
| 1532 | * srpt_handle_cmd() - Process SRP_CMD. |
| 1533 | */ |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame] | 1534 | static void srpt_handle_cmd(struct srpt_rdma_ch *ch, |
| 1535 | struct srpt_recv_ioctx *recv_ioctx, |
| 1536 | struct srpt_send_ioctx *send_ioctx) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1537 | { |
| 1538 | struct se_cmd *cmd; |
| 1539 | struct srp_cmd *srp_cmd; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1540 | u64 data_len; |
| 1541 | enum dma_data_direction dir; |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1542 | int rc; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1543 | |
| 1544 | BUG_ON(!send_ioctx); |
| 1545 | |
| 1546 | srp_cmd = recv_ioctx->ioctx.buf; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1547 | cmd = &send_ioctx->cmd; |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 1548 | cmd->tag = srp_cmd->tag; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1549 | |
| 1550 | switch (srp_cmd->task_attr) { |
| 1551 | case SRP_CMD_SIMPLE_Q: |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1552 | cmd->sam_task_attr = TCM_SIMPLE_TAG; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1553 | break; |
| 1554 | case SRP_CMD_ORDERED_Q: |
| 1555 | default: |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1556 | cmd->sam_task_attr = TCM_ORDERED_TAG; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1557 | break; |
| 1558 | case SRP_CMD_HEAD_OF_Q: |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1559 | cmd->sam_task_attr = TCM_HEAD_TAG; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1560 | break; |
| 1561 | case SRP_CMD_ACA: |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1562 | cmd->sam_task_attr = TCM_ACA_TAG; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1563 | break; |
| 1564 | } |
| 1565 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1566 | if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1567 | pr_err("0x%llx: parsing SRP descriptor table failed.\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1568 | srp_cmd->tag); |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame] | 1569 | goto release_ioctx; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1572 | rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb, |
Bart Van Assche | e1dd413 | 2016-02-11 11:05:19 -0800 | [diff] [blame] | 1573 | &send_ioctx->sense_data[0], |
| 1574 | scsilun_to_int(&srp_cmd->lun), data_len, |
| 1575 | TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF); |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1576 | if (rc != 0) { |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame] | 1577 | pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc, |
| 1578 | srp_cmd->tag); |
| 1579 | goto release_ioctx; |
Nicholas Bellinger | 187e70a | 2012-03-17 20:12:36 -0700 | [diff] [blame] | 1580 | } |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame] | 1581 | return; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1582 | |
Bart Van Assche | 2c7f37f | 2016-02-11 11:06:55 -0800 | [diff] [blame] | 1583 | release_ioctx: |
| 1584 | send_ioctx->state = SRPT_STATE_DONE; |
| 1585 | srpt_release_cmd(cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1588 | static int srp_tmr_to_tcm(int fn) |
| 1589 | { |
| 1590 | switch (fn) { |
| 1591 | case SRP_TSK_ABORT_TASK: |
| 1592 | return TMR_ABORT_TASK; |
| 1593 | case SRP_TSK_ABORT_TASK_SET: |
| 1594 | return TMR_ABORT_TASK_SET; |
| 1595 | case SRP_TSK_CLEAR_TASK_SET: |
| 1596 | return TMR_CLEAR_TASK_SET; |
| 1597 | case SRP_TSK_LUN_RESET: |
| 1598 | return TMR_LUN_RESET; |
| 1599 | case SRP_TSK_CLEAR_ACA: |
| 1600 | return TMR_CLEAR_ACA; |
| 1601 | default: |
| 1602 | return -1; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | /** |
| 1607 | * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit. |
| 1608 | * |
| 1609 | * Returns 0 if and only if the request will be processed by the target core. |
| 1610 | * |
| 1611 | * For more information about SRP_TSK_MGMT information units, see also section |
| 1612 | * 6.7 in the SRP r16a document. |
| 1613 | */ |
| 1614 | static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch, |
| 1615 | struct srpt_recv_ioctx *recv_ioctx, |
| 1616 | struct srpt_send_ioctx *send_ioctx) |
| 1617 | { |
| 1618 | struct srp_tsk_mgmt *srp_tsk; |
| 1619 | struct se_cmd *cmd; |
Nicholas Bellinger | 3e4f574 | 2012-11-28 01:38:04 -0800 | [diff] [blame] | 1620 | struct se_session *sess = ch->sess; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1621 | int tcm_tmr; |
Nicholas Bellinger | 3e4f574 | 2012-11-28 01:38:04 -0800 | [diff] [blame] | 1622 | int rc; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1623 | |
| 1624 | BUG_ON(!send_ioctx); |
| 1625 | |
| 1626 | srp_tsk = recv_ioctx->ioctx.buf; |
| 1627 | cmd = &send_ioctx->cmd; |
| 1628 | |
| 1629 | pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld" |
| 1630 | " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func, |
| 1631 | srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess); |
| 1632 | |
| 1633 | srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT); |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 1634 | send_ioctx->cmd.tag = srp_tsk->tag; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1635 | tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func); |
Bart Van Assche | e1dd413 | 2016-02-11 11:05:19 -0800 | [diff] [blame] | 1636 | rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, |
| 1637 | scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr, |
| 1638 | GFP_KERNEL, srp_tsk->task_tag, |
| 1639 | TARGET_SCF_ACK_KREF); |
Nicholas Bellinger | 3e4f574 | 2012-11-28 01:38:04 -0800 | [diff] [blame] | 1640 | if (rc != 0) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1641 | send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1642 | goto fail; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1643 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1644 | return; |
| 1645 | fail: |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1646 | transport_send_check_condition_and_sense(cmd, 0, 0); // XXX: |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /** |
| 1650 | * srpt_handle_new_iu() - Process a newly received information unit. |
| 1651 | * @ch: RDMA channel through which the information unit has been received. |
| 1652 | * @ioctx: SRPT I/O context associated with the information unit. |
| 1653 | */ |
| 1654 | static void srpt_handle_new_iu(struct srpt_rdma_ch *ch, |
| 1655 | struct srpt_recv_ioctx *recv_ioctx, |
| 1656 | struct srpt_send_ioctx *send_ioctx) |
| 1657 | { |
| 1658 | struct srp_cmd *srp_cmd; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1659 | |
| 1660 | BUG_ON(!ch); |
| 1661 | BUG_ON(!recv_ioctx); |
| 1662 | |
| 1663 | ib_dma_sync_single_for_cpu(ch->sport->sdev->device, |
| 1664 | recv_ioctx->ioctx.dma, srp_max_req_size, |
| 1665 | DMA_FROM_DEVICE); |
| 1666 | |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 1667 | if (unlikely(ch->state == CH_CONNECTING)) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1668 | list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list); |
| 1669 | goto out; |
| 1670 | } |
| 1671 | |
Bart Van Assche | 33912d7 | 2016-02-11 11:04:43 -0800 | [diff] [blame] | 1672 | if (unlikely(ch->state != CH_LIVE)) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1673 | goto out; |
| 1674 | |
| 1675 | srp_cmd = recv_ioctx->ioctx.buf; |
| 1676 | if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) { |
| 1677 | if (!send_ioctx) |
| 1678 | send_ioctx = srpt_get_send_ioctx(ch); |
| 1679 | if (unlikely(!send_ioctx)) { |
| 1680 | list_add_tail(&recv_ioctx->wait_list, |
| 1681 | &ch->cmd_wait_list); |
| 1682 | goto out; |
| 1683 | } |
| 1684 | } |
| 1685 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1686 | switch (srp_cmd->opcode) { |
| 1687 | case SRP_CMD: |
| 1688 | srpt_handle_cmd(ch, recv_ioctx, send_ioctx); |
| 1689 | break; |
| 1690 | case SRP_TSK_MGMT: |
| 1691 | srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx); |
| 1692 | break; |
| 1693 | case SRP_I_LOGOUT: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1694 | pr_err("Not yet implemented: SRP_I_LOGOUT\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1695 | break; |
| 1696 | case SRP_CRED_RSP: |
| 1697 | pr_debug("received SRP_CRED_RSP\n"); |
| 1698 | break; |
| 1699 | case SRP_AER_RSP: |
| 1700 | pr_debug("received SRP_AER_RSP\n"); |
| 1701 | break; |
| 1702 | case SRP_RSP: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1703 | pr_err("Received SRP_RSP\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | default: |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1706 | pr_err("received IU with unknown opcode 0x%x\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1707 | srp_cmd->opcode); |
| 1708 | break; |
| 1709 | } |
| 1710 | |
| 1711 | srpt_post_recv(ch->sport->sdev, recv_ioctx); |
| 1712 | out: |
| 1713 | return; |
| 1714 | } |
| 1715 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1716 | 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] | 1717 | { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1718 | struct srpt_rdma_ch *ch = cq->cq_context; |
| 1719 | struct srpt_recv_ioctx *ioctx = |
| 1720 | container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1721 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1722 | if (wc->status == IB_WC_SUCCESS) { |
| 1723 | int req_lim; |
| 1724 | |
| 1725 | req_lim = atomic_dec_return(&ch->req_lim); |
| 1726 | if (unlikely(req_lim < 0)) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1727 | pr_err("req_lim = %d < 0\n", req_lim); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1728 | srpt_handle_new_iu(ch, ioctx, NULL); |
| 1729 | } else { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1730 | pr_info("receiving failed for ioctx %p with status %d\n", |
| 1731 | ioctx, wc->status); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | |
Bart Van Assche | 539b324 | 2016-02-11 11:09:50 -0800 | [diff] [blame] | 1735 | /* |
| 1736 | * This function must be called from the context in which RDMA completions are |
| 1737 | * processed because it accesses the wait list without protection against |
| 1738 | * access from other threads. |
| 1739 | */ |
| 1740 | static void srpt_process_wait_list(struct srpt_rdma_ch *ch) |
| 1741 | { |
| 1742 | struct srpt_send_ioctx *ioctx; |
| 1743 | |
| 1744 | while (!list_empty(&ch->cmd_wait_list) && |
| 1745 | ch->state >= CH_LIVE && |
| 1746 | (ioctx = srpt_get_send_ioctx(ch)) != NULL) { |
| 1747 | struct srpt_recv_ioctx *recv_ioctx; |
| 1748 | |
| 1749 | recv_ioctx = list_first_entry(&ch->cmd_wait_list, |
| 1750 | struct srpt_recv_ioctx, |
| 1751 | wait_list); |
| 1752 | list_del(&recv_ioctx->wait_list); |
| 1753 | srpt_handle_new_iu(ch, recv_ioctx, ioctx); |
| 1754 | } |
| 1755 | } |
| 1756 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1757 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1758 | * Note: Although this has not yet been observed during tests, at least in |
| 1759 | * theory it is possible that the srpt_get_send_ioctx() call invoked by |
| 1760 | * srpt_handle_new_iu() fails. This is possible because the req_lim_delta |
| 1761 | * value in each response is set to one, and it is possible that this response |
| 1762 | * makes the initiator send a new request before the send completion for that |
| 1763 | * response has been processed. This could e.g. happen if the call to |
| 1764 | * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or |
| 1765 | * if IB retransmission causes generation of the send completion to be |
| 1766 | * delayed. Incoming information units for which srpt_get_send_ioctx() fails |
| 1767 | * are queued on cmd_wait_list. The code below processes these delayed |
| 1768 | * requests one at a time. |
| 1769 | */ |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1770 | 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] | 1771 | { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1772 | struct srpt_rdma_ch *ch = cq->cq_context; |
| 1773 | struct srpt_send_ioctx *ioctx = |
| 1774 | container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe); |
| 1775 | enum srpt_command_state state; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1776 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1777 | state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); |
| 1778 | |
| 1779 | WARN_ON(state != SRPT_STATE_CMD_RSP_SENT && |
| 1780 | state != SRPT_STATE_MGMT_RSP_SENT); |
| 1781 | |
| 1782 | atomic_inc(&ch->sq_wr_avail); |
| 1783 | |
Bart Van Assche | 49f4016 | 2016-02-11 11:07:11 -0800 | [diff] [blame] | 1784 | if (wc->status != IB_WC_SUCCESS) |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1785 | pr_info("sending response for ioctx 0x%p failed" |
| 1786 | " with status %d\n", ioctx, wc->status); |
| 1787 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1788 | if (state != SRPT_STATE_DONE) { |
| 1789 | srpt_unmap_sg_to_ib_sge(ch, ioctx); |
| 1790 | transport_generic_free_cmd(&ioctx->cmd, 0); |
| 1791 | } else { |
| 1792 | pr_err("IB completion has been received too late for" |
| 1793 | " wr_id = %u.\n", ioctx->ioctx.index); |
| 1794 | } |
| 1795 | |
Bart Van Assche | 539b324 | 2016-02-11 11:09:50 -0800 | [diff] [blame] | 1796 | srpt_process_wait_list(ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1799 | /** |
| 1800 | * srpt_create_ch_ib() - Create receive and send completion queues. |
| 1801 | */ |
| 1802 | static int srpt_create_ch_ib(struct srpt_rdma_ch *ch) |
| 1803 | { |
| 1804 | struct ib_qp_init_attr *qp_init; |
| 1805 | struct srpt_port *sport = ch->sport; |
| 1806 | struct srpt_device *sdev = sport->sdev; |
| 1807 | u32 srp_sq_size = sport->port_attrib.srp_sq_size; |
| 1808 | int ret; |
| 1809 | |
| 1810 | WARN_ON(ch->rq_size < 1); |
| 1811 | |
| 1812 | ret = -ENOMEM; |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 1813 | qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1814 | if (!qp_init) |
| 1815 | goto out; |
| 1816 | |
Bart Van Assche | ab477c1 | 2014-10-19 18:05:33 +0300 | [diff] [blame] | 1817 | retry: |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1818 | ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size, |
| 1819 | 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1820 | if (IS_ERR(ch->cq)) { |
| 1821 | ret = PTR_ERR(ch->cq); |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1822 | pr_err("failed to create CQ cqe= %d ret= %d\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1823 | ch->rq_size + srp_sq_size, ret); |
| 1824 | goto out; |
| 1825 | } |
| 1826 | |
| 1827 | qp_init->qp_context = (void *)ch; |
| 1828 | qp_init->event_handler |
| 1829 | = (void(*)(struct ib_event *, void*))srpt_qp_event; |
| 1830 | qp_init->send_cq = ch->cq; |
| 1831 | qp_init->recv_cq = ch->cq; |
| 1832 | qp_init->srq = sdev->srq; |
| 1833 | qp_init->sq_sig_type = IB_SIGNAL_REQ_WR; |
| 1834 | qp_init->qp_type = IB_QPT_RC; |
| 1835 | qp_init->cap.max_send_wr = srp_sq_size; |
| 1836 | qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE; |
| 1837 | |
| 1838 | ch->qp = ib_create_qp(sdev->pd, qp_init); |
| 1839 | if (IS_ERR(ch->qp)) { |
| 1840 | ret = PTR_ERR(ch->qp); |
Bart Van Assche | ab477c1 | 2014-10-19 18:05:33 +0300 | [diff] [blame] | 1841 | if (ret == -ENOMEM) { |
| 1842 | srp_sq_size /= 2; |
| 1843 | if (srp_sq_size >= MIN_SRPT_SQ_SIZE) { |
| 1844 | ib_destroy_cq(ch->cq); |
| 1845 | goto retry; |
| 1846 | } |
| 1847 | } |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 1848 | pr_err("failed to create_qp ret= %d\n", ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1849 | goto err_destroy_cq; |
| 1850 | } |
| 1851 | |
| 1852 | atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr); |
| 1853 | |
| 1854 | pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n", |
| 1855 | __func__, ch->cq->cqe, qp_init->cap.max_send_sge, |
| 1856 | qp_init->cap.max_send_wr, ch->cm_id); |
| 1857 | |
| 1858 | ret = srpt_init_ch_qp(ch, ch->qp); |
| 1859 | if (ret) |
| 1860 | goto err_destroy_qp; |
| 1861 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1862 | out: |
| 1863 | kfree(qp_init); |
| 1864 | return ret; |
| 1865 | |
| 1866 | err_destroy_qp: |
| 1867 | ib_destroy_qp(ch->qp); |
| 1868 | err_destroy_cq: |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1869 | ib_free_cq(ch->cq); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1870 | goto out; |
| 1871 | } |
| 1872 | |
| 1873 | static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch) |
| 1874 | { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1875 | ib_destroy_qp(ch->qp); |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 1876 | ib_free_cq(ch->cq); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | /** |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1880 | * srpt_close_ch() - Close an RDMA channel. |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1881 | * |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1882 | * Make sure all resources associated with the channel will be deallocated at |
| 1883 | * an appropriate time. |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1884 | * |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1885 | * Returns true if and only if the channel state has been modified into |
| 1886 | * CH_DRAINING. |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1887 | */ |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1888 | static bool srpt_close_ch(struct srpt_rdma_ch *ch) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1889 | { |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1890 | int ret; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1891 | |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1892 | if (!srpt_set_ch_state(ch, CH_DRAINING)) { |
| 1893 | pr_debug("%s-%d: already closed\n", ch->sess_name, |
| 1894 | ch->qp->qp_num); |
| 1895 | return false; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1896 | } |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1897 | |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1898 | kref_get(&ch->kref); |
| 1899 | |
| 1900 | ret = srpt_ch_qp_err(ch); |
| 1901 | if (ret < 0) |
| 1902 | pr_err("%s-%d: changing queue pair into error state failed: %d\n", |
| 1903 | ch->sess_name, ch->qp->qp_num, ret); |
| 1904 | |
| 1905 | pr_debug("%s-%d: queued zerolength write\n", ch->sess_name, |
| 1906 | ch->qp->qp_num); |
| 1907 | ret = srpt_zerolength_write(ch); |
| 1908 | if (ret < 0) { |
| 1909 | pr_err("%s-%d: queuing zero-length write failed: %d\n", |
| 1910 | ch->sess_name, ch->qp->qp_num, ret); |
| 1911 | if (srpt_set_ch_state(ch, CH_DISCONNECTED)) |
| 1912 | schedule_work(&ch->release_work); |
| 1913 | else |
| 1914 | WARN_ON_ONCE(true); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1915 | } |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1916 | |
| 1917 | kref_put(&ch->kref, srpt_free_ch); |
| 1918 | |
| 1919 | return true; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1922 | /* |
| 1923 | * Change the channel state into CH_DISCONNECTING. If a channel has not yet |
| 1924 | * reached the connected state, close it. If a channel is in the connected |
| 1925 | * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is |
| 1926 | * the responsibility of the caller to ensure that this function is not |
| 1927 | * invoked concurrently with the code that accepts a connection. This means |
| 1928 | * that this function must either be invoked from inside a CM callback |
| 1929 | * 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] | 1930 | */ |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1931 | static int srpt_disconnect_ch(struct srpt_rdma_ch *ch) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1932 | { |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1933 | int ret; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1934 | |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1935 | if (!srpt_set_ch_state(ch, CH_DISCONNECTING)) |
| 1936 | return -ENOTCONN; |
| 1937 | |
| 1938 | ret = ib_send_cm_dreq(ch->cm_id, NULL, 0); |
| 1939 | if (ret < 0) |
| 1940 | ret = ib_send_cm_drep(ch->cm_id, NULL, 0); |
| 1941 | |
| 1942 | if (ret < 0 && srpt_close_ch(ch)) |
| 1943 | ret = 0; |
| 1944 | |
| 1945 | return ret; |
| 1946 | } |
| 1947 | |
| 1948 | static void __srpt_close_all_ch(struct srpt_device *sdev) |
| 1949 | { |
| 1950 | struct srpt_rdma_ch *ch; |
| 1951 | |
| 1952 | lockdep_assert_held(&sdev->mutex); |
| 1953 | |
| 1954 | list_for_each_entry(ch, &sdev->rch_list, list) { |
| 1955 | if (srpt_disconnect_ch(ch) >= 0) |
| 1956 | pr_info("Closing channel %s-%d because target %s has been disabled\n", |
| 1957 | ch->sess_name, ch->qp->qp_num, |
| 1958 | sdev->device->name); |
| 1959 | srpt_close_ch(ch); |
| 1960 | } |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | /** |
Nicholas Bellinger | 1d19f78 | 2013-05-15 01:30:01 -0700 | [diff] [blame] | 1964 | * srpt_shutdown_session() - Whether or not a session may be shut down. |
| 1965 | */ |
| 1966 | static int srpt_shutdown_session(struct se_session *se_sess) |
| 1967 | { |
Bart Van Assche | 8893625 | 2016-02-11 11:05:58 -0800 | [diff] [blame] | 1968 | return 1; |
Nicholas Bellinger | 1d19f78 | 2013-05-15 01:30:01 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1971 | static void srpt_free_ch(struct kref *kref) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1972 | { |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1973 | 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] | 1974 | |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 1975 | kfree(ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | static void srpt_release_channel_work(struct work_struct *w) |
| 1979 | { |
| 1980 | struct srpt_rdma_ch *ch; |
| 1981 | struct srpt_device *sdev; |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1982 | struct se_session *se_sess; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1983 | |
| 1984 | ch = container_of(w, struct srpt_rdma_ch, release_work); |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 1985 | pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name, |
| 1986 | ch->qp->qp_num, ch->release_done); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1987 | |
| 1988 | sdev = ch->sport->sdev; |
| 1989 | BUG_ON(!sdev); |
| 1990 | |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1991 | se_sess = ch->sess; |
| 1992 | BUG_ON(!se_sess); |
| 1993 | |
Bart Van Assche | 8893625 | 2016-02-11 11:05:58 -0800 | [diff] [blame] | 1994 | target_sess_cmd_list_set_waiting(se_sess); |
Joern Engel | be646c2d | 2013-05-15 00:44:07 -0700 | [diff] [blame] | 1995 | target_wait_for_sess_cmds(se_sess); |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 1996 | |
| 1997 | transport_deregister_session_configfs(se_sess); |
| 1998 | transport_deregister_session(se_sess); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 1999 | ch->sess = NULL; |
| 2000 | |
Nicholas Bellinger | 0b41d6c | 2013-09-18 12:48:27 -0700 | [diff] [blame] | 2001 | ib_destroy_cm_id(ch->cm_id); |
| 2002 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2003 | srpt_destroy_ch_ib(ch); |
| 2004 | |
| 2005 | srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, |
| 2006 | ch->sport->sdev, ch->rq_size, |
| 2007 | ch->rsp_size, DMA_TO_DEVICE); |
| 2008 | |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2009 | mutex_lock(&sdev->mutex); |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 2010 | list_del_init(&ch->list); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2011 | if (ch->release_done) |
| 2012 | complete(ch->release_done); |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2013 | mutex_unlock(&sdev->mutex); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2014 | |
| 2015 | wake_up(&sdev->ch_releaseQ); |
| 2016 | |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2017 | kref_put(&ch->kref, srpt_free_ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2020 | /** |
| 2021 | * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED. |
| 2022 | * |
| 2023 | * Ownership of the cm_id is transferred to the target session if this |
| 2024 | * functions returns zero. Otherwise the caller remains the owner of cm_id. |
| 2025 | */ |
| 2026 | static int srpt_cm_req_recv(struct ib_cm_id *cm_id, |
| 2027 | struct ib_cm_req_event_param *param, |
| 2028 | void *private_data) |
| 2029 | { |
| 2030 | struct srpt_device *sdev = cm_id->context; |
| 2031 | struct srpt_port *sport = &sdev->port[param->port - 1]; |
| 2032 | struct srp_login_req *req; |
| 2033 | struct srp_login_rsp *rsp; |
| 2034 | struct srp_login_rej *rej; |
| 2035 | struct ib_cm_rep_param *rep_param; |
| 2036 | struct srpt_rdma_ch *ch, *tmp_ch; |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2037 | struct se_node_acl *se_acl; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2038 | u32 it_iu_len; |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2039 | int i, ret = 0; |
| 2040 | unsigned char *p; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2041 | |
| 2042 | WARN_ON_ONCE(irqs_disabled()); |
| 2043 | |
| 2044 | if (WARN_ON(!sdev || !private_data)) |
| 2045 | return -EINVAL; |
| 2046 | |
| 2047 | req = (struct srp_login_req *)private_data; |
| 2048 | |
| 2049 | it_iu_len = be32_to_cpu(req->req_it_iu_len); |
| 2050 | |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2051 | pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx," |
| 2052 | " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d" |
| 2053 | " (guid=0x%llx:0x%llx)\n", |
| 2054 | be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]), |
| 2055 | be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]), |
| 2056 | be64_to_cpu(*(__be64 *)&req->target_port_id[0]), |
| 2057 | be64_to_cpu(*(__be64 *)&req->target_port_id[8]), |
| 2058 | it_iu_len, |
| 2059 | param->port, |
| 2060 | be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]), |
| 2061 | 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] | 2062 | |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2063 | rsp = kzalloc(sizeof(*rsp), GFP_KERNEL); |
| 2064 | rej = kzalloc(sizeof(*rej), GFP_KERNEL); |
| 2065 | rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2066 | |
| 2067 | if (!rsp || !rej || !rep_param) { |
| 2068 | ret = -ENOMEM; |
| 2069 | goto out; |
| 2070 | } |
| 2071 | |
| 2072 | if (it_iu_len > srp_max_req_size || it_iu_len < 64) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2073 | rej->reason = cpu_to_be32( |
| 2074 | SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2075 | ret = -EINVAL; |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2076 | pr_err("rejected SRP_LOGIN_REQ because its" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2077 | " length (%d bytes) is out of range (%d .. %d)\n", |
| 2078 | it_iu_len, 64, srp_max_req_size); |
| 2079 | goto reject; |
| 2080 | } |
| 2081 | |
| 2082 | if (!sport->enabled) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2083 | rej->reason = cpu_to_be32( |
| 2084 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2085 | ret = -EINVAL; |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2086 | pr_err("rejected SRP_LOGIN_REQ because the target port" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2087 | " has not yet been enabled\n"); |
| 2088 | goto reject; |
| 2089 | } |
| 2090 | |
| 2091 | if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) { |
| 2092 | rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN; |
| 2093 | |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2094 | mutex_lock(&sdev->mutex); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2095 | |
| 2096 | list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) { |
| 2097 | if (!memcmp(ch->i_port_id, req->initiator_port_id, 16) |
| 2098 | && !memcmp(ch->t_port_id, req->target_port_id, 16) |
| 2099 | && param->port == ch->sport->port |
| 2100 | && param->listen_id == ch->sport->sdev->cm_id |
| 2101 | && ch->cm_id) { |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2102 | if (srpt_disconnect_ch(ch) < 0) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2103 | continue; |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2104 | pr_info("Relogin - closed existing channel %s\n", |
| 2105 | ch->sess_name); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2106 | rsp->rsp_flags = |
| 2107 | SRP_LOGIN_RSP_MULTICHAN_TERMINATED; |
| 2108 | } |
| 2109 | } |
| 2110 | |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2111 | mutex_unlock(&sdev->mutex); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2112 | |
| 2113 | } else |
| 2114 | rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED; |
| 2115 | |
| 2116 | if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid) |
| 2117 | || *(__be64 *)(req->target_port_id + 8) != |
| 2118 | cpu_to_be64(srpt_service_guid)) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2119 | rej->reason = cpu_to_be32( |
| 2120 | SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2121 | ret = -ENOMEM; |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2122 | pr_err("rejected SRP_LOGIN_REQ because it" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2123 | " has an invalid target port identifier.\n"); |
| 2124 | goto reject; |
| 2125 | } |
| 2126 | |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2127 | ch = kzalloc(sizeof(*ch), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2128 | if (!ch) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2129 | rej->reason = cpu_to_be32( |
| 2130 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2131 | pr_err("rejected SRP_LOGIN_REQ because no memory.\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2132 | ret = -ENOMEM; |
| 2133 | goto reject; |
| 2134 | } |
| 2135 | |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2136 | kref_init(&ch->kref); |
| 2137 | ch->zw_cqe.done = srpt_zerolength_write_done; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2138 | INIT_WORK(&ch->release_work, srpt_release_channel_work); |
| 2139 | memcpy(ch->i_port_id, req->initiator_port_id, 16); |
| 2140 | memcpy(ch->t_port_id, req->target_port_id, 16); |
| 2141 | ch->sport = &sdev->port[param->port - 1]; |
| 2142 | ch->cm_id = cm_id; |
Bart Van Assche | 2739b59 | 2016-02-11 11:07:49 -0800 | [diff] [blame] | 2143 | cm_id->context = ch; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2144 | /* |
| 2145 | * Avoid QUEUE_FULL conditions by limiting the number of buffers used |
| 2146 | * for the SRP protocol to the command queue size. |
| 2147 | */ |
| 2148 | ch->rq_size = SRPT_RQ_SIZE; |
| 2149 | spin_lock_init(&ch->spinlock); |
| 2150 | ch->state = CH_CONNECTING; |
| 2151 | INIT_LIST_HEAD(&ch->cmd_wait_list); |
| 2152 | ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size; |
| 2153 | |
| 2154 | ch->ioctx_ring = (struct srpt_send_ioctx **) |
| 2155 | srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size, |
| 2156 | sizeof(*ch->ioctx_ring[0]), |
| 2157 | ch->rsp_size, DMA_TO_DEVICE); |
| 2158 | if (!ch->ioctx_ring) |
| 2159 | goto free_ch; |
| 2160 | |
| 2161 | INIT_LIST_HEAD(&ch->free_list); |
| 2162 | for (i = 0; i < ch->rq_size; i++) { |
| 2163 | ch->ioctx_ring[i]->ch = ch; |
| 2164 | list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list); |
| 2165 | } |
| 2166 | |
| 2167 | ret = srpt_create_ch_ib(ch); |
| 2168 | if (ret) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2169 | rej->reason = cpu_to_be32( |
| 2170 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2171 | pr_err("rejected SRP_LOGIN_REQ because creating" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2172 | " a new RDMA channel failed.\n"); |
| 2173 | goto free_ring; |
| 2174 | } |
| 2175 | |
| 2176 | ret = srpt_ch_qp_rtr(ch, ch->qp); |
| 2177 | if (ret) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2178 | rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2179 | pr_err("rejected SRP_LOGIN_REQ because enabling" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2180 | " RTR failed (error code = %d)\n", ret); |
| 2181 | goto destroy_ib; |
| 2182 | } |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2183 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2184 | /* |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2185 | * Use the initator port identifier as the session name, when |
| 2186 | * checking against se_node_acl->initiatorname[] this can be |
| 2187 | * with or without preceeding '0x'. |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2188 | */ |
| 2189 | snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx", |
| 2190 | be64_to_cpu(*(__be64 *)ch->i_port_id), |
| 2191 | be64_to_cpu(*(__be64 *)(ch->i_port_id + 8))); |
| 2192 | |
| 2193 | pr_debug("registering session %s\n", ch->sess_name); |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2194 | p = &ch->sess_name[0]; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2195 | |
Nicholas Bellinger | e70beee | 2014-04-02 12:52:38 -0700 | [diff] [blame] | 2196 | ch->sess = transport_init_session(TARGET_PROT_NORMAL); |
Dan Carpenter | 3af3363 | 2011-11-04 21:27:32 +0300 | [diff] [blame] | 2197 | if (IS_ERR(ch->sess)) { |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2198 | rej->reason = cpu_to_be32( |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2199 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2200 | pr_debug("Failed to create session\n"); |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2201 | goto destroy_ib; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2202 | } |
Nicholas Bellinger | f246c94 | 2016-01-07 22:19:21 -0800 | [diff] [blame] | 2203 | |
| 2204 | try_again: |
| 2205 | se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p); |
| 2206 | if (!se_acl) { |
| 2207 | pr_info("Rejected login because no ACL has been" |
| 2208 | " configured yet for initiator %s.\n", ch->sess_name); |
| 2209 | /* |
| 2210 | * XXX: Hack to retry of ch->i_port_id without leading '0x' |
| 2211 | */ |
| 2212 | if (p == &ch->sess_name[0]) { |
| 2213 | p += 2; |
| 2214 | goto try_again; |
| 2215 | } |
| 2216 | rej->reason = cpu_to_be32( |
| 2217 | SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED); |
| 2218 | transport_free_session(ch->sess); |
| 2219 | goto destroy_ib; |
| 2220 | } |
| 2221 | ch->sess->se_node_acl = se_acl; |
| 2222 | |
| 2223 | 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] | 2224 | |
| 2225 | pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess, |
| 2226 | ch->sess_name, ch->cm_id); |
| 2227 | |
| 2228 | /* create srp_login_response */ |
| 2229 | rsp->opcode = SRP_LOGIN_RSP; |
| 2230 | rsp->tag = req->tag; |
| 2231 | rsp->max_it_iu_len = req->req_it_iu_len; |
| 2232 | rsp->max_ti_iu_len = req->req_it_iu_len; |
| 2233 | ch->max_ti_iu_len = it_iu_len; |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2234 | rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
| 2235 | | SRP_BUF_FORMAT_INDIRECT); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2236 | rsp->req_lim_delta = cpu_to_be32(ch->rq_size); |
| 2237 | atomic_set(&ch->req_lim, ch->rq_size); |
| 2238 | atomic_set(&ch->req_lim_delta, 0); |
| 2239 | |
| 2240 | /* create cm reply */ |
| 2241 | rep_param->qp_num = ch->qp->qp_num; |
| 2242 | rep_param->private_data = (void *)rsp; |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2243 | rep_param->private_data_len = sizeof(*rsp); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2244 | rep_param->rnr_retry_count = 7; |
| 2245 | rep_param->flow_control = 1; |
| 2246 | rep_param->failover_accepted = 0; |
| 2247 | rep_param->srq = 1; |
| 2248 | rep_param->responder_resources = 4; |
| 2249 | rep_param->initiator_depth = 4; |
| 2250 | |
| 2251 | ret = ib_send_cm_rep(cm_id, rep_param); |
| 2252 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2253 | pr_err("sending SRP_LOGIN_REQ response failed" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2254 | " (error code = %d)\n", ret); |
| 2255 | goto release_channel; |
| 2256 | } |
| 2257 | |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2258 | mutex_lock(&sdev->mutex); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2259 | list_add_tail(&ch->list, &sdev->rch_list); |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2260 | mutex_unlock(&sdev->mutex); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2261 | |
| 2262 | goto out; |
| 2263 | |
| 2264 | release_channel: |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2265 | srpt_disconnect_ch(ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2266 | transport_deregister_session_configfs(ch->sess); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2267 | transport_deregister_session(ch->sess); |
| 2268 | ch->sess = NULL; |
| 2269 | |
| 2270 | destroy_ib: |
| 2271 | srpt_destroy_ch_ib(ch); |
| 2272 | |
| 2273 | free_ring: |
| 2274 | srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, |
| 2275 | ch->sport->sdev, ch->rq_size, |
| 2276 | ch->rsp_size, DMA_TO_DEVICE); |
| 2277 | free_ch: |
| 2278 | kfree(ch); |
| 2279 | |
| 2280 | reject: |
| 2281 | rej->opcode = SRP_LOGIN_REJ; |
| 2282 | rej->tag = req->tag; |
Vaishali Thakkar | b356c1c | 2015-06-24 10:12:13 +0530 | [diff] [blame] | 2283 | rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
| 2284 | | SRP_BUF_FORMAT_INDIRECT); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2285 | |
| 2286 | ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0, |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2287 | (void *)rej, sizeof(*rej)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2288 | |
| 2289 | out: |
| 2290 | kfree(rep_param); |
| 2291 | kfree(rsp); |
| 2292 | kfree(rej); |
| 2293 | |
| 2294 | return ret; |
| 2295 | } |
| 2296 | |
Bart Van Assche | 2739b59 | 2016-02-11 11:07:49 -0800 | [diff] [blame] | 2297 | static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch, |
| 2298 | enum ib_cm_rej_reason reason, |
| 2299 | const u8 *private_data, |
| 2300 | u8 private_data_len) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2301 | { |
Bart Van Assche | c13c90e | 2016-02-11 11:08:12 -0800 | [diff] [blame] | 2302 | char *priv = NULL; |
| 2303 | int i; |
| 2304 | |
| 2305 | if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1, |
| 2306 | GFP_KERNEL))) { |
| 2307 | for (i = 0; i < private_data_len; i++) |
| 2308 | sprintf(priv + 3 * i, " %02x", private_data[i]); |
| 2309 | } |
| 2310 | pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n", |
| 2311 | ch->sess_name, ch->qp->qp_num, reason, private_data_len ? |
| 2312 | "; private data" : "", priv ? priv : " (?)"); |
| 2313 | kfree(priv); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | /** |
| 2317 | * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event. |
| 2318 | * |
| 2319 | * An IB_CM_RTU_RECEIVED message indicates that the connection is established |
| 2320 | * and that the recipient may begin transmitting (RTU = ready to use). |
| 2321 | */ |
Bart Van Assche | 2739b59 | 2016-02-11 11:07:49 -0800 | [diff] [blame] | 2322 | static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2323 | { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2324 | int ret; |
| 2325 | |
Bart Van Assche | f130c22 | 2016-02-11 11:05:38 -0800 | [diff] [blame] | 2326 | if (srpt_set_ch_state(ch, CH_LIVE)) { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2327 | ret = srpt_ch_qp_rts(ch, ch->qp); |
| 2328 | |
Bart Van Assche | 387add4 | 2016-02-11 11:10:09 -0800 | [diff] [blame^] | 2329 | if (ret == 0) { |
| 2330 | /* Trigger wait list processing. */ |
| 2331 | ret = srpt_zerolength_write(ch); |
| 2332 | WARN_ONCE(ret < 0, "%d\n", ret); |
| 2333 | } else { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2334 | srpt_close_ch(ch); |
Bart Van Assche | 387add4 | 2016-02-11 11:10:09 -0800 | [diff] [blame^] | 2335 | } |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2336 | } |
| 2337 | } |
| 2338 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2339 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2340 | * srpt_cm_handler() - IB connection manager callback function. |
| 2341 | * |
| 2342 | * A non-zero return value will cause the caller destroy the CM ID. |
| 2343 | * |
| 2344 | * Note: srpt_cm_handler() must only return a non-zero value when transferring |
| 2345 | * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning |
| 2346 | * a non-zero value in any other case will trigger a race with the |
| 2347 | * ib_destroy_cm_id() call in srpt_release_channel(). |
| 2348 | */ |
| 2349 | static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) |
| 2350 | { |
Bart Van Assche | 2739b59 | 2016-02-11 11:07:49 -0800 | [diff] [blame] | 2351 | struct srpt_rdma_ch *ch = cm_id->context; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2352 | int ret; |
| 2353 | |
| 2354 | ret = 0; |
| 2355 | switch (event->event) { |
| 2356 | case IB_CM_REQ_RECEIVED: |
| 2357 | ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd, |
| 2358 | event->private_data); |
| 2359 | break; |
| 2360 | case IB_CM_REJ_RECEIVED: |
Bart Van Assche | 2739b59 | 2016-02-11 11:07:49 -0800 | [diff] [blame] | 2361 | srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason, |
| 2362 | event->private_data, |
| 2363 | IB_CM_REJ_PRIVATE_DATA_SIZE); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2364 | break; |
| 2365 | case IB_CM_RTU_RECEIVED: |
| 2366 | case IB_CM_USER_ESTABLISHED: |
Bart Van Assche | 2739b59 | 2016-02-11 11:07:49 -0800 | [diff] [blame] | 2367 | srpt_cm_rtu_recv(ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2368 | break; |
| 2369 | case IB_CM_DREQ_RECEIVED: |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2370 | srpt_disconnect_ch(ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2371 | break; |
| 2372 | case IB_CM_DREP_RECEIVED: |
Bart Van Assche | 2739b59 | 2016-02-11 11:07:49 -0800 | [diff] [blame] | 2373 | pr_info("Received CM DREP message for ch %s-%d.\n", |
| 2374 | ch->sess_name, ch->qp->qp_num); |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2375 | srpt_close_ch(ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2376 | break; |
| 2377 | case IB_CM_TIMEWAIT_EXIT: |
Bart Van Assche | 2739b59 | 2016-02-11 11:07:49 -0800 | [diff] [blame] | 2378 | pr_info("Received CM TimeWait exit for ch %s-%d.\n", |
| 2379 | ch->sess_name, ch->qp->qp_num); |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2380 | srpt_close_ch(ch); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2381 | break; |
| 2382 | case IB_CM_REP_ERROR: |
Bart Van Assche | 2739b59 | 2016-02-11 11:07:49 -0800 | [diff] [blame] | 2383 | pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name, |
| 2384 | ch->qp->qp_num); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2385 | break; |
| 2386 | case IB_CM_DREQ_ERROR: |
Bart Van Assche | 1e20a2a | 2016-02-11 11:07:29 -0800 | [diff] [blame] | 2387 | pr_info("Received CM DREQ ERROR event.\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2388 | break; |
| 2389 | case IB_CM_MRA_RECEIVED: |
Bart Van Assche | 1e20a2a | 2016-02-11 11:07:29 -0800 | [diff] [blame] | 2390 | pr_info("Received CM MRA event\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2391 | break; |
| 2392 | default: |
Bart Van Assche | 1e20a2a | 2016-02-11 11:07:29 -0800 | [diff] [blame] | 2393 | pr_err("received unrecognized CM event %d\n", event->event); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2394 | break; |
| 2395 | } |
| 2396 | |
| 2397 | return ret; |
| 2398 | } |
| 2399 | |
| 2400 | /** |
| 2401 | * srpt_perform_rdmas() - Perform IB RDMA. |
| 2402 | * |
| 2403 | * Returns zero upon success or a negative number upon failure. |
| 2404 | */ |
| 2405 | static int srpt_perform_rdmas(struct srpt_rdma_ch *ch, |
| 2406 | struct srpt_send_ioctx *ioctx) |
| 2407 | { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2408 | struct ib_send_wr *bad_wr; |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2409 | int sq_wr_avail, ret, i; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2410 | enum dma_data_direction dir; |
| 2411 | const int n_rdma = ioctx->n_rdma; |
| 2412 | |
| 2413 | dir = ioctx->cmd.data_direction; |
| 2414 | if (dir == DMA_TO_DEVICE) { |
| 2415 | /* write */ |
| 2416 | ret = -ENOMEM; |
| 2417 | sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail); |
| 2418 | if (sq_wr_avail < 0) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2419 | pr_warn("IB send queue full (needed %d)\n", |
| 2420 | n_rdma); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2421 | goto out; |
| 2422 | } |
| 2423 | } |
| 2424 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2425 | for (i = 0; i < n_rdma; i++) { |
| 2426 | struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2427 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2428 | wr->opcode = (dir == DMA_FROM_DEVICE) ? |
| 2429 | IB_WR_RDMA_WRITE : IB_WR_RDMA_READ; |
| 2430 | |
| 2431 | if (i == n_rdma - 1) { |
| 2432 | /* only get completion event for the last rdma read */ |
| 2433 | if (dir == DMA_TO_DEVICE) { |
| 2434 | wr->send_flags = IB_SEND_SIGNALED; |
| 2435 | ioctx->rdma_cqe.done = srpt_rdma_read_done; |
| 2436 | } else { |
| 2437 | ioctx->rdma_cqe.done = srpt_rdma_write_done; |
| 2438 | } |
| 2439 | wr->wr_cqe = &ioctx->rdma_cqe; |
| 2440 | wr->next = NULL; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2441 | } else { |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2442 | wr->wr_cqe = NULL; |
| 2443 | wr->next = &ioctx->rdma_wrs[i + 1].wr; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2444 | } |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
Christoph Hellwig | 59fae4d | 2015-09-29 13:00:44 +0200 | [diff] [blame] | 2447 | 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] | 2448 | if (ret) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2449 | 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] | 2450 | __func__, __LINE__, ret, i, n_rdma); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2451 | out: |
| 2452 | if (unlikely(dir == DMA_TO_DEVICE && ret < 0)) |
| 2453 | atomic_add(n_rdma, &ch->sq_wr_avail); |
| 2454 | return ret; |
| 2455 | } |
| 2456 | |
| 2457 | /** |
| 2458 | * srpt_xfer_data() - Start data transfer from initiator to target. |
| 2459 | */ |
| 2460 | static int srpt_xfer_data(struct srpt_rdma_ch *ch, |
| 2461 | struct srpt_send_ioctx *ioctx) |
| 2462 | { |
| 2463 | int ret; |
| 2464 | |
| 2465 | ret = srpt_map_sg_to_ib_sge(ch, ioctx); |
| 2466 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2467 | pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2468 | goto out; |
| 2469 | } |
| 2470 | |
| 2471 | ret = srpt_perform_rdmas(ch, ioctx); |
| 2472 | if (ret) { |
| 2473 | if (ret == -EAGAIN || ret == -ENOMEM) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2474 | pr_info("%s[%d] queue full -- ret=%d\n", |
| 2475 | __func__, __LINE__, ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2476 | else |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2477 | pr_err("%s[%d] fatal error -- ret=%d\n", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2478 | __func__, __LINE__, ret); |
| 2479 | goto out_unmap; |
| 2480 | } |
| 2481 | |
| 2482 | out: |
| 2483 | return ret; |
| 2484 | out_unmap: |
| 2485 | srpt_unmap_sg_to_ib_sge(ch, ioctx); |
| 2486 | goto out; |
| 2487 | } |
| 2488 | |
| 2489 | static int srpt_write_pending_status(struct se_cmd *se_cmd) |
| 2490 | { |
| 2491 | struct srpt_send_ioctx *ioctx; |
| 2492 | |
| 2493 | ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd); |
| 2494 | return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA; |
| 2495 | } |
| 2496 | |
| 2497 | /* |
| 2498 | * srpt_write_pending() - Start data transfer from initiator to target (write). |
| 2499 | */ |
| 2500 | static int srpt_write_pending(struct se_cmd *se_cmd) |
| 2501 | { |
Bart Van Assche | fc3af58 | 2016-02-11 11:09:10 -0800 | [diff] [blame] | 2502 | struct srpt_send_ioctx *ioctx = |
| 2503 | container_of(se_cmd, struct srpt_send_ioctx, cmd); |
| 2504 | struct srpt_rdma_ch *ch = ioctx->ch; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2505 | enum srpt_command_state new_state; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2506 | |
| 2507 | new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA); |
| 2508 | WARN_ON(new_state == SRPT_STATE_DONE); |
Bart Van Assche | fc3af58 | 2016-02-11 11:09:10 -0800 | [diff] [blame] | 2509 | return srpt_xfer_data(ch, ioctx); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status) |
| 2513 | { |
| 2514 | switch (tcm_mgmt_status) { |
| 2515 | case TMR_FUNCTION_COMPLETE: |
| 2516 | return SRP_TSK_MGMT_SUCCESS; |
| 2517 | case TMR_FUNCTION_REJECTED: |
| 2518 | return SRP_TSK_MGMT_FUNC_NOT_SUPP; |
| 2519 | } |
| 2520 | return SRP_TSK_MGMT_FAILED; |
| 2521 | } |
| 2522 | |
| 2523 | /** |
| 2524 | * srpt_queue_response() - Transmits the response to a SCSI command. |
| 2525 | * |
| 2526 | * Callback function called by the TCM core. Must not block since it can be |
| 2527 | * invoked on the context of the IB completion handler. |
| 2528 | */ |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2529 | static void srpt_queue_response(struct se_cmd *cmd) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2530 | { |
| 2531 | struct srpt_rdma_ch *ch; |
| 2532 | struct srpt_send_ioctx *ioctx; |
| 2533 | enum srpt_command_state state; |
| 2534 | unsigned long flags; |
| 2535 | int ret; |
| 2536 | enum dma_data_direction dir; |
| 2537 | int resp_len; |
| 2538 | u8 srp_tm_status; |
| 2539 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2540 | ioctx = container_of(cmd, struct srpt_send_ioctx, cmd); |
| 2541 | ch = ioctx->ch; |
| 2542 | BUG_ON(!ch); |
| 2543 | |
| 2544 | spin_lock_irqsave(&ioctx->spinlock, flags); |
| 2545 | state = ioctx->state; |
| 2546 | switch (state) { |
| 2547 | case SRPT_STATE_NEW: |
| 2548 | case SRPT_STATE_DATA_IN: |
| 2549 | ioctx->state = SRPT_STATE_CMD_RSP_SENT; |
| 2550 | break; |
| 2551 | case SRPT_STATE_MGMT: |
| 2552 | ioctx->state = SRPT_STATE_MGMT_RSP_SENT; |
| 2553 | break; |
| 2554 | default: |
| 2555 | WARN(true, "ch %p; cmd %d: unexpected command state %d\n", |
| 2556 | ch, ioctx->ioctx.index, ioctx->state); |
| 2557 | break; |
| 2558 | } |
| 2559 | spin_unlock_irqrestore(&ioctx->spinlock, flags); |
| 2560 | |
| 2561 | if (unlikely(transport_check_aborted_status(&ioctx->cmd, false) |
| 2562 | || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) { |
| 2563 | atomic_inc(&ch->req_lim_delta); |
| 2564 | srpt_abort_cmd(ioctx); |
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 | dir = ioctx->cmd.data_direction; |
| 2569 | |
| 2570 | /* For read commands, transfer the data to the initiator. */ |
| 2571 | if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length && |
| 2572 | !ioctx->queue_status_only) { |
| 2573 | ret = srpt_xfer_data(ch, ioctx); |
| 2574 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2575 | pr_err("xfer_data failed for tag %llu\n", |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2576 | ioctx->cmd.tag); |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2577 | return; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | if (state != SRPT_STATE_MGMT) |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2582 | resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2583 | cmd->scsi_status); |
| 2584 | else { |
| 2585 | srp_tm_status |
| 2586 | = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response); |
| 2587 | resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status, |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2588 | ioctx->cmd.tag); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2589 | } |
| 2590 | ret = srpt_post_send(ch, ioctx, resp_len); |
| 2591 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2592 | pr_err("sending cmd response failed for tag %llu\n", |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2593 | ioctx->cmd.tag); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2594 | srpt_unmap_sg_to_ib_sge(ch, ioctx); |
| 2595 | srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 2596 | target_put_sess_cmd(&ioctx->cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2597 | } |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2598 | } |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2599 | |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2600 | static int srpt_queue_data_in(struct se_cmd *cmd) |
| 2601 | { |
| 2602 | srpt_queue_response(cmd); |
| 2603 | return 0; |
| 2604 | } |
| 2605 | |
| 2606 | static void srpt_queue_tm_rsp(struct se_cmd *cmd) |
| 2607 | { |
| 2608 | srpt_queue_response(cmd); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Nicholas Bellinger | 131e6ab | 2014-03-22 14:55:56 -0700 | [diff] [blame] | 2611 | static void srpt_aborted_task(struct se_cmd *cmd) |
| 2612 | { |
| 2613 | struct srpt_send_ioctx *ioctx = container_of(cmd, |
| 2614 | struct srpt_send_ioctx, cmd); |
| 2615 | |
| 2616 | srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx); |
| 2617 | } |
| 2618 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2619 | static int srpt_queue_status(struct se_cmd *cmd) |
| 2620 | { |
| 2621 | struct srpt_send_ioctx *ioctx; |
| 2622 | |
| 2623 | ioctx = container_of(cmd, struct srpt_send_ioctx, cmd); |
| 2624 | BUG_ON(ioctx->sense_data != cmd->sense_buffer); |
| 2625 | if (cmd->se_cmd_flags & |
| 2626 | (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE)) |
| 2627 | WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION); |
| 2628 | ioctx->queue_status_only = true; |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 2629 | srpt_queue_response(cmd); |
| 2630 | return 0; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | static void srpt_refresh_port_work(struct work_struct *work) |
| 2634 | { |
| 2635 | struct srpt_port *sport = container_of(work, struct srpt_port, work); |
| 2636 | |
| 2637 | srpt_refresh_port(sport); |
| 2638 | } |
| 2639 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2640 | /** |
| 2641 | * srpt_release_sdev() - Free the channel resources associated with a target. |
| 2642 | */ |
| 2643 | static int srpt_release_sdev(struct srpt_device *sdev) |
| 2644 | { |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2645 | int i, res; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2646 | |
| 2647 | WARN_ON_ONCE(irqs_disabled()); |
| 2648 | |
| 2649 | BUG_ON(!sdev); |
| 2650 | |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2651 | mutex_lock(&sdev->mutex); |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2652 | for (i = 0; i < ARRAY_SIZE(sdev->port); i++) |
| 2653 | sdev->port[i].enabled = false; |
| 2654 | __srpt_close_all_ch(sdev); |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2655 | mutex_unlock(&sdev->mutex); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2656 | |
| 2657 | res = wait_event_interruptible(sdev->ch_releaseQ, |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2658 | list_empty_careful(&sdev->rch_list)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2659 | if (res) |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2660 | pr_err("%s: interrupted.\n", __func__); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2661 | |
| 2662 | return 0; |
| 2663 | } |
| 2664 | |
| 2665 | static struct srpt_port *__srpt_lookup_port(const char *name) |
| 2666 | { |
| 2667 | struct ib_device *dev; |
| 2668 | struct srpt_device *sdev; |
| 2669 | struct srpt_port *sport; |
| 2670 | int i; |
| 2671 | |
| 2672 | list_for_each_entry(sdev, &srpt_dev_list, list) { |
| 2673 | dev = sdev->device; |
| 2674 | if (!dev) |
| 2675 | continue; |
| 2676 | |
| 2677 | for (i = 0; i < dev->phys_port_cnt; i++) { |
| 2678 | sport = &sdev->port[i]; |
| 2679 | |
| 2680 | if (!strcmp(sport->port_guid, name)) |
| 2681 | return sport; |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | return NULL; |
| 2686 | } |
| 2687 | |
| 2688 | static struct srpt_port *srpt_lookup_port(const char *name) |
| 2689 | { |
| 2690 | struct srpt_port *sport; |
| 2691 | |
| 2692 | spin_lock(&srpt_dev_lock); |
| 2693 | sport = __srpt_lookup_port(name); |
| 2694 | spin_unlock(&srpt_dev_lock); |
| 2695 | |
| 2696 | return sport; |
| 2697 | } |
| 2698 | |
| 2699 | /** |
| 2700 | * srpt_add_one() - Infiniband device addition callback function. |
| 2701 | */ |
| 2702 | static void srpt_add_one(struct ib_device *device) |
| 2703 | { |
| 2704 | struct srpt_device *sdev; |
| 2705 | struct srpt_port *sport; |
| 2706 | struct ib_srq_init_attr srq_attr; |
| 2707 | int i; |
| 2708 | |
| 2709 | pr_debug("device = %p, device->dma_ops = %p\n", device, |
| 2710 | device->dma_ops); |
| 2711 | |
Bart Van Assche | 9d2aa2b | 2016-02-11 11:03:31 -0800 | [diff] [blame] | 2712 | sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2713 | if (!sdev) |
| 2714 | goto err; |
| 2715 | |
| 2716 | sdev->device = device; |
| 2717 | INIT_LIST_HEAD(&sdev->rch_list); |
| 2718 | init_waitqueue_head(&sdev->ch_releaseQ); |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2719 | mutex_init(&sdev->mutex); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2720 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2721 | sdev->pd = ib_alloc_pd(device); |
| 2722 | if (IS_ERR(sdev->pd)) |
| 2723 | goto free_dev; |
| 2724 | |
Or Gerlitz | 4a061b2 | 2015-12-18 10:59:46 +0200 | [diff] [blame] | 2725 | 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] | 2726 | |
| 2727 | srq_attr.event_handler = srpt_srq_event; |
| 2728 | srq_attr.srq_context = (void *)sdev; |
| 2729 | srq_attr.attr.max_wr = sdev->srq_size; |
| 2730 | srq_attr.attr.max_sge = 1; |
| 2731 | srq_attr.attr.srq_limit = 0; |
Roland Dreier | 6f36033 | 2012-04-12 07:51:08 -0700 | [diff] [blame] | 2732 | srq_attr.srq_type = IB_SRQT_BASIC; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2733 | |
| 2734 | sdev->srq = ib_create_srq(sdev->pd, &srq_attr); |
| 2735 | if (IS_ERR(sdev->srq)) |
Jason Gunthorpe | 5a78395 | 2015-07-30 17:22:24 -0600 | [diff] [blame] | 2736 | goto err_pd; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2737 | |
| 2738 | 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] | 2739 | __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2740 | device->name); |
| 2741 | |
| 2742 | if (!srpt_service_guid) |
| 2743 | srpt_service_guid = be64_to_cpu(device->node_guid); |
| 2744 | |
| 2745 | sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev); |
| 2746 | if (IS_ERR(sdev->cm_id)) |
| 2747 | goto err_srq; |
| 2748 | |
| 2749 | /* print out target login information */ |
| 2750 | pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx," |
| 2751 | "pkey=ffff,service_id=%016llx\n", srpt_service_guid, |
| 2752 | srpt_service_guid, srpt_service_guid); |
| 2753 | |
| 2754 | /* |
| 2755 | * We do not have a consistent service_id (ie. also id_ext of target_id) |
| 2756 | * to identify this target. We currently use the guid of the first HCA |
| 2757 | * in the system as service_id; therefore, the target_id will change |
| 2758 | * if this HCA is gone bad and replaced by different HCA |
| 2759 | */ |
Haggai Eran | 73fec7f | 2015-07-30 17:50:26 +0300 | [diff] [blame] | 2760 | 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] | 2761 | goto err_cm; |
| 2762 | |
| 2763 | INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device, |
| 2764 | srpt_event_handler); |
| 2765 | if (ib_register_event_handler(&sdev->event_handler)) |
| 2766 | goto err_cm; |
| 2767 | |
| 2768 | sdev->ioctx_ring = (struct srpt_recv_ioctx **) |
| 2769 | srpt_alloc_ioctx_ring(sdev, sdev->srq_size, |
| 2770 | sizeof(*sdev->ioctx_ring[0]), |
| 2771 | srp_max_req_size, DMA_FROM_DEVICE); |
| 2772 | if (!sdev->ioctx_ring) |
| 2773 | goto err_event; |
| 2774 | |
| 2775 | for (i = 0; i < sdev->srq_size; ++i) |
| 2776 | srpt_post_recv(sdev, sdev->ioctx_ring[i]); |
| 2777 | |
Roland Dreier | f225066 | 2012-02-02 12:55:58 -0800 | [diff] [blame] | 2778 | WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port)); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2779 | |
| 2780 | for (i = 1; i <= sdev->device->phys_port_cnt; i++) { |
| 2781 | sport = &sdev->port[i - 1]; |
| 2782 | sport->sdev = sdev; |
| 2783 | sport->port = i; |
| 2784 | sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE; |
| 2785 | sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE; |
| 2786 | sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE; |
| 2787 | INIT_WORK(&sport->work, srpt_refresh_port_work); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2788 | |
| 2789 | if (srpt_refresh_port(sport)) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2790 | pr_err("MAD registration failed for %s-%d.\n", |
Bart Van Assche | f68cba4e9 | 2016-02-11 11:04:20 -0800 | [diff] [blame] | 2791 | sdev->device->name, i); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2792 | goto err_ring; |
| 2793 | } |
| 2794 | snprintf(sport->port_guid, sizeof(sport->port_guid), |
| 2795 | "0x%016llx%016llx", |
| 2796 | be64_to_cpu(sport->gid.global.subnet_prefix), |
| 2797 | be64_to_cpu(sport->gid.global.interface_id)); |
| 2798 | } |
| 2799 | |
| 2800 | spin_lock(&srpt_dev_lock); |
| 2801 | list_add_tail(&sdev->list, &srpt_dev_list); |
| 2802 | spin_unlock(&srpt_dev_lock); |
| 2803 | |
| 2804 | out: |
| 2805 | ib_set_client_data(device, &srpt_client, sdev); |
| 2806 | pr_debug("added %s.\n", device->name); |
| 2807 | return; |
| 2808 | |
| 2809 | err_ring: |
| 2810 | srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev, |
| 2811 | sdev->srq_size, srp_max_req_size, |
| 2812 | DMA_FROM_DEVICE); |
| 2813 | err_event: |
| 2814 | ib_unregister_event_handler(&sdev->event_handler); |
| 2815 | err_cm: |
| 2816 | ib_destroy_cm_id(sdev->cm_id); |
| 2817 | err_srq: |
| 2818 | ib_destroy_srq(sdev->srq); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2819 | err_pd: |
| 2820 | ib_dealloc_pd(sdev->pd); |
| 2821 | free_dev: |
| 2822 | kfree(sdev); |
| 2823 | err: |
| 2824 | sdev = NULL; |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2825 | pr_info("%s(%s) failed.\n", __func__, device->name); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2826 | goto out; |
| 2827 | } |
| 2828 | |
| 2829 | /** |
| 2830 | * srpt_remove_one() - InfiniBand device removal callback function. |
| 2831 | */ |
Haggai Eran | 7c1eb45 | 2015-07-30 17:50:14 +0300 | [diff] [blame] | 2832 | 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] | 2833 | { |
Haggai Eran | 7c1eb45 | 2015-07-30 17:50:14 +0300 | [diff] [blame] | 2834 | struct srpt_device *sdev = client_data; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2835 | int i; |
| 2836 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2837 | if (!sdev) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 2838 | pr_info("%s(%s): nothing to do.\n", __func__, device->name); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2839 | return; |
| 2840 | } |
| 2841 | |
| 2842 | srpt_unregister_mad_agent(sdev); |
| 2843 | |
| 2844 | ib_unregister_event_handler(&sdev->event_handler); |
| 2845 | |
| 2846 | /* Cancel any work queued by the just unregistered IB event handler. */ |
| 2847 | for (i = 0; i < sdev->device->phys_port_cnt; i++) |
| 2848 | cancel_work_sync(&sdev->port[i].work); |
| 2849 | |
| 2850 | ib_destroy_cm_id(sdev->cm_id); |
| 2851 | |
| 2852 | /* |
| 2853 | * Unregistering a target must happen after destroying sdev->cm_id |
| 2854 | * such that no new SRP_LOGIN_REQ information units can arrive while |
| 2855 | * destroying the target. |
| 2856 | */ |
| 2857 | spin_lock(&srpt_dev_lock); |
| 2858 | list_del(&sdev->list); |
| 2859 | spin_unlock(&srpt_dev_lock); |
| 2860 | srpt_release_sdev(sdev); |
| 2861 | |
| 2862 | ib_destroy_srq(sdev->srq); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2863 | ib_dealloc_pd(sdev->pd); |
| 2864 | |
| 2865 | srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev, |
| 2866 | sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE); |
| 2867 | sdev->ioctx_ring = NULL; |
| 2868 | kfree(sdev); |
| 2869 | } |
| 2870 | |
| 2871 | static struct ib_client srpt_client = { |
| 2872 | .name = DRV_NAME, |
| 2873 | .add = srpt_add_one, |
| 2874 | .remove = srpt_remove_one |
| 2875 | }; |
| 2876 | |
| 2877 | static int srpt_check_true(struct se_portal_group *se_tpg) |
| 2878 | { |
| 2879 | return 1; |
| 2880 | } |
| 2881 | |
| 2882 | static int srpt_check_false(struct se_portal_group *se_tpg) |
| 2883 | { |
| 2884 | return 0; |
| 2885 | } |
| 2886 | |
| 2887 | static char *srpt_get_fabric_name(void) |
| 2888 | { |
| 2889 | return "srpt"; |
| 2890 | } |
| 2891 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2892 | static char *srpt_get_fabric_wwn(struct se_portal_group *tpg) |
| 2893 | { |
| 2894 | struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1); |
| 2895 | |
| 2896 | return sport->port_guid; |
| 2897 | } |
| 2898 | |
| 2899 | static u16 srpt_get_tag(struct se_portal_group *tpg) |
| 2900 | { |
| 2901 | return 1; |
| 2902 | } |
| 2903 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2904 | static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg) |
| 2905 | { |
| 2906 | return 1; |
| 2907 | } |
| 2908 | |
| 2909 | static void srpt_release_cmd(struct se_cmd *se_cmd) |
| 2910 | { |
Nicholas Bellinger | 9474b04 | 2012-11-27 23:55:57 -0800 | [diff] [blame] | 2911 | struct srpt_send_ioctx *ioctx = container_of(se_cmd, |
| 2912 | struct srpt_send_ioctx, cmd); |
| 2913 | struct srpt_rdma_ch *ch = ioctx->ch; |
| 2914 | unsigned long flags; |
| 2915 | |
| 2916 | WARN_ON(ioctx->state != SRPT_STATE_DONE); |
| 2917 | WARN_ON(ioctx->mapped_sg_count != 0); |
| 2918 | |
| 2919 | if (ioctx->n_rbuf > 1) { |
| 2920 | kfree(ioctx->rbufs); |
| 2921 | ioctx->rbufs = NULL; |
| 2922 | ioctx->n_rbuf = 0; |
| 2923 | } |
| 2924 | |
| 2925 | spin_lock_irqsave(&ch->spinlock, flags); |
| 2926 | list_add(&ioctx->free_list, &ch->free_list); |
| 2927 | spin_unlock_irqrestore(&ch->spinlock, flags); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2931 | * srpt_close_session() - Forcibly close a session. |
| 2932 | * |
| 2933 | * Callback function invoked by the TCM core to clean up sessions associated |
| 2934 | * with a node ACL when the user invokes |
| 2935 | * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id |
| 2936 | */ |
| 2937 | static void srpt_close_session(struct se_session *se_sess) |
| 2938 | { |
| 2939 | DECLARE_COMPLETION_ONSTACK(release_done); |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 2940 | struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr; |
| 2941 | struct srpt_device *sdev = ch->sport->sdev; |
| 2942 | bool wait; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2943 | |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 2944 | pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num, |
| 2945 | ch->state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2946 | |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2947 | mutex_lock(&sdev->mutex); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2948 | BUG_ON(ch->release_done); |
| 2949 | ch->release_done = &release_done; |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 2950 | wait = !list_empty(&ch->list); |
Bart Van Assche | aaf45bd | 2016-02-11 11:08:53 -0800 | [diff] [blame] | 2951 | srpt_disconnect_ch(ch); |
Bart Van Assche | 8628991 | 2016-02-11 11:08:34 -0800 | [diff] [blame] | 2952 | mutex_unlock(&sdev->mutex); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2953 | |
Bart Van Assche | f108f0f | 2016-02-11 11:06:14 -0800 | [diff] [blame] | 2954 | if (!wait) |
| 2955 | return; |
| 2956 | |
| 2957 | while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0) |
| 2958 | pr_info("%s(%s-%d state %d): still waiting ...\n", __func__, |
| 2959 | ch->sess_name, ch->qp->qp_num, ch->state); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | /** |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2963 | * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB). |
| 2964 | * |
| 2965 | * A quote from RFC 4455 (SCSI-MIB) about this MIB object: |
| 2966 | * This object represents an arbitrary integer used to uniquely identify a |
| 2967 | * particular attached remote initiator port to a particular SCSI target port |
| 2968 | * within a particular SCSI target device within a particular SCSI instance. |
| 2969 | */ |
| 2970 | static u32 srpt_sess_get_index(struct se_session *se_sess) |
| 2971 | { |
| 2972 | return 0; |
| 2973 | } |
| 2974 | |
| 2975 | static void srpt_set_default_node_attrs(struct se_node_acl *nacl) |
| 2976 | { |
| 2977 | } |
| 2978 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2979 | /* Note: only used from inside debug printk's by the TCM core. */ |
| 2980 | static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd) |
| 2981 | { |
| 2982 | struct srpt_send_ioctx *ioctx; |
| 2983 | |
| 2984 | ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd); |
| 2985 | return srpt_get_cmd_state(ioctx); |
| 2986 | } |
| 2987 | |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 2988 | /** |
| 2989 | * srpt_parse_i_port_id() - Parse an initiator port ID. |
| 2990 | * @name: ASCII representation of a 128-bit initiator port ID. |
| 2991 | * @i_port_id: Binary 128-bit port ID. |
| 2992 | */ |
| 2993 | static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name) |
| 2994 | { |
| 2995 | const char *p; |
| 2996 | unsigned len, count, leading_zero_bytes; |
| 2997 | int ret, rc; |
| 2998 | |
| 2999 | p = name; |
Rasmus Villemoes | b60459f | 2014-10-13 15:54:46 -0700 | [diff] [blame] | 3000 | if (strncasecmp(p, "0x", 2) == 0) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3001 | p += 2; |
| 3002 | ret = -EINVAL; |
| 3003 | len = strlen(p); |
| 3004 | if (len % 2) |
| 3005 | goto out; |
| 3006 | count = min(len / 2, 16U); |
| 3007 | leading_zero_bytes = 16 - count; |
| 3008 | memset(i_port_id, 0, leading_zero_bytes); |
| 3009 | rc = hex2bin(i_port_id + leading_zero_bytes, p, count); |
| 3010 | if (rc < 0) |
| 3011 | pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc); |
| 3012 | ret = 0; |
| 3013 | out: |
| 3014 | return ret; |
| 3015 | } |
| 3016 | |
| 3017 | /* |
| 3018 | * configfs callback function invoked for |
| 3019 | * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id |
| 3020 | */ |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 3021 | 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] | 3022 | { |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3023 | u8 i_port_id[16]; |
| 3024 | |
| 3025 | if (srpt_parse_i_port_id(i_port_id, name) < 0) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3026 | pr_err("invalid initiator port ID %s\n", name); |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 3027 | return -EINVAL; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3028 | } |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 3029 | return 0; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3032 | static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item, |
| 3033 | char *page) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3034 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3035 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3036 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3037 | |
| 3038 | return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size); |
| 3039 | } |
| 3040 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3041 | static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item, |
| 3042 | const char *page, size_t count) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3043 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3044 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3045 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3046 | unsigned long val; |
| 3047 | int ret; |
| 3048 | |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3049 | ret = kstrtoul(page, 0, &val); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3050 | if (ret < 0) { |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3051 | pr_err("kstrtoul() failed with ret: %d\n", ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3052 | return -EINVAL; |
| 3053 | } |
| 3054 | if (val > MAX_SRPT_RDMA_SIZE) { |
| 3055 | pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val, |
| 3056 | MAX_SRPT_RDMA_SIZE); |
| 3057 | return -EINVAL; |
| 3058 | } |
| 3059 | if (val < DEFAULT_MAX_RDMA_SIZE) { |
| 3060 | pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n", |
| 3061 | val, DEFAULT_MAX_RDMA_SIZE); |
| 3062 | return -EINVAL; |
| 3063 | } |
| 3064 | sport->port_attrib.srp_max_rdma_size = val; |
| 3065 | |
| 3066 | return count; |
| 3067 | } |
| 3068 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3069 | static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item, |
| 3070 | char *page) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3071 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3072 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3073 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3074 | |
| 3075 | return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size); |
| 3076 | } |
| 3077 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3078 | static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item, |
| 3079 | const char *page, size_t count) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3080 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3081 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3082 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3083 | unsigned long val; |
| 3084 | int ret; |
| 3085 | |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3086 | ret = kstrtoul(page, 0, &val); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3087 | if (ret < 0) { |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3088 | pr_err("kstrtoul() failed with ret: %d\n", ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3089 | return -EINVAL; |
| 3090 | } |
| 3091 | if (val > MAX_SRPT_RSP_SIZE) { |
| 3092 | pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val, |
| 3093 | MAX_SRPT_RSP_SIZE); |
| 3094 | return -EINVAL; |
| 3095 | } |
| 3096 | if (val < MIN_MAX_RSP_SIZE) { |
| 3097 | pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val, |
| 3098 | MIN_MAX_RSP_SIZE); |
| 3099 | return -EINVAL; |
| 3100 | } |
| 3101 | sport->port_attrib.srp_max_rsp_size = val; |
| 3102 | |
| 3103 | return count; |
| 3104 | } |
| 3105 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3106 | static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item, |
| 3107 | char *page) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3108 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3109 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3110 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3111 | |
| 3112 | return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size); |
| 3113 | } |
| 3114 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3115 | static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item, |
| 3116 | const char *page, size_t count) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3117 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3118 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3119 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3120 | unsigned long val; |
| 3121 | int ret; |
| 3122 | |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3123 | ret = kstrtoul(page, 0, &val); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3124 | if (ret < 0) { |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3125 | pr_err("kstrtoul() failed with ret: %d\n", ret); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3126 | return -EINVAL; |
| 3127 | } |
| 3128 | if (val > MAX_SRPT_SRQ_SIZE) { |
| 3129 | pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val, |
| 3130 | MAX_SRPT_SRQ_SIZE); |
| 3131 | return -EINVAL; |
| 3132 | } |
| 3133 | if (val < MIN_SRPT_SRQ_SIZE) { |
| 3134 | pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val, |
| 3135 | MIN_SRPT_SRQ_SIZE); |
| 3136 | return -EINVAL; |
| 3137 | } |
| 3138 | sport->port_attrib.srp_sq_size = val; |
| 3139 | |
| 3140 | return count; |
| 3141 | } |
| 3142 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3143 | CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size); |
| 3144 | CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size); |
| 3145 | CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3146 | |
| 3147 | static struct configfs_attribute *srpt_tpg_attrib_attrs[] = { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3148 | &srpt_tpg_attrib_attr_srp_max_rdma_size, |
| 3149 | &srpt_tpg_attrib_attr_srp_max_rsp_size, |
| 3150 | &srpt_tpg_attrib_attr_srp_sq_size, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3151 | NULL, |
| 3152 | }; |
| 3153 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3154 | 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] | 3155 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3156 | struct se_portal_group *se_tpg = to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3157 | struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1); |
| 3158 | |
| 3159 | return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0); |
| 3160 | } |
| 3161 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3162 | static ssize_t srpt_tpg_enable_store(struct config_item *item, |
| 3163 | const char *page, size_t count) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3164 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3165 | struct se_portal_group *se_tpg = to_tpg(item); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3166 | 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] | 3167 | struct srpt_device *sdev = sport->sdev; |
| 3168 | struct srpt_rdma_ch *ch; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3169 | unsigned long tmp; |
| 3170 | int ret; |
| 3171 | |
Jingoo Han | 9d8abf4 | 2014-02-05 11:22:05 +0900 | [diff] [blame] | 3172 | ret = kstrtoul(page, 0, &tmp); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3173 | if (ret < 0) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3174 | pr_err("Unable to extract srpt_tpg_store_enable\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3175 | return -EINVAL; |
| 3176 | } |
| 3177 | |
| 3178 | if ((tmp != 0) && (tmp != 1)) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3179 | 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] | 3180 | return -EINVAL; |
| 3181 | } |
Bart Van Assche | 043a680 | 2016-02-11 11:09:28 -0800 | [diff] [blame] | 3182 | if (sport->enabled == tmp) |
| 3183 | goto out; |
| 3184 | sport->enabled = tmp; |
| 3185 | if (sport->enabled) |
| 3186 | goto out; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3187 | |
Bart Van Assche | 043a680 | 2016-02-11 11:09:28 -0800 | [diff] [blame] | 3188 | mutex_lock(&sdev->mutex); |
| 3189 | list_for_each_entry(ch, &sdev->rch_list, list) { |
| 3190 | if (ch->sport == sport) { |
| 3191 | pr_debug("%s: ch %p %s-%d\n", __func__, ch, |
| 3192 | ch->sess_name, ch->qp->qp_num); |
| 3193 | srpt_disconnect_ch(ch); |
| 3194 | srpt_close_ch(ch); |
| 3195 | } |
| 3196 | } |
| 3197 | mutex_unlock(&sdev->mutex); |
| 3198 | |
| 3199 | out: |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3200 | return count; |
| 3201 | } |
| 3202 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3203 | CONFIGFS_ATTR(srpt_tpg_, enable); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3204 | |
| 3205 | static struct configfs_attribute *srpt_tpg_attrs[] = { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3206 | &srpt_tpg_attr_enable, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3207 | NULL, |
| 3208 | }; |
| 3209 | |
| 3210 | /** |
| 3211 | * configfs callback invoked for |
| 3212 | * mkdir /sys/kernel/config/target/$driver/$port/$tpg |
| 3213 | */ |
| 3214 | static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn, |
| 3215 | struct config_group *group, |
| 3216 | const char *name) |
| 3217 | { |
| 3218 | struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn); |
| 3219 | int res; |
| 3220 | |
| 3221 | /* Initialize sport->port_wwn and sport->port_tpg_1 */ |
Nicholas Bellinger | bc0c94b | 2015-05-20 21:48:03 -0700 | [diff] [blame] | 3222 | 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] | 3223 | if (res) |
| 3224 | return ERR_PTR(res); |
| 3225 | |
| 3226 | return &sport->port_tpg_1; |
| 3227 | } |
| 3228 | |
| 3229 | /** |
| 3230 | * configfs callback invoked for |
| 3231 | * rmdir /sys/kernel/config/target/$driver/$port/$tpg |
| 3232 | */ |
| 3233 | static void srpt_drop_tpg(struct se_portal_group *tpg) |
| 3234 | { |
| 3235 | struct srpt_port *sport = container_of(tpg, |
| 3236 | struct srpt_port, port_tpg_1); |
| 3237 | |
| 3238 | sport->enabled = false; |
| 3239 | core_tpg_deregister(&sport->port_tpg_1); |
| 3240 | } |
| 3241 | |
| 3242 | /** |
| 3243 | * configfs callback invoked for |
| 3244 | * mkdir /sys/kernel/config/target/$driver/$port |
| 3245 | */ |
| 3246 | static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf, |
| 3247 | struct config_group *group, |
| 3248 | const char *name) |
| 3249 | { |
| 3250 | struct srpt_port *sport; |
| 3251 | int ret; |
| 3252 | |
| 3253 | sport = srpt_lookup_port(name); |
| 3254 | pr_debug("make_tport(%s)\n", name); |
| 3255 | ret = -EINVAL; |
| 3256 | if (!sport) |
| 3257 | goto err; |
| 3258 | |
| 3259 | return &sport->port_wwn; |
| 3260 | |
| 3261 | err: |
| 3262 | return ERR_PTR(ret); |
| 3263 | } |
| 3264 | |
| 3265 | /** |
| 3266 | * configfs callback invoked for |
| 3267 | * rmdir /sys/kernel/config/target/$driver/$port |
| 3268 | */ |
| 3269 | static void srpt_drop_tport(struct se_wwn *wwn) |
| 3270 | { |
| 3271 | struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn); |
| 3272 | |
| 3273 | pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item)); |
| 3274 | } |
| 3275 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3276 | 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] | 3277 | { |
| 3278 | return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION); |
| 3279 | } |
| 3280 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3281 | CONFIGFS_ATTR_RO(srpt_wwn_, version); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3282 | |
| 3283 | static struct configfs_attribute *srpt_wwn_attrs[] = { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 3284 | &srpt_wwn_attr_version, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3285 | NULL, |
| 3286 | }; |
| 3287 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3288 | static const struct target_core_fabric_ops srpt_template = { |
| 3289 | .module = THIS_MODULE, |
| 3290 | .name = "srpt", |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3291 | .get_fabric_name = srpt_get_fabric_name, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3292 | .tpg_get_wwn = srpt_get_fabric_wwn, |
| 3293 | .tpg_get_tag = srpt_get_tag, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3294 | .tpg_check_demo_mode = srpt_check_false, |
| 3295 | .tpg_check_demo_mode_cache = srpt_check_true, |
| 3296 | .tpg_check_demo_mode_write_protect = srpt_check_true, |
| 3297 | .tpg_check_prod_mode_write_protect = srpt_check_false, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3298 | .tpg_get_inst_index = srpt_tpg_get_inst_index, |
| 3299 | .release_cmd = srpt_release_cmd, |
| 3300 | .check_stop_free = srpt_check_stop_free, |
| 3301 | .shutdown_session = srpt_shutdown_session, |
| 3302 | .close_session = srpt_close_session, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3303 | .sess_get_index = srpt_sess_get_index, |
| 3304 | .sess_get_initiator_sid = NULL, |
| 3305 | .write_pending = srpt_write_pending, |
| 3306 | .write_pending_status = srpt_write_pending_status, |
| 3307 | .set_default_node_attributes = srpt_set_default_node_attrs, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3308 | .get_cmd_state = srpt_get_tcm_cmd_state, |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 3309 | .queue_data_in = srpt_queue_data_in, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3310 | .queue_status = srpt_queue_status, |
Joern Engel | b79fafa | 2013-07-03 11:22:17 -0400 | [diff] [blame] | 3311 | .queue_tm_rsp = srpt_queue_tm_rsp, |
Nicholas Bellinger | 131e6ab | 2014-03-22 14:55:56 -0700 | [diff] [blame] | 3312 | .aborted_task = srpt_aborted_task, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3313 | /* |
| 3314 | * Setup function pointers for generic logic in |
| 3315 | * target_core_fabric_configfs.c |
| 3316 | */ |
| 3317 | .fabric_make_wwn = srpt_make_tport, |
| 3318 | .fabric_drop_wwn = srpt_drop_tport, |
| 3319 | .fabric_make_tpg = srpt_make_tpg, |
| 3320 | .fabric_drop_tpg = srpt_drop_tpg, |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 3321 | .fabric_init_nodeacl = srpt_init_nodeacl, |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3322 | |
| 3323 | .tfc_wwn_attrs = srpt_wwn_attrs, |
| 3324 | .tfc_tpg_base_attrs = srpt_tpg_attrs, |
| 3325 | .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs, |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3326 | }; |
| 3327 | |
| 3328 | /** |
| 3329 | * srpt_init_module() - Kernel module initialization. |
| 3330 | * |
| 3331 | * Note: Since ib_register_client() registers callback functions, and since at |
| 3332 | * least one of these callback functions (srpt_add_one()) calls target core |
| 3333 | * functions, this driver must be registered with the target core before |
| 3334 | * ib_register_client() is called. |
| 3335 | */ |
| 3336 | static int __init srpt_init_module(void) |
| 3337 | { |
| 3338 | int ret; |
| 3339 | |
| 3340 | ret = -EINVAL; |
| 3341 | if (srp_max_req_size < MIN_MAX_REQ_SIZE) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3342 | pr_err("invalid value %d for kernel module parameter" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3343 | " srp_max_req_size -- must be at least %d.\n", |
| 3344 | srp_max_req_size, MIN_MAX_REQ_SIZE); |
| 3345 | goto out; |
| 3346 | } |
| 3347 | |
| 3348 | if (srpt_srq_size < MIN_SRPT_SRQ_SIZE |
| 3349 | || srpt_srq_size > MAX_SRPT_SRQ_SIZE) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3350 | pr_err("invalid value %d for kernel module parameter" |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3351 | " srpt_srq_size -- must be in the range [%d..%d].\n", |
| 3352 | srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE); |
| 3353 | goto out; |
| 3354 | } |
| 3355 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3356 | ret = target_register_template(&srpt_template); |
| 3357 | if (ret) |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3358 | goto out; |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3359 | |
| 3360 | ret = ib_register_client(&srpt_client); |
| 3361 | if (ret) { |
Doug Ledford | 9f5d32a | 2014-10-20 18:25:15 -0400 | [diff] [blame] | 3362 | pr_err("couldn't register IB client\n"); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3363 | goto out_unregister_target; |
| 3364 | } |
| 3365 | |
| 3366 | return 0; |
| 3367 | |
| 3368 | out_unregister_target: |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3369 | target_unregister_template(&srpt_template); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3370 | out: |
| 3371 | return ret; |
| 3372 | } |
| 3373 | |
| 3374 | static void __exit srpt_cleanup_module(void) |
| 3375 | { |
| 3376 | ib_unregister_client(&srpt_client); |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 3377 | target_unregister_template(&srpt_template); |
Bart Van Assche | a42d985 | 2011-10-14 01:30:46 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | module_init(srpt_init_module); |
| 3381 | module_exit(srpt_cleanup_module); |