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